thinkpad-acpi: document HKEY event 3006
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobda9c50f1df5dfdb22676a26d08cf7becdace62b5
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* ACPI HIDs */
125 #define TPACPI_ACPI_HKEY_HID "IBM0068"
127 /* Input IDs */
128 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
129 #define TPACPI_HKEY_INPUT_VERSION 0x4101
131 /* ACPI \WGSV commands */
132 enum {
133 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
134 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
135 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
136 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
139 /* TP_ACPI_WGSV_GET_STATE bits */
140 enum {
141 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
142 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
143 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
144 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
145 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
146 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
147 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
148 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
149 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
150 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
153 /* HKEY events */
154 enum tpacpi_hkey_event_t {
155 /* Hotkey-related */
156 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
157 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
158 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
159 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
160 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
161 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
163 /* Reasons for waking up from S3/S4 */
164 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
165 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
166 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
167 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
168 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
169 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
171 /* Auto-sleep after eject request */
172 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
173 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
175 /* Misc bay events */
176 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
183 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
184 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
185 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
187 /* Thermal events */
188 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
189 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
190 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
191 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
192 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
194 /* Misc */
195 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
198 /****************************************************************************
199 * Main driver
202 #define TPACPI_NAME "thinkpad"
203 #define TPACPI_DESC "ThinkPad ACPI Extras"
204 #define TPACPI_FILE TPACPI_NAME "_acpi"
205 #define TPACPI_URL "http://ibm-acpi.sf.net/"
206 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
208 #define TPACPI_PROC_DIR "ibm"
209 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
210 #define TPACPI_DRVR_NAME TPACPI_FILE
211 #define TPACPI_DRVR_SHORTNAME "tpacpi"
212 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
214 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
215 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
217 #define TPACPI_MAX_ACPI_ARGS 3
219 /* rfkill switches */
220 enum {
221 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
222 TPACPI_RFK_WWAN_SW_ID,
223 TPACPI_RFK_UWB_SW_ID,
226 /* printk headers */
227 #define TPACPI_LOG TPACPI_FILE ": "
228 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231 #define TPACPI_ERR KERN_ERR TPACPI_LOG
232 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
233 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234 #define TPACPI_INFO KERN_INFO TPACPI_LOG
235 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 struct ibm_struct *data;
299 static struct {
300 u32 bluetooth:1;
301 u32 hotkey:1;
302 u32 hotkey_mask:1;
303 u32 hotkey_wlsw:1;
304 u32 hotkey_tablet:1;
305 u32 light:1;
306 u32 light_status:1;
307 u32 bright_16levels:1;
308 u32 bright_acpimode:1;
309 u32 wan:1;
310 u32 uwb:1;
311 u32 fan_ctrl_status_undef:1;
312 u32 second_fan:1;
313 u32 beep_needs_two_args:1;
314 u32 mixer_no_level_control:1;
315 u32 input_device_registered:1;
316 u32 platform_drv_registered:1;
317 u32 platform_drv_attrs_registered:1;
318 u32 sensors_pdrv_registered:1;
319 u32 sensors_pdrv_attrs_registered:1;
320 u32 sensors_pdev_attrs_registered:1;
321 u32 hotkey_poll_active:1;
322 } tp_features;
324 static struct {
325 u16 hotkey_mask_ff:1;
326 u16 volume_ctrl_forbidden:1;
327 } tp_warned;
329 struct thinkpad_id_data {
330 unsigned int vendor; /* ThinkPad vendor:
331 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
333 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
334 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
336 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
337 u16 ec_model;
338 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
339 u16 ec_release;
341 char *model_str; /* ThinkPad T43 */
342 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
344 static struct thinkpad_id_data thinkpad_id;
346 static enum {
347 TPACPI_LIFE_INIT = 0,
348 TPACPI_LIFE_RUNNING,
349 TPACPI_LIFE_EXITING,
350 } tpacpi_lifecycle;
352 static int experimental;
353 static u32 dbg_level;
355 static struct workqueue_struct *tpacpi_wq;
357 enum led_status_t {
358 TPACPI_LED_OFF = 0,
359 TPACPI_LED_ON,
360 TPACPI_LED_BLINK,
363 /* Special LED class that can defer work */
364 struct tpacpi_led_classdev {
365 struct led_classdev led_classdev;
366 struct work_struct work;
367 enum led_status_t new_state;
368 unsigned int led;
371 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
372 static int dbg_wlswemul;
373 static int tpacpi_wlsw_emulstate;
374 static int dbg_bluetoothemul;
375 static int tpacpi_bluetooth_emulstate;
376 static int dbg_wwanemul;
377 static int tpacpi_wwan_emulstate;
378 static int dbg_uwbemul;
379 static int tpacpi_uwb_emulstate;
380 #endif
383 /*************************************************************************
384 * Debugging helpers
387 #define dbg_printk(a_dbg_level, format, arg...) \
388 do { if (dbg_level & (a_dbg_level)) \
389 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
390 } while (0)
392 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
393 #define vdbg_printk dbg_printk
394 static const char *str_supported(int is_supported);
395 #else
396 #define vdbg_printk(a_dbg_level, format, arg...) \
397 do { } while (0)
398 #endif
400 static void tpacpi_log_usertask(const char * const what)
402 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
403 what, task_tgid_vnr(current));
406 #define tpacpi_disclose_usertask(what, format, arg...) \
407 do { \
408 if (unlikely( \
409 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
410 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
411 printk(TPACPI_DEBUG "%s: PID %d: " format, \
412 what, task_tgid_vnr(current), ## arg); \
414 } while (0)
417 * Quirk handling helpers
419 * ThinkPad IDs and versions seen in the field so far
420 * are two-characters from the set [0-9A-Z], i.e. base 36.
422 * We use values well outside that range as specials.
425 #define TPACPI_MATCH_ANY 0xffffU
426 #define TPACPI_MATCH_UNKNOWN 0U
428 /* TPID('1', 'Y') == 0x5931 */
429 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
431 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
432 { .vendor = PCI_VENDOR_ID_IBM, \
433 .bios = TPID(__id1, __id2), \
434 .ec = TPACPI_MATCH_ANY, \
435 .quirks = (__quirk) }
437 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_LENOVO, \
439 .bios = TPID(__id1, __id2), \
440 .ec = TPACPI_MATCH_ANY, \
441 .quirks = (__quirk) }
443 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
444 { .vendor = PCI_VENDOR_ID_LENOVO, \
445 .bios = TPACPI_MATCH_ANY, \
446 .ec = TPID(__id1, __id2), \
447 .quirks = (__quirk) }
449 struct tpacpi_quirk {
450 unsigned int vendor;
451 u16 bios;
452 u16 ec;
453 unsigned long quirks;
457 * tpacpi_check_quirks() - search BIOS/EC version on a list
458 * @qlist: array of &struct tpacpi_quirk
459 * @qlist_size: number of elements in @qlist
461 * Iterates over a quirks list until one is found that matches the
462 * ThinkPad's vendor, BIOS and EC model.
464 * Returns 0 if nothing matches, otherwise returns the quirks field of
465 * the matching &struct tpacpi_quirk entry.
467 * The match criteria is: vendor, ec and bios much match.
469 static unsigned long __init tpacpi_check_quirks(
470 const struct tpacpi_quirk *qlist,
471 unsigned int qlist_size)
473 while (qlist_size) {
474 if ((qlist->vendor == thinkpad_id.vendor ||
475 qlist->vendor == TPACPI_MATCH_ANY) &&
476 (qlist->bios == thinkpad_id.bios_model ||
477 qlist->bios == TPACPI_MATCH_ANY) &&
478 (qlist->ec == thinkpad_id.ec_model ||
479 qlist->ec == TPACPI_MATCH_ANY))
480 return qlist->quirks;
482 qlist_size--;
483 qlist++;
485 return 0;
489 /****************************************************************************
490 ****************************************************************************
492 * ACPI Helpers and device model
494 ****************************************************************************
495 ****************************************************************************/
497 /*************************************************************************
498 * ACPI basic handles
501 static acpi_handle root_handle;
503 #define TPACPI_HANDLE(object, parent, paths...) \
504 static acpi_handle object##_handle; \
505 static acpi_handle *object##_parent = &parent##_handle; \
506 static char *object##_path; \
507 static char *object##_paths[] = { paths }
509 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
510 "\\_SB.PCI.ISA.EC", /* 570 */
511 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
512 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
513 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
514 "\\_SB.PCI0.ICH3.EC0", /* R31 */
515 "\\_SB.PCI0.LPC.EC", /* all others */
518 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
519 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
521 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
522 /* T4x, X31, X40 */
523 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
524 "\\CMS", /* R40, R40e */
525 ); /* all others */
527 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
528 "^HKEY", /* R30, R31 */
529 "HKEY", /* all others */
530 ); /* 570 */
532 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
533 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
534 "\\_SB.PCI0.VID0", /* 770e */
535 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
536 "\\_SB.PCI0.AGP.VID", /* all others */
537 ); /* R30, R31 */
540 /*************************************************************************
541 * ACPI helpers
544 static int acpi_evalf(acpi_handle handle,
545 void *res, char *method, char *fmt, ...)
547 char *fmt0 = fmt;
548 struct acpi_object_list params;
549 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
550 struct acpi_buffer result, *resultp;
551 union acpi_object out_obj;
552 acpi_status status;
553 va_list ap;
554 char res_type;
555 int success;
556 int quiet;
558 if (!*fmt) {
559 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
560 return 0;
563 if (*fmt == 'q') {
564 quiet = 1;
565 fmt++;
566 } else
567 quiet = 0;
569 res_type = *(fmt++);
571 params.count = 0;
572 params.pointer = &in_objs[0];
574 va_start(ap, fmt);
575 while (*fmt) {
576 char c = *(fmt++);
577 switch (c) {
578 case 'd': /* int */
579 in_objs[params.count].integer.value = va_arg(ap, int);
580 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
581 break;
582 /* add more types as needed */
583 default:
584 printk(TPACPI_ERR "acpi_evalf() called "
585 "with invalid format character '%c'\n", c);
586 return 0;
589 va_end(ap);
591 if (res_type != 'v') {
592 result.length = sizeof(out_obj);
593 result.pointer = &out_obj;
594 resultp = &result;
595 } else
596 resultp = NULL;
598 status = acpi_evaluate_object(handle, method, &params, resultp);
600 switch (res_type) {
601 case 'd': /* int */
602 if (res)
603 *(int *)res = out_obj.integer.value;
604 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
605 break;
606 case 'v': /* void */
607 success = status == AE_OK;
608 break;
609 /* add more types as needed */
610 default:
611 printk(TPACPI_ERR "acpi_evalf() called "
612 "with invalid format character '%c'\n", res_type);
613 return 0;
616 if (!success && !quiet)
617 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
618 method, fmt0, status);
620 return success;
623 static int acpi_ec_read(int i, u8 *p)
625 int v;
627 if (ecrd_handle) {
628 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
629 return 0;
630 *p = v;
631 } else {
632 if (ec_read(i, p) < 0)
633 return 0;
636 return 1;
639 static int acpi_ec_write(int i, u8 v)
641 if (ecwr_handle) {
642 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
643 return 0;
644 } else {
645 if (ec_write(i, v) < 0)
646 return 0;
649 return 1;
652 static int issue_thinkpad_cmos_command(int cmos_cmd)
654 if (!cmos_handle)
655 return -ENXIO;
657 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
658 return -EIO;
660 return 0;
663 /*************************************************************************
664 * ACPI device model
667 #define TPACPI_ACPIHANDLE_INIT(object) \
668 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
669 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
671 static void drv_acpi_handle_init(char *name,
672 acpi_handle *handle, acpi_handle parent,
673 char **paths, int num_paths, char **path)
675 int i;
676 acpi_status status;
678 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
679 name);
681 for (i = 0; i < num_paths; i++) {
682 status = acpi_get_handle(parent, paths[i], handle);
683 if (ACPI_SUCCESS(status)) {
684 *path = paths[i];
685 dbg_printk(TPACPI_DBG_INIT,
686 "Found ACPI handle %s for %s\n",
687 *path, name);
688 return;
692 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
693 name);
694 *handle = NULL;
697 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
699 struct ibm_struct *ibm = data;
701 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
702 return;
704 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
705 return;
707 ibm->acpi->notify(ibm, event);
710 static int __init setup_acpi_notify(struct ibm_struct *ibm)
712 acpi_status status;
713 int rc;
715 BUG_ON(!ibm->acpi);
717 if (!*ibm->acpi->handle)
718 return 0;
720 vdbg_printk(TPACPI_DBG_INIT,
721 "setting up ACPI notify for %s\n", ibm->name);
723 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
724 if (rc < 0) {
725 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
726 ibm->name, rc);
727 return -ENODEV;
730 ibm->acpi->device->driver_data = ibm;
731 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
732 TPACPI_ACPI_EVENT_PREFIX,
733 ibm->name);
735 status = acpi_install_notify_handler(*ibm->acpi->handle,
736 ibm->acpi->type, dispatch_acpi_notify, ibm);
737 if (ACPI_FAILURE(status)) {
738 if (status == AE_ALREADY_EXISTS) {
739 printk(TPACPI_NOTICE
740 "another device driver is already "
741 "handling %s events\n", ibm->name);
742 } else {
743 printk(TPACPI_ERR
744 "acpi_install_notify_handler(%s) failed: %d\n",
745 ibm->name, status);
747 return -ENODEV;
749 ibm->flags.acpi_notify_installed = 1;
750 return 0;
753 static int __init tpacpi_device_add(struct acpi_device *device)
755 return 0;
758 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
760 int rc;
762 dbg_printk(TPACPI_DBG_INIT,
763 "registering %s as an ACPI driver\n", ibm->name);
765 BUG_ON(!ibm->acpi);
767 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
768 if (!ibm->acpi->driver) {
769 printk(TPACPI_ERR
770 "failed to allocate memory for ibm->acpi->driver\n");
771 return -ENOMEM;
774 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
775 ibm->acpi->driver->ids = ibm->acpi->hid;
777 ibm->acpi->driver->ops.add = &tpacpi_device_add;
779 rc = acpi_bus_register_driver(ibm->acpi->driver);
780 if (rc < 0) {
781 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
782 ibm->name, rc);
783 kfree(ibm->acpi->driver);
784 ibm->acpi->driver = NULL;
785 } else if (!rc)
786 ibm->flags.acpi_driver_registered = 1;
788 return rc;
792 /****************************************************************************
793 ****************************************************************************
795 * Procfs Helpers
797 ****************************************************************************
798 ****************************************************************************/
800 static int dispatch_proc_show(struct seq_file *m, void *v)
802 struct ibm_struct *ibm = m->private;
804 if (!ibm || !ibm->read)
805 return -EINVAL;
806 return ibm->read(m);
809 static int dispatch_proc_open(struct inode *inode, struct file *file)
811 return single_open(file, dispatch_proc_show, PDE(inode)->data);
814 static ssize_t dispatch_proc_write(struct file *file,
815 const char __user *userbuf,
816 size_t count, loff_t *pos)
818 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
819 char *kernbuf;
820 int ret;
822 if (!ibm || !ibm->write)
823 return -EINVAL;
824 if (count > PAGE_SIZE - 2)
825 return -EINVAL;
827 kernbuf = kmalloc(count + 2, GFP_KERNEL);
828 if (!kernbuf)
829 return -ENOMEM;
831 if (copy_from_user(kernbuf, userbuf, count)) {
832 kfree(kernbuf);
833 return -EFAULT;
836 kernbuf[count] = 0;
837 strcat(kernbuf, ",");
838 ret = ibm->write(kernbuf);
839 if (ret == 0)
840 ret = count;
842 kfree(kernbuf);
844 return ret;
847 static const struct file_operations dispatch_proc_fops = {
848 .owner = THIS_MODULE,
849 .open = dispatch_proc_open,
850 .read = seq_read,
851 .llseek = seq_lseek,
852 .release = single_release,
853 .write = dispatch_proc_write,
856 static char *next_cmd(char **cmds)
858 char *start = *cmds;
859 char *end;
861 while ((end = strchr(start, ',')) && end == start)
862 start = end + 1;
864 if (!end)
865 return NULL;
867 *end = 0;
868 *cmds = end + 1;
869 return start;
873 /****************************************************************************
874 ****************************************************************************
876 * Device model: input, hwmon and platform
878 ****************************************************************************
879 ****************************************************************************/
881 static struct platform_device *tpacpi_pdev;
882 static struct platform_device *tpacpi_sensors_pdev;
883 static struct device *tpacpi_hwmon;
884 static struct input_dev *tpacpi_inputdev;
885 static struct mutex tpacpi_inputdev_send_mutex;
886 static LIST_HEAD(tpacpi_all_drivers);
888 static int tpacpi_suspend_handler(struct platform_device *pdev,
889 pm_message_t state)
891 struct ibm_struct *ibm, *itmp;
893 list_for_each_entry_safe(ibm, itmp,
894 &tpacpi_all_drivers,
895 all_drivers) {
896 if (ibm->suspend)
897 (ibm->suspend)(state);
900 return 0;
903 static int tpacpi_resume_handler(struct platform_device *pdev)
905 struct ibm_struct *ibm, *itmp;
907 list_for_each_entry_safe(ibm, itmp,
908 &tpacpi_all_drivers,
909 all_drivers) {
910 if (ibm->resume)
911 (ibm->resume)();
914 return 0;
917 static void tpacpi_shutdown_handler(struct platform_device *pdev)
919 struct ibm_struct *ibm, *itmp;
921 list_for_each_entry_safe(ibm, itmp,
922 &tpacpi_all_drivers,
923 all_drivers) {
924 if (ibm->shutdown)
925 (ibm->shutdown)();
929 static struct platform_driver tpacpi_pdriver = {
930 .driver = {
931 .name = TPACPI_DRVR_NAME,
932 .owner = THIS_MODULE,
934 .suspend = tpacpi_suspend_handler,
935 .resume = tpacpi_resume_handler,
936 .shutdown = tpacpi_shutdown_handler,
939 static struct platform_driver tpacpi_hwmon_pdriver = {
940 .driver = {
941 .name = TPACPI_HWMON_DRVR_NAME,
942 .owner = THIS_MODULE,
946 /*************************************************************************
947 * sysfs support helpers
950 struct attribute_set {
951 unsigned int members, max_members;
952 struct attribute_group group;
955 struct attribute_set_obj {
956 struct attribute_set s;
957 struct attribute *a;
958 } __attribute__((packed));
960 static struct attribute_set *create_attr_set(unsigned int max_members,
961 const char *name)
963 struct attribute_set_obj *sobj;
965 if (max_members == 0)
966 return NULL;
968 /* Allocates space for implicit NULL at the end too */
969 sobj = kzalloc(sizeof(struct attribute_set_obj) +
970 max_members * sizeof(struct attribute *),
971 GFP_KERNEL);
972 if (!sobj)
973 return NULL;
974 sobj->s.max_members = max_members;
975 sobj->s.group.attrs = &sobj->a;
976 sobj->s.group.name = name;
978 return &sobj->s;
981 #define destroy_attr_set(_set) \
982 kfree(_set);
984 /* not multi-threaded safe, use it in a single thread per set */
985 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
987 if (!s || !attr)
988 return -EINVAL;
990 if (s->members >= s->max_members)
991 return -ENOMEM;
993 s->group.attrs[s->members] = attr;
994 s->members++;
996 return 0;
999 static int add_many_to_attr_set(struct attribute_set *s,
1000 struct attribute **attr,
1001 unsigned int count)
1003 int i, res;
1005 for (i = 0; i < count; i++) {
1006 res = add_to_attr_set(s, attr[i]);
1007 if (res)
1008 return res;
1011 return 0;
1014 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1016 sysfs_remove_group(kobj, &s->group);
1017 destroy_attr_set(s);
1020 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1021 sysfs_create_group(_kobj, &_attr_set->group)
1023 static int parse_strtoul(const char *buf,
1024 unsigned long max, unsigned long *value)
1026 char *endp;
1028 while (*buf && isspace(*buf))
1029 buf++;
1030 *value = simple_strtoul(buf, &endp, 0);
1031 while (*endp && isspace(*endp))
1032 endp++;
1033 if (*endp || *value > max)
1034 return -EINVAL;
1036 return 0;
1039 static void tpacpi_disable_brightness_delay(void)
1041 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1042 printk(TPACPI_NOTICE
1043 "ACPI backlight control delay disabled\n");
1046 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1048 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1049 union acpi_object *obj;
1050 int rc;
1052 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1053 obj = (union acpi_object *)buffer.pointer;
1054 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1055 printk(TPACPI_ERR "Unknown _BCL data, "
1056 "please report this to %s\n", TPACPI_MAIL);
1057 rc = 0;
1058 } else {
1059 rc = obj->package.count;
1061 } else {
1062 return 0;
1065 kfree(buffer.pointer);
1066 return rc;
1069 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1070 u32 lvl, void *context, void **rv)
1072 char name[ACPI_PATH_SEGMENT_LENGTH];
1073 struct acpi_buffer buffer = { sizeof(name), &name };
1075 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1076 !strncmp("_BCL", name, sizeof(name) - 1)) {
1077 BUG_ON(!rv || !*rv);
1078 **(int **)rv = tpacpi_query_bcl_levels(handle);
1079 return AE_CTRL_TERMINATE;
1080 } else {
1081 return AE_OK;
1086 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1088 static int __init tpacpi_check_std_acpi_brightness_support(void)
1090 int status;
1091 int bcl_levels = 0;
1092 void *bcl_ptr = &bcl_levels;
1094 if (!vid_handle) {
1095 TPACPI_ACPIHANDLE_INIT(vid);
1097 if (!vid_handle)
1098 return 0;
1101 * Search for a _BCL method, and execute it. This is safe on all
1102 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1103 * BIOS in ACPI backlight control mode. We do NOT have to care
1104 * about calling the _BCL method in an enabled video device, any
1105 * will do for our purposes.
1108 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1109 tpacpi_acpi_walk_find_bcl, NULL,
1110 &bcl_ptr);
1112 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1113 tp_features.bright_acpimode = 1;
1114 return (bcl_levels - 2);
1117 return 0;
1120 static int __init tpacpi_new_rfkill(const unsigned int id,
1121 struct rfkill **rfk,
1122 const enum rfkill_type rfktype,
1123 const char *name,
1124 const bool set_default,
1125 int (*toggle_radio)(void *, enum rfkill_state),
1126 int (*get_state)(void *, enum rfkill_state *))
1128 int res;
1129 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1131 res = get_state(NULL, &initial_state);
1132 if (res < 0) {
1133 printk(TPACPI_ERR
1134 "failed to read initial state for %s, error %d; "
1135 "will turn radio off\n", name, res);
1136 } else if (set_default) {
1137 /* try to set the initial state as the default for the rfkill
1138 * type, since we ask the firmware to preserve it across S5 in
1139 * NVRAM */
1140 if (rfkill_set_default(rfktype,
1141 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1142 RFKILL_STATE_UNBLOCKED :
1143 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1144 vdbg_printk(TPACPI_DBG_RFKILL,
1145 "Default state for %s cannot be changed\n",
1146 name);
1149 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1150 if (!*rfk) {
1151 printk(TPACPI_ERR
1152 "failed to allocate memory for rfkill class\n");
1153 return -ENOMEM;
1156 (*rfk)->name = name;
1157 (*rfk)->get_state = get_state;
1158 (*rfk)->toggle_radio = toggle_radio;
1159 (*rfk)->state = initial_state;
1161 res = rfkill_register(*rfk);
1162 if (res < 0) {
1163 printk(TPACPI_ERR
1164 "failed to register %s rfkill switch: %d\n",
1165 name, res);
1166 rfkill_free(*rfk);
1167 *rfk = NULL;
1168 return res;
1171 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1172 name, (initial_state == RFKILL_STATE_UNBLOCKED) ?
1173 "un" : (initial_state == RFKILL_STATE_SOFT_BLOCKED) ?
1174 "soft-" : "hard-");
1175 return 0;
1178 static void printk_deprecated_attribute(const char * const what,
1179 const char * const details)
1181 tpacpi_log_usertask("deprecated sysfs attribute");
1182 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1183 "will be removed. %s\n",
1184 what, details);
1187 static void printk_deprecated_rfkill_attribute(const char * const what)
1189 printk_deprecated_attribute(what,
1190 "Please switch to generic rfkill before year 2010");
1193 /*************************************************************************
1194 * thinkpad-acpi driver attributes
1197 /* interface_version --------------------------------------------------- */
1198 static ssize_t tpacpi_driver_interface_version_show(
1199 struct device_driver *drv,
1200 char *buf)
1202 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1205 static DRIVER_ATTR(interface_version, S_IRUGO,
1206 tpacpi_driver_interface_version_show, NULL);
1208 /* debug_level --------------------------------------------------------- */
1209 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1210 char *buf)
1212 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1215 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1216 const char *buf, size_t count)
1218 unsigned long t;
1220 if (parse_strtoul(buf, 0xffff, &t))
1221 return -EINVAL;
1223 dbg_level = t;
1225 return count;
1228 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1229 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1231 /* version ------------------------------------------------------------- */
1232 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1233 char *buf)
1235 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1236 TPACPI_DESC, TPACPI_VERSION);
1239 static DRIVER_ATTR(version, S_IRUGO,
1240 tpacpi_driver_version_show, NULL);
1242 /* --------------------------------------------------------------------- */
1244 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1246 static void tpacpi_send_radiosw_update(void);
1248 /* wlsw_emulstate ------------------------------------------------------ */
1249 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1250 char *buf)
1252 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1255 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1256 const char *buf, size_t count)
1258 unsigned long t;
1260 if (parse_strtoul(buf, 1, &t))
1261 return -EINVAL;
1263 if (tpacpi_wlsw_emulstate != t) {
1264 tpacpi_wlsw_emulstate = !!t;
1265 tpacpi_send_radiosw_update();
1266 } else
1267 tpacpi_wlsw_emulstate = !!t;
1269 return count;
1272 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1273 tpacpi_driver_wlsw_emulstate_show,
1274 tpacpi_driver_wlsw_emulstate_store);
1276 /* bluetooth_emulstate ------------------------------------------------- */
1277 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1278 struct device_driver *drv,
1279 char *buf)
1281 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1284 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1285 struct device_driver *drv,
1286 const char *buf, size_t count)
1288 unsigned long t;
1290 if (parse_strtoul(buf, 1, &t))
1291 return -EINVAL;
1293 tpacpi_bluetooth_emulstate = !!t;
1295 return count;
1298 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1299 tpacpi_driver_bluetooth_emulstate_show,
1300 tpacpi_driver_bluetooth_emulstate_store);
1302 /* wwan_emulstate ------------------------------------------------- */
1303 static ssize_t tpacpi_driver_wwan_emulstate_show(
1304 struct device_driver *drv,
1305 char *buf)
1307 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1310 static ssize_t tpacpi_driver_wwan_emulstate_store(
1311 struct device_driver *drv,
1312 const char *buf, size_t count)
1314 unsigned long t;
1316 if (parse_strtoul(buf, 1, &t))
1317 return -EINVAL;
1319 tpacpi_wwan_emulstate = !!t;
1321 return count;
1324 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1325 tpacpi_driver_wwan_emulstate_show,
1326 tpacpi_driver_wwan_emulstate_store);
1328 /* uwb_emulstate ------------------------------------------------- */
1329 static ssize_t tpacpi_driver_uwb_emulstate_show(
1330 struct device_driver *drv,
1331 char *buf)
1333 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1336 static ssize_t tpacpi_driver_uwb_emulstate_store(
1337 struct device_driver *drv,
1338 const char *buf, size_t count)
1340 unsigned long t;
1342 if (parse_strtoul(buf, 1, &t))
1343 return -EINVAL;
1345 tpacpi_uwb_emulstate = !!t;
1347 return count;
1350 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1351 tpacpi_driver_uwb_emulstate_show,
1352 tpacpi_driver_uwb_emulstate_store);
1353 #endif
1355 /* --------------------------------------------------------------------- */
1357 static struct driver_attribute *tpacpi_driver_attributes[] = {
1358 &driver_attr_debug_level, &driver_attr_version,
1359 &driver_attr_interface_version,
1362 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1364 int i, res;
1366 i = 0;
1367 res = 0;
1368 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1369 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1370 i++;
1373 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1374 if (!res && dbg_wlswemul)
1375 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1376 if (!res && dbg_bluetoothemul)
1377 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1378 if (!res && dbg_wwanemul)
1379 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1380 if (!res && dbg_uwbemul)
1381 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1382 #endif
1384 return res;
1387 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1389 int i;
1391 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1392 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1394 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1395 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1396 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1397 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1398 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1399 #endif
1402 /*************************************************************************
1403 * Firmware Data
1407 * Table of recommended minimum BIOS versions
1409 * Reasons for listing:
1410 * 1. Stable BIOS, listed because the unknown ammount of
1411 * bugs and bad ACPI behaviour on older versions
1413 * 2. BIOS or EC fw with known bugs that trigger on Linux
1415 * 3. BIOS with known reduced functionality in older versions
1417 * We recommend the latest BIOS and EC version.
1418 * We only support the latest BIOS and EC fw version as a rule.
1420 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1421 * Information from users in ThinkWiki
1423 * WARNING: we use this table also to detect that the machine is
1424 * a ThinkPad in some cases, so don't remove entries lightly.
1427 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1428 { .vendor = (__v), \
1429 .bios = TPID(__id1, __id2), \
1430 .ec = TPACPI_MATCH_ANY, \
1431 .quirks = TPACPI_MATCH_ANY << 16 \
1432 | (__bv1) << 8 | (__bv2) }
1434 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1435 __eid, __ev1, __ev2) \
1436 { .vendor = (__v), \
1437 .bios = TPID(__bid1, __bid2), \
1438 .ec = __eid, \
1439 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1440 | (__bv1) << 8 | (__bv2) }
1442 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1443 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1445 /* Outdated IBM BIOSes often lack the EC id string */
1446 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1447 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1448 __bv1, __bv2, TPID(__id1, __id2), \
1449 __ev1, __ev2), \
1450 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1451 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1452 __ev1, __ev2)
1454 /* Outdated IBM BIOSes often lack the EC id string */
1455 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1456 __eid1, __eid2, __ev1, __ev2) \
1457 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1458 __bv1, __bv2, TPID(__eid1, __eid2), \
1459 __ev1, __ev2), \
1460 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1461 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1462 __ev1, __ev2)
1464 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1465 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1467 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1468 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1469 __bv1, __bv2, TPID(__id1, __id2), \
1470 __ev1, __ev2)
1472 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1473 __eid1, __eid2, __ev1, __ev2) \
1474 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1475 __bv1, __bv2, TPID(__eid1, __eid2), \
1476 __ev1, __ev2)
1478 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1479 /* Numeric models ------------------ */
1480 /* FW MODEL BIOS VERS */
1481 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1482 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1483 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1484 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1485 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1486 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1487 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1488 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1489 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1491 /* A-series ------------------------- */
1492 /* FW MODEL BIOS VERS EC VERS */
1493 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1494 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1495 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1496 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1497 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1498 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1499 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1500 TPV_QI0('1', '3', '2', '0'), /* A22m */
1501 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1502 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1503 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1505 /* G-series ------------------------- */
1506 /* FW MODEL BIOS VERS */
1507 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1508 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1510 /* R-series, T-series --------------- */
1511 /* FW MODEL BIOS VERS EC VERS */
1512 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1513 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1514 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1515 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1516 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1517 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1518 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1519 T40/p, T41/p, T42/p (1) */
1520 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1521 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1522 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1523 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1525 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1526 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1527 TPV_QI0('1', '6', '3', '2'), /* T22 */
1528 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1529 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1530 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1532 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1533 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1534 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1536 /* BIOS FW BIOS VERS EC FW EC VERS */
1537 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1538 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1540 /* X-series ------------------------- */
1541 /* FW MODEL BIOS VERS EC VERS */
1542 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1543 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1544 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1545 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1546 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1547 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1548 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1550 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1551 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1553 /* (0) - older versions lack DMI EC fw string and functionality */
1554 /* (1) - older versions known to lack functionality */
1557 #undef TPV_QL1
1558 #undef TPV_QL0
1559 #undef TPV_QI2
1560 #undef TPV_QI1
1561 #undef TPV_QI0
1562 #undef TPV_Q_X
1563 #undef TPV_Q
1565 static void __init tpacpi_check_outdated_fw(void)
1567 unsigned long fwvers;
1568 u16 ec_version, bios_version;
1570 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1571 ARRAY_SIZE(tpacpi_bios_version_qtable));
1573 if (!fwvers)
1574 return;
1576 bios_version = fwvers & 0xffffU;
1577 ec_version = (fwvers >> 16) & 0xffffU;
1579 /* note that unknown versions are set to 0x0000 and we use that */
1580 if ((bios_version > thinkpad_id.bios_release) ||
1581 (ec_version > thinkpad_id.ec_release &&
1582 ec_version != TPACPI_MATCH_ANY)) {
1584 * The changelogs would let us track down the exact
1585 * reason, but it is just too much of a pain to track
1586 * it. We only list BIOSes that are either really
1587 * broken, or really stable to begin with, so it is
1588 * best if the user upgrades the firmware anyway.
1590 printk(TPACPI_WARN
1591 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1592 printk(TPACPI_WARN
1593 "WARNING: This firmware may be missing critical bug "
1594 "fixes and/or important features\n");
1598 static bool __init tpacpi_is_fw_known(void)
1600 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1601 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1604 /****************************************************************************
1605 ****************************************************************************
1607 * Subdrivers
1609 ****************************************************************************
1610 ****************************************************************************/
1612 /*************************************************************************
1613 * thinkpad-acpi init subdriver
1616 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1618 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1619 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1621 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1622 (thinkpad_id.bios_version_str) ?
1623 thinkpad_id.bios_version_str : "unknown",
1624 (thinkpad_id.ec_version_str) ?
1625 thinkpad_id.ec_version_str : "unknown");
1627 if (thinkpad_id.vendor && thinkpad_id.model_str)
1628 printk(TPACPI_INFO "%s %s, model %s\n",
1629 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1630 "IBM" : ((thinkpad_id.vendor ==
1631 PCI_VENDOR_ID_LENOVO) ?
1632 "Lenovo" : "Unknown vendor"),
1633 thinkpad_id.model_str,
1634 (thinkpad_id.nummodel_str) ?
1635 thinkpad_id.nummodel_str : "unknown");
1637 tpacpi_check_outdated_fw();
1638 return 0;
1641 static int thinkpad_acpi_driver_read(struct seq_file *m)
1643 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1644 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1645 return 0;
1648 static struct ibm_struct thinkpad_acpi_driver_data = {
1649 .name = "driver",
1650 .read = thinkpad_acpi_driver_read,
1653 /*************************************************************************
1654 * Hotkey subdriver
1658 * ThinkPad firmware event model
1660 * The ThinkPad firmware has two main event interfaces: normal ACPI
1661 * notifications (which follow the ACPI standard), and a private event
1662 * interface.
1664 * The private event interface also issues events for the hotkeys. As
1665 * the driver gained features, the event handling code ended up being
1666 * built around the hotkey subdriver. This will need to be refactored
1667 * to a more formal event API eventually.
1669 * Some "hotkeys" are actually supposed to be used as event reports,
1670 * such as "brightness has changed", "volume has changed", depending on
1671 * the ThinkPad model and how the firmware is operating.
1673 * Unlike other classes, hotkey-class events have mask/unmask control on
1674 * non-ancient firmware. However, how it behaves changes a lot with the
1675 * firmware model and version.
1678 enum { /* hot key scan codes (derived from ACPI DSDT) */
1679 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1680 TP_ACPI_HOTKEYSCAN_FNF2,
1681 TP_ACPI_HOTKEYSCAN_FNF3,
1682 TP_ACPI_HOTKEYSCAN_FNF4,
1683 TP_ACPI_HOTKEYSCAN_FNF5,
1684 TP_ACPI_HOTKEYSCAN_FNF6,
1685 TP_ACPI_HOTKEYSCAN_FNF7,
1686 TP_ACPI_HOTKEYSCAN_FNF8,
1687 TP_ACPI_HOTKEYSCAN_FNF9,
1688 TP_ACPI_HOTKEYSCAN_FNF10,
1689 TP_ACPI_HOTKEYSCAN_FNF11,
1690 TP_ACPI_HOTKEYSCAN_FNF12,
1691 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1692 TP_ACPI_HOTKEYSCAN_FNINSERT,
1693 TP_ACPI_HOTKEYSCAN_FNDELETE,
1694 TP_ACPI_HOTKEYSCAN_FNHOME,
1695 TP_ACPI_HOTKEYSCAN_FNEND,
1696 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1697 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1698 TP_ACPI_HOTKEYSCAN_FNSPACE,
1699 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1700 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1701 TP_ACPI_HOTKEYSCAN_MUTE,
1702 TP_ACPI_HOTKEYSCAN_THINKPAD,
1705 enum { /* Keys/events available through NVRAM polling */
1706 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1707 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1710 enum { /* Positions of some of the keys in hotkey masks */
1711 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1712 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1713 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1714 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1715 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1716 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1717 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1718 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1719 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1720 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1721 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1724 enum { /* NVRAM to ACPI HKEY group map */
1725 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1726 TP_ACPI_HKEY_ZOOM_MASK |
1727 TP_ACPI_HKEY_DISPSWTCH_MASK |
1728 TP_ACPI_HKEY_HIBERNATE_MASK,
1729 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1730 TP_ACPI_HKEY_BRGHTDWN_MASK,
1731 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1732 TP_ACPI_HKEY_VOLDWN_MASK |
1733 TP_ACPI_HKEY_MUTE_MASK,
1736 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1737 struct tp_nvram_state {
1738 u16 thinkpad_toggle:1;
1739 u16 zoom_toggle:1;
1740 u16 display_toggle:1;
1741 u16 thinklight_toggle:1;
1742 u16 hibernate_toggle:1;
1743 u16 displayexp_toggle:1;
1744 u16 display_state:1;
1745 u16 brightness_toggle:1;
1746 u16 volume_toggle:1;
1747 u16 mute:1;
1749 u8 brightness_level;
1750 u8 volume_level;
1753 /* kthread for the hotkey poller */
1754 static struct task_struct *tpacpi_hotkey_task;
1756 /* Acquired while the poller kthread is running, use to sync start/stop */
1757 static struct mutex hotkey_thread_mutex;
1760 * Acquire mutex to write poller control variables as an
1761 * atomic block.
1763 * Increment hotkey_config_change when changing them if you
1764 * want the kthread to forget old state.
1766 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1768 static struct mutex hotkey_thread_data_mutex;
1769 static unsigned int hotkey_config_change;
1772 * hotkey poller control variables
1774 * Must be atomic or readers will also need to acquire mutex
1776 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1777 * should be used only when the changes need to be taken as
1778 * a block, OR when one needs to force the kthread to forget
1779 * old state.
1781 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1782 static unsigned int hotkey_poll_freq = 10; /* Hz */
1784 #define HOTKEY_CONFIG_CRITICAL_START \
1785 do { \
1786 mutex_lock(&hotkey_thread_data_mutex); \
1787 hotkey_config_change++; \
1788 } while (0);
1789 #define HOTKEY_CONFIG_CRITICAL_END \
1790 mutex_unlock(&hotkey_thread_data_mutex);
1792 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1794 #define hotkey_source_mask 0U
1795 #define HOTKEY_CONFIG_CRITICAL_START
1796 #define HOTKEY_CONFIG_CRITICAL_END
1798 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1800 static struct mutex hotkey_mutex;
1802 static enum { /* Reasons for waking up */
1803 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1804 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1805 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1806 } hotkey_wakeup_reason;
1808 static int hotkey_autosleep_ack;
1810 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1811 static u32 hotkey_all_mask; /* all events supported in fw */
1812 static u32 hotkey_reserved_mask; /* events better left disabled */
1813 static u32 hotkey_driver_mask; /* events needed by the driver */
1814 static u32 hotkey_user_mask; /* events visible to userspace */
1815 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1817 static unsigned int hotkey_report_mode;
1819 static u16 *hotkey_keycode_map;
1821 static struct attribute_set *hotkey_dev_attributes;
1823 static void tpacpi_driver_event(const unsigned int hkey_event);
1824 static void hotkey_driver_event(const unsigned int scancode);
1826 /* HKEY.MHKG() return bits */
1827 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1829 static int hotkey_get_wlsw(int *status)
1831 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1832 if (dbg_wlswemul) {
1833 *status = !!tpacpi_wlsw_emulstate;
1834 return 0;
1836 #endif
1837 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1838 return -EIO;
1839 return 0;
1842 static int hotkey_get_tablet_mode(int *status)
1844 int s;
1846 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1847 return -EIO;
1849 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1850 return 0;
1854 * Reads current event mask from firmware, and updates
1855 * hotkey_acpi_mask accordingly. Also resets any bits
1856 * from hotkey_user_mask that are unavailable to be
1857 * delivered (shadow requirement of the userspace ABI).
1859 * Call with hotkey_mutex held
1861 static int hotkey_mask_get(void)
1863 if (tp_features.hotkey_mask) {
1864 u32 m = 0;
1866 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1867 return -EIO;
1869 hotkey_acpi_mask = m;
1870 } else {
1871 /* no mask support doesn't mean no event support... */
1872 hotkey_acpi_mask = hotkey_all_mask;
1875 /* sync userspace-visible mask */
1876 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1878 return 0;
1881 void static hotkey_mask_warn_incomplete_mask(void)
1883 /* log only what the user can fix... */
1884 const u32 wantedmask = hotkey_driver_mask &
1885 ~(hotkey_acpi_mask | hotkey_source_mask) &
1886 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1888 if (wantedmask)
1889 printk(TPACPI_NOTICE
1890 "required events 0x%08x not enabled!\n",
1891 wantedmask);
1895 * Set the firmware mask when supported
1897 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1899 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1901 * Call with hotkey_mutex held
1903 static int hotkey_mask_set(u32 mask)
1905 int i;
1906 int rc = 0;
1908 const u32 fwmask = mask & ~hotkey_source_mask;
1910 if (tp_features.hotkey_mask) {
1911 for (i = 0; i < 32; i++) {
1912 if (!acpi_evalf(hkey_handle,
1913 NULL, "MHKM", "vdd", i + 1,
1914 !!(mask & (1 << i)))) {
1915 rc = -EIO;
1916 break;
1922 * We *must* make an inconditional call to hotkey_mask_get to
1923 * refresh hotkey_acpi_mask and update hotkey_user_mask
1925 * Take the opportunity to also log when we cannot _enable_
1926 * a given event.
1928 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1929 printk(TPACPI_NOTICE
1930 "asked for hotkey mask 0x%08x, but "
1931 "firmware forced it to 0x%08x\n",
1932 fwmask, hotkey_acpi_mask);
1935 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1936 hotkey_mask_warn_incomplete_mask();
1938 return rc;
1942 * Sets hotkey_user_mask and tries to set the firmware mask
1944 * Call with hotkey_mutex held
1946 static int hotkey_user_mask_set(const u32 mask)
1948 int rc;
1950 /* Give people a chance to notice they are doing something that
1951 * is bound to go boom on their users sooner or later */
1952 if (!tp_warned.hotkey_mask_ff &&
1953 (mask == 0xffff || mask == 0xffffff ||
1954 mask == 0xffffffff)) {
1955 tp_warned.hotkey_mask_ff = 1;
1956 printk(TPACPI_NOTICE
1957 "setting the hotkey mask to 0x%08x is likely "
1958 "not the best way to go about it\n", mask);
1959 printk(TPACPI_NOTICE
1960 "please consider using the driver defaults, "
1961 "and refer to up-to-date thinkpad-acpi "
1962 "documentation\n");
1965 /* Try to enable what the user asked for, plus whatever we need.
1966 * this syncs everything but won't enable bits in hotkey_user_mask */
1967 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1969 /* Enable the available bits in hotkey_user_mask */
1970 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1972 return rc;
1976 * Sets the driver hotkey mask.
1978 * Can be called even if the hotkey subdriver is inactive
1980 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1982 int rc;
1984 /* Do the right thing if hotkey_init has not been called yet */
1985 if (!tp_features.hotkey) {
1986 hotkey_driver_mask = mask;
1987 return 0;
1990 mutex_lock(&hotkey_mutex);
1992 HOTKEY_CONFIG_CRITICAL_START
1993 hotkey_driver_mask = mask;
1994 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1995 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1996 #endif
1997 HOTKEY_CONFIG_CRITICAL_END
1999 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2000 ~hotkey_source_mask);
2001 mutex_unlock(&hotkey_mutex);
2003 return rc;
2006 static int hotkey_status_get(int *status)
2008 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2009 return -EIO;
2011 return 0;
2014 static int hotkey_status_set(bool enable)
2016 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2017 return -EIO;
2019 return 0;
2022 static void tpacpi_input_send_tabletsw(void)
2024 int state;
2026 if (tp_features.hotkey_tablet &&
2027 !hotkey_get_tablet_mode(&state)) {
2028 mutex_lock(&tpacpi_inputdev_send_mutex);
2030 input_report_switch(tpacpi_inputdev,
2031 SW_TABLET_MODE, !!state);
2032 input_sync(tpacpi_inputdev);
2034 mutex_unlock(&tpacpi_inputdev_send_mutex);
2038 /* Do NOT call without validating scancode first */
2039 static void tpacpi_input_send_key(const unsigned int scancode)
2041 const unsigned int keycode = hotkey_keycode_map[scancode];
2043 if (keycode != KEY_RESERVED) {
2044 mutex_lock(&tpacpi_inputdev_send_mutex);
2046 input_report_key(tpacpi_inputdev, keycode, 1);
2047 if (keycode == KEY_UNKNOWN)
2048 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2049 scancode);
2050 input_sync(tpacpi_inputdev);
2052 input_report_key(tpacpi_inputdev, keycode, 0);
2053 if (keycode == KEY_UNKNOWN)
2054 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2055 scancode);
2056 input_sync(tpacpi_inputdev);
2058 mutex_unlock(&tpacpi_inputdev_send_mutex);
2062 /* Do NOT call without validating scancode first */
2063 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2065 hotkey_driver_event(scancode);
2066 if (hotkey_user_mask & (1 << scancode))
2067 tpacpi_input_send_key(scancode);
2070 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2071 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2073 /* Do NOT call without validating scancode first */
2074 static void tpacpi_hotkey_send_key(unsigned int scancode)
2076 tpacpi_input_send_key_masked(scancode);
2077 if (hotkey_report_mode < 2) {
2078 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2079 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2083 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2085 u8 d;
2087 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2088 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2089 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2090 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2091 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2092 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2094 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2095 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2096 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2098 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2099 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2100 n->displayexp_toggle =
2101 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2103 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2104 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2105 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2106 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2107 n->brightness_toggle =
2108 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2110 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2111 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2112 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2113 >> TP_NVRAM_POS_LEVEL_VOLUME;
2114 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2115 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2119 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2120 struct tp_nvram_state *newn,
2121 const u32 event_mask)
2124 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2125 do { \
2126 if ((event_mask & (1 << __scancode)) && \
2127 oldn->__member != newn->__member) \
2128 tpacpi_hotkey_send_key(__scancode); \
2129 } while (0)
2131 #define TPACPI_MAY_SEND_KEY(__scancode) \
2132 do { \
2133 if (event_mask & (1 << __scancode)) \
2134 tpacpi_hotkey_send_key(__scancode); \
2135 } while (0)
2137 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2138 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2139 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2140 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2142 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2144 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2146 /* handle volume */
2147 if (oldn->volume_toggle != newn->volume_toggle) {
2148 if (oldn->mute != newn->mute) {
2149 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2151 if (oldn->volume_level > newn->volume_level) {
2152 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2153 } else if (oldn->volume_level < newn->volume_level) {
2154 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2155 } else if (oldn->mute == newn->mute) {
2156 /* repeated key presses that didn't change state */
2157 if (newn->mute) {
2158 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2159 } else if (newn->volume_level != 0) {
2160 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2161 } else {
2162 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2167 /* handle brightness */
2168 if (oldn->brightness_toggle != newn->brightness_toggle) {
2169 if (oldn->brightness_level < newn->brightness_level) {
2170 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2171 } else if (oldn->brightness_level > newn->brightness_level) {
2172 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2173 } else {
2174 /* repeated key presses that didn't change state */
2175 if (newn->brightness_level != 0) {
2176 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2177 } else {
2178 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2183 #undef TPACPI_COMPARE_KEY
2184 #undef TPACPI_MAY_SEND_KEY
2188 * Polling driver
2190 * We track all events in hotkey_source_mask all the time, since
2191 * most of them are edge-based. We only issue those requested by
2192 * hotkey_user_mask or hotkey_driver_mask, though.
2194 static int hotkey_kthread(void *data)
2196 struct tp_nvram_state s[2];
2197 u32 poll_mask, event_mask;
2198 unsigned int si, so;
2199 unsigned long t;
2200 unsigned int change_detector, must_reset;
2201 unsigned int poll_freq;
2203 mutex_lock(&hotkey_thread_mutex);
2205 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2206 goto exit;
2208 set_freezable();
2210 so = 0;
2211 si = 1;
2212 t = 0;
2214 /* Initial state for compares */
2215 mutex_lock(&hotkey_thread_data_mutex);
2216 change_detector = hotkey_config_change;
2217 poll_mask = hotkey_source_mask;
2218 event_mask = hotkey_source_mask &
2219 (hotkey_driver_mask | hotkey_user_mask);
2220 poll_freq = hotkey_poll_freq;
2221 mutex_unlock(&hotkey_thread_data_mutex);
2222 hotkey_read_nvram(&s[so], poll_mask);
2224 while (!kthread_should_stop()) {
2225 if (t == 0) {
2226 if (likely(poll_freq))
2227 t = 1000/poll_freq;
2228 else
2229 t = 100; /* should never happen... */
2231 t = msleep_interruptible(t);
2232 if (unlikely(kthread_should_stop()))
2233 break;
2234 must_reset = try_to_freeze();
2235 if (t > 0 && !must_reset)
2236 continue;
2238 mutex_lock(&hotkey_thread_data_mutex);
2239 if (must_reset || hotkey_config_change != change_detector) {
2240 /* forget old state on thaw or config change */
2241 si = so;
2242 t = 0;
2243 change_detector = hotkey_config_change;
2245 poll_mask = hotkey_source_mask;
2246 event_mask = hotkey_source_mask &
2247 (hotkey_driver_mask | hotkey_user_mask);
2248 poll_freq = hotkey_poll_freq;
2249 mutex_unlock(&hotkey_thread_data_mutex);
2251 if (likely(poll_mask)) {
2252 hotkey_read_nvram(&s[si], poll_mask);
2253 if (likely(si != so)) {
2254 hotkey_compare_and_issue_event(&s[so], &s[si],
2255 event_mask);
2259 so = si;
2260 si ^= 1;
2263 exit:
2264 mutex_unlock(&hotkey_thread_mutex);
2265 return 0;
2268 /* call with hotkey_mutex held */
2269 static void hotkey_poll_stop_sync(void)
2271 if (tpacpi_hotkey_task) {
2272 if (frozen(tpacpi_hotkey_task) ||
2273 freezing(tpacpi_hotkey_task))
2274 thaw_process(tpacpi_hotkey_task);
2276 kthread_stop(tpacpi_hotkey_task);
2277 tpacpi_hotkey_task = NULL;
2278 mutex_lock(&hotkey_thread_mutex);
2279 /* at this point, the thread did exit */
2280 mutex_unlock(&hotkey_thread_mutex);
2284 /* call with hotkey_mutex held */
2285 static void hotkey_poll_setup(bool may_warn)
2287 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2288 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2290 if (hotkey_poll_freq > 0 &&
2291 (poll_driver_mask ||
2292 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2293 if (!tpacpi_hotkey_task) {
2294 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2295 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2296 if (IS_ERR(tpacpi_hotkey_task)) {
2297 tpacpi_hotkey_task = NULL;
2298 printk(TPACPI_ERR
2299 "could not create kernel thread "
2300 "for hotkey polling\n");
2303 } else {
2304 hotkey_poll_stop_sync();
2305 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2306 hotkey_poll_freq == 0) {
2307 printk(TPACPI_NOTICE
2308 "hot keys 0x%08x and/or events 0x%08x "
2309 "require polling, which is currently "
2310 "disabled\n",
2311 poll_user_mask, poll_driver_mask);
2316 static void hotkey_poll_setup_safe(bool may_warn)
2318 mutex_lock(&hotkey_mutex);
2319 hotkey_poll_setup(may_warn);
2320 mutex_unlock(&hotkey_mutex);
2323 /* call with hotkey_mutex held */
2324 static void hotkey_poll_set_freq(unsigned int freq)
2326 if (!freq)
2327 hotkey_poll_stop_sync();
2329 hotkey_poll_freq = freq;
2332 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2334 static void hotkey_poll_setup_safe(bool __unused)
2338 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2340 static int hotkey_inputdev_open(struct input_dev *dev)
2342 switch (tpacpi_lifecycle) {
2343 case TPACPI_LIFE_INIT:
2345 * hotkey_init will call hotkey_poll_setup_safe
2346 * at the appropriate moment
2348 return 0;
2349 case TPACPI_LIFE_EXITING:
2350 return -EBUSY;
2351 case TPACPI_LIFE_RUNNING:
2352 hotkey_poll_setup_safe(false);
2353 return 0;
2356 /* Should only happen if tpacpi_lifecycle is corrupt */
2357 BUG();
2358 return -EBUSY;
2361 static void hotkey_inputdev_close(struct input_dev *dev)
2363 /* disable hotkey polling when possible */
2364 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2365 !(hotkey_source_mask & hotkey_driver_mask))
2366 hotkey_poll_setup_safe(false);
2369 /* sysfs hotkey enable ------------------------------------------------- */
2370 static ssize_t hotkey_enable_show(struct device *dev,
2371 struct device_attribute *attr,
2372 char *buf)
2374 int res, status;
2376 printk_deprecated_attribute("hotkey_enable",
2377 "Hotkey reporting is always enabled");
2379 res = hotkey_status_get(&status);
2380 if (res)
2381 return res;
2383 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2386 static ssize_t hotkey_enable_store(struct device *dev,
2387 struct device_attribute *attr,
2388 const char *buf, size_t count)
2390 unsigned long t;
2392 printk_deprecated_attribute("hotkey_enable",
2393 "Hotkeys can be disabled through hotkey_mask");
2395 if (parse_strtoul(buf, 1, &t))
2396 return -EINVAL;
2398 if (t == 0)
2399 return -EPERM;
2401 return count;
2404 static struct device_attribute dev_attr_hotkey_enable =
2405 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2406 hotkey_enable_show, hotkey_enable_store);
2408 /* sysfs hotkey mask --------------------------------------------------- */
2409 static ssize_t hotkey_mask_show(struct device *dev,
2410 struct device_attribute *attr,
2411 char *buf)
2413 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2416 static ssize_t hotkey_mask_store(struct device *dev,
2417 struct device_attribute *attr,
2418 const char *buf, size_t count)
2420 unsigned long t;
2421 int res;
2423 if (parse_strtoul(buf, 0xffffffffUL, &t))
2424 return -EINVAL;
2426 if (mutex_lock_killable(&hotkey_mutex))
2427 return -ERESTARTSYS;
2429 res = hotkey_user_mask_set(t);
2431 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2432 hotkey_poll_setup(true);
2433 #endif
2435 mutex_unlock(&hotkey_mutex);
2437 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2439 return (res) ? res : count;
2442 static struct device_attribute dev_attr_hotkey_mask =
2443 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2444 hotkey_mask_show, hotkey_mask_store);
2446 /* sysfs hotkey bios_enabled ------------------------------------------- */
2447 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2448 struct device_attribute *attr,
2449 char *buf)
2451 return sprintf(buf, "0\n");
2454 static struct device_attribute dev_attr_hotkey_bios_enabled =
2455 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2457 /* sysfs hotkey bios_mask ---------------------------------------------- */
2458 static ssize_t hotkey_bios_mask_show(struct device *dev,
2459 struct device_attribute *attr,
2460 char *buf)
2462 printk_deprecated_attribute("hotkey_bios_mask",
2463 "This attribute is useless.");
2464 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2467 static struct device_attribute dev_attr_hotkey_bios_mask =
2468 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2470 /* sysfs hotkey all_mask ----------------------------------------------- */
2471 static ssize_t hotkey_all_mask_show(struct device *dev,
2472 struct device_attribute *attr,
2473 char *buf)
2475 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2476 hotkey_all_mask | hotkey_source_mask);
2479 static struct device_attribute dev_attr_hotkey_all_mask =
2480 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2482 /* sysfs hotkey recommended_mask --------------------------------------- */
2483 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2484 struct device_attribute *attr,
2485 char *buf)
2487 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2488 (hotkey_all_mask | hotkey_source_mask)
2489 & ~hotkey_reserved_mask);
2492 static struct device_attribute dev_attr_hotkey_recommended_mask =
2493 __ATTR(hotkey_recommended_mask, S_IRUGO,
2494 hotkey_recommended_mask_show, NULL);
2496 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2498 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2499 static ssize_t hotkey_source_mask_show(struct device *dev,
2500 struct device_attribute *attr,
2501 char *buf)
2503 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2506 static ssize_t hotkey_source_mask_store(struct device *dev,
2507 struct device_attribute *attr,
2508 const char *buf, size_t count)
2510 unsigned long t;
2511 u32 r_ev;
2512 int rc;
2514 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2515 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2516 return -EINVAL;
2518 if (mutex_lock_killable(&hotkey_mutex))
2519 return -ERESTARTSYS;
2521 HOTKEY_CONFIG_CRITICAL_START
2522 hotkey_source_mask = t;
2523 HOTKEY_CONFIG_CRITICAL_END
2525 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2526 ~hotkey_source_mask);
2527 hotkey_poll_setup(true);
2529 /* check if events needed by the driver got disabled */
2530 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2531 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2533 mutex_unlock(&hotkey_mutex);
2535 if (rc < 0)
2536 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2537 "firmware event mask!\n");
2539 if (r_ev)
2540 printk(TPACPI_NOTICE "hotkey_source_mask: "
2541 "some important events were disabled: "
2542 "0x%04x\n", r_ev);
2544 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2546 return (rc < 0) ? rc : count;
2549 static struct device_attribute dev_attr_hotkey_source_mask =
2550 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2551 hotkey_source_mask_show, hotkey_source_mask_store);
2553 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2554 static ssize_t hotkey_poll_freq_show(struct device *dev,
2555 struct device_attribute *attr,
2556 char *buf)
2558 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2561 static ssize_t hotkey_poll_freq_store(struct device *dev,
2562 struct device_attribute *attr,
2563 const char *buf, size_t count)
2565 unsigned long t;
2567 if (parse_strtoul(buf, 25, &t))
2568 return -EINVAL;
2570 if (mutex_lock_killable(&hotkey_mutex))
2571 return -ERESTARTSYS;
2573 hotkey_poll_set_freq(t);
2574 hotkey_poll_setup(true);
2576 mutex_unlock(&hotkey_mutex);
2578 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2580 return count;
2583 static struct device_attribute dev_attr_hotkey_poll_freq =
2584 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2585 hotkey_poll_freq_show, hotkey_poll_freq_store);
2587 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2589 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2590 static ssize_t hotkey_radio_sw_show(struct device *dev,
2591 struct device_attribute *attr,
2592 char *buf)
2594 int res, s;
2595 res = hotkey_get_wlsw(&s);
2596 if (res < 0)
2597 return res;
2599 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2602 static struct device_attribute dev_attr_hotkey_radio_sw =
2603 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2605 static void hotkey_radio_sw_notify_change(void)
2607 if (tp_features.hotkey_wlsw)
2608 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2609 "hotkey_radio_sw");
2612 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2613 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2614 struct device_attribute *attr,
2615 char *buf)
2617 int res, s;
2618 res = hotkey_get_tablet_mode(&s);
2619 if (res < 0)
2620 return res;
2622 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2625 static struct device_attribute dev_attr_hotkey_tablet_mode =
2626 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2628 static void hotkey_tablet_mode_notify_change(void)
2630 if (tp_features.hotkey_tablet)
2631 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2632 "hotkey_tablet_mode");
2635 /* sysfs hotkey report_mode -------------------------------------------- */
2636 static ssize_t hotkey_report_mode_show(struct device *dev,
2637 struct device_attribute *attr,
2638 char *buf)
2640 return snprintf(buf, PAGE_SIZE, "%d\n",
2641 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2644 static struct device_attribute dev_attr_hotkey_report_mode =
2645 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2647 /* sysfs wakeup reason (pollable) -------------------------------------- */
2648 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2649 struct device_attribute *attr,
2650 char *buf)
2652 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2655 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2656 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2658 static void hotkey_wakeup_reason_notify_change(void)
2660 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2661 "wakeup_reason");
2664 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2665 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2666 struct device_attribute *attr,
2667 char *buf)
2669 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2672 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2673 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2674 hotkey_wakeup_hotunplug_complete_show, NULL);
2676 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2678 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2679 "wakeup_hotunplug_complete");
2682 /* --------------------------------------------------------------------- */
2684 static struct attribute *hotkey_attributes[] __initdata = {
2685 &dev_attr_hotkey_enable.attr,
2686 &dev_attr_hotkey_bios_enabled.attr,
2687 &dev_attr_hotkey_bios_mask.attr,
2688 &dev_attr_hotkey_report_mode.attr,
2689 &dev_attr_hotkey_wakeup_reason.attr,
2690 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2691 &dev_attr_hotkey_mask.attr,
2692 &dev_attr_hotkey_all_mask.attr,
2693 &dev_attr_hotkey_recommended_mask.attr,
2694 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2695 &dev_attr_hotkey_source_mask.attr,
2696 &dev_attr_hotkey_poll_freq.attr,
2697 #endif
2700 static void bluetooth_update_rfk(void);
2701 static void wan_update_rfk(void);
2702 static void uwb_update_rfk(void);
2703 static void tpacpi_send_radiosw_update(void)
2705 int wlsw;
2707 /* Sync these BEFORE sending any rfkill events */
2708 if (tp_features.bluetooth)
2709 bluetooth_update_rfk();
2710 if (tp_features.wan)
2711 wan_update_rfk();
2712 if (tp_features.uwb)
2713 uwb_update_rfk();
2715 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2716 mutex_lock(&tpacpi_inputdev_send_mutex);
2718 input_report_switch(tpacpi_inputdev,
2719 SW_RFKILL_ALL, !!wlsw);
2720 input_sync(tpacpi_inputdev);
2722 mutex_unlock(&tpacpi_inputdev_send_mutex);
2724 hotkey_radio_sw_notify_change();
2727 static void hotkey_exit(void)
2729 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2730 mutex_lock(&hotkey_mutex);
2731 hotkey_poll_stop_sync();
2732 mutex_unlock(&hotkey_mutex);
2733 #endif
2735 if (hotkey_dev_attributes)
2736 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2738 kfree(hotkey_keycode_map);
2740 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2741 "restoring original HKEY status and mask\n");
2742 /* yes, there is a bitwise or below, we want the
2743 * functions to be called even if one of them fail */
2744 if (((tp_features.hotkey_mask &&
2745 hotkey_mask_set(hotkey_orig_mask)) |
2746 hotkey_status_set(false)) != 0)
2747 printk(TPACPI_ERR
2748 "failed to restore hot key mask "
2749 "to BIOS defaults\n");
2752 static void __init hotkey_unmap(const unsigned int scancode)
2754 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2755 clear_bit(hotkey_keycode_map[scancode],
2756 tpacpi_inputdev->keybit);
2757 hotkey_keycode_map[scancode] = KEY_RESERVED;
2762 * HKEY quirks:
2763 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2766 #define TPACPI_HK_Q_INIMASK 0x0001
2768 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2769 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2770 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2771 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2772 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2773 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2774 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2775 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2776 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2777 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2778 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2779 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2780 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2781 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2782 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2783 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2784 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2785 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2786 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2787 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2790 static int __init hotkey_init(struct ibm_init_struct *iibm)
2792 /* Requirements for changing the default keymaps:
2794 * 1. Many of the keys are mapped to KEY_RESERVED for very
2795 * good reasons. Do not change them unless you have deep
2796 * knowledge on the IBM and Lenovo ThinkPad firmware for
2797 * the various ThinkPad models. The driver behaves
2798 * differently for KEY_RESERVED: such keys have their
2799 * hot key mask *unset* in mask_recommended, and also
2800 * in the initial hot key mask programmed into the
2801 * firmware at driver load time, which means the firm-
2802 * ware may react very differently if you change them to
2803 * something else;
2805 * 2. You must be subscribed to the linux-thinkpad and
2806 * ibm-acpi-devel mailing lists, and you should read the
2807 * list archives since 2007 if you want to change the
2808 * keymaps. This requirement exists so that you will
2809 * know the past history of problems with the thinkpad-
2810 * acpi driver keymaps, and also that you will be
2811 * listening to any bug reports;
2813 * 3. Do not send thinkpad-acpi specific patches directly to
2814 * for merging, *ever*. Send them to the linux-acpi
2815 * mailinglist for comments. Merging is to be done only
2816 * through acpi-test and the ACPI maintainer.
2818 * If the above is too much to ask, don't change the keymap.
2819 * Ask the thinkpad-acpi maintainer to do it, instead.
2821 static u16 ibm_keycode_map[] __initdata = {
2822 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2823 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2824 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2825 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2827 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2828 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2829 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2830 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2832 /* brightness: firmware always reacts to them */
2833 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2834 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2836 /* Thinklight: firmware always react to it */
2837 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2839 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2840 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2842 /* Volume: firmware always react to it and reprograms
2843 * the built-in *extra* mixer. Never map it to control
2844 * another mixer by default. */
2845 KEY_RESERVED, /* 0x14: VOLUME UP */
2846 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2847 KEY_RESERVED, /* 0x16: MUTE */
2849 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2851 /* (assignments unknown, please report if found) */
2852 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2853 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2855 static u16 lenovo_keycode_map[] __initdata = {
2856 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2857 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2858 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2859 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2861 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2862 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2863 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2864 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2866 /* These should be enabled --only-- when ACPI video
2867 * is disabled (i.e. in "vendor" mode), and are handled
2868 * in a special way by the init code */
2869 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2870 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2872 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2874 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2875 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2877 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2878 * react to it and reprograms the built-in *extra* mixer.
2879 * Never map it to control another mixer by default.
2881 * T60?, T61, R60?, R61: firmware and EC tries to send
2882 * these over the regular keyboard, so these are no-ops,
2883 * but there are still weird bugs re. MUTE, so do not
2884 * change unless you get test reports from all Lenovo
2885 * models. May cause the BIOS to interfere with the
2886 * HDA mixer.
2888 KEY_RESERVED, /* 0x14: VOLUME UP */
2889 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2890 KEY_RESERVED, /* 0x16: MUTE */
2892 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2894 /* (assignments unknown, please report if found) */
2895 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2896 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2899 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2900 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2901 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2903 int res, i;
2904 int status;
2905 int hkeyv;
2906 bool radiosw_state = false;
2907 bool tabletsw_state = false;
2909 unsigned long quirks;
2911 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2912 "initializing hotkey subdriver\n");
2914 BUG_ON(!tpacpi_inputdev);
2915 BUG_ON(tpacpi_inputdev->open != NULL ||
2916 tpacpi_inputdev->close != NULL);
2918 TPACPI_ACPIHANDLE_INIT(hkey);
2919 mutex_init(&hotkey_mutex);
2921 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2922 mutex_init(&hotkey_thread_mutex);
2923 mutex_init(&hotkey_thread_data_mutex);
2924 #endif
2926 /* hotkey not supported on 570 */
2927 tp_features.hotkey = hkey_handle != NULL;
2929 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2930 "hotkeys are %s\n",
2931 str_supported(tp_features.hotkey));
2933 if (!tp_features.hotkey)
2934 return 1;
2936 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2937 ARRAY_SIZE(tpacpi_hotkey_qtable));
2939 tpacpi_disable_brightness_delay();
2941 /* MUST have enough space for all attributes to be added to
2942 * hotkey_dev_attributes */
2943 hotkey_dev_attributes = create_attr_set(
2944 ARRAY_SIZE(hotkey_attributes) + 2,
2945 NULL);
2946 if (!hotkey_dev_attributes)
2947 return -ENOMEM;
2948 res = add_many_to_attr_set(hotkey_dev_attributes,
2949 hotkey_attributes,
2950 ARRAY_SIZE(hotkey_attributes));
2951 if (res)
2952 goto err_exit;
2954 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2955 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2956 for HKEY interface version 0x100 */
2957 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2958 if ((hkeyv >> 8) != 1) {
2959 printk(TPACPI_ERR "unknown version of the "
2960 "HKEY interface: 0x%x\n", hkeyv);
2961 printk(TPACPI_ERR "please report this to %s\n",
2962 TPACPI_MAIL);
2963 } else {
2965 * MHKV 0x100 in A31, R40, R40e,
2966 * T4x, X31, and later
2968 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2969 "firmware HKEY interface version: 0x%x\n",
2970 hkeyv);
2972 /* Paranoia check AND init hotkey_all_mask */
2973 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2974 "MHKA", "qd")) {
2975 printk(TPACPI_ERR
2976 "missing MHKA handler, "
2977 "please report this to %s\n",
2978 TPACPI_MAIL);
2979 /* Fallback: pre-init for FN+F3,F4,F12 */
2980 hotkey_all_mask = 0x080cU;
2981 } else {
2982 tp_features.hotkey_mask = 1;
2987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2988 "hotkey masks are %s\n",
2989 str_supported(tp_features.hotkey_mask));
2991 /* Init hotkey_all_mask if not initialized yet */
2992 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2993 (quirks & TPACPI_HK_Q_INIMASK))
2994 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2996 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2997 if (tp_features.hotkey_mask) {
2998 /* hotkey_source_mask *must* be zero for
2999 * the first hotkey_mask_get to return hotkey_orig_mask */
3000 res = hotkey_mask_get();
3001 if (res)
3002 goto err_exit;
3004 hotkey_orig_mask = hotkey_acpi_mask;
3005 } else {
3006 hotkey_orig_mask = hotkey_all_mask;
3007 hotkey_acpi_mask = hotkey_all_mask;
3010 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3011 if (dbg_wlswemul) {
3012 tp_features.hotkey_wlsw = 1;
3013 radiosw_state = !!tpacpi_wlsw_emulstate;
3014 printk(TPACPI_INFO
3015 "radio switch emulation enabled\n");
3016 } else
3017 #endif
3018 /* Not all thinkpads have a hardware radio switch */
3019 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3020 tp_features.hotkey_wlsw = 1;
3021 radiosw_state = !!status;
3022 printk(TPACPI_INFO
3023 "radio switch found; radios are %s\n",
3024 enabled(status, 0));
3026 if (tp_features.hotkey_wlsw)
3027 res = add_to_attr_set(hotkey_dev_attributes,
3028 &dev_attr_hotkey_radio_sw.attr);
3030 /* For X41t, X60t, X61t Tablets... */
3031 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3032 tp_features.hotkey_tablet = 1;
3033 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3034 printk(TPACPI_INFO
3035 "possible tablet mode switch found; "
3036 "ThinkPad in %s mode\n",
3037 (tabletsw_state) ? "tablet" : "laptop");
3038 res = add_to_attr_set(hotkey_dev_attributes,
3039 &dev_attr_hotkey_tablet_mode.attr);
3042 if (!res)
3043 res = register_attr_set_with_sysfs(
3044 hotkey_dev_attributes,
3045 &tpacpi_pdev->dev.kobj);
3046 if (res)
3047 goto err_exit;
3049 /* Set up key map */
3051 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3052 GFP_KERNEL);
3053 if (!hotkey_keycode_map) {
3054 printk(TPACPI_ERR
3055 "failed to allocate memory for key map\n");
3056 res = -ENOMEM;
3057 goto err_exit;
3060 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3061 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3062 "using Lenovo default hot key map\n");
3063 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3064 TPACPI_HOTKEY_MAP_SIZE);
3065 } else {
3066 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3067 "using IBM default hot key map\n");
3068 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3069 TPACPI_HOTKEY_MAP_SIZE);
3072 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3073 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3074 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3075 tpacpi_inputdev->keycode = hotkey_keycode_map;
3076 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3077 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3078 input_set_capability(tpacpi_inputdev, EV_KEY,
3079 hotkey_keycode_map[i]);
3080 } else {
3081 if (i < sizeof(hotkey_reserved_mask)*8)
3082 hotkey_reserved_mask |= 1 << i;
3086 if (tp_features.hotkey_wlsw) {
3087 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3088 input_report_switch(tpacpi_inputdev,
3089 SW_RFKILL_ALL, radiosw_state);
3091 if (tp_features.hotkey_tablet) {
3092 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3093 input_report_switch(tpacpi_inputdev,
3094 SW_TABLET_MODE, tabletsw_state);
3097 /* Do not issue duplicate brightness change events to
3098 * userspace */
3099 if (!tp_features.bright_acpimode)
3100 /* update bright_acpimode... */
3101 tpacpi_check_std_acpi_brightness_support();
3103 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3104 printk(TPACPI_INFO
3105 "This ThinkPad has standard ACPI backlight "
3106 "brightness control, supported by the ACPI "
3107 "video driver\n");
3108 printk(TPACPI_NOTICE
3109 "Disabling thinkpad-acpi brightness events "
3110 "by default...\n");
3112 /* Disable brightness up/down on Lenovo thinkpads when
3113 * ACPI is handling them, otherwise it is plain impossible
3114 * for userspace to do something even remotely sane */
3115 hotkey_reserved_mask |=
3116 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3117 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3118 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3119 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3122 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3123 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3124 & ~hotkey_all_mask
3125 & ~hotkey_reserved_mask;
3127 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3128 "hotkey source mask 0x%08x, polling freq %u\n",
3129 hotkey_source_mask, hotkey_poll_freq);
3130 #endif
3132 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3133 "enabling firmware HKEY event interface...\n");
3134 res = hotkey_status_set(true);
3135 if (res) {
3136 hotkey_exit();
3137 return res;
3139 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3140 | hotkey_driver_mask)
3141 & ~hotkey_source_mask);
3142 if (res < 0 && res != -ENXIO) {
3143 hotkey_exit();
3144 return res;
3146 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3147 & ~hotkey_reserved_mask;
3148 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3149 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3150 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3152 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3153 "legacy ibm/hotkey event reporting over procfs %s\n",
3154 (hotkey_report_mode < 2) ?
3155 "enabled" : "disabled");
3157 tpacpi_inputdev->open = &hotkey_inputdev_open;
3158 tpacpi_inputdev->close = &hotkey_inputdev_close;
3160 hotkey_poll_setup_safe(true);
3162 return 0;
3164 err_exit:
3165 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3166 hotkey_dev_attributes = NULL;
3168 return (res < 0)? res : 1;
3171 static bool hotkey_notify_hotkey(const u32 hkey,
3172 bool *send_acpi_ev,
3173 bool *ignore_acpi_ev)
3175 /* 0x1000-0x1FFF: key presses */
3176 unsigned int scancode = hkey & 0xfff;
3177 *send_acpi_ev = true;
3178 *ignore_acpi_ev = false;
3180 if (scancode > 0 && scancode < 0x21) {
3181 scancode--;
3182 if (!(hotkey_source_mask & (1 << scancode))) {
3183 tpacpi_input_send_key_masked(scancode);
3184 *send_acpi_ev = false;
3185 } else {
3186 *ignore_acpi_ev = true;
3188 return true;
3190 return false;
3193 static bool hotkey_notify_wakeup(const u32 hkey,
3194 bool *send_acpi_ev,
3195 bool *ignore_acpi_ev)
3197 /* 0x2000-0x2FFF: Wakeup reason */
3198 *send_acpi_ev = true;
3199 *ignore_acpi_ev = false;
3201 switch (hkey) {
3202 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3203 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3204 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3205 *ignore_acpi_ev = true;
3206 break;
3208 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3209 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3210 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3211 *ignore_acpi_ev = true;
3212 break;
3214 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3215 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3216 printk(TPACPI_ALERT
3217 "EMERGENCY WAKEUP: battery almost empty\n");
3218 /* how to auto-heal: */
3219 /* 2313: woke up from S3, go to S4/S5 */
3220 /* 2413: woke up from S4, go to S5 */
3221 break;
3223 default:
3224 return false;
3227 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3228 printk(TPACPI_INFO
3229 "woke up due to a hot-unplug "
3230 "request...\n");
3231 hotkey_wakeup_reason_notify_change();
3233 return true;
3236 static bool hotkey_notify_usrevent(const u32 hkey,
3237 bool *send_acpi_ev,
3238 bool *ignore_acpi_ev)
3240 /* 0x5000-0x5FFF: human interface helpers */
3241 *send_acpi_ev = true;
3242 *ignore_acpi_ev = false;
3244 switch (hkey) {
3245 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3246 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3247 return true;
3249 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3250 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3251 tpacpi_input_send_tabletsw();
3252 hotkey_tablet_mode_notify_change();
3253 *send_acpi_ev = false;
3254 return true;
3256 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3257 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3258 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3259 /* do not propagate these events */
3260 *ignore_acpi_ev = true;
3261 return true;
3263 default:
3264 return false;
3268 static void thermal_dump_all_sensors(void);
3270 static bool hotkey_notify_thermal(const u32 hkey,
3271 bool *send_acpi_ev,
3272 bool *ignore_acpi_ev)
3274 bool known = true;
3276 /* 0x6000-0x6FFF: thermal alarms */
3277 *send_acpi_ev = true;
3278 *ignore_acpi_ev = false;
3280 switch (hkey) {
3281 case TP_HKEY_EV_THM_TABLE_CHANGED:
3282 printk(TPACPI_INFO
3283 "EC reports that Thermal Table has changed\n");
3284 /* recommended action: do nothing, we don't have
3285 * Lenovo ATM information */
3286 return true;
3287 case TP_HKEY_EV_ALARM_BAT_HOT:
3288 printk(TPACPI_CRIT
3289 "THERMAL ALARM: battery is too hot!\n");
3290 /* recommended action: warn user through gui */
3291 break;
3292 case TP_HKEY_EV_ALARM_BAT_XHOT:
3293 printk(TPACPI_ALERT
3294 "THERMAL EMERGENCY: battery is extremely hot!\n");
3295 /* recommended action: immediate sleep/hibernate */
3296 break;
3297 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3298 printk(TPACPI_CRIT
3299 "THERMAL ALARM: "
3300 "a sensor reports something is too hot!\n");
3301 /* recommended action: warn user through gui, that */
3302 /* some internal component is too hot */
3303 break;
3304 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3305 printk(TPACPI_ALERT
3306 "THERMAL EMERGENCY: "
3307 "a sensor reports something is extremely hot!\n");
3308 /* recommended action: immediate sleep/hibernate */
3309 break;
3310 default:
3311 printk(TPACPI_ALERT
3312 "THERMAL ALERT: unknown thermal alarm received\n");
3313 known = false;
3316 thermal_dump_all_sensors();
3318 return known;
3321 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3323 u32 hkey;
3324 bool send_acpi_ev;
3325 bool ignore_acpi_ev;
3326 bool known_ev;
3328 if (event != 0x80) {
3329 printk(TPACPI_ERR
3330 "unknown HKEY notification event %d\n", event);
3331 /* forward it to userspace, maybe it knows how to handle it */
3332 acpi_bus_generate_netlink_event(
3333 ibm->acpi->device->pnp.device_class,
3334 dev_name(&ibm->acpi->device->dev),
3335 event, 0);
3336 return;
3339 while (1) {
3340 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3341 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3342 return;
3345 if (hkey == 0) {
3346 /* queue empty */
3347 return;
3350 send_acpi_ev = true;
3351 ignore_acpi_ev = false;
3353 switch (hkey >> 12) {
3354 case 1:
3355 /* 0x1000-0x1FFF: key presses */
3356 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3357 &ignore_acpi_ev);
3358 break;
3359 case 2:
3360 /* 0x2000-0x2FFF: Wakeup reason */
3361 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3362 &ignore_acpi_ev);
3363 break;
3364 case 3:
3365 /* 0x3000-0x3FFF: bay-related wakeups */
3366 switch (hkey) {
3367 case TP_HKEY_EV_BAYEJ_ACK:
3368 hotkey_autosleep_ack = 1;
3369 printk(TPACPI_INFO
3370 "bay ejected\n");
3371 hotkey_wakeup_hotunplug_complete_notify_change();
3372 known_ev = true;
3373 break;
3374 case TP_HKEY_EV_OPTDRV_EJ:
3375 /* FIXME: kick libata if SATA link offline */
3376 known_ev = true;
3377 break;
3378 default:
3379 known_ev = false;
3381 break;
3382 case 4:
3383 /* 0x4000-0x4FFF: dock-related wakeups */
3384 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3385 hotkey_autosleep_ack = 1;
3386 printk(TPACPI_INFO
3387 "undocked\n");
3388 hotkey_wakeup_hotunplug_complete_notify_change();
3389 known_ev = true;
3390 } else {
3391 known_ev = false;
3393 break;
3394 case 5:
3395 /* 0x5000-0x5FFF: human interface helpers */
3396 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3397 &ignore_acpi_ev);
3398 break;
3399 case 6:
3400 /* 0x6000-0x6FFF: thermal alarms */
3401 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3402 &ignore_acpi_ev);
3403 break;
3404 case 7:
3405 /* 0x7000-0x7FFF: misc */
3406 if (tp_features.hotkey_wlsw &&
3407 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3408 tpacpi_send_radiosw_update();
3409 send_acpi_ev = 0;
3410 known_ev = true;
3411 break;
3413 /* fallthrough to default */
3414 default:
3415 known_ev = false;
3417 if (!known_ev) {
3418 printk(TPACPI_NOTICE
3419 "unhandled HKEY event 0x%04x\n", hkey);
3420 printk(TPACPI_NOTICE
3421 "please report the conditions when this "
3422 "event happened to %s\n", TPACPI_MAIL);
3425 /* Legacy events */
3426 if (!ignore_acpi_ev &&
3427 (send_acpi_ev || hotkey_report_mode < 2)) {
3428 acpi_bus_generate_proc_event(ibm->acpi->device,
3429 event, hkey);
3432 /* netlink events */
3433 if (!ignore_acpi_ev && send_acpi_ev) {
3434 acpi_bus_generate_netlink_event(
3435 ibm->acpi->device->pnp.device_class,
3436 dev_name(&ibm->acpi->device->dev),
3437 event, hkey);
3442 static void hotkey_suspend(pm_message_t state)
3444 /* Do these on suspend, we get the events on early resume! */
3445 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3446 hotkey_autosleep_ack = 0;
3449 static void hotkey_resume(void)
3451 tpacpi_disable_brightness_delay();
3453 if (hotkey_status_set(true) < 0 ||
3454 hotkey_mask_set(hotkey_acpi_mask) < 0)
3455 printk(TPACPI_ERR
3456 "error while attempting to reset the event "
3457 "firmware interface\n");
3459 tpacpi_send_radiosw_update();
3460 hotkey_tablet_mode_notify_change();
3461 hotkey_wakeup_reason_notify_change();
3462 hotkey_wakeup_hotunplug_complete_notify_change();
3463 hotkey_poll_setup_safe(false);
3466 /* procfs -------------------------------------------------------------- */
3467 static int hotkey_read(struct seq_file *m)
3469 int res, status;
3471 if (!tp_features.hotkey) {
3472 seq_printf(m, "status:\t\tnot supported\n");
3473 return 0;
3476 if (mutex_lock_killable(&hotkey_mutex))
3477 return -ERESTARTSYS;
3478 res = hotkey_status_get(&status);
3479 if (!res)
3480 res = hotkey_mask_get();
3481 mutex_unlock(&hotkey_mutex);
3482 if (res)
3483 return res;
3485 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3486 if (hotkey_all_mask) {
3487 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3488 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3489 } else {
3490 seq_printf(m, "mask:\t\tnot supported\n");
3491 seq_printf(m, "commands:\tenable, disable, reset\n");
3494 return 0;
3497 static void hotkey_enabledisable_warn(bool enable)
3499 tpacpi_log_usertask("procfs hotkey enable/disable");
3500 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3501 TPACPI_WARN
3502 "hotkey enable/disable functionality has been "
3503 "removed from the driver. Hotkeys are always "
3504 "enabled\n"))
3505 printk(TPACPI_ERR
3506 "Please remove the hotkey=enable module "
3507 "parameter, it is deprecated. Hotkeys are always "
3508 "enabled\n");
3511 static int hotkey_write(char *buf)
3513 int res;
3514 u32 mask;
3515 char *cmd;
3517 if (!tp_features.hotkey)
3518 return -ENODEV;
3520 if (mutex_lock_killable(&hotkey_mutex))
3521 return -ERESTARTSYS;
3523 mask = hotkey_user_mask;
3525 res = 0;
3526 while ((cmd = next_cmd(&buf))) {
3527 if (strlencmp(cmd, "enable") == 0) {
3528 hotkey_enabledisable_warn(1);
3529 } else if (strlencmp(cmd, "disable") == 0) {
3530 hotkey_enabledisable_warn(0);
3531 res = -EPERM;
3532 } else if (strlencmp(cmd, "reset") == 0) {
3533 mask = (hotkey_all_mask | hotkey_source_mask)
3534 & ~hotkey_reserved_mask;
3535 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3536 /* mask set */
3537 } else if (sscanf(cmd, "%x", &mask) == 1) {
3538 /* mask set */
3539 } else {
3540 res = -EINVAL;
3541 goto errexit;
3545 if (!res) {
3546 tpacpi_disclose_usertask("procfs hotkey",
3547 "set mask to 0x%08x\n", mask);
3548 res = hotkey_user_mask_set(mask);
3551 errexit:
3552 mutex_unlock(&hotkey_mutex);
3553 return res;
3556 static const struct acpi_device_id ibm_htk_device_ids[] = {
3557 {TPACPI_ACPI_HKEY_HID, 0},
3558 {"", 0},
3561 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3562 .hid = ibm_htk_device_ids,
3563 .notify = hotkey_notify,
3564 .handle = &hkey_handle,
3565 .type = ACPI_DEVICE_NOTIFY,
3568 static struct ibm_struct hotkey_driver_data = {
3569 .name = "hotkey",
3570 .read = hotkey_read,
3571 .write = hotkey_write,
3572 .exit = hotkey_exit,
3573 .resume = hotkey_resume,
3574 .suspend = hotkey_suspend,
3575 .acpi = &ibm_hotkey_acpidriver,
3578 /*************************************************************************
3579 * Bluetooth subdriver
3582 enum {
3583 /* ACPI GBDC/SBDC bits */
3584 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3585 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3586 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3587 off / last state */
3590 enum {
3591 /* ACPI \BLTH commands */
3592 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3593 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3594 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3595 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3596 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3599 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3601 static struct rfkill *tpacpi_bluetooth_rfkill;
3603 static void bluetooth_suspend(pm_message_t state)
3605 /* Try to make sure radio will resume powered off */
3606 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3607 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3608 vdbg_printk(TPACPI_DBG_RFKILL,
3609 "bluetooth power down on resume request failed\n");
3612 static int bluetooth_get_radiosw(void)
3614 int status;
3616 if (!tp_features.bluetooth)
3617 return -ENODEV;
3619 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3620 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3621 return RFKILL_STATE_HARD_BLOCKED;
3623 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3624 if (dbg_bluetoothemul)
3625 return (tpacpi_bluetooth_emulstate) ?
3626 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3627 #endif
3629 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3630 return -EIO;
3632 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3633 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3636 static void bluetooth_update_rfk(void)
3638 int status;
3640 if (!tpacpi_bluetooth_rfkill)
3641 return;
3643 status = bluetooth_get_radiosw();
3644 if (status < 0)
3645 return;
3646 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3648 vdbg_printk(TPACPI_DBG_RFKILL,
3649 "forced rfkill state to %d\n",
3650 status);
3653 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3655 int status;
3657 if (!tp_features.bluetooth)
3658 return -ENODEV;
3660 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3661 * reason to risk weird behaviour. */
3662 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3663 && radio_on)
3664 return -EPERM;
3666 vdbg_printk(TPACPI_DBG_RFKILL,
3667 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3669 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3670 if (dbg_bluetoothemul) {
3671 tpacpi_bluetooth_emulstate = !!radio_on;
3672 if (update_rfk)
3673 bluetooth_update_rfk();
3674 return 0;
3676 #endif
3678 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3679 if (radio_on)
3680 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3681 else
3682 status = 0;
3683 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3684 return -EIO;
3686 if (update_rfk)
3687 bluetooth_update_rfk();
3689 return 0;
3692 /* sysfs bluetooth enable ---------------------------------------------- */
3693 static ssize_t bluetooth_enable_show(struct device *dev,
3694 struct device_attribute *attr,
3695 char *buf)
3697 int status;
3699 printk_deprecated_rfkill_attribute("bluetooth_enable");
3701 status = bluetooth_get_radiosw();
3702 if (status < 0)
3703 return status;
3705 return snprintf(buf, PAGE_SIZE, "%d\n",
3706 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3709 static ssize_t bluetooth_enable_store(struct device *dev,
3710 struct device_attribute *attr,
3711 const char *buf, size_t count)
3713 unsigned long t;
3714 int res;
3716 printk_deprecated_rfkill_attribute("bluetooth_enable");
3718 if (parse_strtoul(buf, 1, &t))
3719 return -EINVAL;
3721 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3723 res = bluetooth_set_radiosw(t, 1);
3725 return (res) ? res : count;
3728 static struct device_attribute dev_attr_bluetooth_enable =
3729 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3730 bluetooth_enable_show, bluetooth_enable_store);
3732 /* --------------------------------------------------------------------- */
3734 static struct attribute *bluetooth_attributes[] = {
3735 &dev_attr_bluetooth_enable.attr,
3736 NULL
3739 static const struct attribute_group bluetooth_attr_group = {
3740 .attrs = bluetooth_attributes,
3743 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3745 int bts = bluetooth_get_radiosw();
3747 if (bts < 0)
3748 return bts;
3750 *state = bts;
3751 return 0;
3754 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3756 dbg_printk(TPACPI_DBG_RFKILL,
3757 "request to change radio state to %d\n", state);
3758 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3761 static void bluetooth_shutdown(void)
3763 /* Order firmware to save current state to NVRAM */
3764 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3765 TP_ACPI_BLTH_SAVE_STATE))
3766 printk(TPACPI_NOTICE
3767 "failed to save bluetooth state to NVRAM\n");
3768 else
3769 vdbg_printk(TPACPI_DBG_RFKILL,
3770 "bluestooth state saved to NVRAM\n");
3773 static void bluetooth_exit(void)
3775 bluetooth_shutdown();
3777 if (tpacpi_bluetooth_rfkill)
3778 rfkill_unregister(tpacpi_bluetooth_rfkill);
3780 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3781 &bluetooth_attr_group);
3784 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3786 int res;
3787 int status = 0;
3789 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3790 "initializing bluetooth subdriver\n");
3792 TPACPI_ACPIHANDLE_INIT(hkey);
3794 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3795 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3796 tp_features.bluetooth = hkey_handle &&
3797 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3799 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3800 "bluetooth is %s, status 0x%02x\n",
3801 str_supported(tp_features.bluetooth),
3802 status);
3804 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3805 if (dbg_bluetoothemul) {
3806 tp_features.bluetooth = 1;
3807 printk(TPACPI_INFO
3808 "bluetooth switch emulation enabled\n");
3809 } else
3810 #endif
3811 if (tp_features.bluetooth &&
3812 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3813 /* no bluetooth hardware present in system */
3814 tp_features.bluetooth = 0;
3815 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3816 "bluetooth hardware not installed\n");
3819 if (!tp_features.bluetooth)
3820 return 1;
3822 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3823 &bluetooth_attr_group);
3824 if (res)
3825 return res;
3827 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3828 &tpacpi_bluetooth_rfkill,
3829 RFKILL_TYPE_BLUETOOTH,
3830 TPACPI_RFK_BLUETOOTH_SW_NAME,
3831 true,
3832 tpacpi_bluetooth_rfk_set,
3833 tpacpi_bluetooth_rfk_get);
3834 if (res) {
3835 bluetooth_exit();
3836 return res;
3839 return 0;
3842 /* procfs -------------------------------------------------------------- */
3843 static int bluetooth_read(struct seq_file *m)
3845 int status = bluetooth_get_radiosw();
3847 if (!tp_features.bluetooth)
3848 seq_printf(m, "status:\t\tnot supported\n");
3849 else {
3850 seq_printf(m, "status:\t\t%s\n",
3851 (status == RFKILL_STATE_UNBLOCKED) ?
3852 "enabled" : "disabled");
3853 seq_printf(m, "commands:\tenable, disable\n");
3856 return 0;
3859 static int bluetooth_write(char *buf)
3861 char *cmd;
3862 int state = -1;
3864 if (!tp_features.bluetooth)
3865 return -ENODEV;
3867 while ((cmd = next_cmd(&buf))) {
3868 if (strlencmp(cmd, "enable") == 0) {
3869 state = 1;
3870 } else if (strlencmp(cmd, "disable") == 0) {
3871 state = 0;
3872 } else
3873 return -EINVAL;
3876 if (state != -1) {
3877 tpacpi_disclose_usertask("procfs bluetooth",
3878 "attempt to %s\n",
3879 state ? "enable" : "disable");
3880 bluetooth_set_radiosw(state, 1);
3883 return 0;
3886 static struct ibm_struct bluetooth_driver_data = {
3887 .name = "bluetooth",
3888 .read = bluetooth_read,
3889 .write = bluetooth_write,
3890 .exit = bluetooth_exit,
3891 .suspend = bluetooth_suspend,
3892 .shutdown = bluetooth_shutdown,
3895 /*************************************************************************
3896 * Wan subdriver
3899 enum {
3900 /* ACPI GWAN/SWAN bits */
3901 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3902 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3903 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3904 off / last state */
3907 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3909 static struct rfkill *tpacpi_wan_rfkill;
3911 static void wan_suspend(pm_message_t state)
3913 /* Try to make sure radio will resume powered off */
3914 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3915 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3916 vdbg_printk(TPACPI_DBG_RFKILL,
3917 "WWAN power down on resume request failed\n");
3920 static int wan_get_radiosw(void)
3922 int status;
3924 if (!tp_features.wan)
3925 return -ENODEV;
3927 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3928 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3929 return RFKILL_STATE_HARD_BLOCKED;
3931 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3932 if (dbg_wwanemul)
3933 return (tpacpi_wwan_emulstate) ?
3934 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3935 #endif
3937 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3938 return -EIO;
3940 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3941 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3944 static void wan_update_rfk(void)
3946 int status;
3948 if (!tpacpi_wan_rfkill)
3949 return;
3951 status = wan_get_radiosw();
3952 if (status < 0)
3953 return;
3954 rfkill_force_state(tpacpi_wan_rfkill, status);
3956 vdbg_printk(TPACPI_DBG_RFKILL,
3957 "forced rfkill state to %d\n",
3958 status);
3961 static int wan_set_radiosw(int radio_on, int update_rfk)
3963 int status;
3965 if (!tp_features.wan)
3966 return -ENODEV;
3968 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3969 * reason to risk weird behaviour. */
3970 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3971 && radio_on)
3972 return -EPERM;
3974 vdbg_printk(TPACPI_DBG_RFKILL,
3975 "will %s WWAN\n", radio_on ? "enable" : "disable");
3977 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3978 if (dbg_wwanemul) {
3979 tpacpi_wwan_emulstate = !!radio_on;
3980 if (update_rfk)
3981 wan_update_rfk();
3982 return 0;
3984 #endif
3986 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3987 if (radio_on)
3988 status = TP_ACPI_WANCARD_RADIOSSW;
3989 else
3990 status = 0;
3991 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3992 return -EIO;
3994 if (update_rfk)
3995 wan_update_rfk();
3997 return 0;
4000 /* sysfs wan enable ---------------------------------------------------- */
4001 static ssize_t wan_enable_show(struct device *dev,
4002 struct device_attribute *attr,
4003 char *buf)
4005 int status;
4007 printk_deprecated_rfkill_attribute("wwan_enable");
4009 status = wan_get_radiosw();
4010 if (status < 0)
4011 return status;
4013 return snprintf(buf, PAGE_SIZE, "%d\n",
4014 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
4017 static ssize_t wan_enable_store(struct device *dev,
4018 struct device_attribute *attr,
4019 const char *buf, size_t count)
4021 unsigned long t;
4022 int res;
4024 printk_deprecated_rfkill_attribute("wwan_enable");
4026 if (parse_strtoul(buf, 1, &t))
4027 return -EINVAL;
4029 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4031 res = wan_set_radiosw(t, 1);
4033 return (res) ? res : count;
4036 static struct device_attribute dev_attr_wan_enable =
4037 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4038 wan_enable_show, wan_enable_store);
4040 /* --------------------------------------------------------------------- */
4042 static struct attribute *wan_attributes[] = {
4043 &dev_attr_wan_enable.attr,
4044 NULL
4047 static const struct attribute_group wan_attr_group = {
4048 .attrs = wan_attributes,
4051 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4053 int wans = wan_get_radiosw();
4055 if (wans < 0)
4056 return wans;
4058 *state = wans;
4059 return 0;
4062 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4064 dbg_printk(TPACPI_DBG_RFKILL,
4065 "request to change radio state to %d\n", state);
4066 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4069 static void wan_shutdown(void)
4071 /* Order firmware to save current state to NVRAM */
4072 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4073 TP_ACPI_WGSV_SAVE_STATE))
4074 printk(TPACPI_NOTICE
4075 "failed to save WWAN state to NVRAM\n");
4076 else
4077 vdbg_printk(TPACPI_DBG_RFKILL,
4078 "WWAN state saved to NVRAM\n");
4081 static void wan_exit(void)
4083 wan_shutdown();
4085 if (tpacpi_wan_rfkill)
4086 rfkill_unregister(tpacpi_wan_rfkill);
4088 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4089 &wan_attr_group);
4092 static int __init wan_init(struct ibm_init_struct *iibm)
4094 int res;
4095 int status = 0;
4097 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4098 "initializing wan subdriver\n");
4100 TPACPI_ACPIHANDLE_INIT(hkey);
4102 tp_features.wan = hkey_handle &&
4103 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4105 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4106 "wan is %s, status 0x%02x\n",
4107 str_supported(tp_features.wan),
4108 status);
4110 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4111 if (dbg_wwanemul) {
4112 tp_features.wan = 1;
4113 printk(TPACPI_INFO
4114 "wwan switch emulation enabled\n");
4115 } else
4116 #endif
4117 if (tp_features.wan &&
4118 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4119 /* no wan hardware present in system */
4120 tp_features.wan = 0;
4121 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4122 "wan hardware not installed\n");
4125 if (!tp_features.wan)
4126 return 1;
4128 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4129 &wan_attr_group);
4130 if (res)
4131 return res;
4133 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4134 &tpacpi_wan_rfkill,
4135 RFKILL_TYPE_WWAN,
4136 TPACPI_RFK_WWAN_SW_NAME,
4137 true,
4138 tpacpi_wan_rfk_set,
4139 tpacpi_wan_rfk_get);
4140 if (res) {
4141 wan_exit();
4142 return res;
4145 return 0;
4148 /* procfs -------------------------------------------------------------- */
4149 static int wan_read(struct seq_file *m)
4151 int status = wan_get_radiosw();
4153 tpacpi_disclose_usertask("procfs wan", "read");
4155 if (!tp_features.wan)
4156 seq_printf(m, "status:\t\tnot supported\n");
4157 else {
4158 seq_printf(m, "status:\t\t%s\n",
4159 (status == RFKILL_STATE_UNBLOCKED) ?
4160 "enabled" : "disabled");
4161 seq_printf(m, "commands:\tenable, disable\n");
4164 return 0;
4167 static int wan_write(char *buf)
4169 char *cmd;
4170 int state = -1;
4172 if (!tp_features.wan)
4173 return -ENODEV;
4175 while ((cmd = next_cmd(&buf))) {
4176 if (strlencmp(cmd, "enable") == 0) {
4177 state = 1;
4178 } else if (strlencmp(cmd, "disable") == 0) {
4179 state = 0;
4180 } else
4181 return -EINVAL;
4184 if (state != -1) {
4185 tpacpi_disclose_usertask("procfs wan",
4186 "attempt to %s\n",
4187 state ? "enable" : "disable");
4188 wan_set_radiosw(state, 1);
4191 return 0;
4194 static struct ibm_struct wan_driver_data = {
4195 .name = "wan",
4196 .read = wan_read,
4197 .write = wan_write,
4198 .exit = wan_exit,
4199 .suspend = wan_suspend,
4200 .shutdown = wan_shutdown,
4203 /*************************************************************************
4204 * UWB subdriver
4207 enum {
4208 /* ACPI GUWB/SUWB bits */
4209 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4210 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4213 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4215 static struct rfkill *tpacpi_uwb_rfkill;
4217 static int uwb_get_radiosw(void)
4219 int status;
4221 if (!tp_features.uwb)
4222 return -ENODEV;
4224 /* WLSW overrides UWB in firmware/hardware, reflect that */
4225 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4226 return RFKILL_STATE_HARD_BLOCKED;
4228 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4229 if (dbg_uwbemul)
4230 return (tpacpi_uwb_emulstate) ?
4231 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4232 #endif
4234 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4235 return -EIO;
4237 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4238 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4241 static void uwb_update_rfk(void)
4243 int status;
4245 if (!tpacpi_uwb_rfkill)
4246 return;
4248 status = uwb_get_radiosw();
4249 if (status < 0)
4250 return;
4251 rfkill_force_state(tpacpi_uwb_rfkill, status);
4253 vdbg_printk(TPACPI_DBG_RFKILL,
4254 "forced rfkill state to %d\n",
4255 status);
4258 static int uwb_set_radiosw(int radio_on, int update_rfk)
4260 int status;
4262 if (!tp_features.uwb)
4263 return -ENODEV;
4265 /* WLSW overrides UWB in firmware/hardware, but there is no
4266 * reason to risk weird behaviour. */
4267 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4268 && radio_on)
4269 return -EPERM;
4271 vdbg_printk(TPACPI_DBG_RFKILL,
4272 "will %s UWB\n", radio_on ? "enable" : "disable");
4274 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4275 if (dbg_uwbemul) {
4276 tpacpi_uwb_emulstate = !!radio_on;
4277 if (update_rfk)
4278 uwb_update_rfk();
4279 return 0;
4281 #endif
4283 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4284 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4285 return -EIO;
4287 if (update_rfk)
4288 uwb_update_rfk();
4290 return 0;
4293 /* --------------------------------------------------------------------- */
4295 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4297 int uwbs = uwb_get_radiosw();
4299 if (uwbs < 0)
4300 return uwbs;
4302 *state = uwbs;
4303 return 0;
4306 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4308 dbg_printk(TPACPI_DBG_RFKILL,
4309 "request to change radio state to %d\n", state);
4310 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4313 static void uwb_exit(void)
4315 if (tpacpi_uwb_rfkill)
4316 rfkill_unregister(tpacpi_uwb_rfkill);
4319 static int __init uwb_init(struct ibm_init_struct *iibm)
4321 int res;
4322 int status = 0;
4324 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4325 "initializing uwb subdriver\n");
4327 TPACPI_ACPIHANDLE_INIT(hkey);
4329 tp_features.uwb = hkey_handle &&
4330 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4332 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4333 "uwb is %s, status 0x%02x\n",
4334 str_supported(tp_features.uwb),
4335 status);
4337 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4338 if (dbg_uwbemul) {
4339 tp_features.uwb = 1;
4340 printk(TPACPI_INFO
4341 "uwb switch emulation enabled\n");
4342 } else
4343 #endif
4344 if (tp_features.uwb &&
4345 !(status & TP_ACPI_UWB_HWPRESENT)) {
4346 /* no uwb hardware present in system */
4347 tp_features.uwb = 0;
4348 dbg_printk(TPACPI_DBG_INIT,
4349 "uwb hardware not installed\n");
4352 if (!tp_features.uwb)
4353 return 1;
4355 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4356 &tpacpi_uwb_rfkill,
4357 RFKILL_TYPE_UWB,
4358 TPACPI_RFK_UWB_SW_NAME,
4359 false,
4360 tpacpi_uwb_rfk_set,
4361 tpacpi_uwb_rfk_get);
4363 return res;
4366 static struct ibm_struct uwb_driver_data = {
4367 .name = "uwb",
4368 .exit = uwb_exit,
4369 .flags.experimental = 1,
4372 /*************************************************************************
4373 * Video subdriver
4376 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4378 enum video_access_mode {
4379 TPACPI_VIDEO_NONE = 0,
4380 TPACPI_VIDEO_570, /* 570 */
4381 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4382 TPACPI_VIDEO_NEW, /* all others */
4385 enum { /* video status flags, based on VIDEO_570 */
4386 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4387 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4388 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4391 enum { /* TPACPI_VIDEO_570 constants */
4392 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4393 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4394 * video_status_flags */
4395 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4396 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4399 static enum video_access_mode video_supported;
4400 static int video_orig_autosw;
4402 static int video_autosw_get(void);
4403 static int video_autosw_set(int enable);
4405 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4407 static int __init video_init(struct ibm_init_struct *iibm)
4409 int ivga;
4411 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4413 TPACPI_ACPIHANDLE_INIT(vid);
4414 TPACPI_ACPIHANDLE_INIT(vid2);
4416 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4417 /* G41, assume IVGA doesn't change */
4418 vid_handle = vid2_handle;
4420 if (!vid_handle)
4421 /* video switching not supported on R30, R31 */
4422 video_supported = TPACPI_VIDEO_NONE;
4423 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4424 /* 570 */
4425 video_supported = TPACPI_VIDEO_570;
4426 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4427 /* 600e/x, 770e, 770x */
4428 video_supported = TPACPI_VIDEO_770;
4429 else
4430 /* all others */
4431 video_supported = TPACPI_VIDEO_NEW;
4433 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4434 str_supported(video_supported != TPACPI_VIDEO_NONE),
4435 video_supported);
4437 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4440 static void video_exit(void)
4442 dbg_printk(TPACPI_DBG_EXIT,
4443 "restoring original video autoswitch mode\n");
4444 if (video_autosw_set(video_orig_autosw))
4445 printk(TPACPI_ERR "error while trying to restore original "
4446 "video autoswitch mode\n");
4449 static int video_outputsw_get(void)
4451 int status = 0;
4452 int i;
4454 switch (video_supported) {
4455 case TPACPI_VIDEO_570:
4456 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4457 TP_ACPI_VIDEO_570_PHSCMD))
4458 return -EIO;
4459 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4460 break;
4461 case TPACPI_VIDEO_770:
4462 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_LCD;
4466 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_CRT;
4470 break;
4471 case TPACPI_VIDEO_NEW:
4472 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4473 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
4478 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4479 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_LCD;
4483 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_DVI;
4487 break;
4488 default:
4489 return -ENOSYS;
4492 return status;
4495 static int video_outputsw_set(int status)
4497 int autosw;
4498 int res = 0;
4500 switch (video_supported) {
4501 case TPACPI_VIDEO_570:
4502 res = acpi_evalf(NULL, NULL,
4503 "\\_SB.PHS2", "vdd",
4504 TP_ACPI_VIDEO_570_PHS2CMD,
4505 status | TP_ACPI_VIDEO_570_PHS2SET);
4506 break;
4507 case TPACPI_VIDEO_770:
4508 autosw = video_autosw_get();
4509 if (autosw < 0)
4510 return autosw;
4512 res = video_autosw_set(1);
4513 if (res)
4514 return res;
4515 res = acpi_evalf(vid_handle, NULL,
4516 "ASWT", "vdd", status * 0x100, 0);
4517 if (!autosw && video_autosw_set(autosw)) {
4518 printk(TPACPI_ERR
4519 "video auto-switch left enabled due to error\n");
4520 return -EIO;
4522 break;
4523 case TPACPI_VIDEO_NEW:
4524 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4525 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4526 break;
4527 default:
4528 return -ENOSYS;
4531 return (res)? 0 : -EIO;
4534 static int video_autosw_get(void)
4536 int autosw = 0;
4538 switch (video_supported) {
4539 case TPACPI_VIDEO_570:
4540 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4541 return -EIO;
4542 break;
4543 case TPACPI_VIDEO_770:
4544 case TPACPI_VIDEO_NEW:
4545 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4546 return -EIO;
4547 break;
4548 default:
4549 return -ENOSYS;
4552 return autosw & 1;
4555 static int video_autosw_set(int enable)
4557 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4558 return -EIO;
4559 return 0;
4562 static int video_outputsw_cycle(void)
4564 int autosw = video_autosw_get();
4565 int res;
4567 if (autosw < 0)
4568 return autosw;
4570 switch (video_supported) {
4571 case TPACPI_VIDEO_570:
4572 res = video_autosw_set(1);
4573 if (res)
4574 return res;
4575 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4576 break;
4577 case TPACPI_VIDEO_770:
4578 case TPACPI_VIDEO_NEW:
4579 res = video_autosw_set(1);
4580 if (res)
4581 return res;
4582 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4583 break;
4584 default:
4585 return -ENOSYS;
4587 if (!autosw && video_autosw_set(autosw)) {
4588 printk(TPACPI_ERR
4589 "video auto-switch left enabled due to error\n");
4590 return -EIO;
4593 return (res)? 0 : -EIO;
4596 static int video_expand_toggle(void)
4598 switch (video_supported) {
4599 case TPACPI_VIDEO_570:
4600 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4601 0 : -EIO;
4602 case TPACPI_VIDEO_770:
4603 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4604 0 : -EIO;
4605 case TPACPI_VIDEO_NEW:
4606 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4607 0 : -EIO;
4608 default:
4609 return -ENOSYS;
4611 /* not reached */
4614 static int video_read(struct seq_file *m)
4616 int status, autosw;
4618 if (video_supported == TPACPI_VIDEO_NONE) {
4619 seq_printf(m, "status:\t\tnot supported\n");
4620 return 0;
4623 status = video_outputsw_get();
4624 if (status < 0)
4625 return status;
4627 autosw = video_autosw_get();
4628 if (autosw < 0)
4629 return autosw;
4631 seq_printf(m, "status:\t\tsupported\n");
4632 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4633 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4634 if (video_supported == TPACPI_VIDEO_NEW)
4635 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4636 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4637 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4638 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4639 if (video_supported == TPACPI_VIDEO_NEW)
4640 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4641 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4642 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4644 return 0;
4647 static int video_write(char *buf)
4649 char *cmd;
4650 int enable, disable, status;
4651 int res;
4653 if (video_supported == TPACPI_VIDEO_NONE)
4654 return -ENODEV;
4656 enable = 0;
4657 disable = 0;
4659 while ((cmd = next_cmd(&buf))) {
4660 if (strlencmp(cmd, "lcd_enable") == 0) {
4661 enable |= TP_ACPI_VIDEO_S_LCD;
4662 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4663 disable |= TP_ACPI_VIDEO_S_LCD;
4664 } else if (strlencmp(cmd, "crt_enable") == 0) {
4665 enable |= TP_ACPI_VIDEO_S_CRT;
4666 } else if (strlencmp(cmd, "crt_disable") == 0) {
4667 disable |= TP_ACPI_VIDEO_S_CRT;
4668 } else if (video_supported == TPACPI_VIDEO_NEW &&
4669 strlencmp(cmd, "dvi_enable") == 0) {
4670 enable |= TP_ACPI_VIDEO_S_DVI;
4671 } else if (video_supported == TPACPI_VIDEO_NEW &&
4672 strlencmp(cmd, "dvi_disable") == 0) {
4673 disable |= TP_ACPI_VIDEO_S_DVI;
4674 } else if (strlencmp(cmd, "auto_enable") == 0) {
4675 res = video_autosw_set(1);
4676 if (res)
4677 return res;
4678 } else if (strlencmp(cmd, "auto_disable") == 0) {
4679 res = video_autosw_set(0);
4680 if (res)
4681 return res;
4682 } else if (strlencmp(cmd, "video_switch") == 0) {
4683 res = video_outputsw_cycle();
4684 if (res)
4685 return res;
4686 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4687 res = video_expand_toggle();
4688 if (res)
4689 return res;
4690 } else
4691 return -EINVAL;
4694 if (enable || disable) {
4695 status = video_outputsw_get();
4696 if (status < 0)
4697 return status;
4698 res = video_outputsw_set((status & ~disable) | enable);
4699 if (res)
4700 return res;
4703 return 0;
4706 static struct ibm_struct video_driver_data = {
4707 .name = "video",
4708 .read = video_read,
4709 .write = video_write,
4710 .exit = video_exit,
4713 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4715 /*************************************************************************
4716 * Light (thinklight) subdriver
4719 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4720 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4722 static int light_get_status(void)
4724 int status = 0;
4726 if (tp_features.light_status) {
4727 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4728 return -EIO;
4729 return (!!status);
4732 return -ENXIO;
4735 static int light_set_status(int status)
4737 int rc;
4739 if (tp_features.light) {
4740 if (cmos_handle) {
4741 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4742 (status)?
4743 TP_CMOS_THINKLIGHT_ON :
4744 TP_CMOS_THINKLIGHT_OFF);
4745 } else {
4746 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4747 (status)? 1 : 0);
4749 return (rc)? 0 : -EIO;
4752 return -ENXIO;
4755 static void light_set_status_worker(struct work_struct *work)
4757 struct tpacpi_led_classdev *data =
4758 container_of(work, struct tpacpi_led_classdev, work);
4760 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4761 light_set_status((data->new_state != TPACPI_LED_OFF));
4764 static void light_sysfs_set(struct led_classdev *led_cdev,
4765 enum led_brightness brightness)
4767 struct tpacpi_led_classdev *data =
4768 container_of(led_cdev,
4769 struct tpacpi_led_classdev,
4770 led_classdev);
4771 data->new_state = (brightness != LED_OFF) ?
4772 TPACPI_LED_ON : TPACPI_LED_OFF;
4773 queue_work(tpacpi_wq, &data->work);
4776 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4778 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4781 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4782 .led_classdev = {
4783 .name = "tpacpi::thinklight",
4784 .brightness_set = &light_sysfs_set,
4785 .brightness_get = &light_sysfs_get,
4789 static int __init light_init(struct ibm_init_struct *iibm)
4791 int rc;
4793 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4795 TPACPI_ACPIHANDLE_INIT(ledb);
4796 TPACPI_ACPIHANDLE_INIT(lght);
4797 TPACPI_ACPIHANDLE_INIT(cmos);
4798 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4800 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4801 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4803 if (tp_features.light)
4804 /* light status not supported on
4805 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4806 tp_features.light_status =
4807 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4809 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4810 str_supported(tp_features.light),
4811 str_supported(tp_features.light_status));
4813 if (!tp_features.light)
4814 return 1;
4816 rc = led_classdev_register(&tpacpi_pdev->dev,
4817 &tpacpi_led_thinklight.led_classdev);
4819 if (rc < 0) {
4820 tp_features.light = 0;
4821 tp_features.light_status = 0;
4822 } else {
4823 rc = 0;
4826 return rc;
4829 static void light_exit(void)
4831 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4832 if (work_pending(&tpacpi_led_thinklight.work))
4833 flush_workqueue(tpacpi_wq);
4836 static int light_read(struct seq_file *m)
4838 int status;
4840 if (!tp_features.light) {
4841 seq_printf(m, "status:\t\tnot supported\n");
4842 } else if (!tp_features.light_status) {
4843 seq_printf(m, "status:\t\tunknown\n");
4844 seq_printf(m, "commands:\ton, off\n");
4845 } else {
4846 status = light_get_status();
4847 if (status < 0)
4848 return status;
4849 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4850 seq_printf(m, "commands:\ton, off\n");
4853 return 0;
4856 static int light_write(char *buf)
4858 char *cmd;
4859 int newstatus = 0;
4861 if (!tp_features.light)
4862 return -ENODEV;
4864 while ((cmd = next_cmd(&buf))) {
4865 if (strlencmp(cmd, "on") == 0) {
4866 newstatus = 1;
4867 } else if (strlencmp(cmd, "off") == 0) {
4868 newstatus = 0;
4869 } else
4870 return -EINVAL;
4873 return light_set_status(newstatus);
4876 static struct ibm_struct light_driver_data = {
4877 .name = "light",
4878 .read = light_read,
4879 .write = light_write,
4880 .exit = light_exit,
4883 /*************************************************************************
4884 * CMOS subdriver
4887 /* sysfs cmos_command -------------------------------------------------- */
4888 static ssize_t cmos_command_store(struct device *dev,
4889 struct device_attribute *attr,
4890 const char *buf, size_t count)
4892 unsigned long cmos_cmd;
4893 int res;
4895 if (parse_strtoul(buf, 21, &cmos_cmd))
4896 return -EINVAL;
4898 res = issue_thinkpad_cmos_command(cmos_cmd);
4899 return (res)? res : count;
4902 static struct device_attribute dev_attr_cmos_command =
4903 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4905 /* --------------------------------------------------------------------- */
4907 static int __init cmos_init(struct ibm_init_struct *iibm)
4909 int res;
4911 vdbg_printk(TPACPI_DBG_INIT,
4912 "initializing cmos commands subdriver\n");
4914 TPACPI_ACPIHANDLE_INIT(cmos);
4916 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4917 str_supported(cmos_handle != NULL));
4919 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4920 if (res)
4921 return res;
4923 return (cmos_handle)? 0 : 1;
4926 static void cmos_exit(void)
4928 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4931 static int cmos_read(struct seq_file *m)
4933 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4934 R30, R31, T20-22, X20-21 */
4935 if (!cmos_handle)
4936 seq_printf(m, "status:\t\tnot supported\n");
4937 else {
4938 seq_printf(m, "status:\t\tsupported\n");
4939 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4942 return 0;
4945 static int cmos_write(char *buf)
4947 char *cmd;
4948 int cmos_cmd, res;
4950 while ((cmd = next_cmd(&buf))) {
4951 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4952 cmos_cmd >= 0 && cmos_cmd <= 21) {
4953 /* cmos_cmd set */
4954 } else
4955 return -EINVAL;
4957 res = issue_thinkpad_cmos_command(cmos_cmd);
4958 if (res)
4959 return res;
4962 return 0;
4965 static struct ibm_struct cmos_driver_data = {
4966 .name = "cmos",
4967 .read = cmos_read,
4968 .write = cmos_write,
4969 .exit = cmos_exit,
4972 /*************************************************************************
4973 * LED subdriver
4976 enum led_access_mode {
4977 TPACPI_LED_NONE = 0,
4978 TPACPI_LED_570, /* 570 */
4979 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4980 TPACPI_LED_NEW, /* all others */
4983 enum { /* For TPACPI_LED_OLD */
4984 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4985 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4986 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4989 static enum led_access_mode led_supported;
4991 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4992 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4993 /* T20-22, X20-21 */
4994 "LED", /* all others */
4995 ); /* R30, R31 */
4997 #define TPACPI_LED_NUMLEDS 16
4998 static struct tpacpi_led_classdev *tpacpi_leds;
4999 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5000 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5001 /* there's a limit of 19 chars + NULL before 2.6.26 */
5002 "tpacpi::power",
5003 "tpacpi:orange:batt",
5004 "tpacpi:green:batt",
5005 "tpacpi::dock_active",
5006 "tpacpi::bay_active",
5007 "tpacpi::dock_batt",
5008 "tpacpi::unknown_led",
5009 "tpacpi::standby",
5010 "tpacpi::dock_status1",
5011 "tpacpi::dock_status2",
5012 "tpacpi::unknown_led2",
5013 "tpacpi::unknown_led3",
5014 "tpacpi::thinkvantage",
5016 #define TPACPI_SAFE_LEDS 0x1081U
5018 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5020 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5021 return false;
5022 #else
5023 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5024 #endif
5027 static int led_get_status(const unsigned int led)
5029 int status;
5030 enum led_status_t led_s;
5032 switch (led_supported) {
5033 case TPACPI_LED_570:
5034 if (!acpi_evalf(ec_handle,
5035 &status, "GLED", "dd", 1 << led))
5036 return -EIO;
5037 led_s = (status == 0)?
5038 TPACPI_LED_OFF :
5039 ((status == 1)?
5040 TPACPI_LED_ON :
5041 TPACPI_LED_BLINK);
5042 tpacpi_led_state_cache[led] = led_s;
5043 return led_s;
5044 default:
5045 return -ENXIO;
5048 /* not reached */
5051 static int led_set_status(const unsigned int led,
5052 const enum led_status_t ledstatus)
5054 /* off, on, blink. Index is led_status_t */
5055 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5056 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5058 int rc = 0;
5060 switch (led_supported) {
5061 case TPACPI_LED_570:
5062 /* 570 */
5063 if (unlikely(led > 7))
5064 return -EINVAL;
5065 if (unlikely(tpacpi_is_led_restricted(led)))
5066 return -EPERM;
5067 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5068 (1 << led), led_sled_arg1[ledstatus]))
5069 rc = -EIO;
5070 break;
5071 case TPACPI_LED_OLD:
5072 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5073 if (unlikely(led > 7))
5074 return -EINVAL;
5075 if (unlikely(tpacpi_is_led_restricted(led)))
5076 return -EPERM;
5077 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5078 if (rc >= 0)
5079 rc = ec_write(TPACPI_LED_EC_HLBL,
5080 (ledstatus == TPACPI_LED_BLINK) << led);
5081 if (rc >= 0)
5082 rc = ec_write(TPACPI_LED_EC_HLCL,
5083 (ledstatus != TPACPI_LED_OFF) << led);
5084 break;
5085 case TPACPI_LED_NEW:
5086 /* all others */
5087 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5088 return -EINVAL;
5089 if (unlikely(tpacpi_is_led_restricted(led)))
5090 return -EPERM;
5091 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5092 led, led_led_arg1[ledstatus]))
5093 rc = -EIO;
5094 break;
5095 default:
5096 rc = -ENXIO;
5099 if (!rc)
5100 tpacpi_led_state_cache[led] = ledstatus;
5102 return rc;
5105 static void led_set_status_worker(struct work_struct *work)
5107 struct tpacpi_led_classdev *data =
5108 container_of(work, struct tpacpi_led_classdev, work);
5110 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5111 led_set_status(data->led, data->new_state);
5114 static void led_sysfs_set(struct led_classdev *led_cdev,
5115 enum led_brightness brightness)
5117 struct tpacpi_led_classdev *data = container_of(led_cdev,
5118 struct tpacpi_led_classdev, led_classdev);
5120 if (brightness == LED_OFF)
5121 data->new_state = TPACPI_LED_OFF;
5122 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5123 data->new_state = TPACPI_LED_ON;
5124 else
5125 data->new_state = TPACPI_LED_BLINK;
5127 queue_work(tpacpi_wq, &data->work);
5130 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5131 unsigned long *delay_on, unsigned long *delay_off)
5133 struct tpacpi_led_classdev *data = container_of(led_cdev,
5134 struct tpacpi_led_classdev, led_classdev);
5136 /* Can we choose the flash rate? */
5137 if (*delay_on == 0 && *delay_off == 0) {
5138 /* yes. set them to the hardware blink rate (1 Hz) */
5139 *delay_on = 500; /* ms */
5140 *delay_off = 500; /* ms */
5141 } else if ((*delay_on != 500) || (*delay_off != 500))
5142 return -EINVAL;
5144 data->new_state = TPACPI_LED_BLINK;
5145 queue_work(tpacpi_wq, &data->work);
5147 return 0;
5150 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5152 int rc;
5154 struct tpacpi_led_classdev *data = container_of(led_cdev,
5155 struct tpacpi_led_classdev, led_classdev);
5157 rc = led_get_status(data->led);
5159 if (rc == TPACPI_LED_OFF || rc < 0)
5160 rc = LED_OFF; /* no error handling in led class :( */
5161 else
5162 rc = LED_FULL;
5164 return rc;
5167 static void led_exit(void)
5169 unsigned int i;
5171 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5172 if (tpacpi_leds[i].led_classdev.name)
5173 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5176 kfree(tpacpi_leds);
5179 static int __init tpacpi_init_led(unsigned int led)
5181 int rc;
5183 tpacpi_leds[led].led = led;
5185 /* LEDs with no name don't get registered */
5186 if (!tpacpi_led_names[led])
5187 return 0;
5189 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5190 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5191 if (led_supported == TPACPI_LED_570)
5192 tpacpi_leds[led].led_classdev.brightness_get =
5193 &led_sysfs_get;
5195 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5197 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5199 rc = led_classdev_register(&tpacpi_pdev->dev,
5200 &tpacpi_leds[led].led_classdev);
5201 if (rc < 0)
5202 tpacpi_leds[led].led_classdev.name = NULL;
5204 return rc;
5207 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5208 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5209 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5210 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5212 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5213 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5214 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5215 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5216 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5217 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5218 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5219 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5221 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5222 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5223 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5224 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5225 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5227 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5228 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5229 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5230 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5232 /* (1) - may have excess leds enabled on MSB */
5234 /* Defaults (order matters, keep last, don't reorder!) */
5235 { /* Lenovo */
5236 .vendor = PCI_VENDOR_ID_LENOVO,
5237 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5238 .quirks = 0x1fffU,
5240 { /* IBM ThinkPads with no EC version string */
5241 .vendor = PCI_VENDOR_ID_IBM,
5242 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5243 .quirks = 0x00ffU,
5245 { /* IBM ThinkPads with EC version string */
5246 .vendor = PCI_VENDOR_ID_IBM,
5247 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5248 .quirks = 0x00bfU,
5252 #undef TPACPI_LEDQ_IBM
5253 #undef TPACPI_LEDQ_LNV
5255 static int __init led_init(struct ibm_init_struct *iibm)
5257 unsigned int i;
5258 int rc;
5259 unsigned long useful_leds;
5261 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5263 TPACPI_ACPIHANDLE_INIT(led);
5265 if (!led_handle)
5266 /* led not supported on R30, R31 */
5267 led_supported = TPACPI_LED_NONE;
5268 else if (strlencmp(led_path, "SLED") == 0)
5269 /* 570 */
5270 led_supported = TPACPI_LED_570;
5271 else if (strlencmp(led_path, "SYSL") == 0)
5272 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5273 led_supported = TPACPI_LED_OLD;
5274 else
5275 /* all others */
5276 led_supported = TPACPI_LED_NEW;
5278 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5279 str_supported(led_supported), led_supported);
5281 if (led_supported == TPACPI_LED_NONE)
5282 return 1;
5284 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5285 GFP_KERNEL);
5286 if (!tpacpi_leds) {
5287 printk(TPACPI_ERR "Out of memory for LED data\n");
5288 return -ENOMEM;
5291 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5292 ARRAY_SIZE(led_useful_qtable));
5294 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5295 if (!tpacpi_is_led_restricted(i) &&
5296 test_bit(i, &useful_leds)) {
5297 rc = tpacpi_init_led(i);
5298 if (rc < 0) {
5299 led_exit();
5300 return rc;
5305 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5306 printk(TPACPI_NOTICE
5307 "warning: userspace override of important "
5308 "firmware LEDs is enabled\n");
5309 #endif
5310 return 0;
5313 #define str_led_status(s) \
5314 ((s) == TPACPI_LED_OFF ? "off" : \
5315 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5317 static int led_read(struct seq_file *m)
5319 if (!led_supported) {
5320 seq_printf(m, "status:\t\tnot supported\n");
5321 return 0;
5323 seq_printf(m, "status:\t\tsupported\n");
5325 if (led_supported == TPACPI_LED_570) {
5326 /* 570 */
5327 int i, status;
5328 for (i = 0; i < 8; i++) {
5329 status = led_get_status(i);
5330 if (status < 0)
5331 return -EIO;
5332 seq_printf(m, "%d:\t\t%s\n",
5333 i, str_led_status(status));
5337 seq_printf(m, "commands:\t"
5338 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5340 return 0;
5343 static int led_write(char *buf)
5345 char *cmd;
5346 int led, rc;
5347 enum led_status_t s;
5349 if (!led_supported)
5350 return -ENODEV;
5352 while ((cmd = next_cmd(&buf))) {
5353 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5354 return -EINVAL;
5356 if (strstr(cmd, "off")) {
5357 s = TPACPI_LED_OFF;
5358 } else if (strstr(cmd, "on")) {
5359 s = TPACPI_LED_ON;
5360 } else if (strstr(cmd, "blink")) {
5361 s = TPACPI_LED_BLINK;
5362 } else {
5363 return -EINVAL;
5366 rc = led_set_status(led, s);
5367 if (rc < 0)
5368 return rc;
5371 return 0;
5374 static struct ibm_struct led_driver_data = {
5375 .name = "led",
5376 .read = led_read,
5377 .write = led_write,
5378 .exit = led_exit,
5381 /*************************************************************************
5382 * Beep subdriver
5385 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5387 #define TPACPI_BEEP_Q1 0x0001
5389 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5390 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5391 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5394 static int __init beep_init(struct ibm_init_struct *iibm)
5396 unsigned long quirks;
5398 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5400 TPACPI_ACPIHANDLE_INIT(beep);
5402 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5403 str_supported(beep_handle != NULL));
5405 quirks = tpacpi_check_quirks(beep_quirk_table,
5406 ARRAY_SIZE(beep_quirk_table));
5408 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5410 return (beep_handle)? 0 : 1;
5413 static int beep_read(struct seq_file *m)
5415 if (!beep_handle)
5416 seq_printf(m, "status:\t\tnot supported\n");
5417 else {
5418 seq_printf(m, "status:\t\tsupported\n");
5419 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5422 return 0;
5425 static int beep_write(char *buf)
5427 char *cmd;
5428 int beep_cmd;
5430 if (!beep_handle)
5431 return -ENODEV;
5433 while ((cmd = next_cmd(&buf))) {
5434 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5435 beep_cmd >= 0 && beep_cmd <= 17) {
5436 /* beep_cmd set */
5437 } else
5438 return -EINVAL;
5439 if (tp_features.beep_needs_two_args) {
5440 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5441 beep_cmd, 0))
5442 return -EIO;
5443 } else {
5444 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5445 beep_cmd))
5446 return -EIO;
5450 return 0;
5453 static struct ibm_struct beep_driver_data = {
5454 .name = "beep",
5455 .read = beep_read,
5456 .write = beep_write,
5459 /*************************************************************************
5460 * Thermal subdriver
5463 enum thermal_access_mode {
5464 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5465 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5466 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5467 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5468 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5471 enum { /* TPACPI_THERMAL_TPEC_* */
5472 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5473 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5474 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5476 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5480 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5481 struct ibm_thermal_sensors_struct {
5482 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5485 static enum thermal_access_mode thermal_read_mode;
5487 /* idx is zero-based */
5488 static int thermal_get_sensor(int idx, s32 *value)
5490 int t;
5491 s8 tmp;
5492 char tmpi[5];
5494 t = TP_EC_THERMAL_TMP0;
5496 switch (thermal_read_mode) {
5497 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5498 case TPACPI_THERMAL_TPEC_16:
5499 if (idx >= 8 && idx <= 15) {
5500 t = TP_EC_THERMAL_TMP8;
5501 idx -= 8;
5503 /* fallthrough */
5504 #endif
5505 case TPACPI_THERMAL_TPEC_8:
5506 if (idx <= 7) {
5507 if (!acpi_ec_read(t + idx, &tmp))
5508 return -EIO;
5509 *value = tmp * 1000;
5510 return 0;
5512 break;
5514 case TPACPI_THERMAL_ACPI_UPDT:
5515 if (idx <= 7) {
5516 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5517 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5518 return -EIO;
5519 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5520 return -EIO;
5521 *value = (t - 2732) * 100;
5522 return 0;
5524 break;
5526 case TPACPI_THERMAL_ACPI_TMP07:
5527 if (idx <= 7) {
5528 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5529 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5530 return -EIO;
5531 if (t > 127 || t < -127)
5532 t = TP_EC_THERMAL_TMP_NA;
5533 *value = t * 1000;
5534 return 0;
5536 break;
5538 case TPACPI_THERMAL_NONE:
5539 default:
5540 return -ENOSYS;
5543 return -EINVAL;
5546 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5548 int res, i;
5549 int n;
5551 n = 8;
5552 i = 0;
5554 if (!s)
5555 return -EINVAL;
5557 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5558 n = 16;
5560 for (i = 0 ; i < n; i++) {
5561 res = thermal_get_sensor(i, &s->temp[i]);
5562 if (res)
5563 return res;
5566 return n;
5569 static void thermal_dump_all_sensors(void)
5571 int n, i;
5572 struct ibm_thermal_sensors_struct t;
5574 n = thermal_get_sensors(&t);
5575 if (n <= 0)
5576 return;
5578 printk(TPACPI_NOTICE
5579 "temperatures (Celsius):");
5581 for (i = 0; i < n; i++) {
5582 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5583 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5584 else
5585 printk(KERN_CONT " N/A");
5588 printk(KERN_CONT "\n");
5591 /* sysfs temp##_input -------------------------------------------------- */
5593 static ssize_t thermal_temp_input_show(struct device *dev,
5594 struct device_attribute *attr,
5595 char *buf)
5597 struct sensor_device_attribute *sensor_attr =
5598 to_sensor_dev_attr(attr);
5599 int idx = sensor_attr->index;
5600 s32 value;
5601 int res;
5603 res = thermal_get_sensor(idx, &value);
5604 if (res)
5605 return res;
5606 if (value == TPACPI_THERMAL_SENSOR_NA)
5607 return -ENXIO;
5609 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5612 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5613 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5614 thermal_temp_input_show, NULL, _idxB)
5616 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5617 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5618 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5619 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5620 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5621 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5622 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5623 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5624 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5625 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5626 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5627 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5628 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5629 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5630 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5631 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5632 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5635 #define THERMAL_ATTRS(X) \
5636 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5638 static struct attribute *thermal_temp_input_attr[] = {
5639 THERMAL_ATTRS(8),
5640 THERMAL_ATTRS(9),
5641 THERMAL_ATTRS(10),
5642 THERMAL_ATTRS(11),
5643 THERMAL_ATTRS(12),
5644 THERMAL_ATTRS(13),
5645 THERMAL_ATTRS(14),
5646 THERMAL_ATTRS(15),
5647 THERMAL_ATTRS(0),
5648 THERMAL_ATTRS(1),
5649 THERMAL_ATTRS(2),
5650 THERMAL_ATTRS(3),
5651 THERMAL_ATTRS(4),
5652 THERMAL_ATTRS(5),
5653 THERMAL_ATTRS(6),
5654 THERMAL_ATTRS(7),
5655 NULL
5658 static const struct attribute_group thermal_temp_input16_group = {
5659 .attrs = thermal_temp_input_attr
5662 static const struct attribute_group thermal_temp_input8_group = {
5663 .attrs = &thermal_temp_input_attr[8]
5666 #undef THERMAL_SENSOR_ATTR_TEMP
5667 #undef THERMAL_ATTRS
5669 /* --------------------------------------------------------------------- */
5671 static int __init thermal_init(struct ibm_init_struct *iibm)
5673 u8 t, ta1, ta2;
5674 int i;
5675 int acpi_tmp7;
5676 int res;
5678 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5680 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5682 if (thinkpad_id.ec_model) {
5684 * Direct EC access mode: sensors at registers
5685 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5686 * non-implemented, thermal sensors return 0x80 when
5687 * not available
5690 ta1 = ta2 = 0;
5691 for (i = 0; i < 8; i++) {
5692 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5693 ta1 |= t;
5694 } else {
5695 ta1 = 0;
5696 break;
5698 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5699 ta2 |= t;
5700 } else {
5701 ta1 = 0;
5702 break;
5705 if (ta1 == 0) {
5706 /* This is sheer paranoia, but we handle it anyway */
5707 if (acpi_tmp7) {
5708 printk(TPACPI_ERR
5709 "ThinkPad ACPI EC access misbehaving, "
5710 "falling back to ACPI TMPx access "
5711 "mode\n");
5712 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5713 } else {
5714 printk(TPACPI_ERR
5715 "ThinkPad ACPI EC access misbehaving, "
5716 "disabling thermal sensors access\n");
5717 thermal_read_mode = TPACPI_THERMAL_NONE;
5719 } else {
5720 thermal_read_mode =
5721 (ta2 != 0) ?
5722 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5724 } else if (acpi_tmp7) {
5725 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5726 /* 600e/x, 770e, 770x */
5727 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5728 } else {
5729 /* Standard ACPI TMPx access, max 8 sensors */
5730 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5732 } else {
5733 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5734 thermal_read_mode = TPACPI_THERMAL_NONE;
5737 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5738 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5739 thermal_read_mode);
5741 switch (thermal_read_mode) {
5742 case TPACPI_THERMAL_TPEC_16:
5743 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5744 &thermal_temp_input16_group);
5745 if (res)
5746 return res;
5747 break;
5748 case TPACPI_THERMAL_TPEC_8:
5749 case TPACPI_THERMAL_ACPI_TMP07:
5750 case TPACPI_THERMAL_ACPI_UPDT:
5751 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5752 &thermal_temp_input8_group);
5753 if (res)
5754 return res;
5755 break;
5756 case TPACPI_THERMAL_NONE:
5757 default:
5758 return 1;
5761 return 0;
5764 static void thermal_exit(void)
5766 switch (thermal_read_mode) {
5767 case TPACPI_THERMAL_TPEC_16:
5768 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5769 &thermal_temp_input16_group);
5770 break;
5771 case TPACPI_THERMAL_TPEC_8:
5772 case TPACPI_THERMAL_ACPI_TMP07:
5773 case TPACPI_THERMAL_ACPI_UPDT:
5774 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5775 &thermal_temp_input16_group);
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 break;
5783 static int thermal_read(struct seq_file *m)
5785 int n, i;
5786 struct ibm_thermal_sensors_struct t;
5788 n = thermal_get_sensors(&t);
5789 if (unlikely(n < 0))
5790 return n;
5792 seq_printf(m, "temperatures:\t");
5794 if (n > 0) {
5795 for (i = 0; i < (n - 1); i++)
5796 seq_printf(m, "%d ", t.temp[i] / 1000);
5797 seq_printf(m, "%d\n", t.temp[i] / 1000);
5798 } else
5799 seq_printf(m, "not supported\n");
5801 return 0;
5804 static struct ibm_struct thermal_driver_data = {
5805 .name = "thermal",
5806 .read = thermal_read,
5807 .exit = thermal_exit,
5810 /*************************************************************************
5811 * EC Dump subdriver
5814 static u8 ecdump_regs[256];
5816 static int ecdump_read(struct seq_file *m)
5818 int i, j;
5819 u8 v;
5821 seq_printf(m, "EC "
5822 " +00 +01 +02 +03 +04 +05 +06 +07"
5823 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5824 for (i = 0; i < 256; i += 16) {
5825 seq_printf(m, "EC 0x%02x:", i);
5826 for (j = 0; j < 16; j++) {
5827 if (!acpi_ec_read(i + j, &v))
5828 break;
5829 if (v != ecdump_regs[i + j])
5830 seq_printf(m, " *%02x", v);
5831 else
5832 seq_printf(m, " %02x", v);
5833 ecdump_regs[i + j] = v;
5835 seq_putc(m, '\n');
5836 if (j != 16)
5837 break;
5840 /* These are way too dangerous to advertise openly... */
5841 #if 0
5842 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5843 " (<offset> is 00-ff, <value> is 00-ff)\n");
5844 seq_printf(m, "commands:\t0x<offset> <value> "
5845 " (<offset> is 00-ff, <value> is 0-255)\n");
5846 #endif
5847 return 0;
5850 static int ecdump_write(char *buf)
5852 char *cmd;
5853 int i, v;
5855 while ((cmd = next_cmd(&buf))) {
5856 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5857 /* i and v set */
5858 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5859 /* i and v set */
5860 } else
5861 return -EINVAL;
5862 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5863 if (!acpi_ec_write(i, v))
5864 return -EIO;
5865 } else
5866 return -EINVAL;
5869 return 0;
5872 static struct ibm_struct ecdump_driver_data = {
5873 .name = "ecdump",
5874 .read = ecdump_read,
5875 .write = ecdump_write,
5876 .flags.experimental = 1,
5879 /*************************************************************************
5880 * Backlight/brightness subdriver
5883 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5886 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5887 * CMOS NVRAM byte 0x5E, bits 0-3.
5889 * EC HBRV (0x31) has the following layout
5890 * Bit 7: unknown function
5891 * Bit 6: unknown function
5892 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5893 * Bit 4: must be set to zero to avoid problems
5894 * Bit 3-0: backlight brightness level
5896 * brightness_get_raw returns status data in the HBRV layout
5898 * WARNING: The X61 has been verified to use HBRV for something else, so
5899 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5900 * testing on the very early *60 Lenovo models...
5903 enum {
5904 TP_EC_BACKLIGHT = 0x31,
5906 /* TP_EC_BACKLIGHT bitmasks */
5907 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5908 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5909 TP_EC_BACKLIGHT_MAPSW = 0x20,
5912 enum tpacpi_brightness_access_mode {
5913 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5914 TPACPI_BRGHT_MODE_EC, /* EC control */
5915 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5916 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5917 TPACPI_BRGHT_MODE_MAX
5920 static struct backlight_device *ibm_backlight_device;
5922 static enum tpacpi_brightness_access_mode brightness_mode =
5923 TPACPI_BRGHT_MODE_MAX;
5925 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5927 static struct mutex brightness_mutex;
5929 /* NVRAM brightness access,
5930 * call with brightness_mutex held! */
5931 static unsigned int tpacpi_brightness_nvram_get(void)
5933 u8 lnvram;
5935 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5936 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5937 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5938 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5940 return lnvram;
5943 static void tpacpi_brightness_checkpoint_nvram(void)
5945 u8 lec = 0;
5946 u8 b_nvram;
5948 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5949 return;
5951 vdbg_printk(TPACPI_DBG_BRGHT,
5952 "trying to checkpoint backlight level to NVRAM...\n");
5954 if (mutex_lock_killable(&brightness_mutex) < 0)
5955 return;
5957 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5958 goto unlock;
5959 lec &= TP_EC_BACKLIGHT_LVLMSK;
5960 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5962 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5963 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5964 /* NVRAM needs update */
5965 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5966 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5967 b_nvram |= lec;
5968 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5969 dbg_printk(TPACPI_DBG_BRGHT,
5970 "updated NVRAM backlight level to %u (0x%02x)\n",
5971 (unsigned int) lec, (unsigned int) b_nvram);
5972 } else
5973 vdbg_printk(TPACPI_DBG_BRGHT,
5974 "NVRAM backlight level already is %u (0x%02x)\n",
5975 (unsigned int) lec, (unsigned int) b_nvram);
5977 unlock:
5978 mutex_unlock(&brightness_mutex);
5982 /* call with brightness_mutex held! */
5983 static int tpacpi_brightness_get_raw(int *status)
5985 u8 lec = 0;
5987 switch (brightness_mode) {
5988 case TPACPI_BRGHT_MODE_UCMS_STEP:
5989 *status = tpacpi_brightness_nvram_get();
5990 return 0;
5991 case TPACPI_BRGHT_MODE_EC:
5992 case TPACPI_BRGHT_MODE_ECNVRAM:
5993 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5994 return -EIO;
5995 *status = lec;
5996 return 0;
5997 default:
5998 return -ENXIO;
6002 /* call with brightness_mutex held! */
6003 /* do NOT call with illegal backlight level value */
6004 static int tpacpi_brightness_set_ec(unsigned int value)
6006 u8 lec = 0;
6008 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6009 return -EIO;
6011 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6012 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6013 (value & TP_EC_BACKLIGHT_LVLMSK))))
6014 return -EIO;
6016 return 0;
6019 /* call with brightness_mutex held! */
6020 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6022 int cmos_cmd, inc;
6023 unsigned int current_value, i;
6025 current_value = tpacpi_brightness_nvram_get();
6027 if (value == current_value)
6028 return 0;
6030 cmos_cmd = (value > current_value) ?
6031 TP_CMOS_BRIGHTNESS_UP :
6032 TP_CMOS_BRIGHTNESS_DOWN;
6033 inc = (value > current_value) ? 1 : -1;
6035 for (i = current_value; i != value; i += inc)
6036 if (issue_thinkpad_cmos_command(cmos_cmd))
6037 return -EIO;
6039 return 0;
6042 /* May return EINTR which can always be mapped to ERESTARTSYS */
6043 static int brightness_set(unsigned int value)
6045 int res;
6047 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6048 value < 0)
6049 return -EINVAL;
6051 vdbg_printk(TPACPI_DBG_BRGHT,
6052 "set backlight level to %d\n", value);
6054 res = mutex_lock_killable(&brightness_mutex);
6055 if (res < 0)
6056 return res;
6058 switch (brightness_mode) {
6059 case TPACPI_BRGHT_MODE_EC:
6060 case TPACPI_BRGHT_MODE_ECNVRAM:
6061 res = tpacpi_brightness_set_ec(value);
6062 break;
6063 case TPACPI_BRGHT_MODE_UCMS_STEP:
6064 res = tpacpi_brightness_set_ucmsstep(value);
6065 break;
6066 default:
6067 res = -ENXIO;
6070 mutex_unlock(&brightness_mutex);
6071 return res;
6074 /* sysfs backlight class ----------------------------------------------- */
6076 static int brightness_update_status(struct backlight_device *bd)
6078 unsigned int level =
6079 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6080 bd->props.power == FB_BLANK_UNBLANK) ?
6081 bd->props.brightness : 0;
6083 dbg_printk(TPACPI_DBG_BRGHT,
6084 "backlight: attempt to set level to %d\n",
6085 level);
6087 /* it is the backlight class's job (caller) to handle
6088 * EINTR and other errors properly */
6089 return brightness_set(level);
6092 static int brightness_get(struct backlight_device *bd)
6094 int status, res;
6096 res = mutex_lock_killable(&brightness_mutex);
6097 if (res < 0)
6098 return 0;
6100 res = tpacpi_brightness_get_raw(&status);
6102 mutex_unlock(&brightness_mutex);
6104 if (res < 0)
6105 return 0;
6107 return status & TP_EC_BACKLIGHT_LVLMSK;
6110 static void tpacpi_brightness_notify_change(void)
6112 backlight_force_update(ibm_backlight_device,
6113 BACKLIGHT_UPDATE_HOTKEY);
6116 static struct backlight_ops ibm_backlight_data = {
6117 .get_brightness = brightness_get,
6118 .update_status = brightness_update_status,
6121 /* --------------------------------------------------------------------- */
6124 * These are only useful for models that have only one possibility
6125 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6126 * these quirks.
6128 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6129 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6130 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6132 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6133 /* Models with ATI GPUs known to require ECNVRAM mode */
6134 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6136 /* Models with ATI GPUs that can use ECNVRAM */
6137 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6138 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6139 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6140 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 /* Models with Intel Extreme Graphics 2 */
6143 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6144 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6145 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6147 /* Models with Intel GMA900 */
6148 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6149 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6150 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6153 static int __init brightness_init(struct ibm_init_struct *iibm)
6155 int b;
6156 unsigned long quirks;
6158 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6160 mutex_init(&brightness_mutex);
6162 quirks = tpacpi_check_quirks(brightness_quirk_table,
6163 ARRAY_SIZE(brightness_quirk_table));
6166 * We always attempt to detect acpi support, so as to switch
6167 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6168 * going to publish a backlight interface
6170 b = tpacpi_check_std_acpi_brightness_support();
6171 if (b > 0) {
6173 if (acpi_video_backlight_support()) {
6174 if (brightness_enable > 1) {
6175 printk(TPACPI_NOTICE
6176 "Standard ACPI backlight interface "
6177 "available, not loading native one.\n");
6178 return 1;
6179 } else if (brightness_enable == 1) {
6180 printk(TPACPI_NOTICE
6181 "Backlight control force enabled, even if standard "
6182 "ACPI backlight interface is available\n");
6184 } else {
6185 if (brightness_enable > 1) {
6186 printk(TPACPI_NOTICE
6187 "Standard ACPI backlight interface not "
6188 "available, thinkpad_acpi native "
6189 "brightness control enabled\n");
6194 if (!brightness_enable) {
6195 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6196 "brightness support disabled by "
6197 "module parameter\n");
6198 return 1;
6201 if (b > 16) {
6202 printk(TPACPI_ERR
6203 "Unsupported brightness interface, "
6204 "please contact %s\n", TPACPI_MAIL);
6205 return 1;
6207 if (b == 16)
6208 tp_features.bright_16levels = 1;
6211 * Check for module parameter bogosity, note that we
6212 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6213 * able to detect "unspecified"
6215 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6216 return -EINVAL;
6218 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6219 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6220 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6221 if (quirks & TPACPI_BRGHT_Q_EC)
6222 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6223 else
6224 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6226 dbg_printk(TPACPI_DBG_BRGHT,
6227 "driver auto-selected brightness_mode=%d\n",
6228 brightness_mode);
6231 /* Safety */
6232 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6233 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6234 brightness_mode == TPACPI_BRGHT_MODE_EC))
6235 return -EINVAL;
6237 if (tpacpi_brightness_get_raw(&b) < 0)
6238 return 1;
6240 if (tp_features.bright_16levels)
6241 printk(TPACPI_INFO
6242 "detected a 16-level brightness capable ThinkPad\n");
6244 ibm_backlight_device = backlight_device_register(
6245 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6246 &ibm_backlight_data);
6247 if (IS_ERR(ibm_backlight_device)) {
6248 int rc = PTR_ERR(ibm_backlight_device);
6249 ibm_backlight_device = NULL;
6250 printk(TPACPI_ERR "Could not register backlight device\n");
6251 return rc;
6253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6254 "brightness is supported\n");
6256 if (quirks & TPACPI_BRGHT_Q_ASK) {
6257 printk(TPACPI_NOTICE
6258 "brightness: will use unverified default: "
6259 "brightness_mode=%d\n", brightness_mode);
6260 printk(TPACPI_NOTICE
6261 "brightness: please report to %s whether it works well "
6262 "or not on your ThinkPad\n", TPACPI_MAIL);
6265 ibm_backlight_device->props.max_brightness =
6266 (tp_features.bright_16levels)? 15 : 7;
6267 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6268 backlight_update_status(ibm_backlight_device);
6270 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6271 "brightness: registering brightness hotkeys "
6272 "as change notification\n");
6273 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6274 | TP_ACPI_HKEY_BRGHTUP_MASK
6275 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6276 return 0;
6279 static void brightness_suspend(pm_message_t state)
6281 tpacpi_brightness_checkpoint_nvram();
6284 static void brightness_shutdown(void)
6286 tpacpi_brightness_checkpoint_nvram();
6289 static void brightness_exit(void)
6291 if (ibm_backlight_device) {
6292 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6293 "calling backlight_device_unregister()\n");
6294 backlight_device_unregister(ibm_backlight_device);
6297 tpacpi_brightness_checkpoint_nvram();
6300 static int brightness_read(struct seq_file *m)
6302 int level;
6304 level = brightness_get(NULL);
6305 if (level < 0) {
6306 seq_printf(m, "level:\t\tunreadable\n");
6307 } else {
6308 seq_printf(m, "level:\t\t%d\n", level);
6309 seq_printf(m, "commands:\tup, down\n");
6310 seq_printf(m, "commands:\tlevel <level>"
6311 " (<level> is 0-%d)\n",
6312 (tp_features.bright_16levels) ? 15 : 7);
6315 return 0;
6318 static int brightness_write(char *buf)
6320 int level;
6321 int rc;
6322 char *cmd;
6323 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6325 level = brightness_get(NULL);
6326 if (level < 0)
6327 return level;
6329 while ((cmd = next_cmd(&buf))) {
6330 if (strlencmp(cmd, "up") == 0) {
6331 if (level < max_level)
6332 level++;
6333 } else if (strlencmp(cmd, "down") == 0) {
6334 if (level > 0)
6335 level--;
6336 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6337 level >= 0 && level <= max_level) {
6338 /* new level set */
6339 } else
6340 return -EINVAL;
6343 tpacpi_disclose_usertask("procfs brightness",
6344 "set level to %d\n", level);
6347 * Now we know what the final level should be, so we try to set it.
6348 * Doing it this way makes the syscall restartable in case of EINTR
6350 rc = brightness_set(level);
6351 if (!rc && ibm_backlight_device)
6352 backlight_force_update(ibm_backlight_device,
6353 BACKLIGHT_UPDATE_SYSFS);
6354 return (rc == -EINTR)? -ERESTARTSYS : rc;
6357 static struct ibm_struct brightness_driver_data = {
6358 .name = "brightness",
6359 .read = brightness_read,
6360 .write = brightness_write,
6361 .exit = brightness_exit,
6362 .suspend = brightness_suspend,
6363 .shutdown = brightness_shutdown,
6366 /*************************************************************************
6367 * Volume subdriver
6371 * IBM ThinkPads have a simple volume controller with MUTE gating.
6372 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6374 * Since the *61 series (and probably also the later *60 series), Lenovo
6375 * ThinkPads only implement the MUTE gate.
6377 * EC register 0x30
6378 * Bit 6: MUTE (1 mutes sound)
6379 * Bit 3-0: Volume
6380 * Other bits should be zero as far as we know.
6382 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6383 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6384 * such as bit 7 which is used to detect repeated presses of MUTE,
6385 * and we leave them unchanged.
6388 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6390 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6391 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6392 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6394 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6395 static char *alsa_id = "ThinkPadEC";
6396 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6398 struct tpacpi_alsa_data {
6399 struct snd_card *card;
6400 struct snd_ctl_elem_id *ctl_mute_id;
6401 struct snd_ctl_elem_id *ctl_vol_id;
6404 static struct snd_card *alsa_card;
6406 enum {
6407 TP_EC_AUDIO = 0x30,
6409 /* TP_EC_AUDIO bits */
6410 TP_EC_AUDIO_MUTESW = 6,
6412 /* TP_EC_AUDIO bitmasks */
6413 TP_EC_AUDIO_LVL_MSK = 0x0F,
6414 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6416 /* Maximum volume */
6417 TP_EC_VOLUME_MAX = 14,
6420 enum tpacpi_volume_access_mode {
6421 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6422 TPACPI_VOL_MODE_EC, /* Pure EC control */
6423 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6424 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6425 TPACPI_VOL_MODE_MAX
6428 enum tpacpi_volume_capabilities {
6429 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6430 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6431 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6432 TPACPI_VOL_CAP_MAX
6435 static enum tpacpi_volume_access_mode volume_mode =
6436 TPACPI_VOL_MODE_MAX;
6438 static enum tpacpi_volume_capabilities volume_capabilities;
6439 static int volume_control_allowed;
6442 * Used to syncronize writers to TP_EC_AUDIO and
6443 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6445 static struct mutex volume_mutex;
6447 static void tpacpi_volume_checkpoint_nvram(void)
6449 u8 lec = 0;
6450 u8 b_nvram;
6451 u8 ec_mask;
6453 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6454 return;
6455 if (!volume_control_allowed)
6456 return;
6458 vdbg_printk(TPACPI_DBG_MIXER,
6459 "trying to checkpoint mixer state to NVRAM...\n");
6461 if (tp_features.mixer_no_level_control)
6462 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6463 else
6464 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6466 if (mutex_lock_killable(&volume_mutex) < 0)
6467 return;
6469 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6470 goto unlock;
6471 lec &= ec_mask;
6472 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6474 if (lec != (b_nvram & ec_mask)) {
6475 /* NVRAM needs update */
6476 b_nvram &= ~ec_mask;
6477 b_nvram |= lec;
6478 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6479 dbg_printk(TPACPI_DBG_MIXER,
6480 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6481 (unsigned int) lec, (unsigned int) b_nvram);
6482 } else {
6483 vdbg_printk(TPACPI_DBG_MIXER,
6484 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6485 (unsigned int) lec, (unsigned int) b_nvram);
6488 unlock:
6489 mutex_unlock(&volume_mutex);
6492 static int volume_get_status_ec(u8 *status)
6494 u8 s;
6496 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6497 return -EIO;
6499 *status = s;
6501 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6503 return 0;
6506 static int volume_get_status(u8 *status)
6508 return volume_get_status_ec(status);
6511 static int volume_set_status_ec(const u8 status)
6513 if (!acpi_ec_write(TP_EC_AUDIO, status))
6514 return -EIO;
6516 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6518 return 0;
6521 static int volume_set_status(const u8 status)
6523 return volume_set_status_ec(status);
6526 static int volume_set_mute_ec(const bool mute)
6528 int rc;
6529 u8 s, n;
6531 if (mutex_lock_killable(&volume_mutex) < 0)
6532 return -EINTR;
6534 rc = volume_get_status_ec(&s);
6535 if (rc)
6536 goto unlock;
6538 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6539 s & ~TP_EC_AUDIO_MUTESW_MSK;
6541 if (n != s)
6542 rc = volume_set_status_ec(n);
6544 unlock:
6545 mutex_unlock(&volume_mutex);
6546 return rc;
6549 static int volume_set_mute(const bool mute)
6551 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6552 (mute) ? "" : "un");
6553 return volume_set_mute_ec(mute);
6556 static int volume_set_volume_ec(const u8 vol)
6558 int rc;
6559 u8 s, n;
6561 if (vol > TP_EC_VOLUME_MAX)
6562 return -EINVAL;
6564 if (mutex_lock_killable(&volume_mutex) < 0)
6565 return -EINTR;
6567 rc = volume_get_status_ec(&s);
6568 if (rc)
6569 goto unlock;
6571 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6573 if (n != s)
6574 rc = volume_set_status_ec(n);
6576 unlock:
6577 mutex_unlock(&volume_mutex);
6578 return rc;
6581 static int volume_set_volume(const u8 vol)
6583 dbg_printk(TPACPI_DBG_MIXER,
6584 "trying to set volume level to %hu\n", vol);
6585 return volume_set_volume_ec(vol);
6588 static void volume_alsa_notify_change(void)
6590 struct tpacpi_alsa_data *d;
6592 if (alsa_card && alsa_card->private_data) {
6593 d = alsa_card->private_data;
6594 if (d->ctl_mute_id)
6595 snd_ctl_notify(alsa_card,
6596 SNDRV_CTL_EVENT_MASK_VALUE,
6597 d->ctl_mute_id);
6598 if (d->ctl_vol_id)
6599 snd_ctl_notify(alsa_card,
6600 SNDRV_CTL_EVENT_MASK_VALUE,
6601 d->ctl_vol_id);
6605 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6606 struct snd_ctl_elem_info *uinfo)
6608 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6609 uinfo->count = 1;
6610 uinfo->value.integer.min = 0;
6611 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6612 return 0;
6615 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6616 struct snd_ctl_elem_value *ucontrol)
6618 u8 s;
6619 int rc;
6621 rc = volume_get_status(&s);
6622 if (rc < 0)
6623 return rc;
6625 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6626 return 0;
6629 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6630 struct snd_ctl_elem_value *ucontrol)
6632 return volume_set_volume(ucontrol->value.integer.value[0]);
6635 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6637 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6638 struct snd_ctl_elem_value *ucontrol)
6640 u8 s;
6641 int rc;
6643 rc = volume_get_status(&s);
6644 if (rc < 0)
6645 return rc;
6647 ucontrol->value.integer.value[0] =
6648 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6649 return 0;
6652 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6653 struct snd_ctl_elem_value *ucontrol)
6655 return volume_set_mute(!ucontrol->value.integer.value[0]);
6658 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6659 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6660 .name = "Console Playback Volume",
6661 .index = 0,
6662 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6663 .info = volume_alsa_vol_info,
6664 .get = volume_alsa_vol_get,
6667 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6669 .name = "Console Playback Switch",
6670 .index = 0,
6671 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6672 .info = volume_alsa_mute_info,
6673 .get = volume_alsa_mute_get,
6676 static void volume_suspend(pm_message_t state)
6678 tpacpi_volume_checkpoint_nvram();
6681 static void volume_resume(void)
6683 volume_alsa_notify_change();
6686 static void volume_shutdown(void)
6688 tpacpi_volume_checkpoint_nvram();
6691 static void volume_exit(void)
6693 if (alsa_card) {
6694 snd_card_free(alsa_card);
6695 alsa_card = NULL;
6698 tpacpi_volume_checkpoint_nvram();
6701 static int __init volume_create_alsa_mixer(void)
6703 struct snd_card *card;
6704 struct tpacpi_alsa_data *data;
6705 struct snd_kcontrol *ctl_vol;
6706 struct snd_kcontrol *ctl_mute;
6707 int rc;
6709 card = snd_card_new(alsa_index, alsa_id, THIS_MODULE,
6710 sizeof(struct tpacpi_alsa_data));
6711 if (!card) {
6712 printk(TPACPI_ERR
6713 "Failed to create ALSA card structures\n");
6714 return 1;
6717 BUG_ON(!card->private_data);
6718 data = card->private_data;
6719 data->card = card;
6721 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6722 sizeof(card->driver));
6723 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6724 sizeof(card->shortname));
6725 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6726 (thinkpad_id.ec_version_str) ?
6727 thinkpad_id.ec_version_str : "(unknown)");
6728 snprintf(card->longname, sizeof(card->longname),
6729 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6730 (thinkpad_id.ec_version_str) ?
6731 thinkpad_id.ec_version_str : "unknown");
6733 if (volume_control_allowed) {
6734 volume_alsa_control_vol.put = volume_alsa_vol_put;
6735 volume_alsa_control_vol.access =
6736 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6738 volume_alsa_control_mute.put = volume_alsa_mute_put;
6739 volume_alsa_control_mute.access =
6740 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6743 if (!tp_features.mixer_no_level_control) {
6744 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6745 rc = snd_ctl_add(card, ctl_vol);
6746 if (rc < 0) {
6747 printk(TPACPI_ERR
6748 "Failed to create ALSA volume control: %d\n",
6749 rc);
6750 goto err_exit;
6752 data->ctl_vol_id = &ctl_vol->id;
6755 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6756 rc = snd_ctl_add(card, ctl_mute);
6757 if (rc < 0) {
6758 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6759 rc);
6760 goto err_exit;
6762 data->ctl_mute_id = &ctl_mute->id;
6764 snd_card_set_dev(card, &tpacpi_pdev->dev);
6765 rc = snd_card_register(card);
6766 if (rc < 0) {
6767 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6768 goto err_exit;
6771 alsa_card = card;
6772 return 0;
6774 err_exit:
6775 snd_card_free(card);
6776 return 1;
6779 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6780 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6782 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6783 /* Whitelist volume level on all IBM by default */
6784 { .vendor = PCI_VENDOR_ID_IBM,
6785 .bios = TPACPI_MATCH_ANY,
6786 .ec = TPACPI_MATCH_ANY,
6787 .quirks = TPACPI_VOL_Q_LEVEL },
6789 /* Lenovo models with volume control (needs confirmation) */
6790 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6791 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6792 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6793 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6794 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6795 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6796 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6798 /* Whitelist mute-only on all Lenovo by default */
6799 { .vendor = PCI_VENDOR_ID_LENOVO,
6800 .bios = TPACPI_MATCH_ANY,
6801 .ec = TPACPI_MATCH_ANY,
6802 .quirks = TPACPI_VOL_Q_MUTEONLY }
6805 static int __init volume_init(struct ibm_init_struct *iibm)
6807 unsigned long quirks;
6808 int rc;
6810 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6812 mutex_init(&volume_mutex);
6815 * Check for module parameter bogosity, note that we
6816 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6817 * able to detect "unspecified"
6819 if (volume_mode > TPACPI_VOL_MODE_MAX)
6820 return -EINVAL;
6822 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6823 printk(TPACPI_ERR
6824 "UCMS step volume mode not implemented, "
6825 "please contact %s\n", TPACPI_MAIL);
6826 return 1;
6829 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6830 return -EINVAL;
6833 * The ALSA mixer is our primary interface.
6834 * When disabled, don't install the subdriver at all
6836 if (!alsa_enable) {
6837 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6838 "ALSA mixer disabled by parameter, "
6839 "not loading volume subdriver...\n");
6840 return 1;
6843 quirks = tpacpi_check_quirks(volume_quirk_table,
6844 ARRAY_SIZE(volume_quirk_table));
6846 switch (volume_capabilities) {
6847 case TPACPI_VOL_CAP_AUTO:
6848 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6849 tp_features.mixer_no_level_control = 1;
6850 else if (quirks & TPACPI_VOL_Q_LEVEL)
6851 tp_features.mixer_no_level_control = 0;
6852 else
6853 return 1; /* no mixer */
6854 break;
6855 case TPACPI_VOL_CAP_VOLMUTE:
6856 tp_features.mixer_no_level_control = 0;
6857 break;
6858 case TPACPI_VOL_CAP_MUTEONLY:
6859 tp_features.mixer_no_level_control = 1;
6860 break;
6861 default:
6862 return 1;
6865 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6866 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6867 "using user-supplied volume_capabilities=%d\n",
6868 volume_capabilities);
6870 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6871 volume_mode == TPACPI_VOL_MODE_MAX) {
6872 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6874 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6875 "driver auto-selected volume_mode=%d\n",
6876 volume_mode);
6877 } else {
6878 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6879 "using user-supplied volume_mode=%d\n",
6880 volume_mode);
6883 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6884 "mute is supported, volume control is %s\n",
6885 str_supported(!tp_features.mixer_no_level_control));
6887 rc = volume_create_alsa_mixer();
6888 if (rc) {
6889 printk(TPACPI_ERR
6890 "Could not create the ALSA mixer interface\n");
6891 return rc;
6894 printk(TPACPI_INFO
6895 "Console audio control enabled, mode: %s\n",
6896 (volume_control_allowed) ?
6897 "override (read/write)" :
6898 "monitor (read only)");
6900 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6901 "registering volume hotkeys as change notification\n");
6902 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6903 | TP_ACPI_HKEY_VOLUP_MASK
6904 | TP_ACPI_HKEY_VOLDWN_MASK
6905 | TP_ACPI_HKEY_MUTE_MASK);
6907 return 0;
6910 static int volume_read(struct seq_file *m)
6912 u8 status;
6914 if (volume_get_status(&status) < 0) {
6915 seq_printf(m, "level:\t\tunreadable\n");
6916 } else {
6917 if (tp_features.mixer_no_level_control)
6918 seq_printf(m, "level:\t\tunsupported\n");
6919 else
6920 seq_printf(m, "level:\t\t%d\n",
6921 status & TP_EC_AUDIO_LVL_MSK);
6923 seq_printf(m, "mute:\t\t%s\n",
6924 onoff(status, TP_EC_AUDIO_MUTESW));
6926 if (volume_control_allowed) {
6927 seq_printf(m, "commands:\tunmute, mute\n");
6928 if (!tp_features.mixer_no_level_control) {
6929 seq_printf(m,
6930 "commands:\tup, down\n");
6931 seq_printf(m,
6932 "commands:\tlevel <level>"
6933 " (<level> is 0-%d)\n",
6934 TP_EC_VOLUME_MAX);
6939 return 0;
6942 static int volume_write(char *buf)
6944 u8 s;
6945 u8 new_level, new_mute;
6946 int l;
6947 char *cmd;
6948 int rc;
6951 * We do allow volume control at driver startup, so that the
6952 * user can set initial state through the volume=... parameter hack.
6954 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6955 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6956 tp_warned.volume_ctrl_forbidden = 1;
6957 printk(TPACPI_NOTICE
6958 "Console audio control in monitor mode, "
6959 "changes are not allowed.\n");
6960 printk(TPACPI_NOTICE
6961 "Use the volume_control=1 module parameter "
6962 "to enable volume control\n");
6964 return -EPERM;
6967 rc = volume_get_status(&s);
6968 if (rc < 0)
6969 return rc;
6971 new_level = s & TP_EC_AUDIO_LVL_MSK;
6972 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6974 while ((cmd = next_cmd(&buf))) {
6975 if (!tp_features.mixer_no_level_control) {
6976 if (strlencmp(cmd, "up") == 0) {
6977 if (new_mute)
6978 new_mute = 0;
6979 else if (new_level < TP_EC_VOLUME_MAX)
6980 new_level++;
6981 continue;
6982 } else if (strlencmp(cmd, "down") == 0) {
6983 if (new_mute)
6984 new_mute = 0;
6985 else if (new_level > 0)
6986 new_level--;
6987 continue;
6988 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6989 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6990 new_level = l;
6991 continue;
6994 if (strlencmp(cmd, "mute") == 0)
6995 new_mute = TP_EC_AUDIO_MUTESW_MSK;
6996 else if (strlencmp(cmd, "unmute") == 0)
6997 new_mute = 0;
6998 else
6999 return -EINVAL;
7002 if (tp_features.mixer_no_level_control) {
7003 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7004 new_mute ? "" : "un");
7005 rc = volume_set_mute(!!new_mute);
7006 } else {
7007 tpacpi_disclose_usertask("procfs volume",
7008 "%smute and set level to %d\n",
7009 new_mute ? "" : "un", new_level);
7010 rc = volume_set_status(new_mute | new_level);
7012 volume_alsa_notify_change();
7014 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7017 static struct ibm_struct volume_driver_data = {
7018 .name = "volume",
7019 .read = volume_read,
7020 .write = volume_write,
7021 .exit = volume_exit,
7022 .suspend = volume_suspend,
7023 .resume = volume_resume,
7024 .shutdown = volume_shutdown,
7027 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7029 #define alsa_card NULL
7031 static void inline volume_alsa_notify_change(void)
7035 static int __init volume_init(struct ibm_init_struct *iibm)
7037 printk(TPACPI_INFO
7038 "volume: disabled as there is no ALSA support in this kernel\n");
7040 return 1;
7043 static struct ibm_struct volume_driver_data = {
7044 .name = "volume",
7047 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7049 /*************************************************************************
7050 * Fan subdriver
7054 * FAN ACCESS MODES
7056 * TPACPI_FAN_RD_ACPI_GFAN:
7057 * ACPI GFAN method: returns fan level
7059 * see TPACPI_FAN_WR_ACPI_SFAN
7060 * EC 0x2f (HFSP) not available if GFAN exists
7062 * TPACPI_FAN_WR_ACPI_SFAN:
7063 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7065 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7066 * it for writing.
7068 * TPACPI_FAN_WR_TPEC:
7069 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7070 * Supported on almost all ThinkPads
7072 * Fan speed changes of any sort (including those caused by the
7073 * disengaged mode) are usually done slowly by the firmware as the
7074 * maximum ammount of fan duty cycle change per second seems to be
7075 * limited.
7077 * Reading is not available if GFAN exists.
7078 * Writing is not available if SFAN exists.
7080 * Bits
7081 * 7 automatic mode engaged;
7082 * (default operation mode of the ThinkPad)
7083 * fan level is ignored in this mode.
7084 * 6 full speed mode (takes precedence over bit 7);
7085 * not available on all thinkpads. May disable
7086 * the tachometer while the fan controller ramps up
7087 * the speed (which can take up to a few *minutes*).
7088 * Speeds up fan to 100% duty-cycle, which is far above
7089 * the standard RPM levels. It is not impossible that
7090 * it could cause hardware damage.
7091 * 5-3 unused in some models. Extra bits for fan level
7092 * in others, but still useless as all values above
7093 * 7 map to the same speed as level 7 in these models.
7094 * 2-0 fan level (0..7 usually)
7095 * 0x00 = stop
7096 * 0x07 = max (set when temperatures critical)
7097 * Some ThinkPads may have other levels, see
7098 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7100 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7101 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7102 * does so, its initial value is meaningless (0x07).
7104 * For firmware bugs, refer to:
7105 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7107 * ----
7109 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7110 * Main fan tachometer reading (in RPM)
7112 * This register is present on all ThinkPads with a new-style EC, and
7113 * it is known not to be present on the A21m/e, and T22, as there is
7114 * something else in offset 0x84 according to the ACPI DSDT. Other
7115 * ThinkPads from this same time period (and earlier) probably lack the
7116 * tachometer as well.
7118 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7119 * was never fixed by IBM to report the EC firmware version string
7120 * probably support the tachometer (like the early X models), so
7121 * detecting it is quite hard. We need more data to know for sure.
7123 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7124 * might result.
7126 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7127 * mode.
7129 * For firmware bugs, refer to:
7130 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7132 * ----
7134 * ThinkPad EC register 0x31 bit 0 (only on select models)
7136 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7137 * show the speed of the main fan. When bit 0 of EC register 0x31
7138 * is one, the tachometer registers show the speed of the auxiliary
7139 * fan.
7141 * Fan control seems to affect both fans, regardless of the state
7142 * of this bit.
7144 * So far, only the firmware for the X60/X61 non-tablet versions
7145 * seem to support this (firmware TP-7M).
7147 * TPACPI_FAN_WR_ACPI_FANS:
7148 * ThinkPad X31, X40, X41. Not available in the X60.
7150 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7151 * high speed. ACPI DSDT seems to map these three speeds to levels
7152 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7153 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7155 * The speeds are stored on handles
7156 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7158 * There are three default speed sets, acessible as handles:
7159 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7161 * ACPI DSDT switches which set is in use depending on various
7162 * factors.
7164 * TPACPI_FAN_WR_TPEC is also available and should be used to
7165 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7166 * but the ACPI tables just mention level 7.
7169 enum { /* Fan control constants */
7170 fan_status_offset = 0x2f, /* EC register 0x2f */
7171 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7172 * 0x84 must be read before 0x85 */
7173 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7174 bit 0 selects which fan is active */
7176 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7177 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7179 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7182 enum fan_status_access_mode {
7183 TPACPI_FAN_NONE = 0, /* No fan status or control */
7184 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7185 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7188 enum fan_control_access_mode {
7189 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7190 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7191 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7192 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7195 enum fan_control_commands {
7196 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7197 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7198 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7199 * and also watchdog cmd */
7202 static int fan_control_allowed;
7204 static enum fan_status_access_mode fan_status_access_mode;
7205 static enum fan_control_access_mode fan_control_access_mode;
7206 static enum fan_control_commands fan_control_commands;
7208 static u8 fan_control_initial_status;
7209 static u8 fan_control_desired_level;
7210 static u8 fan_control_resume_level;
7211 static int fan_watchdog_maxinterval;
7213 static struct mutex fan_mutex;
7215 static void fan_watchdog_fire(struct work_struct *ignored);
7216 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7218 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7219 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7220 "\\FSPD", /* 600e/x, 770e, 770x */
7221 ); /* all others */
7222 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7223 "JFNS", /* 770x-JL */
7224 ); /* all others */
7227 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7228 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7229 * be in auto mode (0x80).
7231 * This is corrected by any write to HFSP either by the driver, or
7232 * by the firmware.
7234 * We assume 0x07 really means auto mode while this quirk is active,
7235 * as this is far more likely than the ThinkPad being in level 7,
7236 * which is only used by the firmware during thermal emergencies.
7238 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7239 * TP-70 (T43, R52), which are known to be buggy.
7242 static void fan_quirk1_setup(void)
7244 if (fan_control_initial_status == 0x07) {
7245 printk(TPACPI_NOTICE
7246 "fan_init: initial fan status is unknown, "
7247 "assuming it is in auto mode\n");
7248 tp_features.fan_ctrl_status_undef = 1;
7252 static void fan_quirk1_handle(u8 *fan_status)
7254 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7255 if (*fan_status != fan_control_initial_status) {
7256 /* something changed the HFSP regisnter since
7257 * driver init time, so it is not undefined
7258 * anymore */
7259 tp_features.fan_ctrl_status_undef = 0;
7260 } else {
7261 /* Return most likely status. In fact, it
7262 * might be the only possible status */
7263 *fan_status = TP_EC_FAN_AUTO;
7268 /* Select main fan on X60/X61, NOOP on others */
7269 static bool fan_select_fan1(void)
7271 if (tp_features.second_fan) {
7272 u8 val;
7274 if (ec_read(fan_select_offset, &val) < 0)
7275 return false;
7276 val &= 0xFEU;
7277 if (ec_write(fan_select_offset, val) < 0)
7278 return false;
7280 return true;
7283 /* Select secondary fan on X60/X61 */
7284 static bool fan_select_fan2(void)
7286 u8 val;
7288 if (!tp_features.second_fan)
7289 return false;
7291 if (ec_read(fan_select_offset, &val) < 0)
7292 return false;
7293 val |= 0x01U;
7294 if (ec_write(fan_select_offset, val) < 0)
7295 return false;
7297 return true;
7301 * Call with fan_mutex held
7303 static void fan_update_desired_level(u8 status)
7305 if ((status &
7306 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7307 if (status > 7)
7308 fan_control_desired_level = 7;
7309 else
7310 fan_control_desired_level = status;
7314 static int fan_get_status(u8 *status)
7316 u8 s;
7318 /* TODO:
7319 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7321 switch (fan_status_access_mode) {
7322 case TPACPI_FAN_RD_ACPI_GFAN:
7323 /* 570, 600e/x, 770e, 770x */
7325 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7326 return -EIO;
7328 if (likely(status))
7329 *status = s & 0x07;
7331 break;
7333 case TPACPI_FAN_RD_TPEC:
7334 /* all except 570, 600e/x, 770e, 770x */
7335 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7336 return -EIO;
7338 if (likely(status)) {
7339 *status = s;
7340 fan_quirk1_handle(status);
7343 break;
7345 default:
7346 return -ENXIO;
7349 return 0;
7352 static int fan_get_status_safe(u8 *status)
7354 int rc;
7355 u8 s;
7357 if (mutex_lock_killable(&fan_mutex))
7358 return -ERESTARTSYS;
7359 rc = fan_get_status(&s);
7360 if (!rc)
7361 fan_update_desired_level(s);
7362 mutex_unlock(&fan_mutex);
7364 if (status)
7365 *status = s;
7367 return rc;
7370 static int fan_get_speed(unsigned int *speed)
7372 u8 hi, lo;
7374 switch (fan_status_access_mode) {
7375 case TPACPI_FAN_RD_TPEC:
7376 /* all except 570, 600e/x, 770e, 770x */
7377 if (unlikely(!fan_select_fan1()))
7378 return -EIO;
7379 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7380 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7381 return -EIO;
7383 if (likely(speed))
7384 *speed = (hi << 8) | lo;
7386 break;
7388 default:
7389 return -ENXIO;
7392 return 0;
7395 static int fan2_get_speed(unsigned int *speed)
7397 u8 hi, lo;
7398 bool rc;
7400 switch (fan_status_access_mode) {
7401 case TPACPI_FAN_RD_TPEC:
7402 /* all except 570, 600e/x, 770e, 770x */
7403 if (unlikely(!fan_select_fan2()))
7404 return -EIO;
7405 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7406 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7407 fan_select_fan1(); /* play it safe */
7408 if (rc)
7409 return -EIO;
7411 if (likely(speed))
7412 *speed = (hi << 8) | lo;
7414 break;
7416 default:
7417 return -ENXIO;
7420 return 0;
7423 static int fan_set_level(int level)
7425 if (!fan_control_allowed)
7426 return -EPERM;
7428 switch (fan_control_access_mode) {
7429 case TPACPI_FAN_WR_ACPI_SFAN:
7430 if (level >= 0 && level <= 7) {
7431 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7432 return -EIO;
7433 } else
7434 return -EINVAL;
7435 break;
7437 case TPACPI_FAN_WR_ACPI_FANS:
7438 case TPACPI_FAN_WR_TPEC:
7439 if (!(level & TP_EC_FAN_AUTO) &&
7440 !(level & TP_EC_FAN_FULLSPEED) &&
7441 ((level < 0) || (level > 7)))
7442 return -EINVAL;
7444 /* safety net should the EC not support AUTO
7445 * or FULLSPEED mode bits and just ignore them */
7446 if (level & TP_EC_FAN_FULLSPEED)
7447 level |= 7; /* safety min speed 7 */
7448 else if (level & TP_EC_FAN_AUTO)
7449 level |= 4; /* safety min speed 4 */
7451 if (!acpi_ec_write(fan_status_offset, level))
7452 return -EIO;
7453 else
7454 tp_features.fan_ctrl_status_undef = 0;
7455 break;
7457 default:
7458 return -ENXIO;
7461 vdbg_printk(TPACPI_DBG_FAN,
7462 "fan control: set fan control register to 0x%02x\n", level);
7463 return 0;
7466 static int fan_set_level_safe(int level)
7468 int rc;
7470 if (!fan_control_allowed)
7471 return -EPERM;
7473 if (mutex_lock_killable(&fan_mutex))
7474 return -ERESTARTSYS;
7476 if (level == TPACPI_FAN_LAST_LEVEL)
7477 level = fan_control_desired_level;
7479 rc = fan_set_level(level);
7480 if (!rc)
7481 fan_update_desired_level(level);
7483 mutex_unlock(&fan_mutex);
7484 return rc;
7487 static int fan_set_enable(void)
7489 u8 s;
7490 int rc;
7492 if (!fan_control_allowed)
7493 return -EPERM;
7495 if (mutex_lock_killable(&fan_mutex))
7496 return -ERESTARTSYS;
7498 switch (fan_control_access_mode) {
7499 case TPACPI_FAN_WR_ACPI_FANS:
7500 case TPACPI_FAN_WR_TPEC:
7501 rc = fan_get_status(&s);
7502 if (rc < 0)
7503 break;
7505 /* Don't go out of emergency fan mode */
7506 if (s != 7) {
7507 s &= 0x07;
7508 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7511 if (!acpi_ec_write(fan_status_offset, s))
7512 rc = -EIO;
7513 else {
7514 tp_features.fan_ctrl_status_undef = 0;
7515 rc = 0;
7517 break;
7519 case TPACPI_FAN_WR_ACPI_SFAN:
7520 rc = fan_get_status(&s);
7521 if (rc < 0)
7522 break;
7524 s &= 0x07;
7526 /* Set fan to at least level 4 */
7527 s |= 4;
7529 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7530 rc = -EIO;
7531 else
7532 rc = 0;
7533 break;
7535 default:
7536 rc = -ENXIO;
7539 mutex_unlock(&fan_mutex);
7541 if (!rc)
7542 vdbg_printk(TPACPI_DBG_FAN,
7543 "fan control: set fan control register to 0x%02x\n",
7545 return rc;
7548 static int fan_set_disable(void)
7550 int rc;
7552 if (!fan_control_allowed)
7553 return -EPERM;
7555 if (mutex_lock_killable(&fan_mutex))
7556 return -ERESTARTSYS;
7558 rc = 0;
7559 switch (fan_control_access_mode) {
7560 case TPACPI_FAN_WR_ACPI_FANS:
7561 case TPACPI_FAN_WR_TPEC:
7562 if (!acpi_ec_write(fan_status_offset, 0x00))
7563 rc = -EIO;
7564 else {
7565 fan_control_desired_level = 0;
7566 tp_features.fan_ctrl_status_undef = 0;
7568 break;
7570 case TPACPI_FAN_WR_ACPI_SFAN:
7571 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7572 rc = -EIO;
7573 else
7574 fan_control_desired_level = 0;
7575 break;
7577 default:
7578 rc = -ENXIO;
7581 if (!rc)
7582 vdbg_printk(TPACPI_DBG_FAN,
7583 "fan control: set fan control register to 0\n");
7585 mutex_unlock(&fan_mutex);
7586 return rc;
7589 static int fan_set_speed(int speed)
7591 int rc;
7593 if (!fan_control_allowed)
7594 return -EPERM;
7596 if (mutex_lock_killable(&fan_mutex))
7597 return -ERESTARTSYS;
7599 rc = 0;
7600 switch (fan_control_access_mode) {
7601 case TPACPI_FAN_WR_ACPI_FANS:
7602 if (speed >= 0 && speed <= 65535) {
7603 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7604 speed, speed, speed))
7605 rc = -EIO;
7606 } else
7607 rc = -EINVAL;
7608 break;
7610 default:
7611 rc = -ENXIO;
7614 mutex_unlock(&fan_mutex);
7615 return rc;
7618 static void fan_watchdog_reset(void)
7620 static int fan_watchdog_active;
7622 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7623 return;
7625 if (fan_watchdog_active)
7626 cancel_delayed_work(&fan_watchdog_task);
7628 if (fan_watchdog_maxinterval > 0 &&
7629 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7630 fan_watchdog_active = 1;
7631 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7632 msecs_to_jiffies(fan_watchdog_maxinterval
7633 * 1000))) {
7634 printk(TPACPI_ERR
7635 "failed to queue the fan watchdog, "
7636 "watchdog will not trigger\n");
7638 } else
7639 fan_watchdog_active = 0;
7642 static void fan_watchdog_fire(struct work_struct *ignored)
7644 int rc;
7646 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7647 return;
7649 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7650 rc = fan_set_enable();
7651 if (rc < 0) {
7652 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7653 "will try again later...\n", -rc);
7654 /* reschedule for later */
7655 fan_watchdog_reset();
7660 * SYSFS fan layout: hwmon compatible (device)
7662 * pwm*_enable:
7663 * 0: "disengaged" mode
7664 * 1: manual mode
7665 * 2: native EC "auto" mode (recommended, hardware default)
7667 * pwm*: set speed in manual mode, ignored otherwise.
7668 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7669 * interpolation.
7671 * fan*_input: tachometer reading, RPM
7674 * SYSFS fan layout: extensions
7676 * fan_watchdog (driver):
7677 * fan watchdog interval in seconds, 0 disables (default), max 120
7680 /* sysfs fan pwm1_enable ----------------------------------------------- */
7681 static ssize_t fan_pwm1_enable_show(struct device *dev,
7682 struct device_attribute *attr,
7683 char *buf)
7685 int res, mode;
7686 u8 status;
7688 res = fan_get_status_safe(&status);
7689 if (res)
7690 return res;
7692 if (status & TP_EC_FAN_FULLSPEED) {
7693 mode = 0;
7694 } else if (status & TP_EC_FAN_AUTO) {
7695 mode = 2;
7696 } else
7697 mode = 1;
7699 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7702 static ssize_t fan_pwm1_enable_store(struct device *dev,
7703 struct device_attribute *attr,
7704 const char *buf, size_t count)
7706 unsigned long t;
7707 int res, level;
7709 if (parse_strtoul(buf, 2, &t))
7710 return -EINVAL;
7712 tpacpi_disclose_usertask("hwmon pwm1_enable",
7713 "set fan mode to %lu\n", t);
7715 switch (t) {
7716 case 0:
7717 level = TP_EC_FAN_FULLSPEED;
7718 break;
7719 case 1:
7720 level = TPACPI_FAN_LAST_LEVEL;
7721 break;
7722 case 2:
7723 level = TP_EC_FAN_AUTO;
7724 break;
7725 case 3:
7726 /* reserved for software-controlled auto mode */
7727 return -ENOSYS;
7728 default:
7729 return -EINVAL;
7732 res = fan_set_level_safe(level);
7733 if (res == -ENXIO)
7734 return -EINVAL;
7735 else if (res < 0)
7736 return res;
7738 fan_watchdog_reset();
7740 return count;
7743 static struct device_attribute dev_attr_fan_pwm1_enable =
7744 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7745 fan_pwm1_enable_show, fan_pwm1_enable_store);
7747 /* sysfs fan pwm1 ------------------------------------------------------ */
7748 static ssize_t fan_pwm1_show(struct device *dev,
7749 struct device_attribute *attr,
7750 char *buf)
7752 int res;
7753 u8 status;
7755 res = fan_get_status_safe(&status);
7756 if (res)
7757 return res;
7759 if ((status &
7760 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7761 status = fan_control_desired_level;
7763 if (status > 7)
7764 status = 7;
7766 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7769 static ssize_t fan_pwm1_store(struct device *dev,
7770 struct device_attribute *attr,
7771 const char *buf, size_t count)
7773 unsigned long s;
7774 int rc;
7775 u8 status, newlevel;
7777 if (parse_strtoul(buf, 255, &s))
7778 return -EINVAL;
7780 tpacpi_disclose_usertask("hwmon pwm1",
7781 "set fan speed to %lu\n", s);
7783 /* scale down from 0-255 to 0-7 */
7784 newlevel = (s >> 5) & 0x07;
7786 if (mutex_lock_killable(&fan_mutex))
7787 return -ERESTARTSYS;
7789 rc = fan_get_status(&status);
7790 if (!rc && (status &
7791 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7792 rc = fan_set_level(newlevel);
7793 if (rc == -ENXIO)
7794 rc = -EINVAL;
7795 else if (!rc) {
7796 fan_update_desired_level(newlevel);
7797 fan_watchdog_reset();
7801 mutex_unlock(&fan_mutex);
7802 return (rc)? rc : count;
7805 static struct device_attribute dev_attr_fan_pwm1 =
7806 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7807 fan_pwm1_show, fan_pwm1_store);
7809 /* sysfs fan fan1_input ------------------------------------------------ */
7810 static ssize_t fan_fan1_input_show(struct device *dev,
7811 struct device_attribute *attr,
7812 char *buf)
7814 int res;
7815 unsigned int speed;
7817 res = fan_get_speed(&speed);
7818 if (res < 0)
7819 return res;
7821 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7824 static struct device_attribute dev_attr_fan_fan1_input =
7825 __ATTR(fan1_input, S_IRUGO,
7826 fan_fan1_input_show, NULL);
7828 /* sysfs fan fan2_input ------------------------------------------------ */
7829 static ssize_t fan_fan2_input_show(struct device *dev,
7830 struct device_attribute *attr,
7831 char *buf)
7833 int res;
7834 unsigned int speed;
7836 res = fan2_get_speed(&speed);
7837 if (res < 0)
7838 return res;
7840 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7843 static struct device_attribute dev_attr_fan_fan2_input =
7844 __ATTR(fan2_input, S_IRUGO,
7845 fan_fan2_input_show, NULL);
7847 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7848 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7849 char *buf)
7851 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7854 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7855 const char *buf, size_t count)
7857 unsigned long t;
7859 if (parse_strtoul(buf, 120, &t))
7860 return -EINVAL;
7862 if (!fan_control_allowed)
7863 return -EPERM;
7865 fan_watchdog_maxinterval = t;
7866 fan_watchdog_reset();
7868 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7870 return count;
7873 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7874 fan_fan_watchdog_show, fan_fan_watchdog_store);
7876 /* --------------------------------------------------------------------- */
7877 static struct attribute *fan_attributes[] = {
7878 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7879 &dev_attr_fan_fan1_input.attr,
7880 NULL, /* for fan2_input */
7881 NULL
7884 static const struct attribute_group fan_attr_group = {
7885 .attrs = fan_attributes,
7888 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7889 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7891 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7892 { .vendor = PCI_VENDOR_ID_IBM, \
7893 .bios = TPACPI_MATCH_ANY, \
7894 .ec = TPID(__id1, __id2), \
7895 .quirks = __quirks }
7897 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7898 { .vendor = PCI_VENDOR_ID_LENOVO, \
7899 .bios = TPACPI_MATCH_ANY, \
7900 .ec = TPID(__id1, __id2), \
7901 .quirks = __quirks }
7903 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7904 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7905 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7906 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7907 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7908 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7911 #undef TPACPI_FAN_QL
7912 #undef TPACPI_FAN_QI
7914 static int __init fan_init(struct ibm_init_struct *iibm)
7916 int rc;
7917 unsigned long quirks;
7919 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7920 "initializing fan subdriver\n");
7922 mutex_init(&fan_mutex);
7923 fan_status_access_mode = TPACPI_FAN_NONE;
7924 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7925 fan_control_commands = 0;
7926 fan_watchdog_maxinterval = 0;
7927 tp_features.fan_ctrl_status_undef = 0;
7928 tp_features.second_fan = 0;
7929 fan_control_desired_level = 7;
7931 TPACPI_ACPIHANDLE_INIT(fans);
7932 TPACPI_ACPIHANDLE_INIT(gfan);
7933 TPACPI_ACPIHANDLE_INIT(sfan);
7935 quirks = tpacpi_check_quirks(fan_quirk_table,
7936 ARRAY_SIZE(fan_quirk_table));
7938 if (gfan_handle) {
7939 /* 570, 600e/x, 770e, 770x */
7940 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7941 } else {
7942 /* all other ThinkPads: note that even old-style
7943 * ThinkPad ECs supports the fan control register */
7944 if (likely(acpi_ec_read(fan_status_offset,
7945 &fan_control_initial_status))) {
7946 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7947 if (quirks & TPACPI_FAN_Q1)
7948 fan_quirk1_setup();
7949 if (quirks & TPACPI_FAN_2FAN) {
7950 tp_features.second_fan = 1;
7951 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7952 "secondary fan support enabled\n");
7954 } else {
7955 printk(TPACPI_ERR
7956 "ThinkPad ACPI EC access misbehaving, "
7957 "fan status and control unavailable\n");
7958 return 1;
7962 if (sfan_handle) {
7963 /* 570, 770x-JL */
7964 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7965 fan_control_commands |=
7966 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7967 } else {
7968 if (!gfan_handle) {
7969 /* gfan without sfan means no fan control */
7970 /* all other models implement TP EC 0x2f control */
7972 if (fans_handle) {
7973 /* X31, X40, X41 */
7974 fan_control_access_mode =
7975 TPACPI_FAN_WR_ACPI_FANS;
7976 fan_control_commands |=
7977 TPACPI_FAN_CMD_SPEED |
7978 TPACPI_FAN_CMD_LEVEL |
7979 TPACPI_FAN_CMD_ENABLE;
7980 } else {
7981 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7982 fan_control_commands |=
7983 TPACPI_FAN_CMD_LEVEL |
7984 TPACPI_FAN_CMD_ENABLE;
7989 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7990 "fan is %s, modes %d, %d\n",
7991 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7992 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7993 fan_status_access_mode, fan_control_access_mode);
7995 /* fan control master switch */
7996 if (!fan_control_allowed) {
7997 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7998 fan_control_commands = 0;
7999 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8000 "fan control features disabled by parameter\n");
8003 /* update fan_control_desired_level */
8004 if (fan_status_access_mode != TPACPI_FAN_NONE)
8005 fan_get_status_safe(NULL);
8007 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8008 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8009 if (tp_features.second_fan) {
8010 /* attach second fan tachometer */
8011 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8012 &dev_attr_fan_fan2_input.attr;
8014 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8015 &fan_attr_group);
8016 if (rc < 0)
8017 return rc;
8019 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8020 &driver_attr_fan_watchdog);
8021 if (rc < 0) {
8022 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8023 &fan_attr_group);
8024 return rc;
8026 return 0;
8027 } else
8028 return 1;
8031 static void fan_exit(void)
8033 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8034 "cancelling any pending fan watchdog tasks\n");
8036 /* FIXME: can we really do this unconditionally? */
8037 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8038 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8039 &driver_attr_fan_watchdog);
8041 cancel_delayed_work(&fan_watchdog_task);
8042 flush_workqueue(tpacpi_wq);
8045 static void fan_suspend(pm_message_t state)
8047 int rc;
8049 if (!fan_control_allowed)
8050 return;
8052 /* Store fan status in cache */
8053 fan_control_resume_level = 0;
8054 rc = fan_get_status_safe(&fan_control_resume_level);
8055 if (rc < 0)
8056 printk(TPACPI_NOTICE
8057 "failed to read fan level for later "
8058 "restore during resume: %d\n", rc);
8060 /* if it is undefined, don't attempt to restore it.
8061 * KEEP THIS LAST */
8062 if (tp_features.fan_ctrl_status_undef)
8063 fan_control_resume_level = 0;
8066 static void fan_resume(void)
8068 u8 current_level = 7;
8069 bool do_set = false;
8070 int rc;
8072 /* DSDT *always* updates status on resume */
8073 tp_features.fan_ctrl_status_undef = 0;
8075 if (!fan_control_allowed ||
8076 !fan_control_resume_level ||
8077 (fan_get_status_safe(&current_level) < 0))
8078 return;
8080 switch (fan_control_access_mode) {
8081 case TPACPI_FAN_WR_ACPI_SFAN:
8082 /* never decrease fan level */
8083 do_set = (fan_control_resume_level > current_level);
8084 break;
8085 case TPACPI_FAN_WR_ACPI_FANS:
8086 case TPACPI_FAN_WR_TPEC:
8087 /* never decrease fan level, scale is:
8088 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8090 * We expect the firmware to set either 7 or AUTO, but we
8091 * handle FULLSPEED out of paranoia.
8093 * So, we can safely only restore FULLSPEED or 7, anything
8094 * else could slow the fan. Restoring AUTO is useless, at
8095 * best that's exactly what the DSDT already set (it is the
8096 * slower it uses).
8098 * Always keep in mind that the DSDT *will* have set the
8099 * fans to what the vendor supposes is the best level. We
8100 * muck with it only to speed the fan up.
8102 if (fan_control_resume_level != 7 &&
8103 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8104 return;
8105 else
8106 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8107 (current_level != fan_control_resume_level);
8108 break;
8109 default:
8110 return;
8112 if (do_set) {
8113 printk(TPACPI_NOTICE
8114 "restoring fan level to 0x%02x\n",
8115 fan_control_resume_level);
8116 rc = fan_set_level_safe(fan_control_resume_level);
8117 if (rc < 0)
8118 printk(TPACPI_NOTICE
8119 "failed to restore fan level: %d\n", rc);
8123 static int fan_read(struct seq_file *m)
8125 int rc;
8126 u8 status;
8127 unsigned int speed = 0;
8129 switch (fan_status_access_mode) {
8130 case TPACPI_FAN_RD_ACPI_GFAN:
8131 /* 570, 600e/x, 770e, 770x */
8132 rc = fan_get_status_safe(&status);
8133 if (rc < 0)
8134 return rc;
8136 seq_printf(m, "status:\t\t%s\n"
8137 "level:\t\t%d\n",
8138 (status != 0) ? "enabled" : "disabled", status);
8139 break;
8141 case TPACPI_FAN_RD_TPEC:
8142 /* all except 570, 600e/x, 770e, 770x */
8143 rc = fan_get_status_safe(&status);
8144 if (rc < 0)
8145 return rc;
8147 seq_printf(m, "status:\t\t%s\n",
8148 (status != 0) ? "enabled" : "disabled");
8150 rc = fan_get_speed(&speed);
8151 if (rc < 0)
8152 return rc;
8154 seq_printf(m, "speed:\t\t%d\n", speed);
8156 if (status & TP_EC_FAN_FULLSPEED)
8157 /* Disengaged mode takes precedence */
8158 seq_printf(m, "level:\t\tdisengaged\n");
8159 else if (status & TP_EC_FAN_AUTO)
8160 seq_printf(m, "level:\t\tauto\n");
8161 else
8162 seq_printf(m, "level:\t\t%d\n", status);
8163 break;
8165 case TPACPI_FAN_NONE:
8166 default:
8167 seq_printf(m, "status:\t\tnot supported\n");
8170 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8171 seq_printf(m, "commands:\tlevel <level>");
8173 switch (fan_control_access_mode) {
8174 case TPACPI_FAN_WR_ACPI_SFAN:
8175 seq_printf(m, " (<level> is 0-7)\n");
8176 break;
8178 default:
8179 seq_printf(m, " (<level> is 0-7, "
8180 "auto, disengaged, full-speed)\n");
8181 break;
8185 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8186 seq_printf(m, "commands:\tenable, disable\n"
8187 "commands:\twatchdog <timeout> (<timeout> "
8188 "is 0 (off), 1-120 (seconds))\n");
8190 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8191 seq_printf(m, "commands:\tspeed <speed>"
8192 " (<speed> is 0-65535)\n");
8194 return 0;
8197 static int fan_write_cmd_level(const char *cmd, int *rc)
8199 int level;
8201 if (strlencmp(cmd, "level auto") == 0)
8202 level = TP_EC_FAN_AUTO;
8203 else if ((strlencmp(cmd, "level disengaged") == 0) |
8204 (strlencmp(cmd, "level full-speed") == 0))
8205 level = TP_EC_FAN_FULLSPEED;
8206 else if (sscanf(cmd, "level %d", &level) != 1)
8207 return 0;
8209 *rc = fan_set_level_safe(level);
8210 if (*rc == -ENXIO)
8211 printk(TPACPI_ERR "level command accepted for unsupported "
8212 "access mode %d", fan_control_access_mode);
8213 else if (!*rc)
8214 tpacpi_disclose_usertask("procfs fan",
8215 "set level to %d\n", level);
8217 return 1;
8220 static int fan_write_cmd_enable(const char *cmd, int *rc)
8222 if (strlencmp(cmd, "enable") != 0)
8223 return 0;
8225 *rc = fan_set_enable();
8226 if (*rc == -ENXIO)
8227 printk(TPACPI_ERR "enable command accepted for unsupported "
8228 "access mode %d", fan_control_access_mode);
8229 else if (!*rc)
8230 tpacpi_disclose_usertask("procfs fan", "enable\n");
8232 return 1;
8235 static int fan_write_cmd_disable(const char *cmd, int *rc)
8237 if (strlencmp(cmd, "disable") != 0)
8238 return 0;
8240 *rc = fan_set_disable();
8241 if (*rc == -ENXIO)
8242 printk(TPACPI_ERR "disable command accepted for unsupported "
8243 "access mode %d", fan_control_access_mode);
8244 else if (!*rc)
8245 tpacpi_disclose_usertask("procfs fan", "disable\n");
8247 return 1;
8250 static int fan_write_cmd_speed(const char *cmd, int *rc)
8252 int speed;
8254 /* TODO:
8255 * Support speed <low> <medium> <high> ? */
8257 if (sscanf(cmd, "speed %d", &speed) != 1)
8258 return 0;
8260 *rc = fan_set_speed(speed);
8261 if (*rc == -ENXIO)
8262 printk(TPACPI_ERR "speed command accepted for unsupported "
8263 "access mode %d", fan_control_access_mode);
8264 else if (!*rc)
8265 tpacpi_disclose_usertask("procfs fan",
8266 "set speed to %d\n", speed);
8268 return 1;
8271 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8273 int interval;
8275 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8276 return 0;
8278 if (interval < 0 || interval > 120)
8279 *rc = -EINVAL;
8280 else {
8281 fan_watchdog_maxinterval = interval;
8282 tpacpi_disclose_usertask("procfs fan",
8283 "set watchdog timer to %d\n",
8284 interval);
8287 return 1;
8290 static int fan_write(char *buf)
8292 char *cmd;
8293 int rc = 0;
8295 while (!rc && (cmd = next_cmd(&buf))) {
8296 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8297 fan_write_cmd_level(cmd, &rc)) &&
8298 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8299 (fan_write_cmd_enable(cmd, &rc) ||
8300 fan_write_cmd_disable(cmd, &rc) ||
8301 fan_write_cmd_watchdog(cmd, &rc))) &&
8302 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8303 fan_write_cmd_speed(cmd, &rc))
8305 rc = -EINVAL;
8306 else if (!rc)
8307 fan_watchdog_reset();
8310 return rc;
8313 static struct ibm_struct fan_driver_data = {
8314 .name = "fan",
8315 .read = fan_read,
8316 .write = fan_write,
8317 .exit = fan_exit,
8318 .suspend = fan_suspend,
8319 .resume = fan_resume,
8322 /****************************************************************************
8323 ****************************************************************************
8325 * Infrastructure
8327 ****************************************************************************
8328 ****************************************************************************/
8331 * HKEY event callout for other subdrivers go here
8332 * (yes, it is ugly, but it is quick, safe, and gets the job done
8334 static void tpacpi_driver_event(const unsigned int hkey_event)
8336 if (ibm_backlight_device) {
8337 switch (hkey_event) {
8338 case TP_HKEY_EV_BRGHT_UP:
8339 case TP_HKEY_EV_BRGHT_DOWN:
8340 tpacpi_brightness_notify_change();
8343 if (alsa_card) {
8344 switch (hkey_event) {
8345 case TP_HKEY_EV_VOL_UP:
8346 case TP_HKEY_EV_VOL_DOWN:
8347 case TP_HKEY_EV_VOL_MUTE:
8348 volume_alsa_notify_change();
8353 static void hotkey_driver_event(const unsigned int scancode)
8355 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8358 /* sysfs name ---------------------------------------------------------- */
8359 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8360 struct device_attribute *attr,
8361 char *buf)
8363 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8366 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8367 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8369 /* --------------------------------------------------------------------- */
8371 /* /proc support */
8372 static struct proc_dir_entry *proc_dir;
8375 * Module and infrastructure proble, init and exit handling
8378 static int force_load;
8380 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8381 static const char * __init str_supported(int is_supported)
8383 static char text_unsupported[] __initdata = "not supported";
8385 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8387 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8389 static void ibm_exit(struct ibm_struct *ibm)
8391 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8393 list_del_init(&ibm->all_drivers);
8395 if (ibm->flags.acpi_notify_installed) {
8396 dbg_printk(TPACPI_DBG_EXIT,
8397 "%s: acpi_remove_notify_handler\n", ibm->name);
8398 BUG_ON(!ibm->acpi);
8399 acpi_remove_notify_handler(*ibm->acpi->handle,
8400 ibm->acpi->type,
8401 dispatch_acpi_notify);
8402 ibm->flags.acpi_notify_installed = 0;
8403 ibm->flags.acpi_notify_installed = 0;
8406 if (ibm->flags.proc_created) {
8407 dbg_printk(TPACPI_DBG_EXIT,
8408 "%s: remove_proc_entry\n", ibm->name);
8409 remove_proc_entry(ibm->name, proc_dir);
8410 ibm->flags.proc_created = 0;
8413 if (ibm->flags.acpi_driver_registered) {
8414 dbg_printk(TPACPI_DBG_EXIT,
8415 "%s: acpi_bus_unregister_driver\n", ibm->name);
8416 BUG_ON(!ibm->acpi);
8417 acpi_bus_unregister_driver(ibm->acpi->driver);
8418 kfree(ibm->acpi->driver);
8419 ibm->acpi->driver = NULL;
8420 ibm->flags.acpi_driver_registered = 0;
8423 if (ibm->flags.init_called && ibm->exit) {
8424 ibm->exit();
8425 ibm->flags.init_called = 0;
8428 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8431 static int __init ibm_init(struct ibm_init_struct *iibm)
8433 int ret;
8434 struct ibm_struct *ibm = iibm->data;
8435 struct proc_dir_entry *entry;
8437 BUG_ON(ibm == NULL);
8439 INIT_LIST_HEAD(&ibm->all_drivers);
8441 if (ibm->flags.experimental && !experimental)
8442 return 0;
8444 dbg_printk(TPACPI_DBG_INIT,
8445 "probing for %s\n", ibm->name);
8447 if (iibm->init) {
8448 ret = iibm->init(iibm);
8449 if (ret > 0)
8450 return 0; /* probe failed */
8451 if (ret)
8452 return ret;
8454 ibm->flags.init_called = 1;
8457 if (ibm->acpi) {
8458 if (ibm->acpi->hid) {
8459 ret = register_tpacpi_subdriver(ibm);
8460 if (ret)
8461 goto err_out;
8464 if (ibm->acpi->notify) {
8465 ret = setup_acpi_notify(ibm);
8466 if (ret == -ENODEV) {
8467 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8468 ibm->name);
8469 ret = 0;
8470 goto err_out;
8472 if (ret < 0)
8473 goto err_out;
8477 dbg_printk(TPACPI_DBG_INIT,
8478 "%s installed\n", ibm->name);
8480 if (ibm->read) {
8481 mode_t mode;
8483 mode = S_IRUGO;
8484 if (ibm->write)
8485 mode |= S_IWUSR;
8486 entry = proc_create_data(ibm->name, mode, proc_dir,
8487 &dispatch_proc_fops, ibm);
8488 if (!entry) {
8489 printk(TPACPI_ERR "unable to create proc entry %s\n",
8490 ibm->name);
8491 ret = -ENODEV;
8492 goto err_out;
8494 ibm->flags.proc_created = 1;
8497 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8499 return 0;
8501 err_out:
8502 dbg_printk(TPACPI_DBG_INIT,
8503 "%s: at error exit path with result %d\n",
8504 ibm->name, ret);
8506 ibm_exit(ibm);
8507 return (ret < 0)? ret : 0;
8510 /* Probing */
8512 static bool __pure __init tpacpi_is_fw_digit(const char c)
8514 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8517 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8518 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8519 const char t)
8521 return s && strlen(s) >= 8 &&
8522 tpacpi_is_fw_digit(s[0]) &&
8523 tpacpi_is_fw_digit(s[1]) &&
8524 s[2] == t && s[3] == 'T' &&
8525 tpacpi_is_fw_digit(s[4]) &&
8526 tpacpi_is_fw_digit(s[5]) &&
8527 s[6] == 'W' && s[7] == 'W';
8530 /* returns 0 - probe ok, or < 0 - probe error.
8531 * Probe ok doesn't mean thinkpad found.
8532 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8533 static int __must_check __init get_thinkpad_model_data(
8534 struct thinkpad_id_data *tp)
8536 const struct dmi_device *dev = NULL;
8537 char ec_fw_string[18];
8538 char const *s;
8540 if (!tp)
8541 return -EINVAL;
8543 memset(tp, 0, sizeof(*tp));
8545 if (dmi_name_in_vendors("IBM"))
8546 tp->vendor = PCI_VENDOR_ID_IBM;
8547 else if (dmi_name_in_vendors("LENOVO"))
8548 tp->vendor = PCI_VENDOR_ID_LENOVO;
8549 else
8550 return 0;
8552 s = dmi_get_system_info(DMI_BIOS_VERSION);
8553 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8554 if (s && !tp->bios_version_str)
8555 return -ENOMEM;
8557 /* Really ancient ThinkPad 240X will fail this, which is fine */
8558 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8559 return 0;
8561 tp->bios_model = tp->bios_version_str[0]
8562 | (tp->bios_version_str[1] << 8);
8563 tp->bios_release = (tp->bios_version_str[4] << 8)
8564 | tp->bios_version_str[5];
8567 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8568 * X32 or newer, all Z series; Some models must have an
8569 * up-to-date BIOS or they will not be detected.
8571 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8573 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8574 if (sscanf(dev->name,
8575 "IBM ThinkPad Embedded Controller -[%17c",
8576 ec_fw_string) == 1) {
8577 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8578 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8580 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8581 if (!tp->ec_version_str)
8582 return -ENOMEM;
8584 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8585 tp->ec_model = ec_fw_string[0]
8586 | (ec_fw_string[1] << 8);
8587 tp->ec_release = (ec_fw_string[4] << 8)
8588 | ec_fw_string[5];
8589 } else {
8590 printk(TPACPI_NOTICE
8591 "ThinkPad firmware release %s "
8592 "doesn't match the known patterns\n",
8593 ec_fw_string);
8594 printk(TPACPI_NOTICE
8595 "please report this to %s\n",
8596 TPACPI_MAIL);
8598 break;
8602 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8603 if (s && !strnicmp(s, "ThinkPad", 8)) {
8604 tp->model_str = kstrdup(s, GFP_KERNEL);
8605 if (!tp->model_str)
8606 return -ENOMEM;
8609 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8610 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8611 if (s && !tp->nummodel_str)
8612 return -ENOMEM;
8614 return 0;
8617 static int __init probe_for_thinkpad(void)
8619 int is_thinkpad;
8621 if (acpi_disabled)
8622 return -ENODEV;
8625 * Non-ancient models have better DMI tagging, but very old models
8626 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8628 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8629 (thinkpad_id.ec_model != 0) ||
8630 tpacpi_is_fw_known();
8632 /* ec is required because many other handles are relative to it */
8633 TPACPI_ACPIHANDLE_INIT(ec);
8634 if (!ec_handle) {
8635 if (is_thinkpad)
8636 printk(TPACPI_ERR
8637 "Not yet supported ThinkPad detected!\n");
8638 return -ENODEV;
8641 if (!is_thinkpad && !force_load)
8642 return -ENODEV;
8644 return 0;
8648 /* Module init, exit, parameters */
8650 static struct ibm_init_struct ibms_init[] __initdata = {
8652 .init = thinkpad_acpi_driver_init,
8653 .data = &thinkpad_acpi_driver_data,
8656 .init = hotkey_init,
8657 .data = &hotkey_driver_data,
8660 .init = bluetooth_init,
8661 .data = &bluetooth_driver_data,
8664 .init = wan_init,
8665 .data = &wan_driver_data,
8668 .init = uwb_init,
8669 .data = &uwb_driver_data,
8671 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8673 .init = video_init,
8674 .data = &video_driver_data,
8676 #endif
8678 .init = light_init,
8679 .data = &light_driver_data,
8682 .init = cmos_init,
8683 .data = &cmos_driver_data,
8686 .init = led_init,
8687 .data = &led_driver_data,
8690 .init = beep_init,
8691 .data = &beep_driver_data,
8694 .init = thermal_init,
8695 .data = &thermal_driver_data,
8698 .data = &ecdump_driver_data,
8701 .init = brightness_init,
8702 .data = &brightness_driver_data,
8705 .init = volume_init,
8706 .data = &volume_driver_data,
8709 .init = fan_init,
8710 .data = &fan_driver_data,
8714 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8716 unsigned int i;
8717 struct ibm_struct *ibm;
8719 if (!kp || !kp->name || !val)
8720 return -EINVAL;
8722 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8723 ibm = ibms_init[i].data;
8724 WARN_ON(ibm == NULL);
8726 if (!ibm || !ibm->name)
8727 continue;
8729 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8730 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8731 return -ENOSPC;
8732 strcpy(ibms_init[i].param, val);
8733 strcat(ibms_init[i].param, ",");
8734 return 0;
8738 return -EINVAL;
8741 module_param(experimental, int, 0444);
8742 MODULE_PARM_DESC(experimental,
8743 "Enables experimental features when non-zero");
8745 module_param_named(debug, dbg_level, uint, 0);
8746 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8748 module_param(force_load, bool, 0444);
8749 MODULE_PARM_DESC(force_load,
8750 "Attempts to load the driver even on a "
8751 "mis-identified ThinkPad when true");
8753 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8754 MODULE_PARM_DESC(fan_control,
8755 "Enables setting fan parameters features when true");
8757 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8758 MODULE_PARM_DESC(brightness_mode,
8759 "Selects brightness control strategy: "
8760 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8762 module_param(brightness_enable, uint, 0444);
8763 MODULE_PARM_DESC(brightness_enable,
8764 "Enables backlight control when 1, disables when 0");
8766 module_param(hotkey_report_mode, uint, 0444);
8767 MODULE_PARM_DESC(hotkey_report_mode,
8768 "used for backwards compatibility with userspace, "
8769 "see documentation");
8771 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8772 module_param_named(volume_mode, volume_mode, uint, 0444);
8773 MODULE_PARM_DESC(volume_mode,
8774 "Selects volume control strategy: "
8775 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8777 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8778 MODULE_PARM_DESC(volume_capabilities,
8779 "Selects the mixer capabilites: "
8780 "0=auto, 1=volume and mute, 2=mute only");
8782 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8783 MODULE_PARM_DESC(volume_control,
8784 "Enables software override for the console audio "
8785 "control when true");
8787 /* ALSA module API parameters */
8788 module_param_named(index, alsa_index, int, 0444);
8789 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8790 module_param_named(id, alsa_id, charp, 0444);
8791 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8792 module_param_named(enable, alsa_enable, bool, 0444);
8793 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8794 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8796 #define TPACPI_PARAM(feature) \
8797 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8798 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8799 "at module load, see documentation")
8801 TPACPI_PARAM(hotkey);
8802 TPACPI_PARAM(bluetooth);
8803 TPACPI_PARAM(video);
8804 TPACPI_PARAM(light);
8805 TPACPI_PARAM(cmos);
8806 TPACPI_PARAM(led);
8807 TPACPI_PARAM(beep);
8808 TPACPI_PARAM(ecdump);
8809 TPACPI_PARAM(brightness);
8810 TPACPI_PARAM(volume);
8811 TPACPI_PARAM(fan);
8813 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8814 module_param(dbg_wlswemul, uint, 0444);
8815 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8816 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8817 MODULE_PARM_DESC(wlsw_state,
8818 "Initial state of the emulated WLSW switch");
8820 module_param(dbg_bluetoothemul, uint, 0444);
8821 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8822 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8823 MODULE_PARM_DESC(bluetooth_state,
8824 "Initial state of the emulated bluetooth switch");
8826 module_param(dbg_wwanemul, uint, 0444);
8827 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8828 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8829 MODULE_PARM_DESC(wwan_state,
8830 "Initial state of the emulated WWAN switch");
8832 module_param(dbg_uwbemul, uint, 0444);
8833 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8834 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8835 MODULE_PARM_DESC(uwb_state,
8836 "Initial state of the emulated UWB switch");
8837 #endif
8839 static void thinkpad_acpi_module_exit(void)
8841 struct ibm_struct *ibm, *itmp;
8843 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8845 list_for_each_entry_safe_reverse(ibm, itmp,
8846 &tpacpi_all_drivers,
8847 all_drivers) {
8848 ibm_exit(ibm);
8851 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8853 if (tpacpi_inputdev) {
8854 if (tp_features.input_device_registered)
8855 input_unregister_device(tpacpi_inputdev);
8856 else
8857 input_free_device(tpacpi_inputdev);
8860 if (tpacpi_hwmon)
8861 hwmon_device_unregister(tpacpi_hwmon);
8863 if (tp_features.sensors_pdev_attrs_registered)
8864 device_remove_file(&tpacpi_sensors_pdev->dev,
8865 &dev_attr_thinkpad_acpi_pdev_name);
8866 if (tpacpi_sensors_pdev)
8867 platform_device_unregister(tpacpi_sensors_pdev);
8868 if (tpacpi_pdev)
8869 platform_device_unregister(tpacpi_pdev);
8871 if (tp_features.sensors_pdrv_attrs_registered)
8872 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8873 if (tp_features.platform_drv_attrs_registered)
8874 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8876 if (tp_features.sensors_pdrv_registered)
8877 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8879 if (tp_features.platform_drv_registered)
8880 platform_driver_unregister(&tpacpi_pdriver);
8882 if (proc_dir)
8883 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8885 if (tpacpi_wq)
8886 destroy_workqueue(tpacpi_wq);
8888 kfree(thinkpad_id.bios_version_str);
8889 kfree(thinkpad_id.ec_version_str);
8890 kfree(thinkpad_id.model_str);
8894 static int __init thinkpad_acpi_module_init(void)
8896 int ret, i;
8898 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8900 /* Parameter checking */
8901 if (hotkey_report_mode > 2)
8902 return -EINVAL;
8904 /* Driver-level probe */
8906 ret = get_thinkpad_model_data(&thinkpad_id);
8907 if (ret) {
8908 printk(TPACPI_ERR
8909 "unable to get DMI data: %d\n", ret);
8910 thinkpad_acpi_module_exit();
8911 return ret;
8913 ret = probe_for_thinkpad();
8914 if (ret) {
8915 thinkpad_acpi_module_exit();
8916 return ret;
8919 /* Driver initialization */
8921 TPACPI_ACPIHANDLE_INIT(ecrd);
8922 TPACPI_ACPIHANDLE_INIT(ecwr);
8924 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8925 if (!tpacpi_wq) {
8926 thinkpad_acpi_module_exit();
8927 return -ENOMEM;
8930 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8931 if (!proc_dir) {
8932 printk(TPACPI_ERR
8933 "unable to create proc dir " TPACPI_PROC_DIR);
8934 thinkpad_acpi_module_exit();
8935 return -ENODEV;
8937 proc_dir->owner = THIS_MODULE;
8939 ret = platform_driver_register(&tpacpi_pdriver);
8940 if (ret) {
8941 printk(TPACPI_ERR
8942 "unable to register main platform driver\n");
8943 thinkpad_acpi_module_exit();
8944 return ret;
8946 tp_features.platform_drv_registered = 1;
8948 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8949 if (ret) {
8950 printk(TPACPI_ERR
8951 "unable to register hwmon platform driver\n");
8952 thinkpad_acpi_module_exit();
8953 return ret;
8955 tp_features.sensors_pdrv_registered = 1;
8957 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8958 if (!ret) {
8959 tp_features.platform_drv_attrs_registered = 1;
8960 ret = tpacpi_create_driver_attributes(
8961 &tpacpi_hwmon_pdriver.driver);
8963 if (ret) {
8964 printk(TPACPI_ERR
8965 "unable to create sysfs driver attributes\n");
8966 thinkpad_acpi_module_exit();
8967 return ret;
8969 tp_features.sensors_pdrv_attrs_registered = 1;
8972 /* Device initialization */
8973 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8974 NULL, 0);
8975 if (IS_ERR(tpacpi_pdev)) {
8976 ret = PTR_ERR(tpacpi_pdev);
8977 tpacpi_pdev = NULL;
8978 printk(TPACPI_ERR "unable to register platform device\n");
8979 thinkpad_acpi_module_exit();
8980 return ret;
8982 tpacpi_sensors_pdev = platform_device_register_simple(
8983 TPACPI_HWMON_DRVR_NAME,
8984 -1, NULL, 0);
8985 if (IS_ERR(tpacpi_sensors_pdev)) {
8986 ret = PTR_ERR(tpacpi_sensors_pdev);
8987 tpacpi_sensors_pdev = NULL;
8988 printk(TPACPI_ERR
8989 "unable to register hwmon platform device\n");
8990 thinkpad_acpi_module_exit();
8991 return ret;
8993 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8994 &dev_attr_thinkpad_acpi_pdev_name);
8995 if (ret) {
8996 printk(TPACPI_ERR
8997 "unable to create sysfs hwmon device attributes\n");
8998 thinkpad_acpi_module_exit();
8999 return ret;
9001 tp_features.sensors_pdev_attrs_registered = 1;
9002 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9003 if (IS_ERR(tpacpi_hwmon)) {
9004 ret = PTR_ERR(tpacpi_hwmon);
9005 tpacpi_hwmon = NULL;
9006 printk(TPACPI_ERR "unable to register hwmon device\n");
9007 thinkpad_acpi_module_exit();
9008 return ret;
9010 mutex_init(&tpacpi_inputdev_send_mutex);
9011 tpacpi_inputdev = input_allocate_device();
9012 if (!tpacpi_inputdev) {
9013 printk(TPACPI_ERR "unable to allocate input device\n");
9014 thinkpad_acpi_module_exit();
9015 return -ENOMEM;
9016 } else {
9017 /* Prepare input device, but don't register */
9018 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9019 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9020 tpacpi_inputdev->id.bustype = BUS_HOST;
9021 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9022 thinkpad_id.vendor :
9023 PCI_VENDOR_ID_IBM;
9024 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9025 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9026 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9028 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9029 ret = ibm_init(&ibms_init[i]);
9030 if (ret >= 0 && *ibms_init[i].param)
9031 ret = ibms_init[i].data->write(ibms_init[i].param);
9032 if (ret < 0) {
9033 thinkpad_acpi_module_exit();
9034 return ret;
9037 ret = input_register_device(tpacpi_inputdev);
9038 if (ret < 0) {
9039 printk(TPACPI_ERR "unable to register input device\n");
9040 thinkpad_acpi_module_exit();
9041 return ret;
9042 } else {
9043 tp_features.input_device_registered = 1;
9046 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9047 return 0;
9050 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9053 * This will autoload the driver in almost every ThinkPad
9054 * in widespread use.
9056 * Only _VERY_ old models, like the 240, 240x and 570 lack
9057 * the HKEY event interface.
9059 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9062 * DMI matching for module autoloading
9064 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9065 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9067 * Only models listed in thinkwiki will be supported, so add yours
9068 * if it is not there yet.
9070 #define IBM_BIOS_MODULE_ALIAS(__type) \
9071 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9073 /* Ancient thinkpad BIOSes have to be identified by
9074 * BIOS type or model number, and there are far less
9075 * BIOS types than model numbers... */
9076 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9078 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9079 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9080 MODULE_DESCRIPTION(TPACPI_DESC);
9081 MODULE_VERSION(TPACPI_VERSION);
9082 MODULE_LICENSE("GPL");
9084 module_init(thinkpad_acpi_module_init);
9085 module_exit(thinkpad_acpi_module_exit);