thinkpad_acpi: Convert printks to pr_<level>
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob77f6e707a2a9a43d4c08777f1a97e42dd49a11a0
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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.24"
27 #define TPACPI_SYSFS_VERSION 0x020700
30 * Changelog:
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
65 #include <linux/nvram.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/sysfs.h>
69 #include <linux/backlight.h>
70 #include <linux/fb.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <asm/uaccess.h>
79 #include <linux/dmi.h>
80 #include <linux/jiffies.h>
81 #include <linux/workqueue.h>
83 #include <sound/core.h>
84 #include <sound/control.h>
85 #include <sound/initval.h>
87 #include <acpi/acpi_drivers.h>
89 #include <linux/pci_ids.h>
92 /* ThinkPad CMOS commands */
93 #define TP_CMOS_VOLUME_DOWN 0
94 #define TP_CMOS_VOLUME_UP 1
95 #define TP_CMOS_VOLUME_MUTE 2
96 #define TP_CMOS_BRIGHTNESS_UP 4
97 #define TP_CMOS_BRIGHTNESS_DOWN 5
98 #define TP_CMOS_THINKLIGHT_ON 12
99 #define TP_CMOS_THINKLIGHT_OFF 13
101 /* NVRAM Addresses */
102 enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
110 /* NVRAM bit masks */
111 enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
127 /* Misc NVRAM-related */
128 enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
132 /* ACPI HIDs */
133 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
137 /* Input IDs */
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
142 enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
150 enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
163 /* HKEY events */
164 enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
197 /* Thermal events */
198 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
199 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
200 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
201 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
202 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
204 /* Misc */
205 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
208 /****************************************************************************
209 * Main driver
212 #define TPACPI_NAME "thinkpad"
213 #define TPACPI_DESC "ThinkPad ACPI Extras"
214 #define TPACPI_FILE TPACPI_NAME "_acpi"
215 #define TPACPI_URL "http://ibm-acpi.sf.net/"
216 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
218 #define TPACPI_PROC_DIR "ibm"
219 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
220 #define TPACPI_DRVR_NAME TPACPI_FILE
221 #define TPACPI_DRVR_SHORTNAME "tpacpi"
222 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
224 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
225 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
227 #define TPACPI_MAX_ACPI_ARGS 3
229 /* Debugging printk groups */
230 #define TPACPI_DBG_ALL 0xffff
231 #define TPACPI_DBG_DISCLOSETASK 0x8000
232 #define TPACPI_DBG_INIT 0x0001
233 #define TPACPI_DBG_EXIT 0x0002
234 #define TPACPI_DBG_RFKILL 0x0004
235 #define TPACPI_DBG_HKEY 0x0008
236 #define TPACPI_DBG_FAN 0x0010
237 #define TPACPI_DBG_BRGHT 0x0020
238 #define TPACPI_DBG_MIXER 0x0040
240 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
241 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
242 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
245 /****************************************************************************
246 * Driver-wide structs and misc. variables
249 struct ibm_struct;
251 struct tp_acpi_drv_struct {
252 const struct acpi_device_id *hid;
253 struct acpi_driver *driver;
255 void (*notify) (struct ibm_struct *, u32);
256 acpi_handle *handle;
257 u32 type;
258 struct acpi_device *device;
261 struct ibm_struct {
262 char *name;
264 int (*read) (struct seq_file *);
265 int (*write) (char *);
266 void (*exit) (void);
267 void (*resume) (void);
268 void (*suspend) (pm_message_t state);
269 void (*shutdown) (void);
271 struct list_head all_drivers;
273 struct tp_acpi_drv_struct *acpi;
275 struct {
276 u8 acpi_driver_registered:1;
277 u8 acpi_notify_installed:1;
278 u8 proc_created:1;
279 u8 init_called:1;
280 u8 experimental:1;
281 } flags;
284 struct ibm_init_struct {
285 char param[32];
287 int (*init) (struct ibm_init_struct *);
288 mode_t base_procfs_mode;
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_acpimode:1;
301 u32 bright_unkfw: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 /* brightness level capabilities */
365 static unsigned int bright_maxlvl; /* 0 = unknown */
367 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
368 static int dbg_wlswemul;
369 static int tpacpi_wlsw_emulstate;
370 static int dbg_bluetoothemul;
371 static int tpacpi_bluetooth_emulstate;
372 static int dbg_wwanemul;
373 static int tpacpi_wwan_emulstate;
374 static int dbg_uwbemul;
375 static int tpacpi_uwb_emulstate;
376 #endif
379 /*************************************************************************
380 * Debugging helpers
383 #define dbg_printk(a_dbg_level, format, arg...) \
384 do { \
385 if (dbg_level & (a_dbg_level)) \
386 printk(KERN_DEBUG pr_fmt("%s: " format), \
387 __func__, ##arg); \
388 } while (0)
390 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
391 #define vdbg_printk dbg_printk
392 static const char *str_supported(int is_supported);
393 #else
394 static inline const char *str_supported(int is_supported) { return ""; }
395 #define vdbg_printk(a_dbg_level, format, arg...) \
396 no_printk(format, ##arg)
397 #endif
399 static void tpacpi_log_usertask(const char * const what)
401 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
402 what, task_tgid_vnr(current));
405 #define tpacpi_disclose_usertask(what, format, arg...) \
406 do { \
407 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
408 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
409 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
410 what, task_tgid_vnr(current), ## arg); \
412 } while (0)
415 * Quirk handling helpers
417 * ThinkPad IDs and versions seen in the field so far
418 * are two-characters from the set [0-9A-Z], i.e. base 36.
420 * We use values well outside that range as specials.
423 #define TPACPI_MATCH_ANY 0xffffU
424 #define TPACPI_MATCH_UNKNOWN 0U
426 /* TPID('1', 'Y') == 0x5931 */
427 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
429 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
430 { .vendor = PCI_VENDOR_ID_IBM, \
431 .bios = TPID(__id1, __id2), \
432 .ec = TPACPI_MATCH_ANY, \
433 .quirks = (__quirk) }
435 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_LENOVO, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPACPI_MATCH_ANY, \
444 .ec = TPID(__id1, __id2), \
445 .quirks = (__quirk) }
447 struct tpacpi_quirk {
448 unsigned int vendor;
449 u16 bios;
450 u16 ec;
451 unsigned long quirks;
455 * tpacpi_check_quirks() - search BIOS/EC version on a list
456 * @qlist: array of &struct tpacpi_quirk
457 * @qlist_size: number of elements in @qlist
459 * Iterates over a quirks list until one is found that matches the
460 * ThinkPad's vendor, BIOS and EC model.
462 * Returns 0 if nothing matches, otherwise returns the quirks field of
463 * the matching &struct tpacpi_quirk entry.
465 * The match criteria is: vendor, ec and bios much match.
467 static unsigned long __init tpacpi_check_quirks(
468 const struct tpacpi_quirk *qlist,
469 unsigned int qlist_size)
471 while (qlist_size) {
472 if ((qlist->vendor == thinkpad_id.vendor ||
473 qlist->vendor == TPACPI_MATCH_ANY) &&
474 (qlist->bios == thinkpad_id.bios_model ||
475 qlist->bios == TPACPI_MATCH_ANY) &&
476 (qlist->ec == thinkpad_id.ec_model ||
477 qlist->ec == TPACPI_MATCH_ANY))
478 return qlist->quirks;
480 qlist_size--;
481 qlist++;
483 return 0;
486 static inline bool __pure __init tpacpi_is_lenovo(void)
488 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
491 static inline bool __pure __init tpacpi_is_ibm(void)
493 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
496 /****************************************************************************
497 ****************************************************************************
499 * ACPI Helpers and device model
501 ****************************************************************************
502 ****************************************************************************/
504 /*************************************************************************
505 * ACPI basic handles
508 static acpi_handle root_handle;
509 static acpi_handle ec_handle;
511 #define TPACPI_HANDLE(object, parent, paths...) \
512 static acpi_handle object##_handle; \
513 static const acpi_handle *object##_parent __initdata = \
514 &parent##_handle; \
515 static char *object##_paths[] __initdata = { paths }
517 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
518 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
520 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
521 /* T4x, X31, X40 */
522 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
523 "\\CMS", /* R40, R40e */
524 ); /* all others */
526 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
527 "^HKEY", /* R30, R31 */
528 "HKEY", /* all others */
529 ); /* 570 */
531 /*************************************************************************
532 * ACPI helpers
535 static int acpi_evalf(acpi_handle handle,
536 void *res, char *method, char *fmt, ...)
538 char *fmt0 = fmt;
539 struct acpi_object_list params;
540 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
541 struct acpi_buffer result, *resultp;
542 union acpi_object out_obj;
543 acpi_status status;
544 va_list ap;
545 char res_type;
546 int success;
547 int quiet;
549 if (!*fmt) {
550 pr_err("acpi_evalf() called with empty format\n");
551 return 0;
554 if (*fmt == 'q') {
555 quiet = 1;
556 fmt++;
557 } else
558 quiet = 0;
560 res_type = *(fmt++);
562 params.count = 0;
563 params.pointer = &in_objs[0];
565 va_start(ap, fmt);
566 while (*fmt) {
567 char c = *(fmt++);
568 switch (c) {
569 case 'd': /* int */
570 in_objs[params.count].integer.value = va_arg(ap, int);
571 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
572 break;
573 /* add more types as needed */
574 default:
575 pr_err("acpi_evalf() called "
576 "with invalid format character '%c'\n", c);
577 va_end(ap);
578 return 0;
581 va_end(ap);
583 if (res_type != 'v') {
584 result.length = sizeof(out_obj);
585 result.pointer = &out_obj;
586 resultp = &result;
587 } else
588 resultp = NULL;
590 status = acpi_evaluate_object(handle, method, &params, resultp);
592 switch (res_type) {
593 case 'd': /* int */
594 success = (status == AE_OK &&
595 out_obj.type == ACPI_TYPE_INTEGER);
596 if (success && res)
597 *(int *)res = out_obj.integer.value;
598 break;
599 case 'v': /* void */
600 success = status == AE_OK;
601 break;
602 /* add more types as needed */
603 default:
604 pr_err("acpi_evalf() called "
605 "with invalid format character '%c'\n", res_type);
606 return 0;
609 if (!success && !quiet)
610 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
611 method, fmt0, acpi_format_exception(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))
664 static void __init drv_acpi_handle_init(const char *name,
665 acpi_handle *handle, const acpi_handle parent,
666 char **paths, const int num_paths)
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 dbg_printk(TPACPI_DBG_INIT,
678 "Found ACPI handle %s for %s\n",
679 paths[i], name);
680 return;
684 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
685 name);
686 *handle = NULL;
689 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
690 u32 level, void *context, void **return_value)
692 *(acpi_handle *)return_value = handle;
694 return AE_CTRL_TERMINATE;
697 static void __init tpacpi_acpi_handle_locate(const char *name,
698 const char *hid,
699 acpi_handle *handle)
701 acpi_status status;
702 acpi_handle device_found;
704 BUG_ON(!name || !hid || !handle);
705 vdbg_printk(TPACPI_DBG_INIT,
706 "trying to locate ACPI handle for %s, using HID %s\n",
707 name, hid);
709 memset(&device_found, 0, sizeof(device_found));
710 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
711 (void *)name, &device_found);
713 *handle = NULL;
715 if (ACPI_SUCCESS(status)) {
716 *handle = device_found;
717 dbg_printk(TPACPI_DBG_INIT,
718 "Found ACPI handle for %s\n", name);
719 } else {
720 vdbg_printk(TPACPI_DBG_INIT,
721 "Could not locate an ACPI handle for %s: %s\n",
722 name, acpi_format_exception(status));
726 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
728 struct ibm_struct *ibm = data;
730 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
731 return;
733 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
734 return;
736 ibm->acpi->notify(ibm, event);
739 static int __init setup_acpi_notify(struct ibm_struct *ibm)
741 acpi_status status;
742 int rc;
744 BUG_ON(!ibm->acpi);
746 if (!*ibm->acpi->handle)
747 return 0;
749 vdbg_printk(TPACPI_DBG_INIT,
750 "setting up ACPI notify for %s\n", ibm->name);
752 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
753 if (rc < 0) {
754 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
755 return -ENODEV;
758 ibm->acpi->device->driver_data = ibm;
759 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
760 TPACPI_ACPI_EVENT_PREFIX,
761 ibm->name);
763 status = acpi_install_notify_handler(*ibm->acpi->handle,
764 ibm->acpi->type, dispatch_acpi_notify, ibm);
765 if (ACPI_FAILURE(status)) {
766 if (status == AE_ALREADY_EXISTS) {
767 pr_notice("another device driver is already "
768 "handling %s events\n", ibm->name);
769 } else {
770 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
771 ibm->name, acpi_format_exception(status));
773 return -ENODEV;
775 ibm->flags.acpi_notify_installed = 1;
776 return 0;
779 static int __init tpacpi_device_add(struct acpi_device *device)
781 return 0;
784 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
786 int rc;
788 dbg_printk(TPACPI_DBG_INIT,
789 "registering %s as an ACPI driver\n", ibm->name);
791 BUG_ON(!ibm->acpi);
793 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
794 if (!ibm->acpi->driver) {
795 pr_err("failed to allocate memory for ibm->acpi->driver\n");
796 return -ENOMEM;
799 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
800 ibm->acpi->driver->ids = ibm->acpi->hid;
802 ibm->acpi->driver->ops.add = &tpacpi_device_add;
804 rc = acpi_bus_register_driver(ibm->acpi->driver);
805 if (rc < 0) {
806 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
807 ibm->name, rc);
808 kfree(ibm->acpi->driver);
809 ibm->acpi->driver = NULL;
810 } else if (!rc)
811 ibm->flags.acpi_driver_registered = 1;
813 return rc;
817 /****************************************************************************
818 ****************************************************************************
820 * Procfs Helpers
822 ****************************************************************************
823 ****************************************************************************/
825 static int dispatch_proc_show(struct seq_file *m, void *v)
827 struct ibm_struct *ibm = m->private;
829 if (!ibm || !ibm->read)
830 return -EINVAL;
831 return ibm->read(m);
834 static int dispatch_proc_open(struct inode *inode, struct file *file)
836 return single_open(file, dispatch_proc_show, PDE(inode)->data);
839 static ssize_t dispatch_proc_write(struct file *file,
840 const char __user *userbuf,
841 size_t count, loff_t *pos)
843 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
844 char *kernbuf;
845 int ret;
847 if (!ibm || !ibm->write)
848 return -EINVAL;
849 if (count > PAGE_SIZE - 2)
850 return -EINVAL;
852 kernbuf = kmalloc(count + 2, GFP_KERNEL);
853 if (!kernbuf)
854 return -ENOMEM;
856 if (copy_from_user(kernbuf, userbuf, count)) {
857 kfree(kernbuf);
858 return -EFAULT;
861 kernbuf[count] = 0;
862 strcat(kernbuf, ",");
863 ret = ibm->write(kernbuf);
864 if (ret == 0)
865 ret = count;
867 kfree(kernbuf);
869 return ret;
872 static const struct file_operations dispatch_proc_fops = {
873 .owner = THIS_MODULE,
874 .open = dispatch_proc_open,
875 .read = seq_read,
876 .llseek = seq_lseek,
877 .release = single_release,
878 .write = dispatch_proc_write,
881 static char *next_cmd(char **cmds)
883 char *start = *cmds;
884 char *end;
886 while ((end = strchr(start, ',')) && end == start)
887 start = end + 1;
889 if (!end)
890 return NULL;
892 *end = 0;
893 *cmds = end + 1;
894 return start;
898 /****************************************************************************
899 ****************************************************************************
901 * Device model: input, hwmon and platform
903 ****************************************************************************
904 ****************************************************************************/
906 static struct platform_device *tpacpi_pdev;
907 static struct platform_device *tpacpi_sensors_pdev;
908 static struct device *tpacpi_hwmon;
909 static struct input_dev *tpacpi_inputdev;
910 static struct mutex tpacpi_inputdev_send_mutex;
911 static LIST_HEAD(tpacpi_all_drivers);
913 static int tpacpi_suspend_handler(struct platform_device *pdev,
914 pm_message_t state)
916 struct ibm_struct *ibm, *itmp;
918 list_for_each_entry_safe(ibm, itmp,
919 &tpacpi_all_drivers,
920 all_drivers) {
921 if (ibm->suspend)
922 (ibm->suspend)(state);
925 return 0;
928 static int tpacpi_resume_handler(struct platform_device *pdev)
930 struct ibm_struct *ibm, *itmp;
932 list_for_each_entry_safe(ibm, itmp,
933 &tpacpi_all_drivers,
934 all_drivers) {
935 if (ibm->resume)
936 (ibm->resume)();
939 return 0;
942 static void tpacpi_shutdown_handler(struct platform_device *pdev)
944 struct ibm_struct *ibm, *itmp;
946 list_for_each_entry_safe(ibm, itmp,
947 &tpacpi_all_drivers,
948 all_drivers) {
949 if (ibm->shutdown)
950 (ibm->shutdown)();
954 static struct platform_driver tpacpi_pdriver = {
955 .driver = {
956 .name = TPACPI_DRVR_NAME,
957 .owner = THIS_MODULE,
959 .suspend = tpacpi_suspend_handler,
960 .resume = tpacpi_resume_handler,
961 .shutdown = tpacpi_shutdown_handler,
964 static struct platform_driver tpacpi_hwmon_pdriver = {
965 .driver = {
966 .name = TPACPI_HWMON_DRVR_NAME,
967 .owner = THIS_MODULE,
971 /*************************************************************************
972 * sysfs support helpers
975 struct attribute_set {
976 unsigned int members, max_members;
977 struct attribute_group group;
980 struct attribute_set_obj {
981 struct attribute_set s;
982 struct attribute *a;
983 } __attribute__((packed));
985 static struct attribute_set *create_attr_set(unsigned int max_members,
986 const char *name)
988 struct attribute_set_obj *sobj;
990 if (max_members == 0)
991 return NULL;
993 /* Allocates space for implicit NULL at the end too */
994 sobj = kzalloc(sizeof(struct attribute_set_obj) +
995 max_members * sizeof(struct attribute *),
996 GFP_KERNEL);
997 if (!sobj)
998 return NULL;
999 sobj->s.max_members = max_members;
1000 sobj->s.group.attrs = &sobj->a;
1001 sobj->s.group.name = name;
1003 return &sobj->s;
1006 #define destroy_attr_set(_set) \
1007 kfree(_set);
1009 /* not multi-threaded safe, use it in a single thread per set */
1010 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1012 if (!s || !attr)
1013 return -EINVAL;
1015 if (s->members >= s->max_members)
1016 return -ENOMEM;
1018 s->group.attrs[s->members] = attr;
1019 s->members++;
1021 return 0;
1024 static int add_many_to_attr_set(struct attribute_set *s,
1025 struct attribute **attr,
1026 unsigned int count)
1028 int i, res;
1030 for (i = 0; i < count; i++) {
1031 res = add_to_attr_set(s, attr[i]);
1032 if (res)
1033 return res;
1036 return 0;
1039 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1041 sysfs_remove_group(kobj, &s->group);
1042 destroy_attr_set(s);
1045 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1046 sysfs_create_group(_kobj, &_attr_set->group)
1048 static int parse_strtoul(const char *buf,
1049 unsigned long max, unsigned long *value)
1051 char *endp;
1053 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1054 endp = skip_spaces(endp);
1055 if (*endp || *value > max)
1056 return -EINVAL;
1058 return 0;
1061 static void tpacpi_disable_brightness_delay(void)
1063 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1064 pr_notice("ACPI backlight control delay disabled\n");
1067 static void printk_deprecated_attribute(const char * const what,
1068 const char * const details)
1070 tpacpi_log_usertask("deprecated sysfs attribute");
1071 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1072 "will be removed. %s\n",
1073 what, details);
1076 /*************************************************************************
1077 * rfkill and radio control support helpers
1081 * ThinkPad-ACPI firmware handling model:
1083 * WLSW (master wireless switch) is event-driven, and is common to all
1084 * firmware-controlled radios. It cannot be controlled, just monitored,
1085 * as expected. It overrides all radio state in firmware
1087 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1088 * (TODO: verify how WLSW interacts with the returned radio state).
1090 * The only time there are shadow radio state changes, is when
1091 * masked-off hotkeys are used.
1095 * Internal driver API for radio state:
1097 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1098 * bool: true means radio blocked (off)
1100 enum tpacpi_rfkill_state {
1101 TPACPI_RFK_RADIO_OFF = 0,
1102 TPACPI_RFK_RADIO_ON
1105 /* rfkill switches */
1106 enum tpacpi_rfk_id {
1107 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1108 TPACPI_RFK_WWAN_SW_ID,
1109 TPACPI_RFK_UWB_SW_ID,
1110 TPACPI_RFK_SW_MAX
1113 static const char *tpacpi_rfkill_names[] = {
1114 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1115 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1116 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1117 [TPACPI_RFK_SW_MAX] = NULL
1120 /* ThinkPad-ACPI rfkill subdriver */
1121 struct tpacpi_rfk {
1122 struct rfkill *rfkill;
1123 enum tpacpi_rfk_id id;
1124 const struct tpacpi_rfk_ops *ops;
1127 struct tpacpi_rfk_ops {
1128 /* firmware interface */
1129 int (*get_status)(void);
1130 int (*set_status)(const enum tpacpi_rfkill_state);
1133 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1135 /* Query FW and update rfkill sw state for a given rfkill switch */
1136 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1138 int status;
1140 if (!tp_rfk)
1141 return -ENODEV;
1143 status = (tp_rfk->ops->get_status)();
1144 if (status < 0)
1145 return status;
1147 rfkill_set_sw_state(tp_rfk->rfkill,
1148 (status == TPACPI_RFK_RADIO_OFF));
1150 return status;
1153 /* Query FW and update rfkill sw state for all rfkill switches */
1154 static void tpacpi_rfk_update_swstate_all(void)
1156 unsigned int i;
1158 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1159 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1163 * Sync the HW-blocking state of all rfkill switches,
1164 * do notice it causes the rfkill core to schedule uevents
1166 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1168 unsigned int i;
1169 struct tpacpi_rfk *tp_rfk;
1171 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1172 tp_rfk = tpacpi_rfkill_switches[i];
1173 if (tp_rfk) {
1174 if (rfkill_set_hw_state(tp_rfk->rfkill,
1175 blocked)) {
1176 /* ignore -- we track sw block */
1182 /* Call to get the WLSW state from the firmware */
1183 static int hotkey_get_wlsw(void);
1185 /* Call to query WLSW state and update all rfkill switches */
1186 static bool tpacpi_rfk_check_hwblock_state(void)
1188 int res = hotkey_get_wlsw();
1189 int hw_blocked;
1191 /* When unknown or unsupported, we have to assume it is unblocked */
1192 if (res < 0)
1193 return false;
1195 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1196 tpacpi_rfk_update_hwblock_state(hw_blocked);
1198 return hw_blocked;
1201 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1203 struct tpacpi_rfk *tp_rfk = data;
1204 int res;
1206 dbg_printk(TPACPI_DBG_RFKILL,
1207 "request to change radio state to %s\n",
1208 blocked ? "blocked" : "unblocked");
1210 /* try to set radio state */
1211 res = (tp_rfk->ops->set_status)(blocked ?
1212 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1214 /* and update the rfkill core with whatever the FW really did */
1215 tpacpi_rfk_update_swstate(tp_rfk);
1217 return (res < 0) ? res : 0;
1220 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1221 .set_block = tpacpi_rfk_hook_set_block,
1224 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1225 const struct tpacpi_rfk_ops *tp_rfkops,
1226 const enum rfkill_type rfktype,
1227 const char *name,
1228 const bool set_default)
1230 struct tpacpi_rfk *atp_rfk;
1231 int res;
1232 bool sw_state = false;
1233 bool hw_state;
1234 int sw_status;
1236 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1238 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1239 if (atp_rfk)
1240 atp_rfk->rfkill = rfkill_alloc(name,
1241 &tpacpi_pdev->dev,
1242 rfktype,
1243 &tpacpi_rfk_rfkill_ops,
1244 atp_rfk);
1245 if (!atp_rfk || !atp_rfk->rfkill) {
1246 pr_err("failed to allocate memory for rfkill class\n");
1247 kfree(atp_rfk);
1248 return -ENOMEM;
1251 atp_rfk->id = id;
1252 atp_rfk->ops = tp_rfkops;
1254 sw_status = (tp_rfkops->get_status)();
1255 if (sw_status < 0) {
1256 pr_err("failed to read initial state for %s, error %d\n",
1257 name, sw_status);
1258 } else {
1259 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1260 if (set_default) {
1261 /* try to keep the initial state, since we ask the
1262 * firmware to preserve it across S5 in NVRAM */
1263 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1266 hw_state = tpacpi_rfk_check_hwblock_state();
1267 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1269 res = rfkill_register(atp_rfk->rfkill);
1270 if (res < 0) {
1271 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1272 rfkill_destroy(atp_rfk->rfkill);
1273 kfree(atp_rfk);
1274 return res;
1277 tpacpi_rfkill_switches[id] = atp_rfk;
1279 pr_info("rfkill switch %s: radio is %sblocked\n",
1280 name, (sw_state || hw_state) ? "" : "un");
1281 return 0;
1284 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1286 struct tpacpi_rfk *tp_rfk;
1288 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1290 tp_rfk = tpacpi_rfkill_switches[id];
1291 if (tp_rfk) {
1292 rfkill_unregister(tp_rfk->rfkill);
1293 rfkill_destroy(tp_rfk->rfkill);
1294 tpacpi_rfkill_switches[id] = NULL;
1295 kfree(tp_rfk);
1299 static void printk_deprecated_rfkill_attribute(const char * const what)
1301 printk_deprecated_attribute(what,
1302 "Please switch to generic rfkill before year 2010");
1305 /* sysfs <radio> enable ------------------------------------------------ */
1306 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1307 struct device_attribute *attr,
1308 char *buf)
1310 int status;
1312 printk_deprecated_rfkill_attribute(attr->attr.name);
1314 /* This is in the ABI... */
1315 if (tpacpi_rfk_check_hwblock_state()) {
1316 status = TPACPI_RFK_RADIO_OFF;
1317 } else {
1318 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1319 if (status < 0)
1320 return status;
1323 return snprintf(buf, PAGE_SIZE, "%d\n",
1324 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1327 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1328 struct device_attribute *attr,
1329 const char *buf, size_t count)
1331 unsigned long t;
1332 int res;
1334 printk_deprecated_rfkill_attribute(attr->attr.name);
1336 if (parse_strtoul(buf, 1, &t))
1337 return -EINVAL;
1339 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state() && !!t)
1343 return -EPERM;
1345 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1346 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1347 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1349 return (res < 0) ? res : count;
1352 /* procfs -------------------------------------------------------------- */
1353 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1355 if (id >= TPACPI_RFK_SW_MAX)
1356 seq_printf(m, "status:\t\tnot supported\n");
1357 else {
1358 int status;
1360 /* This is in the ABI... */
1361 if (tpacpi_rfk_check_hwblock_state()) {
1362 status = TPACPI_RFK_RADIO_OFF;
1363 } else {
1364 status = tpacpi_rfk_update_swstate(
1365 tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1370 seq_printf(m, "status:\t\t%s\n",
1371 (status == TPACPI_RFK_RADIO_ON) ?
1372 "enabled" : "disabled");
1373 seq_printf(m, "commands:\tenable, disable\n");
1376 return 0;
1379 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1381 char *cmd;
1382 int status = -1;
1383 int res = 0;
1385 if (id >= TPACPI_RFK_SW_MAX)
1386 return -ENODEV;
1388 while ((cmd = next_cmd(&buf))) {
1389 if (strlencmp(cmd, "enable") == 0)
1390 status = TPACPI_RFK_RADIO_ON;
1391 else if (strlencmp(cmd, "disable") == 0)
1392 status = TPACPI_RFK_RADIO_OFF;
1393 else
1394 return -EINVAL;
1397 if (status != -1) {
1398 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1399 (status == TPACPI_RFK_RADIO_ON) ?
1400 "enable" : "disable",
1401 tpacpi_rfkill_names[id]);
1402 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1403 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1406 return res;
1409 /*************************************************************************
1410 * thinkpad-acpi driver attributes
1413 /* interface_version --------------------------------------------------- */
1414 static ssize_t tpacpi_driver_interface_version_show(
1415 struct device_driver *drv,
1416 char *buf)
1418 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1421 static DRIVER_ATTR(interface_version, S_IRUGO,
1422 tpacpi_driver_interface_version_show, NULL);
1424 /* debug_level --------------------------------------------------------- */
1425 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1426 char *buf)
1428 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1431 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1432 const char *buf, size_t count)
1434 unsigned long t;
1436 if (parse_strtoul(buf, 0xffff, &t))
1437 return -EINVAL;
1439 dbg_level = t;
1441 return count;
1444 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1445 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1447 /* version ------------------------------------------------------------- */
1448 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1449 char *buf)
1451 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1452 TPACPI_DESC, TPACPI_VERSION);
1455 static DRIVER_ATTR(version, S_IRUGO,
1456 tpacpi_driver_version_show, NULL);
1458 /* --------------------------------------------------------------------- */
1460 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1462 /* wlsw_emulstate ------------------------------------------------------ */
1463 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1464 char *buf)
1466 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1469 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1470 const char *buf, size_t count)
1472 unsigned long t;
1474 if (parse_strtoul(buf, 1, &t))
1475 return -EINVAL;
1477 if (tpacpi_wlsw_emulstate != !!t) {
1478 tpacpi_wlsw_emulstate = !!t;
1479 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1482 return count;
1485 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1486 tpacpi_driver_wlsw_emulstate_show,
1487 tpacpi_driver_wlsw_emulstate_store);
1489 /* bluetooth_emulstate ------------------------------------------------- */
1490 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1491 struct device_driver *drv,
1492 char *buf)
1494 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1497 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1498 struct device_driver *drv,
1499 const char *buf, size_t count)
1501 unsigned long t;
1503 if (parse_strtoul(buf, 1, &t))
1504 return -EINVAL;
1506 tpacpi_bluetooth_emulstate = !!t;
1508 return count;
1511 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1512 tpacpi_driver_bluetooth_emulstate_show,
1513 tpacpi_driver_bluetooth_emulstate_store);
1515 /* wwan_emulstate ------------------------------------------------- */
1516 static ssize_t tpacpi_driver_wwan_emulstate_show(
1517 struct device_driver *drv,
1518 char *buf)
1520 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1523 static ssize_t tpacpi_driver_wwan_emulstate_store(
1524 struct device_driver *drv,
1525 const char *buf, size_t count)
1527 unsigned long t;
1529 if (parse_strtoul(buf, 1, &t))
1530 return -EINVAL;
1532 tpacpi_wwan_emulstate = !!t;
1534 return count;
1537 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1538 tpacpi_driver_wwan_emulstate_show,
1539 tpacpi_driver_wwan_emulstate_store);
1541 /* uwb_emulstate ------------------------------------------------- */
1542 static ssize_t tpacpi_driver_uwb_emulstate_show(
1543 struct device_driver *drv,
1544 char *buf)
1546 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1549 static ssize_t tpacpi_driver_uwb_emulstate_store(
1550 struct device_driver *drv,
1551 const char *buf, size_t count)
1553 unsigned long t;
1555 if (parse_strtoul(buf, 1, &t))
1556 return -EINVAL;
1558 tpacpi_uwb_emulstate = !!t;
1560 return count;
1563 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1564 tpacpi_driver_uwb_emulstate_show,
1565 tpacpi_driver_uwb_emulstate_store);
1566 #endif
1568 /* --------------------------------------------------------------------- */
1570 static struct driver_attribute *tpacpi_driver_attributes[] = {
1571 &driver_attr_debug_level, &driver_attr_version,
1572 &driver_attr_interface_version,
1575 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1577 int i, res;
1579 i = 0;
1580 res = 0;
1581 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1582 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1583 i++;
1586 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1587 if (!res && dbg_wlswemul)
1588 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1589 if (!res && dbg_bluetoothemul)
1590 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1591 if (!res && dbg_wwanemul)
1592 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1593 if (!res && dbg_uwbemul)
1594 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1595 #endif
1597 return res;
1600 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1602 int i;
1604 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1605 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1607 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1608 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1609 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1610 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1611 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1612 #endif
1615 /*************************************************************************
1616 * Firmware Data
1620 * Table of recommended minimum BIOS versions
1622 * Reasons for listing:
1623 * 1. Stable BIOS, listed because the unknown amount of
1624 * bugs and bad ACPI behaviour on older versions
1626 * 2. BIOS or EC fw with known bugs that trigger on Linux
1628 * 3. BIOS with known reduced functionality in older versions
1630 * We recommend the latest BIOS and EC version.
1631 * We only support the latest BIOS and EC fw version as a rule.
1633 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1634 * Information from users in ThinkWiki
1636 * WARNING: we use this table also to detect that the machine is
1637 * a ThinkPad in some cases, so don't remove entries lightly.
1640 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1641 { .vendor = (__v), \
1642 .bios = TPID(__id1, __id2), \
1643 .ec = TPACPI_MATCH_ANY, \
1644 .quirks = TPACPI_MATCH_ANY << 16 \
1645 | (__bv1) << 8 | (__bv2) }
1647 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1648 __eid, __ev1, __ev2) \
1649 { .vendor = (__v), \
1650 .bios = TPID(__bid1, __bid2), \
1651 .ec = __eid, \
1652 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1653 | (__bv1) << 8 | (__bv2) }
1655 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1656 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1658 /* Outdated IBM BIOSes often lack the EC id string */
1659 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1660 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1661 __bv1, __bv2, TPID(__id1, __id2), \
1662 __ev1, __ev2), \
1663 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1664 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1665 __ev1, __ev2)
1667 /* Outdated IBM BIOSes often lack the EC id string */
1668 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1669 __eid1, __eid2, __ev1, __ev2) \
1670 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1671 __bv1, __bv2, TPID(__eid1, __eid2), \
1672 __ev1, __ev2), \
1673 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1674 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1675 __ev1, __ev2)
1677 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1678 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1680 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1682 __bv1, __bv2, TPID(__id1, __id2), \
1683 __ev1, __ev2)
1685 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1686 __eid1, __eid2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1688 __bv1, __bv2, TPID(__eid1, __eid2), \
1689 __ev1, __ev2)
1691 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1692 /* Numeric models ------------------ */
1693 /* FW MODEL BIOS VERS */
1694 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1695 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1696 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1697 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1698 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1699 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1700 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1701 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1702 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1704 /* A-series ------------------------- */
1705 /* FW MODEL BIOS VERS EC VERS */
1706 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1707 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1708 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1709 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1710 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1711 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1712 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1713 TPV_QI0('1', '3', '2', '0'), /* A22m */
1714 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1715 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1716 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1718 /* G-series ------------------------- */
1719 /* FW MODEL BIOS VERS */
1720 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1721 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1723 /* R-series, T-series --------------- */
1724 /* FW MODEL BIOS VERS EC VERS */
1725 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1726 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1727 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1728 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1729 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1730 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1731 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1732 T40/p, T41/p, T42/p (1) */
1733 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1734 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1735 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1736 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1738 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1739 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1740 TPV_QI0('1', '6', '3', '2'), /* T22 */
1741 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1742 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1743 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1745 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1746 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1747 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1749 /* BIOS FW BIOS VERS EC FW EC VERS */
1750 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1751 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1753 /* X-series ------------------------- */
1754 /* FW MODEL BIOS VERS EC VERS */
1755 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1756 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1757 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1758 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1759 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1760 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1761 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1763 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1764 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1766 /* (0) - older versions lack DMI EC fw string and functionality */
1767 /* (1) - older versions known to lack functionality */
1770 #undef TPV_QL1
1771 #undef TPV_QL0
1772 #undef TPV_QI2
1773 #undef TPV_QI1
1774 #undef TPV_QI0
1775 #undef TPV_Q_X
1776 #undef TPV_Q
1778 static void __init tpacpi_check_outdated_fw(void)
1780 unsigned long fwvers;
1781 u16 ec_version, bios_version;
1783 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1784 ARRAY_SIZE(tpacpi_bios_version_qtable));
1786 if (!fwvers)
1787 return;
1789 bios_version = fwvers & 0xffffU;
1790 ec_version = (fwvers >> 16) & 0xffffU;
1792 /* note that unknown versions are set to 0x0000 and we use that */
1793 if ((bios_version > thinkpad_id.bios_release) ||
1794 (ec_version > thinkpad_id.ec_release &&
1795 ec_version != TPACPI_MATCH_ANY)) {
1797 * The changelogs would let us track down the exact
1798 * reason, but it is just too much of a pain to track
1799 * it. We only list BIOSes that are either really
1800 * broken, or really stable to begin with, so it is
1801 * best if the user upgrades the firmware anyway.
1803 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1804 pr_warn("WARNING: This firmware may be missing critical bug "
1805 "fixes and/or important features\n");
1809 static bool __init tpacpi_is_fw_known(void)
1811 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1812 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1815 /****************************************************************************
1816 ****************************************************************************
1818 * Subdrivers
1820 ****************************************************************************
1821 ****************************************************************************/
1823 /*************************************************************************
1824 * thinkpad-acpi metadata subdriver
1827 static int thinkpad_acpi_driver_read(struct seq_file *m)
1829 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1830 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1831 return 0;
1834 static struct ibm_struct thinkpad_acpi_driver_data = {
1835 .name = "driver",
1836 .read = thinkpad_acpi_driver_read,
1839 /*************************************************************************
1840 * Hotkey subdriver
1844 * ThinkPad firmware event model
1846 * The ThinkPad firmware has two main event interfaces: normal ACPI
1847 * notifications (which follow the ACPI standard), and a private event
1848 * interface.
1850 * The private event interface also issues events for the hotkeys. As
1851 * the driver gained features, the event handling code ended up being
1852 * built around the hotkey subdriver. This will need to be refactored
1853 * to a more formal event API eventually.
1855 * Some "hotkeys" are actually supposed to be used as event reports,
1856 * such as "brightness has changed", "volume has changed", depending on
1857 * the ThinkPad model and how the firmware is operating.
1859 * Unlike other classes, hotkey-class events have mask/unmask control on
1860 * non-ancient firmware. However, how it behaves changes a lot with the
1861 * firmware model and version.
1864 enum { /* hot key scan codes (derived from ACPI DSDT) */
1865 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1866 TP_ACPI_HOTKEYSCAN_FNF2,
1867 TP_ACPI_HOTKEYSCAN_FNF3,
1868 TP_ACPI_HOTKEYSCAN_FNF4,
1869 TP_ACPI_HOTKEYSCAN_FNF5,
1870 TP_ACPI_HOTKEYSCAN_FNF6,
1871 TP_ACPI_HOTKEYSCAN_FNF7,
1872 TP_ACPI_HOTKEYSCAN_FNF8,
1873 TP_ACPI_HOTKEYSCAN_FNF9,
1874 TP_ACPI_HOTKEYSCAN_FNF10,
1875 TP_ACPI_HOTKEYSCAN_FNF11,
1876 TP_ACPI_HOTKEYSCAN_FNF12,
1877 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1878 TP_ACPI_HOTKEYSCAN_FNINSERT,
1879 TP_ACPI_HOTKEYSCAN_FNDELETE,
1880 TP_ACPI_HOTKEYSCAN_FNHOME,
1881 TP_ACPI_HOTKEYSCAN_FNEND,
1882 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1883 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1884 TP_ACPI_HOTKEYSCAN_FNSPACE,
1885 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1886 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1887 TP_ACPI_HOTKEYSCAN_MUTE,
1888 TP_ACPI_HOTKEYSCAN_THINKPAD,
1889 TP_ACPI_HOTKEYSCAN_UNK1,
1890 TP_ACPI_HOTKEYSCAN_UNK2,
1891 TP_ACPI_HOTKEYSCAN_UNK3,
1892 TP_ACPI_HOTKEYSCAN_UNK4,
1893 TP_ACPI_HOTKEYSCAN_UNK5,
1894 TP_ACPI_HOTKEYSCAN_UNK6,
1895 TP_ACPI_HOTKEYSCAN_UNK7,
1896 TP_ACPI_HOTKEYSCAN_UNK8,
1898 /* Hotkey keymap size */
1899 TPACPI_HOTKEY_MAP_LEN
1902 enum { /* Keys/events available through NVRAM polling */
1903 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1904 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1907 enum { /* Positions of some of the keys in hotkey masks */
1908 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1909 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1910 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1911 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1912 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1913 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1914 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1915 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1916 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1917 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1918 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1921 enum { /* NVRAM to ACPI HKEY group map */
1922 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1923 TP_ACPI_HKEY_ZOOM_MASK |
1924 TP_ACPI_HKEY_DISPSWTCH_MASK |
1925 TP_ACPI_HKEY_HIBERNATE_MASK,
1926 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1927 TP_ACPI_HKEY_BRGHTDWN_MASK,
1928 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1929 TP_ACPI_HKEY_VOLDWN_MASK |
1930 TP_ACPI_HKEY_MUTE_MASK,
1933 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1934 struct tp_nvram_state {
1935 u16 thinkpad_toggle:1;
1936 u16 zoom_toggle:1;
1937 u16 display_toggle:1;
1938 u16 thinklight_toggle:1;
1939 u16 hibernate_toggle:1;
1940 u16 displayexp_toggle:1;
1941 u16 display_state:1;
1942 u16 brightness_toggle:1;
1943 u16 volume_toggle:1;
1944 u16 mute:1;
1946 u8 brightness_level;
1947 u8 volume_level;
1950 /* kthread for the hotkey poller */
1951 static struct task_struct *tpacpi_hotkey_task;
1953 /* Acquired while the poller kthread is running, use to sync start/stop */
1954 static struct mutex hotkey_thread_mutex;
1957 * Acquire mutex to write poller control variables as an
1958 * atomic block.
1960 * Increment hotkey_config_change when changing them if you
1961 * want the kthread to forget old state.
1963 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1965 static struct mutex hotkey_thread_data_mutex;
1966 static unsigned int hotkey_config_change;
1969 * hotkey poller control variables
1971 * Must be atomic or readers will also need to acquire mutex
1973 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1974 * should be used only when the changes need to be taken as
1975 * a block, OR when one needs to force the kthread to forget
1976 * old state.
1978 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1979 static unsigned int hotkey_poll_freq = 10; /* Hz */
1981 #define HOTKEY_CONFIG_CRITICAL_START \
1982 do { \
1983 mutex_lock(&hotkey_thread_data_mutex); \
1984 hotkey_config_change++; \
1985 } while (0);
1986 #define HOTKEY_CONFIG_CRITICAL_END \
1987 mutex_unlock(&hotkey_thread_data_mutex);
1989 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1991 #define hotkey_source_mask 0U
1992 #define HOTKEY_CONFIG_CRITICAL_START
1993 #define HOTKEY_CONFIG_CRITICAL_END
1995 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1997 static struct mutex hotkey_mutex;
1999 static enum { /* Reasons for waking up */
2000 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2001 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2002 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2003 } hotkey_wakeup_reason;
2005 static int hotkey_autosleep_ack;
2007 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2008 static u32 hotkey_all_mask; /* all events supported in fw */
2009 static u32 hotkey_reserved_mask; /* events better left disabled */
2010 static u32 hotkey_driver_mask; /* events needed by the driver */
2011 static u32 hotkey_user_mask; /* events visible to userspace */
2012 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2014 static unsigned int hotkey_report_mode;
2016 static u16 *hotkey_keycode_map;
2018 static struct attribute_set *hotkey_dev_attributes;
2020 static void tpacpi_driver_event(const unsigned int hkey_event);
2021 static void hotkey_driver_event(const unsigned int scancode);
2022 static void hotkey_poll_setup(const bool may_warn);
2024 /* HKEY.MHKG() return bits */
2025 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2027 static int hotkey_get_wlsw(void)
2029 int status;
2031 if (!tp_features.hotkey_wlsw)
2032 return -ENODEV;
2034 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2035 if (dbg_wlswemul)
2036 return (tpacpi_wlsw_emulstate) ?
2037 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2038 #endif
2040 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2041 return -EIO;
2043 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2046 static int hotkey_get_tablet_mode(int *status)
2048 int s;
2050 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2051 return -EIO;
2053 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2054 return 0;
2058 * Reads current event mask from firmware, and updates
2059 * hotkey_acpi_mask accordingly. Also resets any bits
2060 * from hotkey_user_mask that are unavailable to be
2061 * delivered (shadow requirement of the userspace ABI).
2063 * Call with hotkey_mutex held
2065 static int hotkey_mask_get(void)
2067 if (tp_features.hotkey_mask) {
2068 u32 m = 0;
2070 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2071 return -EIO;
2073 hotkey_acpi_mask = m;
2074 } else {
2075 /* no mask support doesn't mean no event support... */
2076 hotkey_acpi_mask = hotkey_all_mask;
2079 /* sync userspace-visible mask */
2080 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2082 return 0;
2085 void static hotkey_mask_warn_incomplete_mask(void)
2087 /* log only what the user can fix... */
2088 const u32 wantedmask = hotkey_driver_mask &
2089 ~(hotkey_acpi_mask | hotkey_source_mask) &
2090 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2092 if (wantedmask)
2093 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2097 * Set the firmware mask when supported
2099 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2101 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2103 * Call with hotkey_mutex held
2105 static int hotkey_mask_set(u32 mask)
2107 int i;
2108 int rc = 0;
2110 const u32 fwmask = mask & ~hotkey_source_mask;
2112 if (tp_features.hotkey_mask) {
2113 for (i = 0; i < 32; i++) {
2114 if (!acpi_evalf(hkey_handle,
2115 NULL, "MHKM", "vdd", i + 1,
2116 !!(mask & (1 << i)))) {
2117 rc = -EIO;
2118 break;
2124 * We *must* make an inconditional call to hotkey_mask_get to
2125 * refresh hotkey_acpi_mask and update hotkey_user_mask
2127 * Take the opportunity to also log when we cannot _enable_
2128 * a given event.
2130 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2131 pr_notice("asked for hotkey mask 0x%08x, but "
2132 "firmware forced it to 0x%08x\n",
2133 fwmask, hotkey_acpi_mask);
2136 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2137 hotkey_mask_warn_incomplete_mask();
2139 return rc;
2143 * Sets hotkey_user_mask and tries to set the firmware mask
2145 * Call with hotkey_mutex held
2147 static int hotkey_user_mask_set(const u32 mask)
2149 int rc;
2151 /* Give people a chance to notice they are doing something that
2152 * is bound to go boom on their users sooner or later */
2153 if (!tp_warned.hotkey_mask_ff &&
2154 (mask == 0xffff || mask == 0xffffff ||
2155 mask == 0xffffffff)) {
2156 tp_warned.hotkey_mask_ff = 1;
2157 pr_notice("setting the hotkey mask to 0x%08x is likely "
2158 "not the best way to go about it\n", mask);
2159 pr_notice("please consider using the driver defaults, "
2160 "and refer to up-to-date thinkpad-acpi "
2161 "documentation\n");
2164 /* Try to enable what the user asked for, plus whatever we need.
2165 * this syncs everything but won't enable bits in hotkey_user_mask */
2166 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2168 /* Enable the available bits in hotkey_user_mask */
2169 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2171 return rc;
2175 * Sets the driver hotkey mask.
2177 * Can be called even if the hotkey subdriver is inactive
2179 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2181 int rc;
2183 /* Do the right thing if hotkey_init has not been called yet */
2184 if (!tp_features.hotkey) {
2185 hotkey_driver_mask = mask;
2186 return 0;
2189 mutex_lock(&hotkey_mutex);
2191 HOTKEY_CONFIG_CRITICAL_START
2192 hotkey_driver_mask = mask;
2193 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2194 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2195 #endif
2196 HOTKEY_CONFIG_CRITICAL_END
2198 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2199 ~hotkey_source_mask);
2200 hotkey_poll_setup(true);
2202 mutex_unlock(&hotkey_mutex);
2204 return rc;
2207 static int hotkey_status_get(int *status)
2209 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2210 return -EIO;
2212 return 0;
2215 static int hotkey_status_set(bool enable)
2217 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2218 return -EIO;
2220 return 0;
2223 static void tpacpi_input_send_tabletsw(void)
2225 int state;
2227 if (tp_features.hotkey_tablet &&
2228 !hotkey_get_tablet_mode(&state)) {
2229 mutex_lock(&tpacpi_inputdev_send_mutex);
2231 input_report_switch(tpacpi_inputdev,
2232 SW_TABLET_MODE, !!state);
2233 input_sync(tpacpi_inputdev);
2235 mutex_unlock(&tpacpi_inputdev_send_mutex);
2239 /* Do NOT call without validating scancode first */
2240 static void tpacpi_input_send_key(const unsigned int scancode)
2242 const unsigned int keycode = hotkey_keycode_map[scancode];
2244 if (keycode != KEY_RESERVED) {
2245 mutex_lock(&tpacpi_inputdev_send_mutex);
2247 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2248 input_report_key(tpacpi_inputdev, keycode, 1);
2249 input_sync(tpacpi_inputdev);
2251 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2252 input_report_key(tpacpi_inputdev, keycode, 0);
2253 input_sync(tpacpi_inputdev);
2255 mutex_unlock(&tpacpi_inputdev_send_mutex);
2259 /* Do NOT call without validating scancode first */
2260 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2262 hotkey_driver_event(scancode);
2263 if (hotkey_user_mask & (1 << scancode))
2264 tpacpi_input_send_key(scancode);
2267 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2268 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2270 /* Do NOT call without validating scancode first */
2271 static void tpacpi_hotkey_send_key(unsigned int scancode)
2273 tpacpi_input_send_key_masked(scancode);
2274 if (hotkey_report_mode < 2) {
2275 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2276 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2280 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2282 u8 d;
2284 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2285 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2286 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2287 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2288 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2289 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2291 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2292 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2293 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2295 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2296 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2297 n->displayexp_toggle =
2298 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2300 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2301 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2302 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2303 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2304 n->brightness_toggle =
2305 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2307 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2308 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2309 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2310 >> TP_NVRAM_POS_LEVEL_VOLUME;
2311 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2312 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2316 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2317 struct tp_nvram_state *newn,
2318 const u32 event_mask)
2321 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2322 do { \
2323 if ((event_mask & (1 << __scancode)) && \
2324 oldn->__member != newn->__member) \
2325 tpacpi_hotkey_send_key(__scancode); \
2326 } while (0)
2328 #define TPACPI_MAY_SEND_KEY(__scancode) \
2329 do { \
2330 if (event_mask & (1 << __scancode)) \
2331 tpacpi_hotkey_send_key(__scancode); \
2332 } while (0)
2334 void issue_volchange(const unsigned int oldvol,
2335 const unsigned int newvol)
2337 unsigned int i = oldvol;
2339 while (i > newvol) {
2340 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2341 i--;
2343 while (i < newvol) {
2344 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2345 i++;
2349 void issue_brightnesschange(const unsigned int oldbrt,
2350 const unsigned int newbrt)
2352 unsigned int i = oldbrt;
2354 while (i > newbrt) {
2355 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2356 i--;
2358 while (i < newbrt) {
2359 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2360 i++;
2364 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2366 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2367 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2369 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2374 * Handle volume
2376 * This code is supposed to duplicate the IBM firmware behaviour:
2377 * - Pressing MUTE issues mute hotkey message, even when already mute
2378 * - Pressing Volume up/down issues volume up/down hotkey messages,
2379 * even when already at maximum or minimum volume
2380 * - The act of unmuting issues volume up/down notification,
2381 * depending which key was used to unmute
2383 * We are constrained to what the NVRAM can tell us, which is not much
2384 * and certainly not enough if more than one volume hotkey was pressed
2385 * since the last poll cycle.
2387 * Just to make our life interesting, some newer Lenovo ThinkPads have
2388 * bugs in the BIOS and may fail to update volume_toggle properly.
2390 if (newn->mute) {
2391 /* muted */
2392 if (!oldn->mute ||
2393 oldn->volume_toggle != newn->volume_toggle ||
2394 oldn->volume_level != newn->volume_level) {
2395 /* recently muted, or repeated mute keypress, or
2396 * multiple presses ending in mute */
2397 issue_volchange(oldn->volume_level, newn->volume_level);
2398 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2400 } else {
2401 /* unmute */
2402 if (oldn->mute) {
2403 /* recently unmuted, issue 'unmute' keypress */
2404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2406 if (oldn->volume_level != newn->volume_level) {
2407 issue_volchange(oldn->volume_level, newn->volume_level);
2408 } else if (oldn->volume_toggle != newn->volume_toggle) {
2409 /* repeated vol up/down keypress at end of scale ? */
2410 if (newn->volume_level == 0)
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2412 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2417 /* handle brightness */
2418 if (oldn->brightness_level != newn->brightness_level) {
2419 issue_brightnesschange(oldn->brightness_level,
2420 newn->brightness_level);
2421 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2422 /* repeated key presses that didn't change state */
2423 if (newn->brightness_level == 0)
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2425 else if (newn->brightness_level >= bright_maxlvl
2426 && !tp_features.bright_unkfw)
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2430 #undef TPACPI_COMPARE_KEY
2431 #undef TPACPI_MAY_SEND_KEY
2435 * Polling driver
2437 * We track all events in hotkey_source_mask all the time, since
2438 * most of them are edge-based. We only issue those requested by
2439 * hotkey_user_mask or hotkey_driver_mask, though.
2441 static int hotkey_kthread(void *data)
2443 struct tp_nvram_state s[2];
2444 u32 poll_mask, event_mask;
2445 unsigned int si, so;
2446 unsigned long t;
2447 unsigned int change_detector, must_reset;
2448 unsigned int poll_freq;
2450 mutex_lock(&hotkey_thread_mutex);
2452 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2453 goto exit;
2455 set_freezable();
2457 so = 0;
2458 si = 1;
2459 t = 0;
2461 /* Initial state for compares */
2462 mutex_lock(&hotkey_thread_data_mutex);
2463 change_detector = hotkey_config_change;
2464 poll_mask = hotkey_source_mask;
2465 event_mask = hotkey_source_mask &
2466 (hotkey_driver_mask | hotkey_user_mask);
2467 poll_freq = hotkey_poll_freq;
2468 mutex_unlock(&hotkey_thread_data_mutex);
2469 hotkey_read_nvram(&s[so], poll_mask);
2471 while (!kthread_should_stop()) {
2472 if (t == 0) {
2473 if (likely(poll_freq))
2474 t = 1000/poll_freq;
2475 else
2476 t = 100; /* should never happen... */
2478 t = msleep_interruptible(t);
2479 if (unlikely(kthread_should_stop()))
2480 break;
2481 must_reset = try_to_freeze();
2482 if (t > 0 && !must_reset)
2483 continue;
2485 mutex_lock(&hotkey_thread_data_mutex);
2486 if (must_reset || hotkey_config_change != change_detector) {
2487 /* forget old state on thaw or config change */
2488 si = so;
2489 t = 0;
2490 change_detector = hotkey_config_change;
2492 poll_mask = hotkey_source_mask;
2493 event_mask = hotkey_source_mask &
2494 (hotkey_driver_mask | hotkey_user_mask);
2495 poll_freq = hotkey_poll_freq;
2496 mutex_unlock(&hotkey_thread_data_mutex);
2498 if (likely(poll_mask)) {
2499 hotkey_read_nvram(&s[si], poll_mask);
2500 if (likely(si != so)) {
2501 hotkey_compare_and_issue_event(&s[so], &s[si],
2502 event_mask);
2506 so = si;
2507 si ^= 1;
2510 exit:
2511 mutex_unlock(&hotkey_thread_mutex);
2512 return 0;
2515 /* call with hotkey_mutex held */
2516 static void hotkey_poll_stop_sync(void)
2518 if (tpacpi_hotkey_task) {
2519 if (frozen(tpacpi_hotkey_task) ||
2520 freezing(tpacpi_hotkey_task))
2521 thaw_process(tpacpi_hotkey_task);
2523 kthread_stop(tpacpi_hotkey_task);
2524 tpacpi_hotkey_task = NULL;
2525 mutex_lock(&hotkey_thread_mutex);
2526 /* at this point, the thread did exit */
2527 mutex_unlock(&hotkey_thread_mutex);
2531 /* call with hotkey_mutex held */
2532 static void hotkey_poll_setup(const bool may_warn)
2534 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2535 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2537 if (hotkey_poll_freq > 0 &&
2538 (poll_driver_mask ||
2539 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2540 if (!tpacpi_hotkey_task) {
2541 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2542 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2543 if (IS_ERR(tpacpi_hotkey_task)) {
2544 tpacpi_hotkey_task = NULL;
2545 pr_err("could not create kernel thread "
2546 "for hotkey polling\n");
2549 } else {
2550 hotkey_poll_stop_sync();
2551 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2552 hotkey_poll_freq == 0) {
2553 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2554 "require polling, which is currently "
2555 "disabled\n",
2556 poll_user_mask, poll_driver_mask);
2561 static void hotkey_poll_setup_safe(const bool may_warn)
2563 mutex_lock(&hotkey_mutex);
2564 hotkey_poll_setup(may_warn);
2565 mutex_unlock(&hotkey_mutex);
2568 /* call with hotkey_mutex held */
2569 static void hotkey_poll_set_freq(unsigned int freq)
2571 if (!freq)
2572 hotkey_poll_stop_sync();
2574 hotkey_poll_freq = freq;
2577 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2579 static void hotkey_poll_setup(const bool __unused)
2583 static void hotkey_poll_setup_safe(const bool __unused)
2587 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2589 static int hotkey_inputdev_open(struct input_dev *dev)
2591 switch (tpacpi_lifecycle) {
2592 case TPACPI_LIFE_INIT:
2593 case TPACPI_LIFE_RUNNING:
2594 hotkey_poll_setup_safe(false);
2595 return 0;
2596 case TPACPI_LIFE_EXITING:
2597 return -EBUSY;
2600 /* Should only happen if tpacpi_lifecycle is corrupt */
2601 BUG();
2602 return -EBUSY;
2605 static void hotkey_inputdev_close(struct input_dev *dev)
2607 /* disable hotkey polling when possible */
2608 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2609 !(hotkey_source_mask & hotkey_driver_mask))
2610 hotkey_poll_setup_safe(false);
2613 /* sysfs hotkey enable ------------------------------------------------- */
2614 static ssize_t hotkey_enable_show(struct device *dev,
2615 struct device_attribute *attr,
2616 char *buf)
2618 int res, status;
2620 printk_deprecated_attribute("hotkey_enable",
2621 "Hotkey reporting is always enabled");
2623 res = hotkey_status_get(&status);
2624 if (res)
2625 return res;
2627 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2630 static ssize_t hotkey_enable_store(struct device *dev,
2631 struct device_attribute *attr,
2632 const char *buf, size_t count)
2634 unsigned long t;
2636 printk_deprecated_attribute("hotkey_enable",
2637 "Hotkeys can be disabled through hotkey_mask");
2639 if (parse_strtoul(buf, 1, &t))
2640 return -EINVAL;
2642 if (t == 0)
2643 return -EPERM;
2645 return count;
2648 static struct device_attribute dev_attr_hotkey_enable =
2649 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2650 hotkey_enable_show, hotkey_enable_store);
2652 /* sysfs hotkey mask --------------------------------------------------- */
2653 static ssize_t hotkey_mask_show(struct device *dev,
2654 struct device_attribute *attr,
2655 char *buf)
2657 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2660 static ssize_t hotkey_mask_store(struct device *dev,
2661 struct device_attribute *attr,
2662 const char *buf, size_t count)
2664 unsigned long t;
2665 int res;
2667 if (parse_strtoul(buf, 0xffffffffUL, &t))
2668 return -EINVAL;
2670 if (mutex_lock_killable(&hotkey_mutex))
2671 return -ERESTARTSYS;
2673 res = hotkey_user_mask_set(t);
2675 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2676 hotkey_poll_setup(true);
2677 #endif
2679 mutex_unlock(&hotkey_mutex);
2681 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2683 return (res) ? res : count;
2686 static struct device_attribute dev_attr_hotkey_mask =
2687 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2688 hotkey_mask_show, hotkey_mask_store);
2690 /* sysfs hotkey bios_enabled ------------------------------------------- */
2691 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2692 struct device_attribute *attr,
2693 char *buf)
2695 return sprintf(buf, "0\n");
2698 static struct device_attribute dev_attr_hotkey_bios_enabled =
2699 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2701 /* sysfs hotkey bios_mask ---------------------------------------------- */
2702 static ssize_t hotkey_bios_mask_show(struct device *dev,
2703 struct device_attribute *attr,
2704 char *buf)
2706 printk_deprecated_attribute("hotkey_bios_mask",
2707 "This attribute is useless.");
2708 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2711 static struct device_attribute dev_attr_hotkey_bios_mask =
2712 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2714 /* sysfs hotkey all_mask ----------------------------------------------- */
2715 static ssize_t hotkey_all_mask_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2719 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2720 hotkey_all_mask | hotkey_source_mask);
2723 static struct device_attribute dev_attr_hotkey_all_mask =
2724 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2726 /* sysfs hotkey recommended_mask --------------------------------------- */
2727 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2731 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2732 (hotkey_all_mask | hotkey_source_mask)
2733 & ~hotkey_reserved_mask);
2736 static struct device_attribute dev_attr_hotkey_recommended_mask =
2737 __ATTR(hotkey_recommended_mask, S_IRUGO,
2738 hotkey_recommended_mask_show, NULL);
2740 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2742 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2743 static ssize_t hotkey_source_mask_show(struct device *dev,
2744 struct device_attribute *attr,
2745 char *buf)
2747 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2750 static ssize_t hotkey_source_mask_store(struct device *dev,
2751 struct device_attribute *attr,
2752 const char *buf, size_t count)
2754 unsigned long t;
2755 u32 r_ev;
2756 int rc;
2758 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2759 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2760 return -EINVAL;
2762 if (mutex_lock_killable(&hotkey_mutex))
2763 return -ERESTARTSYS;
2765 HOTKEY_CONFIG_CRITICAL_START
2766 hotkey_source_mask = t;
2767 HOTKEY_CONFIG_CRITICAL_END
2769 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2770 ~hotkey_source_mask);
2771 hotkey_poll_setup(true);
2773 /* check if events needed by the driver got disabled */
2774 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2775 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2777 mutex_unlock(&hotkey_mutex);
2779 if (rc < 0)
2780 pr_err("hotkey_source_mask: "
2781 "failed to update the firmware event mask!\n");
2783 if (r_ev)
2784 pr_notice("hotkey_source_mask: "
2785 "some important events were disabled: 0x%04x\n",
2786 r_ev);
2788 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2790 return (rc < 0) ? rc : count;
2793 static struct device_attribute dev_attr_hotkey_source_mask =
2794 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2795 hotkey_source_mask_show, hotkey_source_mask_store);
2797 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2798 static ssize_t hotkey_poll_freq_show(struct device *dev,
2799 struct device_attribute *attr,
2800 char *buf)
2802 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2805 static ssize_t hotkey_poll_freq_store(struct device *dev,
2806 struct device_attribute *attr,
2807 const char *buf, size_t count)
2809 unsigned long t;
2811 if (parse_strtoul(buf, 25, &t))
2812 return -EINVAL;
2814 if (mutex_lock_killable(&hotkey_mutex))
2815 return -ERESTARTSYS;
2817 hotkey_poll_set_freq(t);
2818 hotkey_poll_setup(true);
2820 mutex_unlock(&hotkey_mutex);
2822 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2824 return count;
2827 static struct device_attribute dev_attr_hotkey_poll_freq =
2828 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2829 hotkey_poll_freq_show, hotkey_poll_freq_store);
2831 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2833 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2834 static ssize_t hotkey_radio_sw_show(struct device *dev,
2835 struct device_attribute *attr,
2836 char *buf)
2838 int res;
2839 res = hotkey_get_wlsw();
2840 if (res < 0)
2841 return res;
2843 /* Opportunistic update */
2844 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2846 return snprintf(buf, PAGE_SIZE, "%d\n",
2847 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2850 static struct device_attribute dev_attr_hotkey_radio_sw =
2851 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2853 static void hotkey_radio_sw_notify_change(void)
2855 if (tp_features.hotkey_wlsw)
2856 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2857 "hotkey_radio_sw");
2860 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2861 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2862 struct device_attribute *attr,
2863 char *buf)
2865 int res, s;
2866 res = hotkey_get_tablet_mode(&s);
2867 if (res < 0)
2868 return res;
2870 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2873 static struct device_attribute dev_attr_hotkey_tablet_mode =
2874 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2876 static void hotkey_tablet_mode_notify_change(void)
2878 if (tp_features.hotkey_tablet)
2879 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2880 "hotkey_tablet_mode");
2883 /* sysfs hotkey report_mode -------------------------------------------- */
2884 static ssize_t hotkey_report_mode_show(struct device *dev,
2885 struct device_attribute *attr,
2886 char *buf)
2888 return snprintf(buf, PAGE_SIZE, "%d\n",
2889 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2892 static struct device_attribute dev_attr_hotkey_report_mode =
2893 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2895 /* sysfs wakeup reason (pollable) -------------------------------------- */
2896 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2897 struct device_attribute *attr,
2898 char *buf)
2900 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2903 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2904 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2906 static void hotkey_wakeup_reason_notify_change(void)
2908 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2909 "wakeup_reason");
2912 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2913 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2914 struct device_attribute *attr,
2915 char *buf)
2917 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2920 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2921 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2922 hotkey_wakeup_hotunplug_complete_show, NULL);
2924 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2926 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2927 "wakeup_hotunplug_complete");
2930 /* --------------------------------------------------------------------- */
2932 static struct attribute *hotkey_attributes[] __initdata = {
2933 &dev_attr_hotkey_enable.attr,
2934 &dev_attr_hotkey_bios_enabled.attr,
2935 &dev_attr_hotkey_bios_mask.attr,
2936 &dev_attr_hotkey_report_mode.attr,
2937 &dev_attr_hotkey_wakeup_reason.attr,
2938 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2939 &dev_attr_hotkey_mask.attr,
2940 &dev_attr_hotkey_all_mask.attr,
2941 &dev_attr_hotkey_recommended_mask.attr,
2942 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2943 &dev_attr_hotkey_source_mask.attr,
2944 &dev_attr_hotkey_poll_freq.attr,
2945 #endif
2949 * Sync both the hw and sw blocking state of all switches
2951 static void tpacpi_send_radiosw_update(void)
2953 int wlsw;
2956 * We must sync all rfkill controllers *before* issuing any
2957 * rfkill input events, or we will race the rfkill core input
2958 * handler.
2960 * tpacpi_inputdev_send_mutex works as a synchronization point
2961 * for the above.
2963 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2966 wlsw = hotkey_get_wlsw();
2968 /* Sync hw blocking state first if it is hw-blocked */
2969 if (wlsw == TPACPI_RFK_RADIO_OFF)
2970 tpacpi_rfk_update_hwblock_state(true);
2972 /* Sync sw blocking state */
2973 tpacpi_rfk_update_swstate_all();
2975 /* Sync hw blocking state last if it is hw-unblocked */
2976 if (wlsw == TPACPI_RFK_RADIO_ON)
2977 tpacpi_rfk_update_hwblock_state(false);
2979 /* Issue rfkill input event for WLSW switch */
2980 if (!(wlsw < 0)) {
2981 mutex_lock(&tpacpi_inputdev_send_mutex);
2983 input_report_switch(tpacpi_inputdev,
2984 SW_RFKILL_ALL, (wlsw > 0));
2985 input_sync(tpacpi_inputdev);
2987 mutex_unlock(&tpacpi_inputdev_send_mutex);
2991 * this can be unconditional, as we will poll state again
2992 * if userspace uses the notify to read data
2994 hotkey_radio_sw_notify_change();
2997 static void hotkey_exit(void)
2999 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3000 mutex_lock(&hotkey_mutex);
3001 hotkey_poll_stop_sync();
3002 mutex_unlock(&hotkey_mutex);
3003 #endif
3005 if (hotkey_dev_attributes)
3006 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3008 kfree(hotkey_keycode_map);
3010 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3011 "restoring original HKEY status and mask\n");
3012 /* yes, there is a bitwise or below, we want the
3013 * functions to be called even if one of them fail */
3014 if (((tp_features.hotkey_mask &&
3015 hotkey_mask_set(hotkey_orig_mask)) |
3016 hotkey_status_set(false)) != 0)
3017 pr_err("failed to restore hot key mask "
3018 "to BIOS defaults\n");
3021 static void __init hotkey_unmap(const unsigned int scancode)
3023 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3024 clear_bit(hotkey_keycode_map[scancode],
3025 tpacpi_inputdev->keybit);
3026 hotkey_keycode_map[scancode] = KEY_RESERVED;
3031 * HKEY quirks:
3032 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3035 #define TPACPI_HK_Q_INIMASK 0x0001
3037 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3038 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3039 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3040 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3041 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3042 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3043 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3044 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3045 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3046 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3047 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3048 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3049 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3050 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3051 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3052 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3053 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3054 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3055 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3056 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3059 typedef u16 tpacpi_keymap_entry_t;
3060 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3062 static int __init hotkey_init(struct ibm_init_struct *iibm)
3064 /* Requirements for changing the default keymaps:
3066 * 1. Many of the keys are mapped to KEY_RESERVED for very
3067 * good reasons. Do not change them unless you have deep
3068 * knowledge on the IBM and Lenovo ThinkPad firmware for
3069 * the various ThinkPad models. The driver behaves
3070 * differently for KEY_RESERVED: such keys have their
3071 * hot key mask *unset* in mask_recommended, and also
3072 * in the initial hot key mask programmed into the
3073 * firmware at driver load time, which means the firm-
3074 * ware may react very differently if you change them to
3075 * something else;
3077 * 2. You must be subscribed to the linux-thinkpad and
3078 * ibm-acpi-devel mailing lists, and you should read the
3079 * list archives since 2007 if you want to change the
3080 * keymaps. This requirement exists so that you will
3081 * know the past history of problems with the thinkpad-
3082 * acpi driver keymaps, and also that you will be
3083 * listening to any bug reports;
3085 * 3. Do not send thinkpad-acpi specific patches directly to
3086 * for merging, *ever*. Send them to the linux-acpi
3087 * mailinglist for comments. Merging is to be done only
3088 * through acpi-test and the ACPI maintainer.
3090 * If the above is too much to ask, don't change the keymap.
3091 * Ask the thinkpad-acpi maintainer to do it, instead.
3094 enum keymap_index {
3095 TPACPI_KEYMAP_IBM_GENERIC = 0,
3096 TPACPI_KEYMAP_LENOVO_GENERIC,
3099 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3100 /* Generic keymap for IBM ThinkPads */
3101 [TPACPI_KEYMAP_IBM_GENERIC] = {
3102 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3103 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3104 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3105 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3107 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3108 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3109 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3110 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3112 /* brightness: firmware always reacts to them */
3113 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3114 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3116 /* Thinklight: firmware always react to it */
3117 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3119 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3120 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3122 /* Volume: firmware always react to it and reprograms
3123 * the built-in *extra* mixer. Never map it to control
3124 * another mixer by default. */
3125 KEY_RESERVED, /* 0x14: VOLUME UP */
3126 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3127 KEY_RESERVED, /* 0x16: MUTE */
3129 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3131 /* (assignments unknown, please report if found) */
3132 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3133 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3136 /* Generic keymap for Lenovo ThinkPads */
3137 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3138 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3139 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3140 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3141 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3143 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3144 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3145 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3146 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3148 /* These should be enabled --only-- when ACPI video
3149 * is disabled (i.e. in "vendor" mode), and are handled
3150 * in a special way by the init code */
3151 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3152 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3154 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3156 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3157 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3159 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3160 * react to it and reprograms the built-in *extra* mixer.
3161 * Never map it to control another mixer by default.
3163 * T60?, T61, R60?, R61: firmware and EC tries to send
3164 * these over the regular keyboard, so these are no-ops,
3165 * but there are still weird bugs re. MUTE, so do not
3166 * change unless you get test reports from all Lenovo
3167 * models. May cause the BIOS to interfere with the
3168 * HDA mixer.
3170 KEY_RESERVED, /* 0x14: VOLUME UP */
3171 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3172 KEY_RESERVED, /* 0x16: MUTE */
3174 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3176 /* (assignments unknown, please report if found) */
3177 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3178 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3182 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3183 /* Generic maps (fallback) */
3185 .vendor = PCI_VENDOR_ID_IBM,
3186 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3187 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3190 .vendor = PCI_VENDOR_ID_LENOVO,
3191 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3192 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3196 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3197 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3199 int res, i;
3200 int status;
3201 int hkeyv;
3202 bool radiosw_state = false;
3203 bool tabletsw_state = false;
3205 unsigned long quirks;
3206 unsigned long keymap_id;
3208 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3209 "initializing hotkey subdriver\n");
3211 BUG_ON(!tpacpi_inputdev);
3212 BUG_ON(tpacpi_inputdev->open != NULL ||
3213 tpacpi_inputdev->close != NULL);
3215 TPACPI_ACPIHANDLE_INIT(hkey);
3216 mutex_init(&hotkey_mutex);
3218 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3219 mutex_init(&hotkey_thread_mutex);
3220 mutex_init(&hotkey_thread_data_mutex);
3221 #endif
3223 /* hotkey not supported on 570 */
3224 tp_features.hotkey = hkey_handle != NULL;
3226 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3227 "hotkeys are %s\n",
3228 str_supported(tp_features.hotkey));
3230 if (!tp_features.hotkey)
3231 return 1;
3233 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3234 ARRAY_SIZE(tpacpi_hotkey_qtable));
3236 tpacpi_disable_brightness_delay();
3238 /* MUST have enough space for all attributes to be added to
3239 * hotkey_dev_attributes */
3240 hotkey_dev_attributes = create_attr_set(
3241 ARRAY_SIZE(hotkey_attributes) + 2,
3242 NULL);
3243 if (!hotkey_dev_attributes)
3244 return -ENOMEM;
3245 res = add_many_to_attr_set(hotkey_dev_attributes,
3246 hotkey_attributes,
3247 ARRAY_SIZE(hotkey_attributes));
3248 if (res)
3249 goto err_exit;
3251 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3252 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3253 for HKEY interface version 0x100 */
3254 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3255 if ((hkeyv >> 8) != 1) {
3256 pr_err("unknown version of the HKEY interface: 0x%x\n",
3257 hkeyv);
3258 pr_err("please report this to %s\n", TPACPI_MAIL);
3259 } else {
3261 * MHKV 0x100 in A31, R40, R40e,
3262 * T4x, X31, and later
3264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "firmware HKEY interface version: 0x%x\n",
3266 hkeyv);
3268 /* Paranoia check AND init hotkey_all_mask */
3269 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3270 "MHKA", "qd")) {
3271 pr_err("missing MHKA handler, "
3272 "please report this to %s\n",
3273 TPACPI_MAIL);
3274 /* Fallback: pre-init for FN+F3,F4,F12 */
3275 hotkey_all_mask = 0x080cU;
3276 } else {
3277 tp_features.hotkey_mask = 1;
3282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3283 "hotkey masks are %s\n",
3284 str_supported(tp_features.hotkey_mask));
3286 /* Init hotkey_all_mask if not initialized yet */
3287 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3288 (quirks & TPACPI_HK_Q_INIMASK))
3289 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3291 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3292 if (tp_features.hotkey_mask) {
3293 /* hotkey_source_mask *must* be zero for
3294 * the first hotkey_mask_get to return hotkey_orig_mask */
3295 res = hotkey_mask_get();
3296 if (res)
3297 goto err_exit;
3299 hotkey_orig_mask = hotkey_acpi_mask;
3300 } else {
3301 hotkey_orig_mask = hotkey_all_mask;
3302 hotkey_acpi_mask = hotkey_all_mask;
3305 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3306 if (dbg_wlswemul) {
3307 tp_features.hotkey_wlsw = 1;
3308 radiosw_state = !!tpacpi_wlsw_emulstate;
3309 pr_info("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 pr_info("radio switch found; radios are %s\n",
3317 enabled(status, 0));
3319 if (tp_features.hotkey_wlsw)
3320 res = add_to_attr_set(hotkey_dev_attributes,
3321 &dev_attr_hotkey_radio_sw.attr);
3323 /* For X41t, X60t, X61t Tablets... */
3324 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3325 tp_features.hotkey_tablet = 1;
3326 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3327 pr_info("possible tablet mode switch found; "
3328 "ThinkPad in %s mode\n",
3329 (tabletsw_state) ? "tablet" : "laptop");
3330 res = add_to_attr_set(hotkey_dev_attributes,
3331 &dev_attr_hotkey_tablet_mode.attr);
3334 if (!res)
3335 res = register_attr_set_with_sysfs(
3336 hotkey_dev_attributes,
3337 &tpacpi_pdev->dev.kobj);
3338 if (res)
3339 goto err_exit;
3341 /* Set up key map */
3342 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3343 GFP_KERNEL);
3344 if (!hotkey_keycode_map) {
3345 pr_err("failed to allocate memory for key map\n");
3346 res = -ENOMEM;
3347 goto err_exit;
3350 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3351 ARRAY_SIZE(tpacpi_keymap_qtable));
3352 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3353 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3354 "using keymap number %lu\n", keymap_id);
3356 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3357 TPACPI_HOTKEY_MAP_SIZE);
3359 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3360 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3361 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3362 tpacpi_inputdev->keycode = hotkey_keycode_map;
3363 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3364 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3365 input_set_capability(tpacpi_inputdev, EV_KEY,
3366 hotkey_keycode_map[i]);
3367 } else {
3368 if (i < sizeof(hotkey_reserved_mask)*8)
3369 hotkey_reserved_mask |= 1 << i;
3373 if (tp_features.hotkey_wlsw) {
3374 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3375 input_report_switch(tpacpi_inputdev,
3376 SW_RFKILL_ALL, radiosw_state);
3378 if (tp_features.hotkey_tablet) {
3379 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3380 input_report_switch(tpacpi_inputdev,
3381 SW_TABLET_MODE, tabletsw_state);
3384 /* Do not issue duplicate brightness change events to
3385 * userspace. tpacpi_detect_brightness_capabilities() must have
3386 * been called before this point */
3387 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3388 pr_info("This ThinkPad has standard ACPI backlight "
3389 "brightness control, supported by the ACPI "
3390 "video driver\n");
3391 pr_notice("Disabling thinkpad-acpi brightness events "
3392 "by default...\n");
3394 /* Disable brightness up/down on Lenovo thinkpads when
3395 * ACPI is handling them, otherwise it is plain impossible
3396 * for userspace to do something even remotely sane */
3397 hotkey_reserved_mask |=
3398 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3399 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3400 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3401 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3404 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3405 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3406 & ~hotkey_all_mask
3407 & ~hotkey_reserved_mask;
3409 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3410 "hotkey source mask 0x%08x, polling freq %u\n",
3411 hotkey_source_mask, hotkey_poll_freq);
3412 #endif
3414 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3415 "enabling firmware HKEY event interface...\n");
3416 res = hotkey_status_set(true);
3417 if (res) {
3418 hotkey_exit();
3419 return res;
3421 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3422 | hotkey_driver_mask)
3423 & ~hotkey_source_mask);
3424 if (res < 0 && res != -ENXIO) {
3425 hotkey_exit();
3426 return res;
3428 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3429 & ~hotkey_reserved_mask;
3430 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3431 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3432 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3434 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3435 "legacy ibm/hotkey event reporting over procfs %s\n",
3436 (hotkey_report_mode < 2) ?
3437 "enabled" : "disabled");
3439 tpacpi_inputdev->open = &hotkey_inputdev_open;
3440 tpacpi_inputdev->close = &hotkey_inputdev_close;
3442 hotkey_poll_setup_safe(true);
3444 return 0;
3446 err_exit:
3447 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3448 hotkey_dev_attributes = NULL;
3450 return (res < 0)? res : 1;
3453 static bool hotkey_notify_hotkey(const u32 hkey,
3454 bool *send_acpi_ev,
3455 bool *ignore_acpi_ev)
3457 /* 0x1000-0x1FFF: key presses */
3458 unsigned int scancode = hkey & 0xfff;
3459 *send_acpi_ev = true;
3460 *ignore_acpi_ev = false;
3462 /* HKEY event 0x1001 is scancode 0x00 */
3463 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3464 scancode--;
3465 if (!(hotkey_source_mask & (1 << scancode))) {
3466 tpacpi_input_send_key_masked(scancode);
3467 *send_acpi_ev = false;
3468 } else {
3469 *ignore_acpi_ev = true;
3471 return true;
3473 return false;
3476 static bool hotkey_notify_wakeup(const u32 hkey,
3477 bool *send_acpi_ev,
3478 bool *ignore_acpi_ev)
3480 /* 0x2000-0x2FFF: Wakeup reason */
3481 *send_acpi_ev = true;
3482 *ignore_acpi_ev = false;
3484 switch (hkey) {
3485 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3486 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3487 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3488 *ignore_acpi_ev = true;
3489 break;
3491 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3492 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3493 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3494 *ignore_acpi_ev = true;
3495 break;
3497 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3498 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3499 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3500 /* how to auto-heal: */
3501 /* 2313: woke up from S3, go to S4/S5 */
3502 /* 2413: woke up from S4, go to S5 */
3503 break;
3505 default:
3506 return false;
3509 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3510 pr_info("woke up due to a hot-unplug request...\n");
3511 hotkey_wakeup_reason_notify_change();
3513 return true;
3516 static bool hotkey_notify_usrevent(const u32 hkey,
3517 bool *send_acpi_ev,
3518 bool *ignore_acpi_ev)
3520 /* 0x5000-0x5FFF: human interface helpers */
3521 *send_acpi_ev = true;
3522 *ignore_acpi_ev = false;
3524 switch (hkey) {
3525 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3526 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3527 return true;
3529 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3530 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3531 tpacpi_input_send_tabletsw();
3532 hotkey_tablet_mode_notify_change();
3533 *send_acpi_ev = false;
3534 return true;
3536 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3537 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3538 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3539 /* do not propagate these events */
3540 *ignore_acpi_ev = true;
3541 return true;
3543 default:
3544 return false;
3548 static void thermal_dump_all_sensors(void);
3550 static bool hotkey_notify_thermal(const u32 hkey,
3551 bool *send_acpi_ev,
3552 bool *ignore_acpi_ev)
3554 bool known = true;
3556 /* 0x6000-0x6FFF: thermal alarms */
3557 *send_acpi_ev = true;
3558 *ignore_acpi_ev = false;
3560 switch (hkey) {
3561 case TP_HKEY_EV_THM_TABLE_CHANGED:
3562 pr_info("EC reports that Thermal Table has changed\n");
3563 /* recommended action: do nothing, we don't have
3564 * Lenovo ATM information */
3565 return true;
3566 case TP_HKEY_EV_ALARM_BAT_HOT:
3567 pr_crit("THERMAL ALARM: battery is too hot!\n");
3568 /* recommended action: warn user through gui */
3569 break;
3570 case TP_HKEY_EV_ALARM_BAT_XHOT:
3571 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3572 /* recommended action: immediate sleep/hibernate */
3573 break;
3574 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3575 pr_crit("THERMAL ALARM: "
3576 "a sensor reports something is too hot!\n");
3577 /* recommended action: warn user through gui, that */
3578 /* some internal component is too hot */
3579 break;
3580 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3581 pr_alert("THERMAL EMERGENCY: "
3582 "a sensor reports something is extremely hot!\n");
3583 /* recommended action: immediate sleep/hibernate */
3584 break;
3585 default:
3586 pr_alert("THERMAL ALERT: unknown thermal alarm received\n");
3587 known = false;
3590 thermal_dump_all_sensors();
3592 return known;
3595 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3597 u32 hkey;
3598 bool send_acpi_ev;
3599 bool ignore_acpi_ev;
3600 bool known_ev;
3602 if (event != 0x80) {
3603 pr_err("unknown HKEY notification event %d\n", event);
3604 /* forward it to userspace, maybe it knows how to handle it */
3605 acpi_bus_generate_netlink_event(
3606 ibm->acpi->device->pnp.device_class,
3607 dev_name(&ibm->acpi->device->dev),
3608 event, 0);
3609 return;
3612 while (1) {
3613 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3614 pr_err("failed to retrieve HKEY event\n");
3615 return;
3618 if (hkey == 0) {
3619 /* queue empty */
3620 return;
3623 send_acpi_ev = true;
3624 ignore_acpi_ev = false;
3626 switch (hkey >> 12) {
3627 case 1:
3628 /* 0x1000-0x1FFF: key presses */
3629 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3630 &ignore_acpi_ev);
3631 break;
3632 case 2:
3633 /* 0x2000-0x2FFF: Wakeup reason */
3634 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3635 &ignore_acpi_ev);
3636 break;
3637 case 3:
3638 /* 0x3000-0x3FFF: bay-related wakeups */
3639 switch (hkey) {
3640 case TP_HKEY_EV_BAYEJ_ACK:
3641 hotkey_autosleep_ack = 1;
3642 pr_info("bay ejected\n");
3643 hotkey_wakeup_hotunplug_complete_notify_change();
3644 known_ev = true;
3645 break;
3646 case TP_HKEY_EV_OPTDRV_EJ:
3647 /* FIXME: kick libata if SATA link offline */
3648 known_ev = true;
3649 break;
3650 default:
3651 known_ev = false;
3653 break;
3654 case 4:
3655 /* 0x4000-0x4FFF: dock-related wakeups */
3656 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3657 hotkey_autosleep_ack = 1;
3658 pr_info("undocked\n");
3659 hotkey_wakeup_hotunplug_complete_notify_change();
3660 known_ev = true;
3661 } else {
3662 known_ev = false;
3664 break;
3665 case 5:
3666 /* 0x5000-0x5FFF: human interface helpers */
3667 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3668 &ignore_acpi_ev);
3669 break;
3670 case 6:
3671 /* 0x6000-0x6FFF: thermal alarms */
3672 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3673 &ignore_acpi_ev);
3674 break;
3675 case 7:
3676 /* 0x7000-0x7FFF: misc */
3677 if (tp_features.hotkey_wlsw &&
3678 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3679 tpacpi_send_radiosw_update();
3680 send_acpi_ev = 0;
3681 known_ev = true;
3682 break;
3684 /* fallthrough to default */
3685 default:
3686 known_ev = false;
3688 if (!known_ev) {
3689 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3690 pr_notice("please report the conditions when this "
3691 "event happened to %s\n", TPACPI_MAIL);
3694 /* Legacy events */
3695 if (!ignore_acpi_ev &&
3696 (send_acpi_ev || hotkey_report_mode < 2)) {
3697 acpi_bus_generate_proc_event(ibm->acpi->device,
3698 event, hkey);
3701 /* netlink events */
3702 if (!ignore_acpi_ev && send_acpi_ev) {
3703 acpi_bus_generate_netlink_event(
3704 ibm->acpi->device->pnp.device_class,
3705 dev_name(&ibm->acpi->device->dev),
3706 event, hkey);
3711 static void hotkey_suspend(pm_message_t state)
3713 /* Do these on suspend, we get the events on early resume! */
3714 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3715 hotkey_autosleep_ack = 0;
3718 static void hotkey_resume(void)
3720 tpacpi_disable_brightness_delay();
3722 if (hotkey_status_set(true) < 0 ||
3723 hotkey_mask_set(hotkey_acpi_mask) < 0)
3724 pr_err("error while attempting to reset the event "
3725 "firmware interface\n");
3727 tpacpi_send_radiosw_update();
3728 hotkey_tablet_mode_notify_change();
3729 hotkey_wakeup_reason_notify_change();
3730 hotkey_wakeup_hotunplug_complete_notify_change();
3731 hotkey_poll_setup_safe(false);
3734 /* procfs -------------------------------------------------------------- */
3735 static int hotkey_read(struct seq_file *m)
3737 int res, status;
3739 if (!tp_features.hotkey) {
3740 seq_printf(m, "status:\t\tnot supported\n");
3741 return 0;
3744 if (mutex_lock_killable(&hotkey_mutex))
3745 return -ERESTARTSYS;
3746 res = hotkey_status_get(&status);
3747 if (!res)
3748 res = hotkey_mask_get();
3749 mutex_unlock(&hotkey_mutex);
3750 if (res)
3751 return res;
3753 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3754 if (hotkey_all_mask) {
3755 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3756 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3757 } else {
3758 seq_printf(m, "mask:\t\tnot supported\n");
3759 seq_printf(m, "commands:\tenable, disable, reset\n");
3762 return 0;
3765 static void hotkey_enabledisable_warn(bool enable)
3767 tpacpi_log_usertask("procfs hotkey enable/disable");
3768 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3769 pr_fmt("hotkey enable/disable functionality has been "
3770 "removed from the driver. "
3771 "Hotkeys are always enabled.\n")))
3772 pr_err("Please remove the hotkey=enable module "
3773 "parameter, it is deprecated. "
3774 "Hotkeys are always enabled.\n");
3777 static int hotkey_write(char *buf)
3779 int res;
3780 u32 mask;
3781 char *cmd;
3783 if (!tp_features.hotkey)
3784 return -ENODEV;
3786 if (mutex_lock_killable(&hotkey_mutex))
3787 return -ERESTARTSYS;
3789 mask = hotkey_user_mask;
3791 res = 0;
3792 while ((cmd = next_cmd(&buf))) {
3793 if (strlencmp(cmd, "enable") == 0) {
3794 hotkey_enabledisable_warn(1);
3795 } else if (strlencmp(cmd, "disable") == 0) {
3796 hotkey_enabledisable_warn(0);
3797 res = -EPERM;
3798 } else if (strlencmp(cmd, "reset") == 0) {
3799 mask = (hotkey_all_mask | hotkey_source_mask)
3800 & ~hotkey_reserved_mask;
3801 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3802 /* mask set */
3803 } else if (sscanf(cmd, "%x", &mask) == 1) {
3804 /* mask set */
3805 } else {
3806 res = -EINVAL;
3807 goto errexit;
3811 if (!res) {
3812 tpacpi_disclose_usertask("procfs hotkey",
3813 "set mask to 0x%08x\n", mask);
3814 res = hotkey_user_mask_set(mask);
3817 errexit:
3818 mutex_unlock(&hotkey_mutex);
3819 return res;
3822 static const struct acpi_device_id ibm_htk_device_ids[] = {
3823 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3824 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3825 {"", 0},
3828 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3829 .hid = ibm_htk_device_ids,
3830 .notify = hotkey_notify,
3831 .handle = &hkey_handle,
3832 .type = ACPI_DEVICE_NOTIFY,
3835 static struct ibm_struct hotkey_driver_data = {
3836 .name = "hotkey",
3837 .read = hotkey_read,
3838 .write = hotkey_write,
3839 .exit = hotkey_exit,
3840 .resume = hotkey_resume,
3841 .suspend = hotkey_suspend,
3842 .acpi = &ibm_hotkey_acpidriver,
3845 /*************************************************************************
3846 * Bluetooth subdriver
3849 enum {
3850 /* ACPI GBDC/SBDC bits */
3851 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3852 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3853 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3854 0 = disable, 1 = enable */
3857 enum {
3858 /* ACPI \BLTH commands */
3859 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3860 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3861 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3862 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3863 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3866 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3868 static int bluetooth_get_status(void)
3870 int status;
3872 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3873 if (dbg_bluetoothemul)
3874 return (tpacpi_bluetooth_emulstate) ?
3875 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3876 #endif
3878 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3879 return -EIO;
3881 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3882 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3885 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3887 int status;
3889 vdbg_printk(TPACPI_DBG_RFKILL,
3890 "will attempt to %s bluetooth\n",
3891 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3893 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3894 if (dbg_bluetoothemul) {
3895 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3896 return 0;
3898 #endif
3900 if (state == TPACPI_RFK_RADIO_ON)
3901 status = TP_ACPI_BLUETOOTH_RADIOSSW
3902 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3903 else
3904 status = 0;
3906 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3907 return -EIO;
3909 return 0;
3912 /* sysfs bluetooth enable ---------------------------------------------- */
3913 static ssize_t bluetooth_enable_show(struct device *dev,
3914 struct device_attribute *attr,
3915 char *buf)
3917 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3918 attr, buf);
3921 static ssize_t bluetooth_enable_store(struct device *dev,
3922 struct device_attribute *attr,
3923 const char *buf, size_t count)
3925 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3926 attr, buf, count);
3929 static struct device_attribute dev_attr_bluetooth_enable =
3930 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3931 bluetooth_enable_show, bluetooth_enable_store);
3933 /* --------------------------------------------------------------------- */
3935 static struct attribute *bluetooth_attributes[] = {
3936 &dev_attr_bluetooth_enable.attr,
3937 NULL
3940 static const struct attribute_group bluetooth_attr_group = {
3941 .attrs = bluetooth_attributes,
3944 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3945 .get_status = bluetooth_get_status,
3946 .set_status = bluetooth_set_status,
3949 static void bluetooth_shutdown(void)
3951 /* Order firmware to save current state to NVRAM */
3952 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3953 TP_ACPI_BLTH_SAVE_STATE))
3954 pr_notice("failed to save bluetooth state to NVRAM\n");
3955 else
3956 vdbg_printk(TPACPI_DBG_RFKILL,
3957 "bluestooth state saved to NVRAM\n");
3960 static void bluetooth_exit(void)
3962 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3963 &bluetooth_attr_group);
3965 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3967 bluetooth_shutdown();
3970 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3972 int res;
3973 int status = 0;
3975 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3976 "initializing bluetooth subdriver\n");
3978 TPACPI_ACPIHANDLE_INIT(hkey);
3980 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3981 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3982 tp_features.bluetooth = hkey_handle &&
3983 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3985 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3986 "bluetooth is %s, status 0x%02x\n",
3987 str_supported(tp_features.bluetooth),
3988 status);
3990 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3991 if (dbg_bluetoothemul) {
3992 tp_features.bluetooth = 1;
3993 pr_info("bluetooth switch emulation enabled\n");
3994 } else
3995 #endif
3996 if (tp_features.bluetooth &&
3997 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3998 /* no bluetooth hardware present in system */
3999 tp_features.bluetooth = 0;
4000 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4001 "bluetooth hardware not installed\n");
4004 if (!tp_features.bluetooth)
4005 return 1;
4007 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4008 &bluetooth_tprfk_ops,
4009 RFKILL_TYPE_BLUETOOTH,
4010 TPACPI_RFK_BLUETOOTH_SW_NAME,
4011 true);
4012 if (res)
4013 return res;
4015 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4016 &bluetooth_attr_group);
4017 if (res) {
4018 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4019 return res;
4022 return 0;
4025 /* procfs -------------------------------------------------------------- */
4026 static int bluetooth_read(struct seq_file *m)
4028 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4031 static int bluetooth_write(char *buf)
4033 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4036 static struct ibm_struct bluetooth_driver_data = {
4037 .name = "bluetooth",
4038 .read = bluetooth_read,
4039 .write = bluetooth_write,
4040 .exit = bluetooth_exit,
4041 .shutdown = bluetooth_shutdown,
4044 /*************************************************************************
4045 * Wan subdriver
4048 enum {
4049 /* ACPI GWAN/SWAN bits */
4050 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4051 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4052 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4053 0 = disable, 1 = enable */
4056 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4058 static int wan_get_status(void)
4060 int status;
4062 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4063 if (dbg_wwanemul)
4064 return (tpacpi_wwan_emulstate) ?
4065 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4066 #endif
4068 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4069 return -EIO;
4071 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4072 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4075 static int wan_set_status(enum tpacpi_rfkill_state state)
4077 int status;
4079 vdbg_printk(TPACPI_DBG_RFKILL,
4080 "will attempt to %s wwan\n",
4081 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4083 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4084 if (dbg_wwanemul) {
4085 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4086 return 0;
4088 #endif
4090 if (state == TPACPI_RFK_RADIO_ON)
4091 status = TP_ACPI_WANCARD_RADIOSSW
4092 | TP_ACPI_WANCARD_RESUMECTRL;
4093 else
4094 status = 0;
4096 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4097 return -EIO;
4099 return 0;
4102 /* sysfs wan enable ---------------------------------------------------- */
4103 static ssize_t wan_enable_show(struct device *dev,
4104 struct device_attribute *attr,
4105 char *buf)
4107 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4108 attr, buf);
4111 static ssize_t wan_enable_store(struct device *dev,
4112 struct device_attribute *attr,
4113 const char *buf, size_t count)
4115 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4116 attr, buf, count);
4119 static struct device_attribute dev_attr_wan_enable =
4120 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4121 wan_enable_show, wan_enable_store);
4123 /* --------------------------------------------------------------------- */
4125 static struct attribute *wan_attributes[] = {
4126 &dev_attr_wan_enable.attr,
4127 NULL
4130 static const struct attribute_group wan_attr_group = {
4131 .attrs = wan_attributes,
4134 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4135 .get_status = wan_get_status,
4136 .set_status = wan_set_status,
4139 static void wan_shutdown(void)
4141 /* Order firmware to save current state to NVRAM */
4142 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4143 TP_ACPI_WGSV_SAVE_STATE))
4144 pr_notice("failed to save WWAN state to NVRAM\n");
4145 else
4146 vdbg_printk(TPACPI_DBG_RFKILL,
4147 "WWAN state saved to NVRAM\n");
4150 static void wan_exit(void)
4152 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4153 &wan_attr_group);
4155 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4157 wan_shutdown();
4160 static int __init wan_init(struct ibm_init_struct *iibm)
4162 int res;
4163 int status = 0;
4165 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4166 "initializing wan subdriver\n");
4168 TPACPI_ACPIHANDLE_INIT(hkey);
4170 tp_features.wan = hkey_handle &&
4171 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4173 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4174 "wan is %s, status 0x%02x\n",
4175 str_supported(tp_features.wan),
4176 status);
4178 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4179 if (dbg_wwanemul) {
4180 tp_features.wan = 1;
4181 pr_info("wwan switch emulation enabled\n");
4182 } else
4183 #endif
4184 if (tp_features.wan &&
4185 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4186 /* no wan hardware present in system */
4187 tp_features.wan = 0;
4188 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4189 "wan hardware not installed\n");
4192 if (!tp_features.wan)
4193 return 1;
4195 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4196 &wan_tprfk_ops,
4197 RFKILL_TYPE_WWAN,
4198 TPACPI_RFK_WWAN_SW_NAME,
4199 true);
4200 if (res)
4201 return res;
4203 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4204 &wan_attr_group);
4206 if (res) {
4207 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4208 return res;
4211 return 0;
4214 /* procfs -------------------------------------------------------------- */
4215 static int wan_read(struct seq_file *m)
4217 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4220 static int wan_write(char *buf)
4222 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4225 static struct ibm_struct wan_driver_data = {
4226 .name = "wan",
4227 .read = wan_read,
4228 .write = wan_write,
4229 .exit = wan_exit,
4230 .shutdown = wan_shutdown,
4233 /*************************************************************************
4234 * UWB subdriver
4237 enum {
4238 /* ACPI GUWB/SUWB bits */
4239 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4240 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4243 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4245 static int uwb_get_status(void)
4247 int status;
4249 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4250 if (dbg_uwbemul)
4251 return (tpacpi_uwb_emulstate) ?
4252 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4253 #endif
4255 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4256 return -EIO;
4258 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4259 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4262 static int uwb_set_status(enum tpacpi_rfkill_state state)
4264 int status;
4266 vdbg_printk(TPACPI_DBG_RFKILL,
4267 "will attempt to %s UWB\n",
4268 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4270 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4271 if (dbg_uwbemul) {
4272 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4273 return 0;
4275 #endif
4277 if (state == TPACPI_RFK_RADIO_ON)
4278 status = TP_ACPI_UWB_RADIOSSW;
4279 else
4280 status = 0;
4282 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4283 return -EIO;
4285 return 0;
4288 /* --------------------------------------------------------------------- */
4290 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4291 .get_status = uwb_get_status,
4292 .set_status = uwb_set_status,
4295 static void uwb_exit(void)
4297 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4300 static int __init uwb_init(struct ibm_init_struct *iibm)
4302 int res;
4303 int status = 0;
4305 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4306 "initializing uwb subdriver\n");
4308 TPACPI_ACPIHANDLE_INIT(hkey);
4310 tp_features.uwb = hkey_handle &&
4311 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4314 "uwb is %s, status 0x%02x\n",
4315 str_supported(tp_features.uwb),
4316 status);
4318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4319 if (dbg_uwbemul) {
4320 tp_features.uwb = 1;
4321 pr_info("uwb switch emulation enabled\n");
4322 } else
4323 #endif
4324 if (tp_features.uwb &&
4325 !(status & TP_ACPI_UWB_HWPRESENT)) {
4326 /* no uwb hardware present in system */
4327 tp_features.uwb = 0;
4328 dbg_printk(TPACPI_DBG_INIT,
4329 "uwb hardware not installed\n");
4332 if (!tp_features.uwb)
4333 return 1;
4335 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4336 &uwb_tprfk_ops,
4337 RFKILL_TYPE_UWB,
4338 TPACPI_RFK_UWB_SW_NAME,
4339 false);
4340 return res;
4343 static struct ibm_struct uwb_driver_data = {
4344 .name = "uwb",
4345 .exit = uwb_exit,
4346 .flags.experimental = 1,
4349 /*************************************************************************
4350 * Video subdriver
4353 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4355 enum video_access_mode {
4356 TPACPI_VIDEO_NONE = 0,
4357 TPACPI_VIDEO_570, /* 570 */
4358 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4359 TPACPI_VIDEO_NEW, /* all others */
4362 enum { /* video status flags, based on VIDEO_570 */
4363 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4364 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4365 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4368 enum { /* TPACPI_VIDEO_570 constants */
4369 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4370 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4371 * video_status_flags */
4372 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4373 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4376 static enum video_access_mode video_supported;
4377 static int video_orig_autosw;
4379 static int video_autosw_get(void);
4380 static int video_autosw_set(int enable);
4382 TPACPI_HANDLE(vid, root,
4383 "\\_SB.PCI.AGP.VGA", /* 570 */
4384 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4385 "\\_SB.PCI0.VID0", /* 770e */
4386 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4387 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4388 "\\_SB.PCI0.AGP.VID", /* all others */
4389 ); /* R30, R31 */
4391 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4393 static int __init video_init(struct ibm_init_struct *iibm)
4395 int ivga;
4397 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4399 TPACPI_ACPIHANDLE_INIT(vid);
4400 if (tpacpi_is_ibm())
4401 TPACPI_ACPIHANDLE_INIT(vid2);
4403 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4404 /* G41, assume IVGA doesn't change */
4405 vid_handle = vid2_handle;
4407 if (!vid_handle)
4408 /* video switching not supported on R30, R31 */
4409 video_supported = TPACPI_VIDEO_NONE;
4410 else if (tpacpi_is_ibm() &&
4411 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4412 /* 570 */
4413 video_supported = TPACPI_VIDEO_570;
4414 else if (tpacpi_is_ibm() &&
4415 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4416 /* 600e/x, 770e, 770x */
4417 video_supported = TPACPI_VIDEO_770;
4418 else
4419 /* all others */
4420 video_supported = TPACPI_VIDEO_NEW;
4422 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4423 str_supported(video_supported != TPACPI_VIDEO_NONE),
4424 video_supported);
4426 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4429 static void video_exit(void)
4431 dbg_printk(TPACPI_DBG_EXIT,
4432 "restoring original video autoswitch mode\n");
4433 if (video_autosw_set(video_orig_autosw))
4434 pr_err("error while trying to restore original "
4435 "video autoswitch mode\n");
4438 static int video_outputsw_get(void)
4440 int status = 0;
4441 int i;
4443 switch (video_supported) {
4444 case TPACPI_VIDEO_570:
4445 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4446 TP_ACPI_VIDEO_570_PHSCMD))
4447 return -EIO;
4448 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4449 break;
4450 case TPACPI_VIDEO_770:
4451 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4452 return -EIO;
4453 if (i)
4454 status |= TP_ACPI_VIDEO_S_LCD;
4455 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4456 return -EIO;
4457 if (i)
4458 status |= TP_ACPI_VIDEO_S_CRT;
4459 break;
4460 case TPACPI_VIDEO_NEW:
4461 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4462 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_CRT;
4467 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4468 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_LCD;
4472 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_DVI;
4476 break;
4477 default:
4478 return -ENOSYS;
4481 return status;
4484 static int video_outputsw_set(int status)
4486 int autosw;
4487 int res = 0;
4489 switch (video_supported) {
4490 case TPACPI_VIDEO_570:
4491 res = acpi_evalf(NULL, NULL,
4492 "\\_SB.PHS2", "vdd",
4493 TP_ACPI_VIDEO_570_PHS2CMD,
4494 status | TP_ACPI_VIDEO_570_PHS2SET);
4495 break;
4496 case TPACPI_VIDEO_770:
4497 autosw = video_autosw_get();
4498 if (autosw < 0)
4499 return autosw;
4501 res = video_autosw_set(1);
4502 if (res)
4503 return res;
4504 res = acpi_evalf(vid_handle, NULL,
4505 "ASWT", "vdd", status * 0x100, 0);
4506 if (!autosw && video_autosw_set(autosw)) {
4507 pr_err("video auto-switch left enabled due to error\n");
4508 return -EIO;
4510 break;
4511 case TPACPI_VIDEO_NEW:
4512 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4513 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4514 break;
4515 default:
4516 return -ENOSYS;
4519 return (res)? 0 : -EIO;
4522 static int video_autosw_get(void)
4524 int autosw = 0;
4526 switch (video_supported) {
4527 case TPACPI_VIDEO_570:
4528 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4529 return -EIO;
4530 break;
4531 case TPACPI_VIDEO_770:
4532 case TPACPI_VIDEO_NEW:
4533 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4534 return -EIO;
4535 break;
4536 default:
4537 return -ENOSYS;
4540 return autosw & 1;
4543 static int video_autosw_set(int enable)
4545 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4546 return -EIO;
4547 return 0;
4550 static int video_outputsw_cycle(void)
4552 int autosw = video_autosw_get();
4553 int res;
4555 if (autosw < 0)
4556 return autosw;
4558 switch (video_supported) {
4559 case TPACPI_VIDEO_570:
4560 res = video_autosw_set(1);
4561 if (res)
4562 return res;
4563 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4564 break;
4565 case TPACPI_VIDEO_770:
4566 case TPACPI_VIDEO_NEW:
4567 res = video_autosw_set(1);
4568 if (res)
4569 return res;
4570 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4571 break;
4572 default:
4573 return -ENOSYS;
4575 if (!autosw && video_autosw_set(autosw)) {
4576 pr_err("video auto-switch left enabled due to error\n");
4577 return -EIO;
4580 return (res)? 0 : -EIO;
4583 static int video_expand_toggle(void)
4585 switch (video_supported) {
4586 case TPACPI_VIDEO_570:
4587 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4588 0 : -EIO;
4589 case TPACPI_VIDEO_770:
4590 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4591 0 : -EIO;
4592 case TPACPI_VIDEO_NEW:
4593 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4594 0 : -EIO;
4595 default:
4596 return -ENOSYS;
4598 /* not reached */
4601 static int video_read(struct seq_file *m)
4603 int status, autosw;
4605 if (video_supported == TPACPI_VIDEO_NONE) {
4606 seq_printf(m, "status:\t\tnot supported\n");
4607 return 0;
4610 /* Even reads can crash X.org, so... */
4611 if (!capable(CAP_SYS_ADMIN))
4612 return -EPERM;
4614 status = video_outputsw_get();
4615 if (status < 0)
4616 return status;
4618 autosw = video_autosw_get();
4619 if (autosw < 0)
4620 return autosw;
4622 seq_printf(m, "status:\t\tsupported\n");
4623 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4624 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4625 if (video_supported == TPACPI_VIDEO_NEW)
4626 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4627 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4628 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4629 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4630 if (video_supported == TPACPI_VIDEO_NEW)
4631 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4632 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4633 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4635 return 0;
4638 static int video_write(char *buf)
4640 char *cmd;
4641 int enable, disable, status;
4642 int res;
4644 if (video_supported == TPACPI_VIDEO_NONE)
4645 return -ENODEV;
4647 /* Even reads can crash X.org, let alone writes... */
4648 if (!capable(CAP_SYS_ADMIN))
4649 return -EPERM;
4651 enable = 0;
4652 disable = 0;
4654 while ((cmd = next_cmd(&buf))) {
4655 if (strlencmp(cmd, "lcd_enable") == 0) {
4656 enable |= TP_ACPI_VIDEO_S_LCD;
4657 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4658 disable |= TP_ACPI_VIDEO_S_LCD;
4659 } else if (strlencmp(cmd, "crt_enable") == 0) {
4660 enable |= TP_ACPI_VIDEO_S_CRT;
4661 } else if (strlencmp(cmd, "crt_disable") == 0) {
4662 disable |= TP_ACPI_VIDEO_S_CRT;
4663 } else if (video_supported == TPACPI_VIDEO_NEW &&
4664 strlencmp(cmd, "dvi_enable") == 0) {
4665 enable |= TP_ACPI_VIDEO_S_DVI;
4666 } else if (video_supported == TPACPI_VIDEO_NEW &&
4667 strlencmp(cmd, "dvi_disable") == 0) {
4668 disable |= TP_ACPI_VIDEO_S_DVI;
4669 } else if (strlencmp(cmd, "auto_enable") == 0) {
4670 res = video_autosw_set(1);
4671 if (res)
4672 return res;
4673 } else if (strlencmp(cmd, "auto_disable") == 0) {
4674 res = video_autosw_set(0);
4675 if (res)
4676 return res;
4677 } else if (strlencmp(cmd, "video_switch") == 0) {
4678 res = video_outputsw_cycle();
4679 if (res)
4680 return res;
4681 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4682 res = video_expand_toggle();
4683 if (res)
4684 return res;
4685 } else
4686 return -EINVAL;
4689 if (enable || disable) {
4690 status = video_outputsw_get();
4691 if (status < 0)
4692 return status;
4693 res = video_outputsw_set((status & ~disable) | enable);
4694 if (res)
4695 return res;
4698 return 0;
4701 static struct ibm_struct video_driver_data = {
4702 .name = "video",
4703 .read = video_read,
4704 .write = video_write,
4705 .exit = video_exit,
4708 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4710 /*************************************************************************
4711 * Light (thinklight) subdriver
4714 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4715 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4717 static int light_get_status(void)
4719 int status = 0;
4721 if (tp_features.light_status) {
4722 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4723 return -EIO;
4724 return (!!status);
4727 return -ENXIO;
4730 static int light_set_status(int status)
4732 int rc;
4734 if (tp_features.light) {
4735 if (cmos_handle) {
4736 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4737 (status)?
4738 TP_CMOS_THINKLIGHT_ON :
4739 TP_CMOS_THINKLIGHT_OFF);
4740 } else {
4741 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4742 (status)? 1 : 0);
4744 return (rc)? 0 : -EIO;
4747 return -ENXIO;
4750 static void light_set_status_worker(struct work_struct *work)
4752 struct tpacpi_led_classdev *data =
4753 container_of(work, struct tpacpi_led_classdev, work);
4755 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4756 light_set_status((data->new_state != TPACPI_LED_OFF));
4759 static void light_sysfs_set(struct led_classdev *led_cdev,
4760 enum led_brightness brightness)
4762 struct tpacpi_led_classdev *data =
4763 container_of(led_cdev,
4764 struct tpacpi_led_classdev,
4765 led_classdev);
4766 data->new_state = (brightness != LED_OFF) ?
4767 TPACPI_LED_ON : TPACPI_LED_OFF;
4768 queue_work(tpacpi_wq, &data->work);
4771 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4773 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4776 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4777 .led_classdev = {
4778 .name = "tpacpi::thinklight",
4779 .brightness_set = &light_sysfs_set,
4780 .brightness_get = &light_sysfs_get,
4784 static int __init light_init(struct ibm_init_struct *iibm)
4786 int rc;
4788 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4790 if (tpacpi_is_ibm()) {
4791 TPACPI_ACPIHANDLE_INIT(ledb);
4792 TPACPI_ACPIHANDLE_INIT(lght);
4794 TPACPI_ACPIHANDLE_INIT(cmos);
4795 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4797 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4798 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4800 if (tp_features.light)
4801 /* light status not supported on
4802 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4803 tp_features.light_status =
4804 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4806 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4807 str_supported(tp_features.light),
4808 str_supported(tp_features.light_status));
4810 if (!tp_features.light)
4811 return 1;
4813 rc = led_classdev_register(&tpacpi_pdev->dev,
4814 &tpacpi_led_thinklight.led_classdev);
4816 if (rc < 0) {
4817 tp_features.light = 0;
4818 tp_features.light_status = 0;
4819 } else {
4820 rc = 0;
4823 return rc;
4826 static void light_exit(void)
4828 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4829 if (work_pending(&tpacpi_led_thinklight.work))
4830 flush_workqueue(tpacpi_wq);
4833 static int light_read(struct seq_file *m)
4835 int status;
4837 if (!tp_features.light) {
4838 seq_printf(m, "status:\t\tnot supported\n");
4839 } else if (!tp_features.light_status) {
4840 seq_printf(m, "status:\t\tunknown\n");
4841 seq_printf(m, "commands:\ton, off\n");
4842 } else {
4843 status = light_get_status();
4844 if (status < 0)
4845 return status;
4846 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4847 seq_printf(m, "commands:\ton, off\n");
4850 return 0;
4853 static int light_write(char *buf)
4855 char *cmd;
4856 int newstatus = 0;
4858 if (!tp_features.light)
4859 return -ENODEV;
4861 while ((cmd = next_cmd(&buf))) {
4862 if (strlencmp(cmd, "on") == 0) {
4863 newstatus = 1;
4864 } else if (strlencmp(cmd, "off") == 0) {
4865 newstatus = 0;
4866 } else
4867 return -EINVAL;
4870 return light_set_status(newstatus);
4873 static struct ibm_struct light_driver_data = {
4874 .name = "light",
4875 .read = light_read,
4876 .write = light_write,
4877 .exit = light_exit,
4880 /*************************************************************************
4881 * CMOS subdriver
4884 /* sysfs cmos_command -------------------------------------------------- */
4885 static ssize_t cmos_command_store(struct device *dev,
4886 struct device_attribute *attr,
4887 const char *buf, size_t count)
4889 unsigned long cmos_cmd;
4890 int res;
4892 if (parse_strtoul(buf, 21, &cmos_cmd))
4893 return -EINVAL;
4895 res = issue_thinkpad_cmos_command(cmos_cmd);
4896 return (res)? res : count;
4899 static struct device_attribute dev_attr_cmos_command =
4900 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4902 /* --------------------------------------------------------------------- */
4904 static int __init cmos_init(struct ibm_init_struct *iibm)
4906 int res;
4908 vdbg_printk(TPACPI_DBG_INIT,
4909 "initializing cmos commands subdriver\n");
4911 TPACPI_ACPIHANDLE_INIT(cmos);
4913 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4914 str_supported(cmos_handle != NULL));
4916 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4917 if (res)
4918 return res;
4920 return (cmos_handle)? 0 : 1;
4923 static void cmos_exit(void)
4925 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4928 static int cmos_read(struct seq_file *m)
4930 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4931 R30, R31, T20-22, X20-21 */
4932 if (!cmos_handle)
4933 seq_printf(m, "status:\t\tnot supported\n");
4934 else {
4935 seq_printf(m, "status:\t\tsupported\n");
4936 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4939 return 0;
4942 static int cmos_write(char *buf)
4944 char *cmd;
4945 int cmos_cmd, res;
4947 while ((cmd = next_cmd(&buf))) {
4948 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4949 cmos_cmd >= 0 && cmos_cmd <= 21) {
4950 /* cmos_cmd set */
4951 } else
4952 return -EINVAL;
4954 res = issue_thinkpad_cmos_command(cmos_cmd);
4955 if (res)
4956 return res;
4959 return 0;
4962 static struct ibm_struct cmos_driver_data = {
4963 .name = "cmos",
4964 .read = cmos_read,
4965 .write = cmos_write,
4966 .exit = cmos_exit,
4969 /*************************************************************************
4970 * LED subdriver
4973 enum led_access_mode {
4974 TPACPI_LED_NONE = 0,
4975 TPACPI_LED_570, /* 570 */
4976 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4977 TPACPI_LED_NEW, /* all others */
4980 enum { /* For TPACPI_LED_OLD */
4981 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4982 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4983 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4986 static enum led_access_mode led_supported;
4988 static acpi_handle led_handle;
4990 #define TPACPI_LED_NUMLEDS 16
4991 static struct tpacpi_led_classdev *tpacpi_leds;
4992 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4993 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4994 /* there's a limit of 19 chars + NULL before 2.6.26 */
4995 "tpacpi::power",
4996 "tpacpi:orange:batt",
4997 "tpacpi:green:batt",
4998 "tpacpi::dock_active",
4999 "tpacpi::bay_active",
5000 "tpacpi::dock_batt",
5001 "tpacpi::unknown_led",
5002 "tpacpi::standby",
5003 "tpacpi::dock_status1",
5004 "tpacpi::dock_status2",
5005 "tpacpi::unknown_led2",
5006 "tpacpi::unknown_led3",
5007 "tpacpi::thinkvantage",
5009 #define TPACPI_SAFE_LEDS 0x1081U
5011 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5013 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5014 return false;
5015 #else
5016 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5017 #endif
5020 static int led_get_status(const unsigned int led)
5022 int status;
5023 enum led_status_t led_s;
5025 switch (led_supported) {
5026 case TPACPI_LED_570:
5027 if (!acpi_evalf(ec_handle,
5028 &status, "GLED", "dd", 1 << led))
5029 return -EIO;
5030 led_s = (status == 0)?
5031 TPACPI_LED_OFF :
5032 ((status == 1)?
5033 TPACPI_LED_ON :
5034 TPACPI_LED_BLINK);
5035 tpacpi_led_state_cache[led] = led_s;
5036 return led_s;
5037 default:
5038 return -ENXIO;
5041 /* not reached */
5044 static int led_set_status(const unsigned int led,
5045 const enum led_status_t ledstatus)
5047 /* off, on, blink. Index is led_status_t */
5048 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5049 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5051 int rc = 0;
5053 switch (led_supported) {
5054 case TPACPI_LED_570:
5055 /* 570 */
5056 if (unlikely(led > 7))
5057 return -EINVAL;
5058 if (unlikely(tpacpi_is_led_restricted(led)))
5059 return -EPERM;
5060 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5061 (1 << led), led_sled_arg1[ledstatus]))
5062 rc = -EIO;
5063 break;
5064 case TPACPI_LED_OLD:
5065 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5066 if (unlikely(led > 7))
5067 return -EINVAL;
5068 if (unlikely(tpacpi_is_led_restricted(led)))
5069 return -EPERM;
5070 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5071 if (rc >= 0)
5072 rc = ec_write(TPACPI_LED_EC_HLBL,
5073 (ledstatus == TPACPI_LED_BLINK) << led);
5074 if (rc >= 0)
5075 rc = ec_write(TPACPI_LED_EC_HLCL,
5076 (ledstatus != TPACPI_LED_OFF) << led);
5077 break;
5078 case TPACPI_LED_NEW:
5079 /* all others */
5080 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5081 return -EINVAL;
5082 if (unlikely(tpacpi_is_led_restricted(led)))
5083 return -EPERM;
5084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5085 led, led_led_arg1[ledstatus]))
5086 rc = -EIO;
5087 break;
5088 default:
5089 rc = -ENXIO;
5092 if (!rc)
5093 tpacpi_led_state_cache[led] = ledstatus;
5095 return rc;
5098 static void led_set_status_worker(struct work_struct *work)
5100 struct tpacpi_led_classdev *data =
5101 container_of(work, struct tpacpi_led_classdev, work);
5103 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5104 led_set_status(data->led, data->new_state);
5107 static void led_sysfs_set(struct led_classdev *led_cdev,
5108 enum led_brightness brightness)
5110 struct tpacpi_led_classdev *data = container_of(led_cdev,
5111 struct tpacpi_led_classdev, led_classdev);
5113 if (brightness == LED_OFF)
5114 data->new_state = TPACPI_LED_OFF;
5115 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5116 data->new_state = TPACPI_LED_ON;
5117 else
5118 data->new_state = TPACPI_LED_BLINK;
5120 queue_work(tpacpi_wq, &data->work);
5123 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5124 unsigned long *delay_on, unsigned long *delay_off)
5126 struct tpacpi_led_classdev *data = container_of(led_cdev,
5127 struct tpacpi_led_classdev, led_classdev);
5129 /* Can we choose the flash rate? */
5130 if (*delay_on == 0 && *delay_off == 0) {
5131 /* yes. set them to the hardware blink rate (1 Hz) */
5132 *delay_on = 500; /* ms */
5133 *delay_off = 500; /* ms */
5134 } else if ((*delay_on != 500) || (*delay_off != 500))
5135 return -EINVAL;
5137 data->new_state = TPACPI_LED_BLINK;
5138 queue_work(tpacpi_wq, &data->work);
5140 return 0;
5143 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5145 int rc;
5147 struct tpacpi_led_classdev *data = container_of(led_cdev,
5148 struct tpacpi_led_classdev, led_classdev);
5150 rc = led_get_status(data->led);
5152 if (rc == TPACPI_LED_OFF || rc < 0)
5153 rc = LED_OFF; /* no error handling in led class :( */
5154 else
5155 rc = LED_FULL;
5157 return rc;
5160 static void led_exit(void)
5162 unsigned int i;
5164 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5165 if (tpacpi_leds[i].led_classdev.name)
5166 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5169 kfree(tpacpi_leds);
5172 static int __init tpacpi_init_led(unsigned int led)
5174 int rc;
5176 tpacpi_leds[led].led = led;
5178 /* LEDs with no name don't get registered */
5179 if (!tpacpi_led_names[led])
5180 return 0;
5182 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5183 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5184 if (led_supported == TPACPI_LED_570)
5185 tpacpi_leds[led].led_classdev.brightness_get =
5186 &led_sysfs_get;
5188 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5190 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5192 rc = led_classdev_register(&tpacpi_pdev->dev,
5193 &tpacpi_leds[led].led_classdev);
5194 if (rc < 0)
5195 tpacpi_leds[led].led_classdev.name = NULL;
5197 return rc;
5200 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5201 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5202 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5203 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5205 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5206 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5207 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5208 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5209 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5210 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5211 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5212 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5214 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5215 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5216 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5217 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5218 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5220 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5221 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5222 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5223 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5225 /* (1) - may have excess leds enabled on MSB */
5227 /* Defaults (order matters, keep last, don't reorder!) */
5228 { /* Lenovo */
5229 .vendor = PCI_VENDOR_ID_LENOVO,
5230 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5231 .quirks = 0x1fffU,
5233 { /* IBM ThinkPads with no EC version string */
5234 .vendor = PCI_VENDOR_ID_IBM,
5235 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5236 .quirks = 0x00ffU,
5238 { /* IBM ThinkPads with EC version string */
5239 .vendor = PCI_VENDOR_ID_IBM,
5240 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5241 .quirks = 0x00bfU,
5245 #undef TPACPI_LEDQ_IBM
5246 #undef TPACPI_LEDQ_LNV
5248 static enum led_access_mode __init led_init_detect_mode(void)
5250 acpi_status status;
5252 if (tpacpi_is_ibm()) {
5253 /* 570 */
5254 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5255 if (ACPI_SUCCESS(status))
5256 return TPACPI_LED_570;
5258 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5259 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5260 if (ACPI_SUCCESS(status))
5261 return TPACPI_LED_OLD;
5264 /* most others */
5265 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5266 if (ACPI_SUCCESS(status))
5267 return TPACPI_LED_NEW;
5269 /* R30, R31, and unknown firmwares */
5270 led_handle = NULL;
5271 return TPACPI_LED_NONE;
5274 static int __init led_init(struct ibm_init_struct *iibm)
5276 unsigned int i;
5277 int rc;
5278 unsigned long useful_leds;
5280 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5282 led_supported = led_init_detect_mode();
5284 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5285 str_supported(led_supported), led_supported);
5287 if (led_supported == TPACPI_LED_NONE)
5288 return 1;
5290 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5291 GFP_KERNEL);
5292 if (!tpacpi_leds) {
5293 pr_err("Out of memory for LED data\n");
5294 return -ENOMEM;
5297 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5298 ARRAY_SIZE(led_useful_qtable));
5300 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5301 if (!tpacpi_is_led_restricted(i) &&
5302 test_bit(i, &useful_leds)) {
5303 rc = tpacpi_init_led(i);
5304 if (rc < 0) {
5305 led_exit();
5306 return rc;
5311 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5312 pr_notice("warning: userspace override of important "
5313 "firmware LEDs is enabled\n");
5314 #endif
5315 return 0;
5318 #define str_led_status(s) \
5319 ((s) == TPACPI_LED_OFF ? "off" : \
5320 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5322 static int led_read(struct seq_file *m)
5324 if (!led_supported) {
5325 seq_printf(m, "status:\t\tnot supported\n");
5326 return 0;
5328 seq_printf(m, "status:\t\tsupported\n");
5330 if (led_supported == TPACPI_LED_570) {
5331 /* 570 */
5332 int i, status;
5333 for (i = 0; i < 8; i++) {
5334 status = led_get_status(i);
5335 if (status < 0)
5336 return -EIO;
5337 seq_printf(m, "%d:\t\t%s\n",
5338 i, str_led_status(status));
5342 seq_printf(m, "commands:\t"
5343 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5345 return 0;
5348 static int led_write(char *buf)
5350 char *cmd;
5351 int led, rc;
5352 enum led_status_t s;
5354 if (!led_supported)
5355 return -ENODEV;
5357 while ((cmd = next_cmd(&buf))) {
5358 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5359 return -EINVAL;
5361 if (strstr(cmd, "off")) {
5362 s = TPACPI_LED_OFF;
5363 } else if (strstr(cmd, "on")) {
5364 s = TPACPI_LED_ON;
5365 } else if (strstr(cmd, "blink")) {
5366 s = TPACPI_LED_BLINK;
5367 } else {
5368 return -EINVAL;
5371 rc = led_set_status(led, s);
5372 if (rc < 0)
5373 return rc;
5376 return 0;
5379 static struct ibm_struct led_driver_data = {
5380 .name = "led",
5381 .read = led_read,
5382 .write = led_write,
5383 .exit = led_exit,
5386 /*************************************************************************
5387 * Beep subdriver
5390 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5392 #define TPACPI_BEEP_Q1 0x0001
5394 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5395 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5396 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5399 static int __init beep_init(struct ibm_init_struct *iibm)
5401 unsigned long quirks;
5403 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5405 TPACPI_ACPIHANDLE_INIT(beep);
5407 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5408 str_supported(beep_handle != NULL));
5410 quirks = tpacpi_check_quirks(beep_quirk_table,
5411 ARRAY_SIZE(beep_quirk_table));
5413 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5415 return (beep_handle)? 0 : 1;
5418 static int beep_read(struct seq_file *m)
5420 if (!beep_handle)
5421 seq_printf(m, "status:\t\tnot supported\n");
5422 else {
5423 seq_printf(m, "status:\t\tsupported\n");
5424 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5427 return 0;
5430 static int beep_write(char *buf)
5432 char *cmd;
5433 int beep_cmd;
5435 if (!beep_handle)
5436 return -ENODEV;
5438 while ((cmd = next_cmd(&buf))) {
5439 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5440 beep_cmd >= 0 && beep_cmd <= 17) {
5441 /* beep_cmd set */
5442 } else
5443 return -EINVAL;
5444 if (tp_features.beep_needs_two_args) {
5445 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5446 beep_cmd, 0))
5447 return -EIO;
5448 } else {
5449 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5450 beep_cmd))
5451 return -EIO;
5455 return 0;
5458 static struct ibm_struct beep_driver_data = {
5459 .name = "beep",
5460 .read = beep_read,
5461 .write = beep_write,
5464 /*************************************************************************
5465 * Thermal subdriver
5468 enum thermal_access_mode {
5469 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5470 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5471 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5472 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5473 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5476 enum { /* TPACPI_THERMAL_TPEC_* */
5477 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5478 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5479 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5481 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5485 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5486 struct ibm_thermal_sensors_struct {
5487 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5490 static enum thermal_access_mode thermal_read_mode;
5492 /* idx is zero-based */
5493 static int thermal_get_sensor(int idx, s32 *value)
5495 int t;
5496 s8 tmp;
5497 char tmpi[5];
5499 t = TP_EC_THERMAL_TMP0;
5501 switch (thermal_read_mode) {
5502 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5503 case TPACPI_THERMAL_TPEC_16:
5504 if (idx >= 8 && idx <= 15) {
5505 t = TP_EC_THERMAL_TMP8;
5506 idx -= 8;
5508 /* fallthrough */
5509 #endif
5510 case TPACPI_THERMAL_TPEC_8:
5511 if (idx <= 7) {
5512 if (!acpi_ec_read(t + idx, &tmp))
5513 return -EIO;
5514 *value = tmp * 1000;
5515 return 0;
5517 break;
5519 case TPACPI_THERMAL_ACPI_UPDT:
5520 if (idx <= 7) {
5521 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5522 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5523 return -EIO;
5524 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5525 return -EIO;
5526 *value = (t - 2732) * 100;
5527 return 0;
5529 break;
5531 case TPACPI_THERMAL_ACPI_TMP07:
5532 if (idx <= 7) {
5533 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5534 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5535 return -EIO;
5536 if (t > 127 || t < -127)
5537 t = TP_EC_THERMAL_TMP_NA;
5538 *value = t * 1000;
5539 return 0;
5541 break;
5543 case TPACPI_THERMAL_NONE:
5544 default:
5545 return -ENOSYS;
5548 return -EINVAL;
5551 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5553 int res, i;
5554 int n;
5556 n = 8;
5557 i = 0;
5559 if (!s)
5560 return -EINVAL;
5562 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5563 n = 16;
5565 for (i = 0 ; i < n; i++) {
5566 res = thermal_get_sensor(i, &s->temp[i]);
5567 if (res)
5568 return res;
5571 return n;
5574 static void thermal_dump_all_sensors(void)
5576 int n, i;
5577 struct ibm_thermal_sensors_struct t;
5579 n = thermal_get_sensors(&t);
5580 if (n <= 0)
5581 return;
5583 pr_notice("temperatures (Celsius):");
5585 for (i = 0; i < n; i++) {
5586 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5587 pr_cont(" %d", (int)(t.temp[i] / 1000));
5588 else
5589 pr_cont(" N/A");
5592 pr_cont("\n");
5595 /* sysfs temp##_input -------------------------------------------------- */
5597 static ssize_t thermal_temp_input_show(struct device *dev,
5598 struct device_attribute *attr,
5599 char *buf)
5601 struct sensor_device_attribute *sensor_attr =
5602 to_sensor_dev_attr(attr);
5603 int idx = sensor_attr->index;
5604 s32 value;
5605 int res;
5607 res = thermal_get_sensor(idx, &value);
5608 if (res)
5609 return res;
5610 if (value == TPACPI_THERMAL_SENSOR_NA)
5611 return -ENXIO;
5613 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5616 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5617 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5618 thermal_temp_input_show, NULL, _idxB)
5620 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5621 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5622 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5623 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5624 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5625 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5626 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5627 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5628 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5629 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5630 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5631 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5632 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5633 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5634 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5635 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5636 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5639 #define THERMAL_ATTRS(X) \
5640 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5642 static struct attribute *thermal_temp_input_attr[] = {
5643 THERMAL_ATTRS(8),
5644 THERMAL_ATTRS(9),
5645 THERMAL_ATTRS(10),
5646 THERMAL_ATTRS(11),
5647 THERMAL_ATTRS(12),
5648 THERMAL_ATTRS(13),
5649 THERMAL_ATTRS(14),
5650 THERMAL_ATTRS(15),
5651 THERMAL_ATTRS(0),
5652 THERMAL_ATTRS(1),
5653 THERMAL_ATTRS(2),
5654 THERMAL_ATTRS(3),
5655 THERMAL_ATTRS(4),
5656 THERMAL_ATTRS(5),
5657 THERMAL_ATTRS(6),
5658 THERMAL_ATTRS(7),
5659 NULL
5662 static const struct attribute_group thermal_temp_input16_group = {
5663 .attrs = thermal_temp_input_attr
5666 static const struct attribute_group thermal_temp_input8_group = {
5667 .attrs = &thermal_temp_input_attr[8]
5670 #undef THERMAL_SENSOR_ATTR_TEMP
5671 #undef THERMAL_ATTRS
5673 /* --------------------------------------------------------------------- */
5675 static int __init thermal_init(struct ibm_init_struct *iibm)
5677 u8 t, ta1, ta2;
5678 int i;
5679 int acpi_tmp7;
5680 int res;
5682 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5684 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5686 if (thinkpad_id.ec_model) {
5688 * Direct EC access mode: sensors at registers
5689 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5690 * non-implemented, thermal sensors return 0x80 when
5691 * not available
5694 ta1 = ta2 = 0;
5695 for (i = 0; i < 8; i++) {
5696 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5697 ta1 |= t;
5698 } else {
5699 ta1 = 0;
5700 break;
5702 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5703 ta2 |= t;
5704 } else {
5705 ta1 = 0;
5706 break;
5709 if (ta1 == 0) {
5710 /* This is sheer paranoia, but we handle it anyway */
5711 if (acpi_tmp7) {
5712 pr_err("ThinkPad ACPI EC access misbehaving, "
5713 "falling back to ACPI TMPx access "
5714 "mode\n");
5715 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5716 } else {
5717 pr_err("ThinkPad ACPI EC access misbehaving, "
5718 "disabling thermal sensors access\n");
5719 thermal_read_mode = TPACPI_THERMAL_NONE;
5721 } else {
5722 thermal_read_mode =
5723 (ta2 != 0) ?
5724 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5726 } else if (acpi_tmp7) {
5727 if (tpacpi_is_ibm() &&
5728 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5729 /* 600e/x, 770e, 770x */
5730 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5731 } else {
5732 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5733 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5735 } else {
5736 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5737 thermal_read_mode = TPACPI_THERMAL_NONE;
5740 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5741 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5742 thermal_read_mode);
5744 switch (thermal_read_mode) {
5745 case TPACPI_THERMAL_TPEC_16:
5746 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5747 &thermal_temp_input16_group);
5748 if (res)
5749 return res;
5750 break;
5751 case TPACPI_THERMAL_TPEC_8:
5752 case TPACPI_THERMAL_ACPI_TMP07:
5753 case TPACPI_THERMAL_ACPI_UPDT:
5754 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5755 &thermal_temp_input8_group);
5756 if (res)
5757 return res;
5758 break;
5759 case TPACPI_THERMAL_NONE:
5760 default:
5761 return 1;
5764 return 0;
5767 static void thermal_exit(void)
5769 switch (thermal_read_mode) {
5770 case TPACPI_THERMAL_TPEC_16:
5771 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5772 &thermal_temp_input16_group);
5773 break;
5774 case TPACPI_THERMAL_TPEC_8:
5775 case TPACPI_THERMAL_ACPI_TMP07:
5776 case TPACPI_THERMAL_ACPI_UPDT:
5777 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5778 &thermal_temp_input8_group);
5779 break;
5780 case TPACPI_THERMAL_NONE:
5781 default:
5782 break;
5786 static int thermal_read(struct seq_file *m)
5788 int n, i;
5789 struct ibm_thermal_sensors_struct t;
5791 n = thermal_get_sensors(&t);
5792 if (unlikely(n < 0))
5793 return n;
5795 seq_printf(m, "temperatures:\t");
5797 if (n > 0) {
5798 for (i = 0; i < (n - 1); i++)
5799 seq_printf(m, "%d ", t.temp[i] / 1000);
5800 seq_printf(m, "%d\n", t.temp[i] / 1000);
5801 } else
5802 seq_printf(m, "not supported\n");
5804 return 0;
5807 static struct ibm_struct thermal_driver_data = {
5808 .name = "thermal",
5809 .read = thermal_read,
5810 .exit = thermal_exit,
5813 /*************************************************************************
5814 * Backlight/brightness subdriver
5817 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5820 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5821 * CMOS NVRAM byte 0x5E, bits 0-3.
5823 * EC HBRV (0x31) has the following layout
5824 * Bit 7: unknown function
5825 * Bit 6: unknown function
5826 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5827 * Bit 4: must be set to zero to avoid problems
5828 * Bit 3-0: backlight brightness level
5830 * brightness_get_raw returns status data in the HBRV layout
5832 * WARNING: The X61 has been verified to use HBRV for something else, so
5833 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5834 * testing on the very early *60 Lenovo models...
5837 enum {
5838 TP_EC_BACKLIGHT = 0x31,
5840 /* TP_EC_BACKLIGHT bitmasks */
5841 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5842 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5843 TP_EC_BACKLIGHT_MAPSW = 0x20,
5846 enum tpacpi_brightness_access_mode {
5847 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5848 TPACPI_BRGHT_MODE_EC, /* EC control */
5849 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5850 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5851 TPACPI_BRGHT_MODE_MAX
5854 static struct backlight_device *ibm_backlight_device;
5856 static enum tpacpi_brightness_access_mode brightness_mode =
5857 TPACPI_BRGHT_MODE_MAX;
5859 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5861 static struct mutex brightness_mutex;
5863 /* NVRAM brightness access,
5864 * call with brightness_mutex held! */
5865 static unsigned int tpacpi_brightness_nvram_get(void)
5867 u8 lnvram;
5869 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5870 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5871 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5872 lnvram &= bright_maxlvl;
5874 return lnvram;
5877 static void tpacpi_brightness_checkpoint_nvram(void)
5879 u8 lec = 0;
5880 u8 b_nvram;
5882 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5883 return;
5885 vdbg_printk(TPACPI_DBG_BRGHT,
5886 "trying to checkpoint backlight level to NVRAM...\n");
5888 if (mutex_lock_killable(&brightness_mutex) < 0)
5889 return;
5891 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5892 goto unlock;
5893 lec &= TP_EC_BACKLIGHT_LVLMSK;
5894 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5896 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5897 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5898 /* NVRAM needs update */
5899 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5900 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5901 b_nvram |= lec;
5902 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5903 dbg_printk(TPACPI_DBG_BRGHT,
5904 "updated NVRAM backlight level to %u (0x%02x)\n",
5905 (unsigned int) lec, (unsigned int) b_nvram);
5906 } else
5907 vdbg_printk(TPACPI_DBG_BRGHT,
5908 "NVRAM backlight level already is %u (0x%02x)\n",
5909 (unsigned int) lec, (unsigned int) b_nvram);
5911 unlock:
5912 mutex_unlock(&brightness_mutex);
5916 /* call with brightness_mutex held! */
5917 static int tpacpi_brightness_get_raw(int *status)
5919 u8 lec = 0;
5921 switch (brightness_mode) {
5922 case TPACPI_BRGHT_MODE_UCMS_STEP:
5923 *status = tpacpi_brightness_nvram_get();
5924 return 0;
5925 case TPACPI_BRGHT_MODE_EC:
5926 case TPACPI_BRGHT_MODE_ECNVRAM:
5927 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5928 return -EIO;
5929 *status = lec;
5930 return 0;
5931 default:
5932 return -ENXIO;
5936 /* call with brightness_mutex held! */
5937 /* do NOT call with illegal backlight level value */
5938 static int tpacpi_brightness_set_ec(unsigned int value)
5940 u8 lec = 0;
5942 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5943 return -EIO;
5945 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5946 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5947 (value & TP_EC_BACKLIGHT_LVLMSK))))
5948 return -EIO;
5950 return 0;
5953 /* call with brightness_mutex held! */
5954 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5956 int cmos_cmd, inc;
5957 unsigned int current_value, i;
5959 current_value = tpacpi_brightness_nvram_get();
5961 if (value == current_value)
5962 return 0;
5964 cmos_cmd = (value > current_value) ?
5965 TP_CMOS_BRIGHTNESS_UP :
5966 TP_CMOS_BRIGHTNESS_DOWN;
5967 inc = (value > current_value) ? 1 : -1;
5969 for (i = current_value; i != value; i += inc)
5970 if (issue_thinkpad_cmos_command(cmos_cmd))
5971 return -EIO;
5973 return 0;
5976 /* May return EINTR which can always be mapped to ERESTARTSYS */
5977 static int brightness_set(unsigned int value)
5979 int res;
5981 if (value > bright_maxlvl || value < 0)
5982 return -EINVAL;
5984 vdbg_printk(TPACPI_DBG_BRGHT,
5985 "set backlight level to %d\n", value);
5987 res = mutex_lock_killable(&brightness_mutex);
5988 if (res < 0)
5989 return res;
5991 switch (brightness_mode) {
5992 case TPACPI_BRGHT_MODE_EC:
5993 case TPACPI_BRGHT_MODE_ECNVRAM:
5994 res = tpacpi_brightness_set_ec(value);
5995 break;
5996 case TPACPI_BRGHT_MODE_UCMS_STEP:
5997 res = tpacpi_brightness_set_ucmsstep(value);
5998 break;
5999 default:
6000 res = -ENXIO;
6003 mutex_unlock(&brightness_mutex);
6004 return res;
6007 /* sysfs backlight class ----------------------------------------------- */
6009 static int brightness_update_status(struct backlight_device *bd)
6011 unsigned int level =
6012 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6013 bd->props.power == FB_BLANK_UNBLANK) ?
6014 bd->props.brightness : 0;
6016 dbg_printk(TPACPI_DBG_BRGHT,
6017 "backlight: attempt to set level to %d\n",
6018 level);
6020 /* it is the backlight class's job (caller) to handle
6021 * EINTR and other errors properly */
6022 return brightness_set(level);
6025 static int brightness_get(struct backlight_device *bd)
6027 int status, res;
6029 res = mutex_lock_killable(&brightness_mutex);
6030 if (res < 0)
6031 return 0;
6033 res = tpacpi_brightness_get_raw(&status);
6035 mutex_unlock(&brightness_mutex);
6037 if (res < 0)
6038 return 0;
6040 return status & TP_EC_BACKLIGHT_LVLMSK;
6043 static void tpacpi_brightness_notify_change(void)
6045 backlight_force_update(ibm_backlight_device,
6046 BACKLIGHT_UPDATE_HOTKEY);
6049 static const struct backlight_ops ibm_backlight_data = {
6050 .get_brightness = brightness_get,
6051 .update_status = brightness_update_status,
6054 /* --------------------------------------------------------------------- */
6057 * Call _BCL method of video device. On some ThinkPads this will
6058 * switch the firmware to the ACPI brightness control mode.
6061 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6063 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6064 union acpi_object *obj;
6065 int rc;
6067 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6068 obj = (union acpi_object *)buffer.pointer;
6069 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6070 pr_err("Unknown _BCL data, please report this to %s\n",
6071 TPACPI_MAIL);
6072 rc = 0;
6073 } else {
6074 rc = obj->package.count;
6076 } else {
6077 return 0;
6080 kfree(buffer.pointer);
6081 return rc;
6086 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6088 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6090 acpi_handle video_device;
6091 int bcl_levels = 0;
6093 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6094 if (video_device)
6095 bcl_levels = tpacpi_query_bcl_levels(video_device);
6097 tp_features.bright_acpimode = (bcl_levels > 0);
6099 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6103 * These are only useful for models that have only one possibility
6104 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6105 * these quirks.
6107 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6108 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6109 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6111 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6112 /* Models with ATI GPUs known to require ECNVRAM mode */
6113 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6115 /* Models with ATI GPUs that can use ECNVRAM */
6116 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6117 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6118 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6119 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6121 /* Models with Intel Extreme Graphics 2 */
6122 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6123 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6124 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6126 /* Models with Intel GMA900 */
6127 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6128 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6129 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6133 * Returns < 0 for error, otherwise sets tp_features.bright_*
6134 * and bright_maxlvl.
6136 static void __init tpacpi_detect_brightness_capabilities(void)
6138 unsigned int b;
6140 vdbg_printk(TPACPI_DBG_INIT,
6141 "detecting firmware brightness interface capabilities\n");
6143 /* we could run a quirks check here (same table used by
6144 * brightness_init) if needed */
6147 * We always attempt to detect acpi support, so as to switch
6148 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6149 * going to publish a backlight interface
6151 b = tpacpi_check_std_acpi_brightness_support();
6152 switch (b) {
6153 case 16:
6154 bright_maxlvl = 15;
6155 pr_info("detected a 16-level brightness capable ThinkPad\n");
6156 break;
6157 case 8:
6158 case 0:
6159 bright_maxlvl = 7;
6160 pr_info("detected a 8-level brightness capable ThinkPad\n");
6161 break;
6162 default:
6163 pr_err("Unsupported brightness interface, "
6164 "please contact %s\n", TPACPI_MAIL);
6165 tp_features.bright_unkfw = 1;
6166 bright_maxlvl = b - 1;
6170 static int __init brightness_init(struct ibm_init_struct *iibm)
6172 struct backlight_properties props;
6173 int b;
6174 unsigned long quirks;
6176 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6178 mutex_init(&brightness_mutex);
6180 quirks = tpacpi_check_quirks(brightness_quirk_table,
6181 ARRAY_SIZE(brightness_quirk_table));
6183 /* tpacpi_detect_brightness_capabilities() must have run already */
6185 /* if it is unknown, we don't handle it: it wouldn't be safe */
6186 if (tp_features.bright_unkfw)
6187 return 1;
6189 if (!brightness_enable) {
6190 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6191 "brightness support disabled by "
6192 "module parameter\n");
6193 return 1;
6196 if (acpi_video_backlight_support()) {
6197 if (brightness_enable > 1) {
6198 pr_info("Standard ACPI backlight interface "
6199 "available, not loading native one\n");
6200 return 1;
6201 } else if (brightness_enable == 1) {
6202 pr_warn("Cannot enable backlight brightness support, "
6203 "ACPI is already handling it. Refer to the "
6204 "acpi_backlight kernel parameter.\n");
6205 return 1;
6207 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6208 pr_notice("Standard ACPI backlight interface not "
6209 "available, thinkpad_acpi native "
6210 "brightness control enabled\n");
6214 * Check for module parameter bogosity, note that we
6215 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6216 * able to detect "unspecified"
6218 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6219 return -EINVAL;
6221 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6222 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6223 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6224 if (quirks & TPACPI_BRGHT_Q_EC)
6225 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6226 else
6227 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6229 dbg_printk(TPACPI_DBG_BRGHT,
6230 "driver auto-selected brightness_mode=%d\n",
6231 brightness_mode);
6234 /* Safety */
6235 if (!tpacpi_is_ibm() &&
6236 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6237 brightness_mode == TPACPI_BRGHT_MODE_EC))
6238 return -EINVAL;
6240 if (tpacpi_brightness_get_raw(&b) < 0)
6241 return 1;
6243 memset(&props, 0, sizeof(struct backlight_properties));
6244 props.type = BACKLIGHT_PLATFORM;
6245 props.max_brightness = bright_maxlvl;
6246 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6247 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6248 NULL, NULL,
6249 &ibm_backlight_data,
6250 &props);
6251 if (IS_ERR(ibm_backlight_device)) {
6252 int rc = PTR_ERR(ibm_backlight_device);
6253 ibm_backlight_device = NULL;
6254 pr_err("Could not register backlight device\n");
6255 return rc;
6257 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6258 "brightness is supported\n");
6260 if (quirks & TPACPI_BRGHT_Q_ASK) {
6261 pr_notice("brightness: will use unverified default: "
6262 "brightness_mode=%d\n", brightness_mode);
6263 pr_notice("brightness: please report to %s whether it works well "
6264 "or not on your ThinkPad\n", TPACPI_MAIL);
6267 /* Added by mistake in early 2007. Probably useless, but it could
6268 * be working around some unknown firmware problem where the value
6269 * read at startup doesn't match the real hardware state... so leave
6270 * it in place just in case */
6271 backlight_update_status(ibm_backlight_device);
6273 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6274 "brightness: registering brightness hotkeys "
6275 "as change notification\n");
6276 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6277 | TP_ACPI_HKEY_BRGHTUP_MASK
6278 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6279 return 0;
6282 static void brightness_suspend(pm_message_t state)
6284 tpacpi_brightness_checkpoint_nvram();
6287 static void brightness_shutdown(void)
6289 tpacpi_brightness_checkpoint_nvram();
6292 static void brightness_exit(void)
6294 if (ibm_backlight_device) {
6295 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6296 "calling backlight_device_unregister()\n");
6297 backlight_device_unregister(ibm_backlight_device);
6300 tpacpi_brightness_checkpoint_nvram();
6303 static int brightness_read(struct seq_file *m)
6305 int level;
6307 level = brightness_get(NULL);
6308 if (level < 0) {
6309 seq_printf(m, "level:\t\tunreadable\n");
6310 } else {
6311 seq_printf(m, "level:\t\t%d\n", level);
6312 seq_printf(m, "commands:\tup, down\n");
6313 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6314 bright_maxlvl);
6317 return 0;
6320 static int brightness_write(char *buf)
6322 int level;
6323 int rc;
6324 char *cmd;
6326 level = brightness_get(NULL);
6327 if (level < 0)
6328 return level;
6330 while ((cmd = next_cmd(&buf))) {
6331 if (strlencmp(cmd, "up") == 0) {
6332 if (level < bright_maxlvl)
6333 level++;
6334 } else if (strlencmp(cmd, "down") == 0) {
6335 if (level > 0)
6336 level--;
6337 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6338 level >= 0 && level <= bright_maxlvl) {
6339 /* new level set */
6340 } else
6341 return -EINVAL;
6344 tpacpi_disclose_usertask("procfs brightness",
6345 "set level to %d\n", level);
6348 * Now we know what the final level should be, so we try to set it.
6349 * Doing it this way makes the syscall restartable in case of EINTR
6351 rc = brightness_set(level);
6352 if (!rc && ibm_backlight_device)
6353 backlight_force_update(ibm_backlight_device,
6354 BACKLIGHT_UPDATE_SYSFS);
6355 return (rc == -EINTR)? -ERESTARTSYS : rc;
6358 static struct ibm_struct brightness_driver_data = {
6359 .name = "brightness",
6360 .read = brightness_read,
6361 .write = brightness_write,
6362 .exit = brightness_exit,
6363 .suspend = brightness_suspend,
6364 .shutdown = brightness_shutdown,
6367 /*************************************************************************
6368 * Volume subdriver
6372 * IBM ThinkPads have a simple volume controller with MUTE gating.
6373 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6375 * Since the *61 series (and probably also the later *60 series), Lenovo
6376 * ThinkPads only implement the MUTE gate.
6378 * EC register 0x30
6379 * Bit 6: MUTE (1 mutes sound)
6380 * Bit 3-0: Volume
6381 * Other bits should be zero as far as we know.
6383 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6384 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6385 * such as bit 7 which is used to detect repeated presses of MUTE,
6386 * and we leave them unchanged.
6389 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6391 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6392 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6393 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6395 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6396 static char *alsa_id = "ThinkPadEC";
6397 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6399 struct tpacpi_alsa_data {
6400 struct snd_card *card;
6401 struct snd_ctl_elem_id *ctl_mute_id;
6402 struct snd_ctl_elem_id *ctl_vol_id;
6405 static struct snd_card *alsa_card;
6407 enum {
6408 TP_EC_AUDIO = 0x30,
6410 /* TP_EC_AUDIO bits */
6411 TP_EC_AUDIO_MUTESW = 6,
6413 /* TP_EC_AUDIO bitmasks */
6414 TP_EC_AUDIO_LVL_MSK = 0x0F,
6415 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6417 /* Maximum volume */
6418 TP_EC_VOLUME_MAX = 14,
6421 enum tpacpi_volume_access_mode {
6422 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6423 TPACPI_VOL_MODE_EC, /* Pure EC control */
6424 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6425 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6426 TPACPI_VOL_MODE_MAX
6429 enum tpacpi_volume_capabilities {
6430 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6431 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6432 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6433 TPACPI_VOL_CAP_MAX
6436 static enum tpacpi_volume_access_mode volume_mode =
6437 TPACPI_VOL_MODE_MAX;
6439 static enum tpacpi_volume_capabilities volume_capabilities;
6440 static int volume_control_allowed;
6443 * Used to syncronize writers to TP_EC_AUDIO and
6444 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6446 static struct mutex volume_mutex;
6448 static void tpacpi_volume_checkpoint_nvram(void)
6450 u8 lec = 0;
6451 u8 b_nvram;
6452 u8 ec_mask;
6454 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6455 return;
6456 if (!volume_control_allowed)
6457 return;
6459 vdbg_printk(TPACPI_DBG_MIXER,
6460 "trying to checkpoint mixer state to NVRAM...\n");
6462 if (tp_features.mixer_no_level_control)
6463 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6464 else
6465 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6467 if (mutex_lock_killable(&volume_mutex) < 0)
6468 return;
6470 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6471 goto unlock;
6472 lec &= ec_mask;
6473 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6475 if (lec != (b_nvram & ec_mask)) {
6476 /* NVRAM needs update */
6477 b_nvram &= ~ec_mask;
6478 b_nvram |= lec;
6479 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6480 dbg_printk(TPACPI_DBG_MIXER,
6481 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6482 (unsigned int) lec, (unsigned int) b_nvram);
6483 } else {
6484 vdbg_printk(TPACPI_DBG_MIXER,
6485 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6486 (unsigned int) lec, (unsigned int) b_nvram);
6489 unlock:
6490 mutex_unlock(&volume_mutex);
6493 static int volume_get_status_ec(u8 *status)
6495 u8 s;
6497 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6498 return -EIO;
6500 *status = s;
6502 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6504 return 0;
6507 static int volume_get_status(u8 *status)
6509 return volume_get_status_ec(status);
6512 static int volume_set_status_ec(const u8 status)
6514 if (!acpi_ec_write(TP_EC_AUDIO, status))
6515 return -EIO;
6517 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6519 return 0;
6522 static int volume_set_status(const u8 status)
6524 return volume_set_status_ec(status);
6527 /* returns < 0 on error, 0 on no change, 1 on change */
6528 static int __volume_set_mute_ec(const bool mute)
6530 int rc;
6531 u8 s, n;
6533 if (mutex_lock_killable(&volume_mutex) < 0)
6534 return -EINTR;
6536 rc = volume_get_status_ec(&s);
6537 if (rc)
6538 goto unlock;
6540 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6541 s & ~TP_EC_AUDIO_MUTESW_MSK;
6543 if (n != s) {
6544 rc = volume_set_status_ec(n);
6545 if (!rc)
6546 rc = 1;
6549 unlock:
6550 mutex_unlock(&volume_mutex);
6551 return rc;
6554 static int volume_alsa_set_mute(const bool mute)
6556 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6557 (mute) ? "" : "un");
6558 return __volume_set_mute_ec(mute);
6561 static int volume_set_mute(const bool mute)
6563 int rc;
6565 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6566 (mute) ? "" : "un");
6568 rc = __volume_set_mute_ec(mute);
6569 return (rc < 0) ? rc : 0;
6572 /* returns < 0 on error, 0 on no change, 1 on change */
6573 static int __volume_set_volume_ec(const u8 vol)
6575 int rc;
6576 u8 s, n;
6578 if (vol > TP_EC_VOLUME_MAX)
6579 return -EINVAL;
6581 if (mutex_lock_killable(&volume_mutex) < 0)
6582 return -EINTR;
6584 rc = volume_get_status_ec(&s);
6585 if (rc)
6586 goto unlock;
6588 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6590 if (n != s) {
6591 rc = volume_set_status_ec(n);
6592 if (!rc)
6593 rc = 1;
6596 unlock:
6597 mutex_unlock(&volume_mutex);
6598 return rc;
6601 static int volume_alsa_set_volume(const u8 vol)
6603 dbg_printk(TPACPI_DBG_MIXER,
6604 "ALSA: trying to set volume level to %hu\n", vol);
6605 return __volume_set_volume_ec(vol);
6608 static void volume_alsa_notify_change(void)
6610 struct tpacpi_alsa_data *d;
6612 if (alsa_card && alsa_card->private_data) {
6613 d = alsa_card->private_data;
6614 if (d->ctl_mute_id)
6615 snd_ctl_notify(alsa_card,
6616 SNDRV_CTL_EVENT_MASK_VALUE,
6617 d->ctl_mute_id);
6618 if (d->ctl_vol_id)
6619 snd_ctl_notify(alsa_card,
6620 SNDRV_CTL_EVENT_MASK_VALUE,
6621 d->ctl_vol_id);
6625 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6626 struct snd_ctl_elem_info *uinfo)
6628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6629 uinfo->count = 1;
6630 uinfo->value.integer.min = 0;
6631 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6632 return 0;
6635 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6636 struct snd_ctl_elem_value *ucontrol)
6638 u8 s;
6639 int rc;
6641 rc = volume_get_status(&s);
6642 if (rc < 0)
6643 return rc;
6645 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6646 return 0;
6649 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6650 struct snd_ctl_elem_value *ucontrol)
6652 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6653 ucontrol->value.integer.value[0]);
6654 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6657 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6659 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6660 struct snd_ctl_elem_value *ucontrol)
6662 u8 s;
6663 int rc;
6665 rc = volume_get_status(&s);
6666 if (rc < 0)
6667 return rc;
6669 ucontrol->value.integer.value[0] =
6670 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6671 return 0;
6674 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6675 struct snd_ctl_elem_value *ucontrol)
6677 tpacpi_disclose_usertask("ALSA", "%smute\n",
6678 ucontrol->value.integer.value[0] ?
6679 "un" : "");
6680 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6683 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6685 .name = "Console Playback Volume",
6686 .index = 0,
6687 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6688 .info = volume_alsa_vol_info,
6689 .get = volume_alsa_vol_get,
6692 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6694 .name = "Console Playback Switch",
6695 .index = 0,
6696 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6697 .info = volume_alsa_mute_info,
6698 .get = volume_alsa_mute_get,
6701 static void volume_suspend(pm_message_t state)
6703 tpacpi_volume_checkpoint_nvram();
6706 static void volume_resume(void)
6708 volume_alsa_notify_change();
6711 static void volume_shutdown(void)
6713 tpacpi_volume_checkpoint_nvram();
6716 static void volume_exit(void)
6718 if (alsa_card) {
6719 snd_card_free(alsa_card);
6720 alsa_card = NULL;
6723 tpacpi_volume_checkpoint_nvram();
6726 static int __init volume_create_alsa_mixer(void)
6728 struct snd_card *card;
6729 struct tpacpi_alsa_data *data;
6730 struct snd_kcontrol *ctl_vol;
6731 struct snd_kcontrol *ctl_mute;
6732 int rc;
6734 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6735 sizeof(struct tpacpi_alsa_data), &card);
6736 if (rc < 0 || !card) {
6737 pr_err("Failed to create ALSA card structures: %d\n", rc);
6738 return 1;
6741 BUG_ON(!card->private_data);
6742 data = card->private_data;
6743 data->card = card;
6745 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6746 sizeof(card->driver));
6747 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6748 sizeof(card->shortname));
6749 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6750 (thinkpad_id.ec_version_str) ?
6751 thinkpad_id.ec_version_str : "(unknown)");
6752 snprintf(card->longname, sizeof(card->longname),
6753 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6754 (thinkpad_id.ec_version_str) ?
6755 thinkpad_id.ec_version_str : "unknown");
6757 if (volume_control_allowed) {
6758 volume_alsa_control_vol.put = volume_alsa_vol_put;
6759 volume_alsa_control_vol.access =
6760 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6762 volume_alsa_control_mute.put = volume_alsa_mute_put;
6763 volume_alsa_control_mute.access =
6764 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6767 if (!tp_features.mixer_no_level_control) {
6768 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6769 rc = snd_ctl_add(card, ctl_vol);
6770 if (rc < 0) {
6771 pr_err("Failed to create ALSA volume control: %d\n",
6772 rc);
6773 goto err_exit;
6775 data->ctl_vol_id = &ctl_vol->id;
6778 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6779 rc = snd_ctl_add(card, ctl_mute);
6780 if (rc < 0) {
6781 pr_err("Failed to create ALSA mute control: %d\n", rc);
6782 goto err_exit;
6784 data->ctl_mute_id = &ctl_mute->id;
6786 snd_card_set_dev(card, &tpacpi_pdev->dev);
6787 rc = snd_card_register(card);
6788 if (rc < 0) {
6789 pr_err("Failed to register ALSA card: %d\n", rc);
6790 goto err_exit;
6793 alsa_card = card;
6794 return 0;
6796 err_exit:
6797 snd_card_free(card);
6798 return 1;
6801 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6802 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6804 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6805 /* Whitelist volume level on all IBM by default */
6806 { .vendor = PCI_VENDOR_ID_IBM,
6807 .bios = TPACPI_MATCH_ANY,
6808 .ec = TPACPI_MATCH_ANY,
6809 .quirks = TPACPI_VOL_Q_LEVEL },
6811 /* Lenovo models with volume control (needs confirmation) */
6812 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6813 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6814 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6815 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6816 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6817 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6818 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6820 /* Whitelist mute-only on all Lenovo by default */
6821 { .vendor = PCI_VENDOR_ID_LENOVO,
6822 .bios = TPACPI_MATCH_ANY,
6823 .ec = TPACPI_MATCH_ANY,
6824 .quirks = TPACPI_VOL_Q_MUTEONLY }
6827 static int __init volume_init(struct ibm_init_struct *iibm)
6829 unsigned long quirks;
6830 int rc;
6832 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6834 mutex_init(&volume_mutex);
6837 * Check for module parameter bogosity, note that we
6838 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6839 * able to detect "unspecified"
6841 if (volume_mode > TPACPI_VOL_MODE_MAX)
6842 return -EINVAL;
6844 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6845 pr_err("UCMS step volume mode not implemented, "
6846 "please contact %s\n", TPACPI_MAIL);
6847 return 1;
6850 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6851 return -EINVAL;
6854 * The ALSA mixer is our primary interface.
6855 * When disabled, don't install the subdriver at all
6857 if (!alsa_enable) {
6858 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6859 "ALSA mixer disabled by parameter, "
6860 "not loading volume subdriver...\n");
6861 return 1;
6864 quirks = tpacpi_check_quirks(volume_quirk_table,
6865 ARRAY_SIZE(volume_quirk_table));
6867 switch (volume_capabilities) {
6868 case TPACPI_VOL_CAP_AUTO:
6869 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6870 tp_features.mixer_no_level_control = 1;
6871 else if (quirks & TPACPI_VOL_Q_LEVEL)
6872 tp_features.mixer_no_level_control = 0;
6873 else
6874 return 1; /* no mixer */
6875 break;
6876 case TPACPI_VOL_CAP_VOLMUTE:
6877 tp_features.mixer_no_level_control = 0;
6878 break;
6879 case TPACPI_VOL_CAP_MUTEONLY:
6880 tp_features.mixer_no_level_control = 1;
6881 break;
6882 default:
6883 return 1;
6886 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6887 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6888 "using user-supplied volume_capabilities=%d\n",
6889 volume_capabilities);
6891 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6892 volume_mode == TPACPI_VOL_MODE_MAX) {
6893 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6895 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6896 "driver auto-selected volume_mode=%d\n",
6897 volume_mode);
6898 } else {
6899 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6900 "using user-supplied volume_mode=%d\n",
6901 volume_mode);
6904 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6905 "mute is supported, volume control is %s\n",
6906 str_supported(!tp_features.mixer_no_level_control));
6908 rc = volume_create_alsa_mixer();
6909 if (rc) {
6910 pr_err("Could not create the ALSA mixer interface\n");
6911 return rc;
6914 pr_info("Console audio control enabled, mode: %s\n",
6915 (volume_control_allowed) ?
6916 "override (read/write)" :
6917 "monitor (read only)");
6919 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6920 "registering volume hotkeys as change notification\n");
6921 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6922 | TP_ACPI_HKEY_VOLUP_MASK
6923 | TP_ACPI_HKEY_VOLDWN_MASK
6924 | TP_ACPI_HKEY_MUTE_MASK);
6926 return 0;
6929 static int volume_read(struct seq_file *m)
6931 u8 status;
6933 if (volume_get_status(&status) < 0) {
6934 seq_printf(m, "level:\t\tunreadable\n");
6935 } else {
6936 if (tp_features.mixer_no_level_control)
6937 seq_printf(m, "level:\t\tunsupported\n");
6938 else
6939 seq_printf(m, "level:\t\t%d\n",
6940 status & TP_EC_AUDIO_LVL_MSK);
6942 seq_printf(m, "mute:\t\t%s\n",
6943 onoff(status, TP_EC_AUDIO_MUTESW));
6945 if (volume_control_allowed) {
6946 seq_printf(m, "commands:\tunmute, mute\n");
6947 if (!tp_features.mixer_no_level_control) {
6948 seq_printf(m,
6949 "commands:\tup, down\n");
6950 seq_printf(m,
6951 "commands:\tlevel <level>"
6952 " (<level> is 0-%d)\n",
6953 TP_EC_VOLUME_MAX);
6958 return 0;
6961 static int volume_write(char *buf)
6963 u8 s;
6964 u8 new_level, new_mute;
6965 int l;
6966 char *cmd;
6967 int rc;
6970 * We do allow volume control at driver startup, so that the
6971 * user can set initial state through the volume=... parameter hack.
6973 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6974 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6975 tp_warned.volume_ctrl_forbidden = 1;
6976 pr_notice("Console audio control in monitor mode, "
6977 "changes are not allowed\n");
6978 pr_notice("Use the volume_control=1 module parameter "
6979 "to enable volume control\n");
6981 return -EPERM;
6984 rc = volume_get_status(&s);
6985 if (rc < 0)
6986 return rc;
6988 new_level = s & TP_EC_AUDIO_LVL_MSK;
6989 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6991 while ((cmd = next_cmd(&buf))) {
6992 if (!tp_features.mixer_no_level_control) {
6993 if (strlencmp(cmd, "up") == 0) {
6994 if (new_mute)
6995 new_mute = 0;
6996 else if (new_level < TP_EC_VOLUME_MAX)
6997 new_level++;
6998 continue;
6999 } else if (strlencmp(cmd, "down") == 0) {
7000 if (new_mute)
7001 new_mute = 0;
7002 else if (new_level > 0)
7003 new_level--;
7004 continue;
7005 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7006 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7007 new_level = l;
7008 continue;
7011 if (strlencmp(cmd, "mute") == 0)
7012 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7013 else if (strlencmp(cmd, "unmute") == 0)
7014 new_mute = 0;
7015 else
7016 return -EINVAL;
7019 if (tp_features.mixer_no_level_control) {
7020 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7021 new_mute ? "" : "un");
7022 rc = volume_set_mute(!!new_mute);
7023 } else {
7024 tpacpi_disclose_usertask("procfs volume",
7025 "%smute and set level to %d\n",
7026 new_mute ? "" : "un", new_level);
7027 rc = volume_set_status(new_mute | new_level);
7029 volume_alsa_notify_change();
7031 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7034 static struct ibm_struct volume_driver_data = {
7035 .name = "volume",
7036 .read = volume_read,
7037 .write = volume_write,
7038 .exit = volume_exit,
7039 .suspend = volume_suspend,
7040 .resume = volume_resume,
7041 .shutdown = volume_shutdown,
7044 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7046 #define alsa_card NULL
7048 static void inline volume_alsa_notify_change(void)
7052 static int __init volume_init(struct ibm_init_struct *iibm)
7054 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7056 return 1;
7059 static struct ibm_struct volume_driver_data = {
7060 .name = "volume",
7063 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7065 /*************************************************************************
7066 * Fan subdriver
7070 * FAN ACCESS MODES
7072 * TPACPI_FAN_RD_ACPI_GFAN:
7073 * ACPI GFAN method: returns fan level
7075 * see TPACPI_FAN_WR_ACPI_SFAN
7076 * EC 0x2f (HFSP) not available if GFAN exists
7078 * TPACPI_FAN_WR_ACPI_SFAN:
7079 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7081 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7082 * it for writing.
7084 * TPACPI_FAN_WR_TPEC:
7085 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7086 * Supported on almost all ThinkPads
7088 * Fan speed changes of any sort (including those caused by the
7089 * disengaged mode) are usually done slowly by the firmware as the
7090 * maximum amount of fan duty cycle change per second seems to be
7091 * limited.
7093 * Reading is not available if GFAN exists.
7094 * Writing is not available if SFAN exists.
7096 * Bits
7097 * 7 automatic mode engaged;
7098 * (default operation mode of the ThinkPad)
7099 * fan level is ignored in this mode.
7100 * 6 full speed mode (takes precedence over bit 7);
7101 * not available on all thinkpads. May disable
7102 * the tachometer while the fan controller ramps up
7103 * the speed (which can take up to a few *minutes*).
7104 * Speeds up fan to 100% duty-cycle, which is far above
7105 * the standard RPM levels. It is not impossible that
7106 * it could cause hardware damage.
7107 * 5-3 unused in some models. Extra bits for fan level
7108 * in others, but still useless as all values above
7109 * 7 map to the same speed as level 7 in these models.
7110 * 2-0 fan level (0..7 usually)
7111 * 0x00 = stop
7112 * 0x07 = max (set when temperatures critical)
7113 * Some ThinkPads may have other levels, see
7114 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7116 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7117 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7118 * does so, its initial value is meaningless (0x07).
7120 * For firmware bugs, refer to:
7121 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7123 * ----
7125 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7126 * Main fan tachometer reading (in RPM)
7128 * This register is present on all ThinkPads with a new-style EC, and
7129 * it is known not to be present on the A21m/e, and T22, as there is
7130 * something else in offset 0x84 according to the ACPI DSDT. Other
7131 * ThinkPads from this same time period (and earlier) probably lack the
7132 * tachometer as well.
7134 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7135 * was never fixed by IBM to report the EC firmware version string
7136 * probably support the tachometer (like the early X models), so
7137 * detecting it is quite hard. We need more data to know for sure.
7139 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7140 * might result.
7142 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7143 * mode.
7145 * For firmware bugs, refer to:
7146 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7148 * ----
7150 * ThinkPad EC register 0x31 bit 0 (only on select models)
7152 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7153 * show the speed of the main fan. When bit 0 of EC register 0x31
7154 * is one, the tachometer registers show the speed of the auxiliary
7155 * fan.
7157 * Fan control seems to affect both fans, regardless of the state
7158 * of this bit.
7160 * So far, only the firmware for the X60/X61 non-tablet versions
7161 * seem to support this (firmware TP-7M).
7163 * TPACPI_FAN_WR_ACPI_FANS:
7164 * ThinkPad X31, X40, X41. Not available in the X60.
7166 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7167 * high speed. ACPI DSDT seems to map these three speeds to levels
7168 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7169 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7171 * The speeds are stored on handles
7172 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7174 * There are three default speed sets, accessible as handles:
7175 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7177 * ACPI DSDT switches which set is in use depending on various
7178 * factors.
7180 * TPACPI_FAN_WR_TPEC is also available and should be used to
7181 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7182 * but the ACPI tables just mention level 7.
7185 enum { /* Fan control constants */
7186 fan_status_offset = 0x2f, /* EC register 0x2f */
7187 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7188 * 0x84 must be read before 0x85 */
7189 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7190 bit 0 selects which fan is active */
7192 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7193 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7195 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7198 enum fan_status_access_mode {
7199 TPACPI_FAN_NONE = 0, /* No fan status or control */
7200 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7201 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7204 enum fan_control_access_mode {
7205 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7206 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7207 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7208 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7211 enum fan_control_commands {
7212 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7213 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7214 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7215 * and also watchdog cmd */
7218 static int fan_control_allowed;
7220 static enum fan_status_access_mode fan_status_access_mode;
7221 static enum fan_control_access_mode fan_control_access_mode;
7222 static enum fan_control_commands fan_control_commands;
7224 static u8 fan_control_initial_status;
7225 static u8 fan_control_desired_level;
7226 static u8 fan_control_resume_level;
7227 static int fan_watchdog_maxinterval;
7229 static struct mutex fan_mutex;
7231 static void fan_watchdog_fire(struct work_struct *ignored);
7232 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7234 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7235 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7236 "\\FSPD", /* 600e/x, 770e, 770x */
7237 ); /* all others */
7238 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7239 "JFNS", /* 770x-JL */
7240 ); /* all others */
7243 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7244 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7245 * be in auto mode (0x80).
7247 * This is corrected by any write to HFSP either by the driver, or
7248 * by the firmware.
7250 * We assume 0x07 really means auto mode while this quirk is active,
7251 * as this is far more likely than the ThinkPad being in level 7,
7252 * which is only used by the firmware during thermal emergencies.
7254 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7255 * TP-70 (T43, R52), which are known to be buggy.
7258 static void fan_quirk1_setup(void)
7260 if (fan_control_initial_status == 0x07) {
7261 pr_notice("fan_init: initial fan status is unknown, "
7262 "assuming it is in auto mode\n");
7263 tp_features.fan_ctrl_status_undef = 1;
7267 static void fan_quirk1_handle(u8 *fan_status)
7269 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7270 if (*fan_status != fan_control_initial_status) {
7271 /* something changed the HFSP regisnter since
7272 * driver init time, so it is not undefined
7273 * anymore */
7274 tp_features.fan_ctrl_status_undef = 0;
7275 } else {
7276 /* Return most likely status. In fact, it
7277 * might be the only possible status */
7278 *fan_status = TP_EC_FAN_AUTO;
7283 /* Select main fan on X60/X61, NOOP on others */
7284 static bool fan_select_fan1(void)
7286 if (tp_features.second_fan) {
7287 u8 val;
7289 if (ec_read(fan_select_offset, &val) < 0)
7290 return false;
7291 val &= 0xFEU;
7292 if (ec_write(fan_select_offset, val) < 0)
7293 return false;
7295 return true;
7298 /* Select secondary fan on X60/X61 */
7299 static bool fan_select_fan2(void)
7301 u8 val;
7303 if (!tp_features.second_fan)
7304 return false;
7306 if (ec_read(fan_select_offset, &val) < 0)
7307 return false;
7308 val |= 0x01U;
7309 if (ec_write(fan_select_offset, val) < 0)
7310 return false;
7312 return true;
7316 * Call with fan_mutex held
7318 static void fan_update_desired_level(u8 status)
7320 if ((status &
7321 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7322 if (status > 7)
7323 fan_control_desired_level = 7;
7324 else
7325 fan_control_desired_level = status;
7329 static int fan_get_status(u8 *status)
7331 u8 s;
7333 /* TODO:
7334 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7336 switch (fan_status_access_mode) {
7337 case TPACPI_FAN_RD_ACPI_GFAN:
7338 /* 570, 600e/x, 770e, 770x */
7340 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7341 return -EIO;
7343 if (likely(status))
7344 *status = s & 0x07;
7346 break;
7348 case TPACPI_FAN_RD_TPEC:
7349 /* all except 570, 600e/x, 770e, 770x */
7350 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7351 return -EIO;
7353 if (likely(status)) {
7354 *status = s;
7355 fan_quirk1_handle(status);
7358 break;
7360 default:
7361 return -ENXIO;
7364 return 0;
7367 static int fan_get_status_safe(u8 *status)
7369 int rc;
7370 u8 s;
7372 if (mutex_lock_killable(&fan_mutex))
7373 return -ERESTARTSYS;
7374 rc = fan_get_status(&s);
7375 if (!rc)
7376 fan_update_desired_level(s);
7377 mutex_unlock(&fan_mutex);
7379 if (status)
7380 *status = s;
7382 return rc;
7385 static int fan_get_speed(unsigned int *speed)
7387 u8 hi, lo;
7389 switch (fan_status_access_mode) {
7390 case TPACPI_FAN_RD_TPEC:
7391 /* all except 570, 600e/x, 770e, 770x */
7392 if (unlikely(!fan_select_fan1()))
7393 return -EIO;
7394 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7395 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7396 return -EIO;
7398 if (likely(speed))
7399 *speed = (hi << 8) | lo;
7401 break;
7403 default:
7404 return -ENXIO;
7407 return 0;
7410 static int fan2_get_speed(unsigned int *speed)
7412 u8 hi, lo;
7413 bool rc;
7415 switch (fan_status_access_mode) {
7416 case TPACPI_FAN_RD_TPEC:
7417 /* all except 570, 600e/x, 770e, 770x */
7418 if (unlikely(!fan_select_fan2()))
7419 return -EIO;
7420 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7421 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7422 fan_select_fan1(); /* play it safe */
7423 if (rc)
7424 return -EIO;
7426 if (likely(speed))
7427 *speed = (hi << 8) | lo;
7429 break;
7431 default:
7432 return -ENXIO;
7435 return 0;
7438 static int fan_set_level(int level)
7440 if (!fan_control_allowed)
7441 return -EPERM;
7443 switch (fan_control_access_mode) {
7444 case TPACPI_FAN_WR_ACPI_SFAN:
7445 if (level >= 0 && level <= 7) {
7446 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7447 return -EIO;
7448 } else
7449 return -EINVAL;
7450 break;
7452 case TPACPI_FAN_WR_ACPI_FANS:
7453 case TPACPI_FAN_WR_TPEC:
7454 if (!(level & TP_EC_FAN_AUTO) &&
7455 !(level & TP_EC_FAN_FULLSPEED) &&
7456 ((level < 0) || (level > 7)))
7457 return -EINVAL;
7459 /* safety net should the EC not support AUTO
7460 * or FULLSPEED mode bits and just ignore them */
7461 if (level & TP_EC_FAN_FULLSPEED)
7462 level |= 7; /* safety min speed 7 */
7463 else if (level & TP_EC_FAN_AUTO)
7464 level |= 4; /* safety min speed 4 */
7466 if (!acpi_ec_write(fan_status_offset, level))
7467 return -EIO;
7468 else
7469 tp_features.fan_ctrl_status_undef = 0;
7470 break;
7472 default:
7473 return -ENXIO;
7476 vdbg_printk(TPACPI_DBG_FAN,
7477 "fan control: set fan control register to 0x%02x\n", level);
7478 return 0;
7481 static int fan_set_level_safe(int level)
7483 int rc;
7485 if (!fan_control_allowed)
7486 return -EPERM;
7488 if (mutex_lock_killable(&fan_mutex))
7489 return -ERESTARTSYS;
7491 if (level == TPACPI_FAN_LAST_LEVEL)
7492 level = fan_control_desired_level;
7494 rc = fan_set_level(level);
7495 if (!rc)
7496 fan_update_desired_level(level);
7498 mutex_unlock(&fan_mutex);
7499 return rc;
7502 static int fan_set_enable(void)
7504 u8 s;
7505 int rc;
7507 if (!fan_control_allowed)
7508 return -EPERM;
7510 if (mutex_lock_killable(&fan_mutex))
7511 return -ERESTARTSYS;
7513 switch (fan_control_access_mode) {
7514 case TPACPI_FAN_WR_ACPI_FANS:
7515 case TPACPI_FAN_WR_TPEC:
7516 rc = fan_get_status(&s);
7517 if (rc < 0)
7518 break;
7520 /* Don't go out of emergency fan mode */
7521 if (s != 7) {
7522 s &= 0x07;
7523 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7526 if (!acpi_ec_write(fan_status_offset, s))
7527 rc = -EIO;
7528 else {
7529 tp_features.fan_ctrl_status_undef = 0;
7530 rc = 0;
7532 break;
7534 case TPACPI_FAN_WR_ACPI_SFAN:
7535 rc = fan_get_status(&s);
7536 if (rc < 0)
7537 break;
7539 s &= 0x07;
7541 /* Set fan to at least level 4 */
7542 s |= 4;
7544 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7545 rc = -EIO;
7546 else
7547 rc = 0;
7548 break;
7550 default:
7551 rc = -ENXIO;
7554 mutex_unlock(&fan_mutex);
7556 if (!rc)
7557 vdbg_printk(TPACPI_DBG_FAN,
7558 "fan control: set fan control register to 0x%02x\n",
7560 return rc;
7563 static int fan_set_disable(void)
7565 int rc;
7567 if (!fan_control_allowed)
7568 return -EPERM;
7570 if (mutex_lock_killable(&fan_mutex))
7571 return -ERESTARTSYS;
7573 rc = 0;
7574 switch (fan_control_access_mode) {
7575 case TPACPI_FAN_WR_ACPI_FANS:
7576 case TPACPI_FAN_WR_TPEC:
7577 if (!acpi_ec_write(fan_status_offset, 0x00))
7578 rc = -EIO;
7579 else {
7580 fan_control_desired_level = 0;
7581 tp_features.fan_ctrl_status_undef = 0;
7583 break;
7585 case TPACPI_FAN_WR_ACPI_SFAN:
7586 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7587 rc = -EIO;
7588 else
7589 fan_control_desired_level = 0;
7590 break;
7592 default:
7593 rc = -ENXIO;
7596 if (!rc)
7597 vdbg_printk(TPACPI_DBG_FAN,
7598 "fan control: set fan control register to 0\n");
7600 mutex_unlock(&fan_mutex);
7601 return rc;
7604 static int fan_set_speed(int speed)
7606 int rc;
7608 if (!fan_control_allowed)
7609 return -EPERM;
7611 if (mutex_lock_killable(&fan_mutex))
7612 return -ERESTARTSYS;
7614 rc = 0;
7615 switch (fan_control_access_mode) {
7616 case TPACPI_FAN_WR_ACPI_FANS:
7617 if (speed >= 0 && speed <= 65535) {
7618 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7619 speed, speed, speed))
7620 rc = -EIO;
7621 } else
7622 rc = -EINVAL;
7623 break;
7625 default:
7626 rc = -ENXIO;
7629 mutex_unlock(&fan_mutex);
7630 return rc;
7633 static void fan_watchdog_reset(void)
7635 static int fan_watchdog_active;
7637 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7638 return;
7640 if (fan_watchdog_active)
7641 cancel_delayed_work(&fan_watchdog_task);
7643 if (fan_watchdog_maxinterval > 0 &&
7644 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7645 fan_watchdog_active = 1;
7646 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7647 msecs_to_jiffies(fan_watchdog_maxinterval
7648 * 1000))) {
7649 pr_err("failed to queue the fan watchdog, "
7650 "watchdog will not trigger\n");
7652 } else
7653 fan_watchdog_active = 0;
7656 static void fan_watchdog_fire(struct work_struct *ignored)
7658 int rc;
7660 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7661 return;
7663 pr_notice("fan watchdog: enabling fan\n");
7664 rc = fan_set_enable();
7665 if (rc < 0) {
7666 pr_err("fan watchdog: error %d while enabling fan, "
7667 "will try again later...\n", -rc);
7668 /* reschedule for later */
7669 fan_watchdog_reset();
7674 * SYSFS fan layout: hwmon compatible (device)
7676 * pwm*_enable:
7677 * 0: "disengaged" mode
7678 * 1: manual mode
7679 * 2: native EC "auto" mode (recommended, hardware default)
7681 * pwm*: set speed in manual mode, ignored otherwise.
7682 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7683 * interpolation.
7685 * fan*_input: tachometer reading, RPM
7688 * SYSFS fan layout: extensions
7690 * fan_watchdog (driver):
7691 * fan watchdog interval in seconds, 0 disables (default), max 120
7694 /* sysfs fan pwm1_enable ----------------------------------------------- */
7695 static ssize_t fan_pwm1_enable_show(struct device *dev,
7696 struct device_attribute *attr,
7697 char *buf)
7699 int res, mode;
7700 u8 status;
7702 res = fan_get_status_safe(&status);
7703 if (res)
7704 return res;
7706 if (status & TP_EC_FAN_FULLSPEED) {
7707 mode = 0;
7708 } else if (status & TP_EC_FAN_AUTO) {
7709 mode = 2;
7710 } else
7711 mode = 1;
7713 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7716 static ssize_t fan_pwm1_enable_store(struct device *dev,
7717 struct device_attribute *attr,
7718 const char *buf, size_t count)
7720 unsigned long t;
7721 int res, level;
7723 if (parse_strtoul(buf, 2, &t))
7724 return -EINVAL;
7726 tpacpi_disclose_usertask("hwmon pwm1_enable",
7727 "set fan mode to %lu\n", t);
7729 switch (t) {
7730 case 0:
7731 level = TP_EC_FAN_FULLSPEED;
7732 break;
7733 case 1:
7734 level = TPACPI_FAN_LAST_LEVEL;
7735 break;
7736 case 2:
7737 level = TP_EC_FAN_AUTO;
7738 break;
7739 case 3:
7740 /* reserved for software-controlled auto mode */
7741 return -ENOSYS;
7742 default:
7743 return -EINVAL;
7746 res = fan_set_level_safe(level);
7747 if (res == -ENXIO)
7748 return -EINVAL;
7749 else if (res < 0)
7750 return res;
7752 fan_watchdog_reset();
7754 return count;
7757 static struct device_attribute dev_attr_fan_pwm1_enable =
7758 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7759 fan_pwm1_enable_show, fan_pwm1_enable_store);
7761 /* sysfs fan pwm1 ------------------------------------------------------ */
7762 static ssize_t fan_pwm1_show(struct device *dev,
7763 struct device_attribute *attr,
7764 char *buf)
7766 int res;
7767 u8 status;
7769 res = fan_get_status_safe(&status);
7770 if (res)
7771 return res;
7773 if ((status &
7774 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7775 status = fan_control_desired_level;
7777 if (status > 7)
7778 status = 7;
7780 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7783 static ssize_t fan_pwm1_store(struct device *dev,
7784 struct device_attribute *attr,
7785 const char *buf, size_t count)
7787 unsigned long s;
7788 int rc;
7789 u8 status, newlevel;
7791 if (parse_strtoul(buf, 255, &s))
7792 return -EINVAL;
7794 tpacpi_disclose_usertask("hwmon pwm1",
7795 "set fan speed to %lu\n", s);
7797 /* scale down from 0-255 to 0-7 */
7798 newlevel = (s >> 5) & 0x07;
7800 if (mutex_lock_killable(&fan_mutex))
7801 return -ERESTARTSYS;
7803 rc = fan_get_status(&status);
7804 if (!rc && (status &
7805 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7806 rc = fan_set_level(newlevel);
7807 if (rc == -ENXIO)
7808 rc = -EINVAL;
7809 else if (!rc) {
7810 fan_update_desired_level(newlevel);
7811 fan_watchdog_reset();
7815 mutex_unlock(&fan_mutex);
7816 return (rc)? rc : count;
7819 static struct device_attribute dev_attr_fan_pwm1 =
7820 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7821 fan_pwm1_show, fan_pwm1_store);
7823 /* sysfs fan fan1_input ------------------------------------------------ */
7824 static ssize_t fan_fan1_input_show(struct device *dev,
7825 struct device_attribute *attr,
7826 char *buf)
7828 int res;
7829 unsigned int speed;
7831 res = fan_get_speed(&speed);
7832 if (res < 0)
7833 return res;
7835 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7838 static struct device_attribute dev_attr_fan_fan1_input =
7839 __ATTR(fan1_input, S_IRUGO,
7840 fan_fan1_input_show, NULL);
7842 /* sysfs fan fan2_input ------------------------------------------------ */
7843 static ssize_t fan_fan2_input_show(struct device *dev,
7844 struct device_attribute *attr,
7845 char *buf)
7847 int res;
7848 unsigned int speed;
7850 res = fan2_get_speed(&speed);
7851 if (res < 0)
7852 return res;
7854 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7857 static struct device_attribute dev_attr_fan_fan2_input =
7858 __ATTR(fan2_input, S_IRUGO,
7859 fan_fan2_input_show, NULL);
7861 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7862 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7863 char *buf)
7865 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7868 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7869 const char *buf, size_t count)
7871 unsigned long t;
7873 if (parse_strtoul(buf, 120, &t))
7874 return -EINVAL;
7876 if (!fan_control_allowed)
7877 return -EPERM;
7879 fan_watchdog_maxinterval = t;
7880 fan_watchdog_reset();
7882 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7884 return count;
7887 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7888 fan_fan_watchdog_show, fan_fan_watchdog_store);
7890 /* --------------------------------------------------------------------- */
7891 static struct attribute *fan_attributes[] = {
7892 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7893 &dev_attr_fan_fan1_input.attr,
7894 NULL, /* for fan2_input */
7895 NULL
7898 static const struct attribute_group fan_attr_group = {
7899 .attrs = fan_attributes,
7902 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7903 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7905 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7906 { .vendor = PCI_VENDOR_ID_IBM, \
7907 .bios = TPACPI_MATCH_ANY, \
7908 .ec = TPID(__id1, __id2), \
7909 .quirks = __quirks }
7911 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7912 { .vendor = PCI_VENDOR_ID_LENOVO, \
7913 .bios = TPACPI_MATCH_ANY, \
7914 .ec = TPID(__id1, __id2), \
7915 .quirks = __quirks }
7917 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7918 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7919 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7920 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7921 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7922 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7925 #undef TPACPI_FAN_QL
7926 #undef TPACPI_FAN_QI
7928 static int __init fan_init(struct ibm_init_struct *iibm)
7930 int rc;
7931 unsigned long quirks;
7933 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7934 "initializing fan subdriver\n");
7936 mutex_init(&fan_mutex);
7937 fan_status_access_mode = TPACPI_FAN_NONE;
7938 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7939 fan_control_commands = 0;
7940 fan_watchdog_maxinterval = 0;
7941 tp_features.fan_ctrl_status_undef = 0;
7942 tp_features.second_fan = 0;
7943 fan_control_desired_level = 7;
7945 if (tpacpi_is_ibm()) {
7946 TPACPI_ACPIHANDLE_INIT(fans);
7947 TPACPI_ACPIHANDLE_INIT(gfan);
7948 TPACPI_ACPIHANDLE_INIT(sfan);
7951 quirks = tpacpi_check_quirks(fan_quirk_table,
7952 ARRAY_SIZE(fan_quirk_table));
7954 if (gfan_handle) {
7955 /* 570, 600e/x, 770e, 770x */
7956 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7957 } else {
7958 /* all other ThinkPads: note that even old-style
7959 * ThinkPad ECs supports the fan control register */
7960 if (likely(acpi_ec_read(fan_status_offset,
7961 &fan_control_initial_status))) {
7962 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7963 if (quirks & TPACPI_FAN_Q1)
7964 fan_quirk1_setup();
7965 if (quirks & TPACPI_FAN_2FAN) {
7966 tp_features.second_fan = 1;
7967 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7968 "secondary fan support enabled\n");
7970 } else {
7971 pr_err("ThinkPad ACPI EC access misbehaving, "
7972 "fan status and control unavailable\n");
7973 return 1;
7977 if (sfan_handle) {
7978 /* 570, 770x-JL */
7979 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7980 fan_control_commands |=
7981 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7982 } else {
7983 if (!gfan_handle) {
7984 /* gfan without sfan means no fan control */
7985 /* all other models implement TP EC 0x2f control */
7987 if (fans_handle) {
7988 /* X31, X40, X41 */
7989 fan_control_access_mode =
7990 TPACPI_FAN_WR_ACPI_FANS;
7991 fan_control_commands |=
7992 TPACPI_FAN_CMD_SPEED |
7993 TPACPI_FAN_CMD_LEVEL |
7994 TPACPI_FAN_CMD_ENABLE;
7995 } else {
7996 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7997 fan_control_commands |=
7998 TPACPI_FAN_CMD_LEVEL |
7999 TPACPI_FAN_CMD_ENABLE;
8004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8005 "fan is %s, modes %d, %d\n",
8006 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8007 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8008 fan_status_access_mode, fan_control_access_mode);
8010 /* fan control master switch */
8011 if (!fan_control_allowed) {
8012 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8013 fan_control_commands = 0;
8014 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8015 "fan control features disabled by parameter\n");
8018 /* update fan_control_desired_level */
8019 if (fan_status_access_mode != TPACPI_FAN_NONE)
8020 fan_get_status_safe(NULL);
8022 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8023 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8024 if (tp_features.second_fan) {
8025 /* attach second fan tachometer */
8026 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8027 &dev_attr_fan_fan2_input.attr;
8029 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8030 &fan_attr_group);
8031 if (rc < 0)
8032 return rc;
8034 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8035 &driver_attr_fan_watchdog);
8036 if (rc < 0) {
8037 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8038 &fan_attr_group);
8039 return rc;
8041 return 0;
8042 } else
8043 return 1;
8046 static void fan_exit(void)
8048 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8049 "cancelling any pending fan watchdog tasks\n");
8051 /* FIXME: can we really do this unconditionally? */
8052 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8053 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8054 &driver_attr_fan_watchdog);
8056 cancel_delayed_work(&fan_watchdog_task);
8057 flush_workqueue(tpacpi_wq);
8060 static void fan_suspend(pm_message_t state)
8062 int rc;
8064 if (!fan_control_allowed)
8065 return;
8067 /* Store fan status in cache */
8068 fan_control_resume_level = 0;
8069 rc = fan_get_status_safe(&fan_control_resume_level);
8070 if (rc < 0)
8071 pr_notice("failed to read fan level for later "
8072 "restore during resume: %d\n", rc);
8074 /* if it is undefined, don't attempt to restore it.
8075 * KEEP THIS LAST */
8076 if (tp_features.fan_ctrl_status_undef)
8077 fan_control_resume_level = 0;
8080 static void fan_resume(void)
8082 u8 current_level = 7;
8083 bool do_set = false;
8084 int rc;
8086 /* DSDT *always* updates status on resume */
8087 tp_features.fan_ctrl_status_undef = 0;
8089 if (!fan_control_allowed ||
8090 !fan_control_resume_level ||
8091 (fan_get_status_safe(&current_level) < 0))
8092 return;
8094 switch (fan_control_access_mode) {
8095 case TPACPI_FAN_WR_ACPI_SFAN:
8096 /* never decrease fan level */
8097 do_set = (fan_control_resume_level > current_level);
8098 break;
8099 case TPACPI_FAN_WR_ACPI_FANS:
8100 case TPACPI_FAN_WR_TPEC:
8101 /* never decrease fan level, scale is:
8102 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8104 * We expect the firmware to set either 7 or AUTO, but we
8105 * handle FULLSPEED out of paranoia.
8107 * So, we can safely only restore FULLSPEED or 7, anything
8108 * else could slow the fan. Restoring AUTO is useless, at
8109 * best that's exactly what the DSDT already set (it is the
8110 * slower it uses).
8112 * Always keep in mind that the DSDT *will* have set the
8113 * fans to what the vendor supposes is the best level. We
8114 * muck with it only to speed the fan up.
8116 if (fan_control_resume_level != 7 &&
8117 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8118 return;
8119 else
8120 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8121 (current_level != fan_control_resume_level);
8122 break;
8123 default:
8124 return;
8126 if (do_set) {
8127 pr_notice("restoring fan level to 0x%02x\n",
8128 fan_control_resume_level);
8129 rc = fan_set_level_safe(fan_control_resume_level);
8130 if (rc < 0)
8131 pr_notice("failed to restore fan level: %d\n", rc);
8135 static int fan_read(struct seq_file *m)
8137 int rc;
8138 u8 status;
8139 unsigned int speed = 0;
8141 switch (fan_status_access_mode) {
8142 case TPACPI_FAN_RD_ACPI_GFAN:
8143 /* 570, 600e/x, 770e, 770x */
8144 rc = fan_get_status_safe(&status);
8145 if (rc < 0)
8146 return rc;
8148 seq_printf(m, "status:\t\t%s\n"
8149 "level:\t\t%d\n",
8150 (status != 0) ? "enabled" : "disabled", status);
8151 break;
8153 case TPACPI_FAN_RD_TPEC:
8154 /* all except 570, 600e/x, 770e, 770x */
8155 rc = fan_get_status_safe(&status);
8156 if (rc < 0)
8157 return rc;
8159 seq_printf(m, "status:\t\t%s\n",
8160 (status != 0) ? "enabled" : "disabled");
8162 rc = fan_get_speed(&speed);
8163 if (rc < 0)
8164 return rc;
8166 seq_printf(m, "speed:\t\t%d\n", speed);
8168 if (status & TP_EC_FAN_FULLSPEED)
8169 /* Disengaged mode takes precedence */
8170 seq_printf(m, "level:\t\tdisengaged\n");
8171 else if (status & TP_EC_FAN_AUTO)
8172 seq_printf(m, "level:\t\tauto\n");
8173 else
8174 seq_printf(m, "level:\t\t%d\n", status);
8175 break;
8177 case TPACPI_FAN_NONE:
8178 default:
8179 seq_printf(m, "status:\t\tnot supported\n");
8182 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8183 seq_printf(m, "commands:\tlevel <level>");
8185 switch (fan_control_access_mode) {
8186 case TPACPI_FAN_WR_ACPI_SFAN:
8187 seq_printf(m, " (<level> is 0-7)\n");
8188 break;
8190 default:
8191 seq_printf(m, " (<level> is 0-7, "
8192 "auto, disengaged, full-speed)\n");
8193 break;
8197 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8198 seq_printf(m, "commands:\tenable, disable\n"
8199 "commands:\twatchdog <timeout> (<timeout> "
8200 "is 0 (off), 1-120 (seconds))\n");
8202 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8203 seq_printf(m, "commands:\tspeed <speed>"
8204 " (<speed> is 0-65535)\n");
8206 return 0;
8209 static int fan_write_cmd_level(const char *cmd, int *rc)
8211 int level;
8213 if (strlencmp(cmd, "level auto") == 0)
8214 level = TP_EC_FAN_AUTO;
8215 else if ((strlencmp(cmd, "level disengaged") == 0) |
8216 (strlencmp(cmd, "level full-speed") == 0))
8217 level = TP_EC_FAN_FULLSPEED;
8218 else if (sscanf(cmd, "level %d", &level) != 1)
8219 return 0;
8221 *rc = fan_set_level_safe(level);
8222 if (*rc == -ENXIO)
8223 pr_err("level command accepted for unsupported access mode %d\n",
8224 fan_control_access_mode);
8225 else if (!*rc)
8226 tpacpi_disclose_usertask("procfs fan",
8227 "set level to %d\n", level);
8229 return 1;
8232 static int fan_write_cmd_enable(const char *cmd, int *rc)
8234 if (strlencmp(cmd, "enable") != 0)
8235 return 0;
8237 *rc = fan_set_enable();
8238 if (*rc == -ENXIO)
8239 pr_err("enable command accepted for unsupported access mode %d\n",
8240 fan_control_access_mode);
8241 else if (!*rc)
8242 tpacpi_disclose_usertask("procfs fan", "enable\n");
8244 return 1;
8247 static int fan_write_cmd_disable(const char *cmd, int *rc)
8249 if (strlencmp(cmd, "disable") != 0)
8250 return 0;
8252 *rc = fan_set_disable();
8253 if (*rc == -ENXIO)
8254 pr_err("disable command accepted for unsupported access mode %d\n",
8255 fan_control_access_mode);
8256 else if (!*rc)
8257 tpacpi_disclose_usertask("procfs fan", "disable\n");
8259 return 1;
8262 static int fan_write_cmd_speed(const char *cmd, int *rc)
8264 int speed;
8266 /* TODO:
8267 * Support speed <low> <medium> <high> ? */
8269 if (sscanf(cmd, "speed %d", &speed) != 1)
8270 return 0;
8272 *rc = fan_set_speed(speed);
8273 if (*rc == -ENXIO)
8274 pr_err("speed command accepted for unsupported access mode %d\n",
8275 fan_control_access_mode);
8276 else if (!*rc)
8277 tpacpi_disclose_usertask("procfs fan",
8278 "set speed to %d\n", speed);
8280 return 1;
8283 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8285 int interval;
8287 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8288 return 0;
8290 if (interval < 0 || interval > 120)
8291 *rc = -EINVAL;
8292 else {
8293 fan_watchdog_maxinterval = interval;
8294 tpacpi_disclose_usertask("procfs fan",
8295 "set watchdog timer to %d\n",
8296 interval);
8299 return 1;
8302 static int fan_write(char *buf)
8304 char *cmd;
8305 int rc = 0;
8307 while (!rc && (cmd = next_cmd(&buf))) {
8308 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8309 fan_write_cmd_level(cmd, &rc)) &&
8310 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8311 (fan_write_cmd_enable(cmd, &rc) ||
8312 fan_write_cmd_disable(cmd, &rc) ||
8313 fan_write_cmd_watchdog(cmd, &rc))) &&
8314 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8315 fan_write_cmd_speed(cmd, &rc))
8317 rc = -EINVAL;
8318 else if (!rc)
8319 fan_watchdog_reset();
8322 return rc;
8325 static struct ibm_struct fan_driver_data = {
8326 .name = "fan",
8327 .read = fan_read,
8328 .write = fan_write,
8329 .exit = fan_exit,
8330 .suspend = fan_suspend,
8331 .resume = fan_resume,
8334 /****************************************************************************
8335 ****************************************************************************
8337 * Infrastructure
8339 ****************************************************************************
8340 ****************************************************************************/
8343 * HKEY event callout for other subdrivers go here
8344 * (yes, it is ugly, but it is quick, safe, and gets the job done
8346 static void tpacpi_driver_event(const unsigned int hkey_event)
8348 if (ibm_backlight_device) {
8349 switch (hkey_event) {
8350 case TP_HKEY_EV_BRGHT_UP:
8351 case TP_HKEY_EV_BRGHT_DOWN:
8352 tpacpi_brightness_notify_change();
8355 if (alsa_card) {
8356 switch (hkey_event) {
8357 case TP_HKEY_EV_VOL_UP:
8358 case TP_HKEY_EV_VOL_DOWN:
8359 case TP_HKEY_EV_VOL_MUTE:
8360 volume_alsa_notify_change();
8365 static void hotkey_driver_event(const unsigned int scancode)
8367 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8370 /* sysfs name ---------------------------------------------------------- */
8371 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8372 struct device_attribute *attr,
8373 char *buf)
8375 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8378 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8379 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8381 /* --------------------------------------------------------------------- */
8383 /* /proc support */
8384 static struct proc_dir_entry *proc_dir;
8387 * Module and infrastructure proble, init and exit handling
8390 static int force_load;
8392 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8393 static const char * __init str_supported(int is_supported)
8395 static char text_unsupported[] __initdata = "not supported";
8397 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8399 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8401 static void ibm_exit(struct ibm_struct *ibm)
8403 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8405 list_del_init(&ibm->all_drivers);
8407 if (ibm->flags.acpi_notify_installed) {
8408 dbg_printk(TPACPI_DBG_EXIT,
8409 "%s: acpi_remove_notify_handler\n", ibm->name);
8410 BUG_ON(!ibm->acpi);
8411 acpi_remove_notify_handler(*ibm->acpi->handle,
8412 ibm->acpi->type,
8413 dispatch_acpi_notify);
8414 ibm->flags.acpi_notify_installed = 0;
8417 if (ibm->flags.proc_created) {
8418 dbg_printk(TPACPI_DBG_EXIT,
8419 "%s: remove_proc_entry\n", ibm->name);
8420 remove_proc_entry(ibm->name, proc_dir);
8421 ibm->flags.proc_created = 0;
8424 if (ibm->flags.acpi_driver_registered) {
8425 dbg_printk(TPACPI_DBG_EXIT,
8426 "%s: acpi_bus_unregister_driver\n", ibm->name);
8427 BUG_ON(!ibm->acpi);
8428 acpi_bus_unregister_driver(ibm->acpi->driver);
8429 kfree(ibm->acpi->driver);
8430 ibm->acpi->driver = NULL;
8431 ibm->flags.acpi_driver_registered = 0;
8434 if (ibm->flags.init_called && ibm->exit) {
8435 ibm->exit();
8436 ibm->flags.init_called = 0;
8439 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8442 static int __init ibm_init(struct ibm_init_struct *iibm)
8444 int ret;
8445 struct ibm_struct *ibm = iibm->data;
8446 struct proc_dir_entry *entry;
8448 BUG_ON(ibm == NULL);
8450 INIT_LIST_HEAD(&ibm->all_drivers);
8452 if (ibm->flags.experimental && !experimental)
8453 return 0;
8455 dbg_printk(TPACPI_DBG_INIT,
8456 "probing for %s\n", ibm->name);
8458 if (iibm->init) {
8459 ret = iibm->init(iibm);
8460 if (ret > 0)
8461 return 0; /* probe failed */
8462 if (ret)
8463 return ret;
8465 ibm->flags.init_called = 1;
8468 if (ibm->acpi) {
8469 if (ibm->acpi->hid) {
8470 ret = register_tpacpi_subdriver(ibm);
8471 if (ret)
8472 goto err_out;
8475 if (ibm->acpi->notify) {
8476 ret = setup_acpi_notify(ibm);
8477 if (ret == -ENODEV) {
8478 pr_notice("disabling subdriver %s\n",
8479 ibm->name);
8480 ret = 0;
8481 goto err_out;
8483 if (ret < 0)
8484 goto err_out;
8488 dbg_printk(TPACPI_DBG_INIT,
8489 "%s installed\n", ibm->name);
8491 if (ibm->read) {
8492 mode_t mode = iibm->base_procfs_mode;
8494 if (!mode)
8495 mode = S_IRUGO;
8496 if (ibm->write)
8497 mode |= S_IWUSR;
8498 entry = proc_create_data(ibm->name, mode, proc_dir,
8499 &dispatch_proc_fops, ibm);
8500 if (!entry) {
8501 pr_err("unable to create proc entry %s\n", ibm->name);
8502 ret = -ENODEV;
8503 goto err_out;
8505 ibm->flags.proc_created = 1;
8508 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8510 return 0;
8512 err_out:
8513 dbg_printk(TPACPI_DBG_INIT,
8514 "%s: at error exit path with result %d\n",
8515 ibm->name, ret);
8517 ibm_exit(ibm);
8518 return (ret < 0)? ret : 0;
8521 /* Probing */
8523 static bool __pure __init tpacpi_is_fw_digit(const char c)
8525 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8528 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8529 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8530 const char t)
8532 return s && strlen(s) >= 8 &&
8533 tpacpi_is_fw_digit(s[0]) &&
8534 tpacpi_is_fw_digit(s[1]) &&
8535 s[2] == t && s[3] == 'T' &&
8536 tpacpi_is_fw_digit(s[4]) &&
8537 tpacpi_is_fw_digit(s[5]);
8540 /* returns 0 - probe ok, or < 0 - probe error.
8541 * Probe ok doesn't mean thinkpad found.
8542 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8543 static int __must_check __init get_thinkpad_model_data(
8544 struct thinkpad_id_data *tp)
8546 const struct dmi_device *dev = NULL;
8547 char ec_fw_string[18];
8548 char const *s;
8550 if (!tp)
8551 return -EINVAL;
8553 memset(tp, 0, sizeof(*tp));
8555 if (dmi_name_in_vendors("IBM"))
8556 tp->vendor = PCI_VENDOR_ID_IBM;
8557 else if (dmi_name_in_vendors("LENOVO"))
8558 tp->vendor = PCI_VENDOR_ID_LENOVO;
8559 else
8560 return 0;
8562 s = dmi_get_system_info(DMI_BIOS_VERSION);
8563 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8564 if (s && !tp->bios_version_str)
8565 return -ENOMEM;
8567 /* Really ancient ThinkPad 240X will fail this, which is fine */
8568 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8569 return 0;
8571 tp->bios_model = tp->bios_version_str[0]
8572 | (tp->bios_version_str[1] << 8);
8573 tp->bios_release = (tp->bios_version_str[4] << 8)
8574 | tp->bios_version_str[5];
8577 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8578 * X32 or newer, all Z series; Some models must have an
8579 * up-to-date BIOS or they will not be detected.
8581 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8583 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8584 if (sscanf(dev->name,
8585 "IBM ThinkPad Embedded Controller -[%17c",
8586 ec_fw_string) == 1) {
8587 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8588 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8590 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8591 if (!tp->ec_version_str)
8592 return -ENOMEM;
8594 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8595 tp->ec_model = ec_fw_string[0]
8596 | (ec_fw_string[1] << 8);
8597 tp->ec_release = (ec_fw_string[4] << 8)
8598 | ec_fw_string[5];
8599 } else {
8600 pr_notice("ThinkPad firmware release %s "
8601 "doesn't match the known patterns\n",
8602 ec_fw_string);
8603 pr_notice("please report this to %s\n",
8604 TPACPI_MAIL);
8606 break;
8610 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8611 if (s && !strnicmp(s, "ThinkPad", 8)) {
8612 tp->model_str = kstrdup(s, GFP_KERNEL);
8613 if (!tp->model_str)
8614 return -ENOMEM;
8617 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8618 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8619 if (s && !tp->nummodel_str)
8620 return -ENOMEM;
8622 return 0;
8625 static int __init probe_for_thinkpad(void)
8627 int is_thinkpad;
8629 if (acpi_disabled)
8630 return -ENODEV;
8632 /* It would be dangerous to run the driver in this case */
8633 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8634 return -ENODEV;
8637 * Non-ancient models have better DMI tagging, but very old models
8638 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8640 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8641 (thinkpad_id.ec_model != 0) ||
8642 tpacpi_is_fw_known();
8644 /* The EC handler is required */
8645 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8646 if (!ec_handle) {
8647 if (is_thinkpad)
8648 pr_err("Not yet supported ThinkPad detected!\n");
8649 return -ENODEV;
8652 if (!is_thinkpad && !force_load)
8653 return -ENODEV;
8655 return 0;
8658 static void __init thinkpad_acpi_init_banner(void)
8660 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8661 pr_info("%s\n", TPACPI_URL);
8663 pr_info("ThinkPad BIOS %s, EC %s\n",
8664 (thinkpad_id.bios_version_str) ?
8665 thinkpad_id.bios_version_str : "unknown",
8666 (thinkpad_id.ec_version_str) ?
8667 thinkpad_id.ec_version_str : "unknown");
8669 BUG_ON(!thinkpad_id.vendor);
8671 if (thinkpad_id.model_str)
8672 pr_info("%s %s, model %s\n",
8673 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8674 "IBM" : ((thinkpad_id.vendor ==
8675 PCI_VENDOR_ID_LENOVO) ?
8676 "Lenovo" : "Unknown vendor"),
8677 thinkpad_id.model_str,
8678 (thinkpad_id.nummodel_str) ?
8679 thinkpad_id.nummodel_str : "unknown");
8682 /* Module init, exit, parameters */
8684 static struct ibm_init_struct ibms_init[] __initdata = {
8686 .data = &thinkpad_acpi_driver_data,
8689 .init = hotkey_init,
8690 .data = &hotkey_driver_data,
8693 .init = bluetooth_init,
8694 .data = &bluetooth_driver_data,
8697 .init = wan_init,
8698 .data = &wan_driver_data,
8701 .init = uwb_init,
8702 .data = &uwb_driver_data,
8704 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8706 .init = video_init,
8707 .base_procfs_mode = S_IRUSR,
8708 .data = &video_driver_data,
8710 #endif
8712 .init = light_init,
8713 .data = &light_driver_data,
8716 .init = cmos_init,
8717 .data = &cmos_driver_data,
8720 .init = led_init,
8721 .data = &led_driver_data,
8724 .init = beep_init,
8725 .data = &beep_driver_data,
8728 .init = thermal_init,
8729 .data = &thermal_driver_data,
8732 .init = brightness_init,
8733 .data = &brightness_driver_data,
8736 .init = volume_init,
8737 .data = &volume_driver_data,
8740 .init = fan_init,
8741 .data = &fan_driver_data,
8745 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8747 unsigned int i;
8748 struct ibm_struct *ibm;
8750 if (!kp || !kp->name || !val)
8751 return -EINVAL;
8753 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8754 ibm = ibms_init[i].data;
8755 WARN_ON(ibm == NULL);
8757 if (!ibm || !ibm->name)
8758 continue;
8760 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8761 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8762 return -ENOSPC;
8763 strcpy(ibms_init[i].param, val);
8764 strcat(ibms_init[i].param, ",");
8765 return 0;
8769 return -EINVAL;
8772 module_param(experimental, int, 0444);
8773 MODULE_PARM_DESC(experimental,
8774 "Enables experimental features when non-zero");
8776 module_param_named(debug, dbg_level, uint, 0);
8777 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8779 module_param(force_load, bool, 0444);
8780 MODULE_PARM_DESC(force_load,
8781 "Attempts to load the driver even on a "
8782 "mis-identified ThinkPad when true");
8784 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8785 MODULE_PARM_DESC(fan_control,
8786 "Enables setting fan parameters features when true");
8788 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8789 MODULE_PARM_DESC(brightness_mode,
8790 "Selects brightness control strategy: "
8791 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8793 module_param(brightness_enable, uint, 0444);
8794 MODULE_PARM_DESC(brightness_enable,
8795 "Enables backlight control when 1, disables when 0");
8797 module_param(hotkey_report_mode, uint, 0444);
8798 MODULE_PARM_DESC(hotkey_report_mode,
8799 "used for backwards compatibility with userspace, "
8800 "see documentation");
8802 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8803 module_param_named(volume_mode, volume_mode, uint, 0444);
8804 MODULE_PARM_DESC(volume_mode,
8805 "Selects volume control strategy: "
8806 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8808 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8809 MODULE_PARM_DESC(volume_capabilities,
8810 "Selects the mixer capabilites: "
8811 "0=auto, 1=volume and mute, 2=mute only");
8813 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8814 MODULE_PARM_DESC(volume_control,
8815 "Enables software override for the console audio "
8816 "control when true");
8818 /* ALSA module API parameters */
8819 module_param_named(index, alsa_index, int, 0444);
8820 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8821 module_param_named(id, alsa_id, charp, 0444);
8822 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8823 module_param_named(enable, alsa_enable, bool, 0444);
8824 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8825 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8827 #define TPACPI_PARAM(feature) \
8828 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8829 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8830 "at module load, see documentation")
8832 TPACPI_PARAM(hotkey);
8833 TPACPI_PARAM(bluetooth);
8834 TPACPI_PARAM(video);
8835 TPACPI_PARAM(light);
8836 TPACPI_PARAM(cmos);
8837 TPACPI_PARAM(led);
8838 TPACPI_PARAM(beep);
8839 TPACPI_PARAM(brightness);
8840 TPACPI_PARAM(volume);
8841 TPACPI_PARAM(fan);
8843 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8844 module_param(dbg_wlswemul, uint, 0444);
8845 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8846 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8847 MODULE_PARM_DESC(wlsw_state,
8848 "Initial state of the emulated WLSW switch");
8850 module_param(dbg_bluetoothemul, uint, 0444);
8851 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8852 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8853 MODULE_PARM_DESC(bluetooth_state,
8854 "Initial state of the emulated bluetooth switch");
8856 module_param(dbg_wwanemul, uint, 0444);
8857 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8858 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8859 MODULE_PARM_DESC(wwan_state,
8860 "Initial state of the emulated WWAN switch");
8862 module_param(dbg_uwbemul, uint, 0444);
8863 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8864 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8865 MODULE_PARM_DESC(uwb_state,
8866 "Initial state of the emulated UWB switch");
8867 #endif
8869 static void thinkpad_acpi_module_exit(void)
8871 struct ibm_struct *ibm, *itmp;
8873 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8875 list_for_each_entry_safe_reverse(ibm, itmp,
8876 &tpacpi_all_drivers,
8877 all_drivers) {
8878 ibm_exit(ibm);
8881 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8883 if (tpacpi_inputdev) {
8884 if (tp_features.input_device_registered)
8885 input_unregister_device(tpacpi_inputdev);
8886 else
8887 input_free_device(tpacpi_inputdev);
8890 if (tpacpi_hwmon)
8891 hwmon_device_unregister(tpacpi_hwmon);
8893 if (tp_features.sensors_pdev_attrs_registered)
8894 device_remove_file(&tpacpi_sensors_pdev->dev,
8895 &dev_attr_thinkpad_acpi_pdev_name);
8896 if (tpacpi_sensors_pdev)
8897 platform_device_unregister(tpacpi_sensors_pdev);
8898 if (tpacpi_pdev)
8899 platform_device_unregister(tpacpi_pdev);
8901 if (tp_features.sensors_pdrv_attrs_registered)
8902 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8903 if (tp_features.platform_drv_attrs_registered)
8904 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8906 if (tp_features.sensors_pdrv_registered)
8907 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8909 if (tp_features.platform_drv_registered)
8910 platform_driver_unregister(&tpacpi_pdriver);
8912 if (proc_dir)
8913 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8915 if (tpacpi_wq)
8916 destroy_workqueue(tpacpi_wq);
8918 kfree(thinkpad_id.bios_version_str);
8919 kfree(thinkpad_id.ec_version_str);
8920 kfree(thinkpad_id.model_str);
8924 static int __init thinkpad_acpi_module_init(void)
8926 int ret, i;
8928 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8930 /* Parameter checking */
8931 if (hotkey_report_mode > 2)
8932 return -EINVAL;
8934 /* Driver-level probe */
8936 ret = get_thinkpad_model_data(&thinkpad_id);
8937 if (ret) {
8938 pr_err("unable to get DMI data: %d\n", ret);
8939 thinkpad_acpi_module_exit();
8940 return ret;
8942 ret = probe_for_thinkpad();
8943 if (ret) {
8944 thinkpad_acpi_module_exit();
8945 return ret;
8948 /* Driver initialization */
8950 thinkpad_acpi_init_banner();
8951 tpacpi_check_outdated_fw();
8953 TPACPI_ACPIHANDLE_INIT(ecrd);
8954 TPACPI_ACPIHANDLE_INIT(ecwr);
8956 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8957 if (!tpacpi_wq) {
8958 thinkpad_acpi_module_exit();
8959 return -ENOMEM;
8962 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8963 if (!proc_dir) {
8964 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
8965 thinkpad_acpi_module_exit();
8966 return -ENODEV;
8969 ret = platform_driver_register(&tpacpi_pdriver);
8970 if (ret) {
8971 pr_err("unable to register main platform driver\n");
8972 thinkpad_acpi_module_exit();
8973 return ret;
8975 tp_features.platform_drv_registered = 1;
8977 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8978 if (ret) {
8979 pr_err("unable to register hwmon platform driver\n");
8980 thinkpad_acpi_module_exit();
8981 return ret;
8983 tp_features.sensors_pdrv_registered = 1;
8985 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8986 if (!ret) {
8987 tp_features.platform_drv_attrs_registered = 1;
8988 ret = tpacpi_create_driver_attributes(
8989 &tpacpi_hwmon_pdriver.driver);
8991 if (ret) {
8992 pr_err("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 pr_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 pr_err("unable to register hwmon platform device\n");
9016 thinkpad_acpi_module_exit();
9017 return ret;
9019 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9020 &dev_attr_thinkpad_acpi_pdev_name);
9021 if (ret) {
9022 pr_err("unable to create sysfs hwmon device attributes\n");
9023 thinkpad_acpi_module_exit();
9024 return ret;
9026 tp_features.sensors_pdev_attrs_registered = 1;
9027 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9028 if (IS_ERR(tpacpi_hwmon)) {
9029 ret = PTR_ERR(tpacpi_hwmon);
9030 tpacpi_hwmon = NULL;
9031 pr_err("unable to register hwmon device\n");
9032 thinkpad_acpi_module_exit();
9033 return ret;
9035 mutex_init(&tpacpi_inputdev_send_mutex);
9036 tpacpi_inputdev = input_allocate_device();
9037 if (!tpacpi_inputdev) {
9038 pr_err("unable to allocate input device\n");
9039 thinkpad_acpi_module_exit();
9040 return -ENOMEM;
9041 } else {
9042 /* Prepare input device, but don't register */
9043 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9044 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9045 tpacpi_inputdev->id.bustype = BUS_HOST;
9046 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9047 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9048 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9049 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9052 /* Init subdriver dependencies */
9053 tpacpi_detect_brightness_capabilities();
9055 /* Init subdrivers */
9056 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9057 ret = ibm_init(&ibms_init[i]);
9058 if (ret >= 0 && *ibms_init[i].param)
9059 ret = ibms_init[i].data->write(ibms_init[i].param);
9060 if (ret < 0) {
9061 thinkpad_acpi_module_exit();
9062 return ret;
9066 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9068 ret = input_register_device(tpacpi_inputdev);
9069 if (ret < 0) {
9070 pr_err("unable to register input device\n");
9071 thinkpad_acpi_module_exit();
9072 return ret;
9073 } else {
9074 tp_features.input_device_registered = 1;
9077 return 0;
9080 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9083 * This will autoload the driver in almost every ThinkPad
9084 * in widespread use.
9086 * Only _VERY_ old models, like the 240, 240x and 570 lack
9087 * the HKEY event interface.
9089 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9092 * DMI matching for module autoloading
9094 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9095 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9097 * Only models listed in thinkwiki will be supported, so add yours
9098 * if it is not there yet.
9100 #define IBM_BIOS_MODULE_ALIAS(__type) \
9101 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9103 /* Ancient thinkpad BIOSes have to be identified by
9104 * BIOS type or model number, and there are far less
9105 * BIOS types than model numbers... */
9106 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9108 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9109 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9110 MODULE_DESCRIPTION(TPACPI_DESC);
9111 MODULE_VERSION(TPACPI_VERSION);
9112 MODULE_LICENSE("GPL");
9114 module_init(thinkpad_acpi_module_init);
9115 module_exit(thinkpad_acpi_module_exit);