thinkpad-acpi: fix error return for brightness write
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob9110c83ec8b2a707f00ef81b9d70307c9c8328a3
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020600
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /* HKEY events */
149 enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
182 /* Thermal events */
183 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
189 /* Misc */
190 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
193 /****************************************************************************
194 * Main driver
197 #define TPACPI_NAME "thinkpad"
198 #define TPACPI_DESC "ThinkPad ACPI Extras"
199 #define TPACPI_FILE TPACPI_NAME "_acpi"
200 #define TPACPI_URL "http://ibm-acpi.sf.net/"
201 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
203 #define TPACPI_PROC_DIR "ibm"
204 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
205 #define TPACPI_DRVR_NAME TPACPI_FILE
206 #define TPACPI_DRVR_SHORTNAME "tpacpi"
207 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
209 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
210 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
212 #define TPACPI_MAX_ACPI_ARGS 3
214 /* rfkill switches */
215 enum {
216 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
217 TPACPI_RFK_WWAN_SW_ID,
218 TPACPI_RFK_UWB_SW_ID,
221 /* printk headers */
222 #define TPACPI_LOG TPACPI_FILE ": "
223 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
224 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
225 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
226 #define TPACPI_ERR KERN_ERR TPACPI_LOG
227 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
228 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
229 #define TPACPI_INFO KERN_INFO TPACPI_LOG
230 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
232 /* Debugging printk groups */
233 #define TPACPI_DBG_ALL 0xffff
234 #define TPACPI_DBG_DISCLOSETASK 0x8000
235 #define TPACPI_DBG_INIT 0x0001
236 #define TPACPI_DBG_EXIT 0x0002
237 #define TPACPI_DBG_RFKILL 0x0004
238 #define TPACPI_DBG_HKEY 0x0008
239 #define TPACPI_DBG_FAN 0x0010
240 #define TPACPI_DBG_BRGHT 0x0020
242 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
243 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
244 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
247 /****************************************************************************
248 * Driver-wide structs and misc. variables
251 struct ibm_struct;
253 struct tp_acpi_drv_struct {
254 const struct acpi_device_id *hid;
255 struct acpi_driver *driver;
257 void (*notify) (struct ibm_struct *, u32);
258 acpi_handle *handle;
259 u32 type;
260 struct acpi_device *device;
263 struct ibm_struct {
264 char *name;
266 int (*read) (char *);
267 int (*write) (char *);
268 void (*exit) (void);
269 void (*resume) (void);
270 void (*suspend) (pm_message_t state);
271 void (*shutdown) (void);
273 struct list_head all_drivers;
275 struct tp_acpi_drv_struct *acpi;
277 struct {
278 u8 acpi_driver_registered:1;
279 u8 acpi_notify_installed:1;
280 u8 proc_created:1;
281 u8 init_called:1;
282 u8 experimental:1;
283 } flags;
286 struct ibm_init_struct {
287 char param[32];
289 int (*init) (struct ibm_init_struct *);
290 struct ibm_struct *data;
293 static struct {
294 u32 bluetooth:1;
295 u32 hotkey:1;
296 u32 hotkey_mask:1;
297 u32 hotkey_wlsw:1;
298 u32 hotkey_tablet:1;
299 u32 light:1;
300 u32 light_status:1;
301 u32 bright_16levels:1;
302 u32 bright_acpimode:1;
303 u32 wan:1;
304 u32 uwb:1;
305 u32 fan_ctrl_status_undef:1;
306 u32 second_fan:1;
307 u32 beep_needs_two_args:1;
308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315 } tp_features;
317 static struct {
318 u16 hotkey_mask_ff:1;
319 } tp_warned;
321 struct thinkpad_id_data {
322 unsigned int vendor; /* ThinkPad vendor:
323 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
325 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
326 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
328 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
329 u16 ec_model;
330 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
331 u16 ec_release;
333 char *model_str; /* ThinkPad T43 */
334 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
336 static struct thinkpad_id_data thinkpad_id;
338 static enum {
339 TPACPI_LIFE_INIT = 0,
340 TPACPI_LIFE_RUNNING,
341 TPACPI_LIFE_EXITING,
342 } tpacpi_lifecycle;
344 static int experimental;
345 static u32 dbg_level;
347 static struct workqueue_struct *tpacpi_wq;
349 enum led_status_t {
350 TPACPI_LED_OFF = 0,
351 TPACPI_LED_ON,
352 TPACPI_LED_BLINK,
355 /* Special LED class that can defer work */
356 struct tpacpi_led_classdev {
357 struct led_classdev led_classdev;
358 struct work_struct work;
359 enum led_status_t new_state;
360 unsigned int led;
363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
364 static int dbg_wlswemul;
365 static int tpacpi_wlsw_emulstate;
366 static int dbg_bluetoothemul;
367 static int tpacpi_bluetooth_emulstate;
368 static int dbg_wwanemul;
369 static int tpacpi_wwan_emulstate;
370 static int dbg_uwbemul;
371 static int tpacpi_uwb_emulstate;
372 #endif
375 /*************************************************************************
376 * Debugging helpers
379 #define dbg_printk(a_dbg_level, format, arg...) \
380 do { if (dbg_level & (a_dbg_level)) \
381 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
382 } while (0)
384 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
385 #define vdbg_printk dbg_printk
386 static const char *str_supported(int is_supported);
387 #else
388 #define vdbg_printk(a_dbg_level, format, arg...) \
389 do { } while (0)
390 #endif
392 static void tpacpi_log_usertask(const char * const what)
394 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
395 what, task_tgid_vnr(current));
398 #define tpacpi_disclose_usertask(what, format, arg...) \
399 do { \
400 if (unlikely( \
401 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
402 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
403 printk(TPACPI_DEBUG "%s: PID %d: " format, \
404 what, task_tgid_vnr(current), ## arg); \
406 } while (0)
409 * Quirk handling helpers
411 * ThinkPad IDs and versions seen in the field so far
412 * are two-characters from the set [0-9A-Z], i.e. base 36.
414 * We use values well outside that range as specials.
417 #define TPACPI_MATCH_ANY 0xffffU
418 #define TPACPI_MATCH_UNKNOWN 0U
420 /* TPID('1', 'Y') == 0x5931 */
421 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
423 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
424 { .vendor = PCI_VENDOR_ID_IBM, \
425 .bios = TPID(__id1, __id2), \
426 .ec = TPACPI_MATCH_ANY, \
427 .quirks = (__quirk) }
429 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
430 { .vendor = PCI_VENDOR_ID_LENOVO, \
431 .bios = TPID(__id1, __id2), \
432 .ec = TPACPI_MATCH_ANY, \
433 .quirks = (__quirk) }
435 struct tpacpi_quirk {
436 unsigned int vendor;
437 u16 bios;
438 u16 ec;
439 unsigned long quirks;
443 * tpacpi_check_quirks() - search BIOS/EC version on a list
444 * @qlist: array of &struct tpacpi_quirk
445 * @qlist_size: number of elements in @qlist
447 * Iterates over a quirks list until one is found that matches the
448 * ThinkPad's vendor, BIOS and EC model.
450 * Returns 0 if nothing matches, otherwise returns the quirks field of
451 * the matching &struct tpacpi_quirk entry.
453 * The match criteria is: vendor, ec and bios much match.
455 static unsigned long __init tpacpi_check_quirks(
456 const struct tpacpi_quirk *qlist,
457 unsigned int qlist_size)
459 while (qlist_size) {
460 if ((qlist->vendor == thinkpad_id.vendor ||
461 qlist->vendor == TPACPI_MATCH_ANY) &&
462 (qlist->bios == thinkpad_id.bios_model ||
463 qlist->bios == TPACPI_MATCH_ANY) &&
464 (qlist->ec == thinkpad_id.ec_model ||
465 qlist->ec == TPACPI_MATCH_ANY))
466 return qlist->quirks;
468 qlist_size--;
469 qlist++;
471 return 0;
475 /****************************************************************************
476 ****************************************************************************
478 * ACPI Helpers and device model
480 ****************************************************************************
481 ****************************************************************************/
483 /*************************************************************************
484 * ACPI basic handles
487 static acpi_handle root_handle;
489 #define TPACPI_HANDLE(object, parent, paths...) \
490 static acpi_handle object##_handle; \
491 static acpi_handle *object##_parent = &parent##_handle; \
492 static char *object##_path; \
493 static char *object##_paths[] = { paths }
495 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
496 "\\_SB.PCI.ISA.EC", /* 570 */
497 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
498 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
499 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
500 "\\_SB.PCI0.ICH3.EC0", /* R31 */
501 "\\_SB.PCI0.LPC.EC", /* all others */
504 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
505 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
507 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
508 /* T4x, X31, X40 */
509 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
510 "\\CMS", /* R40, R40e */
511 ); /* all others */
513 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
514 "^HKEY", /* R30, R31 */
515 "HKEY", /* all others */
516 ); /* 570 */
518 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
519 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
520 "\\_SB.PCI0.VID0", /* 770e */
521 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
522 "\\_SB.PCI0.AGP.VID", /* all others */
523 ); /* R30, R31 */
526 /*************************************************************************
527 * ACPI helpers
530 static int acpi_evalf(acpi_handle handle,
531 void *res, char *method, char *fmt, ...)
533 char *fmt0 = fmt;
534 struct acpi_object_list params;
535 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
536 struct acpi_buffer result, *resultp;
537 union acpi_object out_obj;
538 acpi_status status;
539 va_list ap;
540 char res_type;
541 int success;
542 int quiet;
544 if (!*fmt) {
545 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
546 return 0;
549 if (*fmt == 'q') {
550 quiet = 1;
551 fmt++;
552 } else
553 quiet = 0;
555 res_type = *(fmt++);
557 params.count = 0;
558 params.pointer = &in_objs[0];
560 va_start(ap, fmt);
561 while (*fmt) {
562 char c = *(fmt++);
563 switch (c) {
564 case 'd': /* int */
565 in_objs[params.count].integer.value = va_arg(ap, int);
566 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
567 break;
568 /* add more types as needed */
569 default:
570 printk(TPACPI_ERR "acpi_evalf() called "
571 "with invalid format character '%c'\n", c);
572 return 0;
575 va_end(ap);
577 if (res_type != 'v') {
578 result.length = sizeof(out_obj);
579 result.pointer = &out_obj;
580 resultp = &result;
581 } else
582 resultp = NULL;
584 status = acpi_evaluate_object(handle, method, &params, resultp);
586 switch (res_type) {
587 case 'd': /* int */
588 if (res)
589 *(int *)res = out_obj.integer.value;
590 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
591 break;
592 case 'v': /* void */
593 success = status == AE_OK;
594 break;
595 /* add more types as needed */
596 default:
597 printk(TPACPI_ERR "acpi_evalf() called "
598 "with invalid format character '%c'\n", res_type);
599 return 0;
602 if (!success && !quiet)
603 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
604 method, fmt0, status);
606 return success;
609 static int acpi_ec_read(int i, u8 *p)
611 int v;
613 if (ecrd_handle) {
614 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
615 return 0;
616 *p = v;
617 } else {
618 if (ec_read(i, p) < 0)
619 return 0;
622 return 1;
625 static int acpi_ec_write(int i, u8 v)
627 if (ecwr_handle) {
628 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
629 return 0;
630 } else {
631 if (ec_write(i, v) < 0)
632 return 0;
635 return 1;
638 static int issue_thinkpad_cmos_command(int cmos_cmd)
640 if (!cmos_handle)
641 return -ENXIO;
643 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
644 return -EIO;
646 return 0;
649 /*************************************************************************
650 * ACPI device model
653 #define TPACPI_ACPIHANDLE_INIT(object) \
654 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
655 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
657 static void drv_acpi_handle_init(char *name,
658 acpi_handle *handle, acpi_handle parent,
659 char **paths, int num_paths, char **path)
661 int i;
662 acpi_status status;
664 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
665 name);
667 for (i = 0; i < num_paths; i++) {
668 status = acpi_get_handle(parent, paths[i], handle);
669 if (ACPI_SUCCESS(status)) {
670 *path = paths[i];
671 dbg_printk(TPACPI_DBG_INIT,
672 "Found ACPI handle %s for %s\n",
673 *path, name);
674 return;
678 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
679 name);
680 *handle = NULL;
683 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
685 struct ibm_struct *ibm = data;
687 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
688 return;
690 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
691 return;
693 ibm->acpi->notify(ibm, event);
696 static int __init setup_acpi_notify(struct ibm_struct *ibm)
698 acpi_status status;
699 int rc;
701 BUG_ON(!ibm->acpi);
703 if (!*ibm->acpi->handle)
704 return 0;
706 vdbg_printk(TPACPI_DBG_INIT,
707 "setting up ACPI notify for %s\n", ibm->name);
709 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
710 if (rc < 0) {
711 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
712 ibm->name, rc);
713 return -ENODEV;
716 ibm->acpi->device->driver_data = ibm;
717 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
718 TPACPI_ACPI_EVENT_PREFIX,
719 ibm->name);
721 status = acpi_install_notify_handler(*ibm->acpi->handle,
722 ibm->acpi->type, dispatch_acpi_notify, ibm);
723 if (ACPI_FAILURE(status)) {
724 if (status == AE_ALREADY_EXISTS) {
725 printk(TPACPI_NOTICE
726 "another device driver is already "
727 "handling %s events\n", ibm->name);
728 } else {
729 printk(TPACPI_ERR
730 "acpi_install_notify_handler(%s) failed: %d\n",
731 ibm->name, status);
733 return -ENODEV;
735 ibm->flags.acpi_notify_installed = 1;
736 return 0;
739 static int __init tpacpi_device_add(struct acpi_device *device)
741 return 0;
744 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
746 int rc;
748 dbg_printk(TPACPI_DBG_INIT,
749 "registering %s as an ACPI driver\n", ibm->name);
751 BUG_ON(!ibm->acpi);
753 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
754 if (!ibm->acpi->driver) {
755 printk(TPACPI_ERR
756 "failed to allocate memory for ibm->acpi->driver\n");
757 return -ENOMEM;
760 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
761 ibm->acpi->driver->ids = ibm->acpi->hid;
763 ibm->acpi->driver->ops.add = &tpacpi_device_add;
765 rc = acpi_bus_register_driver(ibm->acpi->driver);
766 if (rc < 0) {
767 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
768 ibm->name, rc);
769 kfree(ibm->acpi->driver);
770 ibm->acpi->driver = NULL;
771 } else if (!rc)
772 ibm->flags.acpi_driver_registered = 1;
774 return rc;
778 /****************************************************************************
779 ****************************************************************************
781 * Procfs Helpers
783 ****************************************************************************
784 ****************************************************************************/
786 static int dispatch_procfs_read(char *page, char **start, off_t off,
787 int count, int *eof, void *data)
789 struct ibm_struct *ibm = data;
790 int len;
792 if (!ibm || !ibm->read)
793 return -EINVAL;
795 len = ibm->read(page);
796 if (len < 0)
797 return len;
799 if (len <= off + count)
800 *eof = 1;
801 *start = page + off;
802 len -= off;
803 if (len > count)
804 len = count;
805 if (len < 0)
806 len = 0;
808 return len;
811 static int dispatch_procfs_write(struct file *file,
812 const char __user *userbuf,
813 unsigned long count, void *data)
815 struct ibm_struct *ibm = data;
816 char *kernbuf;
817 int ret;
819 if (!ibm || !ibm->write)
820 return -EINVAL;
821 if (count > PAGE_SIZE - 2)
822 return -EINVAL;
824 kernbuf = kmalloc(count + 2, GFP_KERNEL);
825 if (!kernbuf)
826 return -ENOMEM;
828 if (copy_from_user(kernbuf, userbuf, count)) {
829 kfree(kernbuf);
830 return -EFAULT;
833 kernbuf[count] = 0;
834 strcat(kernbuf, ",");
835 ret = ibm->write(kernbuf);
836 if (ret == 0)
837 ret = count;
839 kfree(kernbuf);
841 return ret;
844 static char *next_cmd(char **cmds)
846 char *start = *cmds;
847 char *end;
849 while ((end = strchr(start, ',')) && end == start)
850 start = end + 1;
852 if (!end)
853 return NULL;
855 *end = 0;
856 *cmds = end + 1;
857 return start;
861 /****************************************************************************
862 ****************************************************************************
864 * Device model: input, hwmon and platform
866 ****************************************************************************
867 ****************************************************************************/
869 static struct platform_device *tpacpi_pdev;
870 static struct platform_device *tpacpi_sensors_pdev;
871 static struct device *tpacpi_hwmon;
872 static struct input_dev *tpacpi_inputdev;
873 static struct mutex tpacpi_inputdev_send_mutex;
874 static LIST_HEAD(tpacpi_all_drivers);
876 static int tpacpi_suspend_handler(struct platform_device *pdev,
877 pm_message_t state)
879 struct ibm_struct *ibm, *itmp;
881 list_for_each_entry_safe(ibm, itmp,
882 &tpacpi_all_drivers,
883 all_drivers) {
884 if (ibm->suspend)
885 (ibm->suspend)(state);
888 return 0;
891 static int tpacpi_resume_handler(struct platform_device *pdev)
893 struct ibm_struct *ibm, *itmp;
895 list_for_each_entry_safe(ibm, itmp,
896 &tpacpi_all_drivers,
897 all_drivers) {
898 if (ibm->resume)
899 (ibm->resume)();
902 return 0;
905 static void tpacpi_shutdown_handler(struct platform_device *pdev)
907 struct ibm_struct *ibm, *itmp;
909 list_for_each_entry_safe(ibm, itmp,
910 &tpacpi_all_drivers,
911 all_drivers) {
912 if (ibm->shutdown)
913 (ibm->shutdown)();
917 static struct platform_driver tpacpi_pdriver = {
918 .driver = {
919 .name = TPACPI_DRVR_NAME,
920 .owner = THIS_MODULE,
922 .suspend = tpacpi_suspend_handler,
923 .resume = tpacpi_resume_handler,
924 .shutdown = tpacpi_shutdown_handler,
927 static struct platform_driver tpacpi_hwmon_pdriver = {
928 .driver = {
929 .name = TPACPI_HWMON_DRVR_NAME,
930 .owner = THIS_MODULE,
934 /*************************************************************************
935 * sysfs support helpers
938 struct attribute_set {
939 unsigned int members, max_members;
940 struct attribute_group group;
943 struct attribute_set_obj {
944 struct attribute_set s;
945 struct attribute *a;
946 } __attribute__((packed));
948 static struct attribute_set *create_attr_set(unsigned int max_members,
949 const char *name)
951 struct attribute_set_obj *sobj;
953 if (max_members == 0)
954 return NULL;
956 /* Allocates space for implicit NULL at the end too */
957 sobj = kzalloc(sizeof(struct attribute_set_obj) +
958 max_members * sizeof(struct attribute *),
959 GFP_KERNEL);
960 if (!sobj)
961 return NULL;
962 sobj->s.max_members = max_members;
963 sobj->s.group.attrs = &sobj->a;
964 sobj->s.group.name = name;
966 return &sobj->s;
969 #define destroy_attr_set(_set) \
970 kfree(_set);
972 /* not multi-threaded safe, use it in a single thread per set */
973 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
975 if (!s || !attr)
976 return -EINVAL;
978 if (s->members >= s->max_members)
979 return -ENOMEM;
981 s->group.attrs[s->members] = attr;
982 s->members++;
984 return 0;
987 static int add_many_to_attr_set(struct attribute_set *s,
988 struct attribute **attr,
989 unsigned int count)
991 int i, res;
993 for (i = 0; i < count; i++) {
994 res = add_to_attr_set(s, attr[i]);
995 if (res)
996 return res;
999 return 0;
1002 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1004 sysfs_remove_group(kobj, &s->group);
1005 destroy_attr_set(s);
1008 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1009 sysfs_create_group(_kobj, &_attr_set->group)
1011 static int parse_strtoul(const char *buf,
1012 unsigned long max, unsigned long *value)
1014 char *endp;
1016 while (*buf && isspace(*buf))
1017 buf++;
1018 *value = simple_strtoul(buf, &endp, 0);
1019 while (*endp && isspace(*endp))
1020 endp++;
1021 if (*endp || *value > max)
1022 return -EINVAL;
1024 return 0;
1027 static void tpacpi_disable_brightness_delay(void)
1029 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1030 printk(TPACPI_NOTICE
1031 "ACPI backlight control delay disabled\n");
1034 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1036 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1037 union acpi_object *obj;
1038 int rc;
1040 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1041 obj = (union acpi_object *)buffer.pointer;
1042 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1043 printk(TPACPI_ERR "Unknown _BCL data, "
1044 "please report this to %s\n", TPACPI_MAIL);
1045 rc = 0;
1046 } else {
1047 rc = obj->package.count;
1049 } else {
1050 return 0;
1053 kfree(buffer.pointer);
1054 return rc;
1057 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1058 u32 lvl, void *context, void **rv)
1060 char name[ACPI_PATH_SEGMENT_LENGTH];
1061 struct acpi_buffer buffer = { sizeof(name), &name };
1063 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1064 !strncmp("_BCL", name, sizeof(name) - 1)) {
1065 BUG_ON(!rv || !*rv);
1066 **(int **)rv = tpacpi_query_bcl_levels(handle);
1067 return AE_CTRL_TERMINATE;
1068 } else {
1069 return AE_OK;
1074 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1076 static int __init tpacpi_check_std_acpi_brightness_support(void)
1078 int status;
1079 int bcl_levels = 0;
1080 void *bcl_ptr = &bcl_levels;
1082 if (!vid_handle) {
1083 TPACPI_ACPIHANDLE_INIT(vid);
1085 if (!vid_handle)
1086 return 0;
1089 * Search for a _BCL method, and execute it. This is safe on all
1090 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1091 * BIOS in ACPI backlight control mode. We do NOT have to care
1092 * about calling the _BCL method in an enabled video device, any
1093 * will do for our purposes.
1096 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1097 tpacpi_acpi_walk_find_bcl, NULL,
1098 &bcl_ptr);
1100 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1101 tp_features.bright_acpimode = 1;
1102 return (bcl_levels - 2);
1105 return 0;
1108 static int __init tpacpi_new_rfkill(const unsigned int id,
1109 struct rfkill **rfk,
1110 const enum rfkill_type rfktype,
1111 const char *name,
1112 const bool set_default,
1113 int (*toggle_radio)(void *, enum rfkill_state),
1114 int (*get_state)(void *, enum rfkill_state *))
1116 int res;
1117 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1119 res = get_state(NULL, &initial_state);
1120 if (res < 0) {
1121 printk(TPACPI_ERR
1122 "failed to read initial state for %s, error %d; "
1123 "will turn radio off\n", name, res);
1124 } else if (set_default) {
1125 /* try to set the initial state as the default for the rfkill
1126 * type, since we ask the firmware to preserve it across S5 in
1127 * NVRAM */
1128 if (rfkill_set_default(rfktype,
1129 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1130 RFKILL_STATE_UNBLOCKED :
1131 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1132 vdbg_printk(TPACPI_DBG_RFKILL,
1133 "Default state for %s cannot be changed\n",
1134 name);
1137 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1138 if (!*rfk) {
1139 printk(TPACPI_ERR
1140 "failed to allocate memory for rfkill class\n");
1141 return -ENOMEM;
1144 (*rfk)->name = name;
1145 (*rfk)->get_state = get_state;
1146 (*rfk)->toggle_radio = toggle_radio;
1147 (*rfk)->state = initial_state;
1149 res = rfkill_register(*rfk);
1150 if (res < 0) {
1151 printk(TPACPI_ERR
1152 "failed to register %s rfkill switch: %d\n",
1153 name, res);
1154 rfkill_free(*rfk);
1155 *rfk = NULL;
1156 return res;
1159 return 0;
1162 static void printk_deprecated_attribute(const char * const what,
1163 const char * const details)
1165 tpacpi_log_usertask("deprecated sysfs attribute");
1166 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1167 "will be removed. %s\n",
1168 what, details);
1171 static void printk_deprecated_rfkill_attribute(const char * const what)
1173 printk_deprecated_attribute(what,
1174 "Please switch to generic rfkill before year 2010");
1177 /*************************************************************************
1178 * thinkpad-acpi driver attributes
1181 /* interface_version --------------------------------------------------- */
1182 static ssize_t tpacpi_driver_interface_version_show(
1183 struct device_driver *drv,
1184 char *buf)
1186 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1189 static DRIVER_ATTR(interface_version, S_IRUGO,
1190 tpacpi_driver_interface_version_show, NULL);
1192 /* debug_level --------------------------------------------------------- */
1193 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1194 char *buf)
1196 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1199 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1200 const char *buf, size_t count)
1202 unsigned long t;
1204 if (parse_strtoul(buf, 0xffff, &t))
1205 return -EINVAL;
1207 dbg_level = t;
1209 return count;
1212 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1213 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1215 /* version ------------------------------------------------------------- */
1216 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1217 char *buf)
1219 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1220 TPACPI_DESC, TPACPI_VERSION);
1223 static DRIVER_ATTR(version, S_IRUGO,
1224 tpacpi_driver_version_show, NULL);
1226 /* --------------------------------------------------------------------- */
1228 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1230 static void tpacpi_send_radiosw_update(void);
1232 /* wlsw_emulstate ------------------------------------------------------ */
1233 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1234 char *buf)
1236 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1239 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1240 const char *buf, size_t count)
1242 unsigned long t;
1244 if (parse_strtoul(buf, 1, &t))
1245 return -EINVAL;
1247 if (tpacpi_wlsw_emulstate != t) {
1248 tpacpi_wlsw_emulstate = !!t;
1249 tpacpi_send_radiosw_update();
1250 } else
1251 tpacpi_wlsw_emulstate = !!t;
1253 return count;
1256 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1257 tpacpi_driver_wlsw_emulstate_show,
1258 tpacpi_driver_wlsw_emulstate_store);
1260 /* bluetooth_emulstate ------------------------------------------------- */
1261 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1262 struct device_driver *drv,
1263 char *buf)
1265 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1268 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1269 struct device_driver *drv,
1270 const char *buf, size_t count)
1272 unsigned long t;
1274 if (parse_strtoul(buf, 1, &t))
1275 return -EINVAL;
1277 tpacpi_bluetooth_emulstate = !!t;
1279 return count;
1282 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1283 tpacpi_driver_bluetooth_emulstate_show,
1284 tpacpi_driver_bluetooth_emulstate_store);
1286 /* wwan_emulstate ------------------------------------------------- */
1287 static ssize_t tpacpi_driver_wwan_emulstate_show(
1288 struct device_driver *drv,
1289 char *buf)
1291 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1294 static ssize_t tpacpi_driver_wwan_emulstate_store(
1295 struct device_driver *drv,
1296 const char *buf, size_t count)
1298 unsigned long t;
1300 if (parse_strtoul(buf, 1, &t))
1301 return -EINVAL;
1303 tpacpi_wwan_emulstate = !!t;
1305 return count;
1308 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1309 tpacpi_driver_wwan_emulstate_show,
1310 tpacpi_driver_wwan_emulstate_store);
1312 /* uwb_emulstate ------------------------------------------------- */
1313 static ssize_t tpacpi_driver_uwb_emulstate_show(
1314 struct device_driver *drv,
1315 char *buf)
1317 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1320 static ssize_t tpacpi_driver_uwb_emulstate_store(
1321 struct device_driver *drv,
1322 const char *buf, size_t count)
1324 unsigned long t;
1326 if (parse_strtoul(buf, 1, &t))
1327 return -EINVAL;
1329 tpacpi_uwb_emulstate = !!t;
1331 return count;
1334 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1335 tpacpi_driver_uwb_emulstate_show,
1336 tpacpi_driver_uwb_emulstate_store);
1337 #endif
1339 /* --------------------------------------------------------------------- */
1341 static struct driver_attribute *tpacpi_driver_attributes[] = {
1342 &driver_attr_debug_level, &driver_attr_version,
1343 &driver_attr_interface_version,
1346 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1348 int i, res;
1350 i = 0;
1351 res = 0;
1352 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1353 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1354 i++;
1357 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1358 if (!res && dbg_wlswemul)
1359 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1360 if (!res && dbg_bluetoothemul)
1361 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1362 if (!res && dbg_wwanemul)
1363 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1364 if (!res && dbg_uwbemul)
1365 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1366 #endif
1368 return res;
1371 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1373 int i;
1375 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1376 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1378 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1379 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1380 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1381 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1382 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1383 #endif
1386 /*************************************************************************
1387 * Firmware Data
1391 * Table of recommended minimum BIOS versions
1393 * Reasons for listing:
1394 * 1. Stable BIOS, listed because the unknown ammount of
1395 * bugs and bad ACPI behaviour on older versions
1397 * 2. BIOS or EC fw with known bugs that trigger on Linux
1399 * 3. BIOS with known reduced functionality in older versions
1401 * We recommend the latest BIOS and EC version.
1402 * We only support the latest BIOS and EC fw version as a rule.
1404 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1405 * Information from users in ThinkWiki
1407 * WARNING: we use this table also to detect that the machine is
1408 * a ThinkPad in some cases, so don't remove entries lightly.
1411 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1412 { .vendor = (__v), \
1413 .bios = TPID(__id1, __id2), \
1414 .ec = TPACPI_MATCH_ANY, \
1415 .quirks = TPACPI_MATCH_ANY << 16 \
1416 | (__bv1) << 8 | (__bv2) }
1418 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1419 __eid, __ev1, __ev2) \
1420 { .vendor = (__v), \
1421 .bios = TPID(__bid1, __bid2), \
1422 .ec = __eid, \
1423 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1424 | (__bv1) << 8 | (__bv2) }
1426 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1427 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1429 /* Outdated IBM BIOSes often lack the EC id string */
1430 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1431 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1432 __bv1, __bv2, TPID(__id1, __id2), \
1433 __ev1, __ev2), \
1434 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1435 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1436 __ev1, __ev2)
1438 /* Outdated IBM BIOSes often lack the EC id string */
1439 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1440 __eid1, __eid2, __ev1, __ev2) \
1441 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1442 __bv1, __bv2, TPID(__eid1, __eid2), \
1443 __ev1, __ev2), \
1444 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1445 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1446 __ev1, __ev2)
1448 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1449 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1451 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1452 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1453 __bv1, __bv2, TPID(__id1, __id2), \
1454 __ev1, __ev2)
1456 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1457 __eid1, __eid2, __ev1, __ev2) \
1458 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1459 __bv1, __bv2, TPID(__eid1, __eid2), \
1460 __ev1, __ev2)
1462 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1463 /* Numeric models ------------------ */
1464 /* FW MODEL BIOS VERS */
1465 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1466 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1467 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1468 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1469 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1470 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1471 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1472 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1473 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1475 /* A-series ------------------------- */
1476 /* FW MODEL BIOS VERS EC VERS */
1477 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1478 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1479 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1480 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1481 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1482 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1483 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1484 TPV_QI0('1', '3', '2', '0'), /* A22m */
1485 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1486 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1487 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1489 /* G-series ------------------------- */
1490 /* FW MODEL BIOS VERS */
1491 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1492 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1494 /* R-series, T-series --------------- */
1495 /* FW MODEL BIOS VERS EC VERS */
1496 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1497 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1498 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1499 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1500 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1501 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1502 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1503 T40/p, T41/p, T42/p (1) */
1504 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1505 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1506 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1507 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1509 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1510 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1511 TPV_QI0('1', '6', '3', '2'), /* T22 */
1512 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1513 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1514 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1516 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1517 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1518 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1520 /* BIOS FW BIOS VERS EC FW EC VERS */
1521 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1522 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1524 /* X-series ------------------------- */
1525 /* FW MODEL BIOS VERS EC VERS */
1526 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1527 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1528 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1529 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1530 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1531 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1532 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1534 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1535 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1537 /* (0) - older versions lack DMI EC fw string and functionality */
1538 /* (1) - older versions known to lack functionality */
1541 #undef TPV_QL1
1542 #undef TPV_QL0
1543 #undef TPV_QI2
1544 #undef TPV_QI1
1545 #undef TPV_QI0
1546 #undef TPV_Q_X
1547 #undef TPV_Q
1549 static void __init tpacpi_check_outdated_fw(void)
1551 unsigned long fwvers;
1552 u16 ec_version, bios_version;
1554 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1555 ARRAY_SIZE(tpacpi_bios_version_qtable));
1557 if (!fwvers)
1558 return;
1560 bios_version = fwvers & 0xffffU;
1561 ec_version = (fwvers >> 16) & 0xffffU;
1563 /* note that unknown versions are set to 0x0000 and we use that */
1564 if ((bios_version > thinkpad_id.bios_release) ||
1565 (ec_version > thinkpad_id.ec_release &&
1566 ec_version != TPACPI_MATCH_ANY)) {
1568 * The changelogs would let us track down the exact
1569 * reason, but it is just too much of a pain to track
1570 * it. We only list BIOSes that are either really
1571 * broken, or really stable to begin with, so it is
1572 * best if the user upgrades the firmware anyway.
1574 printk(TPACPI_WARN
1575 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1576 printk(TPACPI_WARN
1577 "WARNING: This firmware may be missing critical bug "
1578 "fixes and/or important features\n");
1582 static bool __init tpacpi_is_fw_known(void)
1584 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1585 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1588 /****************************************************************************
1589 ****************************************************************************
1591 * Subdrivers
1593 ****************************************************************************
1594 ****************************************************************************/
1596 /*************************************************************************
1597 * thinkpad-acpi init subdriver
1600 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1602 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1603 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1605 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1606 (thinkpad_id.bios_version_str) ?
1607 thinkpad_id.bios_version_str : "unknown",
1608 (thinkpad_id.ec_version_str) ?
1609 thinkpad_id.ec_version_str : "unknown");
1611 if (thinkpad_id.vendor && thinkpad_id.model_str)
1612 printk(TPACPI_INFO "%s %s, model %s\n",
1613 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1614 "IBM" : ((thinkpad_id.vendor ==
1615 PCI_VENDOR_ID_LENOVO) ?
1616 "Lenovo" : "Unknown vendor"),
1617 thinkpad_id.model_str,
1618 (thinkpad_id.nummodel_str) ?
1619 thinkpad_id.nummodel_str : "unknown");
1621 tpacpi_check_outdated_fw();
1622 return 0;
1625 static int thinkpad_acpi_driver_read(char *p)
1627 int len = 0;
1629 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1630 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1632 return len;
1635 static struct ibm_struct thinkpad_acpi_driver_data = {
1636 .name = "driver",
1637 .read = thinkpad_acpi_driver_read,
1640 /*************************************************************************
1641 * Hotkey subdriver
1645 * ThinkPad firmware event model
1647 * The ThinkPad firmware has two main event interfaces: normal ACPI
1648 * notifications (which follow the ACPI standard), and a private event
1649 * interface.
1651 * The private event interface also issues events for the hotkeys. As
1652 * the driver gained features, the event handling code ended up being
1653 * built around the hotkey subdriver. This will need to be refactored
1654 * to a more formal event API eventually.
1656 * Some "hotkeys" are actually supposed to be used as event reports,
1657 * such as "brightness has changed", "volume has changed", depending on
1658 * the ThinkPad model and how the firmware is operating.
1660 * Unlike other classes, hotkey-class events have mask/unmask control on
1661 * non-ancient firmware. However, how it behaves changes a lot with the
1662 * firmware model and version.
1665 enum { /* hot key scan codes (derived from ACPI DSDT) */
1666 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1667 TP_ACPI_HOTKEYSCAN_FNF2,
1668 TP_ACPI_HOTKEYSCAN_FNF3,
1669 TP_ACPI_HOTKEYSCAN_FNF4,
1670 TP_ACPI_HOTKEYSCAN_FNF5,
1671 TP_ACPI_HOTKEYSCAN_FNF6,
1672 TP_ACPI_HOTKEYSCAN_FNF7,
1673 TP_ACPI_HOTKEYSCAN_FNF8,
1674 TP_ACPI_HOTKEYSCAN_FNF9,
1675 TP_ACPI_HOTKEYSCAN_FNF10,
1676 TP_ACPI_HOTKEYSCAN_FNF11,
1677 TP_ACPI_HOTKEYSCAN_FNF12,
1678 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1679 TP_ACPI_HOTKEYSCAN_FNINSERT,
1680 TP_ACPI_HOTKEYSCAN_FNDELETE,
1681 TP_ACPI_HOTKEYSCAN_FNHOME,
1682 TP_ACPI_HOTKEYSCAN_FNEND,
1683 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1684 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1685 TP_ACPI_HOTKEYSCAN_FNSPACE,
1686 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1687 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1688 TP_ACPI_HOTKEYSCAN_MUTE,
1689 TP_ACPI_HOTKEYSCAN_THINKPAD,
1692 enum { /* Keys/events available through NVRAM polling */
1693 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1694 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1697 enum { /* Positions of some of the keys in hotkey masks */
1698 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1699 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1700 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1701 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1702 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1703 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1704 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1705 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1706 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1707 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1708 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1711 enum { /* NVRAM to ACPI HKEY group map */
1712 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1713 TP_ACPI_HKEY_ZOOM_MASK |
1714 TP_ACPI_HKEY_DISPSWTCH_MASK |
1715 TP_ACPI_HKEY_HIBERNATE_MASK,
1716 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1717 TP_ACPI_HKEY_BRGHTDWN_MASK,
1718 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1719 TP_ACPI_HKEY_VOLDWN_MASK |
1720 TP_ACPI_HKEY_MUTE_MASK,
1723 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1724 struct tp_nvram_state {
1725 u16 thinkpad_toggle:1;
1726 u16 zoom_toggle:1;
1727 u16 display_toggle:1;
1728 u16 thinklight_toggle:1;
1729 u16 hibernate_toggle:1;
1730 u16 displayexp_toggle:1;
1731 u16 display_state:1;
1732 u16 brightness_toggle:1;
1733 u16 volume_toggle:1;
1734 u16 mute:1;
1736 u8 brightness_level;
1737 u8 volume_level;
1740 /* kthread for the hotkey poller */
1741 static struct task_struct *tpacpi_hotkey_task;
1743 /* Acquired while the poller kthread is running, use to sync start/stop */
1744 static struct mutex hotkey_thread_mutex;
1747 * Acquire mutex to write poller control variables as an
1748 * atomic block.
1750 * Increment hotkey_config_change when changing them if you
1751 * want the kthread to forget old state.
1753 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1755 static struct mutex hotkey_thread_data_mutex;
1756 static unsigned int hotkey_config_change;
1759 * hotkey poller control variables
1761 * Must be atomic or readers will also need to acquire mutex
1763 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1764 * should be used only when the changes need to be taken as
1765 * a block, OR when one needs to force the kthread to forget
1766 * old state.
1768 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1769 static unsigned int hotkey_poll_freq = 10; /* Hz */
1771 #define HOTKEY_CONFIG_CRITICAL_START \
1772 do { \
1773 mutex_lock(&hotkey_thread_data_mutex); \
1774 hotkey_config_change++; \
1775 } while (0);
1776 #define HOTKEY_CONFIG_CRITICAL_END \
1777 mutex_unlock(&hotkey_thread_data_mutex);
1779 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1781 #define hotkey_source_mask 0U
1782 #define HOTKEY_CONFIG_CRITICAL_START
1783 #define HOTKEY_CONFIG_CRITICAL_END
1785 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1787 static struct mutex hotkey_mutex;
1789 static enum { /* Reasons for waking up */
1790 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1791 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1792 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1793 } hotkey_wakeup_reason;
1795 static int hotkey_autosleep_ack;
1797 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1798 static u32 hotkey_all_mask; /* all events supported in fw */
1799 static u32 hotkey_reserved_mask; /* events better left disabled */
1800 static u32 hotkey_driver_mask; /* events needed by the driver */
1801 static u32 hotkey_user_mask; /* events visible to userspace */
1802 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1804 static unsigned int hotkey_report_mode;
1806 static u16 *hotkey_keycode_map;
1808 static struct attribute_set *hotkey_dev_attributes;
1810 static void tpacpi_driver_event(const unsigned int hkey_event);
1811 static void hotkey_driver_event(const unsigned int scancode);
1813 /* HKEY.MHKG() return bits */
1814 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1816 static int hotkey_get_wlsw(int *status)
1818 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1819 if (dbg_wlswemul) {
1820 *status = !!tpacpi_wlsw_emulstate;
1821 return 0;
1823 #endif
1824 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1825 return -EIO;
1826 return 0;
1829 static int hotkey_get_tablet_mode(int *status)
1831 int s;
1833 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1834 return -EIO;
1836 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1837 return 0;
1841 * Reads current event mask from firmware, and updates
1842 * hotkey_acpi_mask accordingly. Also resets any bits
1843 * from hotkey_user_mask that are unavailable to be
1844 * delivered (shadow requirement of the userspace ABI).
1846 * Call with hotkey_mutex held
1848 static int hotkey_mask_get(void)
1850 if (tp_features.hotkey_mask) {
1851 u32 m = 0;
1853 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1854 return -EIO;
1856 hotkey_acpi_mask = m;
1857 } else {
1858 /* no mask support doesn't mean no event support... */
1859 hotkey_acpi_mask = hotkey_all_mask;
1862 /* sync userspace-visible mask */
1863 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1865 return 0;
1868 void static hotkey_mask_warn_incomplete_mask(void)
1870 /* log only what the user can fix... */
1871 const u32 wantedmask = hotkey_driver_mask &
1872 ~(hotkey_acpi_mask | hotkey_source_mask) &
1873 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1875 if (wantedmask)
1876 printk(TPACPI_NOTICE
1877 "required events 0x%08x not enabled!\n",
1878 wantedmask);
1882 * Set the firmware mask when supported
1884 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1886 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1888 * Call with hotkey_mutex held
1890 static int hotkey_mask_set(u32 mask)
1892 int i;
1893 int rc = 0;
1895 const u32 fwmask = mask & ~hotkey_source_mask;
1897 if (tp_features.hotkey_mask) {
1898 for (i = 0; i < 32; i++) {
1899 if (!acpi_evalf(hkey_handle,
1900 NULL, "MHKM", "vdd", i + 1,
1901 !!(mask & (1 << i)))) {
1902 rc = -EIO;
1903 break;
1909 * We *must* make an inconditional call to hotkey_mask_get to
1910 * refresh hotkey_acpi_mask and update hotkey_user_mask
1912 * Take the opportunity to also log when we cannot _enable_
1913 * a given event.
1915 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1916 printk(TPACPI_NOTICE
1917 "asked for hotkey mask 0x%08x, but "
1918 "firmware forced it to 0x%08x\n",
1919 fwmask, hotkey_acpi_mask);
1922 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1923 hotkey_mask_warn_incomplete_mask();
1925 return rc;
1929 * Sets hotkey_user_mask and tries to set the firmware mask
1931 * Call with hotkey_mutex held
1933 static int hotkey_user_mask_set(const u32 mask)
1935 int rc;
1937 /* Give people a chance to notice they are doing something that
1938 * is bound to go boom on their users sooner or later */
1939 if (!tp_warned.hotkey_mask_ff &&
1940 (mask == 0xffff || mask == 0xffffff ||
1941 mask == 0xffffffff)) {
1942 tp_warned.hotkey_mask_ff = 1;
1943 printk(TPACPI_NOTICE
1944 "setting the hotkey mask to 0x%08x is likely "
1945 "not the best way to go about it\n", mask);
1946 printk(TPACPI_NOTICE
1947 "please consider using the driver defaults, "
1948 "and refer to up-to-date thinkpad-acpi "
1949 "documentation\n");
1952 /* Try to enable what the user asked for, plus whatever we need.
1953 * this syncs everything but won't enable bits in hotkey_user_mask */
1954 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1956 /* Enable the available bits in hotkey_user_mask */
1957 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1959 return rc;
1963 * Sets the driver hotkey mask.
1965 * Can be called even if the hotkey subdriver is inactive
1967 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1969 int rc;
1971 /* Do the right thing if hotkey_init has not been called yet */
1972 if (!tp_features.hotkey) {
1973 hotkey_driver_mask = mask;
1974 return 0;
1977 mutex_lock(&hotkey_mutex);
1979 HOTKEY_CONFIG_CRITICAL_START
1980 hotkey_driver_mask = mask;
1981 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1982 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1983 #endif
1984 HOTKEY_CONFIG_CRITICAL_END
1986 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1987 ~hotkey_source_mask);
1988 mutex_unlock(&hotkey_mutex);
1990 return rc;
1993 static int hotkey_status_get(int *status)
1995 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1996 return -EIO;
1998 return 0;
2001 static int hotkey_status_set(bool enable)
2003 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2004 return -EIO;
2006 return 0;
2009 static void tpacpi_input_send_tabletsw(void)
2011 int state;
2013 if (tp_features.hotkey_tablet &&
2014 !hotkey_get_tablet_mode(&state)) {
2015 mutex_lock(&tpacpi_inputdev_send_mutex);
2017 input_report_switch(tpacpi_inputdev,
2018 SW_TABLET_MODE, !!state);
2019 input_sync(tpacpi_inputdev);
2021 mutex_unlock(&tpacpi_inputdev_send_mutex);
2025 /* Do NOT call without validating scancode first */
2026 static void tpacpi_input_send_key(const unsigned int scancode)
2028 const unsigned int keycode = hotkey_keycode_map[scancode];
2030 if (keycode != KEY_RESERVED) {
2031 mutex_lock(&tpacpi_inputdev_send_mutex);
2033 input_report_key(tpacpi_inputdev, keycode, 1);
2034 if (keycode == KEY_UNKNOWN)
2035 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2036 scancode);
2037 input_sync(tpacpi_inputdev);
2039 input_report_key(tpacpi_inputdev, keycode, 0);
2040 if (keycode == KEY_UNKNOWN)
2041 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2042 scancode);
2043 input_sync(tpacpi_inputdev);
2045 mutex_unlock(&tpacpi_inputdev_send_mutex);
2049 /* Do NOT call without validating scancode first */
2050 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2052 hotkey_driver_event(scancode);
2053 if (hotkey_user_mask & (1 << scancode))
2054 tpacpi_input_send_key(scancode);
2057 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2058 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2060 /* Do NOT call without validating scancode first */
2061 static void tpacpi_hotkey_send_key(unsigned int scancode)
2063 tpacpi_input_send_key_masked(scancode);
2064 if (hotkey_report_mode < 2) {
2065 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2066 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2070 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2072 u8 d;
2074 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2075 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2076 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2077 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2078 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2079 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2081 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2082 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2083 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2085 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2086 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2087 n->displayexp_toggle =
2088 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2090 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2091 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2092 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2093 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2094 n->brightness_toggle =
2095 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2097 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2098 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2099 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2100 >> TP_NVRAM_POS_LEVEL_VOLUME;
2101 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2102 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2106 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2107 struct tp_nvram_state *newn,
2108 const u32 event_mask)
2111 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2112 do { \
2113 if ((event_mask & (1 << __scancode)) && \
2114 oldn->__member != newn->__member) \
2115 tpacpi_hotkey_send_key(__scancode); \
2116 } while (0)
2118 #define TPACPI_MAY_SEND_KEY(__scancode) \
2119 do { \
2120 if (event_mask & (1 << __scancode)) \
2121 tpacpi_hotkey_send_key(__scancode); \
2122 } while (0)
2124 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2125 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2126 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2127 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2129 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2131 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2133 /* handle volume */
2134 if (oldn->volume_toggle != newn->volume_toggle) {
2135 if (oldn->mute != newn->mute) {
2136 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2138 if (oldn->volume_level > newn->volume_level) {
2139 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2140 } else if (oldn->volume_level < newn->volume_level) {
2141 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2142 } else if (oldn->mute == newn->mute) {
2143 /* repeated key presses that didn't change state */
2144 if (newn->mute) {
2145 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2146 } else if (newn->volume_level != 0) {
2147 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2148 } else {
2149 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2154 /* handle brightness */
2155 if (oldn->brightness_toggle != newn->brightness_toggle) {
2156 if (oldn->brightness_level < newn->brightness_level) {
2157 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2158 } else if (oldn->brightness_level > newn->brightness_level) {
2159 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2160 } else {
2161 /* repeated key presses that didn't change state */
2162 if (newn->brightness_level != 0) {
2163 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2164 } else {
2165 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2170 #undef TPACPI_COMPARE_KEY
2171 #undef TPACPI_MAY_SEND_KEY
2175 * Polling driver
2177 * We track all events in hotkey_source_mask all the time, since
2178 * most of them are edge-based. We only issue those requested by
2179 * hotkey_user_mask or hotkey_driver_mask, though.
2181 static int hotkey_kthread(void *data)
2183 struct tp_nvram_state s[2];
2184 u32 poll_mask, event_mask;
2185 unsigned int si, so;
2186 unsigned long t;
2187 unsigned int change_detector, must_reset;
2188 unsigned int poll_freq;
2190 mutex_lock(&hotkey_thread_mutex);
2192 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2193 goto exit;
2195 set_freezable();
2197 so = 0;
2198 si = 1;
2199 t = 0;
2201 /* Initial state for compares */
2202 mutex_lock(&hotkey_thread_data_mutex);
2203 change_detector = hotkey_config_change;
2204 poll_mask = hotkey_source_mask;
2205 event_mask = hotkey_source_mask &
2206 (hotkey_driver_mask | hotkey_user_mask);
2207 poll_freq = hotkey_poll_freq;
2208 mutex_unlock(&hotkey_thread_data_mutex);
2209 hotkey_read_nvram(&s[so], poll_mask);
2211 while (!kthread_should_stop()) {
2212 if (t == 0) {
2213 if (likely(poll_freq))
2214 t = 1000/poll_freq;
2215 else
2216 t = 100; /* should never happen... */
2218 t = msleep_interruptible(t);
2219 if (unlikely(kthread_should_stop()))
2220 break;
2221 must_reset = try_to_freeze();
2222 if (t > 0 && !must_reset)
2223 continue;
2225 mutex_lock(&hotkey_thread_data_mutex);
2226 if (must_reset || hotkey_config_change != change_detector) {
2227 /* forget old state on thaw or config change */
2228 si = so;
2229 t = 0;
2230 change_detector = hotkey_config_change;
2232 poll_mask = hotkey_source_mask;
2233 event_mask = hotkey_source_mask &
2234 (hotkey_driver_mask | hotkey_user_mask);
2235 poll_freq = hotkey_poll_freq;
2236 mutex_unlock(&hotkey_thread_data_mutex);
2238 if (likely(poll_mask)) {
2239 hotkey_read_nvram(&s[si], poll_mask);
2240 if (likely(si != so)) {
2241 hotkey_compare_and_issue_event(&s[so], &s[si],
2242 event_mask);
2246 so = si;
2247 si ^= 1;
2250 exit:
2251 mutex_unlock(&hotkey_thread_mutex);
2252 return 0;
2255 /* call with hotkey_mutex held */
2256 static void hotkey_poll_stop_sync(void)
2258 if (tpacpi_hotkey_task) {
2259 if (frozen(tpacpi_hotkey_task) ||
2260 freezing(tpacpi_hotkey_task))
2261 thaw_process(tpacpi_hotkey_task);
2263 kthread_stop(tpacpi_hotkey_task);
2264 tpacpi_hotkey_task = NULL;
2265 mutex_lock(&hotkey_thread_mutex);
2266 /* at this point, the thread did exit */
2267 mutex_unlock(&hotkey_thread_mutex);
2271 /* call with hotkey_mutex held */
2272 static void hotkey_poll_setup(bool may_warn)
2274 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2275 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2277 if (hotkey_poll_freq > 0 &&
2278 (poll_driver_mask ||
2279 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2280 if (!tpacpi_hotkey_task) {
2281 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2282 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2283 if (IS_ERR(tpacpi_hotkey_task)) {
2284 tpacpi_hotkey_task = NULL;
2285 printk(TPACPI_ERR
2286 "could not create kernel thread "
2287 "for hotkey polling\n");
2290 } else {
2291 hotkey_poll_stop_sync();
2292 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2293 hotkey_poll_freq == 0) {
2294 printk(TPACPI_NOTICE
2295 "hot keys 0x%08x and/or events 0x%08x "
2296 "require polling, which is currently "
2297 "disabled\n",
2298 poll_user_mask, poll_driver_mask);
2303 static void hotkey_poll_setup_safe(bool may_warn)
2305 mutex_lock(&hotkey_mutex);
2306 hotkey_poll_setup(may_warn);
2307 mutex_unlock(&hotkey_mutex);
2310 /* call with hotkey_mutex held */
2311 static void hotkey_poll_set_freq(unsigned int freq)
2313 if (!freq)
2314 hotkey_poll_stop_sync();
2316 hotkey_poll_freq = freq;
2319 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2321 static void hotkey_poll_setup_safe(bool __unused)
2325 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2327 static int hotkey_inputdev_open(struct input_dev *dev)
2329 switch (tpacpi_lifecycle) {
2330 case TPACPI_LIFE_INIT:
2332 * hotkey_init will call hotkey_poll_setup_safe
2333 * at the appropriate moment
2335 return 0;
2336 case TPACPI_LIFE_EXITING:
2337 return -EBUSY;
2338 case TPACPI_LIFE_RUNNING:
2339 hotkey_poll_setup_safe(false);
2340 return 0;
2343 /* Should only happen if tpacpi_lifecycle is corrupt */
2344 BUG();
2345 return -EBUSY;
2348 static void hotkey_inputdev_close(struct input_dev *dev)
2350 /* disable hotkey polling when possible */
2351 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2352 !(hotkey_source_mask & hotkey_driver_mask))
2353 hotkey_poll_setup_safe(false);
2356 /* sysfs hotkey enable ------------------------------------------------- */
2357 static ssize_t hotkey_enable_show(struct device *dev,
2358 struct device_attribute *attr,
2359 char *buf)
2361 int res, status;
2363 printk_deprecated_attribute("hotkey_enable",
2364 "Hotkey reporting is always enabled");
2366 res = hotkey_status_get(&status);
2367 if (res)
2368 return res;
2370 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2373 static ssize_t hotkey_enable_store(struct device *dev,
2374 struct device_attribute *attr,
2375 const char *buf, size_t count)
2377 unsigned long t;
2379 printk_deprecated_attribute("hotkey_enable",
2380 "Hotkeys can be disabled through hotkey_mask");
2382 if (parse_strtoul(buf, 1, &t))
2383 return -EINVAL;
2385 if (t == 0)
2386 return -EPERM;
2388 return count;
2391 static struct device_attribute dev_attr_hotkey_enable =
2392 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2393 hotkey_enable_show, hotkey_enable_store);
2395 /* sysfs hotkey mask --------------------------------------------------- */
2396 static ssize_t hotkey_mask_show(struct device *dev,
2397 struct device_attribute *attr,
2398 char *buf)
2400 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2403 static ssize_t hotkey_mask_store(struct device *dev,
2404 struct device_attribute *attr,
2405 const char *buf, size_t count)
2407 unsigned long t;
2408 int res;
2410 if (parse_strtoul(buf, 0xffffffffUL, &t))
2411 return -EINVAL;
2413 if (mutex_lock_killable(&hotkey_mutex))
2414 return -ERESTARTSYS;
2416 res = hotkey_user_mask_set(t);
2418 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2419 hotkey_poll_setup(true);
2420 #endif
2422 mutex_unlock(&hotkey_mutex);
2424 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2426 return (res) ? res : count;
2429 static struct device_attribute dev_attr_hotkey_mask =
2430 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2431 hotkey_mask_show, hotkey_mask_store);
2433 /* sysfs hotkey bios_enabled ------------------------------------------- */
2434 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2435 struct device_attribute *attr,
2436 char *buf)
2438 return sprintf(buf, "0\n");
2441 static struct device_attribute dev_attr_hotkey_bios_enabled =
2442 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2444 /* sysfs hotkey bios_mask ---------------------------------------------- */
2445 static ssize_t hotkey_bios_mask_show(struct device *dev,
2446 struct device_attribute *attr,
2447 char *buf)
2449 printk_deprecated_attribute("hotkey_bios_mask",
2450 "This attribute is useless.");
2451 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2454 static struct device_attribute dev_attr_hotkey_bios_mask =
2455 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2457 /* sysfs hotkey all_mask ----------------------------------------------- */
2458 static ssize_t hotkey_all_mask_show(struct device *dev,
2459 struct device_attribute *attr,
2460 char *buf)
2462 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2463 hotkey_all_mask | hotkey_source_mask);
2466 static struct device_attribute dev_attr_hotkey_all_mask =
2467 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2469 /* sysfs hotkey recommended_mask --------------------------------------- */
2470 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2471 struct device_attribute *attr,
2472 char *buf)
2474 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2475 (hotkey_all_mask | hotkey_source_mask)
2476 & ~hotkey_reserved_mask);
2479 static struct device_attribute dev_attr_hotkey_recommended_mask =
2480 __ATTR(hotkey_recommended_mask, S_IRUGO,
2481 hotkey_recommended_mask_show, NULL);
2483 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2485 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2486 static ssize_t hotkey_source_mask_show(struct device *dev,
2487 struct device_attribute *attr,
2488 char *buf)
2490 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2493 static ssize_t hotkey_source_mask_store(struct device *dev,
2494 struct device_attribute *attr,
2495 const char *buf, size_t count)
2497 unsigned long t;
2498 u32 r_ev;
2499 int rc;
2501 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2502 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2503 return -EINVAL;
2505 if (mutex_lock_killable(&hotkey_mutex))
2506 return -ERESTARTSYS;
2508 HOTKEY_CONFIG_CRITICAL_START
2509 hotkey_source_mask = t;
2510 HOTKEY_CONFIG_CRITICAL_END
2512 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2513 ~hotkey_source_mask);
2514 hotkey_poll_setup(true);
2516 /* check if events needed by the driver got disabled */
2517 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2518 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2520 mutex_unlock(&hotkey_mutex);
2522 if (rc < 0)
2523 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2524 "firmware event mask!\n");
2526 if (r_ev)
2527 printk(TPACPI_NOTICE "hotkey_source_mask: "
2528 "some important events were disabled: "
2529 "0x%04x\n", r_ev);
2531 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2533 return (rc < 0) ? rc : count;
2536 static struct device_attribute dev_attr_hotkey_source_mask =
2537 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2538 hotkey_source_mask_show, hotkey_source_mask_store);
2540 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2541 static ssize_t hotkey_poll_freq_show(struct device *dev,
2542 struct device_attribute *attr,
2543 char *buf)
2545 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2548 static ssize_t hotkey_poll_freq_store(struct device *dev,
2549 struct device_attribute *attr,
2550 const char *buf, size_t count)
2552 unsigned long t;
2554 if (parse_strtoul(buf, 25, &t))
2555 return -EINVAL;
2557 if (mutex_lock_killable(&hotkey_mutex))
2558 return -ERESTARTSYS;
2560 hotkey_poll_set_freq(t);
2561 hotkey_poll_setup(true);
2563 mutex_unlock(&hotkey_mutex);
2565 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2567 return count;
2570 static struct device_attribute dev_attr_hotkey_poll_freq =
2571 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2572 hotkey_poll_freq_show, hotkey_poll_freq_store);
2574 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2576 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2577 static ssize_t hotkey_radio_sw_show(struct device *dev,
2578 struct device_attribute *attr,
2579 char *buf)
2581 int res, s;
2582 res = hotkey_get_wlsw(&s);
2583 if (res < 0)
2584 return res;
2586 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2589 static struct device_attribute dev_attr_hotkey_radio_sw =
2590 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2592 static void hotkey_radio_sw_notify_change(void)
2594 if (tp_features.hotkey_wlsw)
2595 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2596 "hotkey_radio_sw");
2599 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2600 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2601 struct device_attribute *attr,
2602 char *buf)
2604 int res, s;
2605 res = hotkey_get_tablet_mode(&s);
2606 if (res < 0)
2607 return res;
2609 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2612 static struct device_attribute dev_attr_hotkey_tablet_mode =
2613 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2615 static void hotkey_tablet_mode_notify_change(void)
2617 if (tp_features.hotkey_tablet)
2618 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2619 "hotkey_tablet_mode");
2622 /* sysfs hotkey report_mode -------------------------------------------- */
2623 static ssize_t hotkey_report_mode_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2627 return snprintf(buf, PAGE_SIZE, "%d\n",
2628 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2631 static struct device_attribute dev_attr_hotkey_report_mode =
2632 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2634 /* sysfs wakeup reason (pollable) -------------------------------------- */
2635 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2636 struct device_attribute *attr,
2637 char *buf)
2639 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2642 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2643 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2645 static void hotkey_wakeup_reason_notify_change(void)
2647 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2648 "wakeup_reason");
2651 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2652 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2653 struct device_attribute *attr,
2654 char *buf)
2656 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2659 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2660 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2661 hotkey_wakeup_hotunplug_complete_show, NULL);
2663 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2665 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2666 "wakeup_hotunplug_complete");
2669 /* --------------------------------------------------------------------- */
2671 static struct attribute *hotkey_attributes[] __initdata = {
2672 &dev_attr_hotkey_enable.attr,
2673 &dev_attr_hotkey_bios_enabled.attr,
2674 &dev_attr_hotkey_bios_mask.attr,
2675 &dev_attr_hotkey_report_mode.attr,
2676 &dev_attr_hotkey_wakeup_reason.attr,
2677 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2678 &dev_attr_hotkey_mask.attr,
2679 &dev_attr_hotkey_all_mask.attr,
2680 &dev_attr_hotkey_recommended_mask.attr,
2681 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2682 &dev_attr_hotkey_source_mask.attr,
2683 &dev_attr_hotkey_poll_freq.attr,
2684 #endif
2687 static void bluetooth_update_rfk(void);
2688 static void wan_update_rfk(void);
2689 static void uwb_update_rfk(void);
2690 static void tpacpi_send_radiosw_update(void)
2692 int wlsw;
2694 /* Sync these BEFORE sending any rfkill events */
2695 if (tp_features.bluetooth)
2696 bluetooth_update_rfk();
2697 if (tp_features.wan)
2698 wan_update_rfk();
2699 if (tp_features.uwb)
2700 uwb_update_rfk();
2702 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2703 mutex_lock(&tpacpi_inputdev_send_mutex);
2705 input_report_switch(tpacpi_inputdev,
2706 SW_RFKILL_ALL, !!wlsw);
2707 input_sync(tpacpi_inputdev);
2709 mutex_unlock(&tpacpi_inputdev_send_mutex);
2711 hotkey_radio_sw_notify_change();
2714 static void hotkey_exit(void)
2716 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2717 mutex_lock(&hotkey_mutex);
2718 hotkey_poll_stop_sync();
2719 mutex_unlock(&hotkey_mutex);
2720 #endif
2722 if (hotkey_dev_attributes)
2723 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2725 kfree(hotkey_keycode_map);
2727 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2728 "restoring original HKEY status and mask\n");
2729 /* yes, there is a bitwise or below, we want the
2730 * functions to be called even if one of them fail */
2731 if (((tp_features.hotkey_mask &&
2732 hotkey_mask_set(hotkey_orig_mask)) |
2733 hotkey_status_set(false)) != 0)
2734 printk(TPACPI_ERR
2735 "failed to restore hot key mask "
2736 "to BIOS defaults\n");
2739 static void __init hotkey_unmap(const unsigned int scancode)
2741 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2742 clear_bit(hotkey_keycode_map[scancode],
2743 tpacpi_inputdev->keybit);
2744 hotkey_keycode_map[scancode] = KEY_RESERVED;
2749 * HKEY quirks:
2750 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2753 #define TPACPI_HK_Q_INIMASK 0x0001
2755 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2756 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2757 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2758 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2759 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2760 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2761 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2762 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2763 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2764 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2765 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2766 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2767 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2768 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2769 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2770 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2771 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2772 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2773 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2774 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2777 static int __init hotkey_init(struct ibm_init_struct *iibm)
2779 /* Requirements for changing the default keymaps:
2781 * 1. Many of the keys are mapped to KEY_RESERVED for very
2782 * good reasons. Do not change them unless you have deep
2783 * knowledge on the IBM and Lenovo ThinkPad firmware for
2784 * the various ThinkPad models. The driver behaves
2785 * differently for KEY_RESERVED: such keys have their
2786 * hot key mask *unset* in mask_recommended, and also
2787 * in the initial hot key mask programmed into the
2788 * firmware at driver load time, which means the firm-
2789 * ware may react very differently if you change them to
2790 * something else;
2792 * 2. You must be subscribed to the linux-thinkpad and
2793 * ibm-acpi-devel mailing lists, and you should read the
2794 * list archives since 2007 if you want to change the
2795 * keymaps. This requirement exists so that you will
2796 * know the past history of problems with the thinkpad-
2797 * acpi driver keymaps, and also that you will be
2798 * listening to any bug reports;
2800 * 3. Do not send thinkpad-acpi specific patches directly to
2801 * for merging, *ever*. Send them to the linux-acpi
2802 * mailinglist for comments. Merging is to be done only
2803 * through acpi-test and the ACPI maintainer.
2805 * If the above is too much to ask, don't change the keymap.
2806 * Ask the thinkpad-acpi maintainer to do it, instead.
2808 static u16 ibm_keycode_map[] __initdata = {
2809 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2810 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2811 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2812 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2814 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2815 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2816 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2817 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2819 /* brightness: firmware always reacts to them */
2820 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2821 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2823 /* Thinklight: firmware always react to it */
2824 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2826 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2827 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2829 /* Volume: firmware always react to it and reprograms
2830 * the built-in *extra* mixer. Never map it to control
2831 * another mixer by default. */
2832 KEY_RESERVED, /* 0x14: VOLUME UP */
2833 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2834 KEY_RESERVED, /* 0x16: MUTE */
2836 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2838 /* (assignments unknown, please report if found) */
2839 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2840 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2842 static u16 lenovo_keycode_map[] __initdata = {
2843 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2844 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2845 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2846 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2848 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2849 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2850 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2851 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2853 /* These should be enabled --only-- when ACPI video
2854 * is disabled (i.e. in "vendor" mode), and are handled
2855 * in a special way by the init code */
2856 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2857 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2859 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2861 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2862 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2864 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2865 * react to it and reprograms the built-in *extra* mixer.
2866 * Never map it to control another mixer by default.
2868 * T60?, T61, R60?, R61: firmware and EC tries to send
2869 * these over the regular keyboard, so these are no-ops,
2870 * but there are still weird bugs re. MUTE, so do not
2871 * change unless you get test reports from all Lenovo
2872 * models. May cause the BIOS to interfere with the
2873 * HDA mixer.
2875 KEY_RESERVED, /* 0x14: VOLUME UP */
2876 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2877 KEY_RESERVED, /* 0x16: MUTE */
2879 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2881 /* (assignments unknown, please report if found) */
2882 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2883 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2886 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2887 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2888 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2890 int res, i;
2891 int status;
2892 int hkeyv;
2894 unsigned long quirks;
2896 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2897 "initializing hotkey subdriver\n");
2899 BUG_ON(!tpacpi_inputdev);
2900 BUG_ON(tpacpi_inputdev->open != NULL ||
2901 tpacpi_inputdev->close != NULL);
2903 TPACPI_ACPIHANDLE_INIT(hkey);
2904 mutex_init(&hotkey_mutex);
2906 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2907 mutex_init(&hotkey_thread_mutex);
2908 mutex_init(&hotkey_thread_data_mutex);
2909 #endif
2911 /* hotkey not supported on 570 */
2912 tp_features.hotkey = hkey_handle != NULL;
2914 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2915 "hotkeys are %s\n",
2916 str_supported(tp_features.hotkey));
2918 if (!tp_features.hotkey)
2919 return 1;
2921 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2922 ARRAY_SIZE(tpacpi_hotkey_qtable));
2924 tpacpi_disable_brightness_delay();
2926 /* MUST have enough space for all attributes to be added to
2927 * hotkey_dev_attributes */
2928 hotkey_dev_attributes = create_attr_set(
2929 ARRAY_SIZE(hotkey_attributes) + 2,
2930 NULL);
2931 if (!hotkey_dev_attributes)
2932 return -ENOMEM;
2933 res = add_many_to_attr_set(hotkey_dev_attributes,
2934 hotkey_attributes,
2935 ARRAY_SIZE(hotkey_attributes));
2936 if (res)
2937 goto err_exit;
2939 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2940 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2941 for HKEY interface version 0x100 */
2942 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2943 if ((hkeyv >> 8) != 1) {
2944 printk(TPACPI_ERR "unknown version of the "
2945 "HKEY interface: 0x%x\n", hkeyv);
2946 printk(TPACPI_ERR "please report this to %s\n",
2947 TPACPI_MAIL);
2948 } else {
2950 * MHKV 0x100 in A31, R40, R40e,
2951 * T4x, X31, and later
2953 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2954 "firmware HKEY interface version: 0x%x\n",
2955 hkeyv);
2957 /* Paranoia check AND init hotkey_all_mask */
2958 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2959 "MHKA", "qd")) {
2960 printk(TPACPI_ERR
2961 "missing MHKA handler, "
2962 "please report this to %s\n",
2963 TPACPI_MAIL);
2964 /* Fallback: pre-init for FN+F3,F4,F12 */
2965 hotkey_all_mask = 0x080cU;
2966 } else {
2967 tp_features.hotkey_mask = 1;
2972 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2973 "hotkey masks are %s\n",
2974 str_supported(tp_features.hotkey_mask));
2976 /* Init hotkey_all_mask if not initialized yet */
2977 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2978 (quirks & TPACPI_HK_Q_INIMASK))
2979 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2981 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2982 if (tp_features.hotkey_mask) {
2983 /* hotkey_source_mask *must* be zero for
2984 * the first hotkey_mask_get to return hotkey_orig_mask */
2985 res = hotkey_mask_get();
2986 if (res)
2987 goto err_exit;
2989 hotkey_orig_mask = hotkey_acpi_mask;
2990 } else {
2991 hotkey_orig_mask = hotkey_all_mask;
2992 hotkey_acpi_mask = hotkey_all_mask;
2995 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2996 if (dbg_wlswemul) {
2997 tp_features.hotkey_wlsw = 1;
2998 printk(TPACPI_INFO
2999 "radio switch emulation enabled\n");
3000 } else
3001 #endif
3002 /* Not all thinkpads have a hardware radio switch */
3003 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3004 tp_features.hotkey_wlsw = 1;
3005 printk(TPACPI_INFO
3006 "radio switch found; radios are %s\n",
3007 enabled(status, 0));
3009 if (tp_features.hotkey_wlsw)
3010 res = add_to_attr_set(hotkey_dev_attributes,
3011 &dev_attr_hotkey_radio_sw.attr);
3013 /* For X41t, X60t, X61t Tablets... */
3014 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3015 tp_features.hotkey_tablet = 1;
3016 printk(TPACPI_INFO
3017 "possible tablet mode switch found; "
3018 "ThinkPad in %s mode\n",
3019 (status & TP_HOTKEY_TABLET_MASK)?
3020 "tablet" : "laptop");
3021 res = add_to_attr_set(hotkey_dev_attributes,
3022 &dev_attr_hotkey_tablet_mode.attr);
3025 if (!res)
3026 res = register_attr_set_with_sysfs(
3027 hotkey_dev_attributes,
3028 &tpacpi_pdev->dev.kobj);
3029 if (res)
3030 goto err_exit;
3032 /* Set up key map */
3034 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3035 GFP_KERNEL);
3036 if (!hotkey_keycode_map) {
3037 printk(TPACPI_ERR
3038 "failed to allocate memory for key map\n");
3039 res = -ENOMEM;
3040 goto err_exit;
3043 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3044 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3045 "using Lenovo default hot key map\n");
3046 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3047 TPACPI_HOTKEY_MAP_SIZE);
3048 } else {
3049 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3050 "using IBM default hot key map\n");
3051 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3052 TPACPI_HOTKEY_MAP_SIZE);
3055 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3056 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3057 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3058 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3059 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3060 tpacpi_inputdev->keycode = hotkey_keycode_map;
3061 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3062 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3063 set_bit(hotkey_keycode_map[i],
3064 tpacpi_inputdev->keybit);
3065 } else {
3066 if (i < sizeof(hotkey_reserved_mask)*8)
3067 hotkey_reserved_mask |= 1 << i;
3071 if (tp_features.hotkey_wlsw) {
3072 set_bit(EV_SW, tpacpi_inputdev->evbit);
3073 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3075 if (tp_features.hotkey_tablet) {
3076 set_bit(EV_SW, tpacpi_inputdev->evbit);
3077 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3080 /* Do not issue duplicate brightness change events to
3081 * userspace */
3082 if (!tp_features.bright_acpimode)
3083 /* update bright_acpimode... */
3084 tpacpi_check_std_acpi_brightness_support();
3086 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3087 printk(TPACPI_INFO
3088 "This ThinkPad has standard ACPI backlight "
3089 "brightness control, supported by the ACPI "
3090 "video driver\n");
3091 printk(TPACPI_NOTICE
3092 "Disabling thinkpad-acpi brightness events "
3093 "by default...\n");
3095 /* Disable brightness up/down on Lenovo thinkpads when
3096 * ACPI is handling them, otherwise it is plain impossible
3097 * for userspace to do something even remotely sane */
3098 hotkey_reserved_mask |=
3099 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3100 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3101 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3102 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3105 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3106 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3107 & ~hotkey_all_mask
3108 & ~hotkey_reserved_mask;
3110 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3111 "hotkey source mask 0x%08x, polling freq %u\n",
3112 hotkey_source_mask, hotkey_poll_freq);
3113 #endif
3115 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3116 "enabling firmware HKEY event interface...\n");
3117 res = hotkey_status_set(true);
3118 if (res) {
3119 hotkey_exit();
3120 return res;
3122 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3123 | hotkey_driver_mask)
3124 & ~hotkey_source_mask);
3125 if (res < 0 && res != -ENXIO) {
3126 hotkey_exit();
3127 return res;
3129 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3130 & ~hotkey_reserved_mask;
3131 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3132 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3133 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3135 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3136 "legacy ibm/hotkey event reporting over procfs %s\n",
3137 (hotkey_report_mode < 2) ?
3138 "enabled" : "disabled");
3140 tpacpi_inputdev->open = &hotkey_inputdev_open;
3141 tpacpi_inputdev->close = &hotkey_inputdev_close;
3143 hotkey_poll_setup_safe(true);
3144 tpacpi_send_radiosw_update();
3145 tpacpi_input_send_tabletsw();
3147 return 0;
3149 err_exit:
3150 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3151 hotkey_dev_attributes = NULL;
3153 return (res < 0)? res : 1;
3156 static bool hotkey_notify_hotkey(const u32 hkey,
3157 bool *send_acpi_ev,
3158 bool *ignore_acpi_ev)
3160 /* 0x1000-0x1FFF: key presses */
3161 unsigned int scancode = hkey & 0xfff;
3162 *send_acpi_ev = true;
3163 *ignore_acpi_ev = false;
3165 if (scancode > 0 && scancode < 0x21) {
3166 scancode--;
3167 if (!(hotkey_source_mask & (1 << scancode))) {
3168 tpacpi_input_send_key_masked(scancode);
3169 *send_acpi_ev = false;
3170 } else {
3171 *ignore_acpi_ev = true;
3173 return true;
3175 return false;
3178 static bool hotkey_notify_wakeup(const u32 hkey,
3179 bool *send_acpi_ev,
3180 bool *ignore_acpi_ev)
3182 /* 0x2000-0x2FFF: Wakeup reason */
3183 *send_acpi_ev = true;
3184 *ignore_acpi_ev = false;
3186 switch (hkey) {
3187 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3188 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3189 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3190 *ignore_acpi_ev = true;
3191 break;
3193 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3194 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3195 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3196 *ignore_acpi_ev = true;
3197 break;
3199 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3200 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3201 printk(TPACPI_ALERT
3202 "EMERGENCY WAKEUP: battery almost empty\n");
3203 /* how to auto-heal: */
3204 /* 2313: woke up from S3, go to S4/S5 */
3205 /* 2413: woke up from S4, go to S5 */
3206 break;
3208 default:
3209 return false;
3212 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3213 printk(TPACPI_INFO
3214 "woke up due to a hot-unplug "
3215 "request...\n");
3216 hotkey_wakeup_reason_notify_change();
3218 return true;
3221 static bool hotkey_notify_usrevent(const u32 hkey,
3222 bool *send_acpi_ev,
3223 bool *ignore_acpi_ev)
3225 /* 0x5000-0x5FFF: human interface helpers */
3226 *send_acpi_ev = true;
3227 *ignore_acpi_ev = false;
3229 switch (hkey) {
3230 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3231 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3232 return true;
3234 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3235 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3236 tpacpi_input_send_tabletsw();
3237 hotkey_tablet_mode_notify_change();
3238 *send_acpi_ev = false;
3239 return true;
3241 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3242 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3243 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3244 /* do not propagate these events */
3245 *ignore_acpi_ev = true;
3246 return true;
3248 default:
3249 return false;
3253 static bool hotkey_notify_thermal(const u32 hkey,
3254 bool *send_acpi_ev,
3255 bool *ignore_acpi_ev)
3257 /* 0x6000-0x6FFF: thermal alarms */
3258 *send_acpi_ev = true;
3259 *ignore_acpi_ev = false;
3261 switch (hkey) {
3262 case TP_HKEY_EV_ALARM_BAT_HOT:
3263 printk(TPACPI_CRIT
3264 "THERMAL ALARM: battery is too hot!\n");
3265 /* recommended action: warn user through gui */
3266 return true;
3267 case TP_HKEY_EV_ALARM_BAT_XHOT:
3268 printk(TPACPI_ALERT
3269 "THERMAL EMERGENCY: battery is extremely hot!\n");
3270 /* recommended action: immediate sleep/hibernate */
3271 return true;
3272 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3273 printk(TPACPI_CRIT
3274 "THERMAL ALARM: "
3275 "a sensor reports something is too hot!\n");
3276 /* recommended action: warn user through gui, that */
3277 /* some internal component is too hot */
3278 return true;
3279 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3280 printk(TPACPI_ALERT
3281 "THERMAL EMERGENCY: "
3282 "a sensor reports something is extremely hot!\n");
3283 /* recommended action: immediate sleep/hibernate */
3284 return true;
3285 case TP_HKEY_EV_THM_TABLE_CHANGED:
3286 printk(TPACPI_INFO
3287 "EC reports that Thermal Table has changed\n");
3288 /* recommended action: do nothing, we don't have
3289 * Lenovo ATM information */
3290 return true;
3291 default:
3292 printk(TPACPI_ALERT
3293 "THERMAL ALERT: unknown thermal alarm received\n");
3294 return false;
3298 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3300 u32 hkey;
3301 bool send_acpi_ev;
3302 bool ignore_acpi_ev;
3303 bool known_ev;
3305 if (event != 0x80) {
3306 printk(TPACPI_ERR
3307 "unknown HKEY notification event %d\n", event);
3308 /* forward it to userspace, maybe it knows how to handle it */
3309 acpi_bus_generate_netlink_event(
3310 ibm->acpi->device->pnp.device_class,
3311 dev_name(&ibm->acpi->device->dev),
3312 event, 0);
3313 return;
3316 while (1) {
3317 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3318 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3319 return;
3322 if (hkey == 0) {
3323 /* queue empty */
3324 return;
3327 send_acpi_ev = true;
3328 ignore_acpi_ev = false;
3330 switch (hkey >> 12) {
3331 case 1:
3332 /* 0x1000-0x1FFF: key presses */
3333 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3334 &ignore_acpi_ev);
3335 break;
3336 case 2:
3337 /* 0x2000-0x2FFF: Wakeup reason */
3338 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3339 &ignore_acpi_ev);
3340 break;
3341 case 3:
3342 /* 0x3000-0x3FFF: bay-related wakeups */
3343 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3344 hotkey_autosleep_ack = 1;
3345 printk(TPACPI_INFO
3346 "bay ejected\n");
3347 hotkey_wakeup_hotunplug_complete_notify_change();
3348 known_ev = true;
3349 } else {
3350 known_ev = false;
3352 break;
3353 case 4:
3354 /* 0x4000-0x4FFF: dock-related wakeups */
3355 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3356 hotkey_autosleep_ack = 1;
3357 printk(TPACPI_INFO
3358 "undocked\n");
3359 hotkey_wakeup_hotunplug_complete_notify_change();
3360 known_ev = true;
3361 } else {
3362 known_ev = false;
3364 break;
3365 case 5:
3366 /* 0x5000-0x5FFF: human interface helpers */
3367 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3368 &ignore_acpi_ev);
3369 break;
3370 case 6:
3371 /* 0x6000-0x6FFF: thermal alarms */
3372 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3373 &ignore_acpi_ev);
3374 break;
3375 case 7:
3376 /* 0x7000-0x7FFF: misc */
3377 if (tp_features.hotkey_wlsw &&
3378 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3379 tpacpi_send_radiosw_update();
3380 send_acpi_ev = 0;
3381 known_ev = true;
3382 break;
3384 /* fallthrough to default */
3385 default:
3386 known_ev = false;
3388 if (!known_ev) {
3389 printk(TPACPI_NOTICE
3390 "unhandled HKEY event 0x%04x\n", hkey);
3391 printk(TPACPI_NOTICE
3392 "please report the conditions when this "
3393 "event happened to %s\n", TPACPI_MAIL);
3396 /* Legacy events */
3397 if (!ignore_acpi_ev &&
3398 (send_acpi_ev || hotkey_report_mode < 2)) {
3399 acpi_bus_generate_proc_event(ibm->acpi->device,
3400 event, hkey);
3403 /* netlink events */
3404 if (!ignore_acpi_ev && send_acpi_ev) {
3405 acpi_bus_generate_netlink_event(
3406 ibm->acpi->device->pnp.device_class,
3407 dev_name(&ibm->acpi->device->dev),
3408 event, hkey);
3413 static void hotkey_suspend(pm_message_t state)
3415 /* Do these on suspend, we get the events on early resume! */
3416 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3417 hotkey_autosleep_ack = 0;
3420 static void hotkey_resume(void)
3422 tpacpi_disable_brightness_delay();
3424 if (hotkey_status_set(true) < 0 ||
3425 hotkey_mask_set(hotkey_acpi_mask) < 0)
3426 printk(TPACPI_ERR
3427 "error while attempting to reset the event "
3428 "firmware interface\n");
3430 tpacpi_send_radiosw_update();
3431 hotkey_tablet_mode_notify_change();
3432 hotkey_wakeup_reason_notify_change();
3433 hotkey_wakeup_hotunplug_complete_notify_change();
3434 hotkey_poll_setup_safe(false);
3437 /* procfs -------------------------------------------------------------- */
3438 static int hotkey_read(char *p)
3440 int res, status;
3441 int len = 0;
3443 if (!tp_features.hotkey) {
3444 len += sprintf(p + len, "status:\t\tnot supported\n");
3445 return len;
3448 if (mutex_lock_killable(&hotkey_mutex))
3449 return -ERESTARTSYS;
3450 res = hotkey_status_get(&status);
3451 if (!res)
3452 res = hotkey_mask_get();
3453 mutex_unlock(&hotkey_mutex);
3454 if (res)
3455 return res;
3457 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3458 if (hotkey_all_mask) {
3459 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3460 len += sprintf(p + len,
3461 "commands:\tenable, disable, reset, <mask>\n");
3462 } else {
3463 len += sprintf(p + len, "mask:\t\tnot supported\n");
3464 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3467 return len;
3470 static void hotkey_enabledisable_warn(bool enable)
3472 tpacpi_log_usertask("procfs hotkey enable/disable");
3473 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3474 TPACPI_WARN
3475 "hotkey enable/disable functionality has been "
3476 "removed from the driver. Hotkeys are always "
3477 "enabled\n"))
3478 printk(TPACPI_ERR
3479 "Please remove the hotkey=enable module "
3480 "parameter, it is deprecated. Hotkeys are always "
3481 "enabled\n");
3484 static int hotkey_write(char *buf)
3486 int res;
3487 u32 mask;
3488 char *cmd;
3490 if (!tp_features.hotkey)
3491 return -ENODEV;
3493 if (mutex_lock_killable(&hotkey_mutex))
3494 return -ERESTARTSYS;
3496 mask = hotkey_user_mask;
3498 res = 0;
3499 while ((cmd = next_cmd(&buf))) {
3500 if (strlencmp(cmd, "enable") == 0) {
3501 hotkey_enabledisable_warn(1);
3502 } else if (strlencmp(cmd, "disable") == 0) {
3503 hotkey_enabledisable_warn(0);
3504 res = -EPERM;
3505 } else if (strlencmp(cmd, "reset") == 0) {
3506 mask = (hotkey_all_mask | hotkey_source_mask)
3507 & ~hotkey_reserved_mask;
3508 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3509 /* mask set */
3510 } else if (sscanf(cmd, "%x", &mask) == 1) {
3511 /* mask set */
3512 } else {
3513 res = -EINVAL;
3514 goto errexit;
3518 if (!res) {
3519 tpacpi_disclose_usertask("procfs hotkey",
3520 "set mask to 0x%08x\n", mask);
3521 res = hotkey_user_mask_set(mask);
3524 errexit:
3525 mutex_unlock(&hotkey_mutex);
3526 return res;
3529 static const struct acpi_device_id ibm_htk_device_ids[] = {
3530 {TPACPI_ACPI_HKEY_HID, 0},
3531 {"", 0},
3534 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3535 .hid = ibm_htk_device_ids,
3536 .notify = hotkey_notify,
3537 .handle = &hkey_handle,
3538 .type = ACPI_DEVICE_NOTIFY,
3541 static struct ibm_struct hotkey_driver_data = {
3542 .name = "hotkey",
3543 .read = hotkey_read,
3544 .write = hotkey_write,
3545 .exit = hotkey_exit,
3546 .resume = hotkey_resume,
3547 .suspend = hotkey_suspend,
3548 .acpi = &ibm_hotkey_acpidriver,
3551 /*************************************************************************
3552 * Bluetooth subdriver
3555 enum {
3556 /* ACPI GBDC/SBDC bits */
3557 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3558 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3559 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3560 off / last state */
3563 enum {
3564 /* ACPI \BLTH commands */
3565 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3566 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3567 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3568 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3569 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3572 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3574 static struct rfkill *tpacpi_bluetooth_rfkill;
3576 static void bluetooth_suspend(pm_message_t state)
3578 /* Try to make sure radio will resume powered off */
3579 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3580 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3581 vdbg_printk(TPACPI_DBG_RFKILL,
3582 "bluetooth power down on resume request failed\n");
3585 static int bluetooth_get_radiosw(void)
3587 int status;
3589 if (!tp_features.bluetooth)
3590 return -ENODEV;
3592 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3593 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3594 return RFKILL_STATE_HARD_BLOCKED;
3596 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3597 if (dbg_bluetoothemul)
3598 return (tpacpi_bluetooth_emulstate) ?
3599 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3600 #endif
3602 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3603 return -EIO;
3605 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3606 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3609 static void bluetooth_update_rfk(void)
3611 int status;
3613 if (!tpacpi_bluetooth_rfkill)
3614 return;
3616 status = bluetooth_get_radiosw();
3617 if (status < 0)
3618 return;
3619 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3621 vdbg_printk(TPACPI_DBG_RFKILL,
3622 "forced rfkill state to %d\n",
3623 status);
3626 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3628 int status;
3630 if (!tp_features.bluetooth)
3631 return -ENODEV;
3633 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3634 * reason to risk weird behaviour. */
3635 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3636 && radio_on)
3637 return -EPERM;
3639 vdbg_printk(TPACPI_DBG_RFKILL,
3640 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3642 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3643 if (dbg_bluetoothemul) {
3644 tpacpi_bluetooth_emulstate = !!radio_on;
3645 if (update_rfk)
3646 bluetooth_update_rfk();
3647 return 0;
3649 #endif
3651 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3652 if (radio_on)
3653 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3654 else
3655 status = 0;
3656 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3657 return -EIO;
3659 if (update_rfk)
3660 bluetooth_update_rfk();
3662 return 0;
3665 /* sysfs bluetooth enable ---------------------------------------------- */
3666 static ssize_t bluetooth_enable_show(struct device *dev,
3667 struct device_attribute *attr,
3668 char *buf)
3670 int status;
3672 printk_deprecated_rfkill_attribute("bluetooth_enable");
3674 status = bluetooth_get_radiosw();
3675 if (status < 0)
3676 return status;
3678 return snprintf(buf, PAGE_SIZE, "%d\n",
3679 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3682 static ssize_t bluetooth_enable_store(struct device *dev,
3683 struct device_attribute *attr,
3684 const char *buf, size_t count)
3686 unsigned long t;
3687 int res;
3689 printk_deprecated_rfkill_attribute("bluetooth_enable");
3691 if (parse_strtoul(buf, 1, &t))
3692 return -EINVAL;
3694 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3696 res = bluetooth_set_radiosw(t, 1);
3698 return (res) ? res : count;
3701 static struct device_attribute dev_attr_bluetooth_enable =
3702 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3703 bluetooth_enable_show, bluetooth_enable_store);
3705 /* --------------------------------------------------------------------- */
3707 static struct attribute *bluetooth_attributes[] = {
3708 &dev_attr_bluetooth_enable.attr,
3709 NULL
3712 static const struct attribute_group bluetooth_attr_group = {
3713 .attrs = bluetooth_attributes,
3716 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3718 int bts = bluetooth_get_radiosw();
3720 if (bts < 0)
3721 return bts;
3723 *state = bts;
3724 return 0;
3727 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3729 dbg_printk(TPACPI_DBG_RFKILL,
3730 "request to change radio state to %d\n", state);
3731 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3734 static void bluetooth_shutdown(void)
3736 /* Order firmware to save current state to NVRAM */
3737 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3738 TP_ACPI_BLTH_SAVE_STATE))
3739 printk(TPACPI_NOTICE
3740 "failed to save bluetooth state to NVRAM\n");
3741 else
3742 vdbg_printk(TPACPI_DBG_RFKILL,
3743 "bluestooth state saved to NVRAM\n");
3746 static void bluetooth_exit(void)
3748 bluetooth_shutdown();
3750 if (tpacpi_bluetooth_rfkill)
3751 rfkill_unregister(tpacpi_bluetooth_rfkill);
3753 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3754 &bluetooth_attr_group);
3757 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3759 int res;
3760 int status = 0;
3762 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3763 "initializing bluetooth subdriver\n");
3765 TPACPI_ACPIHANDLE_INIT(hkey);
3767 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3768 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3769 tp_features.bluetooth = hkey_handle &&
3770 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3772 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3773 "bluetooth is %s, status 0x%02x\n",
3774 str_supported(tp_features.bluetooth),
3775 status);
3777 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3778 if (dbg_bluetoothemul) {
3779 tp_features.bluetooth = 1;
3780 printk(TPACPI_INFO
3781 "bluetooth switch emulation enabled\n");
3782 } else
3783 #endif
3784 if (tp_features.bluetooth &&
3785 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3786 /* no bluetooth hardware present in system */
3787 tp_features.bluetooth = 0;
3788 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3789 "bluetooth hardware not installed\n");
3792 if (!tp_features.bluetooth)
3793 return 1;
3795 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3796 &bluetooth_attr_group);
3797 if (res)
3798 return res;
3800 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3801 &tpacpi_bluetooth_rfkill,
3802 RFKILL_TYPE_BLUETOOTH,
3803 TPACPI_RFK_BLUETOOTH_SW_NAME,
3804 true,
3805 tpacpi_bluetooth_rfk_set,
3806 tpacpi_bluetooth_rfk_get);
3807 if (res) {
3808 bluetooth_exit();
3809 return res;
3812 return 0;
3815 /* procfs -------------------------------------------------------------- */
3816 static int bluetooth_read(char *p)
3818 int len = 0;
3819 int status = bluetooth_get_radiosw();
3821 if (!tp_features.bluetooth)
3822 len += sprintf(p + len, "status:\t\tnot supported\n");
3823 else {
3824 len += sprintf(p + len, "status:\t\t%s\n",
3825 (status == RFKILL_STATE_UNBLOCKED) ?
3826 "enabled" : "disabled");
3827 len += sprintf(p + len, "commands:\tenable, disable\n");
3830 return len;
3833 static int bluetooth_write(char *buf)
3835 char *cmd;
3836 int state = -1;
3838 if (!tp_features.bluetooth)
3839 return -ENODEV;
3841 while ((cmd = next_cmd(&buf))) {
3842 if (strlencmp(cmd, "enable") == 0) {
3843 state = 1;
3844 } else if (strlencmp(cmd, "disable") == 0) {
3845 state = 0;
3846 } else
3847 return -EINVAL;
3850 if (state != -1) {
3851 tpacpi_disclose_usertask("procfs bluetooth",
3852 "attempt to %s\n",
3853 state ? "enable" : "disable");
3854 bluetooth_set_radiosw(state, 1);
3857 return 0;
3860 static struct ibm_struct bluetooth_driver_data = {
3861 .name = "bluetooth",
3862 .read = bluetooth_read,
3863 .write = bluetooth_write,
3864 .exit = bluetooth_exit,
3865 .suspend = bluetooth_suspend,
3866 .shutdown = bluetooth_shutdown,
3869 /*************************************************************************
3870 * Wan subdriver
3873 enum {
3874 /* ACPI GWAN/SWAN bits */
3875 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3876 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3877 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3878 off / last state */
3881 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3883 static struct rfkill *tpacpi_wan_rfkill;
3885 static void wan_suspend(pm_message_t state)
3887 /* Try to make sure radio will resume powered off */
3888 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3889 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3890 vdbg_printk(TPACPI_DBG_RFKILL,
3891 "WWAN power down on resume request failed\n");
3894 static int wan_get_radiosw(void)
3896 int status;
3898 if (!tp_features.wan)
3899 return -ENODEV;
3901 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3902 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3903 return RFKILL_STATE_HARD_BLOCKED;
3905 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3906 if (dbg_wwanemul)
3907 return (tpacpi_wwan_emulstate) ?
3908 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3909 #endif
3911 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3912 return -EIO;
3914 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3915 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3918 static void wan_update_rfk(void)
3920 int status;
3922 if (!tpacpi_wan_rfkill)
3923 return;
3925 status = wan_get_radiosw();
3926 if (status < 0)
3927 return;
3928 rfkill_force_state(tpacpi_wan_rfkill, status);
3930 vdbg_printk(TPACPI_DBG_RFKILL,
3931 "forced rfkill state to %d\n",
3932 status);
3935 static int wan_set_radiosw(int radio_on, int update_rfk)
3937 int status;
3939 if (!tp_features.wan)
3940 return -ENODEV;
3942 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3943 * reason to risk weird behaviour. */
3944 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3945 && radio_on)
3946 return -EPERM;
3948 vdbg_printk(TPACPI_DBG_RFKILL,
3949 "will %s WWAN\n", radio_on ? "enable" : "disable");
3951 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3952 if (dbg_wwanemul) {
3953 tpacpi_wwan_emulstate = !!radio_on;
3954 if (update_rfk)
3955 wan_update_rfk();
3956 return 0;
3958 #endif
3960 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3961 if (radio_on)
3962 status = TP_ACPI_WANCARD_RADIOSSW;
3963 else
3964 status = 0;
3965 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3966 return -EIO;
3968 if (update_rfk)
3969 wan_update_rfk();
3971 return 0;
3974 /* sysfs wan enable ---------------------------------------------------- */
3975 static ssize_t wan_enable_show(struct device *dev,
3976 struct device_attribute *attr,
3977 char *buf)
3979 int status;
3981 printk_deprecated_rfkill_attribute("wwan_enable");
3983 status = wan_get_radiosw();
3984 if (status < 0)
3985 return status;
3987 return snprintf(buf, PAGE_SIZE, "%d\n",
3988 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3991 static ssize_t wan_enable_store(struct device *dev,
3992 struct device_attribute *attr,
3993 const char *buf, size_t count)
3995 unsigned long t;
3996 int res;
3998 printk_deprecated_rfkill_attribute("wwan_enable");
4000 if (parse_strtoul(buf, 1, &t))
4001 return -EINVAL;
4003 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4005 res = wan_set_radiosw(t, 1);
4007 return (res) ? res : count;
4010 static struct device_attribute dev_attr_wan_enable =
4011 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4012 wan_enable_show, wan_enable_store);
4014 /* --------------------------------------------------------------------- */
4016 static struct attribute *wan_attributes[] = {
4017 &dev_attr_wan_enable.attr,
4018 NULL
4021 static const struct attribute_group wan_attr_group = {
4022 .attrs = wan_attributes,
4025 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4027 int wans = wan_get_radiosw();
4029 if (wans < 0)
4030 return wans;
4032 *state = wans;
4033 return 0;
4036 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4038 dbg_printk(TPACPI_DBG_RFKILL,
4039 "request to change radio state to %d\n", state);
4040 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4043 static void wan_shutdown(void)
4045 /* Order firmware to save current state to NVRAM */
4046 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4047 TP_ACPI_WGSV_SAVE_STATE))
4048 printk(TPACPI_NOTICE
4049 "failed to save WWAN state to NVRAM\n");
4050 else
4051 vdbg_printk(TPACPI_DBG_RFKILL,
4052 "WWAN state saved to NVRAM\n");
4055 static void wan_exit(void)
4057 wan_shutdown();
4059 if (tpacpi_wan_rfkill)
4060 rfkill_unregister(tpacpi_wan_rfkill);
4062 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4063 &wan_attr_group);
4066 static int __init wan_init(struct ibm_init_struct *iibm)
4068 int res;
4069 int status = 0;
4071 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4072 "initializing wan subdriver\n");
4074 TPACPI_ACPIHANDLE_INIT(hkey);
4076 tp_features.wan = hkey_handle &&
4077 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4079 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4080 "wan is %s, status 0x%02x\n",
4081 str_supported(tp_features.wan),
4082 status);
4084 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4085 if (dbg_wwanemul) {
4086 tp_features.wan = 1;
4087 printk(TPACPI_INFO
4088 "wwan switch emulation enabled\n");
4089 } else
4090 #endif
4091 if (tp_features.wan &&
4092 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4093 /* no wan hardware present in system */
4094 tp_features.wan = 0;
4095 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4096 "wan hardware not installed\n");
4099 if (!tp_features.wan)
4100 return 1;
4102 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4103 &wan_attr_group);
4104 if (res)
4105 return res;
4107 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4108 &tpacpi_wan_rfkill,
4109 RFKILL_TYPE_WWAN,
4110 TPACPI_RFK_WWAN_SW_NAME,
4111 true,
4112 tpacpi_wan_rfk_set,
4113 tpacpi_wan_rfk_get);
4114 if (res) {
4115 wan_exit();
4116 return res;
4119 return 0;
4122 /* procfs -------------------------------------------------------------- */
4123 static int wan_read(char *p)
4125 int len = 0;
4126 int status = wan_get_radiosw();
4128 tpacpi_disclose_usertask("procfs wan", "read");
4130 if (!tp_features.wan)
4131 len += sprintf(p + len, "status:\t\tnot supported\n");
4132 else {
4133 len += sprintf(p + len, "status:\t\t%s\n",
4134 (status == RFKILL_STATE_UNBLOCKED) ?
4135 "enabled" : "disabled");
4136 len += sprintf(p + len, "commands:\tenable, disable\n");
4139 return len;
4142 static int wan_write(char *buf)
4144 char *cmd;
4145 int state = -1;
4147 if (!tp_features.wan)
4148 return -ENODEV;
4150 while ((cmd = next_cmd(&buf))) {
4151 if (strlencmp(cmd, "enable") == 0) {
4152 state = 1;
4153 } else if (strlencmp(cmd, "disable") == 0) {
4154 state = 0;
4155 } else
4156 return -EINVAL;
4159 if (state != -1) {
4160 tpacpi_disclose_usertask("procfs wan",
4161 "attempt to %s\n",
4162 state ? "enable" : "disable");
4163 wan_set_radiosw(state, 1);
4166 return 0;
4169 static struct ibm_struct wan_driver_data = {
4170 .name = "wan",
4171 .read = wan_read,
4172 .write = wan_write,
4173 .exit = wan_exit,
4174 .suspend = wan_suspend,
4175 .shutdown = wan_shutdown,
4178 /*************************************************************************
4179 * UWB subdriver
4182 enum {
4183 /* ACPI GUWB/SUWB bits */
4184 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4185 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4188 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4190 static struct rfkill *tpacpi_uwb_rfkill;
4192 static int uwb_get_radiosw(void)
4194 int status;
4196 if (!tp_features.uwb)
4197 return -ENODEV;
4199 /* WLSW overrides UWB in firmware/hardware, reflect that */
4200 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4201 return RFKILL_STATE_HARD_BLOCKED;
4203 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4204 if (dbg_uwbemul)
4205 return (tpacpi_uwb_emulstate) ?
4206 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4207 #endif
4209 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4210 return -EIO;
4212 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4213 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4216 static void uwb_update_rfk(void)
4218 int status;
4220 if (!tpacpi_uwb_rfkill)
4221 return;
4223 status = uwb_get_radiosw();
4224 if (status < 0)
4225 return;
4226 rfkill_force_state(tpacpi_uwb_rfkill, status);
4228 vdbg_printk(TPACPI_DBG_RFKILL,
4229 "forced rfkill state to %d\n",
4230 status);
4233 static int uwb_set_radiosw(int radio_on, int update_rfk)
4235 int status;
4237 if (!tp_features.uwb)
4238 return -ENODEV;
4240 /* WLSW overrides UWB in firmware/hardware, but there is no
4241 * reason to risk weird behaviour. */
4242 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4243 && radio_on)
4244 return -EPERM;
4246 vdbg_printk(TPACPI_DBG_RFKILL,
4247 "will %s UWB\n", radio_on ? "enable" : "disable");
4249 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4250 if (dbg_uwbemul) {
4251 tpacpi_uwb_emulstate = !!radio_on;
4252 if (update_rfk)
4253 uwb_update_rfk();
4254 return 0;
4256 #endif
4258 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4259 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4260 return -EIO;
4262 if (update_rfk)
4263 uwb_update_rfk();
4265 return 0;
4268 /* --------------------------------------------------------------------- */
4270 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4272 int uwbs = uwb_get_radiosw();
4274 if (uwbs < 0)
4275 return uwbs;
4277 *state = uwbs;
4278 return 0;
4281 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4283 dbg_printk(TPACPI_DBG_RFKILL,
4284 "request to change radio state to %d\n", state);
4285 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4288 static void uwb_exit(void)
4290 if (tpacpi_uwb_rfkill)
4291 rfkill_unregister(tpacpi_uwb_rfkill);
4294 static int __init uwb_init(struct ibm_init_struct *iibm)
4296 int res;
4297 int status = 0;
4299 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4300 "initializing uwb subdriver\n");
4302 TPACPI_ACPIHANDLE_INIT(hkey);
4304 tp_features.uwb = hkey_handle &&
4305 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4307 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4308 "uwb is %s, status 0x%02x\n",
4309 str_supported(tp_features.uwb),
4310 status);
4312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4313 if (dbg_uwbemul) {
4314 tp_features.uwb = 1;
4315 printk(TPACPI_INFO
4316 "uwb switch emulation enabled\n");
4317 } else
4318 #endif
4319 if (tp_features.uwb &&
4320 !(status & TP_ACPI_UWB_HWPRESENT)) {
4321 /* no uwb hardware present in system */
4322 tp_features.uwb = 0;
4323 dbg_printk(TPACPI_DBG_INIT,
4324 "uwb hardware not installed\n");
4327 if (!tp_features.uwb)
4328 return 1;
4330 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4331 &tpacpi_uwb_rfkill,
4332 RFKILL_TYPE_UWB,
4333 TPACPI_RFK_UWB_SW_NAME,
4334 false,
4335 tpacpi_uwb_rfk_set,
4336 tpacpi_uwb_rfk_get);
4338 return res;
4341 static struct ibm_struct uwb_driver_data = {
4342 .name = "uwb",
4343 .exit = uwb_exit,
4344 .flags.experimental = 1,
4347 /*************************************************************************
4348 * Video subdriver
4351 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4353 enum video_access_mode {
4354 TPACPI_VIDEO_NONE = 0,
4355 TPACPI_VIDEO_570, /* 570 */
4356 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4357 TPACPI_VIDEO_NEW, /* all others */
4360 enum { /* video status flags, based on VIDEO_570 */
4361 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4362 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4363 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4366 enum { /* TPACPI_VIDEO_570 constants */
4367 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4368 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4369 * video_status_flags */
4370 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4371 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4374 static enum video_access_mode video_supported;
4375 static int video_orig_autosw;
4377 static int video_autosw_get(void);
4378 static int video_autosw_set(int enable);
4380 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4382 static int __init video_init(struct ibm_init_struct *iibm)
4384 int ivga;
4386 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4388 TPACPI_ACPIHANDLE_INIT(vid);
4389 TPACPI_ACPIHANDLE_INIT(vid2);
4391 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4392 /* G41, assume IVGA doesn't change */
4393 vid_handle = vid2_handle;
4395 if (!vid_handle)
4396 /* video switching not supported on R30, R31 */
4397 video_supported = TPACPI_VIDEO_NONE;
4398 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4399 /* 570 */
4400 video_supported = TPACPI_VIDEO_570;
4401 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4402 /* 600e/x, 770e, 770x */
4403 video_supported = TPACPI_VIDEO_770;
4404 else
4405 /* all others */
4406 video_supported = TPACPI_VIDEO_NEW;
4408 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4409 str_supported(video_supported != TPACPI_VIDEO_NONE),
4410 video_supported);
4412 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4415 static void video_exit(void)
4417 dbg_printk(TPACPI_DBG_EXIT,
4418 "restoring original video autoswitch mode\n");
4419 if (video_autosw_set(video_orig_autosw))
4420 printk(TPACPI_ERR "error while trying to restore original "
4421 "video autoswitch mode\n");
4424 static int video_outputsw_get(void)
4426 int status = 0;
4427 int i;
4429 switch (video_supported) {
4430 case TPACPI_VIDEO_570:
4431 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4432 TP_ACPI_VIDEO_570_PHSCMD))
4433 return -EIO;
4434 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4435 break;
4436 case TPACPI_VIDEO_770:
4437 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4438 return -EIO;
4439 if (i)
4440 status |= TP_ACPI_VIDEO_S_LCD;
4441 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4442 return -EIO;
4443 if (i)
4444 status |= TP_ACPI_VIDEO_S_CRT;
4445 break;
4446 case TPACPI_VIDEO_NEW:
4447 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4448 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4449 return -EIO;
4450 if (i)
4451 status |= TP_ACPI_VIDEO_S_CRT;
4453 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4454 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4455 return -EIO;
4456 if (i)
4457 status |= TP_ACPI_VIDEO_S_LCD;
4458 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4459 return -EIO;
4460 if (i)
4461 status |= TP_ACPI_VIDEO_S_DVI;
4462 break;
4463 default:
4464 return -ENOSYS;
4467 return status;
4470 static int video_outputsw_set(int status)
4472 int autosw;
4473 int res = 0;
4475 switch (video_supported) {
4476 case TPACPI_VIDEO_570:
4477 res = acpi_evalf(NULL, NULL,
4478 "\\_SB.PHS2", "vdd",
4479 TP_ACPI_VIDEO_570_PHS2CMD,
4480 status | TP_ACPI_VIDEO_570_PHS2SET);
4481 break;
4482 case TPACPI_VIDEO_770:
4483 autosw = video_autosw_get();
4484 if (autosw < 0)
4485 return autosw;
4487 res = video_autosw_set(1);
4488 if (res)
4489 return res;
4490 res = acpi_evalf(vid_handle, NULL,
4491 "ASWT", "vdd", status * 0x100, 0);
4492 if (!autosw && video_autosw_set(autosw)) {
4493 printk(TPACPI_ERR
4494 "video auto-switch left enabled due to error\n");
4495 return -EIO;
4497 break;
4498 case TPACPI_VIDEO_NEW:
4499 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4500 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4501 break;
4502 default:
4503 return -ENOSYS;
4506 return (res)? 0 : -EIO;
4509 static int video_autosw_get(void)
4511 int autosw = 0;
4513 switch (video_supported) {
4514 case TPACPI_VIDEO_570:
4515 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4516 return -EIO;
4517 break;
4518 case TPACPI_VIDEO_770:
4519 case TPACPI_VIDEO_NEW:
4520 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4521 return -EIO;
4522 break;
4523 default:
4524 return -ENOSYS;
4527 return autosw & 1;
4530 static int video_autosw_set(int enable)
4532 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4533 return -EIO;
4534 return 0;
4537 static int video_outputsw_cycle(void)
4539 int autosw = video_autosw_get();
4540 int res;
4542 if (autosw < 0)
4543 return autosw;
4545 switch (video_supported) {
4546 case TPACPI_VIDEO_570:
4547 res = video_autosw_set(1);
4548 if (res)
4549 return res;
4550 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4551 break;
4552 case TPACPI_VIDEO_770:
4553 case TPACPI_VIDEO_NEW:
4554 res = video_autosw_set(1);
4555 if (res)
4556 return res;
4557 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4558 break;
4559 default:
4560 return -ENOSYS;
4562 if (!autosw && video_autosw_set(autosw)) {
4563 printk(TPACPI_ERR
4564 "video auto-switch left enabled due to error\n");
4565 return -EIO;
4568 return (res)? 0 : -EIO;
4571 static int video_expand_toggle(void)
4573 switch (video_supported) {
4574 case TPACPI_VIDEO_570:
4575 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4576 0 : -EIO;
4577 case TPACPI_VIDEO_770:
4578 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4579 0 : -EIO;
4580 case TPACPI_VIDEO_NEW:
4581 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4582 0 : -EIO;
4583 default:
4584 return -ENOSYS;
4586 /* not reached */
4589 static int video_read(char *p)
4591 int status, autosw;
4592 int len = 0;
4594 if (video_supported == TPACPI_VIDEO_NONE) {
4595 len += sprintf(p + len, "status:\t\tnot supported\n");
4596 return len;
4599 status = video_outputsw_get();
4600 if (status < 0)
4601 return status;
4603 autosw = video_autosw_get();
4604 if (autosw < 0)
4605 return autosw;
4607 len += sprintf(p + len, "status:\t\tsupported\n");
4608 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4609 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4610 if (video_supported == TPACPI_VIDEO_NEW)
4611 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4612 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4613 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4614 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4615 if (video_supported == TPACPI_VIDEO_NEW)
4616 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4617 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4618 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4620 return len;
4623 static int video_write(char *buf)
4625 char *cmd;
4626 int enable, disable, status;
4627 int res;
4629 if (video_supported == TPACPI_VIDEO_NONE)
4630 return -ENODEV;
4632 enable = 0;
4633 disable = 0;
4635 while ((cmd = next_cmd(&buf))) {
4636 if (strlencmp(cmd, "lcd_enable") == 0) {
4637 enable |= TP_ACPI_VIDEO_S_LCD;
4638 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4639 disable |= TP_ACPI_VIDEO_S_LCD;
4640 } else if (strlencmp(cmd, "crt_enable") == 0) {
4641 enable |= TP_ACPI_VIDEO_S_CRT;
4642 } else if (strlencmp(cmd, "crt_disable") == 0) {
4643 disable |= TP_ACPI_VIDEO_S_CRT;
4644 } else if (video_supported == TPACPI_VIDEO_NEW &&
4645 strlencmp(cmd, "dvi_enable") == 0) {
4646 enable |= TP_ACPI_VIDEO_S_DVI;
4647 } else if (video_supported == TPACPI_VIDEO_NEW &&
4648 strlencmp(cmd, "dvi_disable") == 0) {
4649 disable |= TP_ACPI_VIDEO_S_DVI;
4650 } else if (strlencmp(cmd, "auto_enable") == 0) {
4651 res = video_autosw_set(1);
4652 if (res)
4653 return res;
4654 } else if (strlencmp(cmd, "auto_disable") == 0) {
4655 res = video_autosw_set(0);
4656 if (res)
4657 return res;
4658 } else if (strlencmp(cmd, "video_switch") == 0) {
4659 res = video_outputsw_cycle();
4660 if (res)
4661 return res;
4662 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4663 res = video_expand_toggle();
4664 if (res)
4665 return res;
4666 } else
4667 return -EINVAL;
4670 if (enable || disable) {
4671 status = video_outputsw_get();
4672 if (status < 0)
4673 return status;
4674 res = video_outputsw_set((status & ~disable) | enable);
4675 if (res)
4676 return res;
4679 return 0;
4682 static struct ibm_struct video_driver_data = {
4683 .name = "video",
4684 .read = video_read,
4685 .write = video_write,
4686 .exit = video_exit,
4689 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4691 /*************************************************************************
4692 * Light (thinklight) subdriver
4695 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4696 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4698 static int light_get_status(void)
4700 int status = 0;
4702 if (tp_features.light_status) {
4703 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4704 return -EIO;
4705 return (!!status);
4708 return -ENXIO;
4711 static int light_set_status(int status)
4713 int rc;
4715 if (tp_features.light) {
4716 if (cmos_handle) {
4717 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4718 (status)?
4719 TP_CMOS_THINKLIGHT_ON :
4720 TP_CMOS_THINKLIGHT_OFF);
4721 } else {
4722 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4723 (status)? 1 : 0);
4725 return (rc)? 0 : -EIO;
4728 return -ENXIO;
4731 static void light_set_status_worker(struct work_struct *work)
4733 struct tpacpi_led_classdev *data =
4734 container_of(work, struct tpacpi_led_classdev, work);
4736 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4737 light_set_status((data->new_state != TPACPI_LED_OFF));
4740 static void light_sysfs_set(struct led_classdev *led_cdev,
4741 enum led_brightness brightness)
4743 struct tpacpi_led_classdev *data =
4744 container_of(led_cdev,
4745 struct tpacpi_led_classdev,
4746 led_classdev);
4747 data->new_state = (brightness != LED_OFF) ?
4748 TPACPI_LED_ON : TPACPI_LED_OFF;
4749 queue_work(tpacpi_wq, &data->work);
4752 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4754 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4757 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4758 .led_classdev = {
4759 .name = "tpacpi::thinklight",
4760 .brightness_set = &light_sysfs_set,
4761 .brightness_get = &light_sysfs_get,
4765 static int __init light_init(struct ibm_init_struct *iibm)
4767 int rc;
4769 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4771 TPACPI_ACPIHANDLE_INIT(ledb);
4772 TPACPI_ACPIHANDLE_INIT(lght);
4773 TPACPI_ACPIHANDLE_INIT(cmos);
4774 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4776 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4777 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4779 if (tp_features.light)
4780 /* light status not supported on
4781 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4782 tp_features.light_status =
4783 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4785 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4786 str_supported(tp_features.light),
4787 str_supported(tp_features.light_status));
4789 if (!tp_features.light)
4790 return 1;
4792 rc = led_classdev_register(&tpacpi_pdev->dev,
4793 &tpacpi_led_thinklight.led_classdev);
4795 if (rc < 0) {
4796 tp_features.light = 0;
4797 tp_features.light_status = 0;
4798 } else {
4799 rc = 0;
4802 return rc;
4805 static void light_exit(void)
4807 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4808 if (work_pending(&tpacpi_led_thinklight.work))
4809 flush_workqueue(tpacpi_wq);
4812 static int light_read(char *p)
4814 int len = 0;
4815 int status;
4817 if (!tp_features.light) {
4818 len += sprintf(p + len, "status:\t\tnot supported\n");
4819 } else if (!tp_features.light_status) {
4820 len += sprintf(p + len, "status:\t\tunknown\n");
4821 len += sprintf(p + len, "commands:\ton, off\n");
4822 } else {
4823 status = light_get_status();
4824 if (status < 0)
4825 return status;
4826 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4827 len += sprintf(p + len, "commands:\ton, off\n");
4830 return len;
4833 static int light_write(char *buf)
4835 char *cmd;
4836 int newstatus = 0;
4838 if (!tp_features.light)
4839 return -ENODEV;
4841 while ((cmd = next_cmd(&buf))) {
4842 if (strlencmp(cmd, "on") == 0) {
4843 newstatus = 1;
4844 } else if (strlencmp(cmd, "off") == 0) {
4845 newstatus = 0;
4846 } else
4847 return -EINVAL;
4850 return light_set_status(newstatus);
4853 static struct ibm_struct light_driver_data = {
4854 .name = "light",
4855 .read = light_read,
4856 .write = light_write,
4857 .exit = light_exit,
4860 /*************************************************************************
4861 * CMOS subdriver
4864 /* sysfs cmos_command -------------------------------------------------- */
4865 static ssize_t cmos_command_store(struct device *dev,
4866 struct device_attribute *attr,
4867 const char *buf, size_t count)
4869 unsigned long cmos_cmd;
4870 int res;
4872 if (parse_strtoul(buf, 21, &cmos_cmd))
4873 return -EINVAL;
4875 res = issue_thinkpad_cmos_command(cmos_cmd);
4876 return (res)? res : count;
4879 static struct device_attribute dev_attr_cmos_command =
4880 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4882 /* --------------------------------------------------------------------- */
4884 static int __init cmos_init(struct ibm_init_struct *iibm)
4886 int res;
4888 vdbg_printk(TPACPI_DBG_INIT,
4889 "initializing cmos commands subdriver\n");
4891 TPACPI_ACPIHANDLE_INIT(cmos);
4893 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4894 str_supported(cmos_handle != NULL));
4896 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4897 if (res)
4898 return res;
4900 return (cmos_handle)? 0 : 1;
4903 static void cmos_exit(void)
4905 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4908 static int cmos_read(char *p)
4910 int len = 0;
4912 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4913 R30, R31, T20-22, X20-21 */
4914 if (!cmos_handle)
4915 len += sprintf(p + len, "status:\t\tnot supported\n");
4916 else {
4917 len += sprintf(p + len, "status:\t\tsupported\n");
4918 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4921 return len;
4924 static int cmos_write(char *buf)
4926 char *cmd;
4927 int cmos_cmd, res;
4929 while ((cmd = next_cmd(&buf))) {
4930 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4931 cmos_cmd >= 0 && cmos_cmd <= 21) {
4932 /* cmos_cmd set */
4933 } else
4934 return -EINVAL;
4936 res = issue_thinkpad_cmos_command(cmos_cmd);
4937 if (res)
4938 return res;
4941 return 0;
4944 static struct ibm_struct cmos_driver_data = {
4945 .name = "cmos",
4946 .read = cmos_read,
4947 .write = cmos_write,
4948 .exit = cmos_exit,
4951 /*************************************************************************
4952 * LED subdriver
4955 enum led_access_mode {
4956 TPACPI_LED_NONE = 0,
4957 TPACPI_LED_570, /* 570 */
4958 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4959 TPACPI_LED_NEW, /* all others */
4962 enum { /* For TPACPI_LED_OLD */
4963 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4964 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4965 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4968 static enum led_access_mode led_supported;
4970 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4971 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4972 /* T20-22, X20-21 */
4973 "LED", /* all others */
4974 ); /* R30, R31 */
4976 #define TPACPI_LED_NUMLEDS 16
4977 static struct tpacpi_led_classdev *tpacpi_leds;
4978 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4979 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4980 /* there's a limit of 19 chars + NULL before 2.6.26 */
4981 "tpacpi::power",
4982 "tpacpi:orange:batt",
4983 "tpacpi:green:batt",
4984 "tpacpi::dock_active",
4985 "tpacpi::bay_active",
4986 "tpacpi::dock_batt",
4987 "tpacpi::unknown_led",
4988 "tpacpi::standby",
4989 "tpacpi::dock_status1",
4990 "tpacpi::dock_status2",
4991 "tpacpi::unknown_led2",
4992 "tpacpi::unknown_led3",
4993 "tpacpi::thinkvantage",
4995 #define TPACPI_SAFE_LEDS 0x1081U
4997 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4999 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5000 return false;
5001 #else
5002 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5003 #endif
5006 static int led_get_status(const unsigned int led)
5008 int status;
5009 enum led_status_t led_s;
5011 switch (led_supported) {
5012 case TPACPI_LED_570:
5013 if (!acpi_evalf(ec_handle,
5014 &status, "GLED", "dd", 1 << led))
5015 return -EIO;
5016 led_s = (status == 0)?
5017 TPACPI_LED_OFF :
5018 ((status == 1)?
5019 TPACPI_LED_ON :
5020 TPACPI_LED_BLINK);
5021 tpacpi_led_state_cache[led] = led_s;
5022 return led_s;
5023 default:
5024 return -ENXIO;
5027 /* not reached */
5030 static int led_set_status(const unsigned int led,
5031 const enum led_status_t ledstatus)
5033 /* off, on, blink. Index is led_status_t */
5034 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5035 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5037 int rc = 0;
5039 switch (led_supported) {
5040 case TPACPI_LED_570:
5041 /* 570 */
5042 if (unlikely(led > 7))
5043 return -EINVAL;
5044 if (unlikely(tpacpi_is_led_restricted(led)))
5045 return -EPERM;
5046 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5047 (1 << led), led_sled_arg1[ledstatus]))
5048 rc = -EIO;
5049 break;
5050 case TPACPI_LED_OLD:
5051 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5052 if (unlikely(led > 7))
5053 return -EINVAL;
5054 if (unlikely(tpacpi_is_led_restricted(led)))
5055 return -EPERM;
5056 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5057 if (rc >= 0)
5058 rc = ec_write(TPACPI_LED_EC_HLBL,
5059 (ledstatus == TPACPI_LED_BLINK) << led);
5060 if (rc >= 0)
5061 rc = ec_write(TPACPI_LED_EC_HLCL,
5062 (ledstatus != TPACPI_LED_OFF) << led);
5063 break;
5064 case TPACPI_LED_NEW:
5065 /* all others */
5066 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5067 return -EINVAL;
5068 if (unlikely(tpacpi_is_led_restricted(led)))
5069 return -EPERM;
5070 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5071 led, led_led_arg1[ledstatus]))
5072 rc = -EIO;
5073 break;
5074 default:
5075 rc = -ENXIO;
5078 if (!rc)
5079 tpacpi_led_state_cache[led] = ledstatus;
5081 return rc;
5084 static void led_set_status_worker(struct work_struct *work)
5086 struct tpacpi_led_classdev *data =
5087 container_of(work, struct tpacpi_led_classdev, work);
5089 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5090 led_set_status(data->led, data->new_state);
5093 static void led_sysfs_set(struct led_classdev *led_cdev,
5094 enum led_brightness brightness)
5096 struct tpacpi_led_classdev *data = container_of(led_cdev,
5097 struct tpacpi_led_classdev, led_classdev);
5099 if (brightness == LED_OFF)
5100 data->new_state = TPACPI_LED_OFF;
5101 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5102 data->new_state = TPACPI_LED_ON;
5103 else
5104 data->new_state = TPACPI_LED_BLINK;
5106 queue_work(tpacpi_wq, &data->work);
5109 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5110 unsigned long *delay_on, unsigned long *delay_off)
5112 struct tpacpi_led_classdev *data = container_of(led_cdev,
5113 struct tpacpi_led_classdev, led_classdev);
5115 /* Can we choose the flash rate? */
5116 if (*delay_on == 0 && *delay_off == 0) {
5117 /* yes. set them to the hardware blink rate (1 Hz) */
5118 *delay_on = 500; /* ms */
5119 *delay_off = 500; /* ms */
5120 } else if ((*delay_on != 500) || (*delay_off != 500))
5121 return -EINVAL;
5123 data->new_state = TPACPI_LED_BLINK;
5124 queue_work(tpacpi_wq, &data->work);
5126 return 0;
5129 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5131 int rc;
5133 struct tpacpi_led_classdev *data = container_of(led_cdev,
5134 struct tpacpi_led_classdev, led_classdev);
5136 rc = led_get_status(data->led);
5138 if (rc == TPACPI_LED_OFF || rc < 0)
5139 rc = LED_OFF; /* no error handling in led class :( */
5140 else
5141 rc = LED_FULL;
5143 return rc;
5146 static void led_exit(void)
5148 unsigned int i;
5150 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5151 if (tpacpi_leds[i].led_classdev.name)
5152 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5155 kfree(tpacpi_leds);
5158 static int __init tpacpi_init_led(unsigned int led)
5160 int rc;
5162 tpacpi_leds[led].led = led;
5164 /* LEDs with no name don't get registered */
5165 if (!tpacpi_led_names[led])
5166 return 0;
5168 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5169 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5170 if (led_supported == TPACPI_LED_570)
5171 tpacpi_leds[led].led_classdev.brightness_get =
5172 &led_sysfs_get;
5174 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5176 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5178 rc = led_classdev_register(&tpacpi_pdev->dev,
5179 &tpacpi_leds[led].led_classdev);
5180 if (rc < 0)
5181 tpacpi_leds[led].led_classdev.name = NULL;
5183 return rc;
5186 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5187 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5188 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5189 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5191 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5192 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5193 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5194 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5195 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5196 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5197 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5198 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5200 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5201 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5202 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5203 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5204 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5206 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5207 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5208 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5209 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5211 /* (1) - may have excess leds enabled on MSB */
5213 /* Defaults (order matters, keep last, don't reorder!) */
5214 { /* Lenovo */
5215 .vendor = PCI_VENDOR_ID_LENOVO,
5216 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5217 .quirks = 0x1fffU,
5219 { /* IBM ThinkPads with no EC version string */
5220 .vendor = PCI_VENDOR_ID_IBM,
5221 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5222 .quirks = 0x00ffU,
5224 { /* IBM ThinkPads with EC version string */
5225 .vendor = PCI_VENDOR_ID_IBM,
5226 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5227 .quirks = 0x00bfU,
5231 #undef TPACPI_LEDQ_IBM
5232 #undef TPACPI_LEDQ_LNV
5234 static int __init led_init(struct ibm_init_struct *iibm)
5236 unsigned int i;
5237 int rc;
5238 unsigned long useful_leds;
5240 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5242 TPACPI_ACPIHANDLE_INIT(led);
5244 if (!led_handle)
5245 /* led not supported on R30, R31 */
5246 led_supported = TPACPI_LED_NONE;
5247 else if (strlencmp(led_path, "SLED") == 0)
5248 /* 570 */
5249 led_supported = TPACPI_LED_570;
5250 else if (strlencmp(led_path, "SYSL") == 0)
5251 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5252 led_supported = TPACPI_LED_OLD;
5253 else
5254 /* all others */
5255 led_supported = TPACPI_LED_NEW;
5257 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5258 str_supported(led_supported), led_supported);
5260 if (led_supported == TPACPI_LED_NONE)
5261 return 1;
5263 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5264 GFP_KERNEL);
5265 if (!tpacpi_leds) {
5266 printk(TPACPI_ERR "Out of memory for LED data\n");
5267 return -ENOMEM;
5270 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5271 ARRAY_SIZE(led_useful_qtable));
5273 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5274 if (!tpacpi_is_led_restricted(i) &&
5275 test_bit(i, &useful_leds)) {
5276 rc = tpacpi_init_led(i);
5277 if (rc < 0) {
5278 led_exit();
5279 return rc;
5284 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5285 printk(TPACPI_NOTICE
5286 "warning: userspace override of important "
5287 "firmware LEDs is enabled\n");
5288 #endif
5289 return 0;
5292 #define str_led_status(s) \
5293 ((s) == TPACPI_LED_OFF ? "off" : \
5294 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5296 static int led_read(char *p)
5298 int len = 0;
5300 if (!led_supported) {
5301 len += sprintf(p + len, "status:\t\tnot supported\n");
5302 return len;
5304 len += sprintf(p + len, "status:\t\tsupported\n");
5306 if (led_supported == TPACPI_LED_570) {
5307 /* 570 */
5308 int i, status;
5309 for (i = 0; i < 8; i++) {
5310 status = led_get_status(i);
5311 if (status < 0)
5312 return -EIO;
5313 len += sprintf(p + len, "%d:\t\t%s\n",
5314 i, str_led_status(status));
5318 len += sprintf(p + len, "commands:\t"
5319 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5321 return len;
5324 static int led_write(char *buf)
5326 char *cmd;
5327 int led, rc;
5328 enum led_status_t s;
5330 if (!led_supported)
5331 return -ENODEV;
5333 while ((cmd = next_cmd(&buf))) {
5334 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5335 return -EINVAL;
5337 if (strstr(cmd, "off")) {
5338 s = TPACPI_LED_OFF;
5339 } else if (strstr(cmd, "on")) {
5340 s = TPACPI_LED_ON;
5341 } else if (strstr(cmd, "blink")) {
5342 s = TPACPI_LED_BLINK;
5343 } else {
5344 return -EINVAL;
5347 rc = led_set_status(led, s);
5348 if (rc < 0)
5349 return rc;
5352 return 0;
5355 static struct ibm_struct led_driver_data = {
5356 .name = "led",
5357 .read = led_read,
5358 .write = led_write,
5359 .exit = led_exit,
5362 /*************************************************************************
5363 * Beep subdriver
5366 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5368 #define TPACPI_BEEP_Q1 0x0001
5370 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5371 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5372 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5375 static int __init beep_init(struct ibm_init_struct *iibm)
5377 unsigned long quirks;
5379 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5381 TPACPI_ACPIHANDLE_INIT(beep);
5383 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5384 str_supported(beep_handle != NULL));
5386 quirks = tpacpi_check_quirks(beep_quirk_table,
5387 ARRAY_SIZE(beep_quirk_table));
5389 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5391 return (beep_handle)? 0 : 1;
5394 static int beep_read(char *p)
5396 int len = 0;
5398 if (!beep_handle)
5399 len += sprintf(p + len, "status:\t\tnot supported\n");
5400 else {
5401 len += sprintf(p + len, "status:\t\tsupported\n");
5402 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5405 return len;
5408 static int beep_write(char *buf)
5410 char *cmd;
5411 int beep_cmd;
5413 if (!beep_handle)
5414 return -ENODEV;
5416 while ((cmd = next_cmd(&buf))) {
5417 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5418 beep_cmd >= 0 && beep_cmd <= 17) {
5419 /* beep_cmd set */
5420 } else
5421 return -EINVAL;
5422 if (tp_features.beep_needs_two_args) {
5423 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5424 beep_cmd, 0))
5425 return -EIO;
5426 } else {
5427 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5428 beep_cmd))
5429 return -EIO;
5433 return 0;
5436 static struct ibm_struct beep_driver_data = {
5437 .name = "beep",
5438 .read = beep_read,
5439 .write = beep_write,
5442 /*************************************************************************
5443 * Thermal subdriver
5446 enum thermal_access_mode {
5447 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5448 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5449 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5450 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5451 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5454 enum { /* TPACPI_THERMAL_TPEC_* */
5455 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5456 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5457 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5460 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5461 struct ibm_thermal_sensors_struct {
5462 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5465 static enum thermal_access_mode thermal_read_mode;
5467 /* idx is zero-based */
5468 static int thermal_get_sensor(int idx, s32 *value)
5470 int t;
5471 s8 tmp;
5472 char tmpi[5];
5474 t = TP_EC_THERMAL_TMP0;
5476 switch (thermal_read_mode) {
5477 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5478 case TPACPI_THERMAL_TPEC_16:
5479 if (idx >= 8 && idx <= 15) {
5480 t = TP_EC_THERMAL_TMP8;
5481 idx -= 8;
5483 /* fallthrough */
5484 #endif
5485 case TPACPI_THERMAL_TPEC_8:
5486 if (idx <= 7) {
5487 if (!acpi_ec_read(t + idx, &tmp))
5488 return -EIO;
5489 *value = tmp * 1000;
5490 return 0;
5492 break;
5494 case TPACPI_THERMAL_ACPI_UPDT:
5495 if (idx <= 7) {
5496 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5497 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5498 return -EIO;
5499 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5500 return -EIO;
5501 *value = (t - 2732) * 100;
5502 return 0;
5504 break;
5506 case TPACPI_THERMAL_ACPI_TMP07:
5507 if (idx <= 7) {
5508 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5509 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5510 return -EIO;
5511 if (t > 127 || t < -127)
5512 t = TP_EC_THERMAL_TMP_NA;
5513 *value = t * 1000;
5514 return 0;
5516 break;
5518 case TPACPI_THERMAL_NONE:
5519 default:
5520 return -ENOSYS;
5523 return -EINVAL;
5526 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5528 int res, i;
5529 int n;
5531 n = 8;
5532 i = 0;
5534 if (!s)
5535 return -EINVAL;
5537 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5538 n = 16;
5540 for (i = 0 ; i < n; i++) {
5541 res = thermal_get_sensor(i, &s->temp[i]);
5542 if (res)
5543 return res;
5546 return n;
5549 /* sysfs temp##_input -------------------------------------------------- */
5551 static ssize_t thermal_temp_input_show(struct device *dev,
5552 struct device_attribute *attr,
5553 char *buf)
5555 struct sensor_device_attribute *sensor_attr =
5556 to_sensor_dev_attr(attr);
5557 int idx = sensor_attr->index;
5558 s32 value;
5559 int res;
5561 res = thermal_get_sensor(idx, &value);
5562 if (res)
5563 return res;
5564 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5565 return -ENXIO;
5567 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5570 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5571 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5572 thermal_temp_input_show, NULL, _idxB)
5574 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5575 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5576 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5577 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5578 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5579 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5580 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5581 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5582 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5583 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5584 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5585 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5586 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5587 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5588 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5589 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5590 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5593 #define THERMAL_ATTRS(X) \
5594 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5596 static struct attribute *thermal_temp_input_attr[] = {
5597 THERMAL_ATTRS(8),
5598 THERMAL_ATTRS(9),
5599 THERMAL_ATTRS(10),
5600 THERMAL_ATTRS(11),
5601 THERMAL_ATTRS(12),
5602 THERMAL_ATTRS(13),
5603 THERMAL_ATTRS(14),
5604 THERMAL_ATTRS(15),
5605 THERMAL_ATTRS(0),
5606 THERMAL_ATTRS(1),
5607 THERMAL_ATTRS(2),
5608 THERMAL_ATTRS(3),
5609 THERMAL_ATTRS(4),
5610 THERMAL_ATTRS(5),
5611 THERMAL_ATTRS(6),
5612 THERMAL_ATTRS(7),
5613 NULL
5616 static const struct attribute_group thermal_temp_input16_group = {
5617 .attrs = thermal_temp_input_attr
5620 static const struct attribute_group thermal_temp_input8_group = {
5621 .attrs = &thermal_temp_input_attr[8]
5624 #undef THERMAL_SENSOR_ATTR_TEMP
5625 #undef THERMAL_ATTRS
5627 /* --------------------------------------------------------------------- */
5629 static int __init thermal_init(struct ibm_init_struct *iibm)
5631 u8 t, ta1, ta2;
5632 int i;
5633 int acpi_tmp7;
5634 int res;
5636 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5638 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5640 if (thinkpad_id.ec_model) {
5642 * Direct EC access mode: sensors at registers
5643 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5644 * non-implemented, thermal sensors return 0x80 when
5645 * not available
5648 ta1 = ta2 = 0;
5649 for (i = 0; i < 8; i++) {
5650 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5651 ta1 |= t;
5652 } else {
5653 ta1 = 0;
5654 break;
5656 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5657 ta2 |= t;
5658 } else {
5659 ta1 = 0;
5660 break;
5663 if (ta1 == 0) {
5664 /* This is sheer paranoia, but we handle it anyway */
5665 if (acpi_tmp7) {
5666 printk(TPACPI_ERR
5667 "ThinkPad ACPI EC access misbehaving, "
5668 "falling back to ACPI TMPx access "
5669 "mode\n");
5670 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5671 } else {
5672 printk(TPACPI_ERR
5673 "ThinkPad ACPI EC access misbehaving, "
5674 "disabling thermal sensors access\n");
5675 thermal_read_mode = TPACPI_THERMAL_NONE;
5677 } else {
5678 thermal_read_mode =
5679 (ta2 != 0) ?
5680 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5682 } else if (acpi_tmp7) {
5683 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5684 /* 600e/x, 770e, 770x */
5685 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5686 } else {
5687 /* Standard ACPI TMPx access, max 8 sensors */
5688 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5690 } else {
5691 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5692 thermal_read_mode = TPACPI_THERMAL_NONE;
5695 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5696 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5697 thermal_read_mode);
5699 switch (thermal_read_mode) {
5700 case TPACPI_THERMAL_TPEC_16:
5701 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5702 &thermal_temp_input16_group);
5703 if (res)
5704 return res;
5705 break;
5706 case TPACPI_THERMAL_TPEC_8:
5707 case TPACPI_THERMAL_ACPI_TMP07:
5708 case TPACPI_THERMAL_ACPI_UPDT:
5709 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5710 &thermal_temp_input8_group);
5711 if (res)
5712 return res;
5713 break;
5714 case TPACPI_THERMAL_NONE:
5715 default:
5716 return 1;
5719 return 0;
5722 static void thermal_exit(void)
5724 switch (thermal_read_mode) {
5725 case TPACPI_THERMAL_TPEC_16:
5726 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5727 &thermal_temp_input16_group);
5728 break;
5729 case TPACPI_THERMAL_TPEC_8:
5730 case TPACPI_THERMAL_ACPI_TMP07:
5731 case TPACPI_THERMAL_ACPI_UPDT:
5732 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5733 &thermal_temp_input16_group);
5734 break;
5735 case TPACPI_THERMAL_NONE:
5736 default:
5737 break;
5741 static int thermal_read(char *p)
5743 int len = 0;
5744 int n, i;
5745 struct ibm_thermal_sensors_struct t;
5747 n = thermal_get_sensors(&t);
5748 if (unlikely(n < 0))
5749 return n;
5751 len += sprintf(p + len, "temperatures:\t");
5753 if (n > 0) {
5754 for (i = 0; i < (n - 1); i++)
5755 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5756 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5757 } else
5758 len += sprintf(p + len, "not supported\n");
5760 return len;
5763 static struct ibm_struct thermal_driver_data = {
5764 .name = "thermal",
5765 .read = thermal_read,
5766 .exit = thermal_exit,
5769 /*************************************************************************
5770 * EC Dump subdriver
5773 static u8 ecdump_regs[256];
5775 static int ecdump_read(char *p)
5777 int len = 0;
5778 int i, j;
5779 u8 v;
5781 len += sprintf(p + len, "EC "
5782 " +00 +01 +02 +03 +04 +05 +06 +07"
5783 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5784 for (i = 0; i < 256; i += 16) {
5785 len += sprintf(p + len, "EC 0x%02x:", i);
5786 for (j = 0; j < 16; j++) {
5787 if (!acpi_ec_read(i + j, &v))
5788 break;
5789 if (v != ecdump_regs[i + j])
5790 len += sprintf(p + len, " *%02x", v);
5791 else
5792 len += sprintf(p + len, " %02x", v);
5793 ecdump_regs[i + j] = v;
5795 len += sprintf(p + len, "\n");
5796 if (j != 16)
5797 break;
5800 /* These are way too dangerous to advertise openly... */
5801 #if 0
5802 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5803 " (<offset> is 00-ff, <value> is 00-ff)\n");
5804 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5805 " (<offset> is 00-ff, <value> is 0-255)\n");
5806 #endif
5807 return len;
5810 static int ecdump_write(char *buf)
5812 char *cmd;
5813 int i, v;
5815 while ((cmd = next_cmd(&buf))) {
5816 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5817 /* i and v set */
5818 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5819 /* i and v set */
5820 } else
5821 return -EINVAL;
5822 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5823 if (!acpi_ec_write(i, v))
5824 return -EIO;
5825 } else
5826 return -EINVAL;
5829 return 0;
5832 static struct ibm_struct ecdump_driver_data = {
5833 .name = "ecdump",
5834 .read = ecdump_read,
5835 .write = ecdump_write,
5836 .flags.experimental = 1,
5839 /*************************************************************************
5840 * Backlight/brightness subdriver
5843 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5846 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5847 * CMOS NVRAM byte 0x5E, bits 0-3.
5849 * EC HBRV (0x31) has the following layout
5850 * Bit 7: unknown function
5851 * Bit 6: unknown function
5852 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5853 * Bit 4: must be set to zero to avoid problems
5854 * Bit 3-0: backlight brightness level
5856 * brightness_get_raw returns status data in the HBRV layout
5858 * WARNING: The X61 has been verified to use HBRV for something else, so
5859 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5860 * testing on the very early *60 Lenovo models...
5863 enum {
5864 TP_EC_BACKLIGHT = 0x31,
5866 /* TP_EC_BACKLIGHT bitmasks */
5867 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5868 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5869 TP_EC_BACKLIGHT_MAPSW = 0x20,
5872 enum tpacpi_brightness_access_mode {
5873 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5874 TPACPI_BRGHT_MODE_EC, /* EC control */
5875 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5876 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5877 TPACPI_BRGHT_MODE_MAX
5880 static struct backlight_device *ibm_backlight_device;
5882 static enum tpacpi_brightness_access_mode brightness_mode =
5883 TPACPI_BRGHT_MODE_MAX;
5885 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5887 static struct mutex brightness_mutex;
5889 /* NVRAM brightness access,
5890 * call with brightness_mutex held! */
5891 static unsigned int tpacpi_brightness_nvram_get(void)
5893 u8 lnvram;
5895 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5896 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5897 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5898 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5900 return lnvram;
5903 static void tpacpi_brightness_checkpoint_nvram(void)
5905 u8 lec = 0;
5906 u8 b_nvram;
5908 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5909 return;
5911 vdbg_printk(TPACPI_DBG_BRGHT,
5912 "trying to checkpoint backlight level to NVRAM...\n");
5914 if (mutex_lock_killable(&brightness_mutex) < 0)
5915 return;
5917 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5918 goto unlock;
5919 lec &= TP_EC_BACKLIGHT_LVLMSK;
5920 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5922 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5923 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5924 /* NVRAM needs update */
5925 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5926 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5927 b_nvram |= lec;
5928 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5929 dbg_printk(TPACPI_DBG_BRGHT,
5930 "updated NVRAM backlight level to %u (0x%02x)\n",
5931 (unsigned int) lec, (unsigned int) b_nvram);
5932 } else
5933 vdbg_printk(TPACPI_DBG_BRGHT,
5934 "NVRAM backlight level already is %u (0x%02x)\n",
5935 (unsigned int) lec, (unsigned int) b_nvram);
5937 unlock:
5938 mutex_unlock(&brightness_mutex);
5942 /* call with brightness_mutex held! */
5943 static int tpacpi_brightness_get_raw(int *status)
5945 u8 lec = 0;
5947 switch (brightness_mode) {
5948 case TPACPI_BRGHT_MODE_UCMS_STEP:
5949 *status = tpacpi_brightness_nvram_get();
5950 return 0;
5951 case TPACPI_BRGHT_MODE_EC:
5952 case TPACPI_BRGHT_MODE_ECNVRAM:
5953 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5954 return -EIO;
5955 *status = lec;
5956 return 0;
5957 default:
5958 return -ENXIO;
5962 /* call with brightness_mutex held! */
5963 /* do NOT call with illegal backlight level value */
5964 static int tpacpi_brightness_set_ec(unsigned int value)
5966 u8 lec = 0;
5968 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5969 return -EIO;
5971 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5972 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5973 (value & TP_EC_BACKLIGHT_LVLMSK))))
5974 return -EIO;
5976 return 0;
5979 /* call with brightness_mutex held! */
5980 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5982 int cmos_cmd, inc;
5983 unsigned int current_value, i;
5985 current_value = tpacpi_brightness_nvram_get();
5987 if (value == current_value)
5988 return 0;
5990 cmos_cmd = (value > current_value) ?
5991 TP_CMOS_BRIGHTNESS_UP :
5992 TP_CMOS_BRIGHTNESS_DOWN;
5993 inc = (value > current_value) ? 1 : -1;
5995 for (i = current_value; i != value; i += inc)
5996 if (issue_thinkpad_cmos_command(cmos_cmd))
5997 return -EIO;
5999 return 0;
6002 /* May return EINTR which can always be mapped to ERESTARTSYS */
6003 static int brightness_set(unsigned int value)
6005 int res;
6007 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6008 value < 0)
6009 return -EINVAL;
6011 vdbg_printk(TPACPI_DBG_BRGHT,
6012 "set backlight level to %d\n", value);
6014 res = mutex_lock_killable(&brightness_mutex);
6015 if (res < 0)
6016 return res;
6018 switch (brightness_mode) {
6019 case TPACPI_BRGHT_MODE_EC:
6020 case TPACPI_BRGHT_MODE_ECNVRAM:
6021 res = tpacpi_brightness_set_ec(value);
6022 break;
6023 case TPACPI_BRGHT_MODE_UCMS_STEP:
6024 res = tpacpi_brightness_set_ucmsstep(value);
6025 break;
6026 default:
6027 res = -ENXIO;
6030 mutex_unlock(&brightness_mutex);
6031 return res;
6034 /* sysfs backlight class ----------------------------------------------- */
6036 static int brightness_update_status(struct backlight_device *bd)
6038 unsigned int level =
6039 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6040 bd->props.power == FB_BLANK_UNBLANK) ?
6041 bd->props.brightness : 0;
6043 dbg_printk(TPACPI_DBG_BRGHT,
6044 "backlight: attempt to set level to %d\n",
6045 level);
6047 /* it is the backlight class's job (caller) to handle
6048 * EINTR and other errors properly */
6049 return brightness_set(level);
6052 static int brightness_get(struct backlight_device *bd)
6054 int status, res;
6056 res = mutex_lock_killable(&brightness_mutex);
6057 if (res < 0)
6058 return 0;
6060 res = tpacpi_brightness_get_raw(&status);
6062 mutex_unlock(&brightness_mutex);
6064 if (res < 0)
6065 return 0;
6067 return status & TP_EC_BACKLIGHT_LVLMSK;
6070 static void tpacpi_brightness_notify_change(void)
6072 backlight_force_update(ibm_backlight_device,
6073 BACKLIGHT_UPDATE_HOTKEY);
6076 static struct backlight_ops ibm_backlight_data = {
6077 .get_brightness = brightness_get,
6078 .update_status = brightness_update_status,
6081 /* --------------------------------------------------------------------- */
6084 * These are only useful for models that have only one possibility
6085 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6086 * these quirks.
6088 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6089 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6090 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6092 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6093 /* Models with ATI GPUs known to require ECNVRAM mode */
6094 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6096 /* Models with ATI GPUs that can use ECNVRAM */
6097 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6098 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6099 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6100 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6102 /* Models with Intel Extreme Graphics 2 */
6103 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6104 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6105 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6107 /* Models with Intel GMA900 */
6108 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6109 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6110 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6113 static int __init brightness_init(struct ibm_init_struct *iibm)
6115 int b;
6116 unsigned long quirks;
6118 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6120 mutex_init(&brightness_mutex);
6122 quirks = tpacpi_check_quirks(brightness_quirk_table,
6123 ARRAY_SIZE(brightness_quirk_table));
6126 * We always attempt to detect acpi support, so as to switch
6127 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6128 * going to publish a backlight interface
6130 b = tpacpi_check_std_acpi_brightness_support();
6131 if (b > 0) {
6133 if (acpi_video_backlight_support()) {
6134 if (brightness_enable > 1) {
6135 printk(TPACPI_NOTICE
6136 "Standard ACPI backlight interface "
6137 "available, not loading native one.\n");
6138 return 1;
6139 } else if (brightness_enable == 1) {
6140 printk(TPACPI_NOTICE
6141 "Backlight control force enabled, even if standard "
6142 "ACPI backlight interface is available\n");
6144 } else {
6145 if (brightness_enable > 1) {
6146 printk(TPACPI_NOTICE
6147 "Standard ACPI backlight interface not "
6148 "available, thinkpad_acpi native "
6149 "brightness control enabled\n");
6154 if (!brightness_enable) {
6155 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6156 "brightness support disabled by "
6157 "module parameter\n");
6158 return 1;
6161 if (b > 16) {
6162 printk(TPACPI_ERR
6163 "Unsupported brightness interface, "
6164 "please contact %s\n", TPACPI_MAIL);
6165 return 1;
6167 if (b == 16)
6168 tp_features.bright_16levels = 1;
6171 * Check for module parameter bogosity, note that we
6172 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6173 * able to detect "unspecified"
6175 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6176 return -EINVAL;
6178 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6179 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6180 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6181 if (quirks & TPACPI_BRGHT_Q_EC)
6182 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6183 else
6184 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6186 dbg_printk(TPACPI_DBG_BRGHT,
6187 "driver auto-selected brightness_mode=%d\n",
6188 brightness_mode);
6191 /* Safety */
6192 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6193 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6194 brightness_mode == TPACPI_BRGHT_MODE_EC))
6195 return -EINVAL;
6197 if (tpacpi_brightness_get_raw(&b) < 0)
6198 return 1;
6200 if (tp_features.bright_16levels)
6201 printk(TPACPI_INFO
6202 "detected a 16-level brightness capable ThinkPad\n");
6204 ibm_backlight_device = backlight_device_register(
6205 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6206 &ibm_backlight_data);
6207 if (IS_ERR(ibm_backlight_device)) {
6208 int rc = PTR_ERR(ibm_backlight_device);
6209 ibm_backlight_device = NULL;
6210 printk(TPACPI_ERR "Could not register backlight device\n");
6211 return rc;
6213 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6214 "brightness is supported\n");
6216 if (quirks & TPACPI_BRGHT_Q_ASK) {
6217 printk(TPACPI_NOTICE
6218 "brightness: will use unverified default: "
6219 "brightness_mode=%d\n", brightness_mode);
6220 printk(TPACPI_NOTICE
6221 "brightness: please report to %s whether it works well "
6222 "or not on your ThinkPad\n", TPACPI_MAIL);
6225 ibm_backlight_device->props.max_brightness =
6226 (tp_features.bright_16levels)? 15 : 7;
6227 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6228 backlight_update_status(ibm_backlight_device);
6230 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6231 "brightness: registering brightness hotkeys "
6232 "as change notification\n");
6233 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6234 | TP_ACPI_HKEY_BRGHTUP_MASK
6235 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6236 return 0;
6239 static void brightness_suspend(pm_message_t state)
6241 tpacpi_brightness_checkpoint_nvram();
6244 static void brightness_shutdown(void)
6246 tpacpi_brightness_checkpoint_nvram();
6249 static void brightness_exit(void)
6251 if (ibm_backlight_device) {
6252 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6253 "calling backlight_device_unregister()\n");
6254 backlight_device_unregister(ibm_backlight_device);
6257 tpacpi_brightness_checkpoint_nvram();
6260 static int brightness_read(char *p)
6262 int len = 0;
6263 int level;
6265 level = brightness_get(NULL);
6266 if (level < 0) {
6267 len += sprintf(p + len, "level:\t\tunreadable\n");
6268 } else {
6269 len += sprintf(p + len, "level:\t\t%d\n", level);
6270 len += sprintf(p + len, "commands:\tup, down\n");
6271 len += sprintf(p + len, "commands:\tlevel <level>"
6272 " (<level> is 0-%d)\n",
6273 (tp_features.bright_16levels) ? 15 : 7);
6276 return len;
6279 static int brightness_write(char *buf)
6281 int level;
6282 int rc;
6283 char *cmd;
6284 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6286 level = brightness_get(NULL);
6287 if (level < 0)
6288 return level;
6290 while ((cmd = next_cmd(&buf))) {
6291 if (strlencmp(cmd, "up") == 0) {
6292 if (level < max_level)
6293 level++;
6294 } else if (strlencmp(cmd, "down") == 0) {
6295 if (level > 0)
6296 level--;
6297 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6298 level >= 0 && level <= max_level) {
6299 /* new level set */
6300 } else
6301 return -EINVAL;
6304 tpacpi_disclose_usertask("procfs brightness",
6305 "set level to %d\n", level);
6308 * Now we know what the final level should be, so we try to set it.
6309 * Doing it this way makes the syscall restartable in case of EINTR
6311 rc = brightness_set(level);
6312 if (!rc && ibm_backlight_device)
6313 backlight_force_update(ibm_backlight_device,
6314 BACKLIGHT_UPDATE_SYSFS);
6315 return (rc == -EINTR)? -ERESTARTSYS : rc;
6318 static struct ibm_struct brightness_driver_data = {
6319 .name = "brightness",
6320 .read = brightness_read,
6321 .write = brightness_write,
6322 .exit = brightness_exit,
6323 .suspend = brightness_suspend,
6324 .shutdown = brightness_shutdown,
6327 /*************************************************************************
6328 * Volume subdriver
6331 static int volume_offset = 0x30;
6333 static int volume_read(char *p)
6335 int len = 0;
6336 u8 level;
6338 if (!acpi_ec_read(volume_offset, &level)) {
6339 len += sprintf(p + len, "level:\t\tunreadable\n");
6340 } else {
6341 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6342 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6343 len += sprintf(p + len, "commands:\tup, down, mute\n");
6344 len += sprintf(p + len, "commands:\tlevel <level>"
6345 " (<level> is 0-15)\n");
6348 return len;
6351 static int volume_write(char *buf)
6353 int cmos_cmd, inc, i;
6354 u8 level, mute;
6355 int new_level, new_mute;
6356 char *cmd;
6358 while ((cmd = next_cmd(&buf))) {
6359 if (!acpi_ec_read(volume_offset, &level))
6360 return -EIO;
6361 new_mute = mute = level & 0x40;
6362 new_level = level = level & 0xf;
6364 if (strlencmp(cmd, "up") == 0) {
6365 if (mute)
6366 new_mute = 0;
6367 else
6368 new_level = level == 15 ? 15 : level + 1;
6369 } else if (strlencmp(cmd, "down") == 0) {
6370 if (mute)
6371 new_mute = 0;
6372 else
6373 new_level = level == 0 ? 0 : level - 1;
6374 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6375 new_level >= 0 && new_level <= 15) {
6376 /* new_level set */
6377 } else if (strlencmp(cmd, "mute") == 0) {
6378 new_mute = 0x40;
6379 } else
6380 return -EINVAL;
6382 if (new_level != level) {
6383 /* mute doesn't change */
6385 cmos_cmd = (new_level > level) ?
6386 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6387 inc = new_level > level ? 1 : -1;
6389 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6390 !acpi_ec_write(volume_offset, level)))
6391 return -EIO;
6393 for (i = level; i != new_level; i += inc)
6394 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6395 !acpi_ec_write(volume_offset, i + inc))
6396 return -EIO;
6398 if (mute &&
6399 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6400 !acpi_ec_write(volume_offset, new_level + mute))) {
6401 return -EIO;
6405 if (new_mute != mute) {
6406 /* level doesn't change */
6408 cmos_cmd = (new_mute) ?
6409 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6411 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6412 !acpi_ec_write(volume_offset, level + new_mute))
6413 return -EIO;
6417 return 0;
6420 static struct ibm_struct volume_driver_data = {
6421 .name = "volume",
6422 .read = volume_read,
6423 .write = volume_write,
6426 /*************************************************************************
6427 * Fan subdriver
6431 * FAN ACCESS MODES
6433 * TPACPI_FAN_RD_ACPI_GFAN:
6434 * ACPI GFAN method: returns fan level
6436 * see TPACPI_FAN_WR_ACPI_SFAN
6437 * EC 0x2f (HFSP) not available if GFAN exists
6439 * TPACPI_FAN_WR_ACPI_SFAN:
6440 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6442 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6443 * it for writing.
6445 * TPACPI_FAN_WR_TPEC:
6446 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6447 * Supported on almost all ThinkPads
6449 * Fan speed changes of any sort (including those caused by the
6450 * disengaged mode) are usually done slowly by the firmware as the
6451 * maximum ammount of fan duty cycle change per second seems to be
6452 * limited.
6454 * Reading is not available if GFAN exists.
6455 * Writing is not available if SFAN exists.
6457 * Bits
6458 * 7 automatic mode engaged;
6459 * (default operation mode of the ThinkPad)
6460 * fan level is ignored in this mode.
6461 * 6 full speed mode (takes precedence over bit 7);
6462 * not available on all thinkpads. May disable
6463 * the tachometer while the fan controller ramps up
6464 * the speed (which can take up to a few *minutes*).
6465 * Speeds up fan to 100% duty-cycle, which is far above
6466 * the standard RPM levels. It is not impossible that
6467 * it could cause hardware damage.
6468 * 5-3 unused in some models. Extra bits for fan level
6469 * in others, but still useless as all values above
6470 * 7 map to the same speed as level 7 in these models.
6471 * 2-0 fan level (0..7 usually)
6472 * 0x00 = stop
6473 * 0x07 = max (set when temperatures critical)
6474 * Some ThinkPads may have other levels, see
6475 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6477 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6478 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6479 * does so, its initial value is meaningless (0x07).
6481 * For firmware bugs, refer to:
6482 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6484 * ----
6486 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6487 * Main fan tachometer reading (in RPM)
6489 * This register is present on all ThinkPads with a new-style EC, and
6490 * it is known not to be present on the A21m/e, and T22, as there is
6491 * something else in offset 0x84 according to the ACPI DSDT. Other
6492 * ThinkPads from this same time period (and earlier) probably lack the
6493 * tachometer as well.
6495 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6496 * was never fixed by IBM to report the EC firmware version string
6497 * probably support the tachometer (like the early X models), so
6498 * detecting it is quite hard. We need more data to know for sure.
6500 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6501 * might result.
6503 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6504 * mode.
6506 * For firmware bugs, refer to:
6507 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6509 * ----
6511 * ThinkPad EC register 0x31 bit 0 (only on select models)
6513 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6514 * show the speed of the main fan. When bit 0 of EC register 0x31
6515 * is one, the tachometer registers show the speed of the auxiliary
6516 * fan.
6518 * Fan control seems to affect both fans, regardless of the state
6519 * of this bit.
6521 * So far, only the firmware for the X60/X61 non-tablet versions
6522 * seem to support this (firmware TP-7M).
6524 * TPACPI_FAN_WR_ACPI_FANS:
6525 * ThinkPad X31, X40, X41. Not available in the X60.
6527 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6528 * high speed. ACPI DSDT seems to map these three speeds to levels
6529 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6530 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6532 * The speeds are stored on handles
6533 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6535 * There are three default speed sets, acessible as handles:
6536 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6538 * ACPI DSDT switches which set is in use depending on various
6539 * factors.
6541 * TPACPI_FAN_WR_TPEC is also available and should be used to
6542 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6543 * but the ACPI tables just mention level 7.
6546 enum { /* Fan control constants */
6547 fan_status_offset = 0x2f, /* EC register 0x2f */
6548 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6549 * 0x84 must be read before 0x85 */
6550 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6551 bit 0 selects which fan is active */
6553 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6554 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6556 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6559 enum fan_status_access_mode {
6560 TPACPI_FAN_NONE = 0, /* No fan status or control */
6561 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6562 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6565 enum fan_control_access_mode {
6566 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6567 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6568 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6569 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6572 enum fan_control_commands {
6573 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6574 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6575 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6576 * and also watchdog cmd */
6579 static int fan_control_allowed;
6581 static enum fan_status_access_mode fan_status_access_mode;
6582 static enum fan_control_access_mode fan_control_access_mode;
6583 static enum fan_control_commands fan_control_commands;
6585 static u8 fan_control_initial_status;
6586 static u8 fan_control_desired_level;
6587 static u8 fan_control_resume_level;
6588 static int fan_watchdog_maxinterval;
6590 static struct mutex fan_mutex;
6592 static void fan_watchdog_fire(struct work_struct *ignored);
6593 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6595 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6596 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6597 "\\FSPD", /* 600e/x, 770e, 770x */
6598 ); /* all others */
6599 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6600 "JFNS", /* 770x-JL */
6601 ); /* all others */
6604 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6605 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6606 * be in auto mode (0x80).
6608 * This is corrected by any write to HFSP either by the driver, or
6609 * by the firmware.
6611 * We assume 0x07 really means auto mode while this quirk is active,
6612 * as this is far more likely than the ThinkPad being in level 7,
6613 * which is only used by the firmware during thermal emergencies.
6615 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6616 * TP-70 (T43, R52), which are known to be buggy.
6619 static void fan_quirk1_setup(void)
6621 if (fan_control_initial_status == 0x07) {
6622 printk(TPACPI_NOTICE
6623 "fan_init: initial fan status is unknown, "
6624 "assuming it is in auto mode\n");
6625 tp_features.fan_ctrl_status_undef = 1;
6629 static void fan_quirk1_handle(u8 *fan_status)
6631 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6632 if (*fan_status != fan_control_initial_status) {
6633 /* something changed the HFSP regisnter since
6634 * driver init time, so it is not undefined
6635 * anymore */
6636 tp_features.fan_ctrl_status_undef = 0;
6637 } else {
6638 /* Return most likely status. In fact, it
6639 * might be the only possible status */
6640 *fan_status = TP_EC_FAN_AUTO;
6645 /* Select main fan on X60/X61, NOOP on others */
6646 static bool fan_select_fan1(void)
6648 if (tp_features.second_fan) {
6649 u8 val;
6651 if (ec_read(fan_select_offset, &val) < 0)
6652 return false;
6653 val &= 0xFEU;
6654 if (ec_write(fan_select_offset, val) < 0)
6655 return false;
6657 return true;
6660 /* Select secondary fan on X60/X61 */
6661 static bool fan_select_fan2(void)
6663 u8 val;
6665 if (!tp_features.second_fan)
6666 return false;
6668 if (ec_read(fan_select_offset, &val) < 0)
6669 return false;
6670 val |= 0x01U;
6671 if (ec_write(fan_select_offset, val) < 0)
6672 return false;
6674 return true;
6678 * Call with fan_mutex held
6680 static void fan_update_desired_level(u8 status)
6682 if ((status &
6683 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6684 if (status > 7)
6685 fan_control_desired_level = 7;
6686 else
6687 fan_control_desired_level = status;
6691 static int fan_get_status(u8 *status)
6693 u8 s;
6695 /* TODO:
6696 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6698 switch (fan_status_access_mode) {
6699 case TPACPI_FAN_RD_ACPI_GFAN:
6700 /* 570, 600e/x, 770e, 770x */
6702 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6703 return -EIO;
6705 if (likely(status))
6706 *status = s & 0x07;
6708 break;
6710 case TPACPI_FAN_RD_TPEC:
6711 /* all except 570, 600e/x, 770e, 770x */
6712 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6713 return -EIO;
6715 if (likely(status)) {
6716 *status = s;
6717 fan_quirk1_handle(status);
6720 break;
6722 default:
6723 return -ENXIO;
6726 return 0;
6729 static int fan_get_status_safe(u8 *status)
6731 int rc;
6732 u8 s;
6734 if (mutex_lock_killable(&fan_mutex))
6735 return -ERESTARTSYS;
6736 rc = fan_get_status(&s);
6737 if (!rc)
6738 fan_update_desired_level(s);
6739 mutex_unlock(&fan_mutex);
6741 if (status)
6742 *status = s;
6744 return rc;
6747 static int fan_get_speed(unsigned int *speed)
6749 u8 hi, lo;
6751 switch (fan_status_access_mode) {
6752 case TPACPI_FAN_RD_TPEC:
6753 /* all except 570, 600e/x, 770e, 770x */
6754 if (unlikely(!fan_select_fan1()))
6755 return -EIO;
6756 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6757 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6758 return -EIO;
6760 if (likely(speed))
6761 *speed = (hi << 8) | lo;
6763 break;
6765 default:
6766 return -ENXIO;
6769 return 0;
6772 static int fan2_get_speed(unsigned int *speed)
6774 u8 hi, lo;
6775 bool rc;
6777 switch (fan_status_access_mode) {
6778 case TPACPI_FAN_RD_TPEC:
6779 /* all except 570, 600e/x, 770e, 770x */
6780 if (unlikely(!fan_select_fan2()))
6781 return -EIO;
6782 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6783 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6784 fan_select_fan1(); /* play it safe */
6785 if (rc)
6786 return -EIO;
6788 if (likely(speed))
6789 *speed = (hi << 8) | lo;
6791 break;
6793 default:
6794 return -ENXIO;
6797 return 0;
6800 static int fan_set_level(int level)
6802 if (!fan_control_allowed)
6803 return -EPERM;
6805 switch (fan_control_access_mode) {
6806 case TPACPI_FAN_WR_ACPI_SFAN:
6807 if (level >= 0 && level <= 7) {
6808 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6809 return -EIO;
6810 } else
6811 return -EINVAL;
6812 break;
6814 case TPACPI_FAN_WR_ACPI_FANS:
6815 case TPACPI_FAN_WR_TPEC:
6816 if (!(level & TP_EC_FAN_AUTO) &&
6817 !(level & TP_EC_FAN_FULLSPEED) &&
6818 ((level < 0) || (level > 7)))
6819 return -EINVAL;
6821 /* safety net should the EC not support AUTO
6822 * or FULLSPEED mode bits and just ignore them */
6823 if (level & TP_EC_FAN_FULLSPEED)
6824 level |= 7; /* safety min speed 7 */
6825 else if (level & TP_EC_FAN_AUTO)
6826 level |= 4; /* safety min speed 4 */
6828 if (!acpi_ec_write(fan_status_offset, level))
6829 return -EIO;
6830 else
6831 tp_features.fan_ctrl_status_undef = 0;
6832 break;
6834 default:
6835 return -ENXIO;
6838 vdbg_printk(TPACPI_DBG_FAN,
6839 "fan control: set fan control register to 0x%02x\n", level);
6840 return 0;
6843 static int fan_set_level_safe(int level)
6845 int rc;
6847 if (!fan_control_allowed)
6848 return -EPERM;
6850 if (mutex_lock_killable(&fan_mutex))
6851 return -ERESTARTSYS;
6853 if (level == TPACPI_FAN_LAST_LEVEL)
6854 level = fan_control_desired_level;
6856 rc = fan_set_level(level);
6857 if (!rc)
6858 fan_update_desired_level(level);
6860 mutex_unlock(&fan_mutex);
6861 return rc;
6864 static int fan_set_enable(void)
6866 u8 s;
6867 int rc;
6869 if (!fan_control_allowed)
6870 return -EPERM;
6872 if (mutex_lock_killable(&fan_mutex))
6873 return -ERESTARTSYS;
6875 switch (fan_control_access_mode) {
6876 case TPACPI_FAN_WR_ACPI_FANS:
6877 case TPACPI_FAN_WR_TPEC:
6878 rc = fan_get_status(&s);
6879 if (rc < 0)
6880 break;
6882 /* Don't go out of emergency fan mode */
6883 if (s != 7) {
6884 s &= 0x07;
6885 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6888 if (!acpi_ec_write(fan_status_offset, s))
6889 rc = -EIO;
6890 else {
6891 tp_features.fan_ctrl_status_undef = 0;
6892 rc = 0;
6894 break;
6896 case TPACPI_FAN_WR_ACPI_SFAN:
6897 rc = fan_get_status(&s);
6898 if (rc < 0)
6899 break;
6901 s &= 0x07;
6903 /* Set fan to at least level 4 */
6904 s |= 4;
6906 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6907 rc = -EIO;
6908 else
6909 rc = 0;
6910 break;
6912 default:
6913 rc = -ENXIO;
6916 mutex_unlock(&fan_mutex);
6918 if (!rc)
6919 vdbg_printk(TPACPI_DBG_FAN,
6920 "fan control: set fan control register to 0x%02x\n",
6922 return rc;
6925 static int fan_set_disable(void)
6927 int rc;
6929 if (!fan_control_allowed)
6930 return -EPERM;
6932 if (mutex_lock_killable(&fan_mutex))
6933 return -ERESTARTSYS;
6935 rc = 0;
6936 switch (fan_control_access_mode) {
6937 case TPACPI_FAN_WR_ACPI_FANS:
6938 case TPACPI_FAN_WR_TPEC:
6939 if (!acpi_ec_write(fan_status_offset, 0x00))
6940 rc = -EIO;
6941 else {
6942 fan_control_desired_level = 0;
6943 tp_features.fan_ctrl_status_undef = 0;
6945 break;
6947 case TPACPI_FAN_WR_ACPI_SFAN:
6948 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6949 rc = -EIO;
6950 else
6951 fan_control_desired_level = 0;
6952 break;
6954 default:
6955 rc = -ENXIO;
6958 if (!rc)
6959 vdbg_printk(TPACPI_DBG_FAN,
6960 "fan control: set fan control register to 0\n");
6962 mutex_unlock(&fan_mutex);
6963 return rc;
6966 static int fan_set_speed(int speed)
6968 int rc;
6970 if (!fan_control_allowed)
6971 return -EPERM;
6973 if (mutex_lock_killable(&fan_mutex))
6974 return -ERESTARTSYS;
6976 rc = 0;
6977 switch (fan_control_access_mode) {
6978 case TPACPI_FAN_WR_ACPI_FANS:
6979 if (speed >= 0 && speed <= 65535) {
6980 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6981 speed, speed, speed))
6982 rc = -EIO;
6983 } else
6984 rc = -EINVAL;
6985 break;
6987 default:
6988 rc = -ENXIO;
6991 mutex_unlock(&fan_mutex);
6992 return rc;
6995 static void fan_watchdog_reset(void)
6997 static int fan_watchdog_active;
6999 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7000 return;
7002 if (fan_watchdog_active)
7003 cancel_delayed_work(&fan_watchdog_task);
7005 if (fan_watchdog_maxinterval > 0 &&
7006 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7007 fan_watchdog_active = 1;
7008 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7009 msecs_to_jiffies(fan_watchdog_maxinterval
7010 * 1000))) {
7011 printk(TPACPI_ERR
7012 "failed to queue the fan watchdog, "
7013 "watchdog will not trigger\n");
7015 } else
7016 fan_watchdog_active = 0;
7019 static void fan_watchdog_fire(struct work_struct *ignored)
7021 int rc;
7023 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7024 return;
7026 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7027 rc = fan_set_enable();
7028 if (rc < 0) {
7029 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7030 "will try again later...\n", -rc);
7031 /* reschedule for later */
7032 fan_watchdog_reset();
7037 * SYSFS fan layout: hwmon compatible (device)
7039 * pwm*_enable:
7040 * 0: "disengaged" mode
7041 * 1: manual mode
7042 * 2: native EC "auto" mode (recommended, hardware default)
7044 * pwm*: set speed in manual mode, ignored otherwise.
7045 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7046 * interpolation.
7048 * fan*_input: tachometer reading, RPM
7051 * SYSFS fan layout: extensions
7053 * fan_watchdog (driver):
7054 * fan watchdog interval in seconds, 0 disables (default), max 120
7057 /* sysfs fan pwm1_enable ----------------------------------------------- */
7058 static ssize_t fan_pwm1_enable_show(struct device *dev,
7059 struct device_attribute *attr,
7060 char *buf)
7062 int res, mode;
7063 u8 status;
7065 res = fan_get_status_safe(&status);
7066 if (res)
7067 return res;
7069 if (status & TP_EC_FAN_FULLSPEED) {
7070 mode = 0;
7071 } else if (status & TP_EC_FAN_AUTO) {
7072 mode = 2;
7073 } else
7074 mode = 1;
7076 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7079 static ssize_t fan_pwm1_enable_store(struct device *dev,
7080 struct device_attribute *attr,
7081 const char *buf, size_t count)
7083 unsigned long t;
7084 int res, level;
7086 if (parse_strtoul(buf, 2, &t))
7087 return -EINVAL;
7089 tpacpi_disclose_usertask("hwmon pwm1_enable",
7090 "set fan mode to %lu\n", t);
7092 switch (t) {
7093 case 0:
7094 level = TP_EC_FAN_FULLSPEED;
7095 break;
7096 case 1:
7097 level = TPACPI_FAN_LAST_LEVEL;
7098 break;
7099 case 2:
7100 level = TP_EC_FAN_AUTO;
7101 break;
7102 case 3:
7103 /* reserved for software-controlled auto mode */
7104 return -ENOSYS;
7105 default:
7106 return -EINVAL;
7109 res = fan_set_level_safe(level);
7110 if (res == -ENXIO)
7111 return -EINVAL;
7112 else if (res < 0)
7113 return res;
7115 fan_watchdog_reset();
7117 return count;
7120 static struct device_attribute dev_attr_fan_pwm1_enable =
7121 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7122 fan_pwm1_enable_show, fan_pwm1_enable_store);
7124 /* sysfs fan pwm1 ------------------------------------------------------ */
7125 static ssize_t fan_pwm1_show(struct device *dev,
7126 struct device_attribute *attr,
7127 char *buf)
7129 int res;
7130 u8 status;
7132 res = fan_get_status_safe(&status);
7133 if (res)
7134 return res;
7136 if ((status &
7137 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7138 status = fan_control_desired_level;
7140 if (status > 7)
7141 status = 7;
7143 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7146 static ssize_t fan_pwm1_store(struct device *dev,
7147 struct device_attribute *attr,
7148 const char *buf, size_t count)
7150 unsigned long s;
7151 int rc;
7152 u8 status, newlevel;
7154 if (parse_strtoul(buf, 255, &s))
7155 return -EINVAL;
7157 tpacpi_disclose_usertask("hwmon pwm1",
7158 "set fan speed to %lu\n", s);
7160 /* scale down from 0-255 to 0-7 */
7161 newlevel = (s >> 5) & 0x07;
7163 if (mutex_lock_killable(&fan_mutex))
7164 return -ERESTARTSYS;
7166 rc = fan_get_status(&status);
7167 if (!rc && (status &
7168 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7169 rc = fan_set_level(newlevel);
7170 if (rc == -ENXIO)
7171 rc = -EINVAL;
7172 else if (!rc) {
7173 fan_update_desired_level(newlevel);
7174 fan_watchdog_reset();
7178 mutex_unlock(&fan_mutex);
7179 return (rc)? rc : count;
7182 static struct device_attribute dev_attr_fan_pwm1 =
7183 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7184 fan_pwm1_show, fan_pwm1_store);
7186 /* sysfs fan fan1_input ------------------------------------------------ */
7187 static ssize_t fan_fan1_input_show(struct device *dev,
7188 struct device_attribute *attr,
7189 char *buf)
7191 int res;
7192 unsigned int speed;
7194 res = fan_get_speed(&speed);
7195 if (res < 0)
7196 return res;
7198 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7201 static struct device_attribute dev_attr_fan_fan1_input =
7202 __ATTR(fan1_input, S_IRUGO,
7203 fan_fan1_input_show, NULL);
7205 /* sysfs fan fan2_input ------------------------------------------------ */
7206 static ssize_t fan_fan2_input_show(struct device *dev,
7207 struct device_attribute *attr,
7208 char *buf)
7210 int res;
7211 unsigned int speed;
7213 res = fan2_get_speed(&speed);
7214 if (res < 0)
7215 return res;
7217 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7220 static struct device_attribute dev_attr_fan_fan2_input =
7221 __ATTR(fan2_input, S_IRUGO,
7222 fan_fan2_input_show, NULL);
7224 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7225 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7226 char *buf)
7228 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7231 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7232 const char *buf, size_t count)
7234 unsigned long t;
7236 if (parse_strtoul(buf, 120, &t))
7237 return -EINVAL;
7239 if (!fan_control_allowed)
7240 return -EPERM;
7242 fan_watchdog_maxinterval = t;
7243 fan_watchdog_reset();
7245 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7247 return count;
7250 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7251 fan_fan_watchdog_show, fan_fan_watchdog_store);
7253 /* --------------------------------------------------------------------- */
7254 static struct attribute *fan_attributes[] = {
7255 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7256 &dev_attr_fan_fan1_input.attr,
7257 NULL, /* for fan2_input */
7258 NULL
7261 static const struct attribute_group fan_attr_group = {
7262 .attrs = fan_attributes,
7265 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7266 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7268 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7269 { .vendor = PCI_VENDOR_ID_IBM, \
7270 .bios = TPACPI_MATCH_ANY, \
7271 .ec = TPID(__id1, __id2), \
7272 .quirks = __quirks }
7274 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7275 { .vendor = PCI_VENDOR_ID_LENOVO, \
7276 .bios = TPACPI_MATCH_ANY, \
7277 .ec = TPID(__id1, __id2), \
7278 .quirks = __quirks }
7280 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7281 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7282 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7283 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7284 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7285 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7288 #undef TPACPI_FAN_QL
7289 #undef TPACPI_FAN_QI
7291 static int __init fan_init(struct ibm_init_struct *iibm)
7293 int rc;
7294 unsigned long quirks;
7296 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7297 "initializing fan subdriver\n");
7299 mutex_init(&fan_mutex);
7300 fan_status_access_mode = TPACPI_FAN_NONE;
7301 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7302 fan_control_commands = 0;
7303 fan_watchdog_maxinterval = 0;
7304 tp_features.fan_ctrl_status_undef = 0;
7305 tp_features.second_fan = 0;
7306 fan_control_desired_level = 7;
7308 TPACPI_ACPIHANDLE_INIT(fans);
7309 TPACPI_ACPIHANDLE_INIT(gfan);
7310 TPACPI_ACPIHANDLE_INIT(sfan);
7312 quirks = tpacpi_check_quirks(fan_quirk_table,
7313 ARRAY_SIZE(fan_quirk_table));
7315 if (gfan_handle) {
7316 /* 570, 600e/x, 770e, 770x */
7317 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7318 } else {
7319 /* all other ThinkPads: note that even old-style
7320 * ThinkPad ECs supports the fan control register */
7321 if (likely(acpi_ec_read(fan_status_offset,
7322 &fan_control_initial_status))) {
7323 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7324 if (quirks & TPACPI_FAN_Q1)
7325 fan_quirk1_setup();
7326 if (quirks & TPACPI_FAN_2FAN) {
7327 tp_features.second_fan = 1;
7328 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7329 "secondary fan support enabled\n");
7331 } else {
7332 printk(TPACPI_ERR
7333 "ThinkPad ACPI EC access misbehaving, "
7334 "fan status and control unavailable\n");
7335 return 1;
7339 if (sfan_handle) {
7340 /* 570, 770x-JL */
7341 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7342 fan_control_commands |=
7343 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7344 } else {
7345 if (!gfan_handle) {
7346 /* gfan without sfan means no fan control */
7347 /* all other models implement TP EC 0x2f control */
7349 if (fans_handle) {
7350 /* X31, X40, X41 */
7351 fan_control_access_mode =
7352 TPACPI_FAN_WR_ACPI_FANS;
7353 fan_control_commands |=
7354 TPACPI_FAN_CMD_SPEED |
7355 TPACPI_FAN_CMD_LEVEL |
7356 TPACPI_FAN_CMD_ENABLE;
7357 } else {
7358 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7359 fan_control_commands |=
7360 TPACPI_FAN_CMD_LEVEL |
7361 TPACPI_FAN_CMD_ENABLE;
7366 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7367 "fan is %s, modes %d, %d\n",
7368 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7369 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7370 fan_status_access_mode, fan_control_access_mode);
7372 /* fan control master switch */
7373 if (!fan_control_allowed) {
7374 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7375 fan_control_commands = 0;
7376 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7377 "fan control features disabled by parameter\n");
7380 /* update fan_control_desired_level */
7381 if (fan_status_access_mode != TPACPI_FAN_NONE)
7382 fan_get_status_safe(NULL);
7384 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7385 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7386 if (tp_features.second_fan) {
7387 /* attach second fan tachometer */
7388 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7389 &dev_attr_fan_fan2_input.attr;
7391 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7392 &fan_attr_group);
7393 if (rc < 0)
7394 return rc;
7396 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7397 &driver_attr_fan_watchdog);
7398 if (rc < 0) {
7399 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7400 &fan_attr_group);
7401 return rc;
7403 return 0;
7404 } else
7405 return 1;
7408 static void fan_exit(void)
7410 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7411 "cancelling any pending fan watchdog tasks\n");
7413 /* FIXME: can we really do this unconditionally? */
7414 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7415 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7416 &driver_attr_fan_watchdog);
7418 cancel_delayed_work(&fan_watchdog_task);
7419 flush_workqueue(tpacpi_wq);
7422 static void fan_suspend(pm_message_t state)
7424 int rc;
7426 if (!fan_control_allowed)
7427 return;
7429 /* Store fan status in cache */
7430 fan_control_resume_level = 0;
7431 rc = fan_get_status_safe(&fan_control_resume_level);
7432 if (rc < 0)
7433 printk(TPACPI_NOTICE
7434 "failed to read fan level for later "
7435 "restore during resume: %d\n", rc);
7437 /* if it is undefined, don't attempt to restore it.
7438 * KEEP THIS LAST */
7439 if (tp_features.fan_ctrl_status_undef)
7440 fan_control_resume_level = 0;
7443 static void fan_resume(void)
7445 u8 current_level = 7;
7446 bool do_set = false;
7447 int rc;
7449 /* DSDT *always* updates status on resume */
7450 tp_features.fan_ctrl_status_undef = 0;
7452 if (!fan_control_allowed ||
7453 !fan_control_resume_level ||
7454 (fan_get_status_safe(&current_level) < 0))
7455 return;
7457 switch (fan_control_access_mode) {
7458 case TPACPI_FAN_WR_ACPI_SFAN:
7459 /* never decrease fan level */
7460 do_set = (fan_control_resume_level > current_level);
7461 break;
7462 case TPACPI_FAN_WR_ACPI_FANS:
7463 case TPACPI_FAN_WR_TPEC:
7464 /* never decrease fan level, scale is:
7465 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7467 * We expect the firmware to set either 7 or AUTO, but we
7468 * handle FULLSPEED out of paranoia.
7470 * So, we can safely only restore FULLSPEED or 7, anything
7471 * else could slow the fan. Restoring AUTO is useless, at
7472 * best that's exactly what the DSDT already set (it is the
7473 * slower it uses).
7475 * Always keep in mind that the DSDT *will* have set the
7476 * fans to what the vendor supposes is the best level. We
7477 * muck with it only to speed the fan up.
7479 if (fan_control_resume_level != 7 &&
7480 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7481 return;
7482 else
7483 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7484 (current_level != fan_control_resume_level);
7485 break;
7486 default:
7487 return;
7489 if (do_set) {
7490 printk(TPACPI_NOTICE
7491 "restoring fan level to 0x%02x\n",
7492 fan_control_resume_level);
7493 rc = fan_set_level_safe(fan_control_resume_level);
7494 if (rc < 0)
7495 printk(TPACPI_NOTICE
7496 "failed to restore fan level: %d\n", rc);
7500 static int fan_read(char *p)
7502 int len = 0;
7503 int rc;
7504 u8 status;
7505 unsigned int speed = 0;
7507 switch (fan_status_access_mode) {
7508 case TPACPI_FAN_RD_ACPI_GFAN:
7509 /* 570, 600e/x, 770e, 770x */
7510 rc = fan_get_status_safe(&status);
7511 if (rc < 0)
7512 return rc;
7514 len += sprintf(p + len, "status:\t\t%s\n"
7515 "level:\t\t%d\n",
7516 (status != 0) ? "enabled" : "disabled", status);
7517 break;
7519 case TPACPI_FAN_RD_TPEC:
7520 /* all except 570, 600e/x, 770e, 770x */
7521 rc = fan_get_status_safe(&status);
7522 if (rc < 0)
7523 return rc;
7525 len += sprintf(p + len, "status:\t\t%s\n",
7526 (status != 0) ? "enabled" : "disabled");
7528 rc = fan_get_speed(&speed);
7529 if (rc < 0)
7530 return rc;
7532 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7534 if (status & TP_EC_FAN_FULLSPEED)
7535 /* Disengaged mode takes precedence */
7536 len += sprintf(p + len, "level:\t\tdisengaged\n");
7537 else if (status & TP_EC_FAN_AUTO)
7538 len += sprintf(p + len, "level:\t\tauto\n");
7539 else
7540 len += sprintf(p + len, "level:\t\t%d\n", status);
7541 break;
7543 case TPACPI_FAN_NONE:
7544 default:
7545 len += sprintf(p + len, "status:\t\tnot supported\n");
7548 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7549 len += sprintf(p + len, "commands:\tlevel <level>");
7551 switch (fan_control_access_mode) {
7552 case TPACPI_FAN_WR_ACPI_SFAN:
7553 len += sprintf(p + len, " (<level> is 0-7)\n");
7554 break;
7556 default:
7557 len += sprintf(p + len, " (<level> is 0-7, "
7558 "auto, disengaged, full-speed)\n");
7559 break;
7563 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7564 len += sprintf(p + len, "commands:\tenable, disable\n"
7565 "commands:\twatchdog <timeout> (<timeout> "
7566 "is 0 (off), 1-120 (seconds))\n");
7568 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7569 len += sprintf(p + len, "commands:\tspeed <speed>"
7570 " (<speed> is 0-65535)\n");
7572 return len;
7575 static int fan_write_cmd_level(const char *cmd, int *rc)
7577 int level;
7579 if (strlencmp(cmd, "level auto") == 0)
7580 level = TP_EC_FAN_AUTO;
7581 else if ((strlencmp(cmd, "level disengaged") == 0) |
7582 (strlencmp(cmd, "level full-speed") == 0))
7583 level = TP_EC_FAN_FULLSPEED;
7584 else if (sscanf(cmd, "level %d", &level) != 1)
7585 return 0;
7587 *rc = fan_set_level_safe(level);
7588 if (*rc == -ENXIO)
7589 printk(TPACPI_ERR "level command accepted for unsupported "
7590 "access mode %d", fan_control_access_mode);
7591 else if (!*rc)
7592 tpacpi_disclose_usertask("procfs fan",
7593 "set level to %d\n", level);
7595 return 1;
7598 static int fan_write_cmd_enable(const char *cmd, int *rc)
7600 if (strlencmp(cmd, "enable") != 0)
7601 return 0;
7603 *rc = fan_set_enable();
7604 if (*rc == -ENXIO)
7605 printk(TPACPI_ERR "enable command accepted for unsupported "
7606 "access mode %d", fan_control_access_mode);
7607 else if (!*rc)
7608 tpacpi_disclose_usertask("procfs fan", "enable\n");
7610 return 1;
7613 static int fan_write_cmd_disable(const char *cmd, int *rc)
7615 if (strlencmp(cmd, "disable") != 0)
7616 return 0;
7618 *rc = fan_set_disable();
7619 if (*rc == -ENXIO)
7620 printk(TPACPI_ERR "disable command accepted for unsupported "
7621 "access mode %d", fan_control_access_mode);
7622 else if (!*rc)
7623 tpacpi_disclose_usertask("procfs fan", "disable\n");
7625 return 1;
7628 static int fan_write_cmd_speed(const char *cmd, int *rc)
7630 int speed;
7632 /* TODO:
7633 * Support speed <low> <medium> <high> ? */
7635 if (sscanf(cmd, "speed %d", &speed) != 1)
7636 return 0;
7638 *rc = fan_set_speed(speed);
7639 if (*rc == -ENXIO)
7640 printk(TPACPI_ERR "speed command accepted for unsupported "
7641 "access mode %d", fan_control_access_mode);
7642 else if (!*rc)
7643 tpacpi_disclose_usertask("procfs fan",
7644 "set speed to %d\n", speed);
7646 return 1;
7649 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7651 int interval;
7653 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7654 return 0;
7656 if (interval < 0 || interval > 120)
7657 *rc = -EINVAL;
7658 else {
7659 fan_watchdog_maxinterval = interval;
7660 tpacpi_disclose_usertask("procfs fan",
7661 "set watchdog timer to %d\n",
7662 interval);
7665 return 1;
7668 static int fan_write(char *buf)
7670 char *cmd;
7671 int rc = 0;
7673 while (!rc && (cmd = next_cmd(&buf))) {
7674 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7675 fan_write_cmd_level(cmd, &rc)) &&
7676 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7677 (fan_write_cmd_enable(cmd, &rc) ||
7678 fan_write_cmd_disable(cmd, &rc) ||
7679 fan_write_cmd_watchdog(cmd, &rc))) &&
7680 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7681 fan_write_cmd_speed(cmd, &rc))
7683 rc = -EINVAL;
7684 else if (!rc)
7685 fan_watchdog_reset();
7688 return rc;
7691 static struct ibm_struct fan_driver_data = {
7692 .name = "fan",
7693 .read = fan_read,
7694 .write = fan_write,
7695 .exit = fan_exit,
7696 .suspend = fan_suspend,
7697 .resume = fan_resume,
7700 /****************************************************************************
7701 ****************************************************************************
7703 * Infrastructure
7705 ****************************************************************************
7706 ****************************************************************************/
7709 * HKEY event callout for other subdrivers go here
7710 * (yes, it is ugly, but it is quick, safe, and gets the job done
7712 static void tpacpi_driver_event(const unsigned int hkey_event)
7714 if (ibm_backlight_device) {
7715 switch (hkey_event) {
7716 case TP_HKEY_EV_BRGHT_UP:
7717 case TP_HKEY_EV_BRGHT_DOWN:
7718 tpacpi_brightness_notify_change();
7725 static void hotkey_driver_event(const unsigned int scancode)
7727 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7730 /* sysfs name ---------------------------------------------------------- */
7731 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7732 struct device_attribute *attr,
7733 char *buf)
7735 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7738 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7739 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7741 /* --------------------------------------------------------------------- */
7743 /* /proc support */
7744 static struct proc_dir_entry *proc_dir;
7747 * Module and infrastructure proble, init and exit handling
7750 static int force_load;
7752 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7753 static const char * __init str_supported(int is_supported)
7755 static char text_unsupported[] __initdata = "not supported";
7757 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7759 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7761 static void ibm_exit(struct ibm_struct *ibm)
7763 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7765 list_del_init(&ibm->all_drivers);
7767 if (ibm->flags.acpi_notify_installed) {
7768 dbg_printk(TPACPI_DBG_EXIT,
7769 "%s: acpi_remove_notify_handler\n", ibm->name);
7770 BUG_ON(!ibm->acpi);
7771 acpi_remove_notify_handler(*ibm->acpi->handle,
7772 ibm->acpi->type,
7773 dispatch_acpi_notify);
7774 ibm->flags.acpi_notify_installed = 0;
7775 ibm->flags.acpi_notify_installed = 0;
7778 if (ibm->flags.proc_created) {
7779 dbg_printk(TPACPI_DBG_EXIT,
7780 "%s: remove_proc_entry\n", ibm->name);
7781 remove_proc_entry(ibm->name, proc_dir);
7782 ibm->flags.proc_created = 0;
7785 if (ibm->flags.acpi_driver_registered) {
7786 dbg_printk(TPACPI_DBG_EXIT,
7787 "%s: acpi_bus_unregister_driver\n", ibm->name);
7788 BUG_ON(!ibm->acpi);
7789 acpi_bus_unregister_driver(ibm->acpi->driver);
7790 kfree(ibm->acpi->driver);
7791 ibm->acpi->driver = NULL;
7792 ibm->flags.acpi_driver_registered = 0;
7795 if (ibm->flags.init_called && ibm->exit) {
7796 ibm->exit();
7797 ibm->flags.init_called = 0;
7800 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7803 static int __init ibm_init(struct ibm_init_struct *iibm)
7805 int ret;
7806 struct ibm_struct *ibm = iibm->data;
7807 struct proc_dir_entry *entry;
7809 BUG_ON(ibm == NULL);
7811 INIT_LIST_HEAD(&ibm->all_drivers);
7813 if (ibm->flags.experimental && !experimental)
7814 return 0;
7816 dbg_printk(TPACPI_DBG_INIT,
7817 "probing for %s\n", ibm->name);
7819 if (iibm->init) {
7820 ret = iibm->init(iibm);
7821 if (ret > 0)
7822 return 0; /* probe failed */
7823 if (ret)
7824 return ret;
7826 ibm->flags.init_called = 1;
7829 if (ibm->acpi) {
7830 if (ibm->acpi->hid) {
7831 ret = register_tpacpi_subdriver(ibm);
7832 if (ret)
7833 goto err_out;
7836 if (ibm->acpi->notify) {
7837 ret = setup_acpi_notify(ibm);
7838 if (ret == -ENODEV) {
7839 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7840 ibm->name);
7841 ret = 0;
7842 goto err_out;
7844 if (ret < 0)
7845 goto err_out;
7849 dbg_printk(TPACPI_DBG_INIT,
7850 "%s installed\n", ibm->name);
7852 if (ibm->read) {
7853 entry = create_proc_entry(ibm->name,
7854 S_IFREG | S_IRUGO | S_IWUSR,
7855 proc_dir);
7856 if (!entry) {
7857 printk(TPACPI_ERR "unable to create proc entry %s\n",
7858 ibm->name);
7859 ret = -ENODEV;
7860 goto err_out;
7862 entry->owner = THIS_MODULE;
7863 entry->data = ibm;
7864 entry->read_proc = &dispatch_procfs_read;
7865 if (ibm->write)
7866 entry->write_proc = &dispatch_procfs_write;
7867 ibm->flags.proc_created = 1;
7870 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7872 return 0;
7874 err_out:
7875 dbg_printk(TPACPI_DBG_INIT,
7876 "%s: at error exit path with result %d\n",
7877 ibm->name, ret);
7879 ibm_exit(ibm);
7880 return (ret < 0)? ret : 0;
7883 /* Probing */
7885 static bool __pure __init tpacpi_is_fw_digit(const char c)
7887 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7890 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7891 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7892 const char t)
7894 return s && strlen(s) >= 8 &&
7895 tpacpi_is_fw_digit(s[0]) &&
7896 tpacpi_is_fw_digit(s[1]) &&
7897 s[2] == t && s[3] == 'T' &&
7898 tpacpi_is_fw_digit(s[4]) &&
7899 tpacpi_is_fw_digit(s[5]) &&
7900 s[6] == 'W' && s[7] == 'W';
7903 /* returns 0 - probe ok, or < 0 - probe error.
7904 * Probe ok doesn't mean thinkpad found.
7905 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7906 static int __must_check __init get_thinkpad_model_data(
7907 struct thinkpad_id_data *tp)
7909 const struct dmi_device *dev = NULL;
7910 char ec_fw_string[18];
7911 char const *s;
7913 if (!tp)
7914 return -EINVAL;
7916 memset(tp, 0, sizeof(*tp));
7918 if (dmi_name_in_vendors("IBM"))
7919 tp->vendor = PCI_VENDOR_ID_IBM;
7920 else if (dmi_name_in_vendors("LENOVO"))
7921 tp->vendor = PCI_VENDOR_ID_LENOVO;
7922 else
7923 return 0;
7925 s = dmi_get_system_info(DMI_BIOS_VERSION);
7926 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7927 if (s && !tp->bios_version_str)
7928 return -ENOMEM;
7930 /* Really ancient ThinkPad 240X will fail this, which is fine */
7931 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7932 return 0;
7934 tp->bios_model = tp->bios_version_str[0]
7935 | (tp->bios_version_str[1] << 8);
7936 tp->bios_release = (tp->bios_version_str[4] << 8)
7937 | tp->bios_version_str[5];
7940 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7941 * X32 or newer, all Z series; Some models must have an
7942 * up-to-date BIOS or they will not be detected.
7944 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7946 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7947 if (sscanf(dev->name,
7948 "IBM ThinkPad Embedded Controller -[%17c",
7949 ec_fw_string) == 1) {
7950 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7951 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7953 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7954 if (!tp->ec_version_str)
7955 return -ENOMEM;
7957 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7958 tp->ec_model = ec_fw_string[0]
7959 | (ec_fw_string[1] << 8);
7960 tp->ec_release = (ec_fw_string[4] << 8)
7961 | ec_fw_string[5];
7962 } else {
7963 printk(TPACPI_NOTICE
7964 "ThinkPad firmware release %s "
7965 "doesn't match the known patterns\n",
7966 ec_fw_string);
7967 printk(TPACPI_NOTICE
7968 "please report this to %s\n",
7969 TPACPI_MAIL);
7971 break;
7975 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7976 if (s && !strnicmp(s, "ThinkPad", 8)) {
7977 tp->model_str = kstrdup(s, GFP_KERNEL);
7978 if (!tp->model_str)
7979 return -ENOMEM;
7982 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7983 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7984 if (s && !tp->nummodel_str)
7985 return -ENOMEM;
7987 return 0;
7990 static int __init probe_for_thinkpad(void)
7992 int is_thinkpad;
7994 if (acpi_disabled)
7995 return -ENODEV;
7998 * Non-ancient models have better DMI tagging, but very old models
7999 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8001 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8002 (thinkpad_id.ec_model != 0) ||
8003 tpacpi_is_fw_known();
8005 /* ec is required because many other handles are relative to it */
8006 TPACPI_ACPIHANDLE_INIT(ec);
8007 if (!ec_handle) {
8008 if (is_thinkpad)
8009 printk(TPACPI_ERR
8010 "Not yet supported ThinkPad detected!\n");
8011 return -ENODEV;
8014 if (!is_thinkpad && !force_load)
8015 return -ENODEV;
8017 return 0;
8021 /* Module init, exit, parameters */
8023 static struct ibm_init_struct ibms_init[] __initdata = {
8025 .init = thinkpad_acpi_driver_init,
8026 .data = &thinkpad_acpi_driver_data,
8029 .init = hotkey_init,
8030 .data = &hotkey_driver_data,
8033 .init = bluetooth_init,
8034 .data = &bluetooth_driver_data,
8037 .init = wan_init,
8038 .data = &wan_driver_data,
8041 .init = uwb_init,
8042 .data = &uwb_driver_data,
8044 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8046 .init = video_init,
8047 .data = &video_driver_data,
8049 #endif
8051 .init = light_init,
8052 .data = &light_driver_data,
8055 .init = cmos_init,
8056 .data = &cmos_driver_data,
8059 .init = led_init,
8060 .data = &led_driver_data,
8063 .init = beep_init,
8064 .data = &beep_driver_data,
8067 .init = thermal_init,
8068 .data = &thermal_driver_data,
8071 .data = &ecdump_driver_data,
8074 .init = brightness_init,
8075 .data = &brightness_driver_data,
8078 .data = &volume_driver_data,
8081 .init = fan_init,
8082 .data = &fan_driver_data,
8086 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8088 unsigned int i;
8089 struct ibm_struct *ibm;
8091 if (!kp || !kp->name || !val)
8092 return -EINVAL;
8094 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8095 ibm = ibms_init[i].data;
8096 WARN_ON(ibm == NULL);
8098 if (!ibm || !ibm->name)
8099 continue;
8101 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8102 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8103 return -ENOSPC;
8104 strcpy(ibms_init[i].param, val);
8105 strcat(ibms_init[i].param, ",");
8106 return 0;
8110 return -EINVAL;
8113 module_param(experimental, int, 0);
8114 MODULE_PARM_DESC(experimental,
8115 "Enables experimental features when non-zero");
8117 module_param_named(debug, dbg_level, uint, 0);
8118 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8120 module_param(force_load, bool, 0);
8121 MODULE_PARM_DESC(force_load,
8122 "Attempts to load the driver even on a "
8123 "mis-identified ThinkPad when true");
8125 module_param_named(fan_control, fan_control_allowed, bool, 0);
8126 MODULE_PARM_DESC(fan_control,
8127 "Enables setting fan parameters features when true");
8129 module_param_named(brightness_mode, brightness_mode, uint, 0);
8130 MODULE_PARM_DESC(brightness_mode,
8131 "Selects brightness control strategy: "
8132 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8134 module_param(brightness_enable, uint, 0);
8135 MODULE_PARM_DESC(brightness_enable,
8136 "Enables backlight control when 1, disables when 0");
8138 module_param(hotkey_report_mode, uint, 0);
8139 MODULE_PARM_DESC(hotkey_report_mode,
8140 "used for backwards compatibility with userspace, "
8141 "see documentation");
8143 #define TPACPI_PARAM(feature) \
8144 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8145 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8146 "at module load, see documentation")
8148 TPACPI_PARAM(hotkey);
8149 TPACPI_PARAM(bluetooth);
8150 TPACPI_PARAM(video);
8151 TPACPI_PARAM(light);
8152 TPACPI_PARAM(cmos);
8153 TPACPI_PARAM(led);
8154 TPACPI_PARAM(beep);
8155 TPACPI_PARAM(ecdump);
8156 TPACPI_PARAM(brightness);
8157 TPACPI_PARAM(volume);
8158 TPACPI_PARAM(fan);
8160 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8161 module_param(dbg_wlswemul, uint, 0);
8162 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8163 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8164 MODULE_PARM_DESC(wlsw_state,
8165 "Initial state of the emulated WLSW switch");
8167 module_param(dbg_bluetoothemul, uint, 0);
8168 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8169 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8170 MODULE_PARM_DESC(bluetooth_state,
8171 "Initial state of the emulated bluetooth switch");
8173 module_param(dbg_wwanemul, uint, 0);
8174 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8175 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8176 MODULE_PARM_DESC(wwan_state,
8177 "Initial state of the emulated WWAN switch");
8179 module_param(dbg_uwbemul, uint, 0);
8180 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8181 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8182 MODULE_PARM_DESC(uwb_state,
8183 "Initial state of the emulated UWB switch");
8184 #endif
8186 static void thinkpad_acpi_module_exit(void)
8188 struct ibm_struct *ibm, *itmp;
8190 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8192 list_for_each_entry_safe_reverse(ibm, itmp,
8193 &tpacpi_all_drivers,
8194 all_drivers) {
8195 ibm_exit(ibm);
8198 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8200 if (tpacpi_inputdev) {
8201 if (tp_features.input_device_registered)
8202 input_unregister_device(tpacpi_inputdev);
8203 else
8204 input_free_device(tpacpi_inputdev);
8207 if (tpacpi_hwmon)
8208 hwmon_device_unregister(tpacpi_hwmon);
8210 if (tp_features.sensors_pdev_attrs_registered)
8211 device_remove_file(&tpacpi_sensors_pdev->dev,
8212 &dev_attr_thinkpad_acpi_pdev_name);
8213 if (tpacpi_sensors_pdev)
8214 platform_device_unregister(tpacpi_sensors_pdev);
8215 if (tpacpi_pdev)
8216 platform_device_unregister(tpacpi_pdev);
8218 if (tp_features.sensors_pdrv_attrs_registered)
8219 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8220 if (tp_features.platform_drv_attrs_registered)
8221 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8223 if (tp_features.sensors_pdrv_registered)
8224 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8226 if (tp_features.platform_drv_registered)
8227 platform_driver_unregister(&tpacpi_pdriver);
8229 if (proc_dir)
8230 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8232 if (tpacpi_wq)
8233 destroy_workqueue(tpacpi_wq);
8235 kfree(thinkpad_id.bios_version_str);
8236 kfree(thinkpad_id.ec_version_str);
8237 kfree(thinkpad_id.model_str);
8241 static int __init thinkpad_acpi_module_init(void)
8243 int ret, i;
8245 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8247 /* Parameter checking */
8248 if (hotkey_report_mode > 2)
8249 return -EINVAL;
8251 /* Driver-level probe */
8253 ret = get_thinkpad_model_data(&thinkpad_id);
8254 if (ret) {
8255 printk(TPACPI_ERR
8256 "unable to get DMI data: %d\n", ret);
8257 thinkpad_acpi_module_exit();
8258 return ret;
8260 ret = probe_for_thinkpad();
8261 if (ret) {
8262 thinkpad_acpi_module_exit();
8263 return ret;
8266 /* Driver initialization */
8268 TPACPI_ACPIHANDLE_INIT(ecrd);
8269 TPACPI_ACPIHANDLE_INIT(ecwr);
8271 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8272 if (!tpacpi_wq) {
8273 thinkpad_acpi_module_exit();
8274 return -ENOMEM;
8277 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8278 if (!proc_dir) {
8279 printk(TPACPI_ERR
8280 "unable to create proc dir " TPACPI_PROC_DIR);
8281 thinkpad_acpi_module_exit();
8282 return -ENODEV;
8284 proc_dir->owner = THIS_MODULE;
8286 ret = platform_driver_register(&tpacpi_pdriver);
8287 if (ret) {
8288 printk(TPACPI_ERR
8289 "unable to register main platform driver\n");
8290 thinkpad_acpi_module_exit();
8291 return ret;
8293 tp_features.platform_drv_registered = 1;
8295 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8296 if (ret) {
8297 printk(TPACPI_ERR
8298 "unable to register hwmon platform driver\n");
8299 thinkpad_acpi_module_exit();
8300 return ret;
8302 tp_features.sensors_pdrv_registered = 1;
8304 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8305 if (!ret) {
8306 tp_features.platform_drv_attrs_registered = 1;
8307 ret = tpacpi_create_driver_attributes(
8308 &tpacpi_hwmon_pdriver.driver);
8310 if (ret) {
8311 printk(TPACPI_ERR
8312 "unable to create sysfs driver attributes\n");
8313 thinkpad_acpi_module_exit();
8314 return ret;
8316 tp_features.sensors_pdrv_attrs_registered = 1;
8319 /* Device initialization */
8320 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8321 NULL, 0);
8322 if (IS_ERR(tpacpi_pdev)) {
8323 ret = PTR_ERR(tpacpi_pdev);
8324 tpacpi_pdev = NULL;
8325 printk(TPACPI_ERR "unable to register platform device\n");
8326 thinkpad_acpi_module_exit();
8327 return ret;
8329 tpacpi_sensors_pdev = platform_device_register_simple(
8330 TPACPI_HWMON_DRVR_NAME,
8331 -1, NULL, 0);
8332 if (IS_ERR(tpacpi_sensors_pdev)) {
8333 ret = PTR_ERR(tpacpi_sensors_pdev);
8334 tpacpi_sensors_pdev = NULL;
8335 printk(TPACPI_ERR
8336 "unable to register hwmon platform device\n");
8337 thinkpad_acpi_module_exit();
8338 return ret;
8340 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8341 &dev_attr_thinkpad_acpi_pdev_name);
8342 if (ret) {
8343 printk(TPACPI_ERR
8344 "unable to create sysfs hwmon device attributes\n");
8345 thinkpad_acpi_module_exit();
8346 return ret;
8348 tp_features.sensors_pdev_attrs_registered = 1;
8349 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8350 if (IS_ERR(tpacpi_hwmon)) {
8351 ret = PTR_ERR(tpacpi_hwmon);
8352 tpacpi_hwmon = NULL;
8353 printk(TPACPI_ERR "unable to register hwmon device\n");
8354 thinkpad_acpi_module_exit();
8355 return ret;
8357 mutex_init(&tpacpi_inputdev_send_mutex);
8358 tpacpi_inputdev = input_allocate_device();
8359 if (!tpacpi_inputdev) {
8360 printk(TPACPI_ERR "unable to allocate input device\n");
8361 thinkpad_acpi_module_exit();
8362 return -ENOMEM;
8363 } else {
8364 /* Prepare input device, but don't register */
8365 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8366 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8367 tpacpi_inputdev->id.bustype = BUS_HOST;
8368 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8369 thinkpad_id.vendor :
8370 PCI_VENDOR_ID_IBM;
8371 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8372 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8373 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8375 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8376 ret = ibm_init(&ibms_init[i]);
8377 if (ret >= 0 && *ibms_init[i].param)
8378 ret = ibms_init[i].data->write(ibms_init[i].param);
8379 if (ret < 0) {
8380 thinkpad_acpi_module_exit();
8381 return ret;
8384 ret = input_register_device(tpacpi_inputdev);
8385 if (ret < 0) {
8386 printk(TPACPI_ERR "unable to register input device\n");
8387 thinkpad_acpi_module_exit();
8388 return ret;
8389 } else {
8390 tp_features.input_device_registered = 1;
8393 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8394 return 0;
8397 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8400 * This will autoload the driver in almost every ThinkPad
8401 * in widespread use.
8403 * Only _VERY_ old models, like the 240, 240x and 570 lack
8404 * the HKEY event interface.
8406 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8409 * DMI matching for module autoloading
8411 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8412 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8414 * Only models listed in thinkwiki will be supported, so add yours
8415 * if it is not there yet.
8417 #define IBM_BIOS_MODULE_ALIAS(__type) \
8418 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8420 /* Ancient thinkpad BIOSes have to be identified by
8421 * BIOS type or model number, and there are far less
8422 * BIOS types than model numbers... */
8423 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8425 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8426 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8427 MODULE_DESCRIPTION(TPACPI_DESC);
8428 MODULE_VERSION(TPACPI_VERSION);
8429 MODULE_LICENSE("GPL");
8431 module_init(thinkpad_acpi_module_init);
8432 module_exit(thinkpad_acpi_module_exit);