thinkpad-acpi: sync input device EV_SW initial state
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobd6890c138f78d47d33dad8cbb2422eddbcdd5a8e
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.23"
25 #define TPACPI_SYSFS_VERSION 0x020600
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/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /* HKEY events */
149 enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
182 /* Thermal events */
183 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
189 /* Misc */
190 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
193 /****************************************************************************
194 * Main driver
197 #define TPACPI_NAME "thinkpad"
198 #define TPACPI_DESC "ThinkPad ACPI Extras"
199 #define TPACPI_FILE TPACPI_NAME "_acpi"
200 #define TPACPI_URL "http://ibm-acpi.sf.net/"
201 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
203 #define TPACPI_PROC_DIR "ibm"
204 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
205 #define TPACPI_DRVR_NAME TPACPI_FILE
206 #define TPACPI_DRVR_SHORTNAME "tpacpi"
207 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
209 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
210 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
212 #define TPACPI_MAX_ACPI_ARGS 3
214 /* printk headers */
215 #define TPACPI_LOG TPACPI_FILE ": "
216 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
217 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
218 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
219 #define TPACPI_ERR KERN_ERR TPACPI_LOG
220 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
221 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
222 #define TPACPI_INFO KERN_INFO TPACPI_LOG
223 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
225 /* Debugging printk groups */
226 #define TPACPI_DBG_ALL 0xffff
227 #define TPACPI_DBG_DISCLOSETASK 0x8000
228 #define TPACPI_DBG_INIT 0x0001
229 #define TPACPI_DBG_EXIT 0x0002
230 #define TPACPI_DBG_RFKILL 0x0004
231 #define TPACPI_DBG_HKEY 0x0008
232 #define TPACPI_DBG_FAN 0x0010
233 #define TPACPI_DBG_BRGHT 0x0020
235 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
236 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
237 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
240 /****************************************************************************
241 * Driver-wide structs and misc. variables
244 struct ibm_struct;
246 struct tp_acpi_drv_struct {
247 const struct acpi_device_id *hid;
248 struct acpi_driver *driver;
250 void (*notify) (struct ibm_struct *, u32);
251 acpi_handle *handle;
252 u32 type;
253 struct acpi_device *device;
256 struct ibm_struct {
257 char *name;
259 int (*read) (char *);
260 int (*write) (char *);
261 void (*exit) (void);
262 void (*resume) (void);
263 void (*suspend) (pm_message_t state);
264 void (*shutdown) (void);
266 struct list_head all_drivers;
268 struct tp_acpi_drv_struct *acpi;
270 struct {
271 u8 acpi_driver_registered:1;
272 u8 acpi_notify_installed:1;
273 u8 proc_created:1;
274 u8 init_called:1;
275 u8 experimental:1;
276 } flags;
279 struct ibm_init_struct {
280 char param[32];
282 int (*init) (struct ibm_init_struct *);
283 struct ibm_struct *data;
286 static struct {
287 u32 bluetooth:1;
288 u32 hotkey:1;
289 u32 hotkey_mask:1;
290 u32 hotkey_wlsw:1;
291 u32 hotkey_tablet:1;
292 u32 light:1;
293 u32 light_status:1;
294 u32 bright_16levels:1;
295 u32 bright_acpimode:1;
296 u32 wan:1;
297 u32 uwb:1;
298 u32 fan_ctrl_status_undef:1;
299 u32 second_fan:1;
300 u32 beep_needs_two_args:1;
301 u32 input_device_registered:1;
302 u32 platform_drv_registered:1;
303 u32 platform_drv_attrs_registered:1;
304 u32 sensors_pdrv_registered:1;
305 u32 sensors_pdrv_attrs_registered:1;
306 u32 sensors_pdev_attrs_registered:1;
307 u32 hotkey_poll_active:1;
308 } tp_features;
310 static struct {
311 u16 hotkey_mask_ff:1;
312 } tp_warned;
314 struct thinkpad_id_data {
315 unsigned int vendor; /* ThinkPad vendor:
316 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
318 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
319 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
321 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
322 u16 ec_model;
323 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
324 u16 ec_release;
326 char *model_str; /* ThinkPad T43 */
327 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
329 static struct thinkpad_id_data thinkpad_id;
331 static enum {
332 TPACPI_LIFE_INIT = 0,
333 TPACPI_LIFE_RUNNING,
334 TPACPI_LIFE_EXITING,
335 } tpacpi_lifecycle;
337 static int experimental;
338 static u32 dbg_level;
340 static struct workqueue_struct *tpacpi_wq;
342 enum led_status_t {
343 TPACPI_LED_OFF = 0,
344 TPACPI_LED_ON,
345 TPACPI_LED_BLINK,
348 /* Special LED class that can defer work */
349 struct tpacpi_led_classdev {
350 struct led_classdev led_classdev;
351 struct work_struct work;
352 enum led_status_t new_state;
353 unsigned int led;
356 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
357 static int dbg_wlswemul;
358 static int tpacpi_wlsw_emulstate;
359 static int dbg_bluetoothemul;
360 static int tpacpi_bluetooth_emulstate;
361 static int dbg_wwanemul;
362 static int tpacpi_wwan_emulstate;
363 static int dbg_uwbemul;
364 static int tpacpi_uwb_emulstate;
365 #endif
368 /*************************************************************************
369 * Debugging helpers
372 #define dbg_printk(a_dbg_level, format, arg...) \
373 do { if (dbg_level & (a_dbg_level)) \
374 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
375 } while (0)
377 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
378 #define vdbg_printk dbg_printk
379 static const char *str_supported(int is_supported);
380 #else
381 #define vdbg_printk(a_dbg_level, format, arg...) \
382 do { } while (0)
383 #endif
385 static void tpacpi_log_usertask(const char * const what)
387 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
388 what, task_tgid_vnr(current));
391 #define tpacpi_disclose_usertask(what, format, arg...) \
392 do { \
393 if (unlikely( \
394 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
395 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
396 printk(TPACPI_DEBUG "%s: PID %d: " format, \
397 what, task_tgid_vnr(current), ## arg); \
399 } while (0)
402 * Quirk handling helpers
404 * ThinkPad IDs and versions seen in the field so far
405 * are two-characters from the set [0-9A-Z], i.e. base 36.
407 * We use values well outside that range as specials.
410 #define TPACPI_MATCH_ANY 0xffffU
411 #define TPACPI_MATCH_UNKNOWN 0U
413 /* TPID('1', 'Y') == 0x5931 */
414 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
416 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
417 { .vendor = PCI_VENDOR_ID_IBM, \
418 .bios = TPID(__id1, __id2), \
419 .ec = TPACPI_MATCH_ANY, \
420 .quirks = (__quirk) }
422 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
423 { .vendor = PCI_VENDOR_ID_LENOVO, \
424 .bios = TPID(__id1, __id2), \
425 .ec = TPACPI_MATCH_ANY, \
426 .quirks = (__quirk) }
428 struct tpacpi_quirk {
429 unsigned int vendor;
430 u16 bios;
431 u16 ec;
432 unsigned long quirks;
436 * tpacpi_check_quirks() - search BIOS/EC version on a list
437 * @qlist: array of &struct tpacpi_quirk
438 * @qlist_size: number of elements in @qlist
440 * Iterates over a quirks list until one is found that matches the
441 * ThinkPad's vendor, BIOS and EC model.
443 * Returns 0 if nothing matches, otherwise returns the quirks field of
444 * the matching &struct tpacpi_quirk entry.
446 * The match criteria is: vendor, ec and bios much match.
448 static unsigned long __init tpacpi_check_quirks(
449 const struct tpacpi_quirk *qlist,
450 unsigned int qlist_size)
452 while (qlist_size) {
453 if ((qlist->vendor == thinkpad_id.vendor ||
454 qlist->vendor == TPACPI_MATCH_ANY) &&
455 (qlist->bios == thinkpad_id.bios_model ||
456 qlist->bios == TPACPI_MATCH_ANY) &&
457 (qlist->ec == thinkpad_id.ec_model ||
458 qlist->ec == TPACPI_MATCH_ANY))
459 return qlist->quirks;
461 qlist_size--;
462 qlist++;
464 return 0;
468 /****************************************************************************
469 ****************************************************************************
471 * ACPI Helpers and device model
473 ****************************************************************************
474 ****************************************************************************/
476 /*************************************************************************
477 * ACPI basic handles
480 static acpi_handle root_handle;
482 #define TPACPI_HANDLE(object, parent, paths...) \
483 static acpi_handle object##_handle; \
484 static acpi_handle *object##_parent = &parent##_handle; \
485 static char *object##_path; \
486 static char *object##_paths[] = { paths }
488 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
489 "\\_SB.PCI.ISA.EC", /* 570 */
490 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
491 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
492 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
493 "\\_SB.PCI0.ICH3.EC0", /* R31 */
494 "\\_SB.PCI0.LPC.EC", /* all others */
497 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
498 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
500 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
501 /* T4x, X31, X40 */
502 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
503 "\\CMS", /* R40, R40e */
504 ); /* all others */
506 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
507 "^HKEY", /* R30, R31 */
508 "HKEY", /* all others */
509 ); /* 570 */
511 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
512 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
513 "\\_SB.PCI0.VID0", /* 770e */
514 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
515 "\\_SB.PCI0.AGP.VID", /* all others */
516 ); /* R30, R31 */
519 /*************************************************************************
520 * ACPI helpers
523 static int acpi_evalf(acpi_handle handle,
524 void *res, char *method, char *fmt, ...)
526 char *fmt0 = fmt;
527 struct acpi_object_list params;
528 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
529 struct acpi_buffer result, *resultp;
530 union acpi_object out_obj;
531 acpi_status status;
532 va_list ap;
533 char res_type;
534 int success;
535 int quiet;
537 if (!*fmt) {
538 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
539 return 0;
542 if (*fmt == 'q') {
543 quiet = 1;
544 fmt++;
545 } else
546 quiet = 0;
548 res_type = *(fmt++);
550 params.count = 0;
551 params.pointer = &in_objs[0];
553 va_start(ap, fmt);
554 while (*fmt) {
555 char c = *(fmt++);
556 switch (c) {
557 case 'd': /* int */
558 in_objs[params.count].integer.value = va_arg(ap, int);
559 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
560 break;
561 /* add more types as needed */
562 default:
563 printk(TPACPI_ERR "acpi_evalf() called "
564 "with invalid format character '%c'\n", c);
565 return 0;
568 va_end(ap);
570 if (res_type != 'v') {
571 result.length = sizeof(out_obj);
572 result.pointer = &out_obj;
573 resultp = &result;
574 } else
575 resultp = NULL;
577 status = acpi_evaluate_object(handle, method, &params, resultp);
579 switch (res_type) {
580 case 'd': /* int */
581 if (res)
582 *(int *)res = out_obj.integer.value;
583 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
584 break;
585 case 'v': /* void */
586 success = status == AE_OK;
587 break;
588 /* add more types as needed */
589 default:
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", res_type);
592 return 0;
595 if (!success && !quiet)
596 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
597 method, fmt0, status);
599 return success;
602 static int acpi_ec_read(int i, u8 *p)
604 int v;
606 if (ecrd_handle) {
607 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
608 return 0;
609 *p = v;
610 } else {
611 if (ec_read(i, p) < 0)
612 return 0;
615 return 1;
618 static int acpi_ec_write(int i, u8 v)
620 if (ecwr_handle) {
621 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
622 return 0;
623 } else {
624 if (ec_write(i, v) < 0)
625 return 0;
628 return 1;
631 static int issue_thinkpad_cmos_command(int cmos_cmd)
633 if (!cmos_handle)
634 return -ENXIO;
636 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
637 return -EIO;
639 return 0;
642 /*************************************************************************
643 * ACPI device model
646 #define TPACPI_ACPIHANDLE_INIT(object) \
647 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
648 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
650 static void drv_acpi_handle_init(char *name,
651 acpi_handle *handle, acpi_handle parent,
652 char **paths, int num_paths, char **path)
654 int i;
655 acpi_status status;
657 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
658 name);
660 for (i = 0; i < num_paths; i++) {
661 status = acpi_get_handle(parent, paths[i], handle);
662 if (ACPI_SUCCESS(status)) {
663 *path = paths[i];
664 dbg_printk(TPACPI_DBG_INIT,
665 "Found ACPI handle %s for %s\n",
666 *path, name);
667 return;
671 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
672 name);
673 *handle = NULL;
676 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
678 struct ibm_struct *ibm = data;
680 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
681 return;
683 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
684 return;
686 ibm->acpi->notify(ibm, event);
689 static int __init setup_acpi_notify(struct ibm_struct *ibm)
691 acpi_status status;
692 int rc;
694 BUG_ON(!ibm->acpi);
696 if (!*ibm->acpi->handle)
697 return 0;
699 vdbg_printk(TPACPI_DBG_INIT,
700 "setting up ACPI notify for %s\n", ibm->name);
702 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
703 if (rc < 0) {
704 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
705 ibm->name, rc);
706 return -ENODEV;
709 ibm->acpi->device->driver_data = ibm;
710 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
711 TPACPI_ACPI_EVENT_PREFIX,
712 ibm->name);
714 status = acpi_install_notify_handler(*ibm->acpi->handle,
715 ibm->acpi->type, dispatch_acpi_notify, ibm);
716 if (ACPI_FAILURE(status)) {
717 if (status == AE_ALREADY_EXISTS) {
718 printk(TPACPI_NOTICE
719 "another device driver is already "
720 "handling %s events\n", ibm->name);
721 } else {
722 printk(TPACPI_ERR
723 "acpi_install_notify_handler(%s) failed: %d\n",
724 ibm->name, status);
726 return -ENODEV;
728 ibm->flags.acpi_notify_installed = 1;
729 return 0;
732 static int __init tpacpi_device_add(struct acpi_device *device)
734 return 0;
737 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
739 int rc;
741 dbg_printk(TPACPI_DBG_INIT,
742 "registering %s as an ACPI driver\n", ibm->name);
744 BUG_ON(!ibm->acpi);
746 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
747 if (!ibm->acpi->driver) {
748 printk(TPACPI_ERR
749 "failed to allocate memory for ibm->acpi->driver\n");
750 return -ENOMEM;
753 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
754 ibm->acpi->driver->ids = ibm->acpi->hid;
756 ibm->acpi->driver->ops.add = &tpacpi_device_add;
758 rc = acpi_bus_register_driver(ibm->acpi->driver);
759 if (rc < 0) {
760 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
761 ibm->name, rc);
762 kfree(ibm->acpi->driver);
763 ibm->acpi->driver = NULL;
764 } else if (!rc)
765 ibm->flags.acpi_driver_registered = 1;
767 return rc;
771 /****************************************************************************
772 ****************************************************************************
774 * Procfs Helpers
776 ****************************************************************************
777 ****************************************************************************/
779 static int dispatch_procfs_read(char *page, char **start, off_t off,
780 int count, int *eof, void *data)
782 struct ibm_struct *ibm = data;
783 int len;
785 if (!ibm || !ibm->read)
786 return -EINVAL;
788 len = ibm->read(page);
789 if (len < 0)
790 return len;
792 if (len <= off + count)
793 *eof = 1;
794 *start = page + off;
795 len -= off;
796 if (len > count)
797 len = count;
798 if (len < 0)
799 len = 0;
801 return len;
804 static int dispatch_procfs_write(struct file *file,
805 const char __user *userbuf,
806 unsigned long count, void *data)
808 struct ibm_struct *ibm = data;
809 char *kernbuf;
810 int ret;
812 if (!ibm || !ibm->write)
813 return -EINVAL;
814 if (count > PAGE_SIZE - 2)
815 return -EINVAL;
817 kernbuf = kmalloc(count + 2, GFP_KERNEL);
818 if (!kernbuf)
819 return -ENOMEM;
821 if (copy_from_user(kernbuf, userbuf, count)) {
822 kfree(kernbuf);
823 return -EFAULT;
826 kernbuf[count] = 0;
827 strcat(kernbuf, ",");
828 ret = ibm->write(kernbuf);
829 if (ret == 0)
830 ret = count;
832 kfree(kernbuf);
834 return ret;
837 static char *next_cmd(char **cmds)
839 char *start = *cmds;
840 char *end;
842 while ((end = strchr(start, ',')) && end == start)
843 start = end + 1;
845 if (!end)
846 return NULL;
848 *end = 0;
849 *cmds = end + 1;
850 return start;
854 /****************************************************************************
855 ****************************************************************************
857 * Device model: input, hwmon and platform
859 ****************************************************************************
860 ****************************************************************************/
862 static struct platform_device *tpacpi_pdev;
863 static struct platform_device *tpacpi_sensors_pdev;
864 static struct device *tpacpi_hwmon;
865 static struct input_dev *tpacpi_inputdev;
866 static struct mutex tpacpi_inputdev_send_mutex;
867 static LIST_HEAD(tpacpi_all_drivers);
869 static int tpacpi_suspend_handler(struct platform_device *pdev,
870 pm_message_t state)
872 struct ibm_struct *ibm, *itmp;
874 list_for_each_entry_safe(ibm, itmp,
875 &tpacpi_all_drivers,
876 all_drivers) {
877 if (ibm->suspend)
878 (ibm->suspend)(state);
881 return 0;
884 static int tpacpi_resume_handler(struct platform_device *pdev)
886 struct ibm_struct *ibm, *itmp;
888 list_for_each_entry_safe(ibm, itmp,
889 &tpacpi_all_drivers,
890 all_drivers) {
891 if (ibm->resume)
892 (ibm->resume)();
895 return 0;
898 static void tpacpi_shutdown_handler(struct platform_device *pdev)
900 struct ibm_struct *ibm, *itmp;
902 list_for_each_entry_safe(ibm, itmp,
903 &tpacpi_all_drivers,
904 all_drivers) {
905 if (ibm->shutdown)
906 (ibm->shutdown)();
910 static struct platform_driver tpacpi_pdriver = {
911 .driver = {
912 .name = TPACPI_DRVR_NAME,
913 .owner = THIS_MODULE,
915 .suspend = tpacpi_suspend_handler,
916 .resume = tpacpi_resume_handler,
917 .shutdown = tpacpi_shutdown_handler,
920 static struct platform_driver tpacpi_hwmon_pdriver = {
921 .driver = {
922 .name = TPACPI_HWMON_DRVR_NAME,
923 .owner = THIS_MODULE,
927 /*************************************************************************
928 * sysfs support helpers
931 struct attribute_set {
932 unsigned int members, max_members;
933 struct attribute_group group;
936 struct attribute_set_obj {
937 struct attribute_set s;
938 struct attribute *a;
939 } __attribute__((packed));
941 static struct attribute_set *create_attr_set(unsigned int max_members,
942 const char *name)
944 struct attribute_set_obj *sobj;
946 if (max_members == 0)
947 return NULL;
949 /* Allocates space for implicit NULL at the end too */
950 sobj = kzalloc(sizeof(struct attribute_set_obj) +
951 max_members * sizeof(struct attribute *),
952 GFP_KERNEL);
953 if (!sobj)
954 return NULL;
955 sobj->s.max_members = max_members;
956 sobj->s.group.attrs = &sobj->a;
957 sobj->s.group.name = name;
959 return &sobj->s;
962 #define destroy_attr_set(_set) \
963 kfree(_set);
965 /* not multi-threaded safe, use it in a single thread per set */
966 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
968 if (!s || !attr)
969 return -EINVAL;
971 if (s->members >= s->max_members)
972 return -ENOMEM;
974 s->group.attrs[s->members] = attr;
975 s->members++;
977 return 0;
980 static int add_many_to_attr_set(struct attribute_set *s,
981 struct attribute **attr,
982 unsigned int count)
984 int i, res;
986 for (i = 0; i < count; i++) {
987 res = add_to_attr_set(s, attr[i]);
988 if (res)
989 return res;
992 return 0;
995 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
997 sysfs_remove_group(kobj, &s->group);
998 destroy_attr_set(s);
1001 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1002 sysfs_create_group(_kobj, &_attr_set->group)
1004 static int parse_strtoul(const char *buf,
1005 unsigned long max, unsigned long *value)
1007 char *endp;
1009 while (*buf && isspace(*buf))
1010 buf++;
1011 *value = simple_strtoul(buf, &endp, 0);
1012 while (*endp && isspace(*endp))
1013 endp++;
1014 if (*endp || *value > max)
1015 return -EINVAL;
1017 return 0;
1020 static void tpacpi_disable_brightness_delay(void)
1022 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1023 printk(TPACPI_NOTICE
1024 "ACPI backlight control delay disabled\n");
1027 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1029 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1030 union acpi_object *obj;
1031 int rc;
1033 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1034 obj = (union acpi_object *)buffer.pointer;
1035 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1036 printk(TPACPI_ERR "Unknown _BCL data, "
1037 "please report this to %s\n", TPACPI_MAIL);
1038 rc = 0;
1039 } else {
1040 rc = obj->package.count;
1042 } else {
1043 return 0;
1046 kfree(buffer.pointer);
1047 return rc;
1050 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1051 u32 lvl, void *context, void **rv)
1053 char name[ACPI_PATH_SEGMENT_LENGTH];
1054 struct acpi_buffer buffer = { sizeof(name), &name };
1056 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1057 !strncmp("_BCL", name, sizeof(name) - 1)) {
1058 BUG_ON(!rv || !*rv);
1059 **(int **)rv = tpacpi_query_bcl_levels(handle);
1060 return AE_CTRL_TERMINATE;
1061 } else {
1062 return AE_OK;
1067 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1069 static int __init tpacpi_check_std_acpi_brightness_support(void)
1071 int status;
1072 int bcl_levels = 0;
1073 void *bcl_ptr = &bcl_levels;
1075 if (!vid_handle) {
1076 TPACPI_ACPIHANDLE_INIT(vid);
1078 if (!vid_handle)
1079 return 0;
1082 * Search for a _BCL method, and execute it. This is safe on all
1083 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1084 * BIOS in ACPI backlight control mode. We do NOT have to care
1085 * about calling the _BCL method in an enabled video device, any
1086 * will do for our purposes.
1089 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1090 tpacpi_acpi_walk_find_bcl, NULL,
1091 &bcl_ptr);
1093 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1094 tp_features.bright_acpimode = 1;
1095 return (bcl_levels - 2);
1098 return 0;
1101 static void printk_deprecated_attribute(const char * const what,
1102 const char * const details)
1104 tpacpi_log_usertask("deprecated sysfs attribute");
1105 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1106 "will be removed. %s\n",
1107 what, details);
1110 /*************************************************************************
1111 * rfkill and radio control support helpers
1115 * ThinkPad-ACPI firmware handling model:
1117 * WLSW (master wireless switch) is event-driven, and is common to all
1118 * firmware-controlled radios. It cannot be controlled, just monitored,
1119 * as expected. It overrides all radio state in firmware
1121 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1122 * (TODO: verify how WLSW interacts with the returned radio state).
1124 * The only time there are shadow radio state changes, is when
1125 * masked-off hotkeys are used.
1129 * Internal driver API for radio state:
1131 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1132 * bool: true means radio blocked (off)
1134 enum tpacpi_rfkill_state {
1135 TPACPI_RFK_RADIO_OFF = 0,
1136 TPACPI_RFK_RADIO_ON
1139 /* rfkill switches */
1140 enum tpacpi_rfk_id {
1141 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1142 TPACPI_RFK_WWAN_SW_ID,
1143 TPACPI_RFK_UWB_SW_ID,
1144 TPACPI_RFK_SW_MAX
1147 static const char *tpacpi_rfkill_names[] = {
1148 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1149 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1150 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1151 [TPACPI_RFK_SW_MAX] = NULL
1154 /* ThinkPad-ACPI rfkill subdriver */
1155 struct tpacpi_rfk {
1156 struct rfkill *rfkill;
1157 enum tpacpi_rfk_id id;
1158 const struct tpacpi_rfk_ops *ops;
1161 struct tpacpi_rfk_ops {
1162 /* firmware interface */
1163 int (*get_status)(void);
1164 int (*set_status)(const enum tpacpi_rfkill_state);
1167 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1169 /* Query FW and update rfkill sw state for a given rfkill switch */
1170 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1172 int status;
1174 if (!tp_rfk)
1175 return -ENODEV;
1177 status = (tp_rfk->ops->get_status)();
1178 if (status < 0)
1179 return status;
1181 rfkill_set_sw_state(tp_rfk->rfkill,
1182 (status == TPACPI_RFK_RADIO_OFF));
1184 return status;
1187 /* Query FW and update rfkill sw state for all rfkill switches */
1188 static void tpacpi_rfk_update_swstate_all(void)
1190 unsigned int i;
1192 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1193 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1197 * Sync the HW-blocking state of all rfkill switches,
1198 * do notice it causes the rfkill core to schedule uevents
1200 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1202 unsigned int i;
1203 struct tpacpi_rfk *tp_rfk;
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1206 tp_rfk = tpacpi_rfkill_switches[i];
1207 if (tp_rfk) {
1208 if (rfkill_set_hw_state(tp_rfk->rfkill,
1209 blocked)) {
1210 /* ignore -- we track sw block */
1216 /* Call to get the WLSW state from the firmware */
1217 static int hotkey_get_wlsw(void);
1219 /* Call to query WLSW state and update all rfkill switches */
1220 static bool tpacpi_rfk_check_hwblock_state(void)
1222 int res = hotkey_get_wlsw();
1223 int hw_blocked;
1225 /* When unknown or unsupported, we have to assume it is unblocked */
1226 if (res < 0)
1227 return false;
1229 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1230 tpacpi_rfk_update_hwblock_state(hw_blocked);
1232 return hw_blocked;
1235 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1237 struct tpacpi_rfk *tp_rfk = data;
1238 int res;
1240 dbg_printk(TPACPI_DBG_RFKILL,
1241 "request to change radio state to %s\n",
1242 blocked ? "blocked" : "unblocked");
1244 /* try to set radio state */
1245 res = (tp_rfk->ops->set_status)(blocked ?
1246 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1248 /* and update the rfkill core with whatever the FW really did */
1249 tpacpi_rfk_update_swstate(tp_rfk);
1251 return (res < 0) ? res : 0;
1254 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1255 .set_block = tpacpi_rfk_hook_set_block,
1258 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1259 const struct tpacpi_rfk_ops *tp_rfkops,
1260 const enum rfkill_type rfktype,
1261 const char *name,
1262 const bool set_default)
1264 struct tpacpi_rfk *atp_rfk;
1265 int res;
1266 bool sw_state = false;
1267 int sw_status;
1269 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1271 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1272 if (atp_rfk)
1273 atp_rfk->rfkill = rfkill_alloc(name,
1274 &tpacpi_pdev->dev,
1275 rfktype,
1276 &tpacpi_rfk_rfkill_ops,
1277 atp_rfk);
1278 if (!atp_rfk || !atp_rfk->rfkill) {
1279 printk(TPACPI_ERR
1280 "failed to allocate memory for rfkill class\n");
1281 kfree(atp_rfk);
1282 return -ENOMEM;
1285 atp_rfk->id = id;
1286 atp_rfk->ops = tp_rfkops;
1288 sw_status = (tp_rfkops->get_status)();
1289 if (sw_status < 0) {
1290 printk(TPACPI_ERR
1291 "failed to read initial state for %s, error %d\n",
1292 name, sw_status);
1293 } else {
1294 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1295 if (set_default) {
1296 /* try to keep the initial state, since we ask the
1297 * firmware to preserve it across S5 in NVRAM */
1298 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1301 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
1303 res = rfkill_register(atp_rfk->rfkill);
1304 if (res < 0) {
1305 printk(TPACPI_ERR
1306 "failed to register %s rfkill switch: %d\n",
1307 name, res);
1308 rfkill_destroy(atp_rfk->rfkill);
1309 kfree(atp_rfk);
1310 return res;
1313 tpacpi_rfkill_switches[id] = atp_rfk;
1314 return 0;
1317 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1319 struct tpacpi_rfk *tp_rfk;
1321 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1323 tp_rfk = tpacpi_rfkill_switches[id];
1324 if (tp_rfk) {
1325 rfkill_unregister(tp_rfk->rfkill);
1326 rfkill_destroy(tp_rfk->rfkill);
1327 tpacpi_rfkill_switches[id] = NULL;
1328 kfree(tp_rfk);
1332 static void printk_deprecated_rfkill_attribute(const char * const what)
1334 printk_deprecated_attribute(what,
1335 "Please switch to generic rfkill before year 2010");
1338 /* sysfs <radio> enable ------------------------------------------------ */
1339 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1340 struct device_attribute *attr,
1341 char *buf)
1343 int status;
1345 printk_deprecated_rfkill_attribute(attr->attr.name);
1347 /* This is in the ABI... */
1348 if (tpacpi_rfk_check_hwblock_state()) {
1349 status = TPACPI_RFK_RADIO_OFF;
1350 } else {
1351 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1352 if (status < 0)
1353 return status;
1356 return snprintf(buf, PAGE_SIZE, "%d\n",
1357 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1360 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1361 struct device_attribute *attr,
1362 const char *buf, size_t count)
1364 unsigned long t;
1365 int res;
1367 printk_deprecated_rfkill_attribute(attr->attr.name);
1369 if (parse_strtoul(buf, 1, &t))
1370 return -EINVAL;
1372 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state() && !!t)
1376 return -EPERM;
1378 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1379 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1380 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1382 return (res < 0) ? res : count;
1385 /* procfs -------------------------------------------------------------- */
1386 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1388 int len = 0;
1390 if (id >= TPACPI_RFK_SW_MAX)
1391 len += sprintf(p + len, "status:\t\tnot supported\n");
1392 else {
1393 int status;
1395 /* This is in the ABI... */
1396 if (tpacpi_rfk_check_hwblock_state()) {
1397 status = TPACPI_RFK_RADIO_OFF;
1398 } else {
1399 status = tpacpi_rfk_update_swstate(
1400 tpacpi_rfkill_switches[id]);
1401 if (status < 0)
1402 return status;
1405 len += sprintf(p + len, "status:\t\t%s\n",
1406 (status == TPACPI_RFK_RADIO_ON) ?
1407 "enabled" : "disabled");
1408 len += sprintf(p + len, "commands:\tenable, disable\n");
1411 return len;
1414 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1416 char *cmd;
1417 int status = -1;
1418 int res = 0;
1420 if (id >= TPACPI_RFK_SW_MAX)
1421 return -ENODEV;
1423 while ((cmd = next_cmd(&buf))) {
1424 if (strlencmp(cmd, "enable") == 0)
1425 status = TPACPI_RFK_RADIO_ON;
1426 else if (strlencmp(cmd, "disable") == 0)
1427 status = TPACPI_RFK_RADIO_OFF;
1428 else
1429 return -EINVAL;
1432 if (status != -1) {
1433 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1434 (status == TPACPI_RFK_RADIO_ON) ?
1435 "enable" : "disable",
1436 tpacpi_rfkill_names[id]);
1437 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1438 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1441 return res;
1444 /*************************************************************************
1445 * thinkpad-acpi driver attributes
1448 /* interface_version --------------------------------------------------- */
1449 static ssize_t tpacpi_driver_interface_version_show(
1450 struct device_driver *drv,
1451 char *buf)
1453 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1456 static DRIVER_ATTR(interface_version, S_IRUGO,
1457 tpacpi_driver_interface_version_show, NULL);
1459 /* debug_level --------------------------------------------------------- */
1460 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1461 char *buf)
1463 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1466 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1467 const char *buf, size_t count)
1469 unsigned long t;
1471 if (parse_strtoul(buf, 0xffff, &t))
1472 return -EINVAL;
1474 dbg_level = t;
1476 return count;
1479 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1480 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1482 /* version ------------------------------------------------------------- */
1483 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1484 char *buf)
1486 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1487 TPACPI_DESC, TPACPI_VERSION);
1490 static DRIVER_ATTR(version, S_IRUGO,
1491 tpacpi_driver_version_show, NULL);
1493 /* --------------------------------------------------------------------- */
1495 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1497 /* wlsw_emulstate ------------------------------------------------------ */
1498 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1499 char *buf)
1501 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1504 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1505 const char *buf, size_t count)
1507 unsigned long t;
1509 if (parse_strtoul(buf, 1, &t))
1510 return -EINVAL;
1512 if (tpacpi_wlsw_emulstate != !!t) {
1513 tpacpi_wlsw_emulstate = !!t;
1514 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1517 return count;
1520 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1521 tpacpi_driver_wlsw_emulstate_show,
1522 tpacpi_driver_wlsw_emulstate_store);
1524 /* bluetooth_emulstate ------------------------------------------------- */
1525 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1526 struct device_driver *drv,
1527 char *buf)
1529 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1532 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1533 struct device_driver *drv,
1534 const char *buf, size_t count)
1536 unsigned long t;
1538 if (parse_strtoul(buf, 1, &t))
1539 return -EINVAL;
1541 tpacpi_bluetooth_emulstate = !!t;
1543 return count;
1546 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1547 tpacpi_driver_bluetooth_emulstate_show,
1548 tpacpi_driver_bluetooth_emulstate_store);
1550 /* wwan_emulstate ------------------------------------------------- */
1551 static ssize_t tpacpi_driver_wwan_emulstate_show(
1552 struct device_driver *drv,
1553 char *buf)
1555 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1558 static ssize_t tpacpi_driver_wwan_emulstate_store(
1559 struct device_driver *drv,
1560 const char *buf, size_t count)
1562 unsigned long t;
1564 if (parse_strtoul(buf, 1, &t))
1565 return -EINVAL;
1567 tpacpi_wwan_emulstate = !!t;
1569 return count;
1572 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1573 tpacpi_driver_wwan_emulstate_show,
1574 tpacpi_driver_wwan_emulstate_store);
1576 /* uwb_emulstate ------------------------------------------------- */
1577 static ssize_t tpacpi_driver_uwb_emulstate_show(
1578 struct device_driver *drv,
1579 char *buf)
1581 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1584 static ssize_t tpacpi_driver_uwb_emulstate_store(
1585 struct device_driver *drv,
1586 const char *buf, size_t count)
1588 unsigned long t;
1590 if (parse_strtoul(buf, 1, &t))
1591 return -EINVAL;
1593 tpacpi_uwb_emulstate = !!t;
1595 return count;
1598 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1599 tpacpi_driver_uwb_emulstate_show,
1600 tpacpi_driver_uwb_emulstate_store);
1601 #endif
1603 /* --------------------------------------------------------------------- */
1605 static struct driver_attribute *tpacpi_driver_attributes[] = {
1606 &driver_attr_debug_level, &driver_attr_version,
1607 &driver_attr_interface_version,
1610 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1612 int i, res;
1614 i = 0;
1615 res = 0;
1616 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1617 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1618 i++;
1621 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1622 if (!res && dbg_wlswemul)
1623 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1624 if (!res && dbg_bluetoothemul)
1625 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1626 if (!res && dbg_wwanemul)
1627 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1628 if (!res && dbg_uwbemul)
1629 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1630 #endif
1632 return res;
1635 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1637 int i;
1639 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1640 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1642 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1643 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1644 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1645 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1646 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1647 #endif
1650 /*************************************************************************
1651 * Firmware Data
1655 * Table of recommended minimum BIOS versions
1657 * Reasons for listing:
1658 * 1. Stable BIOS, listed because the unknown ammount of
1659 * bugs and bad ACPI behaviour on older versions
1661 * 2. BIOS or EC fw with known bugs that trigger on Linux
1663 * 3. BIOS with known reduced functionality in older versions
1665 * We recommend the latest BIOS and EC version.
1666 * We only support the latest BIOS and EC fw version as a rule.
1668 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1669 * Information from users in ThinkWiki
1671 * WARNING: we use this table also to detect that the machine is
1672 * a ThinkPad in some cases, so don't remove entries lightly.
1675 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__id1, __id2), \
1678 .ec = TPACPI_MATCH_ANY, \
1679 .quirks = TPACPI_MATCH_ANY << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1682 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1683 __eid, __ev1, __ev2) \
1684 { .vendor = (__v), \
1685 .bios = TPID(__bid1, __bid2), \
1686 .ec = __eid, \
1687 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1688 | (__bv1) << 8 | (__bv2) }
1690 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1691 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1693 /* Outdated IBM BIOSes often lack the EC id string */
1694 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1696 __bv1, __bv2, TPID(__id1, __id2), \
1697 __ev1, __ev2), \
1698 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1699 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1700 __ev1, __ev2)
1702 /* Outdated IBM BIOSes often lack the EC id string */
1703 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1704 __eid1, __eid2, __ev1, __ev2) \
1705 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1706 __bv1, __bv2, TPID(__eid1, __eid2), \
1707 __ev1, __ev2), \
1708 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1709 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1710 __ev1, __ev2)
1712 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1713 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1715 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1716 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1717 __bv1, __bv2, TPID(__id1, __id2), \
1718 __ev1, __ev2)
1720 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1721 __eid1, __eid2, __ev1, __ev2) \
1722 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1723 __bv1, __bv2, TPID(__eid1, __eid2), \
1724 __ev1, __ev2)
1726 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1727 /* Numeric models ------------------ */
1728 /* FW MODEL BIOS VERS */
1729 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1730 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1731 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1732 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1733 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1734 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1735 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1736 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1737 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1739 /* A-series ------------------------- */
1740 /* FW MODEL BIOS VERS EC VERS */
1741 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1742 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1743 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1744 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1745 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1746 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1747 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1748 TPV_QI0('1', '3', '2', '0'), /* A22m */
1749 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1750 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1751 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1753 /* G-series ------------------------- */
1754 /* FW MODEL BIOS VERS */
1755 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1756 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1758 /* R-series, T-series --------------- */
1759 /* FW MODEL BIOS VERS EC VERS */
1760 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1761 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1762 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1763 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1764 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1765 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1766 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1767 T40/p, T41/p, T42/p (1) */
1768 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1769 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1770 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1771 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1773 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1774 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1775 TPV_QI0('1', '6', '3', '2'), /* T22 */
1776 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1777 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1778 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1780 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1781 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1782 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1784 /* BIOS FW BIOS VERS EC FW EC VERS */
1785 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1786 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1788 /* X-series ------------------------- */
1789 /* FW MODEL BIOS VERS EC VERS */
1790 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1791 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1792 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1793 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1794 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1795 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1796 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1798 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1799 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1801 /* (0) - older versions lack DMI EC fw string and functionality */
1802 /* (1) - older versions known to lack functionality */
1805 #undef TPV_QL1
1806 #undef TPV_QL0
1807 #undef TPV_QI2
1808 #undef TPV_QI1
1809 #undef TPV_QI0
1810 #undef TPV_Q_X
1811 #undef TPV_Q
1813 static void __init tpacpi_check_outdated_fw(void)
1815 unsigned long fwvers;
1816 u16 ec_version, bios_version;
1818 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1819 ARRAY_SIZE(tpacpi_bios_version_qtable));
1821 if (!fwvers)
1822 return;
1824 bios_version = fwvers & 0xffffU;
1825 ec_version = (fwvers >> 16) & 0xffffU;
1827 /* note that unknown versions are set to 0x0000 and we use that */
1828 if ((bios_version > thinkpad_id.bios_release) ||
1829 (ec_version > thinkpad_id.ec_release &&
1830 ec_version != TPACPI_MATCH_ANY)) {
1832 * The changelogs would let us track down the exact
1833 * reason, but it is just too much of a pain to track
1834 * it. We only list BIOSes that are either really
1835 * broken, or really stable to begin with, so it is
1836 * best if the user upgrades the firmware anyway.
1838 printk(TPACPI_WARN
1839 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1840 printk(TPACPI_WARN
1841 "WARNING: This firmware may be missing critical bug "
1842 "fixes and/or important features\n");
1846 static bool __init tpacpi_is_fw_known(void)
1848 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1849 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1852 /****************************************************************************
1853 ****************************************************************************
1855 * Subdrivers
1857 ****************************************************************************
1858 ****************************************************************************/
1860 /*************************************************************************
1861 * thinkpad-acpi init subdriver
1864 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1866 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1867 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1869 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1870 (thinkpad_id.bios_version_str) ?
1871 thinkpad_id.bios_version_str : "unknown",
1872 (thinkpad_id.ec_version_str) ?
1873 thinkpad_id.ec_version_str : "unknown");
1875 if (thinkpad_id.vendor && thinkpad_id.model_str)
1876 printk(TPACPI_INFO "%s %s, model %s\n",
1877 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1878 "IBM" : ((thinkpad_id.vendor ==
1879 PCI_VENDOR_ID_LENOVO) ?
1880 "Lenovo" : "Unknown vendor"),
1881 thinkpad_id.model_str,
1882 (thinkpad_id.nummodel_str) ?
1883 thinkpad_id.nummodel_str : "unknown");
1885 tpacpi_check_outdated_fw();
1886 return 0;
1889 static int thinkpad_acpi_driver_read(char *p)
1891 int len = 0;
1893 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1894 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1896 return len;
1899 static struct ibm_struct thinkpad_acpi_driver_data = {
1900 .name = "driver",
1901 .read = thinkpad_acpi_driver_read,
1904 /*************************************************************************
1905 * Hotkey subdriver
1909 * ThinkPad firmware event model
1911 * The ThinkPad firmware has two main event interfaces: normal ACPI
1912 * notifications (which follow the ACPI standard), and a private event
1913 * interface.
1915 * The private event interface also issues events for the hotkeys. As
1916 * the driver gained features, the event handling code ended up being
1917 * built around the hotkey subdriver. This will need to be refactored
1918 * to a more formal event API eventually.
1920 * Some "hotkeys" are actually supposed to be used as event reports,
1921 * such as "brightness has changed", "volume has changed", depending on
1922 * the ThinkPad model and how the firmware is operating.
1924 * Unlike other classes, hotkey-class events have mask/unmask control on
1925 * non-ancient firmware. However, how it behaves changes a lot with the
1926 * firmware model and version.
1929 enum { /* hot key scan codes (derived from ACPI DSDT) */
1930 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1931 TP_ACPI_HOTKEYSCAN_FNF2,
1932 TP_ACPI_HOTKEYSCAN_FNF3,
1933 TP_ACPI_HOTKEYSCAN_FNF4,
1934 TP_ACPI_HOTKEYSCAN_FNF5,
1935 TP_ACPI_HOTKEYSCAN_FNF6,
1936 TP_ACPI_HOTKEYSCAN_FNF7,
1937 TP_ACPI_HOTKEYSCAN_FNF8,
1938 TP_ACPI_HOTKEYSCAN_FNF9,
1939 TP_ACPI_HOTKEYSCAN_FNF10,
1940 TP_ACPI_HOTKEYSCAN_FNF11,
1941 TP_ACPI_HOTKEYSCAN_FNF12,
1942 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1943 TP_ACPI_HOTKEYSCAN_FNINSERT,
1944 TP_ACPI_HOTKEYSCAN_FNDELETE,
1945 TP_ACPI_HOTKEYSCAN_FNHOME,
1946 TP_ACPI_HOTKEYSCAN_FNEND,
1947 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1948 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1949 TP_ACPI_HOTKEYSCAN_FNSPACE,
1950 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1951 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1952 TP_ACPI_HOTKEYSCAN_MUTE,
1953 TP_ACPI_HOTKEYSCAN_THINKPAD,
1956 enum { /* Keys/events available through NVRAM polling */
1957 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1958 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1961 enum { /* Positions of some of the keys in hotkey masks */
1962 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1963 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1964 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1965 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1966 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1967 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1968 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1969 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1970 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1971 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1972 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1975 enum { /* NVRAM to ACPI HKEY group map */
1976 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1977 TP_ACPI_HKEY_ZOOM_MASK |
1978 TP_ACPI_HKEY_DISPSWTCH_MASK |
1979 TP_ACPI_HKEY_HIBERNATE_MASK,
1980 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1981 TP_ACPI_HKEY_BRGHTDWN_MASK,
1982 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1983 TP_ACPI_HKEY_VOLDWN_MASK |
1984 TP_ACPI_HKEY_MUTE_MASK,
1987 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1988 struct tp_nvram_state {
1989 u16 thinkpad_toggle:1;
1990 u16 zoom_toggle:1;
1991 u16 display_toggle:1;
1992 u16 thinklight_toggle:1;
1993 u16 hibernate_toggle:1;
1994 u16 displayexp_toggle:1;
1995 u16 display_state:1;
1996 u16 brightness_toggle:1;
1997 u16 volume_toggle:1;
1998 u16 mute:1;
2000 u8 brightness_level;
2001 u8 volume_level;
2004 /* kthread for the hotkey poller */
2005 static struct task_struct *tpacpi_hotkey_task;
2007 /* Acquired while the poller kthread is running, use to sync start/stop */
2008 static struct mutex hotkey_thread_mutex;
2011 * Acquire mutex to write poller control variables as an
2012 * atomic block.
2014 * Increment hotkey_config_change when changing them if you
2015 * want the kthread to forget old state.
2017 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2019 static struct mutex hotkey_thread_data_mutex;
2020 static unsigned int hotkey_config_change;
2023 * hotkey poller control variables
2025 * Must be atomic or readers will also need to acquire mutex
2027 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2028 * should be used only when the changes need to be taken as
2029 * a block, OR when one needs to force the kthread to forget
2030 * old state.
2032 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2033 static unsigned int hotkey_poll_freq = 10; /* Hz */
2035 #define HOTKEY_CONFIG_CRITICAL_START \
2036 do { \
2037 mutex_lock(&hotkey_thread_data_mutex); \
2038 hotkey_config_change++; \
2039 } while (0);
2040 #define HOTKEY_CONFIG_CRITICAL_END \
2041 mutex_unlock(&hotkey_thread_data_mutex);
2043 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2045 #define hotkey_source_mask 0U
2046 #define HOTKEY_CONFIG_CRITICAL_START
2047 #define HOTKEY_CONFIG_CRITICAL_END
2049 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2051 static struct mutex hotkey_mutex;
2053 static enum { /* Reasons for waking up */
2054 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2055 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2056 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2057 } hotkey_wakeup_reason;
2059 static int hotkey_autosleep_ack;
2061 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2062 static u32 hotkey_all_mask; /* all events supported in fw */
2063 static u32 hotkey_reserved_mask; /* events better left disabled */
2064 static u32 hotkey_driver_mask; /* events needed by the driver */
2065 static u32 hotkey_user_mask; /* events visible to userspace */
2066 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2068 static unsigned int hotkey_report_mode;
2070 static u16 *hotkey_keycode_map;
2072 static struct attribute_set *hotkey_dev_attributes;
2074 static void tpacpi_driver_event(const unsigned int hkey_event);
2075 static void hotkey_driver_event(const unsigned int scancode);
2076 static void hotkey_poll_setup(const bool may_warn);
2078 /* HKEY.MHKG() return bits */
2079 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2081 static int hotkey_get_wlsw(void)
2083 int status;
2085 if (!tp_features.hotkey_wlsw)
2086 return -ENODEV;
2088 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2089 if (dbg_wlswemul)
2090 return (tpacpi_wlsw_emulstate) ?
2091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2092 #endif
2094 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2095 return -EIO;
2097 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2100 static int hotkey_get_tablet_mode(int *status)
2102 int s;
2104 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2105 return -EIO;
2107 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2108 return 0;
2112 * Reads current event mask from firmware, and updates
2113 * hotkey_acpi_mask accordingly. Also resets any bits
2114 * from hotkey_user_mask that are unavailable to be
2115 * delivered (shadow requirement of the userspace ABI).
2117 * Call with hotkey_mutex held
2119 static int hotkey_mask_get(void)
2121 if (tp_features.hotkey_mask) {
2122 u32 m = 0;
2124 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2125 return -EIO;
2127 hotkey_acpi_mask = m;
2128 } else {
2129 /* no mask support doesn't mean no event support... */
2130 hotkey_acpi_mask = hotkey_all_mask;
2133 /* sync userspace-visible mask */
2134 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2136 return 0;
2139 void static hotkey_mask_warn_incomplete_mask(void)
2141 /* log only what the user can fix... */
2142 const u32 wantedmask = hotkey_driver_mask &
2143 ~(hotkey_acpi_mask | hotkey_source_mask) &
2144 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2146 if (wantedmask)
2147 printk(TPACPI_NOTICE
2148 "required events 0x%08x not enabled!\n",
2149 wantedmask);
2153 * Set the firmware mask when supported
2155 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2157 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2159 * Call with hotkey_mutex held
2161 static int hotkey_mask_set(u32 mask)
2163 int i;
2164 int rc = 0;
2166 const u32 fwmask = mask & ~hotkey_source_mask;
2168 if (tp_features.hotkey_mask) {
2169 for (i = 0; i < 32; i++) {
2170 if (!acpi_evalf(hkey_handle,
2171 NULL, "MHKM", "vdd", i + 1,
2172 !!(mask & (1 << i)))) {
2173 rc = -EIO;
2174 break;
2180 * We *must* make an inconditional call to hotkey_mask_get to
2181 * refresh hotkey_acpi_mask and update hotkey_user_mask
2183 * Take the opportunity to also log when we cannot _enable_
2184 * a given event.
2186 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2187 printk(TPACPI_NOTICE
2188 "asked for hotkey mask 0x%08x, but "
2189 "firmware forced it to 0x%08x\n",
2190 fwmask, hotkey_acpi_mask);
2193 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2194 hotkey_mask_warn_incomplete_mask();
2196 return rc;
2200 * Sets hotkey_user_mask and tries to set the firmware mask
2202 * Call with hotkey_mutex held
2204 static int hotkey_user_mask_set(const u32 mask)
2206 int rc;
2208 /* Give people a chance to notice they are doing something that
2209 * is bound to go boom on their users sooner or later */
2210 if (!tp_warned.hotkey_mask_ff &&
2211 (mask == 0xffff || mask == 0xffffff ||
2212 mask == 0xffffffff)) {
2213 tp_warned.hotkey_mask_ff = 1;
2214 printk(TPACPI_NOTICE
2215 "setting the hotkey mask to 0x%08x is likely "
2216 "not the best way to go about it\n", mask);
2217 printk(TPACPI_NOTICE
2218 "please consider using the driver defaults, "
2219 "and refer to up-to-date thinkpad-acpi "
2220 "documentation\n");
2223 /* Try to enable what the user asked for, plus whatever we need.
2224 * this syncs everything but won't enable bits in hotkey_user_mask */
2225 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2227 /* Enable the available bits in hotkey_user_mask */
2228 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2230 return rc;
2234 * Sets the driver hotkey mask.
2236 * Can be called even if the hotkey subdriver is inactive
2238 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2240 int rc;
2242 /* Do the right thing if hotkey_init has not been called yet */
2243 if (!tp_features.hotkey) {
2244 hotkey_driver_mask = mask;
2245 return 0;
2248 mutex_lock(&hotkey_mutex);
2250 HOTKEY_CONFIG_CRITICAL_START
2251 hotkey_driver_mask = mask;
2252 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2253 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2254 #endif
2255 HOTKEY_CONFIG_CRITICAL_END
2257 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2258 ~hotkey_source_mask);
2259 hotkey_poll_setup(true);
2261 mutex_unlock(&hotkey_mutex);
2263 return rc;
2266 static int hotkey_status_get(int *status)
2268 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2269 return -EIO;
2271 return 0;
2274 static int hotkey_status_set(bool enable)
2276 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2277 return -EIO;
2279 return 0;
2282 static void tpacpi_input_send_tabletsw(void)
2284 int state;
2286 if (tp_features.hotkey_tablet &&
2287 !hotkey_get_tablet_mode(&state)) {
2288 mutex_lock(&tpacpi_inputdev_send_mutex);
2290 input_report_switch(tpacpi_inputdev,
2291 SW_TABLET_MODE, !!state);
2292 input_sync(tpacpi_inputdev);
2294 mutex_unlock(&tpacpi_inputdev_send_mutex);
2298 /* Do NOT call without validating scancode first */
2299 static void tpacpi_input_send_key(const unsigned int scancode)
2301 const unsigned int keycode = hotkey_keycode_map[scancode];
2303 if (keycode != KEY_RESERVED) {
2304 mutex_lock(&tpacpi_inputdev_send_mutex);
2306 input_report_key(tpacpi_inputdev, keycode, 1);
2307 if (keycode == KEY_UNKNOWN)
2308 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2309 scancode);
2310 input_sync(tpacpi_inputdev);
2312 input_report_key(tpacpi_inputdev, keycode, 0);
2313 if (keycode == KEY_UNKNOWN)
2314 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2315 scancode);
2316 input_sync(tpacpi_inputdev);
2318 mutex_unlock(&tpacpi_inputdev_send_mutex);
2322 /* Do NOT call without validating scancode first */
2323 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2325 hotkey_driver_event(scancode);
2326 if (hotkey_user_mask & (1 << scancode))
2327 tpacpi_input_send_key(scancode);
2330 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2331 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2333 /* Do NOT call without validating scancode first */
2334 static void tpacpi_hotkey_send_key(unsigned int scancode)
2336 tpacpi_input_send_key_masked(scancode);
2337 if (hotkey_report_mode < 2) {
2338 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2339 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2343 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2345 u8 d;
2347 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2348 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2349 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2350 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2351 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2352 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2354 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2355 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2356 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2358 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2359 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2360 n->displayexp_toggle =
2361 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2363 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2365 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2366 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2367 n->brightness_toggle =
2368 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2370 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2371 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2372 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2373 >> TP_NVRAM_POS_LEVEL_VOLUME;
2374 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2375 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2379 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2380 struct tp_nvram_state *newn,
2381 const u32 event_mask)
2384 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2385 do { \
2386 if ((event_mask & (1 << __scancode)) && \
2387 oldn->__member != newn->__member) \
2388 tpacpi_hotkey_send_key(__scancode); \
2389 } while (0)
2391 #define TPACPI_MAY_SEND_KEY(__scancode) \
2392 do { \
2393 if (event_mask & (1 << __scancode)) \
2394 tpacpi_hotkey_send_key(__scancode); \
2395 } while (0)
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2406 /* handle volume */
2407 if (oldn->volume_toggle != newn->volume_toggle) {
2408 if (oldn->mute != newn->mute) {
2409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2411 if (oldn->volume_level > newn->volume_level) {
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2413 } else if (oldn->volume_level < newn->volume_level) {
2414 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2415 } else if (oldn->mute == newn->mute) {
2416 /* repeated key presses that didn't change state */
2417 if (newn->mute) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2419 } else if (newn->volume_level != 0) {
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2421 } else {
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2427 /* handle brightness */
2428 if (oldn->brightness_toggle != newn->brightness_toggle) {
2429 if (oldn->brightness_level < newn->brightness_level) {
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2431 } else if (oldn->brightness_level > newn->brightness_level) {
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2433 } else {
2434 /* repeated key presses that didn't change state */
2435 if (newn->brightness_level != 0) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2437 } else {
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2443 #undef TPACPI_COMPARE_KEY
2444 #undef TPACPI_MAY_SEND_KEY
2448 * Polling driver
2450 * We track all events in hotkey_source_mask all the time, since
2451 * most of them are edge-based. We only issue those requested by
2452 * hotkey_user_mask or hotkey_driver_mask, though.
2454 static int hotkey_kthread(void *data)
2456 struct tp_nvram_state s[2];
2457 u32 poll_mask, event_mask;
2458 unsigned int si, so;
2459 unsigned long t;
2460 unsigned int change_detector, must_reset;
2461 unsigned int poll_freq;
2463 mutex_lock(&hotkey_thread_mutex);
2465 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2466 goto exit;
2468 set_freezable();
2470 so = 0;
2471 si = 1;
2472 t = 0;
2474 /* Initial state for compares */
2475 mutex_lock(&hotkey_thread_data_mutex);
2476 change_detector = hotkey_config_change;
2477 poll_mask = hotkey_source_mask;
2478 event_mask = hotkey_source_mask &
2479 (hotkey_driver_mask | hotkey_user_mask);
2480 poll_freq = hotkey_poll_freq;
2481 mutex_unlock(&hotkey_thread_data_mutex);
2482 hotkey_read_nvram(&s[so], poll_mask);
2484 while (!kthread_should_stop()) {
2485 if (t == 0) {
2486 if (likely(poll_freq))
2487 t = 1000/poll_freq;
2488 else
2489 t = 100; /* should never happen... */
2491 t = msleep_interruptible(t);
2492 if (unlikely(kthread_should_stop()))
2493 break;
2494 must_reset = try_to_freeze();
2495 if (t > 0 && !must_reset)
2496 continue;
2498 mutex_lock(&hotkey_thread_data_mutex);
2499 if (must_reset || hotkey_config_change != change_detector) {
2500 /* forget old state on thaw or config change */
2501 si = so;
2502 t = 0;
2503 change_detector = hotkey_config_change;
2505 poll_mask = hotkey_source_mask;
2506 event_mask = hotkey_source_mask &
2507 (hotkey_driver_mask | hotkey_user_mask);
2508 poll_freq = hotkey_poll_freq;
2509 mutex_unlock(&hotkey_thread_data_mutex);
2511 if (likely(poll_mask)) {
2512 hotkey_read_nvram(&s[si], poll_mask);
2513 if (likely(si != so)) {
2514 hotkey_compare_and_issue_event(&s[so], &s[si],
2515 event_mask);
2519 so = si;
2520 si ^= 1;
2523 exit:
2524 mutex_unlock(&hotkey_thread_mutex);
2525 return 0;
2528 /* call with hotkey_mutex held */
2529 static void hotkey_poll_stop_sync(void)
2531 if (tpacpi_hotkey_task) {
2532 if (frozen(tpacpi_hotkey_task) ||
2533 freezing(tpacpi_hotkey_task))
2534 thaw_process(tpacpi_hotkey_task);
2536 kthread_stop(tpacpi_hotkey_task);
2537 tpacpi_hotkey_task = NULL;
2538 mutex_lock(&hotkey_thread_mutex);
2539 /* at this point, the thread did exit */
2540 mutex_unlock(&hotkey_thread_mutex);
2544 /* call with hotkey_mutex held */
2545 static void hotkey_poll_setup(const bool may_warn)
2547 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2548 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2550 if (hotkey_poll_freq > 0 &&
2551 (poll_driver_mask ||
2552 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2553 if (!tpacpi_hotkey_task) {
2554 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2555 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2556 if (IS_ERR(tpacpi_hotkey_task)) {
2557 tpacpi_hotkey_task = NULL;
2558 printk(TPACPI_ERR
2559 "could not create kernel thread "
2560 "for hotkey polling\n");
2563 } else {
2564 hotkey_poll_stop_sync();
2565 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2566 hotkey_poll_freq == 0) {
2567 printk(TPACPI_NOTICE
2568 "hot keys 0x%08x and/or events 0x%08x "
2569 "require polling, which is currently "
2570 "disabled\n",
2571 poll_user_mask, poll_driver_mask);
2576 static void hotkey_poll_setup_safe(const bool may_warn)
2578 mutex_lock(&hotkey_mutex);
2579 hotkey_poll_setup(may_warn);
2580 mutex_unlock(&hotkey_mutex);
2583 /* call with hotkey_mutex held */
2584 static void hotkey_poll_set_freq(unsigned int freq)
2586 if (!freq)
2587 hotkey_poll_stop_sync();
2589 hotkey_poll_freq = freq;
2592 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2594 static void hotkey_poll_setup(const bool __unused)
2598 static void hotkey_poll_setup_safe(const bool __unused)
2602 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2604 static int hotkey_inputdev_open(struct input_dev *dev)
2606 switch (tpacpi_lifecycle) {
2607 case TPACPI_LIFE_INIT:
2608 case TPACPI_LIFE_RUNNING:
2609 hotkey_poll_setup_safe(false);
2610 return 0;
2611 case TPACPI_LIFE_EXITING:
2612 return -EBUSY;
2615 /* Should only happen if tpacpi_lifecycle is corrupt */
2616 BUG();
2617 return -EBUSY;
2620 static void hotkey_inputdev_close(struct input_dev *dev)
2622 /* disable hotkey polling when possible */
2623 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2624 !(hotkey_source_mask & hotkey_driver_mask))
2625 hotkey_poll_setup_safe(false);
2628 /* sysfs hotkey enable ------------------------------------------------- */
2629 static ssize_t hotkey_enable_show(struct device *dev,
2630 struct device_attribute *attr,
2631 char *buf)
2633 int res, status;
2635 printk_deprecated_attribute("hotkey_enable",
2636 "Hotkey reporting is always enabled");
2638 res = hotkey_status_get(&status);
2639 if (res)
2640 return res;
2642 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2645 static ssize_t hotkey_enable_store(struct device *dev,
2646 struct device_attribute *attr,
2647 const char *buf, size_t count)
2649 unsigned long t;
2651 printk_deprecated_attribute("hotkey_enable",
2652 "Hotkeys can be disabled through hotkey_mask");
2654 if (parse_strtoul(buf, 1, &t))
2655 return -EINVAL;
2657 if (t == 0)
2658 return -EPERM;
2660 return count;
2663 static struct device_attribute dev_attr_hotkey_enable =
2664 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2665 hotkey_enable_show, hotkey_enable_store);
2667 /* sysfs hotkey mask --------------------------------------------------- */
2668 static ssize_t hotkey_mask_show(struct device *dev,
2669 struct device_attribute *attr,
2670 char *buf)
2672 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2675 static ssize_t hotkey_mask_store(struct device *dev,
2676 struct device_attribute *attr,
2677 const char *buf, size_t count)
2679 unsigned long t;
2680 int res;
2682 if (parse_strtoul(buf, 0xffffffffUL, &t))
2683 return -EINVAL;
2685 if (mutex_lock_killable(&hotkey_mutex))
2686 return -ERESTARTSYS;
2688 res = hotkey_user_mask_set(t);
2690 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2691 hotkey_poll_setup(true);
2692 #endif
2694 mutex_unlock(&hotkey_mutex);
2696 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2698 return (res) ? res : count;
2701 static struct device_attribute dev_attr_hotkey_mask =
2702 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2703 hotkey_mask_show, hotkey_mask_store);
2705 /* sysfs hotkey bios_enabled ------------------------------------------- */
2706 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2707 struct device_attribute *attr,
2708 char *buf)
2710 return sprintf(buf, "0\n");
2713 static struct device_attribute dev_attr_hotkey_bios_enabled =
2714 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2716 /* sysfs hotkey bios_mask ---------------------------------------------- */
2717 static ssize_t hotkey_bios_mask_show(struct device *dev,
2718 struct device_attribute *attr,
2719 char *buf)
2721 printk_deprecated_attribute("hotkey_bios_mask",
2722 "This attribute is useless.");
2723 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2726 static struct device_attribute dev_attr_hotkey_bios_mask =
2727 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2729 /* sysfs hotkey all_mask ----------------------------------------------- */
2730 static ssize_t hotkey_all_mask_show(struct device *dev,
2731 struct device_attribute *attr,
2732 char *buf)
2734 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2735 hotkey_all_mask | hotkey_source_mask);
2738 static struct device_attribute dev_attr_hotkey_all_mask =
2739 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2741 /* sysfs hotkey recommended_mask --------------------------------------- */
2742 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2746 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2747 (hotkey_all_mask | hotkey_source_mask)
2748 & ~hotkey_reserved_mask);
2751 static struct device_attribute dev_attr_hotkey_recommended_mask =
2752 __ATTR(hotkey_recommended_mask, S_IRUGO,
2753 hotkey_recommended_mask_show, NULL);
2755 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2757 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2758 static ssize_t hotkey_source_mask_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2762 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2765 static ssize_t hotkey_source_mask_store(struct device *dev,
2766 struct device_attribute *attr,
2767 const char *buf, size_t count)
2769 unsigned long t;
2770 u32 r_ev;
2771 int rc;
2773 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2774 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2775 return -EINVAL;
2777 if (mutex_lock_killable(&hotkey_mutex))
2778 return -ERESTARTSYS;
2780 HOTKEY_CONFIG_CRITICAL_START
2781 hotkey_source_mask = t;
2782 HOTKEY_CONFIG_CRITICAL_END
2784 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2785 ~hotkey_source_mask);
2786 hotkey_poll_setup(true);
2788 /* check if events needed by the driver got disabled */
2789 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2790 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2792 mutex_unlock(&hotkey_mutex);
2794 if (rc < 0)
2795 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2796 "firmware event mask!\n");
2798 if (r_ev)
2799 printk(TPACPI_NOTICE "hotkey_source_mask: "
2800 "some important events were disabled: "
2801 "0x%04x\n", r_ev);
2803 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2805 return (rc < 0) ? rc : count;
2808 static struct device_attribute dev_attr_hotkey_source_mask =
2809 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2810 hotkey_source_mask_show, hotkey_source_mask_store);
2812 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2813 static ssize_t hotkey_poll_freq_show(struct device *dev,
2814 struct device_attribute *attr,
2815 char *buf)
2817 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2820 static ssize_t hotkey_poll_freq_store(struct device *dev,
2821 struct device_attribute *attr,
2822 const char *buf, size_t count)
2824 unsigned long t;
2826 if (parse_strtoul(buf, 25, &t))
2827 return -EINVAL;
2829 if (mutex_lock_killable(&hotkey_mutex))
2830 return -ERESTARTSYS;
2832 hotkey_poll_set_freq(t);
2833 hotkey_poll_setup(true);
2835 mutex_unlock(&hotkey_mutex);
2837 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2839 return count;
2842 static struct device_attribute dev_attr_hotkey_poll_freq =
2843 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2844 hotkey_poll_freq_show, hotkey_poll_freq_store);
2846 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2848 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2849 static ssize_t hotkey_radio_sw_show(struct device *dev,
2850 struct device_attribute *attr,
2851 char *buf)
2853 int res;
2854 res = hotkey_get_wlsw();
2855 if (res < 0)
2856 return res;
2858 /* Opportunistic update */
2859 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2861 return snprintf(buf, PAGE_SIZE, "%d\n",
2862 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2865 static struct device_attribute dev_attr_hotkey_radio_sw =
2866 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2868 static void hotkey_radio_sw_notify_change(void)
2870 if (tp_features.hotkey_wlsw)
2871 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2872 "hotkey_radio_sw");
2875 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2876 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2877 struct device_attribute *attr,
2878 char *buf)
2880 int res, s;
2881 res = hotkey_get_tablet_mode(&s);
2882 if (res < 0)
2883 return res;
2885 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2888 static struct device_attribute dev_attr_hotkey_tablet_mode =
2889 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2891 static void hotkey_tablet_mode_notify_change(void)
2893 if (tp_features.hotkey_tablet)
2894 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2895 "hotkey_tablet_mode");
2898 /* sysfs hotkey report_mode -------------------------------------------- */
2899 static ssize_t hotkey_report_mode_show(struct device *dev,
2900 struct device_attribute *attr,
2901 char *buf)
2903 return snprintf(buf, PAGE_SIZE, "%d\n",
2904 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2907 static struct device_attribute dev_attr_hotkey_report_mode =
2908 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2910 /* sysfs wakeup reason (pollable) -------------------------------------- */
2911 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2912 struct device_attribute *attr,
2913 char *buf)
2915 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2918 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2919 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2921 static void hotkey_wakeup_reason_notify_change(void)
2923 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2924 "wakeup_reason");
2927 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2928 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2929 struct device_attribute *attr,
2930 char *buf)
2932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2935 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2936 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2937 hotkey_wakeup_hotunplug_complete_show, NULL);
2939 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2941 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2942 "wakeup_hotunplug_complete");
2945 /* --------------------------------------------------------------------- */
2947 static struct attribute *hotkey_attributes[] __initdata = {
2948 &dev_attr_hotkey_enable.attr,
2949 &dev_attr_hotkey_bios_enabled.attr,
2950 &dev_attr_hotkey_bios_mask.attr,
2951 &dev_attr_hotkey_report_mode.attr,
2952 &dev_attr_hotkey_wakeup_reason.attr,
2953 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2954 &dev_attr_hotkey_mask.attr,
2955 &dev_attr_hotkey_all_mask.attr,
2956 &dev_attr_hotkey_recommended_mask.attr,
2957 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2958 &dev_attr_hotkey_source_mask.attr,
2959 &dev_attr_hotkey_poll_freq.attr,
2960 #endif
2964 * Sync both the hw and sw blocking state of all switches
2966 static void tpacpi_send_radiosw_update(void)
2968 int wlsw;
2971 * We must sync all rfkill controllers *before* issuing any
2972 * rfkill input events, or we will race the rfkill core input
2973 * handler.
2975 * tpacpi_inputdev_send_mutex works as a syncronization point
2976 * for the above.
2978 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2981 wlsw = hotkey_get_wlsw();
2983 /* Sync hw blocking state first if it is hw-blocked */
2984 if (wlsw == TPACPI_RFK_RADIO_OFF)
2985 tpacpi_rfk_update_hwblock_state(true);
2987 /* Sync sw blocking state */
2988 tpacpi_rfk_update_swstate_all();
2990 /* Sync hw blocking state last if it is hw-unblocked */
2991 if (wlsw == TPACPI_RFK_RADIO_ON)
2992 tpacpi_rfk_update_hwblock_state(false);
2994 /* Issue rfkill input event for WLSW switch */
2995 if (!(wlsw < 0)) {
2996 mutex_lock(&tpacpi_inputdev_send_mutex);
2998 input_report_switch(tpacpi_inputdev,
2999 SW_RFKILL_ALL, (wlsw > 0));
3000 input_sync(tpacpi_inputdev);
3002 mutex_unlock(&tpacpi_inputdev_send_mutex);
3006 * this can be unconditional, as we will poll state again
3007 * if userspace uses the notify to read data
3009 hotkey_radio_sw_notify_change();
3012 static void hotkey_exit(void)
3014 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3015 mutex_lock(&hotkey_mutex);
3016 hotkey_poll_stop_sync();
3017 mutex_unlock(&hotkey_mutex);
3018 #endif
3020 if (hotkey_dev_attributes)
3021 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3023 kfree(hotkey_keycode_map);
3025 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3026 "restoring original HKEY status and mask\n");
3027 /* yes, there is a bitwise or below, we want the
3028 * functions to be called even if one of them fail */
3029 if (((tp_features.hotkey_mask &&
3030 hotkey_mask_set(hotkey_orig_mask)) |
3031 hotkey_status_set(false)) != 0)
3032 printk(TPACPI_ERR
3033 "failed to restore hot key mask "
3034 "to BIOS defaults\n");
3037 static void __init hotkey_unmap(const unsigned int scancode)
3039 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3040 clear_bit(hotkey_keycode_map[scancode],
3041 tpacpi_inputdev->keybit);
3042 hotkey_keycode_map[scancode] = KEY_RESERVED;
3047 * HKEY quirks:
3048 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3051 #define TPACPI_HK_Q_INIMASK 0x0001
3053 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3054 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3055 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3056 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3057 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3058 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3059 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3060 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3061 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3062 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3063 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3064 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3065 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3066 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3067 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3068 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3069 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3070 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3071 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3072 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3075 static int __init hotkey_init(struct ibm_init_struct *iibm)
3077 /* Requirements for changing the default keymaps:
3079 * 1. Many of the keys are mapped to KEY_RESERVED for very
3080 * good reasons. Do not change them unless you have deep
3081 * knowledge on the IBM and Lenovo ThinkPad firmware for
3082 * the various ThinkPad models. The driver behaves
3083 * differently for KEY_RESERVED: such keys have their
3084 * hot key mask *unset* in mask_recommended, and also
3085 * in the initial hot key mask programmed into the
3086 * firmware at driver load time, which means the firm-
3087 * ware may react very differently if you change them to
3088 * something else;
3090 * 2. You must be subscribed to the linux-thinkpad and
3091 * ibm-acpi-devel mailing lists, and you should read the
3092 * list archives since 2007 if you want to change the
3093 * keymaps. This requirement exists so that you will
3094 * know the past history of problems with the thinkpad-
3095 * acpi driver keymaps, and also that you will be
3096 * listening to any bug reports;
3098 * 3. Do not send thinkpad-acpi specific patches directly to
3099 * for merging, *ever*. Send them to the linux-acpi
3100 * mailinglist for comments. Merging is to be done only
3101 * through acpi-test and the ACPI maintainer.
3103 * If the above is too much to ask, don't change the keymap.
3104 * Ask the thinkpad-acpi maintainer to do it, instead.
3106 static u16 ibm_keycode_map[] __initdata = {
3107 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3108 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3109 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3110 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3112 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3113 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3114 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3115 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3117 /* brightness: firmware always reacts to them */
3118 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3119 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3121 /* Thinklight: firmware always react to it */
3122 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3124 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3125 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3127 /* Volume: firmware always react to it and reprograms
3128 * the built-in *extra* mixer. Never map it to control
3129 * another mixer by default. */
3130 KEY_RESERVED, /* 0x14: VOLUME UP */
3131 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3132 KEY_RESERVED, /* 0x16: MUTE */
3134 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3136 /* (assignments unknown, please report if found) */
3137 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3138 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3140 static u16 lenovo_keycode_map[] __initdata = {
3141 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3142 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3143 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3144 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3146 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3147 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3148 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3149 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3151 /* These should be enabled --only-- when ACPI video
3152 * is disabled (i.e. in "vendor" mode), and are handled
3153 * in a special way by the init code */
3154 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3155 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3157 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3159 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3160 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3162 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3163 * react to it and reprograms the built-in *extra* mixer.
3164 * Never map it to control another mixer by default.
3166 * T60?, T61, R60?, R61: firmware and EC tries to send
3167 * these over the regular keyboard, so these are no-ops,
3168 * but there are still weird bugs re. MUTE, so do not
3169 * change unless you get test reports from all Lenovo
3170 * models. May cause the BIOS to interfere with the
3171 * HDA mixer.
3173 KEY_RESERVED, /* 0x14: VOLUME UP */
3174 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3175 KEY_RESERVED, /* 0x16: MUTE */
3177 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3179 /* (assignments unknown, please report if found) */
3180 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3181 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3184 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3185 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3186 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3188 int res, i;
3189 int status;
3190 int hkeyv;
3191 bool radiosw_state = false;
3192 bool tabletsw_state = false;
3194 unsigned long quirks;
3196 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3197 "initializing hotkey subdriver\n");
3199 BUG_ON(!tpacpi_inputdev);
3200 BUG_ON(tpacpi_inputdev->open != NULL ||
3201 tpacpi_inputdev->close != NULL);
3203 TPACPI_ACPIHANDLE_INIT(hkey);
3204 mutex_init(&hotkey_mutex);
3206 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3207 mutex_init(&hotkey_thread_mutex);
3208 mutex_init(&hotkey_thread_data_mutex);
3209 #endif
3211 /* hotkey not supported on 570 */
3212 tp_features.hotkey = hkey_handle != NULL;
3214 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3215 "hotkeys are %s\n",
3216 str_supported(tp_features.hotkey));
3218 if (!tp_features.hotkey)
3219 return 1;
3221 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3222 ARRAY_SIZE(tpacpi_hotkey_qtable));
3224 tpacpi_disable_brightness_delay();
3226 /* MUST have enough space for all attributes to be added to
3227 * hotkey_dev_attributes */
3228 hotkey_dev_attributes = create_attr_set(
3229 ARRAY_SIZE(hotkey_attributes) + 2,
3230 NULL);
3231 if (!hotkey_dev_attributes)
3232 return -ENOMEM;
3233 res = add_many_to_attr_set(hotkey_dev_attributes,
3234 hotkey_attributes,
3235 ARRAY_SIZE(hotkey_attributes));
3236 if (res)
3237 goto err_exit;
3239 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3240 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3241 for HKEY interface version 0x100 */
3242 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3243 if ((hkeyv >> 8) != 1) {
3244 printk(TPACPI_ERR "unknown version of the "
3245 "HKEY interface: 0x%x\n", hkeyv);
3246 printk(TPACPI_ERR "please report this to %s\n",
3247 TPACPI_MAIL);
3248 } else {
3250 * MHKV 0x100 in A31, R40, R40e,
3251 * T4x, X31, and later
3253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3254 "firmware HKEY interface version: 0x%x\n",
3255 hkeyv);
3257 /* Paranoia check AND init hotkey_all_mask */
3258 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3259 "MHKA", "qd")) {
3260 printk(TPACPI_ERR
3261 "missing MHKA handler, "
3262 "please report this to %s\n",
3263 TPACPI_MAIL);
3264 /* Fallback: pre-init for FN+F3,F4,F12 */
3265 hotkey_all_mask = 0x080cU;
3266 } else {
3267 tp_features.hotkey_mask = 1;
3272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3273 "hotkey masks are %s\n",
3274 str_supported(tp_features.hotkey_mask));
3276 /* Init hotkey_all_mask if not initialized yet */
3277 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3278 (quirks & TPACPI_HK_Q_INIMASK))
3279 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3281 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3282 if (tp_features.hotkey_mask) {
3283 /* hotkey_source_mask *must* be zero for
3284 * the first hotkey_mask_get to return hotkey_orig_mask */
3285 res = hotkey_mask_get();
3286 if (res)
3287 goto err_exit;
3289 hotkey_orig_mask = hotkey_acpi_mask;
3290 } else {
3291 hotkey_orig_mask = hotkey_all_mask;
3292 hotkey_acpi_mask = hotkey_all_mask;
3295 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3296 if (dbg_wlswemul) {
3297 tp_features.hotkey_wlsw = 1;
3298 radiosw_state = !!tpacpi_wlsw_emulstate;
3299 printk(TPACPI_INFO
3300 "radio switch emulation enabled\n");
3301 } else
3302 #endif
3303 /* Not all thinkpads have a hardware radio switch */
3304 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3305 tp_features.hotkey_wlsw = 1;
3306 radiosw_state = !!status;
3307 printk(TPACPI_INFO
3308 "radio switch found; radios are %s\n",
3309 enabled(status, 0));
3311 if (tp_features.hotkey_wlsw)
3312 res = add_to_attr_set(hotkey_dev_attributes,
3313 &dev_attr_hotkey_radio_sw.attr);
3315 /* For X41t, X60t, X61t Tablets... */
3316 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3317 tp_features.hotkey_tablet = 1;
3318 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3319 printk(TPACPI_INFO
3320 "possible tablet mode switch found; "
3321 "ThinkPad in %s mode\n",
3322 (tabletsw_state) ? "tablet" : "laptop");
3323 res = add_to_attr_set(hotkey_dev_attributes,
3324 &dev_attr_hotkey_tablet_mode.attr);
3327 if (!res)
3328 res = register_attr_set_with_sysfs(
3329 hotkey_dev_attributes,
3330 &tpacpi_pdev->dev.kobj);
3331 if (res)
3332 goto err_exit;
3334 /* Set up key map */
3336 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3337 GFP_KERNEL);
3338 if (!hotkey_keycode_map) {
3339 printk(TPACPI_ERR
3340 "failed to allocate memory for key map\n");
3341 res = -ENOMEM;
3342 goto err_exit;
3345 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3346 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3347 "using Lenovo default hot key map\n");
3348 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3349 TPACPI_HOTKEY_MAP_SIZE);
3350 } else {
3351 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3352 "using IBM default hot key map\n");
3353 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3354 TPACPI_HOTKEY_MAP_SIZE);
3357 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3358 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3359 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3360 tpacpi_inputdev->keycode = hotkey_keycode_map;
3361 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3362 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3363 input_set_capability(tpacpi_inputdev, EV_KEY,
3364 hotkey_keycode_map[i]);
3365 } else {
3366 if (i < sizeof(hotkey_reserved_mask)*8)
3367 hotkey_reserved_mask |= 1 << i;
3371 if (tp_features.hotkey_wlsw) {
3372 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3373 input_report_switch(tpacpi_inputdev,
3374 SW_RFKILL_ALL, radiosw_state);
3376 if (tp_features.hotkey_tablet) {
3377 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3378 input_report_switch(tpacpi_inputdev,
3379 SW_TABLET_MODE, tabletsw_state);
3382 /* Do not issue duplicate brightness change events to
3383 * userspace */
3384 if (!tp_features.bright_acpimode)
3385 /* update bright_acpimode... */
3386 tpacpi_check_std_acpi_brightness_support();
3388 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3389 printk(TPACPI_INFO
3390 "This ThinkPad has standard ACPI backlight "
3391 "brightness control, supported by the ACPI "
3392 "video driver\n");
3393 printk(TPACPI_NOTICE
3394 "Disabling thinkpad-acpi brightness events "
3395 "by default...\n");
3397 /* Disable brightness up/down on Lenovo thinkpads when
3398 * ACPI is handling them, otherwise it is plain impossible
3399 * for userspace to do something even remotely sane */
3400 hotkey_reserved_mask |=
3401 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3402 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3403 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3404 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3407 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3408 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3409 & ~hotkey_all_mask
3410 & ~hotkey_reserved_mask;
3412 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3413 "hotkey source mask 0x%08x, polling freq %u\n",
3414 hotkey_source_mask, hotkey_poll_freq);
3415 #endif
3417 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "enabling firmware HKEY event interface...\n");
3419 res = hotkey_status_set(true);
3420 if (res) {
3421 hotkey_exit();
3422 return res;
3424 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3425 | hotkey_driver_mask)
3426 & ~hotkey_source_mask);
3427 if (res < 0 && res != -ENXIO) {
3428 hotkey_exit();
3429 return res;
3431 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3432 & ~hotkey_reserved_mask;
3433 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3434 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3435 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3437 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3438 "legacy ibm/hotkey event reporting over procfs %s\n",
3439 (hotkey_report_mode < 2) ?
3440 "enabled" : "disabled");
3442 tpacpi_inputdev->open = &hotkey_inputdev_open;
3443 tpacpi_inputdev->close = &hotkey_inputdev_close;
3445 hotkey_poll_setup_safe(true);
3447 return 0;
3449 err_exit:
3450 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3451 hotkey_dev_attributes = NULL;
3453 return (res < 0)? res : 1;
3456 static bool hotkey_notify_hotkey(const u32 hkey,
3457 bool *send_acpi_ev,
3458 bool *ignore_acpi_ev)
3460 /* 0x1000-0x1FFF: key presses */
3461 unsigned int scancode = hkey & 0xfff;
3462 *send_acpi_ev = true;
3463 *ignore_acpi_ev = false;
3465 if (scancode > 0 && scancode < 0x21) {
3466 scancode--;
3467 if (!(hotkey_source_mask & (1 << scancode))) {
3468 tpacpi_input_send_key_masked(scancode);
3469 *send_acpi_ev = false;
3470 } else {
3471 *ignore_acpi_ev = true;
3473 return true;
3475 return false;
3478 static bool hotkey_notify_wakeup(const u32 hkey,
3479 bool *send_acpi_ev,
3480 bool *ignore_acpi_ev)
3482 /* 0x2000-0x2FFF: Wakeup reason */
3483 *send_acpi_ev = true;
3484 *ignore_acpi_ev = false;
3486 switch (hkey) {
3487 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3488 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3489 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3490 *ignore_acpi_ev = true;
3491 break;
3493 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3494 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3495 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3496 *ignore_acpi_ev = true;
3497 break;
3499 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3500 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3501 printk(TPACPI_ALERT
3502 "EMERGENCY WAKEUP: battery almost empty\n");
3503 /* how to auto-heal: */
3504 /* 2313: woke up from S3, go to S4/S5 */
3505 /* 2413: woke up from S4, go to S5 */
3506 break;
3508 default:
3509 return false;
3512 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3513 printk(TPACPI_INFO
3514 "woke up due to a hot-unplug "
3515 "request...\n");
3516 hotkey_wakeup_reason_notify_change();
3518 return true;
3521 static bool hotkey_notify_usrevent(const u32 hkey,
3522 bool *send_acpi_ev,
3523 bool *ignore_acpi_ev)
3525 /* 0x5000-0x5FFF: human interface helpers */
3526 *send_acpi_ev = true;
3527 *ignore_acpi_ev = false;
3529 switch (hkey) {
3530 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3531 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3532 return true;
3534 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3535 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3536 tpacpi_input_send_tabletsw();
3537 hotkey_tablet_mode_notify_change();
3538 *send_acpi_ev = false;
3539 return true;
3541 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3542 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3543 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3544 /* do not propagate these events */
3545 *ignore_acpi_ev = true;
3546 return true;
3548 default:
3549 return false;
3553 static void thermal_dump_all_sensors(void);
3555 static bool hotkey_notify_thermal(const u32 hkey,
3556 bool *send_acpi_ev,
3557 bool *ignore_acpi_ev)
3559 bool known = true;
3561 /* 0x6000-0x6FFF: thermal alarms */
3562 *send_acpi_ev = true;
3563 *ignore_acpi_ev = false;
3565 switch (hkey) {
3566 case TP_HKEY_EV_THM_TABLE_CHANGED:
3567 printk(TPACPI_INFO
3568 "EC reports that Thermal Table has changed\n");
3569 /* recommended action: do nothing, we don't have
3570 * Lenovo ATM information */
3571 return true;
3572 case TP_HKEY_EV_ALARM_BAT_HOT:
3573 printk(TPACPI_CRIT
3574 "THERMAL ALARM: battery is too hot!\n");
3575 /* recommended action: warn user through gui */
3576 break;
3577 case TP_HKEY_EV_ALARM_BAT_XHOT:
3578 printk(TPACPI_ALERT
3579 "THERMAL EMERGENCY: battery is extremely hot!\n");
3580 /* recommended action: immediate sleep/hibernate */
3581 break;
3582 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3583 printk(TPACPI_CRIT
3584 "THERMAL ALARM: "
3585 "a sensor reports something is too hot!\n");
3586 /* recommended action: warn user through gui, that */
3587 /* some internal component is too hot */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3590 printk(TPACPI_ALERT
3591 "THERMAL EMERGENCY: "
3592 "a sensor reports something is extremely hot!\n");
3593 /* recommended action: immediate sleep/hibernate */
3594 break;
3595 default:
3596 printk(TPACPI_ALERT
3597 "THERMAL ALERT: unknown thermal alarm received\n");
3598 known = false;
3601 thermal_dump_all_sensors();
3603 return known;
3606 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3608 u32 hkey;
3609 bool send_acpi_ev;
3610 bool ignore_acpi_ev;
3611 bool known_ev;
3613 if (event != 0x80) {
3614 printk(TPACPI_ERR
3615 "unknown HKEY notification event %d\n", event);
3616 /* forward it to userspace, maybe it knows how to handle it */
3617 acpi_bus_generate_netlink_event(
3618 ibm->acpi->device->pnp.device_class,
3619 dev_name(&ibm->acpi->device->dev),
3620 event, 0);
3621 return;
3624 while (1) {
3625 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3626 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3627 return;
3630 if (hkey == 0) {
3631 /* queue empty */
3632 return;
3635 send_acpi_ev = true;
3636 ignore_acpi_ev = false;
3638 switch (hkey >> 12) {
3639 case 1:
3640 /* 0x1000-0x1FFF: key presses */
3641 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3642 &ignore_acpi_ev);
3643 break;
3644 case 2:
3645 /* 0x2000-0x2FFF: Wakeup reason */
3646 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3647 &ignore_acpi_ev);
3648 break;
3649 case 3:
3650 /* 0x3000-0x3FFF: bay-related wakeups */
3651 switch (hkey) {
3652 case TP_HKEY_EV_BAYEJ_ACK:
3653 hotkey_autosleep_ack = 1;
3654 printk(TPACPI_INFO
3655 "bay ejected\n");
3656 hotkey_wakeup_hotunplug_complete_notify_change();
3657 known_ev = true;
3658 break;
3659 case TP_HKEY_EV_OPTDRV_EJ:
3660 /* FIXME: kick libata if SATA link offline */
3661 known_ev = true;
3662 break;
3663 default:
3664 known_ev = false;
3666 break;
3667 case 4:
3668 /* 0x4000-0x4FFF: dock-related wakeups */
3669 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3670 hotkey_autosleep_ack = 1;
3671 printk(TPACPI_INFO
3672 "undocked\n");
3673 hotkey_wakeup_hotunplug_complete_notify_change();
3674 known_ev = true;
3675 } else {
3676 known_ev = false;
3678 break;
3679 case 5:
3680 /* 0x5000-0x5FFF: human interface helpers */
3681 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3682 &ignore_acpi_ev);
3683 break;
3684 case 6:
3685 /* 0x6000-0x6FFF: thermal alarms */
3686 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3687 &ignore_acpi_ev);
3688 break;
3689 case 7:
3690 /* 0x7000-0x7FFF: misc */
3691 if (tp_features.hotkey_wlsw &&
3692 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3693 tpacpi_send_radiosw_update();
3694 send_acpi_ev = 0;
3695 known_ev = true;
3696 break;
3698 /* fallthrough to default */
3699 default:
3700 known_ev = false;
3702 if (!known_ev) {
3703 printk(TPACPI_NOTICE
3704 "unhandled HKEY event 0x%04x\n", hkey);
3705 printk(TPACPI_NOTICE
3706 "please report the conditions when this "
3707 "event happened to %s\n", TPACPI_MAIL);
3710 /* Legacy events */
3711 if (!ignore_acpi_ev &&
3712 (send_acpi_ev || hotkey_report_mode < 2)) {
3713 acpi_bus_generate_proc_event(ibm->acpi->device,
3714 event, hkey);
3717 /* netlink events */
3718 if (!ignore_acpi_ev && send_acpi_ev) {
3719 acpi_bus_generate_netlink_event(
3720 ibm->acpi->device->pnp.device_class,
3721 dev_name(&ibm->acpi->device->dev),
3722 event, hkey);
3727 static void hotkey_suspend(pm_message_t state)
3729 /* Do these on suspend, we get the events on early resume! */
3730 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3731 hotkey_autosleep_ack = 0;
3734 static void hotkey_resume(void)
3736 tpacpi_disable_brightness_delay();
3738 if (hotkey_status_set(true) < 0 ||
3739 hotkey_mask_set(hotkey_acpi_mask) < 0)
3740 printk(TPACPI_ERR
3741 "error while attempting to reset the event "
3742 "firmware interface\n");
3744 tpacpi_send_radiosw_update();
3745 hotkey_tablet_mode_notify_change();
3746 hotkey_wakeup_reason_notify_change();
3747 hotkey_wakeup_hotunplug_complete_notify_change();
3748 hotkey_poll_setup_safe(false);
3751 /* procfs -------------------------------------------------------------- */
3752 static int hotkey_read(char *p)
3754 int res, status;
3755 int len = 0;
3757 if (!tp_features.hotkey) {
3758 len += sprintf(p + len, "status:\t\tnot supported\n");
3759 return len;
3762 if (mutex_lock_killable(&hotkey_mutex))
3763 return -ERESTARTSYS;
3764 res = hotkey_status_get(&status);
3765 if (!res)
3766 res = hotkey_mask_get();
3767 mutex_unlock(&hotkey_mutex);
3768 if (res)
3769 return res;
3771 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3772 if (hotkey_all_mask) {
3773 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3774 len += sprintf(p + len,
3775 "commands:\tenable, disable, reset, <mask>\n");
3776 } else {
3777 len += sprintf(p + len, "mask:\t\tnot supported\n");
3778 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3781 return len;
3784 static void hotkey_enabledisable_warn(bool enable)
3786 tpacpi_log_usertask("procfs hotkey enable/disable");
3787 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3788 TPACPI_WARN
3789 "hotkey enable/disable functionality has been "
3790 "removed from the driver. Hotkeys are always "
3791 "enabled\n"))
3792 printk(TPACPI_ERR
3793 "Please remove the hotkey=enable module "
3794 "parameter, it is deprecated. Hotkeys are always "
3795 "enabled\n");
3798 static int hotkey_write(char *buf)
3800 int res;
3801 u32 mask;
3802 char *cmd;
3804 if (!tp_features.hotkey)
3805 return -ENODEV;
3807 if (mutex_lock_killable(&hotkey_mutex))
3808 return -ERESTARTSYS;
3810 mask = hotkey_user_mask;
3812 res = 0;
3813 while ((cmd = next_cmd(&buf))) {
3814 if (strlencmp(cmd, "enable") == 0) {
3815 hotkey_enabledisable_warn(1);
3816 } else if (strlencmp(cmd, "disable") == 0) {
3817 hotkey_enabledisable_warn(0);
3818 res = -EPERM;
3819 } else if (strlencmp(cmd, "reset") == 0) {
3820 mask = (hotkey_all_mask | hotkey_source_mask)
3821 & ~hotkey_reserved_mask;
3822 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3823 /* mask set */
3824 } else if (sscanf(cmd, "%x", &mask) == 1) {
3825 /* mask set */
3826 } else {
3827 res = -EINVAL;
3828 goto errexit;
3832 if (!res) {
3833 tpacpi_disclose_usertask("procfs hotkey",
3834 "set mask to 0x%08x\n", mask);
3835 res = hotkey_user_mask_set(mask);
3838 errexit:
3839 mutex_unlock(&hotkey_mutex);
3840 return res;
3843 static const struct acpi_device_id ibm_htk_device_ids[] = {
3844 {TPACPI_ACPI_HKEY_HID, 0},
3845 {"", 0},
3848 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3849 .hid = ibm_htk_device_ids,
3850 .notify = hotkey_notify,
3851 .handle = &hkey_handle,
3852 .type = ACPI_DEVICE_NOTIFY,
3855 static struct ibm_struct hotkey_driver_data = {
3856 .name = "hotkey",
3857 .read = hotkey_read,
3858 .write = hotkey_write,
3859 .exit = hotkey_exit,
3860 .resume = hotkey_resume,
3861 .suspend = hotkey_suspend,
3862 .acpi = &ibm_hotkey_acpidriver,
3865 /*************************************************************************
3866 * Bluetooth subdriver
3869 enum {
3870 /* ACPI GBDC/SBDC bits */
3871 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3872 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3873 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3874 0 = disable, 1 = enable */
3877 enum {
3878 /* ACPI \BLTH commands */
3879 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3880 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3881 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3882 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3883 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3886 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3888 static int bluetooth_get_status(void)
3890 int status;
3892 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3893 if (dbg_bluetoothemul)
3894 return (tpacpi_bluetooth_emulstate) ?
3895 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3896 #endif
3898 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3899 return -EIO;
3901 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3902 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3905 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3907 int status;
3909 vdbg_printk(TPACPI_DBG_RFKILL,
3910 "will attempt to %s bluetooth\n",
3911 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3913 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3914 if (dbg_bluetoothemul) {
3915 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3916 return 0;
3918 #endif
3920 if (state == TPACPI_RFK_RADIO_ON)
3921 status = TP_ACPI_BLUETOOTH_RADIOSSW
3922 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3923 else
3924 status = 0;
3926 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3927 return -EIO;
3929 return 0;
3932 /* sysfs bluetooth enable ---------------------------------------------- */
3933 static ssize_t bluetooth_enable_show(struct device *dev,
3934 struct device_attribute *attr,
3935 char *buf)
3937 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3938 attr, buf);
3941 static ssize_t bluetooth_enable_store(struct device *dev,
3942 struct device_attribute *attr,
3943 const char *buf, size_t count)
3945 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3946 attr, buf, count);
3949 static struct device_attribute dev_attr_bluetooth_enable =
3950 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3951 bluetooth_enable_show, bluetooth_enable_store);
3953 /* --------------------------------------------------------------------- */
3955 static struct attribute *bluetooth_attributes[] = {
3956 &dev_attr_bluetooth_enable.attr,
3957 NULL
3960 static const struct attribute_group bluetooth_attr_group = {
3961 .attrs = bluetooth_attributes,
3964 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3965 .get_status = bluetooth_get_status,
3966 .set_status = bluetooth_set_status,
3969 static void bluetooth_shutdown(void)
3971 /* Order firmware to save current state to NVRAM */
3972 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3973 TP_ACPI_BLTH_SAVE_STATE))
3974 printk(TPACPI_NOTICE
3975 "failed to save bluetooth state to NVRAM\n");
3976 else
3977 vdbg_printk(TPACPI_DBG_RFKILL,
3978 "bluestooth state saved to NVRAM\n");
3981 static void bluetooth_exit(void)
3983 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3984 &bluetooth_attr_group);
3986 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3988 bluetooth_shutdown();
3991 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3993 int res;
3994 int status = 0;
3996 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3997 "initializing bluetooth subdriver\n");
3999 TPACPI_ACPIHANDLE_INIT(hkey);
4001 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4002 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4003 tp_features.bluetooth = hkey_handle &&
4004 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4006 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4007 "bluetooth is %s, status 0x%02x\n",
4008 str_supported(tp_features.bluetooth),
4009 status);
4011 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4012 if (dbg_bluetoothemul) {
4013 tp_features.bluetooth = 1;
4014 printk(TPACPI_INFO
4015 "bluetooth switch emulation enabled\n");
4016 } else
4017 #endif
4018 if (tp_features.bluetooth &&
4019 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4020 /* no bluetooth hardware present in system */
4021 tp_features.bluetooth = 0;
4022 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4023 "bluetooth hardware not installed\n");
4026 if (!tp_features.bluetooth)
4027 return 1;
4029 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4030 &bluetooth_tprfk_ops,
4031 RFKILL_TYPE_BLUETOOTH,
4032 TPACPI_RFK_BLUETOOTH_SW_NAME,
4033 true);
4034 if (res)
4035 return res;
4037 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4038 &bluetooth_attr_group);
4039 if (res) {
4040 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4041 return res;
4044 return 0;
4047 /* procfs -------------------------------------------------------------- */
4048 static int bluetooth_read(char *p)
4050 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
4053 static int bluetooth_write(char *buf)
4055 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4058 static struct ibm_struct bluetooth_driver_data = {
4059 .name = "bluetooth",
4060 .read = bluetooth_read,
4061 .write = bluetooth_write,
4062 .exit = bluetooth_exit,
4063 .shutdown = bluetooth_shutdown,
4066 /*************************************************************************
4067 * Wan subdriver
4070 enum {
4071 /* ACPI GWAN/SWAN bits */
4072 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4073 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4074 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4075 0 = disable, 1 = enable */
4078 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4080 static int wan_get_status(void)
4082 int status;
4084 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4085 if (dbg_wwanemul)
4086 return (tpacpi_wwan_emulstate) ?
4087 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4088 #endif
4090 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4091 return -EIO;
4093 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4094 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4097 static int wan_set_status(enum tpacpi_rfkill_state state)
4099 int status;
4101 vdbg_printk(TPACPI_DBG_RFKILL,
4102 "will attempt to %s wwan\n",
4103 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4105 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4106 if (dbg_wwanemul) {
4107 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4108 return 0;
4110 #endif
4112 if (state == TPACPI_RFK_RADIO_ON)
4113 status = TP_ACPI_WANCARD_RADIOSSW
4114 | TP_ACPI_WANCARD_RESUMECTRL;
4115 else
4116 status = 0;
4118 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4119 return -EIO;
4121 return 0;
4124 /* sysfs wan enable ---------------------------------------------------- */
4125 static ssize_t wan_enable_show(struct device *dev,
4126 struct device_attribute *attr,
4127 char *buf)
4129 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4130 attr, buf);
4133 static ssize_t wan_enable_store(struct device *dev,
4134 struct device_attribute *attr,
4135 const char *buf, size_t count)
4137 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4138 attr, buf, count);
4141 static struct device_attribute dev_attr_wan_enable =
4142 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4143 wan_enable_show, wan_enable_store);
4145 /* --------------------------------------------------------------------- */
4147 static struct attribute *wan_attributes[] = {
4148 &dev_attr_wan_enable.attr,
4149 NULL
4152 static const struct attribute_group wan_attr_group = {
4153 .attrs = wan_attributes,
4156 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4157 .get_status = wan_get_status,
4158 .set_status = wan_set_status,
4161 static void wan_shutdown(void)
4163 /* Order firmware to save current state to NVRAM */
4164 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4165 TP_ACPI_WGSV_SAVE_STATE))
4166 printk(TPACPI_NOTICE
4167 "failed to save WWAN state to NVRAM\n");
4168 else
4169 vdbg_printk(TPACPI_DBG_RFKILL,
4170 "WWAN state saved to NVRAM\n");
4173 static void wan_exit(void)
4175 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4176 &wan_attr_group);
4178 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4180 wan_shutdown();
4183 static int __init wan_init(struct ibm_init_struct *iibm)
4185 int res;
4186 int status = 0;
4188 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4189 "initializing wan subdriver\n");
4191 TPACPI_ACPIHANDLE_INIT(hkey);
4193 tp_features.wan = hkey_handle &&
4194 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4196 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4197 "wan is %s, status 0x%02x\n",
4198 str_supported(tp_features.wan),
4199 status);
4201 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4202 if (dbg_wwanemul) {
4203 tp_features.wan = 1;
4204 printk(TPACPI_INFO
4205 "wwan switch emulation enabled\n");
4206 } else
4207 #endif
4208 if (tp_features.wan &&
4209 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4210 /* no wan hardware present in system */
4211 tp_features.wan = 0;
4212 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4213 "wan hardware not installed\n");
4216 if (!tp_features.wan)
4217 return 1;
4219 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4220 &wan_tprfk_ops,
4221 RFKILL_TYPE_WWAN,
4222 TPACPI_RFK_WWAN_SW_NAME,
4223 true);
4224 if (res)
4225 return res;
4227 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4228 &wan_attr_group);
4230 if (res) {
4231 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4232 return res;
4235 return 0;
4238 /* procfs -------------------------------------------------------------- */
4239 static int wan_read(char *p)
4241 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
4244 static int wan_write(char *buf)
4246 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4249 static struct ibm_struct wan_driver_data = {
4250 .name = "wan",
4251 .read = wan_read,
4252 .write = wan_write,
4253 .exit = wan_exit,
4254 .shutdown = wan_shutdown,
4257 /*************************************************************************
4258 * UWB subdriver
4261 enum {
4262 /* ACPI GUWB/SUWB bits */
4263 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4264 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4267 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4269 static int uwb_get_status(void)
4271 int status;
4273 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4274 if (dbg_uwbemul)
4275 return (tpacpi_uwb_emulstate) ?
4276 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4277 #endif
4279 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4280 return -EIO;
4282 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4283 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4286 static int uwb_set_status(enum tpacpi_rfkill_state state)
4288 int status;
4290 vdbg_printk(TPACPI_DBG_RFKILL,
4291 "will attempt to %s UWB\n",
4292 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4294 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4295 if (dbg_uwbemul) {
4296 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4297 return 0;
4299 #endif
4301 if (state == TPACPI_RFK_RADIO_ON)
4302 status = TP_ACPI_UWB_RADIOSSW;
4303 else
4304 status = 0;
4306 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4307 return -EIO;
4309 return 0;
4312 /* --------------------------------------------------------------------- */
4314 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4315 .get_status = uwb_get_status,
4316 .set_status = uwb_set_status,
4319 static void uwb_exit(void)
4321 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
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 &uwb_tprfk_ops,
4362 RFKILL_TYPE_UWB,
4363 TPACPI_RFK_UWB_SW_NAME,
4364 false);
4365 return res;
4368 static struct ibm_struct uwb_driver_data = {
4369 .name = "uwb",
4370 .exit = uwb_exit,
4371 .flags.experimental = 1,
4374 /*************************************************************************
4375 * Video subdriver
4378 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4380 enum video_access_mode {
4381 TPACPI_VIDEO_NONE = 0,
4382 TPACPI_VIDEO_570, /* 570 */
4383 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4384 TPACPI_VIDEO_NEW, /* all others */
4387 enum { /* video status flags, based on VIDEO_570 */
4388 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4389 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4390 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4393 enum { /* TPACPI_VIDEO_570 constants */
4394 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4395 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4396 * video_status_flags */
4397 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4398 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4401 static enum video_access_mode video_supported;
4402 static int video_orig_autosw;
4404 static int video_autosw_get(void);
4405 static int video_autosw_set(int enable);
4407 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4409 static int __init video_init(struct ibm_init_struct *iibm)
4411 int ivga;
4413 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4415 TPACPI_ACPIHANDLE_INIT(vid);
4416 TPACPI_ACPIHANDLE_INIT(vid2);
4418 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4419 /* G41, assume IVGA doesn't change */
4420 vid_handle = vid2_handle;
4422 if (!vid_handle)
4423 /* video switching not supported on R30, R31 */
4424 video_supported = TPACPI_VIDEO_NONE;
4425 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4426 /* 570 */
4427 video_supported = TPACPI_VIDEO_570;
4428 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4429 /* 600e/x, 770e, 770x */
4430 video_supported = TPACPI_VIDEO_770;
4431 else
4432 /* all others */
4433 video_supported = TPACPI_VIDEO_NEW;
4435 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4436 str_supported(video_supported != TPACPI_VIDEO_NONE),
4437 video_supported);
4439 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4442 static void video_exit(void)
4444 dbg_printk(TPACPI_DBG_EXIT,
4445 "restoring original video autoswitch mode\n");
4446 if (video_autosw_set(video_orig_autosw))
4447 printk(TPACPI_ERR "error while trying to restore original "
4448 "video autoswitch mode\n");
4451 static int video_outputsw_get(void)
4453 int status = 0;
4454 int i;
4456 switch (video_supported) {
4457 case TPACPI_VIDEO_570:
4458 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4459 TP_ACPI_VIDEO_570_PHSCMD))
4460 return -EIO;
4461 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4462 break;
4463 case TPACPI_VIDEO_770:
4464 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4465 return -EIO;
4466 if (i)
4467 status |= TP_ACPI_VIDEO_S_LCD;
4468 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_CRT;
4472 break;
4473 case TPACPI_VIDEO_NEW:
4474 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4475 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_CRT;
4480 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4481 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4482 return -EIO;
4483 if (i)
4484 status |= TP_ACPI_VIDEO_S_LCD;
4485 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4486 return -EIO;
4487 if (i)
4488 status |= TP_ACPI_VIDEO_S_DVI;
4489 break;
4490 default:
4491 return -ENOSYS;
4494 return status;
4497 static int video_outputsw_set(int status)
4499 int autosw;
4500 int res = 0;
4502 switch (video_supported) {
4503 case TPACPI_VIDEO_570:
4504 res = acpi_evalf(NULL, NULL,
4505 "\\_SB.PHS2", "vdd",
4506 TP_ACPI_VIDEO_570_PHS2CMD,
4507 status | TP_ACPI_VIDEO_570_PHS2SET);
4508 break;
4509 case TPACPI_VIDEO_770:
4510 autosw = video_autosw_get();
4511 if (autosw < 0)
4512 return autosw;
4514 res = video_autosw_set(1);
4515 if (res)
4516 return res;
4517 res = acpi_evalf(vid_handle, NULL,
4518 "ASWT", "vdd", status * 0x100, 0);
4519 if (!autosw && video_autosw_set(autosw)) {
4520 printk(TPACPI_ERR
4521 "video auto-switch left enabled due to error\n");
4522 return -EIO;
4524 break;
4525 case TPACPI_VIDEO_NEW:
4526 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4527 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4528 break;
4529 default:
4530 return -ENOSYS;
4533 return (res)? 0 : -EIO;
4536 static int video_autosw_get(void)
4538 int autosw = 0;
4540 switch (video_supported) {
4541 case TPACPI_VIDEO_570:
4542 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4543 return -EIO;
4544 break;
4545 case TPACPI_VIDEO_770:
4546 case TPACPI_VIDEO_NEW:
4547 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4548 return -EIO;
4549 break;
4550 default:
4551 return -ENOSYS;
4554 return autosw & 1;
4557 static int video_autosw_set(int enable)
4559 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4560 return -EIO;
4561 return 0;
4564 static int video_outputsw_cycle(void)
4566 int autosw = video_autosw_get();
4567 int res;
4569 if (autosw < 0)
4570 return autosw;
4572 switch (video_supported) {
4573 case TPACPI_VIDEO_570:
4574 res = video_autosw_set(1);
4575 if (res)
4576 return res;
4577 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4578 break;
4579 case TPACPI_VIDEO_770:
4580 case TPACPI_VIDEO_NEW:
4581 res = video_autosw_set(1);
4582 if (res)
4583 return res;
4584 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4585 break;
4586 default:
4587 return -ENOSYS;
4589 if (!autosw && video_autosw_set(autosw)) {
4590 printk(TPACPI_ERR
4591 "video auto-switch left enabled due to error\n");
4592 return -EIO;
4595 return (res)? 0 : -EIO;
4598 static int video_expand_toggle(void)
4600 switch (video_supported) {
4601 case TPACPI_VIDEO_570:
4602 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4603 0 : -EIO;
4604 case TPACPI_VIDEO_770:
4605 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_NEW:
4608 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4609 0 : -EIO;
4610 default:
4611 return -ENOSYS;
4613 /* not reached */
4616 static int video_read(char *p)
4618 int status, autosw;
4619 int len = 0;
4621 if (video_supported == TPACPI_VIDEO_NONE) {
4622 len += sprintf(p + len, "status:\t\tnot supported\n");
4623 return len;
4626 status = video_outputsw_get();
4627 if (status < 0)
4628 return status;
4630 autosw = video_autosw_get();
4631 if (autosw < 0)
4632 return autosw;
4634 len += sprintf(p + len, "status:\t\tsupported\n");
4635 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4636 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4637 if (video_supported == TPACPI_VIDEO_NEW)
4638 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4639 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4640 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4641 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4642 if (video_supported == TPACPI_VIDEO_NEW)
4643 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4644 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4645 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4647 return len;
4650 static int video_write(char *buf)
4652 char *cmd;
4653 int enable, disable, status;
4654 int res;
4656 if (video_supported == TPACPI_VIDEO_NONE)
4657 return -ENODEV;
4659 enable = 0;
4660 disable = 0;
4662 while ((cmd = next_cmd(&buf))) {
4663 if (strlencmp(cmd, "lcd_enable") == 0) {
4664 enable |= TP_ACPI_VIDEO_S_LCD;
4665 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4666 disable |= TP_ACPI_VIDEO_S_LCD;
4667 } else if (strlencmp(cmd, "crt_enable") == 0) {
4668 enable |= TP_ACPI_VIDEO_S_CRT;
4669 } else if (strlencmp(cmd, "crt_disable") == 0) {
4670 disable |= TP_ACPI_VIDEO_S_CRT;
4671 } else if (video_supported == TPACPI_VIDEO_NEW &&
4672 strlencmp(cmd, "dvi_enable") == 0) {
4673 enable |= TP_ACPI_VIDEO_S_DVI;
4674 } else if (video_supported == TPACPI_VIDEO_NEW &&
4675 strlencmp(cmd, "dvi_disable") == 0) {
4676 disable |= TP_ACPI_VIDEO_S_DVI;
4677 } else if (strlencmp(cmd, "auto_enable") == 0) {
4678 res = video_autosw_set(1);
4679 if (res)
4680 return res;
4681 } else if (strlencmp(cmd, "auto_disable") == 0) {
4682 res = video_autosw_set(0);
4683 if (res)
4684 return res;
4685 } else if (strlencmp(cmd, "video_switch") == 0) {
4686 res = video_outputsw_cycle();
4687 if (res)
4688 return res;
4689 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4690 res = video_expand_toggle();
4691 if (res)
4692 return res;
4693 } else
4694 return -EINVAL;
4697 if (enable || disable) {
4698 status = video_outputsw_get();
4699 if (status < 0)
4700 return status;
4701 res = video_outputsw_set((status & ~disable) | enable);
4702 if (res)
4703 return res;
4706 return 0;
4709 static struct ibm_struct video_driver_data = {
4710 .name = "video",
4711 .read = video_read,
4712 .write = video_write,
4713 .exit = video_exit,
4716 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4718 /*************************************************************************
4719 * Light (thinklight) subdriver
4722 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4723 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4725 static int light_get_status(void)
4727 int status = 0;
4729 if (tp_features.light_status) {
4730 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4731 return -EIO;
4732 return (!!status);
4735 return -ENXIO;
4738 static int light_set_status(int status)
4740 int rc;
4742 if (tp_features.light) {
4743 if (cmos_handle) {
4744 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4745 (status)?
4746 TP_CMOS_THINKLIGHT_ON :
4747 TP_CMOS_THINKLIGHT_OFF);
4748 } else {
4749 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4750 (status)? 1 : 0);
4752 return (rc)? 0 : -EIO;
4755 return -ENXIO;
4758 static void light_set_status_worker(struct work_struct *work)
4760 struct tpacpi_led_classdev *data =
4761 container_of(work, struct tpacpi_led_classdev, work);
4763 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4764 light_set_status((data->new_state != TPACPI_LED_OFF));
4767 static void light_sysfs_set(struct led_classdev *led_cdev,
4768 enum led_brightness brightness)
4770 struct tpacpi_led_classdev *data =
4771 container_of(led_cdev,
4772 struct tpacpi_led_classdev,
4773 led_classdev);
4774 data->new_state = (brightness != LED_OFF) ?
4775 TPACPI_LED_ON : TPACPI_LED_OFF;
4776 queue_work(tpacpi_wq, &data->work);
4779 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4781 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4784 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4785 .led_classdev = {
4786 .name = "tpacpi::thinklight",
4787 .brightness_set = &light_sysfs_set,
4788 .brightness_get = &light_sysfs_get,
4792 static int __init light_init(struct ibm_init_struct *iibm)
4794 int rc;
4796 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4798 TPACPI_ACPIHANDLE_INIT(ledb);
4799 TPACPI_ACPIHANDLE_INIT(lght);
4800 TPACPI_ACPIHANDLE_INIT(cmos);
4801 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4803 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4804 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4806 if (tp_features.light)
4807 /* light status not supported on
4808 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4809 tp_features.light_status =
4810 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4812 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4813 str_supported(tp_features.light),
4814 str_supported(tp_features.light_status));
4816 if (!tp_features.light)
4817 return 1;
4819 rc = led_classdev_register(&tpacpi_pdev->dev,
4820 &tpacpi_led_thinklight.led_classdev);
4822 if (rc < 0) {
4823 tp_features.light = 0;
4824 tp_features.light_status = 0;
4825 } else {
4826 rc = 0;
4829 return rc;
4832 static void light_exit(void)
4834 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4835 if (work_pending(&tpacpi_led_thinklight.work))
4836 flush_workqueue(tpacpi_wq);
4839 static int light_read(char *p)
4841 int len = 0;
4842 int status;
4844 if (!tp_features.light) {
4845 len += sprintf(p + len, "status:\t\tnot supported\n");
4846 } else if (!tp_features.light_status) {
4847 len += sprintf(p + len, "status:\t\tunknown\n");
4848 len += sprintf(p + len, "commands:\ton, off\n");
4849 } else {
4850 status = light_get_status();
4851 if (status < 0)
4852 return status;
4853 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4854 len += sprintf(p + len, "commands:\ton, off\n");
4857 return len;
4860 static int light_write(char *buf)
4862 char *cmd;
4863 int newstatus = 0;
4865 if (!tp_features.light)
4866 return -ENODEV;
4868 while ((cmd = next_cmd(&buf))) {
4869 if (strlencmp(cmd, "on") == 0) {
4870 newstatus = 1;
4871 } else if (strlencmp(cmd, "off") == 0) {
4872 newstatus = 0;
4873 } else
4874 return -EINVAL;
4877 return light_set_status(newstatus);
4880 static struct ibm_struct light_driver_data = {
4881 .name = "light",
4882 .read = light_read,
4883 .write = light_write,
4884 .exit = light_exit,
4887 /*************************************************************************
4888 * CMOS subdriver
4891 /* sysfs cmos_command -------------------------------------------------- */
4892 static ssize_t cmos_command_store(struct device *dev,
4893 struct device_attribute *attr,
4894 const char *buf, size_t count)
4896 unsigned long cmos_cmd;
4897 int res;
4899 if (parse_strtoul(buf, 21, &cmos_cmd))
4900 return -EINVAL;
4902 res = issue_thinkpad_cmos_command(cmos_cmd);
4903 return (res)? res : count;
4906 static struct device_attribute dev_attr_cmos_command =
4907 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4909 /* --------------------------------------------------------------------- */
4911 static int __init cmos_init(struct ibm_init_struct *iibm)
4913 int res;
4915 vdbg_printk(TPACPI_DBG_INIT,
4916 "initializing cmos commands subdriver\n");
4918 TPACPI_ACPIHANDLE_INIT(cmos);
4920 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4921 str_supported(cmos_handle != NULL));
4923 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4924 if (res)
4925 return res;
4927 return (cmos_handle)? 0 : 1;
4930 static void cmos_exit(void)
4932 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4935 static int cmos_read(char *p)
4937 int len = 0;
4939 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4940 R30, R31, T20-22, X20-21 */
4941 if (!cmos_handle)
4942 len += sprintf(p + len, "status:\t\tnot supported\n");
4943 else {
4944 len += sprintf(p + len, "status:\t\tsupported\n");
4945 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4948 return len;
4951 static int cmos_write(char *buf)
4953 char *cmd;
4954 int cmos_cmd, res;
4956 while ((cmd = next_cmd(&buf))) {
4957 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4958 cmos_cmd >= 0 && cmos_cmd <= 21) {
4959 /* cmos_cmd set */
4960 } else
4961 return -EINVAL;
4963 res = issue_thinkpad_cmos_command(cmos_cmd);
4964 if (res)
4965 return res;
4968 return 0;
4971 static struct ibm_struct cmos_driver_data = {
4972 .name = "cmos",
4973 .read = cmos_read,
4974 .write = cmos_write,
4975 .exit = cmos_exit,
4978 /*************************************************************************
4979 * LED subdriver
4982 enum led_access_mode {
4983 TPACPI_LED_NONE = 0,
4984 TPACPI_LED_570, /* 570 */
4985 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4986 TPACPI_LED_NEW, /* all others */
4989 enum { /* For TPACPI_LED_OLD */
4990 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4991 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4992 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4995 static enum led_access_mode led_supported;
4997 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4998 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4999 /* T20-22, X20-21 */
5000 "LED", /* all others */
5001 ); /* R30, R31 */
5003 #define TPACPI_LED_NUMLEDS 16
5004 static struct tpacpi_led_classdev *tpacpi_leds;
5005 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5006 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5007 /* there's a limit of 19 chars + NULL before 2.6.26 */
5008 "tpacpi::power",
5009 "tpacpi:orange:batt",
5010 "tpacpi:green:batt",
5011 "tpacpi::dock_active",
5012 "tpacpi::bay_active",
5013 "tpacpi::dock_batt",
5014 "tpacpi::unknown_led",
5015 "tpacpi::standby",
5016 "tpacpi::dock_status1",
5017 "tpacpi::dock_status2",
5018 "tpacpi::unknown_led2",
5019 "tpacpi::unknown_led3",
5020 "tpacpi::thinkvantage",
5022 #define TPACPI_SAFE_LEDS 0x1081U
5024 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5026 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5027 return false;
5028 #else
5029 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5030 #endif
5033 static int led_get_status(const unsigned int led)
5035 int status;
5036 enum led_status_t led_s;
5038 switch (led_supported) {
5039 case TPACPI_LED_570:
5040 if (!acpi_evalf(ec_handle,
5041 &status, "GLED", "dd", 1 << led))
5042 return -EIO;
5043 led_s = (status == 0)?
5044 TPACPI_LED_OFF :
5045 ((status == 1)?
5046 TPACPI_LED_ON :
5047 TPACPI_LED_BLINK);
5048 tpacpi_led_state_cache[led] = led_s;
5049 return led_s;
5050 default:
5051 return -ENXIO;
5054 /* not reached */
5057 static int led_set_status(const unsigned int led,
5058 const enum led_status_t ledstatus)
5060 /* off, on, blink. Index is led_status_t */
5061 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5062 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5064 int rc = 0;
5066 switch (led_supported) {
5067 case TPACPI_LED_570:
5068 /* 570 */
5069 if (unlikely(led > 7))
5070 return -EINVAL;
5071 if (unlikely(tpacpi_is_led_restricted(led)))
5072 return -EPERM;
5073 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5074 (1 << led), led_sled_arg1[ledstatus]))
5075 rc = -EIO;
5076 break;
5077 case TPACPI_LED_OLD:
5078 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5079 if (unlikely(led > 7))
5080 return -EINVAL;
5081 if (unlikely(tpacpi_is_led_restricted(led)))
5082 return -EPERM;
5083 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5084 if (rc >= 0)
5085 rc = ec_write(TPACPI_LED_EC_HLBL,
5086 (ledstatus == TPACPI_LED_BLINK) << led);
5087 if (rc >= 0)
5088 rc = ec_write(TPACPI_LED_EC_HLCL,
5089 (ledstatus != TPACPI_LED_OFF) << led);
5090 break;
5091 case TPACPI_LED_NEW:
5092 /* all others */
5093 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5094 return -EINVAL;
5095 if (unlikely(tpacpi_is_led_restricted(led)))
5096 return -EPERM;
5097 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5098 led, led_led_arg1[ledstatus]))
5099 rc = -EIO;
5100 break;
5101 default:
5102 rc = -ENXIO;
5105 if (!rc)
5106 tpacpi_led_state_cache[led] = ledstatus;
5108 return rc;
5111 static void led_set_status_worker(struct work_struct *work)
5113 struct tpacpi_led_classdev *data =
5114 container_of(work, struct tpacpi_led_classdev, work);
5116 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5117 led_set_status(data->led, data->new_state);
5120 static void led_sysfs_set(struct led_classdev *led_cdev,
5121 enum led_brightness brightness)
5123 struct tpacpi_led_classdev *data = container_of(led_cdev,
5124 struct tpacpi_led_classdev, led_classdev);
5126 if (brightness == LED_OFF)
5127 data->new_state = TPACPI_LED_OFF;
5128 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5129 data->new_state = TPACPI_LED_ON;
5130 else
5131 data->new_state = TPACPI_LED_BLINK;
5133 queue_work(tpacpi_wq, &data->work);
5136 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5137 unsigned long *delay_on, unsigned long *delay_off)
5139 struct tpacpi_led_classdev *data = container_of(led_cdev,
5140 struct tpacpi_led_classdev, led_classdev);
5142 /* Can we choose the flash rate? */
5143 if (*delay_on == 0 && *delay_off == 0) {
5144 /* yes. set them to the hardware blink rate (1 Hz) */
5145 *delay_on = 500; /* ms */
5146 *delay_off = 500; /* ms */
5147 } else if ((*delay_on != 500) || (*delay_off != 500))
5148 return -EINVAL;
5150 data->new_state = TPACPI_LED_BLINK;
5151 queue_work(tpacpi_wq, &data->work);
5153 return 0;
5156 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5158 int rc;
5160 struct tpacpi_led_classdev *data = container_of(led_cdev,
5161 struct tpacpi_led_classdev, led_classdev);
5163 rc = led_get_status(data->led);
5165 if (rc == TPACPI_LED_OFF || rc < 0)
5166 rc = LED_OFF; /* no error handling in led class :( */
5167 else
5168 rc = LED_FULL;
5170 return rc;
5173 static void led_exit(void)
5175 unsigned int i;
5177 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5178 if (tpacpi_leds[i].led_classdev.name)
5179 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5182 kfree(tpacpi_leds);
5185 static int __init tpacpi_init_led(unsigned int led)
5187 int rc;
5189 tpacpi_leds[led].led = led;
5191 /* LEDs with no name don't get registered */
5192 if (!tpacpi_led_names[led])
5193 return 0;
5195 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5196 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5197 if (led_supported == TPACPI_LED_570)
5198 tpacpi_leds[led].led_classdev.brightness_get =
5199 &led_sysfs_get;
5201 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5203 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5205 rc = led_classdev_register(&tpacpi_pdev->dev,
5206 &tpacpi_leds[led].led_classdev);
5207 if (rc < 0)
5208 tpacpi_leds[led].led_classdev.name = NULL;
5210 return rc;
5213 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5214 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5215 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5216 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5218 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5219 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5220 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5221 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5222 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5223 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5224 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5225 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5227 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5228 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5229 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5230 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5231 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5233 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5234 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5235 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5236 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5238 /* (1) - may have excess leds enabled on MSB */
5240 /* Defaults (order matters, keep last, don't reorder!) */
5241 { /* Lenovo */
5242 .vendor = PCI_VENDOR_ID_LENOVO,
5243 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5244 .quirks = 0x1fffU,
5246 { /* IBM ThinkPads with no EC version string */
5247 .vendor = PCI_VENDOR_ID_IBM,
5248 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5249 .quirks = 0x00ffU,
5251 { /* IBM ThinkPads with EC version string */
5252 .vendor = PCI_VENDOR_ID_IBM,
5253 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5254 .quirks = 0x00bfU,
5258 #undef TPACPI_LEDQ_IBM
5259 #undef TPACPI_LEDQ_LNV
5261 static int __init led_init(struct ibm_init_struct *iibm)
5263 unsigned int i;
5264 int rc;
5265 unsigned long useful_leds;
5267 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5269 TPACPI_ACPIHANDLE_INIT(led);
5271 if (!led_handle)
5272 /* led not supported on R30, R31 */
5273 led_supported = TPACPI_LED_NONE;
5274 else if (strlencmp(led_path, "SLED") == 0)
5275 /* 570 */
5276 led_supported = TPACPI_LED_570;
5277 else if (strlencmp(led_path, "SYSL") == 0)
5278 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5279 led_supported = TPACPI_LED_OLD;
5280 else
5281 /* all others */
5282 led_supported = TPACPI_LED_NEW;
5284 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5285 str_supported(led_supported), led_supported);
5287 if (led_supported == TPACPI_LED_NONE)
5288 return 1;
5290 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5291 GFP_KERNEL);
5292 if (!tpacpi_leds) {
5293 printk(TPACPI_ERR "Out of memory for LED data\n");
5294 return -ENOMEM;
5297 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5298 ARRAY_SIZE(led_useful_qtable));
5300 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5301 if (!tpacpi_is_led_restricted(i) &&
5302 test_bit(i, &useful_leds)) {
5303 rc = tpacpi_init_led(i);
5304 if (rc < 0) {
5305 led_exit();
5306 return rc;
5311 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5312 printk(TPACPI_NOTICE
5313 "warning: userspace override of important "
5314 "firmware LEDs is enabled\n");
5315 #endif
5316 return 0;
5319 #define str_led_status(s) \
5320 ((s) == TPACPI_LED_OFF ? "off" : \
5321 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5323 static int led_read(char *p)
5325 int len = 0;
5327 if (!led_supported) {
5328 len += sprintf(p + len, "status:\t\tnot supported\n");
5329 return len;
5331 len += sprintf(p + len, "status:\t\tsupported\n");
5333 if (led_supported == TPACPI_LED_570) {
5334 /* 570 */
5335 int i, status;
5336 for (i = 0; i < 8; i++) {
5337 status = led_get_status(i);
5338 if (status < 0)
5339 return -EIO;
5340 len += sprintf(p + len, "%d:\t\t%s\n",
5341 i, str_led_status(status));
5345 len += sprintf(p + len, "commands:\t"
5346 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5348 return len;
5351 static int led_write(char *buf)
5353 char *cmd;
5354 int led, rc;
5355 enum led_status_t s;
5357 if (!led_supported)
5358 return -ENODEV;
5360 while ((cmd = next_cmd(&buf))) {
5361 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5362 return -EINVAL;
5364 if (strstr(cmd, "off")) {
5365 s = TPACPI_LED_OFF;
5366 } else if (strstr(cmd, "on")) {
5367 s = TPACPI_LED_ON;
5368 } else if (strstr(cmd, "blink")) {
5369 s = TPACPI_LED_BLINK;
5370 } else {
5371 return -EINVAL;
5374 rc = led_set_status(led, s);
5375 if (rc < 0)
5376 return rc;
5379 return 0;
5382 static struct ibm_struct led_driver_data = {
5383 .name = "led",
5384 .read = led_read,
5385 .write = led_write,
5386 .exit = led_exit,
5389 /*************************************************************************
5390 * Beep subdriver
5393 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5395 #define TPACPI_BEEP_Q1 0x0001
5397 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5398 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5399 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5402 static int __init beep_init(struct ibm_init_struct *iibm)
5404 unsigned long quirks;
5406 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5408 TPACPI_ACPIHANDLE_INIT(beep);
5410 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5411 str_supported(beep_handle != NULL));
5413 quirks = tpacpi_check_quirks(beep_quirk_table,
5414 ARRAY_SIZE(beep_quirk_table));
5416 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5418 return (beep_handle)? 0 : 1;
5421 static int beep_read(char *p)
5423 int len = 0;
5425 if (!beep_handle)
5426 len += sprintf(p + len, "status:\t\tnot supported\n");
5427 else {
5428 len += sprintf(p + len, "status:\t\tsupported\n");
5429 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5432 return len;
5435 static int beep_write(char *buf)
5437 char *cmd;
5438 int beep_cmd;
5440 if (!beep_handle)
5441 return -ENODEV;
5443 while ((cmd = next_cmd(&buf))) {
5444 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5445 beep_cmd >= 0 && beep_cmd <= 17) {
5446 /* beep_cmd set */
5447 } else
5448 return -EINVAL;
5449 if (tp_features.beep_needs_two_args) {
5450 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5451 beep_cmd, 0))
5452 return -EIO;
5453 } else {
5454 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5455 beep_cmd))
5456 return -EIO;
5460 return 0;
5463 static struct ibm_struct beep_driver_data = {
5464 .name = "beep",
5465 .read = beep_read,
5466 .write = beep_write,
5469 /*************************************************************************
5470 * Thermal subdriver
5473 enum thermal_access_mode {
5474 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5475 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5476 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5477 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5478 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5481 enum { /* TPACPI_THERMAL_TPEC_* */
5482 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5483 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5484 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5486 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5490 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5491 struct ibm_thermal_sensors_struct {
5492 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5495 static enum thermal_access_mode thermal_read_mode;
5497 /* idx is zero-based */
5498 static int thermal_get_sensor(int idx, s32 *value)
5500 int t;
5501 s8 tmp;
5502 char tmpi[5];
5504 t = TP_EC_THERMAL_TMP0;
5506 switch (thermal_read_mode) {
5507 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5508 case TPACPI_THERMAL_TPEC_16:
5509 if (idx >= 8 && idx <= 15) {
5510 t = TP_EC_THERMAL_TMP8;
5511 idx -= 8;
5513 /* fallthrough */
5514 #endif
5515 case TPACPI_THERMAL_TPEC_8:
5516 if (idx <= 7) {
5517 if (!acpi_ec_read(t + idx, &tmp))
5518 return -EIO;
5519 *value = tmp * 1000;
5520 return 0;
5522 break;
5524 case TPACPI_THERMAL_ACPI_UPDT:
5525 if (idx <= 7) {
5526 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5527 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5528 return -EIO;
5529 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5530 return -EIO;
5531 *value = (t - 2732) * 100;
5532 return 0;
5534 break;
5536 case TPACPI_THERMAL_ACPI_TMP07:
5537 if (idx <= 7) {
5538 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5539 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5540 return -EIO;
5541 if (t > 127 || t < -127)
5542 t = TP_EC_THERMAL_TMP_NA;
5543 *value = t * 1000;
5544 return 0;
5546 break;
5548 case TPACPI_THERMAL_NONE:
5549 default:
5550 return -ENOSYS;
5553 return -EINVAL;
5556 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5558 int res, i;
5559 int n;
5561 n = 8;
5562 i = 0;
5564 if (!s)
5565 return -EINVAL;
5567 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5568 n = 16;
5570 for (i = 0 ; i < n; i++) {
5571 res = thermal_get_sensor(i, &s->temp[i]);
5572 if (res)
5573 return res;
5576 return n;
5579 static void thermal_dump_all_sensors(void)
5581 int n, i;
5582 struct ibm_thermal_sensors_struct t;
5584 n = thermal_get_sensors(&t);
5585 if (n <= 0)
5586 return;
5588 printk(TPACPI_NOTICE
5589 "temperatures (Celsius):");
5591 for (i = 0; i < n; i++) {
5592 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5593 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5594 else
5595 printk(KERN_CONT " N/A");
5598 printk(KERN_CONT "\n");
5601 /* sysfs temp##_input -------------------------------------------------- */
5603 static ssize_t thermal_temp_input_show(struct device *dev,
5604 struct device_attribute *attr,
5605 char *buf)
5607 struct sensor_device_attribute *sensor_attr =
5608 to_sensor_dev_attr(attr);
5609 int idx = sensor_attr->index;
5610 s32 value;
5611 int res;
5613 res = thermal_get_sensor(idx, &value);
5614 if (res)
5615 return res;
5616 if (value == TPACPI_THERMAL_SENSOR_NA)
5617 return -ENXIO;
5619 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5622 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5623 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5624 thermal_temp_input_show, NULL, _idxB)
5626 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5627 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5628 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5629 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5630 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5631 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5632 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5633 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5634 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5635 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5636 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5637 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5638 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5639 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5640 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5641 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5642 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5645 #define THERMAL_ATTRS(X) \
5646 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5648 static struct attribute *thermal_temp_input_attr[] = {
5649 THERMAL_ATTRS(8),
5650 THERMAL_ATTRS(9),
5651 THERMAL_ATTRS(10),
5652 THERMAL_ATTRS(11),
5653 THERMAL_ATTRS(12),
5654 THERMAL_ATTRS(13),
5655 THERMAL_ATTRS(14),
5656 THERMAL_ATTRS(15),
5657 THERMAL_ATTRS(0),
5658 THERMAL_ATTRS(1),
5659 THERMAL_ATTRS(2),
5660 THERMAL_ATTRS(3),
5661 THERMAL_ATTRS(4),
5662 THERMAL_ATTRS(5),
5663 THERMAL_ATTRS(6),
5664 THERMAL_ATTRS(7),
5665 NULL
5668 static const struct attribute_group thermal_temp_input16_group = {
5669 .attrs = thermal_temp_input_attr
5672 static const struct attribute_group thermal_temp_input8_group = {
5673 .attrs = &thermal_temp_input_attr[8]
5676 #undef THERMAL_SENSOR_ATTR_TEMP
5677 #undef THERMAL_ATTRS
5679 /* --------------------------------------------------------------------- */
5681 static int __init thermal_init(struct ibm_init_struct *iibm)
5683 u8 t, ta1, ta2;
5684 int i;
5685 int acpi_tmp7;
5686 int res;
5688 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5690 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5692 if (thinkpad_id.ec_model) {
5694 * Direct EC access mode: sensors at registers
5695 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5696 * non-implemented, thermal sensors return 0x80 when
5697 * not available
5700 ta1 = ta2 = 0;
5701 for (i = 0; i < 8; i++) {
5702 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5703 ta1 |= t;
5704 } else {
5705 ta1 = 0;
5706 break;
5708 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5709 ta2 |= t;
5710 } else {
5711 ta1 = 0;
5712 break;
5715 if (ta1 == 0) {
5716 /* This is sheer paranoia, but we handle it anyway */
5717 if (acpi_tmp7) {
5718 printk(TPACPI_ERR
5719 "ThinkPad ACPI EC access misbehaving, "
5720 "falling back to ACPI TMPx access "
5721 "mode\n");
5722 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5723 } else {
5724 printk(TPACPI_ERR
5725 "ThinkPad ACPI EC access misbehaving, "
5726 "disabling thermal sensors access\n");
5727 thermal_read_mode = TPACPI_THERMAL_NONE;
5729 } else {
5730 thermal_read_mode =
5731 (ta2 != 0) ?
5732 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5734 } else if (acpi_tmp7) {
5735 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5736 /* 600e/x, 770e, 770x */
5737 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5738 } else {
5739 /* Standard ACPI TMPx access, max 8 sensors */
5740 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5742 } else {
5743 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5744 thermal_read_mode = TPACPI_THERMAL_NONE;
5747 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5748 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5749 thermal_read_mode);
5751 switch (thermal_read_mode) {
5752 case TPACPI_THERMAL_TPEC_16:
5753 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5754 &thermal_temp_input16_group);
5755 if (res)
5756 return res;
5757 break;
5758 case TPACPI_THERMAL_TPEC_8:
5759 case TPACPI_THERMAL_ACPI_TMP07:
5760 case TPACPI_THERMAL_ACPI_UPDT:
5761 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5762 &thermal_temp_input8_group);
5763 if (res)
5764 return res;
5765 break;
5766 case TPACPI_THERMAL_NONE:
5767 default:
5768 return 1;
5771 return 0;
5774 static void thermal_exit(void)
5776 switch (thermal_read_mode) {
5777 case TPACPI_THERMAL_TPEC_16:
5778 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5779 &thermal_temp_input16_group);
5780 break;
5781 case TPACPI_THERMAL_TPEC_8:
5782 case TPACPI_THERMAL_ACPI_TMP07:
5783 case TPACPI_THERMAL_ACPI_UPDT:
5784 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5785 &thermal_temp_input8_group);
5786 break;
5787 case TPACPI_THERMAL_NONE:
5788 default:
5789 break;
5793 static int thermal_read(char *p)
5795 int len = 0;
5796 int n, i;
5797 struct ibm_thermal_sensors_struct t;
5799 n = thermal_get_sensors(&t);
5800 if (unlikely(n < 0))
5801 return n;
5803 len += sprintf(p + len, "temperatures:\t");
5805 if (n > 0) {
5806 for (i = 0; i < (n - 1); i++)
5807 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5808 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5809 } else
5810 len += sprintf(p + len, "not supported\n");
5812 return len;
5815 static struct ibm_struct thermal_driver_data = {
5816 .name = "thermal",
5817 .read = thermal_read,
5818 .exit = thermal_exit,
5821 /*************************************************************************
5822 * EC Dump subdriver
5825 static u8 ecdump_regs[256];
5827 static int ecdump_read(char *p)
5829 int len = 0;
5830 int i, j;
5831 u8 v;
5833 len += sprintf(p + len, "EC "
5834 " +00 +01 +02 +03 +04 +05 +06 +07"
5835 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5836 for (i = 0; i < 256; i += 16) {
5837 len += sprintf(p + len, "EC 0x%02x:", i);
5838 for (j = 0; j < 16; j++) {
5839 if (!acpi_ec_read(i + j, &v))
5840 break;
5841 if (v != ecdump_regs[i + j])
5842 len += sprintf(p + len, " *%02x", v);
5843 else
5844 len += sprintf(p + len, " %02x", v);
5845 ecdump_regs[i + j] = v;
5847 len += sprintf(p + len, "\n");
5848 if (j != 16)
5849 break;
5852 /* These are way too dangerous to advertise openly... */
5853 #if 0
5854 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5855 " (<offset> is 00-ff, <value> is 00-ff)\n");
5856 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5857 " (<offset> is 00-ff, <value> is 0-255)\n");
5858 #endif
5859 return len;
5862 static int ecdump_write(char *buf)
5864 char *cmd;
5865 int i, v;
5867 while ((cmd = next_cmd(&buf))) {
5868 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5869 /* i and v set */
5870 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5871 /* i and v set */
5872 } else
5873 return -EINVAL;
5874 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5875 if (!acpi_ec_write(i, v))
5876 return -EIO;
5877 } else
5878 return -EINVAL;
5881 return 0;
5884 static struct ibm_struct ecdump_driver_data = {
5885 .name = "ecdump",
5886 .read = ecdump_read,
5887 .write = ecdump_write,
5888 .flags.experimental = 1,
5891 /*************************************************************************
5892 * Backlight/brightness subdriver
5895 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5898 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5899 * CMOS NVRAM byte 0x5E, bits 0-3.
5901 * EC HBRV (0x31) has the following layout
5902 * Bit 7: unknown function
5903 * Bit 6: unknown function
5904 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5905 * Bit 4: must be set to zero to avoid problems
5906 * Bit 3-0: backlight brightness level
5908 * brightness_get_raw returns status data in the HBRV layout
5910 * WARNING: The X61 has been verified to use HBRV for something else, so
5911 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5912 * testing on the very early *60 Lenovo models...
5915 enum {
5916 TP_EC_BACKLIGHT = 0x31,
5918 /* TP_EC_BACKLIGHT bitmasks */
5919 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5920 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5921 TP_EC_BACKLIGHT_MAPSW = 0x20,
5924 enum tpacpi_brightness_access_mode {
5925 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5926 TPACPI_BRGHT_MODE_EC, /* EC control */
5927 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5928 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5929 TPACPI_BRGHT_MODE_MAX
5932 static struct backlight_device *ibm_backlight_device;
5934 static enum tpacpi_brightness_access_mode brightness_mode =
5935 TPACPI_BRGHT_MODE_MAX;
5937 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5939 static struct mutex brightness_mutex;
5941 /* NVRAM brightness access,
5942 * call with brightness_mutex held! */
5943 static unsigned int tpacpi_brightness_nvram_get(void)
5945 u8 lnvram;
5947 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5948 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5949 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5950 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5952 return lnvram;
5955 static void tpacpi_brightness_checkpoint_nvram(void)
5957 u8 lec = 0;
5958 u8 b_nvram;
5960 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5961 return;
5963 vdbg_printk(TPACPI_DBG_BRGHT,
5964 "trying to checkpoint backlight level to NVRAM...\n");
5966 if (mutex_lock_killable(&brightness_mutex) < 0)
5967 return;
5969 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5970 goto unlock;
5971 lec &= TP_EC_BACKLIGHT_LVLMSK;
5972 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5974 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5975 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5976 /* NVRAM needs update */
5977 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5978 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5979 b_nvram |= lec;
5980 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5981 dbg_printk(TPACPI_DBG_BRGHT,
5982 "updated NVRAM backlight level to %u (0x%02x)\n",
5983 (unsigned int) lec, (unsigned int) b_nvram);
5984 } else
5985 vdbg_printk(TPACPI_DBG_BRGHT,
5986 "NVRAM backlight level already is %u (0x%02x)\n",
5987 (unsigned int) lec, (unsigned int) b_nvram);
5989 unlock:
5990 mutex_unlock(&brightness_mutex);
5994 /* call with brightness_mutex held! */
5995 static int tpacpi_brightness_get_raw(int *status)
5997 u8 lec = 0;
5999 switch (brightness_mode) {
6000 case TPACPI_BRGHT_MODE_UCMS_STEP:
6001 *status = tpacpi_brightness_nvram_get();
6002 return 0;
6003 case TPACPI_BRGHT_MODE_EC:
6004 case TPACPI_BRGHT_MODE_ECNVRAM:
6005 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6006 return -EIO;
6007 *status = lec;
6008 return 0;
6009 default:
6010 return -ENXIO;
6014 /* call with brightness_mutex held! */
6015 /* do NOT call with illegal backlight level value */
6016 static int tpacpi_brightness_set_ec(unsigned int value)
6018 u8 lec = 0;
6020 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6021 return -EIO;
6023 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6024 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6025 (value & TP_EC_BACKLIGHT_LVLMSK))))
6026 return -EIO;
6028 return 0;
6031 /* call with brightness_mutex held! */
6032 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6034 int cmos_cmd, inc;
6035 unsigned int current_value, i;
6037 current_value = tpacpi_brightness_nvram_get();
6039 if (value == current_value)
6040 return 0;
6042 cmos_cmd = (value > current_value) ?
6043 TP_CMOS_BRIGHTNESS_UP :
6044 TP_CMOS_BRIGHTNESS_DOWN;
6045 inc = (value > current_value) ? 1 : -1;
6047 for (i = current_value; i != value; i += inc)
6048 if (issue_thinkpad_cmos_command(cmos_cmd))
6049 return -EIO;
6051 return 0;
6054 /* May return EINTR which can always be mapped to ERESTARTSYS */
6055 static int brightness_set(unsigned int value)
6057 int res;
6059 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6060 value < 0)
6061 return -EINVAL;
6063 vdbg_printk(TPACPI_DBG_BRGHT,
6064 "set backlight level to %d\n", value);
6066 res = mutex_lock_killable(&brightness_mutex);
6067 if (res < 0)
6068 return res;
6070 switch (brightness_mode) {
6071 case TPACPI_BRGHT_MODE_EC:
6072 case TPACPI_BRGHT_MODE_ECNVRAM:
6073 res = tpacpi_brightness_set_ec(value);
6074 break;
6075 case TPACPI_BRGHT_MODE_UCMS_STEP:
6076 res = tpacpi_brightness_set_ucmsstep(value);
6077 break;
6078 default:
6079 res = -ENXIO;
6082 mutex_unlock(&brightness_mutex);
6083 return res;
6086 /* sysfs backlight class ----------------------------------------------- */
6088 static int brightness_update_status(struct backlight_device *bd)
6090 unsigned int level =
6091 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6092 bd->props.power == FB_BLANK_UNBLANK) ?
6093 bd->props.brightness : 0;
6095 dbg_printk(TPACPI_DBG_BRGHT,
6096 "backlight: attempt to set level to %d\n",
6097 level);
6099 /* it is the backlight class's job (caller) to handle
6100 * EINTR and other errors properly */
6101 return brightness_set(level);
6104 static int brightness_get(struct backlight_device *bd)
6106 int status, res;
6108 res = mutex_lock_killable(&brightness_mutex);
6109 if (res < 0)
6110 return 0;
6112 res = tpacpi_brightness_get_raw(&status);
6114 mutex_unlock(&brightness_mutex);
6116 if (res < 0)
6117 return 0;
6119 return status & TP_EC_BACKLIGHT_LVLMSK;
6122 static void tpacpi_brightness_notify_change(void)
6124 backlight_force_update(ibm_backlight_device,
6125 BACKLIGHT_UPDATE_HOTKEY);
6128 static struct backlight_ops ibm_backlight_data = {
6129 .get_brightness = brightness_get,
6130 .update_status = brightness_update_status,
6133 /* --------------------------------------------------------------------- */
6136 * These are only useful for models that have only one possibility
6137 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6138 * these quirks.
6140 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6141 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6142 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6144 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6145 /* Models with ATI GPUs known to require ECNVRAM mode */
6146 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6148 /* Models with ATI GPUs that can use ECNVRAM */
6149 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6150 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6151 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6152 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6154 /* Models with Intel Extreme Graphics 2 */
6155 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6156 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6157 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6159 /* Models with Intel GMA900 */
6160 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6161 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6162 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6165 static int __init brightness_init(struct ibm_init_struct *iibm)
6167 int b;
6168 unsigned long quirks;
6170 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6172 mutex_init(&brightness_mutex);
6174 quirks = tpacpi_check_quirks(brightness_quirk_table,
6175 ARRAY_SIZE(brightness_quirk_table));
6178 * We always attempt to detect acpi support, so as to switch
6179 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6180 * going to publish a backlight interface
6182 b = tpacpi_check_std_acpi_brightness_support();
6183 if (b > 0) {
6185 if (acpi_video_backlight_support()) {
6186 if (brightness_enable > 1) {
6187 printk(TPACPI_NOTICE
6188 "Standard ACPI backlight interface "
6189 "available, not loading native one.\n");
6190 return 1;
6191 } else if (brightness_enable == 1) {
6192 printk(TPACPI_NOTICE
6193 "Backlight control force enabled, even if standard "
6194 "ACPI backlight interface is available\n");
6196 } else {
6197 if (brightness_enable > 1) {
6198 printk(TPACPI_NOTICE
6199 "Standard ACPI backlight interface not "
6200 "available, thinkpad_acpi native "
6201 "brightness control enabled\n");
6206 if (!brightness_enable) {
6207 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6208 "brightness support disabled by "
6209 "module parameter\n");
6210 return 1;
6213 if (b > 16) {
6214 printk(TPACPI_ERR
6215 "Unsupported brightness interface, "
6216 "please contact %s\n", TPACPI_MAIL);
6217 return 1;
6219 if (b == 16)
6220 tp_features.bright_16levels = 1;
6223 * Check for module parameter bogosity, note that we
6224 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6225 * able to detect "unspecified"
6227 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6228 return -EINVAL;
6230 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6231 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6232 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6233 if (quirks & TPACPI_BRGHT_Q_EC)
6234 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6235 else
6236 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6238 dbg_printk(TPACPI_DBG_BRGHT,
6239 "driver auto-selected brightness_mode=%d\n",
6240 brightness_mode);
6243 /* Safety */
6244 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6245 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6246 brightness_mode == TPACPI_BRGHT_MODE_EC))
6247 return -EINVAL;
6249 if (tpacpi_brightness_get_raw(&b) < 0)
6250 return 1;
6252 if (tp_features.bright_16levels)
6253 printk(TPACPI_INFO
6254 "detected a 16-level brightness capable ThinkPad\n");
6256 ibm_backlight_device = backlight_device_register(
6257 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6258 &ibm_backlight_data);
6259 if (IS_ERR(ibm_backlight_device)) {
6260 int rc = PTR_ERR(ibm_backlight_device);
6261 ibm_backlight_device = NULL;
6262 printk(TPACPI_ERR "Could not register backlight device\n");
6263 return rc;
6265 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6266 "brightness is supported\n");
6268 if (quirks & TPACPI_BRGHT_Q_ASK) {
6269 printk(TPACPI_NOTICE
6270 "brightness: will use unverified default: "
6271 "brightness_mode=%d\n", brightness_mode);
6272 printk(TPACPI_NOTICE
6273 "brightness: please report to %s whether it works well "
6274 "or not on your ThinkPad\n", TPACPI_MAIL);
6277 ibm_backlight_device->props.max_brightness =
6278 (tp_features.bright_16levels)? 15 : 7;
6279 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6280 backlight_update_status(ibm_backlight_device);
6282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6283 "brightness: registering brightness hotkeys "
6284 "as change notification\n");
6285 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6286 | TP_ACPI_HKEY_BRGHTUP_MASK
6287 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6288 return 0;
6291 static void brightness_suspend(pm_message_t state)
6293 tpacpi_brightness_checkpoint_nvram();
6296 static void brightness_shutdown(void)
6298 tpacpi_brightness_checkpoint_nvram();
6301 static void brightness_exit(void)
6303 if (ibm_backlight_device) {
6304 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6305 "calling backlight_device_unregister()\n");
6306 backlight_device_unregister(ibm_backlight_device);
6309 tpacpi_brightness_checkpoint_nvram();
6312 static int brightness_read(char *p)
6314 int len = 0;
6315 int level;
6317 level = brightness_get(NULL);
6318 if (level < 0) {
6319 len += sprintf(p + len, "level:\t\tunreadable\n");
6320 } else {
6321 len += sprintf(p + len, "level:\t\t%d\n", level);
6322 len += sprintf(p + len, "commands:\tup, down\n");
6323 len += sprintf(p + len, "commands:\tlevel <level>"
6324 " (<level> is 0-%d)\n",
6325 (tp_features.bright_16levels) ? 15 : 7);
6328 return len;
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
6383 static int volume_offset = 0x30;
6385 static int volume_read(char *p)
6387 int len = 0;
6388 u8 level;
6390 if (!acpi_ec_read(volume_offset, &level)) {
6391 len += sprintf(p + len, "level:\t\tunreadable\n");
6392 } else {
6393 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6394 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6395 len += sprintf(p + len, "commands:\tup, down, mute\n");
6396 len += sprintf(p + len, "commands:\tlevel <level>"
6397 " (<level> is 0-15)\n");
6400 return len;
6403 static int volume_write(char *buf)
6405 int cmos_cmd, inc, i;
6406 u8 level, mute;
6407 int new_level, new_mute;
6408 char *cmd;
6410 while ((cmd = next_cmd(&buf))) {
6411 if (!acpi_ec_read(volume_offset, &level))
6412 return -EIO;
6413 new_mute = mute = level & 0x40;
6414 new_level = level = level & 0xf;
6416 if (strlencmp(cmd, "up") == 0) {
6417 if (mute)
6418 new_mute = 0;
6419 else
6420 new_level = level == 15 ? 15 : level + 1;
6421 } else if (strlencmp(cmd, "down") == 0) {
6422 if (mute)
6423 new_mute = 0;
6424 else
6425 new_level = level == 0 ? 0 : level - 1;
6426 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6427 new_level >= 0 && new_level <= 15) {
6428 /* new_level set */
6429 } else if (strlencmp(cmd, "mute") == 0) {
6430 new_mute = 0x40;
6431 } else
6432 return -EINVAL;
6434 if (new_level != level) {
6435 /* mute doesn't change */
6437 cmos_cmd = (new_level > level) ?
6438 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6439 inc = new_level > level ? 1 : -1;
6441 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6442 !acpi_ec_write(volume_offset, level)))
6443 return -EIO;
6445 for (i = level; i != new_level; i += inc)
6446 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6447 !acpi_ec_write(volume_offset, i + inc))
6448 return -EIO;
6450 if (mute &&
6451 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6452 !acpi_ec_write(volume_offset, new_level + mute))) {
6453 return -EIO;
6457 if (new_mute != mute) {
6458 /* level doesn't change */
6460 cmos_cmd = (new_mute) ?
6461 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6463 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6464 !acpi_ec_write(volume_offset, level + new_mute))
6465 return -EIO;
6469 return 0;
6472 static struct ibm_struct volume_driver_data = {
6473 .name = "volume",
6474 .read = volume_read,
6475 .write = volume_write,
6478 /*************************************************************************
6479 * Fan subdriver
6483 * FAN ACCESS MODES
6485 * TPACPI_FAN_RD_ACPI_GFAN:
6486 * ACPI GFAN method: returns fan level
6488 * see TPACPI_FAN_WR_ACPI_SFAN
6489 * EC 0x2f (HFSP) not available if GFAN exists
6491 * TPACPI_FAN_WR_ACPI_SFAN:
6492 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6494 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6495 * it for writing.
6497 * TPACPI_FAN_WR_TPEC:
6498 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6499 * Supported on almost all ThinkPads
6501 * Fan speed changes of any sort (including those caused by the
6502 * disengaged mode) are usually done slowly by the firmware as the
6503 * maximum ammount of fan duty cycle change per second seems to be
6504 * limited.
6506 * Reading is not available if GFAN exists.
6507 * Writing is not available if SFAN exists.
6509 * Bits
6510 * 7 automatic mode engaged;
6511 * (default operation mode of the ThinkPad)
6512 * fan level is ignored in this mode.
6513 * 6 full speed mode (takes precedence over bit 7);
6514 * not available on all thinkpads. May disable
6515 * the tachometer while the fan controller ramps up
6516 * the speed (which can take up to a few *minutes*).
6517 * Speeds up fan to 100% duty-cycle, which is far above
6518 * the standard RPM levels. It is not impossible that
6519 * it could cause hardware damage.
6520 * 5-3 unused in some models. Extra bits for fan level
6521 * in others, but still useless as all values above
6522 * 7 map to the same speed as level 7 in these models.
6523 * 2-0 fan level (0..7 usually)
6524 * 0x00 = stop
6525 * 0x07 = max (set when temperatures critical)
6526 * Some ThinkPads may have other levels, see
6527 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6529 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6530 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6531 * does so, its initial value is meaningless (0x07).
6533 * For firmware bugs, refer to:
6534 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6536 * ----
6538 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6539 * Main fan tachometer reading (in RPM)
6541 * This register is present on all ThinkPads with a new-style EC, and
6542 * it is known not to be present on the A21m/e, and T22, as there is
6543 * something else in offset 0x84 according to the ACPI DSDT. Other
6544 * ThinkPads from this same time period (and earlier) probably lack the
6545 * tachometer as well.
6547 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6548 * was never fixed by IBM to report the EC firmware version string
6549 * probably support the tachometer (like the early X models), so
6550 * detecting it is quite hard. We need more data to know for sure.
6552 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6553 * might result.
6555 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6556 * mode.
6558 * For firmware bugs, refer to:
6559 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6561 * ----
6563 * ThinkPad EC register 0x31 bit 0 (only on select models)
6565 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6566 * show the speed of the main fan. When bit 0 of EC register 0x31
6567 * is one, the tachometer registers show the speed of the auxiliary
6568 * fan.
6570 * Fan control seems to affect both fans, regardless of the state
6571 * of this bit.
6573 * So far, only the firmware for the X60/X61 non-tablet versions
6574 * seem to support this (firmware TP-7M).
6576 * TPACPI_FAN_WR_ACPI_FANS:
6577 * ThinkPad X31, X40, X41. Not available in the X60.
6579 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6580 * high speed. ACPI DSDT seems to map these three speeds to levels
6581 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6582 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6584 * The speeds are stored on handles
6585 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6587 * There are three default speed sets, acessible as handles:
6588 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6590 * ACPI DSDT switches which set is in use depending on various
6591 * factors.
6593 * TPACPI_FAN_WR_TPEC is also available and should be used to
6594 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6595 * but the ACPI tables just mention level 7.
6598 enum { /* Fan control constants */
6599 fan_status_offset = 0x2f, /* EC register 0x2f */
6600 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6601 * 0x84 must be read before 0x85 */
6602 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6603 bit 0 selects which fan is active */
6605 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6606 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6608 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6611 enum fan_status_access_mode {
6612 TPACPI_FAN_NONE = 0, /* No fan status or control */
6613 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6614 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6617 enum fan_control_access_mode {
6618 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6619 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6620 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6621 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6624 enum fan_control_commands {
6625 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6626 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6627 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6628 * and also watchdog cmd */
6631 static int fan_control_allowed;
6633 static enum fan_status_access_mode fan_status_access_mode;
6634 static enum fan_control_access_mode fan_control_access_mode;
6635 static enum fan_control_commands fan_control_commands;
6637 static u8 fan_control_initial_status;
6638 static u8 fan_control_desired_level;
6639 static u8 fan_control_resume_level;
6640 static int fan_watchdog_maxinterval;
6642 static struct mutex fan_mutex;
6644 static void fan_watchdog_fire(struct work_struct *ignored);
6645 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6647 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6648 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6649 "\\FSPD", /* 600e/x, 770e, 770x */
6650 ); /* all others */
6651 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6652 "JFNS", /* 770x-JL */
6653 ); /* all others */
6656 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6657 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6658 * be in auto mode (0x80).
6660 * This is corrected by any write to HFSP either by the driver, or
6661 * by the firmware.
6663 * We assume 0x07 really means auto mode while this quirk is active,
6664 * as this is far more likely than the ThinkPad being in level 7,
6665 * which is only used by the firmware during thermal emergencies.
6667 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6668 * TP-70 (T43, R52), which are known to be buggy.
6671 static void fan_quirk1_setup(void)
6673 if (fan_control_initial_status == 0x07) {
6674 printk(TPACPI_NOTICE
6675 "fan_init: initial fan status is unknown, "
6676 "assuming it is in auto mode\n");
6677 tp_features.fan_ctrl_status_undef = 1;
6681 static void fan_quirk1_handle(u8 *fan_status)
6683 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6684 if (*fan_status != fan_control_initial_status) {
6685 /* something changed the HFSP regisnter since
6686 * driver init time, so it is not undefined
6687 * anymore */
6688 tp_features.fan_ctrl_status_undef = 0;
6689 } else {
6690 /* Return most likely status. In fact, it
6691 * might be the only possible status */
6692 *fan_status = TP_EC_FAN_AUTO;
6697 /* Select main fan on X60/X61, NOOP on others */
6698 static bool fan_select_fan1(void)
6700 if (tp_features.second_fan) {
6701 u8 val;
6703 if (ec_read(fan_select_offset, &val) < 0)
6704 return false;
6705 val &= 0xFEU;
6706 if (ec_write(fan_select_offset, val) < 0)
6707 return false;
6709 return true;
6712 /* Select secondary fan on X60/X61 */
6713 static bool fan_select_fan2(void)
6715 u8 val;
6717 if (!tp_features.second_fan)
6718 return false;
6720 if (ec_read(fan_select_offset, &val) < 0)
6721 return false;
6722 val |= 0x01U;
6723 if (ec_write(fan_select_offset, val) < 0)
6724 return false;
6726 return true;
6730 * Call with fan_mutex held
6732 static void fan_update_desired_level(u8 status)
6734 if ((status &
6735 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6736 if (status > 7)
6737 fan_control_desired_level = 7;
6738 else
6739 fan_control_desired_level = status;
6743 static int fan_get_status(u8 *status)
6745 u8 s;
6747 /* TODO:
6748 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6750 switch (fan_status_access_mode) {
6751 case TPACPI_FAN_RD_ACPI_GFAN:
6752 /* 570, 600e/x, 770e, 770x */
6754 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6755 return -EIO;
6757 if (likely(status))
6758 *status = s & 0x07;
6760 break;
6762 case TPACPI_FAN_RD_TPEC:
6763 /* all except 570, 600e/x, 770e, 770x */
6764 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6765 return -EIO;
6767 if (likely(status)) {
6768 *status = s;
6769 fan_quirk1_handle(status);
6772 break;
6774 default:
6775 return -ENXIO;
6778 return 0;
6781 static int fan_get_status_safe(u8 *status)
6783 int rc;
6784 u8 s;
6786 if (mutex_lock_killable(&fan_mutex))
6787 return -ERESTARTSYS;
6788 rc = fan_get_status(&s);
6789 if (!rc)
6790 fan_update_desired_level(s);
6791 mutex_unlock(&fan_mutex);
6793 if (status)
6794 *status = s;
6796 return rc;
6799 static int fan_get_speed(unsigned int *speed)
6801 u8 hi, lo;
6803 switch (fan_status_access_mode) {
6804 case TPACPI_FAN_RD_TPEC:
6805 /* all except 570, 600e/x, 770e, 770x */
6806 if (unlikely(!fan_select_fan1()))
6807 return -EIO;
6808 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6809 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6810 return -EIO;
6812 if (likely(speed))
6813 *speed = (hi << 8) | lo;
6815 break;
6817 default:
6818 return -ENXIO;
6821 return 0;
6824 static int fan2_get_speed(unsigned int *speed)
6826 u8 hi, lo;
6827 bool rc;
6829 switch (fan_status_access_mode) {
6830 case TPACPI_FAN_RD_TPEC:
6831 /* all except 570, 600e/x, 770e, 770x */
6832 if (unlikely(!fan_select_fan2()))
6833 return -EIO;
6834 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6835 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6836 fan_select_fan1(); /* play it safe */
6837 if (rc)
6838 return -EIO;
6840 if (likely(speed))
6841 *speed = (hi << 8) | lo;
6843 break;
6845 default:
6846 return -ENXIO;
6849 return 0;
6852 static int fan_set_level(int level)
6854 if (!fan_control_allowed)
6855 return -EPERM;
6857 switch (fan_control_access_mode) {
6858 case TPACPI_FAN_WR_ACPI_SFAN:
6859 if (level >= 0 && level <= 7) {
6860 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6861 return -EIO;
6862 } else
6863 return -EINVAL;
6864 break;
6866 case TPACPI_FAN_WR_ACPI_FANS:
6867 case TPACPI_FAN_WR_TPEC:
6868 if (!(level & TP_EC_FAN_AUTO) &&
6869 !(level & TP_EC_FAN_FULLSPEED) &&
6870 ((level < 0) || (level > 7)))
6871 return -EINVAL;
6873 /* safety net should the EC not support AUTO
6874 * or FULLSPEED mode bits and just ignore them */
6875 if (level & TP_EC_FAN_FULLSPEED)
6876 level |= 7; /* safety min speed 7 */
6877 else if (level & TP_EC_FAN_AUTO)
6878 level |= 4; /* safety min speed 4 */
6880 if (!acpi_ec_write(fan_status_offset, level))
6881 return -EIO;
6882 else
6883 tp_features.fan_ctrl_status_undef = 0;
6884 break;
6886 default:
6887 return -ENXIO;
6890 vdbg_printk(TPACPI_DBG_FAN,
6891 "fan control: set fan control register to 0x%02x\n", level);
6892 return 0;
6895 static int fan_set_level_safe(int level)
6897 int rc;
6899 if (!fan_control_allowed)
6900 return -EPERM;
6902 if (mutex_lock_killable(&fan_mutex))
6903 return -ERESTARTSYS;
6905 if (level == TPACPI_FAN_LAST_LEVEL)
6906 level = fan_control_desired_level;
6908 rc = fan_set_level(level);
6909 if (!rc)
6910 fan_update_desired_level(level);
6912 mutex_unlock(&fan_mutex);
6913 return rc;
6916 static int fan_set_enable(void)
6918 u8 s;
6919 int rc;
6921 if (!fan_control_allowed)
6922 return -EPERM;
6924 if (mutex_lock_killable(&fan_mutex))
6925 return -ERESTARTSYS;
6927 switch (fan_control_access_mode) {
6928 case TPACPI_FAN_WR_ACPI_FANS:
6929 case TPACPI_FAN_WR_TPEC:
6930 rc = fan_get_status(&s);
6931 if (rc < 0)
6932 break;
6934 /* Don't go out of emergency fan mode */
6935 if (s != 7) {
6936 s &= 0x07;
6937 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6940 if (!acpi_ec_write(fan_status_offset, s))
6941 rc = -EIO;
6942 else {
6943 tp_features.fan_ctrl_status_undef = 0;
6944 rc = 0;
6946 break;
6948 case TPACPI_FAN_WR_ACPI_SFAN:
6949 rc = fan_get_status(&s);
6950 if (rc < 0)
6951 break;
6953 s &= 0x07;
6955 /* Set fan to at least level 4 */
6956 s |= 4;
6958 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6959 rc = -EIO;
6960 else
6961 rc = 0;
6962 break;
6964 default:
6965 rc = -ENXIO;
6968 mutex_unlock(&fan_mutex);
6970 if (!rc)
6971 vdbg_printk(TPACPI_DBG_FAN,
6972 "fan control: set fan control register to 0x%02x\n",
6974 return rc;
6977 static int fan_set_disable(void)
6979 int rc;
6981 if (!fan_control_allowed)
6982 return -EPERM;
6984 if (mutex_lock_killable(&fan_mutex))
6985 return -ERESTARTSYS;
6987 rc = 0;
6988 switch (fan_control_access_mode) {
6989 case TPACPI_FAN_WR_ACPI_FANS:
6990 case TPACPI_FAN_WR_TPEC:
6991 if (!acpi_ec_write(fan_status_offset, 0x00))
6992 rc = -EIO;
6993 else {
6994 fan_control_desired_level = 0;
6995 tp_features.fan_ctrl_status_undef = 0;
6997 break;
6999 case TPACPI_FAN_WR_ACPI_SFAN:
7000 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7001 rc = -EIO;
7002 else
7003 fan_control_desired_level = 0;
7004 break;
7006 default:
7007 rc = -ENXIO;
7010 if (!rc)
7011 vdbg_printk(TPACPI_DBG_FAN,
7012 "fan control: set fan control register to 0\n");
7014 mutex_unlock(&fan_mutex);
7015 return rc;
7018 static int fan_set_speed(int speed)
7020 int rc;
7022 if (!fan_control_allowed)
7023 return -EPERM;
7025 if (mutex_lock_killable(&fan_mutex))
7026 return -ERESTARTSYS;
7028 rc = 0;
7029 switch (fan_control_access_mode) {
7030 case TPACPI_FAN_WR_ACPI_FANS:
7031 if (speed >= 0 && speed <= 65535) {
7032 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7033 speed, speed, speed))
7034 rc = -EIO;
7035 } else
7036 rc = -EINVAL;
7037 break;
7039 default:
7040 rc = -ENXIO;
7043 mutex_unlock(&fan_mutex);
7044 return rc;
7047 static void fan_watchdog_reset(void)
7049 static int fan_watchdog_active;
7051 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7052 return;
7054 if (fan_watchdog_active)
7055 cancel_delayed_work(&fan_watchdog_task);
7057 if (fan_watchdog_maxinterval > 0 &&
7058 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7059 fan_watchdog_active = 1;
7060 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7061 msecs_to_jiffies(fan_watchdog_maxinterval
7062 * 1000))) {
7063 printk(TPACPI_ERR
7064 "failed to queue the fan watchdog, "
7065 "watchdog will not trigger\n");
7067 } else
7068 fan_watchdog_active = 0;
7071 static void fan_watchdog_fire(struct work_struct *ignored)
7073 int rc;
7075 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7076 return;
7078 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7079 rc = fan_set_enable();
7080 if (rc < 0) {
7081 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7082 "will try again later...\n", -rc);
7083 /* reschedule for later */
7084 fan_watchdog_reset();
7089 * SYSFS fan layout: hwmon compatible (device)
7091 * pwm*_enable:
7092 * 0: "disengaged" mode
7093 * 1: manual mode
7094 * 2: native EC "auto" mode (recommended, hardware default)
7096 * pwm*: set speed in manual mode, ignored otherwise.
7097 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7098 * interpolation.
7100 * fan*_input: tachometer reading, RPM
7103 * SYSFS fan layout: extensions
7105 * fan_watchdog (driver):
7106 * fan watchdog interval in seconds, 0 disables (default), max 120
7109 /* sysfs fan pwm1_enable ----------------------------------------------- */
7110 static ssize_t fan_pwm1_enable_show(struct device *dev,
7111 struct device_attribute *attr,
7112 char *buf)
7114 int res, mode;
7115 u8 status;
7117 res = fan_get_status_safe(&status);
7118 if (res)
7119 return res;
7121 if (status & TP_EC_FAN_FULLSPEED) {
7122 mode = 0;
7123 } else if (status & TP_EC_FAN_AUTO) {
7124 mode = 2;
7125 } else
7126 mode = 1;
7128 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7131 static ssize_t fan_pwm1_enable_store(struct device *dev,
7132 struct device_attribute *attr,
7133 const char *buf, size_t count)
7135 unsigned long t;
7136 int res, level;
7138 if (parse_strtoul(buf, 2, &t))
7139 return -EINVAL;
7141 tpacpi_disclose_usertask("hwmon pwm1_enable",
7142 "set fan mode to %lu\n", t);
7144 switch (t) {
7145 case 0:
7146 level = TP_EC_FAN_FULLSPEED;
7147 break;
7148 case 1:
7149 level = TPACPI_FAN_LAST_LEVEL;
7150 break;
7151 case 2:
7152 level = TP_EC_FAN_AUTO;
7153 break;
7154 case 3:
7155 /* reserved for software-controlled auto mode */
7156 return -ENOSYS;
7157 default:
7158 return -EINVAL;
7161 res = fan_set_level_safe(level);
7162 if (res == -ENXIO)
7163 return -EINVAL;
7164 else if (res < 0)
7165 return res;
7167 fan_watchdog_reset();
7169 return count;
7172 static struct device_attribute dev_attr_fan_pwm1_enable =
7173 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7174 fan_pwm1_enable_show, fan_pwm1_enable_store);
7176 /* sysfs fan pwm1 ------------------------------------------------------ */
7177 static ssize_t fan_pwm1_show(struct device *dev,
7178 struct device_attribute *attr,
7179 char *buf)
7181 int res;
7182 u8 status;
7184 res = fan_get_status_safe(&status);
7185 if (res)
7186 return res;
7188 if ((status &
7189 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7190 status = fan_control_desired_level;
7192 if (status > 7)
7193 status = 7;
7195 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7198 static ssize_t fan_pwm1_store(struct device *dev,
7199 struct device_attribute *attr,
7200 const char *buf, size_t count)
7202 unsigned long s;
7203 int rc;
7204 u8 status, newlevel;
7206 if (parse_strtoul(buf, 255, &s))
7207 return -EINVAL;
7209 tpacpi_disclose_usertask("hwmon pwm1",
7210 "set fan speed to %lu\n", s);
7212 /* scale down from 0-255 to 0-7 */
7213 newlevel = (s >> 5) & 0x07;
7215 if (mutex_lock_killable(&fan_mutex))
7216 return -ERESTARTSYS;
7218 rc = fan_get_status(&status);
7219 if (!rc && (status &
7220 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7221 rc = fan_set_level(newlevel);
7222 if (rc == -ENXIO)
7223 rc = -EINVAL;
7224 else if (!rc) {
7225 fan_update_desired_level(newlevel);
7226 fan_watchdog_reset();
7230 mutex_unlock(&fan_mutex);
7231 return (rc)? rc : count;
7234 static struct device_attribute dev_attr_fan_pwm1 =
7235 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7236 fan_pwm1_show, fan_pwm1_store);
7238 /* sysfs fan fan1_input ------------------------------------------------ */
7239 static ssize_t fan_fan1_input_show(struct device *dev,
7240 struct device_attribute *attr,
7241 char *buf)
7243 int res;
7244 unsigned int speed;
7246 res = fan_get_speed(&speed);
7247 if (res < 0)
7248 return res;
7250 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7253 static struct device_attribute dev_attr_fan_fan1_input =
7254 __ATTR(fan1_input, S_IRUGO,
7255 fan_fan1_input_show, NULL);
7257 /* sysfs fan fan2_input ------------------------------------------------ */
7258 static ssize_t fan_fan2_input_show(struct device *dev,
7259 struct device_attribute *attr,
7260 char *buf)
7262 int res;
7263 unsigned int speed;
7265 res = fan2_get_speed(&speed);
7266 if (res < 0)
7267 return res;
7269 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7272 static struct device_attribute dev_attr_fan_fan2_input =
7273 __ATTR(fan2_input, S_IRUGO,
7274 fan_fan2_input_show, NULL);
7276 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7277 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7278 char *buf)
7280 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7283 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7284 const char *buf, size_t count)
7286 unsigned long t;
7288 if (parse_strtoul(buf, 120, &t))
7289 return -EINVAL;
7291 if (!fan_control_allowed)
7292 return -EPERM;
7294 fan_watchdog_maxinterval = t;
7295 fan_watchdog_reset();
7297 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7299 return count;
7302 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7303 fan_fan_watchdog_show, fan_fan_watchdog_store);
7305 /* --------------------------------------------------------------------- */
7306 static struct attribute *fan_attributes[] = {
7307 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7308 &dev_attr_fan_fan1_input.attr,
7309 NULL, /* for fan2_input */
7310 NULL
7313 static const struct attribute_group fan_attr_group = {
7314 .attrs = fan_attributes,
7317 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7318 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7320 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7321 { .vendor = PCI_VENDOR_ID_IBM, \
7322 .bios = TPACPI_MATCH_ANY, \
7323 .ec = TPID(__id1, __id2), \
7324 .quirks = __quirks }
7326 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7327 { .vendor = PCI_VENDOR_ID_LENOVO, \
7328 .bios = TPACPI_MATCH_ANY, \
7329 .ec = TPID(__id1, __id2), \
7330 .quirks = __quirks }
7332 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7333 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7334 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7335 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7336 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7337 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7340 #undef TPACPI_FAN_QL
7341 #undef TPACPI_FAN_QI
7343 static int __init fan_init(struct ibm_init_struct *iibm)
7345 int rc;
7346 unsigned long quirks;
7348 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7349 "initializing fan subdriver\n");
7351 mutex_init(&fan_mutex);
7352 fan_status_access_mode = TPACPI_FAN_NONE;
7353 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7354 fan_control_commands = 0;
7355 fan_watchdog_maxinterval = 0;
7356 tp_features.fan_ctrl_status_undef = 0;
7357 tp_features.second_fan = 0;
7358 fan_control_desired_level = 7;
7360 TPACPI_ACPIHANDLE_INIT(fans);
7361 TPACPI_ACPIHANDLE_INIT(gfan);
7362 TPACPI_ACPIHANDLE_INIT(sfan);
7364 quirks = tpacpi_check_quirks(fan_quirk_table,
7365 ARRAY_SIZE(fan_quirk_table));
7367 if (gfan_handle) {
7368 /* 570, 600e/x, 770e, 770x */
7369 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7370 } else {
7371 /* all other ThinkPads: note that even old-style
7372 * ThinkPad ECs supports the fan control register */
7373 if (likely(acpi_ec_read(fan_status_offset,
7374 &fan_control_initial_status))) {
7375 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7376 if (quirks & TPACPI_FAN_Q1)
7377 fan_quirk1_setup();
7378 if (quirks & TPACPI_FAN_2FAN) {
7379 tp_features.second_fan = 1;
7380 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7381 "secondary fan support enabled\n");
7383 } else {
7384 printk(TPACPI_ERR
7385 "ThinkPad ACPI EC access misbehaving, "
7386 "fan status and control unavailable\n");
7387 return 1;
7391 if (sfan_handle) {
7392 /* 570, 770x-JL */
7393 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7394 fan_control_commands |=
7395 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7396 } else {
7397 if (!gfan_handle) {
7398 /* gfan without sfan means no fan control */
7399 /* all other models implement TP EC 0x2f control */
7401 if (fans_handle) {
7402 /* X31, X40, X41 */
7403 fan_control_access_mode =
7404 TPACPI_FAN_WR_ACPI_FANS;
7405 fan_control_commands |=
7406 TPACPI_FAN_CMD_SPEED |
7407 TPACPI_FAN_CMD_LEVEL |
7408 TPACPI_FAN_CMD_ENABLE;
7409 } else {
7410 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7411 fan_control_commands |=
7412 TPACPI_FAN_CMD_LEVEL |
7413 TPACPI_FAN_CMD_ENABLE;
7418 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7419 "fan is %s, modes %d, %d\n",
7420 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7421 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7422 fan_status_access_mode, fan_control_access_mode);
7424 /* fan control master switch */
7425 if (!fan_control_allowed) {
7426 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7427 fan_control_commands = 0;
7428 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7429 "fan control features disabled by parameter\n");
7432 /* update fan_control_desired_level */
7433 if (fan_status_access_mode != TPACPI_FAN_NONE)
7434 fan_get_status_safe(NULL);
7436 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7437 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7438 if (tp_features.second_fan) {
7439 /* attach second fan tachometer */
7440 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7441 &dev_attr_fan_fan2_input.attr;
7443 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7444 &fan_attr_group);
7445 if (rc < 0)
7446 return rc;
7448 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7449 &driver_attr_fan_watchdog);
7450 if (rc < 0) {
7451 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7452 &fan_attr_group);
7453 return rc;
7455 return 0;
7456 } else
7457 return 1;
7460 static void fan_exit(void)
7462 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7463 "cancelling any pending fan watchdog tasks\n");
7465 /* FIXME: can we really do this unconditionally? */
7466 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7467 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7468 &driver_attr_fan_watchdog);
7470 cancel_delayed_work(&fan_watchdog_task);
7471 flush_workqueue(tpacpi_wq);
7474 static void fan_suspend(pm_message_t state)
7476 int rc;
7478 if (!fan_control_allowed)
7479 return;
7481 /* Store fan status in cache */
7482 fan_control_resume_level = 0;
7483 rc = fan_get_status_safe(&fan_control_resume_level);
7484 if (rc < 0)
7485 printk(TPACPI_NOTICE
7486 "failed to read fan level for later "
7487 "restore during resume: %d\n", rc);
7489 /* if it is undefined, don't attempt to restore it.
7490 * KEEP THIS LAST */
7491 if (tp_features.fan_ctrl_status_undef)
7492 fan_control_resume_level = 0;
7495 static void fan_resume(void)
7497 u8 current_level = 7;
7498 bool do_set = false;
7499 int rc;
7501 /* DSDT *always* updates status on resume */
7502 tp_features.fan_ctrl_status_undef = 0;
7504 if (!fan_control_allowed ||
7505 !fan_control_resume_level ||
7506 (fan_get_status_safe(&current_level) < 0))
7507 return;
7509 switch (fan_control_access_mode) {
7510 case TPACPI_FAN_WR_ACPI_SFAN:
7511 /* never decrease fan level */
7512 do_set = (fan_control_resume_level > current_level);
7513 break;
7514 case TPACPI_FAN_WR_ACPI_FANS:
7515 case TPACPI_FAN_WR_TPEC:
7516 /* never decrease fan level, scale is:
7517 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7519 * We expect the firmware to set either 7 or AUTO, but we
7520 * handle FULLSPEED out of paranoia.
7522 * So, we can safely only restore FULLSPEED or 7, anything
7523 * else could slow the fan. Restoring AUTO is useless, at
7524 * best that's exactly what the DSDT already set (it is the
7525 * slower it uses).
7527 * Always keep in mind that the DSDT *will* have set the
7528 * fans to what the vendor supposes is the best level. We
7529 * muck with it only to speed the fan up.
7531 if (fan_control_resume_level != 7 &&
7532 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7533 return;
7534 else
7535 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7536 (current_level != fan_control_resume_level);
7537 break;
7538 default:
7539 return;
7541 if (do_set) {
7542 printk(TPACPI_NOTICE
7543 "restoring fan level to 0x%02x\n",
7544 fan_control_resume_level);
7545 rc = fan_set_level_safe(fan_control_resume_level);
7546 if (rc < 0)
7547 printk(TPACPI_NOTICE
7548 "failed to restore fan level: %d\n", rc);
7552 static int fan_read(char *p)
7554 int len = 0;
7555 int rc;
7556 u8 status;
7557 unsigned int speed = 0;
7559 switch (fan_status_access_mode) {
7560 case TPACPI_FAN_RD_ACPI_GFAN:
7561 /* 570, 600e/x, 770e, 770x */
7562 rc = fan_get_status_safe(&status);
7563 if (rc < 0)
7564 return rc;
7566 len += sprintf(p + len, "status:\t\t%s\n"
7567 "level:\t\t%d\n",
7568 (status != 0) ? "enabled" : "disabled", status);
7569 break;
7571 case TPACPI_FAN_RD_TPEC:
7572 /* all except 570, 600e/x, 770e, 770x */
7573 rc = fan_get_status_safe(&status);
7574 if (rc < 0)
7575 return rc;
7577 len += sprintf(p + len, "status:\t\t%s\n",
7578 (status != 0) ? "enabled" : "disabled");
7580 rc = fan_get_speed(&speed);
7581 if (rc < 0)
7582 return rc;
7584 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7586 if (status & TP_EC_FAN_FULLSPEED)
7587 /* Disengaged mode takes precedence */
7588 len += sprintf(p + len, "level:\t\tdisengaged\n");
7589 else if (status & TP_EC_FAN_AUTO)
7590 len += sprintf(p + len, "level:\t\tauto\n");
7591 else
7592 len += sprintf(p + len, "level:\t\t%d\n", status);
7593 break;
7595 case TPACPI_FAN_NONE:
7596 default:
7597 len += sprintf(p + len, "status:\t\tnot supported\n");
7600 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7601 len += sprintf(p + len, "commands:\tlevel <level>");
7603 switch (fan_control_access_mode) {
7604 case TPACPI_FAN_WR_ACPI_SFAN:
7605 len += sprintf(p + len, " (<level> is 0-7)\n");
7606 break;
7608 default:
7609 len += sprintf(p + len, " (<level> is 0-7, "
7610 "auto, disengaged, full-speed)\n");
7611 break;
7615 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7616 len += sprintf(p + len, "commands:\tenable, disable\n"
7617 "commands:\twatchdog <timeout> (<timeout> "
7618 "is 0 (off), 1-120 (seconds))\n");
7620 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7621 len += sprintf(p + len, "commands:\tspeed <speed>"
7622 " (<speed> is 0-65535)\n");
7624 return len;
7627 static int fan_write_cmd_level(const char *cmd, int *rc)
7629 int level;
7631 if (strlencmp(cmd, "level auto") == 0)
7632 level = TP_EC_FAN_AUTO;
7633 else if ((strlencmp(cmd, "level disengaged") == 0) |
7634 (strlencmp(cmd, "level full-speed") == 0))
7635 level = TP_EC_FAN_FULLSPEED;
7636 else if (sscanf(cmd, "level %d", &level) != 1)
7637 return 0;
7639 *rc = fan_set_level_safe(level);
7640 if (*rc == -ENXIO)
7641 printk(TPACPI_ERR "level command accepted for unsupported "
7642 "access mode %d", fan_control_access_mode);
7643 else if (!*rc)
7644 tpacpi_disclose_usertask("procfs fan",
7645 "set level to %d\n", level);
7647 return 1;
7650 static int fan_write_cmd_enable(const char *cmd, int *rc)
7652 if (strlencmp(cmd, "enable") != 0)
7653 return 0;
7655 *rc = fan_set_enable();
7656 if (*rc == -ENXIO)
7657 printk(TPACPI_ERR "enable command accepted for unsupported "
7658 "access mode %d", fan_control_access_mode);
7659 else if (!*rc)
7660 tpacpi_disclose_usertask("procfs fan", "enable\n");
7662 return 1;
7665 static int fan_write_cmd_disable(const char *cmd, int *rc)
7667 if (strlencmp(cmd, "disable") != 0)
7668 return 0;
7670 *rc = fan_set_disable();
7671 if (*rc == -ENXIO)
7672 printk(TPACPI_ERR "disable command accepted for unsupported "
7673 "access mode %d", fan_control_access_mode);
7674 else if (!*rc)
7675 tpacpi_disclose_usertask("procfs fan", "disable\n");
7677 return 1;
7680 static int fan_write_cmd_speed(const char *cmd, int *rc)
7682 int speed;
7684 /* TODO:
7685 * Support speed <low> <medium> <high> ? */
7687 if (sscanf(cmd, "speed %d", &speed) != 1)
7688 return 0;
7690 *rc = fan_set_speed(speed);
7691 if (*rc == -ENXIO)
7692 printk(TPACPI_ERR "speed command accepted for unsupported "
7693 "access mode %d", fan_control_access_mode);
7694 else if (!*rc)
7695 tpacpi_disclose_usertask("procfs fan",
7696 "set speed to %d\n", speed);
7698 return 1;
7701 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7703 int interval;
7705 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7706 return 0;
7708 if (interval < 0 || interval > 120)
7709 *rc = -EINVAL;
7710 else {
7711 fan_watchdog_maxinterval = interval;
7712 tpacpi_disclose_usertask("procfs fan",
7713 "set watchdog timer to %d\n",
7714 interval);
7717 return 1;
7720 static int fan_write(char *buf)
7722 char *cmd;
7723 int rc = 0;
7725 while (!rc && (cmd = next_cmd(&buf))) {
7726 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7727 fan_write_cmd_level(cmd, &rc)) &&
7728 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7729 (fan_write_cmd_enable(cmd, &rc) ||
7730 fan_write_cmd_disable(cmd, &rc) ||
7731 fan_write_cmd_watchdog(cmd, &rc))) &&
7732 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7733 fan_write_cmd_speed(cmd, &rc))
7735 rc = -EINVAL;
7736 else if (!rc)
7737 fan_watchdog_reset();
7740 return rc;
7743 static struct ibm_struct fan_driver_data = {
7744 .name = "fan",
7745 .read = fan_read,
7746 .write = fan_write,
7747 .exit = fan_exit,
7748 .suspend = fan_suspend,
7749 .resume = fan_resume,
7752 /****************************************************************************
7753 ****************************************************************************
7755 * Infrastructure
7757 ****************************************************************************
7758 ****************************************************************************/
7761 * HKEY event callout for other subdrivers go here
7762 * (yes, it is ugly, but it is quick, safe, and gets the job done
7764 static void tpacpi_driver_event(const unsigned int hkey_event)
7766 if (ibm_backlight_device) {
7767 switch (hkey_event) {
7768 case TP_HKEY_EV_BRGHT_UP:
7769 case TP_HKEY_EV_BRGHT_DOWN:
7770 tpacpi_brightness_notify_change();
7777 static void hotkey_driver_event(const unsigned int scancode)
7779 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7782 /* sysfs name ---------------------------------------------------------- */
7783 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7784 struct device_attribute *attr,
7785 char *buf)
7787 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7790 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7791 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7793 /* --------------------------------------------------------------------- */
7795 /* /proc support */
7796 static struct proc_dir_entry *proc_dir;
7799 * Module and infrastructure proble, init and exit handling
7802 static int force_load;
7804 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7805 static const char * __init str_supported(int is_supported)
7807 static char text_unsupported[] __initdata = "not supported";
7809 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7811 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7813 static void ibm_exit(struct ibm_struct *ibm)
7815 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7817 list_del_init(&ibm->all_drivers);
7819 if (ibm->flags.acpi_notify_installed) {
7820 dbg_printk(TPACPI_DBG_EXIT,
7821 "%s: acpi_remove_notify_handler\n", ibm->name);
7822 BUG_ON(!ibm->acpi);
7823 acpi_remove_notify_handler(*ibm->acpi->handle,
7824 ibm->acpi->type,
7825 dispatch_acpi_notify);
7826 ibm->flags.acpi_notify_installed = 0;
7827 ibm->flags.acpi_notify_installed = 0;
7830 if (ibm->flags.proc_created) {
7831 dbg_printk(TPACPI_DBG_EXIT,
7832 "%s: remove_proc_entry\n", ibm->name);
7833 remove_proc_entry(ibm->name, proc_dir);
7834 ibm->flags.proc_created = 0;
7837 if (ibm->flags.acpi_driver_registered) {
7838 dbg_printk(TPACPI_DBG_EXIT,
7839 "%s: acpi_bus_unregister_driver\n", ibm->name);
7840 BUG_ON(!ibm->acpi);
7841 acpi_bus_unregister_driver(ibm->acpi->driver);
7842 kfree(ibm->acpi->driver);
7843 ibm->acpi->driver = NULL;
7844 ibm->flags.acpi_driver_registered = 0;
7847 if (ibm->flags.init_called && ibm->exit) {
7848 ibm->exit();
7849 ibm->flags.init_called = 0;
7852 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7855 static int __init ibm_init(struct ibm_init_struct *iibm)
7857 int ret;
7858 struct ibm_struct *ibm = iibm->data;
7859 struct proc_dir_entry *entry;
7861 BUG_ON(ibm == NULL);
7863 INIT_LIST_HEAD(&ibm->all_drivers);
7865 if (ibm->flags.experimental && !experimental)
7866 return 0;
7868 dbg_printk(TPACPI_DBG_INIT,
7869 "probing for %s\n", ibm->name);
7871 if (iibm->init) {
7872 ret = iibm->init(iibm);
7873 if (ret > 0)
7874 return 0; /* probe failed */
7875 if (ret)
7876 return ret;
7878 ibm->flags.init_called = 1;
7881 if (ibm->acpi) {
7882 if (ibm->acpi->hid) {
7883 ret = register_tpacpi_subdriver(ibm);
7884 if (ret)
7885 goto err_out;
7888 if (ibm->acpi->notify) {
7889 ret = setup_acpi_notify(ibm);
7890 if (ret == -ENODEV) {
7891 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7892 ibm->name);
7893 ret = 0;
7894 goto err_out;
7896 if (ret < 0)
7897 goto err_out;
7901 dbg_printk(TPACPI_DBG_INIT,
7902 "%s installed\n", ibm->name);
7904 if (ibm->read) {
7905 entry = create_proc_entry(ibm->name,
7906 S_IFREG | S_IRUGO | S_IWUSR,
7907 proc_dir);
7908 if (!entry) {
7909 printk(TPACPI_ERR "unable to create proc entry %s\n",
7910 ibm->name);
7911 ret = -ENODEV;
7912 goto err_out;
7914 entry->data = ibm;
7915 entry->read_proc = &dispatch_procfs_read;
7916 if (ibm->write)
7917 entry->write_proc = &dispatch_procfs_write;
7918 ibm->flags.proc_created = 1;
7921 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7923 return 0;
7925 err_out:
7926 dbg_printk(TPACPI_DBG_INIT,
7927 "%s: at error exit path with result %d\n",
7928 ibm->name, ret);
7930 ibm_exit(ibm);
7931 return (ret < 0)? ret : 0;
7934 /* Probing */
7936 static bool __pure __init tpacpi_is_fw_digit(const char c)
7938 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7941 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7942 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7943 const char t)
7945 return s && strlen(s) >= 8 &&
7946 tpacpi_is_fw_digit(s[0]) &&
7947 tpacpi_is_fw_digit(s[1]) &&
7948 s[2] == t && s[3] == 'T' &&
7949 tpacpi_is_fw_digit(s[4]) &&
7950 tpacpi_is_fw_digit(s[5]) &&
7951 s[6] == 'W' && s[7] == 'W';
7954 /* returns 0 - probe ok, or < 0 - probe error.
7955 * Probe ok doesn't mean thinkpad found.
7956 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7957 static int __must_check __init get_thinkpad_model_data(
7958 struct thinkpad_id_data *tp)
7960 const struct dmi_device *dev = NULL;
7961 char ec_fw_string[18];
7962 char const *s;
7964 if (!tp)
7965 return -EINVAL;
7967 memset(tp, 0, sizeof(*tp));
7969 if (dmi_name_in_vendors("IBM"))
7970 tp->vendor = PCI_VENDOR_ID_IBM;
7971 else if (dmi_name_in_vendors("LENOVO"))
7972 tp->vendor = PCI_VENDOR_ID_LENOVO;
7973 else
7974 return 0;
7976 s = dmi_get_system_info(DMI_BIOS_VERSION);
7977 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7978 if (s && !tp->bios_version_str)
7979 return -ENOMEM;
7981 /* Really ancient ThinkPad 240X will fail this, which is fine */
7982 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7983 return 0;
7985 tp->bios_model = tp->bios_version_str[0]
7986 | (tp->bios_version_str[1] << 8);
7987 tp->bios_release = (tp->bios_version_str[4] << 8)
7988 | tp->bios_version_str[5];
7991 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7992 * X32 or newer, all Z series; Some models must have an
7993 * up-to-date BIOS or they will not be detected.
7995 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7997 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7998 if (sscanf(dev->name,
7999 "IBM ThinkPad Embedded Controller -[%17c",
8000 ec_fw_string) == 1) {
8001 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8002 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8004 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8005 if (!tp->ec_version_str)
8006 return -ENOMEM;
8008 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8009 tp->ec_model = ec_fw_string[0]
8010 | (ec_fw_string[1] << 8);
8011 tp->ec_release = (ec_fw_string[4] << 8)
8012 | ec_fw_string[5];
8013 } else {
8014 printk(TPACPI_NOTICE
8015 "ThinkPad firmware release %s "
8016 "doesn't match the known patterns\n",
8017 ec_fw_string);
8018 printk(TPACPI_NOTICE
8019 "please report this to %s\n",
8020 TPACPI_MAIL);
8022 break;
8026 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8027 if (s && !strnicmp(s, "ThinkPad", 8)) {
8028 tp->model_str = kstrdup(s, GFP_KERNEL);
8029 if (!tp->model_str)
8030 return -ENOMEM;
8033 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8034 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8035 if (s && !tp->nummodel_str)
8036 return -ENOMEM;
8038 return 0;
8041 static int __init probe_for_thinkpad(void)
8043 int is_thinkpad;
8045 if (acpi_disabled)
8046 return -ENODEV;
8049 * Non-ancient models have better DMI tagging, but very old models
8050 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8052 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8053 (thinkpad_id.ec_model != 0) ||
8054 tpacpi_is_fw_known();
8056 /* ec is required because many other handles are relative to it */
8057 TPACPI_ACPIHANDLE_INIT(ec);
8058 if (!ec_handle) {
8059 if (is_thinkpad)
8060 printk(TPACPI_ERR
8061 "Not yet supported ThinkPad detected!\n");
8062 return -ENODEV;
8065 if (!is_thinkpad && !force_load)
8066 return -ENODEV;
8068 return 0;
8072 /* Module init, exit, parameters */
8074 static struct ibm_init_struct ibms_init[] __initdata = {
8076 .init = thinkpad_acpi_driver_init,
8077 .data = &thinkpad_acpi_driver_data,
8080 .init = hotkey_init,
8081 .data = &hotkey_driver_data,
8084 .init = bluetooth_init,
8085 .data = &bluetooth_driver_data,
8088 .init = wan_init,
8089 .data = &wan_driver_data,
8092 .init = uwb_init,
8093 .data = &uwb_driver_data,
8095 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8097 .init = video_init,
8098 .data = &video_driver_data,
8100 #endif
8102 .init = light_init,
8103 .data = &light_driver_data,
8106 .init = cmos_init,
8107 .data = &cmos_driver_data,
8110 .init = led_init,
8111 .data = &led_driver_data,
8114 .init = beep_init,
8115 .data = &beep_driver_data,
8118 .init = thermal_init,
8119 .data = &thermal_driver_data,
8122 .data = &ecdump_driver_data,
8125 .init = brightness_init,
8126 .data = &brightness_driver_data,
8129 .data = &volume_driver_data,
8132 .init = fan_init,
8133 .data = &fan_driver_data,
8137 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8139 unsigned int i;
8140 struct ibm_struct *ibm;
8142 if (!kp || !kp->name || !val)
8143 return -EINVAL;
8145 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8146 ibm = ibms_init[i].data;
8147 WARN_ON(ibm == NULL);
8149 if (!ibm || !ibm->name)
8150 continue;
8152 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8153 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8154 return -ENOSPC;
8155 strcpy(ibms_init[i].param, val);
8156 strcat(ibms_init[i].param, ",");
8157 return 0;
8161 return -EINVAL;
8164 module_param(experimental, int, 0444);
8165 MODULE_PARM_DESC(experimental,
8166 "Enables experimental features when non-zero");
8168 module_param_named(debug, dbg_level, uint, 0);
8169 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8171 module_param(force_load, bool, 0444);
8172 MODULE_PARM_DESC(force_load,
8173 "Attempts to load the driver even on a "
8174 "mis-identified ThinkPad when true");
8176 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8177 MODULE_PARM_DESC(fan_control,
8178 "Enables setting fan parameters features when true");
8180 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8181 MODULE_PARM_DESC(brightness_mode,
8182 "Selects brightness control strategy: "
8183 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8185 module_param(brightness_enable, uint, 0444);
8186 MODULE_PARM_DESC(brightness_enable,
8187 "Enables backlight control when 1, disables when 0");
8189 module_param(hotkey_report_mode, uint, 0444);
8190 MODULE_PARM_DESC(hotkey_report_mode,
8191 "used for backwards compatibility with userspace, "
8192 "see documentation");
8194 #define TPACPI_PARAM(feature) \
8195 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8196 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8197 "at module load, see documentation")
8199 TPACPI_PARAM(hotkey);
8200 TPACPI_PARAM(bluetooth);
8201 TPACPI_PARAM(video);
8202 TPACPI_PARAM(light);
8203 TPACPI_PARAM(cmos);
8204 TPACPI_PARAM(led);
8205 TPACPI_PARAM(beep);
8206 TPACPI_PARAM(ecdump);
8207 TPACPI_PARAM(brightness);
8208 TPACPI_PARAM(volume);
8209 TPACPI_PARAM(fan);
8211 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8212 module_param(dbg_wlswemul, uint, 0444);
8213 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8214 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8215 MODULE_PARM_DESC(wlsw_state,
8216 "Initial state of the emulated WLSW switch");
8218 module_param(dbg_bluetoothemul, uint, 0444);
8219 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8220 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8221 MODULE_PARM_DESC(bluetooth_state,
8222 "Initial state of the emulated bluetooth switch");
8224 module_param(dbg_wwanemul, uint, 0444);
8225 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8226 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8227 MODULE_PARM_DESC(wwan_state,
8228 "Initial state of the emulated WWAN switch");
8230 module_param(dbg_uwbemul, uint, 0444);
8231 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8232 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8233 MODULE_PARM_DESC(uwb_state,
8234 "Initial state of the emulated UWB switch");
8235 #endif
8237 static void thinkpad_acpi_module_exit(void)
8239 struct ibm_struct *ibm, *itmp;
8241 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8243 list_for_each_entry_safe_reverse(ibm, itmp,
8244 &tpacpi_all_drivers,
8245 all_drivers) {
8246 ibm_exit(ibm);
8249 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8251 if (tpacpi_inputdev) {
8252 if (tp_features.input_device_registered)
8253 input_unregister_device(tpacpi_inputdev);
8254 else
8255 input_free_device(tpacpi_inputdev);
8258 if (tpacpi_hwmon)
8259 hwmon_device_unregister(tpacpi_hwmon);
8261 if (tp_features.sensors_pdev_attrs_registered)
8262 device_remove_file(&tpacpi_sensors_pdev->dev,
8263 &dev_attr_thinkpad_acpi_pdev_name);
8264 if (tpacpi_sensors_pdev)
8265 platform_device_unregister(tpacpi_sensors_pdev);
8266 if (tpacpi_pdev)
8267 platform_device_unregister(tpacpi_pdev);
8269 if (tp_features.sensors_pdrv_attrs_registered)
8270 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8271 if (tp_features.platform_drv_attrs_registered)
8272 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8274 if (tp_features.sensors_pdrv_registered)
8275 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8277 if (tp_features.platform_drv_registered)
8278 platform_driver_unregister(&tpacpi_pdriver);
8280 if (proc_dir)
8281 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8283 if (tpacpi_wq)
8284 destroy_workqueue(tpacpi_wq);
8286 kfree(thinkpad_id.bios_version_str);
8287 kfree(thinkpad_id.ec_version_str);
8288 kfree(thinkpad_id.model_str);
8292 static int __init thinkpad_acpi_module_init(void)
8294 int ret, i;
8296 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8298 /* Parameter checking */
8299 if (hotkey_report_mode > 2)
8300 return -EINVAL;
8302 /* Driver-level probe */
8304 ret = get_thinkpad_model_data(&thinkpad_id);
8305 if (ret) {
8306 printk(TPACPI_ERR
8307 "unable to get DMI data: %d\n", ret);
8308 thinkpad_acpi_module_exit();
8309 return ret;
8311 ret = probe_for_thinkpad();
8312 if (ret) {
8313 thinkpad_acpi_module_exit();
8314 return ret;
8317 /* Driver initialization */
8319 TPACPI_ACPIHANDLE_INIT(ecrd);
8320 TPACPI_ACPIHANDLE_INIT(ecwr);
8322 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8323 if (!tpacpi_wq) {
8324 thinkpad_acpi_module_exit();
8325 return -ENOMEM;
8328 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8329 if (!proc_dir) {
8330 printk(TPACPI_ERR
8331 "unable to create proc dir " TPACPI_PROC_DIR);
8332 thinkpad_acpi_module_exit();
8333 return -ENODEV;
8336 ret = platform_driver_register(&tpacpi_pdriver);
8337 if (ret) {
8338 printk(TPACPI_ERR
8339 "unable to register main platform driver\n");
8340 thinkpad_acpi_module_exit();
8341 return ret;
8343 tp_features.platform_drv_registered = 1;
8345 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8346 if (ret) {
8347 printk(TPACPI_ERR
8348 "unable to register hwmon platform driver\n");
8349 thinkpad_acpi_module_exit();
8350 return ret;
8352 tp_features.sensors_pdrv_registered = 1;
8354 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8355 if (!ret) {
8356 tp_features.platform_drv_attrs_registered = 1;
8357 ret = tpacpi_create_driver_attributes(
8358 &tpacpi_hwmon_pdriver.driver);
8360 if (ret) {
8361 printk(TPACPI_ERR
8362 "unable to create sysfs driver attributes\n");
8363 thinkpad_acpi_module_exit();
8364 return ret;
8366 tp_features.sensors_pdrv_attrs_registered = 1;
8369 /* Device initialization */
8370 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8371 NULL, 0);
8372 if (IS_ERR(tpacpi_pdev)) {
8373 ret = PTR_ERR(tpacpi_pdev);
8374 tpacpi_pdev = NULL;
8375 printk(TPACPI_ERR "unable to register platform device\n");
8376 thinkpad_acpi_module_exit();
8377 return ret;
8379 tpacpi_sensors_pdev = platform_device_register_simple(
8380 TPACPI_HWMON_DRVR_NAME,
8381 -1, NULL, 0);
8382 if (IS_ERR(tpacpi_sensors_pdev)) {
8383 ret = PTR_ERR(tpacpi_sensors_pdev);
8384 tpacpi_sensors_pdev = NULL;
8385 printk(TPACPI_ERR
8386 "unable to register hwmon platform device\n");
8387 thinkpad_acpi_module_exit();
8388 return ret;
8390 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8391 &dev_attr_thinkpad_acpi_pdev_name);
8392 if (ret) {
8393 printk(TPACPI_ERR
8394 "unable to create sysfs hwmon device attributes\n");
8395 thinkpad_acpi_module_exit();
8396 return ret;
8398 tp_features.sensors_pdev_attrs_registered = 1;
8399 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8400 if (IS_ERR(tpacpi_hwmon)) {
8401 ret = PTR_ERR(tpacpi_hwmon);
8402 tpacpi_hwmon = NULL;
8403 printk(TPACPI_ERR "unable to register hwmon device\n");
8404 thinkpad_acpi_module_exit();
8405 return ret;
8407 mutex_init(&tpacpi_inputdev_send_mutex);
8408 tpacpi_inputdev = input_allocate_device();
8409 if (!tpacpi_inputdev) {
8410 printk(TPACPI_ERR "unable to allocate input device\n");
8411 thinkpad_acpi_module_exit();
8412 return -ENOMEM;
8413 } else {
8414 /* Prepare input device, but don't register */
8415 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8416 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8417 tpacpi_inputdev->id.bustype = BUS_HOST;
8418 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8419 thinkpad_id.vendor :
8420 PCI_VENDOR_ID_IBM;
8421 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8422 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8423 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8425 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8426 ret = ibm_init(&ibms_init[i]);
8427 if (ret >= 0 && *ibms_init[i].param)
8428 ret = ibms_init[i].data->write(ibms_init[i].param);
8429 if (ret < 0) {
8430 thinkpad_acpi_module_exit();
8431 return ret;
8435 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8437 ret = input_register_device(tpacpi_inputdev);
8438 if (ret < 0) {
8439 printk(TPACPI_ERR "unable to register input device\n");
8440 thinkpad_acpi_module_exit();
8441 return ret;
8442 } else {
8443 tp_features.input_device_registered = 1;
8446 return 0;
8449 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8452 * This will autoload the driver in almost every ThinkPad
8453 * in widespread use.
8455 * Only _VERY_ old models, like the 240, 240x and 570 lack
8456 * the HKEY event interface.
8458 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8461 * DMI matching for module autoloading
8463 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8464 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8466 * Only models listed in thinkwiki will be supported, so add yours
8467 * if it is not there yet.
8469 #define IBM_BIOS_MODULE_ALIAS(__type) \
8470 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8472 /* Ancient thinkpad BIOSes have to be identified by
8473 * BIOS type or model number, and there are far less
8474 * BIOS types than model numbers... */
8475 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8477 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8478 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8479 MODULE_DESCRIPTION(TPACPI_DESC);
8480 MODULE_VERSION(TPACPI_VERSION);
8481 MODULE_LICENSE("GPL");
8483 module_init(thinkpad_acpi_module_init);
8484 module_exit(thinkpad_acpi_module_exit);