thinkpad-acpi: wrong thermal attribute_group removed in thermal_exit()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
bloba3f83257e2c80672508052f0040d91d6765a83fb
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:
2344 case TPACPI_LIFE_RUNNING:
2345 hotkey_poll_setup_safe(false);
2346 return 0;
2347 case TPACPI_LIFE_EXITING:
2348 return -EBUSY;
2351 /* Should only happen if tpacpi_lifecycle is corrupt */
2352 BUG();
2353 return -EBUSY;
2356 static void hotkey_inputdev_close(struct input_dev *dev)
2358 /* disable hotkey polling when possible */
2359 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2360 !(hotkey_source_mask & hotkey_driver_mask))
2361 hotkey_poll_setup_safe(false);
2364 /* sysfs hotkey enable ------------------------------------------------- */
2365 static ssize_t hotkey_enable_show(struct device *dev,
2366 struct device_attribute *attr,
2367 char *buf)
2369 int res, status;
2371 printk_deprecated_attribute("hotkey_enable",
2372 "Hotkey reporting is always enabled");
2374 res = hotkey_status_get(&status);
2375 if (res)
2376 return res;
2378 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2381 static ssize_t hotkey_enable_store(struct device *dev,
2382 struct device_attribute *attr,
2383 const char *buf, size_t count)
2385 unsigned long t;
2387 printk_deprecated_attribute("hotkey_enable",
2388 "Hotkeys can be disabled through hotkey_mask");
2390 if (parse_strtoul(buf, 1, &t))
2391 return -EINVAL;
2393 if (t == 0)
2394 return -EPERM;
2396 return count;
2399 static struct device_attribute dev_attr_hotkey_enable =
2400 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2401 hotkey_enable_show, hotkey_enable_store);
2403 /* sysfs hotkey mask --------------------------------------------------- */
2404 static ssize_t hotkey_mask_show(struct device *dev,
2405 struct device_attribute *attr,
2406 char *buf)
2408 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2411 static ssize_t hotkey_mask_store(struct device *dev,
2412 struct device_attribute *attr,
2413 const char *buf, size_t count)
2415 unsigned long t;
2416 int res;
2418 if (parse_strtoul(buf, 0xffffffffUL, &t))
2419 return -EINVAL;
2421 if (mutex_lock_killable(&hotkey_mutex))
2422 return -ERESTARTSYS;
2424 res = hotkey_user_mask_set(t);
2426 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2427 hotkey_poll_setup(true);
2428 #endif
2430 mutex_unlock(&hotkey_mutex);
2432 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2434 return (res) ? res : count;
2437 static struct device_attribute dev_attr_hotkey_mask =
2438 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2439 hotkey_mask_show, hotkey_mask_store);
2441 /* sysfs hotkey bios_enabled ------------------------------------------- */
2442 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2443 struct device_attribute *attr,
2444 char *buf)
2446 return sprintf(buf, "0\n");
2449 static struct device_attribute dev_attr_hotkey_bios_enabled =
2450 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2452 /* sysfs hotkey bios_mask ---------------------------------------------- */
2453 static ssize_t hotkey_bios_mask_show(struct device *dev,
2454 struct device_attribute *attr,
2455 char *buf)
2457 printk_deprecated_attribute("hotkey_bios_mask",
2458 "This attribute is useless.");
2459 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2462 static struct device_attribute dev_attr_hotkey_bios_mask =
2463 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2465 /* sysfs hotkey all_mask ----------------------------------------------- */
2466 static ssize_t hotkey_all_mask_show(struct device *dev,
2467 struct device_attribute *attr,
2468 char *buf)
2470 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2471 hotkey_all_mask | hotkey_source_mask);
2474 static struct device_attribute dev_attr_hotkey_all_mask =
2475 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2477 /* sysfs hotkey recommended_mask --------------------------------------- */
2478 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2479 struct device_attribute *attr,
2480 char *buf)
2482 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2483 (hotkey_all_mask | hotkey_source_mask)
2484 & ~hotkey_reserved_mask);
2487 static struct device_attribute dev_attr_hotkey_recommended_mask =
2488 __ATTR(hotkey_recommended_mask, S_IRUGO,
2489 hotkey_recommended_mask_show, NULL);
2491 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2493 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2494 static ssize_t hotkey_source_mask_show(struct device *dev,
2495 struct device_attribute *attr,
2496 char *buf)
2498 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2501 static ssize_t hotkey_source_mask_store(struct device *dev,
2502 struct device_attribute *attr,
2503 const char *buf, size_t count)
2505 unsigned long t;
2506 u32 r_ev;
2507 int rc;
2509 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2510 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2511 return -EINVAL;
2513 if (mutex_lock_killable(&hotkey_mutex))
2514 return -ERESTARTSYS;
2516 HOTKEY_CONFIG_CRITICAL_START
2517 hotkey_source_mask = t;
2518 HOTKEY_CONFIG_CRITICAL_END
2520 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2521 ~hotkey_source_mask);
2522 hotkey_poll_setup(true);
2524 /* check if events needed by the driver got disabled */
2525 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2526 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2528 mutex_unlock(&hotkey_mutex);
2530 if (rc < 0)
2531 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2532 "firmware event mask!\n");
2534 if (r_ev)
2535 printk(TPACPI_NOTICE "hotkey_source_mask: "
2536 "some important events were disabled: "
2537 "0x%04x\n", r_ev);
2539 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2541 return (rc < 0) ? rc : count;
2544 static struct device_attribute dev_attr_hotkey_source_mask =
2545 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2546 hotkey_source_mask_show, hotkey_source_mask_store);
2548 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2549 static ssize_t hotkey_poll_freq_show(struct device *dev,
2550 struct device_attribute *attr,
2551 char *buf)
2553 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2556 static ssize_t hotkey_poll_freq_store(struct device *dev,
2557 struct device_attribute *attr,
2558 const char *buf, size_t count)
2560 unsigned long t;
2562 if (parse_strtoul(buf, 25, &t))
2563 return -EINVAL;
2565 if (mutex_lock_killable(&hotkey_mutex))
2566 return -ERESTARTSYS;
2568 hotkey_poll_set_freq(t);
2569 hotkey_poll_setup(true);
2571 mutex_unlock(&hotkey_mutex);
2573 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2575 return count;
2578 static struct device_attribute dev_attr_hotkey_poll_freq =
2579 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2580 hotkey_poll_freq_show, hotkey_poll_freq_store);
2582 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2584 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2585 static ssize_t hotkey_radio_sw_show(struct device *dev,
2586 struct device_attribute *attr,
2587 char *buf)
2589 int res, s;
2590 res = hotkey_get_wlsw(&s);
2591 if (res < 0)
2592 return res;
2594 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2597 static struct device_attribute dev_attr_hotkey_radio_sw =
2598 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2600 static void hotkey_radio_sw_notify_change(void)
2602 if (tp_features.hotkey_wlsw)
2603 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2604 "hotkey_radio_sw");
2607 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2608 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2609 struct device_attribute *attr,
2610 char *buf)
2612 int res, s;
2613 res = hotkey_get_tablet_mode(&s);
2614 if (res < 0)
2615 return res;
2617 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2620 static struct device_attribute dev_attr_hotkey_tablet_mode =
2621 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2623 static void hotkey_tablet_mode_notify_change(void)
2625 if (tp_features.hotkey_tablet)
2626 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2627 "hotkey_tablet_mode");
2630 /* sysfs hotkey report_mode -------------------------------------------- */
2631 static ssize_t hotkey_report_mode_show(struct device *dev,
2632 struct device_attribute *attr,
2633 char *buf)
2635 return snprintf(buf, PAGE_SIZE, "%d\n",
2636 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2639 static struct device_attribute dev_attr_hotkey_report_mode =
2640 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2642 /* sysfs wakeup reason (pollable) -------------------------------------- */
2643 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2644 struct device_attribute *attr,
2645 char *buf)
2647 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2650 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2651 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2653 static void hotkey_wakeup_reason_notify_change(void)
2655 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2656 "wakeup_reason");
2659 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2660 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2661 struct device_attribute *attr,
2662 char *buf)
2664 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2667 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2668 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2669 hotkey_wakeup_hotunplug_complete_show, NULL);
2671 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2673 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2674 "wakeup_hotunplug_complete");
2677 /* --------------------------------------------------------------------- */
2679 static struct attribute *hotkey_attributes[] __initdata = {
2680 &dev_attr_hotkey_enable.attr,
2681 &dev_attr_hotkey_bios_enabled.attr,
2682 &dev_attr_hotkey_bios_mask.attr,
2683 &dev_attr_hotkey_report_mode.attr,
2684 &dev_attr_hotkey_wakeup_reason.attr,
2685 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2686 &dev_attr_hotkey_mask.attr,
2687 &dev_attr_hotkey_all_mask.attr,
2688 &dev_attr_hotkey_recommended_mask.attr,
2689 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2690 &dev_attr_hotkey_source_mask.attr,
2691 &dev_attr_hotkey_poll_freq.attr,
2692 #endif
2695 static void bluetooth_update_rfk(void);
2696 static void wan_update_rfk(void);
2697 static void uwb_update_rfk(void);
2698 static void tpacpi_send_radiosw_update(void)
2700 int wlsw;
2702 /* Sync these BEFORE sending any rfkill events */
2703 if (tp_features.bluetooth)
2704 bluetooth_update_rfk();
2705 if (tp_features.wan)
2706 wan_update_rfk();
2707 if (tp_features.uwb)
2708 uwb_update_rfk();
2710 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2711 mutex_lock(&tpacpi_inputdev_send_mutex);
2713 input_report_switch(tpacpi_inputdev,
2714 SW_RFKILL_ALL, !!wlsw);
2715 input_sync(tpacpi_inputdev);
2717 mutex_unlock(&tpacpi_inputdev_send_mutex);
2719 hotkey_radio_sw_notify_change();
2722 static void hotkey_exit(void)
2724 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2725 mutex_lock(&hotkey_mutex);
2726 hotkey_poll_stop_sync();
2727 mutex_unlock(&hotkey_mutex);
2728 #endif
2730 if (hotkey_dev_attributes)
2731 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2733 kfree(hotkey_keycode_map);
2735 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2736 "restoring original HKEY status and mask\n");
2737 /* yes, there is a bitwise or below, we want the
2738 * functions to be called even if one of them fail */
2739 if (((tp_features.hotkey_mask &&
2740 hotkey_mask_set(hotkey_orig_mask)) |
2741 hotkey_status_set(false)) != 0)
2742 printk(TPACPI_ERR
2743 "failed to restore hot key mask "
2744 "to BIOS defaults\n");
2747 static void __init hotkey_unmap(const unsigned int scancode)
2749 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2750 clear_bit(hotkey_keycode_map[scancode],
2751 tpacpi_inputdev->keybit);
2752 hotkey_keycode_map[scancode] = KEY_RESERVED;
2757 * HKEY quirks:
2758 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2761 #define TPACPI_HK_Q_INIMASK 0x0001
2763 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2764 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2765 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2766 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2767 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2768 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2769 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2770 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2771 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2772 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2773 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2774 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2775 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2776 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2777 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2778 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2779 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2780 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2781 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2782 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2785 static int __init hotkey_init(struct ibm_init_struct *iibm)
2787 /* Requirements for changing the default keymaps:
2789 * 1. Many of the keys are mapped to KEY_RESERVED for very
2790 * good reasons. Do not change them unless you have deep
2791 * knowledge on the IBM and Lenovo ThinkPad firmware for
2792 * the various ThinkPad models. The driver behaves
2793 * differently for KEY_RESERVED: such keys have their
2794 * hot key mask *unset* in mask_recommended, and also
2795 * in the initial hot key mask programmed into the
2796 * firmware at driver load time, which means the firm-
2797 * ware may react very differently if you change them to
2798 * something else;
2800 * 2. You must be subscribed to the linux-thinkpad and
2801 * ibm-acpi-devel mailing lists, and you should read the
2802 * list archives since 2007 if you want to change the
2803 * keymaps. This requirement exists so that you will
2804 * know the past history of problems with the thinkpad-
2805 * acpi driver keymaps, and also that you will be
2806 * listening to any bug reports;
2808 * 3. Do not send thinkpad-acpi specific patches directly to
2809 * for merging, *ever*. Send them to the linux-acpi
2810 * mailinglist for comments. Merging is to be done only
2811 * through acpi-test and the ACPI maintainer.
2813 * If the above is too much to ask, don't change the keymap.
2814 * Ask the thinkpad-acpi maintainer to do it, instead.
2816 static u16 ibm_keycode_map[] __initdata = {
2817 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2818 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2819 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2820 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2822 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2823 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2824 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2825 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2827 /* brightness: firmware always reacts to them */
2828 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2829 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2831 /* Thinklight: firmware always react to it */
2832 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2834 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2835 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2837 /* Volume: firmware always react to it and reprograms
2838 * the built-in *extra* mixer. Never map it to control
2839 * another mixer by default. */
2840 KEY_RESERVED, /* 0x14: VOLUME UP */
2841 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2842 KEY_RESERVED, /* 0x16: MUTE */
2844 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2846 /* (assignments unknown, please report if found) */
2847 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2848 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2850 static u16 lenovo_keycode_map[] __initdata = {
2851 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2852 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2853 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2854 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2856 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2857 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2858 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2859 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2861 /* These should be enabled --only-- when ACPI video
2862 * is disabled (i.e. in "vendor" mode), and are handled
2863 * in a special way by the init code */
2864 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2865 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2867 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2869 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2870 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2872 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2873 * react to it and reprograms the built-in *extra* mixer.
2874 * Never map it to control another mixer by default.
2876 * T60?, T61, R60?, R61: firmware and EC tries to send
2877 * these over the regular keyboard, so these are no-ops,
2878 * but there are still weird bugs re. MUTE, so do not
2879 * change unless you get test reports from all Lenovo
2880 * models. May cause the BIOS to interfere with the
2881 * HDA mixer.
2883 KEY_RESERVED, /* 0x14: VOLUME UP */
2884 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2885 KEY_RESERVED, /* 0x16: MUTE */
2887 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2889 /* (assignments unknown, please report if found) */
2890 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2891 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2894 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2895 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2896 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2898 int res, i;
2899 int status;
2900 int hkeyv;
2901 bool radiosw_state = false;
2902 bool tabletsw_state = false;
2904 unsigned long quirks;
2906 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2907 "initializing hotkey subdriver\n");
2909 BUG_ON(!tpacpi_inputdev);
2910 BUG_ON(tpacpi_inputdev->open != NULL ||
2911 tpacpi_inputdev->close != NULL);
2913 TPACPI_ACPIHANDLE_INIT(hkey);
2914 mutex_init(&hotkey_mutex);
2916 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2917 mutex_init(&hotkey_thread_mutex);
2918 mutex_init(&hotkey_thread_data_mutex);
2919 #endif
2921 /* hotkey not supported on 570 */
2922 tp_features.hotkey = hkey_handle != NULL;
2924 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2925 "hotkeys are %s\n",
2926 str_supported(tp_features.hotkey));
2928 if (!tp_features.hotkey)
2929 return 1;
2931 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2932 ARRAY_SIZE(tpacpi_hotkey_qtable));
2934 tpacpi_disable_brightness_delay();
2936 /* MUST have enough space for all attributes to be added to
2937 * hotkey_dev_attributes */
2938 hotkey_dev_attributes = create_attr_set(
2939 ARRAY_SIZE(hotkey_attributes) + 2,
2940 NULL);
2941 if (!hotkey_dev_attributes)
2942 return -ENOMEM;
2943 res = add_many_to_attr_set(hotkey_dev_attributes,
2944 hotkey_attributes,
2945 ARRAY_SIZE(hotkey_attributes));
2946 if (res)
2947 goto err_exit;
2949 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2950 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2951 for HKEY interface version 0x100 */
2952 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2953 if ((hkeyv >> 8) != 1) {
2954 printk(TPACPI_ERR "unknown version of the "
2955 "HKEY interface: 0x%x\n", hkeyv);
2956 printk(TPACPI_ERR "please report this to %s\n",
2957 TPACPI_MAIL);
2958 } else {
2960 * MHKV 0x100 in A31, R40, R40e,
2961 * T4x, X31, and later
2963 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2964 "firmware HKEY interface version: 0x%x\n",
2965 hkeyv);
2967 /* Paranoia check AND init hotkey_all_mask */
2968 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2969 "MHKA", "qd")) {
2970 printk(TPACPI_ERR
2971 "missing MHKA handler, "
2972 "please report this to %s\n",
2973 TPACPI_MAIL);
2974 /* Fallback: pre-init for FN+F3,F4,F12 */
2975 hotkey_all_mask = 0x080cU;
2976 } else {
2977 tp_features.hotkey_mask = 1;
2982 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2983 "hotkey masks are %s\n",
2984 str_supported(tp_features.hotkey_mask));
2986 /* Init hotkey_all_mask if not initialized yet */
2987 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2988 (quirks & TPACPI_HK_Q_INIMASK))
2989 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2991 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2992 if (tp_features.hotkey_mask) {
2993 /* hotkey_source_mask *must* be zero for
2994 * the first hotkey_mask_get to return hotkey_orig_mask */
2995 res = hotkey_mask_get();
2996 if (res)
2997 goto err_exit;
2999 hotkey_orig_mask = hotkey_acpi_mask;
3000 } else {
3001 hotkey_orig_mask = hotkey_all_mask;
3002 hotkey_acpi_mask = hotkey_all_mask;
3005 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3006 if (dbg_wlswemul) {
3007 tp_features.hotkey_wlsw = 1;
3008 radiosw_state = !!tpacpi_wlsw_emulstate;
3009 printk(TPACPI_INFO
3010 "radio switch emulation enabled\n");
3011 } else
3012 #endif
3013 /* Not all thinkpads have a hardware radio switch */
3014 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3015 tp_features.hotkey_wlsw = 1;
3016 radiosw_state = !!status;
3017 printk(TPACPI_INFO
3018 "radio switch found; radios are %s\n",
3019 enabled(status, 0));
3021 if (tp_features.hotkey_wlsw)
3022 res = add_to_attr_set(hotkey_dev_attributes,
3023 &dev_attr_hotkey_radio_sw.attr);
3025 /* For X41t, X60t, X61t Tablets... */
3026 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3027 tp_features.hotkey_tablet = 1;
3028 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3029 printk(TPACPI_INFO
3030 "possible tablet mode switch found; "
3031 "ThinkPad in %s mode\n",
3032 (tabletsw_state) ? "tablet" : "laptop");
3033 res = add_to_attr_set(hotkey_dev_attributes,
3034 &dev_attr_hotkey_tablet_mode.attr);
3037 if (!res)
3038 res = register_attr_set_with_sysfs(
3039 hotkey_dev_attributes,
3040 &tpacpi_pdev->dev.kobj);
3041 if (res)
3042 goto err_exit;
3044 /* Set up key map */
3046 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3047 GFP_KERNEL);
3048 if (!hotkey_keycode_map) {
3049 printk(TPACPI_ERR
3050 "failed to allocate memory for key map\n");
3051 res = -ENOMEM;
3052 goto err_exit;
3055 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3056 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3057 "using Lenovo default hot key map\n");
3058 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3059 TPACPI_HOTKEY_MAP_SIZE);
3060 } else {
3061 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3062 "using IBM default hot key map\n");
3063 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3064 TPACPI_HOTKEY_MAP_SIZE);
3067 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3068 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3069 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3070 tpacpi_inputdev->keycode = hotkey_keycode_map;
3071 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3072 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3073 input_set_capability(tpacpi_inputdev, EV_KEY,
3074 hotkey_keycode_map[i]);
3075 } else {
3076 if (i < sizeof(hotkey_reserved_mask)*8)
3077 hotkey_reserved_mask |= 1 << i;
3081 if (tp_features.hotkey_wlsw) {
3082 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3083 input_report_switch(tpacpi_inputdev,
3084 SW_RFKILL_ALL, radiosw_state);
3086 if (tp_features.hotkey_tablet) {
3087 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3088 input_report_switch(tpacpi_inputdev,
3089 SW_TABLET_MODE, tabletsw_state);
3092 /* Do not issue duplicate brightness change events to
3093 * userspace */
3094 if (!tp_features.bright_acpimode)
3095 /* update bright_acpimode... */
3096 tpacpi_check_std_acpi_brightness_support();
3098 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3099 printk(TPACPI_INFO
3100 "This ThinkPad has standard ACPI backlight "
3101 "brightness control, supported by the ACPI "
3102 "video driver\n");
3103 printk(TPACPI_NOTICE
3104 "Disabling thinkpad-acpi brightness events "
3105 "by default...\n");
3107 /* Disable brightness up/down on Lenovo thinkpads when
3108 * ACPI is handling them, otherwise it is plain impossible
3109 * for userspace to do something even remotely sane */
3110 hotkey_reserved_mask |=
3111 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3112 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3113 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3114 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3117 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3118 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3119 & ~hotkey_all_mask
3120 & ~hotkey_reserved_mask;
3122 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3123 "hotkey source mask 0x%08x, polling freq %u\n",
3124 hotkey_source_mask, hotkey_poll_freq);
3125 #endif
3127 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3128 "enabling firmware HKEY event interface...\n");
3129 res = hotkey_status_set(true);
3130 if (res) {
3131 hotkey_exit();
3132 return res;
3134 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3135 | hotkey_driver_mask)
3136 & ~hotkey_source_mask);
3137 if (res < 0 && res != -ENXIO) {
3138 hotkey_exit();
3139 return res;
3141 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3142 & ~hotkey_reserved_mask;
3143 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3144 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3145 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3147 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3148 "legacy ibm/hotkey event reporting over procfs %s\n",
3149 (hotkey_report_mode < 2) ?
3150 "enabled" : "disabled");
3152 tpacpi_inputdev->open = &hotkey_inputdev_open;
3153 tpacpi_inputdev->close = &hotkey_inputdev_close;
3155 hotkey_poll_setup_safe(true);
3157 return 0;
3159 err_exit:
3160 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3161 hotkey_dev_attributes = NULL;
3163 return (res < 0)? res : 1;
3166 static bool hotkey_notify_hotkey(const u32 hkey,
3167 bool *send_acpi_ev,
3168 bool *ignore_acpi_ev)
3170 /* 0x1000-0x1FFF: key presses */
3171 unsigned int scancode = hkey & 0xfff;
3172 *send_acpi_ev = true;
3173 *ignore_acpi_ev = false;
3175 if (scancode > 0 && scancode < 0x21) {
3176 scancode--;
3177 if (!(hotkey_source_mask & (1 << scancode))) {
3178 tpacpi_input_send_key_masked(scancode);
3179 *send_acpi_ev = false;
3180 } else {
3181 *ignore_acpi_ev = true;
3183 return true;
3185 return false;
3188 static bool hotkey_notify_wakeup(const u32 hkey,
3189 bool *send_acpi_ev,
3190 bool *ignore_acpi_ev)
3192 /* 0x2000-0x2FFF: Wakeup reason */
3193 *send_acpi_ev = true;
3194 *ignore_acpi_ev = false;
3196 switch (hkey) {
3197 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3198 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3199 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3200 *ignore_acpi_ev = true;
3201 break;
3203 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3204 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3205 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3206 *ignore_acpi_ev = true;
3207 break;
3209 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3210 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3211 printk(TPACPI_ALERT
3212 "EMERGENCY WAKEUP: battery almost empty\n");
3213 /* how to auto-heal: */
3214 /* 2313: woke up from S3, go to S4/S5 */
3215 /* 2413: woke up from S4, go to S5 */
3216 break;
3218 default:
3219 return false;
3222 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3223 printk(TPACPI_INFO
3224 "woke up due to a hot-unplug "
3225 "request...\n");
3226 hotkey_wakeup_reason_notify_change();
3228 return true;
3231 static bool hotkey_notify_usrevent(const u32 hkey,
3232 bool *send_acpi_ev,
3233 bool *ignore_acpi_ev)
3235 /* 0x5000-0x5FFF: human interface helpers */
3236 *send_acpi_ev = true;
3237 *ignore_acpi_ev = false;
3239 switch (hkey) {
3240 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3241 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3242 return true;
3244 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3245 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3246 tpacpi_input_send_tabletsw();
3247 hotkey_tablet_mode_notify_change();
3248 *send_acpi_ev = false;
3249 return true;
3251 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3252 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3253 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3254 /* do not propagate these events */
3255 *ignore_acpi_ev = true;
3256 return true;
3258 default:
3259 return false;
3263 static void thermal_dump_all_sensors(void);
3265 static bool hotkey_notify_thermal(const u32 hkey,
3266 bool *send_acpi_ev,
3267 bool *ignore_acpi_ev)
3269 bool known = true;
3271 /* 0x6000-0x6FFF: thermal alarms */
3272 *send_acpi_ev = true;
3273 *ignore_acpi_ev = false;
3275 switch (hkey) {
3276 case TP_HKEY_EV_THM_TABLE_CHANGED:
3277 printk(TPACPI_INFO
3278 "EC reports that Thermal Table has changed\n");
3279 /* recommended action: do nothing, we don't have
3280 * Lenovo ATM information */
3281 return true;
3282 case TP_HKEY_EV_ALARM_BAT_HOT:
3283 printk(TPACPI_CRIT
3284 "THERMAL ALARM: battery is too hot!\n");
3285 /* recommended action: warn user through gui */
3286 break;
3287 case TP_HKEY_EV_ALARM_BAT_XHOT:
3288 printk(TPACPI_ALERT
3289 "THERMAL EMERGENCY: battery is extremely hot!\n");
3290 /* recommended action: immediate sleep/hibernate */
3291 break;
3292 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3293 printk(TPACPI_CRIT
3294 "THERMAL ALARM: "
3295 "a sensor reports something is too hot!\n");
3296 /* recommended action: warn user through gui, that */
3297 /* some internal component is too hot */
3298 break;
3299 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3300 printk(TPACPI_ALERT
3301 "THERMAL EMERGENCY: "
3302 "a sensor reports something is extremely hot!\n");
3303 /* recommended action: immediate sleep/hibernate */
3304 break;
3305 default:
3306 printk(TPACPI_ALERT
3307 "THERMAL ALERT: unknown thermal alarm received\n");
3308 known = false;
3311 thermal_dump_all_sensors();
3313 return known;
3316 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3318 u32 hkey;
3319 bool send_acpi_ev;
3320 bool ignore_acpi_ev;
3321 bool known_ev;
3323 if (event != 0x80) {
3324 printk(TPACPI_ERR
3325 "unknown HKEY notification event %d\n", event);
3326 /* forward it to userspace, maybe it knows how to handle it */
3327 acpi_bus_generate_netlink_event(
3328 ibm->acpi->device->pnp.device_class,
3329 dev_name(&ibm->acpi->device->dev),
3330 event, 0);
3331 return;
3334 while (1) {
3335 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3336 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3337 return;
3340 if (hkey == 0) {
3341 /* queue empty */
3342 return;
3345 send_acpi_ev = true;
3346 ignore_acpi_ev = false;
3348 switch (hkey >> 12) {
3349 case 1:
3350 /* 0x1000-0x1FFF: key presses */
3351 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3352 &ignore_acpi_ev);
3353 break;
3354 case 2:
3355 /* 0x2000-0x2FFF: Wakeup reason */
3356 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3357 &ignore_acpi_ev);
3358 break;
3359 case 3:
3360 /* 0x3000-0x3FFF: bay-related wakeups */
3361 switch (hkey) {
3362 case TP_HKEY_EV_BAYEJ_ACK:
3363 hotkey_autosleep_ack = 1;
3364 printk(TPACPI_INFO
3365 "bay ejected\n");
3366 hotkey_wakeup_hotunplug_complete_notify_change();
3367 known_ev = true;
3368 break;
3369 case TP_HKEY_EV_OPTDRV_EJ:
3370 /* FIXME: kick libata if SATA link offline */
3371 known_ev = true;
3372 break;
3373 default:
3374 known_ev = false;
3376 break;
3377 case 4:
3378 /* 0x4000-0x4FFF: dock-related wakeups */
3379 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3380 hotkey_autosleep_ack = 1;
3381 printk(TPACPI_INFO
3382 "undocked\n");
3383 hotkey_wakeup_hotunplug_complete_notify_change();
3384 known_ev = true;
3385 } else {
3386 known_ev = false;
3388 break;
3389 case 5:
3390 /* 0x5000-0x5FFF: human interface helpers */
3391 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3392 &ignore_acpi_ev);
3393 break;
3394 case 6:
3395 /* 0x6000-0x6FFF: thermal alarms */
3396 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3397 &ignore_acpi_ev);
3398 break;
3399 case 7:
3400 /* 0x7000-0x7FFF: misc */
3401 if (tp_features.hotkey_wlsw &&
3402 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3403 tpacpi_send_radiosw_update();
3404 send_acpi_ev = 0;
3405 known_ev = true;
3406 break;
3408 /* fallthrough to default */
3409 default:
3410 known_ev = false;
3412 if (!known_ev) {
3413 printk(TPACPI_NOTICE
3414 "unhandled HKEY event 0x%04x\n", hkey);
3415 printk(TPACPI_NOTICE
3416 "please report the conditions when this "
3417 "event happened to %s\n", TPACPI_MAIL);
3420 /* Legacy events */
3421 if (!ignore_acpi_ev &&
3422 (send_acpi_ev || hotkey_report_mode < 2)) {
3423 acpi_bus_generate_proc_event(ibm->acpi->device,
3424 event, hkey);
3427 /* netlink events */
3428 if (!ignore_acpi_ev && send_acpi_ev) {
3429 acpi_bus_generate_netlink_event(
3430 ibm->acpi->device->pnp.device_class,
3431 dev_name(&ibm->acpi->device->dev),
3432 event, hkey);
3437 static void hotkey_suspend(pm_message_t state)
3439 /* Do these on suspend, we get the events on early resume! */
3440 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3441 hotkey_autosleep_ack = 0;
3444 static void hotkey_resume(void)
3446 tpacpi_disable_brightness_delay();
3448 if (hotkey_status_set(true) < 0 ||
3449 hotkey_mask_set(hotkey_acpi_mask) < 0)
3450 printk(TPACPI_ERR
3451 "error while attempting to reset the event "
3452 "firmware interface\n");
3454 tpacpi_send_radiosw_update();
3455 hotkey_tablet_mode_notify_change();
3456 hotkey_wakeup_reason_notify_change();
3457 hotkey_wakeup_hotunplug_complete_notify_change();
3458 hotkey_poll_setup_safe(false);
3461 /* procfs -------------------------------------------------------------- */
3462 static int hotkey_read(struct seq_file *m)
3464 int res, status;
3466 if (!tp_features.hotkey) {
3467 seq_printf(m, "status:\t\tnot supported\n");
3468 return 0;
3471 if (mutex_lock_killable(&hotkey_mutex))
3472 return -ERESTARTSYS;
3473 res = hotkey_status_get(&status);
3474 if (!res)
3475 res = hotkey_mask_get();
3476 mutex_unlock(&hotkey_mutex);
3477 if (res)
3478 return res;
3480 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3481 if (hotkey_all_mask) {
3482 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3483 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3484 } else {
3485 seq_printf(m, "mask:\t\tnot supported\n");
3486 seq_printf(m, "commands:\tenable, disable, reset\n");
3489 return 0;
3492 static void hotkey_enabledisable_warn(bool enable)
3494 tpacpi_log_usertask("procfs hotkey enable/disable");
3495 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3496 TPACPI_WARN
3497 "hotkey enable/disable functionality has been "
3498 "removed from the driver. Hotkeys are always "
3499 "enabled\n"))
3500 printk(TPACPI_ERR
3501 "Please remove the hotkey=enable module "
3502 "parameter, it is deprecated. Hotkeys are always "
3503 "enabled\n");
3506 static int hotkey_write(char *buf)
3508 int res;
3509 u32 mask;
3510 char *cmd;
3512 if (!tp_features.hotkey)
3513 return -ENODEV;
3515 if (mutex_lock_killable(&hotkey_mutex))
3516 return -ERESTARTSYS;
3518 mask = hotkey_user_mask;
3520 res = 0;
3521 while ((cmd = next_cmd(&buf))) {
3522 if (strlencmp(cmd, "enable") == 0) {
3523 hotkey_enabledisable_warn(1);
3524 } else if (strlencmp(cmd, "disable") == 0) {
3525 hotkey_enabledisable_warn(0);
3526 res = -EPERM;
3527 } else if (strlencmp(cmd, "reset") == 0) {
3528 mask = (hotkey_all_mask | hotkey_source_mask)
3529 & ~hotkey_reserved_mask;
3530 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3531 /* mask set */
3532 } else if (sscanf(cmd, "%x", &mask) == 1) {
3533 /* mask set */
3534 } else {
3535 res = -EINVAL;
3536 goto errexit;
3540 if (!res) {
3541 tpacpi_disclose_usertask("procfs hotkey",
3542 "set mask to 0x%08x\n", mask);
3543 res = hotkey_user_mask_set(mask);
3546 errexit:
3547 mutex_unlock(&hotkey_mutex);
3548 return res;
3551 static const struct acpi_device_id ibm_htk_device_ids[] = {
3552 {TPACPI_ACPI_HKEY_HID, 0},
3553 {"", 0},
3556 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3557 .hid = ibm_htk_device_ids,
3558 .notify = hotkey_notify,
3559 .handle = &hkey_handle,
3560 .type = ACPI_DEVICE_NOTIFY,
3563 static struct ibm_struct hotkey_driver_data = {
3564 .name = "hotkey",
3565 .read = hotkey_read,
3566 .write = hotkey_write,
3567 .exit = hotkey_exit,
3568 .resume = hotkey_resume,
3569 .suspend = hotkey_suspend,
3570 .acpi = &ibm_hotkey_acpidriver,
3573 /*************************************************************************
3574 * Bluetooth subdriver
3577 enum {
3578 /* ACPI GBDC/SBDC bits */
3579 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3580 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3581 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3582 off / last state */
3585 enum {
3586 /* ACPI \BLTH commands */
3587 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3588 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3589 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3590 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3591 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3594 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3596 static struct rfkill *tpacpi_bluetooth_rfkill;
3598 static void bluetooth_suspend(pm_message_t state)
3600 /* Try to make sure radio will resume powered off */
3601 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3602 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3603 vdbg_printk(TPACPI_DBG_RFKILL,
3604 "bluetooth power down on resume request failed\n");
3607 static int bluetooth_get_radiosw(void)
3609 int status;
3611 if (!tp_features.bluetooth)
3612 return -ENODEV;
3614 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3615 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3616 return RFKILL_STATE_HARD_BLOCKED;
3618 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3619 if (dbg_bluetoothemul)
3620 return (tpacpi_bluetooth_emulstate) ?
3621 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3622 #endif
3624 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3625 return -EIO;
3627 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3628 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3631 static void bluetooth_update_rfk(void)
3633 int status;
3635 if (!tpacpi_bluetooth_rfkill)
3636 return;
3638 status = bluetooth_get_radiosw();
3639 if (status < 0)
3640 return;
3641 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3643 vdbg_printk(TPACPI_DBG_RFKILL,
3644 "forced rfkill state to %d\n",
3645 status);
3648 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3650 int status;
3652 if (!tp_features.bluetooth)
3653 return -ENODEV;
3655 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3656 * reason to risk weird behaviour. */
3657 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3658 && radio_on)
3659 return -EPERM;
3661 vdbg_printk(TPACPI_DBG_RFKILL,
3662 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3664 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3665 if (dbg_bluetoothemul) {
3666 tpacpi_bluetooth_emulstate = !!radio_on;
3667 if (update_rfk)
3668 bluetooth_update_rfk();
3669 return 0;
3671 #endif
3673 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3674 if (radio_on)
3675 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3676 else
3677 status = 0;
3678 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3679 return -EIO;
3681 if (update_rfk)
3682 bluetooth_update_rfk();
3684 return 0;
3687 /* sysfs bluetooth enable ---------------------------------------------- */
3688 static ssize_t bluetooth_enable_show(struct device *dev,
3689 struct device_attribute *attr,
3690 char *buf)
3692 int status;
3694 printk_deprecated_rfkill_attribute("bluetooth_enable");
3696 status = bluetooth_get_radiosw();
3697 if (status < 0)
3698 return status;
3700 return snprintf(buf, PAGE_SIZE, "%d\n",
3701 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3704 static ssize_t bluetooth_enable_store(struct device *dev,
3705 struct device_attribute *attr,
3706 const char *buf, size_t count)
3708 unsigned long t;
3709 int res;
3711 printk_deprecated_rfkill_attribute("bluetooth_enable");
3713 if (parse_strtoul(buf, 1, &t))
3714 return -EINVAL;
3716 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3718 res = bluetooth_set_radiosw(t, 1);
3720 return (res) ? res : count;
3723 static struct device_attribute dev_attr_bluetooth_enable =
3724 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3725 bluetooth_enable_show, bluetooth_enable_store);
3727 /* --------------------------------------------------------------------- */
3729 static struct attribute *bluetooth_attributes[] = {
3730 &dev_attr_bluetooth_enable.attr,
3731 NULL
3734 static const struct attribute_group bluetooth_attr_group = {
3735 .attrs = bluetooth_attributes,
3738 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3740 int bts = bluetooth_get_radiosw();
3742 if (bts < 0)
3743 return bts;
3745 *state = bts;
3746 return 0;
3749 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3751 dbg_printk(TPACPI_DBG_RFKILL,
3752 "request to change radio state to %d\n", state);
3753 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3756 static void bluetooth_shutdown(void)
3758 /* Order firmware to save current state to NVRAM */
3759 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3760 TP_ACPI_BLTH_SAVE_STATE))
3761 printk(TPACPI_NOTICE
3762 "failed to save bluetooth state to NVRAM\n");
3763 else
3764 vdbg_printk(TPACPI_DBG_RFKILL,
3765 "bluestooth state saved to NVRAM\n");
3768 static void bluetooth_exit(void)
3770 bluetooth_shutdown();
3772 if (tpacpi_bluetooth_rfkill)
3773 rfkill_unregister(tpacpi_bluetooth_rfkill);
3775 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3776 &bluetooth_attr_group);
3779 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3781 int res;
3782 int status = 0;
3784 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3785 "initializing bluetooth subdriver\n");
3787 TPACPI_ACPIHANDLE_INIT(hkey);
3789 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3790 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3791 tp_features.bluetooth = hkey_handle &&
3792 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3794 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3795 "bluetooth is %s, status 0x%02x\n",
3796 str_supported(tp_features.bluetooth),
3797 status);
3799 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3800 if (dbg_bluetoothemul) {
3801 tp_features.bluetooth = 1;
3802 printk(TPACPI_INFO
3803 "bluetooth switch emulation enabled\n");
3804 } else
3805 #endif
3806 if (tp_features.bluetooth &&
3807 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3808 /* no bluetooth hardware present in system */
3809 tp_features.bluetooth = 0;
3810 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3811 "bluetooth hardware not installed\n");
3814 if (!tp_features.bluetooth)
3815 return 1;
3817 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3818 &bluetooth_attr_group);
3819 if (res)
3820 return res;
3822 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3823 &tpacpi_bluetooth_rfkill,
3824 RFKILL_TYPE_BLUETOOTH,
3825 TPACPI_RFK_BLUETOOTH_SW_NAME,
3826 true,
3827 tpacpi_bluetooth_rfk_set,
3828 tpacpi_bluetooth_rfk_get);
3829 if (res) {
3830 bluetooth_exit();
3831 return res;
3834 return 0;
3837 /* procfs -------------------------------------------------------------- */
3838 static int bluetooth_read(struct seq_file *m)
3840 int status = bluetooth_get_radiosw();
3842 if (!tp_features.bluetooth)
3843 seq_printf(m, "status:\t\tnot supported\n");
3844 else {
3845 seq_printf(m, "status:\t\t%s\n",
3846 (status == RFKILL_STATE_UNBLOCKED) ?
3847 "enabled" : "disabled");
3848 seq_printf(m, "commands:\tenable, disable\n");
3851 return 0;
3854 static int bluetooth_write(char *buf)
3856 char *cmd;
3857 int state = -1;
3859 if (!tp_features.bluetooth)
3860 return -ENODEV;
3862 while ((cmd = next_cmd(&buf))) {
3863 if (strlencmp(cmd, "enable") == 0) {
3864 state = 1;
3865 } else if (strlencmp(cmd, "disable") == 0) {
3866 state = 0;
3867 } else
3868 return -EINVAL;
3871 if (state != -1) {
3872 tpacpi_disclose_usertask("procfs bluetooth",
3873 "attempt to %s\n",
3874 state ? "enable" : "disable");
3875 bluetooth_set_radiosw(state, 1);
3878 return 0;
3881 static struct ibm_struct bluetooth_driver_data = {
3882 .name = "bluetooth",
3883 .read = bluetooth_read,
3884 .write = bluetooth_write,
3885 .exit = bluetooth_exit,
3886 .suspend = bluetooth_suspend,
3887 .shutdown = bluetooth_shutdown,
3890 /*************************************************************************
3891 * Wan subdriver
3894 enum {
3895 /* ACPI GWAN/SWAN bits */
3896 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3897 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3898 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3899 off / last state */
3902 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3904 static struct rfkill *tpacpi_wan_rfkill;
3906 static void wan_suspend(pm_message_t state)
3908 /* Try to make sure radio will resume powered off */
3909 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3910 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3911 vdbg_printk(TPACPI_DBG_RFKILL,
3912 "WWAN power down on resume request failed\n");
3915 static int wan_get_radiosw(void)
3917 int status;
3919 if (!tp_features.wan)
3920 return -ENODEV;
3922 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3923 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3924 return RFKILL_STATE_HARD_BLOCKED;
3926 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3927 if (dbg_wwanemul)
3928 return (tpacpi_wwan_emulstate) ?
3929 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3930 #endif
3932 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3933 return -EIO;
3935 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3936 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3939 static void wan_update_rfk(void)
3941 int status;
3943 if (!tpacpi_wan_rfkill)
3944 return;
3946 status = wan_get_radiosw();
3947 if (status < 0)
3948 return;
3949 rfkill_force_state(tpacpi_wan_rfkill, status);
3951 vdbg_printk(TPACPI_DBG_RFKILL,
3952 "forced rfkill state to %d\n",
3953 status);
3956 static int wan_set_radiosw(int radio_on, int update_rfk)
3958 int status;
3960 if (!tp_features.wan)
3961 return -ENODEV;
3963 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3964 * reason to risk weird behaviour. */
3965 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3966 && radio_on)
3967 return -EPERM;
3969 vdbg_printk(TPACPI_DBG_RFKILL,
3970 "will %s WWAN\n", radio_on ? "enable" : "disable");
3972 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3973 if (dbg_wwanemul) {
3974 tpacpi_wwan_emulstate = !!radio_on;
3975 if (update_rfk)
3976 wan_update_rfk();
3977 return 0;
3979 #endif
3981 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3982 if (radio_on)
3983 status = TP_ACPI_WANCARD_RADIOSSW;
3984 else
3985 status = 0;
3986 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3987 return -EIO;
3989 if (update_rfk)
3990 wan_update_rfk();
3992 return 0;
3995 /* sysfs wan enable ---------------------------------------------------- */
3996 static ssize_t wan_enable_show(struct device *dev,
3997 struct device_attribute *attr,
3998 char *buf)
4000 int status;
4002 printk_deprecated_rfkill_attribute("wwan_enable");
4004 status = wan_get_radiosw();
4005 if (status < 0)
4006 return status;
4008 return snprintf(buf, PAGE_SIZE, "%d\n",
4009 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
4012 static ssize_t wan_enable_store(struct device *dev,
4013 struct device_attribute *attr,
4014 const char *buf, size_t count)
4016 unsigned long t;
4017 int res;
4019 printk_deprecated_rfkill_attribute("wwan_enable");
4021 if (parse_strtoul(buf, 1, &t))
4022 return -EINVAL;
4024 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4026 res = wan_set_radiosw(t, 1);
4028 return (res) ? res : count;
4031 static struct device_attribute dev_attr_wan_enable =
4032 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4033 wan_enable_show, wan_enable_store);
4035 /* --------------------------------------------------------------------- */
4037 static struct attribute *wan_attributes[] = {
4038 &dev_attr_wan_enable.attr,
4039 NULL
4042 static const struct attribute_group wan_attr_group = {
4043 .attrs = wan_attributes,
4046 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4048 int wans = wan_get_radiosw();
4050 if (wans < 0)
4051 return wans;
4053 *state = wans;
4054 return 0;
4057 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4059 dbg_printk(TPACPI_DBG_RFKILL,
4060 "request to change radio state to %d\n", state);
4061 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4064 static void wan_shutdown(void)
4066 /* Order firmware to save current state to NVRAM */
4067 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4068 TP_ACPI_WGSV_SAVE_STATE))
4069 printk(TPACPI_NOTICE
4070 "failed to save WWAN state to NVRAM\n");
4071 else
4072 vdbg_printk(TPACPI_DBG_RFKILL,
4073 "WWAN state saved to NVRAM\n");
4076 static void wan_exit(void)
4078 wan_shutdown();
4080 if (tpacpi_wan_rfkill)
4081 rfkill_unregister(tpacpi_wan_rfkill);
4083 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4084 &wan_attr_group);
4087 static int __init wan_init(struct ibm_init_struct *iibm)
4089 int res;
4090 int status = 0;
4092 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4093 "initializing wan subdriver\n");
4095 TPACPI_ACPIHANDLE_INIT(hkey);
4097 tp_features.wan = hkey_handle &&
4098 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4100 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4101 "wan is %s, status 0x%02x\n",
4102 str_supported(tp_features.wan),
4103 status);
4105 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4106 if (dbg_wwanemul) {
4107 tp_features.wan = 1;
4108 printk(TPACPI_INFO
4109 "wwan switch emulation enabled\n");
4110 } else
4111 #endif
4112 if (tp_features.wan &&
4113 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4114 /* no wan hardware present in system */
4115 tp_features.wan = 0;
4116 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4117 "wan hardware not installed\n");
4120 if (!tp_features.wan)
4121 return 1;
4123 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4124 &wan_attr_group);
4125 if (res)
4126 return res;
4128 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4129 &tpacpi_wan_rfkill,
4130 RFKILL_TYPE_WWAN,
4131 TPACPI_RFK_WWAN_SW_NAME,
4132 true,
4133 tpacpi_wan_rfk_set,
4134 tpacpi_wan_rfk_get);
4135 if (res) {
4136 wan_exit();
4137 return res;
4140 return 0;
4143 /* procfs -------------------------------------------------------------- */
4144 static int wan_read(struct seq_file *m)
4146 int status = wan_get_radiosw();
4148 tpacpi_disclose_usertask("procfs wan", "read");
4150 if (!tp_features.wan)
4151 seq_printf(m, "status:\t\tnot supported\n");
4152 else {
4153 seq_printf(m, "status:\t\t%s\n",
4154 (status == RFKILL_STATE_UNBLOCKED) ?
4155 "enabled" : "disabled");
4156 seq_printf(m, "commands:\tenable, disable\n");
4159 return 0;
4162 static int wan_write(char *buf)
4164 char *cmd;
4165 int state = -1;
4167 if (!tp_features.wan)
4168 return -ENODEV;
4170 while ((cmd = next_cmd(&buf))) {
4171 if (strlencmp(cmd, "enable") == 0) {
4172 state = 1;
4173 } else if (strlencmp(cmd, "disable") == 0) {
4174 state = 0;
4175 } else
4176 return -EINVAL;
4179 if (state != -1) {
4180 tpacpi_disclose_usertask("procfs wan",
4181 "attempt to %s\n",
4182 state ? "enable" : "disable");
4183 wan_set_radiosw(state, 1);
4186 return 0;
4189 static struct ibm_struct wan_driver_data = {
4190 .name = "wan",
4191 .read = wan_read,
4192 .write = wan_write,
4193 .exit = wan_exit,
4194 .suspend = wan_suspend,
4195 .shutdown = wan_shutdown,
4198 /*************************************************************************
4199 * UWB subdriver
4202 enum {
4203 /* ACPI GUWB/SUWB bits */
4204 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4205 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4208 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4210 static struct rfkill *tpacpi_uwb_rfkill;
4212 static int uwb_get_radiosw(void)
4214 int status;
4216 if (!tp_features.uwb)
4217 return -ENODEV;
4219 /* WLSW overrides UWB in firmware/hardware, reflect that */
4220 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4221 return RFKILL_STATE_HARD_BLOCKED;
4223 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4224 if (dbg_uwbemul)
4225 return (tpacpi_uwb_emulstate) ?
4226 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4227 #endif
4229 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4230 return -EIO;
4232 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4233 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4236 static void uwb_update_rfk(void)
4238 int status;
4240 if (!tpacpi_uwb_rfkill)
4241 return;
4243 status = uwb_get_radiosw();
4244 if (status < 0)
4245 return;
4246 rfkill_force_state(tpacpi_uwb_rfkill, status);
4248 vdbg_printk(TPACPI_DBG_RFKILL,
4249 "forced rfkill state to %d\n",
4250 status);
4253 static int uwb_set_radiosw(int radio_on, int update_rfk)
4255 int status;
4257 if (!tp_features.uwb)
4258 return -ENODEV;
4260 /* WLSW overrides UWB in firmware/hardware, but there is no
4261 * reason to risk weird behaviour. */
4262 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4263 && radio_on)
4264 return -EPERM;
4266 vdbg_printk(TPACPI_DBG_RFKILL,
4267 "will %s UWB\n", radio_on ? "enable" : "disable");
4269 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4270 if (dbg_uwbemul) {
4271 tpacpi_uwb_emulstate = !!radio_on;
4272 if (update_rfk)
4273 uwb_update_rfk();
4274 return 0;
4276 #endif
4278 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4279 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4280 return -EIO;
4282 if (update_rfk)
4283 uwb_update_rfk();
4285 return 0;
4288 /* --------------------------------------------------------------------- */
4290 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4292 int uwbs = uwb_get_radiosw();
4294 if (uwbs < 0)
4295 return uwbs;
4297 *state = uwbs;
4298 return 0;
4301 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4303 dbg_printk(TPACPI_DBG_RFKILL,
4304 "request to change radio state to %d\n", state);
4305 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4308 static void uwb_exit(void)
4310 if (tpacpi_uwb_rfkill)
4311 rfkill_unregister(tpacpi_uwb_rfkill);
4314 static int __init uwb_init(struct ibm_init_struct *iibm)
4316 int res;
4317 int status = 0;
4319 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4320 "initializing uwb subdriver\n");
4322 TPACPI_ACPIHANDLE_INIT(hkey);
4324 tp_features.uwb = hkey_handle &&
4325 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4327 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4328 "uwb is %s, status 0x%02x\n",
4329 str_supported(tp_features.uwb),
4330 status);
4332 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4333 if (dbg_uwbemul) {
4334 tp_features.uwb = 1;
4335 printk(TPACPI_INFO
4336 "uwb switch emulation enabled\n");
4337 } else
4338 #endif
4339 if (tp_features.uwb &&
4340 !(status & TP_ACPI_UWB_HWPRESENT)) {
4341 /* no uwb hardware present in system */
4342 tp_features.uwb = 0;
4343 dbg_printk(TPACPI_DBG_INIT,
4344 "uwb hardware not installed\n");
4347 if (!tp_features.uwb)
4348 return 1;
4350 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4351 &tpacpi_uwb_rfkill,
4352 RFKILL_TYPE_UWB,
4353 TPACPI_RFK_UWB_SW_NAME,
4354 false,
4355 tpacpi_uwb_rfk_set,
4356 tpacpi_uwb_rfk_get);
4358 return res;
4361 static struct ibm_struct uwb_driver_data = {
4362 .name = "uwb",
4363 .exit = uwb_exit,
4364 .flags.experimental = 1,
4367 /*************************************************************************
4368 * Video subdriver
4371 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4373 enum video_access_mode {
4374 TPACPI_VIDEO_NONE = 0,
4375 TPACPI_VIDEO_570, /* 570 */
4376 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4377 TPACPI_VIDEO_NEW, /* all others */
4380 enum { /* video status flags, based on VIDEO_570 */
4381 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4382 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4383 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4386 enum { /* TPACPI_VIDEO_570 constants */
4387 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4388 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4389 * video_status_flags */
4390 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4391 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4394 static enum video_access_mode video_supported;
4395 static int video_orig_autosw;
4397 static int video_autosw_get(void);
4398 static int video_autosw_set(int enable);
4400 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4402 static int __init video_init(struct ibm_init_struct *iibm)
4404 int ivga;
4406 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4408 TPACPI_ACPIHANDLE_INIT(vid);
4409 TPACPI_ACPIHANDLE_INIT(vid2);
4411 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4412 /* G41, assume IVGA doesn't change */
4413 vid_handle = vid2_handle;
4415 if (!vid_handle)
4416 /* video switching not supported on R30, R31 */
4417 video_supported = TPACPI_VIDEO_NONE;
4418 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4419 /* 570 */
4420 video_supported = TPACPI_VIDEO_570;
4421 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4422 /* 600e/x, 770e, 770x */
4423 video_supported = TPACPI_VIDEO_770;
4424 else
4425 /* all others */
4426 video_supported = TPACPI_VIDEO_NEW;
4428 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4429 str_supported(video_supported != TPACPI_VIDEO_NONE),
4430 video_supported);
4432 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4435 static void video_exit(void)
4437 dbg_printk(TPACPI_DBG_EXIT,
4438 "restoring original video autoswitch mode\n");
4439 if (video_autosw_set(video_orig_autosw))
4440 printk(TPACPI_ERR "error while trying to restore original "
4441 "video autoswitch mode\n");
4444 static int video_outputsw_get(void)
4446 int status = 0;
4447 int i;
4449 switch (video_supported) {
4450 case TPACPI_VIDEO_570:
4451 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4452 TP_ACPI_VIDEO_570_PHSCMD))
4453 return -EIO;
4454 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4455 break;
4456 case TPACPI_VIDEO_770:
4457 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4458 return -EIO;
4459 if (i)
4460 status |= TP_ACPI_VIDEO_S_LCD;
4461 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4462 return -EIO;
4463 if (i)
4464 status |= TP_ACPI_VIDEO_S_CRT;
4465 break;
4466 case TPACPI_VIDEO_NEW:
4467 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4468 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_CRT;
4473 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4474 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4475 return -EIO;
4476 if (i)
4477 status |= TP_ACPI_VIDEO_S_LCD;
4478 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_DVI;
4482 break;
4483 default:
4484 return -ENOSYS;
4487 return status;
4490 static int video_outputsw_set(int status)
4492 int autosw;
4493 int res = 0;
4495 switch (video_supported) {
4496 case TPACPI_VIDEO_570:
4497 res = acpi_evalf(NULL, NULL,
4498 "\\_SB.PHS2", "vdd",
4499 TP_ACPI_VIDEO_570_PHS2CMD,
4500 status | TP_ACPI_VIDEO_570_PHS2SET);
4501 break;
4502 case TPACPI_VIDEO_770:
4503 autosw = video_autosw_get();
4504 if (autosw < 0)
4505 return autosw;
4507 res = video_autosw_set(1);
4508 if (res)
4509 return res;
4510 res = acpi_evalf(vid_handle, NULL,
4511 "ASWT", "vdd", status * 0x100, 0);
4512 if (!autosw && video_autosw_set(autosw)) {
4513 printk(TPACPI_ERR
4514 "video auto-switch left enabled due to error\n");
4515 return -EIO;
4517 break;
4518 case TPACPI_VIDEO_NEW:
4519 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4520 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4521 break;
4522 default:
4523 return -ENOSYS;
4526 return (res)? 0 : -EIO;
4529 static int video_autosw_get(void)
4531 int autosw = 0;
4533 switch (video_supported) {
4534 case TPACPI_VIDEO_570:
4535 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4536 return -EIO;
4537 break;
4538 case TPACPI_VIDEO_770:
4539 case TPACPI_VIDEO_NEW:
4540 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4541 return -EIO;
4542 break;
4543 default:
4544 return -ENOSYS;
4547 return autosw & 1;
4550 static int video_autosw_set(int enable)
4552 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4553 return -EIO;
4554 return 0;
4557 static int video_outputsw_cycle(void)
4559 int autosw = video_autosw_get();
4560 int res;
4562 if (autosw < 0)
4563 return autosw;
4565 switch (video_supported) {
4566 case TPACPI_VIDEO_570:
4567 res = video_autosw_set(1);
4568 if (res)
4569 return res;
4570 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4571 break;
4572 case TPACPI_VIDEO_770:
4573 case TPACPI_VIDEO_NEW:
4574 res = video_autosw_set(1);
4575 if (res)
4576 return res;
4577 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4578 break;
4579 default:
4580 return -ENOSYS;
4582 if (!autosw && video_autosw_set(autosw)) {
4583 printk(TPACPI_ERR
4584 "video auto-switch left enabled due to error\n");
4585 return -EIO;
4588 return (res)? 0 : -EIO;
4591 static int video_expand_toggle(void)
4593 switch (video_supported) {
4594 case TPACPI_VIDEO_570:
4595 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4596 0 : -EIO;
4597 case TPACPI_VIDEO_770:
4598 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4599 0 : -EIO;
4600 case TPACPI_VIDEO_NEW:
4601 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4602 0 : -EIO;
4603 default:
4604 return -ENOSYS;
4606 /* not reached */
4609 static int video_read(struct seq_file *m)
4611 int status, autosw;
4613 if (video_supported == TPACPI_VIDEO_NONE) {
4614 seq_printf(m, "status:\t\tnot supported\n");
4615 return 0;
4618 status = video_outputsw_get();
4619 if (status < 0)
4620 return status;
4622 autosw = video_autosw_get();
4623 if (autosw < 0)
4624 return autosw;
4626 seq_printf(m, "status:\t\tsupported\n");
4627 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4628 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4629 if (video_supported == TPACPI_VIDEO_NEW)
4630 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4631 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4632 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4633 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4634 if (video_supported == TPACPI_VIDEO_NEW)
4635 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4636 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4637 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4639 return 0;
4642 static int video_write(char *buf)
4644 char *cmd;
4645 int enable, disable, status;
4646 int res;
4648 if (video_supported == TPACPI_VIDEO_NONE)
4649 return -ENODEV;
4651 enable = 0;
4652 disable = 0;
4654 while ((cmd = next_cmd(&buf))) {
4655 if (strlencmp(cmd, "lcd_enable") == 0) {
4656 enable |= TP_ACPI_VIDEO_S_LCD;
4657 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4658 disable |= TP_ACPI_VIDEO_S_LCD;
4659 } else if (strlencmp(cmd, "crt_enable") == 0) {
4660 enable |= TP_ACPI_VIDEO_S_CRT;
4661 } else if (strlencmp(cmd, "crt_disable") == 0) {
4662 disable |= TP_ACPI_VIDEO_S_CRT;
4663 } else if (video_supported == TPACPI_VIDEO_NEW &&
4664 strlencmp(cmd, "dvi_enable") == 0) {
4665 enable |= TP_ACPI_VIDEO_S_DVI;
4666 } else if (video_supported == TPACPI_VIDEO_NEW &&
4667 strlencmp(cmd, "dvi_disable") == 0) {
4668 disable |= TP_ACPI_VIDEO_S_DVI;
4669 } else if (strlencmp(cmd, "auto_enable") == 0) {
4670 res = video_autosw_set(1);
4671 if (res)
4672 return res;
4673 } else if (strlencmp(cmd, "auto_disable") == 0) {
4674 res = video_autosw_set(0);
4675 if (res)
4676 return res;
4677 } else if (strlencmp(cmd, "video_switch") == 0) {
4678 res = video_outputsw_cycle();
4679 if (res)
4680 return res;
4681 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4682 res = video_expand_toggle();
4683 if (res)
4684 return res;
4685 } else
4686 return -EINVAL;
4689 if (enable || disable) {
4690 status = video_outputsw_get();
4691 if (status < 0)
4692 return status;
4693 res = video_outputsw_set((status & ~disable) | enable);
4694 if (res)
4695 return res;
4698 return 0;
4701 static struct ibm_struct video_driver_data = {
4702 .name = "video",
4703 .read = video_read,
4704 .write = video_write,
4705 .exit = video_exit,
4708 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4710 /*************************************************************************
4711 * Light (thinklight) subdriver
4714 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4715 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4717 static int light_get_status(void)
4719 int status = 0;
4721 if (tp_features.light_status) {
4722 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4723 return -EIO;
4724 return (!!status);
4727 return -ENXIO;
4730 static int light_set_status(int status)
4732 int rc;
4734 if (tp_features.light) {
4735 if (cmos_handle) {
4736 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4737 (status)?
4738 TP_CMOS_THINKLIGHT_ON :
4739 TP_CMOS_THINKLIGHT_OFF);
4740 } else {
4741 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4742 (status)? 1 : 0);
4744 return (rc)? 0 : -EIO;
4747 return -ENXIO;
4750 static void light_set_status_worker(struct work_struct *work)
4752 struct tpacpi_led_classdev *data =
4753 container_of(work, struct tpacpi_led_classdev, work);
4755 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4756 light_set_status((data->new_state != TPACPI_LED_OFF));
4759 static void light_sysfs_set(struct led_classdev *led_cdev,
4760 enum led_brightness brightness)
4762 struct tpacpi_led_classdev *data =
4763 container_of(led_cdev,
4764 struct tpacpi_led_classdev,
4765 led_classdev);
4766 data->new_state = (brightness != LED_OFF) ?
4767 TPACPI_LED_ON : TPACPI_LED_OFF;
4768 queue_work(tpacpi_wq, &data->work);
4771 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4773 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4776 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4777 .led_classdev = {
4778 .name = "tpacpi::thinklight",
4779 .brightness_set = &light_sysfs_set,
4780 .brightness_get = &light_sysfs_get,
4784 static int __init light_init(struct ibm_init_struct *iibm)
4786 int rc;
4788 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4790 TPACPI_ACPIHANDLE_INIT(ledb);
4791 TPACPI_ACPIHANDLE_INIT(lght);
4792 TPACPI_ACPIHANDLE_INIT(cmos);
4793 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4795 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4796 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4798 if (tp_features.light)
4799 /* light status not supported on
4800 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4801 tp_features.light_status =
4802 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4804 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4805 str_supported(tp_features.light),
4806 str_supported(tp_features.light_status));
4808 if (!tp_features.light)
4809 return 1;
4811 rc = led_classdev_register(&tpacpi_pdev->dev,
4812 &tpacpi_led_thinklight.led_classdev);
4814 if (rc < 0) {
4815 tp_features.light = 0;
4816 tp_features.light_status = 0;
4817 } else {
4818 rc = 0;
4821 return rc;
4824 static void light_exit(void)
4826 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4827 if (work_pending(&tpacpi_led_thinklight.work))
4828 flush_workqueue(tpacpi_wq);
4831 static int light_read(struct seq_file *m)
4833 int status;
4835 if (!tp_features.light) {
4836 seq_printf(m, "status:\t\tnot supported\n");
4837 } else if (!tp_features.light_status) {
4838 seq_printf(m, "status:\t\tunknown\n");
4839 seq_printf(m, "commands:\ton, off\n");
4840 } else {
4841 status = light_get_status();
4842 if (status < 0)
4843 return status;
4844 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4845 seq_printf(m, "commands:\ton, off\n");
4848 return 0;
4851 static int light_write(char *buf)
4853 char *cmd;
4854 int newstatus = 0;
4856 if (!tp_features.light)
4857 return -ENODEV;
4859 while ((cmd = next_cmd(&buf))) {
4860 if (strlencmp(cmd, "on") == 0) {
4861 newstatus = 1;
4862 } else if (strlencmp(cmd, "off") == 0) {
4863 newstatus = 0;
4864 } else
4865 return -EINVAL;
4868 return light_set_status(newstatus);
4871 static struct ibm_struct light_driver_data = {
4872 .name = "light",
4873 .read = light_read,
4874 .write = light_write,
4875 .exit = light_exit,
4878 /*************************************************************************
4879 * CMOS subdriver
4882 /* sysfs cmos_command -------------------------------------------------- */
4883 static ssize_t cmos_command_store(struct device *dev,
4884 struct device_attribute *attr,
4885 const char *buf, size_t count)
4887 unsigned long cmos_cmd;
4888 int res;
4890 if (parse_strtoul(buf, 21, &cmos_cmd))
4891 return -EINVAL;
4893 res = issue_thinkpad_cmos_command(cmos_cmd);
4894 return (res)? res : count;
4897 static struct device_attribute dev_attr_cmos_command =
4898 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4900 /* --------------------------------------------------------------------- */
4902 static int __init cmos_init(struct ibm_init_struct *iibm)
4904 int res;
4906 vdbg_printk(TPACPI_DBG_INIT,
4907 "initializing cmos commands subdriver\n");
4909 TPACPI_ACPIHANDLE_INIT(cmos);
4911 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4912 str_supported(cmos_handle != NULL));
4914 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4915 if (res)
4916 return res;
4918 return (cmos_handle)? 0 : 1;
4921 static void cmos_exit(void)
4923 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4926 static int cmos_read(struct seq_file *m)
4928 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4929 R30, R31, T20-22, X20-21 */
4930 if (!cmos_handle)
4931 seq_printf(m, "status:\t\tnot supported\n");
4932 else {
4933 seq_printf(m, "status:\t\tsupported\n");
4934 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4937 return 0;
4940 static int cmos_write(char *buf)
4942 char *cmd;
4943 int cmos_cmd, res;
4945 while ((cmd = next_cmd(&buf))) {
4946 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4947 cmos_cmd >= 0 && cmos_cmd <= 21) {
4948 /* cmos_cmd set */
4949 } else
4950 return -EINVAL;
4952 res = issue_thinkpad_cmos_command(cmos_cmd);
4953 if (res)
4954 return res;
4957 return 0;
4960 static struct ibm_struct cmos_driver_data = {
4961 .name = "cmos",
4962 .read = cmos_read,
4963 .write = cmos_write,
4964 .exit = cmos_exit,
4967 /*************************************************************************
4968 * LED subdriver
4971 enum led_access_mode {
4972 TPACPI_LED_NONE = 0,
4973 TPACPI_LED_570, /* 570 */
4974 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4975 TPACPI_LED_NEW, /* all others */
4978 enum { /* For TPACPI_LED_OLD */
4979 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4980 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4981 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4984 static enum led_access_mode led_supported;
4986 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4987 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4988 /* T20-22, X20-21 */
4989 "LED", /* all others */
4990 ); /* R30, R31 */
4992 #define TPACPI_LED_NUMLEDS 16
4993 static struct tpacpi_led_classdev *tpacpi_leds;
4994 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4995 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4996 /* there's a limit of 19 chars + NULL before 2.6.26 */
4997 "tpacpi::power",
4998 "tpacpi:orange:batt",
4999 "tpacpi:green:batt",
5000 "tpacpi::dock_active",
5001 "tpacpi::bay_active",
5002 "tpacpi::dock_batt",
5003 "tpacpi::unknown_led",
5004 "tpacpi::standby",
5005 "tpacpi::dock_status1",
5006 "tpacpi::dock_status2",
5007 "tpacpi::unknown_led2",
5008 "tpacpi::unknown_led3",
5009 "tpacpi::thinkvantage",
5011 #define TPACPI_SAFE_LEDS 0x1081U
5013 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5015 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5016 return false;
5017 #else
5018 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5019 #endif
5022 static int led_get_status(const unsigned int led)
5024 int status;
5025 enum led_status_t led_s;
5027 switch (led_supported) {
5028 case TPACPI_LED_570:
5029 if (!acpi_evalf(ec_handle,
5030 &status, "GLED", "dd", 1 << led))
5031 return -EIO;
5032 led_s = (status == 0)?
5033 TPACPI_LED_OFF :
5034 ((status == 1)?
5035 TPACPI_LED_ON :
5036 TPACPI_LED_BLINK);
5037 tpacpi_led_state_cache[led] = led_s;
5038 return led_s;
5039 default:
5040 return -ENXIO;
5043 /* not reached */
5046 static int led_set_status(const unsigned int led,
5047 const enum led_status_t ledstatus)
5049 /* off, on, blink. Index is led_status_t */
5050 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5051 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5053 int rc = 0;
5055 switch (led_supported) {
5056 case TPACPI_LED_570:
5057 /* 570 */
5058 if (unlikely(led > 7))
5059 return -EINVAL;
5060 if (unlikely(tpacpi_is_led_restricted(led)))
5061 return -EPERM;
5062 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5063 (1 << led), led_sled_arg1[ledstatus]))
5064 rc = -EIO;
5065 break;
5066 case TPACPI_LED_OLD:
5067 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5068 if (unlikely(led > 7))
5069 return -EINVAL;
5070 if (unlikely(tpacpi_is_led_restricted(led)))
5071 return -EPERM;
5072 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5073 if (rc >= 0)
5074 rc = ec_write(TPACPI_LED_EC_HLBL,
5075 (ledstatus == TPACPI_LED_BLINK) << led);
5076 if (rc >= 0)
5077 rc = ec_write(TPACPI_LED_EC_HLCL,
5078 (ledstatus != TPACPI_LED_OFF) << led);
5079 break;
5080 case TPACPI_LED_NEW:
5081 /* all others */
5082 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5083 return -EINVAL;
5084 if (unlikely(tpacpi_is_led_restricted(led)))
5085 return -EPERM;
5086 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5087 led, led_led_arg1[ledstatus]))
5088 rc = -EIO;
5089 break;
5090 default:
5091 rc = -ENXIO;
5094 if (!rc)
5095 tpacpi_led_state_cache[led] = ledstatus;
5097 return rc;
5100 static void led_set_status_worker(struct work_struct *work)
5102 struct tpacpi_led_classdev *data =
5103 container_of(work, struct tpacpi_led_classdev, work);
5105 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5106 led_set_status(data->led, data->new_state);
5109 static void led_sysfs_set(struct led_classdev *led_cdev,
5110 enum led_brightness brightness)
5112 struct tpacpi_led_classdev *data = container_of(led_cdev,
5113 struct tpacpi_led_classdev, led_classdev);
5115 if (brightness == LED_OFF)
5116 data->new_state = TPACPI_LED_OFF;
5117 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5118 data->new_state = TPACPI_LED_ON;
5119 else
5120 data->new_state = TPACPI_LED_BLINK;
5122 queue_work(tpacpi_wq, &data->work);
5125 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5126 unsigned long *delay_on, unsigned long *delay_off)
5128 struct tpacpi_led_classdev *data = container_of(led_cdev,
5129 struct tpacpi_led_classdev, led_classdev);
5131 /* Can we choose the flash rate? */
5132 if (*delay_on == 0 && *delay_off == 0) {
5133 /* yes. set them to the hardware blink rate (1 Hz) */
5134 *delay_on = 500; /* ms */
5135 *delay_off = 500; /* ms */
5136 } else if ((*delay_on != 500) || (*delay_off != 500))
5137 return -EINVAL;
5139 data->new_state = TPACPI_LED_BLINK;
5140 queue_work(tpacpi_wq, &data->work);
5142 return 0;
5145 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5147 int rc;
5149 struct tpacpi_led_classdev *data = container_of(led_cdev,
5150 struct tpacpi_led_classdev, led_classdev);
5152 rc = led_get_status(data->led);
5154 if (rc == TPACPI_LED_OFF || rc < 0)
5155 rc = LED_OFF; /* no error handling in led class :( */
5156 else
5157 rc = LED_FULL;
5159 return rc;
5162 static void led_exit(void)
5164 unsigned int i;
5166 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5167 if (tpacpi_leds[i].led_classdev.name)
5168 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5171 kfree(tpacpi_leds);
5174 static int __init tpacpi_init_led(unsigned int led)
5176 int rc;
5178 tpacpi_leds[led].led = led;
5180 /* LEDs with no name don't get registered */
5181 if (!tpacpi_led_names[led])
5182 return 0;
5184 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5185 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5186 if (led_supported == TPACPI_LED_570)
5187 tpacpi_leds[led].led_classdev.brightness_get =
5188 &led_sysfs_get;
5190 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5192 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5194 rc = led_classdev_register(&tpacpi_pdev->dev,
5195 &tpacpi_leds[led].led_classdev);
5196 if (rc < 0)
5197 tpacpi_leds[led].led_classdev.name = NULL;
5199 return rc;
5202 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5203 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5204 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5205 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5207 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5208 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5209 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5210 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5211 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5212 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5213 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5214 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5216 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5217 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5218 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5219 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5220 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5222 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5223 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5224 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5225 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5227 /* (1) - may have excess leds enabled on MSB */
5229 /* Defaults (order matters, keep last, don't reorder!) */
5230 { /* Lenovo */
5231 .vendor = PCI_VENDOR_ID_LENOVO,
5232 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5233 .quirks = 0x1fffU,
5235 { /* IBM ThinkPads with no EC version string */
5236 .vendor = PCI_VENDOR_ID_IBM,
5237 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5238 .quirks = 0x00ffU,
5240 { /* IBM ThinkPads with EC version string */
5241 .vendor = PCI_VENDOR_ID_IBM,
5242 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5243 .quirks = 0x00bfU,
5247 #undef TPACPI_LEDQ_IBM
5248 #undef TPACPI_LEDQ_LNV
5250 static int __init led_init(struct ibm_init_struct *iibm)
5252 unsigned int i;
5253 int rc;
5254 unsigned long useful_leds;
5256 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5258 TPACPI_ACPIHANDLE_INIT(led);
5260 if (!led_handle)
5261 /* led not supported on R30, R31 */
5262 led_supported = TPACPI_LED_NONE;
5263 else if (strlencmp(led_path, "SLED") == 0)
5264 /* 570 */
5265 led_supported = TPACPI_LED_570;
5266 else if (strlencmp(led_path, "SYSL") == 0)
5267 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5268 led_supported = TPACPI_LED_OLD;
5269 else
5270 /* all others */
5271 led_supported = TPACPI_LED_NEW;
5273 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5274 str_supported(led_supported), led_supported);
5276 if (led_supported == TPACPI_LED_NONE)
5277 return 1;
5279 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5280 GFP_KERNEL);
5281 if (!tpacpi_leds) {
5282 printk(TPACPI_ERR "Out of memory for LED data\n");
5283 return -ENOMEM;
5286 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5287 ARRAY_SIZE(led_useful_qtable));
5289 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5290 if (!tpacpi_is_led_restricted(i) &&
5291 test_bit(i, &useful_leds)) {
5292 rc = tpacpi_init_led(i);
5293 if (rc < 0) {
5294 led_exit();
5295 return rc;
5300 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5301 printk(TPACPI_NOTICE
5302 "warning: userspace override of important "
5303 "firmware LEDs is enabled\n");
5304 #endif
5305 return 0;
5308 #define str_led_status(s) \
5309 ((s) == TPACPI_LED_OFF ? "off" : \
5310 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5312 static int led_read(struct seq_file *m)
5314 if (!led_supported) {
5315 seq_printf(m, "status:\t\tnot supported\n");
5316 return 0;
5318 seq_printf(m, "status:\t\tsupported\n");
5320 if (led_supported == TPACPI_LED_570) {
5321 /* 570 */
5322 int i, status;
5323 for (i = 0; i < 8; i++) {
5324 status = led_get_status(i);
5325 if (status < 0)
5326 return -EIO;
5327 seq_printf(m, "%d:\t\t%s\n",
5328 i, str_led_status(status));
5332 seq_printf(m, "commands:\t"
5333 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5335 return 0;
5338 static int led_write(char *buf)
5340 char *cmd;
5341 int led, rc;
5342 enum led_status_t s;
5344 if (!led_supported)
5345 return -ENODEV;
5347 while ((cmd = next_cmd(&buf))) {
5348 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5349 return -EINVAL;
5351 if (strstr(cmd, "off")) {
5352 s = TPACPI_LED_OFF;
5353 } else if (strstr(cmd, "on")) {
5354 s = TPACPI_LED_ON;
5355 } else if (strstr(cmd, "blink")) {
5356 s = TPACPI_LED_BLINK;
5357 } else {
5358 return -EINVAL;
5361 rc = led_set_status(led, s);
5362 if (rc < 0)
5363 return rc;
5366 return 0;
5369 static struct ibm_struct led_driver_data = {
5370 .name = "led",
5371 .read = led_read,
5372 .write = led_write,
5373 .exit = led_exit,
5376 /*************************************************************************
5377 * Beep subdriver
5380 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5382 #define TPACPI_BEEP_Q1 0x0001
5384 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5385 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5386 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5389 static int __init beep_init(struct ibm_init_struct *iibm)
5391 unsigned long quirks;
5393 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5395 TPACPI_ACPIHANDLE_INIT(beep);
5397 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5398 str_supported(beep_handle != NULL));
5400 quirks = tpacpi_check_quirks(beep_quirk_table,
5401 ARRAY_SIZE(beep_quirk_table));
5403 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5405 return (beep_handle)? 0 : 1;
5408 static int beep_read(struct seq_file *m)
5410 if (!beep_handle)
5411 seq_printf(m, "status:\t\tnot supported\n");
5412 else {
5413 seq_printf(m, "status:\t\tsupported\n");
5414 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5417 return 0;
5420 static int beep_write(char *buf)
5422 char *cmd;
5423 int beep_cmd;
5425 if (!beep_handle)
5426 return -ENODEV;
5428 while ((cmd = next_cmd(&buf))) {
5429 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5430 beep_cmd >= 0 && beep_cmd <= 17) {
5431 /* beep_cmd set */
5432 } else
5433 return -EINVAL;
5434 if (tp_features.beep_needs_two_args) {
5435 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5436 beep_cmd, 0))
5437 return -EIO;
5438 } else {
5439 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5440 beep_cmd))
5441 return -EIO;
5445 return 0;
5448 static struct ibm_struct beep_driver_data = {
5449 .name = "beep",
5450 .read = beep_read,
5451 .write = beep_write,
5454 /*************************************************************************
5455 * Thermal subdriver
5458 enum thermal_access_mode {
5459 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5460 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5461 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5462 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5463 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5466 enum { /* TPACPI_THERMAL_TPEC_* */
5467 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5468 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5469 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5471 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5475 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5476 struct ibm_thermal_sensors_struct {
5477 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5480 static enum thermal_access_mode thermal_read_mode;
5482 /* idx is zero-based */
5483 static int thermal_get_sensor(int idx, s32 *value)
5485 int t;
5486 s8 tmp;
5487 char tmpi[5];
5489 t = TP_EC_THERMAL_TMP0;
5491 switch (thermal_read_mode) {
5492 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5493 case TPACPI_THERMAL_TPEC_16:
5494 if (idx >= 8 && idx <= 15) {
5495 t = TP_EC_THERMAL_TMP8;
5496 idx -= 8;
5498 /* fallthrough */
5499 #endif
5500 case TPACPI_THERMAL_TPEC_8:
5501 if (idx <= 7) {
5502 if (!acpi_ec_read(t + idx, &tmp))
5503 return -EIO;
5504 *value = tmp * 1000;
5505 return 0;
5507 break;
5509 case TPACPI_THERMAL_ACPI_UPDT:
5510 if (idx <= 7) {
5511 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5512 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5513 return -EIO;
5514 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5515 return -EIO;
5516 *value = (t - 2732) * 100;
5517 return 0;
5519 break;
5521 case TPACPI_THERMAL_ACPI_TMP07:
5522 if (idx <= 7) {
5523 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5524 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5525 return -EIO;
5526 if (t > 127 || t < -127)
5527 t = TP_EC_THERMAL_TMP_NA;
5528 *value = t * 1000;
5529 return 0;
5531 break;
5533 case TPACPI_THERMAL_NONE:
5534 default:
5535 return -ENOSYS;
5538 return -EINVAL;
5541 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5543 int res, i;
5544 int n;
5546 n = 8;
5547 i = 0;
5549 if (!s)
5550 return -EINVAL;
5552 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5553 n = 16;
5555 for (i = 0 ; i < n; i++) {
5556 res = thermal_get_sensor(i, &s->temp[i]);
5557 if (res)
5558 return res;
5561 return n;
5564 static void thermal_dump_all_sensors(void)
5566 int n, i;
5567 struct ibm_thermal_sensors_struct t;
5569 n = thermal_get_sensors(&t);
5570 if (n <= 0)
5571 return;
5573 printk(TPACPI_NOTICE
5574 "temperatures (Celsius):");
5576 for (i = 0; i < n; i++) {
5577 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5578 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5579 else
5580 printk(KERN_CONT " N/A");
5583 printk(KERN_CONT "\n");
5586 /* sysfs temp##_input -------------------------------------------------- */
5588 static ssize_t thermal_temp_input_show(struct device *dev,
5589 struct device_attribute *attr,
5590 char *buf)
5592 struct sensor_device_attribute *sensor_attr =
5593 to_sensor_dev_attr(attr);
5594 int idx = sensor_attr->index;
5595 s32 value;
5596 int res;
5598 res = thermal_get_sensor(idx, &value);
5599 if (res)
5600 return res;
5601 if (value == TPACPI_THERMAL_SENSOR_NA)
5602 return -ENXIO;
5604 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5607 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5608 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5609 thermal_temp_input_show, NULL, _idxB)
5611 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5612 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5613 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5614 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5615 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5616 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5617 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5618 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5619 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5620 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5621 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5622 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5623 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5624 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5625 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5626 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5627 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5630 #define THERMAL_ATTRS(X) \
5631 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5633 static struct attribute *thermal_temp_input_attr[] = {
5634 THERMAL_ATTRS(8),
5635 THERMAL_ATTRS(9),
5636 THERMAL_ATTRS(10),
5637 THERMAL_ATTRS(11),
5638 THERMAL_ATTRS(12),
5639 THERMAL_ATTRS(13),
5640 THERMAL_ATTRS(14),
5641 THERMAL_ATTRS(15),
5642 THERMAL_ATTRS(0),
5643 THERMAL_ATTRS(1),
5644 THERMAL_ATTRS(2),
5645 THERMAL_ATTRS(3),
5646 THERMAL_ATTRS(4),
5647 THERMAL_ATTRS(5),
5648 THERMAL_ATTRS(6),
5649 THERMAL_ATTRS(7),
5650 NULL
5653 static const struct attribute_group thermal_temp_input16_group = {
5654 .attrs = thermal_temp_input_attr
5657 static const struct attribute_group thermal_temp_input8_group = {
5658 .attrs = &thermal_temp_input_attr[8]
5661 #undef THERMAL_SENSOR_ATTR_TEMP
5662 #undef THERMAL_ATTRS
5664 /* --------------------------------------------------------------------- */
5666 static int __init thermal_init(struct ibm_init_struct *iibm)
5668 u8 t, ta1, ta2;
5669 int i;
5670 int acpi_tmp7;
5671 int res;
5673 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5675 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5677 if (thinkpad_id.ec_model) {
5679 * Direct EC access mode: sensors at registers
5680 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5681 * non-implemented, thermal sensors return 0x80 when
5682 * not available
5685 ta1 = ta2 = 0;
5686 for (i = 0; i < 8; i++) {
5687 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5688 ta1 |= t;
5689 } else {
5690 ta1 = 0;
5691 break;
5693 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5694 ta2 |= t;
5695 } else {
5696 ta1 = 0;
5697 break;
5700 if (ta1 == 0) {
5701 /* This is sheer paranoia, but we handle it anyway */
5702 if (acpi_tmp7) {
5703 printk(TPACPI_ERR
5704 "ThinkPad ACPI EC access misbehaving, "
5705 "falling back to ACPI TMPx access "
5706 "mode\n");
5707 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5708 } else {
5709 printk(TPACPI_ERR
5710 "ThinkPad ACPI EC access misbehaving, "
5711 "disabling thermal sensors access\n");
5712 thermal_read_mode = TPACPI_THERMAL_NONE;
5714 } else {
5715 thermal_read_mode =
5716 (ta2 != 0) ?
5717 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5719 } else if (acpi_tmp7) {
5720 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5721 /* 600e/x, 770e, 770x */
5722 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5723 } else {
5724 /* Standard ACPI TMPx access, max 8 sensors */
5725 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5727 } else {
5728 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5729 thermal_read_mode = TPACPI_THERMAL_NONE;
5732 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5733 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5734 thermal_read_mode);
5736 switch (thermal_read_mode) {
5737 case TPACPI_THERMAL_TPEC_16:
5738 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5739 &thermal_temp_input16_group);
5740 if (res)
5741 return res;
5742 break;
5743 case TPACPI_THERMAL_TPEC_8:
5744 case TPACPI_THERMAL_ACPI_TMP07:
5745 case TPACPI_THERMAL_ACPI_UPDT:
5746 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5747 &thermal_temp_input8_group);
5748 if (res)
5749 return res;
5750 break;
5751 case TPACPI_THERMAL_NONE:
5752 default:
5753 return 1;
5756 return 0;
5759 static void thermal_exit(void)
5761 switch (thermal_read_mode) {
5762 case TPACPI_THERMAL_TPEC_16:
5763 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5764 &thermal_temp_input16_group);
5765 break;
5766 case TPACPI_THERMAL_TPEC_8:
5767 case TPACPI_THERMAL_ACPI_TMP07:
5768 case TPACPI_THERMAL_ACPI_UPDT:
5769 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5770 &thermal_temp_input8_group);
5771 break;
5772 case TPACPI_THERMAL_NONE:
5773 default:
5774 break;
5778 static int thermal_read(struct seq_file *m)
5780 int n, i;
5781 struct ibm_thermal_sensors_struct t;
5783 n = thermal_get_sensors(&t);
5784 if (unlikely(n < 0))
5785 return n;
5787 seq_printf(m, "temperatures:\t");
5789 if (n > 0) {
5790 for (i = 0; i < (n - 1); i++)
5791 seq_printf(m, "%d ", t.temp[i] / 1000);
5792 seq_printf(m, "%d\n", t.temp[i] / 1000);
5793 } else
5794 seq_printf(m, "not supported\n");
5796 return 0;
5799 static struct ibm_struct thermal_driver_data = {
5800 .name = "thermal",
5801 .read = thermal_read,
5802 .exit = thermal_exit,
5805 /*************************************************************************
5806 * EC Dump subdriver
5809 static u8 ecdump_regs[256];
5811 static int ecdump_read(struct seq_file *m)
5813 int i, j;
5814 u8 v;
5816 seq_printf(m, "EC "
5817 " +00 +01 +02 +03 +04 +05 +06 +07"
5818 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5819 for (i = 0; i < 256; i += 16) {
5820 seq_printf(m, "EC 0x%02x:", i);
5821 for (j = 0; j < 16; j++) {
5822 if (!acpi_ec_read(i + j, &v))
5823 break;
5824 if (v != ecdump_regs[i + j])
5825 seq_printf(m, " *%02x", v);
5826 else
5827 seq_printf(m, " %02x", v);
5828 ecdump_regs[i + j] = v;
5830 seq_putc(m, '\n');
5831 if (j != 16)
5832 break;
5835 /* These are way too dangerous to advertise openly... */
5836 #if 0
5837 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5838 " (<offset> is 00-ff, <value> is 00-ff)\n");
5839 seq_printf(m, "commands:\t0x<offset> <value> "
5840 " (<offset> is 00-ff, <value> is 0-255)\n");
5841 #endif
5842 return 0;
5845 static int ecdump_write(char *buf)
5847 char *cmd;
5848 int i, v;
5850 while ((cmd = next_cmd(&buf))) {
5851 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5852 /* i and v set */
5853 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5854 /* i and v set */
5855 } else
5856 return -EINVAL;
5857 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5858 if (!acpi_ec_write(i, v))
5859 return -EIO;
5860 } else
5861 return -EINVAL;
5864 return 0;
5867 static struct ibm_struct ecdump_driver_data = {
5868 .name = "ecdump",
5869 .read = ecdump_read,
5870 .write = ecdump_write,
5871 .flags.experimental = 1,
5874 /*************************************************************************
5875 * Backlight/brightness subdriver
5878 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5881 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5882 * CMOS NVRAM byte 0x5E, bits 0-3.
5884 * EC HBRV (0x31) has the following layout
5885 * Bit 7: unknown function
5886 * Bit 6: unknown function
5887 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5888 * Bit 4: must be set to zero to avoid problems
5889 * Bit 3-0: backlight brightness level
5891 * brightness_get_raw returns status data in the HBRV layout
5893 * WARNING: The X61 has been verified to use HBRV for something else, so
5894 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5895 * testing on the very early *60 Lenovo models...
5898 enum {
5899 TP_EC_BACKLIGHT = 0x31,
5901 /* TP_EC_BACKLIGHT bitmasks */
5902 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5903 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5904 TP_EC_BACKLIGHT_MAPSW = 0x20,
5907 enum tpacpi_brightness_access_mode {
5908 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5909 TPACPI_BRGHT_MODE_EC, /* EC control */
5910 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5911 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5912 TPACPI_BRGHT_MODE_MAX
5915 static struct backlight_device *ibm_backlight_device;
5917 static enum tpacpi_brightness_access_mode brightness_mode =
5918 TPACPI_BRGHT_MODE_MAX;
5920 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5922 static struct mutex brightness_mutex;
5924 /* NVRAM brightness access,
5925 * call with brightness_mutex held! */
5926 static unsigned int tpacpi_brightness_nvram_get(void)
5928 u8 lnvram;
5930 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5931 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5932 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5933 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5935 return lnvram;
5938 static void tpacpi_brightness_checkpoint_nvram(void)
5940 u8 lec = 0;
5941 u8 b_nvram;
5943 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5944 return;
5946 vdbg_printk(TPACPI_DBG_BRGHT,
5947 "trying to checkpoint backlight level to NVRAM...\n");
5949 if (mutex_lock_killable(&brightness_mutex) < 0)
5950 return;
5952 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5953 goto unlock;
5954 lec &= TP_EC_BACKLIGHT_LVLMSK;
5955 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5957 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5958 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5959 /* NVRAM needs update */
5960 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5961 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5962 b_nvram |= lec;
5963 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5964 dbg_printk(TPACPI_DBG_BRGHT,
5965 "updated NVRAM backlight level to %u (0x%02x)\n",
5966 (unsigned int) lec, (unsigned int) b_nvram);
5967 } else
5968 vdbg_printk(TPACPI_DBG_BRGHT,
5969 "NVRAM backlight level already is %u (0x%02x)\n",
5970 (unsigned int) lec, (unsigned int) b_nvram);
5972 unlock:
5973 mutex_unlock(&brightness_mutex);
5977 /* call with brightness_mutex held! */
5978 static int tpacpi_brightness_get_raw(int *status)
5980 u8 lec = 0;
5982 switch (brightness_mode) {
5983 case TPACPI_BRGHT_MODE_UCMS_STEP:
5984 *status = tpacpi_brightness_nvram_get();
5985 return 0;
5986 case TPACPI_BRGHT_MODE_EC:
5987 case TPACPI_BRGHT_MODE_ECNVRAM:
5988 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5989 return -EIO;
5990 *status = lec;
5991 return 0;
5992 default:
5993 return -ENXIO;
5997 /* call with brightness_mutex held! */
5998 /* do NOT call with illegal backlight level value */
5999 static int tpacpi_brightness_set_ec(unsigned int value)
6001 u8 lec = 0;
6003 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6004 return -EIO;
6006 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6007 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6008 (value & TP_EC_BACKLIGHT_LVLMSK))))
6009 return -EIO;
6011 return 0;
6014 /* call with brightness_mutex held! */
6015 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6017 int cmos_cmd, inc;
6018 unsigned int current_value, i;
6020 current_value = tpacpi_brightness_nvram_get();
6022 if (value == current_value)
6023 return 0;
6025 cmos_cmd = (value > current_value) ?
6026 TP_CMOS_BRIGHTNESS_UP :
6027 TP_CMOS_BRIGHTNESS_DOWN;
6028 inc = (value > current_value) ? 1 : -1;
6030 for (i = current_value; i != value; i += inc)
6031 if (issue_thinkpad_cmos_command(cmos_cmd))
6032 return -EIO;
6034 return 0;
6037 /* May return EINTR which can always be mapped to ERESTARTSYS */
6038 static int brightness_set(unsigned int value)
6040 int res;
6042 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6043 value < 0)
6044 return -EINVAL;
6046 vdbg_printk(TPACPI_DBG_BRGHT,
6047 "set backlight level to %d\n", value);
6049 res = mutex_lock_killable(&brightness_mutex);
6050 if (res < 0)
6051 return res;
6053 switch (brightness_mode) {
6054 case TPACPI_BRGHT_MODE_EC:
6055 case TPACPI_BRGHT_MODE_ECNVRAM:
6056 res = tpacpi_brightness_set_ec(value);
6057 break;
6058 case TPACPI_BRGHT_MODE_UCMS_STEP:
6059 res = tpacpi_brightness_set_ucmsstep(value);
6060 break;
6061 default:
6062 res = -ENXIO;
6065 mutex_unlock(&brightness_mutex);
6066 return res;
6069 /* sysfs backlight class ----------------------------------------------- */
6071 static int brightness_update_status(struct backlight_device *bd)
6073 unsigned int level =
6074 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6075 bd->props.power == FB_BLANK_UNBLANK) ?
6076 bd->props.brightness : 0;
6078 dbg_printk(TPACPI_DBG_BRGHT,
6079 "backlight: attempt to set level to %d\n",
6080 level);
6082 /* it is the backlight class's job (caller) to handle
6083 * EINTR and other errors properly */
6084 return brightness_set(level);
6087 static int brightness_get(struct backlight_device *bd)
6089 int status, res;
6091 res = mutex_lock_killable(&brightness_mutex);
6092 if (res < 0)
6093 return 0;
6095 res = tpacpi_brightness_get_raw(&status);
6097 mutex_unlock(&brightness_mutex);
6099 if (res < 0)
6100 return 0;
6102 return status & TP_EC_BACKLIGHT_LVLMSK;
6105 static void tpacpi_brightness_notify_change(void)
6107 backlight_force_update(ibm_backlight_device,
6108 BACKLIGHT_UPDATE_HOTKEY);
6111 static struct backlight_ops ibm_backlight_data = {
6112 .get_brightness = brightness_get,
6113 .update_status = brightness_update_status,
6116 /* --------------------------------------------------------------------- */
6119 * These are only useful for models that have only one possibility
6120 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6121 * these quirks.
6123 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6124 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6125 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6127 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6128 /* Models with ATI GPUs known to require ECNVRAM mode */
6129 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6131 /* Models with ATI GPUs that can use ECNVRAM */
6132 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6133 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6134 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6135 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6137 /* Models with Intel Extreme Graphics 2 */
6138 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6139 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6140 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 /* Models with Intel GMA900 */
6143 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6144 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6145 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6148 static int __init brightness_init(struct ibm_init_struct *iibm)
6150 int b;
6151 unsigned long quirks;
6153 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6155 mutex_init(&brightness_mutex);
6157 quirks = tpacpi_check_quirks(brightness_quirk_table,
6158 ARRAY_SIZE(brightness_quirk_table));
6161 * We always attempt to detect acpi support, so as to switch
6162 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6163 * going to publish a backlight interface
6165 b = tpacpi_check_std_acpi_brightness_support();
6166 if (b > 0) {
6168 if (acpi_video_backlight_support()) {
6169 if (brightness_enable > 1) {
6170 printk(TPACPI_NOTICE
6171 "Standard ACPI backlight interface "
6172 "available, not loading native one.\n");
6173 return 1;
6174 } else if (brightness_enable == 1) {
6175 printk(TPACPI_NOTICE
6176 "Backlight control force enabled, even if standard "
6177 "ACPI backlight interface is available\n");
6179 } else {
6180 if (brightness_enable > 1) {
6181 printk(TPACPI_NOTICE
6182 "Standard ACPI backlight interface not "
6183 "available, thinkpad_acpi native "
6184 "brightness control enabled\n");
6189 if (!brightness_enable) {
6190 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6191 "brightness support disabled by "
6192 "module parameter\n");
6193 return 1;
6196 if (b > 16) {
6197 printk(TPACPI_ERR
6198 "Unsupported brightness interface, "
6199 "please contact %s\n", TPACPI_MAIL);
6200 return 1;
6202 if (b == 16)
6203 tp_features.bright_16levels = 1;
6206 * Check for module parameter bogosity, note that we
6207 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6208 * able to detect "unspecified"
6210 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6211 return -EINVAL;
6213 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6214 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6215 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6216 if (quirks & TPACPI_BRGHT_Q_EC)
6217 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6218 else
6219 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6221 dbg_printk(TPACPI_DBG_BRGHT,
6222 "driver auto-selected brightness_mode=%d\n",
6223 brightness_mode);
6226 /* Safety */
6227 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6228 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6229 brightness_mode == TPACPI_BRGHT_MODE_EC))
6230 return -EINVAL;
6232 if (tpacpi_brightness_get_raw(&b) < 0)
6233 return 1;
6235 if (tp_features.bright_16levels)
6236 printk(TPACPI_INFO
6237 "detected a 16-level brightness capable ThinkPad\n");
6239 ibm_backlight_device = backlight_device_register(
6240 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6241 &ibm_backlight_data);
6242 if (IS_ERR(ibm_backlight_device)) {
6243 int rc = PTR_ERR(ibm_backlight_device);
6244 ibm_backlight_device = NULL;
6245 printk(TPACPI_ERR "Could not register backlight device\n");
6246 return rc;
6248 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6249 "brightness is supported\n");
6251 if (quirks & TPACPI_BRGHT_Q_ASK) {
6252 printk(TPACPI_NOTICE
6253 "brightness: will use unverified default: "
6254 "brightness_mode=%d\n", brightness_mode);
6255 printk(TPACPI_NOTICE
6256 "brightness: please report to %s whether it works well "
6257 "or not on your ThinkPad\n", TPACPI_MAIL);
6260 ibm_backlight_device->props.max_brightness =
6261 (tp_features.bright_16levels)? 15 : 7;
6262 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6263 backlight_update_status(ibm_backlight_device);
6265 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6266 "brightness: registering brightness hotkeys "
6267 "as change notification\n");
6268 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6269 | TP_ACPI_HKEY_BRGHTUP_MASK
6270 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6271 return 0;
6274 static void brightness_suspend(pm_message_t state)
6276 tpacpi_brightness_checkpoint_nvram();
6279 static void brightness_shutdown(void)
6281 tpacpi_brightness_checkpoint_nvram();
6284 static void brightness_exit(void)
6286 if (ibm_backlight_device) {
6287 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6288 "calling backlight_device_unregister()\n");
6289 backlight_device_unregister(ibm_backlight_device);
6292 tpacpi_brightness_checkpoint_nvram();
6295 static int brightness_read(struct seq_file *m)
6297 int level;
6299 level = brightness_get(NULL);
6300 if (level < 0) {
6301 seq_printf(m, "level:\t\tunreadable\n");
6302 } else {
6303 seq_printf(m, "level:\t\t%d\n", level);
6304 seq_printf(m, "commands:\tup, down\n");
6305 seq_printf(m, "commands:\tlevel <level>"
6306 " (<level> is 0-%d)\n",
6307 (tp_features.bright_16levels) ? 15 : 7);
6310 return 0;
6313 static int brightness_write(char *buf)
6315 int level;
6316 int rc;
6317 char *cmd;
6318 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6320 level = brightness_get(NULL);
6321 if (level < 0)
6322 return level;
6324 while ((cmd = next_cmd(&buf))) {
6325 if (strlencmp(cmd, "up") == 0) {
6326 if (level < max_level)
6327 level++;
6328 } else if (strlencmp(cmd, "down") == 0) {
6329 if (level > 0)
6330 level--;
6331 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6332 level >= 0 && level <= max_level) {
6333 /* new level set */
6334 } else
6335 return -EINVAL;
6338 tpacpi_disclose_usertask("procfs brightness",
6339 "set level to %d\n", level);
6342 * Now we know what the final level should be, so we try to set it.
6343 * Doing it this way makes the syscall restartable in case of EINTR
6345 rc = brightness_set(level);
6346 if (!rc && ibm_backlight_device)
6347 backlight_force_update(ibm_backlight_device,
6348 BACKLIGHT_UPDATE_SYSFS);
6349 return (rc == -EINTR)? -ERESTARTSYS : rc;
6352 static struct ibm_struct brightness_driver_data = {
6353 .name = "brightness",
6354 .read = brightness_read,
6355 .write = brightness_write,
6356 .exit = brightness_exit,
6357 .suspend = brightness_suspend,
6358 .shutdown = brightness_shutdown,
6361 /*************************************************************************
6362 * Volume subdriver
6366 * IBM ThinkPads have a simple volume controller with MUTE gating.
6367 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6369 * Since the *61 series (and probably also the later *60 series), Lenovo
6370 * ThinkPads only implement the MUTE gate.
6372 * EC register 0x30
6373 * Bit 6: MUTE (1 mutes sound)
6374 * Bit 3-0: Volume
6375 * Other bits should be zero as far as we know.
6377 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6378 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6379 * such as bit 7 which is used to detect repeated presses of MUTE,
6380 * and we leave them unchanged.
6383 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6385 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6386 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6387 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6389 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6390 static char *alsa_id = "ThinkPadEC";
6391 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6393 struct tpacpi_alsa_data {
6394 struct snd_card *card;
6395 struct snd_ctl_elem_id *ctl_mute_id;
6396 struct snd_ctl_elem_id *ctl_vol_id;
6399 static struct snd_card *alsa_card;
6401 enum {
6402 TP_EC_AUDIO = 0x30,
6404 /* TP_EC_AUDIO bits */
6405 TP_EC_AUDIO_MUTESW = 6,
6407 /* TP_EC_AUDIO bitmasks */
6408 TP_EC_AUDIO_LVL_MSK = 0x0F,
6409 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6411 /* Maximum volume */
6412 TP_EC_VOLUME_MAX = 14,
6415 enum tpacpi_volume_access_mode {
6416 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6417 TPACPI_VOL_MODE_EC, /* Pure EC control */
6418 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6419 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6420 TPACPI_VOL_MODE_MAX
6423 enum tpacpi_volume_capabilities {
6424 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6425 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6426 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6427 TPACPI_VOL_CAP_MAX
6430 static enum tpacpi_volume_access_mode volume_mode =
6431 TPACPI_VOL_MODE_MAX;
6433 static enum tpacpi_volume_capabilities volume_capabilities;
6434 static int volume_control_allowed;
6437 * Used to syncronize writers to TP_EC_AUDIO and
6438 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6440 static struct mutex volume_mutex;
6442 static void tpacpi_volume_checkpoint_nvram(void)
6444 u8 lec = 0;
6445 u8 b_nvram;
6446 u8 ec_mask;
6448 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6449 return;
6450 if (!volume_control_allowed)
6451 return;
6453 vdbg_printk(TPACPI_DBG_MIXER,
6454 "trying to checkpoint mixer state to NVRAM...\n");
6456 if (tp_features.mixer_no_level_control)
6457 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6458 else
6459 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6461 if (mutex_lock_killable(&volume_mutex) < 0)
6462 return;
6464 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6465 goto unlock;
6466 lec &= ec_mask;
6467 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6469 if (lec != (b_nvram & ec_mask)) {
6470 /* NVRAM needs update */
6471 b_nvram &= ~ec_mask;
6472 b_nvram |= lec;
6473 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6474 dbg_printk(TPACPI_DBG_MIXER,
6475 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6476 (unsigned int) lec, (unsigned int) b_nvram);
6477 } else {
6478 vdbg_printk(TPACPI_DBG_MIXER,
6479 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6480 (unsigned int) lec, (unsigned int) b_nvram);
6483 unlock:
6484 mutex_unlock(&volume_mutex);
6487 static int volume_get_status_ec(u8 *status)
6489 u8 s;
6491 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6492 return -EIO;
6494 *status = s;
6496 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6498 return 0;
6501 static int volume_get_status(u8 *status)
6503 return volume_get_status_ec(status);
6506 static int volume_set_status_ec(const u8 status)
6508 if (!acpi_ec_write(TP_EC_AUDIO, status))
6509 return -EIO;
6511 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6513 return 0;
6516 static int volume_set_status(const u8 status)
6518 return volume_set_status_ec(status);
6521 static int volume_set_mute_ec(const bool mute)
6523 int rc;
6524 u8 s, n;
6526 if (mutex_lock_killable(&volume_mutex) < 0)
6527 return -EINTR;
6529 rc = volume_get_status_ec(&s);
6530 if (rc)
6531 goto unlock;
6533 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6534 s & ~TP_EC_AUDIO_MUTESW_MSK;
6536 if (n != s)
6537 rc = volume_set_status_ec(n);
6539 unlock:
6540 mutex_unlock(&volume_mutex);
6541 return rc;
6544 static int volume_set_mute(const bool mute)
6546 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6547 (mute) ? "" : "un");
6548 return volume_set_mute_ec(mute);
6551 static int volume_set_volume_ec(const u8 vol)
6553 int rc;
6554 u8 s, n;
6556 if (vol > TP_EC_VOLUME_MAX)
6557 return -EINVAL;
6559 if (mutex_lock_killable(&volume_mutex) < 0)
6560 return -EINTR;
6562 rc = volume_get_status_ec(&s);
6563 if (rc)
6564 goto unlock;
6566 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6568 if (n != s)
6569 rc = volume_set_status_ec(n);
6571 unlock:
6572 mutex_unlock(&volume_mutex);
6573 return rc;
6576 static int volume_set_volume(const u8 vol)
6578 dbg_printk(TPACPI_DBG_MIXER,
6579 "trying to set volume level to %hu\n", vol);
6580 return volume_set_volume_ec(vol);
6583 static void volume_alsa_notify_change(void)
6585 struct tpacpi_alsa_data *d;
6587 if (alsa_card && alsa_card->private_data) {
6588 d = alsa_card->private_data;
6589 if (d->ctl_mute_id)
6590 snd_ctl_notify(alsa_card,
6591 SNDRV_CTL_EVENT_MASK_VALUE,
6592 d->ctl_mute_id);
6593 if (d->ctl_vol_id)
6594 snd_ctl_notify(alsa_card,
6595 SNDRV_CTL_EVENT_MASK_VALUE,
6596 d->ctl_vol_id);
6600 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6601 struct snd_ctl_elem_info *uinfo)
6603 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6604 uinfo->count = 1;
6605 uinfo->value.integer.min = 0;
6606 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6607 return 0;
6610 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6611 struct snd_ctl_elem_value *ucontrol)
6613 u8 s;
6614 int rc;
6616 rc = volume_get_status(&s);
6617 if (rc < 0)
6618 return rc;
6620 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6621 return 0;
6624 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6625 struct snd_ctl_elem_value *ucontrol)
6627 return volume_set_volume(ucontrol->value.integer.value[0]);
6630 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6632 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6633 struct snd_ctl_elem_value *ucontrol)
6635 u8 s;
6636 int rc;
6638 rc = volume_get_status(&s);
6639 if (rc < 0)
6640 return rc;
6642 ucontrol->value.integer.value[0] =
6643 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6644 return 0;
6647 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6648 struct snd_ctl_elem_value *ucontrol)
6650 return volume_set_mute(!ucontrol->value.integer.value[0]);
6653 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6654 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6655 .name = "Console Playback Volume",
6656 .index = 0,
6657 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6658 .info = volume_alsa_vol_info,
6659 .get = volume_alsa_vol_get,
6662 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6664 .name = "Console Playback Switch",
6665 .index = 0,
6666 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6667 .info = volume_alsa_mute_info,
6668 .get = volume_alsa_mute_get,
6671 static void volume_suspend(pm_message_t state)
6673 tpacpi_volume_checkpoint_nvram();
6676 static void volume_resume(void)
6678 volume_alsa_notify_change();
6681 static void volume_shutdown(void)
6683 tpacpi_volume_checkpoint_nvram();
6686 static void volume_exit(void)
6688 if (alsa_card) {
6689 snd_card_free(alsa_card);
6690 alsa_card = NULL;
6693 tpacpi_volume_checkpoint_nvram();
6696 static int __init volume_create_alsa_mixer(void)
6698 struct snd_card *card;
6699 struct tpacpi_alsa_data *data;
6700 struct snd_kcontrol *ctl_vol;
6701 struct snd_kcontrol *ctl_mute;
6702 int rc;
6704 card = snd_card_new(alsa_index, alsa_id, THIS_MODULE,
6705 sizeof(struct tpacpi_alsa_data));
6706 if (!card) {
6707 printk(TPACPI_ERR
6708 "Failed to create ALSA card structures\n");
6709 return 1;
6712 BUG_ON(!card->private_data);
6713 data = card->private_data;
6714 data->card = card;
6716 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6717 sizeof(card->driver));
6718 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6719 sizeof(card->shortname));
6720 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6721 (thinkpad_id.ec_version_str) ?
6722 thinkpad_id.ec_version_str : "(unknown)");
6723 snprintf(card->longname, sizeof(card->longname),
6724 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6725 (thinkpad_id.ec_version_str) ?
6726 thinkpad_id.ec_version_str : "unknown");
6728 if (volume_control_allowed) {
6729 volume_alsa_control_vol.put = volume_alsa_vol_put;
6730 volume_alsa_control_vol.access =
6731 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6733 volume_alsa_control_mute.put = volume_alsa_mute_put;
6734 volume_alsa_control_mute.access =
6735 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6738 if (!tp_features.mixer_no_level_control) {
6739 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6740 rc = snd_ctl_add(card, ctl_vol);
6741 if (rc < 0) {
6742 printk(TPACPI_ERR
6743 "Failed to create ALSA volume control: %d\n",
6744 rc);
6745 goto err_exit;
6747 data->ctl_vol_id = &ctl_vol->id;
6750 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6751 rc = snd_ctl_add(card, ctl_mute);
6752 if (rc < 0) {
6753 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6754 rc);
6755 goto err_exit;
6757 data->ctl_mute_id = &ctl_mute->id;
6759 snd_card_set_dev(card, &tpacpi_pdev->dev);
6760 rc = snd_card_register(card);
6761 if (rc < 0) {
6762 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6763 goto err_exit;
6766 alsa_card = card;
6767 return 0;
6769 err_exit:
6770 snd_card_free(card);
6771 return 1;
6774 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6775 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6777 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6778 /* Whitelist volume level on all IBM by default */
6779 { .vendor = PCI_VENDOR_ID_IBM,
6780 .bios = TPACPI_MATCH_ANY,
6781 .ec = TPACPI_MATCH_ANY,
6782 .quirks = TPACPI_VOL_Q_LEVEL },
6784 /* Lenovo models with volume control (needs confirmation) */
6785 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6786 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6787 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6788 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6789 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6790 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6791 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6793 /* Whitelist mute-only on all Lenovo by default */
6794 { .vendor = PCI_VENDOR_ID_LENOVO,
6795 .bios = TPACPI_MATCH_ANY,
6796 .ec = TPACPI_MATCH_ANY,
6797 .quirks = TPACPI_VOL_Q_MUTEONLY }
6800 static int __init volume_init(struct ibm_init_struct *iibm)
6802 unsigned long quirks;
6803 int rc;
6805 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6807 mutex_init(&volume_mutex);
6810 * Check for module parameter bogosity, note that we
6811 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6812 * able to detect "unspecified"
6814 if (volume_mode > TPACPI_VOL_MODE_MAX)
6815 return -EINVAL;
6817 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6818 printk(TPACPI_ERR
6819 "UCMS step volume mode not implemented, "
6820 "please contact %s\n", TPACPI_MAIL);
6821 return 1;
6824 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6825 return -EINVAL;
6828 * The ALSA mixer is our primary interface.
6829 * When disabled, don't install the subdriver at all
6831 if (!alsa_enable) {
6832 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6833 "ALSA mixer disabled by parameter, "
6834 "not loading volume subdriver...\n");
6835 return 1;
6838 quirks = tpacpi_check_quirks(volume_quirk_table,
6839 ARRAY_SIZE(volume_quirk_table));
6841 switch (volume_capabilities) {
6842 case TPACPI_VOL_CAP_AUTO:
6843 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6844 tp_features.mixer_no_level_control = 1;
6845 else if (quirks & TPACPI_VOL_Q_LEVEL)
6846 tp_features.mixer_no_level_control = 0;
6847 else
6848 return 1; /* no mixer */
6849 break;
6850 case TPACPI_VOL_CAP_VOLMUTE:
6851 tp_features.mixer_no_level_control = 0;
6852 break;
6853 case TPACPI_VOL_CAP_MUTEONLY:
6854 tp_features.mixer_no_level_control = 1;
6855 break;
6856 default:
6857 return 1;
6860 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6861 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6862 "using user-supplied volume_capabilities=%d\n",
6863 volume_capabilities);
6865 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6866 volume_mode == TPACPI_VOL_MODE_MAX) {
6867 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6869 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6870 "driver auto-selected volume_mode=%d\n",
6871 volume_mode);
6872 } else {
6873 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6874 "using user-supplied volume_mode=%d\n",
6875 volume_mode);
6878 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6879 "mute is supported, volume control is %s\n",
6880 str_supported(!tp_features.mixer_no_level_control));
6882 rc = volume_create_alsa_mixer();
6883 if (rc) {
6884 printk(TPACPI_ERR
6885 "Could not create the ALSA mixer interface\n");
6886 return rc;
6889 printk(TPACPI_INFO
6890 "Console audio control enabled, mode: %s\n",
6891 (volume_control_allowed) ?
6892 "override (read/write)" :
6893 "monitor (read only)");
6895 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6896 "registering volume hotkeys as change notification\n");
6897 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6898 | TP_ACPI_HKEY_VOLUP_MASK
6899 | TP_ACPI_HKEY_VOLDWN_MASK
6900 | TP_ACPI_HKEY_MUTE_MASK);
6902 return 0;
6905 static int volume_read(struct seq_file *m)
6907 u8 status;
6909 if (volume_get_status(&status) < 0) {
6910 seq_printf(m, "level:\t\tunreadable\n");
6911 } else {
6912 if (tp_features.mixer_no_level_control)
6913 seq_printf(m, "level:\t\tunsupported\n");
6914 else
6915 seq_printf(m, "level:\t\t%d\n",
6916 status & TP_EC_AUDIO_LVL_MSK);
6918 seq_printf(m, "mute:\t\t%s\n",
6919 onoff(status, TP_EC_AUDIO_MUTESW));
6921 if (volume_control_allowed) {
6922 seq_printf(m, "commands:\tunmute, mute\n");
6923 if (!tp_features.mixer_no_level_control) {
6924 seq_printf(m,
6925 "commands:\tup, down\n");
6926 seq_printf(m,
6927 "commands:\tlevel <level>"
6928 " (<level> is 0-%d)\n",
6929 TP_EC_VOLUME_MAX);
6934 return 0;
6937 static int volume_write(char *buf)
6939 u8 s;
6940 u8 new_level, new_mute;
6941 int l;
6942 char *cmd;
6943 int rc;
6946 * We do allow volume control at driver startup, so that the
6947 * user can set initial state through the volume=... parameter hack.
6949 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6950 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6951 tp_warned.volume_ctrl_forbidden = 1;
6952 printk(TPACPI_NOTICE
6953 "Console audio control in monitor mode, "
6954 "changes are not allowed.\n");
6955 printk(TPACPI_NOTICE
6956 "Use the volume_control=1 module parameter "
6957 "to enable volume control\n");
6959 return -EPERM;
6962 rc = volume_get_status(&s);
6963 if (rc < 0)
6964 return rc;
6966 new_level = s & TP_EC_AUDIO_LVL_MSK;
6967 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6969 while ((cmd = next_cmd(&buf))) {
6970 if (!tp_features.mixer_no_level_control) {
6971 if (strlencmp(cmd, "up") == 0) {
6972 if (new_mute)
6973 new_mute = 0;
6974 else if (new_level < TP_EC_VOLUME_MAX)
6975 new_level++;
6976 continue;
6977 } else if (strlencmp(cmd, "down") == 0) {
6978 if (new_mute)
6979 new_mute = 0;
6980 else if (new_level > 0)
6981 new_level--;
6982 continue;
6983 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6984 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6985 new_level = l;
6986 continue;
6989 if (strlencmp(cmd, "mute") == 0)
6990 new_mute = TP_EC_AUDIO_MUTESW_MSK;
6991 else if (strlencmp(cmd, "unmute") == 0)
6992 new_mute = 0;
6993 else
6994 return -EINVAL;
6997 if (tp_features.mixer_no_level_control) {
6998 tpacpi_disclose_usertask("procfs volume", "%smute\n",
6999 new_mute ? "" : "un");
7000 rc = volume_set_mute(!!new_mute);
7001 } else {
7002 tpacpi_disclose_usertask("procfs volume",
7003 "%smute and set level to %d\n",
7004 new_mute ? "" : "un", new_level);
7005 rc = volume_set_status(new_mute | new_level);
7007 volume_alsa_notify_change();
7009 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7012 static struct ibm_struct volume_driver_data = {
7013 .name = "volume",
7014 .read = volume_read,
7015 .write = volume_write,
7016 .exit = volume_exit,
7017 .suspend = volume_suspend,
7018 .resume = volume_resume,
7019 .shutdown = volume_shutdown,
7022 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7024 #define alsa_card NULL
7026 static void inline volume_alsa_notify_change(void)
7030 static int __init volume_init(struct ibm_init_struct *iibm)
7032 printk(TPACPI_INFO
7033 "volume: disabled as there is no ALSA support in this kernel\n");
7035 return 1;
7038 static struct ibm_struct volume_driver_data = {
7039 .name = "volume",
7042 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7044 /*************************************************************************
7045 * Fan subdriver
7049 * FAN ACCESS MODES
7051 * TPACPI_FAN_RD_ACPI_GFAN:
7052 * ACPI GFAN method: returns fan level
7054 * see TPACPI_FAN_WR_ACPI_SFAN
7055 * EC 0x2f (HFSP) not available if GFAN exists
7057 * TPACPI_FAN_WR_ACPI_SFAN:
7058 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7060 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7061 * it for writing.
7063 * TPACPI_FAN_WR_TPEC:
7064 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7065 * Supported on almost all ThinkPads
7067 * Fan speed changes of any sort (including those caused by the
7068 * disengaged mode) are usually done slowly by the firmware as the
7069 * maximum ammount of fan duty cycle change per second seems to be
7070 * limited.
7072 * Reading is not available if GFAN exists.
7073 * Writing is not available if SFAN exists.
7075 * Bits
7076 * 7 automatic mode engaged;
7077 * (default operation mode of the ThinkPad)
7078 * fan level is ignored in this mode.
7079 * 6 full speed mode (takes precedence over bit 7);
7080 * not available on all thinkpads. May disable
7081 * the tachometer while the fan controller ramps up
7082 * the speed (which can take up to a few *minutes*).
7083 * Speeds up fan to 100% duty-cycle, which is far above
7084 * the standard RPM levels. It is not impossible that
7085 * it could cause hardware damage.
7086 * 5-3 unused in some models. Extra bits for fan level
7087 * in others, but still useless as all values above
7088 * 7 map to the same speed as level 7 in these models.
7089 * 2-0 fan level (0..7 usually)
7090 * 0x00 = stop
7091 * 0x07 = max (set when temperatures critical)
7092 * Some ThinkPads may have other levels, see
7093 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7095 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7096 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7097 * does so, its initial value is meaningless (0x07).
7099 * For firmware bugs, refer to:
7100 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7102 * ----
7104 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7105 * Main fan tachometer reading (in RPM)
7107 * This register is present on all ThinkPads with a new-style EC, and
7108 * it is known not to be present on the A21m/e, and T22, as there is
7109 * something else in offset 0x84 according to the ACPI DSDT. Other
7110 * ThinkPads from this same time period (and earlier) probably lack the
7111 * tachometer as well.
7113 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7114 * was never fixed by IBM to report the EC firmware version string
7115 * probably support the tachometer (like the early X models), so
7116 * detecting it is quite hard. We need more data to know for sure.
7118 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7119 * might result.
7121 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7122 * mode.
7124 * For firmware bugs, refer to:
7125 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7127 * ----
7129 * ThinkPad EC register 0x31 bit 0 (only on select models)
7131 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7132 * show the speed of the main fan. When bit 0 of EC register 0x31
7133 * is one, the tachometer registers show the speed of the auxiliary
7134 * fan.
7136 * Fan control seems to affect both fans, regardless of the state
7137 * of this bit.
7139 * So far, only the firmware for the X60/X61 non-tablet versions
7140 * seem to support this (firmware TP-7M).
7142 * TPACPI_FAN_WR_ACPI_FANS:
7143 * ThinkPad X31, X40, X41. Not available in the X60.
7145 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7146 * high speed. ACPI DSDT seems to map these three speeds to levels
7147 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7148 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7150 * The speeds are stored on handles
7151 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7153 * There are three default speed sets, acessible as handles:
7154 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7156 * ACPI DSDT switches which set is in use depending on various
7157 * factors.
7159 * TPACPI_FAN_WR_TPEC is also available and should be used to
7160 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7161 * but the ACPI tables just mention level 7.
7164 enum { /* Fan control constants */
7165 fan_status_offset = 0x2f, /* EC register 0x2f */
7166 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7167 * 0x84 must be read before 0x85 */
7168 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7169 bit 0 selects which fan is active */
7171 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7172 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7174 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7177 enum fan_status_access_mode {
7178 TPACPI_FAN_NONE = 0, /* No fan status or control */
7179 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7180 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7183 enum fan_control_access_mode {
7184 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7185 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7186 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7187 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7190 enum fan_control_commands {
7191 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7192 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7193 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7194 * and also watchdog cmd */
7197 static int fan_control_allowed;
7199 static enum fan_status_access_mode fan_status_access_mode;
7200 static enum fan_control_access_mode fan_control_access_mode;
7201 static enum fan_control_commands fan_control_commands;
7203 static u8 fan_control_initial_status;
7204 static u8 fan_control_desired_level;
7205 static u8 fan_control_resume_level;
7206 static int fan_watchdog_maxinterval;
7208 static struct mutex fan_mutex;
7210 static void fan_watchdog_fire(struct work_struct *ignored);
7211 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7213 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7214 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7215 "\\FSPD", /* 600e/x, 770e, 770x */
7216 ); /* all others */
7217 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7218 "JFNS", /* 770x-JL */
7219 ); /* all others */
7222 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7223 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7224 * be in auto mode (0x80).
7226 * This is corrected by any write to HFSP either by the driver, or
7227 * by the firmware.
7229 * We assume 0x07 really means auto mode while this quirk is active,
7230 * as this is far more likely than the ThinkPad being in level 7,
7231 * which is only used by the firmware during thermal emergencies.
7233 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7234 * TP-70 (T43, R52), which are known to be buggy.
7237 static void fan_quirk1_setup(void)
7239 if (fan_control_initial_status == 0x07) {
7240 printk(TPACPI_NOTICE
7241 "fan_init: initial fan status is unknown, "
7242 "assuming it is in auto mode\n");
7243 tp_features.fan_ctrl_status_undef = 1;
7247 static void fan_quirk1_handle(u8 *fan_status)
7249 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7250 if (*fan_status != fan_control_initial_status) {
7251 /* something changed the HFSP regisnter since
7252 * driver init time, so it is not undefined
7253 * anymore */
7254 tp_features.fan_ctrl_status_undef = 0;
7255 } else {
7256 /* Return most likely status. In fact, it
7257 * might be the only possible status */
7258 *fan_status = TP_EC_FAN_AUTO;
7263 /* Select main fan on X60/X61, NOOP on others */
7264 static bool fan_select_fan1(void)
7266 if (tp_features.second_fan) {
7267 u8 val;
7269 if (ec_read(fan_select_offset, &val) < 0)
7270 return false;
7271 val &= 0xFEU;
7272 if (ec_write(fan_select_offset, val) < 0)
7273 return false;
7275 return true;
7278 /* Select secondary fan on X60/X61 */
7279 static bool fan_select_fan2(void)
7281 u8 val;
7283 if (!tp_features.second_fan)
7284 return false;
7286 if (ec_read(fan_select_offset, &val) < 0)
7287 return false;
7288 val |= 0x01U;
7289 if (ec_write(fan_select_offset, val) < 0)
7290 return false;
7292 return true;
7296 * Call with fan_mutex held
7298 static void fan_update_desired_level(u8 status)
7300 if ((status &
7301 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7302 if (status > 7)
7303 fan_control_desired_level = 7;
7304 else
7305 fan_control_desired_level = status;
7309 static int fan_get_status(u8 *status)
7311 u8 s;
7313 /* TODO:
7314 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7316 switch (fan_status_access_mode) {
7317 case TPACPI_FAN_RD_ACPI_GFAN:
7318 /* 570, 600e/x, 770e, 770x */
7320 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7321 return -EIO;
7323 if (likely(status))
7324 *status = s & 0x07;
7326 break;
7328 case TPACPI_FAN_RD_TPEC:
7329 /* all except 570, 600e/x, 770e, 770x */
7330 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7331 return -EIO;
7333 if (likely(status)) {
7334 *status = s;
7335 fan_quirk1_handle(status);
7338 break;
7340 default:
7341 return -ENXIO;
7344 return 0;
7347 static int fan_get_status_safe(u8 *status)
7349 int rc;
7350 u8 s;
7352 if (mutex_lock_killable(&fan_mutex))
7353 return -ERESTARTSYS;
7354 rc = fan_get_status(&s);
7355 if (!rc)
7356 fan_update_desired_level(s);
7357 mutex_unlock(&fan_mutex);
7359 if (status)
7360 *status = s;
7362 return rc;
7365 static int fan_get_speed(unsigned int *speed)
7367 u8 hi, lo;
7369 switch (fan_status_access_mode) {
7370 case TPACPI_FAN_RD_TPEC:
7371 /* all except 570, 600e/x, 770e, 770x */
7372 if (unlikely(!fan_select_fan1()))
7373 return -EIO;
7374 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7375 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7376 return -EIO;
7378 if (likely(speed))
7379 *speed = (hi << 8) | lo;
7381 break;
7383 default:
7384 return -ENXIO;
7387 return 0;
7390 static int fan2_get_speed(unsigned int *speed)
7392 u8 hi, lo;
7393 bool rc;
7395 switch (fan_status_access_mode) {
7396 case TPACPI_FAN_RD_TPEC:
7397 /* all except 570, 600e/x, 770e, 770x */
7398 if (unlikely(!fan_select_fan2()))
7399 return -EIO;
7400 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7401 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7402 fan_select_fan1(); /* play it safe */
7403 if (rc)
7404 return -EIO;
7406 if (likely(speed))
7407 *speed = (hi << 8) | lo;
7409 break;
7411 default:
7412 return -ENXIO;
7415 return 0;
7418 static int fan_set_level(int level)
7420 if (!fan_control_allowed)
7421 return -EPERM;
7423 switch (fan_control_access_mode) {
7424 case TPACPI_FAN_WR_ACPI_SFAN:
7425 if (level >= 0 && level <= 7) {
7426 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7427 return -EIO;
7428 } else
7429 return -EINVAL;
7430 break;
7432 case TPACPI_FAN_WR_ACPI_FANS:
7433 case TPACPI_FAN_WR_TPEC:
7434 if (!(level & TP_EC_FAN_AUTO) &&
7435 !(level & TP_EC_FAN_FULLSPEED) &&
7436 ((level < 0) || (level > 7)))
7437 return -EINVAL;
7439 /* safety net should the EC not support AUTO
7440 * or FULLSPEED mode bits and just ignore them */
7441 if (level & TP_EC_FAN_FULLSPEED)
7442 level |= 7; /* safety min speed 7 */
7443 else if (level & TP_EC_FAN_AUTO)
7444 level |= 4; /* safety min speed 4 */
7446 if (!acpi_ec_write(fan_status_offset, level))
7447 return -EIO;
7448 else
7449 tp_features.fan_ctrl_status_undef = 0;
7450 break;
7452 default:
7453 return -ENXIO;
7456 vdbg_printk(TPACPI_DBG_FAN,
7457 "fan control: set fan control register to 0x%02x\n", level);
7458 return 0;
7461 static int fan_set_level_safe(int level)
7463 int rc;
7465 if (!fan_control_allowed)
7466 return -EPERM;
7468 if (mutex_lock_killable(&fan_mutex))
7469 return -ERESTARTSYS;
7471 if (level == TPACPI_FAN_LAST_LEVEL)
7472 level = fan_control_desired_level;
7474 rc = fan_set_level(level);
7475 if (!rc)
7476 fan_update_desired_level(level);
7478 mutex_unlock(&fan_mutex);
7479 return rc;
7482 static int fan_set_enable(void)
7484 u8 s;
7485 int rc;
7487 if (!fan_control_allowed)
7488 return -EPERM;
7490 if (mutex_lock_killable(&fan_mutex))
7491 return -ERESTARTSYS;
7493 switch (fan_control_access_mode) {
7494 case TPACPI_FAN_WR_ACPI_FANS:
7495 case TPACPI_FAN_WR_TPEC:
7496 rc = fan_get_status(&s);
7497 if (rc < 0)
7498 break;
7500 /* Don't go out of emergency fan mode */
7501 if (s != 7) {
7502 s &= 0x07;
7503 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7506 if (!acpi_ec_write(fan_status_offset, s))
7507 rc = -EIO;
7508 else {
7509 tp_features.fan_ctrl_status_undef = 0;
7510 rc = 0;
7512 break;
7514 case TPACPI_FAN_WR_ACPI_SFAN:
7515 rc = fan_get_status(&s);
7516 if (rc < 0)
7517 break;
7519 s &= 0x07;
7521 /* Set fan to at least level 4 */
7522 s |= 4;
7524 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7525 rc = -EIO;
7526 else
7527 rc = 0;
7528 break;
7530 default:
7531 rc = -ENXIO;
7534 mutex_unlock(&fan_mutex);
7536 if (!rc)
7537 vdbg_printk(TPACPI_DBG_FAN,
7538 "fan control: set fan control register to 0x%02x\n",
7540 return rc;
7543 static int fan_set_disable(void)
7545 int rc;
7547 if (!fan_control_allowed)
7548 return -EPERM;
7550 if (mutex_lock_killable(&fan_mutex))
7551 return -ERESTARTSYS;
7553 rc = 0;
7554 switch (fan_control_access_mode) {
7555 case TPACPI_FAN_WR_ACPI_FANS:
7556 case TPACPI_FAN_WR_TPEC:
7557 if (!acpi_ec_write(fan_status_offset, 0x00))
7558 rc = -EIO;
7559 else {
7560 fan_control_desired_level = 0;
7561 tp_features.fan_ctrl_status_undef = 0;
7563 break;
7565 case TPACPI_FAN_WR_ACPI_SFAN:
7566 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7567 rc = -EIO;
7568 else
7569 fan_control_desired_level = 0;
7570 break;
7572 default:
7573 rc = -ENXIO;
7576 if (!rc)
7577 vdbg_printk(TPACPI_DBG_FAN,
7578 "fan control: set fan control register to 0\n");
7580 mutex_unlock(&fan_mutex);
7581 return rc;
7584 static int fan_set_speed(int speed)
7586 int rc;
7588 if (!fan_control_allowed)
7589 return -EPERM;
7591 if (mutex_lock_killable(&fan_mutex))
7592 return -ERESTARTSYS;
7594 rc = 0;
7595 switch (fan_control_access_mode) {
7596 case TPACPI_FAN_WR_ACPI_FANS:
7597 if (speed >= 0 && speed <= 65535) {
7598 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7599 speed, speed, speed))
7600 rc = -EIO;
7601 } else
7602 rc = -EINVAL;
7603 break;
7605 default:
7606 rc = -ENXIO;
7609 mutex_unlock(&fan_mutex);
7610 return rc;
7613 static void fan_watchdog_reset(void)
7615 static int fan_watchdog_active;
7617 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7618 return;
7620 if (fan_watchdog_active)
7621 cancel_delayed_work(&fan_watchdog_task);
7623 if (fan_watchdog_maxinterval > 0 &&
7624 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7625 fan_watchdog_active = 1;
7626 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7627 msecs_to_jiffies(fan_watchdog_maxinterval
7628 * 1000))) {
7629 printk(TPACPI_ERR
7630 "failed to queue the fan watchdog, "
7631 "watchdog will not trigger\n");
7633 } else
7634 fan_watchdog_active = 0;
7637 static void fan_watchdog_fire(struct work_struct *ignored)
7639 int rc;
7641 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7642 return;
7644 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7645 rc = fan_set_enable();
7646 if (rc < 0) {
7647 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7648 "will try again later...\n", -rc);
7649 /* reschedule for later */
7650 fan_watchdog_reset();
7655 * SYSFS fan layout: hwmon compatible (device)
7657 * pwm*_enable:
7658 * 0: "disengaged" mode
7659 * 1: manual mode
7660 * 2: native EC "auto" mode (recommended, hardware default)
7662 * pwm*: set speed in manual mode, ignored otherwise.
7663 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7664 * interpolation.
7666 * fan*_input: tachometer reading, RPM
7669 * SYSFS fan layout: extensions
7671 * fan_watchdog (driver):
7672 * fan watchdog interval in seconds, 0 disables (default), max 120
7675 /* sysfs fan pwm1_enable ----------------------------------------------- */
7676 static ssize_t fan_pwm1_enable_show(struct device *dev,
7677 struct device_attribute *attr,
7678 char *buf)
7680 int res, mode;
7681 u8 status;
7683 res = fan_get_status_safe(&status);
7684 if (res)
7685 return res;
7687 if (status & TP_EC_FAN_FULLSPEED) {
7688 mode = 0;
7689 } else if (status & TP_EC_FAN_AUTO) {
7690 mode = 2;
7691 } else
7692 mode = 1;
7694 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7697 static ssize_t fan_pwm1_enable_store(struct device *dev,
7698 struct device_attribute *attr,
7699 const char *buf, size_t count)
7701 unsigned long t;
7702 int res, level;
7704 if (parse_strtoul(buf, 2, &t))
7705 return -EINVAL;
7707 tpacpi_disclose_usertask("hwmon pwm1_enable",
7708 "set fan mode to %lu\n", t);
7710 switch (t) {
7711 case 0:
7712 level = TP_EC_FAN_FULLSPEED;
7713 break;
7714 case 1:
7715 level = TPACPI_FAN_LAST_LEVEL;
7716 break;
7717 case 2:
7718 level = TP_EC_FAN_AUTO;
7719 break;
7720 case 3:
7721 /* reserved for software-controlled auto mode */
7722 return -ENOSYS;
7723 default:
7724 return -EINVAL;
7727 res = fan_set_level_safe(level);
7728 if (res == -ENXIO)
7729 return -EINVAL;
7730 else if (res < 0)
7731 return res;
7733 fan_watchdog_reset();
7735 return count;
7738 static struct device_attribute dev_attr_fan_pwm1_enable =
7739 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7740 fan_pwm1_enable_show, fan_pwm1_enable_store);
7742 /* sysfs fan pwm1 ------------------------------------------------------ */
7743 static ssize_t fan_pwm1_show(struct device *dev,
7744 struct device_attribute *attr,
7745 char *buf)
7747 int res;
7748 u8 status;
7750 res = fan_get_status_safe(&status);
7751 if (res)
7752 return res;
7754 if ((status &
7755 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7756 status = fan_control_desired_level;
7758 if (status > 7)
7759 status = 7;
7761 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7764 static ssize_t fan_pwm1_store(struct device *dev,
7765 struct device_attribute *attr,
7766 const char *buf, size_t count)
7768 unsigned long s;
7769 int rc;
7770 u8 status, newlevel;
7772 if (parse_strtoul(buf, 255, &s))
7773 return -EINVAL;
7775 tpacpi_disclose_usertask("hwmon pwm1",
7776 "set fan speed to %lu\n", s);
7778 /* scale down from 0-255 to 0-7 */
7779 newlevel = (s >> 5) & 0x07;
7781 if (mutex_lock_killable(&fan_mutex))
7782 return -ERESTARTSYS;
7784 rc = fan_get_status(&status);
7785 if (!rc && (status &
7786 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7787 rc = fan_set_level(newlevel);
7788 if (rc == -ENXIO)
7789 rc = -EINVAL;
7790 else if (!rc) {
7791 fan_update_desired_level(newlevel);
7792 fan_watchdog_reset();
7796 mutex_unlock(&fan_mutex);
7797 return (rc)? rc : count;
7800 static struct device_attribute dev_attr_fan_pwm1 =
7801 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7802 fan_pwm1_show, fan_pwm1_store);
7804 /* sysfs fan fan1_input ------------------------------------------------ */
7805 static ssize_t fan_fan1_input_show(struct device *dev,
7806 struct device_attribute *attr,
7807 char *buf)
7809 int res;
7810 unsigned int speed;
7812 res = fan_get_speed(&speed);
7813 if (res < 0)
7814 return res;
7816 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7819 static struct device_attribute dev_attr_fan_fan1_input =
7820 __ATTR(fan1_input, S_IRUGO,
7821 fan_fan1_input_show, NULL);
7823 /* sysfs fan fan2_input ------------------------------------------------ */
7824 static ssize_t fan_fan2_input_show(struct device *dev,
7825 struct device_attribute *attr,
7826 char *buf)
7828 int res;
7829 unsigned int speed;
7831 res = fan2_get_speed(&speed);
7832 if (res < 0)
7833 return res;
7835 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7838 static struct device_attribute dev_attr_fan_fan2_input =
7839 __ATTR(fan2_input, S_IRUGO,
7840 fan_fan2_input_show, NULL);
7842 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7843 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7844 char *buf)
7846 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7849 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7850 const char *buf, size_t count)
7852 unsigned long t;
7854 if (parse_strtoul(buf, 120, &t))
7855 return -EINVAL;
7857 if (!fan_control_allowed)
7858 return -EPERM;
7860 fan_watchdog_maxinterval = t;
7861 fan_watchdog_reset();
7863 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7865 return count;
7868 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7869 fan_fan_watchdog_show, fan_fan_watchdog_store);
7871 /* --------------------------------------------------------------------- */
7872 static struct attribute *fan_attributes[] = {
7873 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7874 &dev_attr_fan_fan1_input.attr,
7875 NULL, /* for fan2_input */
7876 NULL
7879 static const struct attribute_group fan_attr_group = {
7880 .attrs = fan_attributes,
7883 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7884 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7886 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7887 { .vendor = PCI_VENDOR_ID_IBM, \
7888 .bios = TPACPI_MATCH_ANY, \
7889 .ec = TPID(__id1, __id2), \
7890 .quirks = __quirks }
7892 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7893 { .vendor = PCI_VENDOR_ID_LENOVO, \
7894 .bios = TPACPI_MATCH_ANY, \
7895 .ec = TPID(__id1, __id2), \
7896 .quirks = __quirks }
7898 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7899 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7900 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7901 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7902 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7903 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7906 #undef TPACPI_FAN_QL
7907 #undef TPACPI_FAN_QI
7909 static int __init fan_init(struct ibm_init_struct *iibm)
7911 int rc;
7912 unsigned long quirks;
7914 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7915 "initializing fan subdriver\n");
7917 mutex_init(&fan_mutex);
7918 fan_status_access_mode = TPACPI_FAN_NONE;
7919 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7920 fan_control_commands = 0;
7921 fan_watchdog_maxinterval = 0;
7922 tp_features.fan_ctrl_status_undef = 0;
7923 tp_features.second_fan = 0;
7924 fan_control_desired_level = 7;
7926 TPACPI_ACPIHANDLE_INIT(fans);
7927 TPACPI_ACPIHANDLE_INIT(gfan);
7928 TPACPI_ACPIHANDLE_INIT(sfan);
7930 quirks = tpacpi_check_quirks(fan_quirk_table,
7931 ARRAY_SIZE(fan_quirk_table));
7933 if (gfan_handle) {
7934 /* 570, 600e/x, 770e, 770x */
7935 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7936 } else {
7937 /* all other ThinkPads: note that even old-style
7938 * ThinkPad ECs supports the fan control register */
7939 if (likely(acpi_ec_read(fan_status_offset,
7940 &fan_control_initial_status))) {
7941 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7942 if (quirks & TPACPI_FAN_Q1)
7943 fan_quirk1_setup();
7944 if (quirks & TPACPI_FAN_2FAN) {
7945 tp_features.second_fan = 1;
7946 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7947 "secondary fan support enabled\n");
7949 } else {
7950 printk(TPACPI_ERR
7951 "ThinkPad ACPI EC access misbehaving, "
7952 "fan status and control unavailable\n");
7953 return 1;
7957 if (sfan_handle) {
7958 /* 570, 770x-JL */
7959 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7960 fan_control_commands |=
7961 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7962 } else {
7963 if (!gfan_handle) {
7964 /* gfan without sfan means no fan control */
7965 /* all other models implement TP EC 0x2f control */
7967 if (fans_handle) {
7968 /* X31, X40, X41 */
7969 fan_control_access_mode =
7970 TPACPI_FAN_WR_ACPI_FANS;
7971 fan_control_commands |=
7972 TPACPI_FAN_CMD_SPEED |
7973 TPACPI_FAN_CMD_LEVEL |
7974 TPACPI_FAN_CMD_ENABLE;
7975 } else {
7976 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7977 fan_control_commands |=
7978 TPACPI_FAN_CMD_LEVEL |
7979 TPACPI_FAN_CMD_ENABLE;
7984 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7985 "fan is %s, modes %d, %d\n",
7986 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7987 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7988 fan_status_access_mode, fan_control_access_mode);
7990 /* fan control master switch */
7991 if (!fan_control_allowed) {
7992 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7993 fan_control_commands = 0;
7994 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7995 "fan control features disabled by parameter\n");
7998 /* update fan_control_desired_level */
7999 if (fan_status_access_mode != TPACPI_FAN_NONE)
8000 fan_get_status_safe(NULL);
8002 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8003 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8004 if (tp_features.second_fan) {
8005 /* attach second fan tachometer */
8006 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8007 &dev_attr_fan_fan2_input.attr;
8009 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8010 &fan_attr_group);
8011 if (rc < 0)
8012 return rc;
8014 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8015 &driver_attr_fan_watchdog);
8016 if (rc < 0) {
8017 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8018 &fan_attr_group);
8019 return rc;
8021 return 0;
8022 } else
8023 return 1;
8026 static void fan_exit(void)
8028 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8029 "cancelling any pending fan watchdog tasks\n");
8031 /* FIXME: can we really do this unconditionally? */
8032 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8033 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8034 &driver_attr_fan_watchdog);
8036 cancel_delayed_work(&fan_watchdog_task);
8037 flush_workqueue(tpacpi_wq);
8040 static void fan_suspend(pm_message_t state)
8042 int rc;
8044 if (!fan_control_allowed)
8045 return;
8047 /* Store fan status in cache */
8048 fan_control_resume_level = 0;
8049 rc = fan_get_status_safe(&fan_control_resume_level);
8050 if (rc < 0)
8051 printk(TPACPI_NOTICE
8052 "failed to read fan level for later "
8053 "restore during resume: %d\n", rc);
8055 /* if it is undefined, don't attempt to restore it.
8056 * KEEP THIS LAST */
8057 if (tp_features.fan_ctrl_status_undef)
8058 fan_control_resume_level = 0;
8061 static void fan_resume(void)
8063 u8 current_level = 7;
8064 bool do_set = false;
8065 int rc;
8067 /* DSDT *always* updates status on resume */
8068 tp_features.fan_ctrl_status_undef = 0;
8070 if (!fan_control_allowed ||
8071 !fan_control_resume_level ||
8072 (fan_get_status_safe(&current_level) < 0))
8073 return;
8075 switch (fan_control_access_mode) {
8076 case TPACPI_FAN_WR_ACPI_SFAN:
8077 /* never decrease fan level */
8078 do_set = (fan_control_resume_level > current_level);
8079 break;
8080 case TPACPI_FAN_WR_ACPI_FANS:
8081 case TPACPI_FAN_WR_TPEC:
8082 /* never decrease fan level, scale is:
8083 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8085 * We expect the firmware to set either 7 or AUTO, but we
8086 * handle FULLSPEED out of paranoia.
8088 * So, we can safely only restore FULLSPEED or 7, anything
8089 * else could slow the fan. Restoring AUTO is useless, at
8090 * best that's exactly what the DSDT already set (it is the
8091 * slower it uses).
8093 * Always keep in mind that the DSDT *will* have set the
8094 * fans to what the vendor supposes is the best level. We
8095 * muck with it only to speed the fan up.
8097 if (fan_control_resume_level != 7 &&
8098 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8099 return;
8100 else
8101 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8102 (current_level != fan_control_resume_level);
8103 break;
8104 default:
8105 return;
8107 if (do_set) {
8108 printk(TPACPI_NOTICE
8109 "restoring fan level to 0x%02x\n",
8110 fan_control_resume_level);
8111 rc = fan_set_level_safe(fan_control_resume_level);
8112 if (rc < 0)
8113 printk(TPACPI_NOTICE
8114 "failed to restore fan level: %d\n", rc);
8118 static int fan_read(struct seq_file *m)
8120 int rc;
8121 u8 status;
8122 unsigned int speed = 0;
8124 switch (fan_status_access_mode) {
8125 case TPACPI_FAN_RD_ACPI_GFAN:
8126 /* 570, 600e/x, 770e, 770x */
8127 rc = fan_get_status_safe(&status);
8128 if (rc < 0)
8129 return rc;
8131 seq_printf(m, "status:\t\t%s\n"
8132 "level:\t\t%d\n",
8133 (status != 0) ? "enabled" : "disabled", status);
8134 break;
8136 case TPACPI_FAN_RD_TPEC:
8137 /* all except 570, 600e/x, 770e, 770x */
8138 rc = fan_get_status_safe(&status);
8139 if (rc < 0)
8140 return rc;
8142 seq_printf(m, "status:\t\t%s\n",
8143 (status != 0) ? "enabled" : "disabled");
8145 rc = fan_get_speed(&speed);
8146 if (rc < 0)
8147 return rc;
8149 seq_printf(m, "speed:\t\t%d\n", speed);
8151 if (status & TP_EC_FAN_FULLSPEED)
8152 /* Disengaged mode takes precedence */
8153 seq_printf(m, "level:\t\tdisengaged\n");
8154 else if (status & TP_EC_FAN_AUTO)
8155 seq_printf(m, "level:\t\tauto\n");
8156 else
8157 seq_printf(m, "level:\t\t%d\n", status);
8158 break;
8160 case TPACPI_FAN_NONE:
8161 default:
8162 seq_printf(m, "status:\t\tnot supported\n");
8165 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8166 seq_printf(m, "commands:\tlevel <level>");
8168 switch (fan_control_access_mode) {
8169 case TPACPI_FAN_WR_ACPI_SFAN:
8170 seq_printf(m, " (<level> is 0-7)\n");
8171 break;
8173 default:
8174 seq_printf(m, " (<level> is 0-7, "
8175 "auto, disengaged, full-speed)\n");
8176 break;
8180 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8181 seq_printf(m, "commands:\tenable, disable\n"
8182 "commands:\twatchdog <timeout> (<timeout> "
8183 "is 0 (off), 1-120 (seconds))\n");
8185 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8186 seq_printf(m, "commands:\tspeed <speed>"
8187 " (<speed> is 0-65535)\n");
8189 return 0;
8192 static int fan_write_cmd_level(const char *cmd, int *rc)
8194 int level;
8196 if (strlencmp(cmd, "level auto") == 0)
8197 level = TP_EC_FAN_AUTO;
8198 else if ((strlencmp(cmd, "level disengaged") == 0) |
8199 (strlencmp(cmd, "level full-speed") == 0))
8200 level = TP_EC_FAN_FULLSPEED;
8201 else if (sscanf(cmd, "level %d", &level) != 1)
8202 return 0;
8204 *rc = fan_set_level_safe(level);
8205 if (*rc == -ENXIO)
8206 printk(TPACPI_ERR "level command accepted for unsupported "
8207 "access mode %d", fan_control_access_mode);
8208 else if (!*rc)
8209 tpacpi_disclose_usertask("procfs fan",
8210 "set level to %d\n", level);
8212 return 1;
8215 static int fan_write_cmd_enable(const char *cmd, int *rc)
8217 if (strlencmp(cmd, "enable") != 0)
8218 return 0;
8220 *rc = fan_set_enable();
8221 if (*rc == -ENXIO)
8222 printk(TPACPI_ERR "enable command accepted for unsupported "
8223 "access mode %d", fan_control_access_mode);
8224 else if (!*rc)
8225 tpacpi_disclose_usertask("procfs fan", "enable\n");
8227 return 1;
8230 static int fan_write_cmd_disable(const char *cmd, int *rc)
8232 if (strlencmp(cmd, "disable") != 0)
8233 return 0;
8235 *rc = fan_set_disable();
8236 if (*rc == -ENXIO)
8237 printk(TPACPI_ERR "disable command accepted for unsupported "
8238 "access mode %d", fan_control_access_mode);
8239 else if (!*rc)
8240 tpacpi_disclose_usertask("procfs fan", "disable\n");
8242 return 1;
8245 static int fan_write_cmd_speed(const char *cmd, int *rc)
8247 int speed;
8249 /* TODO:
8250 * Support speed <low> <medium> <high> ? */
8252 if (sscanf(cmd, "speed %d", &speed) != 1)
8253 return 0;
8255 *rc = fan_set_speed(speed);
8256 if (*rc == -ENXIO)
8257 printk(TPACPI_ERR "speed command accepted for unsupported "
8258 "access mode %d", fan_control_access_mode);
8259 else if (!*rc)
8260 tpacpi_disclose_usertask("procfs fan",
8261 "set speed to %d\n", speed);
8263 return 1;
8266 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8268 int interval;
8270 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8271 return 0;
8273 if (interval < 0 || interval > 120)
8274 *rc = -EINVAL;
8275 else {
8276 fan_watchdog_maxinterval = interval;
8277 tpacpi_disclose_usertask("procfs fan",
8278 "set watchdog timer to %d\n",
8279 interval);
8282 return 1;
8285 static int fan_write(char *buf)
8287 char *cmd;
8288 int rc = 0;
8290 while (!rc && (cmd = next_cmd(&buf))) {
8291 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8292 fan_write_cmd_level(cmd, &rc)) &&
8293 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8294 (fan_write_cmd_enable(cmd, &rc) ||
8295 fan_write_cmd_disable(cmd, &rc) ||
8296 fan_write_cmd_watchdog(cmd, &rc))) &&
8297 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8298 fan_write_cmd_speed(cmd, &rc))
8300 rc = -EINVAL;
8301 else if (!rc)
8302 fan_watchdog_reset();
8305 return rc;
8308 static struct ibm_struct fan_driver_data = {
8309 .name = "fan",
8310 .read = fan_read,
8311 .write = fan_write,
8312 .exit = fan_exit,
8313 .suspend = fan_suspend,
8314 .resume = fan_resume,
8317 /****************************************************************************
8318 ****************************************************************************
8320 * Infrastructure
8322 ****************************************************************************
8323 ****************************************************************************/
8326 * HKEY event callout for other subdrivers go here
8327 * (yes, it is ugly, but it is quick, safe, and gets the job done
8329 static void tpacpi_driver_event(const unsigned int hkey_event)
8331 if (ibm_backlight_device) {
8332 switch (hkey_event) {
8333 case TP_HKEY_EV_BRGHT_UP:
8334 case TP_HKEY_EV_BRGHT_DOWN:
8335 tpacpi_brightness_notify_change();
8338 if (alsa_card) {
8339 switch (hkey_event) {
8340 case TP_HKEY_EV_VOL_UP:
8341 case TP_HKEY_EV_VOL_DOWN:
8342 case TP_HKEY_EV_VOL_MUTE:
8343 volume_alsa_notify_change();
8348 static void hotkey_driver_event(const unsigned int scancode)
8350 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8353 /* sysfs name ---------------------------------------------------------- */
8354 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8355 struct device_attribute *attr,
8356 char *buf)
8358 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8361 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8362 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8364 /* --------------------------------------------------------------------- */
8366 /* /proc support */
8367 static struct proc_dir_entry *proc_dir;
8370 * Module and infrastructure proble, init and exit handling
8373 static int force_load;
8375 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8376 static const char * __init str_supported(int is_supported)
8378 static char text_unsupported[] __initdata = "not supported";
8380 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8382 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8384 static void ibm_exit(struct ibm_struct *ibm)
8386 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8388 list_del_init(&ibm->all_drivers);
8390 if (ibm->flags.acpi_notify_installed) {
8391 dbg_printk(TPACPI_DBG_EXIT,
8392 "%s: acpi_remove_notify_handler\n", ibm->name);
8393 BUG_ON(!ibm->acpi);
8394 acpi_remove_notify_handler(*ibm->acpi->handle,
8395 ibm->acpi->type,
8396 dispatch_acpi_notify);
8397 ibm->flags.acpi_notify_installed = 0;
8398 ibm->flags.acpi_notify_installed = 0;
8401 if (ibm->flags.proc_created) {
8402 dbg_printk(TPACPI_DBG_EXIT,
8403 "%s: remove_proc_entry\n", ibm->name);
8404 remove_proc_entry(ibm->name, proc_dir);
8405 ibm->flags.proc_created = 0;
8408 if (ibm->flags.acpi_driver_registered) {
8409 dbg_printk(TPACPI_DBG_EXIT,
8410 "%s: acpi_bus_unregister_driver\n", ibm->name);
8411 BUG_ON(!ibm->acpi);
8412 acpi_bus_unregister_driver(ibm->acpi->driver);
8413 kfree(ibm->acpi->driver);
8414 ibm->acpi->driver = NULL;
8415 ibm->flags.acpi_driver_registered = 0;
8418 if (ibm->flags.init_called && ibm->exit) {
8419 ibm->exit();
8420 ibm->flags.init_called = 0;
8423 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8426 static int __init ibm_init(struct ibm_init_struct *iibm)
8428 int ret;
8429 struct ibm_struct *ibm = iibm->data;
8430 struct proc_dir_entry *entry;
8432 BUG_ON(ibm == NULL);
8434 INIT_LIST_HEAD(&ibm->all_drivers);
8436 if (ibm->flags.experimental && !experimental)
8437 return 0;
8439 dbg_printk(TPACPI_DBG_INIT,
8440 "probing for %s\n", ibm->name);
8442 if (iibm->init) {
8443 ret = iibm->init(iibm);
8444 if (ret > 0)
8445 return 0; /* probe failed */
8446 if (ret)
8447 return ret;
8449 ibm->flags.init_called = 1;
8452 if (ibm->acpi) {
8453 if (ibm->acpi->hid) {
8454 ret = register_tpacpi_subdriver(ibm);
8455 if (ret)
8456 goto err_out;
8459 if (ibm->acpi->notify) {
8460 ret = setup_acpi_notify(ibm);
8461 if (ret == -ENODEV) {
8462 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8463 ibm->name);
8464 ret = 0;
8465 goto err_out;
8467 if (ret < 0)
8468 goto err_out;
8472 dbg_printk(TPACPI_DBG_INIT,
8473 "%s installed\n", ibm->name);
8475 if (ibm->read) {
8476 mode_t mode;
8478 mode = S_IRUGO;
8479 if (ibm->write)
8480 mode |= S_IWUSR;
8481 entry = proc_create_data(ibm->name, mode, proc_dir,
8482 &dispatch_proc_fops, ibm);
8483 if (!entry) {
8484 printk(TPACPI_ERR "unable to create proc entry %s\n",
8485 ibm->name);
8486 ret = -ENODEV;
8487 goto err_out;
8489 ibm->flags.proc_created = 1;
8492 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8494 return 0;
8496 err_out:
8497 dbg_printk(TPACPI_DBG_INIT,
8498 "%s: at error exit path with result %d\n",
8499 ibm->name, ret);
8501 ibm_exit(ibm);
8502 return (ret < 0)? ret : 0;
8505 /* Probing */
8507 static bool __pure __init tpacpi_is_fw_digit(const char c)
8509 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8512 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8513 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8514 const char t)
8516 return s && strlen(s) >= 8 &&
8517 tpacpi_is_fw_digit(s[0]) &&
8518 tpacpi_is_fw_digit(s[1]) &&
8519 s[2] == t && s[3] == 'T' &&
8520 tpacpi_is_fw_digit(s[4]) &&
8521 tpacpi_is_fw_digit(s[5]) &&
8522 s[6] == 'W' && s[7] == 'W';
8525 /* returns 0 - probe ok, or < 0 - probe error.
8526 * Probe ok doesn't mean thinkpad found.
8527 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8528 static int __must_check __init get_thinkpad_model_data(
8529 struct thinkpad_id_data *tp)
8531 const struct dmi_device *dev = NULL;
8532 char ec_fw_string[18];
8533 char const *s;
8535 if (!tp)
8536 return -EINVAL;
8538 memset(tp, 0, sizeof(*tp));
8540 if (dmi_name_in_vendors("IBM"))
8541 tp->vendor = PCI_VENDOR_ID_IBM;
8542 else if (dmi_name_in_vendors("LENOVO"))
8543 tp->vendor = PCI_VENDOR_ID_LENOVO;
8544 else
8545 return 0;
8547 s = dmi_get_system_info(DMI_BIOS_VERSION);
8548 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8549 if (s && !tp->bios_version_str)
8550 return -ENOMEM;
8552 /* Really ancient ThinkPad 240X will fail this, which is fine */
8553 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8554 return 0;
8556 tp->bios_model = tp->bios_version_str[0]
8557 | (tp->bios_version_str[1] << 8);
8558 tp->bios_release = (tp->bios_version_str[4] << 8)
8559 | tp->bios_version_str[5];
8562 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8563 * X32 or newer, all Z series; Some models must have an
8564 * up-to-date BIOS or they will not be detected.
8566 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8568 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8569 if (sscanf(dev->name,
8570 "IBM ThinkPad Embedded Controller -[%17c",
8571 ec_fw_string) == 1) {
8572 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8573 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8575 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8576 if (!tp->ec_version_str)
8577 return -ENOMEM;
8579 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8580 tp->ec_model = ec_fw_string[0]
8581 | (ec_fw_string[1] << 8);
8582 tp->ec_release = (ec_fw_string[4] << 8)
8583 | ec_fw_string[5];
8584 } else {
8585 printk(TPACPI_NOTICE
8586 "ThinkPad firmware release %s "
8587 "doesn't match the known patterns\n",
8588 ec_fw_string);
8589 printk(TPACPI_NOTICE
8590 "please report this to %s\n",
8591 TPACPI_MAIL);
8593 break;
8597 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8598 if (s && !strnicmp(s, "ThinkPad", 8)) {
8599 tp->model_str = kstrdup(s, GFP_KERNEL);
8600 if (!tp->model_str)
8601 return -ENOMEM;
8604 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8605 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8606 if (s && !tp->nummodel_str)
8607 return -ENOMEM;
8609 return 0;
8612 static int __init probe_for_thinkpad(void)
8614 int is_thinkpad;
8616 if (acpi_disabled)
8617 return -ENODEV;
8620 * Non-ancient models have better DMI tagging, but very old models
8621 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8623 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8624 (thinkpad_id.ec_model != 0) ||
8625 tpacpi_is_fw_known();
8627 /* ec is required because many other handles are relative to it */
8628 TPACPI_ACPIHANDLE_INIT(ec);
8629 if (!ec_handle) {
8630 if (is_thinkpad)
8631 printk(TPACPI_ERR
8632 "Not yet supported ThinkPad detected!\n");
8633 return -ENODEV;
8636 if (!is_thinkpad && !force_load)
8637 return -ENODEV;
8639 return 0;
8643 /* Module init, exit, parameters */
8645 static struct ibm_init_struct ibms_init[] __initdata = {
8647 .init = thinkpad_acpi_driver_init,
8648 .data = &thinkpad_acpi_driver_data,
8651 .init = hotkey_init,
8652 .data = &hotkey_driver_data,
8655 .init = bluetooth_init,
8656 .data = &bluetooth_driver_data,
8659 .init = wan_init,
8660 .data = &wan_driver_data,
8663 .init = uwb_init,
8664 .data = &uwb_driver_data,
8666 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8668 .init = video_init,
8669 .data = &video_driver_data,
8671 #endif
8673 .init = light_init,
8674 .data = &light_driver_data,
8677 .init = cmos_init,
8678 .data = &cmos_driver_data,
8681 .init = led_init,
8682 .data = &led_driver_data,
8685 .init = beep_init,
8686 .data = &beep_driver_data,
8689 .init = thermal_init,
8690 .data = &thermal_driver_data,
8693 .data = &ecdump_driver_data,
8696 .init = brightness_init,
8697 .data = &brightness_driver_data,
8700 .init = volume_init,
8701 .data = &volume_driver_data,
8704 .init = fan_init,
8705 .data = &fan_driver_data,
8709 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8711 unsigned int i;
8712 struct ibm_struct *ibm;
8714 if (!kp || !kp->name || !val)
8715 return -EINVAL;
8717 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8718 ibm = ibms_init[i].data;
8719 WARN_ON(ibm == NULL);
8721 if (!ibm || !ibm->name)
8722 continue;
8724 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8725 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8726 return -ENOSPC;
8727 strcpy(ibms_init[i].param, val);
8728 strcat(ibms_init[i].param, ",");
8729 return 0;
8733 return -EINVAL;
8736 module_param(experimental, int, 0444);
8737 MODULE_PARM_DESC(experimental,
8738 "Enables experimental features when non-zero");
8740 module_param_named(debug, dbg_level, uint, 0);
8741 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8743 module_param(force_load, bool, 0444);
8744 MODULE_PARM_DESC(force_load,
8745 "Attempts to load the driver even on a "
8746 "mis-identified ThinkPad when true");
8748 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8749 MODULE_PARM_DESC(fan_control,
8750 "Enables setting fan parameters features when true");
8752 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8753 MODULE_PARM_DESC(brightness_mode,
8754 "Selects brightness control strategy: "
8755 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8757 module_param(brightness_enable, uint, 0444);
8758 MODULE_PARM_DESC(brightness_enable,
8759 "Enables backlight control when 1, disables when 0");
8761 module_param(hotkey_report_mode, uint, 0444);
8762 MODULE_PARM_DESC(hotkey_report_mode,
8763 "used for backwards compatibility with userspace, "
8764 "see documentation");
8766 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8767 module_param_named(volume_mode, volume_mode, uint, 0444);
8768 MODULE_PARM_DESC(volume_mode,
8769 "Selects volume control strategy: "
8770 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8772 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8773 MODULE_PARM_DESC(volume_capabilities,
8774 "Selects the mixer capabilites: "
8775 "0=auto, 1=volume and mute, 2=mute only");
8777 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8778 MODULE_PARM_DESC(volume_control,
8779 "Enables software override for the console audio "
8780 "control when true");
8782 /* ALSA module API parameters */
8783 module_param_named(index, alsa_index, int, 0444);
8784 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8785 module_param_named(id, alsa_id, charp, 0444);
8786 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8787 module_param_named(enable, alsa_enable, bool, 0444);
8788 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8789 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8791 #define TPACPI_PARAM(feature) \
8792 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8793 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8794 "at module load, see documentation")
8796 TPACPI_PARAM(hotkey);
8797 TPACPI_PARAM(bluetooth);
8798 TPACPI_PARAM(video);
8799 TPACPI_PARAM(light);
8800 TPACPI_PARAM(cmos);
8801 TPACPI_PARAM(led);
8802 TPACPI_PARAM(beep);
8803 TPACPI_PARAM(ecdump);
8804 TPACPI_PARAM(brightness);
8805 TPACPI_PARAM(volume);
8806 TPACPI_PARAM(fan);
8808 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8809 module_param(dbg_wlswemul, uint, 0444);
8810 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8811 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8812 MODULE_PARM_DESC(wlsw_state,
8813 "Initial state of the emulated WLSW switch");
8815 module_param(dbg_bluetoothemul, uint, 0444);
8816 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8817 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8818 MODULE_PARM_DESC(bluetooth_state,
8819 "Initial state of the emulated bluetooth switch");
8821 module_param(dbg_wwanemul, uint, 0444);
8822 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8823 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8824 MODULE_PARM_DESC(wwan_state,
8825 "Initial state of the emulated WWAN switch");
8827 module_param(dbg_uwbemul, uint, 0444);
8828 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8829 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8830 MODULE_PARM_DESC(uwb_state,
8831 "Initial state of the emulated UWB switch");
8832 #endif
8834 static void thinkpad_acpi_module_exit(void)
8836 struct ibm_struct *ibm, *itmp;
8838 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8840 list_for_each_entry_safe_reverse(ibm, itmp,
8841 &tpacpi_all_drivers,
8842 all_drivers) {
8843 ibm_exit(ibm);
8846 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8848 if (tpacpi_inputdev) {
8849 if (tp_features.input_device_registered)
8850 input_unregister_device(tpacpi_inputdev);
8851 else
8852 input_free_device(tpacpi_inputdev);
8855 if (tpacpi_hwmon)
8856 hwmon_device_unregister(tpacpi_hwmon);
8858 if (tp_features.sensors_pdev_attrs_registered)
8859 device_remove_file(&tpacpi_sensors_pdev->dev,
8860 &dev_attr_thinkpad_acpi_pdev_name);
8861 if (tpacpi_sensors_pdev)
8862 platform_device_unregister(tpacpi_sensors_pdev);
8863 if (tpacpi_pdev)
8864 platform_device_unregister(tpacpi_pdev);
8866 if (tp_features.sensors_pdrv_attrs_registered)
8867 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8868 if (tp_features.platform_drv_attrs_registered)
8869 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8871 if (tp_features.sensors_pdrv_registered)
8872 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8874 if (tp_features.platform_drv_registered)
8875 platform_driver_unregister(&tpacpi_pdriver);
8877 if (proc_dir)
8878 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8880 if (tpacpi_wq)
8881 destroy_workqueue(tpacpi_wq);
8883 kfree(thinkpad_id.bios_version_str);
8884 kfree(thinkpad_id.ec_version_str);
8885 kfree(thinkpad_id.model_str);
8889 static int __init thinkpad_acpi_module_init(void)
8891 int ret, i;
8893 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8895 /* Parameter checking */
8896 if (hotkey_report_mode > 2)
8897 return -EINVAL;
8899 /* Driver-level probe */
8901 ret = get_thinkpad_model_data(&thinkpad_id);
8902 if (ret) {
8903 printk(TPACPI_ERR
8904 "unable to get DMI data: %d\n", ret);
8905 thinkpad_acpi_module_exit();
8906 return ret;
8908 ret = probe_for_thinkpad();
8909 if (ret) {
8910 thinkpad_acpi_module_exit();
8911 return ret;
8914 /* Driver initialization */
8916 TPACPI_ACPIHANDLE_INIT(ecrd);
8917 TPACPI_ACPIHANDLE_INIT(ecwr);
8919 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8920 if (!tpacpi_wq) {
8921 thinkpad_acpi_module_exit();
8922 return -ENOMEM;
8925 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8926 if (!proc_dir) {
8927 printk(TPACPI_ERR
8928 "unable to create proc dir " TPACPI_PROC_DIR);
8929 thinkpad_acpi_module_exit();
8930 return -ENODEV;
8932 proc_dir->owner = THIS_MODULE;
8934 ret = platform_driver_register(&tpacpi_pdriver);
8935 if (ret) {
8936 printk(TPACPI_ERR
8937 "unable to register main platform driver\n");
8938 thinkpad_acpi_module_exit();
8939 return ret;
8941 tp_features.platform_drv_registered = 1;
8943 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8944 if (ret) {
8945 printk(TPACPI_ERR
8946 "unable to register hwmon platform driver\n");
8947 thinkpad_acpi_module_exit();
8948 return ret;
8950 tp_features.sensors_pdrv_registered = 1;
8952 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8953 if (!ret) {
8954 tp_features.platform_drv_attrs_registered = 1;
8955 ret = tpacpi_create_driver_attributes(
8956 &tpacpi_hwmon_pdriver.driver);
8958 if (ret) {
8959 printk(TPACPI_ERR
8960 "unable to create sysfs driver attributes\n");
8961 thinkpad_acpi_module_exit();
8962 return ret;
8964 tp_features.sensors_pdrv_attrs_registered = 1;
8967 /* Device initialization */
8968 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8969 NULL, 0);
8970 if (IS_ERR(tpacpi_pdev)) {
8971 ret = PTR_ERR(tpacpi_pdev);
8972 tpacpi_pdev = NULL;
8973 printk(TPACPI_ERR "unable to register platform device\n");
8974 thinkpad_acpi_module_exit();
8975 return ret;
8977 tpacpi_sensors_pdev = platform_device_register_simple(
8978 TPACPI_HWMON_DRVR_NAME,
8979 -1, NULL, 0);
8980 if (IS_ERR(tpacpi_sensors_pdev)) {
8981 ret = PTR_ERR(tpacpi_sensors_pdev);
8982 tpacpi_sensors_pdev = NULL;
8983 printk(TPACPI_ERR
8984 "unable to register hwmon platform device\n");
8985 thinkpad_acpi_module_exit();
8986 return ret;
8988 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8989 &dev_attr_thinkpad_acpi_pdev_name);
8990 if (ret) {
8991 printk(TPACPI_ERR
8992 "unable to create sysfs hwmon device attributes\n");
8993 thinkpad_acpi_module_exit();
8994 return ret;
8996 tp_features.sensors_pdev_attrs_registered = 1;
8997 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8998 if (IS_ERR(tpacpi_hwmon)) {
8999 ret = PTR_ERR(tpacpi_hwmon);
9000 tpacpi_hwmon = NULL;
9001 printk(TPACPI_ERR "unable to register hwmon device\n");
9002 thinkpad_acpi_module_exit();
9003 return ret;
9005 mutex_init(&tpacpi_inputdev_send_mutex);
9006 tpacpi_inputdev = input_allocate_device();
9007 if (!tpacpi_inputdev) {
9008 printk(TPACPI_ERR "unable to allocate input device\n");
9009 thinkpad_acpi_module_exit();
9010 return -ENOMEM;
9011 } else {
9012 /* Prepare input device, but don't register */
9013 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9014 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9015 tpacpi_inputdev->id.bustype = BUS_HOST;
9016 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9017 thinkpad_id.vendor :
9018 PCI_VENDOR_ID_IBM;
9019 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9020 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9021 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9023 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9024 ret = ibm_init(&ibms_init[i]);
9025 if (ret >= 0 && *ibms_init[i].param)
9026 ret = ibms_init[i].data->write(ibms_init[i].param);
9027 if (ret < 0) {
9028 thinkpad_acpi_module_exit();
9029 return ret;
9033 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9035 ret = input_register_device(tpacpi_inputdev);
9036 if (ret < 0) {
9037 printk(TPACPI_ERR "unable to register input device\n");
9038 thinkpad_acpi_module_exit();
9039 return ret;
9040 } else {
9041 tp_features.input_device_registered = 1;
9044 return 0;
9047 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9050 * This will autoload the driver in almost every ThinkPad
9051 * in widespread use.
9053 * Only _VERY_ old models, like the 240, 240x and 570 lack
9054 * the HKEY event interface.
9056 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9059 * DMI matching for module autoloading
9061 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9062 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9064 * Only models listed in thinkwiki will be supported, so add yours
9065 * if it is not there yet.
9067 #define IBM_BIOS_MODULE_ALIAS(__type) \
9068 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9070 /* Ancient thinkpad BIOSes have to be identified by
9071 * BIOS type or model number, and there are far less
9072 * BIOS types than model numbers... */
9073 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9075 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9076 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9077 MODULE_DESCRIPTION(TPACPI_DESC);
9078 MODULE_VERSION(TPACPI_VERSION);
9079 MODULE_LICENSE("GPL");
9081 module_init(thinkpad_acpi_module_init);
9082 module_exit(thinkpad_acpi_module_exit);