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
blob7e4308cc0331a0940643d1c62a27d4c7c8c48a79
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 /* rfkill switches */
215 enum {
216 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
217 TPACPI_RFK_WWAN_SW_ID,
218 TPACPI_RFK_UWB_SW_ID,
221 /* printk headers */
222 #define TPACPI_LOG TPACPI_FILE ": "
223 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
224 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
225 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
226 #define TPACPI_ERR KERN_ERR TPACPI_LOG
227 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
228 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
229 #define TPACPI_INFO KERN_INFO TPACPI_LOG
230 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
232 /* Debugging printk groups */
233 #define TPACPI_DBG_ALL 0xffff
234 #define TPACPI_DBG_DISCLOSETASK 0x8000
235 #define TPACPI_DBG_INIT 0x0001
236 #define TPACPI_DBG_EXIT 0x0002
237 #define TPACPI_DBG_RFKILL 0x0004
238 #define TPACPI_DBG_HKEY 0x0008
239 #define TPACPI_DBG_FAN 0x0010
240 #define TPACPI_DBG_BRGHT 0x0020
242 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
243 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
244 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
247 /****************************************************************************
248 * Driver-wide structs and misc. variables
251 struct ibm_struct;
253 struct tp_acpi_drv_struct {
254 const struct acpi_device_id *hid;
255 struct acpi_driver *driver;
257 void (*notify) (struct ibm_struct *, u32);
258 acpi_handle *handle;
259 u32 type;
260 struct acpi_device *device;
263 struct ibm_struct {
264 char *name;
266 int (*read) (char *);
267 int (*write) (char *);
268 void (*exit) (void);
269 void (*resume) (void);
270 void (*suspend) (pm_message_t state);
271 void (*shutdown) (void);
273 struct list_head all_drivers;
275 struct tp_acpi_drv_struct *acpi;
277 struct {
278 u8 acpi_driver_registered:1;
279 u8 acpi_notify_installed:1;
280 u8 proc_created:1;
281 u8 init_called:1;
282 u8 experimental:1;
283 } flags;
286 struct ibm_init_struct {
287 char param[32];
289 int (*init) (struct ibm_init_struct *);
290 struct ibm_struct *data;
293 static struct {
294 u32 bluetooth:1;
295 u32 hotkey:1;
296 u32 hotkey_mask:1;
297 u32 hotkey_wlsw:1;
298 u32 hotkey_tablet:1;
299 u32 light:1;
300 u32 light_status:1;
301 u32 bright_16levels:1;
302 u32 bright_acpimode:1;
303 u32 wan:1;
304 u32 uwb:1;
305 u32 fan_ctrl_status_undef:1;
306 u32 second_fan:1;
307 u32 beep_needs_two_args:1;
308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315 } tp_features;
317 static struct {
318 u16 hotkey_mask_ff:1;
319 } tp_warned;
321 struct thinkpad_id_data {
322 unsigned int vendor; /* ThinkPad vendor:
323 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
325 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
326 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
328 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
329 u16 ec_model;
330 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
331 u16 ec_release;
333 char *model_str; /* ThinkPad T43 */
334 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
336 static struct thinkpad_id_data thinkpad_id;
338 static enum {
339 TPACPI_LIFE_INIT = 0,
340 TPACPI_LIFE_RUNNING,
341 TPACPI_LIFE_EXITING,
342 } tpacpi_lifecycle;
344 static int experimental;
345 static u32 dbg_level;
347 static struct workqueue_struct *tpacpi_wq;
349 enum led_status_t {
350 TPACPI_LED_OFF = 0,
351 TPACPI_LED_ON,
352 TPACPI_LED_BLINK,
355 /* Special LED class that can defer work */
356 struct tpacpi_led_classdev {
357 struct led_classdev led_classdev;
358 struct work_struct work;
359 enum led_status_t new_state;
360 unsigned int led;
363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
364 static int dbg_wlswemul;
365 static int tpacpi_wlsw_emulstate;
366 static int dbg_bluetoothemul;
367 static int tpacpi_bluetooth_emulstate;
368 static int dbg_wwanemul;
369 static int tpacpi_wwan_emulstate;
370 static int dbg_uwbemul;
371 static int tpacpi_uwb_emulstate;
372 #endif
375 /*************************************************************************
376 * Debugging helpers
379 #define dbg_printk(a_dbg_level, format, arg...) \
380 do { if (dbg_level & (a_dbg_level)) \
381 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
382 } while (0)
384 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
385 #define vdbg_printk dbg_printk
386 static const char *str_supported(int is_supported);
387 #else
388 #define vdbg_printk(a_dbg_level, format, arg...) \
389 do { } while (0)
390 #endif
392 static void tpacpi_log_usertask(const char * const what)
394 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
395 what, task_tgid_vnr(current));
398 #define tpacpi_disclose_usertask(what, format, arg...) \
399 do { \
400 if (unlikely( \
401 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
402 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
403 printk(TPACPI_DEBUG "%s: PID %d: " format, \
404 what, task_tgid_vnr(current), ## arg); \
406 } while (0)
409 * Quirk handling helpers
411 * ThinkPad IDs and versions seen in the field so far
412 * are two-characters from the set [0-9A-Z], i.e. base 36.
414 * We use values well outside that range as specials.
417 #define TPACPI_MATCH_ANY 0xffffU
418 #define TPACPI_MATCH_UNKNOWN 0U
420 /* TPID('1', 'Y') == 0x5931 */
421 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
423 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
424 { .vendor = PCI_VENDOR_ID_IBM, \
425 .bios = TPID(__id1, __id2), \
426 .ec = TPACPI_MATCH_ANY, \
427 .quirks = (__quirk) }
429 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
430 { .vendor = PCI_VENDOR_ID_LENOVO, \
431 .bios = TPID(__id1, __id2), \
432 .ec = TPACPI_MATCH_ANY, \
433 .quirks = (__quirk) }
435 struct tpacpi_quirk {
436 unsigned int vendor;
437 u16 bios;
438 u16 ec;
439 unsigned long quirks;
443 * tpacpi_check_quirks() - search BIOS/EC version on a list
444 * @qlist: array of &struct tpacpi_quirk
445 * @qlist_size: number of elements in @qlist
447 * Iterates over a quirks list until one is found that matches the
448 * ThinkPad's vendor, BIOS and EC model.
450 * Returns 0 if nothing matches, otherwise returns the quirks field of
451 * the matching &struct tpacpi_quirk entry.
453 * The match criteria is: vendor, ec and bios much match.
455 static unsigned long __init tpacpi_check_quirks(
456 const struct tpacpi_quirk *qlist,
457 unsigned int qlist_size)
459 while (qlist_size) {
460 if ((qlist->vendor == thinkpad_id.vendor ||
461 qlist->vendor == TPACPI_MATCH_ANY) &&
462 (qlist->bios == thinkpad_id.bios_model ||
463 qlist->bios == TPACPI_MATCH_ANY) &&
464 (qlist->ec == thinkpad_id.ec_model ||
465 qlist->ec == TPACPI_MATCH_ANY))
466 return qlist->quirks;
468 qlist_size--;
469 qlist++;
471 return 0;
475 /****************************************************************************
476 ****************************************************************************
478 * ACPI Helpers and device model
480 ****************************************************************************
481 ****************************************************************************/
483 /*************************************************************************
484 * ACPI basic handles
487 static acpi_handle root_handle;
489 #define TPACPI_HANDLE(object, parent, paths...) \
490 static acpi_handle object##_handle; \
491 static acpi_handle *object##_parent = &parent##_handle; \
492 static char *object##_path; \
493 static char *object##_paths[] = { paths }
495 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
496 "\\_SB.PCI.ISA.EC", /* 570 */
497 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
498 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
499 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
500 "\\_SB.PCI0.ICH3.EC0", /* R31 */
501 "\\_SB.PCI0.LPC.EC", /* all others */
504 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
505 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
507 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
508 /* T4x, X31, X40 */
509 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
510 "\\CMS", /* R40, R40e */
511 ); /* all others */
513 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
514 "^HKEY", /* R30, R31 */
515 "HKEY", /* all others */
516 ); /* 570 */
518 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
519 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
520 "\\_SB.PCI0.VID0", /* 770e */
521 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
522 "\\_SB.PCI0.AGP.VID", /* all others */
523 ); /* R30, R31 */
526 /*************************************************************************
527 * ACPI helpers
530 static int acpi_evalf(acpi_handle handle,
531 void *res, char *method, char *fmt, ...)
533 char *fmt0 = fmt;
534 struct acpi_object_list params;
535 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
536 struct acpi_buffer result, *resultp;
537 union acpi_object out_obj;
538 acpi_status status;
539 va_list ap;
540 char res_type;
541 int success;
542 int quiet;
544 if (!*fmt) {
545 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
546 return 0;
549 if (*fmt == 'q') {
550 quiet = 1;
551 fmt++;
552 } else
553 quiet = 0;
555 res_type = *(fmt++);
557 params.count = 0;
558 params.pointer = &in_objs[0];
560 va_start(ap, fmt);
561 while (*fmt) {
562 char c = *(fmt++);
563 switch (c) {
564 case 'd': /* int */
565 in_objs[params.count].integer.value = va_arg(ap, int);
566 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
567 break;
568 /* add more types as needed */
569 default:
570 printk(TPACPI_ERR "acpi_evalf() called "
571 "with invalid format character '%c'\n", c);
572 return 0;
575 va_end(ap);
577 if (res_type != 'v') {
578 result.length = sizeof(out_obj);
579 result.pointer = &out_obj;
580 resultp = &result;
581 } else
582 resultp = NULL;
584 status = acpi_evaluate_object(handle, method, &params, resultp);
586 switch (res_type) {
587 case 'd': /* int */
588 if (res)
589 *(int *)res = out_obj.integer.value;
590 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
591 break;
592 case 'v': /* void */
593 success = status == AE_OK;
594 break;
595 /* add more types as needed */
596 default:
597 printk(TPACPI_ERR "acpi_evalf() called "
598 "with invalid format character '%c'\n", res_type);
599 return 0;
602 if (!success && !quiet)
603 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
604 method, fmt0, status);
606 return success;
609 static int acpi_ec_read(int i, u8 *p)
611 int v;
613 if (ecrd_handle) {
614 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
615 return 0;
616 *p = v;
617 } else {
618 if (ec_read(i, p) < 0)
619 return 0;
622 return 1;
625 static int acpi_ec_write(int i, u8 v)
627 if (ecwr_handle) {
628 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
629 return 0;
630 } else {
631 if (ec_write(i, v) < 0)
632 return 0;
635 return 1;
638 static int issue_thinkpad_cmos_command(int cmos_cmd)
640 if (!cmos_handle)
641 return -ENXIO;
643 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
644 return -EIO;
646 return 0;
649 /*************************************************************************
650 * ACPI device model
653 #define TPACPI_ACPIHANDLE_INIT(object) \
654 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
655 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
657 static void drv_acpi_handle_init(char *name,
658 acpi_handle *handle, acpi_handle parent,
659 char **paths, int num_paths, char **path)
661 int i;
662 acpi_status status;
664 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
665 name);
667 for (i = 0; i < num_paths; i++) {
668 status = acpi_get_handle(parent, paths[i], handle);
669 if (ACPI_SUCCESS(status)) {
670 *path = paths[i];
671 dbg_printk(TPACPI_DBG_INIT,
672 "Found ACPI handle %s for %s\n",
673 *path, name);
674 return;
678 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
679 name);
680 *handle = NULL;
683 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
685 struct ibm_struct *ibm = data;
687 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
688 return;
690 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
691 return;
693 ibm->acpi->notify(ibm, event);
696 static int __init setup_acpi_notify(struct ibm_struct *ibm)
698 acpi_status status;
699 int rc;
701 BUG_ON(!ibm->acpi);
703 if (!*ibm->acpi->handle)
704 return 0;
706 vdbg_printk(TPACPI_DBG_INIT,
707 "setting up ACPI notify for %s\n", ibm->name);
709 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
710 if (rc < 0) {
711 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
712 ibm->name, rc);
713 return -ENODEV;
716 ibm->acpi->device->driver_data = ibm;
717 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
718 TPACPI_ACPI_EVENT_PREFIX,
719 ibm->name);
721 status = acpi_install_notify_handler(*ibm->acpi->handle,
722 ibm->acpi->type, dispatch_acpi_notify, ibm);
723 if (ACPI_FAILURE(status)) {
724 if (status == AE_ALREADY_EXISTS) {
725 printk(TPACPI_NOTICE
726 "another device driver is already "
727 "handling %s events\n", ibm->name);
728 } else {
729 printk(TPACPI_ERR
730 "acpi_install_notify_handler(%s) failed: %d\n",
731 ibm->name, status);
733 return -ENODEV;
735 ibm->flags.acpi_notify_installed = 1;
736 return 0;
739 static int __init tpacpi_device_add(struct acpi_device *device)
741 return 0;
744 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
746 int rc;
748 dbg_printk(TPACPI_DBG_INIT,
749 "registering %s as an ACPI driver\n", ibm->name);
751 BUG_ON(!ibm->acpi);
753 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
754 if (!ibm->acpi->driver) {
755 printk(TPACPI_ERR
756 "failed to allocate memory for ibm->acpi->driver\n");
757 return -ENOMEM;
760 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
761 ibm->acpi->driver->ids = ibm->acpi->hid;
763 ibm->acpi->driver->ops.add = &tpacpi_device_add;
765 rc = acpi_bus_register_driver(ibm->acpi->driver);
766 if (rc < 0) {
767 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
768 ibm->name, rc);
769 kfree(ibm->acpi->driver);
770 ibm->acpi->driver = NULL;
771 } else if (!rc)
772 ibm->flags.acpi_driver_registered = 1;
774 return rc;
778 /****************************************************************************
779 ****************************************************************************
781 * Procfs Helpers
783 ****************************************************************************
784 ****************************************************************************/
786 static int dispatch_procfs_read(char *page, char **start, off_t off,
787 int count, int *eof, void *data)
789 struct ibm_struct *ibm = data;
790 int len;
792 if (!ibm || !ibm->read)
793 return -EINVAL;
795 len = ibm->read(page);
796 if (len < 0)
797 return len;
799 if (len <= off + count)
800 *eof = 1;
801 *start = page + off;
802 len -= off;
803 if (len > count)
804 len = count;
805 if (len < 0)
806 len = 0;
808 return len;
811 static int dispatch_procfs_write(struct file *file,
812 const char __user *userbuf,
813 unsigned long count, void *data)
815 struct ibm_struct *ibm = data;
816 char *kernbuf;
817 int ret;
819 if (!ibm || !ibm->write)
820 return -EINVAL;
821 if (count > PAGE_SIZE - 2)
822 return -EINVAL;
824 kernbuf = kmalloc(count + 2, GFP_KERNEL);
825 if (!kernbuf)
826 return -ENOMEM;
828 if (copy_from_user(kernbuf, userbuf, count)) {
829 kfree(kernbuf);
830 return -EFAULT;
833 kernbuf[count] = 0;
834 strcat(kernbuf, ",");
835 ret = ibm->write(kernbuf);
836 if (ret == 0)
837 ret = count;
839 kfree(kernbuf);
841 return ret;
844 static char *next_cmd(char **cmds)
846 char *start = *cmds;
847 char *end;
849 while ((end = strchr(start, ',')) && end == start)
850 start = end + 1;
852 if (!end)
853 return NULL;
855 *end = 0;
856 *cmds = end + 1;
857 return start;
861 /****************************************************************************
862 ****************************************************************************
864 * Device model: input, hwmon and platform
866 ****************************************************************************
867 ****************************************************************************/
869 static struct platform_device *tpacpi_pdev;
870 static struct platform_device *tpacpi_sensors_pdev;
871 static struct device *tpacpi_hwmon;
872 static struct input_dev *tpacpi_inputdev;
873 static struct mutex tpacpi_inputdev_send_mutex;
874 static LIST_HEAD(tpacpi_all_drivers);
876 static int tpacpi_suspend_handler(struct platform_device *pdev,
877 pm_message_t state)
879 struct ibm_struct *ibm, *itmp;
881 list_for_each_entry_safe(ibm, itmp,
882 &tpacpi_all_drivers,
883 all_drivers) {
884 if (ibm->suspend)
885 (ibm->suspend)(state);
888 return 0;
891 static int tpacpi_resume_handler(struct platform_device *pdev)
893 struct ibm_struct *ibm, *itmp;
895 list_for_each_entry_safe(ibm, itmp,
896 &tpacpi_all_drivers,
897 all_drivers) {
898 if (ibm->resume)
899 (ibm->resume)();
902 return 0;
905 static void tpacpi_shutdown_handler(struct platform_device *pdev)
907 struct ibm_struct *ibm, *itmp;
909 list_for_each_entry_safe(ibm, itmp,
910 &tpacpi_all_drivers,
911 all_drivers) {
912 if (ibm->shutdown)
913 (ibm->shutdown)();
917 static struct platform_driver tpacpi_pdriver = {
918 .driver = {
919 .name = TPACPI_DRVR_NAME,
920 .owner = THIS_MODULE,
922 .suspend = tpacpi_suspend_handler,
923 .resume = tpacpi_resume_handler,
924 .shutdown = tpacpi_shutdown_handler,
927 static struct platform_driver tpacpi_hwmon_pdriver = {
928 .driver = {
929 .name = TPACPI_HWMON_DRVR_NAME,
930 .owner = THIS_MODULE,
934 /*************************************************************************
935 * sysfs support helpers
938 struct attribute_set {
939 unsigned int members, max_members;
940 struct attribute_group group;
943 struct attribute_set_obj {
944 struct attribute_set s;
945 struct attribute *a;
946 } __attribute__((packed));
948 static struct attribute_set *create_attr_set(unsigned int max_members,
949 const char *name)
951 struct attribute_set_obj *sobj;
953 if (max_members == 0)
954 return NULL;
956 /* Allocates space for implicit NULL at the end too */
957 sobj = kzalloc(sizeof(struct attribute_set_obj) +
958 max_members * sizeof(struct attribute *),
959 GFP_KERNEL);
960 if (!sobj)
961 return NULL;
962 sobj->s.max_members = max_members;
963 sobj->s.group.attrs = &sobj->a;
964 sobj->s.group.name = name;
966 return &sobj->s;
969 #define destroy_attr_set(_set) \
970 kfree(_set);
972 /* not multi-threaded safe, use it in a single thread per set */
973 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
975 if (!s || !attr)
976 return -EINVAL;
978 if (s->members >= s->max_members)
979 return -ENOMEM;
981 s->group.attrs[s->members] = attr;
982 s->members++;
984 return 0;
987 static int add_many_to_attr_set(struct attribute_set *s,
988 struct attribute **attr,
989 unsigned int count)
991 int i, res;
993 for (i = 0; i < count; i++) {
994 res = add_to_attr_set(s, attr[i]);
995 if (res)
996 return res;
999 return 0;
1002 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1004 sysfs_remove_group(kobj, &s->group);
1005 destroy_attr_set(s);
1008 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1009 sysfs_create_group(_kobj, &_attr_set->group)
1011 static int parse_strtoul(const char *buf,
1012 unsigned long max, unsigned long *value)
1014 char *endp;
1016 while (*buf && isspace(*buf))
1017 buf++;
1018 *value = simple_strtoul(buf, &endp, 0);
1019 while (*endp && isspace(*endp))
1020 endp++;
1021 if (*endp || *value > max)
1022 return -EINVAL;
1024 return 0;
1027 static void tpacpi_disable_brightness_delay(void)
1029 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1030 printk(TPACPI_NOTICE
1031 "ACPI backlight control delay disabled\n");
1034 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1036 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1037 union acpi_object *obj;
1038 int rc;
1040 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1041 obj = (union acpi_object *)buffer.pointer;
1042 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1043 printk(TPACPI_ERR "Unknown _BCL data, "
1044 "please report this to %s\n", TPACPI_MAIL);
1045 rc = 0;
1046 } else {
1047 rc = obj->package.count;
1049 } else {
1050 return 0;
1053 kfree(buffer.pointer);
1054 return rc;
1057 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1058 u32 lvl, void *context, void **rv)
1060 char name[ACPI_PATH_SEGMENT_LENGTH];
1061 struct acpi_buffer buffer = { sizeof(name), &name };
1063 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1064 !strncmp("_BCL", name, sizeof(name) - 1)) {
1065 BUG_ON(!rv || !*rv);
1066 **(int **)rv = tpacpi_query_bcl_levels(handle);
1067 return AE_CTRL_TERMINATE;
1068 } else {
1069 return AE_OK;
1074 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1076 static int __init tpacpi_check_std_acpi_brightness_support(void)
1078 int status;
1079 int bcl_levels = 0;
1080 void *bcl_ptr = &bcl_levels;
1082 if (!vid_handle) {
1083 TPACPI_ACPIHANDLE_INIT(vid);
1085 if (!vid_handle)
1086 return 0;
1089 * Search for a _BCL method, and execute it. This is safe on all
1090 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1091 * BIOS in ACPI backlight control mode. We do NOT have to care
1092 * about calling the _BCL method in an enabled video device, any
1093 * will do for our purposes.
1096 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1097 tpacpi_acpi_walk_find_bcl, NULL,
1098 &bcl_ptr);
1100 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1101 tp_features.bright_acpimode = 1;
1102 return (bcl_levels - 2);
1105 return 0;
1108 static int __init tpacpi_new_rfkill(const unsigned int id,
1109 struct rfkill **rfk,
1110 const enum rfkill_type rfktype,
1111 const char *name,
1112 const bool set_default,
1113 int (*toggle_radio)(void *, enum rfkill_state),
1114 int (*get_state)(void *, enum rfkill_state *))
1116 int res;
1117 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1119 res = get_state(NULL, &initial_state);
1120 if (res < 0) {
1121 printk(TPACPI_ERR
1122 "failed to read initial state for %s, error %d; "
1123 "will turn radio off\n", name, res);
1124 } else if (set_default) {
1125 /* try to set the initial state as the default for the rfkill
1126 * type, since we ask the firmware to preserve it across S5 in
1127 * NVRAM */
1128 if (rfkill_set_default(rfktype,
1129 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1130 RFKILL_STATE_UNBLOCKED :
1131 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1132 vdbg_printk(TPACPI_DBG_RFKILL,
1133 "Default state for %s cannot be changed\n",
1134 name);
1137 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1138 if (!*rfk) {
1139 printk(TPACPI_ERR
1140 "failed to allocate memory for rfkill class\n");
1141 return -ENOMEM;
1144 (*rfk)->name = name;
1145 (*rfk)->get_state = get_state;
1146 (*rfk)->toggle_radio = toggle_radio;
1147 (*rfk)->state = initial_state;
1149 res = rfkill_register(*rfk);
1150 if (res < 0) {
1151 printk(TPACPI_ERR
1152 "failed to register %s rfkill switch: %d\n",
1153 name, res);
1154 rfkill_free(*rfk);
1155 *rfk = NULL;
1156 return res;
1159 return 0;
1162 static void printk_deprecated_attribute(const char * const what,
1163 const char * const details)
1165 tpacpi_log_usertask("deprecated sysfs attribute");
1166 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1167 "will be removed. %s\n",
1168 what, details);
1171 static void printk_deprecated_rfkill_attribute(const char * const what)
1173 printk_deprecated_attribute(what,
1174 "Please switch to generic rfkill before year 2010");
1177 /*************************************************************************
1178 * thinkpad-acpi driver attributes
1181 /* interface_version --------------------------------------------------- */
1182 static ssize_t tpacpi_driver_interface_version_show(
1183 struct device_driver *drv,
1184 char *buf)
1186 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1189 static DRIVER_ATTR(interface_version, S_IRUGO,
1190 tpacpi_driver_interface_version_show, NULL);
1192 /* debug_level --------------------------------------------------------- */
1193 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1194 char *buf)
1196 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1199 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1200 const char *buf, size_t count)
1202 unsigned long t;
1204 if (parse_strtoul(buf, 0xffff, &t))
1205 return -EINVAL;
1207 dbg_level = t;
1209 return count;
1212 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1213 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1215 /* version ------------------------------------------------------------- */
1216 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1217 char *buf)
1219 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1220 TPACPI_DESC, TPACPI_VERSION);
1223 static DRIVER_ATTR(version, S_IRUGO,
1224 tpacpi_driver_version_show, NULL);
1226 /* --------------------------------------------------------------------- */
1228 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1230 static void tpacpi_send_radiosw_update(void);
1232 /* wlsw_emulstate ------------------------------------------------------ */
1233 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1234 char *buf)
1236 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1239 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1240 const char *buf, size_t count)
1242 unsigned long t;
1244 if (parse_strtoul(buf, 1, &t))
1245 return -EINVAL;
1247 if (tpacpi_wlsw_emulstate != t) {
1248 tpacpi_wlsw_emulstate = !!t;
1249 tpacpi_send_radiosw_update();
1250 } else
1251 tpacpi_wlsw_emulstate = !!t;
1253 return count;
1256 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1257 tpacpi_driver_wlsw_emulstate_show,
1258 tpacpi_driver_wlsw_emulstate_store);
1260 /* bluetooth_emulstate ------------------------------------------------- */
1261 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1262 struct device_driver *drv,
1263 char *buf)
1265 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1268 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1269 struct device_driver *drv,
1270 const char *buf, size_t count)
1272 unsigned long t;
1274 if (parse_strtoul(buf, 1, &t))
1275 return -EINVAL;
1277 tpacpi_bluetooth_emulstate = !!t;
1279 return count;
1282 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1283 tpacpi_driver_bluetooth_emulstate_show,
1284 tpacpi_driver_bluetooth_emulstate_store);
1286 /* wwan_emulstate ------------------------------------------------- */
1287 static ssize_t tpacpi_driver_wwan_emulstate_show(
1288 struct device_driver *drv,
1289 char *buf)
1291 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1294 static ssize_t tpacpi_driver_wwan_emulstate_store(
1295 struct device_driver *drv,
1296 const char *buf, size_t count)
1298 unsigned long t;
1300 if (parse_strtoul(buf, 1, &t))
1301 return -EINVAL;
1303 tpacpi_wwan_emulstate = !!t;
1305 return count;
1308 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1309 tpacpi_driver_wwan_emulstate_show,
1310 tpacpi_driver_wwan_emulstate_store);
1312 /* uwb_emulstate ------------------------------------------------- */
1313 static ssize_t tpacpi_driver_uwb_emulstate_show(
1314 struct device_driver *drv,
1315 char *buf)
1317 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1320 static ssize_t tpacpi_driver_uwb_emulstate_store(
1321 struct device_driver *drv,
1322 const char *buf, size_t count)
1324 unsigned long t;
1326 if (parse_strtoul(buf, 1, &t))
1327 return -EINVAL;
1329 tpacpi_uwb_emulstate = !!t;
1331 return count;
1334 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1335 tpacpi_driver_uwb_emulstate_show,
1336 tpacpi_driver_uwb_emulstate_store);
1337 #endif
1339 /* --------------------------------------------------------------------- */
1341 static struct driver_attribute *tpacpi_driver_attributes[] = {
1342 &driver_attr_debug_level, &driver_attr_version,
1343 &driver_attr_interface_version,
1346 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1348 int i, res;
1350 i = 0;
1351 res = 0;
1352 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1353 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1354 i++;
1357 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1358 if (!res && dbg_wlswemul)
1359 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1360 if (!res && dbg_bluetoothemul)
1361 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1362 if (!res && dbg_wwanemul)
1363 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1364 if (!res && dbg_uwbemul)
1365 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1366 #endif
1368 return res;
1371 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1373 int i;
1375 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1376 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1378 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1379 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1380 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1381 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1382 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1383 #endif
1386 /*************************************************************************
1387 * Firmware Data
1391 * Table of recommended minimum BIOS versions
1393 * Reasons for listing:
1394 * 1. Stable BIOS, listed because the unknown ammount of
1395 * bugs and bad ACPI behaviour on older versions
1397 * 2. BIOS or EC fw with known bugs that trigger on Linux
1399 * 3. BIOS with known reduced functionality in older versions
1401 * We recommend the latest BIOS and EC version.
1402 * We only support the latest BIOS and EC fw version as a rule.
1404 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1405 * Information from users in ThinkWiki
1407 * WARNING: we use this table also to detect that the machine is
1408 * a ThinkPad in some cases, so don't remove entries lightly.
1411 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1412 { .vendor = (__v), \
1413 .bios = TPID(__id1, __id2), \
1414 .ec = TPACPI_MATCH_ANY, \
1415 .quirks = TPACPI_MATCH_ANY << 16 \
1416 | (__bv1) << 8 | (__bv2) }
1418 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1419 __eid, __ev1, __ev2) \
1420 { .vendor = (__v), \
1421 .bios = TPID(__bid1, __bid2), \
1422 .ec = __eid, \
1423 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1424 | (__bv1) << 8 | (__bv2) }
1426 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1427 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1429 /* Outdated IBM BIOSes often lack the EC id string */
1430 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1431 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1432 __bv1, __bv2, TPID(__id1, __id2), \
1433 __ev1, __ev2), \
1434 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1435 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1436 __ev1, __ev2)
1438 /* Outdated IBM BIOSes often lack the EC id string */
1439 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1440 __eid1, __eid2, __ev1, __ev2) \
1441 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1442 __bv1, __bv2, TPID(__eid1, __eid2), \
1443 __ev1, __ev2), \
1444 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1445 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1446 __ev1, __ev2)
1448 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1449 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1451 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1452 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1453 __bv1, __bv2, TPID(__id1, __id2), \
1454 __ev1, __ev2)
1456 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1457 __eid1, __eid2, __ev1, __ev2) \
1458 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1459 __bv1, __bv2, TPID(__eid1, __eid2), \
1460 __ev1, __ev2)
1462 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1463 /* Numeric models ------------------ */
1464 /* FW MODEL BIOS VERS */
1465 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1466 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1467 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1468 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1469 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1470 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1471 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1472 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1473 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1475 /* A-series ------------------------- */
1476 /* FW MODEL BIOS VERS EC VERS */
1477 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1478 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1479 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1480 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1481 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1482 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1483 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1484 TPV_QI0('1', '3', '2', '0'), /* A22m */
1485 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1486 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1487 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1489 /* G-series ------------------------- */
1490 /* FW MODEL BIOS VERS */
1491 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1492 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1494 /* R-series, T-series --------------- */
1495 /* FW MODEL BIOS VERS EC VERS */
1496 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1497 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1498 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1499 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1500 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1501 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1502 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1503 T40/p, T41/p, T42/p (1) */
1504 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1505 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1506 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1507 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1509 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1510 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1511 TPV_QI0('1', '6', '3', '2'), /* T22 */
1512 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1513 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1514 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1516 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1517 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1518 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1520 /* BIOS FW BIOS VERS EC FW EC VERS */
1521 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1522 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1524 /* X-series ------------------------- */
1525 /* FW MODEL BIOS VERS EC VERS */
1526 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1527 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1528 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1529 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1530 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1531 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1532 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1534 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1535 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1537 /* (0) - older versions lack DMI EC fw string and functionality */
1538 /* (1) - older versions known to lack functionality */
1541 #undef TPV_QL1
1542 #undef TPV_QL0
1543 #undef TPV_QI2
1544 #undef TPV_QI1
1545 #undef TPV_QI0
1546 #undef TPV_Q_X
1547 #undef TPV_Q
1549 static void __init tpacpi_check_outdated_fw(void)
1551 unsigned long fwvers;
1552 u16 ec_version, bios_version;
1554 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1555 ARRAY_SIZE(tpacpi_bios_version_qtable));
1557 if (!fwvers)
1558 return;
1560 bios_version = fwvers & 0xffffU;
1561 ec_version = (fwvers >> 16) & 0xffffU;
1563 /* note that unknown versions are set to 0x0000 and we use that */
1564 if ((bios_version > thinkpad_id.bios_release) ||
1565 (ec_version > thinkpad_id.ec_release &&
1566 ec_version != TPACPI_MATCH_ANY)) {
1568 * The changelogs would let us track down the exact
1569 * reason, but it is just too much of a pain to track
1570 * it. We only list BIOSes that are either really
1571 * broken, or really stable to begin with, so it is
1572 * best if the user upgrades the firmware anyway.
1574 printk(TPACPI_WARN
1575 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1576 printk(TPACPI_WARN
1577 "WARNING: This firmware may be missing critical bug "
1578 "fixes and/or important features\n");
1582 static bool __init tpacpi_is_fw_known(void)
1584 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1585 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1588 /****************************************************************************
1589 ****************************************************************************
1591 * Subdrivers
1593 ****************************************************************************
1594 ****************************************************************************/
1596 /*************************************************************************
1597 * thinkpad-acpi init subdriver
1600 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1602 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1603 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1605 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1606 (thinkpad_id.bios_version_str) ?
1607 thinkpad_id.bios_version_str : "unknown",
1608 (thinkpad_id.ec_version_str) ?
1609 thinkpad_id.ec_version_str : "unknown");
1611 if (thinkpad_id.vendor && thinkpad_id.model_str)
1612 printk(TPACPI_INFO "%s %s, model %s\n",
1613 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1614 "IBM" : ((thinkpad_id.vendor ==
1615 PCI_VENDOR_ID_LENOVO) ?
1616 "Lenovo" : "Unknown vendor"),
1617 thinkpad_id.model_str,
1618 (thinkpad_id.nummodel_str) ?
1619 thinkpad_id.nummodel_str : "unknown");
1621 tpacpi_check_outdated_fw();
1622 return 0;
1625 static int thinkpad_acpi_driver_read(char *p)
1627 int len = 0;
1629 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1630 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1632 return len;
1635 static struct ibm_struct thinkpad_acpi_driver_data = {
1636 .name = "driver",
1637 .read = thinkpad_acpi_driver_read,
1640 /*************************************************************************
1641 * Hotkey subdriver
1645 * ThinkPad firmware event model
1647 * The ThinkPad firmware has two main event interfaces: normal ACPI
1648 * notifications (which follow the ACPI standard), and a private event
1649 * interface.
1651 * The private event interface also issues events for the hotkeys. As
1652 * the driver gained features, the event handling code ended up being
1653 * built around the hotkey subdriver. This will need to be refactored
1654 * to a more formal event API eventually.
1656 * Some "hotkeys" are actually supposed to be used as event reports,
1657 * such as "brightness has changed", "volume has changed", depending on
1658 * the ThinkPad model and how the firmware is operating.
1660 * Unlike other classes, hotkey-class events have mask/unmask control on
1661 * non-ancient firmware. However, how it behaves changes a lot with the
1662 * firmware model and version.
1665 enum { /* hot key scan codes (derived from ACPI DSDT) */
1666 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1667 TP_ACPI_HOTKEYSCAN_FNF2,
1668 TP_ACPI_HOTKEYSCAN_FNF3,
1669 TP_ACPI_HOTKEYSCAN_FNF4,
1670 TP_ACPI_HOTKEYSCAN_FNF5,
1671 TP_ACPI_HOTKEYSCAN_FNF6,
1672 TP_ACPI_HOTKEYSCAN_FNF7,
1673 TP_ACPI_HOTKEYSCAN_FNF8,
1674 TP_ACPI_HOTKEYSCAN_FNF9,
1675 TP_ACPI_HOTKEYSCAN_FNF10,
1676 TP_ACPI_HOTKEYSCAN_FNF11,
1677 TP_ACPI_HOTKEYSCAN_FNF12,
1678 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1679 TP_ACPI_HOTKEYSCAN_FNINSERT,
1680 TP_ACPI_HOTKEYSCAN_FNDELETE,
1681 TP_ACPI_HOTKEYSCAN_FNHOME,
1682 TP_ACPI_HOTKEYSCAN_FNEND,
1683 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1684 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1685 TP_ACPI_HOTKEYSCAN_FNSPACE,
1686 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1687 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1688 TP_ACPI_HOTKEYSCAN_MUTE,
1689 TP_ACPI_HOTKEYSCAN_THINKPAD,
1692 enum { /* Keys/events available through NVRAM polling */
1693 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1694 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1697 enum { /* Positions of some of the keys in hotkey masks */
1698 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1699 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1700 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1701 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1702 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1703 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1704 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1705 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1706 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1707 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1708 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1711 enum { /* NVRAM to ACPI HKEY group map */
1712 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1713 TP_ACPI_HKEY_ZOOM_MASK |
1714 TP_ACPI_HKEY_DISPSWTCH_MASK |
1715 TP_ACPI_HKEY_HIBERNATE_MASK,
1716 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1717 TP_ACPI_HKEY_BRGHTDWN_MASK,
1718 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1719 TP_ACPI_HKEY_VOLDWN_MASK |
1720 TP_ACPI_HKEY_MUTE_MASK,
1723 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1724 struct tp_nvram_state {
1725 u16 thinkpad_toggle:1;
1726 u16 zoom_toggle:1;
1727 u16 display_toggle:1;
1728 u16 thinklight_toggle:1;
1729 u16 hibernate_toggle:1;
1730 u16 displayexp_toggle:1;
1731 u16 display_state:1;
1732 u16 brightness_toggle:1;
1733 u16 volume_toggle:1;
1734 u16 mute:1;
1736 u8 brightness_level;
1737 u8 volume_level;
1740 /* kthread for the hotkey poller */
1741 static struct task_struct *tpacpi_hotkey_task;
1743 /* Acquired while the poller kthread is running, use to sync start/stop */
1744 static struct mutex hotkey_thread_mutex;
1747 * Acquire mutex to write poller control variables as an
1748 * atomic block.
1750 * Increment hotkey_config_change when changing them if you
1751 * want the kthread to forget old state.
1753 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1755 static struct mutex hotkey_thread_data_mutex;
1756 static unsigned int hotkey_config_change;
1759 * hotkey poller control variables
1761 * Must be atomic or readers will also need to acquire mutex
1763 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1764 * should be used only when the changes need to be taken as
1765 * a block, OR when one needs to force the kthread to forget
1766 * old state.
1768 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1769 static unsigned int hotkey_poll_freq = 10; /* Hz */
1771 #define HOTKEY_CONFIG_CRITICAL_START \
1772 do { \
1773 mutex_lock(&hotkey_thread_data_mutex); \
1774 hotkey_config_change++; \
1775 } while (0);
1776 #define HOTKEY_CONFIG_CRITICAL_END \
1777 mutex_unlock(&hotkey_thread_data_mutex);
1779 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1781 #define hotkey_source_mask 0U
1782 #define HOTKEY_CONFIG_CRITICAL_START
1783 #define HOTKEY_CONFIG_CRITICAL_END
1785 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1787 static struct mutex hotkey_mutex;
1789 static enum { /* Reasons for waking up */
1790 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1791 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1792 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1793 } hotkey_wakeup_reason;
1795 static int hotkey_autosleep_ack;
1797 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1798 static u32 hotkey_all_mask; /* all events supported in fw */
1799 static u32 hotkey_reserved_mask; /* events better left disabled */
1800 static u32 hotkey_driver_mask; /* events needed by the driver */
1801 static u32 hotkey_user_mask; /* events visible to userspace */
1802 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1804 static unsigned int hotkey_report_mode;
1806 static u16 *hotkey_keycode_map;
1808 static struct attribute_set *hotkey_dev_attributes;
1810 static void tpacpi_driver_event(const unsigned int hkey_event);
1811 static void hotkey_driver_event(const unsigned int scancode);
1813 /* HKEY.MHKG() return bits */
1814 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1816 static int hotkey_get_wlsw(int *status)
1818 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1819 if (dbg_wlswemul) {
1820 *status = !!tpacpi_wlsw_emulstate;
1821 return 0;
1823 #endif
1824 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1825 return -EIO;
1826 return 0;
1829 static int hotkey_get_tablet_mode(int *status)
1831 int s;
1833 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1834 return -EIO;
1836 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1837 return 0;
1841 * Reads current event mask from firmware, and updates
1842 * hotkey_acpi_mask accordingly. Also resets any bits
1843 * from hotkey_user_mask that are unavailable to be
1844 * delivered (shadow requirement of the userspace ABI).
1846 * Call with hotkey_mutex held
1848 static int hotkey_mask_get(void)
1850 if (tp_features.hotkey_mask) {
1851 u32 m = 0;
1853 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1854 return -EIO;
1856 hotkey_acpi_mask = m;
1857 } else {
1858 /* no mask support doesn't mean no event support... */
1859 hotkey_acpi_mask = hotkey_all_mask;
1862 /* sync userspace-visible mask */
1863 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1865 return 0;
1868 void static hotkey_mask_warn_incomplete_mask(void)
1870 /* log only what the user can fix... */
1871 const u32 wantedmask = hotkey_driver_mask &
1872 ~(hotkey_acpi_mask | hotkey_source_mask) &
1873 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1875 if (wantedmask)
1876 printk(TPACPI_NOTICE
1877 "required events 0x%08x not enabled!\n",
1878 wantedmask);
1882 * Set the firmware mask when supported
1884 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1886 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1888 * Call with hotkey_mutex held
1890 static int hotkey_mask_set(u32 mask)
1892 int i;
1893 int rc = 0;
1895 const u32 fwmask = mask & ~hotkey_source_mask;
1897 if (tp_features.hotkey_mask) {
1898 for (i = 0; i < 32; i++) {
1899 if (!acpi_evalf(hkey_handle,
1900 NULL, "MHKM", "vdd", i + 1,
1901 !!(mask & (1 << i)))) {
1902 rc = -EIO;
1903 break;
1909 * We *must* make an inconditional call to hotkey_mask_get to
1910 * refresh hotkey_acpi_mask and update hotkey_user_mask
1912 * Take the opportunity to also log when we cannot _enable_
1913 * a given event.
1915 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1916 printk(TPACPI_NOTICE
1917 "asked for hotkey mask 0x%08x, but "
1918 "firmware forced it to 0x%08x\n",
1919 fwmask, hotkey_acpi_mask);
1922 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1923 hotkey_mask_warn_incomplete_mask();
1925 return rc;
1929 * Sets hotkey_user_mask and tries to set the firmware mask
1931 * Call with hotkey_mutex held
1933 static int hotkey_user_mask_set(const u32 mask)
1935 int rc;
1937 /* Give people a chance to notice they are doing something that
1938 * is bound to go boom on their users sooner or later */
1939 if (!tp_warned.hotkey_mask_ff &&
1940 (mask == 0xffff || mask == 0xffffff ||
1941 mask == 0xffffffff)) {
1942 tp_warned.hotkey_mask_ff = 1;
1943 printk(TPACPI_NOTICE
1944 "setting the hotkey mask to 0x%08x is likely "
1945 "not the best way to go about it\n", mask);
1946 printk(TPACPI_NOTICE
1947 "please consider using the driver defaults, "
1948 "and refer to up-to-date thinkpad-acpi "
1949 "documentation\n");
1952 /* Try to enable what the user asked for, plus whatever we need.
1953 * this syncs everything but won't enable bits in hotkey_user_mask */
1954 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1956 /* Enable the available bits in hotkey_user_mask */
1957 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1959 return rc;
1963 * Sets the driver hotkey mask.
1965 * Can be called even if the hotkey subdriver is inactive
1967 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1969 int rc;
1971 /* Do the right thing if hotkey_init has not been called yet */
1972 if (!tp_features.hotkey) {
1973 hotkey_driver_mask = mask;
1974 return 0;
1977 mutex_lock(&hotkey_mutex);
1979 HOTKEY_CONFIG_CRITICAL_START
1980 hotkey_driver_mask = mask;
1981 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1982 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1983 #endif
1984 HOTKEY_CONFIG_CRITICAL_END
1986 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1987 ~hotkey_source_mask);
1988 mutex_unlock(&hotkey_mutex);
1990 return rc;
1993 static int hotkey_status_get(int *status)
1995 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1996 return -EIO;
1998 return 0;
2001 static int hotkey_status_set(bool enable)
2003 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2004 return -EIO;
2006 return 0;
2009 static void tpacpi_input_send_tabletsw(void)
2011 int state;
2013 if (tp_features.hotkey_tablet &&
2014 !hotkey_get_tablet_mode(&state)) {
2015 mutex_lock(&tpacpi_inputdev_send_mutex);
2017 input_report_switch(tpacpi_inputdev,
2018 SW_TABLET_MODE, !!state);
2019 input_sync(tpacpi_inputdev);
2021 mutex_unlock(&tpacpi_inputdev_send_mutex);
2025 /* Do NOT call without validating scancode first */
2026 static void tpacpi_input_send_key(const unsigned int scancode)
2028 const unsigned int keycode = hotkey_keycode_map[scancode];
2030 if (keycode != KEY_RESERVED) {
2031 mutex_lock(&tpacpi_inputdev_send_mutex);
2033 input_report_key(tpacpi_inputdev, keycode, 1);
2034 if (keycode == KEY_UNKNOWN)
2035 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2036 scancode);
2037 input_sync(tpacpi_inputdev);
2039 input_report_key(tpacpi_inputdev, keycode, 0);
2040 if (keycode == KEY_UNKNOWN)
2041 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2042 scancode);
2043 input_sync(tpacpi_inputdev);
2045 mutex_unlock(&tpacpi_inputdev_send_mutex);
2049 /* Do NOT call without validating scancode first */
2050 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2052 hotkey_driver_event(scancode);
2053 if (hotkey_user_mask & (1 << scancode))
2054 tpacpi_input_send_key(scancode);
2057 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2058 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2060 /* Do NOT call without validating scancode first */
2061 static void tpacpi_hotkey_send_key(unsigned int scancode)
2063 tpacpi_input_send_key_masked(scancode);
2064 if (hotkey_report_mode < 2) {
2065 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2066 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2070 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2072 u8 d;
2074 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2075 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2076 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2077 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2078 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2079 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2081 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2082 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2083 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2085 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2086 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2087 n->displayexp_toggle =
2088 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2090 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2091 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2092 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2093 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2094 n->brightness_toggle =
2095 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2097 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2098 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2099 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2100 >> TP_NVRAM_POS_LEVEL_VOLUME;
2101 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2102 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2106 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2107 struct tp_nvram_state *newn,
2108 const u32 event_mask)
2111 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2112 do { \
2113 if ((event_mask & (1 << __scancode)) && \
2114 oldn->__member != newn->__member) \
2115 tpacpi_hotkey_send_key(__scancode); \
2116 } while (0)
2118 #define TPACPI_MAY_SEND_KEY(__scancode) \
2119 do { \
2120 if (event_mask & (1 << __scancode)) \
2121 tpacpi_hotkey_send_key(__scancode); \
2122 } while (0)
2124 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2125 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2126 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2127 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2129 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2131 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2133 /* handle volume */
2134 if (oldn->volume_toggle != newn->volume_toggle) {
2135 if (oldn->mute != newn->mute) {
2136 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2138 if (oldn->volume_level > newn->volume_level) {
2139 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2140 } else if (oldn->volume_level < newn->volume_level) {
2141 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2142 } else if (oldn->mute == newn->mute) {
2143 /* repeated key presses that didn't change state */
2144 if (newn->mute) {
2145 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2146 } else if (newn->volume_level != 0) {
2147 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2148 } else {
2149 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2154 /* handle brightness */
2155 if (oldn->brightness_toggle != newn->brightness_toggle) {
2156 if (oldn->brightness_level < newn->brightness_level) {
2157 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2158 } else if (oldn->brightness_level > newn->brightness_level) {
2159 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2160 } else {
2161 /* repeated key presses that didn't change state */
2162 if (newn->brightness_level != 0) {
2163 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2164 } else {
2165 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2170 #undef TPACPI_COMPARE_KEY
2171 #undef TPACPI_MAY_SEND_KEY
2175 * Polling driver
2177 * We track all events in hotkey_source_mask all the time, since
2178 * most of them are edge-based. We only issue those requested by
2179 * hotkey_user_mask or hotkey_driver_mask, though.
2181 static int hotkey_kthread(void *data)
2183 struct tp_nvram_state s[2];
2184 u32 poll_mask, event_mask;
2185 unsigned int si, so;
2186 unsigned long t;
2187 unsigned int change_detector, must_reset;
2188 unsigned int poll_freq;
2190 mutex_lock(&hotkey_thread_mutex);
2192 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2193 goto exit;
2195 set_freezable();
2197 so = 0;
2198 si = 1;
2199 t = 0;
2201 /* Initial state for compares */
2202 mutex_lock(&hotkey_thread_data_mutex);
2203 change_detector = hotkey_config_change;
2204 poll_mask = hotkey_source_mask;
2205 event_mask = hotkey_source_mask &
2206 (hotkey_driver_mask | hotkey_user_mask);
2207 poll_freq = hotkey_poll_freq;
2208 mutex_unlock(&hotkey_thread_data_mutex);
2209 hotkey_read_nvram(&s[so], poll_mask);
2211 while (!kthread_should_stop()) {
2212 if (t == 0) {
2213 if (likely(poll_freq))
2214 t = 1000/poll_freq;
2215 else
2216 t = 100; /* should never happen... */
2218 t = msleep_interruptible(t);
2219 if (unlikely(kthread_should_stop()))
2220 break;
2221 must_reset = try_to_freeze();
2222 if (t > 0 && !must_reset)
2223 continue;
2225 mutex_lock(&hotkey_thread_data_mutex);
2226 if (must_reset || hotkey_config_change != change_detector) {
2227 /* forget old state on thaw or config change */
2228 si = so;
2229 t = 0;
2230 change_detector = hotkey_config_change;
2232 poll_mask = hotkey_source_mask;
2233 event_mask = hotkey_source_mask &
2234 (hotkey_driver_mask | hotkey_user_mask);
2235 poll_freq = hotkey_poll_freq;
2236 mutex_unlock(&hotkey_thread_data_mutex);
2238 if (likely(poll_mask)) {
2239 hotkey_read_nvram(&s[si], poll_mask);
2240 if (likely(si != so)) {
2241 hotkey_compare_and_issue_event(&s[so], &s[si],
2242 event_mask);
2246 so = si;
2247 si ^= 1;
2250 exit:
2251 mutex_unlock(&hotkey_thread_mutex);
2252 return 0;
2255 /* call with hotkey_mutex held */
2256 static void hotkey_poll_stop_sync(void)
2258 if (tpacpi_hotkey_task) {
2259 if (frozen(tpacpi_hotkey_task) ||
2260 freezing(tpacpi_hotkey_task))
2261 thaw_process(tpacpi_hotkey_task);
2263 kthread_stop(tpacpi_hotkey_task);
2264 tpacpi_hotkey_task = NULL;
2265 mutex_lock(&hotkey_thread_mutex);
2266 /* at this point, the thread did exit */
2267 mutex_unlock(&hotkey_thread_mutex);
2271 /* call with hotkey_mutex held */
2272 static void hotkey_poll_setup(bool may_warn)
2274 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2275 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2277 if (hotkey_poll_freq > 0 &&
2278 (poll_driver_mask ||
2279 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2280 if (!tpacpi_hotkey_task) {
2281 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2282 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2283 if (IS_ERR(tpacpi_hotkey_task)) {
2284 tpacpi_hotkey_task = NULL;
2285 printk(TPACPI_ERR
2286 "could not create kernel thread "
2287 "for hotkey polling\n");
2290 } else {
2291 hotkey_poll_stop_sync();
2292 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2293 hotkey_poll_freq == 0) {
2294 printk(TPACPI_NOTICE
2295 "hot keys 0x%08x and/or events 0x%08x "
2296 "require polling, which is currently "
2297 "disabled\n",
2298 poll_user_mask, poll_driver_mask);
2303 static void hotkey_poll_setup_safe(bool may_warn)
2305 mutex_lock(&hotkey_mutex);
2306 hotkey_poll_setup(may_warn);
2307 mutex_unlock(&hotkey_mutex);
2310 /* call with hotkey_mutex held */
2311 static void hotkey_poll_set_freq(unsigned int freq)
2313 if (!freq)
2314 hotkey_poll_stop_sync();
2316 hotkey_poll_freq = freq;
2319 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2321 static void hotkey_poll_setup_safe(bool __unused)
2325 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2327 static int hotkey_inputdev_open(struct input_dev *dev)
2329 switch (tpacpi_lifecycle) {
2330 case TPACPI_LIFE_INIT:
2332 * hotkey_init will call hotkey_poll_setup_safe
2333 * at the appropriate moment
2335 return 0;
2336 case TPACPI_LIFE_EXITING:
2337 return -EBUSY;
2338 case TPACPI_LIFE_RUNNING:
2339 hotkey_poll_setup_safe(false);
2340 return 0;
2343 /* Should only happen if tpacpi_lifecycle is corrupt */
2344 BUG();
2345 return -EBUSY;
2348 static void hotkey_inputdev_close(struct input_dev *dev)
2350 /* disable hotkey polling when possible */
2351 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2352 !(hotkey_source_mask & hotkey_driver_mask))
2353 hotkey_poll_setup_safe(false);
2356 /* sysfs hotkey enable ------------------------------------------------- */
2357 static ssize_t hotkey_enable_show(struct device *dev,
2358 struct device_attribute *attr,
2359 char *buf)
2361 int res, status;
2363 printk_deprecated_attribute("hotkey_enable",
2364 "Hotkey reporting is always enabled");
2366 res = hotkey_status_get(&status);
2367 if (res)
2368 return res;
2370 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2373 static ssize_t hotkey_enable_store(struct device *dev,
2374 struct device_attribute *attr,
2375 const char *buf, size_t count)
2377 unsigned long t;
2379 printk_deprecated_attribute("hotkey_enable",
2380 "Hotkeys can be disabled through hotkey_mask");
2382 if (parse_strtoul(buf, 1, &t))
2383 return -EINVAL;
2385 if (t == 0)
2386 return -EPERM;
2388 return count;
2391 static struct device_attribute dev_attr_hotkey_enable =
2392 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2393 hotkey_enable_show, hotkey_enable_store);
2395 /* sysfs hotkey mask --------------------------------------------------- */
2396 static ssize_t hotkey_mask_show(struct device *dev,
2397 struct device_attribute *attr,
2398 char *buf)
2400 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2403 static ssize_t hotkey_mask_store(struct device *dev,
2404 struct device_attribute *attr,
2405 const char *buf, size_t count)
2407 unsigned long t;
2408 int res;
2410 if (parse_strtoul(buf, 0xffffffffUL, &t))
2411 return -EINVAL;
2413 if (mutex_lock_killable(&hotkey_mutex))
2414 return -ERESTARTSYS;
2416 res = hotkey_user_mask_set(t);
2418 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2419 hotkey_poll_setup(true);
2420 #endif
2422 mutex_unlock(&hotkey_mutex);
2424 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2426 return (res) ? res : count;
2429 static struct device_attribute dev_attr_hotkey_mask =
2430 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2431 hotkey_mask_show, hotkey_mask_store);
2433 /* sysfs hotkey bios_enabled ------------------------------------------- */
2434 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2435 struct device_attribute *attr,
2436 char *buf)
2438 return sprintf(buf, "0\n");
2441 static struct device_attribute dev_attr_hotkey_bios_enabled =
2442 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2444 /* sysfs hotkey bios_mask ---------------------------------------------- */
2445 static ssize_t hotkey_bios_mask_show(struct device *dev,
2446 struct device_attribute *attr,
2447 char *buf)
2449 printk_deprecated_attribute("hotkey_bios_mask",
2450 "This attribute is useless.");
2451 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2454 static struct device_attribute dev_attr_hotkey_bios_mask =
2455 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2457 /* sysfs hotkey all_mask ----------------------------------------------- */
2458 static ssize_t hotkey_all_mask_show(struct device *dev,
2459 struct device_attribute *attr,
2460 char *buf)
2462 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2463 hotkey_all_mask | hotkey_source_mask);
2466 static struct device_attribute dev_attr_hotkey_all_mask =
2467 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2469 /* sysfs hotkey recommended_mask --------------------------------------- */
2470 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2471 struct device_attribute *attr,
2472 char *buf)
2474 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2475 (hotkey_all_mask | hotkey_source_mask)
2476 & ~hotkey_reserved_mask);
2479 static struct device_attribute dev_attr_hotkey_recommended_mask =
2480 __ATTR(hotkey_recommended_mask, S_IRUGO,
2481 hotkey_recommended_mask_show, NULL);
2483 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2485 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2486 static ssize_t hotkey_source_mask_show(struct device *dev,
2487 struct device_attribute *attr,
2488 char *buf)
2490 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2493 static ssize_t hotkey_source_mask_store(struct device *dev,
2494 struct device_attribute *attr,
2495 const char *buf, size_t count)
2497 unsigned long t;
2498 u32 r_ev;
2499 int rc;
2501 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2502 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2503 return -EINVAL;
2505 if (mutex_lock_killable(&hotkey_mutex))
2506 return -ERESTARTSYS;
2508 HOTKEY_CONFIG_CRITICAL_START
2509 hotkey_source_mask = t;
2510 HOTKEY_CONFIG_CRITICAL_END
2512 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2513 ~hotkey_source_mask);
2514 hotkey_poll_setup(true);
2516 /* check if events needed by the driver got disabled */
2517 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2518 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2520 mutex_unlock(&hotkey_mutex);
2522 if (rc < 0)
2523 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2524 "firmware event mask!\n");
2526 if (r_ev)
2527 printk(TPACPI_NOTICE "hotkey_source_mask: "
2528 "some important events were disabled: "
2529 "0x%04x\n", r_ev);
2531 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2533 return (rc < 0) ? rc : count;
2536 static struct device_attribute dev_attr_hotkey_source_mask =
2537 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2538 hotkey_source_mask_show, hotkey_source_mask_store);
2540 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2541 static ssize_t hotkey_poll_freq_show(struct device *dev,
2542 struct device_attribute *attr,
2543 char *buf)
2545 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2548 static ssize_t hotkey_poll_freq_store(struct device *dev,
2549 struct device_attribute *attr,
2550 const char *buf, size_t count)
2552 unsigned long t;
2554 if (parse_strtoul(buf, 25, &t))
2555 return -EINVAL;
2557 if (mutex_lock_killable(&hotkey_mutex))
2558 return -ERESTARTSYS;
2560 hotkey_poll_set_freq(t);
2561 hotkey_poll_setup(true);
2563 mutex_unlock(&hotkey_mutex);
2565 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2567 return count;
2570 static struct device_attribute dev_attr_hotkey_poll_freq =
2571 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2572 hotkey_poll_freq_show, hotkey_poll_freq_store);
2574 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2576 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2577 static ssize_t hotkey_radio_sw_show(struct device *dev,
2578 struct device_attribute *attr,
2579 char *buf)
2581 int res, s;
2582 res = hotkey_get_wlsw(&s);
2583 if (res < 0)
2584 return res;
2586 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2589 static struct device_attribute dev_attr_hotkey_radio_sw =
2590 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2592 static void hotkey_radio_sw_notify_change(void)
2594 if (tp_features.hotkey_wlsw)
2595 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2596 "hotkey_radio_sw");
2599 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2600 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2601 struct device_attribute *attr,
2602 char *buf)
2604 int res, s;
2605 res = hotkey_get_tablet_mode(&s);
2606 if (res < 0)
2607 return res;
2609 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2612 static struct device_attribute dev_attr_hotkey_tablet_mode =
2613 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2615 static void hotkey_tablet_mode_notify_change(void)
2617 if (tp_features.hotkey_tablet)
2618 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2619 "hotkey_tablet_mode");
2622 /* sysfs hotkey report_mode -------------------------------------------- */
2623 static ssize_t hotkey_report_mode_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2627 return snprintf(buf, PAGE_SIZE, "%d\n",
2628 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2631 static struct device_attribute dev_attr_hotkey_report_mode =
2632 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2634 /* sysfs wakeup reason (pollable) -------------------------------------- */
2635 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2636 struct device_attribute *attr,
2637 char *buf)
2639 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2642 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2643 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2645 static void hotkey_wakeup_reason_notify_change(void)
2647 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2648 "wakeup_reason");
2651 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2652 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2653 struct device_attribute *attr,
2654 char *buf)
2656 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2659 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2660 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2661 hotkey_wakeup_hotunplug_complete_show, NULL);
2663 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2665 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2666 "wakeup_hotunplug_complete");
2669 /* --------------------------------------------------------------------- */
2671 static struct attribute *hotkey_attributes[] __initdata = {
2672 &dev_attr_hotkey_enable.attr,
2673 &dev_attr_hotkey_bios_enabled.attr,
2674 &dev_attr_hotkey_bios_mask.attr,
2675 &dev_attr_hotkey_report_mode.attr,
2676 &dev_attr_hotkey_wakeup_reason.attr,
2677 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2678 &dev_attr_hotkey_mask.attr,
2679 &dev_attr_hotkey_all_mask.attr,
2680 &dev_attr_hotkey_recommended_mask.attr,
2681 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2682 &dev_attr_hotkey_source_mask.attr,
2683 &dev_attr_hotkey_poll_freq.attr,
2684 #endif
2687 static void bluetooth_update_rfk(void);
2688 static void wan_update_rfk(void);
2689 static void uwb_update_rfk(void);
2690 static void tpacpi_send_radiosw_update(void)
2692 int wlsw;
2694 /* Sync these BEFORE sending any rfkill events */
2695 if (tp_features.bluetooth)
2696 bluetooth_update_rfk();
2697 if (tp_features.wan)
2698 wan_update_rfk();
2699 if (tp_features.uwb)
2700 uwb_update_rfk();
2702 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2703 mutex_lock(&tpacpi_inputdev_send_mutex);
2705 input_report_switch(tpacpi_inputdev,
2706 SW_RFKILL_ALL, !!wlsw);
2707 input_sync(tpacpi_inputdev);
2709 mutex_unlock(&tpacpi_inputdev_send_mutex);
2711 hotkey_radio_sw_notify_change();
2714 static void hotkey_exit(void)
2716 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2717 mutex_lock(&hotkey_mutex);
2718 hotkey_poll_stop_sync();
2719 mutex_unlock(&hotkey_mutex);
2720 #endif
2722 if (hotkey_dev_attributes)
2723 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2725 kfree(hotkey_keycode_map);
2727 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2728 "restoring original HKEY status and mask\n");
2729 /* yes, there is a bitwise or below, we want the
2730 * functions to be called even if one of them fail */
2731 if (((tp_features.hotkey_mask &&
2732 hotkey_mask_set(hotkey_orig_mask)) |
2733 hotkey_status_set(false)) != 0)
2734 printk(TPACPI_ERR
2735 "failed to restore hot key mask "
2736 "to BIOS defaults\n");
2739 static void __init hotkey_unmap(const unsigned int scancode)
2741 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2742 clear_bit(hotkey_keycode_map[scancode],
2743 tpacpi_inputdev->keybit);
2744 hotkey_keycode_map[scancode] = KEY_RESERVED;
2749 * HKEY quirks:
2750 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2753 #define TPACPI_HK_Q_INIMASK 0x0001
2755 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2756 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2757 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2758 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2759 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2760 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2761 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2762 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2763 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2764 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2765 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2766 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2767 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2768 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2769 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2770 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2771 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2772 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2773 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2774 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2777 static int __init hotkey_init(struct ibm_init_struct *iibm)
2779 /* Requirements for changing the default keymaps:
2781 * 1. Many of the keys are mapped to KEY_RESERVED for very
2782 * good reasons. Do not change them unless you have deep
2783 * knowledge on the IBM and Lenovo ThinkPad firmware for
2784 * the various ThinkPad models. The driver behaves
2785 * differently for KEY_RESERVED: such keys have their
2786 * hot key mask *unset* in mask_recommended, and also
2787 * in the initial hot key mask programmed into the
2788 * firmware at driver load time, which means the firm-
2789 * ware may react very differently if you change them to
2790 * something else;
2792 * 2. You must be subscribed to the linux-thinkpad and
2793 * ibm-acpi-devel mailing lists, and you should read the
2794 * list archives since 2007 if you want to change the
2795 * keymaps. This requirement exists so that you will
2796 * know the past history of problems with the thinkpad-
2797 * acpi driver keymaps, and also that you will be
2798 * listening to any bug reports;
2800 * 3. Do not send thinkpad-acpi specific patches directly to
2801 * for merging, *ever*. Send them to the linux-acpi
2802 * mailinglist for comments. Merging is to be done only
2803 * through acpi-test and the ACPI maintainer.
2805 * If the above is too much to ask, don't change the keymap.
2806 * Ask the thinkpad-acpi maintainer to do it, instead.
2808 static u16 ibm_keycode_map[] __initdata = {
2809 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2810 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2811 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2812 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2814 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2815 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2816 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2817 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2819 /* brightness: firmware always reacts to them */
2820 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2821 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2823 /* Thinklight: firmware always react to it */
2824 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2826 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2827 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2829 /* Volume: firmware always react to it and reprograms
2830 * the built-in *extra* mixer. Never map it to control
2831 * another mixer by default. */
2832 KEY_RESERVED, /* 0x14: VOLUME UP */
2833 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2834 KEY_RESERVED, /* 0x16: MUTE */
2836 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2838 /* (assignments unknown, please report if found) */
2839 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2840 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2842 static u16 lenovo_keycode_map[] __initdata = {
2843 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2844 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2845 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2846 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2848 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2849 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2850 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2851 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2853 /* These should be enabled --only-- when ACPI video
2854 * is disabled (i.e. in "vendor" mode), and are handled
2855 * in a special way by the init code */
2856 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2857 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2859 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2861 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2862 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2864 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2865 * react to it and reprograms the built-in *extra* mixer.
2866 * Never map it to control another mixer by default.
2868 * T60?, T61, R60?, R61: firmware and EC tries to send
2869 * these over the regular keyboard, so these are no-ops,
2870 * but there are still weird bugs re. MUTE, so do not
2871 * change unless you get test reports from all Lenovo
2872 * models. May cause the BIOS to interfere with the
2873 * HDA mixer.
2875 KEY_RESERVED, /* 0x14: VOLUME UP */
2876 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2877 KEY_RESERVED, /* 0x16: MUTE */
2879 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2881 /* (assignments unknown, please report if found) */
2882 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2883 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2886 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2887 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2888 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2890 int res, i;
2891 int status;
2892 int hkeyv;
2893 bool radiosw_state = false;
2894 bool tabletsw_state = false;
2896 unsigned long quirks;
2898 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2899 "initializing hotkey subdriver\n");
2901 BUG_ON(!tpacpi_inputdev);
2902 BUG_ON(tpacpi_inputdev->open != NULL ||
2903 tpacpi_inputdev->close != NULL);
2905 TPACPI_ACPIHANDLE_INIT(hkey);
2906 mutex_init(&hotkey_mutex);
2908 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2909 mutex_init(&hotkey_thread_mutex);
2910 mutex_init(&hotkey_thread_data_mutex);
2911 #endif
2913 /* hotkey not supported on 570 */
2914 tp_features.hotkey = hkey_handle != NULL;
2916 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2917 "hotkeys are %s\n",
2918 str_supported(tp_features.hotkey));
2920 if (!tp_features.hotkey)
2921 return 1;
2923 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2924 ARRAY_SIZE(tpacpi_hotkey_qtable));
2926 tpacpi_disable_brightness_delay();
2928 /* MUST have enough space for all attributes to be added to
2929 * hotkey_dev_attributes */
2930 hotkey_dev_attributes = create_attr_set(
2931 ARRAY_SIZE(hotkey_attributes) + 2,
2932 NULL);
2933 if (!hotkey_dev_attributes)
2934 return -ENOMEM;
2935 res = add_many_to_attr_set(hotkey_dev_attributes,
2936 hotkey_attributes,
2937 ARRAY_SIZE(hotkey_attributes));
2938 if (res)
2939 goto err_exit;
2941 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2942 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2943 for HKEY interface version 0x100 */
2944 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2945 if ((hkeyv >> 8) != 1) {
2946 printk(TPACPI_ERR "unknown version of the "
2947 "HKEY interface: 0x%x\n", hkeyv);
2948 printk(TPACPI_ERR "please report this to %s\n",
2949 TPACPI_MAIL);
2950 } else {
2952 * MHKV 0x100 in A31, R40, R40e,
2953 * T4x, X31, and later
2955 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2956 "firmware HKEY interface version: 0x%x\n",
2957 hkeyv);
2959 /* Paranoia check AND init hotkey_all_mask */
2960 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2961 "MHKA", "qd")) {
2962 printk(TPACPI_ERR
2963 "missing MHKA handler, "
2964 "please report this to %s\n",
2965 TPACPI_MAIL);
2966 /* Fallback: pre-init for FN+F3,F4,F12 */
2967 hotkey_all_mask = 0x080cU;
2968 } else {
2969 tp_features.hotkey_mask = 1;
2974 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2975 "hotkey masks are %s\n",
2976 str_supported(tp_features.hotkey_mask));
2978 /* Init hotkey_all_mask if not initialized yet */
2979 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2980 (quirks & TPACPI_HK_Q_INIMASK))
2981 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2983 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2984 if (tp_features.hotkey_mask) {
2985 /* hotkey_source_mask *must* be zero for
2986 * the first hotkey_mask_get to return hotkey_orig_mask */
2987 res = hotkey_mask_get();
2988 if (res)
2989 goto err_exit;
2991 hotkey_orig_mask = hotkey_acpi_mask;
2992 } else {
2993 hotkey_orig_mask = hotkey_all_mask;
2994 hotkey_acpi_mask = hotkey_all_mask;
2997 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2998 if (dbg_wlswemul) {
2999 tp_features.hotkey_wlsw = 1;
3000 radiosw_state = !!tpacpi_wlsw_emulstate;
3001 printk(TPACPI_INFO
3002 "radio switch emulation enabled\n");
3003 } else
3004 #endif
3005 /* Not all thinkpads have a hardware radio switch */
3006 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3007 tp_features.hotkey_wlsw = 1;
3008 radiosw_state = !!status;
3009 printk(TPACPI_INFO
3010 "radio switch found; radios are %s\n",
3011 enabled(status, 0));
3013 if (tp_features.hotkey_wlsw)
3014 res = add_to_attr_set(hotkey_dev_attributes,
3015 &dev_attr_hotkey_radio_sw.attr);
3017 /* For X41t, X60t, X61t Tablets... */
3018 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3019 tp_features.hotkey_tablet = 1;
3020 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3021 printk(TPACPI_INFO
3022 "possible tablet mode switch found; "
3023 "ThinkPad in %s mode\n",
3024 (tabletsw_state) ? "tablet" : "laptop");
3025 res = add_to_attr_set(hotkey_dev_attributes,
3026 &dev_attr_hotkey_tablet_mode.attr);
3029 if (!res)
3030 res = register_attr_set_with_sysfs(
3031 hotkey_dev_attributes,
3032 &tpacpi_pdev->dev.kobj);
3033 if (res)
3034 goto err_exit;
3036 /* Set up key map */
3038 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3039 GFP_KERNEL);
3040 if (!hotkey_keycode_map) {
3041 printk(TPACPI_ERR
3042 "failed to allocate memory for key map\n");
3043 res = -ENOMEM;
3044 goto err_exit;
3047 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3048 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3049 "using Lenovo default hot key map\n");
3050 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3051 TPACPI_HOTKEY_MAP_SIZE);
3052 } else {
3053 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3054 "using IBM default hot key map\n");
3055 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3056 TPACPI_HOTKEY_MAP_SIZE);
3059 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3060 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3061 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3062 tpacpi_inputdev->keycode = hotkey_keycode_map;
3063 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3064 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3065 input_set_capability(tpacpi_inputdev, EV_KEY,
3066 hotkey_keycode_map[i]);
3067 } else {
3068 if (i < sizeof(hotkey_reserved_mask)*8)
3069 hotkey_reserved_mask |= 1 << i;
3073 if (tp_features.hotkey_wlsw) {
3074 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3075 input_report_switch(tpacpi_inputdev,
3076 SW_RFKILL_ALL, radiosw_state);
3078 if (tp_features.hotkey_tablet) {
3079 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3080 input_report_switch(tpacpi_inputdev,
3081 SW_TABLET_MODE, tabletsw_state);
3084 /* Do not issue duplicate brightness change events to
3085 * userspace */
3086 if (!tp_features.bright_acpimode)
3087 /* update bright_acpimode... */
3088 tpacpi_check_std_acpi_brightness_support();
3090 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3091 printk(TPACPI_INFO
3092 "This ThinkPad has standard ACPI backlight "
3093 "brightness control, supported by the ACPI "
3094 "video driver\n");
3095 printk(TPACPI_NOTICE
3096 "Disabling thinkpad-acpi brightness events "
3097 "by default...\n");
3099 /* Disable brightness up/down on Lenovo thinkpads when
3100 * ACPI is handling them, otherwise it is plain impossible
3101 * for userspace to do something even remotely sane */
3102 hotkey_reserved_mask |=
3103 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3104 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3105 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3106 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3109 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3110 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3111 & ~hotkey_all_mask
3112 & ~hotkey_reserved_mask;
3114 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3115 "hotkey source mask 0x%08x, polling freq %u\n",
3116 hotkey_source_mask, hotkey_poll_freq);
3117 #endif
3119 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3120 "enabling firmware HKEY event interface...\n");
3121 res = hotkey_status_set(true);
3122 if (res) {
3123 hotkey_exit();
3124 return res;
3126 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3127 | hotkey_driver_mask)
3128 & ~hotkey_source_mask);
3129 if (res < 0 && res != -ENXIO) {
3130 hotkey_exit();
3131 return res;
3133 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3134 & ~hotkey_reserved_mask;
3135 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3136 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3137 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3139 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3140 "legacy ibm/hotkey event reporting over procfs %s\n",
3141 (hotkey_report_mode < 2) ?
3142 "enabled" : "disabled");
3144 tpacpi_inputdev->open = &hotkey_inputdev_open;
3145 tpacpi_inputdev->close = &hotkey_inputdev_close;
3147 hotkey_poll_setup_safe(true);
3149 return 0;
3151 err_exit:
3152 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3153 hotkey_dev_attributes = NULL;
3155 return (res < 0)? res : 1;
3158 static bool hotkey_notify_hotkey(const u32 hkey,
3159 bool *send_acpi_ev,
3160 bool *ignore_acpi_ev)
3162 /* 0x1000-0x1FFF: key presses */
3163 unsigned int scancode = hkey & 0xfff;
3164 *send_acpi_ev = true;
3165 *ignore_acpi_ev = false;
3167 if (scancode > 0 && scancode < 0x21) {
3168 scancode--;
3169 if (!(hotkey_source_mask & (1 << scancode))) {
3170 tpacpi_input_send_key_masked(scancode);
3171 *send_acpi_ev = false;
3172 } else {
3173 *ignore_acpi_ev = true;
3175 return true;
3177 return false;
3180 static bool hotkey_notify_wakeup(const u32 hkey,
3181 bool *send_acpi_ev,
3182 bool *ignore_acpi_ev)
3184 /* 0x2000-0x2FFF: Wakeup reason */
3185 *send_acpi_ev = true;
3186 *ignore_acpi_ev = false;
3188 switch (hkey) {
3189 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3190 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3191 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3192 *ignore_acpi_ev = true;
3193 break;
3195 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3196 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3197 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3198 *ignore_acpi_ev = true;
3199 break;
3201 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3202 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3203 printk(TPACPI_ALERT
3204 "EMERGENCY WAKEUP: battery almost empty\n");
3205 /* how to auto-heal: */
3206 /* 2313: woke up from S3, go to S4/S5 */
3207 /* 2413: woke up from S4, go to S5 */
3208 break;
3210 default:
3211 return false;
3214 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3215 printk(TPACPI_INFO
3216 "woke up due to a hot-unplug "
3217 "request...\n");
3218 hotkey_wakeup_reason_notify_change();
3220 return true;
3223 static bool hotkey_notify_usrevent(const u32 hkey,
3224 bool *send_acpi_ev,
3225 bool *ignore_acpi_ev)
3227 /* 0x5000-0x5FFF: human interface helpers */
3228 *send_acpi_ev = true;
3229 *ignore_acpi_ev = false;
3231 switch (hkey) {
3232 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3233 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3234 return true;
3236 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3237 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3238 tpacpi_input_send_tabletsw();
3239 hotkey_tablet_mode_notify_change();
3240 *send_acpi_ev = false;
3241 return true;
3243 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3244 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3245 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3246 /* do not propagate these events */
3247 *ignore_acpi_ev = true;
3248 return true;
3250 default:
3251 return false;
3255 static void thermal_dump_all_sensors(void);
3257 static bool hotkey_notify_thermal(const u32 hkey,
3258 bool *send_acpi_ev,
3259 bool *ignore_acpi_ev)
3261 bool known = true;
3263 /* 0x6000-0x6FFF: thermal alarms */
3264 *send_acpi_ev = true;
3265 *ignore_acpi_ev = false;
3267 switch (hkey) {
3268 case TP_HKEY_EV_THM_TABLE_CHANGED:
3269 printk(TPACPI_INFO
3270 "EC reports that Thermal Table has changed\n");
3271 /* recommended action: do nothing, we don't have
3272 * Lenovo ATM information */
3273 return true;
3274 case TP_HKEY_EV_ALARM_BAT_HOT:
3275 printk(TPACPI_CRIT
3276 "THERMAL ALARM: battery is too hot!\n");
3277 /* recommended action: warn user through gui */
3278 break;
3279 case TP_HKEY_EV_ALARM_BAT_XHOT:
3280 printk(TPACPI_ALERT
3281 "THERMAL EMERGENCY: battery is extremely hot!\n");
3282 /* recommended action: immediate sleep/hibernate */
3283 break;
3284 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3285 printk(TPACPI_CRIT
3286 "THERMAL ALARM: "
3287 "a sensor reports something is too hot!\n");
3288 /* recommended action: warn user through gui, that */
3289 /* some internal component is too hot */
3290 break;
3291 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3292 printk(TPACPI_ALERT
3293 "THERMAL EMERGENCY: "
3294 "a sensor reports something is extremely hot!\n");
3295 /* recommended action: immediate sleep/hibernate */
3296 break;
3297 default:
3298 printk(TPACPI_ALERT
3299 "THERMAL ALERT: unknown thermal alarm received\n");
3300 known = false;
3303 thermal_dump_all_sensors();
3305 return known;
3308 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3310 u32 hkey;
3311 bool send_acpi_ev;
3312 bool ignore_acpi_ev;
3313 bool known_ev;
3315 if (event != 0x80) {
3316 printk(TPACPI_ERR
3317 "unknown HKEY notification event %d\n", event);
3318 /* forward it to userspace, maybe it knows how to handle it */
3319 acpi_bus_generate_netlink_event(
3320 ibm->acpi->device->pnp.device_class,
3321 dev_name(&ibm->acpi->device->dev),
3322 event, 0);
3323 return;
3326 while (1) {
3327 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3328 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3329 return;
3332 if (hkey == 0) {
3333 /* queue empty */
3334 return;
3337 send_acpi_ev = true;
3338 ignore_acpi_ev = false;
3340 switch (hkey >> 12) {
3341 case 1:
3342 /* 0x1000-0x1FFF: key presses */
3343 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3344 &ignore_acpi_ev);
3345 break;
3346 case 2:
3347 /* 0x2000-0x2FFF: Wakeup reason */
3348 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3349 &ignore_acpi_ev);
3350 break;
3351 case 3:
3352 /* 0x3000-0x3FFF: bay-related wakeups */
3353 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3354 hotkey_autosleep_ack = 1;
3355 printk(TPACPI_INFO
3356 "bay ejected\n");
3357 hotkey_wakeup_hotunplug_complete_notify_change();
3358 known_ev = true;
3359 } else {
3360 known_ev = false;
3362 break;
3363 case 4:
3364 /* 0x4000-0x4FFF: dock-related wakeups */
3365 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3366 hotkey_autosleep_ack = 1;
3367 printk(TPACPI_INFO
3368 "undocked\n");
3369 hotkey_wakeup_hotunplug_complete_notify_change();
3370 known_ev = true;
3371 } else {
3372 known_ev = false;
3374 break;
3375 case 5:
3376 /* 0x5000-0x5FFF: human interface helpers */
3377 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3378 &ignore_acpi_ev);
3379 break;
3380 case 6:
3381 /* 0x6000-0x6FFF: thermal alarms */
3382 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3383 &ignore_acpi_ev);
3384 break;
3385 case 7:
3386 /* 0x7000-0x7FFF: misc */
3387 if (tp_features.hotkey_wlsw &&
3388 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3389 tpacpi_send_radiosw_update();
3390 send_acpi_ev = 0;
3391 known_ev = true;
3392 break;
3394 /* fallthrough to default */
3395 default:
3396 known_ev = false;
3398 if (!known_ev) {
3399 printk(TPACPI_NOTICE
3400 "unhandled HKEY event 0x%04x\n", hkey);
3401 printk(TPACPI_NOTICE
3402 "please report the conditions when this "
3403 "event happened to %s\n", TPACPI_MAIL);
3406 /* Legacy events */
3407 if (!ignore_acpi_ev &&
3408 (send_acpi_ev || hotkey_report_mode < 2)) {
3409 acpi_bus_generate_proc_event(ibm->acpi->device,
3410 event, hkey);
3413 /* netlink events */
3414 if (!ignore_acpi_ev && send_acpi_ev) {
3415 acpi_bus_generate_netlink_event(
3416 ibm->acpi->device->pnp.device_class,
3417 dev_name(&ibm->acpi->device->dev),
3418 event, hkey);
3423 static void hotkey_suspend(pm_message_t state)
3425 /* Do these on suspend, we get the events on early resume! */
3426 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3427 hotkey_autosleep_ack = 0;
3430 static void hotkey_resume(void)
3432 tpacpi_disable_brightness_delay();
3434 if (hotkey_status_set(true) < 0 ||
3435 hotkey_mask_set(hotkey_acpi_mask) < 0)
3436 printk(TPACPI_ERR
3437 "error while attempting to reset the event "
3438 "firmware interface\n");
3440 tpacpi_send_radiosw_update();
3441 hotkey_tablet_mode_notify_change();
3442 hotkey_wakeup_reason_notify_change();
3443 hotkey_wakeup_hotunplug_complete_notify_change();
3444 hotkey_poll_setup_safe(false);
3447 /* procfs -------------------------------------------------------------- */
3448 static int hotkey_read(char *p)
3450 int res, status;
3451 int len = 0;
3453 if (!tp_features.hotkey) {
3454 len += sprintf(p + len, "status:\t\tnot supported\n");
3455 return len;
3458 if (mutex_lock_killable(&hotkey_mutex))
3459 return -ERESTARTSYS;
3460 res = hotkey_status_get(&status);
3461 if (!res)
3462 res = hotkey_mask_get();
3463 mutex_unlock(&hotkey_mutex);
3464 if (res)
3465 return res;
3467 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3468 if (hotkey_all_mask) {
3469 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3470 len += sprintf(p + len,
3471 "commands:\tenable, disable, reset, <mask>\n");
3472 } else {
3473 len += sprintf(p + len, "mask:\t\tnot supported\n");
3474 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3477 return len;
3480 static void hotkey_enabledisable_warn(bool enable)
3482 tpacpi_log_usertask("procfs hotkey enable/disable");
3483 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3484 TPACPI_WARN
3485 "hotkey enable/disable functionality has been "
3486 "removed from the driver. Hotkeys are always "
3487 "enabled\n"))
3488 printk(TPACPI_ERR
3489 "Please remove the hotkey=enable module "
3490 "parameter, it is deprecated. Hotkeys are always "
3491 "enabled\n");
3494 static int hotkey_write(char *buf)
3496 int res;
3497 u32 mask;
3498 char *cmd;
3500 if (!tp_features.hotkey)
3501 return -ENODEV;
3503 if (mutex_lock_killable(&hotkey_mutex))
3504 return -ERESTARTSYS;
3506 mask = hotkey_user_mask;
3508 res = 0;
3509 while ((cmd = next_cmd(&buf))) {
3510 if (strlencmp(cmd, "enable") == 0) {
3511 hotkey_enabledisable_warn(1);
3512 } else if (strlencmp(cmd, "disable") == 0) {
3513 hotkey_enabledisable_warn(0);
3514 res = -EPERM;
3515 } else if (strlencmp(cmd, "reset") == 0) {
3516 mask = (hotkey_all_mask | hotkey_source_mask)
3517 & ~hotkey_reserved_mask;
3518 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3519 /* mask set */
3520 } else if (sscanf(cmd, "%x", &mask) == 1) {
3521 /* mask set */
3522 } else {
3523 res = -EINVAL;
3524 goto errexit;
3528 if (!res) {
3529 tpacpi_disclose_usertask("procfs hotkey",
3530 "set mask to 0x%08x\n", mask);
3531 res = hotkey_user_mask_set(mask);
3534 errexit:
3535 mutex_unlock(&hotkey_mutex);
3536 return res;
3539 static const struct acpi_device_id ibm_htk_device_ids[] = {
3540 {TPACPI_ACPI_HKEY_HID, 0},
3541 {"", 0},
3544 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3545 .hid = ibm_htk_device_ids,
3546 .notify = hotkey_notify,
3547 .handle = &hkey_handle,
3548 .type = ACPI_DEVICE_NOTIFY,
3551 static struct ibm_struct hotkey_driver_data = {
3552 .name = "hotkey",
3553 .read = hotkey_read,
3554 .write = hotkey_write,
3555 .exit = hotkey_exit,
3556 .resume = hotkey_resume,
3557 .suspend = hotkey_suspend,
3558 .acpi = &ibm_hotkey_acpidriver,
3561 /*************************************************************************
3562 * Bluetooth subdriver
3565 enum {
3566 /* ACPI GBDC/SBDC bits */
3567 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3568 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3569 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3570 off / last state */
3573 enum {
3574 /* ACPI \BLTH commands */
3575 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3576 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3577 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3578 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3579 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3582 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3584 static struct rfkill *tpacpi_bluetooth_rfkill;
3586 static void bluetooth_suspend(pm_message_t state)
3588 /* Try to make sure radio will resume powered off */
3589 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3590 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3591 vdbg_printk(TPACPI_DBG_RFKILL,
3592 "bluetooth power down on resume request failed\n");
3595 static int bluetooth_get_radiosw(void)
3597 int status;
3599 if (!tp_features.bluetooth)
3600 return -ENODEV;
3602 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3603 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3604 return RFKILL_STATE_HARD_BLOCKED;
3606 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3607 if (dbg_bluetoothemul)
3608 return (tpacpi_bluetooth_emulstate) ?
3609 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3610 #endif
3612 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3613 return -EIO;
3615 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3616 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3619 static void bluetooth_update_rfk(void)
3621 int status;
3623 if (!tpacpi_bluetooth_rfkill)
3624 return;
3626 status = bluetooth_get_radiosw();
3627 if (status < 0)
3628 return;
3629 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3631 vdbg_printk(TPACPI_DBG_RFKILL,
3632 "forced rfkill state to %d\n",
3633 status);
3636 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3638 int status;
3640 if (!tp_features.bluetooth)
3641 return -ENODEV;
3643 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3644 * reason to risk weird behaviour. */
3645 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3646 && radio_on)
3647 return -EPERM;
3649 vdbg_printk(TPACPI_DBG_RFKILL,
3650 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3652 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3653 if (dbg_bluetoothemul) {
3654 tpacpi_bluetooth_emulstate = !!radio_on;
3655 if (update_rfk)
3656 bluetooth_update_rfk();
3657 return 0;
3659 #endif
3661 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3662 if (radio_on)
3663 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3664 else
3665 status = 0;
3666 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3667 return -EIO;
3669 if (update_rfk)
3670 bluetooth_update_rfk();
3672 return 0;
3675 /* sysfs bluetooth enable ---------------------------------------------- */
3676 static ssize_t bluetooth_enable_show(struct device *dev,
3677 struct device_attribute *attr,
3678 char *buf)
3680 int status;
3682 printk_deprecated_rfkill_attribute("bluetooth_enable");
3684 status = bluetooth_get_radiosw();
3685 if (status < 0)
3686 return status;
3688 return snprintf(buf, PAGE_SIZE, "%d\n",
3689 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3692 static ssize_t bluetooth_enable_store(struct device *dev,
3693 struct device_attribute *attr,
3694 const char *buf, size_t count)
3696 unsigned long t;
3697 int res;
3699 printk_deprecated_rfkill_attribute("bluetooth_enable");
3701 if (parse_strtoul(buf, 1, &t))
3702 return -EINVAL;
3704 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3706 res = bluetooth_set_radiosw(t, 1);
3708 return (res) ? res : count;
3711 static struct device_attribute dev_attr_bluetooth_enable =
3712 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3713 bluetooth_enable_show, bluetooth_enable_store);
3715 /* --------------------------------------------------------------------- */
3717 static struct attribute *bluetooth_attributes[] = {
3718 &dev_attr_bluetooth_enable.attr,
3719 NULL
3722 static const struct attribute_group bluetooth_attr_group = {
3723 .attrs = bluetooth_attributes,
3726 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3728 int bts = bluetooth_get_radiosw();
3730 if (bts < 0)
3731 return bts;
3733 *state = bts;
3734 return 0;
3737 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3739 dbg_printk(TPACPI_DBG_RFKILL,
3740 "request to change radio state to %d\n", state);
3741 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3744 static void bluetooth_shutdown(void)
3746 /* Order firmware to save current state to NVRAM */
3747 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3748 TP_ACPI_BLTH_SAVE_STATE))
3749 printk(TPACPI_NOTICE
3750 "failed to save bluetooth state to NVRAM\n");
3751 else
3752 vdbg_printk(TPACPI_DBG_RFKILL,
3753 "bluestooth state saved to NVRAM\n");
3756 static void bluetooth_exit(void)
3758 bluetooth_shutdown();
3760 if (tpacpi_bluetooth_rfkill)
3761 rfkill_unregister(tpacpi_bluetooth_rfkill);
3763 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3764 &bluetooth_attr_group);
3767 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3769 int res;
3770 int status = 0;
3772 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3773 "initializing bluetooth subdriver\n");
3775 TPACPI_ACPIHANDLE_INIT(hkey);
3777 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3778 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3779 tp_features.bluetooth = hkey_handle &&
3780 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3782 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3783 "bluetooth is %s, status 0x%02x\n",
3784 str_supported(tp_features.bluetooth),
3785 status);
3787 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3788 if (dbg_bluetoothemul) {
3789 tp_features.bluetooth = 1;
3790 printk(TPACPI_INFO
3791 "bluetooth switch emulation enabled\n");
3792 } else
3793 #endif
3794 if (tp_features.bluetooth &&
3795 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3796 /* no bluetooth hardware present in system */
3797 tp_features.bluetooth = 0;
3798 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3799 "bluetooth hardware not installed\n");
3802 if (!tp_features.bluetooth)
3803 return 1;
3805 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3806 &bluetooth_attr_group);
3807 if (res)
3808 return res;
3810 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3811 &tpacpi_bluetooth_rfkill,
3812 RFKILL_TYPE_BLUETOOTH,
3813 TPACPI_RFK_BLUETOOTH_SW_NAME,
3814 true,
3815 tpacpi_bluetooth_rfk_set,
3816 tpacpi_bluetooth_rfk_get);
3817 if (res) {
3818 bluetooth_exit();
3819 return res;
3822 return 0;
3825 /* procfs -------------------------------------------------------------- */
3826 static int bluetooth_read(char *p)
3828 int len = 0;
3829 int status = bluetooth_get_radiosw();
3831 if (!tp_features.bluetooth)
3832 len += sprintf(p + len, "status:\t\tnot supported\n");
3833 else {
3834 len += sprintf(p + len, "status:\t\t%s\n",
3835 (status == RFKILL_STATE_UNBLOCKED) ?
3836 "enabled" : "disabled");
3837 len += sprintf(p + len, "commands:\tenable, disable\n");
3840 return len;
3843 static int bluetooth_write(char *buf)
3845 char *cmd;
3846 int state = -1;
3848 if (!tp_features.bluetooth)
3849 return -ENODEV;
3851 while ((cmd = next_cmd(&buf))) {
3852 if (strlencmp(cmd, "enable") == 0) {
3853 state = 1;
3854 } else if (strlencmp(cmd, "disable") == 0) {
3855 state = 0;
3856 } else
3857 return -EINVAL;
3860 if (state != -1) {
3861 tpacpi_disclose_usertask("procfs bluetooth",
3862 "attempt to %s\n",
3863 state ? "enable" : "disable");
3864 bluetooth_set_radiosw(state, 1);
3867 return 0;
3870 static struct ibm_struct bluetooth_driver_data = {
3871 .name = "bluetooth",
3872 .read = bluetooth_read,
3873 .write = bluetooth_write,
3874 .exit = bluetooth_exit,
3875 .suspend = bluetooth_suspend,
3876 .shutdown = bluetooth_shutdown,
3879 /*************************************************************************
3880 * Wan subdriver
3883 enum {
3884 /* ACPI GWAN/SWAN bits */
3885 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3886 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3887 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3888 off / last state */
3891 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3893 static struct rfkill *tpacpi_wan_rfkill;
3895 static void wan_suspend(pm_message_t state)
3897 /* Try to make sure radio will resume powered off */
3898 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3899 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3900 vdbg_printk(TPACPI_DBG_RFKILL,
3901 "WWAN power down on resume request failed\n");
3904 static int wan_get_radiosw(void)
3906 int status;
3908 if (!tp_features.wan)
3909 return -ENODEV;
3911 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3912 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3913 return RFKILL_STATE_HARD_BLOCKED;
3915 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3916 if (dbg_wwanemul)
3917 return (tpacpi_wwan_emulstate) ?
3918 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3919 #endif
3921 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3922 return -EIO;
3924 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3925 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3928 static void wan_update_rfk(void)
3930 int status;
3932 if (!tpacpi_wan_rfkill)
3933 return;
3935 status = wan_get_radiosw();
3936 if (status < 0)
3937 return;
3938 rfkill_force_state(tpacpi_wan_rfkill, status);
3940 vdbg_printk(TPACPI_DBG_RFKILL,
3941 "forced rfkill state to %d\n",
3942 status);
3945 static int wan_set_radiosw(int radio_on, int update_rfk)
3947 int status;
3949 if (!tp_features.wan)
3950 return -ENODEV;
3952 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3953 * reason to risk weird behaviour. */
3954 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3955 && radio_on)
3956 return -EPERM;
3958 vdbg_printk(TPACPI_DBG_RFKILL,
3959 "will %s WWAN\n", radio_on ? "enable" : "disable");
3961 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3962 if (dbg_wwanemul) {
3963 tpacpi_wwan_emulstate = !!radio_on;
3964 if (update_rfk)
3965 wan_update_rfk();
3966 return 0;
3968 #endif
3970 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3971 if (radio_on)
3972 status = TP_ACPI_WANCARD_RADIOSSW;
3973 else
3974 status = 0;
3975 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3976 return -EIO;
3978 if (update_rfk)
3979 wan_update_rfk();
3981 return 0;
3984 /* sysfs wan enable ---------------------------------------------------- */
3985 static ssize_t wan_enable_show(struct device *dev,
3986 struct device_attribute *attr,
3987 char *buf)
3989 int status;
3991 printk_deprecated_rfkill_attribute("wwan_enable");
3993 status = wan_get_radiosw();
3994 if (status < 0)
3995 return status;
3997 return snprintf(buf, PAGE_SIZE, "%d\n",
3998 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
4001 static ssize_t wan_enable_store(struct device *dev,
4002 struct device_attribute *attr,
4003 const char *buf, size_t count)
4005 unsigned long t;
4006 int res;
4008 printk_deprecated_rfkill_attribute("wwan_enable");
4010 if (parse_strtoul(buf, 1, &t))
4011 return -EINVAL;
4013 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4015 res = wan_set_radiosw(t, 1);
4017 return (res) ? res : count;
4020 static struct device_attribute dev_attr_wan_enable =
4021 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4022 wan_enable_show, wan_enable_store);
4024 /* --------------------------------------------------------------------- */
4026 static struct attribute *wan_attributes[] = {
4027 &dev_attr_wan_enable.attr,
4028 NULL
4031 static const struct attribute_group wan_attr_group = {
4032 .attrs = wan_attributes,
4035 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4037 int wans = wan_get_radiosw();
4039 if (wans < 0)
4040 return wans;
4042 *state = wans;
4043 return 0;
4046 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4048 dbg_printk(TPACPI_DBG_RFKILL,
4049 "request to change radio state to %d\n", state);
4050 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4053 static void wan_shutdown(void)
4055 /* Order firmware to save current state to NVRAM */
4056 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4057 TP_ACPI_WGSV_SAVE_STATE))
4058 printk(TPACPI_NOTICE
4059 "failed to save WWAN state to NVRAM\n");
4060 else
4061 vdbg_printk(TPACPI_DBG_RFKILL,
4062 "WWAN state saved to NVRAM\n");
4065 static void wan_exit(void)
4067 wan_shutdown();
4069 if (tpacpi_wan_rfkill)
4070 rfkill_unregister(tpacpi_wan_rfkill);
4072 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4073 &wan_attr_group);
4076 static int __init wan_init(struct ibm_init_struct *iibm)
4078 int res;
4079 int status = 0;
4081 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4082 "initializing wan subdriver\n");
4084 TPACPI_ACPIHANDLE_INIT(hkey);
4086 tp_features.wan = hkey_handle &&
4087 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4089 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4090 "wan is %s, status 0x%02x\n",
4091 str_supported(tp_features.wan),
4092 status);
4094 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4095 if (dbg_wwanemul) {
4096 tp_features.wan = 1;
4097 printk(TPACPI_INFO
4098 "wwan switch emulation enabled\n");
4099 } else
4100 #endif
4101 if (tp_features.wan &&
4102 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4103 /* no wan hardware present in system */
4104 tp_features.wan = 0;
4105 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4106 "wan hardware not installed\n");
4109 if (!tp_features.wan)
4110 return 1;
4112 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4113 &wan_attr_group);
4114 if (res)
4115 return res;
4117 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4118 &tpacpi_wan_rfkill,
4119 RFKILL_TYPE_WWAN,
4120 TPACPI_RFK_WWAN_SW_NAME,
4121 true,
4122 tpacpi_wan_rfk_set,
4123 tpacpi_wan_rfk_get);
4124 if (res) {
4125 wan_exit();
4126 return res;
4129 return 0;
4132 /* procfs -------------------------------------------------------------- */
4133 static int wan_read(char *p)
4135 int len = 0;
4136 int status = wan_get_radiosw();
4138 tpacpi_disclose_usertask("procfs wan", "read");
4140 if (!tp_features.wan)
4141 len += sprintf(p + len, "status:\t\tnot supported\n");
4142 else {
4143 len += sprintf(p + len, "status:\t\t%s\n",
4144 (status == RFKILL_STATE_UNBLOCKED) ?
4145 "enabled" : "disabled");
4146 len += sprintf(p + len, "commands:\tenable, disable\n");
4149 return len;
4152 static int wan_write(char *buf)
4154 char *cmd;
4155 int state = -1;
4157 if (!tp_features.wan)
4158 return -ENODEV;
4160 while ((cmd = next_cmd(&buf))) {
4161 if (strlencmp(cmd, "enable") == 0) {
4162 state = 1;
4163 } else if (strlencmp(cmd, "disable") == 0) {
4164 state = 0;
4165 } else
4166 return -EINVAL;
4169 if (state != -1) {
4170 tpacpi_disclose_usertask("procfs wan",
4171 "attempt to %s\n",
4172 state ? "enable" : "disable");
4173 wan_set_radiosw(state, 1);
4176 return 0;
4179 static struct ibm_struct wan_driver_data = {
4180 .name = "wan",
4181 .read = wan_read,
4182 .write = wan_write,
4183 .exit = wan_exit,
4184 .suspend = wan_suspend,
4185 .shutdown = wan_shutdown,
4188 /*************************************************************************
4189 * UWB subdriver
4192 enum {
4193 /* ACPI GUWB/SUWB bits */
4194 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4195 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4198 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4200 static struct rfkill *tpacpi_uwb_rfkill;
4202 static int uwb_get_radiosw(void)
4204 int status;
4206 if (!tp_features.uwb)
4207 return -ENODEV;
4209 /* WLSW overrides UWB in firmware/hardware, reflect that */
4210 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4211 return RFKILL_STATE_HARD_BLOCKED;
4213 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4214 if (dbg_uwbemul)
4215 return (tpacpi_uwb_emulstate) ?
4216 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4217 #endif
4219 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4220 return -EIO;
4222 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4223 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4226 static void uwb_update_rfk(void)
4228 int status;
4230 if (!tpacpi_uwb_rfkill)
4231 return;
4233 status = uwb_get_radiosw();
4234 if (status < 0)
4235 return;
4236 rfkill_force_state(tpacpi_uwb_rfkill, status);
4238 vdbg_printk(TPACPI_DBG_RFKILL,
4239 "forced rfkill state to %d\n",
4240 status);
4243 static int uwb_set_radiosw(int radio_on, int update_rfk)
4245 int status;
4247 if (!tp_features.uwb)
4248 return -ENODEV;
4250 /* WLSW overrides UWB in firmware/hardware, but there is no
4251 * reason to risk weird behaviour. */
4252 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4253 && radio_on)
4254 return -EPERM;
4256 vdbg_printk(TPACPI_DBG_RFKILL,
4257 "will %s UWB\n", radio_on ? "enable" : "disable");
4259 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4260 if (dbg_uwbemul) {
4261 tpacpi_uwb_emulstate = !!radio_on;
4262 if (update_rfk)
4263 uwb_update_rfk();
4264 return 0;
4266 #endif
4268 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4269 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4270 return -EIO;
4272 if (update_rfk)
4273 uwb_update_rfk();
4275 return 0;
4278 /* --------------------------------------------------------------------- */
4280 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4282 int uwbs = uwb_get_radiosw();
4284 if (uwbs < 0)
4285 return uwbs;
4287 *state = uwbs;
4288 return 0;
4291 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4293 dbg_printk(TPACPI_DBG_RFKILL,
4294 "request to change radio state to %d\n", state);
4295 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4298 static void uwb_exit(void)
4300 if (tpacpi_uwb_rfkill)
4301 rfkill_unregister(tpacpi_uwb_rfkill);
4304 static int __init uwb_init(struct ibm_init_struct *iibm)
4306 int res;
4307 int status = 0;
4309 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4310 "initializing uwb subdriver\n");
4312 TPACPI_ACPIHANDLE_INIT(hkey);
4314 tp_features.uwb = hkey_handle &&
4315 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4317 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4318 "uwb is %s, status 0x%02x\n",
4319 str_supported(tp_features.uwb),
4320 status);
4322 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4323 if (dbg_uwbemul) {
4324 tp_features.uwb = 1;
4325 printk(TPACPI_INFO
4326 "uwb switch emulation enabled\n");
4327 } else
4328 #endif
4329 if (tp_features.uwb &&
4330 !(status & TP_ACPI_UWB_HWPRESENT)) {
4331 /* no uwb hardware present in system */
4332 tp_features.uwb = 0;
4333 dbg_printk(TPACPI_DBG_INIT,
4334 "uwb hardware not installed\n");
4337 if (!tp_features.uwb)
4338 return 1;
4340 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4341 &tpacpi_uwb_rfkill,
4342 RFKILL_TYPE_UWB,
4343 TPACPI_RFK_UWB_SW_NAME,
4344 false,
4345 tpacpi_uwb_rfk_set,
4346 tpacpi_uwb_rfk_get);
4348 return res;
4351 static struct ibm_struct uwb_driver_data = {
4352 .name = "uwb",
4353 .exit = uwb_exit,
4354 .flags.experimental = 1,
4357 /*************************************************************************
4358 * Video subdriver
4361 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4363 enum video_access_mode {
4364 TPACPI_VIDEO_NONE = 0,
4365 TPACPI_VIDEO_570, /* 570 */
4366 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4367 TPACPI_VIDEO_NEW, /* all others */
4370 enum { /* video status flags, based on VIDEO_570 */
4371 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4372 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4373 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4376 enum { /* TPACPI_VIDEO_570 constants */
4377 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4378 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4379 * video_status_flags */
4380 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4381 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4384 static enum video_access_mode video_supported;
4385 static int video_orig_autosw;
4387 static int video_autosw_get(void);
4388 static int video_autosw_set(int enable);
4390 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4392 static int __init video_init(struct ibm_init_struct *iibm)
4394 int ivga;
4396 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4398 TPACPI_ACPIHANDLE_INIT(vid);
4399 TPACPI_ACPIHANDLE_INIT(vid2);
4401 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4402 /* G41, assume IVGA doesn't change */
4403 vid_handle = vid2_handle;
4405 if (!vid_handle)
4406 /* video switching not supported on R30, R31 */
4407 video_supported = TPACPI_VIDEO_NONE;
4408 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4409 /* 570 */
4410 video_supported = TPACPI_VIDEO_570;
4411 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4412 /* 600e/x, 770e, 770x */
4413 video_supported = TPACPI_VIDEO_770;
4414 else
4415 /* all others */
4416 video_supported = TPACPI_VIDEO_NEW;
4418 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4419 str_supported(video_supported != TPACPI_VIDEO_NONE),
4420 video_supported);
4422 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4425 static void video_exit(void)
4427 dbg_printk(TPACPI_DBG_EXIT,
4428 "restoring original video autoswitch mode\n");
4429 if (video_autosw_set(video_orig_autosw))
4430 printk(TPACPI_ERR "error while trying to restore original "
4431 "video autoswitch mode\n");
4434 static int video_outputsw_get(void)
4436 int status = 0;
4437 int i;
4439 switch (video_supported) {
4440 case TPACPI_VIDEO_570:
4441 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4442 TP_ACPI_VIDEO_570_PHSCMD))
4443 return -EIO;
4444 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4445 break;
4446 case TPACPI_VIDEO_770:
4447 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4448 return -EIO;
4449 if (i)
4450 status |= TP_ACPI_VIDEO_S_LCD;
4451 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4452 return -EIO;
4453 if (i)
4454 status |= TP_ACPI_VIDEO_S_CRT;
4455 break;
4456 case TPACPI_VIDEO_NEW:
4457 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4458 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4459 return -EIO;
4460 if (i)
4461 status |= TP_ACPI_VIDEO_S_CRT;
4463 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4464 !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, "\\VCDD", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_DVI;
4472 break;
4473 default:
4474 return -ENOSYS;
4477 return status;
4480 static int video_outputsw_set(int status)
4482 int autosw;
4483 int res = 0;
4485 switch (video_supported) {
4486 case TPACPI_VIDEO_570:
4487 res = acpi_evalf(NULL, NULL,
4488 "\\_SB.PHS2", "vdd",
4489 TP_ACPI_VIDEO_570_PHS2CMD,
4490 status | TP_ACPI_VIDEO_570_PHS2SET);
4491 break;
4492 case TPACPI_VIDEO_770:
4493 autosw = video_autosw_get();
4494 if (autosw < 0)
4495 return autosw;
4497 res = video_autosw_set(1);
4498 if (res)
4499 return res;
4500 res = acpi_evalf(vid_handle, NULL,
4501 "ASWT", "vdd", status * 0x100, 0);
4502 if (!autosw && video_autosw_set(autosw)) {
4503 printk(TPACPI_ERR
4504 "video auto-switch left enabled due to error\n");
4505 return -EIO;
4507 break;
4508 case TPACPI_VIDEO_NEW:
4509 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4510 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4511 break;
4512 default:
4513 return -ENOSYS;
4516 return (res)? 0 : -EIO;
4519 static int video_autosw_get(void)
4521 int autosw = 0;
4523 switch (video_supported) {
4524 case TPACPI_VIDEO_570:
4525 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4526 return -EIO;
4527 break;
4528 case TPACPI_VIDEO_770:
4529 case TPACPI_VIDEO_NEW:
4530 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4531 return -EIO;
4532 break;
4533 default:
4534 return -ENOSYS;
4537 return autosw & 1;
4540 static int video_autosw_set(int enable)
4542 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4543 return -EIO;
4544 return 0;
4547 static int video_outputsw_cycle(void)
4549 int autosw = video_autosw_get();
4550 int res;
4552 if (autosw < 0)
4553 return autosw;
4555 switch (video_supported) {
4556 case TPACPI_VIDEO_570:
4557 res = video_autosw_set(1);
4558 if (res)
4559 return res;
4560 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4561 break;
4562 case TPACPI_VIDEO_770:
4563 case TPACPI_VIDEO_NEW:
4564 res = video_autosw_set(1);
4565 if (res)
4566 return res;
4567 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4568 break;
4569 default:
4570 return -ENOSYS;
4572 if (!autosw && video_autosw_set(autosw)) {
4573 printk(TPACPI_ERR
4574 "video auto-switch left enabled due to error\n");
4575 return -EIO;
4578 return (res)? 0 : -EIO;
4581 static int video_expand_toggle(void)
4583 switch (video_supported) {
4584 case TPACPI_VIDEO_570:
4585 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4586 0 : -EIO;
4587 case TPACPI_VIDEO_770:
4588 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4589 0 : -EIO;
4590 case TPACPI_VIDEO_NEW:
4591 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4592 0 : -EIO;
4593 default:
4594 return -ENOSYS;
4596 /* not reached */
4599 static int video_read(char *p)
4601 int status, autosw;
4602 int len = 0;
4604 if (video_supported == TPACPI_VIDEO_NONE) {
4605 len += sprintf(p + len, "status:\t\tnot supported\n");
4606 return len;
4609 status = video_outputsw_get();
4610 if (status < 0)
4611 return status;
4613 autosw = video_autosw_get();
4614 if (autosw < 0)
4615 return autosw;
4617 len += sprintf(p + len, "status:\t\tsupported\n");
4618 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4619 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4620 if (video_supported == TPACPI_VIDEO_NEW)
4621 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4622 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4623 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4624 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4625 if (video_supported == TPACPI_VIDEO_NEW)
4626 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4627 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4628 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4630 return len;
4633 static int video_write(char *buf)
4635 char *cmd;
4636 int enable, disable, status;
4637 int res;
4639 if (video_supported == TPACPI_VIDEO_NONE)
4640 return -ENODEV;
4642 enable = 0;
4643 disable = 0;
4645 while ((cmd = next_cmd(&buf))) {
4646 if (strlencmp(cmd, "lcd_enable") == 0) {
4647 enable |= TP_ACPI_VIDEO_S_LCD;
4648 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4649 disable |= TP_ACPI_VIDEO_S_LCD;
4650 } else if (strlencmp(cmd, "crt_enable") == 0) {
4651 enable |= TP_ACPI_VIDEO_S_CRT;
4652 } else if (strlencmp(cmd, "crt_disable") == 0) {
4653 disable |= TP_ACPI_VIDEO_S_CRT;
4654 } else if (video_supported == TPACPI_VIDEO_NEW &&
4655 strlencmp(cmd, "dvi_enable") == 0) {
4656 enable |= TP_ACPI_VIDEO_S_DVI;
4657 } else if (video_supported == TPACPI_VIDEO_NEW &&
4658 strlencmp(cmd, "dvi_disable") == 0) {
4659 disable |= TP_ACPI_VIDEO_S_DVI;
4660 } else if (strlencmp(cmd, "auto_enable") == 0) {
4661 res = video_autosw_set(1);
4662 if (res)
4663 return res;
4664 } else if (strlencmp(cmd, "auto_disable") == 0) {
4665 res = video_autosw_set(0);
4666 if (res)
4667 return res;
4668 } else if (strlencmp(cmd, "video_switch") == 0) {
4669 res = video_outputsw_cycle();
4670 if (res)
4671 return res;
4672 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4673 res = video_expand_toggle();
4674 if (res)
4675 return res;
4676 } else
4677 return -EINVAL;
4680 if (enable || disable) {
4681 status = video_outputsw_get();
4682 if (status < 0)
4683 return status;
4684 res = video_outputsw_set((status & ~disable) | enable);
4685 if (res)
4686 return res;
4689 return 0;
4692 static struct ibm_struct video_driver_data = {
4693 .name = "video",
4694 .read = video_read,
4695 .write = video_write,
4696 .exit = video_exit,
4699 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4701 /*************************************************************************
4702 * Light (thinklight) subdriver
4705 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4706 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4708 static int light_get_status(void)
4710 int status = 0;
4712 if (tp_features.light_status) {
4713 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4714 return -EIO;
4715 return (!!status);
4718 return -ENXIO;
4721 static int light_set_status(int status)
4723 int rc;
4725 if (tp_features.light) {
4726 if (cmos_handle) {
4727 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4728 (status)?
4729 TP_CMOS_THINKLIGHT_ON :
4730 TP_CMOS_THINKLIGHT_OFF);
4731 } else {
4732 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4733 (status)? 1 : 0);
4735 return (rc)? 0 : -EIO;
4738 return -ENXIO;
4741 static void light_set_status_worker(struct work_struct *work)
4743 struct tpacpi_led_classdev *data =
4744 container_of(work, struct tpacpi_led_classdev, work);
4746 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4747 light_set_status((data->new_state != TPACPI_LED_OFF));
4750 static void light_sysfs_set(struct led_classdev *led_cdev,
4751 enum led_brightness brightness)
4753 struct tpacpi_led_classdev *data =
4754 container_of(led_cdev,
4755 struct tpacpi_led_classdev,
4756 led_classdev);
4757 data->new_state = (brightness != LED_OFF) ?
4758 TPACPI_LED_ON : TPACPI_LED_OFF;
4759 queue_work(tpacpi_wq, &data->work);
4762 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4764 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4767 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4768 .led_classdev = {
4769 .name = "tpacpi::thinklight",
4770 .brightness_set = &light_sysfs_set,
4771 .brightness_get = &light_sysfs_get,
4775 static int __init light_init(struct ibm_init_struct *iibm)
4777 int rc;
4779 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4781 TPACPI_ACPIHANDLE_INIT(ledb);
4782 TPACPI_ACPIHANDLE_INIT(lght);
4783 TPACPI_ACPIHANDLE_INIT(cmos);
4784 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4786 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4787 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4789 if (tp_features.light)
4790 /* light status not supported on
4791 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4792 tp_features.light_status =
4793 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4795 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4796 str_supported(tp_features.light),
4797 str_supported(tp_features.light_status));
4799 if (!tp_features.light)
4800 return 1;
4802 rc = led_classdev_register(&tpacpi_pdev->dev,
4803 &tpacpi_led_thinklight.led_classdev);
4805 if (rc < 0) {
4806 tp_features.light = 0;
4807 tp_features.light_status = 0;
4808 } else {
4809 rc = 0;
4812 return rc;
4815 static void light_exit(void)
4817 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4818 if (work_pending(&tpacpi_led_thinklight.work))
4819 flush_workqueue(tpacpi_wq);
4822 static int light_read(char *p)
4824 int len = 0;
4825 int status;
4827 if (!tp_features.light) {
4828 len += sprintf(p + len, "status:\t\tnot supported\n");
4829 } else if (!tp_features.light_status) {
4830 len += sprintf(p + len, "status:\t\tunknown\n");
4831 len += sprintf(p + len, "commands:\ton, off\n");
4832 } else {
4833 status = light_get_status();
4834 if (status < 0)
4835 return status;
4836 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4837 len += sprintf(p + len, "commands:\ton, off\n");
4840 return len;
4843 static int light_write(char *buf)
4845 char *cmd;
4846 int newstatus = 0;
4848 if (!tp_features.light)
4849 return -ENODEV;
4851 while ((cmd = next_cmd(&buf))) {
4852 if (strlencmp(cmd, "on") == 0) {
4853 newstatus = 1;
4854 } else if (strlencmp(cmd, "off") == 0) {
4855 newstatus = 0;
4856 } else
4857 return -EINVAL;
4860 return light_set_status(newstatus);
4863 static struct ibm_struct light_driver_data = {
4864 .name = "light",
4865 .read = light_read,
4866 .write = light_write,
4867 .exit = light_exit,
4870 /*************************************************************************
4871 * CMOS subdriver
4874 /* sysfs cmos_command -------------------------------------------------- */
4875 static ssize_t cmos_command_store(struct device *dev,
4876 struct device_attribute *attr,
4877 const char *buf, size_t count)
4879 unsigned long cmos_cmd;
4880 int res;
4882 if (parse_strtoul(buf, 21, &cmos_cmd))
4883 return -EINVAL;
4885 res = issue_thinkpad_cmos_command(cmos_cmd);
4886 return (res)? res : count;
4889 static struct device_attribute dev_attr_cmos_command =
4890 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4892 /* --------------------------------------------------------------------- */
4894 static int __init cmos_init(struct ibm_init_struct *iibm)
4896 int res;
4898 vdbg_printk(TPACPI_DBG_INIT,
4899 "initializing cmos commands subdriver\n");
4901 TPACPI_ACPIHANDLE_INIT(cmos);
4903 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4904 str_supported(cmos_handle != NULL));
4906 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4907 if (res)
4908 return res;
4910 return (cmos_handle)? 0 : 1;
4913 static void cmos_exit(void)
4915 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4918 static int cmos_read(char *p)
4920 int len = 0;
4922 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4923 R30, R31, T20-22, X20-21 */
4924 if (!cmos_handle)
4925 len += sprintf(p + len, "status:\t\tnot supported\n");
4926 else {
4927 len += sprintf(p + len, "status:\t\tsupported\n");
4928 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4931 return len;
4934 static int cmos_write(char *buf)
4936 char *cmd;
4937 int cmos_cmd, res;
4939 while ((cmd = next_cmd(&buf))) {
4940 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4941 cmos_cmd >= 0 && cmos_cmd <= 21) {
4942 /* cmos_cmd set */
4943 } else
4944 return -EINVAL;
4946 res = issue_thinkpad_cmos_command(cmos_cmd);
4947 if (res)
4948 return res;
4951 return 0;
4954 static struct ibm_struct cmos_driver_data = {
4955 .name = "cmos",
4956 .read = cmos_read,
4957 .write = cmos_write,
4958 .exit = cmos_exit,
4961 /*************************************************************************
4962 * LED subdriver
4965 enum led_access_mode {
4966 TPACPI_LED_NONE = 0,
4967 TPACPI_LED_570, /* 570 */
4968 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4969 TPACPI_LED_NEW, /* all others */
4972 enum { /* For TPACPI_LED_OLD */
4973 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4974 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4975 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4978 static enum led_access_mode led_supported;
4980 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4981 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4982 /* T20-22, X20-21 */
4983 "LED", /* all others */
4984 ); /* R30, R31 */
4986 #define TPACPI_LED_NUMLEDS 16
4987 static struct tpacpi_led_classdev *tpacpi_leds;
4988 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4989 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4990 /* there's a limit of 19 chars + NULL before 2.6.26 */
4991 "tpacpi::power",
4992 "tpacpi:orange:batt",
4993 "tpacpi:green:batt",
4994 "tpacpi::dock_active",
4995 "tpacpi::bay_active",
4996 "tpacpi::dock_batt",
4997 "tpacpi::unknown_led",
4998 "tpacpi::standby",
4999 "tpacpi::dock_status1",
5000 "tpacpi::dock_status2",
5001 "tpacpi::unknown_led2",
5002 "tpacpi::unknown_led3",
5003 "tpacpi::thinkvantage",
5005 #define TPACPI_SAFE_LEDS 0x1081U
5007 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5009 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5010 return false;
5011 #else
5012 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5013 #endif
5016 static int led_get_status(const unsigned int led)
5018 int status;
5019 enum led_status_t led_s;
5021 switch (led_supported) {
5022 case TPACPI_LED_570:
5023 if (!acpi_evalf(ec_handle,
5024 &status, "GLED", "dd", 1 << led))
5025 return -EIO;
5026 led_s = (status == 0)?
5027 TPACPI_LED_OFF :
5028 ((status == 1)?
5029 TPACPI_LED_ON :
5030 TPACPI_LED_BLINK);
5031 tpacpi_led_state_cache[led] = led_s;
5032 return led_s;
5033 default:
5034 return -ENXIO;
5037 /* not reached */
5040 static int led_set_status(const unsigned int led,
5041 const enum led_status_t ledstatus)
5043 /* off, on, blink. Index is led_status_t */
5044 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5045 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5047 int rc = 0;
5049 switch (led_supported) {
5050 case TPACPI_LED_570:
5051 /* 570 */
5052 if (unlikely(led > 7))
5053 return -EINVAL;
5054 if (unlikely(tpacpi_is_led_restricted(led)))
5055 return -EPERM;
5056 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5057 (1 << led), led_sled_arg1[ledstatus]))
5058 rc = -EIO;
5059 break;
5060 case TPACPI_LED_OLD:
5061 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5062 if (unlikely(led > 7))
5063 return -EINVAL;
5064 if (unlikely(tpacpi_is_led_restricted(led)))
5065 return -EPERM;
5066 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5067 if (rc >= 0)
5068 rc = ec_write(TPACPI_LED_EC_HLBL,
5069 (ledstatus == TPACPI_LED_BLINK) << led);
5070 if (rc >= 0)
5071 rc = ec_write(TPACPI_LED_EC_HLCL,
5072 (ledstatus != TPACPI_LED_OFF) << led);
5073 break;
5074 case TPACPI_LED_NEW:
5075 /* all others */
5076 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5077 return -EINVAL;
5078 if (unlikely(tpacpi_is_led_restricted(led)))
5079 return -EPERM;
5080 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5081 led, led_led_arg1[ledstatus]))
5082 rc = -EIO;
5083 break;
5084 default:
5085 rc = -ENXIO;
5088 if (!rc)
5089 tpacpi_led_state_cache[led] = ledstatus;
5091 return rc;
5094 static void led_set_status_worker(struct work_struct *work)
5096 struct tpacpi_led_classdev *data =
5097 container_of(work, struct tpacpi_led_classdev, work);
5099 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5100 led_set_status(data->led, data->new_state);
5103 static void led_sysfs_set(struct led_classdev *led_cdev,
5104 enum led_brightness brightness)
5106 struct tpacpi_led_classdev *data = container_of(led_cdev,
5107 struct tpacpi_led_classdev, led_classdev);
5109 if (brightness == LED_OFF)
5110 data->new_state = TPACPI_LED_OFF;
5111 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5112 data->new_state = TPACPI_LED_ON;
5113 else
5114 data->new_state = TPACPI_LED_BLINK;
5116 queue_work(tpacpi_wq, &data->work);
5119 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5120 unsigned long *delay_on, unsigned long *delay_off)
5122 struct tpacpi_led_classdev *data = container_of(led_cdev,
5123 struct tpacpi_led_classdev, led_classdev);
5125 /* Can we choose the flash rate? */
5126 if (*delay_on == 0 && *delay_off == 0) {
5127 /* yes. set them to the hardware blink rate (1 Hz) */
5128 *delay_on = 500; /* ms */
5129 *delay_off = 500; /* ms */
5130 } else if ((*delay_on != 500) || (*delay_off != 500))
5131 return -EINVAL;
5133 data->new_state = TPACPI_LED_BLINK;
5134 queue_work(tpacpi_wq, &data->work);
5136 return 0;
5139 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5141 int rc;
5143 struct tpacpi_led_classdev *data = container_of(led_cdev,
5144 struct tpacpi_led_classdev, led_classdev);
5146 rc = led_get_status(data->led);
5148 if (rc == TPACPI_LED_OFF || rc < 0)
5149 rc = LED_OFF; /* no error handling in led class :( */
5150 else
5151 rc = LED_FULL;
5153 return rc;
5156 static void led_exit(void)
5158 unsigned int i;
5160 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5161 if (tpacpi_leds[i].led_classdev.name)
5162 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5165 kfree(tpacpi_leds);
5168 static int __init tpacpi_init_led(unsigned int led)
5170 int rc;
5172 tpacpi_leds[led].led = led;
5174 /* LEDs with no name don't get registered */
5175 if (!tpacpi_led_names[led])
5176 return 0;
5178 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5179 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5180 if (led_supported == TPACPI_LED_570)
5181 tpacpi_leds[led].led_classdev.brightness_get =
5182 &led_sysfs_get;
5184 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5186 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5188 rc = led_classdev_register(&tpacpi_pdev->dev,
5189 &tpacpi_leds[led].led_classdev);
5190 if (rc < 0)
5191 tpacpi_leds[led].led_classdev.name = NULL;
5193 return rc;
5196 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5197 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5198 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5199 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5201 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5202 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5203 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5204 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5205 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5206 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5207 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5208 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5210 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5211 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5212 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5213 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5214 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5216 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5217 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5218 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5219 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5221 /* (1) - may have excess leds enabled on MSB */
5223 /* Defaults (order matters, keep last, don't reorder!) */
5224 { /* Lenovo */
5225 .vendor = PCI_VENDOR_ID_LENOVO,
5226 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5227 .quirks = 0x1fffU,
5229 { /* IBM ThinkPads with no EC version string */
5230 .vendor = PCI_VENDOR_ID_IBM,
5231 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5232 .quirks = 0x00ffU,
5234 { /* IBM ThinkPads with EC version string */
5235 .vendor = PCI_VENDOR_ID_IBM,
5236 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5237 .quirks = 0x00bfU,
5241 #undef TPACPI_LEDQ_IBM
5242 #undef TPACPI_LEDQ_LNV
5244 static int __init led_init(struct ibm_init_struct *iibm)
5246 unsigned int i;
5247 int rc;
5248 unsigned long useful_leds;
5250 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5252 TPACPI_ACPIHANDLE_INIT(led);
5254 if (!led_handle)
5255 /* led not supported on R30, R31 */
5256 led_supported = TPACPI_LED_NONE;
5257 else if (strlencmp(led_path, "SLED") == 0)
5258 /* 570 */
5259 led_supported = TPACPI_LED_570;
5260 else if (strlencmp(led_path, "SYSL") == 0)
5261 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5262 led_supported = TPACPI_LED_OLD;
5263 else
5264 /* all others */
5265 led_supported = TPACPI_LED_NEW;
5267 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5268 str_supported(led_supported), led_supported);
5270 if (led_supported == TPACPI_LED_NONE)
5271 return 1;
5273 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5274 GFP_KERNEL);
5275 if (!tpacpi_leds) {
5276 printk(TPACPI_ERR "Out of memory for LED data\n");
5277 return -ENOMEM;
5280 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5281 ARRAY_SIZE(led_useful_qtable));
5283 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5284 if (!tpacpi_is_led_restricted(i) &&
5285 test_bit(i, &useful_leds)) {
5286 rc = tpacpi_init_led(i);
5287 if (rc < 0) {
5288 led_exit();
5289 return rc;
5294 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5295 printk(TPACPI_NOTICE
5296 "warning: userspace override of important "
5297 "firmware LEDs is enabled\n");
5298 #endif
5299 return 0;
5302 #define str_led_status(s) \
5303 ((s) == TPACPI_LED_OFF ? "off" : \
5304 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5306 static int led_read(char *p)
5308 int len = 0;
5310 if (!led_supported) {
5311 len += sprintf(p + len, "status:\t\tnot supported\n");
5312 return len;
5314 len += sprintf(p + len, "status:\t\tsupported\n");
5316 if (led_supported == TPACPI_LED_570) {
5317 /* 570 */
5318 int i, status;
5319 for (i = 0; i < 8; i++) {
5320 status = led_get_status(i);
5321 if (status < 0)
5322 return -EIO;
5323 len += sprintf(p + len, "%d:\t\t%s\n",
5324 i, str_led_status(status));
5328 len += sprintf(p + len, "commands:\t"
5329 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5331 return len;
5334 static int led_write(char *buf)
5336 char *cmd;
5337 int led, rc;
5338 enum led_status_t s;
5340 if (!led_supported)
5341 return -ENODEV;
5343 while ((cmd = next_cmd(&buf))) {
5344 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5345 return -EINVAL;
5347 if (strstr(cmd, "off")) {
5348 s = TPACPI_LED_OFF;
5349 } else if (strstr(cmd, "on")) {
5350 s = TPACPI_LED_ON;
5351 } else if (strstr(cmd, "blink")) {
5352 s = TPACPI_LED_BLINK;
5353 } else {
5354 return -EINVAL;
5357 rc = led_set_status(led, s);
5358 if (rc < 0)
5359 return rc;
5362 return 0;
5365 static struct ibm_struct led_driver_data = {
5366 .name = "led",
5367 .read = led_read,
5368 .write = led_write,
5369 .exit = led_exit,
5372 /*************************************************************************
5373 * Beep subdriver
5376 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5378 #define TPACPI_BEEP_Q1 0x0001
5380 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5381 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5382 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5385 static int __init beep_init(struct ibm_init_struct *iibm)
5387 unsigned long quirks;
5389 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5391 TPACPI_ACPIHANDLE_INIT(beep);
5393 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5394 str_supported(beep_handle != NULL));
5396 quirks = tpacpi_check_quirks(beep_quirk_table,
5397 ARRAY_SIZE(beep_quirk_table));
5399 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5401 return (beep_handle)? 0 : 1;
5404 static int beep_read(char *p)
5406 int len = 0;
5408 if (!beep_handle)
5409 len += sprintf(p + len, "status:\t\tnot supported\n");
5410 else {
5411 len += sprintf(p + len, "status:\t\tsupported\n");
5412 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5415 return len;
5418 static int beep_write(char *buf)
5420 char *cmd;
5421 int beep_cmd;
5423 if (!beep_handle)
5424 return -ENODEV;
5426 while ((cmd = next_cmd(&buf))) {
5427 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5428 beep_cmd >= 0 && beep_cmd <= 17) {
5429 /* beep_cmd set */
5430 } else
5431 return -EINVAL;
5432 if (tp_features.beep_needs_two_args) {
5433 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5434 beep_cmd, 0))
5435 return -EIO;
5436 } else {
5437 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5438 beep_cmd))
5439 return -EIO;
5443 return 0;
5446 static struct ibm_struct beep_driver_data = {
5447 .name = "beep",
5448 .read = beep_read,
5449 .write = beep_write,
5452 /*************************************************************************
5453 * Thermal subdriver
5456 enum thermal_access_mode {
5457 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5458 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5459 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5460 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5461 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5464 enum { /* TPACPI_THERMAL_TPEC_* */
5465 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5466 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5467 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5469 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5473 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5474 struct ibm_thermal_sensors_struct {
5475 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5478 static enum thermal_access_mode thermal_read_mode;
5480 /* idx is zero-based */
5481 static int thermal_get_sensor(int idx, s32 *value)
5483 int t;
5484 s8 tmp;
5485 char tmpi[5];
5487 t = TP_EC_THERMAL_TMP0;
5489 switch (thermal_read_mode) {
5490 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5491 case TPACPI_THERMAL_TPEC_16:
5492 if (idx >= 8 && idx <= 15) {
5493 t = TP_EC_THERMAL_TMP8;
5494 idx -= 8;
5496 /* fallthrough */
5497 #endif
5498 case TPACPI_THERMAL_TPEC_8:
5499 if (idx <= 7) {
5500 if (!acpi_ec_read(t + idx, &tmp))
5501 return -EIO;
5502 *value = tmp * 1000;
5503 return 0;
5505 break;
5507 case TPACPI_THERMAL_ACPI_UPDT:
5508 if (idx <= 7) {
5509 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5510 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5511 return -EIO;
5512 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5513 return -EIO;
5514 *value = (t - 2732) * 100;
5515 return 0;
5517 break;
5519 case TPACPI_THERMAL_ACPI_TMP07:
5520 if (idx <= 7) {
5521 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5522 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5523 return -EIO;
5524 if (t > 127 || t < -127)
5525 t = TP_EC_THERMAL_TMP_NA;
5526 *value = t * 1000;
5527 return 0;
5529 break;
5531 case TPACPI_THERMAL_NONE:
5532 default:
5533 return -ENOSYS;
5536 return -EINVAL;
5539 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5541 int res, i;
5542 int n;
5544 n = 8;
5545 i = 0;
5547 if (!s)
5548 return -EINVAL;
5550 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5551 n = 16;
5553 for (i = 0 ; i < n; i++) {
5554 res = thermal_get_sensor(i, &s->temp[i]);
5555 if (res)
5556 return res;
5559 return n;
5562 static void thermal_dump_all_sensors(void)
5564 int n, i;
5565 struct ibm_thermal_sensors_struct t;
5567 n = thermal_get_sensors(&t);
5568 if (n <= 0)
5569 return;
5571 printk(TPACPI_NOTICE
5572 "temperatures (Celsius):");
5574 for (i = 0; i < n; i++) {
5575 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5576 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5577 else
5578 printk(KERN_CONT " N/A");
5581 printk(KERN_CONT "\n");
5584 /* sysfs temp##_input -------------------------------------------------- */
5586 static ssize_t thermal_temp_input_show(struct device *dev,
5587 struct device_attribute *attr,
5588 char *buf)
5590 struct sensor_device_attribute *sensor_attr =
5591 to_sensor_dev_attr(attr);
5592 int idx = sensor_attr->index;
5593 s32 value;
5594 int res;
5596 res = thermal_get_sensor(idx, &value);
5597 if (res)
5598 return res;
5599 if (value == TPACPI_THERMAL_SENSOR_NA)
5600 return -ENXIO;
5602 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5605 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5606 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5607 thermal_temp_input_show, NULL, _idxB)
5609 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5610 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5611 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5612 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5613 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5614 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5615 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5616 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5617 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5618 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5619 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5620 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5621 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5622 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5623 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5624 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5625 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5628 #define THERMAL_ATTRS(X) \
5629 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5631 static struct attribute *thermal_temp_input_attr[] = {
5632 THERMAL_ATTRS(8),
5633 THERMAL_ATTRS(9),
5634 THERMAL_ATTRS(10),
5635 THERMAL_ATTRS(11),
5636 THERMAL_ATTRS(12),
5637 THERMAL_ATTRS(13),
5638 THERMAL_ATTRS(14),
5639 THERMAL_ATTRS(15),
5640 THERMAL_ATTRS(0),
5641 THERMAL_ATTRS(1),
5642 THERMAL_ATTRS(2),
5643 THERMAL_ATTRS(3),
5644 THERMAL_ATTRS(4),
5645 THERMAL_ATTRS(5),
5646 THERMAL_ATTRS(6),
5647 THERMAL_ATTRS(7),
5648 NULL
5651 static const struct attribute_group thermal_temp_input16_group = {
5652 .attrs = thermal_temp_input_attr
5655 static const struct attribute_group thermal_temp_input8_group = {
5656 .attrs = &thermal_temp_input_attr[8]
5659 #undef THERMAL_SENSOR_ATTR_TEMP
5660 #undef THERMAL_ATTRS
5662 /* --------------------------------------------------------------------- */
5664 static int __init thermal_init(struct ibm_init_struct *iibm)
5666 u8 t, ta1, ta2;
5667 int i;
5668 int acpi_tmp7;
5669 int res;
5671 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5673 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5675 if (thinkpad_id.ec_model) {
5677 * Direct EC access mode: sensors at registers
5678 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5679 * non-implemented, thermal sensors return 0x80 when
5680 * not available
5683 ta1 = ta2 = 0;
5684 for (i = 0; i < 8; i++) {
5685 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5686 ta1 |= t;
5687 } else {
5688 ta1 = 0;
5689 break;
5691 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5692 ta2 |= t;
5693 } else {
5694 ta1 = 0;
5695 break;
5698 if (ta1 == 0) {
5699 /* This is sheer paranoia, but we handle it anyway */
5700 if (acpi_tmp7) {
5701 printk(TPACPI_ERR
5702 "ThinkPad ACPI EC access misbehaving, "
5703 "falling back to ACPI TMPx access "
5704 "mode\n");
5705 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5706 } else {
5707 printk(TPACPI_ERR
5708 "ThinkPad ACPI EC access misbehaving, "
5709 "disabling thermal sensors access\n");
5710 thermal_read_mode = TPACPI_THERMAL_NONE;
5712 } else {
5713 thermal_read_mode =
5714 (ta2 != 0) ?
5715 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5717 } else if (acpi_tmp7) {
5718 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5719 /* 600e/x, 770e, 770x */
5720 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5721 } else {
5722 /* Standard ACPI TMPx access, max 8 sensors */
5723 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5725 } else {
5726 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5727 thermal_read_mode = TPACPI_THERMAL_NONE;
5730 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5731 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5732 thermal_read_mode);
5734 switch (thermal_read_mode) {
5735 case TPACPI_THERMAL_TPEC_16:
5736 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5737 &thermal_temp_input16_group);
5738 if (res)
5739 return res;
5740 break;
5741 case TPACPI_THERMAL_TPEC_8:
5742 case TPACPI_THERMAL_ACPI_TMP07:
5743 case TPACPI_THERMAL_ACPI_UPDT:
5744 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5745 &thermal_temp_input8_group);
5746 if (res)
5747 return res;
5748 break;
5749 case TPACPI_THERMAL_NONE:
5750 default:
5751 return 1;
5754 return 0;
5757 static void thermal_exit(void)
5759 switch (thermal_read_mode) {
5760 case TPACPI_THERMAL_TPEC_16:
5761 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5762 &thermal_temp_input16_group);
5763 break;
5764 case TPACPI_THERMAL_TPEC_8:
5765 case TPACPI_THERMAL_ACPI_TMP07:
5766 case TPACPI_THERMAL_ACPI_UPDT:
5767 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5768 &thermal_temp_input16_group);
5769 break;
5770 case TPACPI_THERMAL_NONE:
5771 default:
5772 break;
5776 static int thermal_read(char *p)
5778 int len = 0;
5779 int n, i;
5780 struct ibm_thermal_sensors_struct t;
5782 n = thermal_get_sensors(&t);
5783 if (unlikely(n < 0))
5784 return n;
5786 len += sprintf(p + len, "temperatures:\t");
5788 if (n > 0) {
5789 for (i = 0; i < (n - 1); i++)
5790 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5791 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5792 } else
5793 len += sprintf(p + len, "not supported\n");
5795 return len;
5798 static struct ibm_struct thermal_driver_data = {
5799 .name = "thermal",
5800 .read = thermal_read,
5801 .exit = thermal_exit,
5804 /*************************************************************************
5805 * EC Dump subdriver
5808 static u8 ecdump_regs[256];
5810 static int ecdump_read(char *p)
5812 int len = 0;
5813 int i, j;
5814 u8 v;
5816 len += sprintf(p + len, "EC "
5817 " +00 +01 +02 +03 +04 +05 +06 +07"
5818 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5819 for (i = 0; i < 256; i += 16) {
5820 len += sprintf(p + len, "EC 0x%02x:", i);
5821 for (j = 0; j < 16; j++) {
5822 if (!acpi_ec_read(i + j, &v))
5823 break;
5824 if (v != ecdump_regs[i + j])
5825 len += sprintf(p + len, " *%02x", v);
5826 else
5827 len += sprintf(p + len, " %02x", v);
5828 ecdump_regs[i + j] = v;
5830 len += sprintf(p + len, "\n");
5831 if (j != 16)
5832 break;
5835 /* These are way too dangerous to advertise openly... */
5836 #if 0
5837 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5838 " (<offset> is 00-ff, <value> is 00-ff)\n");
5839 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5840 " (<offset> is 00-ff, <value> is 0-255)\n");
5841 #endif
5842 return len;
5845 static int ecdump_write(char *buf)
5847 char *cmd;
5848 int i, v;
5850 while ((cmd = next_cmd(&buf))) {
5851 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5852 /* i and v set */
5853 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5854 /* i and v set */
5855 } else
5856 return -EINVAL;
5857 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5858 if (!acpi_ec_write(i, v))
5859 return -EIO;
5860 } else
5861 return -EINVAL;
5864 return 0;
5867 static struct ibm_struct ecdump_driver_data = {
5868 .name = "ecdump",
5869 .read = ecdump_read,
5870 .write = ecdump_write,
5871 .flags.experimental = 1,
5874 /*************************************************************************
5875 * Backlight/brightness subdriver
5878 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5881 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5882 * CMOS NVRAM byte 0x5E, bits 0-3.
5884 * EC HBRV (0x31) has the following layout
5885 * Bit 7: unknown function
5886 * Bit 6: unknown function
5887 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5888 * Bit 4: must be set to zero to avoid problems
5889 * Bit 3-0: backlight brightness level
5891 * brightness_get_raw returns status data in the HBRV layout
5893 * WARNING: The X61 has been verified to use HBRV for something else, so
5894 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5895 * testing on the very early *60 Lenovo models...
5898 enum {
5899 TP_EC_BACKLIGHT = 0x31,
5901 /* TP_EC_BACKLIGHT bitmasks */
5902 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5903 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5904 TP_EC_BACKLIGHT_MAPSW = 0x20,
5907 enum tpacpi_brightness_access_mode {
5908 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5909 TPACPI_BRGHT_MODE_EC, /* EC control */
5910 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5911 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5912 TPACPI_BRGHT_MODE_MAX
5915 static struct backlight_device *ibm_backlight_device;
5917 static enum tpacpi_brightness_access_mode brightness_mode =
5918 TPACPI_BRGHT_MODE_MAX;
5920 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5922 static struct mutex brightness_mutex;
5924 /* NVRAM brightness access,
5925 * call with brightness_mutex held! */
5926 static unsigned int tpacpi_brightness_nvram_get(void)
5928 u8 lnvram;
5930 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5931 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5932 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5933 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5935 return lnvram;
5938 static void tpacpi_brightness_checkpoint_nvram(void)
5940 u8 lec = 0;
5941 u8 b_nvram;
5943 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5944 return;
5946 vdbg_printk(TPACPI_DBG_BRGHT,
5947 "trying to checkpoint backlight level to NVRAM...\n");
5949 if (mutex_lock_killable(&brightness_mutex) < 0)
5950 return;
5952 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5953 goto unlock;
5954 lec &= TP_EC_BACKLIGHT_LVLMSK;
5955 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5957 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5958 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5959 /* NVRAM needs update */
5960 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5961 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5962 b_nvram |= lec;
5963 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5964 dbg_printk(TPACPI_DBG_BRGHT,
5965 "updated NVRAM backlight level to %u (0x%02x)\n",
5966 (unsigned int) lec, (unsigned int) b_nvram);
5967 } else
5968 vdbg_printk(TPACPI_DBG_BRGHT,
5969 "NVRAM backlight level already is %u (0x%02x)\n",
5970 (unsigned int) lec, (unsigned int) b_nvram);
5972 unlock:
5973 mutex_unlock(&brightness_mutex);
5977 /* call with brightness_mutex held! */
5978 static int tpacpi_brightness_get_raw(int *status)
5980 u8 lec = 0;
5982 switch (brightness_mode) {
5983 case TPACPI_BRGHT_MODE_UCMS_STEP:
5984 *status = tpacpi_brightness_nvram_get();
5985 return 0;
5986 case TPACPI_BRGHT_MODE_EC:
5987 case TPACPI_BRGHT_MODE_ECNVRAM:
5988 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5989 return -EIO;
5990 *status = lec;
5991 return 0;
5992 default:
5993 return -ENXIO;
5997 /* call with brightness_mutex held! */
5998 /* do NOT call with illegal backlight level value */
5999 static int tpacpi_brightness_set_ec(unsigned int value)
6001 u8 lec = 0;
6003 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6004 return -EIO;
6006 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6007 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6008 (value & TP_EC_BACKLIGHT_LVLMSK))))
6009 return -EIO;
6011 return 0;
6014 /* call with brightness_mutex held! */
6015 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6017 int cmos_cmd, inc;
6018 unsigned int current_value, i;
6020 current_value = tpacpi_brightness_nvram_get();
6022 if (value == current_value)
6023 return 0;
6025 cmos_cmd = (value > current_value) ?
6026 TP_CMOS_BRIGHTNESS_UP :
6027 TP_CMOS_BRIGHTNESS_DOWN;
6028 inc = (value > current_value) ? 1 : -1;
6030 for (i = current_value; i != value; i += inc)
6031 if (issue_thinkpad_cmos_command(cmos_cmd))
6032 return -EIO;
6034 return 0;
6037 /* May return EINTR which can always be mapped to ERESTARTSYS */
6038 static int brightness_set(unsigned int value)
6040 int res;
6042 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6043 value < 0)
6044 return -EINVAL;
6046 vdbg_printk(TPACPI_DBG_BRGHT,
6047 "set backlight level to %d\n", value);
6049 res = mutex_lock_killable(&brightness_mutex);
6050 if (res < 0)
6051 return res;
6053 switch (brightness_mode) {
6054 case TPACPI_BRGHT_MODE_EC:
6055 case TPACPI_BRGHT_MODE_ECNVRAM:
6056 res = tpacpi_brightness_set_ec(value);
6057 break;
6058 case TPACPI_BRGHT_MODE_UCMS_STEP:
6059 res = tpacpi_brightness_set_ucmsstep(value);
6060 break;
6061 default:
6062 res = -ENXIO;
6065 mutex_unlock(&brightness_mutex);
6066 return res;
6069 /* sysfs backlight class ----------------------------------------------- */
6071 static int brightness_update_status(struct backlight_device *bd)
6073 unsigned int level =
6074 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6075 bd->props.power == FB_BLANK_UNBLANK) ?
6076 bd->props.brightness : 0;
6078 dbg_printk(TPACPI_DBG_BRGHT,
6079 "backlight: attempt to set level to %d\n",
6080 level);
6082 /* it is the backlight class's job (caller) to handle
6083 * EINTR and other errors properly */
6084 return brightness_set(level);
6087 static int brightness_get(struct backlight_device *bd)
6089 int status, res;
6091 res = mutex_lock_killable(&brightness_mutex);
6092 if (res < 0)
6093 return 0;
6095 res = tpacpi_brightness_get_raw(&status);
6097 mutex_unlock(&brightness_mutex);
6099 if (res < 0)
6100 return 0;
6102 return status & TP_EC_BACKLIGHT_LVLMSK;
6105 static void tpacpi_brightness_notify_change(void)
6107 backlight_force_update(ibm_backlight_device,
6108 BACKLIGHT_UPDATE_HOTKEY);
6111 static struct backlight_ops ibm_backlight_data = {
6112 .get_brightness = brightness_get,
6113 .update_status = brightness_update_status,
6116 /* --------------------------------------------------------------------- */
6119 * These are only useful for models that have only one possibility
6120 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6121 * these quirks.
6123 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6124 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6125 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6127 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6128 /* Models with ATI GPUs known to require ECNVRAM mode */
6129 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6131 /* Models with ATI GPUs that can use ECNVRAM */
6132 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6133 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6134 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6135 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6137 /* Models with Intel Extreme Graphics 2 */
6138 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6139 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6140 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 /* Models with Intel GMA900 */
6143 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6144 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6145 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6148 static int __init brightness_init(struct ibm_init_struct *iibm)
6150 int b;
6151 unsigned long quirks;
6153 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6155 mutex_init(&brightness_mutex);
6157 quirks = tpacpi_check_quirks(brightness_quirk_table,
6158 ARRAY_SIZE(brightness_quirk_table));
6161 * We always attempt to detect acpi support, so as to switch
6162 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6163 * going to publish a backlight interface
6165 b = tpacpi_check_std_acpi_brightness_support();
6166 if (b > 0) {
6168 if (acpi_video_backlight_support()) {
6169 if (brightness_enable > 1) {
6170 printk(TPACPI_NOTICE
6171 "Standard ACPI backlight interface "
6172 "available, not loading native one.\n");
6173 return 1;
6174 } else if (brightness_enable == 1) {
6175 printk(TPACPI_NOTICE
6176 "Backlight control force enabled, even if standard "
6177 "ACPI backlight interface is available\n");
6179 } else {
6180 if (brightness_enable > 1) {
6181 printk(TPACPI_NOTICE
6182 "Standard ACPI backlight interface not "
6183 "available, thinkpad_acpi native "
6184 "brightness control enabled\n");
6189 if (!brightness_enable) {
6190 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6191 "brightness support disabled by "
6192 "module parameter\n");
6193 return 1;
6196 if (b > 16) {
6197 printk(TPACPI_ERR
6198 "Unsupported brightness interface, "
6199 "please contact %s\n", TPACPI_MAIL);
6200 return 1;
6202 if (b == 16)
6203 tp_features.bright_16levels = 1;
6206 * Check for module parameter bogosity, note that we
6207 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6208 * able to detect "unspecified"
6210 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6211 return -EINVAL;
6213 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6214 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6215 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6216 if (quirks & TPACPI_BRGHT_Q_EC)
6217 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6218 else
6219 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6221 dbg_printk(TPACPI_DBG_BRGHT,
6222 "driver auto-selected brightness_mode=%d\n",
6223 brightness_mode);
6226 /* Safety */
6227 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6228 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6229 brightness_mode == TPACPI_BRGHT_MODE_EC))
6230 return -EINVAL;
6232 if (tpacpi_brightness_get_raw(&b) < 0)
6233 return 1;
6235 if (tp_features.bright_16levels)
6236 printk(TPACPI_INFO
6237 "detected a 16-level brightness capable ThinkPad\n");
6239 ibm_backlight_device = backlight_device_register(
6240 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6241 &ibm_backlight_data);
6242 if (IS_ERR(ibm_backlight_device)) {
6243 int rc = PTR_ERR(ibm_backlight_device);
6244 ibm_backlight_device = NULL;
6245 printk(TPACPI_ERR "Could not register backlight device\n");
6246 return rc;
6248 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6249 "brightness is supported\n");
6251 if (quirks & TPACPI_BRGHT_Q_ASK) {
6252 printk(TPACPI_NOTICE
6253 "brightness: will use unverified default: "
6254 "brightness_mode=%d\n", brightness_mode);
6255 printk(TPACPI_NOTICE
6256 "brightness: please report to %s whether it works well "
6257 "or not on your ThinkPad\n", TPACPI_MAIL);
6260 ibm_backlight_device->props.max_brightness =
6261 (tp_features.bright_16levels)? 15 : 7;
6262 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6263 backlight_update_status(ibm_backlight_device);
6265 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6266 "brightness: registering brightness hotkeys "
6267 "as change notification\n");
6268 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6269 | TP_ACPI_HKEY_BRGHTUP_MASK
6270 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6271 return 0;
6274 static void brightness_suspend(pm_message_t state)
6276 tpacpi_brightness_checkpoint_nvram();
6279 static void brightness_shutdown(void)
6281 tpacpi_brightness_checkpoint_nvram();
6284 static void brightness_exit(void)
6286 if (ibm_backlight_device) {
6287 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6288 "calling backlight_device_unregister()\n");
6289 backlight_device_unregister(ibm_backlight_device);
6292 tpacpi_brightness_checkpoint_nvram();
6295 static int brightness_read(char *p)
6297 int len = 0;
6298 int level;
6300 level = brightness_get(NULL);
6301 if (level < 0) {
6302 len += sprintf(p + len, "level:\t\tunreadable\n");
6303 } else {
6304 len += sprintf(p + len, "level:\t\t%d\n", level);
6305 len += sprintf(p + len, "commands:\tup, down\n");
6306 len += sprintf(p + len, "commands:\tlevel <level>"
6307 " (<level> is 0-%d)\n",
6308 (tp_features.bright_16levels) ? 15 : 7);
6311 return len;
6314 static int brightness_write(char *buf)
6316 int level;
6317 int rc;
6318 char *cmd;
6319 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6321 level = brightness_get(NULL);
6322 if (level < 0)
6323 return level;
6325 while ((cmd = next_cmd(&buf))) {
6326 if (strlencmp(cmd, "up") == 0) {
6327 if (level < max_level)
6328 level++;
6329 } else if (strlencmp(cmd, "down") == 0) {
6330 if (level > 0)
6331 level--;
6332 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6333 level >= 0 && level <= max_level) {
6334 /* new level set */
6335 } else
6336 return -EINVAL;
6339 tpacpi_disclose_usertask("procfs brightness",
6340 "set level to %d\n", level);
6343 * Now we know what the final level should be, so we try to set it.
6344 * Doing it this way makes the syscall restartable in case of EINTR
6346 rc = brightness_set(level);
6347 if (!rc && ibm_backlight_device)
6348 backlight_force_update(ibm_backlight_device,
6349 BACKLIGHT_UPDATE_SYSFS);
6350 return (rc == -EINTR)? -ERESTARTSYS : rc;
6353 static struct ibm_struct brightness_driver_data = {
6354 .name = "brightness",
6355 .read = brightness_read,
6356 .write = brightness_write,
6357 .exit = brightness_exit,
6358 .suspend = brightness_suspend,
6359 .shutdown = brightness_shutdown,
6362 /*************************************************************************
6363 * Volume subdriver
6366 static int volume_offset = 0x30;
6368 static int volume_read(char *p)
6370 int len = 0;
6371 u8 level;
6373 if (!acpi_ec_read(volume_offset, &level)) {
6374 len += sprintf(p + len, "level:\t\tunreadable\n");
6375 } else {
6376 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6377 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6378 len += sprintf(p + len, "commands:\tup, down, mute\n");
6379 len += sprintf(p + len, "commands:\tlevel <level>"
6380 " (<level> is 0-15)\n");
6383 return len;
6386 static int volume_write(char *buf)
6388 int cmos_cmd, inc, i;
6389 u8 level, mute;
6390 int new_level, new_mute;
6391 char *cmd;
6393 while ((cmd = next_cmd(&buf))) {
6394 if (!acpi_ec_read(volume_offset, &level))
6395 return -EIO;
6396 new_mute = mute = level & 0x40;
6397 new_level = level = level & 0xf;
6399 if (strlencmp(cmd, "up") == 0) {
6400 if (mute)
6401 new_mute = 0;
6402 else
6403 new_level = level == 15 ? 15 : level + 1;
6404 } else if (strlencmp(cmd, "down") == 0) {
6405 if (mute)
6406 new_mute = 0;
6407 else
6408 new_level = level == 0 ? 0 : level - 1;
6409 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6410 new_level >= 0 && new_level <= 15) {
6411 /* new_level set */
6412 } else if (strlencmp(cmd, "mute") == 0) {
6413 new_mute = 0x40;
6414 } else
6415 return -EINVAL;
6417 if (new_level != level) {
6418 /* mute doesn't change */
6420 cmos_cmd = (new_level > level) ?
6421 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6422 inc = new_level > level ? 1 : -1;
6424 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6425 !acpi_ec_write(volume_offset, level)))
6426 return -EIO;
6428 for (i = level; i != new_level; i += inc)
6429 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6430 !acpi_ec_write(volume_offset, i + inc))
6431 return -EIO;
6433 if (mute &&
6434 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6435 !acpi_ec_write(volume_offset, new_level + mute))) {
6436 return -EIO;
6440 if (new_mute != mute) {
6441 /* level doesn't change */
6443 cmos_cmd = (new_mute) ?
6444 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6446 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6447 !acpi_ec_write(volume_offset, level + new_mute))
6448 return -EIO;
6452 return 0;
6455 static struct ibm_struct volume_driver_data = {
6456 .name = "volume",
6457 .read = volume_read,
6458 .write = volume_write,
6461 /*************************************************************************
6462 * Fan subdriver
6466 * FAN ACCESS MODES
6468 * TPACPI_FAN_RD_ACPI_GFAN:
6469 * ACPI GFAN method: returns fan level
6471 * see TPACPI_FAN_WR_ACPI_SFAN
6472 * EC 0x2f (HFSP) not available if GFAN exists
6474 * TPACPI_FAN_WR_ACPI_SFAN:
6475 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6477 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6478 * it for writing.
6480 * TPACPI_FAN_WR_TPEC:
6481 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6482 * Supported on almost all ThinkPads
6484 * Fan speed changes of any sort (including those caused by the
6485 * disengaged mode) are usually done slowly by the firmware as the
6486 * maximum ammount of fan duty cycle change per second seems to be
6487 * limited.
6489 * Reading is not available if GFAN exists.
6490 * Writing is not available if SFAN exists.
6492 * Bits
6493 * 7 automatic mode engaged;
6494 * (default operation mode of the ThinkPad)
6495 * fan level is ignored in this mode.
6496 * 6 full speed mode (takes precedence over bit 7);
6497 * not available on all thinkpads. May disable
6498 * the tachometer while the fan controller ramps up
6499 * the speed (which can take up to a few *minutes*).
6500 * Speeds up fan to 100% duty-cycle, which is far above
6501 * the standard RPM levels. It is not impossible that
6502 * it could cause hardware damage.
6503 * 5-3 unused in some models. Extra bits for fan level
6504 * in others, but still useless as all values above
6505 * 7 map to the same speed as level 7 in these models.
6506 * 2-0 fan level (0..7 usually)
6507 * 0x00 = stop
6508 * 0x07 = max (set when temperatures critical)
6509 * Some ThinkPads may have other levels, see
6510 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6512 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6513 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6514 * does so, its initial value is meaningless (0x07).
6516 * For firmware bugs, refer to:
6517 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6519 * ----
6521 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6522 * Main fan tachometer reading (in RPM)
6524 * This register is present on all ThinkPads with a new-style EC, and
6525 * it is known not to be present on the A21m/e, and T22, as there is
6526 * something else in offset 0x84 according to the ACPI DSDT. Other
6527 * ThinkPads from this same time period (and earlier) probably lack the
6528 * tachometer as well.
6530 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6531 * was never fixed by IBM to report the EC firmware version string
6532 * probably support the tachometer (like the early X models), so
6533 * detecting it is quite hard. We need more data to know for sure.
6535 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6536 * might result.
6538 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6539 * mode.
6541 * For firmware bugs, refer to:
6542 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6544 * ----
6546 * ThinkPad EC register 0x31 bit 0 (only on select models)
6548 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6549 * show the speed of the main fan. When bit 0 of EC register 0x31
6550 * is one, the tachometer registers show the speed of the auxiliary
6551 * fan.
6553 * Fan control seems to affect both fans, regardless of the state
6554 * of this bit.
6556 * So far, only the firmware for the X60/X61 non-tablet versions
6557 * seem to support this (firmware TP-7M).
6559 * TPACPI_FAN_WR_ACPI_FANS:
6560 * ThinkPad X31, X40, X41. Not available in the X60.
6562 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6563 * high speed. ACPI DSDT seems to map these three speeds to levels
6564 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6565 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6567 * The speeds are stored on handles
6568 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6570 * There are three default speed sets, acessible as handles:
6571 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6573 * ACPI DSDT switches which set is in use depending on various
6574 * factors.
6576 * TPACPI_FAN_WR_TPEC is also available and should be used to
6577 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6578 * but the ACPI tables just mention level 7.
6581 enum { /* Fan control constants */
6582 fan_status_offset = 0x2f, /* EC register 0x2f */
6583 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6584 * 0x84 must be read before 0x85 */
6585 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6586 bit 0 selects which fan is active */
6588 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6589 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6591 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6594 enum fan_status_access_mode {
6595 TPACPI_FAN_NONE = 0, /* No fan status or control */
6596 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6597 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6600 enum fan_control_access_mode {
6601 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6602 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6603 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6604 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6607 enum fan_control_commands {
6608 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6609 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6610 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6611 * and also watchdog cmd */
6614 static int fan_control_allowed;
6616 static enum fan_status_access_mode fan_status_access_mode;
6617 static enum fan_control_access_mode fan_control_access_mode;
6618 static enum fan_control_commands fan_control_commands;
6620 static u8 fan_control_initial_status;
6621 static u8 fan_control_desired_level;
6622 static u8 fan_control_resume_level;
6623 static int fan_watchdog_maxinterval;
6625 static struct mutex fan_mutex;
6627 static void fan_watchdog_fire(struct work_struct *ignored);
6628 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6630 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6631 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6632 "\\FSPD", /* 600e/x, 770e, 770x */
6633 ); /* all others */
6634 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6635 "JFNS", /* 770x-JL */
6636 ); /* all others */
6639 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6640 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6641 * be in auto mode (0x80).
6643 * This is corrected by any write to HFSP either by the driver, or
6644 * by the firmware.
6646 * We assume 0x07 really means auto mode while this quirk is active,
6647 * as this is far more likely than the ThinkPad being in level 7,
6648 * which is only used by the firmware during thermal emergencies.
6650 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6651 * TP-70 (T43, R52), which are known to be buggy.
6654 static void fan_quirk1_setup(void)
6656 if (fan_control_initial_status == 0x07) {
6657 printk(TPACPI_NOTICE
6658 "fan_init: initial fan status is unknown, "
6659 "assuming it is in auto mode\n");
6660 tp_features.fan_ctrl_status_undef = 1;
6664 static void fan_quirk1_handle(u8 *fan_status)
6666 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6667 if (*fan_status != fan_control_initial_status) {
6668 /* something changed the HFSP regisnter since
6669 * driver init time, so it is not undefined
6670 * anymore */
6671 tp_features.fan_ctrl_status_undef = 0;
6672 } else {
6673 /* Return most likely status. In fact, it
6674 * might be the only possible status */
6675 *fan_status = TP_EC_FAN_AUTO;
6680 /* Select main fan on X60/X61, NOOP on others */
6681 static bool fan_select_fan1(void)
6683 if (tp_features.second_fan) {
6684 u8 val;
6686 if (ec_read(fan_select_offset, &val) < 0)
6687 return false;
6688 val &= 0xFEU;
6689 if (ec_write(fan_select_offset, val) < 0)
6690 return false;
6692 return true;
6695 /* Select secondary fan on X60/X61 */
6696 static bool fan_select_fan2(void)
6698 u8 val;
6700 if (!tp_features.second_fan)
6701 return false;
6703 if (ec_read(fan_select_offset, &val) < 0)
6704 return false;
6705 val |= 0x01U;
6706 if (ec_write(fan_select_offset, val) < 0)
6707 return false;
6709 return true;
6713 * Call with fan_mutex held
6715 static void fan_update_desired_level(u8 status)
6717 if ((status &
6718 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6719 if (status > 7)
6720 fan_control_desired_level = 7;
6721 else
6722 fan_control_desired_level = status;
6726 static int fan_get_status(u8 *status)
6728 u8 s;
6730 /* TODO:
6731 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6733 switch (fan_status_access_mode) {
6734 case TPACPI_FAN_RD_ACPI_GFAN:
6735 /* 570, 600e/x, 770e, 770x */
6737 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6738 return -EIO;
6740 if (likely(status))
6741 *status = s & 0x07;
6743 break;
6745 case TPACPI_FAN_RD_TPEC:
6746 /* all except 570, 600e/x, 770e, 770x */
6747 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6748 return -EIO;
6750 if (likely(status)) {
6751 *status = s;
6752 fan_quirk1_handle(status);
6755 break;
6757 default:
6758 return -ENXIO;
6761 return 0;
6764 static int fan_get_status_safe(u8 *status)
6766 int rc;
6767 u8 s;
6769 if (mutex_lock_killable(&fan_mutex))
6770 return -ERESTARTSYS;
6771 rc = fan_get_status(&s);
6772 if (!rc)
6773 fan_update_desired_level(s);
6774 mutex_unlock(&fan_mutex);
6776 if (status)
6777 *status = s;
6779 return rc;
6782 static int fan_get_speed(unsigned int *speed)
6784 u8 hi, lo;
6786 switch (fan_status_access_mode) {
6787 case TPACPI_FAN_RD_TPEC:
6788 /* all except 570, 600e/x, 770e, 770x */
6789 if (unlikely(!fan_select_fan1()))
6790 return -EIO;
6791 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6792 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6793 return -EIO;
6795 if (likely(speed))
6796 *speed = (hi << 8) | lo;
6798 break;
6800 default:
6801 return -ENXIO;
6804 return 0;
6807 static int fan2_get_speed(unsigned int *speed)
6809 u8 hi, lo;
6810 bool rc;
6812 switch (fan_status_access_mode) {
6813 case TPACPI_FAN_RD_TPEC:
6814 /* all except 570, 600e/x, 770e, 770x */
6815 if (unlikely(!fan_select_fan2()))
6816 return -EIO;
6817 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6818 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6819 fan_select_fan1(); /* play it safe */
6820 if (rc)
6821 return -EIO;
6823 if (likely(speed))
6824 *speed = (hi << 8) | lo;
6826 break;
6828 default:
6829 return -ENXIO;
6832 return 0;
6835 static int fan_set_level(int level)
6837 if (!fan_control_allowed)
6838 return -EPERM;
6840 switch (fan_control_access_mode) {
6841 case TPACPI_FAN_WR_ACPI_SFAN:
6842 if (level >= 0 && level <= 7) {
6843 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6844 return -EIO;
6845 } else
6846 return -EINVAL;
6847 break;
6849 case TPACPI_FAN_WR_ACPI_FANS:
6850 case TPACPI_FAN_WR_TPEC:
6851 if (!(level & TP_EC_FAN_AUTO) &&
6852 !(level & TP_EC_FAN_FULLSPEED) &&
6853 ((level < 0) || (level > 7)))
6854 return -EINVAL;
6856 /* safety net should the EC not support AUTO
6857 * or FULLSPEED mode bits and just ignore them */
6858 if (level & TP_EC_FAN_FULLSPEED)
6859 level |= 7; /* safety min speed 7 */
6860 else if (level & TP_EC_FAN_AUTO)
6861 level |= 4; /* safety min speed 4 */
6863 if (!acpi_ec_write(fan_status_offset, level))
6864 return -EIO;
6865 else
6866 tp_features.fan_ctrl_status_undef = 0;
6867 break;
6869 default:
6870 return -ENXIO;
6873 vdbg_printk(TPACPI_DBG_FAN,
6874 "fan control: set fan control register to 0x%02x\n", level);
6875 return 0;
6878 static int fan_set_level_safe(int level)
6880 int rc;
6882 if (!fan_control_allowed)
6883 return -EPERM;
6885 if (mutex_lock_killable(&fan_mutex))
6886 return -ERESTARTSYS;
6888 if (level == TPACPI_FAN_LAST_LEVEL)
6889 level = fan_control_desired_level;
6891 rc = fan_set_level(level);
6892 if (!rc)
6893 fan_update_desired_level(level);
6895 mutex_unlock(&fan_mutex);
6896 return rc;
6899 static int fan_set_enable(void)
6901 u8 s;
6902 int rc;
6904 if (!fan_control_allowed)
6905 return -EPERM;
6907 if (mutex_lock_killable(&fan_mutex))
6908 return -ERESTARTSYS;
6910 switch (fan_control_access_mode) {
6911 case TPACPI_FAN_WR_ACPI_FANS:
6912 case TPACPI_FAN_WR_TPEC:
6913 rc = fan_get_status(&s);
6914 if (rc < 0)
6915 break;
6917 /* Don't go out of emergency fan mode */
6918 if (s != 7) {
6919 s &= 0x07;
6920 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6923 if (!acpi_ec_write(fan_status_offset, s))
6924 rc = -EIO;
6925 else {
6926 tp_features.fan_ctrl_status_undef = 0;
6927 rc = 0;
6929 break;
6931 case TPACPI_FAN_WR_ACPI_SFAN:
6932 rc = fan_get_status(&s);
6933 if (rc < 0)
6934 break;
6936 s &= 0x07;
6938 /* Set fan to at least level 4 */
6939 s |= 4;
6941 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6942 rc = -EIO;
6943 else
6944 rc = 0;
6945 break;
6947 default:
6948 rc = -ENXIO;
6951 mutex_unlock(&fan_mutex);
6953 if (!rc)
6954 vdbg_printk(TPACPI_DBG_FAN,
6955 "fan control: set fan control register to 0x%02x\n",
6957 return rc;
6960 static int fan_set_disable(void)
6962 int rc;
6964 if (!fan_control_allowed)
6965 return -EPERM;
6967 if (mutex_lock_killable(&fan_mutex))
6968 return -ERESTARTSYS;
6970 rc = 0;
6971 switch (fan_control_access_mode) {
6972 case TPACPI_FAN_WR_ACPI_FANS:
6973 case TPACPI_FAN_WR_TPEC:
6974 if (!acpi_ec_write(fan_status_offset, 0x00))
6975 rc = -EIO;
6976 else {
6977 fan_control_desired_level = 0;
6978 tp_features.fan_ctrl_status_undef = 0;
6980 break;
6982 case TPACPI_FAN_WR_ACPI_SFAN:
6983 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6984 rc = -EIO;
6985 else
6986 fan_control_desired_level = 0;
6987 break;
6989 default:
6990 rc = -ENXIO;
6993 if (!rc)
6994 vdbg_printk(TPACPI_DBG_FAN,
6995 "fan control: set fan control register to 0\n");
6997 mutex_unlock(&fan_mutex);
6998 return rc;
7001 static int fan_set_speed(int speed)
7003 int rc;
7005 if (!fan_control_allowed)
7006 return -EPERM;
7008 if (mutex_lock_killable(&fan_mutex))
7009 return -ERESTARTSYS;
7011 rc = 0;
7012 switch (fan_control_access_mode) {
7013 case TPACPI_FAN_WR_ACPI_FANS:
7014 if (speed >= 0 && speed <= 65535) {
7015 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7016 speed, speed, speed))
7017 rc = -EIO;
7018 } else
7019 rc = -EINVAL;
7020 break;
7022 default:
7023 rc = -ENXIO;
7026 mutex_unlock(&fan_mutex);
7027 return rc;
7030 static void fan_watchdog_reset(void)
7032 static int fan_watchdog_active;
7034 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7035 return;
7037 if (fan_watchdog_active)
7038 cancel_delayed_work(&fan_watchdog_task);
7040 if (fan_watchdog_maxinterval > 0 &&
7041 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7042 fan_watchdog_active = 1;
7043 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7044 msecs_to_jiffies(fan_watchdog_maxinterval
7045 * 1000))) {
7046 printk(TPACPI_ERR
7047 "failed to queue the fan watchdog, "
7048 "watchdog will not trigger\n");
7050 } else
7051 fan_watchdog_active = 0;
7054 static void fan_watchdog_fire(struct work_struct *ignored)
7056 int rc;
7058 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7059 return;
7061 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7062 rc = fan_set_enable();
7063 if (rc < 0) {
7064 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7065 "will try again later...\n", -rc);
7066 /* reschedule for later */
7067 fan_watchdog_reset();
7072 * SYSFS fan layout: hwmon compatible (device)
7074 * pwm*_enable:
7075 * 0: "disengaged" mode
7076 * 1: manual mode
7077 * 2: native EC "auto" mode (recommended, hardware default)
7079 * pwm*: set speed in manual mode, ignored otherwise.
7080 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7081 * interpolation.
7083 * fan*_input: tachometer reading, RPM
7086 * SYSFS fan layout: extensions
7088 * fan_watchdog (driver):
7089 * fan watchdog interval in seconds, 0 disables (default), max 120
7092 /* sysfs fan pwm1_enable ----------------------------------------------- */
7093 static ssize_t fan_pwm1_enable_show(struct device *dev,
7094 struct device_attribute *attr,
7095 char *buf)
7097 int res, mode;
7098 u8 status;
7100 res = fan_get_status_safe(&status);
7101 if (res)
7102 return res;
7104 if (status & TP_EC_FAN_FULLSPEED) {
7105 mode = 0;
7106 } else if (status & TP_EC_FAN_AUTO) {
7107 mode = 2;
7108 } else
7109 mode = 1;
7111 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7114 static ssize_t fan_pwm1_enable_store(struct device *dev,
7115 struct device_attribute *attr,
7116 const char *buf, size_t count)
7118 unsigned long t;
7119 int res, level;
7121 if (parse_strtoul(buf, 2, &t))
7122 return -EINVAL;
7124 tpacpi_disclose_usertask("hwmon pwm1_enable",
7125 "set fan mode to %lu\n", t);
7127 switch (t) {
7128 case 0:
7129 level = TP_EC_FAN_FULLSPEED;
7130 break;
7131 case 1:
7132 level = TPACPI_FAN_LAST_LEVEL;
7133 break;
7134 case 2:
7135 level = TP_EC_FAN_AUTO;
7136 break;
7137 case 3:
7138 /* reserved for software-controlled auto mode */
7139 return -ENOSYS;
7140 default:
7141 return -EINVAL;
7144 res = fan_set_level_safe(level);
7145 if (res == -ENXIO)
7146 return -EINVAL;
7147 else if (res < 0)
7148 return res;
7150 fan_watchdog_reset();
7152 return count;
7155 static struct device_attribute dev_attr_fan_pwm1_enable =
7156 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7157 fan_pwm1_enable_show, fan_pwm1_enable_store);
7159 /* sysfs fan pwm1 ------------------------------------------------------ */
7160 static ssize_t fan_pwm1_show(struct device *dev,
7161 struct device_attribute *attr,
7162 char *buf)
7164 int res;
7165 u8 status;
7167 res = fan_get_status_safe(&status);
7168 if (res)
7169 return res;
7171 if ((status &
7172 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7173 status = fan_control_desired_level;
7175 if (status > 7)
7176 status = 7;
7178 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7181 static ssize_t fan_pwm1_store(struct device *dev,
7182 struct device_attribute *attr,
7183 const char *buf, size_t count)
7185 unsigned long s;
7186 int rc;
7187 u8 status, newlevel;
7189 if (parse_strtoul(buf, 255, &s))
7190 return -EINVAL;
7192 tpacpi_disclose_usertask("hwmon pwm1",
7193 "set fan speed to %lu\n", s);
7195 /* scale down from 0-255 to 0-7 */
7196 newlevel = (s >> 5) & 0x07;
7198 if (mutex_lock_killable(&fan_mutex))
7199 return -ERESTARTSYS;
7201 rc = fan_get_status(&status);
7202 if (!rc && (status &
7203 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7204 rc = fan_set_level(newlevel);
7205 if (rc == -ENXIO)
7206 rc = -EINVAL;
7207 else if (!rc) {
7208 fan_update_desired_level(newlevel);
7209 fan_watchdog_reset();
7213 mutex_unlock(&fan_mutex);
7214 return (rc)? rc : count;
7217 static struct device_attribute dev_attr_fan_pwm1 =
7218 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7219 fan_pwm1_show, fan_pwm1_store);
7221 /* sysfs fan fan1_input ------------------------------------------------ */
7222 static ssize_t fan_fan1_input_show(struct device *dev,
7223 struct device_attribute *attr,
7224 char *buf)
7226 int res;
7227 unsigned int speed;
7229 res = fan_get_speed(&speed);
7230 if (res < 0)
7231 return res;
7233 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7236 static struct device_attribute dev_attr_fan_fan1_input =
7237 __ATTR(fan1_input, S_IRUGO,
7238 fan_fan1_input_show, NULL);
7240 /* sysfs fan fan2_input ------------------------------------------------ */
7241 static ssize_t fan_fan2_input_show(struct device *dev,
7242 struct device_attribute *attr,
7243 char *buf)
7245 int res;
7246 unsigned int speed;
7248 res = fan2_get_speed(&speed);
7249 if (res < 0)
7250 return res;
7252 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7255 static struct device_attribute dev_attr_fan_fan2_input =
7256 __ATTR(fan2_input, S_IRUGO,
7257 fan_fan2_input_show, NULL);
7259 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7260 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7261 char *buf)
7263 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7266 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7267 const char *buf, size_t count)
7269 unsigned long t;
7271 if (parse_strtoul(buf, 120, &t))
7272 return -EINVAL;
7274 if (!fan_control_allowed)
7275 return -EPERM;
7277 fan_watchdog_maxinterval = t;
7278 fan_watchdog_reset();
7280 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7282 return count;
7285 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7286 fan_fan_watchdog_show, fan_fan_watchdog_store);
7288 /* --------------------------------------------------------------------- */
7289 static struct attribute *fan_attributes[] = {
7290 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7291 &dev_attr_fan_fan1_input.attr,
7292 NULL, /* for fan2_input */
7293 NULL
7296 static const struct attribute_group fan_attr_group = {
7297 .attrs = fan_attributes,
7300 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7301 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7303 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7304 { .vendor = PCI_VENDOR_ID_IBM, \
7305 .bios = TPACPI_MATCH_ANY, \
7306 .ec = TPID(__id1, __id2), \
7307 .quirks = __quirks }
7309 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7310 { .vendor = PCI_VENDOR_ID_LENOVO, \
7311 .bios = TPACPI_MATCH_ANY, \
7312 .ec = TPID(__id1, __id2), \
7313 .quirks = __quirks }
7315 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7316 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7317 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7318 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7319 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7320 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7323 #undef TPACPI_FAN_QL
7324 #undef TPACPI_FAN_QI
7326 static int __init fan_init(struct ibm_init_struct *iibm)
7328 int rc;
7329 unsigned long quirks;
7331 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7332 "initializing fan subdriver\n");
7334 mutex_init(&fan_mutex);
7335 fan_status_access_mode = TPACPI_FAN_NONE;
7336 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7337 fan_control_commands = 0;
7338 fan_watchdog_maxinterval = 0;
7339 tp_features.fan_ctrl_status_undef = 0;
7340 tp_features.second_fan = 0;
7341 fan_control_desired_level = 7;
7343 TPACPI_ACPIHANDLE_INIT(fans);
7344 TPACPI_ACPIHANDLE_INIT(gfan);
7345 TPACPI_ACPIHANDLE_INIT(sfan);
7347 quirks = tpacpi_check_quirks(fan_quirk_table,
7348 ARRAY_SIZE(fan_quirk_table));
7350 if (gfan_handle) {
7351 /* 570, 600e/x, 770e, 770x */
7352 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7353 } else {
7354 /* all other ThinkPads: note that even old-style
7355 * ThinkPad ECs supports the fan control register */
7356 if (likely(acpi_ec_read(fan_status_offset,
7357 &fan_control_initial_status))) {
7358 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7359 if (quirks & TPACPI_FAN_Q1)
7360 fan_quirk1_setup();
7361 if (quirks & TPACPI_FAN_2FAN) {
7362 tp_features.second_fan = 1;
7363 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7364 "secondary fan support enabled\n");
7366 } else {
7367 printk(TPACPI_ERR
7368 "ThinkPad ACPI EC access misbehaving, "
7369 "fan status and control unavailable\n");
7370 return 1;
7374 if (sfan_handle) {
7375 /* 570, 770x-JL */
7376 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7377 fan_control_commands |=
7378 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7379 } else {
7380 if (!gfan_handle) {
7381 /* gfan without sfan means no fan control */
7382 /* all other models implement TP EC 0x2f control */
7384 if (fans_handle) {
7385 /* X31, X40, X41 */
7386 fan_control_access_mode =
7387 TPACPI_FAN_WR_ACPI_FANS;
7388 fan_control_commands |=
7389 TPACPI_FAN_CMD_SPEED |
7390 TPACPI_FAN_CMD_LEVEL |
7391 TPACPI_FAN_CMD_ENABLE;
7392 } else {
7393 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7394 fan_control_commands |=
7395 TPACPI_FAN_CMD_LEVEL |
7396 TPACPI_FAN_CMD_ENABLE;
7401 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7402 "fan is %s, modes %d, %d\n",
7403 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7404 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7405 fan_status_access_mode, fan_control_access_mode);
7407 /* fan control master switch */
7408 if (!fan_control_allowed) {
7409 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7410 fan_control_commands = 0;
7411 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7412 "fan control features disabled by parameter\n");
7415 /* update fan_control_desired_level */
7416 if (fan_status_access_mode != TPACPI_FAN_NONE)
7417 fan_get_status_safe(NULL);
7419 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7420 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7421 if (tp_features.second_fan) {
7422 /* attach second fan tachometer */
7423 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7424 &dev_attr_fan_fan2_input.attr;
7426 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7427 &fan_attr_group);
7428 if (rc < 0)
7429 return rc;
7431 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7432 &driver_attr_fan_watchdog);
7433 if (rc < 0) {
7434 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7435 &fan_attr_group);
7436 return rc;
7438 return 0;
7439 } else
7440 return 1;
7443 static void fan_exit(void)
7445 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7446 "cancelling any pending fan watchdog tasks\n");
7448 /* FIXME: can we really do this unconditionally? */
7449 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7450 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7451 &driver_attr_fan_watchdog);
7453 cancel_delayed_work(&fan_watchdog_task);
7454 flush_workqueue(tpacpi_wq);
7457 static void fan_suspend(pm_message_t state)
7459 int rc;
7461 if (!fan_control_allowed)
7462 return;
7464 /* Store fan status in cache */
7465 fan_control_resume_level = 0;
7466 rc = fan_get_status_safe(&fan_control_resume_level);
7467 if (rc < 0)
7468 printk(TPACPI_NOTICE
7469 "failed to read fan level for later "
7470 "restore during resume: %d\n", rc);
7472 /* if it is undefined, don't attempt to restore it.
7473 * KEEP THIS LAST */
7474 if (tp_features.fan_ctrl_status_undef)
7475 fan_control_resume_level = 0;
7478 static void fan_resume(void)
7480 u8 current_level = 7;
7481 bool do_set = false;
7482 int rc;
7484 /* DSDT *always* updates status on resume */
7485 tp_features.fan_ctrl_status_undef = 0;
7487 if (!fan_control_allowed ||
7488 !fan_control_resume_level ||
7489 (fan_get_status_safe(&current_level) < 0))
7490 return;
7492 switch (fan_control_access_mode) {
7493 case TPACPI_FAN_WR_ACPI_SFAN:
7494 /* never decrease fan level */
7495 do_set = (fan_control_resume_level > current_level);
7496 break;
7497 case TPACPI_FAN_WR_ACPI_FANS:
7498 case TPACPI_FAN_WR_TPEC:
7499 /* never decrease fan level, scale is:
7500 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7502 * We expect the firmware to set either 7 or AUTO, but we
7503 * handle FULLSPEED out of paranoia.
7505 * So, we can safely only restore FULLSPEED or 7, anything
7506 * else could slow the fan. Restoring AUTO is useless, at
7507 * best that's exactly what the DSDT already set (it is the
7508 * slower it uses).
7510 * Always keep in mind that the DSDT *will* have set the
7511 * fans to what the vendor supposes is the best level. We
7512 * muck with it only to speed the fan up.
7514 if (fan_control_resume_level != 7 &&
7515 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7516 return;
7517 else
7518 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7519 (current_level != fan_control_resume_level);
7520 break;
7521 default:
7522 return;
7524 if (do_set) {
7525 printk(TPACPI_NOTICE
7526 "restoring fan level to 0x%02x\n",
7527 fan_control_resume_level);
7528 rc = fan_set_level_safe(fan_control_resume_level);
7529 if (rc < 0)
7530 printk(TPACPI_NOTICE
7531 "failed to restore fan level: %d\n", rc);
7535 static int fan_read(char *p)
7537 int len = 0;
7538 int rc;
7539 u8 status;
7540 unsigned int speed = 0;
7542 switch (fan_status_access_mode) {
7543 case TPACPI_FAN_RD_ACPI_GFAN:
7544 /* 570, 600e/x, 770e, 770x */
7545 rc = fan_get_status_safe(&status);
7546 if (rc < 0)
7547 return rc;
7549 len += sprintf(p + len, "status:\t\t%s\n"
7550 "level:\t\t%d\n",
7551 (status != 0) ? "enabled" : "disabled", status);
7552 break;
7554 case TPACPI_FAN_RD_TPEC:
7555 /* all except 570, 600e/x, 770e, 770x */
7556 rc = fan_get_status_safe(&status);
7557 if (rc < 0)
7558 return rc;
7560 len += sprintf(p + len, "status:\t\t%s\n",
7561 (status != 0) ? "enabled" : "disabled");
7563 rc = fan_get_speed(&speed);
7564 if (rc < 0)
7565 return rc;
7567 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7569 if (status & TP_EC_FAN_FULLSPEED)
7570 /* Disengaged mode takes precedence */
7571 len += sprintf(p + len, "level:\t\tdisengaged\n");
7572 else if (status & TP_EC_FAN_AUTO)
7573 len += sprintf(p + len, "level:\t\tauto\n");
7574 else
7575 len += sprintf(p + len, "level:\t\t%d\n", status);
7576 break;
7578 case TPACPI_FAN_NONE:
7579 default:
7580 len += sprintf(p + len, "status:\t\tnot supported\n");
7583 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7584 len += sprintf(p + len, "commands:\tlevel <level>");
7586 switch (fan_control_access_mode) {
7587 case TPACPI_FAN_WR_ACPI_SFAN:
7588 len += sprintf(p + len, " (<level> is 0-7)\n");
7589 break;
7591 default:
7592 len += sprintf(p + len, " (<level> is 0-7, "
7593 "auto, disengaged, full-speed)\n");
7594 break;
7598 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7599 len += sprintf(p + len, "commands:\tenable, disable\n"
7600 "commands:\twatchdog <timeout> (<timeout> "
7601 "is 0 (off), 1-120 (seconds))\n");
7603 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7604 len += sprintf(p + len, "commands:\tspeed <speed>"
7605 " (<speed> is 0-65535)\n");
7607 return len;
7610 static int fan_write_cmd_level(const char *cmd, int *rc)
7612 int level;
7614 if (strlencmp(cmd, "level auto") == 0)
7615 level = TP_EC_FAN_AUTO;
7616 else if ((strlencmp(cmd, "level disengaged") == 0) |
7617 (strlencmp(cmd, "level full-speed") == 0))
7618 level = TP_EC_FAN_FULLSPEED;
7619 else if (sscanf(cmd, "level %d", &level) != 1)
7620 return 0;
7622 *rc = fan_set_level_safe(level);
7623 if (*rc == -ENXIO)
7624 printk(TPACPI_ERR "level command accepted for unsupported "
7625 "access mode %d", fan_control_access_mode);
7626 else if (!*rc)
7627 tpacpi_disclose_usertask("procfs fan",
7628 "set level to %d\n", level);
7630 return 1;
7633 static int fan_write_cmd_enable(const char *cmd, int *rc)
7635 if (strlencmp(cmd, "enable") != 0)
7636 return 0;
7638 *rc = fan_set_enable();
7639 if (*rc == -ENXIO)
7640 printk(TPACPI_ERR "enable command accepted for unsupported "
7641 "access mode %d", fan_control_access_mode);
7642 else if (!*rc)
7643 tpacpi_disclose_usertask("procfs fan", "enable\n");
7645 return 1;
7648 static int fan_write_cmd_disable(const char *cmd, int *rc)
7650 if (strlencmp(cmd, "disable") != 0)
7651 return 0;
7653 *rc = fan_set_disable();
7654 if (*rc == -ENXIO)
7655 printk(TPACPI_ERR "disable command accepted for unsupported "
7656 "access mode %d", fan_control_access_mode);
7657 else if (!*rc)
7658 tpacpi_disclose_usertask("procfs fan", "disable\n");
7660 return 1;
7663 static int fan_write_cmd_speed(const char *cmd, int *rc)
7665 int speed;
7667 /* TODO:
7668 * Support speed <low> <medium> <high> ? */
7670 if (sscanf(cmd, "speed %d", &speed) != 1)
7671 return 0;
7673 *rc = fan_set_speed(speed);
7674 if (*rc == -ENXIO)
7675 printk(TPACPI_ERR "speed command accepted for unsupported "
7676 "access mode %d", fan_control_access_mode);
7677 else if (!*rc)
7678 tpacpi_disclose_usertask("procfs fan",
7679 "set speed to %d\n", speed);
7681 return 1;
7684 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7686 int interval;
7688 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7689 return 0;
7691 if (interval < 0 || interval > 120)
7692 *rc = -EINVAL;
7693 else {
7694 fan_watchdog_maxinterval = interval;
7695 tpacpi_disclose_usertask("procfs fan",
7696 "set watchdog timer to %d\n",
7697 interval);
7700 return 1;
7703 static int fan_write(char *buf)
7705 char *cmd;
7706 int rc = 0;
7708 while (!rc && (cmd = next_cmd(&buf))) {
7709 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7710 fan_write_cmd_level(cmd, &rc)) &&
7711 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7712 (fan_write_cmd_enable(cmd, &rc) ||
7713 fan_write_cmd_disable(cmd, &rc) ||
7714 fan_write_cmd_watchdog(cmd, &rc))) &&
7715 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7716 fan_write_cmd_speed(cmd, &rc))
7718 rc = -EINVAL;
7719 else if (!rc)
7720 fan_watchdog_reset();
7723 return rc;
7726 static struct ibm_struct fan_driver_data = {
7727 .name = "fan",
7728 .read = fan_read,
7729 .write = fan_write,
7730 .exit = fan_exit,
7731 .suspend = fan_suspend,
7732 .resume = fan_resume,
7735 /****************************************************************************
7736 ****************************************************************************
7738 * Infrastructure
7740 ****************************************************************************
7741 ****************************************************************************/
7744 * HKEY event callout for other subdrivers go here
7745 * (yes, it is ugly, but it is quick, safe, and gets the job done
7747 static void tpacpi_driver_event(const unsigned int hkey_event)
7749 if (ibm_backlight_device) {
7750 switch (hkey_event) {
7751 case TP_HKEY_EV_BRGHT_UP:
7752 case TP_HKEY_EV_BRGHT_DOWN:
7753 tpacpi_brightness_notify_change();
7760 static void hotkey_driver_event(const unsigned int scancode)
7762 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7765 /* sysfs name ---------------------------------------------------------- */
7766 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7767 struct device_attribute *attr,
7768 char *buf)
7770 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7773 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7774 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7776 /* --------------------------------------------------------------------- */
7778 /* /proc support */
7779 static struct proc_dir_entry *proc_dir;
7782 * Module and infrastructure proble, init and exit handling
7785 static int force_load;
7787 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7788 static const char * __init str_supported(int is_supported)
7790 static char text_unsupported[] __initdata = "not supported";
7792 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7794 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7796 static void ibm_exit(struct ibm_struct *ibm)
7798 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7800 list_del_init(&ibm->all_drivers);
7802 if (ibm->flags.acpi_notify_installed) {
7803 dbg_printk(TPACPI_DBG_EXIT,
7804 "%s: acpi_remove_notify_handler\n", ibm->name);
7805 BUG_ON(!ibm->acpi);
7806 acpi_remove_notify_handler(*ibm->acpi->handle,
7807 ibm->acpi->type,
7808 dispatch_acpi_notify);
7809 ibm->flags.acpi_notify_installed = 0;
7810 ibm->flags.acpi_notify_installed = 0;
7813 if (ibm->flags.proc_created) {
7814 dbg_printk(TPACPI_DBG_EXIT,
7815 "%s: remove_proc_entry\n", ibm->name);
7816 remove_proc_entry(ibm->name, proc_dir);
7817 ibm->flags.proc_created = 0;
7820 if (ibm->flags.acpi_driver_registered) {
7821 dbg_printk(TPACPI_DBG_EXIT,
7822 "%s: acpi_bus_unregister_driver\n", ibm->name);
7823 BUG_ON(!ibm->acpi);
7824 acpi_bus_unregister_driver(ibm->acpi->driver);
7825 kfree(ibm->acpi->driver);
7826 ibm->acpi->driver = NULL;
7827 ibm->flags.acpi_driver_registered = 0;
7830 if (ibm->flags.init_called && ibm->exit) {
7831 ibm->exit();
7832 ibm->flags.init_called = 0;
7835 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7838 static int __init ibm_init(struct ibm_init_struct *iibm)
7840 int ret;
7841 struct ibm_struct *ibm = iibm->data;
7842 struct proc_dir_entry *entry;
7844 BUG_ON(ibm == NULL);
7846 INIT_LIST_HEAD(&ibm->all_drivers);
7848 if (ibm->flags.experimental && !experimental)
7849 return 0;
7851 dbg_printk(TPACPI_DBG_INIT,
7852 "probing for %s\n", ibm->name);
7854 if (iibm->init) {
7855 ret = iibm->init(iibm);
7856 if (ret > 0)
7857 return 0; /* probe failed */
7858 if (ret)
7859 return ret;
7861 ibm->flags.init_called = 1;
7864 if (ibm->acpi) {
7865 if (ibm->acpi->hid) {
7866 ret = register_tpacpi_subdriver(ibm);
7867 if (ret)
7868 goto err_out;
7871 if (ibm->acpi->notify) {
7872 ret = setup_acpi_notify(ibm);
7873 if (ret == -ENODEV) {
7874 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7875 ibm->name);
7876 ret = 0;
7877 goto err_out;
7879 if (ret < 0)
7880 goto err_out;
7884 dbg_printk(TPACPI_DBG_INIT,
7885 "%s installed\n", ibm->name);
7887 if (ibm->read) {
7888 entry = create_proc_entry(ibm->name,
7889 S_IFREG | S_IRUGO | S_IWUSR,
7890 proc_dir);
7891 if (!entry) {
7892 printk(TPACPI_ERR "unable to create proc entry %s\n",
7893 ibm->name);
7894 ret = -ENODEV;
7895 goto err_out;
7897 entry->owner = THIS_MODULE;
7898 entry->data = ibm;
7899 entry->read_proc = &dispatch_procfs_read;
7900 if (ibm->write)
7901 entry->write_proc = &dispatch_procfs_write;
7902 ibm->flags.proc_created = 1;
7905 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7907 return 0;
7909 err_out:
7910 dbg_printk(TPACPI_DBG_INIT,
7911 "%s: at error exit path with result %d\n",
7912 ibm->name, ret);
7914 ibm_exit(ibm);
7915 return (ret < 0)? ret : 0;
7918 /* Probing */
7920 static bool __pure __init tpacpi_is_fw_digit(const char c)
7922 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7925 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7926 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7927 const char t)
7929 return s && strlen(s) >= 8 &&
7930 tpacpi_is_fw_digit(s[0]) &&
7931 tpacpi_is_fw_digit(s[1]) &&
7932 s[2] == t && s[3] == 'T' &&
7933 tpacpi_is_fw_digit(s[4]) &&
7934 tpacpi_is_fw_digit(s[5]) &&
7935 s[6] == 'W' && s[7] == 'W';
7938 /* returns 0 - probe ok, or < 0 - probe error.
7939 * Probe ok doesn't mean thinkpad found.
7940 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7941 static int __must_check __init get_thinkpad_model_data(
7942 struct thinkpad_id_data *tp)
7944 const struct dmi_device *dev = NULL;
7945 char ec_fw_string[18];
7946 char const *s;
7948 if (!tp)
7949 return -EINVAL;
7951 memset(tp, 0, sizeof(*tp));
7953 if (dmi_name_in_vendors("IBM"))
7954 tp->vendor = PCI_VENDOR_ID_IBM;
7955 else if (dmi_name_in_vendors("LENOVO"))
7956 tp->vendor = PCI_VENDOR_ID_LENOVO;
7957 else
7958 return 0;
7960 s = dmi_get_system_info(DMI_BIOS_VERSION);
7961 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7962 if (s && !tp->bios_version_str)
7963 return -ENOMEM;
7965 /* Really ancient ThinkPad 240X will fail this, which is fine */
7966 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7967 return 0;
7969 tp->bios_model = tp->bios_version_str[0]
7970 | (tp->bios_version_str[1] << 8);
7971 tp->bios_release = (tp->bios_version_str[4] << 8)
7972 | tp->bios_version_str[5];
7975 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7976 * X32 or newer, all Z series; Some models must have an
7977 * up-to-date BIOS or they will not be detected.
7979 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7981 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7982 if (sscanf(dev->name,
7983 "IBM ThinkPad Embedded Controller -[%17c",
7984 ec_fw_string) == 1) {
7985 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7986 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7988 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7989 if (!tp->ec_version_str)
7990 return -ENOMEM;
7992 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7993 tp->ec_model = ec_fw_string[0]
7994 | (ec_fw_string[1] << 8);
7995 tp->ec_release = (ec_fw_string[4] << 8)
7996 | ec_fw_string[5];
7997 } else {
7998 printk(TPACPI_NOTICE
7999 "ThinkPad firmware release %s "
8000 "doesn't match the known patterns\n",
8001 ec_fw_string);
8002 printk(TPACPI_NOTICE
8003 "please report this to %s\n",
8004 TPACPI_MAIL);
8006 break;
8010 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8011 if (s && !strnicmp(s, "ThinkPad", 8)) {
8012 tp->model_str = kstrdup(s, GFP_KERNEL);
8013 if (!tp->model_str)
8014 return -ENOMEM;
8017 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8018 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8019 if (s && !tp->nummodel_str)
8020 return -ENOMEM;
8022 return 0;
8025 static int __init probe_for_thinkpad(void)
8027 int is_thinkpad;
8029 if (acpi_disabled)
8030 return -ENODEV;
8033 * Non-ancient models have better DMI tagging, but very old models
8034 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8036 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8037 (thinkpad_id.ec_model != 0) ||
8038 tpacpi_is_fw_known();
8040 /* ec is required because many other handles are relative to it */
8041 TPACPI_ACPIHANDLE_INIT(ec);
8042 if (!ec_handle) {
8043 if (is_thinkpad)
8044 printk(TPACPI_ERR
8045 "Not yet supported ThinkPad detected!\n");
8046 return -ENODEV;
8049 if (!is_thinkpad && !force_load)
8050 return -ENODEV;
8052 return 0;
8056 /* Module init, exit, parameters */
8058 static struct ibm_init_struct ibms_init[] __initdata = {
8060 .init = thinkpad_acpi_driver_init,
8061 .data = &thinkpad_acpi_driver_data,
8064 .init = hotkey_init,
8065 .data = &hotkey_driver_data,
8068 .init = bluetooth_init,
8069 .data = &bluetooth_driver_data,
8072 .init = wan_init,
8073 .data = &wan_driver_data,
8076 .init = uwb_init,
8077 .data = &uwb_driver_data,
8079 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8081 .init = video_init,
8082 .data = &video_driver_data,
8084 #endif
8086 .init = light_init,
8087 .data = &light_driver_data,
8090 .init = cmos_init,
8091 .data = &cmos_driver_data,
8094 .init = led_init,
8095 .data = &led_driver_data,
8098 .init = beep_init,
8099 .data = &beep_driver_data,
8102 .init = thermal_init,
8103 .data = &thermal_driver_data,
8106 .data = &ecdump_driver_data,
8109 .init = brightness_init,
8110 .data = &brightness_driver_data,
8113 .data = &volume_driver_data,
8116 .init = fan_init,
8117 .data = &fan_driver_data,
8121 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8123 unsigned int i;
8124 struct ibm_struct *ibm;
8126 if (!kp || !kp->name || !val)
8127 return -EINVAL;
8129 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8130 ibm = ibms_init[i].data;
8131 WARN_ON(ibm == NULL);
8133 if (!ibm || !ibm->name)
8134 continue;
8136 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8137 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8138 return -ENOSPC;
8139 strcpy(ibms_init[i].param, val);
8140 strcat(ibms_init[i].param, ",");
8141 return 0;
8145 return -EINVAL;
8148 module_param(experimental, int, 0444);
8149 MODULE_PARM_DESC(experimental,
8150 "Enables experimental features when non-zero");
8152 module_param_named(debug, dbg_level, uint, 0);
8153 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8155 module_param(force_load, bool, 0444);
8156 MODULE_PARM_DESC(force_load,
8157 "Attempts to load the driver even on a "
8158 "mis-identified ThinkPad when true");
8160 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8161 MODULE_PARM_DESC(fan_control,
8162 "Enables setting fan parameters features when true");
8164 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8165 MODULE_PARM_DESC(brightness_mode,
8166 "Selects brightness control strategy: "
8167 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8169 module_param(brightness_enable, uint, 0444);
8170 MODULE_PARM_DESC(brightness_enable,
8171 "Enables backlight control when 1, disables when 0");
8173 module_param(hotkey_report_mode, uint, 0444);
8174 MODULE_PARM_DESC(hotkey_report_mode,
8175 "used for backwards compatibility with userspace, "
8176 "see documentation");
8178 #define TPACPI_PARAM(feature) \
8179 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8180 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8181 "at module load, see documentation")
8183 TPACPI_PARAM(hotkey);
8184 TPACPI_PARAM(bluetooth);
8185 TPACPI_PARAM(video);
8186 TPACPI_PARAM(light);
8187 TPACPI_PARAM(cmos);
8188 TPACPI_PARAM(led);
8189 TPACPI_PARAM(beep);
8190 TPACPI_PARAM(ecdump);
8191 TPACPI_PARAM(brightness);
8192 TPACPI_PARAM(volume);
8193 TPACPI_PARAM(fan);
8195 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8196 module_param(dbg_wlswemul, uint, 0444);
8197 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8198 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8199 MODULE_PARM_DESC(wlsw_state,
8200 "Initial state of the emulated WLSW switch");
8202 module_param(dbg_bluetoothemul, uint, 0444);
8203 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8204 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8205 MODULE_PARM_DESC(bluetooth_state,
8206 "Initial state of the emulated bluetooth switch");
8208 module_param(dbg_wwanemul, uint, 0444);
8209 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8210 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8211 MODULE_PARM_DESC(wwan_state,
8212 "Initial state of the emulated WWAN switch");
8214 module_param(dbg_uwbemul, uint, 0444);
8215 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8216 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8217 MODULE_PARM_DESC(uwb_state,
8218 "Initial state of the emulated UWB switch");
8219 #endif
8221 static void thinkpad_acpi_module_exit(void)
8223 struct ibm_struct *ibm, *itmp;
8225 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8227 list_for_each_entry_safe_reverse(ibm, itmp,
8228 &tpacpi_all_drivers,
8229 all_drivers) {
8230 ibm_exit(ibm);
8233 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8235 if (tpacpi_inputdev) {
8236 if (tp_features.input_device_registered)
8237 input_unregister_device(tpacpi_inputdev);
8238 else
8239 input_free_device(tpacpi_inputdev);
8242 if (tpacpi_hwmon)
8243 hwmon_device_unregister(tpacpi_hwmon);
8245 if (tp_features.sensors_pdev_attrs_registered)
8246 device_remove_file(&tpacpi_sensors_pdev->dev,
8247 &dev_attr_thinkpad_acpi_pdev_name);
8248 if (tpacpi_sensors_pdev)
8249 platform_device_unregister(tpacpi_sensors_pdev);
8250 if (tpacpi_pdev)
8251 platform_device_unregister(tpacpi_pdev);
8253 if (tp_features.sensors_pdrv_attrs_registered)
8254 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8255 if (tp_features.platform_drv_attrs_registered)
8256 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8258 if (tp_features.sensors_pdrv_registered)
8259 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8261 if (tp_features.platform_drv_registered)
8262 platform_driver_unregister(&tpacpi_pdriver);
8264 if (proc_dir)
8265 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8267 if (tpacpi_wq)
8268 destroy_workqueue(tpacpi_wq);
8270 kfree(thinkpad_id.bios_version_str);
8271 kfree(thinkpad_id.ec_version_str);
8272 kfree(thinkpad_id.model_str);
8276 static int __init thinkpad_acpi_module_init(void)
8278 int ret, i;
8280 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8282 /* Parameter checking */
8283 if (hotkey_report_mode > 2)
8284 return -EINVAL;
8286 /* Driver-level probe */
8288 ret = get_thinkpad_model_data(&thinkpad_id);
8289 if (ret) {
8290 printk(TPACPI_ERR
8291 "unable to get DMI data: %d\n", ret);
8292 thinkpad_acpi_module_exit();
8293 return ret;
8295 ret = probe_for_thinkpad();
8296 if (ret) {
8297 thinkpad_acpi_module_exit();
8298 return ret;
8301 /* Driver initialization */
8303 TPACPI_ACPIHANDLE_INIT(ecrd);
8304 TPACPI_ACPIHANDLE_INIT(ecwr);
8306 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8307 if (!tpacpi_wq) {
8308 thinkpad_acpi_module_exit();
8309 return -ENOMEM;
8312 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8313 if (!proc_dir) {
8314 printk(TPACPI_ERR
8315 "unable to create proc dir " TPACPI_PROC_DIR);
8316 thinkpad_acpi_module_exit();
8317 return -ENODEV;
8319 proc_dir->owner = THIS_MODULE;
8321 ret = platform_driver_register(&tpacpi_pdriver);
8322 if (ret) {
8323 printk(TPACPI_ERR
8324 "unable to register main platform driver\n");
8325 thinkpad_acpi_module_exit();
8326 return ret;
8328 tp_features.platform_drv_registered = 1;
8330 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8331 if (ret) {
8332 printk(TPACPI_ERR
8333 "unable to register hwmon platform driver\n");
8334 thinkpad_acpi_module_exit();
8335 return ret;
8337 tp_features.sensors_pdrv_registered = 1;
8339 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8340 if (!ret) {
8341 tp_features.platform_drv_attrs_registered = 1;
8342 ret = tpacpi_create_driver_attributes(
8343 &tpacpi_hwmon_pdriver.driver);
8345 if (ret) {
8346 printk(TPACPI_ERR
8347 "unable to create sysfs driver attributes\n");
8348 thinkpad_acpi_module_exit();
8349 return ret;
8351 tp_features.sensors_pdrv_attrs_registered = 1;
8354 /* Device initialization */
8355 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8356 NULL, 0);
8357 if (IS_ERR(tpacpi_pdev)) {
8358 ret = PTR_ERR(tpacpi_pdev);
8359 tpacpi_pdev = NULL;
8360 printk(TPACPI_ERR "unable to register platform device\n");
8361 thinkpad_acpi_module_exit();
8362 return ret;
8364 tpacpi_sensors_pdev = platform_device_register_simple(
8365 TPACPI_HWMON_DRVR_NAME,
8366 -1, NULL, 0);
8367 if (IS_ERR(tpacpi_sensors_pdev)) {
8368 ret = PTR_ERR(tpacpi_sensors_pdev);
8369 tpacpi_sensors_pdev = NULL;
8370 printk(TPACPI_ERR
8371 "unable to register hwmon platform device\n");
8372 thinkpad_acpi_module_exit();
8373 return ret;
8375 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8376 &dev_attr_thinkpad_acpi_pdev_name);
8377 if (ret) {
8378 printk(TPACPI_ERR
8379 "unable to create sysfs hwmon device attributes\n");
8380 thinkpad_acpi_module_exit();
8381 return ret;
8383 tp_features.sensors_pdev_attrs_registered = 1;
8384 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8385 if (IS_ERR(tpacpi_hwmon)) {
8386 ret = PTR_ERR(tpacpi_hwmon);
8387 tpacpi_hwmon = NULL;
8388 printk(TPACPI_ERR "unable to register hwmon device\n");
8389 thinkpad_acpi_module_exit();
8390 return ret;
8392 mutex_init(&tpacpi_inputdev_send_mutex);
8393 tpacpi_inputdev = input_allocate_device();
8394 if (!tpacpi_inputdev) {
8395 printk(TPACPI_ERR "unable to allocate input device\n");
8396 thinkpad_acpi_module_exit();
8397 return -ENOMEM;
8398 } else {
8399 /* Prepare input device, but don't register */
8400 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8401 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8402 tpacpi_inputdev->id.bustype = BUS_HOST;
8403 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8404 thinkpad_id.vendor :
8405 PCI_VENDOR_ID_IBM;
8406 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8407 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8408 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8410 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8411 ret = ibm_init(&ibms_init[i]);
8412 if (ret >= 0 && *ibms_init[i].param)
8413 ret = ibms_init[i].data->write(ibms_init[i].param);
8414 if (ret < 0) {
8415 thinkpad_acpi_module_exit();
8416 return ret;
8419 ret = input_register_device(tpacpi_inputdev);
8420 if (ret < 0) {
8421 printk(TPACPI_ERR "unable to register input device\n");
8422 thinkpad_acpi_module_exit();
8423 return ret;
8424 } else {
8425 tp_features.input_device_registered = 1;
8428 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8429 return 0;
8432 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8435 * This will autoload the driver in almost every ThinkPad
8436 * in widespread use.
8438 * Only _VERY_ old models, like the 240, 240x and 570 lack
8439 * the HKEY event interface.
8441 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8444 * DMI matching for module autoloading
8446 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8447 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8449 * Only models listed in thinkwiki will be supported, so add yours
8450 * if it is not there yet.
8452 #define IBM_BIOS_MODULE_ALIAS(__type) \
8453 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8455 /* Ancient thinkpad BIOSes have to be identified by
8456 * BIOS type or model number, and there are far less
8457 * BIOS types than model numbers... */
8458 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8460 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8461 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8462 MODULE_DESCRIPTION(TPACPI_DESC);
8463 MODULE_VERSION(TPACPI_VERSION);
8464 MODULE_LICENSE("GPL");
8466 module_init(thinkpad_acpi_module_init);
8467 module_exit(thinkpad_acpi_module_exit);