ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob577d61527162149b3ae7c02ed64933c16957c783
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-20100220"
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 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_16levels:1;
309 u32 bright_acpimode:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
373 static int dbg_wlswemul;
374 static int tpacpi_wlsw_emulstate;
375 static int dbg_bluetoothemul;
376 static int tpacpi_bluetooth_emulstate;
377 static int dbg_wwanemul;
378 static int tpacpi_wwan_emulstate;
379 static int dbg_uwbemul;
380 static int tpacpi_uwb_emulstate;
381 #endif
384 /*************************************************************************
385 * Debugging helpers
388 #define dbg_printk(a_dbg_level, format, arg...) \
389 do { if (dbg_level & (a_dbg_level)) \
390 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
391 } while (0)
393 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
394 #define vdbg_printk dbg_printk
395 static const char *str_supported(int is_supported);
396 #else
397 #define vdbg_printk(a_dbg_level, format, arg...) \
398 do { } while (0)
399 #endif
401 static void tpacpi_log_usertask(const char * const what)
403 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
404 what, task_tgid_vnr(current));
407 #define tpacpi_disclose_usertask(what, format, arg...) \
408 do { \
409 if (unlikely( \
410 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
411 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
412 printk(TPACPI_DEBUG "%s: PID %d: " format, \
413 what, task_tgid_vnr(current), ## arg); \
415 } while (0)
418 * Quirk handling helpers
420 * ThinkPad IDs and versions seen in the field so far
421 * are two-characters from the set [0-9A-Z], i.e. base 36.
423 * We use values well outside that range as specials.
426 #define TPACPI_MATCH_ANY 0xffffU
427 #define TPACPI_MATCH_UNKNOWN 0U
429 /* TPID('1', 'Y') == 0x5931 */
430 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
432 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
433 { .vendor = PCI_VENDOR_ID_IBM, \
434 .bios = TPID(__id1, __id2), \
435 .ec = TPACPI_MATCH_ANY, \
436 .quirks = (__quirk) }
438 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
439 { .vendor = PCI_VENDOR_ID_LENOVO, \
440 .bios = TPID(__id1, __id2), \
441 .ec = TPACPI_MATCH_ANY, \
442 .quirks = (__quirk) }
444 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
445 { .vendor = PCI_VENDOR_ID_LENOVO, \
446 .bios = TPACPI_MATCH_ANY, \
447 .ec = TPID(__id1, __id2), \
448 .quirks = (__quirk) }
450 struct tpacpi_quirk {
451 unsigned int vendor;
452 u16 bios;
453 u16 ec;
454 unsigned long quirks;
458 * tpacpi_check_quirks() - search BIOS/EC version on a list
459 * @qlist: array of &struct tpacpi_quirk
460 * @qlist_size: number of elements in @qlist
462 * Iterates over a quirks list until one is found that matches the
463 * ThinkPad's vendor, BIOS and EC model.
465 * Returns 0 if nothing matches, otherwise returns the quirks field of
466 * the matching &struct tpacpi_quirk entry.
468 * The match criteria is: vendor, ec and bios much match.
470 static unsigned long __init tpacpi_check_quirks(
471 const struct tpacpi_quirk *qlist,
472 unsigned int qlist_size)
474 while (qlist_size) {
475 if ((qlist->vendor == thinkpad_id.vendor ||
476 qlist->vendor == TPACPI_MATCH_ANY) &&
477 (qlist->bios == thinkpad_id.bios_model ||
478 qlist->bios == TPACPI_MATCH_ANY) &&
479 (qlist->ec == thinkpad_id.ec_model ||
480 qlist->ec == TPACPI_MATCH_ANY))
481 return qlist->quirks;
483 qlist_size--;
484 qlist++;
486 return 0;
490 /****************************************************************************
491 ****************************************************************************
493 * ACPI Helpers and device model
495 ****************************************************************************
496 ****************************************************************************/
498 /*************************************************************************
499 * ACPI basic handles
502 static acpi_handle root_handle;
504 #define TPACPI_HANDLE(object, parent, paths...) \
505 static acpi_handle object##_handle; \
506 static acpi_handle *object##_parent = &parent##_handle; \
507 static char *object##_path; \
508 static char *object##_paths[] = { paths }
510 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
511 "\\_SB.PCI.ISA.EC", /* 570 */
512 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
513 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
514 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
515 "\\_SB.PCI0.ICH3.EC0", /* R31 */
516 "\\_SB.PCI0.LPC.EC", /* all others */
519 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
520 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
522 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
523 /* T4x, X31, X40 */
524 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
525 "\\CMS", /* R40, R40e */
526 ); /* all others */
528 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
529 "^HKEY", /* R30, R31 */
530 "HKEY", /* all others */
531 ); /* 570 */
533 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
534 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
535 "\\_SB.PCI0.VID0", /* 770e */
536 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
537 "\\_SB.PCI0.AGP.VID", /* all others */
538 ); /* R30, R31 */
541 /*************************************************************************
542 * ACPI helpers
545 static int acpi_evalf(acpi_handle handle,
546 void *res, char *method, char *fmt, ...)
548 char *fmt0 = fmt;
549 struct acpi_object_list params;
550 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
551 struct acpi_buffer result, *resultp;
552 union acpi_object out_obj;
553 acpi_status status;
554 va_list ap;
555 char res_type;
556 int success;
557 int quiet;
559 if (!*fmt) {
560 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
561 return 0;
564 if (*fmt == 'q') {
565 quiet = 1;
566 fmt++;
567 } else
568 quiet = 0;
570 res_type = *(fmt++);
572 params.count = 0;
573 params.pointer = &in_objs[0];
575 va_start(ap, fmt);
576 while (*fmt) {
577 char c = *(fmt++);
578 switch (c) {
579 case 'd': /* int */
580 in_objs[params.count].integer.value = va_arg(ap, int);
581 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
582 break;
583 /* add more types as needed */
584 default:
585 printk(TPACPI_ERR "acpi_evalf() called "
586 "with invalid format character '%c'\n", c);
587 return 0;
590 va_end(ap);
592 if (res_type != 'v') {
593 result.length = sizeof(out_obj);
594 result.pointer = &out_obj;
595 resultp = &result;
596 } else
597 resultp = NULL;
599 status = acpi_evaluate_object(handle, method, &params, resultp);
601 switch (res_type) {
602 case 'd': /* int */
603 if (res)
604 *(int *)res = out_obj.integer.value;
605 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
606 break;
607 case 'v': /* void */
608 success = status == AE_OK;
609 break;
610 /* add more types as needed */
611 default:
612 printk(TPACPI_ERR "acpi_evalf() called "
613 "with invalid format character '%c'\n", res_type);
614 return 0;
617 if (!success && !quiet)
618 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
619 method, fmt0, status);
621 return success;
624 static int acpi_ec_read(int i, u8 *p)
626 int v;
628 if (ecrd_handle) {
629 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
630 return 0;
631 *p = v;
632 } else {
633 if (ec_read(i, p) < 0)
634 return 0;
637 return 1;
640 static int acpi_ec_write(int i, u8 v)
642 if (ecwr_handle) {
643 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
644 return 0;
645 } else {
646 if (ec_write(i, v) < 0)
647 return 0;
650 return 1;
653 static int issue_thinkpad_cmos_command(int cmos_cmd)
655 if (!cmos_handle)
656 return -ENXIO;
658 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
659 return -EIO;
661 return 0;
664 /*************************************************************************
665 * ACPI device model
668 #define TPACPI_ACPIHANDLE_INIT(object) \
669 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
670 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
672 static void drv_acpi_handle_init(char *name,
673 acpi_handle *handle, acpi_handle parent,
674 char **paths, int num_paths, char **path)
676 int i;
677 acpi_status status;
679 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
680 name);
682 for (i = 0; i < num_paths; i++) {
683 status = acpi_get_handle(parent, paths[i], handle);
684 if (ACPI_SUCCESS(status)) {
685 *path = paths[i];
686 dbg_printk(TPACPI_DBG_INIT,
687 "Found ACPI handle %s for %s\n",
688 *path, name);
689 return;
693 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
694 name);
695 *handle = NULL;
698 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
700 struct ibm_struct *ibm = data;
702 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
703 return;
705 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
706 return;
708 ibm->acpi->notify(ibm, event);
711 static int __init setup_acpi_notify(struct ibm_struct *ibm)
713 acpi_status status;
714 int rc;
716 BUG_ON(!ibm->acpi);
718 if (!*ibm->acpi->handle)
719 return 0;
721 vdbg_printk(TPACPI_DBG_INIT,
722 "setting up ACPI notify for %s\n", ibm->name);
724 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
725 if (rc < 0) {
726 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
727 ibm->name, rc);
728 return -ENODEV;
731 ibm->acpi->device->driver_data = ibm;
732 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
733 TPACPI_ACPI_EVENT_PREFIX,
734 ibm->name);
736 status = acpi_install_notify_handler(*ibm->acpi->handle,
737 ibm->acpi->type, dispatch_acpi_notify, ibm);
738 if (ACPI_FAILURE(status)) {
739 if (status == AE_ALREADY_EXISTS) {
740 printk(TPACPI_NOTICE
741 "another device driver is already "
742 "handling %s events\n", ibm->name);
743 } else {
744 printk(TPACPI_ERR
745 "acpi_install_notify_handler(%s) failed: %d\n",
746 ibm->name, status);
748 return -ENODEV;
750 ibm->flags.acpi_notify_installed = 1;
751 return 0;
754 static int __init tpacpi_device_add(struct acpi_device *device)
756 return 0;
759 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
761 int rc;
763 dbg_printk(TPACPI_DBG_INIT,
764 "registering %s as an ACPI driver\n", ibm->name);
766 BUG_ON(!ibm->acpi);
768 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
769 if (!ibm->acpi->driver) {
770 printk(TPACPI_ERR
771 "failed to allocate memory for ibm->acpi->driver\n");
772 return -ENOMEM;
775 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
776 ibm->acpi->driver->ids = ibm->acpi->hid;
778 ibm->acpi->driver->ops.add = &tpacpi_device_add;
780 rc = acpi_bus_register_driver(ibm->acpi->driver);
781 if (rc < 0) {
782 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
783 ibm->name, rc);
784 kfree(ibm->acpi->driver);
785 ibm->acpi->driver = NULL;
786 } else if (!rc)
787 ibm->flags.acpi_driver_registered = 1;
789 return rc;
793 /****************************************************************************
794 ****************************************************************************
796 * Procfs Helpers
798 ****************************************************************************
799 ****************************************************************************/
801 static int dispatch_proc_show(struct seq_file *m, void *v)
803 struct ibm_struct *ibm = m->private;
805 if (!ibm || !ibm->read)
806 return -EINVAL;
807 return ibm->read(m);
810 static int dispatch_proc_open(struct inode *inode, struct file *file)
812 return single_open(file, dispatch_proc_show, PDE(inode)->data);
815 static ssize_t dispatch_proc_write(struct file *file,
816 const char __user *userbuf,
817 size_t count, loff_t *pos)
819 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
820 char *kernbuf;
821 int ret;
823 if (!ibm || !ibm->write)
824 return -EINVAL;
825 if (count > PAGE_SIZE - 2)
826 return -EINVAL;
828 kernbuf = kmalloc(count + 2, GFP_KERNEL);
829 if (!kernbuf)
830 return -ENOMEM;
832 if (copy_from_user(kernbuf, userbuf, count)) {
833 kfree(kernbuf);
834 return -EFAULT;
837 kernbuf[count] = 0;
838 strcat(kernbuf, ",");
839 ret = ibm->write(kernbuf);
840 if (ret == 0)
841 ret = count;
843 kfree(kernbuf);
845 return ret;
848 static const struct file_operations dispatch_proc_fops = {
849 .owner = THIS_MODULE,
850 .open = dispatch_proc_open,
851 .read = seq_read,
852 .llseek = seq_lseek,
853 .release = single_release,
854 .write = dispatch_proc_write,
857 static char *next_cmd(char **cmds)
859 char *start = *cmds;
860 char *end;
862 while ((end = strchr(start, ',')) && end == start)
863 start = end + 1;
865 if (!end)
866 return NULL;
868 *end = 0;
869 *cmds = end + 1;
870 return start;
874 /****************************************************************************
875 ****************************************************************************
877 * Device model: input, hwmon and platform
879 ****************************************************************************
880 ****************************************************************************/
882 static struct platform_device *tpacpi_pdev;
883 static struct platform_device *tpacpi_sensors_pdev;
884 static struct device *tpacpi_hwmon;
885 static struct input_dev *tpacpi_inputdev;
886 static struct mutex tpacpi_inputdev_send_mutex;
887 static LIST_HEAD(tpacpi_all_drivers);
889 static int tpacpi_suspend_handler(struct platform_device *pdev,
890 pm_message_t state)
892 struct ibm_struct *ibm, *itmp;
894 list_for_each_entry_safe(ibm, itmp,
895 &tpacpi_all_drivers,
896 all_drivers) {
897 if (ibm->suspend)
898 (ibm->suspend)(state);
901 return 0;
904 static int tpacpi_resume_handler(struct platform_device *pdev)
906 struct ibm_struct *ibm, *itmp;
908 list_for_each_entry_safe(ibm, itmp,
909 &tpacpi_all_drivers,
910 all_drivers) {
911 if (ibm->resume)
912 (ibm->resume)();
915 return 0;
918 static void tpacpi_shutdown_handler(struct platform_device *pdev)
920 struct ibm_struct *ibm, *itmp;
922 list_for_each_entry_safe(ibm, itmp,
923 &tpacpi_all_drivers,
924 all_drivers) {
925 if (ibm->shutdown)
926 (ibm->shutdown)();
930 static struct platform_driver tpacpi_pdriver = {
931 .driver = {
932 .name = TPACPI_DRVR_NAME,
933 .owner = THIS_MODULE,
935 .suspend = tpacpi_suspend_handler,
936 .resume = tpacpi_resume_handler,
937 .shutdown = tpacpi_shutdown_handler,
940 static struct platform_driver tpacpi_hwmon_pdriver = {
941 .driver = {
942 .name = TPACPI_HWMON_DRVR_NAME,
943 .owner = THIS_MODULE,
947 /*************************************************************************
948 * sysfs support helpers
951 struct attribute_set {
952 unsigned int members, max_members;
953 struct attribute_group group;
956 struct attribute_set_obj {
957 struct attribute_set s;
958 struct attribute *a;
959 } __attribute__((packed));
961 static struct attribute_set *create_attr_set(unsigned int max_members,
962 const char *name)
964 struct attribute_set_obj *sobj;
966 if (max_members == 0)
967 return NULL;
969 /* Allocates space for implicit NULL at the end too */
970 sobj = kzalloc(sizeof(struct attribute_set_obj) +
971 max_members * sizeof(struct attribute *),
972 GFP_KERNEL);
973 if (!sobj)
974 return NULL;
975 sobj->s.max_members = max_members;
976 sobj->s.group.attrs = &sobj->a;
977 sobj->s.group.name = name;
979 return &sobj->s;
982 #define destroy_attr_set(_set) \
983 kfree(_set);
985 /* not multi-threaded safe, use it in a single thread per set */
986 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
988 if (!s || !attr)
989 return -EINVAL;
991 if (s->members >= s->max_members)
992 return -ENOMEM;
994 s->group.attrs[s->members] = attr;
995 s->members++;
997 return 0;
1000 static int add_many_to_attr_set(struct attribute_set *s,
1001 struct attribute **attr,
1002 unsigned int count)
1004 int i, res;
1006 for (i = 0; i < count; i++) {
1007 res = add_to_attr_set(s, attr[i]);
1008 if (res)
1009 return res;
1012 return 0;
1015 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1017 sysfs_remove_group(kobj, &s->group);
1018 destroy_attr_set(s);
1021 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1022 sysfs_create_group(_kobj, &_attr_set->group)
1024 static int parse_strtoul(const char *buf,
1025 unsigned long max, unsigned long *value)
1027 char *endp;
1029 while (*buf && isspace(*buf))
1030 buf++;
1031 *value = simple_strtoul(buf, &endp, 0);
1032 while (*endp && isspace(*endp))
1033 endp++;
1034 if (*endp || *value > max)
1035 return -EINVAL;
1037 return 0;
1040 static void tpacpi_disable_brightness_delay(void)
1042 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1043 printk(TPACPI_NOTICE
1044 "ACPI backlight control delay disabled\n");
1047 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1049 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1050 union acpi_object *obj;
1051 int rc;
1053 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1054 obj = (union acpi_object *)buffer.pointer;
1055 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1056 printk(TPACPI_ERR "Unknown _BCL data, "
1057 "please report this to %s\n", TPACPI_MAIL);
1058 rc = 0;
1059 } else {
1060 rc = obj->package.count;
1062 } else {
1063 return 0;
1066 kfree(buffer.pointer);
1067 return rc;
1070 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1071 u32 lvl, void *context, void **rv)
1073 char name[ACPI_PATH_SEGMENT_LENGTH];
1074 struct acpi_buffer buffer = { sizeof(name), &name };
1076 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1077 !strncmp("_BCL", name, sizeof(name) - 1)) {
1078 BUG_ON(!rv || !*rv);
1079 **(int **)rv = tpacpi_query_bcl_levels(handle);
1080 return AE_CTRL_TERMINATE;
1081 } else {
1082 return AE_OK;
1087 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1089 static int __init tpacpi_check_std_acpi_brightness_support(void)
1091 int status;
1092 int bcl_levels = 0;
1093 void *bcl_ptr = &bcl_levels;
1095 if (!vid_handle) {
1096 TPACPI_ACPIHANDLE_INIT(vid);
1098 if (!vid_handle)
1099 return 0;
1102 * Search for a _BCL method, and execute it. This is safe on all
1103 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1104 * BIOS in ACPI backlight control mode. We do NOT have to care
1105 * about calling the _BCL method in an enabled video device, any
1106 * will do for our purposes.
1109 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1110 tpacpi_acpi_walk_find_bcl, NULL,
1111 &bcl_ptr);
1113 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1114 tp_features.bright_acpimode = 1;
1115 return (bcl_levels - 2);
1118 return 0;
1121 static int __init tpacpi_new_rfkill(const unsigned int id,
1122 struct rfkill **rfk,
1123 const enum rfkill_type rfktype,
1124 const char *name,
1125 const bool set_default,
1126 int (*toggle_radio)(void *, enum rfkill_state),
1127 int (*get_state)(void *, enum rfkill_state *))
1129 int res;
1130 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1132 res = get_state(NULL, &initial_state);
1133 if (res < 0) {
1134 printk(TPACPI_ERR
1135 "failed to read initial state for %s, error %d; "
1136 "will turn radio off\n", name, res);
1137 } else if (set_default) {
1138 /* try to set the initial state as the default for the rfkill
1139 * type, since we ask the firmware to preserve it across S5 in
1140 * NVRAM */
1141 if (rfkill_set_default(rfktype,
1142 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1143 RFKILL_STATE_UNBLOCKED :
1144 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1145 vdbg_printk(TPACPI_DBG_RFKILL,
1146 "Default state for %s cannot be changed\n",
1147 name);
1150 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1151 if (!*rfk) {
1152 printk(TPACPI_ERR
1153 "failed to allocate memory for rfkill class\n");
1154 return -ENOMEM;
1157 (*rfk)->name = name;
1158 (*rfk)->get_state = get_state;
1159 (*rfk)->toggle_radio = toggle_radio;
1160 (*rfk)->state = initial_state;
1162 res = rfkill_register(*rfk);
1163 if (res < 0) {
1164 printk(TPACPI_ERR
1165 "failed to register %s rfkill switch: %d\n",
1166 name, res);
1167 rfkill_free(*rfk);
1168 *rfk = NULL;
1169 return res;
1172 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1173 name, (initial_state == RFKILL_STATE_UNBLOCKED) ?
1174 "un" : (initial_state == RFKILL_STATE_SOFT_BLOCKED) ?
1175 "soft-" : "hard-");
1176 return 0;
1179 static void printk_deprecated_attribute(const char * const what,
1180 const char * const details)
1182 tpacpi_log_usertask("deprecated sysfs attribute");
1183 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1184 "will be removed. %s\n",
1185 what, details);
1188 static void printk_deprecated_rfkill_attribute(const char * const what)
1190 printk_deprecated_attribute(what,
1191 "Please switch to generic rfkill before year 2010");
1194 /*************************************************************************
1195 * thinkpad-acpi driver attributes
1198 /* interface_version --------------------------------------------------- */
1199 static ssize_t tpacpi_driver_interface_version_show(
1200 struct device_driver *drv,
1201 char *buf)
1203 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1206 static DRIVER_ATTR(interface_version, S_IRUGO,
1207 tpacpi_driver_interface_version_show, NULL);
1209 /* debug_level --------------------------------------------------------- */
1210 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1211 char *buf)
1213 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1216 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1217 const char *buf, size_t count)
1219 unsigned long t;
1221 if (parse_strtoul(buf, 0xffff, &t))
1222 return -EINVAL;
1224 dbg_level = t;
1226 return count;
1229 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1230 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1232 /* version ------------------------------------------------------------- */
1233 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1234 char *buf)
1236 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1237 TPACPI_DESC, TPACPI_VERSION);
1240 static DRIVER_ATTR(version, S_IRUGO,
1241 tpacpi_driver_version_show, NULL);
1243 /* --------------------------------------------------------------------- */
1245 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1247 static void tpacpi_send_radiosw_update(void);
1249 /* wlsw_emulstate ------------------------------------------------------ */
1250 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1251 char *buf)
1253 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1256 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1257 const char *buf, size_t count)
1259 unsigned long t;
1261 if (parse_strtoul(buf, 1, &t))
1262 return -EINVAL;
1264 if (tpacpi_wlsw_emulstate != t) {
1265 tpacpi_wlsw_emulstate = !!t;
1266 tpacpi_send_radiosw_update();
1267 } else
1268 tpacpi_wlsw_emulstate = !!t;
1270 return count;
1273 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1274 tpacpi_driver_wlsw_emulstate_show,
1275 tpacpi_driver_wlsw_emulstate_store);
1277 /* bluetooth_emulstate ------------------------------------------------- */
1278 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1279 struct device_driver *drv,
1280 char *buf)
1282 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1285 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1286 struct device_driver *drv,
1287 const char *buf, size_t count)
1289 unsigned long t;
1291 if (parse_strtoul(buf, 1, &t))
1292 return -EINVAL;
1294 tpacpi_bluetooth_emulstate = !!t;
1296 return count;
1299 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1300 tpacpi_driver_bluetooth_emulstate_show,
1301 tpacpi_driver_bluetooth_emulstate_store);
1303 /* wwan_emulstate ------------------------------------------------- */
1304 static ssize_t tpacpi_driver_wwan_emulstate_show(
1305 struct device_driver *drv,
1306 char *buf)
1308 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1311 static ssize_t tpacpi_driver_wwan_emulstate_store(
1312 struct device_driver *drv,
1313 const char *buf, size_t count)
1315 unsigned long t;
1317 if (parse_strtoul(buf, 1, &t))
1318 return -EINVAL;
1320 tpacpi_wwan_emulstate = !!t;
1322 return count;
1325 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1326 tpacpi_driver_wwan_emulstate_show,
1327 tpacpi_driver_wwan_emulstate_store);
1329 /* uwb_emulstate ------------------------------------------------- */
1330 static ssize_t tpacpi_driver_uwb_emulstate_show(
1331 struct device_driver *drv,
1332 char *buf)
1334 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1337 static ssize_t tpacpi_driver_uwb_emulstate_store(
1338 struct device_driver *drv,
1339 const char *buf, size_t count)
1341 unsigned long t;
1343 if (parse_strtoul(buf, 1, &t))
1344 return -EINVAL;
1346 tpacpi_uwb_emulstate = !!t;
1348 return count;
1351 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1352 tpacpi_driver_uwb_emulstate_show,
1353 tpacpi_driver_uwb_emulstate_store);
1354 #endif
1356 /* --------------------------------------------------------------------- */
1358 static struct driver_attribute *tpacpi_driver_attributes[] = {
1359 &driver_attr_debug_level, &driver_attr_version,
1360 &driver_attr_interface_version,
1363 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1365 int i, res;
1367 i = 0;
1368 res = 0;
1369 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1370 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1371 i++;
1374 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1375 if (!res && dbg_wlswemul)
1376 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1377 if (!res && dbg_bluetoothemul)
1378 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1379 if (!res && dbg_wwanemul)
1380 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1381 if (!res && dbg_uwbemul)
1382 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1383 #endif
1385 return res;
1388 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1390 int i;
1392 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1393 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1395 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1396 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1397 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1398 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1399 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1400 #endif
1403 /*************************************************************************
1404 * Firmware Data
1408 * Table of recommended minimum BIOS versions
1410 * Reasons for listing:
1411 * 1. Stable BIOS, listed because the unknown ammount of
1412 * bugs and bad ACPI behaviour on older versions
1414 * 2. BIOS or EC fw with known bugs that trigger on Linux
1416 * 3. BIOS with known reduced functionality in older versions
1418 * We recommend the latest BIOS and EC version.
1419 * We only support the latest BIOS and EC fw version as a rule.
1421 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1422 * Information from users in ThinkWiki
1424 * WARNING: we use this table also to detect that the machine is
1425 * a ThinkPad in some cases, so don't remove entries lightly.
1428 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1429 { .vendor = (__v), \
1430 .bios = TPID(__id1, __id2), \
1431 .ec = TPACPI_MATCH_ANY, \
1432 .quirks = TPACPI_MATCH_ANY << 16 \
1433 | (__bv1) << 8 | (__bv2) }
1435 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1436 __eid, __ev1, __ev2) \
1437 { .vendor = (__v), \
1438 .bios = TPID(__bid1, __bid2), \
1439 .ec = __eid, \
1440 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1441 | (__bv1) << 8 | (__bv2) }
1443 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1444 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1446 /* Outdated IBM BIOSes often lack the EC id string */
1447 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1448 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1449 __bv1, __bv2, TPID(__id1, __id2), \
1450 __ev1, __ev2), \
1451 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1452 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1453 __ev1, __ev2)
1455 /* Outdated IBM BIOSes often lack the EC id string */
1456 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1457 __eid1, __eid2, __ev1, __ev2) \
1458 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1459 __bv1, __bv2, TPID(__eid1, __eid2), \
1460 __ev1, __ev2), \
1461 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1462 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1463 __ev1, __ev2)
1465 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1466 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1468 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1469 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1470 __bv1, __bv2, TPID(__id1, __id2), \
1471 __ev1, __ev2)
1473 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1474 __eid1, __eid2, __ev1, __ev2) \
1475 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1476 __bv1, __bv2, TPID(__eid1, __eid2), \
1477 __ev1, __ev2)
1479 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1480 /* Numeric models ------------------ */
1481 /* FW MODEL BIOS VERS */
1482 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1483 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1484 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1485 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1486 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1487 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1488 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1489 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1490 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1492 /* A-series ------------------------- */
1493 /* FW MODEL BIOS VERS EC VERS */
1494 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1495 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1496 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1497 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1498 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1499 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1500 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1501 TPV_QI0('1', '3', '2', '0'), /* A22m */
1502 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1503 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1504 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1506 /* G-series ------------------------- */
1507 /* FW MODEL BIOS VERS */
1508 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1509 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1511 /* R-series, T-series --------------- */
1512 /* FW MODEL BIOS VERS EC VERS */
1513 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1514 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1515 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1516 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1517 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1518 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1519 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1520 T40/p, T41/p, T42/p (1) */
1521 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1522 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1523 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1524 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1526 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1527 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1528 TPV_QI0('1', '6', '3', '2'), /* T22 */
1529 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1530 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1531 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1533 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1534 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1535 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1537 /* BIOS FW BIOS VERS EC FW EC VERS */
1538 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1539 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1541 /* X-series ------------------------- */
1542 /* FW MODEL BIOS VERS EC VERS */
1543 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1544 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1545 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1546 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1547 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1548 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1549 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1551 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1552 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1554 /* (0) - older versions lack DMI EC fw string and functionality */
1555 /* (1) - older versions known to lack functionality */
1558 #undef TPV_QL1
1559 #undef TPV_QL0
1560 #undef TPV_QI2
1561 #undef TPV_QI1
1562 #undef TPV_QI0
1563 #undef TPV_Q_X
1564 #undef TPV_Q
1566 static void __init tpacpi_check_outdated_fw(void)
1568 unsigned long fwvers;
1569 u16 ec_version, bios_version;
1571 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1572 ARRAY_SIZE(tpacpi_bios_version_qtable));
1574 if (!fwvers)
1575 return;
1577 bios_version = fwvers & 0xffffU;
1578 ec_version = (fwvers >> 16) & 0xffffU;
1580 /* note that unknown versions are set to 0x0000 and we use that */
1581 if ((bios_version > thinkpad_id.bios_release) ||
1582 (ec_version > thinkpad_id.ec_release &&
1583 ec_version != TPACPI_MATCH_ANY)) {
1585 * The changelogs would let us track down the exact
1586 * reason, but it is just too much of a pain to track
1587 * it. We only list BIOSes that are either really
1588 * broken, or really stable to begin with, so it is
1589 * best if the user upgrades the firmware anyway.
1591 printk(TPACPI_WARN
1592 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1593 printk(TPACPI_WARN
1594 "WARNING: This firmware may be missing critical bug "
1595 "fixes and/or important features\n");
1599 static bool __init tpacpi_is_fw_known(void)
1601 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1602 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1605 /****************************************************************************
1606 ****************************************************************************
1608 * Subdrivers
1610 ****************************************************************************
1611 ****************************************************************************/
1613 /*************************************************************************
1614 * thinkpad-acpi init subdriver
1617 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1619 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1620 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1622 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1623 (thinkpad_id.bios_version_str) ?
1624 thinkpad_id.bios_version_str : "unknown",
1625 (thinkpad_id.ec_version_str) ?
1626 thinkpad_id.ec_version_str : "unknown");
1628 if (thinkpad_id.vendor && thinkpad_id.model_str)
1629 printk(TPACPI_INFO "%s %s, model %s\n",
1630 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1631 "IBM" : ((thinkpad_id.vendor ==
1632 PCI_VENDOR_ID_LENOVO) ?
1633 "Lenovo" : "Unknown vendor"),
1634 thinkpad_id.model_str,
1635 (thinkpad_id.nummodel_str) ?
1636 thinkpad_id.nummodel_str : "unknown");
1638 tpacpi_check_outdated_fw();
1639 return 0;
1642 static int thinkpad_acpi_driver_read(struct seq_file *m)
1644 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1645 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1646 return 0;
1649 static struct ibm_struct thinkpad_acpi_driver_data = {
1650 .name = "driver",
1651 .read = thinkpad_acpi_driver_read,
1654 /*************************************************************************
1655 * Hotkey subdriver
1659 * ThinkPad firmware event model
1661 * The ThinkPad firmware has two main event interfaces: normal ACPI
1662 * notifications (which follow the ACPI standard), and a private event
1663 * interface.
1665 * The private event interface also issues events for the hotkeys. As
1666 * the driver gained features, the event handling code ended up being
1667 * built around the hotkey subdriver. This will need to be refactored
1668 * to a more formal event API eventually.
1670 * Some "hotkeys" are actually supposed to be used as event reports,
1671 * such as "brightness has changed", "volume has changed", depending on
1672 * the ThinkPad model and how the firmware is operating.
1674 * Unlike other classes, hotkey-class events have mask/unmask control on
1675 * non-ancient firmware. However, how it behaves changes a lot with the
1676 * firmware model and version.
1679 enum { /* hot key scan codes (derived from ACPI DSDT) */
1680 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1681 TP_ACPI_HOTKEYSCAN_FNF2,
1682 TP_ACPI_HOTKEYSCAN_FNF3,
1683 TP_ACPI_HOTKEYSCAN_FNF4,
1684 TP_ACPI_HOTKEYSCAN_FNF5,
1685 TP_ACPI_HOTKEYSCAN_FNF6,
1686 TP_ACPI_HOTKEYSCAN_FNF7,
1687 TP_ACPI_HOTKEYSCAN_FNF8,
1688 TP_ACPI_HOTKEYSCAN_FNF9,
1689 TP_ACPI_HOTKEYSCAN_FNF10,
1690 TP_ACPI_HOTKEYSCAN_FNF11,
1691 TP_ACPI_HOTKEYSCAN_FNF12,
1692 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1693 TP_ACPI_HOTKEYSCAN_FNINSERT,
1694 TP_ACPI_HOTKEYSCAN_FNDELETE,
1695 TP_ACPI_HOTKEYSCAN_FNHOME,
1696 TP_ACPI_HOTKEYSCAN_FNEND,
1697 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1698 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1699 TP_ACPI_HOTKEYSCAN_FNSPACE,
1700 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1701 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1702 TP_ACPI_HOTKEYSCAN_MUTE,
1703 TP_ACPI_HOTKEYSCAN_THINKPAD,
1706 enum { /* Keys/events available through NVRAM polling */
1707 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1708 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1711 enum { /* Positions of some of the keys in hotkey masks */
1712 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1713 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1714 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1715 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1716 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1717 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1718 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1719 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1720 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1721 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1722 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1725 enum { /* NVRAM to ACPI HKEY group map */
1726 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1727 TP_ACPI_HKEY_ZOOM_MASK |
1728 TP_ACPI_HKEY_DISPSWTCH_MASK |
1729 TP_ACPI_HKEY_HIBERNATE_MASK,
1730 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1731 TP_ACPI_HKEY_BRGHTDWN_MASK,
1732 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1733 TP_ACPI_HKEY_VOLDWN_MASK |
1734 TP_ACPI_HKEY_MUTE_MASK,
1737 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1738 struct tp_nvram_state {
1739 u16 thinkpad_toggle:1;
1740 u16 zoom_toggle:1;
1741 u16 display_toggle:1;
1742 u16 thinklight_toggle:1;
1743 u16 hibernate_toggle:1;
1744 u16 displayexp_toggle:1;
1745 u16 display_state:1;
1746 u16 brightness_toggle:1;
1747 u16 volume_toggle:1;
1748 u16 mute:1;
1750 u8 brightness_level;
1751 u8 volume_level;
1754 /* kthread for the hotkey poller */
1755 static struct task_struct *tpacpi_hotkey_task;
1757 /* Acquired while the poller kthread is running, use to sync start/stop */
1758 static struct mutex hotkey_thread_mutex;
1761 * Acquire mutex to write poller control variables as an
1762 * atomic block.
1764 * Increment hotkey_config_change when changing them if you
1765 * want the kthread to forget old state.
1767 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1769 static struct mutex hotkey_thread_data_mutex;
1770 static unsigned int hotkey_config_change;
1773 * hotkey poller control variables
1775 * Must be atomic or readers will also need to acquire mutex
1777 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1778 * should be used only when the changes need to be taken as
1779 * a block, OR when one needs to force the kthread to forget
1780 * old state.
1782 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1783 static unsigned int hotkey_poll_freq = 10; /* Hz */
1785 #define HOTKEY_CONFIG_CRITICAL_START \
1786 do { \
1787 mutex_lock(&hotkey_thread_data_mutex); \
1788 hotkey_config_change++; \
1789 } while (0);
1790 #define HOTKEY_CONFIG_CRITICAL_END \
1791 mutex_unlock(&hotkey_thread_data_mutex);
1793 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1795 #define hotkey_source_mask 0U
1796 #define HOTKEY_CONFIG_CRITICAL_START
1797 #define HOTKEY_CONFIG_CRITICAL_END
1799 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1801 static struct mutex hotkey_mutex;
1803 static enum { /* Reasons for waking up */
1804 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1805 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1806 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1807 } hotkey_wakeup_reason;
1809 static int hotkey_autosleep_ack;
1811 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1812 static u32 hotkey_all_mask; /* all events supported in fw */
1813 static u32 hotkey_reserved_mask; /* events better left disabled */
1814 static u32 hotkey_driver_mask; /* events needed by the driver */
1815 static u32 hotkey_user_mask; /* events visible to userspace */
1816 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1818 static unsigned int hotkey_report_mode;
1820 static u16 *hotkey_keycode_map;
1822 static struct attribute_set *hotkey_dev_attributes;
1824 static void tpacpi_driver_event(const unsigned int hkey_event);
1825 static void hotkey_driver_event(const unsigned int scancode);
1826 static void hotkey_poll_setup(const bool may_warn);
1828 /* HKEY.MHKG() return bits */
1829 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1831 static int hotkey_get_wlsw(int *status)
1833 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1834 if (dbg_wlswemul) {
1835 *status = !!tpacpi_wlsw_emulstate;
1836 return 0;
1838 #endif
1839 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1840 return -EIO;
1841 return 0;
1844 static int hotkey_get_tablet_mode(int *status)
1846 int s;
1848 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1849 return -EIO;
1851 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1852 return 0;
1856 * Reads current event mask from firmware, and updates
1857 * hotkey_acpi_mask accordingly. Also resets any bits
1858 * from hotkey_user_mask that are unavailable to be
1859 * delivered (shadow requirement of the userspace ABI).
1861 * Call with hotkey_mutex held
1863 static int hotkey_mask_get(void)
1865 if (tp_features.hotkey_mask) {
1866 u32 m = 0;
1868 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1869 return -EIO;
1871 hotkey_acpi_mask = m;
1872 } else {
1873 /* no mask support doesn't mean no event support... */
1874 hotkey_acpi_mask = hotkey_all_mask;
1877 /* sync userspace-visible mask */
1878 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1880 return 0;
1883 void static hotkey_mask_warn_incomplete_mask(void)
1885 /* log only what the user can fix... */
1886 const u32 wantedmask = hotkey_driver_mask &
1887 ~(hotkey_acpi_mask | hotkey_source_mask) &
1888 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1890 if (wantedmask)
1891 printk(TPACPI_NOTICE
1892 "required events 0x%08x not enabled!\n",
1893 wantedmask);
1897 * Set the firmware mask when supported
1899 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1901 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1903 * Call with hotkey_mutex held
1905 static int hotkey_mask_set(u32 mask)
1907 int i;
1908 int rc = 0;
1910 const u32 fwmask = mask & ~hotkey_source_mask;
1912 if (tp_features.hotkey_mask) {
1913 for (i = 0; i < 32; i++) {
1914 if (!acpi_evalf(hkey_handle,
1915 NULL, "MHKM", "vdd", i + 1,
1916 !!(mask & (1 << i)))) {
1917 rc = -EIO;
1918 break;
1924 * We *must* make an inconditional call to hotkey_mask_get to
1925 * refresh hotkey_acpi_mask and update hotkey_user_mask
1927 * Take the opportunity to also log when we cannot _enable_
1928 * a given event.
1930 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1931 printk(TPACPI_NOTICE
1932 "asked for hotkey mask 0x%08x, but "
1933 "firmware forced it to 0x%08x\n",
1934 fwmask, hotkey_acpi_mask);
1937 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1938 hotkey_mask_warn_incomplete_mask();
1940 return rc;
1944 * Sets hotkey_user_mask and tries to set the firmware mask
1946 * Call with hotkey_mutex held
1948 static int hotkey_user_mask_set(const u32 mask)
1950 int rc;
1952 /* Give people a chance to notice they are doing something that
1953 * is bound to go boom on their users sooner or later */
1954 if (!tp_warned.hotkey_mask_ff &&
1955 (mask == 0xffff || mask == 0xffffff ||
1956 mask == 0xffffffff)) {
1957 tp_warned.hotkey_mask_ff = 1;
1958 printk(TPACPI_NOTICE
1959 "setting the hotkey mask to 0x%08x is likely "
1960 "not the best way to go about it\n", mask);
1961 printk(TPACPI_NOTICE
1962 "please consider using the driver defaults, "
1963 "and refer to up-to-date thinkpad-acpi "
1964 "documentation\n");
1967 /* Try to enable what the user asked for, plus whatever we need.
1968 * this syncs everything but won't enable bits in hotkey_user_mask */
1969 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1971 /* Enable the available bits in hotkey_user_mask */
1972 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1974 return rc;
1978 * Sets the driver hotkey mask.
1980 * Can be called even if the hotkey subdriver is inactive
1982 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1984 int rc;
1986 /* Do the right thing if hotkey_init has not been called yet */
1987 if (!tp_features.hotkey) {
1988 hotkey_driver_mask = mask;
1989 return 0;
1992 mutex_lock(&hotkey_mutex);
1994 HOTKEY_CONFIG_CRITICAL_START
1995 hotkey_driver_mask = mask;
1996 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1997 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1998 #endif
1999 HOTKEY_CONFIG_CRITICAL_END
2001 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2002 ~hotkey_source_mask);
2003 hotkey_poll_setup(true);
2005 mutex_unlock(&hotkey_mutex);
2007 return rc;
2010 static int hotkey_status_get(int *status)
2012 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2013 return -EIO;
2015 return 0;
2018 static int hotkey_status_set(bool enable)
2020 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2021 return -EIO;
2023 return 0;
2026 static void tpacpi_input_send_tabletsw(void)
2028 int state;
2030 if (tp_features.hotkey_tablet &&
2031 !hotkey_get_tablet_mode(&state)) {
2032 mutex_lock(&tpacpi_inputdev_send_mutex);
2034 input_report_switch(tpacpi_inputdev,
2035 SW_TABLET_MODE, !!state);
2036 input_sync(tpacpi_inputdev);
2038 mutex_unlock(&tpacpi_inputdev_send_mutex);
2042 /* Do NOT call without validating scancode first */
2043 static void tpacpi_input_send_key(const unsigned int scancode)
2045 const unsigned int keycode = hotkey_keycode_map[scancode];
2047 if (keycode != KEY_RESERVED) {
2048 mutex_lock(&tpacpi_inputdev_send_mutex);
2050 input_report_key(tpacpi_inputdev, keycode, 1);
2051 if (keycode == KEY_UNKNOWN)
2052 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2053 scancode);
2054 input_sync(tpacpi_inputdev);
2056 input_report_key(tpacpi_inputdev, keycode, 0);
2057 if (keycode == KEY_UNKNOWN)
2058 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2059 scancode);
2060 input_sync(tpacpi_inputdev);
2062 mutex_unlock(&tpacpi_inputdev_send_mutex);
2066 /* Do NOT call without validating scancode first */
2067 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2069 hotkey_driver_event(scancode);
2070 if (hotkey_user_mask & (1 << scancode))
2071 tpacpi_input_send_key(scancode);
2074 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2075 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2077 /* Do NOT call without validating scancode first */
2078 static void tpacpi_hotkey_send_key(unsigned int scancode)
2080 tpacpi_input_send_key_masked(scancode);
2081 if (hotkey_report_mode < 2) {
2082 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2083 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2087 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2089 u8 d;
2091 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2092 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2093 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2094 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2095 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2096 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2098 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2099 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2100 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2102 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2103 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2104 n->displayexp_toggle =
2105 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2107 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2108 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2109 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2110 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2111 n->brightness_toggle =
2112 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2114 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2115 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2116 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2117 >> TP_NVRAM_POS_LEVEL_VOLUME;
2118 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2119 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2123 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2124 struct tp_nvram_state *newn,
2125 const u32 event_mask)
2128 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2129 do { \
2130 if ((event_mask & (1 << __scancode)) && \
2131 oldn->__member != newn->__member) \
2132 tpacpi_hotkey_send_key(__scancode); \
2133 } while (0)
2135 #define TPACPI_MAY_SEND_KEY(__scancode) \
2136 do { \
2137 if (event_mask & (1 << __scancode)) \
2138 tpacpi_hotkey_send_key(__scancode); \
2139 } while (0)
2141 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2142 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2143 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2144 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2146 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2148 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2150 /* handle volume */
2151 if (oldn->volume_toggle != newn->volume_toggle) {
2152 if (oldn->mute != newn->mute) {
2153 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2155 if (oldn->volume_level > newn->volume_level) {
2156 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2157 } else if (oldn->volume_level < newn->volume_level) {
2158 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2159 } else if (oldn->mute == newn->mute) {
2160 /* repeated key presses that didn't change state */
2161 if (newn->mute) {
2162 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2163 } else if (newn->volume_level != 0) {
2164 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2165 } else {
2166 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2171 /* handle brightness */
2172 if (oldn->brightness_toggle != newn->brightness_toggle) {
2173 if (oldn->brightness_level < newn->brightness_level) {
2174 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2175 } else if (oldn->brightness_level > newn->brightness_level) {
2176 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2177 } else {
2178 /* repeated key presses that didn't change state */
2179 if (newn->brightness_level != 0) {
2180 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2181 } else {
2182 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2187 #undef TPACPI_COMPARE_KEY
2188 #undef TPACPI_MAY_SEND_KEY
2192 * Polling driver
2194 * We track all events in hotkey_source_mask all the time, since
2195 * most of them are edge-based. We only issue those requested by
2196 * hotkey_user_mask or hotkey_driver_mask, though.
2198 static int hotkey_kthread(void *data)
2200 struct tp_nvram_state s[2];
2201 u32 poll_mask, event_mask;
2202 unsigned int si, so;
2203 unsigned long t;
2204 unsigned int change_detector, must_reset;
2205 unsigned int poll_freq;
2207 mutex_lock(&hotkey_thread_mutex);
2209 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2210 goto exit;
2212 set_freezable();
2214 so = 0;
2215 si = 1;
2216 t = 0;
2218 /* Initial state for compares */
2219 mutex_lock(&hotkey_thread_data_mutex);
2220 change_detector = hotkey_config_change;
2221 poll_mask = hotkey_source_mask;
2222 event_mask = hotkey_source_mask &
2223 (hotkey_driver_mask | hotkey_user_mask);
2224 poll_freq = hotkey_poll_freq;
2225 mutex_unlock(&hotkey_thread_data_mutex);
2226 hotkey_read_nvram(&s[so], poll_mask);
2228 while (!kthread_should_stop()) {
2229 if (t == 0) {
2230 if (likely(poll_freq))
2231 t = 1000/poll_freq;
2232 else
2233 t = 100; /* should never happen... */
2235 t = msleep_interruptible(t);
2236 if (unlikely(kthread_should_stop()))
2237 break;
2238 must_reset = try_to_freeze();
2239 if (t > 0 && !must_reset)
2240 continue;
2242 mutex_lock(&hotkey_thread_data_mutex);
2243 if (must_reset || hotkey_config_change != change_detector) {
2244 /* forget old state on thaw or config change */
2245 si = so;
2246 t = 0;
2247 change_detector = hotkey_config_change;
2249 poll_mask = hotkey_source_mask;
2250 event_mask = hotkey_source_mask &
2251 (hotkey_driver_mask | hotkey_user_mask);
2252 poll_freq = hotkey_poll_freq;
2253 mutex_unlock(&hotkey_thread_data_mutex);
2255 if (likely(poll_mask)) {
2256 hotkey_read_nvram(&s[si], poll_mask);
2257 if (likely(si != so)) {
2258 hotkey_compare_and_issue_event(&s[so], &s[si],
2259 event_mask);
2263 so = si;
2264 si ^= 1;
2267 exit:
2268 mutex_unlock(&hotkey_thread_mutex);
2269 return 0;
2272 /* call with hotkey_mutex held */
2273 static void hotkey_poll_stop_sync(void)
2275 if (tpacpi_hotkey_task) {
2276 if (frozen(tpacpi_hotkey_task) ||
2277 freezing(tpacpi_hotkey_task))
2278 thaw_process(tpacpi_hotkey_task);
2280 kthread_stop(tpacpi_hotkey_task);
2281 tpacpi_hotkey_task = NULL;
2282 mutex_lock(&hotkey_thread_mutex);
2283 /* at this point, the thread did exit */
2284 mutex_unlock(&hotkey_thread_mutex);
2288 /* call with hotkey_mutex held */
2289 static void hotkey_poll_setup(const bool may_warn)
2291 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2292 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2294 if (hotkey_poll_freq > 0 &&
2295 (poll_driver_mask ||
2296 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2297 if (!tpacpi_hotkey_task) {
2298 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2299 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2300 if (IS_ERR(tpacpi_hotkey_task)) {
2301 tpacpi_hotkey_task = NULL;
2302 printk(TPACPI_ERR
2303 "could not create kernel thread "
2304 "for hotkey polling\n");
2307 } else {
2308 hotkey_poll_stop_sync();
2309 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2310 hotkey_poll_freq == 0) {
2311 printk(TPACPI_NOTICE
2312 "hot keys 0x%08x and/or events 0x%08x "
2313 "require polling, which is currently "
2314 "disabled\n",
2315 poll_user_mask, poll_driver_mask);
2320 static void hotkey_poll_setup_safe(const bool may_warn)
2322 mutex_lock(&hotkey_mutex);
2323 hotkey_poll_setup(may_warn);
2324 mutex_unlock(&hotkey_mutex);
2327 /* call with hotkey_mutex held */
2328 static void hotkey_poll_set_freq(unsigned int freq)
2330 if (!freq)
2331 hotkey_poll_stop_sync();
2333 hotkey_poll_freq = freq;
2336 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2338 static void hotkey_poll_setup(const bool __unused)
2342 static void hotkey_poll_setup_safe(const bool __unused)
2346 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2348 static int hotkey_inputdev_open(struct input_dev *dev)
2350 switch (tpacpi_lifecycle) {
2351 case TPACPI_LIFE_INIT:
2352 case TPACPI_LIFE_RUNNING:
2353 hotkey_poll_setup_safe(false);
2354 return 0;
2355 case TPACPI_LIFE_EXITING:
2356 return -EBUSY;
2359 /* Should only happen if tpacpi_lifecycle is corrupt */
2360 BUG();
2361 return -EBUSY;
2364 static void hotkey_inputdev_close(struct input_dev *dev)
2366 /* disable hotkey polling when possible */
2367 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2368 !(hotkey_source_mask & hotkey_driver_mask))
2369 hotkey_poll_setup_safe(false);
2372 /* sysfs hotkey enable ------------------------------------------------- */
2373 static ssize_t hotkey_enable_show(struct device *dev,
2374 struct device_attribute *attr,
2375 char *buf)
2377 int res, status;
2379 printk_deprecated_attribute("hotkey_enable",
2380 "Hotkey reporting is always enabled");
2382 res = hotkey_status_get(&status);
2383 if (res)
2384 return res;
2386 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2389 static ssize_t hotkey_enable_store(struct device *dev,
2390 struct device_attribute *attr,
2391 const char *buf, size_t count)
2393 unsigned long t;
2395 printk_deprecated_attribute("hotkey_enable",
2396 "Hotkeys can be disabled through hotkey_mask");
2398 if (parse_strtoul(buf, 1, &t))
2399 return -EINVAL;
2401 if (t == 0)
2402 return -EPERM;
2404 return count;
2407 static struct device_attribute dev_attr_hotkey_enable =
2408 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2409 hotkey_enable_show, hotkey_enable_store);
2411 /* sysfs hotkey mask --------------------------------------------------- */
2412 static ssize_t hotkey_mask_show(struct device *dev,
2413 struct device_attribute *attr,
2414 char *buf)
2416 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2419 static ssize_t hotkey_mask_store(struct device *dev,
2420 struct device_attribute *attr,
2421 const char *buf, size_t count)
2423 unsigned long t;
2424 int res;
2426 if (parse_strtoul(buf, 0xffffffffUL, &t))
2427 return -EINVAL;
2429 if (mutex_lock_killable(&hotkey_mutex))
2430 return -ERESTARTSYS;
2432 res = hotkey_user_mask_set(t);
2434 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2435 hotkey_poll_setup(true);
2436 #endif
2438 mutex_unlock(&hotkey_mutex);
2440 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2442 return (res) ? res : count;
2445 static struct device_attribute dev_attr_hotkey_mask =
2446 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2447 hotkey_mask_show, hotkey_mask_store);
2449 /* sysfs hotkey bios_enabled ------------------------------------------- */
2450 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2451 struct device_attribute *attr,
2452 char *buf)
2454 return sprintf(buf, "0\n");
2457 static struct device_attribute dev_attr_hotkey_bios_enabled =
2458 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2460 /* sysfs hotkey bios_mask ---------------------------------------------- */
2461 static ssize_t hotkey_bios_mask_show(struct device *dev,
2462 struct device_attribute *attr,
2463 char *buf)
2465 printk_deprecated_attribute("hotkey_bios_mask",
2466 "This attribute is useless.");
2467 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2470 static struct device_attribute dev_attr_hotkey_bios_mask =
2471 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2473 /* sysfs hotkey all_mask ----------------------------------------------- */
2474 static ssize_t hotkey_all_mask_show(struct device *dev,
2475 struct device_attribute *attr,
2476 char *buf)
2478 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2479 hotkey_all_mask | hotkey_source_mask);
2482 static struct device_attribute dev_attr_hotkey_all_mask =
2483 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2485 /* sysfs hotkey recommended_mask --------------------------------------- */
2486 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2487 struct device_attribute *attr,
2488 char *buf)
2490 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2491 (hotkey_all_mask | hotkey_source_mask)
2492 & ~hotkey_reserved_mask);
2495 static struct device_attribute dev_attr_hotkey_recommended_mask =
2496 __ATTR(hotkey_recommended_mask, S_IRUGO,
2497 hotkey_recommended_mask_show, NULL);
2499 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2501 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2502 static ssize_t hotkey_source_mask_show(struct device *dev,
2503 struct device_attribute *attr,
2504 char *buf)
2506 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2509 static ssize_t hotkey_source_mask_store(struct device *dev,
2510 struct device_attribute *attr,
2511 const char *buf, size_t count)
2513 unsigned long t;
2514 u32 r_ev;
2515 int rc;
2517 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2518 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2519 return -EINVAL;
2521 if (mutex_lock_killable(&hotkey_mutex))
2522 return -ERESTARTSYS;
2524 HOTKEY_CONFIG_CRITICAL_START
2525 hotkey_source_mask = t;
2526 HOTKEY_CONFIG_CRITICAL_END
2528 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2529 ~hotkey_source_mask);
2530 hotkey_poll_setup(true);
2532 /* check if events needed by the driver got disabled */
2533 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2534 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2536 mutex_unlock(&hotkey_mutex);
2538 if (rc < 0)
2539 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2540 "firmware event mask!\n");
2542 if (r_ev)
2543 printk(TPACPI_NOTICE "hotkey_source_mask: "
2544 "some important events were disabled: "
2545 "0x%04x\n", r_ev);
2547 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2549 return (rc < 0) ? rc : count;
2552 static struct device_attribute dev_attr_hotkey_source_mask =
2553 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2554 hotkey_source_mask_show, hotkey_source_mask_store);
2556 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2557 static ssize_t hotkey_poll_freq_show(struct device *dev,
2558 struct device_attribute *attr,
2559 char *buf)
2561 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2564 static ssize_t hotkey_poll_freq_store(struct device *dev,
2565 struct device_attribute *attr,
2566 const char *buf, size_t count)
2568 unsigned long t;
2570 if (parse_strtoul(buf, 25, &t))
2571 return -EINVAL;
2573 if (mutex_lock_killable(&hotkey_mutex))
2574 return -ERESTARTSYS;
2576 hotkey_poll_set_freq(t);
2577 hotkey_poll_setup(true);
2579 mutex_unlock(&hotkey_mutex);
2581 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2583 return count;
2586 static struct device_attribute dev_attr_hotkey_poll_freq =
2587 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2588 hotkey_poll_freq_show, hotkey_poll_freq_store);
2590 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2592 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2593 static ssize_t hotkey_radio_sw_show(struct device *dev,
2594 struct device_attribute *attr,
2595 char *buf)
2597 int res, s;
2598 res = hotkey_get_wlsw(&s);
2599 if (res < 0)
2600 return res;
2602 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2605 static struct device_attribute dev_attr_hotkey_radio_sw =
2606 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2608 static void hotkey_radio_sw_notify_change(void)
2610 if (tp_features.hotkey_wlsw)
2611 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2612 "hotkey_radio_sw");
2615 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2616 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2617 struct device_attribute *attr,
2618 char *buf)
2620 int res, s;
2621 res = hotkey_get_tablet_mode(&s);
2622 if (res < 0)
2623 return res;
2625 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2628 static struct device_attribute dev_attr_hotkey_tablet_mode =
2629 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2631 static void hotkey_tablet_mode_notify_change(void)
2633 if (tp_features.hotkey_tablet)
2634 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2635 "hotkey_tablet_mode");
2638 /* sysfs hotkey report_mode -------------------------------------------- */
2639 static ssize_t hotkey_report_mode_show(struct device *dev,
2640 struct device_attribute *attr,
2641 char *buf)
2643 return snprintf(buf, PAGE_SIZE, "%d\n",
2644 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2647 static struct device_attribute dev_attr_hotkey_report_mode =
2648 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2650 /* sysfs wakeup reason (pollable) -------------------------------------- */
2651 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2652 struct device_attribute *attr,
2653 char *buf)
2655 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2658 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2659 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2661 static void hotkey_wakeup_reason_notify_change(void)
2663 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2664 "wakeup_reason");
2667 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2668 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2669 struct device_attribute *attr,
2670 char *buf)
2672 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2675 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2676 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2677 hotkey_wakeup_hotunplug_complete_show, NULL);
2679 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2681 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2682 "wakeup_hotunplug_complete");
2685 /* --------------------------------------------------------------------- */
2687 static struct attribute *hotkey_attributes[] __initdata = {
2688 &dev_attr_hotkey_enable.attr,
2689 &dev_attr_hotkey_bios_enabled.attr,
2690 &dev_attr_hotkey_bios_mask.attr,
2691 &dev_attr_hotkey_report_mode.attr,
2692 &dev_attr_hotkey_wakeup_reason.attr,
2693 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2694 &dev_attr_hotkey_mask.attr,
2695 &dev_attr_hotkey_all_mask.attr,
2696 &dev_attr_hotkey_recommended_mask.attr,
2697 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2698 &dev_attr_hotkey_source_mask.attr,
2699 &dev_attr_hotkey_poll_freq.attr,
2700 #endif
2703 static void bluetooth_update_rfk(void);
2704 static void wan_update_rfk(void);
2705 static void uwb_update_rfk(void);
2706 static void tpacpi_send_radiosw_update(void)
2708 int wlsw;
2710 /* Sync these BEFORE sending any rfkill events */
2711 if (tp_features.bluetooth)
2712 bluetooth_update_rfk();
2713 if (tp_features.wan)
2714 wan_update_rfk();
2715 if (tp_features.uwb)
2716 uwb_update_rfk();
2718 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2719 mutex_lock(&tpacpi_inputdev_send_mutex);
2721 input_report_switch(tpacpi_inputdev,
2722 SW_RFKILL_ALL, !!wlsw);
2723 input_sync(tpacpi_inputdev);
2725 mutex_unlock(&tpacpi_inputdev_send_mutex);
2727 hotkey_radio_sw_notify_change();
2730 static void hotkey_exit(void)
2732 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2733 mutex_lock(&hotkey_mutex);
2734 hotkey_poll_stop_sync();
2735 mutex_unlock(&hotkey_mutex);
2736 #endif
2738 if (hotkey_dev_attributes)
2739 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2741 kfree(hotkey_keycode_map);
2743 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2744 "restoring original HKEY status and mask\n");
2745 /* yes, there is a bitwise or below, we want the
2746 * functions to be called even if one of them fail */
2747 if (((tp_features.hotkey_mask &&
2748 hotkey_mask_set(hotkey_orig_mask)) |
2749 hotkey_status_set(false)) != 0)
2750 printk(TPACPI_ERR
2751 "failed to restore hot key mask "
2752 "to BIOS defaults\n");
2755 static void __init hotkey_unmap(const unsigned int scancode)
2757 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2758 clear_bit(hotkey_keycode_map[scancode],
2759 tpacpi_inputdev->keybit);
2760 hotkey_keycode_map[scancode] = KEY_RESERVED;
2765 * HKEY quirks:
2766 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2769 #define TPACPI_HK_Q_INIMASK 0x0001
2771 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2772 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2773 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2774 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2775 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2776 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2777 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2778 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2779 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2780 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2781 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2782 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2783 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2784 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2785 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2786 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2787 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2788 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2789 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2790 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2793 static int __init hotkey_init(struct ibm_init_struct *iibm)
2795 /* Requirements for changing the default keymaps:
2797 * 1. Many of the keys are mapped to KEY_RESERVED for very
2798 * good reasons. Do not change them unless you have deep
2799 * knowledge on the IBM and Lenovo ThinkPad firmware for
2800 * the various ThinkPad models. The driver behaves
2801 * differently for KEY_RESERVED: such keys have their
2802 * hot key mask *unset* in mask_recommended, and also
2803 * in the initial hot key mask programmed into the
2804 * firmware at driver load time, which means the firm-
2805 * ware may react very differently if you change them to
2806 * something else;
2808 * 2. You must be subscribed to the linux-thinkpad and
2809 * ibm-acpi-devel mailing lists, and you should read the
2810 * list archives since 2007 if you want to change the
2811 * keymaps. This requirement exists so that you will
2812 * know the past history of problems with the thinkpad-
2813 * acpi driver keymaps, and also that you will be
2814 * listening to any bug reports;
2816 * 3. Do not send thinkpad-acpi specific patches directly to
2817 * for merging, *ever*. Send them to the linux-acpi
2818 * mailinglist for comments. Merging is to be done only
2819 * through acpi-test and the ACPI maintainer.
2821 * If the above is too much to ask, don't change the keymap.
2822 * Ask the thinkpad-acpi maintainer to do it, instead.
2824 static u16 ibm_keycode_map[] __initdata = {
2825 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2826 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2827 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2828 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2830 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2831 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2832 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2833 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2835 /* brightness: firmware always reacts to them */
2836 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2837 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2839 /* Thinklight: firmware always react to it */
2840 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2842 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2843 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2845 /* Volume: firmware always react to it and reprograms
2846 * the built-in *extra* mixer. Never map it to control
2847 * another mixer by default. */
2848 KEY_RESERVED, /* 0x14: VOLUME UP */
2849 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2850 KEY_RESERVED, /* 0x16: MUTE */
2852 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2854 /* (assignments unknown, please report if found) */
2855 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2856 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2858 static u16 lenovo_keycode_map[] __initdata = {
2859 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2860 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2861 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2862 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2864 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2865 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2866 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2867 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2869 /* These should be enabled --only-- when ACPI video
2870 * is disabled (i.e. in "vendor" mode), and are handled
2871 * in a special way by the init code */
2872 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2873 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2875 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2877 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2878 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2880 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2881 * react to it and reprograms the built-in *extra* mixer.
2882 * Never map it to control another mixer by default.
2884 * T60?, T61, R60?, R61: firmware and EC tries to send
2885 * these over the regular keyboard, so these are no-ops,
2886 * but there are still weird bugs re. MUTE, so do not
2887 * change unless you get test reports from all Lenovo
2888 * models. May cause the BIOS to interfere with the
2889 * HDA mixer.
2891 KEY_RESERVED, /* 0x14: VOLUME UP */
2892 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2893 KEY_RESERVED, /* 0x16: MUTE */
2895 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2897 /* (assignments unknown, please report if found) */
2898 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2899 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2902 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2903 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2904 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2906 int res, i;
2907 int status;
2908 int hkeyv;
2909 bool radiosw_state = false;
2910 bool tabletsw_state = false;
2912 unsigned long quirks;
2914 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2915 "initializing hotkey subdriver\n");
2917 BUG_ON(!tpacpi_inputdev);
2918 BUG_ON(tpacpi_inputdev->open != NULL ||
2919 tpacpi_inputdev->close != NULL);
2921 TPACPI_ACPIHANDLE_INIT(hkey);
2922 mutex_init(&hotkey_mutex);
2924 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2925 mutex_init(&hotkey_thread_mutex);
2926 mutex_init(&hotkey_thread_data_mutex);
2927 #endif
2929 /* hotkey not supported on 570 */
2930 tp_features.hotkey = hkey_handle != NULL;
2932 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2933 "hotkeys are %s\n",
2934 str_supported(tp_features.hotkey));
2936 if (!tp_features.hotkey)
2937 return 1;
2939 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2940 ARRAY_SIZE(tpacpi_hotkey_qtable));
2942 tpacpi_disable_brightness_delay();
2944 /* MUST have enough space for all attributes to be added to
2945 * hotkey_dev_attributes */
2946 hotkey_dev_attributes = create_attr_set(
2947 ARRAY_SIZE(hotkey_attributes) + 2,
2948 NULL);
2949 if (!hotkey_dev_attributes)
2950 return -ENOMEM;
2951 res = add_many_to_attr_set(hotkey_dev_attributes,
2952 hotkey_attributes,
2953 ARRAY_SIZE(hotkey_attributes));
2954 if (res)
2955 goto err_exit;
2957 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2958 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2959 for HKEY interface version 0x100 */
2960 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2961 if ((hkeyv >> 8) != 1) {
2962 printk(TPACPI_ERR "unknown version of the "
2963 "HKEY interface: 0x%x\n", hkeyv);
2964 printk(TPACPI_ERR "please report this to %s\n",
2965 TPACPI_MAIL);
2966 } else {
2968 * MHKV 0x100 in A31, R40, R40e,
2969 * T4x, X31, and later
2971 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2972 "firmware HKEY interface version: 0x%x\n",
2973 hkeyv);
2975 /* Paranoia check AND init hotkey_all_mask */
2976 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2977 "MHKA", "qd")) {
2978 printk(TPACPI_ERR
2979 "missing MHKA handler, "
2980 "please report this to %s\n",
2981 TPACPI_MAIL);
2982 /* Fallback: pre-init for FN+F3,F4,F12 */
2983 hotkey_all_mask = 0x080cU;
2984 } else {
2985 tp_features.hotkey_mask = 1;
2990 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2991 "hotkey masks are %s\n",
2992 str_supported(tp_features.hotkey_mask));
2994 /* Init hotkey_all_mask if not initialized yet */
2995 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2996 (quirks & TPACPI_HK_Q_INIMASK))
2997 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2999 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3000 if (tp_features.hotkey_mask) {
3001 /* hotkey_source_mask *must* be zero for
3002 * the first hotkey_mask_get to return hotkey_orig_mask */
3003 res = hotkey_mask_get();
3004 if (res)
3005 goto err_exit;
3007 hotkey_orig_mask = hotkey_acpi_mask;
3008 } else {
3009 hotkey_orig_mask = hotkey_all_mask;
3010 hotkey_acpi_mask = hotkey_all_mask;
3013 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3014 if (dbg_wlswemul) {
3015 tp_features.hotkey_wlsw = 1;
3016 radiosw_state = !!tpacpi_wlsw_emulstate;
3017 printk(TPACPI_INFO
3018 "radio switch emulation enabled\n");
3019 } else
3020 #endif
3021 /* Not all thinkpads have a hardware radio switch */
3022 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3023 tp_features.hotkey_wlsw = 1;
3024 radiosw_state = !!status;
3025 printk(TPACPI_INFO
3026 "radio switch found; radios are %s\n",
3027 enabled(status, 0));
3029 if (tp_features.hotkey_wlsw)
3030 res = add_to_attr_set(hotkey_dev_attributes,
3031 &dev_attr_hotkey_radio_sw.attr);
3033 /* For X41t, X60t, X61t Tablets... */
3034 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3035 tp_features.hotkey_tablet = 1;
3036 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3037 printk(TPACPI_INFO
3038 "possible tablet mode switch found; "
3039 "ThinkPad in %s mode\n",
3040 (tabletsw_state) ? "tablet" : "laptop");
3041 res = add_to_attr_set(hotkey_dev_attributes,
3042 &dev_attr_hotkey_tablet_mode.attr);
3045 if (!res)
3046 res = register_attr_set_with_sysfs(
3047 hotkey_dev_attributes,
3048 &tpacpi_pdev->dev.kobj);
3049 if (res)
3050 goto err_exit;
3052 /* Set up key map */
3054 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3055 GFP_KERNEL);
3056 if (!hotkey_keycode_map) {
3057 printk(TPACPI_ERR
3058 "failed to allocate memory for key map\n");
3059 res = -ENOMEM;
3060 goto err_exit;
3063 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3064 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3065 "using Lenovo default hot key map\n");
3066 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3067 TPACPI_HOTKEY_MAP_SIZE);
3068 } else {
3069 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3070 "using IBM default hot key map\n");
3071 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3072 TPACPI_HOTKEY_MAP_SIZE);
3075 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3076 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3077 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3078 tpacpi_inputdev->keycode = hotkey_keycode_map;
3079 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3080 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3081 input_set_capability(tpacpi_inputdev, EV_KEY,
3082 hotkey_keycode_map[i]);
3083 } else {
3084 if (i < sizeof(hotkey_reserved_mask)*8)
3085 hotkey_reserved_mask |= 1 << i;
3089 if (tp_features.hotkey_wlsw) {
3090 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3091 input_report_switch(tpacpi_inputdev,
3092 SW_RFKILL_ALL, radiosw_state);
3094 if (tp_features.hotkey_tablet) {
3095 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3096 input_report_switch(tpacpi_inputdev,
3097 SW_TABLET_MODE, tabletsw_state);
3100 /* Do not issue duplicate brightness change events to
3101 * userspace */
3102 if (!tp_features.bright_acpimode)
3103 /* update bright_acpimode... */
3104 tpacpi_check_std_acpi_brightness_support();
3106 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3107 printk(TPACPI_INFO
3108 "This ThinkPad has standard ACPI backlight "
3109 "brightness control, supported by the ACPI "
3110 "video driver\n");
3111 printk(TPACPI_NOTICE
3112 "Disabling thinkpad-acpi brightness events "
3113 "by default...\n");
3115 /* Disable brightness up/down on Lenovo thinkpads when
3116 * ACPI is handling them, otherwise it is plain impossible
3117 * for userspace to do something even remotely sane */
3118 hotkey_reserved_mask |=
3119 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3120 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3121 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3122 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3125 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3126 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3127 & ~hotkey_all_mask
3128 & ~hotkey_reserved_mask;
3130 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3131 "hotkey source mask 0x%08x, polling freq %u\n",
3132 hotkey_source_mask, hotkey_poll_freq);
3133 #endif
3135 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3136 "enabling firmware HKEY event interface...\n");
3137 res = hotkey_status_set(true);
3138 if (res) {
3139 hotkey_exit();
3140 return res;
3142 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3143 | hotkey_driver_mask)
3144 & ~hotkey_source_mask);
3145 if (res < 0 && res != -ENXIO) {
3146 hotkey_exit();
3147 return res;
3149 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3150 & ~hotkey_reserved_mask;
3151 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3152 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3153 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3155 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3156 "legacy ibm/hotkey event reporting over procfs %s\n",
3157 (hotkey_report_mode < 2) ?
3158 "enabled" : "disabled");
3160 tpacpi_inputdev->open = &hotkey_inputdev_open;
3161 tpacpi_inputdev->close = &hotkey_inputdev_close;
3163 hotkey_poll_setup_safe(true);
3165 return 0;
3167 err_exit:
3168 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3169 hotkey_dev_attributes = NULL;
3171 return (res < 0)? res : 1;
3174 static bool hotkey_notify_hotkey(const u32 hkey,
3175 bool *send_acpi_ev,
3176 bool *ignore_acpi_ev)
3178 /* 0x1000-0x1FFF: key presses */
3179 unsigned int scancode = hkey & 0xfff;
3180 *send_acpi_ev = true;
3181 *ignore_acpi_ev = false;
3183 if (scancode > 0 && scancode < 0x21) {
3184 scancode--;
3185 if (!(hotkey_source_mask & (1 << scancode))) {
3186 tpacpi_input_send_key_masked(scancode);
3187 *send_acpi_ev = false;
3188 } else {
3189 *ignore_acpi_ev = true;
3191 return true;
3193 return false;
3196 static bool hotkey_notify_wakeup(const u32 hkey,
3197 bool *send_acpi_ev,
3198 bool *ignore_acpi_ev)
3200 /* 0x2000-0x2FFF: Wakeup reason */
3201 *send_acpi_ev = true;
3202 *ignore_acpi_ev = false;
3204 switch (hkey) {
3205 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3206 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3207 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3208 *ignore_acpi_ev = true;
3209 break;
3211 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3212 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3213 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3214 *ignore_acpi_ev = true;
3215 break;
3217 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3218 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3219 printk(TPACPI_ALERT
3220 "EMERGENCY WAKEUP: battery almost empty\n");
3221 /* how to auto-heal: */
3222 /* 2313: woke up from S3, go to S4/S5 */
3223 /* 2413: woke up from S4, go to S5 */
3224 break;
3226 default:
3227 return false;
3230 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3231 printk(TPACPI_INFO
3232 "woke up due to a hot-unplug "
3233 "request...\n");
3234 hotkey_wakeup_reason_notify_change();
3236 return true;
3239 static bool hotkey_notify_usrevent(const u32 hkey,
3240 bool *send_acpi_ev,
3241 bool *ignore_acpi_ev)
3243 /* 0x5000-0x5FFF: human interface helpers */
3244 *send_acpi_ev = true;
3245 *ignore_acpi_ev = false;
3247 switch (hkey) {
3248 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3249 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3250 return true;
3252 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3253 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3254 tpacpi_input_send_tabletsw();
3255 hotkey_tablet_mode_notify_change();
3256 *send_acpi_ev = false;
3257 return true;
3259 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3260 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3261 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3262 /* do not propagate these events */
3263 *ignore_acpi_ev = true;
3264 return true;
3266 default:
3267 return false;
3271 static void thermal_dump_all_sensors(void);
3273 static bool hotkey_notify_thermal(const u32 hkey,
3274 bool *send_acpi_ev,
3275 bool *ignore_acpi_ev)
3277 bool known = true;
3279 /* 0x6000-0x6FFF: thermal alarms */
3280 *send_acpi_ev = true;
3281 *ignore_acpi_ev = false;
3283 switch (hkey) {
3284 case TP_HKEY_EV_THM_TABLE_CHANGED:
3285 printk(TPACPI_INFO
3286 "EC reports that Thermal Table has changed\n");
3287 /* recommended action: do nothing, we don't have
3288 * Lenovo ATM information */
3289 return true;
3290 case TP_HKEY_EV_ALARM_BAT_HOT:
3291 printk(TPACPI_CRIT
3292 "THERMAL ALARM: battery is too hot!\n");
3293 /* recommended action: warn user through gui */
3294 break;
3295 case TP_HKEY_EV_ALARM_BAT_XHOT:
3296 printk(TPACPI_ALERT
3297 "THERMAL EMERGENCY: battery is extremely hot!\n");
3298 /* recommended action: immediate sleep/hibernate */
3299 break;
3300 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3301 printk(TPACPI_CRIT
3302 "THERMAL ALARM: "
3303 "a sensor reports something is too hot!\n");
3304 /* recommended action: warn user through gui, that */
3305 /* some internal component is too hot */
3306 break;
3307 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3308 printk(TPACPI_ALERT
3309 "THERMAL EMERGENCY: "
3310 "a sensor reports something is extremely hot!\n");
3311 /* recommended action: immediate sleep/hibernate */
3312 break;
3313 default:
3314 printk(TPACPI_ALERT
3315 "THERMAL ALERT: unknown thermal alarm received\n");
3316 known = false;
3319 thermal_dump_all_sensors();
3321 return known;
3324 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3326 u32 hkey;
3327 bool send_acpi_ev;
3328 bool ignore_acpi_ev;
3329 bool known_ev;
3331 if (event != 0x80) {
3332 printk(TPACPI_ERR
3333 "unknown HKEY notification event %d\n", event);
3334 /* forward it to userspace, maybe it knows how to handle it */
3335 acpi_bus_generate_netlink_event(
3336 ibm->acpi->device->pnp.device_class,
3337 dev_name(&ibm->acpi->device->dev),
3338 event, 0);
3339 return;
3342 while (1) {
3343 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3344 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3345 return;
3348 if (hkey == 0) {
3349 /* queue empty */
3350 return;
3353 send_acpi_ev = true;
3354 ignore_acpi_ev = false;
3356 switch (hkey >> 12) {
3357 case 1:
3358 /* 0x1000-0x1FFF: key presses */
3359 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3360 &ignore_acpi_ev);
3361 break;
3362 case 2:
3363 /* 0x2000-0x2FFF: Wakeup reason */
3364 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3365 &ignore_acpi_ev);
3366 break;
3367 case 3:
3368 /* 0x3000-0x3FFF: bay-related wakeups */
3369 switch (hkey) {
3370 case TP_HKEY_EV_BAYEJ_ACK:
3371 hotkey_autosleep_ack = 1;
3372 printk(TPACPI_INFO
3373 "bay ejected\n");
3374 hotkey_wakeup_hotunplug_complete_notify_change();
3375 known_ev = true;
3376 break;
3377 case TP_HKEY_EV_OPTDRV_EJ:
3378 /* FIXME: kick libata if SATA link offline */
3379 known_ev = true;
3380 break;
3381 default:
3382 known_ev = false;
3384 break;
3385 case 4:
3386 /* 0x4000-0x4FFF: dock-related wakeups */
3387 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3388 hotkey_autosleep_ack = 1;
3389 printk(TPACPI_INFO
3390 "undocked\n");
3391 hotkey_wakeup_hotunplug_complete_notify_change();
3392 known_ev = true;
3393 } else {
3394 known_ev = false;
3396 break;
3397 case 5:
3398 /* 0x5000-0x5FFF: human interface helpers */
3399 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3400 &ignore_acpi_ev);
3401 break;
3402 case 6:
3403 /* 0x6000-0x6FFF: thermal alarms */
3404 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3405 &ignore_acpi_ev);
3406 break;
3407 case 7:
3408 /* 0x7000-0x7FFF: misc */
3409 if (tp_features.hotkey_wlsw &&
3410 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3411 tpacpi_send_radiosw_update();
3412 send_acpi_ev = 0;
3413 known_ev = true;
3414 break;
3416 /* fallthrough to default */
3417 default:
3418 known_ev = false;
3420 if (!known_ev) {
3421 printk(TPACPI_NOTICE
3422 "unhandled HKEY event 0x%04x\n", hkey);
3423 printk(TPACPI_NOTICE
3424 "please report the conditions when this "
3425 "event happened to %s\n", TPACPI_MAIL);
3428 /* Legacy events */
3429 if (!ignore_acpi_ev &&
3430 (send_acpi_ev || hotkey_report_mode < 2)) {
3431 acpi_bus_generate_proc_event(ibm->acpi->device,
3432 event, hkey);
3435 /* netlink events */
3436 if (!ignore_acpi_ev && send_acpi_ev) {
3437 acpi_bus_generate_netlink_event(
3438 ibm->acpi->device->pnp.device_class,
3439 dev_name(&ibm->acpi->device->dev),
3440 event, hkey);
3445 static void hotkey_suspend(pm_message_t state)
3447 /* Do these on suspend, we get the events on early resume! */
3448 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3449 hotkey_autosleep_ack = 0;
3452 static void hotkey_resume(void)
3454 tpacpi_disable_brightness_delay();
3456 if (hotkey_status_set(true) < 0 ||
3457 hotkey_mask_set(hotkey_acpi_mask) < 0)
3458 printk(TPACPI_ERR
3459 "error while attempting to reset the event "
3460 "firmware interface\n");
3462 tpacpi_send_radiosw_update();
3463 hotkey_tablet_mode_notify_change();
3464 hotkey_wakeup_reason_notify_change();
3465 hotkey_wakeup_hotunplug_complete_notify_change();
3466 hotkey_poll_setup_safe(false);
3469 /* procfs -------------------------------------------------------------- */
3470 static int hotkey_read(struct seq_file *m)
3472 int res, status;
3474 if (!tp_features.hotkey) {
3475 seq_printf(m, "status:\t\tnot supported\n");
3476 return 0;
3479 if (mutex_lock_killable(&hotkey_mutex))
3480 return -ERESTARTSYS;
3481 res = hotkey_status_get(&status);
3482 if (!res)
3483 res = hotkey_mask_get();
3484 mutex_unlock(&hotkey_mutex);
3485 if (res)
3486 return res;
3488 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3489 if (hotkey_all_mask) {
3490 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3491 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3492 } else {
3493 seq_printf(m, "mask:\t\tnot supported\n");
3494 seq_printf(m, "commands:\tenable, disable, reset\n");
3497 return 0;
3500 static void hotkey_enabledisable_warn(bool enable)
3502 tpacpi_log_usertask("procfs hotkey enable/disable");
3503 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3504 TPACPI_WARN
3505 "hotkey enable/disable functionality has been "
3506 "removed from the driver. Hotkeys are always "
3507 "enabled\n"))
3508 printk(TPACPI_ERR
3509 "Please remove the hotkey=enable module "
3510 "parameter, it is deprecated. Hotkeys are always "
3511 "enabled\n");
3514 static int hotkey_write(char *buf)
3516 int res;
3517 u32 mask;
3518 char *cmd;
3520 if (!tp_features.hotkey)
3521 return -ENODEV;
3523 if (mutex_lock_killable(&hotkey_mutex))
3524 return -ERESTARTSYS;
3526 mask = hotkey_user_mask;
3528 res = 0;
3529 while ((cmd = next_cmd(&buf))) {
3530 if (strlencmp(cmd, "enable") == 0) {
3531 hotkey_enabledisable_warn(1);
3532 } else if (strlencmp(cmd, "disable") == 0) {
3533 hotkey_enabledisable_warn(0);
3534 res = -EPERM;
3535 } else if (strlencmp(cmd, "reset") == 0) {
3536 mask = (hotkey_all_mask | hotkey_source_mask)
3537 & ~hotkey_reserved_mask;
3538 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3539 /* mask set */
3540 } else if (sscanf(cmd, "%x", &mask) == 1) {
3541 /* mask set */
3542 } else {
3543 res = -EINVAL;
3544 goto errexit;
3548 if (!res) {
3549 tpacpi_disclose_usertask("procfs hotkey",
3550 "set mask to 0x%08x\n", mask);
3551 res = hotkey_user_mask_set(mask);
3554 errexit:
3555 mutex_unlock(&hotkey_mutex);
3556 return res;
3559 static const struct acpi_device_id ibm_htk_device_ids[] = {
3560 {TPACPI_ACPI_HKEY_HID, 0},
3561 {"", 0},
3564 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3565 .hid = ibm_htk_device_ids,
3566 .notify = hotkey_notify,
3567 .handle = &hkey_handle,
3568 .type = ACPI_DEVICE_NOTIFY,
3571 static struct ibm_struct hotkey_driver_data = {
3572 .name = "hotkey",
3573 .read = hotkey_read,
3574 .write = hotkey_write,
3575 .exit = hotkey_exit,
3576 .resume = hotkey_resume,
3577 .suspend = hotkey_suspend,
3578 .acpi = &ibm_hotkey_acpidriver,
3581 /*************************************************************************
3582 * Bluetooth subdriver
3585 enum {
3586 /* ACPI GBDC/SBDC bits */
3587 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3588 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3589 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3590 0 = disable, 1 = enable */
3593 enum {
3594 /* ACPI \BLTH commands */
3595 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3596 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3597 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3598 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3599 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3602 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3604 static struct rfkill *tpacpi_bluetooth_rfkill;
3606 static void bluetooth_suspend(pm_message_t state)
3608 /* Try to make sure radio will resume powered off */
3609 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3610 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3611 vdbg_printk(TPACPI_DBG_RFKILL,
3612 "bluetooth power down on resume request failed\n");
3615 static int bluetooth_get_radiosw(void)
3617 int status;
3619 if (!tp_features.bluetooth)
3620 return -ENODEV;
3622 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3623 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3624 return RFKILL_STATE_HARD_BLOCKED;
3626 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3627 if (dbg_bluetoothemul)
3628 return (tpacpi_bluetooth_emulstate) ?
3629 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3630 #endif
3632 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3633 return -EIO;
3635 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3636 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3639 static void bluetooth_update_rfk(void)
3641 int status;
3643 if (!tpacpi_bluetooth_rfkill)
3644 return;
3646 status = bluetooth_get_radiosw();
3647 if (status < 0)
3648 return;
3649 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3651 vdbg_printk(TPACPI_DBG_RFKILL,
3652 "forced rfkill state to %d\n",
3653 status);
3656 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3658 int status;
3660 if (!tp_features.bluetooth)
3661 return -ENODEV;
3663 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3664 * reason to risk weird behaviour. */
3665 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3666 && radio_on)
3667 return -EPERM;
3669 vdbg_printk(TPACPI_DBG_RFKILL,
3670 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3672 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3673 if (dbg_bluetoothemul) {
3674 tpacpi_bluetooth_emulstate = !!radio_on;
3675 if (update_rfk)
3676 bluetooth_update_rfk();
3677 return 0;
3679 #endif
3681 if (radio_on)
3682 status = TP_ACPI_BLUETOOTH_RADIOSSW
3683 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3684 else
3685 status = 0;
3687 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3688 return -EIO;
3690 if (update_rfk)
3691 bluetooth_update_rfk();
3693 return 0;
3696 /* sysfs bluetooth enable ---------------------------------------------- */
3697 static ssize_t bluetooth_enable_show(struct device *dev,
3698 struct device_attribute *attr,
3699 char *buf)
3701 int status;
3703 printk_deprecated_rfkill_attribute("bluetooth_enable");
3705 status = bluetooth_get_radiosw();
3706 if (status < 0)
3707 return status;
3709 return snprintf(buf, PAGE_SIZE, "%d\n",
3710 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3713 static ssize_t bluetooth_enable_store(struct device *dev,
3714 struct device_attribute *attr,
3715 const char *buf, size_t count)
3717 unsigned long t;
3718 int res;
3720 printk_deprecated_rfkill_attribute("bluetooth_enable");
3722 if (parse_strtoul(buf, 1, &t))
3723 return -EINVAL;
3725 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3727 res = bluetooth_set_radiosw(t, 1);
3729 return (res) ? res : count;
3732 static struct device_attribute dev_attr_bluetooth_enable =
3733 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3734 bluetooth_enable_show, bluetooth_enable_store);
3736 /* --------------------------------------------------------------------- */
3738 static struct attribute *bluetooth_attributes[] = {
3739 &dev_attr_bluetooth_enable.attr,
3740 NULL
3743 static const struct attribute_group bluetooth_attr_group = {
3744 .attrs = bluetooth_attributes,
3747 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3749 int bts = bluetooth_get_radiosw();
3751 if (bts < 0)
3752 return bts;
3754 *state = bts;
3755 return 0;
3758 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3760 dbg_printk(TPACPI_DBG_RFKILL,
3761 "request to change radio state to %d\n", state);
3762 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3765 static void bluetooth_shutdown(void)
3767 /* Order firmware to save current state to NVRAM */
3768 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3769 TP_ACPI_BLTH_SAVE_STATE))
3770 printk(TPACPI_NOTICE
3771 "failed to save bluetooth state to NVRAM\n");
3772 else
3773 vdbg_printk(TPACPI_DBG_RFKILL,
3774 "bluestooth state saved to NVRAM\n");
3777 static void bluetooth_exit(void)
3779 bluetooth_shutdown();
3781 if (tpacpi_bluetooth_rfkill)
3782 rfkill_unregister(tpacpi_bluetooth_rfkill);
3784 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3785 &bluetooth_attr_group);
3788 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3790 int res;
3791 int status = 0;
3793 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3794 "initializing bluetooth subdriver\n");
3796 TPACPI_ACPIHANDLE_INIT(hkey);
3798 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3799 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3800 tp_features.bluetooth = hkey_handle &&
3801 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3803 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3804 "bluetooth is %s, status 0x%02x\n",
3805 str_supported(tp_features.bluetooth),
3806 status);
3808 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3809 if (dbg_bluetoothemul) {
3810 tp_features.bluetooth = 1;
3811 printk(TPACPI_INFO
3812 "bluetooth switch emulation enabled\n");
3813 } else
3814 #endif
3815 if (tp_features.bluetooth &&
3816 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3817 /* no bluetooth hardware present in system */
3818 tp_features.bluetooth = 0;
3819 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3820 "bluetooth hardware not installed\n");
3823 if (!tp_features.bluetooth)
3824 return 1;
3826 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3827 &bluetooth_attr_group);
3828 if (res)
3829 return res;
3831 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3832 &tpacpi_bluetooth_rfkill,
3833 RFKILL_TYPE_BLUETOOTH,
3834 TPACPI_RFK_BLUETOOTH_SW_NAME,
3835 true,
3836 tpacpi_bluetooth_rfk_set,
3837 tpacpi_bluetooth_rfk_get);
3838 if (res) {
3839 bluetooth_exit();
3840 return res;
3843 return 0;
3846 /* procfs -------------------------------------------------------------- */
3847 static int bluetooth_read(struct seq_file *m)
3849 int status = bluetooth_get_radiosw();
3851 if (!tp_features.bluetooth)
3852 seq_printf(m, "status:\t\tnot supported\n");
3853 else {
3854 seq_printf(m, "status:\t\t%s\n",
3855 (status == RFKILL_STATE_UNBLOCKED) ?
3856 "enabled" : "disabled");
3857 seq_printf(m, "commands:\tenable, disable\n");
3860 return 0;
3863 static int bluetooth_write(char *buf)
3865 char *cmd;
3866 int state = -1;
3868 if (!tp_features.bluetooth)
3869 return -ENODEV;
3871 while ((cmd = next_cmd(&buf))) {
3872 if (strlencmp(cmd, "enable") == 0) {
3873 state = 1;
3874 } else if (strlencmp(cmd, "disable") == 0) {
3875 state = 0;
3876 } else
3877 return -EINVAL;
3880 if (state != -1) {
3881 tpacpi_disclose_usertask("procfs bluetooth",
3882 "attempt to %s\n",
3883 state ? "enable" : "disable");
3884 bluetooth_set_radiosw(state, 1);
3887 return 0;
3890 static struct ibm_struct bluetooth_driver_data = {
3891 .name = "bluetooth",
3892 .read = bluetooth_read,
3893 .write = bluetooth_write,
3894 .exit = bluetooth_exit,
3895 .suspend = bluetooth_suspend,
3896 .shutdown = bluetooth_shutdown,
3899 /*************************************************************************
3900 * Wan subdriver
3903 enum {
3904 /* ACPI GWAN/SWAN bits */
3905 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3906 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3907 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3908 0 = disable, 1 = enable */
3911 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3913 static struct rfkill *tpacpi_wan_rfkill;
3915 static void wan_suspend(pm_message_t state)
3917 /* Try to make sure radio will resume powered off */
3918 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3919 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3920 vdbg_printk(TPACPI_DBG_RFKILL,
3921 "WWAN power down on resume request failed\n");
3924 static int wan_get_radiosw(void)
3926 int status;
3928 if (!tp_features.wan)
3929 return -ENODEV;
3931 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3932 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3933 return RFKILL_STATE_HARD_BLOCKED;
3935 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3936 if (dbg_wwanemul)
3937 return (tpacpi_wwan_emulstate) ?
3938 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3939 #endif
3941 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3942 return -EIO;
3944 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3945 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3948 static void wan_update_rfk(void)
3950 int status;
3952 if (!tpacpi_wan_rfkill)
3953 return;
3955 status = wan_get_radiosw();
3956 if (status < 0)
3957 return;
3958 rfkill_force_state(tpacpi_wan_rfkill, status);
3960 vdbg_printk(TPACPI_DBG_RFKILL,
3961 "forced rfkill state to %d\n",
3962 status);
3965 static int wan_set_radiosw(int radio_on, int update_rfk)
3967 int status;
3969 if (!tp_features.wan)
3970 return -ENODEV;
3972 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3973 * reason to risk weird behaviour. */
3974 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3975 && radio_on)
3976 return -EPERM;
3978 vdbg_printk(TPACPI_DBG_RFKILL,
3979 "will %s WWAN\n", radio_on ? "enable" : "disable");
3981 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3982 if (dbg_wwanemul) {
3983 tpacpi_wwan_emulstate = !!radio_on;
3984 if (update_rfk)
3985 wan_update_rfk();
3986 return 0;
3988 #endif
3990 if (radio_on)
3991 status = TP_ACPI_WANCARD_RADIOSSW
3992 | TP_ACPI_WANCARD_RESUMECTRL;
3993 else
3994 status = 0;
3996 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3997 return -EIO;
3999 if (update_rfk)
4000 wan_update_rfk();
4002 return 0;
4005 /* sysfs wan enable ---------------------------------------------------- */
4006 static ssize_t wan_enable_show(struct device *dev,
4007 struct device_attribute *attr,
4008 char *buf)
4010 int status;
4012 printk_deprecated_rfkill_attribute("wwan_enable");
4014 status = wan_get_radiosw();
4015 if (status < 0)
4016 return status;
4018 return snprintf(buf, PAGE_SIZE, "%d\n",
4019 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
4022 static ssize_t wan_enable_store(struct device *dev,
4023 struct device_attribute *attr,
4024 const char *buf, size_t count)
4026 unsigned long t;
4027 int res;
4029 printk_deprecated_rfkill_attribute("wwan_enable");
4031 if (parse_strtoul(buf, 1, &t))
4032 return -EINVAL;
4034 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4036 res = wan_set_radiosw(t, 1);
4038 return (res) ? res : count;
4041 static struct device_attribute dev_attr_wan_enable =
4042 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4043 wan_enable_show, wan_enable_store);
4045 /* --------------------------------------------------------------------- */
4047 static struct attribute *wan_attributes[] = {
4048 &dev_attr_wan_enable.attr,
4049 NULL
4052 static const struct attribute_group wan_attr_group = {
4053 .attrs = wan_attributes,
4056 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4058 int wans = wan_get_radiosw();
4060 if (wans < 0)
4061 return wans;
4063 *state = wans;
4064 return 0;
4067 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4069 dbg_printk(TPACPI_DBG_RFKILL,
4070 "request to change radio state to %d\n", state);
4071 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4074 static void wan_shutdown(void)
4076 /* Order firmware to save current state to NVRAM */
4077 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4078 TP_ACPI_WGSV_SAVE_STATE))
4079 printk(TPACPI_NOTICE
4080 "failed to save WWAN state to NVRAM\n");
4081 else
4082 vdbg_printk(TPACPI_DBG_RFKILL,
4083 "WWAN state saved to NVRAM\n");
4086 static void wan_exit(void)
4088 wan_shutdown();
4090 if (tpacpi_wan_rfkill)
4091 rfkill_unregister(tpacpi_wan_rfkill);
4093 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4094 &wan_attr_group);
4097 static int __init wan_init(struct ibm_init_struct *iibm)
4099 int res;
4100 int status = 0;
4102 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4103 "initializing wan subdriver\n");
4105 TPACPI_ACPIHANDLE_INIT(hkey);
4107 tp_features.wan = hkey_handle &&
4108 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4110 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4111 "wan is %s, status 0x%02x\n",
4112 str_supported(tp_features.wan),
4113 status);
4115 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4116 if (dbg_wwanemul) {
4117 tp_features.wan = 1;
4118 printk(TPACPI_INFO
4119 "wwan switch emulation enabled\n");
4120 } else
4121 #endif
4122 if (tp_features.wan &&
4123 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4124 /* no wan hardware present in system */
4125 tp_features.wan = 0;
4126 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4127 "wan hardware not installed\n");
4130 if (!tp_features.wan)
4131 return 1;
4133 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4134 &wan_attr_group);
4135 if (res)
4136 return res;
4138 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4139 &tpacpi_wan_rfkill,
4140 RFKILL_TYPE_WWAN,
4141 TPACPI_RFK_WWAN_SW_NAME,
4142 true,
4143 tpacpi_wan_rfk_set,
4144 tpacpi_wan_rfk_get);
4145 if (res) {
4146 wan_exit();
4147 return res;
4150 return 0;
4153 /* procfs -------------------------------------------------------------- */
4154 static int wan_read(struct seq_file *m)
4156 int status = wan_get_radiosw();
4158 tpacpi_disclose_usertask("procfs wan", "read");
4160 if (!tp_features.wan)
4161 seq_printf(m, "status:\t\tnot supported\n");
4162 else {
4163 seq_printf(m, "status:\t\t%s\n",
4164 (status == RFKILL_STATE_UNBLOCKED) ?
4165 "enabled" : "disabled");
4166 seq_printf(m, "commands:\tenable, disable\n");
4169 return 0;
4172 static int wan_write(char *buf)
4174 char *cmd;
4175 int state = -1;
4177 if (!tp_features.wan)
4178 return -ENODEV;
4180 while ((cmd = next_cmd(&buf))) {
4181 if (strlencmp(cmd, "enable") == 0) {
4182 state = 1;
4183 } else if (strlencmp(cmd, "disable") == 0) {
4184 state = 0;
4185 } else
4186 return -EINVAL;
4189 if (state != -1) {
4190 tpacpi_disclose_usertask("procfs wan",
4191 "attempt to %s\n",
4192 state ? "enable" : "disable");
4193 wan_set_radiosw(state, 1);
4196 return 0;
4199 static struct ibm_struct wan_driver_data = {
4200 .name = "wan",
4201 .read = wan_read,
4202 .write = wan_write,
4203 .exit = wan_exit,
4204 .suspend = wan_suspend,
4205 .shutdown = wan_shutdown,
4208 /*************************************************************************
4209 * UWB subdriver
4212 enum {
4213 /* ACPI GUWB/SUWB bits */
4214 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4215 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4218 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4220 static struct rfkill *tpacpi_uwb_rfkill;
4222 static int uwb_get_radiosw(void)
4224 int status;
4226 if (!tp_features.uwb)
4227 return -ENODEV;
4229 /* WLSW overrides UWB in firmware/hardware, reflect that */
4230 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4231 return RFKILL_STATE_HARD_BLOCKED;
4233 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4234 if (dbg_uwbemul)
4235 return (tpacpi_uwb_emulstate) ?
4236 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4237 #endif
4239 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4240 return -EIO;
4242 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4243 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4246 static void uwb_update_rfk(void)
4248 int status;
4250 if (!tpacpi_uwb_rfkill)
4251 return;
4253 status = uwb_get_radiosw();
4254 if (status < 0)
4255 return;
4256 rfkill_force_state(tpacpi_uwb_rfkill, status);
4258 vdbg_printk(TPACPI_DBG_RFKILL,
4259 "forced rfkill state to %d\n",
4260 status);
4263 static int uwb_set_radiosw(int radio_on, int update_rfk)
4265 int status;
4267 if (!tp_features.uwb)
4268 return -ENODEV;
4270 /* WLSW overrides UWB in firmware/hardware, but there is no
4271 * reason to risk weird behaviour. */
4272 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4273 && radio_on)
4274 return -EPERM;
4276 vdbg_printk(TPACPI_DBG_RFKILL,
4277 "will %s UWB\n", radio_on ? "enable" : "disable");
4279 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4280 if (dbg_uwbemul) {
4281 tpacpi_uwb_emulstate = !!radio_on;
4282 if (update_rfk)
4283 uwb_update_rfk();
4284 return 0;
4286 #endif
4288 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4289 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4290 return -EIO;
4292 if (update_rfk)
4293 uwb_update_rfk();
4295 return 0;
4298 /* --------------------------------------------------------------------- */
4300 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4302 int uwbs = uwb_get_radiosw();
4304 if (uwbs < 0)
4305 return uwbs;
4307 *state = uwbs;
4308 return 0;
4311 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4313 dbg_printk(TPACPI_DBG_RFKILL,
4314 "request to change radio state to %d\n", state);
4315 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4318 static void uwb_exit(void)
4320 if (tpacpi_uwb_rfkill)
4321 rfkill_unregister(tpacpi_uwb_rfkill);
4324 static int __init uwb_init(struct ibm_init_struct *iibm)
4326 int res;
4327 int status = 0;
4329 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4330 "initializing uwb subdriver\n");
4332 TPACPI_ACPIHANDLE_INIT(hkey);
4334 tp_features.uwb = hkey_handle &&
4335 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4337 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4338 "uwb is %s, status 0x%02x\n",
4339 str_supported(tp_features.uwb),
4340 status);
4342 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4343 if (dbg_uwbemul) {
4344 tp_features.uwb = 1;
4345 printk(TPACPI_INFO
4346 "uwb switch emulation enabled\n");
4347 } else
4348 #endif
4349 if (tp_features.uwb &&
4350 !(status & TP_ACPI_UWB_HWPRESENT)) {
4351 /* no uwb hardware present in system */
4352 tp_features.uwb = 0;
4353 dbg_printk(TPACPI_DBG_INIT,
4354 "uwb hardware not installed\n");
4357 if (!tp_features.uwb)
4358 return 1;
4360 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4361 &tpacpi_uwb_rfkill,
4362 RFKILL_TYPE_UWB,
4363 TPACPI_RFK_UWB_SW_NAME,
4364 false,
4365 tpacpi_uwb_rfk_set,
4366 tpacpi_uwb_rfk_get);
4368 return res;
4371 static struct ibm_struct uwb_driver_data = {
4372 .name = "uwb",
4373 .exit = uwb_exit,
4374 .flags.experimental = 1,
4377 /*************************************************************************
4378 * Video subdriver
4381 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4383 enum video_access_mode {
4384 TPACPI_VIDEO_NONE = 0,
4385 TPACPI_VIDEO_570, /* 570 */
4386 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4387 TPACPI_VIDEO_NEW, /* all others */
4390 enum { /* video status flags, based on VIDEO_570 */
4391 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4392 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4393 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4396 enum { /* TPACPI_VIDEO_570 constants */
4397 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4398 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4399 * video_status_flags */
4400 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4401 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4404 static enum video_access_mode video_supported;
4405 static int video_orig_autosw;
4407 static int video_autosw_get(void);
4408 static int video_autosw_set(int enable);
4410 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4412 static int __init video_init(struct ibm_init_struct *iibm)
4414 int ivga;
4416 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4418 TPACPI_ACPIHANDLE_INIT(vid);
4419 TPACPI_ACPIHANDLE_INIT(vid2);
4421 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4422 /* G41, assume IVGA doesn't change */
4423 vid_handle = vid2_handle;
4425 if (!vid_handle)
4426 /* video switching not supported on R30, R31 */
4427 video_supported = TPACPI_VIDEO_NONE;
4428 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4429 /* 570 */
4430 video_supported = TPACPI_VIDEO_570;
4431 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4432 /* 600e/x, 770e, 770x */
4433 video_supported = TPACPI_VIDEO_770;
4434 else
4435 /* all others */
4436 video_supported = TPACPI_VIDEO_NEW;
4438 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4439 str_supported(video_supported != TPACPI_VIDEO_NONE),
4440 video_supported);
4442 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4445 static void video_exit(void)
4447 dbg_printk(TPACPI_DBG_EXIT,
4448 "restoring original video autoswitch mode\n");
4449 if (video_autosw_set(video_orig_autosw))
4450 printk(TPACPI_ERR "error while trying to restore original "
4451 "video autoswitch mode\n");
4454 static int video_outputsw_get(void)
4456 int status = 0;
4457 int i;
4459 switch (video_supported) {
4460 case TPACPI_VIDEO_570:
4461 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4462 TP_ACPI_VIDEO_570_PHSCMD))
4463 return -EIO;
4464 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4465 break;
4466 case TPACPI_VIDEO_770:
4467 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4468 return -EIO;
4469 if (i)
4470 status |= TP_ACPI_VIDEO_S_LCD;
4471 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_CRT;
4475 break;
4476 case TPACPI_VIDEO_NEW:
4477 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4478 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_CRT;
4483 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4484 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4485 return -EIO;
4486 if (i)
4487 status |= TP_ACPI_VIDEO_S_LCD;
4488 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_DVI;
4492 break;
4493 default:
4494 return -ENOSYS;
4497 return status;
4500 static int video_outputsw_set(int status)
4502 int autosw;
4503 int res = 0;
4505 switch (video_supported) {
4506 case TPACPI_VIDEO_570:
4507 res = acpi_evalf(NULL, NULL,
4508 "\\_SB.PHS2", "vdd",
4509 TP_ACPI_VIDEO_570_PHS2CMD,
4510 status | TP_ACPI_VIDEO_570_PHS2SET);
4511 break;
4512 case TPACPI_VIDEO_770:
4513 autosw = video_autosw_get();
4514 if (autosw < 0)
4515 return autosw;
4517 res = video_autosw_set(1);
4518 if (res)
4519 return res;
4520 res = acpi_evalf(vid_handle, NULL,
4521 "ASWT", "vdd", status * 0x100, 0);
4522 if (!autosw && video_autosw_set(autosw)) {
4523 printk(TPACPI_ERR
4524 "video auto-switch left enabled due to error\n");
4525 return -EIO;
4527 break;
4528 case TPACPI_VIDEO_NEW:
4529 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4530 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4531 break;
4532 default:
4533 return -ENOSYS;
4536 return (res)? 0 : -EIO;
4539 static int video_autosw_get(void)
4541 int autosw = 0;
4543 switch (video_supported) {
4544 case TPACPI_VIDEO_570:
4545 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4546 return -EIO;
4547 break;
4548 case TPACPI_VIDEO_770:
4549 case TPACPI_VIDEO_NEW:
4550 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4551 return -EIO;
4552 break;
4553 default:
4554 return -ENOSYS;
4557 return autosw & 1;
4560 static int video_autosw_set(int enable)
4562 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4563 return -EIO;
4564 return 0;
4567 static int video_outputsw_cycle(void)
4569 int autosw = video_autosw_get();
4570 int res;
4572 if (autosw < 0)
4573 return autosw;
4575 switch (video_supported) {
4576 case TPACPI_VIDEO_570:
4577 res = video_autosw_set(1);
4578 if (res)
4579 return res;
4580 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4581 break;
4582 case TPACPI_VIDEO_770:
4583 case TPACPI_VIDEO_NEW:
4584 res = video_autosw_set(1);
4585 if (res)
4586 return res;
4587 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4588 break;
4589 default:
4590 return -ENOSYS;
4592 if (!autosw && video_autosw_set(autosw)) {
4593 printk(TPACPI_ERR
4594 "video auto-switch left enabled due to error\n");
4595 return -EIO;
4598 return (res)? 0 : -EIO;
4601 static int video_expand_toggle(void)
4603 switch (video_supported) {
4604 case TPACPI_VIDEO_570:
4605 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_770:
4608 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4609 0 : -EIO;
4610 case TPACPI_VIDEO_NEW:
4611 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4612 0 : -EIO;
4613 default:
4614 return -ENOSYS;
4616 /* not reached */
4619 static int video_read(struct seq_file *m)
4621 int status, autosw;
4623 if (video_supported == TPACPI_VIDEO_NONE) {
4624 seq_printf(m, "status:\t\tnot supported\n");
4625 return 0;
4628 /* Even reads can crash X.org, so... */
4629 if (!capable(CAP_SYS_ADMIN))
4630 return -EPERM;
4632 status = video_outputsw_get();
4633 if (status < 0)
4634 return status;
4636 autosw = video_autosw_get();
4637 if (autosw < 0)
4638 return autosw;
4640 seq_printf(m, "status:\t\tsupported\n");
4641 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4642 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4643 if (video_supported == TPACPI_VIDEO_NEW)
4644 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4645 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4646 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4647 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4648 if (video_supported == TPACPI_VIDEO_NEW)
4649 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4650 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4651 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4653 return 0;
4656 static int video_write(char *buf)
4658 char *cmd;
4659 int enable, disable, status;
4660 int res;
4662 if (video_supported == TPACPI_VIDEO_NONE)
4663 return -ENODEV;
4665 /* Even reads can crash X.org, let alone writes... */
4666 if (!capable(CAP_SYS_ADMIN))
4667 return -EPERM;
4669 enable = 0;
4670 disable = 0;
4672 while ((cmd = next_cmd(&buf))) {
4673 if (strlencmp(cmd, "lcd_enable") == 0) {
4674 enable |= TP_ACPI_VIDEO_S_LCD;
4675 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4676 disable |= TP_ACPI_VIDEO_S_LCD;
4677 } else if (strlencmp(cmd, "crt_enable") == 0) {
4678 enable |= TP_ACPI_VIDEO_S_CRT;
4679 } else if (strlencmp(cmd, "crt_disable") == 0) {
4680 disable |= TP_ACPI_VIDEO_S_CRT;
4681 } else if (video_supported == TPACPI_VIDEO_NEW &&
4682 strlencmp(cmd, "dvi_enable") == 0) {
4683 enable |= TP_ACPI_VIDEO_S_DVI;
4684 } else if (video_supported == TPACPI_VIDEO_NEW &&
4685 strlencmp(cmd, "dvi_disable") == 0) {
4686 disable |= TP_ACPI_VIDEO_S_DVI;
4687 } else if (strlencmp(cmd, "auto_enable") == 0) {
4688 res = video_autosw_set(1);
4689 if (res)
4690 return res;
4691 } else if (strlencmp(cmd, "auto_disable") == 0) {
4692 res = video_autosw_set(0);
4693 if (res)
4694 return res;
4695 } else if (strlencmp(cmd, "video_switch") == 0) {
4696 res = video_outputsw_cycle();
4697 if (res)
4698 return res;
4699 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4700 res = video_expand_toggle();
4701 if (res)
4702 return res;
4703 } else
4704 return -EINVAL;
4707 if (enable || disable) {
4708 status = video_outputsw_get();
4709 if (status < 0)
4710 return status;
4711 res = video_outputsw_set((status & ~disable) | enable);
4712 if (res)
4713 return res;
4716 return 0;
4719 static struct ibm_struct video_driver_data = {
4720 .name = "video",
4721 .read = video_read,
4722 .write = video_write,
4723 .exit = video_exit,
4726 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4728 /*************************************************************************
4729 * Light (thinklight) subdriver
4732 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4733 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4735 static int light_get_status(void)
4737 int status = 0;
4739 if (tp_features.light_status) {
4740 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4741 return -EIO;
4742 return (!!status);
4745 return -ENXIO;
4748 static int light_set_status(int status)
4750 int rc;
4752 if (tp_features.light) {
4753 if (cmos_handle) {
4754 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4755 (status)?
4756 TP_CMOS_THINKLIGHT_ON :
4757 TP_CMOS_THINKLIGHT_OFF);
4758 } else {
4759 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4760 (status)? 1 : 0);
4762 return (rc)? 0 : -EIO;
4765 return -ENXIO;
4768 static void light_set_status_worker(struct work_struct *work)
4770 struct tpacpi_led_classdev *data =
4771 container_of(work, struct tpacpi_led_classdev, work);
4773 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4774 light_set_status((data->new_state != TPACPI_LED_OFF));
4777 static void light_sysfs_set(struct led_classdev *led_cdev,
4778 enum led_brightness brightness)
4780 struct tpacpi_led_classdev *data =
4781 container_of(led_cdev,
4782 struct tpacpi_led_classdev,
4783 led_classdev);
4784 data->new_state = (brightness != LED_OFF) ?
4785 TPACPI_LED_ON : TPACPI_LED_OFF;
4786 queue_work(tpacpi_wq, &data->work);
4789 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4791 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4794 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4795 .led_classdev = {
4796 .name = "tpacpi::thinklight",
4797 .brightness_set = &light_sysfs_set,
4798 .brightness_get = &light_sysfs_get,
4802 static int __init light_init(struct ibm_init_struct *iibm)
4804 int rc;
4806 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4808 TPACPI_ACPIHANDLE_INIT(ledb);
4809 TPACPI_ACPIHANDLE_INIT(lght);
4810 TPACPI_ACPIHANDLE_INIT(cmos);
4811 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4813 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4814 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4816 if (tp_features.light)
4817 /* light status not supported on
4818 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4819 tp_features.light_status =
4820 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4822 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4823 str_supported(tp_features.light),
4824 str_supported(tp_features.light_status));
4826 if (!tp_features.light)
4827 return 1;
4829 rc = led_classdev_register(&tpacpi_pdev->dev,
4830 &tpacpi_led_thinklight.led_classdev);
4832 if (rc < 0) {
4833 tp_features.light = 0;
4834 tp_features.light_status = 0;
4835 } else {
4836 rc = 0;
4839 return rc;
4842 static void light_exit(void)
4844 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4845 if (work_pending(&tpacpi_led_thinklight.work))
4846 flush_workqueue(tpacpi_wq);
4849 static int light_read(struct seq_file *m)
4851 int status;
4853 if (!tp_features.light) {
4854 seq_printf(m, "status:\t\tnot supported\n");
4855 } else if (!tp_features.light_status) {
4856 seq_printf(m, "status:\t\tunknown\n");
4857 seq_printf(m, "commands:\ton, off\n");
4858 } else {
4859 status = light_get_status();
4860 if (status < 0)
4861 return status;
4862 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4863 seq_printf(m, "commands:\ton, off\n");
4866 return 0;
4869 static int light_write(char *buf)
4871 char *cmd;
4872 int newstatus = 0;
4874 if (!tp_features.light)
4875 return -ENODEV;
4877 while ((cmd = next_cmd(&buf))) {
4878 if (strlencmp(cmd, "on") == 0) {
4879 newstatus = 1;
4880 } else if (strlencmp(cmd, "off") == 0) {
4881 newstatus = 0;
4882 } else
4883 return -EINVAL;
4886 return light_set_status(newstatus);
4889 static struct ibm_struct light_driver_data = {
4890 .name = "light",
4891 .read = light_read,
4892 .write = light_write,
4893 .exit = light_exit,
4896 /*************************************************************************
4897 * CMOS subdriver
4900 /* sysfs cmos_command -------------------------------------------------- */
4901 static ssize_t cmos_command_store(struct device *dev,
4902 struct device_attribute *attr,
4903 const char *buf, size_t count)
4905 unsigned long cmos_cmd;
4906 int res;
4908 if (parse_strtoul(buf, 21, &cmos_cmd))
4909 return -EINVAL;
4911 res = issue_thinkpad_cmos_command(cmos_cmd);
4912 return (res)? res : count;
4915 static struct device_attribute dev_attr_cmos_command =
4916 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4918 /* --------------------------------------------------------------------- */
4920 static int __init cmos_init(struct ibm_init_struct *iibm)
4922 int res;
4924 vdbg_printk(TPACPI_DBG_INIT,
4925 "initializing cmos commands subdriver\n");
4927 TPACPI_ACPIHANDLE_INIT(cmos);
4929 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4930 str_supported(cmos_handle != NULL));
4932 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4933 if (res)
4934 return res;
4936 return (cmos_handle)? 0 : 1;
4939 static void cmos_exit(void)
4941 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4944 static int cmos_read(struct seq_file *m)
4946 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4947 R30, R31, T20-22, X20-21 */
4948 if (!cmos_handle)
4949 seq_printf(m, "status:\t\tnot supported\n");
4950 else {
4951 seq_printf(m, "status:\t\tsupported\n");
4952 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4955 return 0;
4958 static int cmos_write(char *buf)
4960 char *cmd;
4961 int cmos_cmd, res;
4963 while ((cmd = next_cmd(&buf))) {
4964 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4965 cmos_cmd >= 0 && cmos_cmd <= 21) {
4966 /* cmos_cmd set */
4967 } else
4968 return -EINVAL;
4970 res = issue_thinkpad_cmos_command(cmos_cmd);
4971 if (res)
4972 return res;
4975 return 0;
4978 static struct ibm_struct cmos_driver_data = {
4979 .name = "cmos",
4980 .read = cmos_read,
4981 .write = cmos_write,
4982 .exit = cmos_exit,
4985 /*************************************************************************
4986 * LED subdriver
4989 enum led_access_mode {
4990 TPACPI_LED_NONE = 0,
4991 TPACPI_LED_570, /* 570 */
4992 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4993 TPACPI_LED_NEW, /* all others */
4996 enum { /* For TPACPI_LED_OLD */
4997 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4998 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4999 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5002 static enum led_access_mode led_supported;
5004 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5005 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5006 /* T20-22, X20-21 */
5007 "LED", /* all others */
5008 ); /* R30, R31 */
5010 #define TPACPI_LED_NUMLEDS 16
5011 static struct tpacpi_led_classdev *tpacpi_leds;
5012 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5013 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5014 /* there's a limit of 19 chars + NULL before 2.6.26 */
5015 "tpacpi::power",
5016 "tpacpi:orange:batt",
5017 "tpacpi:green:batt",
5018 "tpacpi::dock_active",
5019 "tpacpi::bay_active",
5020 "tpacpi::dock_batt",
5021 "tpacpi::unknown_led",
5022 "tpacpi::standby",
5023 "tpacpi::dock_status1",
5024 "tpacpi::dock_status2",
5025 "tpacpi::unknown_led2",
5026 "tpacpi::unknown_led3",
5027 "tpacpi::thinkvantage",
5029 #define TPACPI_SAFE_LEDS 0x1081U
5031 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5033 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5034 return false;
5035 #else
5036 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5037 #endif
5040 static int led_get_status(const unsigned int led)
5042 int status;
5043 enum led_status_t led_s;
5045 switch (led_supported) {
5046 case TPACPI_LED_570:
5047 if (!acpi_evalf(ec_handle,
5048 &status, "GLED", "dd", 1 << led))
5049 return -EIO;
5050 led_s = (status == 0)?
5051 TPACPI_LED_OFF :
5052 ((status == 1)?
5053 TPACPI_LED_ON :
5054 TPACPI_LED_BLINK);
5055 tpacpi_led_state_cache[led] = led_s;
5056 return led_s;
5057 default:
5058 return -ENXIO;
5061 /* not reached */
5064 static int led_set_status(const unsigned int led,
5065 const enum led_status_t ledstatus)
5067 /* off, on, blink. Index is led_status_t */
5068 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5069 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5071 int rc = 0;
5073 switch (led_supported) {
5074 case TPACPI_LED_570:
5075 /* 570 */
5076 if (unlikely(led > 7))
5077 return -EINVAL;
5078 if (unlikely(tpacpi_is_led_restricted(led)))
5079 return -EPERM;
5080 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5081 (1 << led), led_sled_arg1[ledstatus]))
5082 rc = -EIO;
5083 break;
5084 case TPACPI_LED_OLD:
5085 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5086 if (unlikely(led > 7))
5087 return -EINVAL;
5088 if (unlikely(tpacpi_is_led_restricted(led)))
5089 return -EPERM;
5090 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5091 if (rc >= 0)
5092 rc = ec_write(TPACPI_LED_EC_HLBL,
5093 (ledstatus == TPACPI_LED_BLINK) << led);
5094 if (rc >= 0)
5095 rc = ec_write(TPACPI_LED_EC_HLCL,
5096 (ledstatus != TPACPI_LED_OFF) << led);
5097 break;
5098 case TPACPI_LED_NEW:
5099 /* all others */
5100 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5101 return -EINVAL;
5102 if (unlikely(tpacpi_is_led_restricted(led)))
5103 return -EPERM;
5104 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5105 led, led_led_arg1[ledstatus]))
5106 rc = -EIO;
5107 break;
5108 default:
5109 rc = -ENXIO;
5112 if (!rc)
5113 tpacpi_led_state_cache[led] = ledstatus;
5115 return rc;
5118 static void led_set_status_worker(struct work_struct *work)
5120 struct tpacpi_led_classdev *data =
5121 container_of(work, struct tpacpi_led_classdev, work);
5123 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5124 led_set_status(data->led, data->new_state);
5127 static void led_sysfs_set(struct led_classdev *led_cdev,
5128 enum led_brightness brightness)
5130 struct tpacpi_led_classdev *data = container_of(led_cdev,
5131 struct tpacpi_led_classdev, led_classdev);
5133 if (brightness == LED_OFF)
5134 data->new_state = TPACPI_LED_OFF;
5135 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5136 data->new_state = TPACPI_LED_ON;
5137 else
5138 data->new_state = TPACPI_LED_BLINK;
5140 queue_work(tpacpi_wq, &data->work);
5143 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5144 unsigned long *delay_on, unsigned long *delay_off)
5146 struct tpacpi_led_classdev *data = container_of(led_cdev,
5147 struct tpacpi_led_classdev, led_classdev);
5149 /* Can we choose the flash rate? */
5150 if (*delay_on == 0 && *delay_off == 0) {
5151 /* yes. set them to the hardware blink rate (1 Hz) */
5152 *delay_on = 500; /* ms */
5153 *delay_off = 500; /* ms */
5154 } else if ((*delay_on != 500) || (*delay_off != 500))
5155 return -EINVAL;
5157 data->new_state = TPACPI_LED_BLINK;
5158 queue_work(tpacpi_wq, &data->work);
5160 return 0;
5163 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5165 int rc;
5167 struct tpacpi_led_classdev *data = container_of(led_cdev,
5168 struct tpacpi_led_classdev, led_classdev);
5170 rc = led_get_status(data->led);
5172 if (rc == TPACPI_LED_OFF || rc < 0)
5173 rc = LED_OFF; /* no error handling in led class :( */
5174 else
5175 rc = LED_FULL;
5177 return rc;
5180 static void led_exit(void)
5182 unsigned int i;
5184 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5185 if (tpacpi_leds[i].led_classdev.name)
5186 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5189 kfree(tpacpi_leds);
5192 static int __init tpacpi_init_led(unsigned int led)
5194 int rc;
5196 tpacpi_leds[led].led = led;
5198 /* LEDs with no name don't get registered */
5199 if (!tpacpi_led_names[led])
5200 return 0;
5202 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5203 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5204 if (led_supported == TPACPI_LED_570)
5205 tpacpi_leds[led].led_classdev.brightness_get =
5206 &led_sysfs_get;
5208 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5210 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5212 rc = led_classdev_register(&tpacpi_pdev->dev,
5213 &tpacpi_leds[led].led_classdev);
5214 if (rc < 0)
5215 tpacpi_leds[led].led_classdev.name = NULL;
5217 return rc;
5220 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5221 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5222 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5223 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5225 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5226 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5227 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5228 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5229 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5230 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5231 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5232 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5234 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5235 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5236 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5237 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5238 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5240 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5241 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5242 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5243 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5245 /* (1) - may have excess leds enabled on MSB */
5247 /* Defaults (order matters, keep last, don't reorder!) */
5248 { /* Lenovo */
5249 .vendor = PCI_VENDOR_ID_LENOVO,
5250 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5251 .quirks = 0x1fffU,
5253 { /* IBM ThinkPads with no EC version string */
5254 .vendor = PCI_VENDOR_ID_IBM,
5255 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5256 .quirks = 0x00ffU,
5258 { /* IBM ThinkPads with EC version string */
5259 .vendor = PCI_VENDOR_ID_IBM,
5260 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5261 .quirks = 0x00bfU,
5265 #undef TPACPI_LEDQ_IBM
5266 #undef TPACPI_LEDQ_LNV
5268 static int __init led_init(struct ibm_init_struct *iibm)
5270 unsigned int i;
5271 int rc;
5272 unsigned long useful_leds;
5274 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5276 TPACPI_ACPIHANDLE_INIT(led);
5278 if (!led_handle)
5279 /* led not supported on R30, R31 */
5280 led_supported = TPACPI_LED_NONE;
5281 else if (strlencmp(led_path, "SLED") == 0)
5282 /* 570 */
5283 led_supported = TPACPI_LED_570;
5284 else if (strlencmp(led_path, "SYSL") == 0)
5285 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5286 led_supported = TPACPI_LED_OLD;
5287 else
5288 /* all others */
5289 led_supported = TPACPI_LED_NEW;
5291 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5292 str_supported(led_supported), led_supported);
5294 if (led_supported == TPACPI_LED_NONE)
5295 return 1;
5297 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5298 GFP_KERNEL);
5299 if (!tpacpi_leds) {
5300 printk(TPACPI_ERR "Out of memory for LED data\n");
5301 return -ENOMEM;
5304 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5305 ARRAY_SIZE(led_useful_qtable));
5307 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5308 if (!tpacpi_is_led_restricted(i) &&
5309 test_bit(i, &useful_leds)) {
5310 rc = tpacpi_init_led(i);
5311 if (rc < 0) {
5312 led_exit();
5313 return rc;
5318 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5319 printk(TPACPI_NOTICE
5320 "warning: userspace override of important "
5321 "firmware LEDs is enabled\n");
5322 #endif
5323 return 0;
5326 #define str_led_status(s) \
5327 ((s) == TPACPI_LED_OFF ? "off" : \
5328 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5330 static int led_read(struct seq_file *m)
5332 if (!led_supported) {
5333 seq_printf(m, "status:\t\tnot supported\n");
5334 return 0;
5336 seq_printf(m, "status:\t\tsupported\n");
5338 if (led_supported == TPACPI_LED_570) {
5339 /* 570 */
5340 int i, status;
5341 for (i = 0; i < 8; i++) {
5342 status = led_get_status(i);
5343 if (status < 0)
5344 return -EIO;
5345 seq_printf(m, "%d:\t\t%s\n",
5346 i, str_led_status(status));
5350 seq_printf(m, "commands:\t"
5351 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5353 return 0;
5356 static int led_write(char *buf)
5358 char *cmd;
5359 int led, rc;
5360 enum led_status_t s;
5362 if (!led_supported)
5363 return -ENODEV;
5365 while ((cmd = next_cmd(&buf))) {
5366 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5367 return -EINVAL;
5369 if (strstr(cmd, "off")) {
5370 s = TPACPI_LED_OFF;
5371 } else if (strstr(cmd, "on")) {
5372 s = TPACPI_LED_ON;
5373 } else if (strstr(cmd, "blink")) {
5374 s = TPACPI_LED_BLINK;
5375 } else {
5376 return -EINVAL;
5379 rc = led_set_status(led, s);
5380 if (rc < 0)
5381 return rc;
5384 return 0;
5387 static struct ibm_struct led_driver_data = {
5388 .name = "led",
5389 .read = led_read,
5390 .write = led_write,
5391 .exit = led_exit,
5394 /*************************************************************************
5395 * Beep subdriver
5398 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5400 #define TPACPI_BEEP_Q1 0x0001
5402 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5403 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5404 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5407 static int __init beep_init(struct ibm_init_struct *iibm)
5409 unsigned long quirks;
5411 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5413 TPACPI_ACPIHANDLE_INIT(beep);
5415 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5416 str_supported(beep_handle != NULL));
5418 quirks = tpacpi_check_quirks(beep_quirk_table,
5419 ARRAY_SIZE(beep_quirk_table));
5421 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5423 return (beep_handle)? 0 : 1;
5426 static int beep_read(struct seq_file *m)
5428 if (!beep_handle)
5429 seq_printf(m, "status:\t\tnot supported\n");
5430 else {
5431 seq_printf(m, "status:\t\tsupported\n");
5432 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5435 return 0;
5438 static int beep_write(char *buf)
5440 char *cmd;
5441 int beep_cmd;
5443 if (!beep_handle)
5444 return -ENODEV;
5446 while ((cmd = next_cmd(&buf))) {
5447 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5448 beep_cmd >= 0 && beep_cmd <= 17) {
5449 /* beep_cmd set */
5450 } else
5451 return -EINVAL;
5452 if (tp_features.beep_needs_two_args) {
5453 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5454 beep_cmd, 0))
5455 return -EIO;
5456 } else {
5457 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5458 beep_cmd))
5459 return -EIO;
5463 return 0;
5466 static struct ibm_struct beep_driver_data = {
5467 .name = "beep",
5468 .read = beep_read,
5469 .write = beep_write,
5472 /*************************************************************************
5473 * Thermal subdriver
5476 enum thermal_access_mode {
5477 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5478 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5479 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5480 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5481 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5484 enum { /* TPACPI_THERMAL_TPEC_* */
5485 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5486 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5487 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5489 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5493 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5494 struct ibm_thermal_sensors_struct {
5495 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5498 static enum thermal_access_mode thermal_read_mode;
5500 /* idx is zero-based */
5501 static int thermal_get_sensor(int idx, s32 *value)
5503 int t;
5504 s8 tmp;
5505 char tmpi[5];
5507 t = TP_EC_THERMAL_TMP0;
5509 switch (thermal_read_mode) {
5510 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5511 case TPACPI_THERMAL_TPEC_16:
5512 if (idx >= 8 && idx <= 15) {
5513 t = TP_EC_THERMAL_TMP8;
5514 idx -= 8;
5516 /* fallthrough */
5517 #endif
5518 case TPACPI_THERMAL_TPEC_8:
5519 if (idx <= 7) {
5520 if (!acpi_ec_read(t + idx, &tmp))
5521 return -EIO;
5522 *value = tmp * 1000;
5523 return 0;
5525 break;
5527 case TPACPI_THERMAL_ACPI_UPDT:
5528 if (idx <= 7) {
5529 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5530 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5531 return -EIO;
5532 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5533 return -EIO;
5534 *value = (t - 2732) * 100;
5535 return 0;
5537 break;
5539 case TPACPI_THERMAL_ACPI_TMP07:
5540 if (idx <= 7) {
5541 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5542 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5543 return -EIO;
5544 if (t > 127 || t < -127)
5545 t = TP_EC_THERMAL_TMP_NA;
5546 *value = t * 1000;
5547 return 0;
5549 break;
5551 case TPACPI_THERMAL_NONE:
5552 default:
5553 return -ENOSYS;
5556 return -EINVAL;
5559 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5561 int res, i;
5562 int n;
5564 n = 8;
5565 i = 0;
5567 if (!s)
5568 return -EINVAL;
5570 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5571 n = 16;
5573 for (i = 0 ; i < n; i++) {
5574 res = thermal_get_sensor(i, &s->temp[i]);
5575 if (res)
5576 return res;
5579 return n;
5582 static void thermal_dump_all_sensors(void)
5584 int n, i;
5585 struct ibm_thermal_sensors_struct t;
5587 n = thermal_get_sensors(&t);
5588 if (n <= 0)
5589 return;
5591 printk(TPACPI_NOTICE
5592 "temperatures (Celsius):");
5594 for (i = 0; i < n; i++) {
5595 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5596 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5597 else
5598 printk(KERN_CONT " N/A");
5601 printk(KERN_CONT "\n");
5604 /* sysfs temp##_input -------------------------------------------------- */
5606 static ssize_t thermal_temp_input_show(struct device *dev,
5607 struct device_attribute *attr,
5608 char *buf)
5610 struct sensor_device_attribute *sensor_attr =
5611 to_sensor_dev_attr(attr);
5612 int idx = sensor_attr->index;
5613 s32 value;
5614 int res;
5616 res = thermal_get_sensor(idx, &value);
5617 if (res)
5618 return res;
5619 if (value == TPACPI_THERMAL_SENSOR_NA)
5620 return -ENXIO;
5622 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5625 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5626 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5627 thermal_temp_input_show, NULL, _idxB)
5629 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5630 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5631 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5632 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5633 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5634 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5635 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5636 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5637 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5638 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5639 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5640 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5641 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5642 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5643 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5644 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5645 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5648 #define THERMAL_ATTRS(X) \
5649 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5651 static struct attribute *thermal_temp_input_attr[] = {
5652 THERMAL_ATTRS(8),
5653 THERMAL_ATTRS(9),
5654 THERMAL_ATTRS(10),
5655 THERMAL_ATTRS(11),
5656 THERMAL_ATTRS(12),
5657 THERMAL_ATTRS(13),
5658 THERMAL_ATTRS(14),
5659 THERMAL_ATTRS(15),
5660 THERMAL_ATTRS(0),
5661 THERMAL_ATTRS(1),
5662 THERMAL_ATTRS(2),
5663 THERMAL_ATTRS(3),
5664 THERMAL_ATTRS(4),
5665 THERMAL_ATTRS(5),
5666 THERMAL_ATTRS(6),
5667 THERMAL_ATTRS(7),
5668 NULL
5671 static const struct attribute_group thermal_temp_input16_group = {
5672 .attrs = thermal_temp_input_attr
5675 static const struct attribute_group thermal_temp_input8_group = {
5676 .attrs = &thermal_temp_input_attr[8]
5679 #undef THERMAL_SENSOR_ATTR_TEMP
5680 #undef THERMAL_ATTRS
5682 /* --------------------------------------------------------------------- */
5684 static int __init thermal_init(struct ibm_init_struct *iibm)
5686 u8 t, ta1, ta2;
5687 int i;
5688 int acpi_tmp7;
5689 int res;
5691 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5693 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5695 if (thinkpad_id.ec_model) {
5697 * Direct EC access mode: sensors at registers
5698 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5699 * non-implemented, thermal sensors return 0x80 when
5700 * not available
5703 ta1 = ta2 = 0;
5704 for (i = 0; i < 8; i++) {
5705 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5706 ta1 |= t;
5707 } else {
5708 ta1 = 0;
5709 break;
5711 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5712 ta2 |= t;
5713 } else {
5714 ta1 = 0;
5715 break;
5718 if (ta1 == 0) {
5719 /* This is sheer paranoia, but we handle it anyway */
5720 if (acpi_tmp7) {
5721 printk(TPACPI_ERR
5722 "ThinkPad ACPI EC access misbehaving, "
5723 "falling back to ACPI TMPx access "
5724 "mode\n");
5725 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5726 } else {
5727 printk(TPACPI_ERR
5728 "ThinkPad ACPI EC access misbehaving, "
5729 "disabling thermal sensors access\n");
5730 thermal_read_mode = TPACPI_THERMAL_NONE;
5732 } else {
5733 thermal_read_mode =
5734 (ta2 != 0) ?
5735 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5737 } else if (acpi_tmp7) {
5738 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5739 /* 600e/x, 770e, 770x */
5740 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5741 } else {
5742 /* Standard ACPI TMPx access, max 8 sensors */
5743 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5745 } else {
5746 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5747 thermal_read_mode = TPACPI_THERMAL_NONE;
5750 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5751 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5752 thermal_read_mode);
5754 switch (thermal_read_mode) {
5755 case TPACPI_THERMAL_TPEC_16:
5756 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5757 &thermal_temp_input16_group);
5758 if (res)
5759 return res;
5760 break;
5761 case TPACPI_THERMAL_TPEC_8:
5762 case TPACPI_THERMAL_ACPI_TMP07:
5763 case TPACPI_THERMAL_ACPI_UPDT:
5764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5765 &thermal_temp_input8_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_NONE:
5770 default:
5771 return 1;
5774 return 0;
5777 static void thermal_exit(void)
5779 switch (thermal_read_mode) {
5780 case TPACPI_THERMAL_TPEC_16:
5781 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5782 &thermal_temp_input16_group);
5783 break;
5784 case TPACPI_THERMAL_TPEC_8:
5785 case TPACPI_THERMAL_ACPI_TMP07:
5786 case TPACPI_THERMAL_ACPI_UPDT:
5787 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5788 &thermal_temp_input8_group);
5789 break;
5790 case TPACPI_THERMAL_NONE:
5791 default:
5792 break;
5796 static int thermal_read(struct seq_file *m)
5798 int n, i;
5799 struct ibm_thermal_sensors_struct t;
5801 n = thermal_get_sensors(&t);
5802 if (unlikely(n < 0))
5803 return n;
5805 seq_printf(m, "temperatures:\t");
5807 if (n > 0) {
5808 for (i = 0; i < (n - 1); i++)
5809 seq_printf(m, "%d ", t.temp[i] / 1000);
5810 seq_printf(m, "%d\n", t.temp[i] / 1000);
5811 } else
5812 seq_printf(m, "not supported\n");
5814 return 0;
5817 static struct ibm_struct thermal_driver_data = {
5818 .name = "thermal",
5819 .read = thermal_read,
5820 .exit = thermal_exit,
5823 /*************************************************************************
5824 * EC Dump subdriver
5827 static u8 ecdump_regs[256];
5829 static int ecdump_read(struct seq_file *m)
5831 int i, j;
5832 u8 v;
5834 seq_printf(m, "EC "
5835 " +00 +01 +02 +03 +04 +05 +06 +07"
5836 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5837 for (i = 0; i < 256; i += 16) {
5838 seq_printf(m, "EC 0x%02x:", i);
5839 for (j = 0; j < 16; j++) {
5840 if (!acpi_ec_read(i + j, &v))
5841 break;
5842 if (v != ecdump_regs[i + j])
5843 seq_printf(m, " *%02x", v);
5844 else
5845 seq_printf(m, " %02x", v);
5846 ecdump_regs[i + j] = v;
5848 seq_putc(m, '\n');
5849 if (j != 16)
5850 break;
5853 /* These are way too dangerous to advertise openly... */
5854 #if 0
5855 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5856 " (<offset> is 00-ff, <value> is 00-ff)\n");
5857 seq_printf(m, "commands:\t0x<offset> <value> "
5858 " (<offset> is 00-ff, <value> is 0-255)\n");
5859 #endif
5860 return 0;
5863 static int ecdump_write(char *buf)
5865 char *cmd;
5866 int i, v;
5868 while ((cmd = next_cmd(&buf))) {
5869 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5870 /* i and v set */
5871 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5872 /* i and v set */
5873 } else
5874 return -EINVAL;
5875 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5876 if (!acpi_ec_write(i, v))
5877 return -EIO;
5878 } else
5879 return -EINVAL;
5882 return 0;
5885 static struct ibm_struct ecdump_driver_data = {
5886 .name = "ecdump",
5887 .read = ecdump_read,
5888 .write = ecdump_write,
5889 .flags.experimental = 1,
5892 /*************************************************************************
5893 * Backlight/brightness subdriver
5896 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5899 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5900 * CMOS NVRAM byte 0x5E, bits 0-3.
5902 * EC HBRV (0x31) has the following layout
5903 * Bit 7: unknown function
5904 * Bit 6: unknown function
5905 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5906 * Bit 4: must be set to zero to avoid problems
5907 * Bit 3-0: backlight brightness level
5909 * brightness_get_raw returns status data in the HBRV layout
5911 * WARNING: The X61 has been verified to use HBRV for something else, so
5912 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5913 * testing on the very early *60 Lenovo models...
5916 enum {
5917 TP_EC_BACKLIGHT = 0x31,
5919 /* TP_EC_BACKLIGHT bitmasks */
5920 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5921 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5922 TP_EC_BACKLIGHT_MAPSW = 0x20,
5925 enum tpacpi_brightness_access_mode {
5926 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5927 TPACPI_BRGHT_MODE_EC, /* EC control */
5928 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5929 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5930 TPACPI_BRGHT_MODE_MAX
5933 static struct backlight_device *ibm_backlight_device;
5935 static enum tpacpi_brightness_access_mode brightness_mode =
5936 TPACPI_BRGHT_MODE_MAX;
5938 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5940 static struct mutex brightness_mutex;
5942 /* NVRAM brightness access,
5943 * call with brightness_mutex held! */
5944 static unsigned int tpacpi_brightness_nvram_get(void)
5946 u8 lnvram;
5948 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5949 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5950 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5951 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5953 return lnvram;
5956 static void tpacpi_brightness_checkpoint_nvram(void)
5958 u8 lec = 0;
5959 u8 b_nvram;
5961 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5962 return;
5964 vdbg_printk(TPACPI_DBG_BRGHT,
5965 "trying to checkpoint backlight level to NVRAM...\n");
5967 if (mutex_lock_killable(&brightness_mutex) < 0)
5968 return;
5970 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5971 goto unlock;
5972 lec &= TP_EC_BACKLIGHT_LVLMSK;
5973 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5975 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5976 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5977 /* NVRAM needs update */
5978 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5979 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5980 b_nvram |= lec;
5981 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5982 dbg_printk(TPACPI_DBG_BRGHT,
5983 "updated NVRAM backlight level to %u (0x%02x)\n",
5984 (unsigned int) lec, (unsigned int) b_nvram);
5985 } else
5986 vdbg_printk(TPACPI_DBG_BRGHT,
5987 "NVRAM backlight level already is %u (0x%02x)\n",
5988 (unsigned int) lec, (unsigned int) b_nvram);
5990 unlock:
5991 mutex_unlock(&brightness_mutex);
5995 /* call with brightness_mutex held! */
5996 static int tpacpi_brightness_get_raw(int *status)
5998 u8 lec = 0;
6000 switch (brightness_mode) {
6001 case TPACPI_BRGHT_MODE_UCMS_STEP:
6002 *status = tpacpi_brightness_nvram_get();
6003 return 0;
6004 case TPACPI_BRGHT_MODE_EC:
6005 case TPACPI_BRGHT_MODE_ECNVRAM:
6006 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6007 return -EIO;
6008 *status = lec;
6009 return 0;
6010 default:
6011 return -ENXIO;
6015 /* call with brightness_mutex held! */
6016 /* do NOT call with illegal backlight level value */
6017 static int tpacpi_brightness_set_ec(unsigned int value)
6019 u8 lec = 0;
6021 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6022 return -EIO;
6024 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6025 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6026 (value & TP_EC_BACKLIGHT_LVLMSK))))
6027 return -EIO;
6029 return 0;
6032 /* call with brightness_mutex held! */
6033 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6035 int cmos_cmd, inc;
6036 unsigned int current_value, i;
6038 current_value = tpacpi_brightness_nvram_get();
6040 if (value == current_value)
6041 return 0;
6043 cmos_cmd = (value > current_value) ?
6044 TP_CMOS_BRIGHTNESS_UP :
6045 TP_CMOS_BRIGHTNESS_DOWN;
6046 inc = (value > current_value) ? 1 : -1;
6048 for (i = current_value; i != value; i += inc)
6049 if (issue_thinkpad_cmos_command(cmos_cmd))
6050 return -EIO;
6052 return 0;
6055 /* May return EINTR which can always be mapped to ERESTARTSYS */
6056 static int brightness_set(unsigned int value)
6058 int res;
6060 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6061 value < 0)
6062 return -EINVAL;
6064 vdbg_printk(TPACPI_DBG_BRGHT,
6065 "set backlight level to %d\n", value);
6067 res = mutex_lock_killable(&brightness_mutex);
6068 if (res < 0)
6069 return res;
6071 switch (brightness_mode) {
6072 case TPACPI_BRGHT_MODE_EC:
6073 case TPACPI_BRGHT_MODE_ECNVRAM:
6074 res = tpacpi_brightness_set_ec(value);
6075 break;
6076 case TPACPI_BRGHT_MODE_UCMS_STEP:
6077 res = tpacpi_brightness_set_ucmsstep(value);
6078 break;
6079 default:
6080 res = -ENXIO;
6083 mutex_unlock(&brightness_mutex);
6084 return res;
6087 /* sysfs backlight class ----------------------------------------------- */
6089 static int brightness_update_status(struct backlight_device *bd)
6091 unsigned int level =
6092 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6093 bd->props.power == FB_BLANK_UNBLANK) ?
6094 bd->props.brightness : 0;
6096 dbg_printk(TPACPI_DBG_BRGHT,
6097 "backlight: attempt to set level to %d\n",
6098 level);
6100 /* it is the backlight class's job (caller) to handle
6101 * EINTR and other errors properly */
6102 return brightness_set(level);
6105 static int brightness_get(struct backlight_device *bd)
6107 int status, res;
6109 res = mutex_lock_killable(&brightness_mutex);
6110 if (res < 0)
6111 return 0;
6113 res = tpacpi_brightness_get_raw(&status);
6115 mutex_unlock(&brightness_mutex);
6117 if (res < 0)
6118 return 0;
6120 return status & TP_EC_BACKLIGHT_LVLMSK;
6123 static void tpacpi_brightness_notify_change(void)
6125 backlight_force_update(ibm_backlight_device,
6126 BACKLIGHT_UPDATE_HOTKEY);
6129 static struct backlight_ops ibm_backlight_data = {
6130 .get_brightness = brightness_get,
6131 .update_status = brightness_update_status,
6134 /* --------------------------------------------------------------------- */
6137 * These are only useful for models that have only one possibility
6138 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6139 * these quirks.
6141 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6142 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6143 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6145 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6146 /* Models with ATI GPUs known to require ECNVRAM mode */
6147 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6149 /* Models with ATI GPUs that can use ECNVRAM */
6150 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6151 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6152 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6153 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6155 /* Models with Intel Extreme Graphics 2 */
6156 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6157 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6158 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6160 /* Models with Intel GMA900 */
6161 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6162 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6163 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6166 static int __init brightness_init(struct ibm_init_struct *iibm)
6168 int b;
6169 unsigned long quirks;
6171 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6173 mutex_init(&brightness_mutex);
6175 quirks = tpacpi_check_quirks(brightness_quirk_table,
6176 ARRAY_SIZE(brightness_quirk_table));
6179 * We always attempt to detect acpi support, so as to switch
6180 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6181 * going to publish a backlight interface
6183 b = tpacpi_check_std_acpi_brightness_support();
6184 if (b > 0) {
6186 if (acpi_video_backlight_support()) {
6187 if (brightness_enable > 1) {
6188 printk(TPACPI_NOTICE
6189 "Standard ACPI backlight interface "
6190 "available, not loading native one.\n");
6191 return 1;
6192 } else if (brightness_enable == 1) {
6193 printk(TPACPI_NOTICE
6194 "Backlight control force enabled, even if standard "
6195 "ACPI backlight interface is available\n");
6197 } else {
6198 if (brightness_enable > 1) {
6199 printk(TPACPI_NOTICE
6200 "Standard ACPI backlight interface not "
6201 "available, thinkpad_acpi native "
6202 "brightness control enabled\n");
6207 if (!brightness_enable) {
6208 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6209 "brightness support disabled by "
6210 "module parameter\n");
6211 return 1;
6214 if (b > 16) {
6215 printk(TPACPI_ERR
6216 "Unsupported brightness interface, "
6217 "please contact %s\n", TPACPI_MAIL);
6218 return 1;
6220 if (b == 16)
6221 tp_features.bright_16levels = 1;
6224 * Check for module parameter bogosity, note that we
6225 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6226 * able to detect "unspecified"
6228 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6229 return -EINVAL;
6231 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6232 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6233 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6234 if (quirks & TPACPI_BRGHT_Q_EC)
6235 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6236 else
6237 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6239 dbg_printk(TPACPI_DBG_BRGHT,
6240 "driver auto-selected brightness_mode=%d\n",
6241 brightness_mode);
6244 /* Safety */
6245 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6246 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6247 brightness_mode == TPACPI_BRGHT_MODE_EC))
6248 return -EINVAL;
6250 if (tpacpi_brightness_get_raw(&b) < 0)
6251 return 1;
6253 if (tp_features.bright_16levels)
6254 printk(TPACPI_INFO
6255 "detected a 16-level brightness capable ThinkPad\n");
6257 ibm_backlight_device = backlight_device_register(
6258 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6259 &ibm_backlight_data);
6260 if (IS_ERR(ibm_backlight_device)) {
6261 int rc = PTR_ERR(ibm_backlight_device);
6262 ibm_backlight_device = NULL;
6263 printk(TPACPI_ERR "Could not register backlight device\n");
6264 return rc;
6266 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6267 "brightness is supported\n");
6269 if (quirks & TPACPI_BRGHT_Q_ASK) {
6270 printk(TPACPI_NOTICE
6271 "brightness: will use unverified default: "
6272 "brightness_mode=%d\n", brightness_mode);
6273 printk(TPACPI_NOTICE
6274 "brightness: please report to %s whether it works well "
6275 "or not on your ThinkPad\n", TPACPI_MAIL);
6278 ibm_backlight_device->props.max_brightness =
6279 (tp_features.bright_16levels)? 15 : 7;
6280 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6281 backlight_update_status(ibm_backlight_device);
6283 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6284 "brightness: registering brightness hotkeys "
6285 "as change notification\n");
6286 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6287 | TP_ACPI_HKEY_BRGHTUP_MASK
6288 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6289 return 0;
6292 static void brightness_suspend(pm_message_t state)
6294 tpacpi_brightness_checkpoint_nvram();
6297 static void brightness_shutdown(void)
6299 tpacpi_brightness_checkpoint_nvram();
6302 static void brightness_exit(void)
6304 if (ibm_backlight_device) {
6305 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6306 "calling backlight_device_unregister()\n");
6307 backlight_device_unregister(ibm_backlight_device);
6310 tpacpi_brightness_checkpoint_nvram();
6313 static int brightness_read(struct seq_file *m)
6315 int level;
6317 level = brightness_get(NULL);
6318 if (level < 0) {
6319 seq_printf(m, "level:\t\tunreadable\n");
6320 } else {
6321 seq_printf(m, "level:\t\t%d\n", level);
6322 seq_printf(m, "commands:\tup, down\n");
6323 seq_printf(m, "commands:\tlevel <level>"
6324 " (<level> is 0-%d)\n",
6325 (tp_features.bright_16levels) ? 15 : 7);
6328 return 0;
6331 static int brightness_write(char *buf)
6333 int level;
6334 int rc;
6335 char *cmd;
6336 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6338 level = brightness_get(NULL);
6339 if (level < 0)
6340 return level;
6342 while ((cmd = next_cmd(&buf))) {
6343 if (strlencmp(cmd, "up") == 0) {
6344 if (level < max_level)
6345 level++;
6346 } else if (strlencmp(cmd, "down") == 0) {
6347 if (level > 0)
6348 level--;
6349 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6350 level >= 0 && level <= max_level) {
6351 /* new level set */
6352 } else
6353 return -EINVAL;
6356 tpacpi_disclose_usertask("procfs brightness",
6357 "set level to %d\n", level);
6360 * Now we know what the final level should be, so we try to set it.
6361 * Doing it this way makes the syscall restartable in case of EINTR
6363 rc = brightness_set(level);
6364 if (!rc && ibm_backlight_device)
6365 backlight_force_update(ibm_backlight_device,
6366 BACKLIGHT_UPDATE_SYSFS);
6367 return (rc == -EINTR)? -ERESTARTSYS : rc;
6370 static struct ibm_struct brightness_driver_data = {
6371 .name = "brightness",
6372 .read = brightness_read,
6373 .write = brightness_write,
6374 .exit = brightness_exit,
6375 .suspend = brightness_suspend,
6376 .shutdown = brightness_shutdown,
6379 /*************************************************************************
6380 * Volume subdriver
6384 * IBM ThinkPads have a simple volume controller with MUTE gating.
6385 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6387 * Since the *61 series (and probably also the later *60 series), Lenovo
6388 * ThinkPads only implement the MUTE gate.
6390 * EC register 0x30
6391 * Bit 6: MUTE (1 mutes sound)
6392 * Bit 3-0: Volume
6393 * Other bits should be zero as far as we know.
6395 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6396 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6397 * such as bit 7 which is used to detect repeated presses of MUTE,
6398 * and we leave them unchanged.
6401 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6403 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6404 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6405 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6407 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6408 static char *alsa_id = "ThinkPadEC";
6409 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6411 struct tpacpi_alsa_data {
6412 struct snd_card *card;
6413 struct snd_ctl_elem_id *ctl_mute_id;
6414 struct snd_ctl_elem_id *ctl_vol_id;
6417 static struct snd_card *alsa_card;
6419 enum {
6420 TP_EC_AUDIO = 0x30,
6422 /* TP_EC_AUDIO bits */
6423 TP_EC_AUDIO_MUTESW = 6,
6425 /* TP_EC_AUDIO bitmasks */
6426 TP_EC_AUDIO_LVL_MSK = 0x0F,
6427 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6429 /* Maximum volume */
6430 TP_EC_VOLUME_MAX = 14,
6433 enum tpacpi_volume_access_mode {
6434 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6435 TPACPI_VOL_MODE_EC, /* Pure EC control */
6436 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6437 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6438 TPACPI_VOL_MODE_MAX
6441 enum tpacpi_volume_capabilities {
6442 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6443 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6444 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6445 TPACPI_VOL_CAP_MAX
6448 static enum tpacpi_volume_access_mode volume_mode =
6449 TPACPI_VOL_MODE_MAX;
6451 static enum tpacpi_volume_capabilities volume_capabilities;
6452 static int volume_control_allowed;
6455 * Used to syncronize writers to TP_EC_AUDIO and
6456 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6458 static struct mutex volume_mutex;
6460 static void tpacpi_volume_checkpoint_nvram(void)
6462 u8 lec = 0;
6463 u8 b_nvram;
6464 u8 ec_mask;
6466 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6467 return;
6468 if (!volume_control_allowed)
6469 return;
6471 vdbg_printk(TPACPI_DBG_MIXER,
6472 "trying to checkpoint mixer state to NVRAM...\n");
6474 if (tp_features.mixer_no_level_control)
6475 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6476 else
6477 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6479 if (mutex_lock_killable(&volume_mutex) < 0)
6480 return;
6482 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6483 goto unlock;
6484 lec &= ec_mask;
6485 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6487 if (lec != (b_nvram & ec_mask)) {
6488 /* NVRAM needs update */
6489 b_nvram &= ~ec_mask;
6490 b_nvram |= lec;
6491 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6492 dbg_printk(TPACPI_DBG_MIXER,
6493 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6494 (unsigned int) lec, (unsigned int) b_nvram);
6495 } else {
6496 vdbg_printk(TPACPI_DBG_MIXER,
6497 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6498 (unsigned int) lec, (unsigned int) b_nvram);
6501 unlock:
6502 mutex_unlock(&volume_mutex);
6505 static int volume_get_status_ec(u8 *status)
6507 u8 s;
6509 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6510 return -EIO;
6512 *status = s;
6514 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6516 return 0;
6519 static int volume_get_status(u8 *status)
6521 return volume_get_status_ec(status);
6524 static int volume_set_status_ec(const u8 status)
6526 if (!acpi_ec_write(TP_EC_AUDIO, status))
6527 return -EIO;
6529 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6531 return 0;
6534 static int volume_set_status(const u8 status)
6536 return volume_set_status_ec(status);
6539 static int volume_set_mute_ec(const bool mute)
6541 int rc;
6542 u8 s, n;
6544 if (mutex_lock_killable(&volume_mutex) < 0)
6545 return -EINTR;
6547 rc = volume_get_status_ec(&s);
6548 if (rc)
6549 goto unlock;
6551 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6552 s & ~TP_EC_AUDIO_MUTESW_MSK;
6554 if (n != s)
6555 rc = volume_set_status_ec(n);
6557 unlock:
6558 mutex_unlock(&volume_mutex);
6559 return rc;
6562 static int volume_set_mute(const bool mute)
6564 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6565 (mute) ? "" : "un");
6566 return volume_set_mute_ec(mute);
6569 static int volume_set_volume_ec(const u8 vol)
6571 int rc;
6572 u8 s, n;
6574 if (vol > TP_EC_VOLUME_MAX)
6575 return -EINVAL;
6577 if (mutex_lock_killable(&volume_mutex) < 0)
6578 return -EINTR;
6580 rc = volume_get_status_ec(&s);
6581 if (rc)
6582 goto unlock;
6584 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6586 if (n != s)
6587 rc = volume_set_status_ec(n);
6589 unlock:
6590 mutex_unlock(&volume_mutex);
6591 return rc;
6594 static int volume_set_volume(const u8 vol)
6596 dbg_printk(TPACPI_DBG_MIXER,
6597 "trying to set volume level to %hu\n", vol);
6598 return volume_set_volume_ec(vol);
6601 static void volume_alsa_notify_change(void)
6603 struct tpacpi_alsa_data *d;
6605 if (alsa_card && alsa_card->private_data) {
6606 d = alsa_card->private_data;
6607 if (d->ctl_mute_id)
6608 snd_ctl_notify(alsa_card,
6609 SNDRV_CTL_EVENT_MASK_VALUE,
6610 d->ctl_mute_id);
6611 if (d->ctl_vol_id)
6612 snd_ctl_notify(alsa_card,
6613 SNDRV_CTL_EVENT_MASK_VALUE,
6614 d->ctl_vol_id);
6618 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6619 struct snd_ctl_elem_info *uinfo)
6621 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6622 uinfo->count = 1;
6623 uinfo->value.integer.min = 0;
6624 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6625 return 0;
6628 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6629 struct snd_ctl_elem_value *ucontrol)
6631 u8 s;
6632 int rc;
6634 rc = volume_get_status(&s);
6635 if (rc < 0)
6636 return rc;
6638 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6639 return 0;
6642 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6643 struct snd_ctl_elem_value *ucontrol)
6645 return volume_set_volume(ucontrol->value.integer.value[0]);
6648 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6650 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6651 struct snd_ctl_elem_value *ucontrol)
6653 u8 s;
6654 int rc;
6656 rc = volume_get_status(&s);
6657 if (rc < 0)
6658 return rc;
6660 ucontrol->value.integer.value[0] =
6661 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6662 return 0;
6665 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6666 struct snd_ctl_elem_value *ucontrol)
6668 return volume_set_mute(!ucontrol->value.integer.value[0]);
6671 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6672 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6673 .name = "Console Playback Volume",
6674 .index = 0,
6675 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6676 .info = volume_alsa_vol_info,
6677 .get = volume_alsa_vol_get,
6680 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6681 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6682 .name = "Console Playback Switch",
6683 .index = 0,
6684 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6685 .info = volume_alsa_mute_info,
6686 .get = volume_alsa_mute_get,
6689 static void volume_suspend(pm_message_t state)
6691 tpacpi_volume_checkpoint_nvram();
6694 static void volume_resume(void)
6696 volume_alsa_notify_change();
6699 static void volume_shutdown(void)
6701 tpacpi_volume_checkpoint_nvram();
6704 static void volume_exit(void)
6706 if (alsa_card) {
6707 snd_card_free(alsa_card);
6708 alsa_card = NULL;
6711 tpacpi_volume_checkpoint_nvram();
6714 static int __init volume_create_alsa_mixer(void)
6716 struct snd_card *card;
6717 struct tpacpi_alsa_data *data;
6718 struct snd_kcontrol *ctl_vol;
6719 struct snd_kcontrol *ctl_mute;
6720 int rc;
6722 card = snd_card_new(alsa_index, alsa_id, THIS_MODULE,
6723 sizeof(struct tpacpi_alsa_data));
6724 if (!card) {
6725 printk(TPACPI_ERR
6726 "Failed to create ALSA card structures\n");
6727 return 1;
6730 BUG_ON(!card->private_data);
6731 data = card->private_data;
6732 data->card = card;
6734 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6735 sizeof(card->driver));
6736 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6737 sizeof(card->shortname));
6738 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6739 (thinkpad_id.ec_version_str) ?
6740 thinkpad_id.ec_version_str : "(unknown)");
6741 snprintf(card->longname, sizeof(card->longname),
6742 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6743 (thinkpad_id.ec_version_str) ?
6744 thinkpad_id.ec_version_str : "unknown");
6746 if (volume_control_allowed) {
6747 volume_alsa_control_vol.put = volume_alsa_vol_put;
6748 volume_alsa_control_vol.access =
6749 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6751 volume_alsa_control_mute.put = volume_alsa_mute_put;
6752 volume_alsa_control_mute.access =
6753 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6756 if (!tp_features.mixer_no_level_control) {
6757 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6758 rc = snd_ctl_add(card, ctl_vol);
6759 if (rc < 0) {
6760 printk(TPACPI_ERR
6761 "Failed to create ALSA volume control: %d\n",
6762 rc);
6763 goto err_exit;
6765 data->ctl_vol_id = &ctl_vol->id;
6768 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6769 rc = snd_ctl_add(card, ctl_mute);
6770 if (rc < 0) {
6771 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6772 rc);
6773 goto err_exit;
6775 data->ctl_mute_id = &ctl_mute->id;
6777 snd_card_set_dev(card, &tpacpi_pdev->dev);
6778 rc = snd_card_register(card);
6779 if (rc < 0) {
6780 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6781 goto err_exit;
6784 alsa_card = card;
6785 return 0;
6787 err_exit:
6788 snd_card_free(card);
6789 return 1;
6792 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6793 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6795 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6796 /* Whitelist volume level on all IBM by default */
6797 { .vendor = PCI_VENDOR_ID_IBM,
6798 .bios = TPACPI_MATCH_ANY,
6799 .ec = TPACPI_MATCH_ANY,
6800 .quirks = TPACPI_VOL_Q_LEVEL },
6802 /* Lenovo models with volume control (needs confirmation) */
6803 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6804 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6805 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6806 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6807 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6808 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6809 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6811 /* Whitelist mute-only on all Lenovo by default */
6812 { .vendor = PCI_VENDOR_ID_LENOVO,
6813 .bios = TPACPI_MATCH_ANY,
6814 .ec = TPACPI_MATCH_ANY,
6815 .quirks = TPACPI_VOL_Q_MUTEONLY }
6818 static int __init volume_init(struct ibm_init_struct *iibm)
6820 unsigned long quirks;
6821 int rc;
6823 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6825 mutex_init(&volume_mutex);
6828 * Check for module parameter bogosity, note that we
6829 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6830 * able to detect "unspecified"
6832 if (volume_mode > TPACPI_VOL_MODE_MAX)
6833 return -EINVAL;
6835 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6836 printk(TPACPI_ERR
6837 "UCMS step volume mode not implemented, "
6838 "please contact %s\n", TPACPI_MAIL);
6839 return 1;
6842 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6843 return -EINVAL;
6846 * The ALSA mixer is our primary interface.
6847 * When disabled, don't install the subdriver at all
6849 if (!alsa_enable) {
6850 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6851 "ALSA mixer disabled by parameter, "
6852 "not loading volume subdriver...\n");
6853 return 1;
6856 quirks = tpacpi_check_quirks(volume_quirk_table,
6857 ARRAY_SIZE(volume_quirk_table));
6859 switch (volume_capabilities) {
6860 case TPACPI_VOL_CAP_AUTO:
6861 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6862 tp_features.mixer_no_level_control = 1;
6863 else if (quirks & TPACPI_VOL_Q_LEVEL)
6864 tp_features.mixer_no_level_control = 0;
6865 else
6866 return 1; /* no mixer */
6867 break;
6868 case TPACPI_VOL_CAP_VOLMUTE:
6869 tp_features.mixer_no_level_control = 0;
6870 break;
6871 case TPACPI_VOL_CAP_MUTEONLY:
6872 tp_features.mixer_no_level_control = 1;
6873 break;
6874 default:
6875 return 1;
6878 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6879 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6880 "using user-supplied volume_capabilities=%d\n",
6881 volume_capabilities);
6883 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6884 volume_mode == TPACPI_VOL_MODE_MAX) {
6885 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6887 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6888 "driver auto-selected volume_mode=%d\n",
6889 volume_mode);
6890 } else {
6891 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6892 "using user-supplied volume_mode=%d\n",
6893 volume_mode);
6896 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6897 "mute is supported, volume control is %s\n",
6898 str_supported(!tp_features.mixer_no_level_control));
6900 rc = volume_create_alsa_mixer();
6901 if (rc) {
6902 printk(TPACPI_ERR
6903 "Could not create the ALSA mixer interface\n");
6904 return rc;
6907 printk(TPACPI_INFO
6908 "Console audio control enabled, mode: %s\n",
6909 (volume_control_allowed) ?
6910 "override (read/write)" :
6911 "monitor (read only)");
6913 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6914 "registering volume hotkeys as change notification\n");
6915 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6916 | TP_ACPI_HKEY_VOLUP_MASK
6917 | TP_ACPI_HKEY_VOLDWN_MASK
6918 | TP_ACPI_HKEY_MUTE_MASK);
6920 return 0;
6923 static int volume_read(struct seq_file *m)
6925 u8 status;
6927 if (volume_get_status(&status) < 0) {
6928 seq_printf(m, "level:\t\tunreadable\n");
6929 } else {
6930 if (tp_features.mixer_no_level_control)
6931 seq_printf(m, "level:\t\tunsupported\n");
6932 else
6933 seq_printf(m, "level:\t\t%d\n",
6934 status & TP_EC_AUDIO_LVL_MSK);
6936 seq_printf(m, "mute:\t\t%s\n",
6937 onoff(status, TP_EC_AUDIO_MUTESW));
6939 if (volume_control_allowed) {
6940 seq_printf(m, "commands:\tunmute, mute\n");
6941 if (!tp_features.mixer_no_level_control) {
6942 seq_printf(m,
6943 "commands:\tup, down\n");
6944 seq_printf(m,
6945 "commands:\tlevel <level>"
6946 " (<level> is 0-%d)\n",
6947 TP_EC_VOLUME_MAX);
6952 return 0;
6955 static int volume_write(char *buf)
6957 u8 s;
6958 u8 new_level, new_mute;
6959 int l;
6960 char *cmd;
6961 int rc;
6964 * We do allow volume control at driver startup, so that the
6965 * user can set initial state through the volume=... parameter hack.
6967 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6968 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6969 tp_warned.volume_ctrl_forbidden = 1;
6970 printk(TPACPI_NOTICE
6971 "Console audio control in monitor mode, "
6972 "changes are not allowed.\n");
6973 printk(TPACPI_NOTICE
6974 "Use the volume_control=1 module parameter "
6975 "to enable volume control\n");
6977 return -EPERM;
6980 rc = volume_get_status(&s);
6981 if (rc < 0)
6982 return rc;
6984 new_level = s & TP_EC_AUDIO_LVL_MSK;
6985 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6987 while ((cmd = next_cmd(&buf))) {
6988 if (!tp_features.mixer_no_level_control) {
6989 if (strlencmp(cmd, "up") == 0) {
6990 if (new_mute)
6991 new_mute = 0;
6992 else if (new_level < TP_EC_VOLUME_MAX)
6993 new_level++;
6994 continue;
6995 } else if (strlencmp(cmd, "down") == 0) {
6996 if (new_mute)
6997 new_mute = 0;
6998 else if (new_level > 0)
6999 new_level--;
7000 continue;
7001 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7002 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7003 new_level = l;
7004 continue;
7007 if (strlencmp(cmd, "mute") == 0)
7008 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7009 else if (strlencmp(cmd, "unmute") == 0)
7010 new_mute = 0;
7011 else
7012 return -EINVAL;
7015 if (tp_features.mixer_no_level_control) {
7016 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7017 new_mute ? "" : "un");
7018 rc = volume_set_mute(!!new_mute);
7019 } else {
7020 tpacpi_disclose_usertask("procfs volume",
7021 "%smute and set level to %d\n",
7022 new_mute ? "" : "un", new_level);
7023 rc = volume_set_status(new_mute | new_level);
7025 volume_alsa_notify_change();
7027 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7030 static struct ibm_struct volume_driver_data = {
7031 .name = "volume",
7032 .read = volume_read,
7033 .write = volume_write,
7034 .exit = volume_exit,
7035 .suspend = volume_suspend,
7036 .resume = volume_resume,
7037 .shutdown = volume_shutdown,
7040 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7042 #define alsa_card NULL
7044 static void inline volume_alsa_notify_change(void)
7048 static int __init volume_init(struct ibm_init_struct *iibm)
7050 printk(TPACPI_INFO
7051 "volume: disabled as there is no ALSA support in this kernel\n");
7053 return 1;
7056 static struct ibm_struct volume_driver_data = {
7057 .name = "volume",
7060 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7062 /*************************************************************************
7063 * Fan subdriver
7067 * FAN ACCESS MODES
7069 * TPACPI_FAN_RD_ACPI_GFAN:
7070 * ACPI GFAN method: returns fan level
7072 * see TPACPI_FAN_WR_ACPI_SFAN
7073 * EC 0x2f (HFSP) not available if GFAN exists
7075 * TPACPI_FAN_WR_ACPI_SFAN:
7076 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7078 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7079 * it for writing.
7081 * TPACPI_FAN_WR_TPEC:
7082 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7083 * Supported on almost all ThinkPads
7085 * Fan speed changes of any sort (including those caused by the
7086 * disengaged mode) are usually done slowly by the firmware as the
7087 * maximum ammount of fan duty cycle change per second seems to be
7088 * limited.
7090 * Reading is not available if GFAN exists.
7091 * Writing is not available if SFAN exists.
7093 * Bits
7094 * 7 automatic mode engaged;
7095 * (default operation mode of the ThinkPad)
7096 * fan level is ignored in this mode.
7097 * 6 full speed mode (takes precedence over bit 7);
7098 * not available on all thinkpads. May disable
7099 * the tachometer while the fan controller ramps up
7100 * the speed (which can take up to a few *minutes*).
7101 * Speeds up fan to 100% duty-cycle, which is far above
7102 * the standard RPM levels. It is not impossible that
7103 * it could cause hardware damage.
7104 * 5-3 unused in some models. Extra bits for fan level
7105 * in others, but still useless as all values above
7106 * 7 map to the same speed as level 7 in these models.
7107 * 2-0 fan level (0..7 usually)
7108 * 0x00 = stop
7109 * 0x07 = max (set when temperatures critical)
7110 * Some ThinkPads may have other levels, see
7111 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7113 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7114 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7115 * does so, its initial value is meaningless (0x07).
7117 * For firmware bugs, refer to:
7118 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7120 * ----
7122 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7123 * Main fan tachometer reading (in RPM)
7125 * This register is present on all ThinkPads with a new-style EC, and
7126 * it is known not to be present on the A21m/e, and T22, as there is
7127 * something else in offset 0x84 according to the ACPI DSDT. Other
7128 * ThinkPads from this same time period (and earlier) probably lack the
7129 * tachometer as well.
7131 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7132 * was never fixed by IBM to report the EC firmware version string
7133 * probably support the tachometer (like the early X models), so
7134 * detecting it is quite hard. We need more data to know for sure.
7136 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7137 * might result.
7139 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7140 * mode.
7142 * For firmware bugs, refer to:
7143 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7145 * ----
7147 * ThinkPad EC register 0x31 bit 0 (only on select models)
7149 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7150 * show the speed of the main fan. When bit 0 of EC register 0x31
7151 * is one, the tachometer registers show the speed of the auxiliary
7152 * fan.
7154 * Fan control seems to affect both fans, regardless of the state
7155 * of this bit.
7157 * So far, only the firmware for the X60/X61 non-tablet versions
7158 * seem to support this (firmware TP-7M).
7160 * TPACPI_FAN_WR_ACPI_FANS:
7161 * ThinkPad X31, X40, X41. Not available in the X60.
7163 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7164 * high speed. ACPI DSDT seems to map these three speeds to levels
7165 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7166 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7168 * The speeds are stored on handles
7169 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7171 * There are three default speed sets, acessible as handles:
7172 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7174 * ACPI DSDT switches which set is in use depending on various
7175 * factors.
7177 * TPACPI_FAN_WR_TPEC is also available and should be used to
7178 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7179 * but the ACPI tables just mention level 7.
7182 enum { /* Fan control constants */
7183 fan_status_offset = 0x2f, /* EC register 0x2f */
7184 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7185 * 0x84 must be read before 0x85 */
7186 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7187 bit 0 selects which fan is active */
7189 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7190 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7192 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7195 enum fan_status_access_mode {
7196 TPACPI_FAN_NONE = 0, /* No fan status or control */
7197 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7198 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7201 enum fan_control_access_mode {
7202 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7203 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7204 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7205 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7208 enum fan_control_commands {
7209 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7210 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7211 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7212 * and also watchdog cmd */
7215 static int fan_control_allowed;
7217 static enum fan_status_access_mode fan_status_access_mode;
7218 static enum fan_control_access_mode fan_control_access_mode;
7219 static enum fan_control_commands fan_control_commands;
7221 static u8 fan_control_initial_status;
7222 static u8 fan_control_desired_level;
7223 static u8 fan_control_resume_level;
7224 static int fan_watchdog_maxinterval;
7226 static struct mutex fan_mutex;
7228 static void fan_watchdog_fire(struct work_struct *ignored);
7229 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7231 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7232 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7233 "\\FSPD", /* 600e/x, 770e, 770x */
7234 ); /* all others */
7235 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7236 "JFNS", /* 770x-JL */
7237 ); /* all others */
7240 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7241 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7242 * be in auto mode (0x80).
7244 * This is corrected by any write to HFSP either by the driver, or
7245 * by the firmware.
7247 * We assume 0x07 really means auto mode while this quirk is active,
7248 * as this is far more likely than the ThinkPad being in level 7,
7249 * which is only used by the firmware during thermal emergencies.
7251 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7252 * TP-70 (T43, R52), which are known to be buggy.
7255 static void fan_quirk1_setup(void)
7257 if (fan_control_initial_status == 0x07) {
7258 printk(TPACPI_NOTICE
7259 "fan_init: initial fan status is unknown, "
7260 "assuming it is in auto mode\n");
7261 tp_features.fan_ctrl_status_undef = 1;
7265 static void fan_quirk1_handle(u8 *fan_status)
7267 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7268 if (*fan_status != fan_control_initial_status) {
7269 /* something changed the HFSP regisnter since
7270 * driver init time, so it is not undefined
7271 * anymore */
7272 tp_features.fan_ctrl_status_undef = 0;
7273 } else {
7274 /* Return most likely status. In fact, it
7275 * might be the only possible status */
7276 *fan_status = TP_EC_FAN_AUTO;
7281 /* Select main fan on X60/X61, NOOP on others */
7282 static bool fan_select_fan1(void)
7284 if (tp_features.second_fan) {
7285 u8 val;
7287 if (ec_read(fan_select_offset, &val) < 0)
7288 return false;
7289 val &= 0xFEU;
7290 if (ec_write(fan_select_offset, val) < 0)
7291 return false;
7293 return true;
7296 /* Select secondary fan on X60/X61 */
7297 static bool fan_select_fan2(void)
7299 u8 val;
7301 if (!tp_features.second_fan)
7302 return false;
7304 if (ec_read(fan_select_offset, &val) < 0)
7305 return false;
7306 val |= 0x01U;
7307 if (ec_write(fan_select_offset, val) < 0)
7308 return false;
7310 return true;
7314 * Call with fan_mutex held
7316 static void fan_update_desired_level(u8 status)
7318 if ((status &
7319 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7320 if (status > 7)
7321 fan_control_desired_level = 7;
7322 else
7323 fan_control_desired_level = status;
7327 static int fan_get_status(u8 *status)
7329 u8 s;
7331 /* TODO:
7332 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7334 switch (fan_status_access_mode) {
7335 case TPACPI_FAN_RD_ACPI_GFAN:
7336 /* 570, 600e/x, 770e, 770x */
7338 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7339 return -EIO;
7341 if (likely(status))
7342 *status = s & 0x07;
7344 break;
7346 case TPACPI_FAN_RD_TPEC:
7347 /* all except 570, 600e/x, 770e, 770x */
7348 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7349 return -EIO;
7351 if (likely(status)) {
7352 *status = s;
7353 fan_quirk1_handle(status);
7356 break;
7358 default:
7359 return -ENXIO;
7362 return 0;
7365 static int fan_get_status_safe(u8 *status)
7367 int rc;
7368 u8 s;
7370 if (mutex_lock_killable(&fan_mutex))
7371 return -ERESTARTSYS;
7372 rc = fan_get_status(&s);
7373 if (!rc)
7374 fan_update_desired_level(s);
7375 mutex_unlock(&fan_mutex);
7377 if (status)
7378 *status = s;
7380 return rc;
7383 static int fan_get_speed(unsigned int *speed)
7385 u8 hi, lo;
7387 switch (fan_status_access_mode) {
7388 case TPACPI_FAN_RD_TPEC:
7389 /* all except 570, 600e/x, 770e, 770x */
7390 if (unlikely(!fan_select_fan1()))
7391 return -EIO;
7392 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7393 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7394 return -EIO;
7396 if (likely(speed))
7397 *speed = (hi << 8) | lo;
7399 break;
7401 default:
7402 return -ENXIO;
7405 return 0;
7408 static int fan2_get_speed(unsigned int *speed)
7410 u8 hi, lo;
7411 bool rc;
7413 switch (fan_status_access_mode) {
7414 case TPACPI_FAN_RD_TPEC:
7415 /* all except 570, 600e/x, 770e, 770x */
7416 if (unlikely(!fan_select_fan2()))
7417 return -EIO;
7418 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7419 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7420 fan_select_fan1(); /* play it safe */
7421 if (rc)
7422 return -EIO;
7424 if (likely(speed))
7425 *speed = (hi << 8) | lo;
7427 break;
7429 default:
7430 return -ENXIO;
7433 return 0;
7436 static int fan_set_level(int level)
7438 if (!fan_control_allowed)
7439 return -EPERM;
7441 switch (fan_control_access_mode) {
7442 case TPACPI_FAN_WR_ACPI_SFAN:
7443 if (level >= 0 && level <= 7) {
7444 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7445 return -EIO;
7446 } else
7447 return -EINVAL;
7448 break;
7450 case TPACPI_FAN_WR_ACPI_FANS:
7451 case TPACPI_FAN_WR_TPEC:
7452 if (!(level & TP_EC_FAN_AUTO) &&
7453 !(level & TP_EC_FAN_FULLSPEED) &&
7454 ((level < 0) || (level > 7)))
7455 return -EINVAL;
7457 /* safety net should the EC not support AUTO
7458 * or FULLSPEED mode bits and just ignore them */
7459 if (level & TP_EC_FAN_FULLSPEED)
7460 level |= 7; /* safety min speed 7 */
7461 else if (level & TP_EC_FAN_AUTO)
7462 level |= 4; /* safety min speed 4 */
7464 if (!acpi_ec_write(fan_status_offset, level))
7465 return -EIO;
7466 else
7467 tp_features.fan_ctrl_status_undef = 0;
7468 break;
7470 default:
7471 return -ENXIO;
7474 vdbg_printk(TPACPI_DBG_FAN,
7475 "fan control: set fan control register to 0x%02x\n", level);
7476 return 0;
7479 static int fan_set_level_safe(int level)
7481 int rc;
7483 if (!fan_control_allowed)
7484 return -EPERM;
7486 if (mutex_lock_killable(&fan_mutex))
7487 return -ERESTARTSYS;
7489 if (level == TPACPI_FAN_LAST_LEVEL)
7490 level = fan_control_desired_level;
7492 rc = fan_set_level(level);
7493 if (!rc)
7494 fan_update_desired_level(level);
7496 mutex_unlock(&fan_mutex);
7497 return rc;
7500 static int fan_set_enable(void)
7502 u8 s;
7503 int rc;
7505 if (!fan_control_allowed)
7506 return -EPERM;
7508 if (mutex_lock_killable(&fan_mutex))
7509 return -ERESTARTSYS;
7511 switch (fan_control_access_mode) {
7512 case TPACPI_FAN_WR_ACPI_FANS:
7513 case TPACPI_FAN_WR_TPEC:
7514 rc = fan_get_status(&s);
7515 if (rc < 0)
7516 break;
7518 /* Don't go out of emergency fan mode */
7519 if (s != 7) {
7520 s &= 0x07;
7521 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7524 if (!acpi_ec_write(fan_status_offset, s))
7525 rc = -EIO;
7526 else {
7527 tp_features.fan_ctrl_status_undef = 0;
7528 rc = 0;
7530 break;
7532 case TPACPI_FAN_WR_ACPI_SFAN:
7533 rc = fan_get_status(&s);
7534 if (rc < 0)
7535 break;
7537 s &= 0x07;
7539 /* Set fan to at least level 4 */
7540 s |= 4;
7542 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7543 rc = -EIO;
7544 else
7545 rc = 0;
7546 break;
7548 default:
7549 rc = -ENXIO;
7552 mutex_unlock(&fan_mutex);
7554 if (!rc)
7555 vdbg_printk(TPACPI_DBG_FAN,
7556 "fan control: set fan control register to 0x%02x\n",
7558 return rc;
7561 static int fan_set_disable(void)
7563 int rc;
7565 if (!fan_control_allowed)
7566 return -EPERM;
7568 if (mutex_lock_killable(&fan_mutex))
7569 return -ERESTARTSYS;
7571 rc = 0;
7572 switch (fan_control_access_mode) {
7573 case TPACPI_FAN_WR_ACPI_FANS:
7574 case TPACPI_FAN_WR_TPEC:
7575 if (!acpi_ec_write(fan_status_offset, 0x00))
7576 rc = -EIO;
7577 else {
7578 fan_control_desired_level = 0;
7579 tp_features.fan_ctrl_status_undef = 0;
7581 break;
7583 case TPACPI_FAN_WR_ACPI_SFAN:
7584 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7585 rc = -EIO;
7586 else
7587 fan_control_desired_level = 0;
7588 break;
7590 default:
7591 rc = -ENXIO;
7594 if (!rc)
7595 vdbg_printk(TPACPI_DBG_FAN,
7596 "fan control: set fan control register to 0\n");
7598 mutex_unlock(&fan_mutex);
7599 return rc;
7602 static int fan_set_speed(int speed)
7604 int rc;
7606 if (!fan_control_allowed)
7607 return -EPERM;
7609 if (mutex_lock_killable(&fan_mutex))
7610 return -ERESTARTSYS;
7612 rc = 0;
7613 switch (fan_control_access_mode) {
7614 case TPACPI_FAN_WR_ACPI_FANS:
7615 if (speed >= 0 && speed <= 65535) {
7616 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7617 speed, speed, speed))
7618 rc = -EIO;
7619 } else
7620 rc = -EINVAL;
7621 break;
7623 default:
7624 rc = -ENXIO;
7627 mutex_unlock(&fan_mutex);
7628 return rc;
7631 static void fan_watchdog_reset(void)
7633 static int fan_watchdog_active;
7635 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7636 return;
7638 if (fan_watchdog_active)
7639 cancel_delayed_work(&fan_watchdog_task);
7641 if (fan_watchdog_maxinterval > 0 &&
7642 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7643 fan_watchdog_active = 1;
7644 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7645 msecs_to_jiffies(fan_watchdog_maxinterval
7646 * 1000))) {
7647 printk(TPACPI_ERR
7648 "failed to queue the fan watchdog, "
7649 "watchdog will not trigger\n");
7651 } else
7652 fan_watchdog_active = 0;
7655 static void fan_watchdog_fire(struct work_struct *ignored)
7657 int rc;
7659 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7660 return;
7662 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7663 rc = fan_set_enable();
7664 if (rc < 0) {
7665 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7666 "will try again later...\n", -rc);
7667 /* reschedule for later */
7668 fan_watchdog_reset();
7673 * SYSFS fan layout: hwmon compatible (device)
7675 * pwm*_enable:
7676 * 0: "disengaged" mode
7677 * 1: manual mode
7678 * 2: native EC "auto" mode (recommended, hardware default)
7680 * pwm*: set speed in manual mode, ignored otherwise.
7681 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7682 * interpolation.
7684 * fan*_input: tachometer reading, RPM
7687 * SYSFS fan layout: extensions
7689 * fan_watchdog (driver):
7690 * fan watchdog interval in seconds, 0 disables (default), max 120
7693 /* sysfs fan pwm1_enable ----------------------------------------------- */
7694 static ssize_t fan_pwm1_enable_show(struct device *dev,
7695 struct device_attribute *attr,
7696 char *buf)
7698 int res, mode;
7699 u8 status;
7701 res = fan_get_status_safe(&status);
7702 if (res)
7703 return res;
7705 if (status & TP_EC_FAN_FULLSPEED) {
7706 mode = 0;
7707 } else if (status & TP_EC_FAN_AUTO) {
7708 mode = 2;
7709 } else
7710 mode = 1;
7712 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7715 static ssize_t fan_pwm1_enable_store(struct device *dev,
7716 struct device_attribute *attr,
7717 const char *buf, size_t count)
7719 unsigned long t;
7720 int res, level;
7722 if (parse_strtoul(buf, 2, &t))
7723 return -EINVAL;
7725 tpacpi_disclose_usertask("hwmon pwm1_enable",
7726 "set fan mode to %lu\n", t);
7728 switch (t) {
7729 case 0:
7730 level = TP_EC_FAN_FULLSPEED;
7731 break;
7732 case 1:
7733 level = TPACPI_FAN_LAST_LEVEL;
7734 break;
7735 case 2:
7736 level = TP_EC_FAN_AUTO;
7737 break;
7738 case 3:
7739 /* reserved for software-controlled auto mode */
7740 return -ENOSYS;
7741 default:
7742 return -EINVAL;
7745 res = fan_set_level_safe(level);
7746 if (res == -ENXIO)
7747 return -EINVAL;
7748 else if (res < 0)
7749 return res;
7751 fan_watchdog_reset();
7753 return count;
7756 static struct device_attribute dev_attr_fan_pwm1_enable =
7757 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7758 fan_pwm1_enable_show, fan_pwm1_enable_store);
7760 /* sysfs fan pwm1 ------------------------------------------------------ */
7761 static ssize_t fan_pwm1_show(struct device *dev,
7762 struct device_attribute *attr,
7763 char *buf)
7765 int res;
7766 u8 status;
7768 res = fan_get_status_safe(&status);
7769 if (res)
7770 return res;
7772 if ((status &
7773 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7774 status = fan_control_desired_level;
7776 if (status > 7)
7777 status = 7;
7779 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7782 static ssize_t fan_pwm1_store(struct device *dev,
7783 struct device_attribute *attr,
7784 const char *buf, size_t count)
7786 unsigned long s;
7787 int rc;
7788 u8 status, newlevel;
7790 if (parse_strtoul(buf, 255, &s))
7791 return -EINVAL;
7793 tpacpi_disclose_usertask("hwmon pwm1",
7794 "set fan speed to %lu\n", s);
7796 /* scale down from 0-255 to 0-7 */
7797 newlevel = (s >> 5) & 0x07;
7799 if (mutex_lock_killable(&fan_mutex))
7800 return -ERESTARTSYS;
7802 rc = fan_get_status(&status);
7803 if (!rc && (status &
7804 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7805 rc = fan_set_level(newlevel);
7806 if (rc == -ENXIO)
7807 rc = -EINVAL;
7808 else if (!rc) {
7809 fan_update_desired_level(newlevel);
7810 fan_watchdog_reset();
7814 mutex_unlock(&fan_mutex);
7815 return (rc)? rc : count;
7818 static struct device_attribute dev_attr_fan_pwm1 =
7819 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7820 fan_pwm1_show, fan_pwm1_store);
7822 /* sysfs fan fan1_input ------------------------------------------------ */
7823 static ssize_t fan_fan1_input_show(struct device *dev,
7824 struct device_attribute *attr,
7825 char *buf)
7827 int res;
7828 unsigned int speed;
7830 res = fan_get_speed(&speed);
7831 if (res < 0)
7832 return res;
7834 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7837 static struct device_attribute dev_attr_fan_fan1_input =
7838 __ATTR(fan1_input, S_IRUGO,
7839 fan_fan1_input_show, NULL);
7841 /* sysfs fan fan2_input ------------------------------------------------ */
7842 static ssize_t fan_fan2_input_show(struct device *dev,
7843 struct device_attribute *attr,
7844 char *buf)
7846 int res;
7847 unsigned int speed;
7849 res = fan2_get_speed(&speed);
7850 if (res < 0)
7851 return res;
7853 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7856 static struct device_attribute dev_attr_fan_fan2_input =
7857 __ATTR(fan2_input, S_IRUGO,
7858 fan_fan2_input_show, NULL);
7860 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7861 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7862 char *buf)
7864 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7867 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7868 const char *buf, size_t count)
7870 unsigned long t;
7872 if (parse_strtoul(buf, 120, &t))
7873 return -EINVAL;
7875 if (!fan_control_allowed)
7876 return -EPERM;
7878 fan_watchdog_maxinterval = t;
7879 fan_watchdog_reset();
7881 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7883 return count;
7886 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7887 fan_fan_watchdog_show, fan_fan_watchdog_store);
7889 /* --------------------------------------------------------------------- */
7890 static struct attribute *fan_attributes[] = {
7891 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7892 &dev_attr_fan_fan1_input.attr,
7893 NULL, /* for fan2_input */
7894 NULL
7897 static const struct attribute_group fan_attr_group = {
7898 .attrs = fan_attributes,
7901 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7902 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7904 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7905 { .vendor = PCI_VENDOR_ID_IBM, \
7906 .bios = TPACPI_MATCH_ANY, \
7907 .ec = TPID(__id1, __id2), \
7908 .quirks = __quirks }
7910 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7911 { .vendor = PCI_VENDOR_ID_LENOVO, \
7912 .bios = TPACPI_MATCH_ANY, \
7913 .ec = TPID(__id1, __id2), \
7914 .quirks = __quirks }
7916 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7917 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7918 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7919 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7920 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7921 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7924 #undef TPACPI_FAN_QL
7925 #undef TPACPI_FAN_QI
7927 static int __init fan_init(struct ibm_init_struct *iibm)
7929 int rc;
7930 unsigned long quirks;
7932 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7933 "initializing fan subdriver\n");
7935 mutex_init(&fan_mutex);
7936 fan_status_access_mode = TPACPI_FAN_NONE;
7937 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7938 fan_control_commands = 0;
7939 fan_watchdog_maxinterval = 0;
7940 tp_features.fan_ctrl_status_undef = 0;
7941 tp_features.second_fan = 0;
7942 fan_control_desired_level = 7;
7944 TPACPI_ACPIHANDLE_INIT(fans);
7945 TPACPI_ACPIHANDLE_INIT(gfan);
7946 TPACPI_ACPIHANDLE_INIT(sfan);
7948 quirks = tpacpi_check_quirks(fan_quirk_table,
7949 ARRAY_SIZE(fan_quirk_table));
7951 if (gfan_handle) {
7952 /* 570, 600e/x, 770e, 770x */
7953 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7954 } else {
7955 /* all other ThinkPads: note that even old-style
7956 * ThinkPad ECs supports the fan control register */
7957 if (likely(acpi_ec_read(fan_status_offset,
7958 &fan_control_initial_status))) {
7959 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7960 if (quirks & TPACPI_FAN_Q1)
7961 fan_quirk1_setup();
7962 if (quirks & TPACPI_FAN_2FAN) {
7963 tp_features.second_fan = 1;
7964 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7965 "secondary fan support enabled\n");
7967 } else {
7968 printk(TPACPI_ERR
7969 "ThinkPad ACPI EC access misbehaving, "
7970 "fan status and control unavailable\n");
7971 return 1;
7975 if (sfan_handle) {
7976 /* 570, 770x-JL */
7977 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7978 fan_control_commands |=
7979 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7980 } else {
7981 if (!gfan_handle) {
7982 /* gfan without sfan means no fan control */
7983 /* all other models implement TP EC 0x2f control */
7985 if (fans_handle) {
7986 /* X31, X40, X41 */
7987 fan_control_access_mode =
7988 TPACPI_FAN_WR_ACPI_FANS;
7989 fan_control_commands |=
7990 TPACPI_FAN_CMD_SPEED |
7991 TPACPI_FAN_CMD_LEVEL |
7992 TPACPI_FAN_CMD_ENABLE;
7993 } else {
7994 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7995 fan_control_commands |=
7996 TPACPI_FAN_CMD_LEVEL |
7997 TPACPI_FAN_CMD_ENABLE;
8002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8003 "fan is %s, modes %d, %d\n",
8004 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8005 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8006 fan_status_access_mode, fan_control_access_mode);
8008 /* fan control master switch */
8009 if (!fan_control_allowed) {
8010 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8011 fan_control_commands = 0;
8012 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8013 "fan control features disabled by parameter\n");
8016 /* update fan_control_desired_level */
8017 if (fan_status_access_mode != TPACPI_FAN_NONE)
8018 fan_get_status_safe(NULL);
8020 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8021 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8022 if (tp_features.second_fan) {
8023 /* attach second fan tachometer */
8024 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8025 &dev_attr_fan_fan2_input.attr;
8027 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8028 &fan_attr_group);
8029 if (rc < 0)
8030 return rc;
8032 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8033 &driver_attr_fan_watchdog);
8034 if (rc < 0) {
8035 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8036 &fan_attr_group);
8037 return rc;
8039 return 0;
8040 } else
8041 return 1;
8044 static void fan_exit(void)
8046 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8047 "cancelling any pending fan watchdog tasks\n");
8049 /* FIXME: can we really do this unconditionally? */
8050 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8051 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8052 &driver_attr_fan_watchdog);
8054 cancel_delayed_work(&fan_watchdog_task);
8055 flush_workqueue(tpacpi_wq);
8058 static void fan_suspend(pm_message_t state)
8060 int rc;
8062 if (!fan_control_allowed)
8063 return;
8065 /* Store fan status in cache */
8066 fan_control_resume_level = 0;
8067 rc = fan_get_status_safe(&fan_control_resume_level);
8068 if (rc < 0)
8069 printk(TPACPI_NOTICE
8070 "failed to read fan level for later "
8071 "restore during resume: %d\n", rc);
8073 /* if it is undefined, don't attempt to restore it.
8074 * KEEP THIS LAST */
8075 if (tp_features.fan_ctrl_status_undef)
8076 fan_control_resume_level = 0;
8079 static void fan_resume(void)
8081 u8 current_level = 7;
8082 bool do_set = false;
8083 int rc;
8085 /* DSDT *always* updates status on resume */
8086 tp_features.fan_ctrl_status_undef = 0;
8088 if (!fan_control_allowed ||
8089 !fan_control_resume_level ||
8090 (fan_get_status_safe(&current_level) < 0))
8091 return;
8093 switch (fan_control_access_mode) {
8094 case TPACPI_FAN_WR_ACPI_SFAN:
8095 /* never decrease fan level */
8096 do_set = (fan_control_resume_level > current_level);
8097 break;
8098 case TPACPI_FAN_WR_ACPI_FANS:
8099 case TPACPI_FAN_WR_TPEC:
8100 /* never decrease fan level, scale is:
8101 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8103 * We expect the firmware to set either 7 or AUTO, but we
8104 * handle FULLSPEED out of paranoia.
8106 * So, we can safely only restore FULLSPEED or 7, anything
8107 * else could slow the fan. Restoring AUTO is useless, at
8108 * best that's exactly what the DSDT already set (it is the
8109 * slower it uses).
8111 * Always keep in mind that the DSDT *will* have set the
8112 * fans to what the vendor supposes is the best level. We
8113 * muck with it only to speed the fan up.
8115 if (fan_control_resume_level != 7 &&
8116 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8117 return;
8118 else
8119 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8120 (current_level != fan_control_resume_level);
8121 break;
8122 default:
8123 return;
8125 if (do_set) {
8126 printk(TPACPI_NOTICE
8127 "restoring fan level to 0x%02x\n",
8128 fan_control_resume_level);
8129 rc = fan_set_level_safe(fan_control_resume_level);
8130 if (rc < 0)
8131 printk(TPACPI_NOTICE
8132 "failed to restore fan level: %d\n", rc);
8136 static int fan_read(struct seq_file *m)
8138 int rc;
8139 u8 status;
8140 unsigned int speed = 0;
8142 switch (fan_status_access_mode) {
8143 case TPACPI_FAN_RD_ACPI_GFAN:
8144 /* 570, 600e/x, 770e, 770x */
8145 rc = fan_get_status_safe(&status);
8146 if (rc < 0)
8147 return rc;
8149 seq_printf(m, "status:\t\t%s\n"
8150 "level:\t\t%d\n",
8151 (status != 0) ? "enabled" : "disabled", status);
8152 break;
8154 case TPACPI_FAN_RD_TPEC:
8155 /* all except 570, 600e/x, 770e, 770x */
8156 rc = fan_get_status_safe(&status);
8157 if (rc < 0)
8158 return rc;
8160 seq_printf(m, "status:\t\t%s\n",
8161 (status != 0) ? "enabled" : "disabled");
8163 rc = fan_get_speed(&speed);
8164 if (rc < 0)
8165 return rc;
8167 seq_printf(m, "speed:\t\t%d\n", speed);
8169 if (status & TP_EC_FAN_FULLSPEED)
8170 /* Disengaged mode takes precedence */
8171 seq_printf(m, "level:\t\tdisengaged\n");
8172 else if (status & TP_EC_FAN_AUTO)
8173 seq_printf(m, "level:\t\tauto\n");
8174 else
8175 seq_printf(m, "level:\t\t%d\n", status);
8176 break;
8178 case TPACPI_FAN_NONE:
8179 default:
8180 seq_printf(m, "status:\t\tnot supported\n");
8183 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8184 seq_printf(m, "commands:\tlevel <level>");
8186 switch (fan_control_access_mode) {
8187 case TPACPI_FAN_WR_ACPI_SFAN:
8188 seq_printf(m, " (<level> is 0-7)\n");
8189 break;
8191 default:
8192 seq_printf(m, " (<level> is 0-7, "
8193 "auto, disengaged, full-speed)\n");
8194 break;
8198 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8199 seq_printf(m, "commands:\tenable, disable\n"
8200 "commands:\twatchdog <timeout> (<timeout> "
8201 "is 0 (off), 1-120 (seconds))\n");
8203 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8204 seq_printf(m, "commands:\tspeed <speed>"
8205 " (<speed> is 0-65535)\n");
8207 return 0;
8210 static int fan_write_cmd_level(const char *cmd, int *rc)
8212 int level;
8214 if (strlencmp(cmd, "level auto") == 0)
8215 level = TP_EC_FAN_AUTO;
8216 else if ((strlencmp(cmd, "level disengaged") == 0) |
8217 (strlencmp(cmd, "level full-speed") == 0))
8218 level = TP_EC_FAN_FULLSPEED;
8219 else if (sscanf(cmd, "level %d", &level) != 1)
8220 return 0;
8222 *rc = fan_set_level_safe(level);
8223 if (*rc == -ENXIO)
8224 printk(TPACPI_ERR "level command accepted for unsupported "
8225 "access mode %d", fan_control_access_mode);
8226 else if (!*rc)
8227 tpacpi_disclose_usertask("procfs fan",
8228 "set level to %d\n", level);
8230 return 1;
8233 static int fan_write_cmd_enable(const char *cmd, int *rc)
8235 if (strlencmp(cmd, "enable") != 0)
8236 return 0;
8238 *rc = fan_set_enable();
8239 if (*rc == -ENXIO)
8240 printk(TPACPI_ERR "enable command accepted for unsupported "
8241 "access mode %d", fan_control_access_mode);
8242 else if (!*rc)
8243 tpacpi_disclose_usertask("procfs fan", "enable\n");
8245 return 1;
8248 static int fan_write_cmd_disable(const char *cmd, int *rc)
8250 if (strlencmp(cmd, "disable") != 0)
8251 return 0;
8253 *rc = fan_set_disable();
8254 if (*rc == -ENXIO)
8255 printk(TPACPI_ERR "disable command accepted for unsupported "
8256 "access mode %d", fan_control_access_mode);
8257 else if (!*rc)
8258 tpacpi_disclose_usertask("procfs fan", "disable\n");
8260 return 1;
8263 static int fan_write_cmd_speed(const char *cmd, int *rc)
8265 int speed;
8267 /* TODO:
8268 * Support speed <low> <medium> <high> ? */
8270 if (sscanf(cmd, "speed %d", &speed) != 1)
8271 return 0;
8273 *rc = fan_set_speed(speed);
8274 if (*rc == -ENXIO)
8275 printk(TPACPI_ERR "speed command accepted for unsupported "
8276 "access mode %d", fan_control_access_mode);
8277 else if (!*rc)
8278 tpacpi_disclose_usertask("procfs fan",
8279 "set speed to %d\n", speed);
8281 return 1;
8284 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8286 int interval;
8288 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8289 return 0;
8291 if (interval < 0 || interval > 120)
8292 *rc = -EINVAL;
8293 else {
8294 fan_watchdog_maxinterval = interval;
8295 tpacpi_disclose_usertask("procfs fan",
8296 "set watchdog timer to %d\n",
8297 interval);
8300 return 1;
8303 static int fan_write(char *buf)
8305 char *cmd;
8306 int rc = 0;
8308 while (!rc && (cmd = next_cmd(&buf))) {
8309 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8310 fan_write_cmd_level(cmd, &rc)) &&
8311 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8312 (fan_write_cmd_enable(cmd, &rc) ||
8313 fan_write_cmd_disable(cmd, &rc) ||
8314 fan_write_cmd_watchdog(cmd, &rc))) &&
8315 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8316 fan_write_cmd_speed(cmd, &rc))
8318 rc = -EINVAL;
8319 else if (!rc)
8320 fan_watchdog_reset();
8323 return rc;
8326 static struct ibm_struct fan_driver_data = {
8327 .name = "fan",
8328 .read = fan_read,
8329 .write = fan_write,
8330 .exit = fan_exit,
8331 .suspend = fan_suspend,
8332 .resume = fan_resume,
8335 /****************************************************************************
8336 ****************************************************************************
8338 * Infrastructure
8340 ****************************************************************************
8341 ****************************************************************************/
8344 * HKEY event callout for other subdrivers go here
8345 * (yes, it is ugly, but it is quick, safe, and gets the job done
8347 static void tpacpi_driver_event(const unsigned int hkey_event)
8349 if (ibm_backlight_device) {
8350 switch (hkey_event) {
8351 case TP_HKEY_EV_BRGHT_UP:
8352 case TP_HKEY_EV_BRGHT_DOWN:
8353 tpacpi_brightness_notify_change();
8356 if (alsa_card) {
8357 switch (hkey_event) {
8358 case TP_HKEY_EV_VOL_UP:
8359 case TP_HKEY_EV_VOL_DOWN:
8360 case TP_HKEY_EV_VOL_MUTE:
8361 volume_alsa_notify_change();
8366 static void hotkey_driver_event(const unsigned int scancode)
8368 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8371 /* sysfs name ---------------------------------------------------------- */
8372 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8373 struct device_attribute *attr,
8374 char *buf)
8376 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8379 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8380 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8382 /* --------------------------------------------------------------------- */
8384 /* /proc support */
8385 static struct proc_dir_entry *proc_dir;
8388 * Module and infrastructure proble, init and exit handling
8391 static int force_load;
8393 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8394 static const char * __init str_supported(int is_supported)
8396 static char text_unsupported[] __initdata = "not supported";
8398 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8400 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8402 static void ibm_exit(struct ibm_struct *ibm)
8404 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8406 list_del_init(&ibm->all_drivers);
8408 if (ibm->flags.acpi_notify_installed) {
8409 dbg_printk(TPACPI_DBG_EXIT,
8410 "%s: acpi_remove_notify_handler\n", ibm->name);
8411 BUG_ON(!ibm->acpi);
8412 acpi_remove_notify_handler(*ibm->acpi->handle,
8413 ibm->acpi->type,
8414 dispatch_acpi_notify);
8415 ibm->flags.acpi_notify_installed = 0;
8416 ibm->flags.acpi_notify_installed = 0;
8419 if (ibm->flags.proc_created) {
8420 dbg_printk(TPACPI_DBG_EXIT,
8421 "%s: remove_proc_entry\n", ibm->name);
8422 remove_proc_entry(ibm->name, proc_dir);
8423 ibm->flags.proc_created = 0;
8426 if (ibm->flags.acpi_driver_registered) {
8427 dbg_printk(TPACPI_DBG_EXIT,
8428 "%s: acpi_bus_unregister_driver\n", ibm->name);
8429 BUG_ON(!ibm->acpi);
8430 acpi_bus_unregister_driver(ibm->acpi->driver);
8431 kfree(ibm->acpi->driver);
8432 ibm->acpi->driver = NULL;
8433 ibm->flags.acpi_driver_registered = 0;
8436 if (ibm->flags.init_called && ibm->exit) {
8437 ibm->exit();
8438 ibm->flags.init_called = 0;
8441 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8444 static int __init ibm_init(struct ibm_init_struct *iibm)
8446 int ret;
8447 struct ibm_struct *ibm = iibm->data;
8448 struct proc_dir_entry *entry;
8450 BUG_ON(ibm == NULL);
8452 INIT_LIST_HEAD(&ibm->all_drivers);
8454 if (ibm->flags.experimental && !experimental)
8455 return 0;
8457 dbg_printk(TPACPI_DBG_INIT,
8458 "probing for %s\n", ibm->name);
8460 if (iibm->init) {
8461 ret = iibm->init(iibm);
8462 if (ret > 0)
8463 return 0; /* probe failed */
8464 if (ret)
8465 return ret;
8467 ibm->flags.init_called = 1;
8470 if (ibm->acpi) {
8471 if (ibm->acpi->hid) {
8472 ret = register_tpacpi_subdriver(ibm);
8473 if (ret)
8474 goto err_out;
8477 if (ibm->acpi->notify) {
8478 ret = setup_acpi_notify(ibm);
8479 if (ret == -ENODEV) {
8480 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8481 ibm->name);
8482 ret = 0;
8483 goto err_out;
8485 if (ret < 0)
8486 goto err_out;
8490 dbg_printk(TPACPI_DBG_INIT,
8491 "%s installed\n", ibm->name);
8493 if (ibm->read) {
8494 mode_t mode = iibm->base_procfs_mode;
8496 if (!mode)
8497 mode = S_IRUGO;
8498 if (ibm->write)
8499 mode |= S_IWUSR;
8500 entry = proc_create_data(ibm->name, mode, proc_dir,
8501 &dispatch_proc_fops, ibm);
8502 if (!entry) {
8503 printk(TPACPI_ERR "unable to create proc entry %s\n",
8504 ibm->name);
8505 ret = -ENODEV;
8506 goto err_out;
8508 ibm->flags.proc_created = 1;
8511 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8513 return 0;
8515 err_out:
8516 dbg_printk(TPACPI_DBG_INIT,
8517 "%s: at error exit path with result %d\n",
8518 ibm->name, ret);
8520 ibm_exit(ibm);
8521 return (ret < 0)? ret : 0;
8524 /* Probing */
8526 static bool __pure __init tpacpi_is_fw_digit(const char c)
8528 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8531 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8532 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8533 const char t)
8535 return s && strlen(s) >= 8 &&
8536 tpacpi_is_fw_digit(s[0]) &&
8537 tpacpi_is_fw_digit(s[1]) &&
8538 s[2] == t && s[3] == 'T' &&
8539 tpacpi_is_fw_digit(s[4]) &&
8540 tpacpi_is_fw_digit(s[5]) &&
8541 s[6] == 'W' && s[7] == 'W';
8544 /* returns 0 - probe ok, or < 0 - probe error.
8545 * Probe ok doesn't mean thinkpad found.
8546 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8547 static int __must_check __init get_thinkpad_model_data(
8548 struct thinkpad_id_data *tp)
8550 const struct dmi_device *dev = NULL;
8551 char ec_fw_string[18];
8552 char const *s;
8554 if (!tp)
8555 return -EINVAL;
8557 memset(tp, 0, sizeof(*tp));
8559 if (dmi_name_in_vendors("IBM"))
8560 tp->vendor = PCI_VENDOR_ID_IBM;
8561 else if (dmi_name_in_vendors("LENOVO"))
8562 tp->vendor = PCI_VENDOR_ID_LENOVO;
8563 else
8564 return 0;
8566 s = dmi_get_system_info(DMI_BIOS_VERSION);
8567 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8568 if (s && !tp->bios_version_str)
8569 return -ENOMEM;
8571 /* Really ancient ThinkPad 240X will fail this, which is fine */
8572 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8573 return 0;
8575 tp->bios_model = tp->bios_version_str[0]
8576 | (tp->bios_version_str[1] << 8);
8577 tp->bios_release = (tp->bios_version_str[4] << 8)
8578 | tp->bios_version_str[5];
8581 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8582 * X32 or newer, all Z series; Some models must have an
8583 * up-to-date BIOS or they will not be detected.
8585 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8587 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8588 if (sscanf(dev->name,
8589 "IBM ThinkPad Embedded Controller -[%17c",
8590 ec_fw_string) == 1) {
8591 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8592 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8594 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8595 if (!tp->ec_version_str)
8596 return -ENOMEM;
8598 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8599 tp->ec_model = ec_fw_string[0]
8600 | (ec_fw_string[1] << 8);
8601 tp->ec_release = (ec_fw_string[4] << 8)
8602 | ec_fw_string[5];
8603 } else {
8604 printk(TPACPI_NOTICE
8605 "ThinkPad firmware release %s "
8606 "doesn't match the known patterns\n",
8607 ec_fw_string);
8608 printk(TPACPI_NOTICE
8609 "please report this to %s\n",
8610 TPACPI_MAIL);
8612 break;
8616 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8617 if (s && !strnicmp(s, "ThinkPad", 8)) {
8618 tp->model_str = kstrdup(s, GFP_KERNEL);
8619 if (!tp->model_str)
8620 return -ENOMEM;
8623 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8624 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8625 if (s && !tp->nummodel_str)
8626 return -ENOMEM;
8628 return 0;
8631 static int __init probe_for_thinkpad(void)
8633 int is_thinkpad;
8635 if (acpi_disabled)
8636 return -ENODEV;
8639 * Non-ancient models have better DMI tagging, but very old models
8640 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8642 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8643 (thinkpad_id.ec_model != 0) ||
8644 tpacpi_is_fw_known();
8646 /* ec is required because many other handles are relative to it */
8647 TPACPI_ACPIHANDLE_INIT(ec);
8648 if (!ec_handle) {
8649 if (is_thinkpad)
8650 printk(TPACPI_ERR
8651 "Not yet supported ThinkPad detected!\n");
8652 return -ENODEV;
8655 if (!is_thinkpad && !force_load)
8656 return -ENODEV;
8658 return 0;
8662 /* Module init, exit, parameters */
8664 static struct ibm_init_struct ibms_init[] __initdata = {
8666 .init = thinkpad_acpi_driver_init,
8667 .data = &thinkpad_acpi_driver_data,
8670 .init = hotkey_init,
8671 .data = &hotkey_driver_data,
8674 .init = bluetooth_init,
8675 .data = &bluetooth_driver_data,
8678 .init = wan_init,
8679 .data = &wan_driver_data,
8682 .init = uwb_init,
8683 .data = &uwb_driver_data,
8685 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8687 .init = video_init,
8688 .base_procfs_mode = S_IRUSR,
8689 .data = &video_driver_data,
8691 #endif
8693 .init = light_init,
8694 .data = &light_driver_data,
8697 .init = cmos_init,
8698 .data = &cmos_driver_data,
8701 .init = led_init,
8702 .data = &led_driver_data,
8705 .init = beep_init,
8706 .data = &beep_driver_data,
8709 .init = thermal_init,
8710 .data = &thermal_driver_data,
8713 .data = &ecdump_driver_data,
8716 .init = brightness_init,
8717 .data = &brightness_driver_data,
8720 .init = volume_init,
8721 .data = &volume_driver_data,
8724 .init = fan_init,
8725 .data = &fan_driver_data,
8729 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8731 unsigned int i;
8732 struct ibm_struct *ibm;
8734 if (!kp || !kp->name || !val)
8735 return -EINVAL;
8737 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8738 ibm = ibms_init[i].data;
8739 WARN_ON(ibm == NULL);
8741 if (!ibm || !ibm->name)
8742 continue;
8744 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8745 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8746 return -ENOSPC;
8747 strcpy(ibms_init[i].param, val);
8748 strcat(ibms_init[i].param, ",");
8749 return 0;
8753 return -EINVAL;
8756 module_param(experimental, int, 0444);
8757 MODULE_PARM_DESC(experimental,
8758 "Enables experimental features when non-zero");
8760 module_param_named(debug, dbg_level, uint, 0);
8761 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8763 module_param(force_load, bool, 0444);
8764 MODULE_PARM_DESC(force_load,
8765 "Attempts to load the driver even on a "
8766 "mis-identified ThinkPad when true");
8768 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8769 MODULE_PARM_DESC(fan_control,
8770 "Enables setting fan parameters features when true");
8772 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8773 MODULE_PARM_DESC(brightness_mode,
8774 "Selects brightness control strategy: "
8775 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8777 module_param(brightness_enable, uint, 0444);
8778 MODULE_PARM_DESC(brightness_enable,
8779 "Enables backlight control when 1, disables when 0");
8781 module_param(hotkey_report_mode, uint, 0444);
8782 MODULE_PARM_DESC(hotkey_report_mode,
8783 "used for backwards compatibility with userspace, "
8784 "see documentation");
8786 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8787 module_param_named(volume_mode, volume_mode, uint, 0444);
8788 MODULE_PARM_DESC(volume_mode,
8789 "Selects volume control strategy: "
8790 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8792 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8793 MODULE_PARM_DESC(volume_capabilities,
8794 "Selects the mixer capabilites: "
8795 "0=auto, 1=volume and mute, 2=mute only");
8797 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8798 MODULE_PARM_DESC(volume_control,
8799 "Enables software override for the console audio "
8800 "control when true");
8802 /* ALSA module API parameters */
8803 module_param_named(index, alsa_index, int, 0444);
8804 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8805 module_param_named(id, alsa_id, charp, 0444);
8806 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8807 module_param_named(enable, alsa_enable, bool, 0444);
8808 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8809 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8811 #define TPACPI_PARAM(feature) \
8812 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8813 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8814 "at module load, see documentation")
8816 TPACPI_PARAM(hotkey);
8817 TPACPI_PARAM(bluetooth);
8818 TPACPI_PARAM(video);
8819 TPACPI_PARAM(light);
8820 TPACPI_PARAM(cmos);
8821 TPACPI_PARAM(led);
8822 TPACPI_PARAM(beep);
8823 TPACPI_PARAM(ecdump);
8824 TPACPI_PARAM(brightness);
8825 TPACPI_PARAM(volume);
8826 TPACPI_PARAM(fan);
8828 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8829 module_param(dbg_wlswemul, uint, 0444);
8830 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8831 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8832 MODULE_PARM_DESC(wlsw_state,
8833 "Initial state of the emulated WLSW switch");
8835 module_param(dbg_bluetoothemul, uint, 0444);
8836 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8837 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8838 MODULE_PARM_DESC(bluetooth_state,
8839 "Initial state of the emulated bluetooth switch");
8841 module_param(dbg_wwanemul, uint, 0444);
8842 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8843 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8844 MODULE_PARM_DESC(wwan_state,
8845 "Initial state of the emulated WWAN switch");
8847 module_param(dbg_uwbemul, uint, 0444);
8848 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8849 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8850 MODULE_PARM_DESC(uwb_state,
8851 "Initial state of the emulated UWB switch");
8852 #endif
8854 static void thinkpad_acpi_module_exit(void)
8856 struct ibm_struct *ibm, *itmp;
8858 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8860 list_for_each_entry_safe_reverse(ibm, itmp,
8861 &tpacpi_all_drivers,
8862 all_drivers) {
8863 ibm_exit(ibm);
8866 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8868 if (tpacpi_inputdev) {
8869 if (tp_features.input_device_registered)
8870 input_unregister_device(tpacpi_inputdev);
8871 else
8872 input_free_device(tpacpi_inputdev);
8875 if (tpacpi_hwmon)
8876 hwmon_device_unregister(tpacpi_hwmon);
8878 if (tp_features.sensors_pdev_attrs_registered)
8879 device_remove_file(&tpacpi_sensors_pdev->dev,
8880 &dev_attr_thinkpad_acpi_pdev_name);
8881 if (tpacpi_sensors_pdev)
8882 platform_device_unregister(tpacpi_sensors_pdev);
8883 if (tpacpi_pdev)
8884 platform_device_unregister(tpacpi_pdev);
8886 if (tp_features.sensors_pdrv_attrs_registered)
8887 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8888 if (tp_features.platform_drv_attrs_registered)
8889 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8891 if (tp_features.sensors_pdrv_registered)
8892 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8894 if (tp_features.platform_drv_registered)
8895 platform_driver_unregister(&tpacpi_pdriver);
8897 if (proc_dir)
8898 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8900 if (tpacpi_wq)
8901 destroy_workqueue(tpacpi_wq);
8903 kfree(thinkpad_id.bios_version_str);
8904 kfree(thinkpad_id.ec_version_str);
8905 kfree(thinkpad_id.model_str);
8909 static int __init thinkpad_acpi_module_init(void)
8911 int ret, i;
8913 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8915 /* Parameter checking */
8916 if (hotkey_report_mode > 2)
8917 return -EINVAL;
8919 /* Driver-level probe */
8921 ret = get_thinkpad_model_data(&thinkpad_id);
8922 if (ret) {
8923 printk(TPACPI_ERR
8924 "unable to get DMI data: %d\n", ret);
8925 thinkpad_acpi_module_exit();
8926 return ret;
8928 ret = probe_for_thinkpad();
8929 if (ret) {
8930 thinkpad_acpi_module_exit();
8931 return ret;
8934 /* Driver initialization */
8936 TPACPI_ACPIHANDLE_INIT(ecrd);
8937 TPACPI_ACPIHANDLE_INIT(ecwr);
8939 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8940 if (!tpacpi_wq) {
8941 thinkpad_acpi_module_exit();
8942 return -ENOMEM;
8945 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8946 if (!proc_dir) {
8947 printk(TPACPI_ERR
8948 "unable to create proc dir " TPACPI_PROC_DIR);
8949 thinkpad_acpi_module_exit();
8950 return -ENODEV;
8952 proc_dir->owner = THIS_MODULE;
8954 ret = platform_driver_register(&tpacpi_pdriver);
8955 if (ret) {
8956 printk(TPACPI_ERR
8957 "unable to register main platform driver\n");
8958 thinkpad_acpi_module_exit();
8959 return ret;
8961 tp_features.platform_drv_registered = 1;
8963 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8964 if (ret) {
8965 printk(TPACPI_ERR
8966 "unable to register hwmon platform driver\n");
8967 thinkpad_acpi_module_exit();
8968 return ret;
8970 tp_features.sensors_pdrv_registered = 1;
8972 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8973 if (!ret) {
8974 tp_features.platform_drv_attrs_registered = 1;
8975 ret = tpacpi_create_driver_attributes(
8976 &tpacpi_hwmon_pdriver.driver);
8978 if (ret) {
8979 printk(TPACPI_ERR
8980 "unable to create sysfs driver attributes\n");
8981 thinkpad_acpi_module_exit();
8982 return ret;
8984 tp_features.sensors_pdrv_attrs_registered = 1;
8987 /* Device initialization */
8988 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8989 NULL, 0);
8990 if (IS_ERR(tpacpi_pdev)) {
8991 ret = PTR_ERR(tpacpi_pdev);
8992 tpacpi_pdev = NULL;
8993 printk(TPACPI_ERR "unable to register platform device\n");
8994 thinkpad_acpi_module_exit();
8995 return ret;
8997 tpacpi_sensors_pdev = platform_device_register_simple(
8998 TPACPI_HWMON_DRVR_NAME,
8999 -1, NULL, 0);
9000 if (IS_ERR(tpacpi_sensors_pdev)) {
9001 ret = PTR_ERR(tpacpi_sensors_pdev);
9002 tpacpi_sensors_pdev = NULL;
9003 printk(TPACPI_ERR
9004 "unable to register hwmon platform device\n");
9005 thinkpad_acpi_module_exit();
9006 return ret;
9008 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9009 &dev_attr_thinkpad_acpi_pdev_name);
9010 if (ret) {
9011 printk(TPACPI_ERR
9012 "unable to create sysfs hwmon device attributes\n");
9013 thinkpad_acpi_module_exit();
9014 return ret;
9016 tp_features.sensors_pdev_attrs_registered = 1;
9017 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9018 if (IS_ERR(tpacpi_hwmon)) {
9019 ret = PTR_ERR(tpacpi_hwmon);
9020 tpacpi_hwmon = NULL;
9021 printk(TPACPI_ERR "unable to register hwmon device\n");
9022 thinkpad_acpi_module_exit();
9023 return ret;
9025 mutex_init(&tpacpi_inputdev_send_mutex);
9026 tpacpi_inputdev = input_allocate_device();
9027 if (!tpacpi_inputdev) {
9028 printk(TPACPI_ERR "unable to allocate input device\n");
9029 thinkpad_acpi_module_exit();
9030 return -ENOMEM;
9031 } else {
9032 /* Prepare input device, but don't register */
9033 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9034 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9035 tpacpi_inputdev->id.bustype = BUS_HOST;
9036 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9037 thinkpad_id.vendor :
9038 PCI_VENDOR_ID_IBM;
9039 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9040 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9041 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9043 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9044 ret = ibm_init(&ibms_init[i]);
9045 if (ret >= 0 && *ibms_init[i].param)
9046 ret = ibms_init[i].data->write(ibms_init[i].param);
9047 if (ret < 0) {
9048 thinkpad_acpi_module_exit();
9049 return ret;
9053 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9055 ret = input_register_device(tpacpi_inputdev);
9056 if (ret < 0) {
9057 printk(TPACPI_ERR "unable to register input device\n");
9058 thinkpad_acpi_module_exit();
9059 return ret;
9060 } else {
9061 tp_features.input_device_registered = 1;
9064 return 0;
9067 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9070 * This will autoload the driver in almost every ThinkPad
9071 * in widespread use.
9073 * Only _VERY_ old models, like the 240, 240x and 570 lack
9074 * the HKEY event interface.
9076 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9079 * DMI matching for module autoloading
9081 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9082 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9084 * Only models listed in thinkwiki will be supported, so add yours
9085 * if it is not there yet.
9087 #define IBM_BIOS_MODULE_ALIAS(__type) \
9088 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9090 /* Ancient thinkpad BIOSes have to be identified by
9091 * BIOS type or model number, and there are far less
9092 * BIOS types than model numbers... */
9093 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9095 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9096 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9097 MODULE_DESCRIPTION(TPACPI_DESC);
9098 MODULE_VERSION(TPACPI_VERSION);
9099 MODULE_LICENSE("GPL");
9101 module_init(thinkpad_acpi_module_init);
9102 module_exit(thinkpad_acpi_module_exit);