thinkpad-acpi: name event constants
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob51c209c18532e76f51df2bafd104984ac9623494
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 0x020500
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 __eid1, __eid2, __ev1, __ev2) \
1420 { .vendor = (__v), \
1421 .bios = TPID(__bid1, __bid2), \
1422 .ec = TPID(__eid1, __eid2), \
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 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1430 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1431 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1433 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1434 __eid1, __eid2, __ev1, __ev2) \
1435 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1436 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1438 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1439 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1441 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1442 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1443 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1445 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1446 __eid1, __eid2, __ev1, __ev2) \
1447 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1448 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1450 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1451 /* Numeric models ------------------ */
1452 /* FW MODEL BIOS VERS */
1453 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1454 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1455 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1456 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1457 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1458 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1459 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1460 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1461 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1463 /* A-series ------------------------- */
1464 /* FW MODEL BIOS VERS EC VERS */
1465 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1466 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1467 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1468 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1469 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1470 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1471 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1472 TPV_QI0('1', '3', '2', '0'), /* A22m */
1473 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1474 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1475 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1477 /* G-series ------------------------- */
1478 /* FW MODEL BIOS VERS */
1479 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1480 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1482 /* R-series, T-series --------------- */
1483 /* FW MODEL BIOS VERS EC VERS */
1484 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1485 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1486 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1487 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1488 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1489 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1490 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1491 T40/p, T41/p, T42/p (1) */
1492 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1493 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1494 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1495 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1497 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1498 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1499 TPV_QI0('1', '6', '3', '2'), /* T22 */
1500 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1501 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1502 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1504 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1505 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1506 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1508 /* BIOS FW BIOS VERS EC FW EC VERS */
1509 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1510 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1512 /* X-series ------------------------- */
1513 /* FW MODEL BIOS VERS EC VERS */
1514 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1515 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1516 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1517 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1518 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1519 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1520 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1522 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1523 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1525 /* (0) - older versions lack DMI EC fw string and functionality */
1526 /* (1) - older versions known to lack functionality */
1529 #undef TPV_QL1
1530 #undef TPV_QL0
1531 #undef TPV_QI2
1532 #undef TPV_QI1
1533 #undef TPV_QI0
1534 #undef TPV_Q_X
1535 #undef TPV_Q
1537 static void __init tpacpi_check_outdated_fw(void)
1539 unsigned long fwvers;
1540 u16 ec_version, bios_version;
1542 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1543 ARRAY_SIZE(tpacpi_bios_version_qtable));
1545 if (!fwvers)
1546 return;
1548 bios_version = fwvers & 0xffffU;
1549 ec_version = (fwvers >> 16) & 0xffffU;
1551 /* note that unknown versions are set to 0x0000 and we use that */
1552 if ((bios_version > thinkpad_id.bios_release) ||
1553 (ec_version > thinkpad_id.ec_release &&
1554 ec_version != TPACPI_MATCH_ANY)) {
1556 * The changelogs would let us track down the exact
1557 * reason, but it is just too much of a pain to track
1558 * it. We only list BIOSes that are either really
1559 * broken, or really stable to begin with, so it is
1560 * best if the user upgrades the firmware anyway.
1562 printk(TPACPI_WARN
1563 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1564 printk(TPACPI_WARN
1565 "WARNING: This firmware may be missing critical bug "
1566 "fixes and/or important features\n");
1570 static bool __init tpacpi_is_fw_known(void)
1572 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1573 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1576 /****************************************************************************
1577 ****************************************************************************
1579 * Subdrivers
1581 ****************************************************************************
1582 ****************************************************************************/
1584 /*************************************************************************
1585 * thinkpad-acpi init subdriver
1588 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1590 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1591 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1593 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1594 (thinkpad_id.bios_version_str) ?
1595 thinkpad_id.bios_version_str : "unknown",
1596 (thinkpad_id.ec_version_str) ?
1597 thinkpad_id.ec_version_str : "unknown");
1599 if (thinkpad_id.vendor && thinkpad_id.model_str)
1600 printk(TPACPI_INFO "%s %s, model %s\n",
1601 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1602 "IBM" : ((thinkpad_id.vendor ==
1603 PCI_VENDOR_ID_LENOVO) ?
1604 "Lenovo" : "Unknown vendor"),
1605 thinkpad_id.model_str,
1606 (thinkpad_id.nummodel_str) ?
1607 thinkpad_id.nummodel_str : "unknown");
1609 tpacpi_check_outdated_fw();
1610 return 0;
1613 static int thinkpad_acpi_driver_read(char *p)
1615 int len = 0;
1617 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1618 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1620 return len;
1623 static struct ibm_struct thinkpad_acpi_driver_data = {
1624 .name = "driver",
1625 .read = thinkpad_acpi_driver_read,
1628 /*************************************************************************
1629 * Hotkey subdriver
1633 * ThinkPad firmware event model
1635 * The ThinkPad firmware has two main event interfaces: normal ACPI
1636 * notifications (which follow the ACPI standard), and a private event
1637 * interface.
1639 * The private event interface also issues events for the hotkeys. As
1640 * the driver gained features, the event handling code ended up being
1641 * built around the hotkey subdriver. This will need to be refactored
1642 * to a more formal event API eventually.
1644 * Some "hotkeys" are actually supposed to be used as event reports,
1645 * such as "brightness has changed", "volume has changed", depending on
1646 * the ThinkPad model and how the firmware is operating.
1648 * Unlike other classes, hotkey-class events have mask/unmask control on
1649 * non-ancient firmware. However, how it behaves changes a lot with the
1650 * firmware model and version.
1653 enum { /* hot key scan codes (derived from ACPI DSDT) */
1654 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1655 TP_ACPI_HOTKEYSCAN_FNF2,
1656 TP_ACPI_HOTKEYSCAN_FNF3,
1657 TP_ACPI_HOTKEYSCAN_FNF4,
1658 TP_ACPI_HOTKEYSCAN_FNF5,
1659 TP_ACPI_HOTKEYSCAN_FNF6,
1660 TP_ACPI_HOTKEYSCAN_FNF7,
1661 TP_ACPI_HOTKEYSCAN_FNF8,
1662 TP_ACPI_HOTKEYSCAN_FNF9,
1663 TP_ACPI_HOTKEYSCAN_FNF10,
1664 TP_ACPI_HOTKEYSCAN_FNF11,
1665 TP_ACPI_HOTKEYSCAN_FNF12,
1666 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1667 TP_ACPI_HOTKEYSCAN_FNINSERT,
1668 TP_ACPI_HOTKEYSCAN_FNDELETE,
1669 TP_ACPI_HOTKEYSCAN_FNHOME,
1670 TP_ACPI_HOTKEYSCAN_FNEND,
1671 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1672 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1673 TP_ACPI_HOTKEYSCAN_FNSPACE,
1674 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1675 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1676 TP_ACPI_HOTKEYSCAN_MUTE,
1677 TP_ACPI_HOTKEYSCAN_THINKPAD,
1680 enum { /* Keys/events available through NVRAM polling */
1681 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1682 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1685 enum { /* Positions of some of the keys in hotkey masks */
1686 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1687 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1688 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1689 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1690 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1691 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1692 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1693 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1694 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1695 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1696 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1699 enum { /* NVRAM to ACPI HKEY group map */
1700 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1701 TP_ACPI_HKEY_ZOOM_MASK |
1702 TP_ACPI_HKEY_DISPSWTCH_MASK |
1703 TP_ACPI_HKEY_HIBERNATE_MASK,
1704 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1705 TP_ACPI_HKEY_BRGHTDWN_MASK,
1706 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1707 TP_ACPI_HKEY_VOLDWN_MASK |
1708 TP_ACPI_HKEY_MUTE_MASK,
1711 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1712 struct tp_nvram_state {
1713 u16 thinkpad_toggle:1;
1714 u16 zoom_toggle:1;
1715 u16 display_toggle:1;
1716 u16 thinklight_toggle:1;
1717 u16 hibernate_toggle:1;
1718 u16 displayexp_toggle:1;
1719 u16 display_state:1;
1720 u16 brightness_toggle:1;
1721 u16 volume_toggle:1;
1722 u16 mute:1;
1724 u8 brightness_level;
1725 u8 volume_level;
1728 /* kthread for the hotkey poller */
1729 static struct task_struct *tpacpi_hotkey_task;
1731 /* Acquired while the poller kthread is running, use to sync start/stop */
1732 static struct mutex hotkey_thread_mutex;
1735 * Acquire mutex to write poller control variables as an
1736 * atomic block.
1738 * Increment hotkey_config_change when changing them if you
1739 * want the kthread to forget old state.
1741 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1743 static struct mutex hotkey_thread_data_mutex;
1744 static unsigned int hotkey_config_change;
1747 * hotkey poller control variables
1749 * Must be atomic or readers will also need to acquire mutex
1751 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1752 * should be used only when the changes need to be taken as
1753 * a block, OR when one needs to force the kthread to forget
1754 * old state.
1756 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1757 static unsigned int hotkey_poll_freq = 10; /* Hz */
1759 #define HOTKEY_CONFIG_CRITICAL_START \
1760 do { \
1761 mutex_lock(&hotkey_thread_data_mutex); \
1762 hotkey_config_change++; \
1763 } while (0);
1764 #define HOTKEY_CONFIG_CRITICAL_END \
1765 mutex_unlock(&hotkey_thread_data_mutex);
1767 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1769 #define hotkey_source_mask 0U
1770 #define HOTKEY_CONFIG_CRITICAL_START
1771 #define HOTKEY_CONFIG_CRITICAL_END
1773 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1775 static struct mutex hotkey_mutex;
1777 static enum { /* Reasons for waking up */
1778 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1779 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1780 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1781 } hotkey_wakeup_reason;
1783 static int hotkey_autosleep_ack;
1785 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1786 static u32 hotkey_all_mask; /* all events supported in fw */
1787 static u32 hotkey_reserved_mask; /* events better left disabled */
1788 static u32 hotkey_driver_mask; /* events needed by the driver */
1789 static u32 hotkey_user_mask; /* events visible to userspace */
1790 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1792 static unsigned int hotkey_report_mode;
1794 static u16 *hotkey_keycode_map;
1796 static struct attribute_set *hotkey_dev_attributes;
1798 static void tpacpi_driver_event(const unsigned int hkey_event);
1799 static void hotkey_driver_event(const unsigned int scancode);
1801 /* HKEY.MHKG() return bits */
1802 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1804 static int hotkey_get_wlsw(int *status)
1806 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1807 if (dbg_wlswemul) {
1808 *status = !!tpacpi_wlsw_emulstate;
1809 return 0;
1811 #endif
1812 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1813 return -EIO;
1814 return 0;
1817 static int hotkey_get_tablet_mode(int *status)
1819 int s;
1821 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1822 return -EIO;
1824 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1825 return 0;
1829 * Reads current event mask from firmware, and updates
1830 * hotkey_acpi_mask accordingly. Also resets any bits
1831 * from hotkey_user_mask that are unavailable to be
1832 * delivered (shadow requirement of the userspace ABI).
1834 * Call with hotkey_mutex held
1836 static int hotkey_mask_get(void)
1838 if (tp_features.hotkey_mask) {
1839 u32 m = 0;
1841 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1842 return -EIO;
1844 hotkey_acpi_mask = m;
1845 } else {
1846 /* no mask support doesn't mean no event support... */
1847 hotkey_acpi_mask = hotkey_all_mask;
1850 /* sync userspace-visible mask */
1851 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1853 return 0;
1856 void static hotkey_mask_warn_incomplete_mask(void)
1858 /* log only what the user can fix... */
1859 const u32 wantedmask = hotkey_driver_mask &
1860 ~(hotkey_acpi_mask | hotkey_source_mask) &
1861 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1863 if (wantedmask)
1864 printk(TPACPI_NOTICE
1865 "required events 0x%08x not enabled!\n",
1866 wantedmask);
1870 * Set the firmware mask when supported
1872 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1874 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1876 * Call with hotkey_mutex held
1878 static int hotkey_mask_set(u32 mask)
1880 int i;
1881 int rc = 0;
1883 const u32 fwmask = mask & ~hotkey_source_mask;
1885 if (tp_features.hotkey_mask) {
1886 for (i = 0; i < 32; i++) {
1887 if (!acpi_evalf(hkey_handle,
1888 NULL, "MHKM", "vdd", i + 1,
1889 !!(mask & (1 << i)))) {
1890 rc = -EIO;
1891 break;
1897 * We *must* make an inconditional call to hotkey_mask_get to
1898 * refresh hotkey_acpi_mask and update hotkey_user_mask
1900 * Take the opportunity to also log when we cannot _enable_
1901 * a given event.
1903 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1904 printk(TPACPI_NOTICE
1905 "asked for hotkey mask 0x%08x, but "
1906 "firmware forced it to 0x%08x\n",
1907 fwmask, hotkey_acpi_mask);
1910 hotkey_mask_warn_incomplete_mask();
1912 return rc;
1916 * Sets hotkey_user_mask and tries to set the firmware mask
1918 * Call with hotkey_mutex held
1920 static int hotkey_user_mask_set(const u32 mask)
1922 int rc;
1924 /* Give people a chance to notice they are doing something that
1925 * is bound to go boom on their users sooner or later */
1926 if (!tp_warned.hotkey_mask_ff &&
1927 (mask == 0xffff || mask == 0xffffff ||
1928 mask == 0xffffffff)) {
1929 tp_warned.hotkey_mask_ff = 1;
1930 printk(TPACPI_NOTICE
1931 "setting the hotkey mask to 0x%08x is likely "
1932 "not the best way to go about it\n", mask);
1933 printk(TPACPI_NOTICE
1934 "please consider using the driver defaults, "
1935 "and refer to up-to-date thinkpad-acpi "
1936 "documentation\n");
1939 /* Try to enable what the user asked for, plus whatever we need.
1940 * this syncs everything but won't enable bits in hotkey_user_mask */
1941 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1943 /* Enable the available bits in hotkey_user_mask */
1944 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1946 return rc;
1950 * Sets the driver hotkey mask.
1952 * Can be called even if the hotkey subdriver is inactive
1954 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1956 int rc;
1958 /* Do the right thing if hotkey_init has not been called yet */
1959 if (!tp_features.hotkey) {
1960 hotkey_driver_mask = mask;
1961 return 0;
1964 mutex_lock(&hotkey_mutex);
1966 HOTKEY_CONFIG_CRITICAL_START
1967 hotkey_driver_mask = mask;
1968 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1969 HOTKEY_CONFIG_CRITICAL_END
1971 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1972 ~hotkey_source_mask);
1973 mutex_unlock(&hotkey_mutex);
1975 return rc;
1978 static int hotkey_status_get(int *status)
1980 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1981 return -EIO;
1983 return 0;
1986 static int hotkey_status_set(bool enable)
1988 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1989 return -EIO;
1991 return 0;
1994 static void tpacpi_input_send_tabletsw(void)
1996 int state;
1998 if (tp_features.hotkey_tablet &&
1999 !hotkey_get_tablet_mode(&state)) {
2000 mutex_lock(&tpacpi_inputdev_send_mutex);
2002 input_report_switch(tpacpi_inputdev,
2003 SW_TABLET_MODE, !!state);
2004 input_sync(tpacpi_inputdev);
2006 mutex_unlock(&tpacpi_inputdev_send_mutex);
2010 /* Do NOT call without validating scancode first */
2011 static void tpacpi_input_send_key(const unsigned int scancode)
2013 const unsigned int keycode = hotkey_keycode_map[scancode];
2015 if (keycode != KEY_RESERVED) {
2016 mutex_lock(&tpacpi_inputdev_send_mutex);
2018 input_report_key(tpacpi_inputdev, keycode, 1);
2019 if (keycode == KEY_UNKNOWN)
2020 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2021 scancode);
2022 input_sync(tpacpi_inputdev);
2024 input_report_key(tpacpi_inputdev, keycode, 0);
2025 if (keycode == KEY_UNKNOWN)
2026 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2027 scancode);
2028 input_sync(tpacpi_inputdev);
2030 mutex_unlock(&tpacpi_inputdev_send_mutex);
2034 /* Do NOT call without validating scancode first */
2035 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2037 hotkey_driver_event(scancode);
2038 if (hotkey_user_mask & (1 << scancode))
2039 tpacpi_input_send_key(scancode);
2042 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2043 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2045 /* Do NOT call without validating scancode first */
2046 static void tpacpi_hotkey_send_key(unsigned int scancode)
2048 tpacpi_input_send_key_masked(scancode);
2049 if (hotkey_report_mode < 2) {
2050 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2051 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2055 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2057 u8 d;
2059 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2060 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2061 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2062 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2063 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2064 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2066 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2067 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2068 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2070 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2071 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2072 n->displayexp_toggle =
2073 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2075 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2076 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2077 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2078 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2079 n->brightness_toggle =
2080 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2082 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2083 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2084 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2085 >> TP_NVRAM_POS_LEVEL_VOLUME;
2086 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2087 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2091 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2092 struct tp_nvram_state *newn,
2093 const u32 event_mask)
2096 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2097 do { \
2098 if ((event_mask & (1 << __scancode)) && \
2099 oldn->__member != newn->__member) \
2100 tpacpi_hotkey_send_key(__scancode); \
2101 } while (0)
2103 #define TPACPI_MAY_SEND_KEY(__scancode) \
2104 do { \
2105 if (event_mask & (1 << __scancode)) \
2106 tpacpi_hotkey_send_key(__scancode); \
2107 } while (0)
2109 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2110 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2111 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2112 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2114 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2116 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2118 /* handle volume */
2119 if (oldn->volume_toggle != newn->volume_toggle) {
2120 if (oldn->mute != newn->mute) {
2121 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2123 if (oldn->volume_level > newn->volume_level) {
2124 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2125 } else if (oldn->volume_level < newn->volume_level) {
2126 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2127 } else if (oldn->mute == newn->mute) {
2128 /* repeated key presses that didn't change state */
2129 if (newn->mute) {
2130 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2131 } else if (newn->volume_level != 0) {
2132 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2133 } else {
2134 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2139 /* handle brightness */
2140 if (oldn->brightness_toggle != newn->brightness_toggle) {
2141 if (oldn->brightness_level < newn->brightness_level) {
2142 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2143 } else if (oldn->brightness_level > newn->brightness_level) {
2144 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2145 } else {
2146 /* repeated key presses that didn't change state */
2147 if (newn->brightness_level != 0) {
2148 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2149 } else {
2150 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2155 #undef TPACPI_COMPARE_KEY
2156 #undef TPACPI_MAY_SEND_KEY
2160 * Polling driver
2162 * We track all events in hotkey_source_mask all the time, since
2163 * most of them are edge-based. We only issue those requested by
2164 * hotkey_user_mask or hotkey_driver_mask, though.
2166 static int hotkey_kthread(void *data)
2168 struct tp_nvram_state s[2];
2169 u32 poll_mask, event_mask;
2170 unsigned int si, so;
2171 unsigned long t;
2172 unsigned int change_detector, must_reset;
2173 unsigned int poll_freq;
2175 mutex_lock(&hotkey_thread_mutex);
2177 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2178 goto exit;
2180 set_freezable();
2182 so = 0;
2183 si = 1;
2184 t = 0;
2186 /* Initial state for compares */
2187 mutex_lock(&hotkey_thread_data_mutex);
2188 change_detector = hotkey_config_change;
2189 poll_mask = hotkey_source_mask;
2190 event_mask = hotkey_source_mask &
2191 (hotkey_driver_mask | hotkey_user_mask);
2192 poll_freq = hotkey_poll_freq;
2193 mutex_unlock(&hotkey_thread_data_mutex);
2194 hotkey_read_nvram(&s[so], poll_mask);
2196 while (!kthread_should_stop()) {
2197 if (t == 0) {
2198 if (likely(poll_freq))
2199 t = 1000/poll_freq;
2200 else
2201 t = 100; /* should never happen... */
2203 t = msleep_interruptible(t);
2204 if (unlikely(kthread_should_stop()))
2205 break;
2206 must_reset = try_to_freeze();
2207 if (t > 0 && !must_reset)
2208 continue;
2210 mutex_lock(&hotkey_thread_data_mutex);
2211 if (must_reset || hotkey_config_change != change_detector) {
2212 /* forget old state on thaw or config change */
2213 si = so;
2214 t = 0;
2215 change_detector = hotkey_config_change;
2217 poll_mask = hotkey_source_mask;
2218 event_mask = hotkey_source_mask &
2219 (hotkey_driver_mask | hotkey_user_mask);
2220 poll_freq = hotkey_poll_freq;
2221 mutex_unlock(&hotkey_thread_data_mutex);
2223 if (likely(poll_mask)) {
2224 hotkey_read_nvram(&s[si], poll_mask);
2225 if (likely(si != so)) {
2226 hotkey_compare_and_issue_event(&s[so], &s[si],
2227 event_mask);
2231 so = si;
2232 si ^= 1;
2235 exit:
2236 mutex_unlock(&hotkey_thread_mutex);
2237 return 0;
2240 /* call with hotkey_mutex held */
2241 static void hotkey_poll_stop_sync(void)
2243 if (tpacpi_hotkey_task) {
2244 if (frozen(tpacpi_hotkey_task) ||
2245 freezing(tpacpi_hotkey_task))
2246 thaw_process(tpacpi_hotkey_task);
2248 kthread_stop(tpacpi_hotkey_task);
2249 tpacpi_hotkey_task = NULL;
2250 mutex_lock(&hotkey_thread_mutex);
2251 /* at this point, the thread did exit */
2252 mutex_unlock(&hotkey_thread_mutex);
2256 /* call with hotkey_mutex held */
2257 static void hotkey_poll_setup(bool may_warn)
2259 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2260 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2262 if (hotkey_poll_freq > 0 &&
2263 (poll_driver_mask ||
2264 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2265 if (!tpacpi_hotkey_task) {
2266 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2267 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2268 if (IS_ERR(tpacpi_hotkey_task)) {
2269 tpacpi_hotkey_task = NULL;
2270 printk(TPACPI_ERR
2271 "could not create kernel thread "
2272 "for hotkey polling\n");
2275 } else {
2276 hotkey_poll_stop_sync();
2277 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2278 hotkey_poll_freq == 0) {
2279 printk(TPACPI_NOTICE
2280 "hot keys 0x%08x and/or events 0x%08x "
2281 "require polling, which is currently "
2282 "disabled\n",
2283 poll_user_mask, poll_driver_mask);
2288 static void hotkey_poll_setup_safe(bool may_warn)
2290 mutex_lock(&hotkey_mutex);
2291 hotkey_poll_setup(may_warn);
2292 mutex_unlock(&hotkey_mutex);
2295 /* call with hotkey_mutex held */
2296 static void hotkey_poll_set_freq(unsigned int freq)
2298 if (!freq)
2299 hotkey_poll_stop_sync();
2301 hotkey_poll_freq = freq;
2304 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2306 static void hotkey_poll_setup_safe(bool __unused)
2310 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2312 static int hotkey_inputdev_open(struct input_dev *dev)
2314 switch (tpacpi_lifecycle) {
2315 case TPACPI_LIFE_INIT:
2317 * hotkey_init will call hotkey_poll_setup_safe
2318 * at the appropriate moment
2320 return 0;
2321 case TPACPI_LIFE_EXITING:
2322 return -EBUSY;
2323 case TPACPI_LIFE_RUNNING:
2324 hotkey_poll_setup_safe(false);
2325 return 0;
2328 /* Should only happen if tpacpi_lifecycle is corrupt */
2329 BUG();
2330 return -EBUSY;
2333 static void hotkey_inputdev_close(struct input_dev *dev)
2335 /* disable hotkey polling when possible */
2336 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2337 !(hotkey_source_mask & hotkey_driver_mask))
2338 hotkey_poll_setup_safe(false);
2341 /* sysfs hotkey enable ------------------------------------------------- */
2342 static ssize_t hotkey_enable_show(struct device *dev,
2343 struct device_attribute *attr,
2344 char *buf)
2346 int res, status;
2348 printk_deprecated_attribute("hotkey_enable",
2349 "Hotkey reporting is always enabled");
2351 res = hotkey_status_get(&status);
2352 if (res)
2353 return res;
2355 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2358 static ssize_t hotkey_enable_store(struct device *dev,
2359 struct device_attribute *attr,
2360 const char *buf, size_t count)
2362 unsigned long t;
2364 printk_deprecated_attribute("hotkey_enable",
2365 "Hotkeys can be disabled through hotkey_mask");
2367 if (parse_strtoul(buf, 1, &t))
2368 return -EINVAL;
2370 if (t == 0)
2371 return -EPERM;
2373 return count;
2376 static struct device_attribute dev_attr_hotkey_enable =
2377 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2378 hotkey_enable_show, hotkey_enable_store);
2380 /* sysfs hotkey mask --------------------------------------------------- */
2381 static ssize_t hotkey_mask_show(struct device *dev,
2382 struct device_attribute *attr,
2383 char *buf)
2385 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2388 static ssize_t hotkey_mask_store(struct device *dev,
2389 struct device_attribute *attr,
2390 const char *buf, size_t count)
2392 unsigned long t;
2393 int res;
2395 if (parse_strtoul(buf, 0xffffffffUL, &t))
2396 return -EINVAL;
2398 if (mutex_lock_killable(&hotkey_mutex))
2399 return -ERESTARTSYS;
2401 res = hotkey_user_mask_set(t);
2403 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2404 hotkey_poll_setup(true);
2405 #endif
2407 mutex_unlock(&hotkey_mutex);
2409 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2411 return (res) ? res : count;
2414 static struct device_attribute dev_attr_hotkey_mask =
2415 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2416 hotkey_mask_show, hotkey_mask_store);
2418 /* sysfs hotkey bios_enabled ------------------------------------------- */
2419 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2420 struct device_attribute *attr,
2421 char *buf)
2423 return sprintf(buf, "0\n");
2426 static struct device_attribute dev_attr_hotkey_bios_enabled =
2427 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2429 /* sysfs hotkey bios_mask ---------------------------------------------- */
2430 static ssize_t hotkey_bios_mask_show(struct device *dev,
2431 struct device_attribute *attr,
2432 char *buf)
2434 printk_deprecated_attribute("hotkey_bios_mask",
2435 "This attribute is useless.");
2436 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2439 static struct device_attribute dev_attr_hotkey_bios_mask =
2440 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2442 /* sysfs hotkey all_mask ----------------------------------------------- */
2443 static ssize_t hotkey_all_mask_show(struct device *dev,
2444 struct device_attribute *attr,
2445 char *buf)
2447 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2448 hotkey_all_mask | hotkey_source_mask);
2451 static struct device_attribute dev_attr_hotkey_all_mask =
2452 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2454 /* sysfs hotkey recommended_mask --------------------------------------- */
2455 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2456 struct device_attribute *attr,
2457 char *buf)
2459 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2460 (hotkey_all_mask | hotkey_source_mask)
2461 & ~hotkey_reserved_mask);
2464 static struct device_attribute dev_attr_hotkey_recommended_mask =
2465 __ATTR(hotkey_recommended_mask, S_IRUGO,
2466 hotkey_recommended_mask_show, NULL);
2468 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2470 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2471 static ssize_t hotkey_source_mask_show(struct device *dev,
2472 struct device_attribute *attr,
2473 char *buf)
2475 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2478 static ssize_t hotkey_source_mask_store(struct device *dev,
2479 struct device_attribute *attr,
2480 const char *buf, size_t count)
2482 unsigned long t;
2483 u32 r_ev;
2484 int rc;
2486 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2487 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2488 return -EINVAL;
2490 if (mutex_lock_killable(&hotkey_mutex))
2491 return -ERESTARTSYS;
2493 HOTKEY_CONFIG_CRITICAL_START
2494 hotkey_source_mask = t;
2495 HOTKEY_CONFIG_CRITICAL_END
2497 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2498 ~hotkey_source_mask);
2499 hotkey_poll_setup(true);
2501 /* check if events needed by the driver got disabled */
2502 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2503 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2505 mutex_unlock(&hotkey_mutex);
2507 if (rc < 0)
2508 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2509 "firmware event mask!\n");
2511 if (r_ev)
2512 printk(TPACPI_NOTICE "hotkey_source_mask: "
2513 "some important events were disabled: "
2514 "0x%04x\n", r_ev);
2516 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2518 return (rc < 0) ? rc : count;
2521 static struct device_attribute dev_attr_hotkey_source_mask =
2522 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2523 hotkey_source_mask_show, hotkey_source_mask_store);
2525 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2526 static ssize_t hotkey_poll_freq_show(struct device *dev,
2527 struct device_attribute *attr,
2528 char *buf)
2530 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2533 static ssize_t hotkey_poll_freq_store(struct device *dev,
2534 struct device_attribute *attr,
2535 const char *buf, size_t count)
2537 unsigned long t;
2539 if (parse_strtoul(buf, 25, &t))
2540 return -EINVAL;
2542 if (mutex_lock_killable(&hotkey_mutex))
2543 return -ERESTARTSYS;
2545 hotkey_poll_set_freq(t);
2546 hotkey_poll_setup(true);
2548 mutex_unlock(&hotkey_mutex);
2550 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2552 return count;
2555 static struct device_attribute dev_attr_hotkey_poll_freq =
2556 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2557 hotkey_poll_freq_show, hotkey_poll_freq_store);
2559 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2561 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2562 static ssize_t hotkey_radio_sw_show(struct device *dev,
2563 struct device_attribute *attr,
2564 char *buf)
2566 int res, s;
2567 res = hotkey_get_wlsw(&s);
2568 if (res < 0)
2569 return res;
2571 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2574 static struct device_attribute dev_attr_hotkey_radio_sw =
2575 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2577 static void hotkey_radio_sw_notify_change(void)
2579 if (tp_features.hotkey_wlsw)
2580 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2581 "hotkey_radio_sw");
2584 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2585 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2586 struct device_attribute *attr,
2587 char *buf)
2589 int res, s;
2590 res = hotkey_get_tablet_mode(&s);
2591 if (res < 0)
2592 return res;
2594 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2597 static struct device_attribute dev_attr_hotkey_tablet_mode =
2598 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2600 static void hotkey_tablet_mode_notify_change(void)
2602 if (tp_features.hotkey_tablet)
2603 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2604 "hotkey_tablet_mode");
2607 /* sysfs hotkey report_mode -------------------------------------------- */
2608 static ssize_t hotkey_report_mode_show(struct device *dev,
2609 struct device_attribute *attr,
2610 char *buf)
2612 return snprintf(buf, PAGE_SIZE, "%d\n",
2613 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2616 static struct device_attribute dev_attr_hotkey_report_mode =
2617 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2619 /* sysfs wakeup reason (pollable) -------------------------------------- */
2620 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2621 struct device_attribute *attr,
2622 char *buf)
2624 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2627 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2628 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2630 static void hotkey_wakeup_reason_notify_change(void)
2632 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2633 "wakeup_reason");
2636 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2637 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2638 struct device_attribute *attr,
2639 char *buf)
2641 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2644 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2645 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2646 hotkey_wakeup_hotunplug_complete_show, NULL);
2648 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2650 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2651 "wakeup_hotunplug_complete");
2654 /* --------------------------------------------------------------------- */
2656 static struct attribute *hotkey_attributes[] __initdata = {
2657 &dev_attr_hotkey_enable.attr,
2658 &dev_attr_hotkey_bios_enabled.attr,
2659 &dev_attr_hotkey_bios_mask.attr,
2660 &dev_attr_hotkey_report_mode.attr,
2661 &dev_attr_hotkey_wakeup_reason.attr,
2662 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2663 &dev_attr_hotkey_mask.attr,
2664 &dev_attr_hotkey_all_mask.attr,
2665 &dev_attr_hotkey_recommended_mask.attr,
2666 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2667 &dev_attr_hotkey_source_mask.attr,
2668 &dev_attr_hotkey_poll_freq.attr,
2669 #endif
2672 static void bluetooth_update_rfk(void);
2673 static void wan_update_rfk(void);
2674 static void uwb_update_rfk(void);
2675 static void tpacpi_send_radiosw_update(void)
2677 int wlsw;
2679 /* Sync these BEFORE sending any rfkill events */
2680 if (tp_features.bluetooth)
2681 bluetooth_update_rfk();
2682 if (tp_features.wan)
2683 wan_update_rfk();
2684 if (tp_features.uwb)
2685 uwb_update_rfk();
2687 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2688 mutex_lock(&tpacpi_inputdev_send_mutex);
2690 input_report_switch(tpacpi_inputdev,
2691 SW_RFKILL_ALL, !!wlsw);
2692 input_sync(tpacpi_inputdev);
2694 mutex_unlock(&tpacpi_inputdev_send_mutex);
2696 hotkey_radio_sw_notify_change();
2699 static void hotkey_exit(void)
2701 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2702 mutex_lock(&hotkey_mutex);
2703 hotkey_poll_stop_sync();
2704 mutex_unlock(&hotkey_mutex);
2705 #endif
2707 if (hotkey_dev_attributes)
2708 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2710 kfree(hotkey_keycode_map);
2712 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2713 "restoring original HKEY status and mask\n");
2714 /* yes, there is a bitwise or below, we want the
2715 * functions to be called even if one of them fail */
2716 if (((tp_features.hotkey_mask &&
2717 hotkey_mask_set(hotkey_orig_mask)) |
2718 hotkey_status_set(false)) != 0)
2719 printk(TPACPI_ERR
2720 "failed to restore hot key mask "
2721 "to BIOS defaults\n");
2724 static void __init hotkey_unmap(const unsigned int scancode)
2726 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2727 clear_bit(hotkey_keycode_map[scancode],
2728 tpacpi_inputdev->keybit);
2729 hotkey_keycode_map[scancode] = KEY_RESERVED;
2734 * HKEY quirks:
2735 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2738 #define TPACPI_HK_Q_INIMASK 0x0001
2740 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2741 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2742 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2743 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2744 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2745 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2746 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2747 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2748 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2749 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2750 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2751 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2752 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2753 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2754 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2755 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2756 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2757 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2758 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2759 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2762 static int __init hotkey_init(struct ibm_init_struct *iibm)
2764 /* Requirements for changing the default keymaps:
2766 * 1. Many of the keys are mapped to KEY_RESERVED for very
2767 * good reasons. Do not change them unless you have deep
2768 * knowledge on the IBM and Lenovo ThinkPad firmware for
2769 * the various ThinkPad models. The driver behaves
2770 * differently for KEY_RESERVED: such keys have their
2771 * hot key mask *unset* in mask_recommended, and also
2772 * in the initial hot key mask programmed into the
2773 * firmware at driver load time, which means the firm-
2774 * ware may react very differently if you change them to
2775 * something else;
2777 * 2. You must be subscribed to the linux-thinkpad and
2778 * ibm-acpi-devel mailing lists, and you should read the
2779 * list archives since 2007 if you want to change the
2780 * keymaps. This requirement exists so that you will
2781 * know the past history of problems with the thinkpad-
2782 * acpi driver keymaps, and also that you will be
2783 * listening to any bug reports;
2785 * 3. Do not send thinkpad-acpi specific patches directly to
2786 * for merging, *ever*. Send them to the linux-acpi
2787 * mailinglist for comments. Merging is to be done only
2788 * through acpi-test and the ACPI maintainer.
2790 * If the above is too much to ask, don't change the keymap.
2791 * Ask the thinkpad-acpi maintainer to do it, instead.
2793 static u16 ibm_keycode_map[] __initdata = {
2794 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2795 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2796 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2797 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2799 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2800 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2801 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2802 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2804 /* brightness: firmware always reacts to them */
2805 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2806 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2808 /* Thinklight: firmware always react to it */
2809 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2811 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2812 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2814 /* Volume: firmware always react to it and reprograms
2815 * the built-in *extra* mixer. Never map it to control
2816 * another mixer by default. */
2817 KEY_RESERVED, /* 0x14: VOLUME UP */
2818 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2819 KEY_RESERVED, /* 0x16: MUTE */
2821 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2823 /* (assignments unknown, please report if found) */
2824 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2825 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2827 static u16 lenovo_keycode_map[] __initdata = {
2828 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2829 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2830 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2831 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2833 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2834 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2835 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2836 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2838 /* These should be enabled --only-- when ACPI video
2839 * is disabled (i.e. in "vendor" mode), and are handled
2840 * in a special way by the init code */
2841 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2842 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2844 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2846 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2847 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2849 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2850 * react to it and reprograms the built-in *extra* mixer.
2851 * Never map it to control another mixer by default.
2853 * T60?, T61, R60?, R61: firmware and EC tries to send
2854 * these over the regular keyboard, so these are no-ops,
2855 * but there are still weird bugs re. MUTE, so do not
2856 * change unless you get test reports from all Lenovo
2857 * models. May cause the BIOS to interfere with the
2858 * HDA mixer.
2860 KEY_RESERVED, /* 0x14: VOLUME UP */
2861 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2862 KEY_RESERVED, /* 0x16: MUTE */
2864 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2866 /* (assignments unknown, please report if found) */
2867 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2868 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2871 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2872 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2873 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2875 int res, i;
2876 int status;
2877 int hkeyv;
2879 unsigned long quirks;
2881 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2882 "initializing hotkey subdriver\n");
2884 BUG_ON(!tpacpi_inputdev);
2885 BUG_ON(tpacpi_inputdev->open != NULL ||
2886 tpacpi_inputdev->close != NULL);
2888 TPACPI_ACPIHANDLE_INIT(hkey);
2889 mutex_init(&hotkey_mutex);
2891 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2892 mutex_init(&hotkey_thread_mutex);
2893 mutex_init(&hotkey_thread_data_mutex);
2894 #endif
2896 /* hotkey not supported on 570 */
2897 tp_features.hotkey = hkey_handle != NULL;
2899 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2900 "hotkeys are %s\n",
2901 str_supported(tp_features.hotkey));
2903 if (!tp_features.hotkey)
2904 return 1;
2906 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2907 ARRAY_SIZE(tpacpi_hotkey_qtable));
2909 tpacpi_disable_brightness_delay();
2911 /* MUST have enough space for all attributes to be added to
2912 * hotkey_dev_attributes */
2913 hotkey_dev_attributes = create_attr_set(
2914 ARRAY_SIZE(hotkey_attributes) + 2,
2915 NULL);
2916 if (!hotkey_dev_attributes)
2917 return -ENOMEM;
2918 res = add_many_to_attr_set(hotkey_dev_attributes,
2919 hotkey_attributes,
2920 ARRAY_SIZE(hotkey_attributes));
2921 if (res)
2922 goto err_exit;
2924 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2925 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2926 for HKEY interface version 0x100 */
2927 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2928 if ((hkeyv >> 8) != 1) {
2929 printk(TPACPI_ERR "unknown version of the "
2930 "HKEY interface: 0x%x\n", hkeyv);
2931 printk(TPACPI_ERR "please report this to %s\n",
2932 TPACPI_MAIL);
2933 } else {
2935 * MHKV 0x100 in A31, R40, R40e,
2936 * T4x, X31, and later
2938 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2939 "firmware HKEY interface version: 0x%x\n",
2940 hkeyv);
2942 /* Paranoia check AND init hotkey_all_mask */
2943 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2944 "MHKA", "qd")) {
2945 printk(TPACPI_ERR
2946 "missing MHKA handler, "
2947 "please report this to %s\n",
2948 TPACPI_MAIL);
2949 /* Fallback: pre-init for FN+F3,F4,F12 */
2950 hotkey_all_mask = 0x080cU;
2951 } else {
2952 tp_features.hotkey_mask = 1;
2957 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2958 "hotkey masks are %s\n",
2959 str_supported(tp_features.hotkey_mask));
2961 /* Init hotkey_all_mask if not initialized yet */
2962 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2963 (quirks & TPACPI_HK_Q_INIMASK))
2964 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2966 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2967 if (tp_features.hotkey_mask) {
2968 /* hotkey_source_mask *must* be zero for
2969 * the first hotkey_mask_get to return hotkey_orig_mask */
2970 res = hotkey_mask_get();
2971 if (res)
2972 goto err_exit;
2974 hotkey_orig_mask = hotkey_acpi_mask;
2975 } else {
2976 hotkey_orig_mask = hotkey_all_mask;
2977 hotkey_acpi_mask = hotkey_all_mask;
2980 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2981 if (dbg_wlswemul) {
2982 tp_features.hotkey_wlsw = 1;
2983 printk(TPACPI_INFO
2984 "radio switch emulation enabled\n");
2985 } else
2986 #endif
2987 /* Not all thinkpads have a hardware radio switch */
2988 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2989 tp_features.hotkey_wlsw = 1;
2990 printk(TPACPI_INFO
2991 "radio switch found; radios are %s\n",
2992 enabled(status, 0));
2994 if (tp_features.hotkey_wlsw)
2995 res = add_to_attr_set(hotkey_dev_attributes,
2996 &dev_attr_hotkey_radio_sw.attr);
2998 /* For X41t, X60t, X61t Tablets... */
2999 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3000 tp_features.hotkey_tablet = 1;
3001 printk(TPACPI_INFO
3002 "possible tablet mode switch found; "
3003 "ThinkPad in %s mode\n",
3004 (status & TP_HOTKEY_TABLET_MASK)?
3005 "tablet" : "laptop");
3006 res = add_to_attr_set(hotkey_dev_attributes,
3007 &dev_attr_hotkey_tablet_mode.attr);
3010 if (!res)
3011 res = register_attr_set_with_sysfs(
3012 hotkey_dev_attributes,
3013 &tpacpi_pdev->dev.kobj);
3014 if (res)
3015 goto err_exit;
3017 /* Set up key map */
3019 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3020 GFP_KERNEL);
3021 if (!hotkey_keycode_map) {
3022 printk(TPACPI_ERR
3023 "failed to allocate memory for key map\n");
3024 res = -ENOMEM;
3025 goto err_exit;
3028 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3029 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3030 "using Lenovo default hot key map\n");
3031 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3032 TPACPI_HOTKEY_MAP_SIZE);
3033 } else {
3034 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3035 "using IBM default hot key map\n");
3036 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3037 TPACPI_HOTKEY_MAP_SIZE);
3040 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3041 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3042 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3043 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3044 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3045 tpacpi_inputdev->keycode = hotkey_keycode_map;
3046 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3047 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3048 set_bit(hotkey_keycode_map[i],
3049 tpacpi_inputdev->keybit);
3050 } else {
3051 if (i < sizeof(hotkey_reserved_mask)*8)
3052 hotkey_reserved_mask |= 1 << i;
3056 if (tp_features.hotkey_wlsw) {
3057 set_bit(EV_SW, tpacpi_inputdev->evbit);
3058 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3060 if (tp_features.hotkey_tablet) {
3061 set_bit(EV_SW, tpacpi_inputdev->evbit);
3062 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3065 /* Do not issue duplicate brightness change events to
3066 * userspace */
3067 if (!tp_features.bright_acpimode)
3068 /* update bright_acpimode... */
3069 tpacpi_check_std_acpi_brightness_support();
3071 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3072 printk(TPACPI_INFO
3073 "This ThinkPad has standard ACPI backlight "
3074 "brightness control, supported by the ACPI "
3075 "video driver\n");
3076 printk(TPACPI_NOTICE
3077 "Disabling thinkpad-acpi brightness events "
3078 "by default...\n");
3080 /* Disable brightness up/down on Lenovo thinkpads when
3081 * ACPI is handling them, otherwise it is plain impossible
3082 * for userspace to do something even remotely sane */
3083 hotkey_reserved_mask |=
3084 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3085 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3086 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3087 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3090 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3091 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3092 & ~hotkey_all_mask
3093 & ~hotkey_reserved_mask;
3095 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3096 "hotkey source mask 0x%08x, polling freq %u\n",
3097 hotkey_source_mask, hotkey_poll_freq);
3098 #endif
3100 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3101 "enabling firmware HKEY event interface...\n");
3102 res = hotkey_status_set(true);
3103 if (res) {
3104 hotkey_exit();
3105 return res;
3107 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3108 | hotkey_driver_mask)
3109 & ~hotkey_source_mask);
3110 if (res < 0 && res != -ENXIO) {
3111 hotkey_exit();
3112 return res;
3114 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3115 & ~hotkey_reserved_mask;
3116 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3117 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3118 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3120 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3121 "legacy ibm/hotkey event reporting over procfs %s\n",
3122 (hotkey_report_mode < 2) ?
3123 "enabled" : "disabled");
3125 tpacpi_inputdev->open = &hotkey_inputdev_open;
3126 tpacpi_inputdev->close = &hotkey_inputdev_close;
3128 hotkey_poll_setup_safe(true);
3129 tpacpi_send_radiosw_update();
3130 tpacpi_input_send_tabletsw();
3132 return 0;
3134 err_exit:
3135 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3136 hotkey_dev_attributes = NULL;
3138 return (res < 0)? res : 1;
3141 static bool hotkey_notify_hotkey(const u32 hkey,
3142 bool *send_acpi_ev,
3143 bool *ignore_acpi_ev)
3145 /* 0x1000-0x1FFF: key presses */
3146 unsigned int scancode = hkey & 0xfff;
3147 *send_acpi_ev = true;
3148 *ignore_acpi_ev = false;
3150 if (scancode > 0 && scancode < 0x21) {
3151 scancode--;
3152 if (!(hotkey_source_mask & (1 << scancode))) {
3153 tpacpi_input_send_key_masked(scancode);
3154 *send_acpi_ev = false;
3155 } else {
3156 *ignore_acpi_ev = true;
3158 return true;
3160 return false;
3163 static bool hotkey_notify_wakeup(const u32 hkey,
3164 bool *send_acpi_ev,
3165 bool *ignore_acpi_ev)
3167 /* 0x2000-0x2FFF: Wakeup reason */
3168 *send_acpi_ev = true;
3169 *ignore_acpi_ev = false;
3171 switch (hkey) {
3172 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3173 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3174 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3175 *ignore_acpi_ev = true;
3176 break;
3178 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3179 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3180 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3181 *ignore_acpi_ev = true;
3182 break;
3184 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3185 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3186 printk(TPACPI_ALERT
3187 "EMERGENCY WAKEUP: battery almost empty\n");
3188 /* how to auto-heal: */
3189 /* 2313: woke up from S3, go to S4/S5 */
3190 /* 2413: woke up from S4, go to S5 */
3191 break;
3193 default:
3194 return false;
3197 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3198 printk(TPACPI_INFO
3199 "woke up due to a hot-unplug "
3200 "request...\n");
3201 hotkey_wakeup_reason_notify_change();
3203 return true;
3206 static bool hotkey_notify_usrevent(const u32 hkey,
3207 bool *send_acpi_ev,
3208 bool *ignore_acpi_ev)
3210 /* 0x5000-0x5FFF: human interface helpers */
3211 *send_acpi_ev = true;
3212 *ignore_acpi_ev = false;
3214 switch (hkey) {
3215 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3216 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3217 return true;
3219 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3220 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3221 tpacpi_input_send_tabletsw();
3222 hotkey_tablet_mode_notify_change();
3223 *send_acpi_ev = false;
3224 return true;
3226 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3227 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3228 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3229 /* do not propagate these events */
3230 *ignore_acpi_ev = true;
3231 return true;
3233 default:
3234 return false;
3238 static bool hotkey_notify_thermal(const u32 hkey,
3239 bool *send_acpi_ev,
3240 bool *ignore_acpi_ev)
3242 /* 0x6000-0x6FFF: thermal alarms */
3243 *send_acpi_ev = true;
3244 *ignore_acpi_ev = false;
3246 switch (hkey) {
3247 case TP_HKEY_EV_ALARM_BAT_HOT:
3248 printk(TPACPI_CRIT
3249 "THERMAL ALARM: battery is too hot!\n");
3250 /* recommended action: warn user through gui */
3251 return true;
3252 case TP_HKEY_EV_ALARM_BAT_XHOT:
3253 printk(TPACPI_ALERT
3254 "THERMAL EMERGENCY: battery is extremely hot!\n");
3255 /* recommended action: immediate sleep/hibernate */
3256 return true;
3257 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3258 printk(TPACPI_CRIT
3259 "THERMAL ALARM: "
3260 "a sensor reports something is too hot!\n");
3261 /* recommended action: warn user through gui, that */
3262 /* some internal component is too hot */
3263 return true;
3264 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3265 printk(TPACPI_ALERT
3266 "THERMAL EMERGENCY: "
3267 "a sensor reports something is extremely hot!\n");
3268 /* recommended action: immediate sleep/hibernate */
3269 return true;
3270 case TP_HKEY_EV_THM_TABLE_CHANGED:
3271 printk(TPACPI_INFO
3272 "EC reports that Thermal Table has changed\n");
3273 /* recommended action: do nothing, we don't have
3274 * Lenovo ATM information */
3275 return true;
3276 default:
3277 printk(TPACPI_ALERT
3278 "THERMAL ALERT: unknown thermal alarm received\n");
3279 return false;
3283 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3285 u32 hkey;
3286 bool send_acpi_ev;
3287 bool ignore_acpi_ev;
3288 bool known_ev;
3290 if (event != 0x80) {
3291 printk(TPACPI_ERR
3292 "unknown HKEY notification event %d\n", event);
3293 /* forward it to userspace, maybe it knows how to handle it */
3294 acpi_bus_generate_netlink_event(
3295 ibm->acpi->device->pnp.device_class,
3296 dev_name(&ibm->acpi->device->dev),
3297 event, 0);
3298 return;
3301 while (1) {
3302 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3303 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3304 return;
3307 if (hkey == 0) {
3308 /* queue empty */
3309 return;
3312 send_acpi_ev = true;
3313 ignore_acpi_ev = false;
3315 switch (hkey >> 12) {
3316 case 1:
3317 /* 0x1000-0x1FFF: key presses */
3318 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3319 &ignore_acpi_ev);
3320 break;
3321 case 2:
3322 /* 0x2000-0x2FFF: Wakeup reason */
3323 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3324 &ignore_acpi_ev);
3325 break;
3326 case 3:
3327 /* 0x3000-0x3FFF: bay-related wakeups */
3328 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3329 hotkey_autosleep_ack = 1;
3330 printk(TPACPI_INFO
3331 "bay ejected\n");
3332 hotkey_wakeup_hotunplug_complete_notify_change();
3333 known_ev = true;
3334 } else {
3335 known_ev = false;
3337 break;
3338 case 4:
3339 /* 0x4000-0x4FFF: dock-related wakeups */
3340 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3341 hotkey_autosleep_ack = 1;
3342 printk(TPACPI_INFO
3343 "undocked\n");
3344 hotkey_wakeup_hotunplug_complete_notify_change();
3345 known_ev = true;
3346 } else {
3347 known_ev = false;
3349 break;
3350 case 5:
3351 /* 0x5000-0x5FFF: human interface helpers */
3352 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3353 &ignore_acpi_ev);
3354 break;
3355 case 6:
3356 /* 0x6000-0x6FFF: thermal alarms */
3357 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3358 &ignore_acpi_ev);
3359 break;
3360 case 7:
3361 /* 0x7000-0x7FFF: misc */
3362 if (tp_features.hotkey_wlsw &&
3363 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3364 tpacpi_send_radiosw_update();
3365 send_acpi_ev = 0;
3366 known_ev = true;
3367 break;
3369 /* fallthrough to default */
3370 default:
3371 known_ev = false;
3373 if (!known_ev) {
3374 printk(TPACPI_NOTICE
3375 "unhandled HKEY event 0x%04x\n", hkey);
3376 printk(TPACPI_NOTICE
3377 "please report the conditions when this "
3378 "event happened to %s\n", TPACPI_MAIL);
3381 /* Legacy events */
3382 if (!ignore_acpi_ev &&
3383 (send_acpi_ev || hotkey_report_mode < 2)) {
3384 acpi_bus_generate_proc_event(ibm->acpi->device,
3385 event, hkey);
3388 /* netlink events */
3389 if (!ignore_acpi_ev && send_acpi_ev) {
3390 acpi_bus_generate_netlink_event(
3391 ibm->acpi->device->pnp.device_class,
3392 dev_name(&ibm->acpi->device->dev),
3393 event, hkey);
3398 static void hotkey_suspend(pm_message_t state)
3400 /* Do these on suspend, we get the events on early resume! */
3401 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3402 hotkey_autosleep_ack = 0;
3405 static void hotkey_resume(void)
3407 tpacpi_disable_brightness_delay();
3409 if (hotkey_status_set(true) < 0 ||
3410 hotkey_mask_set(hotkey_acpi_mask) < 0)
3411 printk(TPACPI_ERR
3412 "error while attempting to reset the event "
3413 "firmware interface\n");
3415 tpacpi_send_radiosw_update();
3416 hotkey_tablet_mode_notify_change();
3417 hotkey_wakeup_reason_notify_change();
3418 hotkey_wakeup_hotunplug_complete_notify_change();
3419 hotkey_poll_setup_safe(false);
3422 /* procfs -------------------------------------------------------------- */
3423 static int hotkey_read(char *p)
3425 int res, status;
3426 int len = 0;
3428 if (!tp_features.hotkey) {
3429 len += sprintf(p + len, "status:\t\tnot supported\n");
3430 return len;
3433 if (mutex_lock_killable(&hotkey_mutex))
3434 return -ERESTARTSYS;
3435 res = hotkey_status_get(&status);
3436 if (!res)
3437 res = hotkey_mask_get();
3438 mutex_unlock(&hotkey_mutex);
3439 if (res)
3440 return res;
3442 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3443 if (hotkey_all_mask) {
3444 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3445 len += sprintf(p + len,
3446 "commands:\tenable, disable, reset, <mask>\n");
3447 } else {
3448 len += sprintf(p + len, "mask:\t\tnot supported\n");
3449 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3452 return len;
3455 static void hotkey_enabledisable_warn(bool enable)
3457 tpacpi_log_usertask("procfs hotkey enable/disable");
3458 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3459 TPACPI_WARN
3460 "hotkey enable/disable functionality has been "
3461 "removed from the driver. Hotkeys are always "
3462 "enabled\n"))
3463 printk(TPACPI_ERR
3464 "Please remove the hotkey=enable module "
3465 "parameter, it is deprecated. Hotkeys are always "
3466 "enabled\n");
3469 static int hotkey_write(char *buf)
3471 int res;
3472 u32 mask;
3473 char *cmd;
3475 if (!tp_features.hotkey)
3476 return -ENODEV;
3478 if (mutex_lock_killable(&hotkey_mutex))
3479 return -ERESTARTSYS;
3481 mask = hotkey_user_mask;
3483 res = 0;
3484 while ((cmd = next_cmd(&buf))) {
3485 if (strlencmp(cmd, "enable") == 0) {
3486 hotkey_enabledisable_warn(1);
3487 } else if (strlencmp(cmd, "disable") == 0) {
3488 hotkey_enabledisable_warn(0);
3489 res = -EPERM;
3490 } else if (strlencmp(cmd, "reset") == 0) {
3491 mask = (hotkey_all_mask | hotkey_source_mask)
3492 & ~hotkey_reserved_mask;
3493 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3494 /* mask set */
3495 } else if (sscanf(cmd, "%x", &mask) == 1) {
3496 /* mask set */
3497 } else {
3498 res = -EINVAL;
3499 goto errexit;
3503 if (!res) {
3504 tpacpi_disclose_usertask("procfs hotkey",
3505 "set mask to 0x%08x\n", mask);
3506 res = hotkey_user_mask_set(mask);
3509 errexit:
3510 mutex_unlock(&hotkey_mutex);
3511 return res;
3514 static const struct acpi_device_id ibm_htk_device_ids[] = {
3515 {TPACPI_ACPI_HKEY_HID, 0},
3516 {"", 0},
3519 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3520 .hid = ibm_htk_device_ids,
3521 .notify = hotkey_notify,
3522 .handle = &hkey_handle,
3523 .type = ACPI_DEVICE_NOTIFY,
3526 static struct ibm_struct hotkey_driver_data = {
3527 .name = "hotkey",
3528 .read = hotkey_read,
3529 .write = hotkey_write,
3530 .exit = hotkey_exit,
3531 .resume = hotkey_resume,
3532 .suspend = hotkey_suspend,
3533 .acpi = &ibm_hotkey_acpidriver,
3536 /*************************************************************************
3537 * Bluetooth subdriver
3540 enum {
3541 /* ACPI GBDC/SBDC bits */
3542 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3543 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3544 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3545 off / last state */
3548 enum {
3549 /* ACPI \BLTH commands */
3550 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3551 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3552 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3553 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3554 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3557 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3559 static struct rfkill *tpacpi_bluetooth_rfkill;
3561 static void bluetooth_suspend(pm_message_t state)
3563 /* Try to make sure radio will resume powered off */
3564 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3565 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3566 vdbg_printk(TPACPI_DBG_RFKILL,
3567 "bluetooth power down on resume request failed\n");
3570 static int bluetooth_get_radiosw(void)
3572 int status;
3574 if (!tp_features.bluetooth)
3575 return -ENODEV;
3577 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3578 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3579 return RFKILL_STATE_HARD_BLOCKED;
3581 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3582 if (dbg_bluetoothemul)
3583 return (tpacpi_bluetooth_emulstate) ?
3584 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3585 #endif
3587 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3588 return -EIO;
3590 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3591 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3594 static void bluetooth_update_rfk(void)
3596 int status;
3598 if (!tpacpi_bluetooth_rfkill)
3599 return;
3601 status = bluetooth_get_radiosw();
3602 if (status < 0)
3603 return;
3604 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3606 vdbg_printk(TPACPI_DBG_RFKILL,
3607 "forced rfkill state to %d\n",
3608 status);
3611 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3613 int status;
3615 if (!tp_features.bluetooth)
3616 return -ENODEV;
3618 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3619 * reason to risk weird behaviour. */
3620 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3621 && radio_on)
3622 return -EPERM;
3624 vdbg_printk(TPACPI_DBG_RFKILL,
3625 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3627 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3628 if (dbg_bluetoothemul) {
3629 tpacpi_bluetooth_emulstate = !!radio_on;
3630 if (update_rfk)
3631 bluetooth_update_rfk();
3632 return 0;
3634 #endif
3636 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3637 if (radio_on)
3638 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3639 else
3640 status = 0;
3641 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3642 return -EIO;
3644 if (update_rfk)
3645 bluetooth_update_rfk();
3647 return 0;
3650 /* sysfs bluetooth enable ---------------------------------------------- */
3651 static ssize_t bluetooth_enable_show(struct device *dev,
3652 struct device_attribute *attr,
3653 char *buf)
3655 int status;
3657 printk_deprecated_rfkill_attribute("bluetooth_enable");
3659 status = bluetooth_get_radiosw();
3660 if (status < 0)
3661 return status;
3663 return snprintf(buf, PAGE_SIZE, "%d\n",
3664 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3667 static ssize_t bluetooth_enable_store(struct device *dev,
3668 struct device_attribute *attr,
3669 const char *buf, size_t count)
3671 unsigned long t;
3672 int res;
3674 printk_deprecated_rfkill_attribute("bluetooth_enable");
3676 if (parse_strtoul(buf, 1, &t))
3677 return -EINVAL;
3679 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3681 res = bluetooth_set_radiosw(t, 1);
3683 return (res) ? res : count;
3686 static struct device_attribute dev_attr_bluetooth_enable =
3687 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3688 bluetooth_enable_show, bluetooth_enable_store);
3690 /* --------------------------------------------------------------------- */
3692 static struct attribute *bluetooth_attributes[] = {
3693 &dev_attr_bluetooth_enable.attr,
3694 NULL
3697 static const struct attribute_group bluetooth_attr_group = {
3698 .attrs = bluetooth_attributes,
3701 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3703 int bts = bluetooth_get_radiosw();
3705 if (bts < 0)
3706 return bts;
3708 *state = bts;
3709 return 0;
3712 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3714 dbg_printk(TPACPI_DBG_RFKILL,
3715 "request to change radio state to %d\n", state);
3716 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3719 static void bluetooth_shutdown(void)
3721 /* Order firmware to save current state to NVRAM */
3722 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3723 TP_ACPI_BLTH_SAVE_STATE))
3724 printk(TPACPI_NOTICE
3725 "failed to save bluetooth state to NVRAM\n");
3726 else
3727 vdbg_printk(TPACPI_DBG_RFKILL,
3728 "bluestooth state saved to NVRAM\n");
3731 static void bluetooth_exit(void)
3733 bluetooth_shutdown();
3735 if (tpacpi_bluetooth_rfkill)
3736 rfkill_unregister(tpacpi_bluetooth_rfkill);
3738 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3739 &bluetooth_attr_group);
3742 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3744 int res;
3745 int status = 0;
3747 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3748 "initializing bluetooth subdriver\n");
3750 TPACPI_ACPIHANDLE_INIT(hkey);
3752 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3753 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3754 tp_features.bluetooth = hkey_handle &&
3755 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3757 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3758 "bluetooth is %s, status 0x%02x\n",
3759 str_supported(tp_features.bluetooth),
3760 status);
3762 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3763 if (dbg_bluetoothemul) {
3764 tp_features.bluetooth = 1;
3765 printk(TPACPI_INFO
3766 "bluetooth switch emulation enabled\n");
3767 } else
3768 #endif
3769 if (tp_features.bluetooth &&
3770 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3771 /* no bluetooth hardware present in system */
3772 tp_features.bluetooth = 0;
3773 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3774 "bluetooth hardware not installed\n");
3777 if (!tp_features.bluetooth)
3778 return 1;
3780 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3781 &bluetooth_attr_group);
3782 if (res)
3783 return res;
3785 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3786 &tpacpi_bluetooth_rfkill,
3787 RFKILL_TYPE_BLUETOOTH,
3788 TPACPI_RFK_BLUETOOTH_SW_NAME,
3789 true,
3790 tpacpi_bluetooth_rfk_set,
3791 tpacpi_bluetooth_rfk_get);
3792 if (res) {
3793 bluetooth_exit();
3794 return res;
3797 return 0;
3800 /* procfs -------------------------------------------------------------- */
3801 static int bluetooth_read(char *p)
3803 int len = 0;
3804 int status = bluetooth_get_radiosw();
3806 if (!tp_features.bluetooth)
3807 len += sprintf(p + len, "status:\t\tnot supported\n");
3808 else {
3809 len += sprintf(p + len, "status:\t\t%s\n",
3810 (status == RFKILL_STATE_UNBLOCKED) ?
3811 "enabled" : "disabled");
3812 len += sprintf(p + len, "commands:\tenable, disable\n");
3815 return len;
3818 static int bluetooth_write(char *buf)
3820 char *cmd;
3821 int state = -1;
3823 if (!tp_features.bluetooth)
3824 return -ENODEV;
3826 while ((cmd = next_cmd(&buf))) {
3827 if (strlencmp(cmd, "enable") == 0) {
3828 state = 1;
3829 } else if (strlencmp(cmd, "disable") == 0) {
3830 state = 0;
3831 } else
3832 return -EINVAL;
3835 if (state != -1) {
3836 tpacpi_disclose_usertask("procfs bluetooth",
3837 "attempt to %s\n",
3838 state ? "enable" : "disable");
3839 bluetooth_set_radiosw(state, 1);
3842 return 0;
3845 static struct ibm_struct bluetooth_driver_data = {
3846 .name = "bluetooth",
3847 .read = bluetooth_read,
3848 .write = bluetooth_write,
3849 .exit = bluetooth_exit,
3850 .suspend = bluetooth_suspend,
3851 .shutdown = bluetooth_shutdown,
3854 /*************************************************************************
3855 * Wan subdriver
3858 enum {
3859 /* ACPI GWAN/SWAN bits */
3860 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3861 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3862 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3863 off / last state */
3866 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3868 static struct rfkill *tpacpi_wan_rfkill;
3870 static void wan_suspend(pm_message_t state)
3872 /* Try to make sure radio will resume powered off */
3873 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3874 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3875 vdbg_printk(TPACPI_DBG_RFKILL,
3876 "WWAN power down on resume request failed\n");
3879 static int wan_get_radiosw(void)
3881 int status;
3883 if (!tp_features.wan)
3884 return -ENODEV;
3886 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3887 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3888 return RFKILL_STATE_HARD_BLOCKED;
3890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_wwanemul)
3892 return (tpacpi_wwan_emulstate) ?
3893 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3894 #endif
3896 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3897 return -EIO;
3899 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3900 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3903 static void wan_update_rfk(void)
3905 int status;
3907 if (!tpacpi_wan_rfkill)
3908 return;
3910 status = wan_get_radiosw();
3911 if (status < 0)
3912 return;
3913 rfkill_force_state(tpacpi_wan_rfkill, status);
3915 vdbg_printk(TPACPI_DBG_RFKILL,
3916 "forced rfkill state to %d\n",
3917 status);
3920 static int wan_set_radiosw(int radio_on, int update_rfk)
3922 int status;
3924 if (!tp_features.wan)
3925 return -ENODEV;
3927 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3928 * reason to risk weird behaviour. */
3929 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3930 && radio_on)
3931 return -EPERM;
3933 vdbg_printk(TPACPI_DBG_RFKILL,
3934 "will %s WWAN\n", radio_on ? "enable" : "disable");
3936 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3937 if (dbg_wwanemul) {
3938 tpacpi_wwan_emulstate = !!radio_on;
3939 if (update_rfk)
3940 wan_update_rfk();
3941 return 0;
3943 #endif
3945 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3946 if (radio_on)
3947 status = TP_ACPI_WANCARD_RADIOSSW;
3948 else
3949 status = 0;
3950 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3951 return -EIO;
3953 if (update_rfk)
3954 wan_update_rfk();
3956 return 0;
3959 /* sysfs wan enable ---------------------------------------------------- */
3960 static ssize_t wan_enable_show(struct device *dev,
3961 struct device_attribute *attr,
3962 char *buf)
3964 int status;
3966 printk_deprecated_rfkill_attribute("wwan_enable");
3968 status = wan_get_radiosw();
3969 if (status < 0)
3970 return status;
3972 return snprintf(buf, PAGE_SIZE, "%d\n",
3973 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3976 static ssize_t wan_enable_store(struct device *dev,
3977 struct device_attribute *attr,
3978 const char *buf, size_t count)
3980 unsigned long t;
3981 int res;
3983 printk_deprecated_rfkill_attribute("wwan_enable");
3985 if (parse_strtoul(buf, 1, &t))
3986 return -EINVAL;
3988 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3990 res = wan_set_radiosw(t, 1);
3992 return (res) ? res : count;
3995 static struct device_attribute dev_attr_wan_enable =
3996 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3997 wan_enable_show, wan_enable_store);
3999 /* --------------------------------------------------------------------- */
4001 static struct attribute *wan_attributes[] = {
4002 &dev_attr_wan_enable.attr,
4003 NULL
4006 static const struct attribute_group wan_attr_group = {
4007 .attrs = wan_attributes,
4010 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4012 int wans = wan_get_radiosw();
4014 if (wans < 0)
4015 return wans;
4017 *state = wans;
4018 return 0;
4021 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4023 dbg_printk(TPACPI_DBG_RFKILL,
4024 "request to change radio state to %d\n", state);
4025 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4028 static void wan_shutdown(void)
4030 /* Order firmware to save current state to NVRAM */
4031 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4032 TP_ACPI_WGSV_SAVE_STATE))
4033 printk(TPACPI_NOTICE
4034 "failed to save WWAN state to NVRAM\n");
4035 else
4036 vdbg_printk(TPACPI_DBG_RFKILL,
4037 "WWAN state saved to NVRAM\n");
4040 static void wan_exit(void)
4042 wan_shutdown();
4044 if (tpacpi_wan_rfkill)
4045 rfkill_unregister(tpacpi_wan_rfkill);
4047 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4048 &wan_attr_group);
4051 static int __init wan_init(struct ibm_init_struct *iibm)
4053 int res;
4054 int status = 0;
4056 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4057 "initializing wan subdriver\n");
4059 TPACPI_ACPIHANDLE_INIT(hkey);
4061 tp_features.wan = hkey_handle &&
4062 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4064 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4065 "wan is %s, status 0x%02x\n",
4066 str_supported(tp_features.wan),
4067 status);
4069 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4070 if (dbg_wwanemul) {
4071 tp_features.wan = 1;
4072 printk(TPACPI_INFO
4073 "wwan switch emulation enabled\n");
4074 } else
4075 #endif
4076 if (tp_features.wan &&
4077 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4078 /* no wan hardware present in system */
4079 tp_features.wan = 0;
4080 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4081 "wan hardware not installed\n");
4084 if (!tp_features.wan)
4085 return 1;
4087 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4088 &wan_attr_group);
4089 if (res)
4090 return res;
4092 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4093 &tpacpi_wan_rfkill,
4094 RFKILL_TYPE_WWAN,
4095 TPACPI_RFK_WWAN_SW_NAME,
4096 true,
4097 tpacpi_wan_rfk_set,
4098 tpacpi_wan_rfk_get);
4099 if (res) {
4100 wan_exit();
4101 return res;
4104 return 0;
4107 /* procfs -------------------------------------------------------------- */
4108 static int wan_read(char *p)
4110 int len = 0;
4111 int status = wan_get_radiosw();
4113 tpacpi_disclose_usertask("procfs wan", "read");
4115 if (!tp_features.wan)
4116 len += sprintf(p + len, "status:\t\tnot supported\n");
4117 else {
4118 len += sprintf(p + len, "status:\t\t%s\n",
4119 (status == RFKILL_STATE_UNBLOCKED) ?
4120 "enabled" : "disabled");
4121 len += sprintf(p + len, "commands:\tenable, disable\n");
4124 return len;
4127 static int wan_write(char *buf)
4129 char *cmd;
4130 int state = -1;
4132 if (!tp_features.wan)
4133 return -ENODEV;
4135 while ((cmd = next_cmd(&buf))) {
4136 if (strlencmp(cmd, "enable") == 0) {
4137 state = 1;
4138 } else if (strlencmp(cmd, "disable") == 0) {
4139 state = 0;
4140 } else
4141 return -EINVAL;
4144 if (state != -1) {
4145 tpacpi_disclose_usertask("procfs wan",
4146 "attempt to %s\n",
4147 state ? "enable" : "disable");
4148 wan_set_radiosw(state, 1);
4151 return 0;
4154 static struct ibm_struct wan_driver_data = {
4155 .name = "wan",
4156 .read = wan_read,
4157 .write = wan_write,
4158 .exit = wan_exit,
4159 .suspend = wan_suspend,
4160 .shutdown = wan_shutdown,
4163 /*************************************************************************
4164 * UWB subdriver
4167 enum {
4168 /* ACPI GUWB/SUWB bits */
4169 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4170 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4173 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4175 static struct rfkill *tpacpi_uwb_rfkill;
4177 static int uwb_get_radiosw(void)
4179 int status;
4181 if (!tp_features.uwb)
4182 return -ENODEV;
4184 /* WLSW overrides UWB in firmware/hardware, reflect that */
4185 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4186 return RFKILL_STATE_HARD_BLOCKED;
4188 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4189 if (dbg_uwbemul)
4190 return (tpacpi_uwb_emulstate) ?
4191 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4192 #endif
4194 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4195 return -EIO;
4197 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4198 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4201 static void uwb_update_rfk(void)
4203 int status;
4205 if (!tpacpi_uwb_rfkill)
4206 return;
4208 status = uwb_get_radiosw();
4209 if (status < 0)
4210 return;
4211 rfkill_force_state(tpacpi_uwb_rfkill, status);
4213 vdbg_printk(TPACPI_DBG_RFKILL,
4214 "forced rfkill state to %d\n",
4215 status);
4218 static int uwb_set_radiosw(int radio_on, int update_rfk)
4220 int status;
4222 if (!tp_features.uwb)
4223 return -ENODEV;
4225 /* WLSW overrides UWB in firmware/hardware, but there is no
4226 * reason to risk weird behaviour. */
4227 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4228 && radio_on)
4229 return -EPERM;
4231 vdbg_printk(TPACPI_DBG_RFKILL,
4232 "will %s UWB\n", radio_on ? "enable" : "disable");
4234 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4235 if (dbg_uwbemul) {
4236 tpacpi_uwb_emulstate = !!radio_on;
4237 if (update_rfk)
4238 uwb_update_rfk();
4239 return 0;
4241 #endif
4243 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4244 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4245 return -EIO;
4247 if (update_rfk)
4248 uwb_update_rfk();
4250 return 0;
4253 /* --------------------------------------------------------------------- */
4255 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4257 int uwbs = uwb_get_radiosw();
4259 if (uwbs < 0)
4260 return uwbs;
4262 *state = uwbs;
4263 return 0;
4266 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4268 dbg_printk(TPACPI_DBG_RFKILL,
4269 "request to change radio state to %d\n", state);
4270 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4273 static void uwb_exit(void)
4275 if (tpacpi_uwb_rfkill)
4276 rfkill_unregister(tpacpi_uwb_rfkill);
4279 static int __init uwb_init(struct ibm_init_struct *iibm)
4281 int res;
4282 int status = 0;
4284 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4285 "initializing uwb subdriver\n");
4287 TPACPI_ACPIHANDLE_INIT(hkey);
4289 tp_features.uwb = hkey_handle &&
4290 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4292 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4293 "uwb is %s, status 0x%02x\n",
4294 str_supported(tp_features.uwb),
4295 status);
4297 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4298 if (dbg_uwbemul) {
4299 tp_features.uwb = 1;
4300 printk(TPACPI_INFO
4301 "uwb switch emulation enabled\n");
4302 } else
4303 #endif
4304 if (tp_features.uwb &&
4305 !(status & TP_ACPI_UWB_HWPRESENT)) {
4306 /* no uwb hardware present in system */
4307 tp_features.uwb = 0;
4308 dbg_printk(TPACPI_DBG_INIT,
4309 "uwb hardware not installed\n");
4312 if (!tp_features.uwb)
4313 return 1;
4315 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4316 &tpacpi_uwb_rfkill,
4317 RFKILL_TYPE_UWB,
4318 TPACPI_RFK_UWB_SW_NAME,
4319 false,
4320 tpacpi_uwb_rfk_set,
4321 tpacpi_uwb_rfk_get);
4323 return res;
4326 static struct ibm_struct uwb_driver_data = {
4327 .name = "uwb",
4328 .exit = uwb_exit,
4329 .flags.experimental = 1,
4332 /*************************************************************************
4333 * Video subdriver
4336 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4338 enum video_access_mode {
4339 TPACPI_VIDEO_NONE = 0,
4340 TPACPI_VIDEO_570, /* 570 */
4341 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4342 TPACPI_VIDEO_NEW, /* all others */
4345 enum { /* video status flags, based on VIDEO_570 */
4346 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4347 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4348 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4351 enum { /* TPACPI_VIDEO_570 constants */
4352 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4353 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4354 * video_status_flags */
4355 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4356 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4359 static enum video_access_mode video_supported;
4360 static int video_orig_autosw;
4362 static int video_autosw_get(void);
4363 static int video_autosw_set(int enable);
4365 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4367 static int __init video_init(struct ibm_init_struct *iibm)
4369 int ivga;
4371 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4373 TPACPI_ACPIHANDLE_INIT(vid);
4374 TPACPI_ACPIHANDLE_INIT(vid2);
4376 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4377 /* G41, assume IVGA doesn't change */
4378 vid_handle = vid2_handle;
4380 if (!vid_handle)
4381 /* video switching not supported on R30, R31 */
4382 video_supported = TPACPI_VIDEO_NONE;
4383 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4384 /* 570 */
4385 video_supported = TPACPI_VIDEO_570;
4386 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4387 /* 600e/x, 770e, 770x */
4388 video_supported = TPACPI_VIDEO_770;
4389 else
4390 /* all others */
4391 video_supported = TPACPI_VIDEO_NEW;
4393 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4394 str_supported(video_supported != TPACPI_VIDEO_NONE),
4395 video_supported);
4397 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4400 static void video_exit(void)
4402 dbg_printk(TPACPI_DBG_EXIT,
4403 "restoring original video autoswitch mode\n");
4404 if (video_autosw_set(video_orig_autosw))
4405 printk(TPACPI_ERR "error while trying to restore original "
4406 "video autoswitch mode\n");
4409 static int video_outputsw_get(void)
4411 int status = 0;
4412 int i;
4414 switch (video_supported) {
4415 case TPACPI_VIDEO_570:
4416 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4417 TP_ACPI_VIDEO_570_PHSCMD))
4418 return -EIO;
4419 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4420 break;
4421 case TPACPI_VIDEO_770:
4422 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4423 return -EIO;
4424 if (i)
4425 status |= TP_ACPI_VIDEO_S_LCD;
4426 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4427 return -EIO;
4428 if (i)
4429 status |= TP_ACPI_VIDEO_S_CRT;
4430 break;
4431 case TPACPI_VIDEO_NEW:
4432 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4433 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4434 return -EIO;
4435 if (i)
4436 status |= TP_ACPI_VIDEO_S_CRT;
4438 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4439 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4440 return -EIO;
4441 if (i)
4442 status |= TP_ACPI_VIDEO_S_LCD;
4443 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4444 return -EIO;
4445 if (i)
4446 status |= TP_ACPI_VIDEO_S_DVI;
4447 break;
4448 default:
4449 return -ENOSYS;
4452 return status;
4455 static int video_outputsw_set(int status)
4457 int autosw;
4458 int res = 0;
4460 switch (video_supported) {
4461 case TPACPI_VIDEO_570:
4462 res = acpi_evalf(NULL, NULL,
4463 "\\_SB.PHS2", "vdd",
4464 TP_ACPI_VIDEO_570_PHS2CMD,
4465 status | TP_ACPI_VIDEO_570_PHS2SET);
4466 break;
4467 case TPACPI_VIDEO_770:
4468 autosw = video_autosw_get();
4469 if (autosw < 0)
4470 return autosw;
4472 res = video_autosw_set(1);
4473 if (res)
4474 return res;
4475 res = acpi_evalf(vid_handle, NULL,
4476 "ASWT", "vdd", status * 0x100, 0);
4477 if (!autosw && video_autosw_set(autosw)) {
4478 printk(TPACPI_ERR
4479 "video auto-switch left enabled due to error\n");
4480 return -EIO;
4482 break;
4483 case TPACPI_VIDEO_NEW:
4484 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4485 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4486 break;
4487 default:
4488 return -ENOSYS;
4491 return (res)? 0 : -EIO;
4494 static int video_autosw_get(void)
4496 int autosw = 0;
4498 switch (video_supported) {
4499 case TPACPI_VIDEO_570:
4500 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4501 return -EIO;
4502 break;
4503 case TPACPI_VIDEO_770:
4504 case TPACPI_VIDEO_NEW:
4505 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4506 return -EIO;
4507 break;
4508 default:
4509 return -ENOSYS;
4512 return autosw & 1;
4515 static int video_autosw_set(int enable)
4517 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4518 return -EIO;
4519 return 0;
4522 static int video_outputsw_cycle(void)
4524 int autosw = video_autosw_get();
4525 int res;
4527 if (autosw < 0)
4528 return autosw;
4530 switch (video_supported) {
4531 case TPACPI_VIDEO_570:
4532 res = video_autosw_set(1);
4533 if (res)
4534 return res;
4535 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4536 break;
4537 case TPACPI_VIDEO_770:
4538 case TPACPI_VIDEO_NEW:
4539 res = video_autosw_set(1);
4540 if (res)
4541 return res;
4542 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4543 break;
4544 default:
4545 return -ENOSYS;
4547 if (!autosw && video_autosw_set(autosw)) {
4548 printk(TPACPI_ERR
4549 "video auto-switch left enabled due to error\n");
4550 return -EIO;
4553 return (res)? 0 : -EIO;
4556 static int video_expand_toggle(void)
4558 switch (video_supported) {
4559 case TPACPI_VIDEO_570:
4560 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4561 0 : -EIO;
4562 case TPACPI_VIDEO_770:
4563 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4564 0 : -EIO;
4565 case TPACPI_VIDEO_NEW:
4566 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4567 0 : -EIO;
4568 default:
4569 return -ENOSYS;
4571 /* not reached */
4574 static int video_read(char *p)
4576 int status, autosw;
4577 int len = 0;
4579 if (video_supported == TPACPI_VIDEO_NONE) {
4580 len += sprintf(p + len, "status:\t\tnot supported\n");
4581 return len;
4584 status = video_outputsw_get();
4585 if (status < 0)
4586 return status;
4588 autosw = video_autosw_get();
4589 if (autosw < 0)
4590 return autosw;
4592 len += sprintf(p + len, "status:\t\tsupported\n");
4593 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4594 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4595 if (video_supported == TPACPI_VIDEO_NEW)
4596 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4597 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4598 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4599 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4600 if (video_supported == TPACPI_VIDEO_NEW)
4601 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4602 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4603 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4605 return len;
4608 static int video_write(char *buf)
4610 char *cmd;
4611 int enable, disable, status;
4612 int res;
4614 if (video_supported == TPACPI_VIDEO_NONE)
4615 return -ENODEV;
4617 enable = 0;
4618 disable = 0;
4620 while ((cmd = next_cmd(&buf))) {
4621 if (strlencmp(cmd, "lcd_enable") == 0) {
4622 enable |= TP_ACPI_VIDEO_S_LCD;
4623 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4624 disable |= TP_ACPI_VIDEO_S_LCD;
4625 } else if (strlencmp(cmd, "crt_enable") == 0) {
4626 enable |= TP_ACPI_VIDEO_S_CRT;
4627 } else if (strlencmp(cmd, "crt_disable") == 0) {
4628 disable |= TP_ACPI_VIDEO_S_CRT;
4629 } else if (video_supported == TPACPI_VIDEO_NEW &&
4630 strlencmp(cmd, "dvi_enable") == 0) {
4631 enable |= TP_ACPI_VIDEO_S_DVI;
4632 } else if (video_supported == TPACPI_VIDEO_NEW &&
4633 strlencmp(cmd, "dvi_disable") == 0) {
4634 disable |= TP_ACPI_VIDEO_S_DVI;
4635 } else if (strlencmp(cmd, "auto_enable") == 0) {
4636 res = video_autosw_set(1);
4637 if (res)
4638 return res;
4639 } else if (strlencmp(cmd, "auto_disable") == 0) {
4640 res = video_autosw_set(0);
4641 if (res)
4642 return res;
4643 } else if (strlencmp(cmd, "video_switch") == 0) {
4644 res = video_outputsw_cycle();
4645 if (res)
4646 return res;
4647 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4648 res = video_expand_toggle();
4649 if (res)
4650 return res;
4651 } else
4652 return -EINVAL;
4655 if (enable || disable) {
4656 status = video_outputsw_get();
4657 if (status < 0)
4658 return status;
4659 res = video_outputsw_set((status & ~disable) | enable);
4660 if (res)
4661 return res;
4664 return 0;
4667 static struct ibm_struct video_driver_data = {
4668 .name = "video",
4669 .read = video_read,
4670 .write = video_write,
4671 .exit = video_exit,
4674 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4676 /*************************************************************************
4677 * Light (thinklight) subdriver
4680 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4681 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4683 static int light_get_status(void)
4685 int status = 0;
4687 if (tp_features.light_status) {
4688 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4689 return -EIO;
4690 return (!!status);
4693 return -ENXIO;
4696 static int light_set_status(int status)
4698 int rc;
4700 if (tp_features.light) {
4701 if (cmos_handle) {
4702 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4703 (status)?
4704 TP_CMOS_THINKLIGHT_ON :
4705 TP_CMOS_THINKLIGHT_OFF);
4706 } else {
4707 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4708 (status)? 1 : 0);
4710 return (rc)? 0 : -EIO;
4713 return -ENXIO;
4716 static void light_set_status_worker(struct work_struct *work)
4718 struct tpacpi_led_classdev *data =
4719 container_of(work, struct tpacpi_led_classdev, work);
4721 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4722 light_set_status((data->new_state != TPACPI_LED_OFF));
4725 static void light_sysfs_set(struct led_classdev *led_cdev,
4726 enum led_brightness brightness)
4728 struct tpacpi_led_classdev *data =
4729 container_of(led_cdev,
4730 struct tpacpi_led_classdev,
4731 led_classdev);
4732 data->new_state = (brightness != LED_OFF) ?
4733 TPACPI_LED_ON : TPACPI_LED_OFF;
4734 queue_work(tpacpi_wq, &data->work);
4737 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4739 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4742 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4743 .led_classdev = {
4744 .name = "tpacpi::thinklight",
4745 .brightness_set = &light_sysfs_set,
4746 .brightness_get = &light_sysfs_get,
4750 static int __init light_init(struct ibm_init_struct *iibm)
4752 int rc;
4754 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4756 TPACPI_ACPIHANDLE_INIT(ledb);
4757 TPACPI_ACPIHANDLE_INIT(lght);
4758 TPACPI_ACPIHANDLE_INIT(cmos);
4759 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4761 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4762 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4764 if (tp_features.light)
4765 /* light status not supported on
4766 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4767 tp_features.light_status =
4768 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4770 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4771 str_supported(tp_features.light),
4772 str_supported(tp_features.light_status));
4774 if (!tp_features.light)
4775 return 1;
4777 rc = led_classdev_register(&tpacpi_pdev->dev,
4778 &tpacpi_led_thinklight.led_classdev);
4780 if (rc < 0) {
4781 tp_features.light = 0;
4782 tp_features.light_status = 0;
4783 } else {
4784 rc = 0;
4787 return rc;
4790 static void light_exit(void)
4792 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4793 if (work_pending(&tpacpi_led_thinklight.work))
4794 flush_workqueue(tpacpi_wq);
4797 static int light_read(char *p)
4799 int len = 0;
4800 int status;
4802 if (!tp_features.light) {
4803 len += sprintf(p + len, "status:\t\tnot supported\n");
4804 } else if (!tp_features.light_status) {
4805 len += sprintf(p + len, "status:\t\tunknown\n");
4806 len += sprintf(p + len, "commands:\ton, off\n");
4807 } else {
4808 status = light_get_status();
4809 if (status < 0)
4810 return status;
4811 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4812 len += sprintf(p + len, "commands:\ton, off\n");
4815 return len;
4818 static int light_write(char *buf)
4820 char *cmd;
4821 int newstatus = 0;
4823 if (!tp_features.light)
4824 return -ENODEV;
4826 while ((cmd = next_cmd(&buf))) {
4827 if (strlencmp(cmd, "on") == 0) {
4828 newstatus = 1;
4829 } else if (strlencmp(cmd, "off") == 0) {
4830 newstatus = 0;
4831 } else
4832 return -EINVAL;
4835 return light_set_status(newstatus);
4838 static struct ibm_struct light_driver_data = {
4839 .name = "light",
4840 .read = light_read,
4841 .write = light_write,
4842 .exit = light_exit,
4845 /*************************************************************************
4846 * CMOS subdriver
4849 /* sysfs cmos_command -------------------------------------------------- */
4850 static ssize_t cmos_command_store(struct device *dev,
4851 struct device_attribute *attr,
4852 const char *buf, size_t count)
4854 unsigned long cmos_cmd;
4855 int res;
4857 if (parse_strtoul(buf, 21, &cmos_cmd))
4858 return -EINVAL;
4860 res = issue_thinkpad_cmos_command(cmos_cmd);
4861 return (res)? res : count;
4864 static struct device_attribute dev_attr_cmos_command =
4865 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4867 /* --------------------------------------------------------------------- */
4869 static int __init cmos_init(struct ibm_init_struct *iibm)
4871 int res;
4873 vdbg_printk(TPACPI_DBG_INIT,
4874 "initializing cmos commands subdriver\n");
4876 TPACPI_ACPIHANDLE_INIT(cmos);
4878 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4879 str_supported(cmos_handle != NULL));
4881 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4882 if (res)
4883 return res;
4885 return (cmos_handle)? 0 : 1;
4888 static void cmos_exit(void)
4890 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4893 static int cmos_read(char *p)
4895 int len = 0;
4897 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4898 R30, R31, T20-22, X20-21 */
4899 if (!cmos_handle)
4900 len += sprintf(p + len, "status:\t\tnot supported\n");
4901 else {
4902 len += sprintf(p + len, "status:\t\tsupported\n");
4903 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4906 return len;
4909 static int cmos_write(char *buf)
4911 char *cmd;
4912 int cmos_cmd, res;
4914 while ((cmd = next_cmd(&buf))) {
4915 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4916 cmos_cmd >= 0 && cmos_cmd <= 21) {
4917 /* cmos_cmd set */
4918 } else
4919 return -EINVAL;
4921 res = issue_thinkpad_cmos_command(cmos_cmd);
4922 if (res)
4923 return res;
4926 return 0;
4929 static struct ibm_struct cmos_driver_data = {
4930 .name = "cmos",
4931 .read = cmos_read,
4932 .write = cmos_write,
4933 .exit = cmos_exit,
4936 /*************************************************************************
4937 * LED subdriver
4940 enum led_access_mode {
4941 TPACPI_LED_NONE = 0,
4942 TPACPI_LED_570, /* 570 */
4943 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4944 TPACPI_LED_NEW, /* all others */
4947 enum { /* For TPACPI_LED_OLD */
4948 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4949 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4950 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4953 static enum led_access_mode led_supported;
4955 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4956 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4957 /* T20-22, X20-21 */
4958 "LED", /* all others */
4959 ); /* R30, R31 */
4961 #define TPACPI_LED_NUMLEDS 16
4962 static struct tpacpi_led_classdev *tpacpi_leds;
4963 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4964 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4965 /* there's a limit of 19 chars + NULL before 2.6.26 */
4966 "tpacpi::power",
4967 "tpacpi:orange:batt",
4968 "tpacpi:green:batt",
4969 "tpacpi::dock_active",
4970 "tpacpi::bay_active",
4971 "tpacpi::dock_batt",
4972 "tpacpi::unknown_led",
4973 "tpacpi::standby",
4974 "tpacpi::dock_status1",
4975 "tpacpi::dock_status2",
4976 "tpacpi::unknown_led2",
4977 "tpacpi::unknown_led3",
4978 "tpacpi::thinkvantage",
4980 #define TPACPI_SAFE_LEDS 0x1081U
4982 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4984 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4985 return false;
4986 #else
4987 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4988 #endif
4991 static int led_get_status(const unsigned int led)
4993 int status;
4994 enum led_status_t led_s;
4996 switch (led_supported) {
4997 case TPACPI_LED_570:
4998 if (!acpi_evalf(ec_handle,
4999 &status, "GLED", "dd", 1 << led))
5000 return -EIO;
5001 led_s = (status == 0)?
5002 TPACPI_LED_OFF :
5003 ((status == 1)?
5004 TPACPI_LED_ON :
5005 TPACPI_LED_BLINK);
5006 tpacpi_led_state_cache[led] = led_s;
5007 return led_s;
5008 default:
5009 return -ENXIO;
5012 /* not reached */
5015 static int led_set_status(const unsigned int led,
5016 const enum led_status_t ledstatus)
5018 /* off, on, blink. Index is led_status_t */
5019 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5020 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5022 int rc = 0;
5024 switch (led_supported) {
5025 case TPACPI_LED_570:
5026 /* 570 */
5027 if (unlikely(led > 7))
5028 return -EINVAL;
5029 if (unlikely(tpacpi_is_led_restricted(led)))
5030 return -EPERM;
5031 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5032 (1 << led), led_sled_arg1[ledstatus]))
5033 rc = -EIO;
5034 break;
5035 case TPACPI_LED_OLD:
5036 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5037 if (unlikely(led > 7))
5038 return -EINVAL;
5039 if (unlikely(tpacpi_is_led_restricted(led)))
5040 return -EPERM;
5041 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5042 if (rc >= 0)
5043 rc = ec_write(TPACPI_LED_EC_HLBL,
5044 (ledstatus == TPACPI_LED_BLINK) << led);
5045 if (rc >= 0)
5046 rc = ec_write(TPACPI_LED_EC_HLCL,
5047 (ledstatus != TPACPI_LED_OFF) << led);
5048 break;
5049 case TPACPI_LED_NEW:
5050 /* all others */
5051 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5052 return -EINVAL;
5053 if (unlikely(tpacpi_is_led_restricted(led)))
5054 return -EPERM;
5055 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5056 led, led_led_arg1[ledstatus]))
5057 rc = -EIO;
5058 break;
5059 default:
5060 rc = -ENXIO;
5063 if (!rc)
5064 tpacpi_led_state_cache[led] = ledstatus;
5066 return rc;
5069 static void led_set_status_worker(struct work_struct *work)
5071 struct tpacpi_led_classdev *data =
5072 container_of(work, struct tpacpi_led_classdev, work);
5074 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5075 led_set_status(data->led, data->new_state);
5078 static void led_sysfs_set(struct led_classdev *led_cdev,
5079 enum led_brightness brightness)
5081 struct tpacpi_led_classdev *data = container_of(led_cdev,
5082 struct tpacpi_led_classdev, led_classdev);
5084 if (brightness == LED_OFF)
5085 data->new_state = TPACPI_LED_OFF;
5086 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5087 data->new_state = TPACPI_LED_ON;
5088 else
5089 data->new_state = TPACPI_LED_BLINK;
5091 queue_work(tpacpi_wq, &data->work);
5094 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5095 unsigned long *delay_on, unsigned long *delay_off)
5097 struct tpacpi_led_classdev *data = container_of(led_cdev,
5098 struct tpacpi_led_classdev, led_classdev);
5100 /* Can we choose the flash rate? */
5101 if (*delay_on == 0 && *delay_off == 0) {
5102 /* yes. set them to the hardware blink rate (1 Hz) */
5103 *delay_on = 500; /* ms */
5104 *delay_off = 500; /* ms */
5105 } else if ((*delay_on != 500) || (*delay_off != 500))
5106 return -EINVAL;
5108 data->new_state = TPACPI_LED_BLINK;
5109 queue_work(tpacpi_wq, &data->work);
5111 return 0;
5114 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5116 int rc;
5118 struct tpacpi_led_classdev *data = container_of(led_cdev,
5119 struct tpacpi_led_classdev, led_classdev);
5121 rc = led_get_status(data->led);
5123 if (rc == TPACPI_LED_OFF || rc < 0)
5124 rc = LED_OFF; /* no error handling in led class :( */
5125 else
5126 rc = LED_FULL;
5128 return rc;
5131 static void led_exit(void)
5133 unsigned int i;
5135 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5136 if (tpacpi_leds[i].led_classdev.name)
5137 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5140 kfree(tpacpi_leds);
5143 static int __init tpacpi_init_led(unsigned int led)
5145 int rc;
5147 tpacpi_leds[led].led = led;
5149 /* LEDs with no name don't get registered */
5150 if (!tpacpi_led_names[led])
5151 return 0;
5153 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5154 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5155 if (led_supported == TPACPI_LED_570)
5156 tpacpi_leds[led].led_classdev.brightness_get =
5157 &led_sysfs_get;
5159 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5161 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5163 rc = led_classdev_register(&tpacpi_pdev->dev,
5164 &tpacpi_leds[led].led_classdev);
5165 if (rc < 0)
5166 tpacpi_leds[led].led_classdev.name = NULL;
5168 return rc;
5171 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5172 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5173 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5174 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5176 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5177 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5178 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5179 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5180 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5181 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5182 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5183 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5185 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5186 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5187 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5188 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5189 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5191 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5192 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5193 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5194 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5196 /* (1) - may have excess leds enabled on MSB */
5198 /* Defaults (order matters, keep last, don't reorder!) */
5199 { /* Lenovo */
5200 .vendor = PCI_VENDOR_ID_LENOVO,
5201 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5202 .quirks = 0x1fffU,
5204 { /* IBM ThinkPads with no EC version string */
5205 .vendor = PCI_VENDOR_ID_IBM,
5206 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5207 .quirks = 0x00ffU,
5209 { /* IBM ThinkPads with EC version string */
5210 .vendor = PCI_VENDOR_ID_IBM,
5211 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5212 .quirks = 0x00bfU,
5216 #undef TPACPI_LEDQ_IBM
5217 #undef TPACPI_LEDQ_LNV
5219 static int __init led_init(struct ibm_init_struct *iibm)
5221 unsigned int i;
5222 int rc;
5223 unsigned long useful_leds;
5225 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5227 TPACPI_ACPIHANDLE_INIT(led);
5229 if (!led_handle)
5230 /* led not supported on R30, R31 */
5231 led_supported = TPACPI_LED_NONE;
5232 else if (strlencmp(led_path, "SLED") == 0)
5233 /* 570 */
5234 led_supported = TPACPI_LED_570;
5235 else if (strlencmp(led_path, "SYSL") == 0)
5236 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5237 led_supported = TPACPI_LED_OLD;
5238 else
5239 /* all others */
5240 led_supported = TPACPI_LED_NEW;
5242 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5243 str_supported(led_supported), led_supported);
5245 if (led_supported == TPACPI_LED_NONE)
5246 return 1;
5248 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5249 GFP_KERNEL);
5250 if (!tpacpi_leds) {
5251 printk(TPACPI_ERR "Out of memory for LED data\n");
5252 return -ENOMEM;
5255 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5256 ARRAY_SIZE(led_useful_qtable));
5258 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5259 if (!tpacpi_is_led_restricted(i) &&
5260 test_bit(i, &useful_leds)) {
5261 rc = tpacpi_init_led(i);
5262 if (rc < 0) {
5263 led_exit();
5264 return rc;
5269 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5270 printk(TPACPI_NOTICE
5271 "warning: userspace override of important "
5272 "firmware LEDs is enabled\n");
5273 #endif
5274 return 0;
5277 #define str_led_status(s) \
5278 ((s) == TPACPI_LED_OFF ? "off" : \
5279 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5281 static int led_read(char *p)
5283 int len = 0;
5285 if (!led_supported) {
5286 len += sprintf(p + len, "status:\t\tnot supported\n");
5287 return len;
5289 len += sprintf(p + len, "status:\t\tsupported\n");
5291 if (led_supported == TPACPI_LED_570) {
5292 /* 570 */
5293 int i, status;
5294 for (i = 0; i < 8; i++) {
5295 status = led_get_status(i);
5296 if (status < 0)
5297 return -EIO;
5298 len += sprintf(p + len, "%d:\t\t%s\n",
5299 i, str_led_status(status));
5303 len += sprintf(p + len, "commands:\t"
5304 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5306 return len;
5309 static int led_write(char *buf)
5311 char *cmd;
5312 int led, rc;
5313 enum led_status_t s;
5315 if (!led_supported)
5316 return -ENODEV;
5318 while ((cmd = next_cmd(&buf))) {
5319 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5320 return -EINVAL;
5322 if (strstr(cmd, "off")) {
5323 s = TPACPI_LED_OFF;
5324 } else if (strstr(cmd, "on")) {
5325 s = TPACPI_LED_ON;
5326 } else if (strstr(cmd, "blink")) {
5327 s = TPACPI_LED_BLINK;
5328 } else {
5329 return -EINVAL;
5332 rc = led_set_status(led, s);
5333 if (rc < 0)
5334 return rc;
5337 return 0;
5340 static struct ibm_struct led_driver_data = {
5341 .name = "led",
5342 .read = led_read,
5343 .write = led_write,
5344 .exit = led_exit,
5347 /*************************************************************************
5348 * Beep subdriver
5351 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5353 #define TPACPI_BEEP_Q1 0x0001
5355 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5356 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5357 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5360 static int __init beep_init(struct ibm_init_struct *iibm)
5362 unsigned long quirks;
5364 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5366 TPACPI_ACPIHANDLE_INIT(beep);
5368 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5369 str_supported(beep_handle != NULL));
5371 quirks = tpacpi_check_quirks(beep_quirk_table,
5372 ARRAY_SIZE(beep_quirk_table));
5374 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5376 return (beep_handle)? 0 : 1;
5379 static int beep_read(char *p)
5381 int len = 0;
5383 if (!beep_handle)
5384 len += sprintf(p + len, "status:\t\tnot supported\n");
5385 else {
5386 len += sprintf(p + len, "status:\t\tsupported\n");
5387 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5390 return len;
5393 static int beep_write(char *buf)
5395 char *cmd;
5396 int beep_cmd;
5398 if (!beep_handle)
5399 return -ENODEV;
5401 while ((cmd = next_cmd(&buf))) {
5402 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5403 beep_cmd >= 0 && beep_cmd <= 17) {
5404 /* beep_cmd set */
5405 } else
5406 return -EINVAL;
5407 if (tp_features.beep_needs_two_args) {
5408 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5409 beep_cmd, 0))
5410 return -EIO;
5411 } else {
5412 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5413 beep_cmd))
5414 return -EIO;
5418 return 0;
5421 static struct ibm_struct beep_driver_data = {
5422 .name = "beep",
5423 .read = beep_read,
5424 .write = beep_write,
5427 /*************************************************************************
5428 * Thermal subdriver
5431 enum thermal_access_mode {
5432 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5433 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5434 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5435 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5436 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5439 enum { /* TPACPI_THERMAL_TPEC_* */
5440 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5441 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5442 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5445 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5446 struct ibm_thermal_sensors_struct {
5447 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5450 static enum thermal_access_mode thermal_read_mode;
5452 /* idx is zero-based */
5453 static int thermal_get_sensor(int idx, s32 *value)
5455 int t;
5456 s8 tmp;
5457 char tmpi[5];
5459 t = TP_EC_THERMAL_TMP0;
5461 switch (thermal_read_mode) {
5462 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5463 case TPACPI_THERMAL_TPEC_16:
5464 if (idx >= 8 && idx <= 15) {
5465 t = TP_EC_THERMAL_TMP8;
5466 idx -= 8;
5468 /* fallthrough */
5469 #endif
5470 case TPACPI_THERMAL_TPEC_8:
5471 if (idx <= 7) {
5472 if (!acpi_ec_read(t + idx, &tmp))
5473 return -EIO;
5474 *value = tmp * 1000;
5475 return 0;
5477 break;
5479 case TPACPI_THERMAL_ACPI_UPDT:
5480 if (idx <= 7) {
5481 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5482 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5483 return -EIO;
5484 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5485 return -EIO;
5486 *value = (t - 2732) * 100;
5487 return 0;
5489 break;
5491 case TPACPI_THERMAL_ACPI_TMP07:
5492 if (idx <= 7) {
5493 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5494 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5495 return -EIO;
5496 if (t > 127 || t < -127)
5497 t = TP_EC_THERMAL_TMP_NA;
5498 *value = t * 1000;
5499 return 0;
5501 break;
5503 case TPACPI_THERMAL_NONE:
5504 default:
5505 return -ENOSYS;
5508 return -EINVAL;
5511 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5513 int res, i;
5514 int n;
5516 n = 8;
5517 i = 0;
5519 if (!s)
5520 return -EINVAL;
5522 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5523 n = 16;
5525 for (i = 0 ; i < n; i++) {
5526 res = thermal_get_sensor(i, &s->temp[i]);
5527 if (res)
5528 return res;
5531 return n;
5534 /* sysfs temp##_input -------------------------------------------------- */
5536 static ssize_t thermal_temp_input_show(struct device *dev,
5537 struct device_attribute *attr,
5538 char *buf)
5540 struct sensor_device_attribute *sensor_attr =
5541 to_sensor_dev_attr(attr);
5542 int idx = sensor_attr->index;
5543 s32 value;
5544 int res;
5546 res = thermal_get_sensor(idx, &value);
5547 if (res)
5548 return res;
5549 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5550 return -ENXIO;
5552 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5555 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5556 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5557 thermal_temp_input_show, NULL, _idxB)
5559 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5560 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5561 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5562 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5563 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5564 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5565 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5566 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5567 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5568 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5569 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5570 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5571 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5572 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5573 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5574 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5575 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5578 #define THERMAL_ATTRS(X) \
5579 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5581 static struct attribute *thermal_temp_input_attr[] = {
5582 THERMAL_ATTRS(8),
5583 THERMAL_ATTRS(9),
5584 THERMAL_ATTRS(10),
5585 THERMAL_ATTRS(11),
5586 THERMAL_ATTRS(12),
5587 THERMAL_ATTRS(13),
5588 THERMAL_ATTRS(14),
5589 THERMAL_ATTRS(15),
5590 THERMAL_ATTRS(0),
5591 THERMAL_ATTRS(1),
5592 THERMAL_ATTRS(2),
5593 THERMAL_ATTRS(3),
5594 THERMAL_ATTRS(4),
5595 THERMAL_ATTRS(5),
5596 THERMAL_ATTRS(6),
5597 THERMAL_ATTRS(7),
5598 NULL
5601 static const struct attribute_group thermal_temp_input16_group = {
5602 .attrs = thermal_temp_input_attr
5605 static const struct attribute_group thermal_temp_input8_group = {
5606 .attrs = &thermal_temp_input_attr[8]
5609 #undef THERMAL_SENSOR_ATTR_TEMP
5610 #undef THERMAL_ATTRS
5612 /* --------------------------------------------------------------------- */
5614 static int __init thermal_init(struct ibm_init_struct *iibm)
5616 u8 t, ta1, ta2;
5617 int i;
5618 int acpi_tmp7;
5619 int res;
5621 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5623 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5625 if (thinkpad_id.ec_model) {
5627 * Direct EC access mode: sensors at registers
5628 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5629 * non-implemented, thermal sensors return 0x80 when
5630 * not available
5633 ta1 = ta2 = 0;
5634 for (i = 0; i < 8; i++) {
5635 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5636 ta1 |= t;
5637 } else {
5638 ta1 = 0;
5639 break;
5641 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5642 ta2 |= t;
5643 } else {
5644 ta1 = 0;
5645 break;
5648 if (ta1 == 0) {
5649 /* This is sheer paranoia, but we handle it anyway */
5650 if (acpi_tmp7) {
5651 printk(TPACPI_ERR
5652 "ThinkPad ACPI EC access misbehaving, "
5653 "falling back to ACPI TMPx access "
5654 "mode\n");
5655 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5656 } else {
5657 printk(TPACPI_ERR
5658 "ThinkPad ACPI EC access misbehaving, "
5659 "disabling thermal sensors access\n");
5660 thermal_read_mode = TPACPI_THERMAL_NONE;
5662 } else {
5663 thermal_read_mode =
5664 (ta2 != 0) ?
5665 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5667 } else if (acpi_tmp7) {
5668 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5669 /* 600e/x, 770e, 770x */
5670 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5671 } else {
5672 /* Standard ACPI TMPx access, max 8 sensors */
5673 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5675 } else {
5676 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5677 thermal_read_mode = TPACPI_THERMAL_NONE;
5680 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5681 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5682 thermal_read_mode);
5684 switch (thermal_read_mode) {
5685 case TPACPI_THERMAL_TPEC_16:
5686 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5687 &thermal_temp_input16_group);
5688 if (res)
5689 return res;
5690 break;
5691 case TPACPI_THERMAL_TPEC_8:
5692 case TPACPI_THERMAL_ACPI_TMP07:
5693 case TPACPI_THERMAL_ACPI_UPDT:
5694 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5695 &thermal_temp_input8_group);
5696 if (res)
5697 return res;
5698 break;
5699 case TPACPI_THERMAL_NONE:
5700 default:
5701 return 1;
5704 return 0;
5707 static void thermal_exit(void)
5709 switch (thermal_read_mode) {
5710 case TPACPI_THERMAL_TPEC_16:
5711 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5712 &thermal_temp_input16_group);
5713 break;
5714 case TPACPI_THERMAL_TPEC_8:
5715 case TPACPI_THERMAL_ACPI_TMP07:
5716 case TPACPI_THERMAL_ACPI_UPDT:
5717 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5718 &thermal_temp_input16_group);
5719 break;
5720 case TPACPI_THERMAL_NONE:
5721 default:
5722 break;
5726 static int thermal_read(char *p)
5728 int len = 0;
5729 int n, i;
5730 struct ibm_thermal_sensors_struct t;
5732 n = thermal_get_sensors(&t);
5733 if (unlikely(n < 0))
5734 return n;
5736 len += sprintf(p + len, "temperatures:\t");
5738 if (n > 0) {
5739 for (i = 0; i < (n - 1); i++)
5740 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5741 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5742 } else
5743 len += sprintf(p + len, "not supported\n");
5745 return len;
5748 static struct ibm_struct thermal_driver_data = {
5749 .name = "thermal",
5750 .read = thermal_read,
5751 .exit = thermal_exit,
5754 /*************************************************************************
5755 * EC Dump subdriver
5758 static u8 ecdump_regs[256];
5760 static int ecdump_read(char *p)
5762 int len = 0;
5763 int i, j;
5764 u8 v;
5766 len += sprintf(p + len, "EC "
5767 " +00 +01 +02 +03 +04 +05 +06 +07"
5768 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5769 for (i = 0; i < 256; i += 16) {
5770 len += sprintf(p + len, "EC 0x%02x:", i);
5771 for (j = 0; j < 16; j++) {
5772 if (!acpi_ec_read(i + j, &v))
5773 break;
5774 if (v != ecdump_regs[i + j])
5775 len += sprintf(p + len, " *%02x", v);
5776 else
5777 len += sprintf(p + len, " %02x", v);
5778 ecdump_regs[i + j] = v;
5780 len += sprintf(p + len, "\n");
5781 if (j != 16)
5782 break;
5785 /* These are way too dangerous to advertise openly... */
5786 #if 0
5787 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5788 " (<offset> is 00-ff, <value> is 00-ff)\n");
5789 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5790 " (<offset> is 00-ff, <value> is 0-255)\n");
5791 #endif
5792 return len;
5795 static int ecdump_write(char *buf)
5797 char *cmd;
5798 int i, v;
5800 while ((cmd = next_cmd(&buf))) {
5801 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5802 /* i and v set */
5803 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5804 /* i and v set */
5805 } else
5806 return -EINVAL;
5807 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5808 if (!acpi_ec_write(i, v))
5809 return -EIO;
5810 } else
5811 return -EINVAL;
5814 return 0;
5817 static struct ibm_struct ecdump_driver_data = {
5818 .name = "ecdump",
5819 .read = ecdump_read,
5820 .write = ecdump_write,
5821 .flags.experimental = 1,
5824 /*************************************************************************
5825 * Backlight/brightness subdriver
5828 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5831 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5832 * CMOS NVRAM byte 0x5E, bits 0-3.
5834 * EC HBRV (0x31) has the following layout
5835 * Bit 7: unknown function
5836 * Bit 6: unknown function
5837 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5838 * Bit 4: must be set to zero to avoid problems
5839 * Bit 3-0: backlight brightness level
5841 * brightness_get_raw returns status data in the HBRV layout
5843 * WARNING: The X61 has been verified to use HBRV for something else, so
5844 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5845 * testing on the very early *60 Lenovo models...
5848 enum {
5849 TP_EC_BACKLIGHT = 0x31,
5851 /* TP_EC_BACKLIGHT bitmasks */
5852 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5853 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5854 TP_EC_BACKLIGHT_MAPSW = 0x20,
5857 enum tpacpi_brightness_access_mode {
5858 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5859 TPACPI_BRGHT_MODE_EC, /* EC control */
5860 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5861 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5862 TPACPI_BRGHT_MODE_MAX
5865 static struct backlight_device *ibm_backlight_device;
5867 static enum tpacpi_brightness_access_mode brightness_mode =
5868 TPACPI_BRGHT_MODE_MAX;
5870 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5872 static struct mutex brightness_mutex;
5874 /* NVRAM brightness access,
5875 * call with brightness_mutex held! */
5876 static unsigned int tpacpi_brightness_nvram_get(void)
5878 u8 lnvram;
5880 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5881 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5882 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5883 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5885 return lnvram;
5888 static void tpacpi_brightness_checkpoint_nvram(void)
5890 u8 lec = 0;
5891 u8 b_nvram;
5893 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5894 return;
5896 vdbg_printk(TPACPI_DBG_BRGHT,
5897 "trying to checkpoint backlight level to NVRAM...\n");
5899 if (mutex_lock_killable(&brightness_mutex) < 0)
5900 return;
5902 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5903 goto unlock;
5904 lec &= TP_EC_BACKLIGHT_LVLMSK;
5905 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5907 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5908 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5909 /* NVRAM needs update */
5910 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5911 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5912 b_nvram |= lec;
5913 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5914 dbg_printk(TPACPI_DBG_BRGHT,
5915 "updated NVRAM backlight level to %u (0x%02x)\n",
5916 (unsigned int) lec, (unsigned int) b_nvram);
5917 } else
5918 vdbg_printk(TPACPI_DBG_BRGHT,
5919 "NVRAM backlight level already is %u (0x%02x)\n",
5920 (unsigned int) lec, (unsigned int) b_nvram);
5922 unlock:
5923 mutex_unlock(&brightness_mutex);
5927 /* call with brightness_mutex held! */
5928 static int tpacpi_brightness_get_raw(int *status)
5930 u8 lec = 0;
5932 switch (brightness_mode) {
5933 case TPACPI_BRGHT_MODE_UCMS_STEP:
5934 *status = tpacpi_brightness_nvram_get();
5935 return 0;
5936 case TPACPI_BRGHT_MODE_EC:
5937 case TPACPI_BRGHT_MODE_ECNVRAM:
5938 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5939 return -EIO;
5940 *status = lec;
5941 return 0;
5942 default:
5943 return -ENXIO;
5947 /* call with brightness_mutex held! */
5948 /* do NOT call with illegal backlight level value */
5949 static int tpacpi_brightness_set_ec(unsigned int value)
5951 u8 lec = 0;
5953 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5954 return -EIO;
5956 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5957 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5958 (value & TP_EC_BACKLIGHT_LVLMSK))))
5959 return -EIO;
5961 return 0;
5964 /* call with brightness_mutex held! */
5965 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5967 int cmos_cmd, inc;
5968 unsigned int current_value, i;
5970 current_value = tpacpi_brightness_nvram_get();
5972 if (value == current_value)
5973 return 0;
5975 cmos_cmd = (value > current_value) ?
5976 TP_CMOS_BRIGHTNESS_UP :
5977 TP_CMOS_BRIGHTNESS_DOWN;
5978 inc = (value > current_value) ? 1 : -1;
5980 for (i = current_value; i != value; i += inc)
5981 if (issue_thinkpad_cmos_command(cmos_cmd))
5982 return -EIO;
5984 return 0;
5987 /* May return EINTR which can always be mapped to ERESTARTSYS */
5988 static int brightness_set(unsigned int value)
5990 int res;
5992 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5993 value < 0)
5994 return -EINVAL;
5996 vdbg_printk(TPACPI_DBG_BRGHT,
5997 "set backlight level to %d\n", value);
5999 res = mutex_lock_killable(&brightness_mutex);
6000 if (res < 0)
6001 return res;
6003 switch (brightness_mode) {
6004 case TPACPI_BRGHT_MODE_EC:
6005 case TPACPI_BRGHT_MODE_ECNVRAM:
6006 res = tpacpi_brightness_set_ec(value);
6007 break;
6008 case TPACPI_BRGHT_MODE_UCMS_STEP:
6009 res = tpacpi_brightness_set_ucmsstep(value);
6010 break;
6011 default:
6012 res = -ENXIO;
6015 mutex_unlock(&brightness_mutex);
6016 return res;
6019 /* sysfs backlight class ----------------------------------------------- */
6021 static int brightness_update_status(struct backlight_device *bd)
6023 unsigned int level =
6024 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6025 bd->props.power == FB_BLANK_UNBLANK) ?
6026 bd->props.brightness : 0;
6028 dbg_printk(TPACPI_DBG_BRGHT,
6029 "backlight: attempt to set level to %d\n",
6030 level);
6032 /* it is the backlight class's job (caller) to handle
6033 * EINTR and other errors properly */
6034 return brightness_set(level);
6037 static int brightness_get(struct backlight_device *bd)
6039 int status, res;
6041 res = mutex_lock_killable(&brightness_mutex);
6042 if (res < 0)
6043 return 0;
6045 res = tpacpi_brightness_get_raw(&status);
6047 mutex_unlock(&brightness_mutex);
6049 if (res < 0)
6050 return 0;
6052 return status & TP_EC_BACKLIGHT_LVLMSK;
6055 static struct backlight_ops ibm_backlight_data = {
6056 .get_brightness = brightness_get,
6057 .update_status = brightness_update_status,
6060 /* --------------------------------------------------------------------- */
6063 * These are only useful for models that have only one possibility
6064 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6065 * these quirks.
6067 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6068 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6069 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6071 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6072 /* Models with ATI GPUs known to require ECNVRAM mode */
6073 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6075 /* Models with ATI GPUs that can use ECNVRAM */
6076 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6077 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6078 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6079 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6081 /* Models with Intel Extreme Graphics 2 */
6082 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6083 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6084 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6086 /* Models with Intel GMA900 */
6087 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6088 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6089 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6092 static int __init brightness_init(struct ibm_init_struct *iibm)
6094 int b;
6095 unsigned long quirks;
6097 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6099 mutex_init(&brightness_mutex);
6101 quirks = tpacpi_check_quirks(brightness_quirk_table,
6102 ARRAY_SIZE(brightness_quirk_table));
6105 * We always attempt to detect acpi support, so as to switch
6106 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6107 * going to publish a backlight interface
6109 b = tpacpi_check_std_acpi_brightness_support();
6110 if (b > 0) {
6112 if (acpi_video_backlight_support()) {
6113 if (brightness_enable > 1) {
6114 printk(TPACPI_NOTICE
6115 "Standard ACPI backlight interface "
6116 "available, not loading native one.\n");
6117 return 1;
6118 } else if (brightness_enable == 1) {
6119 printk(TPACPI_NOTICE
6120 "Backlight control force enabled, even if standard "
6121 "ACPI backlight interface is available\n");
6123 } else {
6124 if (brightness_enable > 1) {
6125 printk(TPACPI_NOTICE
6126 "Standard ACPI backlight interface not "
6127 "available, thinkpad_acpi native "
6128 "brightness control enabled\n");
6133 if (!brightness_enable) {
6134 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6135 "brightness support disabled by "
6136 "module parameter\n");
6137 return 1;
6140 if (b > 16) {
6141 printk(TPACPI_ERR
6142 "Unsupported brightness interface, "
6143 "please contact %s\n", TPACPI_MAIL);
6144 return 1;
6146 if (b == 16)
6147 tp_features.bright_16levels = 1;
6150 * Check for module parameter bogosity, note that we
6151 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6152 * able to detect "unspecified"
6154 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6155 return -EINVAL;
6157 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6158 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6159 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6160 if (quirks & TPACPI_BRGHT_Q_EC)
6161 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6162 else
6163 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6165 dbg_printk(TPACPI_DBG_BRGHT,
6166 "driver auto-selected brightness_mode=%d\n",
6167 brightness_mode);
6170 /* Safety */
6171 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6172 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6173 brightness_mode == TPACPI_BRGHT_MODE_EC))
6174 return -EINVAL;
6176 if (tpacpi_brightness_get_raw(&b) < 0)
6177 return 1;
6179 if (tp_features.bright_16levels)
6180 printk(TPACPI_INFO
6181 "detected a 16-level brightness capable ThinkPad\n");
6183 ibm_backlight_device = backlight_device_register(
6184 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6185 &ibm_backlight_data);
6186 if (IS_ERR(ibm_backlight_device)) {
6187 int rc = PTR_ERR(ibm_backlight_device);
6188 ibm_backlight_device = NULL;
6189 printk(TPACPI_ERR "Could not register backlight device\n");
6190 return rc;
6192 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6193 "brightness is supported\n");
6195 if (quirks & TPACPI_BRGHT_Q_ASK) {
6196 printk(TPACPI_NOTICE
6197 "brightness: will use unverified default: "
6198 "brightness_mode=%d\n", brightness_mode);
6199 printk(TPACPI_NOTICE
6200 "brightness: please report to %s whether it works well "
6201 "or not on your ThinkPad\n", TPACPI_MAIL);
6204 ibm_backlight_device->props.max_brightness =
6205 (tp_features.bright_16levels)? 15 : 7;
6206 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6207 backlight_update_status(ibm_backlight_device);
6209 return 0;
6212 static void brightness_suspend(pm_message_t state)
6214 tpacpi_brightness_checkpoint_nvram();
6217 static void brightness_shutdown(void)
6219 tpacpi_brightness_checkpoint_nvram();
6222 static void brightness_exit(void)
6224 if (ibm_backlight_device) {
6225 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6226 "calling backlight_device_unregister()\n");
6227 backlight_device_unregister(ibm_backlight_device);
6230 tpacpi_brightness_checkpoint_nvram();
6233 static int brightness_read(char *p)
6235 int len = 0;
6236 int level;
6238 level = brightness_get(NULL);
6239 if (level < 0) {
6240 len += sprintf(p + len, "level:\t\tunreadable\n");
6241 } else {
6242 len += sprintf(p + len, "level:\t\t%d\n", level);
6243 len += sprintf(p + len, "commands:\tup, down\n");
6244 len += sprintf(p + len, "commands:\tlevel <level>"
6245 " (<level> is 0-%d)\n",
6246 (tp_features.bright_16levels) ? 15 : 7);
6249 return len;
6252 static int brightness_write(char *buf)
6254 int level;
6255 int rc;
6256 char *cmd;
6257 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6259 level = brightness_get(NULL);
6260 if (level < 0)
6261 return level;
6263 while ((cmd = next_cmd(&buf))) {
6264 if (strlencmp(cmd, "up") == 0) {
6265 if (level < max_level)
6266 level++;
6267 } else if (strlencmp(cmd, "down") == 0) {
6268 if (level > 0)
6269 level--;
6270 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6271 level >= 0 && level <= max_level) {
6272 /* new level set */
6273 } else
6274 return -EINVAL;
6277 tpacpi_disclose_usertask("procfs brightness",
6278 "set level to %d\n", level);
6281 * Now we know what the final level should be, so we try to set it.
6282 * Doing it this way makes the syscall restartable in case of EINTR
6284 rc = brightness_set(level);
6285 return (rc == -EINTR)? ERESTARTSYS : rc;
6288 static struct ibm_struct brightness_driver_data = {
6289 .name = "brightness",
6290 .read = brightness_read,
6291 .write = brightness_write,
6292 .exit = brightness_exit,
6293 .suspend = brightness_suspend,
6294 .shutdown = brightness_shutdown,
6297 /*************************************************************************
6298 * Volume subdriver
6301 static int volume_offset = 0x30;
6303 static int volume_read(char *p)
6305 int len = 0;
6306 u8 level;
6308 if (!acpi_ec_read(volume_offset, &level)) {
6309 len += sprintf(p + len, "level:\t\tunreadable\n");
6310 } else {
6311 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6312 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6313 len += sprintf(p + len, "commands:\tup, down, mute\n");
6314 len += sprintf(p + len, "commands:\tlevel <level>"
6315 " (<level> is 0-15)\n");
6318 return len;
6321 static int volume_write(char *buf)
6323 int cmos_cmd, inc, i;
6324 u8 level, mute;
6325 int new_level, new_mute;
6326 char *cmd;
6328 while ((cmd = next_cmd(&buf))) {
6329 if (!acpi_ec_read(volume_offset, &level))
6330 return -EIO;
6331 new_mute = mute = level & 0x40;
6332 new_level = level = level & 0xf;
6334 if (strlencmp(cmd, "up") == 0) {
6335 if (mute)
6336 new_mute = 0;
6337 else
6338 new_level = level == 15 ? 15 : level + 1;
6339 } else if (strlencmp(cmd, "down") == 0) {
6340 if (mute)
6341 new_mute = 0;
6342 else
6343 new_level = level == 0 ? 0 : level - 1;
6344 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6345 new_level >= 0 && new_level <= 15) {
6346 /* new_level set */
6347 } else if (strlencmp(cmd, "mute") == 0) {
6348 new_mute = 0x40;
6349 } else
6350 return -EINVAL;
6352 if (new_level != level) {
6353 /* mute doesn't change */
6355 cmos_cmd = (new_level > level) ?
6356 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6357 inc = new_level > level ? 1 : -1;
6359 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6360 !acpi_ec_write(volume_offset, level)))
6361 return -EIO;
6363 for (i = level; i != new_level; i += inc)
6364 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6365 !acpi_ec_write(volume_offset, i + inc))
6366 return -EIO;
6368 if (mute &&
6369 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6370 !acpi_ec_write(volume_offset, new_level + mute))) {
6371 return -EIO;
6375 if (new_mute != mute) {
6376 /* level doesn't change */
6378 cmos_cmd = (new_mute) ?
6379 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6381 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6382 !acpi_ec_write(volume_offset, level + new_mute))
6383 return -EIO;
6387 return 0;
6390 static struct ibm_struct volume_driver_data = {
6391 .name = "volume",
6392 .read = volume_read,
6393 .write = volume_write,
6396 /*************************************************************************
6397 * Fan subdriver
6401 * FAN ACCESS MODES
6403 * TPACPI_FAN_RD_ACPI_GFAN:
6404 * ACPI GFAN method: returns fan level
6406 * see TPACPI_FAN_WR_ACPI_SFAN
6407 * EC 0x2f (HFSP) not available if GFAN exists
6409 * TPACPI_FAN_WR_ACPI_SFAN:
6410 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6412 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6413 * it for writing.
6415 * TPACPI_FAN_WR_TPEC:
6416 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6417 * Supported on almost all ThinkPads
6419 * Fan speed changes of any sort (including those caused by the
6420 * disengaged mode) are usually done slowly by the firmware as the
6421 * maximum ammount of fan duty cycle change per second seems to be
6422 * limited.
6424 * Reading is not available if GFAN exists.
6425 * Writing is not available if SFAN exists.
6427 * Bits
6428 * 7 automatic mode engaged;
6429 * (default operation mode of the ThinkPad)
6430 * fan level is ignored in this mode.
6431 * 6 full speed mode (takes precedence over bit 7);
6432 * not available on all thinkpads. May disable
6433 * the tachometer while the fan controller ramps up
6434 * the speed (which can take up to a few *minutes*).
6435 * Speeds up fan to 100% duty-cycle, which is far above
6436 * the standard RPM levels. It is not impossible that
6437 * it could cause hardware damage.
6438 * 5-3 unused in some models. Extra bits for fan level
6439 * in others, but still useless as all values above
6440 * 7 map to the same speed as level 7 in these models.
6441 * 2-0 fan level (0..7 usually)
6442 * 0x00 = stop
6443 * 0x07 = max (set when temperatures critical)
6444 * Some ThinkPads may have other levels, see
6445 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6447 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6448 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6449 * does so, its initial value is meaningless (0x07).
6451 * For firmware bugs, refer to:
6452 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6454 * ----
6456 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6457 * Main fan tachometer reading (in RPM)
6459 * This register is present on all ThinkPads with a new-style EC, and
6460 * it is known not to be present on the A21m/e, and T22, as there is
6461 * something else in offset 0x84 according to the ACPI DSDT. Other
6462 * ThinkPads from this same time period (and earlier) probably lack the
6463 * tachometer as well.
6465 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6466 * was never fixed by IBM to report the EC firmware version string
6467 * probably support the tachometer (like the early X models), so
6468 * detecting it is quite hard. We need more data to know for sure.
6470 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6471 * might result.
6473 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6474 * mode.
6476 * For firmware bugs, refer to:
6477 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6479 * ----
6481 * ThinkPad EC register 0x31 bit 0 (only on select models)
6483 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6484 * show the speed of the main fan. When bit 0 of EC register 0x31
6485 * is one, the tachometer registers show the speed of the auxiliary
6486 * fan.
6488 * Fan control seems to affect both fans, regardless of the state
6489 * of this bit.
6491 * So far, only the firmware for the X60/X61 non-tablet versions
6492 * seem to support this (firmware TP-7M).
6494 * TPACPI_FAN_WR_ACPI_FANS:
6495 * ThinkPad X31, X40, X41. Not available in the X60.
6497 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6498 * high speed. ACPI DSDT seems to map these three speeds to levels
6499 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6500 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6502 * The speeds are stored on handles
6503 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6505 * There are three default speed sets, acessible as handles:
6506 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6508 * ACPI DSDT switches which set is in use depending on various
6509 * factors.
6511 * TPACPI_FAN_WR_TPEC is also available and should be used to
6512 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6513 * but the ACPI tables just mention level 7.
6516 enum { /* Fan control constants */
6517 fan_status_offset = 0x2f, /* EC register 0x2f */
6518 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6519 * 0x84 must be read before 0x85 */
6520 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6521 bit 0 selects which fan is active */
6523 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6524 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6526 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6529 enum fan_status_access_mode {
6530 TPACPI_FAN_NONE = 0, /* No fan status or control */
6531 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6532 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6535 enum fan_control_access_mode {
6536 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6537 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6538 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6539 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6542 enum fan_control_commands {
6543 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6544 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6545 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6546 * and also watchdog cmd */
6549 static int fan_control_allowed;
6551 static enum fan_status_access_mode fan_status_access_mode;
6552 static enum fan_control_access_mode fan_control_access_mode;
6553 static enum fan_control_commands fan_control_commands;
6555 static u8 fan_control_initial_status;
6556 static u8 fan_control_desired_level;
6557 static u8 fan_control_resume_level;
6558 static int fan_watchdog_maxinterval;
6560 static struct mutex fan_mutex;
6562 static void fan_watchdog_fire(struct work_struct *ignored);
6563 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6565 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6566 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6567 "\\FSPD", /* 600e/x, 770e, 770x */
6568 ); /* all others */
6569 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6570 "JFNS", /* 770x-JL */
6571 ); /* all others */
6574 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6575 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6576 * be in auto mode (0x80).
6578 * This is corrected by any write to HFSP either by the driver, or
6579 * by the firmware.
6581 * We assume 0x07 really means auto mode while this quirk is active,
6582 * as this is far more likely than the ThinkPad being in level 7,
6583 * which is only used by the firmware during thermal emergencies.
6585 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6586 * TP-70 (T43, R52), which are known to be buggy.
6589 static void fan_quirk1_setup(void)
6591 if (fan_control_initial_status == 0x07) {
6592 printk(TPACPI_NOTICE
6593 "fan_init: initial fan status is unknown, "
6594 "assuming it is in auto mode\n");
6595 tp_features.fan_ctrl_status_undef = 1;
6599 static void fan_quirk1_handle(u8 *fan_status)
6601 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6602 if (*fan_status != fan_control_initial_status) {
6603 /* something changed the HFSP regisnter since
6604 * driver init time, so it is not undefined
6605 * anymore */
6606 tp_features.fan_ctrl_status_undef = 0;
6607 } else {
6608 /* Return most likely status. In fact, it
6609 * might be the only possible status */
6610 *fan_status = TP_EC_FAN_AUTO;
6615 /* Select main fan on X60/X61, NOOP on others */
6616 static bool fan_select_fan1(void)
6618 if (tp_features.second_fan) {
6619 u8 val;
6621 if (ec_read(fan_select_offset, &val) < 0)
6622 return false;
6623 val &= 0xFEU;
6624 if (ec_write(fan_select_offset, val) < 0)
6625 return false;
6627 return true;
6630 /* Select secondary fan on X60/X61 */
6631 static bool fan_select_fan2(void)
6633 u8 val;
6635 if (!tp_features.second_fan)
6636 return false;
6638 if (ec_read(fan_select_offset, &val) < 0)
6639 return false;
6640 val |= 0x01U;
6641 if (ec_write(fan_select_offset, val) < 0)
6642 return false;
6644 return true;
6648 * Call with fan_mutex held
6650 static void fan_update_desired_level(u8 status)
6652 if ((status &
6653 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6654 if (status > 7)
6655 fan_control_desired_level = 7;
6656 else
6657 fan_control_desired_level = status;
6661 static int fan_get_status(u8 *status)
6663 u8 s;
6665 /* TODO:
6666 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6668 switch (fan_status_access_mode) {
6669 case TPACPI_FAN_RD_ACPI_GFAN:
6670 /* 570, 600e/x, 770e, 770x */
6672 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6673 return -EIO;
6675 if (likely(status))
6676 *status = s & 0x07;
6678 break;
6680 case TPACPI_FAN_RD_TPEC:
6681 /* all except 570, 600e/x, 770e, 770x */
6682 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6683 return -EIO;
6685 if (likely(status)) {
6686 *status = s;
6687 fan_quirk1_handle(status);
6690 break;
6692 default:
6693 return -ENXIO;
6696 return 0;
6699 static int fan_get_status_safe(u8 *status)
6701 int rc;
6702 u8 s;
6704 if (mutex_lock_killable(&fan_mutex))
6705 return -ERESTARTSYS;
6706 rc = fan_get_status(&s);
6707 if (!rc)
6708 fan_update_desired_level(s);
6709 mutex_unlock(&fan_mutex);
6711 if (status)
6712 *status = s;
6714 return rc;
6717 static int fan_get_speed(unsigned int *speed)
6719 u8 hi, lo;
6721 switch (fan_status_access_mode) {
6722 case TPACPI_FAN_RD_TPEC:
6723 /* all except 570, 600e/x, 770e, 770x */
6724 if (unlikely(!fan_select_fan1()))
6725 return -EIO;
6726 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6727 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6728 return -EIO;
6730 if (likely(speed))
6731 *speed = (hi << 8) | lo;
6733 break;
6735 default:
6736 return -ENXIO;
6739 return 0;
6742 static int fan2_get_speed(unsigned int *speed)
6744 u8 hi, lo;
6745 bool rc;
6747 switch (fan_status_access_mode) {
6748 case TPACPI_FAN_RD_TPEC:
6749 /* all except 570, 600e/x, 770e, 770x */
6750 if (unlikely(!fan_select_fan2()))
6751 return -EIO;
6752 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6753 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6754 fan_select_fan1(); /* play it safe */
6755 if (rc)
6756 return -EIO;
6758 if (likely(speed))
6759 *speed = (hi << 8) | lo;
6761 break;
6763 default:
6764 return -ENXIO;
6767 return 0;
6770 static int fan_set_level(int level)
6772 if (!fan_control_allowed)
6773 return -EPERM;
6775 switch (fan_control_access_mode) {
6776 case TPACPI_FAN_WR_ACPI_SFAN:
6777 if (level >= 0 && level <= 7) {
6778 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6779 return -EIO;
6780 } else
6781 return -EINVAL;
6782 break;
6784 case TPACPI_FAN_WR_ACPI_FANS:
6785 case TPACPI_FAN_WR_TPEC:
6786 if (!(level & TP_EC_FAN_AUTO) &&
6787 !(level & TP_EC_FAN_FULLSPEED) &&
6788 ((level < 0) || (level > 7)))
6789 return -EINVAL;
6791 /* safety net should the EC not support AUTO
6792 * or FULLSPEED mode bits and just ignore them */
6793 if (level & TP_EC_FAN_FULLSPEED)
6794 level |= 7; /* safety min speed 7 */
6795 else if (level & TP_EC_FAN_AUTO)
6796 level |= 4; /* safety min speed 4 */
6798 if (!acpi_ec_write(fan_status_offset, level))
6799 return -EIO;
6800 else
6801 tp_features.fan_ctrl_status_undef = 0;
6802 break;
6804 default:
6805 return -ENXIO;
6808 vdbg_printk(TPACPI_DBG_FAN,
6809 "fan control: set fan control register to 0x%02x\n", level);
6810 return 0;
6813 static int fan_set_level_safe(int level)
6815 int rc;
6817 if (!fan_control_allowed)
6818 return -EPERM;
6820 if (mutex_lock_killable(&fan_mutex))
6821 return -ERESTARTSYS;
6823 if (level == TPACPI_FAN_LAST_LEVEL)
6824 level = fan_control_desired_level;
6826 rc = fan_set_level(level);
6827 if (!rc)
6828 fan_update_desired_level(level);
6830 mutex_unlock(&fan_mutex);
6831 return rc;
6834 static int fan_set_enable(void)
6836 u8 s;
6837 int rc;
6839 if (!fan_control_allowed)
6840 return -EPERM;
6842 if (mutex_lock_killable(&fan_mutex))
6843 return -ERESTARTSYS;
6845 switch (fan_control_access_mode) {
6846 case TPACPI_FAN_WR_ACPI_FANS:
6847 case TPACPI_FAN_WR_TPEC:
6848 rc = fan_get_status(&s);
6849 if (rc < 0)
6850 break;
6852 /* Don't go out of emergency fan mode */
6853 if (s != 7) {
6854 s &= 0x07;
6855 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6858 if (!acpi_ec_write(fan_status_offset, s))
6859 rc = -EIO;
6860 else {
6861 tp_features.fan_ctrl_status_undef = 0;
6862 rc = 0;
6864 break;
6866 case TPACPI_FAN_WR_ACPI_SFAN:
6867 rc = fan_get_status(&s);
6868 if (rc < 0)
6869 break;
6871 s &= 0x07;
6873 /* Set fan to at least level 4 */
6874 s |= 4;
6876 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6877 rc = -EIO;
6878 else
6879 rc = 0;
6880 break;
6882 default:
6883 rc = -ENXIO;
6886 mutex_unlock(&fan_mutex);
6888 if (!rc)
6889 vdbg_printk(TPACPI_DBG_FAN,
6890 "fan control: set fan control register to 0x%02x\n",
6892 return rc;
6895 static int fan_set_disable(void)
6897 int rc;
6899 if (!fan_control_allowed)
6900 return -EPERM;
6902 if (mutex_lock_killable(&fan_mutex))
6903 return -ERESTARTSYS;
6905 rc = 0;
6906 switch (fan_control_access_mode) {
6907 case TPACPI_FAN_WR_ACPI_FANS:
6908 case TPACPI_FAN_WR_TPEC:
6909 if (!acpi_ec_write(fan_status_offset, 0x00))
6910 rc = -EIO;
6911 else {
6912 fan_control_desired_level = 0;
6913 tp_features.fan_ctrl_status_undef = 0;
6915 break;
6917 case TPACPI_FAN_WR_ACPI_SFAN:
6918 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6919 rc = -EIO;
6920 else
6921 fan_control_desired_level = 0;
6922 break;
6924 default:
6925 rc = -ENXIO;
6928 if (!rc)
6929 vdbg_printk(TPACPI_DBG_FAN,
6930 "fan control: set fan control register to 0\n");
6932 mutex_unlock(&fan_mutex);
6933 return rc;
6936 static int fan_set_speed(int speed)
6938 int rc;
6940 if (!fan_control_allowed)
6941 return -EPERM;
6943 if (mutex_lock_killable(&fan_mutex))
6944 return -ERESTARTSYS;
6946 rc = 0;
6947 switch (fan_control_access_mode) {
6948 case TPACPI_FAN_WR_ACPI_FANS:
6949 if (speed >= 0 && speed <= 65535) {
6950 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6951 speed, speed, speed))
6952 rc = -EIO;
6953 } else
6954 rc = -EINVAL;
6955 break;
6957 default:
6958 rc = -ENXIO;
6961 mutex_unlock(&fan_mutex);
6962 return rc;
6965 static void fan_watchdog_reset(void)
6967 static int fan_watchdog_active;
6969 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6970 return;
6972 if (fan_watchdog_active)
6973 cancel_delayed_work(&fan_watchdog_task);
6975 if (fan_watchdog_maxinterval > 0 &&
6976 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6977 fan_watchdog_active = 1;
6978 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6979 msecs_to_jiffies(fan_watchdog_maxinterval
6980 * 1000))) {
6981 printk(TPACPI_ERR
6982 "failed to queue the fan watchdog, "
6983 "watchdog will not trigger\n");
6985 } else
6986 fan_watchdog_active = 0;
6989 static void fan_watchdog_fire(struct work_struct *ignored)
6991 int rc;
6993 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6994 return;
6996 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6997 rc = fan_set_enable();
6998 if (rc < 0) {
6999 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7000 "will try again later...\n", -rc);
7001 /* reschedule for later */
7002 fan_watchdog_reset();
7007 * SYSFS fan layout: hwmon compatible (device)
7009 * pwm*_enable:
7010 * 0: "disengaged" mode
7011 * 1: manual mode
7012 * 2: native EC "auto" mode (recommended, hardware default)
7014 * pwm*: set speed in manual mode, ignored otherwise.
7015 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7016 * interpolation.
7018 * fan*_input: tachometer reading, RPM
7021 * SYSFS fan layout: extensions
7023 * fan_watchdog (driver):
7024 * fan watchdog interval in seconds, 0 disables (default), max 120
7027 /* sysfs fan pwm1_enable ----------------------------------------------- */
7028 static ssize_t fan_pwm1_enable_show(struct device *dev,
7029 struct device_attribute *attr,
7030 char *buf)
7032 int res, mode;
7033 u8 status;
7035 res = fan_get_status_safe(&status);
7036 if (res)
7037 return res;
7039 if (status & TP_EC_FAN_FULLSPEED) {
7040 mode = 0;
7041 } else if (status & TP_EC_FAN_AUTO) {
7042 mode = 2;
7043 } else
7044 mode = 1;
7046 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7049 static ssize_t fan_pwm1_enable_store(struct device *dev,
7050 struct device_attribute *attr,
7051 const char *buf, size_t count)
7053 unsigned long t;
7054 int res, level;
7056 if (parse_strtoul(buf, 2, &t))
7057 return -EINVAL;
7059 tpacpi_disclose_usertask("hwmon pwm1_enable",
7060 "set fan mode to %lu\n", t);
7062 switch (t) {
7063 case 0:
7064 level = TP_EC_FAN_FULLSPEED;
7065 break;
7066 case 1:
7067 level = TPACPI_FAN_LAST_LEVEL;
7068 break;
7069 case 2:
7070 level = TP_EC_FAN_AUTO;
7071 break;
7072 case 3:
7073 /* reserved for software-controlled auto mode */
7074 return -ENOSYS;
7075 default:
7076 return -EINVAL;
7079 res = fan_set_level_safe(level);
7080 if (res == -ENXIO)
7081 return -EINVAL;
7082 else if (res < 0)
7083 return res;
7085 fan_watchdog_reset();
7087 return count;
7090 static struct device_attribute dev_attr_fan_pwm1_enable =
7091 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7092 fan_pwm1_enable_show, fan_pwm1_enable_store);
7094 /* sysfs fan pwm1 ------------------------------------------------------ */
7095 static ssize_t fan_pwm1_show(struct device *dev,
7096 struct device_attribute *attr,
7097 char *buf)
7099 int res;
7100 u8 status;
7102 res = fan_get_status_safe(&status);
7103 if (res)
7104 return res;
7106 if ((status &
7107 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7108 status = fan_control_desired_level;
7110 if (status > 7)
7111 status = 7;
7113 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7116 static ssize_t fan_pwm1_store(struct device *dev,
7117 struct device_attribute *attr,
7118 const char *buf, size_t count)
7120 unsigned long s;
7121 int rc;
7122 u8 status, newlevel;
7124 if (parse_strtoul(buf, 255, &s))
7125 return -EINVAL;
7127 tpacpi_disclose_usertask("hwmon pwm1",
7128 "set fan speed to %lu\n", s);
7130 /* scale down from 0-255 to 0-7 */
7131 newlevel = (s >> 5) & 0x07;
7133 if (mutex_lock_killable(&fan_mutex))
7134 return -ERESTARTSYS;
7136 rc = fan_get_status(&status);
7137 if (!rc && (status &
7138 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7139 rc = fan_set_level(newlevel);
7140 if (rc == -ENXIO)
7141 rc = -EINVAL;
7142 else if (!rc) {
7143 fan_update_desired_level(newlevel);
7144 fan_watchdog_reset();
7148 mutex_unlock(&fan_mutex);
7149 return (rc)? rc : count;
7152 static struct device_attribute dev_attr_fan_pwm1 =
7153 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7154 fan_pwm1_show, fan_pwm1_store);
7156 /* sysfs fan fan1_input ------------------------------------------------ */
7157 static ssize_t fan_fan1_input_show(struct device *dev,
7158 struct device_attribute *attr,
7159 char *buf)
7161 int res;
7162 unsigned int speed;
7164 res = fan_get_speed(&speed);
7165 if (res < 0)
7166 return res;
7168 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7171 static struct device_attribute dev_attr_fan_fan1_input =
7172 __ATTR(fan1_input, S_IRUGO,
7173 fan_fan1_input_show, NULL);
7175 /* sysfs fan fan2_input ------------------------------------------------ */
7176 static ssize_t fan_fan2_input_show(struct device *dev,
7177 struct device_attribute *attr,
7178 char *buf)
7180 int res;
7181 unsigned int speed;
7183 res = fan2_get_speed(&speed);
7184 if (res < 0)
7185 return res;
7187 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7190 static struct device_attribute dev_attr_fan_fan2_input =
7191 __ATTR(fan2_input, S_IRUGO,
7192 fan_fan2_input_show, NULL);
7194 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7195 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7196 char *buf)
7198 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7201 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7202 const char *buf, size_t count)
7204 unsigned long t;
7206 if (parse_strtoul(buf, 120, &t))
7207 return -EINVAL;
7209 if (!fan_control_allowed)
7210 return -EPERM;
7212 fan_watchdog_maxinterval = t;
7213 fan_watchdog_reset();
7215 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7217 return count;
7220 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7221 fan_fan_watchdog_show, fan_fan_watchdog_store);
7223 /* --------------------------------------------------------------------- */
7224 static struct attribute *fan_attributes[] = {
7225 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7226 &dev_attr_fan_fan1_input.attr,
7227 NULL, /* for fan2_input */
7228 NULL
7231 static const struct attribute_group fan_attr_group = {
7232 .attrs = fan_attributes,
7235 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7236 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7238 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7239 { .vendor = PCI_VENDOR_ID_IBM, \
7240 .bios = TPACPI_MATCH_ANY, \
7241 .ec = TPID(__id1, __id2), \
7242 .quirks = __quirks }
7244 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7245 { .vendor = PCI_VENDOR_ID_LENOVO, \
7246 .bios = TPACPI_MATCH_ANY, \
7247 .ec = TPID(__id1, __id2), \
7248 .quirks = __quirks }
7250 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7251 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7252 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7253 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7254 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7255 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7258 #undef TPACPI_FAN_QL
7259 #undef TPACPI_FAN_QI
7261 static int __init fan_init(struct ibm_init_struct *iibm)
7263 int rc;
7264 unsigned long quirks;
7266 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7267 "initializing fan subdriver\n");
7269 mutex_init(&fan_mutex);
7270 fan_status_access_mode = TPACPI_FAN_NONE;
7271 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7272 fan_control_commands = 0;
7273 fan_watchdog_maxinterval = 0;
7274 tp_features.fan_ctrl_status_undef = 0;
7275 tp_features.second_fan = 0;
7276 fan_control_desired_level = 7;
7278 TPACPI_ACPIHANDLE_INIT(fans);
7279 TPACPI_ACPIHANDLE_INIT(gfan);
7280 TPACPI_ACPIHANDLE_INIT(sfan);
7282 quirks = tpacpi_check_quirks(fan_quirk_table,
7283 ARRAY_SIZE(fan_quirk_table));
7285 if (gfan_handle) {
7286 /* 570, 600e/x, 770e, 770x */
7287 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7288 } else {
7289 /* all other ThinkPads: note that even old-style
7290 * ThinkPad ECs supports the fan control register */
7291 if (likely(acpi_ec_read(fan_status_offset,
7292 &fan_control_initial_status))) {
7293 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7294 if (quirks & TPACPI_FAN_Q1)
7295 fan_quirk1_setup();
7296 if (quirks & TPACPI_FAN_2FAN) {
7297 tp_features.second_fan = 1;
7298 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7299 "secondary fan support enabled\n");
7301 } else {
7302 printk(TPACPI_ERR
7303 "ThinkPad ACPI EC access misbehaving, "
7304 "fan status and control unavailable\n");
7305 return 1;
7309 if (sfan_handle) {
7310 /* 570, 770x-JL */
7311 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7312 fan_control_commands |=
7313 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7314 } else {
7315 if (!gfan_handle) {
7316 /* gfan without sfan means no fan control */
7317 /* all other models implement TP EC 0x2f control */
7319 if (fans_handle) {
7320 /* X31, X40, X41 */
7321 fan_control_access_mode =
7322 TPACPI_FAN_WR_ACPI_FANS;
7323 fan_control_commands |=
7324 TPACPI_FAN_CMD_SPEED |
7325 TPACPI_FAN_CMD_LEVEL |
7326 TPACPI_FAN_CMD_ENABLE;
7327 } else {
7328 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7329 fan_control_commands |=
7330 TPACPI_FAN_CMD_LEVEL |
7331 TPACPI_FAN_CMD_ENABLE;
7336 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7337 "fan is %s, modes %d, %d\n",
7338 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7339 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7340 fan_status_access_mode, fan_control_access_mode);
7342 /* fan control master switch */
7343 if (!fan_control_allowed) {
7344 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7345 fan_control_commands = 0;
7346 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7347 "fan control features disabled by parameter\n");
7350 /* update fan_control_desired_level */
7351 if (fan_status_access_mode != TPACPI_FAN_NONE)
7352 fan_get_status_safe(NULL);
7354 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7355 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7356 if (tp_features.second_fan) {
7357 /* attach second fan tachometer */
7358 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7359 &dev_attr_fan_fan2_input.attr;
7361 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7362 &fan_attr_group);
7363 if (rc < 0)
7364 return rc;
7366 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7367 &driver_attr_fan_watchdog);
7368 if (rc < 0) {
7369 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7370 &fan_attr_group);
7371 return rc;
7373 return 0;
7374 } else
7375 return 1;
7378 static void fan_exit(void)
7380 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7381 "cancelling any pending fan watchdog tasks\n");
7383 /* FIXME: can we really do this unconditionally? */
7384 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7385 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7386 &driver_attr_fan_watchdog);
7388 cancel_delayed_work(&fan_watchdog_task);
7389 flush_workqueue(tpacpi_wq);
7392 static void fan_suspend(pm_message_t state)
7394 int rc;
7396 if (!fan_control_allowed)
7397 return;
7399 /* Store fan status in cache */
7400 fan_control_resume_level = 0;
7401 rc = fan_get_status_safe(&fan_control_resume_level);
7402 if (rc < 0)
7403 printk(TPACPI_NOTICE
7404 "failed to read fan level for later "
7405 "restore during resume: %d\n", rc);
7407 /* if it is undefined, don't attempt to restore it.
7408 * KEEP THIS LAST */
7409 if (tp_features.fan_ctrl_status_undef)
7410 fan_control_resume_level = 0;
7413 static void fan_resume(void)
7415 u8 current_level = 7;
7416 bool do_set = false;
7417 int rc;
7419 /* DSDT *always* updates status on resume */
7420 tp_features.fan_ctrl_status_undef = 0;
7422 if (!fan_control_allowed ||
7423 !fan_control_resume_level ||
7424 (fan_get_status_safe(&current_level) < 0))
7425 return;
7427 switch (fan_control_access_mode) {
7428 case TPACPI_FAN_WR_ACPI_SFAN:
7429 /* never decrease fan level */
7430 do_set = (fan_control_resume_level > current_level);
7431 break;
7432 case TPACPI_FAN_WR_ACPI_FANS:
7433 case TPACPI_FAN_WR_TPEC:
7434 /* never decrease fan level, scale is:
7435 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7437 * We expect the firmware to set either 7 or AUTO, but we
7438 * handle FULLSPEED out of paranoia.
7440 * So, we can safely only restore FULLSPEED or 7, anything
7441 * else could slow the fan. Restoring AUTO is useless, at
7442 * best that's exactly what the DSDT already set (it is the
7443 * slower it uses).
7445 * Always keep in mind that the DSDT *will* have set the
7446 * fans to what the vendor supposes is the best level. We
7447 * muck with it only to speed the fan up.
7449 if (fan_control_resume_level != 7 &&
7450 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7451 return;
7452 else
7453 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7454 (current_level != fan_control_resume_level);
7455 break;
7456 default:
7457 return;
7459 if (do_set) {
7460 printk(TPACPI_NOTICE
7461 "restoring fan level to 0x%02x\n",
7462 fan_control_resume_level);
7463 rc = fan_set_level_safe(fan_control_resume_level);
7464 if (rc < 0)
7465 printk(TPACPI_NOTICE
7466 "failed to restore fan level: %d\n", rc);
7470 static int fan_read(char *p)
7472 int len = 0;
7473 int rc;
7474 u8 status;
7475 unsigned int speed = 0;
7477 switch (fan_status_access_mode) {
7478 case TPACPI_FAN_RD_ACPI_GFAN:
7479 /* 570, 600e/x, 770e, 770x */
7480 rc = fan_get_status_safe(&status);
7481 if (rc < 0)
7482 return rc;
7484 len += sprintf(p + len, "status:\t\t%s\n"
7485 "level:\t\t%d\n",
7486 (status != 0) ? "enabled" : "disabled", status);
7487 break;
7489 case TPACPI_FAN_RD_TPEC:
7490 /* all except 570, 600e/x, 770e, 770x */
7491 rc = fan_get_status_safe(&status);
7492 if (rc < 0)
7493 return rc;
7495 len += sprintf(p + len, "status:\t\t%s\n",
7496 (status != 0) ? "enabled" : "disabled");
7498 rc = fan_get_speed(&speed);
7499 if (rc < 0)
7500 return rc;
7502 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7504 if (status & TP_EC_FAN_FULLSPEED)
7505 /* Disengaged mode takes precedence */
7506 len += sprintf(p + len, "level:\t\tdisengaged\n");
7507 else if (status & TP_EC_FAN_AUTO)
7508 len += sprintf(p + len, "level:\t\tauto\n");
7509 else
7510 len += sprintf(p + len, "level:\t\t%d\n", status);
7511 break;
7513 case TPACPI_FAN_NONE:
7514 default:
7515 len += sprintf(p + len, "status:\t\tnot supported\n");
7518 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7519 len += sprintf(p + len, "commands:\tlevel <level>");
7521 switch (fan_control_access_mode) {
7522 case TPACPI_FAN_WR_ACPI_SFAN:
7523 len += sprintf(p + len, " (<level> is 0-7)\n");
7524 break;
7526 default:
7527 len += sprintf(p + len, " (<level> is 0-7, "
7528 "auto, disengaged, full-speed)\n");
7529 break;
7533 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7534 len += sprintf(p + len, "commands:\tenable, disable\n"
7535 "commands:\twatchdog <timeout> (<timeout> "
7536 "is 0 (off), 1-120 (seconds))\n");
7538 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7539 len += sprintf(p + len, "commands:\tspeed <speed>"
7540 " (<speed> is 0-65535)\n");
7542 return len;
7545 static int fan_write_cmd_level(const char *cmd, int *rc)
7547 int level;
7549 if (strlencmp(cmd, "level auto") == 0)
7550 level = TP_EC_FAN_AUTO;
7551 else if ((strlencmp(cmd, "level disengaged") == 0) |
7552 (strlencmp(cmd, "level full-speed") == 0))
7553 level = TP_EC_FAN_FULLSPEED;
7554 else if (sscanf(cmd, "level %d", &level) != 1)
7555 return 0;
7557 *rc = fan_set_level_safe(level);
7558 if (*rc == -ENXIO)
7559 printk(TPACPI_ERR "level command accepted for unsupported "
7560 "access mode %d", fan_control_access_mode);
7561 else if (!*rc)
7562 tpacpi_disclose_usertask("procfs fan",
7563 "set level to %d\n", level);
7565 return 1;
7568 static int fan_write_cmd_enable(const char *cmd, int *rc)
7570 if (strlencmp(cmd, "enable") != 0)
7571 return 0;
7573 *rc = fan_set_enable();
7574 if (*rc == -ENXIO)
7575 printk(TPACPI_ERR "enable command accepted for unsupported "
7576 "access mode %d", fan_control_access_mode);
7577 else if (!*rc)
7578 tpacpi_disclose_usertask("procfs fan", "enable\n");
7580 return 1;
7583 static int fan_write_cmd_disable(const char *cmd, int *rc)
7585 if (strlencmp(cmd, "disable") != 0)
7586 return 0;
7588 *rc = fan_set_disable();
7589 if (*rc == -ENXIO)
7590 printk(TPACPI_ERR "disable command accepted for unsupported "
7591 "access mode %d", fan_control_access_mode);
7592 else if (!*rc)
7593 tpacpi_disclose_usertask("procfs fan", "disable\n");
7595 return 1;
7598 static int fan_write_cmd_speed(const char *cmd, int *rc)
7600 int speed;
7602 /* TODO:
7603 * Support speed <low> <medium> <high> ? */
7605 if (sscanf(cmd, "speed %d", &speed) != 1)
7606 return 0;
7608 *rc = fan_set_speed(speed);
7609 if (*rc == -ENXIO)
7610 printk(TPACPI_ERR "speed command accepted for unsupported "
7611 "access mode %d", fan_control_access_mode);
7612 else if (!*rc)
7613 tpacpi_disclose_usertask("procfs fan",
7614 "set speed to %d\n", speed);
7616 return 1;
7619 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7621 int interval;
7623 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7624 return 0;
7626 if (interval < 0 || interval > 120)
7627 *rc = -EINVAL;
7628 else {
7629 fan_watchdog_maxinterval = interval;
7630 tpacpi_disclose_usertask("procfs fan",
7631 "set watchdog timer to %d\n",
7632 interval);
7635 return 1;
7638 static int fan_write(char *buf)
7640 char *cmd;
7641 int rc = 0;
7643 while (!rc && (cmd = next_cmd(&buf))) {
7644 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7645 fan_write_cmd_level(cmd, &rc)) &&
7646 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7647 (fan_write_cmd_enable(cmd, &rc) ||
7648 fan_write_cmd_disable(cmd, &rc) ||
7649 fan_write_cmd_watchdog(cmd, &rc))) &&
7650 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7651 fan_write_cmd_speed(cmd, &rc))
7653 rc = -EINVAL;
7654 else if (!rc)
7655 fan_watchdog_reset();
7658 return rc;
7661 static struct ibm_struct fan_driver_data = {
7662 .name = "fan",
7663 .read = fan_read,
7664 .write = fan_write,
7665 .exit = fan_exit,
7666 .suspend = fan_suspend,
7667 .resume = fan_resume,
7670 /****************************************************************************
7671 ****************************************************************************
7673 * Infrastructure
7675 ****************************************************************************
7676 ****************************************************************************/
7679 * HKEY event callout for other subdrivers go here
7680 * (yes, it is ugly, but it is quick, safe, and gets the job done
7682 static void tpacpi_driver_event(const unsigned int hkey_event)
7688 static void hotkey_driver_event(const unsigned int scancode)
7690 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7693 /* sysfs name ---------------------------------------------------------- */
7694 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7695 struct device_attribute *attr,
7696 char *buf)
7698 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7701 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7702 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7704 /* --------------------------------------------------------------------- */
7706 /* /proc support */
7707 static struct proc_dir_entry *proc_dir;
7710 * Module and infrastructure proble, init and exit handling
7713 static int force_load;
7715 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7716 static const char * __init str_supported(int is_supported)
7718 static char text_unsupported[] __initdata = "not supported";
7720 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7722 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7724 static void ibm_exit(struct ibm_struct *ibm)
7726 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7728 list_del_init(&ibm->all_drivers);
7730 if (ibm->flags.acpi_notify_installed) {
7731 dbg_printk(TPACPI_DBG_EXIT,
7732 "%s: acpi_remove_notify_handler\n", ibm->name);
7733 BUG_ON(!ibm->acpi);
7734 acpi_remove_notify_handler(*ibm->acpi->handle,
7735 ibm->acpi->type,
7736 dispatch_acpi_notify);
7737 ibm->flags.acpi_notify_installed = 0;
7738 ibm->flags.acpi_notify_installed = 0;
7741 if (ibm->flags.proc_created) {
7742 dbg_printk(TPACPI_DBG_EXIT,
7743 "%s: remove_proc_entry\n", ibm->name);
7744 remove_proc_entry(ibm->name, proc_dir);
7745 ibm->flags.proc_created = 0;
7748 if (ibm->flags.acpi_driver_registered) {
7749 dbg_printk(TPACPI_DBG_EXIT,
7750 "%s: acpi_bus_unregister_driver\n", ibm->name);
7751 BUG_ON(!ibm->acpi);
7752 acpi_bus_unregister_driver(ibm->acpi->driver);
7753 kfree(ibm->acpi->driver);
7754 ibm->acpi->driver = NULL;
7755 ibm->flags.acpi_driver_registered = 0;
7758 if (ibm->flags.init_called && ibm->exit) {
7759 ibm->exit();
7760 ibm->flags.init_called = 0;
7763 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7766 static int __init ibm_init(struct ibm_init_struct *iibm)
7768 int ret;
7769 struct ibm_struct *ibm = iibm->data;
7770 struct proc_dir_entry *entry;
7772 BUG_ON(ibm == NULL);
7774 INIT_LIST_HEAD(&ibm->all_drivers);
7776 if (ibm->flags.experimental && !experimental)
7777 return 0;
7779 dbg_printk(TPACPI_DBG_INIT,
7780 "probing for %s\n", ibm->name);
7782 if (iibm->init) {
7783 ret = iibm->init(iibm);
7784 if (ret > 0)
7785 return 0; /* probe failed */
7786 if (ret)
7787 return ret;
7789 ibm->flags.init_called = 1;
7792 if (ibm->acpi) {
7793 if (ibm->acpi->hid) {
7794 ret = register_tpacpi_subdriver(ibm);
7795 if (ret)
7796 goto err_out;
7799 if (ibm->acpi->notify) {
7800 ret = setup_acpi_notify(ibm);
7801 if (ret == -ENODEV) {
7802 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7803 ibm->name);
7804 ret = 0;
7805 goto err_out;
7807 if (ret < 0)
7808 goto err_out;
7812 dbg_printk(TPACPI_DBG_INIT,
7813 "%s installed\n", ibm->name);
7815 if (ibm->read) {
7816 entry = create_proc_entry(ibm->name,
7817 S_IFREG | S_IRUGO | S_IWUSR,
7818 proc_dir);
7819 if (!entry) {
7820 printk(TPACPI_ERR "unable to create proc entry %s\n",
7821 ibm->name);
7822 ret = -ENODEV;
7823 goto err_out;
7825 entry->data = ibm;
7826 entry->read_proc = &dispatch_procfs_read;
7827 if (ibm->write)
7828 entry->write_proc = &dispatch_procfs_write;
7829 ibm->flags.proc_created = 1;
7832 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7834 return 0;
7836 err_out:
7837 dbg_printk(TPACPI_DBG_INIT,
7838 "%s: at error exit path with result %d\n",
7839 ibm->name, ret);
7841 ibm_exit(ibm);
7842 return (ret < 0)? ret : 0;
7845 /* Probing */
7847 static bool __pure __init tpacpi_is_fw_digit(const char c)
7849 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7852 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7853 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7854 const char t)
7856 return s && strlen(s) >= 8 &&
7857 tpacpi_is_fw_digit(s[0]) &&
7858 tpacpi_is_fw_digit(s[1]) &&
7859 s[2] == t && s[3] == 'T' &&
7860 tpacpi_is_fw_digit(s[4]) &&
7861 tpacpi_is_fw_digit(s[5]) &&
7862 s[6] == 'W' && s[7] == 'W';
7865 /* returns 0 - probe ok, or < 0 - probe error.
7866 * Probe ok doesn't mean thinkpad found.
7867 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7868 static int __must_check __init get_thinkpad_model_data(
7869 struct thinkpad_id_data *tp)
7871 const struct dmi_device *dev = NULL;
7872 char ec_fw_string[18];
7873 char const *s;
7875 if (!tp)
7876 return -EINVAL;
7878 memset(tp, 0, sizeof(*tp));
7880 if (dmi_name_in_vendors("IBM"))
7881 tp->vendor = PCI_VENDOR_ID_IBM;
7882 else if (dmi_name_in_vendors("LENOVO"))
7883 tp->vendor = PCI_VENDOR_ID_LENOVO;
7884 else
7885 return 0;
7887 s = dmi_get_system_info(DMI_BIOS_VERSION);
7888 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7889 if (s && !tp->bios_version_str)
7890 return -ENOMEM;
7892 /* Really ancient ThinkPad 240X will fail this, which is fine */
7893 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7894 return 0;
7896 tp->bios_model = tp->bios_version_str[0]
7897 | (tp->bios_version_str[1] << 8);
7898 tp->bios_release = (tp->bios_version_str[4] << 8)
7899 | tp->bios_version_str[5];
7902 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7903 * X32 or newer, all Z series; Some models must have an
7904 * up-to-date BIOS or they will not be detected.
7906 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7908 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7909 if (sscanf(dev->name,
7910 "IBM ThinkPad Embedded Controller -[%17c",
7911 ec_fw_string) == 1) {
7912 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7913 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7915 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7916 if (!tp->ec_version_str)
7917 return -ENOMEM;
7919 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7920 tp->ec_model = ec_fw_string[0]
7921 | (ec_fw_string[1] << 8);
7922 tp->ec_release = (ec_fw_string[4] << 8)
7923 | ec_fw_string[5];
7924 } else {
7925 printk(TPACPI_NOTICE
7926 "ThinkPad firmware release %s "
7927 "doesn't match the known patterns\n",
7928 ec_fw_string);
7929 printk(TPACPI_NOTICE
7930 "please report this to %s\n",
7931 TPACPI_MAIL);
7933 break;
7937 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7938 if (s && !strnicmp(s, "ThinkPad", 8)) {
7939 tp->model_str = kstrdup(s, GFP_KERNEL);
7940 if (!tp->model_str)
7941 return -ENOMEM;
7944 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7945 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7946 if (s && !tp->nummodel_str)
7947 return -ENOMEM;
7949 return 0;
7952 static int __init probe_for_thinkpad(void)
7954 int is_thinkpad;
7956 if (acpi_disabled)
7957 return -ENODEV;
7960 * Non-ancient models have better DMI tagging, but very old models
7961 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7963 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7964 (thinkpad_id.ec_model != 0) ||
7965 tpacpi_is_fw_known();
7967 /* ec is required because many other handles are relative to it */
7968 TPACPI_ACPIHANDLE_INIT(ec);
7969 if (!ec_handle) {
7970 if (is_thinkpad)
7971 printk(TPACPI_ERR
7972 "Not yet supported ThinkPad detected!\n");
7973 return -ENODEV;
7976 if (!is_thinkpad && !force_load)
7977 return -ENODEV;
7979 return 0;
7983 /* Module init, exit, parameters */
7985 static struct ibm_init_struct ibms_init[] __initdata = {
7987 .init = thinkpad_acpi_driver_init,
7988 .data = &thinkpad_acpi_driver_data,
7991 .init = hotkey_init,
7992 .data = &hotkey_driver_data,
7995 .init = bluetooth_init,
7996 .data = &bluetooth_driver_data,
7999 .init = wan_init,
8000 .data = &wan_driver_data,
8003 .init = uwb_init,
8004 .data = &uwb_driver_data,
8006 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8008 .init = video_init,
8009 .data = &video_driver_data,
8011 #endif
8013 .init = light_init,
8014 .data = &light_driver_data,
8017 .init = cmos_init,
8018 .data = &cmos_driver_data,
8021 .init = led_init,
8022 .data = &led_driver_data,
8025 .init = beep_init,
8026 .data = &beep_driver_data,
8029 .init = thermal_init,
8030 .data = &thermal_driver_data,
8033 .data = &ecdump_driver_data,
8036 .init = brightness_init,
8037 .data = &brightness_driver_data,
8040 .data = &volume_driver_data,
8043 .init = fan_init,
8044 .data = &fan_driver_data,
8048 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8050 unsigned int i;
8051 struct ibm_struct *ibm;
8053 if (!kp || !kp->name || !val)
8054 return -EINVAL;
8056 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8057 ibm = ibms_init[i].data;
8058 WARN_ON(ibm == NULL);
8060 if (!ibm || !ibm->name)
8061 continue;
8063 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8064 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8065 return -ENOSPC;
8066 strcpy(ibms_init[i].param, val);
8067 strcat(ibms_init[i].param, ",");
8068 return 0;
8072 return -EINVAL;
8075 module_param(experimental, int, 0);
8076 MODULE_PARM_DESC(experimental,
8077 "Enables experimental features when non-zero");
8079 module_param_named(debug, dbg_level, uint, 0);
8080 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8082 module_param(force_load, bool, 0);
8083 MODULE_PARM_DESC(force_load,
8084 "Attempts to load the driver even on a "
8085 "mis-identified ThinkPad when true");
8087 module_param_named(fan_control, fan_control_allowed, bool, 0);
8088 MODULE_PARM_DESC(fan_control,
8089 "Enables setting fan parameters features when true");
8091 module_param_named(brightness_mode, brightness_mode, uint, 0);
8092 MODULE_PARM_DESC(brightness_mode,
8093 "Selects brightness control strategy: "
8094 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8096 module_param(brightness_enable, uint, 0);
8097 MODULE_PARM_DESC(brightness_enable,
8098 "Enables backlight control when 1, disables when 0");
8100 module_param(hotkey_report_mode, uint, 0);
8101 MODULE_PARM_DESC(hotkey_report_mode,
8102 "used for backwards compatibility with userspace, "
8103 "see documentation");
8105 #define TPACPI_PARAM(feature) \
8106 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8107 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8108 "at module load, see documentation")
8110 TPACPI_PARAM(hotkey);
8111 TPACPI_PARAM(bluetooth);
8112 TPACPI_PARAM(video);
8113 TPACPI_PARAM(light);
8114 TPACPI_PARAM(cmos);
8115 TPACPI_PARAM(led);
8116 TPACPI_PARAM(beep);
8117 TPACPI_PARAM(ecdump);
8118 TPACPI_PARAM(brightness);
8119 TPACPI_PARAM(volume);
8120 TPACPI_PARAM(fan);
8122 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8123 module_param(dbg_wlswemul, uint, 0);
8124 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8125 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8126 MODULE_PARM_DESC(wlsw_state,
8127 "Initial state of the emulated WLSW switch");
8129 module_param(dbg_bluetoothemul, uint, 0);
8130 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8131 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8132 MODULE_PARM_DESC(bluetooth_state,
8133 "Initial state of the emulated bluetooth switch");
8135 module_param(dbg_wwanemul, uint, 0);
8136 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8137 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8138 MODULE_PARM_DESC(wwan_state,
8139 "Initial state of the emulated WWAN switch");
8141 module_param(dbg_uwbemul, uint, 0);
8142 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8143 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8144 MODULE_PARM_DESC(uwb_state,
8145 "Initial state of the emulated UWB switch");
8146 #endif
8148 static void thinkpad_acpi_module_exit(void)
8150 struct ibm_struct *ibm, *itmp;
8152 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8154 list_for_each_entry_safe_reverse(ibm, itmp,
8155 &tpacpi_all_drivers,
8156 all_drivers) {
8157 ibm_exit(ibm);
8160 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8162 if (tpacpi_inputdev) {
8163 if (tp_features.input_device_registered)
8164 input_unregister_device(tpacpi_inputdev);
8165 else
8166 input_free_device(tpacpi_inputdev);
8169 if (tpacpi_hwmon)
8170 hwmon_device_unregister(tpacpi_hwmon);
8172 if (tp_features.sensors_pdev_attrs_registered)
8173 device_remove_file(&tpacpi_sensors_pdev->dev,
8174 &dev_attr_thinkpad_acpi_pdev_name);
8175 if (tpacpi_sensors_pdev)
8176 platform_device_unregister(tpacpi_sensors_pdev);
8177 if (tpacpi_pdev)
8178 platform_device_unregister(tpacpi_pdev);
8180 if (tp_features.sensors_pdrv_attrs_registered)
8181 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8182 if (tp_features.platform_drv_attrs_registered)
8183 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8185 if (tp_features.sensors_pdrv_registered)
8186 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8188 if (tp_features.platform_drv_registered)
8189 platform_driver_unregister(&tpacpi_pdriver);
8191 if (proc_dir)
8192 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8194 if (tpacpi_wq)
8195 destroy_workqueue(tpacpi_wq);
8197 kfree(thinkpad_id.bios_version_str);
8198 kfree(thinkpad_id.ec_version_str);
8199 kfree(thinkpad_id.model_str);
8203 static int __init thinkpad_acpi_module_init(void)
8205 int ret, i;
8207 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8209 /* Parameter checking */
8210 if (hotkey_report_mode > 2)
8211 return -EINVAL;
8213 /* Driver-level probe */
8215 ret = get_thinkpad_model_data(&thinkpad_id);
8216 if (ret) {
8217 printk(TPACPI_ERR
8218 "unable to get DMI data: %d\n", ret);
8219 thinkpad_acpi_module_exit();
8220 return ret;
8222 ret = probe_for_thinkpad();
8223 if (ret) {
8224 thinkpad_acpi_module_exit();
8225 return ret;
8228 /* Driver initialization */
8230 TPACPI_ACPIHANDLE_INIT(ecrd);
8231 TPACPI_ACPIHANDLE_INIT(ecwr);
8233 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8234 if (!tpacpi_wq) {
8235 thinkpad_acpi_module_exit();
8236 return -ENOMEM;
8239 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8240 if (!proc_dir) {
8241 printk(TPACPI_ERR
8242 "unable to create proc dir " TPACPI_PROC_DIR);
8243 thinkpad_acpi_module_exit();
8244 return -ENODEV;
8247 ret = platform_driver_register(&tpacpi_pdriver);
8248 if (ret) {
8249 printk(TPACPI_ERR
8250 "unable to register main platform driver\n");
8251 thinkpad_acpi_module_exit();
8252 return ret;
8254 tp_features.platform_drv_registered = 1;
8256 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8257 if (ret) {
8258 printk(TPACPI_ERR
8259 "unable to register hwmon platform driver\n");
8260 thinkpad_acpi_module_exit();
8261 return ret;
8263 tp_features.sensors_pdrv_registered = 1;
8265 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8266 if (!ret) {
8267 tp_features.platform_drv_attrs_registered = 1;
8268 ret = tpacpi_create_driver_attributes(
8269 &tpacpi_hwmon_pdriver.driver);
8271 if (ret) {
8272 printk(TPACPI_ERR
8273 "unable to create sysfs driver attributes\n");
8274 thinkpad_acpi_module_exit();
8275 return ret;
8277 tp_features.sensors_pdrv_attrs_registered = 1;
8280 /* Device initialization */
8281 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8282 NULL, 0);
8283 if (IS_ERR(tpacpi_pdev)) {
8284 ret = PTR_ERR(tpacpi_pdev);
8285 tpacpi_pdev = NULL;
8286 printk(TPACPI_ERR "unable to register platform device\n");
8287 thinkpad_acpi_module_exit();
8288 return ret;
8290 tpacpi_sensors_pdev = platform_device_register_simple(
8291 TPACPI_HWMON_DRVR_NAME,
8292 -1, NULL, 0);
8293 if (IS_ERR(tpacpi_sensors_pdev)) {
8294 ret = PTR_ERR(tpacpi_sensors_pdev);
8295 tpacpi_sensors_pdev = NULL;
8296 printk(TPACPI_ERR
8297 "unable to register hwmon platform device\n");
8298 thinkpad_acpi_module_exit();
8299 return ret;
8301 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8302 &dev_attr_thinkpad_acpi_pdev_name);
8303 if (ret) {
8304 printk(TPACPI_ERR
8305 "unable to create sysfs hwmon device attributes\n");
8306 thinkpad_acpi_module_exit();
8307 return ret;
8309 tp_features.sensors_pdev_attrs_registered = 1;
8310 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8311 if (IS_ERR(tpacpi_hwmon)) {
8312 ret = PTR_ERR(tpacpi_hwmon);
8313 tpacpi_hwmon = NULL;
8314 printk(TPACPI_ERR "unable to register hwmon device\n");
8315 thinkpad_acpi_module_exit();
8316 return ret;
8318 mutex_init(&tpacpi_inputdev_send_mutex);
8319 tpacpi_inputdev = input_allocate_device();
8320 if (!tpacpi_inputdev) {
8321 printk(TPACPI_ERR "unable to allocate input device\n");
8322 thinkpad_acpi_module_exit();
8323 return -ENOMEM;
8324 } else {
8325 /* Prepare input device, but don't register */
8326 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8327 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8328 tpacpi_inputdev->id.bustype = BUS_HOST;
8329 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8330 thinkpad_id.vendor :
8331 PCI_VENDOR_ID_IBM;
8332 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8333 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8335 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8336 ret = ibm_init(&ibms_init[i]);
8337 if (ret >= 0 && *ibms_init[i].param)
8338 ret = ibms_init[i].data->write(ibms_init[i].param);
8339 if (ret < 0) {
8340 thinkpad_acpi_module_exit();
8341 return ret;
8344 ret = input_register_device(tpacpi_inputdev);
8345 if (ret < 0) {
8346 printk(TPACPI_ERR "unable to register input device\n");
8347 thinkpad_acpi_module_exit();
8348 return ret;
8349 } else {
8350 tp_features.input_device_registered = 1;
8353 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8354 return 0;
8357 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8360 * This will autoload the driver in almost every ThinkPad
8361 * in widespread use.
8363 * Only _VERY_ old models, like the 240, 240x and 570 lack
8364 * the HKEY event interface.
8366 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8369 * DMI matching for module autoloading
8371 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8372 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8374 * Only models listed in thinkwiki will be supported, so add yours
8375 * if it is not there yet.
8377 #define IBM_BIOS_MODULE_ALIAS(__type) \
8378 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8380 /* Ancient thinkpad BIOSes have to be identified by
8381 * BIOS type or model number, and there are far less
8382 * BIOS types than model numbers... */
8383 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8385 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8386 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8387 MODULE_DESCRIPTION(TPACPI_DESC);
8388 MODULE_VERSION(TPACPI_VERSION);
8389 MODULE_LICENSE("GPL");
8391 module_init(thinkpad_acpi_module_init);
8392 module_exit(thinkpad_acpi_module_exit);