ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobdc31091e9894bcb2de2184321c9fd40d1c2debfa
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-20091010"
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 __eid1, __eid2, __ev1, __ev2) \
1420 { .vendor = (__v), \
1421 .bios = TPID(__bid1, __bid2), \
1422 .ec = TPID(__eid1, __eid2), \
1423 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1424 | (__bv1) << 8 | (__bv2) }
1426 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1427 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1429 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1430 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1431 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1433 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1434 __eid1, __eid2, __ev1, __ev2) \
1435 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1436 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1438 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1439 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1441 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1442 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1443 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1445 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1446 __eid1, __eid2, __ev1, __ev2) \
1447 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1448 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1450 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1451 /* Numeric models ------------------ */
1452 /* FW MODEL BIOS VERS */
1453 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1454 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1455 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1456 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1457 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1458 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1459 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1460 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1461 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1463 /* A-series ------------------------- */
1464 /* FW MODEL BIOS VERS EC VERS */
1465 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1466 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1467 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1468 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1469 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1470 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1471 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1472 TPV_QI0('1', '3', '2', '0'), /* A22m */
1473 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1474 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1475 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1477 /* G-series ------------------------- */
1478 /* FW MODEL BIOS VERS */
1479 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1480 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1482 /* R-series, T-series --------------- */
1483 /* FW MODEL BIOS VERS EC VERS */
1484 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1485 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1486 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1487 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1488 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1489 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1490 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1491 T40/p, T41/p, T42/p (1) */
1492 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1493 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1494 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1495 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1497 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1498 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1499 TPV_QI0('1', '6', '3', '2'), /* T22 */
1500 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1501 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1502 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1504 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1505 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1506 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1508 /* BIOS FW BIOS VERS EC FW EC VERS */
1509 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1510 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1512 /* X-series ------------------------- */
1513 /* FW MODEL BIOS VERS EC VERS */
1514 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1515 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1516 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1517 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1518 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1519 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1520 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1522 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1523 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1525 /* (0) - older versions lack DMI EC fw string and functionality */
1526 /* (1) - older versions known to lack functionality */
1529 #undef TPV_QL1
1530 #undef TPV_QL0
1531 #undef TPV_QI2
1532 #undef TPV_QI1
1533 #undef TPV_QI0
1534 #undef TPV_Q_X
1535 #undef TPV_Q
1537 static void __init tpacpi_check_outdated_fw(void)
1539 unsigned long fwvers;
1540 u16 ec_version, bios_version;
1542 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1543 ARRAY_SIZE(tpacpi_bios_version_qtable));
1545 if (!fwvers)
1546 return;
1548 bios_version = fwvers & 0xffffU;
1549 ec_version = (fwvers >> 16) & 0xffffU;
1551 /* note that unknown versions are set to 0x0000 and we use that */
1552 if ((bios_version > thinkpad_id.bios_release) ||
1553 (ec_version > thinkpad_id.ec_release &&
1554 ec_version != TPACPI_MATCH_ANY)) {
1556 * The changelogs would let us track down the exact
1557 * reason, but it is just too much of a pain to track
1558 * it. We only list BIOSes that are either really
1559 * broken, or really stable to begin with, so it is
1560 * best if the user upgrades the firmware anyway.
1562 printk(TPACPI_WARN
1563 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1564 printk(TPACPI_WARN
1565 "WARNING: This firmware may be missing critical bug "
1566 "fixes and/or important features\n");
1570 static bool __init tpacpi_is_fw_known(void)
1572 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1573 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1576 /****************************************************************************
1577 ****************************************************************************
1579 * Subdrivers
1581 ****************************************************************************
1582 ****************************************************************************/
1584 /*************************************************************************
1585 * thinkpad-acpi init subdriver
1588 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1590 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1591 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1593 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1594 (thinkpad_id.bios_version_str) ?
1595 thinkpad_id.bios_version_str : "unknown",
1596 (thinkpad_id.ec_version_str) ?
1597 thinkpad_id.ec_version_str : "unknown");
1599 if (thinkpad_id.vendor && thinkpad_id.model_str)
1600 printk(TPACPI_INFO "%s %s, model %s\n",
1601 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1602 "IBM" : ((thinkpad_id.vendor ==
1603 PCI_VENDOR_ID_LENOVO) ?
1604 "Lenovo" : "Unknown vendor"),
1605 thinkpad_id.model_str,
1606 (thinkpad_id.nummodel_str) ?
1607 thinkpad_id.nummodel_str : "unknown");
1609 tpacpi_check_outdated_fw();
1610 return 0;
1613 static int thinkpad_acpi_driver_read(char *p)
1615 int len = 0;
1617 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1618 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1620 return len;
1623 static struct ibm_struct thinkpad_acpi_driver_data = {
1624 .name = "driver",
1625 .read = thinkpad_acpi_driver_read,
1628 /*************************************************************************
1629 * Hotkey subdriver
1633 * ThinkPad firmware event model
1635 * The ThinkPad firmware has two main event interfaces: normal ACPI
1636 * notifications (which follow the ACPI standard), and a private event
1637 * interface.
1639 * The private event interface also issues events for the hotkeys. As
1640 * the driver gained features, the event handling code ended up being
1641 * built around the hotkey subdriver. This will need to be refactored
1642 * to a more formal event API eventually.
1644 * Some "hotkeys" are actually supposed to be used as event reports,
1645 * such as "brightness has changed", "volume has changed", depending on
1646 * the ThinkPad model and how the firmware is operating.
1648 * Unlike other classes, hotkey-class events have mask/unmask control on
1649 * non-ancient firmware. However, how it behaves changes a lot with the
1650 * firmware model and version.
1653 enum { /* hot key scan codes (derived from ACPI DSDT) */
1654 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1655 TP_ACPI_HOTKEYSCAN_FNF2,
1656 TP_ACPI_HOTKEYSCAN_FNF3,
1657 TP_ACPI_HOTKEYSCAN_FNF4,
1658 TP_ACPI_HOTKEYSCAN_FNF5,
1659 TP_ACPI_HOTKEYSCAN_FNF6,
1660 TP_ACPI_HOTKEYSCAN_FNF7,
1661 TP_ACPI_HOTKEYSCAN_FNF8,
1662 TP_ACPI_HOTKEYSCAN_FNF9,
1663 TP_ACPI_HOTKEYSCAN_FNF10,
1664 TP_ACPI_HOTKEYSCAN_FNF11,
1665 TP_ACPI_HOTKEYSCAN_FNF12,
1666 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1667 TP_ACPI_HOTKEYSCAN_FNINSERT,
1668 TP_ACPI_HOTKEYSCAN_FNDELETE,
1669 TP_ACPI_HOTKEYSCAN_FNHOME,
1670 TP_ACPI_HOTKEYSCAN_FNEND,
1671 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1672 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1673 TP_ACPI_HOTKEYSCAN_FNSPACE,
1674 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1675 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1676 TP_ACPI_HOTKEYSCAN_MUTE,
1677 TP_ACPI_HOTKEYSCAN_THINKPAD,
1680 enum { /* Keys/events available through NVRAM polling */
1681 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1682 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1685 enum { /* Positions of some of the keys in hotkey masks */
1686 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1687 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1688 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1689 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1690 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1691 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1692 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1693 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1694 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1695 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1696 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1699 enum { /* NVRAM to ACPI HKEY group map */
1700 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1701 TP_ACPI_HKEY_ZOOM_MASK |
1702 TP_ACPI_HKEY_DISPSWTCH_MASK |
1703 TP_ACPI_HKEY_HIBERNATE_MASK,
1704 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1705 TP_ACPI_HKEY_BRGHTDWN_MASK,
1706 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1707 TP_ACPI_HKEY_VOLDWN_MASK |
1708 TP_ACPI_HKEY_MUTE_MASK,
1711 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1712 struct tp_nvram_state {
1713 u16 thinkpad_toggle:1;
1714 u16 zoom_toggle:1;
1715 u16 display_toggle:1;
1716 u16 thinklight_toggle:1;
1717 u16 hibernate_toggle:1;
1718 u16 displayexp_toggle:1;
1719 u16 display_state:1;
1720 u16 brightness_toggle:1;
1721 u16 volume_toggle:1;
1722 u16 mute:1;
1724 u8 brightness_level;
1725 u8 volume_level;
1728 /* kthread for the hotkey poller */
1729 static struct task_struct *tpacpi_hotkey_task;
1731 /* Acquired while the poller kthread is running, use to sync start/stop */
1732 static struct mutex hotkey_thread_mutex;
1735 * Acquire mutex to write poller control variables as an
1736 * atomic block.
1738 * Increment hotkey_config_change when changing them if you
1739 * want the kthread to forget old state.
1741 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1743 static struct mutex hotkey_thread_data_mutex;
1744 static unsigned int hotkey_config_change;
1747 * hotkey poller control variables
1749 * Must be atomic or readers will also need to acquire mutex
1751 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1752 * should be used only when the changes need to be taken as
1753 * a block, OR when one needs to force the kthread to forget
1754 * old state.
1756 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1757 static unsigned int hotkey_poll_freq = 10; /* Hz */
1759 #define HOTKEY_CONFIG_CRITICAL_START \
1760 do { \
1761 mutex_lock(&hotkey_thread_data_mutex); \
1762 hotkey_config_change++; \
1763 } while (0);
1764 #define HOTKEY_CONFIG_CRITICAL_END \
1765 mutex_unlock(&hotkey_thread_data_mutex);
1767 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1769 #define hotkey_source_mask 0U
1770 #define HOTKEY_CONFIG_CRITICAL_START
1771 #define HOTKEY_CONFIG_CRITICAL_END
1773 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1775 static struct mutex hotkey_mutex;
1777 static enum { /* Reasons for waking up */
1778 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1779 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1780 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1781 } hotkey_wakeup_reason;
1783 static int hotkey_autosleep_ack;
1785 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1786 static u32 hotkey_all_mask; /* all events supported in fw */
1787 static u32 hotkey_reserved_mask; /* events better left disabled */
1788 static u32 hotkey_driver_mask; /* events needed by the driver */
1789 static u32 hotkey_user_mask; /* events visible to userspace */
1790 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1792 static unsigned int hotkey_report_mode;
1794 static u16 *hotkey_keycode_map;
1796 static struct attribute_set *hotkey_dev_attributes;
1798 static void tpacpi_driver_event(const unsigned int hkey_event);
1799 static void hotkey_driver_event(const unsigned int scancode);
1801 /* HKEY.MHKG() return bits */
1802 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1804 static int hotkey_get_wlsw(int *status)
1806 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1807 if (dbg_wlswemul) {
1808 *status = !!tpacpi_wlsw_emulstate;
1809 return 0;
1811 #endif
1812 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1813 return -EIO;
1814 return 0;
1817 static int hotkey_get_tablet_mode(int *status)
1819 int s;
1821 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1822 return -EIO;
1824 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1825 return 0;
1829 * Reads current event mask from firmware, and updates
1830 * hotkey_acpi_mask accordingly. Also resets any bits
1831 * from hotkey_user_mask that are unavailable to be
1832 * delivered (shadow requirement of the userspace ABI).
1834 * Call with hotkey_mutex held
1836 static int hotkey_mask_get(void)
1838 if (tp_features.hotkey_mask) {
1839 u32 m = 0;
1841 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1842 return -EIO;
1844 hotkey_acpi_mask = m;
1845 } else {
1846 /* no mask support doesn't mean no event support... */
1847 hotkey_acpi_mask = hotkey_all_mask;
1850 /* sync userspace-visible mask */
1851 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1853 return 0;
1856 void static hotkey_mask_warn_incomplete_mask(void)
1858 /* log only what the user can fix... */
1859 const u32 wantedmask = hotkey_driver_mask &
1860 ~(hotkey_acpi_mask | hotkey_source_mask) &
1861 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1863 if (wantedmask)
1864 printk(TPACPI_NOTICE
1865 "required events 0x%08x not enabled!\n",
1866 wantedmask);
1870 * Set the firmware mask when supported
1872 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1874 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1876 * Call with hotkey_mutex held
1878 static int hotkey_mask_set(u32 mask)
1880 int i;
1881 int rc = 0;
1883 const u32 fwmask = mask & ~hotkey_source_mask;
1885 if (tp_features.hotkey_mask) {
1886 for (i = 0; i < 32; i++) {
1887 if (!acpi_evalf(hkey_handle,
1888 NULL, "MHKM", "vdd", i + 1,
1889 !!(mask & (1 << i)))) {
1890 rc = -EIO;
1891 break;
1897 * We *must* make an inconditional call to hotkey_mask_get to
1898 * refresh hotkey_acpi_mask and update hotkey_user_mask
1900 * Take the opportunity to also log when we cannot _enable_
1901 * a given event.
1903 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1904 printk(TPACPI_NOTICE
1905 "asked for hotkey mask 0x%08x, but "
1906 "firmware forced it to 0x%08x\n",
1907 fwmask, hotkey_acpi_mask);
1910 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1911 hotkey_mask_warn_incomplete_mask();
1913 return rc;
1917 * Sets hotkey_user_mask and tries to set the firmware mask
1919 * Call with hotkey_mutex held
1921 static int hotkey_user_mask_set(const u32 mask)
1923 int rc;
1925 /* Give people a chance to notice they are doing something that
1926 * is bound to go boom on their users sooner or later */
1927 if (!tp_warned.hotkey_mask_ff &&
1928 (mask == 0xffff || mask == 0xffffff ||
1929 mask == 0xffffffff)) {
1930 tp_warned.hotkey_mask_ff = 1;
1931 printk(TPACPI_NOTICE
1932 "setting the hotkey mask to 0x%08x is likely "
1933 "not the best way to go about it\n", mask);
1934 printk(TPACPI_NOTICE
1935 "please consider using the driver defaults, "
1936 "and refer to up-to-date thinkpad-acpi "
1937 "documentation\n");
1940 /* Try to enable what the user asked for, plus whatever we need.
1941 * this syncs everything but won't enable bits in hotkey_user_mask */
1942 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1944 /* Enable the available bits in hotkey_user_mask */
1945 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1947 return rc;
1951 * Sets the driver hotkey mask.
1953 * Can be called even if the hotkey subdriver is inactive
1955 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1957 int rc;
1959 /* Do the right thing if hotkey_init has not been called yet */
1960 if (!tp_features.hotkey) {
1961 hotkey_driver_mask = mask;
1962 return 0;
1965 mutex_lock(&hotkey_mutex);
1967 HOTKEY_CONFIG_CRITICAL_START
1968 hotkey_driver_mask = mask;
1969 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1970 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1971 #endif
1972 HOTKEY_CONFIG_CRITICAL_END
1974 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1975 ~hotkey_source_mask);
1976 mutex_unlock(&hotkey_mutex);
1978 return rc;
1981 static int hotkey_status_get(int *status)
1983 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1984 return -EIO;
1986 return 0;
1989 static int hotkey_status_set(bool enable)
1991 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1992 return -EIO;
1994 return 0;
1997 static void tpacpi_input_send_tabletsw(void)
1999 int state;
2001 if (tp_features.hotkey_tablet &&
2002 !hotkey_get_tablet_mode(&state)) {
2003 mutex_lock(&tpacpi_inputdev_send_mutex);
2005 input_report_switch(tpacpi_inputdev,
2006 SW_TABLET_MODE, !!state);
2007 input_sync(tpacpi_inputdev);
2009 mutex_unlock(&tpacpi_inputdev_send_mutex);
2013 /* Do NOT call without validating scancode first */
2014 static void tpacpi_input_send_key(const unsigned int scancode)
2016 const unsigned int keycode = hotkey_keycode_map[scancode];
2018 if (keycode != KEY_RESERVED) {
2019 mutex_lock(&tpacpi_inputdev_send_mutex);
2021 input_report_key(tpacpi_inputdev, keycode, 1);
2022 if (keycode == KEY_UNKNOWN)
2023 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2024 scancode);
2025 input_sync(tpacpi_inputdev);
2027 input_report_key(tpacpi_inputdev, keycode, 0);
2028 if (keycode == KEY_UNKNOWN)
2029 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2030 scancode);
2031 input_sync(tpacpi_inputdev);
2033 mutex_unlock(&tpacpi_inputdev_send_mutex);
2037 /* Do NOT call without validating scancode first */
2038 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2040 hotkey_driver_event(scancode);
2041 if (hotkey_user_mask & (1 << scancode))
2042 tpacpi_input_send_key(scancode);
2045 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2046 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2048 /* Do NOT call without validating scancode first */
2049 static void tpacpi_hotkey_send_key(unsigned int scancode)
2051 tpacpi_input_send_key_masked(scancode);
2052 if (hotkey_report_mode < 2) {
2053 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2054 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2058 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2060 u8 d;
2062 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2063 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2064 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2065 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2066 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2067 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2069 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2070 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2071 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2073 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2074 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2075 n->displayexp_toggle =
2076 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2078 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2079 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2080 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2081 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2082 n->brightness_toggle =
2083 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2085 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2086 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2087 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2088 >> TP_NVRAM_POS_LEVEL_VOLUME;
2089 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2090 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2094 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2095 struct tp_nvram_state *newn,
2096 const u32 event_mask)
2099 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2100 do { \
2101 if ((event_mask & (1 << __scancode)) && \
2102 oldn->__member != newn->__member) \
2103 tpacpi_hotkey_send_key(__scancode); \
2104 } while (0)
2106 #define TPACPI_MAY_SEND_KEY(__scancode) \
2107 do { \
2108 if (event_mask & (1 << __scancode)) \
2109 tpacpi_hotkey_send_key(__scancode); \
2110 } while (0)
2112 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2113 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2114 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2115 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2117 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2119 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2121 /* handle volume */
2122 if (oldn->volume_toggle != newn->volume_toggle) {
2123 if (oldn->mute != newn->mute) {
2124 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2126 if (oldn->volume_level > newn->volume_level) {
2127 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2128 } else if (oldn->volume_level < newn->volume_level) {
2129 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2130 } else if (oldn->mute == newn->mute) {
2131 /* repeated key presses that didn't change state */
2132 if (newn->mute) {
2133 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2134 } else if (newn->volume_level != 0) {
2135 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2136 } else {
2137 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2142 /* handle brightness */
2143 if (oldn->brightness_toggle != newn->brightness_toggle) {
2144 if (oldn->brightness_level < newn->brightness_level) {
2145 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2146 } else if (oldn->brightness_level > newn->brightness_level) {
2147 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2148 } else {
2149 /* repeated key presses that didn't change state */
2150 if (newn->brightness_level != 0) {
2151 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2152 } else {
2153 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2158 #undef TPACPI_COMPARE_KEY
2159 #undef TPACPI_MAY_SEND_KEY
2163 * Polling driver
2165 * We track all events in hotkey_source_mask all the time, since
2166 * most of them are edge-based. We only issue those requested by
2167 * hotkey_user_mask or hotkey_driver_mask, though.
2169 static int hotkey_kthread(void *data)
2171 struct tp_nvram_state s[2];
2172 u32 poll_mask, event_mask;
2173 unsigned int si, so;
2174 unsigned long t;
2175 unsigned int change_detector, must_reset;
2176 unsigned int poll_freq;
2178 mutex_lock(&hotkey_thread_mutex);
2180 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2181 goto exit;
2183 set_freezable();
2185 so = 0;
2186 si = 1;
2187 t = 0;
2189 /* Initial state for compares */
2190 mutex_lock(&hotkey_thread_data_mutex);
2191 change_detector = hotkey_config_change;
2192 poll_mask = hotkey_source_mask;
2193 event_mask = hotkey_source_mask &
2194 (hotkey_driver_mask | hotkey_user_mask);
2195 poll_freq = hotkey_poll_freq;
2196 mutex_unlock(&hotkey_thread_data_mutex);
2197 hotkey_read_nvram(&s[so], poll_mask);
2199 while (!kthread_should_stop()) {
2200 if (t == 0) {
2201 if (likely(poll_freq))
2202 t = 1000/poll_freq;
2203 else
2204 t = 100; /* should never happen... */
2206 t = msleep_interruptible(t);
2207 if (unlikely(kthread_should_stop()))
2208 break;
2209 must_reset = try_to_freeze();
2210 if (t > 0 && !must_reset)
2211 continue;
2213 mutex_lock(&hotkey_thread_data_mutex);
2214 if (must_reset || hotkey_config_change != change_detector) {
2215 /* forget old state on thaw or config change */
2216 si = so;
2217 t = 0;
2218 change_detector = hotkey_config_change;
2220 poll_mask = hotkey_source_mask;
2221 event_mask = hotkey_source_mask &
2222 (hotkey_driver_mask | hotkey_user_mask);
2223 poll_freq = hotkey_poll_freq;
2224 mutex_unlock(&hotkey_thread_data_mutex);
2226 if (likely(poll_mask)) {
2227 hotkey_read_nvram(&s[si], poll_mask);
2228 if (likely(si != so)) {
2229 hotkey_compare_and_issue_event(&s[so], &s[si],
2230 event_mask);
2234 so = si;
2235 si ^= 1;
2238 exit:
2239 mutex_unlock(&hotkey_thread_mutex);
2240 return 0;
2243 /* call with hotkey_mutex held */
2244 static void hotkey_poll_stop_sync(void)
2246 if (tpacpi_hotkey_task) {
2247 if (frozen(tpacpi_hotkey_task) ||
2248 freezing(tpacpi_hotkey_task))
2249 thaw_process(tpacpi_hotkey_task);
2251 kthread_stop(tpacpi_hotkey_task);
2252 tpacpi_hotkey_task = NULL;
2253 mutex_lock(&hotkey_thread_mutex);
2254 /* at this point, the thread did exit */
2255 mutex_unlock(&hotkey_thread_mutex);
2259 /* call with hotkey_mutex held */
2260 static void hotkey_poll_setup(bool may_warn)
2262 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2263 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2265 if (hotkey_poll_freq > 0 &&
2266 (poll_driver_mask ||
2267 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2268 if (!tpacpi_hotkey_task) {
2269 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2270 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2271 if (IS_ERR(tpacpi_hotkey_task)) {
2272 tpacpi_hotkey_task = NULL;
2273 printk(TPACPI_ERR
2274 "could not create kernel thread "
2275 "for hotkey polling\n");
2278 } else {
2279 hotkey_poll_stop_sync();
2280 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2281 hotkey_poll_freq == 0) {
2282 printk(TPACPI_NOTICE
2283 "hot keys 0x%08x and/or events 0x%08x "
2284 "require polling, which is currently "
2285 "disabled\n",
2286 poll_user_mask, poll_driver_mask);
2291 static void hotkey_poll_setup_safe(bool may_warn)
2293 mutex_lock(&hotkey_mutex);
2294 hotkey_poll_setup(may_warn);
2295 mutex_unlock(&hotkey_mutex);
2298 /* call with hotkey_mutex held */
2299 static void hotkey_poll_set_freq(unsigned int freq)
2301 if (!freq)
2302 hotkey_poll_stop_sync();
2304 hotkey_poll_freq = freq;
2307 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2309 static void hotkey_poll_setup_safe(bool __unused)
2313 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2315 static int hotkey_inputdev_open(struct input_dev *dev)
2317 switch (tpacpi_lifecycle) {
2318 case TPACPI_LIFE_INIT:
2320 * hotkey_init will call hotkey_poll_setup_safe
2321 * at the appropriate moment
2323 return 0;
2324 case TPACPI_LIFE_EXITING:
2325 return -EBUSY;
2326 case TPACPI_LIFE_RUNNING:
2327 hotkey_poll_setup_safe(false);
2328 return 0;
2331 /* Should only happen if tpacpi_lifecycle is corrupt */
2332 BUG();
2333 return -EBUSY;
2336 static void hotkey_inputdev_close(struct input_dev *dev)
2338 /* disable hotkey polling when possible */
2339 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2340 !(hotkey_source_mask & hotkey_driver_mask))
2341 hotkey_poll_setup_safe(false);
2344 /* sysfs hotkey enable ------------------------------------------------- */
2345 static ssize_t hotkey_enable_show(struct device *dev,
2346 struct device_attribute *attr,
2347 char *buf)
2349 int res, status;
2351 printk_deprecated_attribute("hotkey_enable",
2352 "Hotkey reporting is always enabled");
2354 res = hotkey_status_get(&status);
2355 if (res)
2356 return res;
2358 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2361 static ssize_t hotkey_enable_store(struct device *dev,
2362 struct device_attribute *attr,
2363 const char *buf, size_t count)
2365 unsigned long t;
2367 printk_deprecated_attribute("hotkey_enable",
2368 "Hotkeys can be disabled through hotkey_mask");
2370 if (parse_strtoul(buf, 1, &t))
2371 return -EINVAL;
2373 if (t == 0)
2374 return -EPERM;
2376 return count;
2379 static struct device_attribute dev_attr_hotkey_enable =
2380 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2381 hotkey_enable_show, hotkey_enable_store);
2383 /* sysfs hotkey mask --------------------------------------------------- */
2384 static ssize_t hotkey_mask_show(struct device *dev,
2385 struct device_attribute *attr,
2386 char *buf)
2388 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2391 static ssize_t hotkey_mask_store(struct device *dev,
2392 struct device_attribute *attr,
2393 const char *buf, size_t count)
2395 unsigned long t;
2396 int res;
2398 if (parse_strtoul(buf, 0xffffffffUL, &t))
2399 return -EINVAL;
2401 if (mutex_lock_killable(&hotkey_mutex))
2402 return -ERESTARTSYS;
2404 res = hotkey_user_mask_set(t);
2406 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2407 hotkey_poll_setup(true);
2408 #endif
2410 mutex_unlock(&hotkey_mutex);
2412 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2414 return (res) ? res : count;
2417 static struct device_attribute dev_attr_hotkey_mask =
2418 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2419 hotkey_mask_show, hotkey_mask_store);
2421 /* sysfs hotkey bios_enabled ------------------------------------------- */
2422 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2423 struct device_attribute *attr,
2424 char *buf)
2426 return sprintf(buf, "0\n");
2429 static struct device_attribute dev_attr_hotkey_bios_enabled =
2430 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2432 /* sysfs hotkey bios_mask ---------------------------------------------- */
2433 static ssize_t hotkey_bios_mask_show(struct device *dev,
2434 struct device_attribute *attr,
2435 char *buf)
2437 printk_deprecated_attribute("hotkey_bios_mask",
2438 "This attribute is useless.");
2439 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2442 static struct device_attribute dev_attr_hotkey_bios_mask =
2443 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2445 /* sysfs hotkey all_mask ----------------------------------------------- */
2446 static ssize_t hotkey_all_mask_show(struct device *dev,
2447 struct device_attribute *attr,
2448 char *buf)
2450 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2451 hotkey_all_mask | hotkey_source_mask);
2454 static struct device_attribute dev_attr_hotkey_all_mask =
2455 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2457 /* sysfs hotkey recommended_mask --------------------------------------- */
2458 static ssize_t hotkey_recommended_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)
2464 & ~hotkey_reserved_mask);
2467 static struct device_attribute dev_attr_hotkey_recommended_mask =
2468 __ATTR(hotkey_recommended_mask, S_IRUGO,
2469 hotkey_recommended_mask_show, NULL);
2471 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2473 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2474 static ssize_t hotkey_source_mask_show(struct device *dev,
2475 struct device_attribute *attr,
2476 char *buf)
2478 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2481 static ssize_t hotkey_source_mask_store(struct device *dev,
2482 struct device_attribute *attr,
2483 const char *buf, size_t count)
2485 unsigned long t;
2486 u32 r_ev;
2487 int rc;
2489 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2490 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2491 return -EINVAL;
2493 if (mutex_lock_killable(&hotkey_mutex))
2494 return -ERESTARTSYS;
2496 HOTKEY_CONFIG_CRITICAL_START
2497 hotkey_source_mask = t;
2498 HOTKEY_CONFIG_CRITICAL_END
2500 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2501 ~hotkey_source_mask);
2502 hotkey_poll_setup(true);
2504 /* check if events needed by the driver got disabled */
2505 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2506 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2508 mutex_unlock(&hotkey_mutex);
2510 if (rc < 0)
2511 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2512 "firmware event mask!\n");
2514 if (r_ev)
2515 printk(TPACPI_NOTICE "hotkey_source_mask: "
2516 "some important events were disabled: "
2517 "0x%04x\n", r_ev);
2519 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2521 return (rc < 0) ? rc : count;
2524 static struct device_attribute dev_attr_hotkey_source_mask =
2525 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2526 hotkey_source_mask_show, hotkey_source_mask_store);
2528 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2529 static ssize_t hotkey_poll_freq_show(struct device *dev,
2530 struct device_attribute *attr,
2531 char *buf)
2533 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2536 static ssize_t hotkey_poll_freq_store(struct device *dev,
2537 struct device_attribute *attr,
2538 const char *buf, size_t count)
2540 unsigned long t;
2542 if (parse_strtoul(buf, 25, &t))
2543 return -EINVAL;
2545 if (mutex_lock_killable(&hotkey_mutex))
2546 return -ERESTARTSYS;
2548 hotkey_poll_set_freq(t);
2549 hotkey_poll_setup(true);
2551 mutex_unlock(&hotkey_mutex);
2553 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2555 return count;
2558 static struct device_attribute dev_attr_hotkey_poll_freq =
2559 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2560 hotkey_poll_freq_show, hotkey_poll_freq_store);
2562 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2564 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2565 static ssize_t hotkey_radio_sw_show(struct device *dev,
2566 struct device_attribute *attr,
2567 char *buf)
2569 int res, s;
2570 res = hotkey_get_wlsw(&s);
2571 if (res < 0)
2572 return res;
2574 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2577 static struct device_attribute dev_attr_hotkey_radio_sw =
2578 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2580 static void hotkey_radio_sw_notify_change(void)
2582 if (tp_features.hotkey_wlsw)
2583 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2584 "hotkey_radio_sw");
2587 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2588 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2589 struct device_attribute *attr,
2590 char *buf)
2592 int res, s;
2593 res = hotkey_get_tablet_mode(&s);
2594 if (res < 0)
2595 return res;
2597 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2600 static struct device_attribute dev_attr_hotkey_tablet_mode =
2601 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2603 static void hotkey_tablet_mode_notify_change(void)
2605 if (tp_features.hotkey_tablet)
2606 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2607 "hotkey_tablet_mode");
2610 /* sysfs hotkey report_mode -------------------------------------------- */
2611 static ssize_t hotkey_report_mode_show(struct device *dev,
2612 struct device_attribute *attr,
2613 char *buf)
2615 return snprintf(buf, PAGE_SIZE, "%d\n",
2616 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2619 static struct device_attribute dev_attr_hotkey_report_mode =
2620 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2622 /* sysfs wakeup reason (pollable) -------------------------------------- */
2623 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2627 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2630 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2631 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2633 static void hotkey_wakeup_reason_notify_change(void)
2635 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2636 "wakeup_reason");
2639 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2640 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2641 struct device_attribute *attr,
2642 char *buf)
2644 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2647 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2648 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2649 hotkey_wakeup_hotunplug_complete_show, NULL);
2651 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2653 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2654 "wakeup_hotunplug_complete");
2657 /* --------------------------------------------------------------------- */
2659 static struct attribute *hotkey_attributes[] __initdata = {
2660 &dev_attr_hotkey_enable.attr,
2661 &dev_attr_hotkey_bios_enabled.attr,
2662 &dev_attr_hotkey_bios_mask.attr,
2663 &dev_attr_hotkey_report_mode.attr,
2664 &dev_attr_hotkey_wakeup_reason.attr,
2665 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2666 &dev_attr_hotkey_mask.attr,
2667 &dev_attr_hotkey_all_mask.attr,
2668 &dev_attr_hotkey_recommended_mask.attr,
2669 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2670 &dev_attr_hotkey_source_mask.attr,
2671 &dev_attr_hotkey_poll_freq.attr,
2672 #endif
2675 static void bluetooth_update_rfk(void);
2676 static void wan_update_rfk(void);
2677 static void uwb_update_rfk(void);
2678 static void tpacpi_send_radiosw_update(void)
2680 int wlsw;
2682 /* Sync these BEFORE sending any rfkill events */
2683 if (tp_features.bluetooth)
2684 bluetooth_update_rfk();
2685 if (tp_features.wan)
2686 wan_update_rfk();
2687 if (tp_features.uwb)
2688 uwb_update_rfk();
2690 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2691 mutex_lock(&tpacpi_inputdev_send_mutex);
2693 input_report_switch(tpacpi_inputdev,
2694 SW_RFKILL_ALL, !!wlsw);
2695 input_sync(tpacpi_inputdev);
2697 mutex_unlock(&tpacpi_inputdev_send_mutex);
2699 hotkey_radio_sw_notify_change();
2702 static void hotkey_exit(void)
2704 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2705 mutex_lock(&hotkey_mutex);
2706 hotkey_poll_stop_sync();
2707 mutex_unlock(&hotkey_mutex);
2708 #endif
2710 if (hotkey_dev_attributes)
2711 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2713 kfree(hotkey_keycode_map);
2715 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2716 "restoring original HKEY status and mask\n");
2717 /* yes, there is a bitwise or below, we want the
2718 * functions to be called even if one of them fail */
2719 if (((tp_features.hotkey_mask &&
2720 hotkey_mask_set(hotkey_orig_mask)) |
2721 hotkey_status_set(false)) != 0)
2722 printk(TPACPI_ERR
2723 "failed to restore hot key mask "
2724 "to BIOS defaults\n");
2727 static void __init hotkey_unmap(const unsigned int scancode)
2729 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2730 clear_bit(hotkey_keycode_map[scancode],
2731 tpacpi_inputdev->keybit);
2732 hotkey_keycode_map[scancode] = KEY_RESERVED;
2737 * HKEY quirks:
2738 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2741 #define TPACPI_HK_Q_INIMASK 0x0001
2743 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2744 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2745 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2746 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2747 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2748 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2749 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2750 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2751 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2752 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2753 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2754 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2755 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2756 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2757 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2758 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2759 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2760 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2761 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2762 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2765 static int __init hotkey_init(struct ibm_init_struct *iibm)
2767 /* Requirements for changing the default keymaps:
2769 * 1. Many of the keys are mapped to KEY_RESERVED for very
2770 * good reasons. Do not change them unless you have deep
2771 * knowledge on the IBM and Lenovo ThinkPad firmware for
2772 * the various ThinkPad models. The driver behaves
2773 * differently for KEY_RESERVED: such keys have their
2774 * hot key mask *unset* in mask_recommended, and also
2775 * in the initial hot key mask programmed into the
2776 * firmware at driver load time, which means the firm-
2777 * ware may react very differently if you change them to
2778 * something else;
2780 * 2. You must be subscribed to the linux-thinkpad and
2781 * ibm-acpi-devel mailing lists, and you should read the
2782 * list archives since 2007 if you want to change the
2783 * keymaps. This requirement exists so that you will
2784 * know the past history of problems with the thinkpad-
2785 * acpi driver keymaps, and also that you will be
2786 * listening to any bug reports;
2788 * 3. Do not send thinkpad-acpi specific patches directly to
2789 * for merging, *ever*. Send them to the linux-acpi
2790 * mailinglist for comments. Merging is to be done only
2791 * through acpi-test and the ACPI maintainer.
2793 * If the above is too much to ask, don't change the keymap.
2794 * Ask the thinkpad-acpi maintainer to do it, instead.
2796 static u16 ibm_keycode_map[] __initdata = {
2797 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2798 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2799 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2800 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2802 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2803 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2804 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2805 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2807 /* brightness: firmware always reacts to them */
2808 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2809 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2811 /* Thinklight: firmware always react to it */
2812 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2814 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2815 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2817 /* Volume: firmware always react to it and reprograms
2818 * the built-in *extra* mixer. Never map it to control
2819 * another mixer by default. */
2820 KEY_RESERVED, /* 0x14: VOLUME UP */
2821 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2822 KEY_RESERVED, /* 0x16: MUTE */
2824 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2826 /* (assignments unknown, please report if found) */
2827 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2828 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2830 static u16 lenovo_keycode_map[] __initdata = {
2831 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2832 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2833 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2834 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2836 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2837 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2838 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2839 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2841 /* These should be enabled --only-- when ACPI video
2842 * is disabled (i.e. in "vendor" mode), and are handled
2843 * in a special way by the init code */
2844 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2845 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2847 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2849 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2850 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2852 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2853 * react to it and reprograms the built-in *extra* mixer.
2854 * Never map it to control another mixer by default.
2856 * T60?, T61, R60?, R61: firmware and EC tries to send
2857 * these over the regular keyboard, so these are no-ops,
2858 * but there are still weird bugs re. MUTE, so do not
2859 * change unless you get test reports from all Lenovo
2860 * models. May cause the BIOS to interfere with the
2861 * HDA mixer.
2863 KEY_RESERVED, /* 0x14: VOLUME UP */
2864 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2865 KEY_RESERVED, /* 0x16: MUTE */
2867 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2869 /* (assignments unknown, please report if found) */
2870 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2871 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2874 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2875 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2876 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2878 int res, i;
2879 int status;
2880 int hkeyv;
2882 unsigned long quirks;
2884 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2885 "initializing hotkey subdriver\n");
2887 BUG_ON(!tpacpi_inputdev);
2888 BUG_ON(tpacpi_inputdev->open != NULL ||
2889 tpacpi_inputdev->close != NULL);
2891 TPACPI_ACPIHANDLE_INIT(hkey);
2892 mutex_init(&hotkey_mutex);
2894 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2895 mutex_init(&hotkey_thread_mutex);
2896 mutex_init(&hotkey_thread_data_mutex);
2897 #endif
2899 /* hotkey not supported on 570 */
2900 tp_features.hotkey = hkey_handle != NULL;
2902 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2903 "hotkeys are %s\n",
2904 str_supported(tp_features.hotkey));
2906 if (!tp_features.hotkey)
2907 return 1;
2909 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2910 ARRAY_SIZE(tpacpi_hotkey_qtable));
2912 tpacpi_disable_brightness_delay();
2914 /* MUST have enough space for all attributes to be added to
2915 * hotkey_dev_attributes */
2916 hotkey_dev_attributes = create_attr_set(
2917 ARRAY_SIZE(hotkey_attributes) + 2,
2918 NULL);
2919 if (!hotkey_dev_attributes)
2920 return -ENOMEM;
2921 res = add_many_to_attr_set(hotkey_dev_attributes,
2922 hotkey_attributes,
2923 ARRAY_SIZE(hotkey_attributes));
2924 if (res)
2925 goto err_exit;
2927 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2928 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2929 for HKEY interface version 0x100 */
2930 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2931 if ((hkeyv >> 8) != 1) {
2932 printk(TPACPI_ERR "unknown version of the "
2933 "HKEY interface: 0x%x\n", hkeyv);
2934 printk(TPACPI_ERR "please report this to %s\n",
2935 TPACPI_MAIL);
2936 } else {
2938 * MHKV 0x100 in A31, R40, R40e,
2939 * T4x, X31, and later
2941 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2942 "firmware HKEY interface version: 0x%x\n",
2943 hkeyv);
2945 /* Paranoia check AND init hotkey_all_mask */
2946 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2947 "MHKA", "qd")) {
2948 printk(TPACPI_ERR
2949 "missing MHKA handler, "
2950 "please report this to %s\n",
2951 TPACPI_MAIL);
2952 /* Fallback: pre-init for FN+F3,F4,F12 */
2953 hotkey_all_mask = 0x080cU;
2954 } else {
2955 tp_features.hotkey_mask = 1;
2960 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2961 "hotkey masks are %s\n",
2962 str_supported(tp_features.hotkey_mask));
2964 /* Init hotkey_all_mask if not initialized yet */
2965 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2966 (quirks & TPACPI_HK_Q_INIMASK))
2967 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2969 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2970 if (tp_features.hotkey_mask) {
2971 /* hotkey_source_mask *must* be zero for
2972 * the first hotkey_mask_get to return hotkey_orig_mask */
2973 res = hotkey_mask_get();
2974 if (res)
2975 goto err_exit;
2977 hotkey_orig_mask = hotkey_acpi_mask;
2978 } else {
2979 hotkey_orig_mask = hotkey_all_mask;
2980 hotkey_acpi_mask = hotkey_all_mask;
2983 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2984 if (dbg_wlswemul) {
2985 tp_features.hotkey_wlsw = 1;
2986 printk(TPACPI_INFO
2987 "radio switch emulation enabled\n");
2988 } else
2989 #endif
2990 /* Not all thinkpads have a hardware radio switch */
2991 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2992 tp_features.hotkey_wlsw = 1;
2993 printk(TPACPI_INFO
2994 "radio switch found; radios are %s\n",
2995 enabled(status, 0));
2997 if (tp_features.hotkey_wlsw)
2998 res = add_to_attr_set(hotkey_dev_attributes,
2999 &dev_attr_hotkey_radio_sw.attr);
3001 /* For X41t, X60t, X61t Tablets... */
3002 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3003 tp_features.hotkey_tablet = 1;
3004 printk(TPACPI_INFO
3005 "possible tablet mode switch found; "
3006 "ThinkPad in %s mode\n",
3007 (status & TP_HOTKEY_TABLET_MASK)?
3008 "tablet" : "laptop");
3009 res = add_to_attr_set(hotkey_dev_attributes,
3010 &dev_attr_hotkey_tablet_mode.attr);
3013 if (!res)
3014 res = register_attr_set_with_sysfs(
3015 hotkey_dev_attributes,
3016 &tpacpi_pdev->dev.kobj);
3017 if (res)
3018 goto err_exit;
3020 /* Set up key map */
3022 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3023 GFP_KERNEL);
3024 if (!hotkey_keycode_map) {
3025 printk(TPACPI_ERR
3026 "failed to allocate memory for key map\n");
3027 res = -ENOMEM;
3028 goto err_exit;
3031 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3032 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3033 "using Lenovo default hot key map\n");
3034 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3035 TPACPI_HOTKEY_MAP_SIZE);
3036 } else {
3037 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3038 "using IBM default hot key map\n");
3039 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3040 TPACPI_HOTKEY_MAP_SIZE);
3043 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3044 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3045 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3046 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3047 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3048 tpacpi_inputdev->keycode = hotkey_keycode_map;
3049 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3050 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3051 set_bit(hotkey_keycode_map[i],
3052 tpacpi_inputdev->keybit);
3053 } else {
3054 if (i < sizeof(hotkey_reserved_mask)*8)
3055 hotkey_reserved_mask |= 1 << i;
3059 if (tp_features.hotkey_wlsw) {
3060 set_bit(EV_SW, tpacpi_inputdev->evbit);
3061 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3063 if (tp_features.hotkey_tablet) {
3064 set_bit(EV_SW, tpacpi_inputdev->evbit);
3065 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3068 /* Do not issue duplicate brightness change events to
3069 * userspace */
3070 if (!tp_features.bright_acpimode)
3071 /* update bright_acpimode... */
3072 tpacpi_check_std_acpi_brightness_support();
3074 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3075 printk(TPACPI_INFO
3076 "This ThinkPad has standard ACPI backlight "
3077 "brightness control, supported by the ACPI "
3078 "video driver\n");
3079 printk(TPACPI_NOTICE
3080 "Disabling thinkpad-acpi brightness events "
3081 "by default...\n");
3083 /* Disable brightness up/down on Lenovo thinkpads when
3084 * ACPI is handling them, otherwise it is plain impossible
3085 * for userspace to do something even remotely sane */
3086 hotkey_reserved_mask |=
3087 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3088 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3089 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3090 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3093 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3094 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3095 & ~hotkey_all_mask
3096 & ~hotkey_reserved_mask;
3098 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3099 "hotkey source mask 0x%08x, polling freq %u\n",
3100 hotkey_source_mask, hotkey_poll_freq);
3101 #endif
3103 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3104 "enabling firmware HKEY event interface...\n");
3105 res = hotkey_status_set(true);
3106 if (res) {
3107 hotkey_exit();
3108 return res;
3110 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3111 | hotkey_driver_mask)
3112 & ~hotkey_source_mask);
3113 if (res < 0 && res != -ENXIO) {
3114 hotkey_exit();
3115 return res;
3117 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3118 & ~hotkey_reserved_mask;
3119 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3120 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3121 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3123 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3124 "legacy ibm/hotkey event reporting over procfs %s\n",
3125 (hotkey_report_mode < 2) ?
3126 "enabled" : "disabled");
3128 tpacpi_inputdev->open = &hotkey_inputdev_open;
3129 tpacpi_inputdev->close = &hotkey_inputdev_close;
3131 hotkey_poll_setup_safe(true);
3132 tpacpi_send_radiosw_update();
3133 tpacpi_input_send_tabletsw();
3135 return 0;
3137 err_exit:
3138 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3139 hotkey_dev_attributes = NULL;
3141 return (res < 0)? res : 1;
3144 static bool hotkey_notify_hotkey(const u32 hkey,
3145 bool *send_acpi_ev,
3146 bool *ignore_acpi_ev)
3148 /* 0x1000-0x1FFF: key presses */
3149 unsigned int scancode = hkey & 0xfff;
3150 *send_acpi_ev = true;
3151 *ignore_acpi_ev = false;
3153 if (scancode > 0 && scancode < 0x21) {
3154 scancode--;
3155 if (!(hotkey_source_mask & (1 << scancode))) {
3156 tpacpi_input_send_key_masked(scancode);
3157 *send_acpi_ev = false;
3158 } else {
3159 *ignore_acpi_ev = true;
3161 return true;
3163 return false;
3166 static bool hotkey_notify_wakeup(const u32 hkey,
3167 bool *send_acpi_ev,
3168 bool *ignore_acpi_ev)
3170 /* 0x2000-0x2FFF: Wakeup reason */
3171 *send_acpi_ev = true;
3172 *ignore_acpi_ev = false;
3174 switch (hkey) {
3175 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3176 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3177 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3178 *ignore_acpi_ev = true;
3179 break;
3181 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3182 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3183 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3184 *ignore_acpi_ev = true;
3185 break;
3187 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3188 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3189 printk(TPACPI_ALERT
3190 "EMERGENCY WAKEUP: battery almost empty\n");
3191 /* how to auto-heal: */
3192 /* 2313: woke up from S3, go to S4/S5 */
3193 /* 2413: woke up from S4, go to S5 */
3194 break;
3196 default:
3197 return false;
3200 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3201 printk(TPACPI_INFO
3202 "woke up due to a hot-unplug "
3203 "request...\n");
3204 hotkey_wakeup_reason_notify_change();
3206 return true;
3209 static bool hotkey_notify_usrevent(const u32 hkey,
3210 bool *send_acpi_ev,
3211 bool *ignore_acpi_ev)
3213 /* 0x5000-0x5FFF: human interface helpers */
3214 *send_acpi_ev = true;
3215 *ignore_acpi_ev = false;
3217 switch (hkey) {
3218 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3219 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3220 return true;
3222 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3223 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3224 tpacpi_input_send_tabletsw();
3225 hotkey_tablet_mode_notify_change();
3226 *send_acpi_ev = false;
3227 return true;
3229 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3230 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3231 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3232 /* do not propagate these events */
3233 *ignore_acpi_ev = true;
3234 return true;
3236 default:
3237 return false;
3241 static bool hotkey_notify_thermal(const u32 hkey,
3242 bool *send_acpi_ev,
3243 bool *ignore_acpi_ev)
3245 /* 0x6000-0x6FFF: thermal alarms */
3246 *send_acpi_ev = true;
3247 *ignore_acpi_ev = false;
3249 switch (hkey) {
3250 case TP_HKEY_EV_ALARM_BAT_HOT:
3251 printk(TPACPI_CRIT
3252 "THERMAL ALARM: battery is too hot!\n");
3253 /* recommended action: warn user through gui */
3254 return true;
3255 case TP_HKEY_EV_ALARM_BAT_XHOT:
3256 printk(TPACPI_ALERT
3257 "THERMAL EMERGENCY: battery is extremely hot!\n");
3258 /* recommended action: immediate sleep/hibernate */
3259 return true;
3260 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3261 printk(TPACPI_CRIT
3262 "THERMAL ALARM: "
3263 "a sensor reports something is too hot!\n");
3264 /* recommended action: warn user through gui, that */
3265 /* some internal component is too hot */
3266 return true;
3267 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3268 printk(TPACPI_ALERT
3269 "THERMAL EMERGENCY: "
3270 "a sensor reports something is extremely hot!\n");
3271 /* recommended action: immediate sleep/hibernate */
3272 return true;
3273 case TP_HKEY_EV_THM_TABLE_CHANGED:
3274 printk(TPACPI_INFO
3275 "EC reports that Thermal Table has changed\n");
3276 /* recommended action: do nothing, we don't have
3277 * Lenovo ATM information */
3278 return true;
3279 default:
3280 printk(TPACPI_ALERT
3281 "THERMAL ALERT: unknown thermal alarm received\n");
3282 return false;
3286 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3288 u32 hkey;
3289 bool send_acpi_ev;
3290 bool ignore_acpi_ev;
3291 bool known_ev;
3293 if (event != 0x80) {
3294 printk(TPACPI_ERR
3295 "unknown HKEY notification event %d\n", event);
3296 /* forward it to userspace, maybe it knows how to handle it */
3297 acpi_bus_generate_netlink_event(
3298 ibm->acpi->device->pnp.device_class,
3299 dev_name(&ibm->acpi->device->dev),
3300 event, 0);
3301 return;
3304 while (1) {
3305 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3306 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3307 return;
3310 if (hkey == 0) {
3311 /* queue empty */
3312 return;
3315 send_acpi_ev = true;
3316 ignore_acpi_ev = false;
3318 switch (hkey >> 12) {
3319 case 1:
3320 /* 0x1000-0x1FFF: key presses */
3321 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3322 &ignore_acpi_ev);
3323 break;
3324 case 2:
3325 /* 0x2000-0x2FFF: Wakeup reason */
3326 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3327 &ignore_acpi_ev);
3328 break;
3329 case 3:
3330 /* 0x3000-0x3FFF: bay-related wakeups */
3331 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3332 hotkey_autosleep_ack = 1;
3333 printk(TPACPI_INFO
3334 "bay ejected\n");
3335 hotkey_wakeup_hotunplug_complete_notify_change();
3336 known_ev = true;
3337 } else {
3338 known_ev = false;
3340 break;
3341 case 4:
3342 /* 0x4000-0x4FFF: dock-related wakeups */
3343 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3344 hotkey_autosleep_ack = 1;
3345 printk(TPACPI_INFO
3346 "undocked\n");
3347 hotkey_wakeup_hotunplug_complete_notify_change();
3348 known_ev = true;
3349 } else {
3350 known_ev = false;
3352 break;
3353 case 5:
3354 /* 0x5000-0x5FFF: human interface helpers */
3355 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3356 &ignore_acpi_ev);
3357 break;
3358 case 6:
3359 /* 0x6000-0x6FFF: thermal alarms */
3360 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3361 &ignore_acpi_ev);
3362 break;
3363 case 7:
3364 /* 0x7000-0x7FFF: misc */
3365 if (tp_features.hotkey_wlsw &&
3366 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3367 tpacpi_send_radiosw_update();
3368 send_acpi_ev = 0;
3369 known_ev = true;
3370 break;
3372 /* fallthrough to default */
3373 default:
3374 known_ev = false;
3376 if (!known_ev) {
3377 printk(TPACPI_NOTICE
3378 "unhandled HKEY event 0x%04x\n", hkey);
3379 printk(TPACPI_NOTICE
3380 "please report the conditions when this "
3381 "event happened to %s\n", TPACPI_MAIL);
3384 /* Legacy events */
3385 if (!ignore_acpi_ev &&
3386 (send_acpi_ev || hotkey_report_mode < 2)) {
3387 acpi_bus_generate_proc_event(ibm->acpi->device,
3388 event, hkey);
3391 /* netlink events */
3392 if (!ignore_acpi_ev && send_acpi_ev) {
3393 acpi_bus_generate_netlink_event(
3394 ibm->acpi->device->pnp.device_class,
3395 dev_name(&ibm->acpi->device->dev),
3396 event, hkey);
3401 static void hotkey_suspend(pm_message_t state)
3403 /* Do these on suspend, we get the events on early resume! */
3404 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3405 hotkey_autosleep_ack = 0;
3408 static void hotkey_resume(void)
3410 tpacpi_disable_brightness_delay();
3412 if (hotkey_status_set(true) < 0 ||
3413 hotkey_mask_set(hotkey_acpi_mask) < 0)
3414 printk(TPACPI_ERR
3415 "error while attempting to reset the event "
3416 "firmware interface\n");
3418 tpacpi_send_radiosw_update();
3419 hotkey_tablet_mode_notify_change();
3420 hotkey_wakeup_reason_notify_change();
3421 hotkey_wakeup_hotunplug_complete_notify_change();
3422 hotkey_poll_setup_safe(false);
3425 /* procfs -------------------------------------------------------------- */
3426 static int hotkey_read(char *p)
3428 int res, status;
3429 int len = 0;
3431 if (!tp_features.hotkey) {
3432 len += sprintf(p + len, "status:\t\tnot supported\n");
3433 return len;
3436 if (mutex_lock_killable(&hotkey_mutex))
3437 return -ERESTARTSYS;
3438 res = hotkey_status_get(&status);
3439 if (!res)
3440 res = hotkey_mask_get();
3441 mutex_unlock(&hotkey_mutex);
3442 if (res)
3443 return res;
3445 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3446 if (hotkey_all_mask) {
3447 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3448 len += sprintf(p + len,
3449 "commands:\tenable, disable, reset, <mask>\n");
3450 } else {
3451 len += sprintf(p + len, "mask:\t\tnot supported\n");
3452 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3455 return len;
3458 static void hotkey_enabledisable_warn(bool enable)
3460 tpacpi_log_usertask("procfs hotkey enable/disable");
3461 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3462 TPACPI_WARN
3463 "hotkey enable/disable functionality has been "
3464 "removed from the driver. Hotkeys are always "
3465 "enabled\n"))
3466 printk(TPACPI_ERR
3467 "Please remove the hotkey=enable module "
3468 "parameter, it is deprecated. Hotkeys are always "
3469 "enabled\n");
3472 static int hotkey_write(char *buf)
3474 int res;
3475 u32 mask;
3476 char *cmd;
3478 if (!tp_features.hotkey)
3479 return -ENODEV;
3481 if (mutex_lock_killable(&hotkey_mutex))
3482 return -ERESTARTSYS;
3484 mask = hotkey_user_mask;
3486 res = 0;
3487 while ((cmd = next_cmd(&buf))) {
3488 if (strlencmp(cmd, "enable") == 0) {
3489 hotkey_enabledisable_warn(1);
3490 } else if (strlencmp(cmd, "disable") == 0) {
3491 hotkey_enabledisable_warn(0);
3492 res = -EPERM;
3493 } else if (strlencmp(cmd, "reset") == 0) {
3494 mask = (hotkey_all_mask | hotkey_source_mask)
3495 & ~hotkey_reserved_mask;
3496 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3497 /* mask set */
3498 } else if (sscanf(cmd, "%x", &mask) == 1) {
3499 /* mask set */
3500 } else {
3501 res = -EINVAL;
3502 goto errexit;
3506 if (!res) {
3507 tpacpi_disclose_usertask("procfs hotkey",
3508 "set mask to 0x%08x\n", mask);
3509 res = hotkey_user_mask_set(mask);
3512 errexit:
3513 mutex_unlock(&hotkey_mutex);
3514 return res;
3517 static const struct acpi_device_id ibm_htk_device_ids[] = {
3518 {TPACPI_ACPI_HKEY_HID, 0},
3519 {"", 0},
3522 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3523 .hid = ibm_htk_device_ids,
3524 .notify = hotkey_notify,
3525 .handle = &hkey_handle,
3526 .type = ACPI_DEVICE_NOTIFY,
3529 static struct ibm_struct hotkey_driver_data = {
3530 .name = "hotkey",
3531 .read = hotkey_read,
3532 .write = hotkey_write,
3533 .exit = hotkey_exit,
3534 .resume = hotkey_resume,
3535 .suspend = hotkey_suspend,
3536 .acpi = &ibm_hotkey_acpidriver,
3539 /*************************************************************************
3540 * Bluetooth subdriver
3543 enum {
3544 /* ACPI GBDC/SBDC bits */
3545 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3546 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3547 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3548 off / last state */
3551 enum {
3552 /* ACPI \BLTH commands */
3553 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3554 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3555 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3556 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3557 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3560 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3562 static struct rfkill *tpacpi_bluetooth_rfkill;
3564 static void bluetooth_suspend(pm_message_t state)
3566 /* Try to make sure radio will resume powered off */
3567 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3568 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3569 vdbg_printk(TPACPI_DBG_RFKILL,
3570 "bluetooth power down on resume request failed\n");
3573 static int bluetooth_get_radiosw(void)
3575 int status;
3577 if (!tp_features.bluetooth)
3578 return -ENODEV;
3580 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3581 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3582 return RFKILL_STATE_HARD_BLOCKED;
3584 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3585 if (dbg_bluetoothemul)
3586 return (tpacpi_bluetooth_emulstate) ?
3587 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3588 #endif
3590 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3591 return -EIO;
3593 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3594 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3597 static void bluetooth_update_rfk(void)
3599 int status;
3601 if (!tpacpi_bluetooth_rfkill)
3602 return;
3604 status = bluetooth_get_radiosw();
3605 if (status < 0)
3606 return;
3607 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3609 vdbg_printk(TPACPI_DBG_RFKILL,
3610 "forced rfkill state to %d\n",
3611 status);
3614 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3616 int status;
3618 if (!tp_features.bluetooth)
3619 return -ENODEV;
3621 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3622 * reason to risk weird behaviour. */
3623 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3624 && radio_on)
3625 return -EPERM;
3627 vdbg_printk(TPACPI_DBG_RFKILL,
3628 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3630 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3631 if (dbg_bluetoothemul) {
3632 tpacpi_bluetooth_emulstate = !!radio_on;
3633 if (update_rfk)
3634 bluetooth_update_rfk();
3635 return 0;
3637 #endif
3639 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3640 if (radio_on)
3641 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3642 else
3643 status = 0;
3644 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3645 return -EIO;
3647 if (update_rfk)
3648 bluetooth_update_rfk();
3650 return 0;
3653 /* sysfs bluetooth enable ---------------------------------------------- */
3654 static ssize_t bluetooth_enable_show(struct device *dev,
3655 struct device_attribute *attr,
3656 char *buf)
3658 int status;
3660 printk_deprecated_rfkill_attribute("bluetooth_enable");
3662 status = bluetooth_get_radiosw();
3663 if (status < 0)
3664 return status;
3666 return snprintf(buf, PAGE_SIZE, "%d\n",
3667 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3670 static ssize_t bluetooth_enable_store(struct device *dev,
3671 struct device_attribute *attr,
3672 const char *buf, size_t count)
3674 unsigned long t;
3675 int res;
3677 printk_deprecated_rfkill_attribute("bluetooth_enable");
3679 if (parse_strtoul(buf, 1, &t))
3680 return -EINVAL;
3682 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3684 res = bluetooth_set_radiosw(t, 1);
3686 return (res) ? res : count;
3689 static struct device_attribute dev_attr_bluetooth_enable =
3690 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3691 bluetooth_enable_show, bluetooth_enable_store);
3693 /* --------------------------------------------------------------------- */
3695 static struct attribute *bluetooth_attributes[] = {
3696 &dev_attr_bluetooth_enable.attr,
3697 NULL
3700 static const struct attribute_group bluetooth_attr_group = {
3701 .attrs = bluetooth_attributes,
3704 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3706 int bts = bluetooth_get_radiosw();
3708 if (bts < 0)
3709 return bts;
3711 *state = bts;
3712 return 0;
3715 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3717 dbg_printk(TPACPI_DBG_RFKILL,
3718 "request to change radio state to %d\n", state);
3719 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3722 static void bluetooth_shutdown(void)
3724 /* Order firmware to save current state to NVRAM */
3725 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3726 TP_ACPI_BLTH_SAVE_STATE))
3727 printk(TPACPI_NOTICE
3728 "failed to save bluetooth state to NVRAM\n");
3729 else
3730 vdbg_printk(TPACPI_DBG_RFKILL,
3731 "bluestooth state saved to NVRAM\n");
3734 static void bluetooth_exit(void)
3736 bluetooth_shutdown();
3738 if (tpacpi_bluetooth_rfkill)
3739 rfkill_unregister(tpacpi_bluetooth_rfkill);
3741 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3742 &bluetooth_attr_group);
3745 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3747 int res;
3748 int status = 0;
3750 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3751 "initializing bluetooth subdriver\n");
3753 TPACPI_ACPIHANDLE_INIT(hkey);
3755 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3756 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3757 tp_features.bluetooth = hkey_handle &&
3758 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3760 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3761 "bluetooth is %s, status 0x%02x\n",
3762 str_supported(tp_features.bluetooth),
3763 status);
3765 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3766 if (dbg_bluetoothemul) {
3767 tp_features.bluetooth = 1;
3768 printk(TPACPI_INFO
3769 "bluetooth switch emulation enabled\n");
3770 } else
3771 #endif
3772 if (tp_features.bluetooth &&
3773 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3774 /* no bluetooth hardware present in system */
3775 tp_features.bluetooth = 0;
3776 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3777 "bluetooth hardware not installed\n");
3780 if (!tp_features.bluetooth)
3781 return 1;
3783 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3784 &bluetooth_attr_group);
3785 if (res)
3786 return res;
3788 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3789 &tpacpi_bluetooth_rfkill,
3790 RFKILL_TYPE_BLUETOOTH,
3791 TPACPI_RFK_BLUETOOTH_SW_NAME,
3792 true,
3793 tpacpi_bluetooth_rfk_set,
3794 tpacpi_bluetooth_rfk_get);
3795 if (res) {
3796 bluetooth_exit();
3797 return res;
3800 return 0;
3803 /* procfs -------------------------------------------------------------- */
3804 static int bluetooth_read(char *p)
3806 int len = 0;
3807 int status = bluetooth_get_radiosw();
3809 if (!tp_features.bluetooth)
3810 len += sprintf(p + len, "status:\t\tnot supported\n");
3811 else {
3812 len += sprintf(p + len, "status:\t\t%s\n",
3813 (status == RFKILL_STATE_UNBLOCKED) ?
3814 "enabled" : "disabled");
3815 len += sprintf(p + len, "commands:\tenable, disable\n");
3818 return len;
3821 static int bluetooth_write(char *buf)
3823 char *cmd;
3824 int state = -1;
3826 if (!tp_features.bluetooth)
3827 return -ENODEV;
3829 while ((cmd = next_cmd(&buf))) {
3830 if (strlencmp(cmd, "enable") == 0) {
3831 state = 1;
3832 } else if (strlencmp(cmd, "disable") == 0) {
3833 state = 0;
3834 } else
3835 return -EINVAL;
3838 if (state != -1) {
3839 tpacpi_disclose_usertask("procfs bluetooth",
3840 "attempt to %s\n",
3841 state ? "enable" : "disable");
3842 bluetooth_set_radiosw(state, 1);
3845 return 0;
3848 static struct ibm_struct bluetooth_driver_data = {
3849 .name = "bluetooth",
3850 .read = bluetooth_read,
3851 .write = bluetooth_write,
3852 .exit = bluetooth_exit,
3853 .suspend = bluetooth_suspend,
3854 .shutdown = bluetooth_shutdown,
3857 /*************************************************************************
3858 * Wan subdriver
3861 enum {
3862 /* ACPI GWAN/SWAN bits */
3863 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3864 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3865 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3866 off / last state */
3869 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3871 static struct rfkill *tpacpi_wan_rfkill;
3873 static void wan_suspend(pm_message_t state)
3875 /* Try to make sure radio will resume powered off */
3876 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3877 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3878 vdbg_printk(TPACPI_DBG_RFKILL,
3879 "WWAN power down on resume request failed\n");
3882 static int wan_get_radiosw(void)
3884 int status;
3886 if (!tp_features.wan)
3887 return -ENODEV;
3889 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3890 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3891 return RFKILL_STATE_HARD_BLOCKED;
3893 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3894 if (dbg_wwanemul)
3895 return (tpacpi_wwan_emulstate) ?
3896 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3897 #endif
3899 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3900 return -EIO;
3902 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3903 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3906 static void wan_update_rfk(void)
3908 int status;
3910 if (!tpacpi_wan_rfkill)
3911 return;
3913 status = wan_get_radiosw();
3914 if (status < 0)
3915 return;
3916 rfkill_force_state(tpacpi_wan_rfkill, status);
3918 vdbg_printk(TPACPI_DBG_RFKILL,
3919 "forced rfkill state to %d\n",
3920 status);
3923 static int wan_set_radiosw(int radio_on, int update_rfk)
3925 int status;
3927 if (!tp_features.wan)
3928 return -ENODEV;
3930 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3931 * reason to risk weird behaviour. */
3932 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3933 && radio_on)
3934 return -EPERM;
3936 vdbg_printk(TPACPI_DBG_RFKILL,
3937 "will %s WWAN\n", radio_on ? "enable" : "disable");
3939 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3940 if (dbg_wwanemul) {
3941 tpacpi_wwan_emulstate = !!radio_on;
3942 if (update_rfk)
3943 wan_update_rfk();
3944 return 0;
3946 #endif
3948 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3949 if (radio_on)
3950 status = TP_ACPI_WANCARD_RADIOSSW;
3951 else
3952 status = 0;
3953 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3954 return -EIO;
3956 if (update_rfk)
3957 wan_update_rfk();
3959 return 0;
3962 /* sysfs wan enable ---------------------------------------------------- */
3963 static ssize_t wan_enable_show(struct device *dev,
3964 struct device_attribute *attr,
3965 char *buf)
3967 int status;
3969 printk_deprecated_rfkill_attribute("wwan_enable");
3971 status = wan_get_radiosw();
3972 if (status < 0)
3973 return status;
3975 return snprintf(buf, PAGE_SIZE, "%d\n",
3976 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3979 static ssize_t wan_enable_store(struct device *dev,
3980 struct device_attribute *attr,
3981 const char *buf, size_t count)
3983 unsigned long t;
3984 int res;
3986 printk_deprecated_rfkill_attribute("wwan_enable");
3988 if (parse_strtoul(buf, 1, &t))
3989 return -EINVAL;
3991 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3993 res = wan_set_radiosw(t, 1);
3995 return (res) ? res : count;
3998 static struct device_attribute dev_attr_wan_enable =
3999 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4000 wan_enable_show, wan_enable_store);
4002 /* --------------------------------------------------------------------- */
4004 static struct attribute *wan_attributes[] = {
4005 &dev_attr_wan_enable.attr,
4006 NULL
4009 static const struct attribute_group wan_attr_group = {
4010 .attrs = wan_attributes,
4013 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4015 int wans = wan_get_radiosw();
4017 if (wans < 0)
4018 return wans;
4020 *state = wans;
4021 return 0;
4024 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4026 dbg_printk(TPACPI_DBG_RFKILL,
4027 "request to change radio state to %d\n", state);
4028 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4031 static void wan_shutdown(void)
4033 /* Order firmware to save current state to NVRAM */
4034 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4035 TP_ACPI_WGSV_SAVE_STATE))
4036 printk(TPACPI_NOTICE
4037 "failed to save WWAN state to NVRAM\n");
4038 else
4039 vdbg_printk(TPACPI_DBG_RFKILL,
4040 "WWAN state saved to NVRAM\n");
4043 static void wan_exit(void)
4045 wan_shutdown();
4047 if (tpacpi_wan_rfkill)
4048 rfkill_unregister(tpacpi_wan_rfkill);
4050 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4051 &wan_attr_group);
4054 static int __init wan_init(struct ibm_init_struct *iibm)
4056 int res;
4057 int status = 0;
4059 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4060 "initializing wan subdriver\n");
4062 TPACPI_ACPIHANDLE_INIT(hkey);
4064 tp_features.wan = hkey_handle &&
4065 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4067 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4068 "wan is %s, status 0x%02x\n",
4069 str_supported(tp_features.wan),
4070 status);
4072 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4073 if (dbg_wwanemul) {
4074 tp_features.wan = 1;
4075 printk(TPACPI_INFO
4076 "wwan switch emulation enabled\n");
4077 } else
4078 #endif
4079 if (tp_features.wan &&
4080 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4081 /* no wan hardware present in system */
4082 tp_features.wan = 0;
4083 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4084 "wan hardware not installed\n");
4087 if (!tp_features.wan)
4088 return 1;
4090 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4091 &wan_attr_group);
4092 if (res)
4093 return res;
4095 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4096 &tpacpi_wan_rfkill,
4097 RFKILL_TYPE_WWAN,
4098 TPACPI_RFK_WWAN_SW_NAME,
4099 true,
4100 tpacpi_wan_rfk_set,
4101 tpacpi_wan_rfk_get);
4102 if (res) {
4103 wan_exit();
4104 return res;
4107 return 0;
4110 /* procfs -------------------------------------------------------------- */
4111 static int wan_read(char *p)
4113 int len = 0;
4114 int status = wan_get_radiosw();
4116 tpacpi_disclose_usertask("procfs wan", "read");
4118 if (!tp_features.wan)
4119 len += sprintf(p + len, "status:\t\tnot supported\n");
4120 else {
4121 len += sprintf(p + len, "status:\t\t%s\n",
4122 (status == RFKILL_STATE_UNBLOCKED) ?
4123 "enabled" : "disabled");
4124 len += sprintf(p + len, "commands:\tenable, disable\n");
4127 return len;
4130 static int wan_write(char *buf)
4132 char *cmd;
4133 int state = -1;
4135 if (!tp_features.wan)
4136 return -ENODEV;
4138 while ((cmd = next_cmd(&buf))) {
4139 if (strlencmp(cmd, "enable") == 0) {
4140 state = 1;
4141 } else if (strlencmp(cmd, "disable") == 0) {
4142 state = 0;
4143 } else
4144 return -EINVAL;
4147 if (state != -1) {
4148 tpacpi_disclose_usertask("procfs wan",
4149 "attempt to %s\n",
4150 state ? "enable" : "disable");
4151 wan_set_radiosw(state, 1);
4154 return 0;
4157 static struct ibm_struct wan_driver_data = {
4158 .name = "wan",
4159 .read = wan_read,
4160 .write = wan_write,
4161 .exit = wan_exit,
4162 .suspend = wan_suspend,
4163 .shutdown = wan_shutdown,
4166 /*************************************************************************
4167 * UWB subdriver
4170 enum {
4171 /* ACPI GUWB/SUWB bits */
4172 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4173 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4176 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4178 static struct rfkill *tpacpi_uwb_rfkill;
4180 static int uwb_get_radiosw(void)
4182 int status;
4184 if (!tp_features.uwb)
4185 return -ENODEV;
4187 /* WLSW overrides UWB in firmware/hardware, reflect that */
4188 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4189 return RFKILL_STATE_HARD_BLOCKED;
4191 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4192 if (dbg_uwbemul)
4193 return (tpacpi_uwb_emulstate) ?
4194 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4195 #endif
4197 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4198 return -EIO;
4200 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4201 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4204 static void uwb_update_rfk(void)
4206 int status;
4208 if (!tpacpi_uwb_rfkill)
4209 return;
4211 status = uwb_get_radiosw();
4212 if (status < 0)
4213 return;
4214 rfkill_force_state(tpacpi_uwb_rfkill, status);
4216 vdbg_printk(TPACPI_DBG_RFKILL,
4217 "forced rfkill state to %d\n",
4218 status);
4221 static int uwb_set_radiosw(int radio_on, int update_rfk)
4223 int status;
4225 if (!tp_features.uwb)
4226 return -ENODEV;
4228 /* WLSW overrides UWB in firmware/hardware, but there is no
4229 * reason to risk weird behaviour. */
4230 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4231 && radio_on)
4232 return -EPERM;
4234 vdbg_printk(TPACPI_DBG_RFKILL,
4235 "will %s UWB\n", radio_on ? "enable" : "disable");
4237 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4238 if (dbg_uwbemul) {
4239 tpacpi_uwb_emulstate = !!radio_on;
4240 if (update_rfk)
4241 uwb_update_rfk();
4242 return 0;
4244 #endif
4246 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4247 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4248 return -EIO;
4250 if (update_rfk)
4251 uwb_update_rfk();
4253 return 0;
4256 /* --------------------------------------------------------------------- */
4258 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4260 int uwbs = uwb_get_radiosw();
4262 if (uwbs < 0)
4263 return uwbs;
4265 *state = uwbs;
4266 return 0;
4269 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4271 dbg_printk(TPACPI_DBG_RFKILL,
4272 "request to change radio state to %d\n", state);
4273 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4276 static void uwb_exit(void)
4278 if (tpacpi_uwb_rfkill)
4279 rfkill_unregister(tpacpi_uwb_rfkill);
4282 static int __init uwb_init(struct ibm_init_struct *iibm)
4284 int res;
4285 int status = 0;
4287 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4288 "initializing uwb subdriver\n");
4290 TPACPI_ACPIHANDLE_INIT(hkey);
4292 tp_features.uwb = hkey_handle &&
4293 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4295 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4296 "uwb is %s, status 0x%02x\n",
4297 str_supported(tp_features.uwb),
4298 status);
4300 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4301 if (dbg_uwbemul) {
4302 tp_features.uwb = 1;
4303 printk(TPACPI_INFO
4304 "uwb switch emulation enabled\n");
4305 } else
4306 #endif
4307 if (tp_features.uwb &&
4308 !(status & TP_ACPI_UWB_HWPRESENT)) {
4309 /* no uwb hardware present in system */
4310 tp_features.uwb = 0;
4311 dbg_printk(TPACPI_DBG_INIT,
4312 "uwb hardware not installed\n");
4315 if (!tp_features.uwb)
4316 return 1;
4318 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4319 &tpacpi_uwb_rfkill,
4320 RFKILL_TYPE_UWB,
4321 TPACPI_RFK_UWB_SW_NAME,
4322 false,
4323 tpacpi_uwb_rfk_set,
4324 tpacpi_uwb_rfk_get);
4326 return res;
4329 static struct ibm_struct uwb_driver_data = {
4330 .name = "uwb",
4331 .exit = uwb_exit,
4332 .flags.experimental = 1,
4335 /*************************************************************************
4336 * Video subdriver
4339 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4341 enum video_access_mode {
4342 TPACPI_VIDEO_NONE = 0,
4343 TPACPI_VIDEO_570, /* 570 */
4344 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4345 TPACPI_VIDEO_NEW, /* all others */
4348 enum { /* video status flags, based on VIDEO_570 */
4349 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4350 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4351 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4354 enum { /* TPACPI_VIDEO_570 constants */
4355 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4356 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4357 * video_status_flags */
4358 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4359 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4362 static enum video_access_mode video_supported;
4363 static int video_orig_autosw;
4365 static int video_autosw_get(void);
4366 static int video_autosw_set(int enable);
4368 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4370 static int __init video_init(struct ibm_init_struct *iibm)
4372 int ivga;
4374 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4376 TPACPI_ACPIHANDLE_INIT(vid);
4377 TPACPI_ACPIHANDLE_INIT(vid2);
4379 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4380 /* G41, assume IVGA doesn't change */
4381 vid_handle = vid2_handle;
4383 if (!vid_handle)
4384 /* video switching not supported on R30, R31 */
4385 video_supported = TPACPI_VIDEO_NONE;
4386 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4387 /* 570 */
4388 video_supported = TPACPI_VIDEO_570;
4389 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4390 /* 600e/x, 770e, 770x */
4391 video_supported = TPACPI_VIDEO_770;
4392 else
4393 /* all others */
4394 video_supported = TPACPI_VIDEO_NEW;
4396 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4397 str_supported(video_supported != TPACPI_VIDEO_NONE),
4398 video_supported);
4400 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4403 static void video_exit(void)
4405 dbg_printk(TPACPI_DBG_EXIT,
4406 "restoring original video autoswitch mode\n");
4407 if (video_autosw_set(video_orig_autosw))
4408 printk(TPACPI_ERR "error while trying to restore original "
4409 "video autoswitch mode\n");
4412 static int video_outputsw_get(void)
4414 int status = 0;
4415 int i;
4417 switch (video_supported) {
4418 case TPACPI_VIDEO_570:
4419 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4420 TP_ACPI_VIDEO_570_PHSCMD))
4421 return -EIO;
4422 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4423 break;
4424 case TPACPI_VIDEO_770:
4425 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4426 return -EIO;
4427 if (i)
4428 status |= TP_ACPI_VIDEO_S_LCD;
4429 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4430 return -EIO;
4431 if (i)
4432 status |= TP_ACPI_VIDEO_S_CRT;
4433 break;
4434 case TPACPI_VIDEO_NEW:
4435 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4436 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4437 return -EIO;
4438 if (i)
4439 status |= TP_ACPI_VIDEO_S_CRT;
4441 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4442 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4443 return -EIO;
4444 if (i)
4445 status |= TP_ACPI_VIDEO_S_LCD;
4446 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4447 return -EIO;
4448 if (i)
4449 status |= TP_ACPI_VIDEO_S_DVI;
4450 break;
4451 default:
4452 return -ENOSYS;
4455 return status;
4458 static int video_outputsw_set(int status)
4460 int autosw;
4461 int res = 0;
4463 switch (video_supported) {
4464 case TPACPI_VIDEO_570:
4465 res = acpi_evalf(NULL, NULL,
4466 "\\_SB.PHS2", "vdd",
4467 TP_ACPI_VIDEO_570_PHS2CMD,
4468 status | TP_ACPI_VIDEO_570_PHS2SET);
4469 break;
4470 case TPACPI_VIDEO_770:
4471 autosw = video_autosw_get();
4472 if (autosw < 0)
4473 return autosw;
4475 res = video_autosw_set(1);
4476 if (res)
4477 return res;
4478 res = acpi_evalf(vid_handle, NULL,
4479 "ASWT", "vdd", status * 0x100, 0);
4480 if (!autosw && video_autosw_set(autosw)) {
4481 printk(TPACPI_ERR
4482 "video auto-switch left enabled due to error\n");
4483 return -EIO;
4485 break;
4486 case TPACPI_VIDEO_NEW:
4487 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4488 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4489 break;
4490 default:
4491 return -ENOSYS;
4494 return (res)? 0 : -EIO;
4497 static int video_autosw_get(void)
4499 int autosw = 0;
4501 switch (video_supported) {
4502 case TPACPI_VIDEO_570:
4503 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4504 return -EIO;
4505 break;
4506 case TPACPI_VIDEO_770:
4507 case TPACPI_VIDEO_NEW:
4508 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4509 return -EIO;
4510 break;
4511 default:
4512 return -ENOSYS;
4515 return autosw & 1;
4518 static int video_autosw_set(int enable)
4520 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4521 return -EIO;
4522 return 0;
4525 static int video_outputsw_cycle(void)
4527 int autosw = video_autosw_get();
4528 int res;
4530 if (autosw < 0)
4531 return autosw;
4533 switch (video_supported) {
4534 case TPACPI_VIDEO_570:
4535 res = video_autosw_set(1);
4536 if (res)
4537 return res;
4538 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4539 break;
4540 case TPACPI_VIDEO_770:
4541 case TPACPI_VIDEO_NEW:
4542 res = video_autosw_set(1);
4543 if (res)
4544 return res;
4545 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4546 break;
4547 default:
4548 return -ENOSYS;
4550 if (!autosw && video_autosw_set(autosw)) {
4551 printk(TPACPI_ERR
4552 "video auto-switch left enabled due to error\n");
4553 return -EIO;
4556 return (res)? 0 : -EIO;
4559 static int video_expand_toggle(void)
4561 switch (video_supported) {
4562 case TPACPI_VIDEO_570:
4563 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4564 0 : -EIO;
4565 case TPACPI_VIDEO_770:
4566 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4567 0 : -EIO;
4568 case TPACPI_VIDEO_NEW:
4569 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4570 0 : -EIO;
4571 default:
4572 return -ENOSYS;
4574 /* not reached */
4577 static int video_read(char *p)
4579 int status, autosw;
4580 int len = 0;
4582 if (video_supported == TPACPI_VIDEO_NONE) {
4583 len += sprintf(p + len, "status:\t\tnot supported\n");
4584 return len;
4587 status = video_outputsw_get();
4588 if (status < 0)
4589 return status;
4591 autosw = video_autosw_get();
4592 if (autosw < 0)
4593 return autosw;
4595 len += sprintf(p + len, "status:\t\tsupported\n");
4596 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4597 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4598 if (video_supported == TPACPI_VIDEO_NEW)
4599 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4600 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4601 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4602 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4603 if (video_supported == TPACPI_VIDEO_NEW)
4604 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4605 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4606 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4608 return len;
4611 static int video_write(char *buf)
4613 char *cmd;
4614 int enable, disable, status;
4615 int res;
4617 if (video_supported == TPACPI_VIDEO_NONE)
4618 return -ENODEV;
4620 enable = 0;
4621 disable = 0;
4623 while ((cmd = next_cmd(&buf))) {
4624 if (strlencmp(cmd, "lcd_enable") == 0) {
4625 enable |= TP_ACPI_VIDEO_S_LCD;
4626 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4627 disable |= TP_ACPI_VIDEO_S_LCD;
4628 } else if (strlencmp(cmd, "crt_enable") == 0) {
4629 enable |= TP_ACPI_VIDEO_S_CRT;
4630 } else if (strlencmp(cmd, "crt_disable") == 0) {
4631 disable |= TP_ACPI_VIDEO_S_CRT;
4632 } else if (video_supported == TPACPI_VIDEO_NEW &&
4633 strlencmp(cmd, "dvi_enable") == 0) {
4634 enable |= TP_ACPI_VIDEO_S_DVI;
4635 } else if (video_supported == TPACPI_VIDEO_NEW &&
4636 strlencmp(cmd, "dvi_disable") == 0) {
4637 disable |= TP_ACPI_VIDEO_S_DVI;
4638 } else if (strlencmp(cmd, "auto_enable") == 0) {
4639 res = video_autosw_set(1);
4640 if (res)
4641 return res;
4642 } else if (strlencmp(cmd, "auto_disable") == 0) {
4643 res = video_autosw_set(0);
4644 if (res)
4645 return res;
4646 } else if (strlencmp(cmd, "video_switch") == 0) {
4647 res = video_outputsw_cycle();
4648 if (res)
4649 return res;
4650 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4651 res = video_expand_toggle();
4652 if (res)
4653 return res;
4654 } else
4655 return -EINVAL;
4658 if (enable || disable) {
4659 status = video_outputsw_get();
4660 if (status < 0)
4661 return status;
4662 res = video_outputsw_set((status & ~disable) | enable);
4663 if (res)
4664 return res;
4667 return 0;
4670 static struct ibm_struct video_driver_data = {
4671 .name = "video",
4672 .read = video_read,
4673 .write = video_write,
4674 .exit = video_exit,
4677 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4679 /*************************************************************************
4680 * Light (thinklight) subdriver
4683 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4684 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4686 static int light_get_status(void)
4688 int status = 0;
4690 if (tp_features.light_status) {
4691 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4692 return -EIO;
4693 return (!!status);
4696 return -ENXIO;
4699 static int light_set_status(int status)
4701 int rc;
4703 if (tp_features.light) {
4704 if (cmos_handle) {
4705 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4706 (status)?
4707 TP_CMOS_THINKLIGHT_ON :
4708 TP_CMOS_THINKLIGHT_OFF);
4709 } else {
4710 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4711 (status)? 1 : 0);
4713 return (rc)? 0 : -EIO;
4716 return -ENXIO;
4719 static void light_set_status_worker(struct work_struct *work)
4721 struct tpacpi_led_classdev *data =
4722 container_of(work, struct tpacpi_led_classdev, work);
4724 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4725 light_set_status((data->new_state != TPACPI_LED_OFF));
4728 static void light_sysfs_set(struct led_classdev *led_cdev,
4729 enum led_brightness brightness)
4731 struct tpacpi_led_classdev *data =
4732 container_of(led_cdev,
4733 struct tpacpi_led_classdev,
4734 led_classdev);
4735 data->new_state = (brightness != LED_OFF) ?
4736 TPACPI_LED_ON : TPACPI_LED_OFF;
4737 queue_work(tpacpi_wq, &data->work);
4740 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4742 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4745 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4746 .led_classdev = {
4747 .name = "tpacpi::thinklight",
4748 .brightness_set = &light_sysfs_set,
4749 .brightness_get = &light_sysfs_get,
4753 static int __init light_init(struct ibm_init_struct *iibm)
4755 int rc;
4757 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4759 TPACPI_ACPIHANDLE_INIT(ledb);
4760 TPACPI_ACPIHANDLE_INIT(lght);
4761 TPACPI_ACPIHANDLE_INIT(cmos);
4762 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4764 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4765 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4767 if (tp_features.light)
4768 /* light status not supported on
4769 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4770 tp_features.light_status =
4771 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4773 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4774 str_supported(tp_features.light),
4775 str_supported(tp_features.light_status));
4777 if (!tp_features.light)
4778 return 1;
4780 rc = led_classdev_register(&tpacpi_pdev->dev,
4781 &tpacpi_led_thinklight.led_classdev);
4783 if (rc < 0) {
4784 tp_features.light = 0;
4785 tp_features.light_status = 0;
4786 } else {
4787 rc = 0;
4790 return rc;
4793 static void light_exit(void)
4795 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4796 if (work_pending(&tpacpi_led_thinklight.work))
4797 flush_workqueue(tpacpi_wq);
4800 static int light_read(char *p)
4802 int len = 0;
4803 int status;
4805 if (!tp_features.light) {
4806 len += sprintf(p + len, "status:\t\tnot supported\n");
4807 } else if (!tp_features.light_status) {
4808 len += sprintf(p + len, "status:\t\tunknown\n");
4809 len += sprintf(p + len, "commands:\ton, off\n");
4810 } else {
4811 status = light_get_status();
4812 if (status < 0)
4813 return status;
4814 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4815 len += sprintf(p + len, "commands:\ton, off\n");
4818 return len;
4821 static int light_write(char *buf)
4823 char *cmd;
4824 int newstatus = 0;
4826 if (!tp_features.light)
4827 return -ENODEV;
4829 while ((cmd = next_cmd(&buf))) {
4830 if (strlencmp(cmd, "on") == 0) {
4831 newstatus = 1;
4832 } else if (strlencmp(cmd, "off") == 0) {
4833 newstatus = 0;
4834 } else
4835 return -EINVAL;
4838 return light_set_status(newstatus);
4841 static struct ibm_struct light_driver_data = {
4842 .name = "light",
4843 .read = light_read,
4844 .write = light_write,
4845 .exit = light_exit,
4848 /*************************************************************************
4849 * CMOS subdriver
4852 /* sysfs cmos_command -------------------------------------------------- */
4853 static ssize_t cmos_command_store(struct device *dev,
4854 struct device_attribute *attr,
4855 const char *buf, size_t count)
4857 unsigned long cmos_cmd;
4858 int res;
4860 if (parse_strtoul(buf, 21, &cmos_cmd))
4861 return -EINVAL;
4863 res = issue_thinkpad_cmos_command(cmos_cmd);
4864 return (res)? res : count;
4867 static struct device_attribute dev_attr_cmos_command =
4868 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4870 /* --------------------------------------------------------------------- */
4872 static int __init cmos_init(struct ibm_init_struct *iibm)
4874 int res;
4876 vdbg_printk(TPACPI_DBG_INIT,
4877 "initializing cmos commands subdriver\n");
4879 TPACPI_ACPIHANDLE_INIT(cmos);
4881 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4882 str_supported(cmos_handle != NULL));
4884 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4885 if (res)
4886 return res;
4888 return (cmos_handle)? 0 : 1;
4891 static void cmos_exit(void)
4893 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4896 static int cmos_read(char *p)
4898 int len = 0;
4900 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4901 R30, R31, T20-22, X20-21 */
4902 if (!cmos_handle)
4903 len += sprintf(p + len, "status:\t\tnot supported\n");
4904 else {
4905 len += sprintf(p + len, "status:\t\tsupported\n");
4906 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4909 return len;
4912 static int cmos_write(char *buf)
4914 char *cmd;
4915 int cmos_cmd, res;
4917 while ((cmd = next_cmd(&buf))) {
4918 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4919 cmos_cmd >= 0 && cmos_cmd <= 21) {
4920 /* cmos_cmd set */
4921 } else
4922 return -EINVAL;
4924 res = issue_thinkpad_cmos_command(cmos_cmd);
4925 if (res)
4926 return res;
4929 return 0;
4932 static struct ibm_struct cmos_driver_data = {
4933 .name = "cmos",
4934 .read = cmos_read,
4935 .write = cmos_write,
4936 .exit = cmos_exit,
4939 /*************************************************************************
4940 * LED subdriver
4943 enum led_access_mode {
4944 TPACPI_LED_NONE = 0,
4945 TPACPI_LED_570, /* 570 */
4946 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4947 TPACPI_LED_NEW, /* all others */
4950 enum { /* For TPACPI_LED_OLD */
4951 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4952 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4953 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4956 static enum led_access_mode led_supported;
4958 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4959 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4960 /* T20-22, X20-21 */
4961 "LED", /* all others */
4962 ); /* R30, R31 */
4964 #define TPACPI_LED_NUMLEDS 16
4965 static struct tpacpi_led_classdev *tpacpi_leds;
4966 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4967 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4968 /* there's a limit of 19 chars + NULL before 2.6.26 */
4969 "tpacpi::power",
4970 "tpacpi:orange:batt",
4971 "tpacpi:green:batt",
4972 "tpacpi::dock_active",
4973 "tpacpi::bay_active",
4974 "tpacpi::dock_batt",
4975 "tpacpi::unknown_led",
4976 "tpacpi::standby",
4977 "tpacpi::dock_status1",
4978 "tpacpi::dock_status2",
4979 "tpacpi::unknown_led2",
4980 "tpacpi::unknown_led3",
4981 "tpacpi::thinkvantage",
4983 #define TPACPI_SAFE_LEDS 0x1081U
4985 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4987 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4988 return false;
4989 #else
4990 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4991 #endif
4994 static int led_get_status(const unsigned int led)
4996 int status;
4997 enum led_status_t led_s;
4999 switch (led_supported) {
5000 case TPACPI_LED_570:
5001 if (!acpi_evalf(ec_handle,
5002 &status, "GLED", "dd", 1 << led))
5003 return -EIO;
5004 led_s = (status == 0)?
5005 TPACPI_LED_OFF :
5006 ((status == 1)?
5007 TPACPI_LED_ON :
5008 TPACPI_LED_BLINK);
5009 tpacpi_led_state_cache[led] = led_s;
5010 return led_s;
5011 default:
5012 return -ENXIO;
5015 /* not reached */
5018 static int led_set_status(const unsigned int led,
5019 const enum led_status_t ledstatus)
5021 /* off, on, blink. Index is led_status_t */
5022 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5023 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5025 int rc = 0;
5027 switch (led_supported) {
5028 case TPACPI_LED_570:
5029 /* 570 */
5030 if (unlikely(led > 7))
5031 return -EINVAL;
5032 if (unlikely(tpacpi_is_led_restricted(led)))
5033 return -EPERM;
5034 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5035 (1 << led), led_sled_arg1[ledstatus]))
5036 rc = -EIO;
5037 break;
5038 case TPACPI_LED_OLD:
5039 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5040 if (unlikely(led > 7))
5041 return -EINVAL;
5042 if (unlikely(tpacpi_is_led_restricted(led)))
5043 return -EPERM;
5044 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5045 if (rc >= 0)
5046 rc = ec_write(TPACPI_LED_EC_HLBL,
5047 (ledstatus == TPACPI_LED_BLINK) << led);
5048 if (rc >= 0)
5049 rc = ec_write(TPACPI_LED_EC_HLCL,
5050 (ledstatus != TPACPI_LED_OFF) << led);
5051 break;
5052 case TPACPI_LED_NEW:
5053 /* all others */
5054 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5055 return -EINVAL;
5056 if (unlikely(tpacpi_is_led_restricted(led)))
5057 return -EPERM;
5058 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5059 led, led_led_arg1[ledstatus]))
5060 rc = -EIO;
5061 break;
5062 default:
5063 rc = -ENXIO;
5066 if (!rc)
5067 tpacpi_led_state_cache[led] = ledstatus;
5069 return rc;
5072 static void led_set_status_worker(struct work_struct *work)
5074 struct tpacpi_led_classdev *data =
5075 container_of(work, struct tpacpi_led_classdev, work);
5077 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5078 led_set_status(data->led, data->new_state);
5081 static void led_sysfs_set(struct led_classdev *led_cdev,
5082 enum led_brightness brightness)
5084 struct tpacpi_led_classdev *data = container_of(led_cdev,
5085 struct tpacpi_led_classdev, led_classdev);
5087 if (brightness == LED_OFF)
5088 data->new_state = TPACPI_LED_OFF;
5089 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5090 data->new_state = TPACPI_LED_ON;
5091 else
5092 data->new_state = TPACPI_LED_BLINK;
5094 queue_work(tpacpi_wq, &data->work);
5097 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5098 unsigned long *delay_on, unsigned long *delay_off)
5100 struct tpacpi_led_classdev *data = container_of(led_cdev,
5101 struct tpacpi_led_classdev, led_classdev);
5103 /* Can we choose the flash rate? */
5104 if (*delay_on == 0 && *delay_off == 0) {
5105 /* yes. set them to the hardware blink rate (1 Hz) */
5106 *delay_on = 500; /* ms */
5107 *delay_off = 500; /* ms */
5108 } else if ((*delay_on != 500) || (*delay_off != 500))
5109 return -EINVAL;
5111 data->new_state = TPACPI_LED_BLINK;
5112 queue_work(tpacpi_wq, &data->work);
5114 return 0;
5117 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5119 int rc;
5121 struct tpacpi_led_classdev *data = container_of(led_cdev,
5122 struct tpacpi_led_classdev, led_classdev);
5124 rc = led_get_status(data->led);
5126 if (rc == TPACPI_LED_OFF || rc < 0)
5127 rc = LED_OFF; /* no error handling in led class :( */
5128 else
5129 rc = LED_FULL;
5131 return rc;
5134 static void led_exit(void)
5136 unsigned int i;
5138 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5139 if (tpacpi_leds[i].led_classdev.name)
5140 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5143 kfree(tpacpi_leds);
5146 static int __init tpacpi_init_led(unsigned int led)
5148 int rc;
5150 tpacpi_leds[led].led = led;
5152 /* LEDs with no name don't get registered */
5153 if (!tpacpi_led_names[led])
5154 return 0;
5156 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5157 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5158 if (led_supported == TPACPI_LED_570)
5159 tpacpi_leds[led].led_classdev.brightness_get =
5160 &led_sysfs_get;
5162 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5164 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5166 rc = led_classdev_register(&tpacpi_pdev->dev,
5167 &tpacpi_leds[led].led_classdev);
5168 if (rc < 0)
5169 tpacpi_leds[led].led_classdev.name = NULL;
5171 return rc;
5174 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5175 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5176 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5177 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5179 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5180 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5181 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5182 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5183 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5184 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5185 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5186 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5188 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5189 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5190 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5191 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5192 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5194 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5195 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5196 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5197 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5199 /* (1) - may have excess leds enabled on MSB */
5201 /* Defaults (order matters, keep last, don't reorder!) */
5202 { /* Lenovo */
5203 .vendor = PCI_VENDOR_ID_LENOVO,
5204 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5205 .quirks = 0x1fffU,
5207 { /* IBM ThinkPads with no EC version string */
5208 .vendor = PCI_VENDOR_ID_IBM,
5209 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5210 .quirks = 0x00ffU,
5212 { /* IBM ThinkPads with EC version string */
5213 .vendor = PCI_VENDOR_ID_IBM,
5214 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5215 .quirks = 0x00bfU,
5219 #undef TPACPI_LEDQ_IBM
5220 #undef TPACPI_LEDQ_LNV
5222 static int __init led_init(struct ibm_init_struct *iibm)
5224 unsigned int i;
5225 int rc;
5226 unsigned long useful_leds;
5228 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5230 TPACPI_ACPIHANDLE_INIT(led);
5232 if (!led_handle)
5233 /* led not supported on R30, R31 */
5234 led_supported = TPACPI_LED_NONE;
5235 else if (strlencmp(led_path, "SLED") == 0)
5236 /* 570 */
5237 led_supported = TPACPI_LED_570;
5238 else if (strlencmp(led_path, "SYSL") == 0)
5239 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5240 led_supported = TPACPI_LED_OLD;
5241 else
5242 /* all others */
5243 led_supported = TPACPI_LED_NEW;
5245 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5246 str_supported(led_supported), led_supported);
5248 if (led_supported == TPACPI_LED_NONE)
5249 return 1;
5251 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5252 GFP_KERNEL);
5253 if (!tpacpi_leds) {
5254 printk(TPACPI_ERR "Out of memory for LED data\n");
5255 return -ENOMEM;
5258 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5259 ARRAY_SIZE(led_useful_qtable));
5261 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5262 if (!tpacpi_is_led_restricted(i) &&
5263 test_bit(i, &useful_leds)) {
5264 rc = tpacpi_init_led(i);
5265 if (rc < 0) {
5266 led_exit();
5267 return rc;
5272 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5273 printk(TPACPI_NOTICE
5274 "warning: userspace override of important "
5275 "firmware LEDs is enabled\n");
5276 #endif
5277 return 0;
5280 #define str_led_status(s) \
5281 ((s) == TPACPI_LED_OFF ? "off" : \
5282 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5284 static int led_read(char *p)
5286 int len = 0;
5288 if (!led_supported) {
5289 len += sprintf(p + len, "status:\t\tnot supported\n");
5290 return len;
5292 len += sprintf(p + len, "status:\t\tsupported\n");
5294 if (led_supported == TPACPI_LED_570) {
5295 /* 570 */
5296 int i, status;
5297 for (i = 0; i < 8; i++) {
5298 status = led_get_status(i);
5299 if (status < 0)
5300 return -EIO;
5301 len += sprintf(p + len, "%d:\t\t%s\n",
5302 i, str_led_status(status));
5306 len += sprintf(p + len, "commands:\t"
5307 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5309 return len;
5312 static int led_write(char *buf)
5314 char *cmd;
5315 int led, rc;
5316 enum led_status_t s;
5318 if (!led_supported)
5319 return -ENODEV;
5321 while ((cmd = next_cmd(&buf))) {
5322 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5323 return -EINVAL;
5325 if (strstr(cmd, "off")) {
5326 s = TPACPI_LED_OFF;
5327 } else if (strstr(cmd, "on")) {
5328 s = TPACPI_LED_ON;
5329 } else if (strstr(cmd, "blink")) {
5330 s = TPACPI_LED_BLINK;
5331 } else {
5332 return -EINVAL;
5335 rc = led_set_status(led, s);
5336 if (rc < 0)
5337 return rc;
5340 return 0;
5343 static struct ibm_struct led_driver_data = {
5344 .name = "led",
5345 .read = led_read,
5346 .write = led_write,
5347 .exit = led_exit,
5350 /*************************************************************************
5351 * Beep subdriver
5354 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5356 #define TPACPI_BEEP_Q1 0x0001
5358 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5359 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5360 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5363 static int __init beep_init(struct ibm_init_struct *iibm)
5365 unsigned long quirks;
5367 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5369 TPACPI_ACPIHANDLE_INIT(beep);
5371 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5372 str_supported(beep_handle != NULL));
5374 quirks = tpacpi_check_quirks(beep_quirk_table,
5375 ARRAY_SIZE(beep_quirk_table));
5377 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5379 return (beep_handle)? 0 : 1;
5382 static int beep_read(char *p)
5384 int len = 0;
5386 if (!beep_handle)
5387 len += sprintf(p + len, "status:\t\tnot supported\n");
5388 else {
5389 len += sprintf(p + len, "status:\t\tsupported\n");
5390 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5393 return len;
5396 static int beep_write(char *buf)
5398 char *cmd;
5399 int beep_cmd;
5401 if (!beep_handle)
5402 return -ENODEV;
5404 while ((cmd = next_cmd(&buf))) {
5405 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5406 beep_cmd >= 0 && beep_cmd <= 17) {
5407 /* beep_cmd set */
5408 } else
5409 return -EINVAL;
5410 if (tp_features.beep_needs_two_args) {
5411 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5412 beep_cmd, 0))
5413 return -EIO;
5414 } else {
5415 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5416 beep_cmd))
5417 return -EIO;
5421 return 0;
5424 static struct ibm_struct beep_driver_data = {
5425 .name = "beep",
5426 .read = beep_read,
5427 .write = beep_write,
5430 /*************************************************************************
5431 * Thermal subdriver
5434 enum thermal_access_mode {
5435 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5436 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5437 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5438 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5439 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5442 enum { /* TPACPI_THERMAL_TPEC_* */
5443 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5444 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5445 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5448 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5449 struct ibm_thermal_sensors_struct {
5450 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5453 static enum thermal_access_mode thermal_read_mode;
5455 /* idx is zero-based */
5456 static int thermal_get_sensor(int idx, s32 *value)
5458 int t;
5459 s8 tmp;
5460 char tmpi[5];
5462 t = TP_EC_THERMAL_TMP0;
5464 switch (thermal_read_mode) {
5465 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5466 case TPACPI_THERMAL_TPEC_16:
5467 if (idx >= 8 && idx <= 15) {
5468 t = TP_EC_THERMAL_TMP8;
5469 idx -= 8;
5471 /* fallthrough */
5472 #endif
5473 case TPACPI_THERMAL_TPEC_8:
5474 if (idx <= 7) {
5475 if (!acpi_ec_read(t + idx, &tmp))
5476 return -EIO;
5477 *value = tmp * 1000;
5478 return 0;
5480 break;
5482 case TPACPI_THERMAL_ACPI_UPDT:
5483 if (idx <= 7) {
5484 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5485 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5486 return -EIO;
5487 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5488 return -EIO;
5489 *value = (t - 2732) * 100;
5490 return 0;
5492 break;
5494 case TPACPI_THERMAL_ACPI_TMP07:
5495 if (idx <= 7) {
5496 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5497 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5498 return -EIO;
5499 if (t > 127 || t < -127)
5500 t = TP_EC_THERMAL_TMP_NA;
5501 *value = t * 1000;
5502 return 0;
5504 break;
5506 case TPACPI_THERMAL_NONE:
5507 default:
5508 return -ENOSYS;
5511 return -EINVAL;
5514 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5516 int res, i;
5517 int n;
5519 n = 8;
5520 i = 0;
5522 if (!s)
5523 return -EINVAL;
5525 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5526 n = 16;
5528 for (i = 0 ; i < n; i++) {
5529 res = thermal_get_sensor(i, &s->temp[i]);
5530 if (res)
5531 return res;
5534 return n;
5537 /* sysfs temp##_input -------------------------------------------------- */
5539 static ssize_t thermal_temp_input_show(struct device *dev,
5540 struct device_attribute *attr,
5541 char *buf)
5543 struct sensor_device_attribute *sensor_attr =
5544 to_sensor_dev_attr(attr);
5545 int idx = sensor_attr->index;
5546 s32 value;
5547 int res;
5549 res = thermal_get_sensor(idx, &value);
5550 if (res)
5551 return res;
5552 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5553 return -ENXIO;
5555 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5558 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5559 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5560 thermal_temp_input_show, NULL, _idxB)
5562 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5563 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5564 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5565 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5566 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5567 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5568 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5569 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5570 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5571 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5572 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5573 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5574 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5575 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5576 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5577 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5578 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5581 #define THERMAL_ATTRS(X) \
5582 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5584 static struct attribute *thermal_temp_input_attr[] = {
5585 THERMAL_ATTRS(8),
5586 THERMAL_ATTRS(9),
5587 THERMAL_ATTRS(10),
5588 THERMAL_ATTRS(11),
5589 THERMAL_ATTRS(12),
5590 THERMAL_ATTRS(13),
5591 THERMAL_ATTRS(14),
5592 THERMAL_ATTRS(15),
5593 THERMAL_ATTRS(0),
5594 THERMAL_ATTRS(1),
5595 THERMAL_ATTRS(2),
5596 THERMAL_ATTRS(3),
5597 THERMAL_ATTRS(4),
5598 THERMAL_ATTRS(5),
5599 THERMAL_ATTRS(6),
5600 THERMAL_ATTRS(7),
5601 NULL
5604 static const struct attribute_group thermal_temp_input16_group = {
5605 .attrs = thermal_temp_input_attr
5608 static const struct attribute_group thermal_temp_input8_group = {
5609 .attrs = &thermal_temp_input_attr[8]
5612 #undef THERMAL_SENSOR_ATTR_TEMP
5613 #undef THERMAL_ATTRS
5615 /* --------------------------------------------------------------------- */
5617 static int __init thermal_init(struct ibm_init_struct *iibm)
5619 u8 t, ta1, ta2;
5620 int i;
5621 int acpi_tmp7;
5622 int res;
5624 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5626 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5628 if (thinkpad_id.ec_model) {
5630 * Direct EC access mode: sensors at registers
5631 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5632 * non-implemented, thermal sensors return 0x80 when
5633 * not available
5636 ta1 = ta2 = 0;
5637 for (i = 0; i < 8; i++) {
5638 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5639 ta1 |= t;
5640 } else {
5641 ta1 = 0;
5642 break;
5644 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5645 ta2 |= t;
5646 } else {
5647 ta1 = 0;
5648 break;
5651 if (ta1 == 0) {
5652 /* This is sheer paranoia, but we handle it anyway */
5653 if (acpi_tmp7) {
5654 printk(TPACPI_ERR
5655 "ThinkPad ACPI EC access misbehaving, "
5656 "falling back to ACPI TMPx access "
5657 "mode\n");
5658 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5659 } else {
5660 printk(TPACPI_ERR
5661 "ThinkPad ACPI EC access misbehaving, "
5662 "disabling thermal sensors access\n");
5663 thermal_read_mode = TPACPI_THERMAL_NONE;
5665 } else {
5666 thermal_read_mode =
5667 (ta2 != 0) ?
5668 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5670 } else if (acpi_tmp7) {
5671 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5672 /* 600e/x, 770e, 770x */
5673 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5674 } else {
5675 /* Standard ACPI TMPx access, max 8 sensors */
5676 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5678 } else {
5679 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5680 thermal_read_mode = TPACPI_THERMAL_NONE;
5683 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5684 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5685 thermal_read_mode);
5687 switch (thermal_read_mode) {
5688 case TPACPI_THERMAL_TPEC_16:
5689 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5690 &thermal_temp_input16_group);
5691 if (res)
5692 return res;
5693 break;
5694 case TPACPI_THERMAL_TPEC_8:
5695 case TPACPI_THERMAL_ACPI_TMP07:
5696 case TPACPI_THERMAL_ACPI_UPDT:
5697 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5698 &thermal_temp_input8_group);
5699 if (res)
5700 return res;
5701 break;
5702 case TPACPI_THERMAL_NONE:
5703 default:
5704 return 1;
5707 return 0;
5710 static void thermal_exit(void)
5712 switch (thermal_read_mode) {
5713 case TPACPI_THERMAL_TPEC_16:
5714 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5715 &thermal_temp_input16_group);
5716 break;
5717 case TPACPI_THERMAL_TPEC_8:
5718 case TPACPI_THERMAL_ACPI_TMP07:
5719 case TPACPI_THERMAL_ACPI_UPDT:
5720 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5721 &thermal_temp_input16_group);
5722 break;
5723 case TPACPI_THERMAL_NONE:
5724 default:
5725 break;
5729 static int thermal_read(char *p)
5731 int len = 0;
5732 int n, i;
5733 struct ibm_thermal_sensors_struct t;
5735 n = thermal_get_sensors(&t);
5736 if (unlikely(n < 0))
5737 return n;
5739 len += sprintf(p + len, "temperatures:\t");
5741 if (n > 0) {
5742 for (i = 0; i < (n - 1); i++)
5743 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5744 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5745 } else
5746 len += sprintf(p + len, "not supported\n");
5748 return len;
5751 static struct ibm_struct thermal_driver_data = {
5752 .name = "thermal",
5753 .read = thermal_read,
5754 .exit = thermal_exit,
5757 /*************************************************************************
5758 * EC Dump subdriver
5761 static u8 ecdump_regs[256];
5763 static int ecdump_read(char *p)
5765 int len = 0;
5766 int i, j;
5767 u8 v;
5769 len += sprintf(p + len, "EC "
5770 " +00 +01 +02 +03 +04 +05 +06 +07"
5771 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5772 for (i = 0; i < 256; i += 16) {
5773 len += sprintf(p + len, "EC 0x%02x:", i);
5774 for (j = 0; j < 16; j++) {
5775 if (!acpi_ec_read(i + j, &v))
5776 break;
5777 if (v != ecdump_regs[i + j])
5778 len += sprintf(p + len, " *%02x", v);
5779 else
5780 len += sprintf(p + len, " %02x", v);
5781 ecdump_regs[i + j] = v;
5783 len += sprintf(p + len, "\n");
5784 if (j != 16)
5785 break;
5788 /* These are way too dangerous to advertise openly... */
5789 #if 0
5790 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5791 " (<offset> is 00-ff, <value> is 00-ff)\n");
5792 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5793 " (<offset> is 00-ff, <value> is 0-255)\n");
5794 #endif
5795 return len;
5798 static int ecdump_write(char *buf)
5800 char *cmd;
5801 int i, v;
5803 while ((cmd = next_cmd(&buf))) {
5804 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5805 /* i and v set */
5806 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5807 /* i and v set */
5808 } else
5809 return -EINVAL;
5810 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5811 if (!acpi_ec_write(i, v))
5812 return -EIO;
5813 } else
5814 return -EINVAL;
5817 return 0;
5820 static struct ibm_struct ecdump_driver_data = {
5821 .name = "ecdump",
5822 .read = ecdump_read,
5823 .write = ecdump_write,
5824 .flags.experimental = 1,
5827 /*************************************************************************
5828 * Backlight/brightness subdriver
5831 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5834 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5835 * CMOS NVRAM byte 0x5E, bits 0-3.
5837 * EC HBRV (0x31) has the following layout
5838 * Bit 7: unknown function
5839 * Bit 6: unknown function
5840 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5841 * Bit 4: must be set to zero to avoid problems
5842 * Bit 3-0: backlight brightness level
5844 * brightness_get_raw returns status data in the HBRV layout
5846 * WARNING: The X61 has been verified to use HBRV for something else, so
5847 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5848 * testing on the very early *60 Lenovo models...
5851 enum {
5852 TP_EC_BACKLIGHT = 0x31,
5854 /* TP_EC_BACKLIGHT bitmasks */
5855 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5856 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5857 TP_EC_BACKLIGHT_MAPSW = 0x20,
5860 enum tpacpi_brightness_access_mode {
5861 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5862 TPACPI_BRGHT_MODE_EC, /* EC control */
5863 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5864 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5865 TPACPI_BRGHT_MODE_MAX
5868 static struct backlight_device *ibm_backlight_device;
5870 static enum tpacpi_brightness_access_mode brightness_mode =
5871 TPACPI_BRGHT_MODE_MAX;
5873 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5875 static struct mutex brightness_mutex;
5877 /* NVRAM brightness access,
5878 * call with brightness_mutex held! */
5879 static unsigned int tpacpi_brightness_nvram_get(void)
5881 u8 lnvram;
5883 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5884 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5885 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5886 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5888 return lnvram;
5891 static void tpacpi_brightness_checkpoint_nvram(void)
5893 u8 lec = 0;
5894 u8 b_nvram;
5896 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5897 return;
5899 vdbg_printk(TPACPI_DBG_BRGHT,
5900 "trying to checkpoint backlight level to NVRAM...\n");
5902 if (mutex_lock_killable(&brightness_mutex) < 0)
5903 return;
5905 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5906 goto unlock;
5907 lec &= TP_EC_BACKLIGHT_LVLMSK;
5908 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5910 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5911 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5912 /* NVRAM needs update */
5913 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5914 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5915 b_nvram |= lec;
5916 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5917 dbg_printk(TPACPI_DBG_BRGHT,
5918 "updated NVRAM backlight level to %u (0x%02x)\n",
5919 (unsigned int) lec, (unsigned int) b_nvram);
5920 } else
5921 vdbg_printk(TPACPI_DBG_BRGHT,
5922 "NVRAM backlight level already is %u (0x%02x)\n",
5923 (unsigned int) lec, (unsigned int) b_nvram);
5925 unlock:
5926 mutex_unlock(&brightness_mutex);
5930 /* call with brightness_mutex held! */
5931 static int tpacpi_brightness_get_raw(int *status)
5933 u8 lec = 0;
5935 switch (brightness_mode) {
5936 case TPACPI_BRGHT_MODE_UCMS_STEP:
5937 *status = tpacpi_brightness_nvram_get();
5938 return 0;
5939 case TPACPI_BRGHT_MODE_EC:
5940 case TPACPI_BRGHT_MODE_ECNVRAM:
5941 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5942 return -EIO;
5943 *status = lec;
5944 return 0;
5945 default:
5946 return -ENXIO;
5950 /* call with brightness_mutex held! */
5951 /* do NOT call with illegal backlight level value */
5952 static int tpacpi_brightness_set_ec(unsigned int value)
5954 u8 lec = 0;
5956 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5957 return -EIO;
5959 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5960 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5961 (value & TP_EC_BACKLIGHT_LVLMSK))))
5962 return -EIO;
5964 return 0;
5967 /* call with brightness_mutex held! */
5968 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5970 int cmos_cmd, inc;
5971 unsigned int current_value, i;
5973 current_value = tpacpi_brightness_nvram_get();
5975 if (value == current_value)
5976 return 0;
5978 cmos_cmd = (value > current_value) ?
5979 TP_CMOS_BRIGHTNESS_UP :
5980 TP_CMOS_BRIGHTNESS_DOWN;
5981 inc = (value > current_value) ? 1 : -1;
5983 for (i = current_value; i != value; i += inc)
5984 if (issue_thinkpad_cmos_command(cmos_cmd))
5985 return -EIO;
5987 return 0;
5990 /* May return EINTR which can always be mapped to ERESTARTSYS */
5991 static int brightness_set(unsigned int value)
5993 int res;
5995 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5996 value < 0)
5997 return -EINVAL;
5999 vdbg_printk(TPACPI_DBG_BRGHT,
6000 "set backlight level to %d\n", value);
6002 res = mutex_lock_killable(&brightness_mutex);
6003 if (res < 0)
6004 return res;
6006 switch (brightness_mode) {
6007 case TPACPI_BRGHT_MODE_EC:
6008 case TPACPI_BRGHT_MODE_ECNVRAM:
6009 res = tpacpi_brightness_set_ec(value);
6010 break;
6011 case TPACPI_BRGHT_MODE_UCMS_STEP:
6012 res = tpacpi_brightness_set_ucmsstep(value);
6013 break;
6014 default:
6015 res = -ENXIO;
6018 mutex_unlock(&brightness_mutex);
6019 return res;
6022 /* sysfs backlight class ----------------------------------------------- */
6024 static int brightness_update_status(struct backlight_device *bd)
6026 unsigned int level =
6027 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6028 bd->props.power == FB_BLANK_UNBLANK) ?
6029 bd->props.brightness : 0;
6031 dbg_printk(TPACPI_DBG_BRGHT,
6032 "backlight: attempt to set level to %d\n",
6033 level);
6035 /* it is the backlight class's job (caller) to handle
6036 * EINTR and other errors properly */
6037 return brightness_set(level);
6040 static int brightness_get(struct backlight_device *bd)
6042 int status, res;
6044 res = mutex_lock_killable(&brightness_mutex);
6045 if (res < 0)
6046 return 0;
6048 res = tpacpi_brightness_get_raw(&status);
6050 mutex_unlock(&brightness_mutex);
6052 if (res < 0)
6053 return 0;
6055 return status & TP_EC_BACKLIGHT_LVLMSK;
6058 static void tpacpi_brightness_notify_change(void)
6060 backlight_force_update(ibm_backlight_device,
6061 BACKLIGHT_UPDATE_HOTKEY);
6064 static struct backlight_ops ibm_backlight_data = {
6065 .get_brightness = brightness_get,
6066 .update_status = brightness_update_status,
6069 /* --------------------------------------------------------------------- */
6072 * These are only useful for models that have only one possibility
6073 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6074 * these quirks.
6076 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6077 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6078 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6080 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6081 /* Models with ATI GPUs known to require ECNVRAM mode */
6082 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6084 /* Models with ATI GPUs that can use ECNVRAM */
6085 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6086 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6087 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6088 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6090 /* Models with Intel Extreme Graphics 2 */
6091 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6092 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6093 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6095 /* Models with Intel GMA900 */
6096 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6097 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6098 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6101 static int __init brightness_init(struct ibm_init_struct *iibm)
6103 int b;
6104 unsigned long quirks;
6106 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6108 mutex_init(&brightness_mutex);
6110 quirks = tpacpi_check_quirks(brightness_quirk_table,
6111 ARRAY_SIZE(brightness_quirk_table));
6114 * We always attempt to detect acpi support, so as to switch
6115 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6116 * going to publish a backlight interface
6118 b = tpacpi_check_std_acpi_brightness_support();
6119 if (b > 0) {
6121 if (acpi_video_backlight_support()) {
6122 if (brightness_enable > 1) {
6123 printk(TPACPI_NOTICE
6124 "Standard ACPI backlight interface "
6125 "available, not loading native one.\n");
6126 return 1;
6127 } else if (brightness_enable == 1) {
6128 printk(TPACPI_NOTICE
6129 "Backlight control force enabled, even if standard "
6130 "ACPI backlight interface is available\n");
6132 } else {
6133 if (brightness_enable > 1) {
6134 printk(TPACPI_NOTICE
6135 "Standard ACPI backlight interface not "
6136 "available, thinkpad_acpi native "
6137 "brightness control enabled\n");
6142 if (!brightness_enable) {
6143 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6144 "brightness support disabled by "
6145 "module parameter\n");
6146 return 1;
6149 if (b > 16) {
6150 printk(TPACPI_ERR
6151 "Unsupported brightness interface, "
6152 "please contact %s\n", TPACPI_MAIL);
6153 return 1;
6155 if (b == 16)
6156 tp_features.bright_16levels = 1;
6159 * Check for module parameter bogosity, note that we
6160 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6161 * able to detect "unspecified"
6163 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6164 return -EINVAL;
6166 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6167 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6168 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6169 if (quirks & TPACPI_BRGHT_Q_EC)
6170 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6171 else
6172 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6174 dbg_printk(TPACPI_DBG_BRGHT,
6175 "driver auto-selected brightness_mode=%d\n",
6176 brightness_mode);
6179 /* Safety */
6180 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6181 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6182 brightness_mode == TPACPI_BRGHT_MODE_EC))
6183 return -EINVAL;
6185 if (tpacpi_brightness_get_raw(&b) < 0)
6186 return 1;
6188 if (tp_features.bright_16levels)
6189 printk(TPACPI_INFO
6190 "detected a 16-level brightness capable ThinkPad\n");
6192 ibm_backlight_device = backlight_device_register(
6193 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6194 &ibm_backlight_data);
6195 if (IS_ERR(ibm_backlight_device)) {
6196 int rc = PTR_ERR(ibm_backlight_device);
6197 ibm_backlight_device = NULL;
6198 printk(TPACPI_ERR "Could not register backlight device\n");
6199 return rc;
6201 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6202 "brightness is supported\n");
6204 if (quirks & TPACPI_BRGHT_Q_ASK) {
6205 printk(TPACPI_NOTICE
6206 "brightness: will use unverified default: "
6207 "brightness_mode=%d\n", brightness_mode);
6208 printk(TPACPI_NOTICE
6209 "brightness: please report to %s whether it works well "
6210 "or not on your ThinkPad\n", TPACPI_MAIL);
6213 ibm_backlight_device->props.max_brightness =
6214 (tp_features.bright_16levels)? 15 : 7;
6215 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6216 backlight_update_status(ibm_backlight_device);
6218 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6219 "brightness: registering brightness hotkeys "
6220 "as change notification\n");
6221 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6222 | TP_ACPI_HKEY_BRGHTUP_MASK
6223 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6224 return 0;
6227 static void brightness_suspend(pm_message_t state)
6229 tpacpi_brightness_checkpoint_nvram();
6232 static void brightness_shutdown(void)
6234 tpacpi_brightness_checkpoint_nvram();
6237 static void brightness_exit(void)
6239 if (ibm_backlight_device) {
6240 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6241 "calling backlight_device_unregister()\n");
6242 backlight_device_unregister(ibm_backlight_device);
6245 tpacpi_brightness_checkpoint_nvram();
6248 static int brightness_read(char *p)
6250 int len = 0;
6251 int level;
6253 level = brightness_get(NULL);
6254 if (level < 0) {
6255 len += sprintf(p + len, "level:\t\tunreadable\n");
6256 } else {
6257 len += sprintf(p + len, "level:\t\t%d\n", level);
6258 len += sprintf(p + len, "commands:\tup, down\n");
6259 len += sprintf(p + len, "commands:\tlevel <level>"
6260 " (<level> is 0-%d)\n",
6261 (tp_features.bright_16levels) ? 15 : 7);
6264 return len;
6267 static int brightness_write(char *buf)
6269 int level;
6270 int rc;
6271 char *cmd;
6272 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6274 level = brightness_get(NULL);
6275 if (level < 0)
6276 return level;
6278 while ((cmd = next_cmd(&buf))) {
6279 if (strlencmp(cmd, "up") == 0) {
6280 if (level < max_level)
6281 level++;
6282 } else if (strlencmp(cmd, "down") == 0) {
6283 if (level > 0)
6284 level--;
6285 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6286 level >= 0 && level <= max_level) {
6287 /* new level set */
6288 } else
6289 return -EINVAL;
6292 tpacpi_disclose_usertask("procfs brightness",
6293 "set level to %d\n", level);
6296 * Now we know what the final level should be, so we try to set it.
6297 * Doing it this way makes the syscall restartable in case of EINTR
6299 rc = brightness_set(level);
6300 if (!rc && ibm_backlight_device)
6301 backlight_force_update(ibm_backlight_device,
6302 BACKLIGHT_UPDATE_SYSFS);
6303 return (rc == -EINTR)? ERESTARTSYS : rc;
6306 static struct ibm_struct brightness_driver_data = {
6307 .name = "brightness",
6308 .read = brightness_read,
6309 .write = brightness_write,
6310 .exit = brightness_exit,
6311 .suspend = brightness_suspend,
6312 .shutdown = brightness_shutdown,
6315 /*************************************************************************
6316 * Volume subdriver
6319 static int volume_offset = 0x30;
6321 static int volume_read(char *p)
6323 int len = 0;
6324 u8 level;
6326 if (!acpi_ec_read(volume_offset, &level)) {
6327 len += sprintf(p + len, "level:\t\tunreadable\n");
6328 } else {
6329 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6330 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6331 len += sprintf(p + len, "commands:\tup, down, mute\n");
6332 len += sprintf(p + len, "commands:\tlevel <level>"
6333 " (<level> is 0-15)\n");
6336 return len;
6339 static int volume_write(char *buf)
6341 int cmos_cmd, inc, i;
6342 u8 level, mute;
6343 int new_level, new_mute;
6344 char *cmd;
6346 while ((cmd = next_cmd(&buf))) {
6347 if (!acpi_ec_read(volume_offset, &level))
6348 return -EIO;
6349 new_mute = mute = level & 0x40;
6350 new_level = level = level & 0xf;
6352 if (strlencmp(cmd, "up") == 0) {
6353 if (mute)
6354 new_mute = 0;
6355 else
6356 new_level = level == 15 ? 15 : level + 1;
6357 } else if (strlencmp(cmd, "down") == 0) {
6358 if (mute)
6359 new_mute = 0;
6360 else
6361 new_level = level == 0 ? 0 : level - 1;
6362 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6363 new_level >= 0 && new_level <= 15) {
6364 /* new_level set */
6365 } else if (strlencmp(cmd, "mute") == 0) {
6366 new_mute = 0x40;
6367 } else
6368 return -EINVAL;
6370 if (new_level != level) {
6371 /* mute doesn't change */
6373 cmos_cmd = (new_level > level) ?
6374 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6375 inc = new_level > level ? 1 : -1;
6377 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6378 !acpi_ec_write(volume_offset, level)))
6379 return -EIO;
6381 for (i = level; i != new_level; i += inc)
6382 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6383 !acpi_ec_write(volume_offset, i + inc))
6384 return -EIO;
6386 if (mute &&
6387 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6388 !acpi_ec_write(volume_offset, new_level + mute))) {
6389 return -EIO;
6393 if (new_mute != mute) {
6394 /* level doesn't change */
6396 cmos_cmd = (new_mute) ?
6397 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6399 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6400 !acpi_ec_write(volume_offset, level + new_mute))
6401 return -EIO;
6405 return 0;
6408 static struct ibm_struct volume_driver_data = {
6409 .name = "volume",
6410 .read = volume_read,
6411 .write = volume_write,
6414 /*************************************************************************
6415 * Fan subdriver
6419 * FAN ACCESS MODES
6421 * TPACPI_FAN_RD_ACPI_GFAN:
6422 * ACPI GFAN method: returns fan level
6424 * see TPACPI_FAN_WR_ACPI_SFAN
6425 * EC 0x2f (HFSP) not available if GFAN exists
6427 * TPACPI_FAN_WR_ACPI_SFAN:
6428 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6430 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6431 * it for writing.
6433 * TPACPI_FAN_WR_TPEC:
6434 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6435 * Supported on almost all ThinkPads
6437 * Fan speed changes of any sort (including those caused by the
6438 * disengaged mode) are usually done slowly by the firmware as the
6439 * maximum ammount of fan duty cycle change per second seems to be
6440 * limited.
6442 * Reading is not available if GFAN exists.
6443 * Writing is not available if SFAN exists.
6445 * Bits
6446 * 7 automatic mode engaged;
6447 * (default operation mode of the ThinkPad)
6448 * fan level is ignored in this mode.
6449 * 6 full speed mode (takes precedence over bit 7);
6450 * not available on all thinkpads. May disable
6451 * the tachometer while the fan controller ramps up
6452 * the speed (which can take up to a few *minutes*).
6453 * Speeds up fan to 100% duty-cycle, which is far above
6454 * the standard RPM levels. It is not impossible that
6455 * it could cause hardware damage.
6456 * 5-3 unused in some models. Extra bits for fan level
6457 * in others, but still useless as all values above
6458 * 7 map to the same speed as level 7 in these models.
6459 * 2-0 fan level (0..7 usually)
6460 * 0x00 = stop
6461 * 0x07 = max (set when temperatures critical)
6462 * Some ThinkPads may have other levels, see
6463 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6465 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6466 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6467 * does so, its initial value is meaningless (0x07).
6469 * For firmware bugs, refer to:
6470 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6472 * ----
6474 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6475 * Main fan tachometer reading (in RPM)
6477 * This register is present on all ThinkPads with a new-style EC, and
6478 * it is known not to be present on the A21m/e, and T22, as there is
6479 * something else in offset 0x84 according to the ACPI DSDT. Other
6480 * ThinkPads from this same time period (and earlier) probably lack the
6481 * tachometer as well.
6483 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6484 * was never fixed by IBM to report the EC firmware version string
6485 * probably support the tachometer (like the early X models), so
6486 * detecting it is quite hard. We need more data to know for sure.
6488 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6489 * might result.
6491 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6492 * mode.
6494 * For firmware bugs, refer to:
6495 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6497 * ----
6499 * ThinkPad EC register 0x31 bit 0 (only on select models)
6501 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6502 * show the speed of the main fan. When bit 0 of EC register 0x31
6503 * is one, the tachometer registers show the speed of the auxiliary
6504 * fan.
6506 * Fan control seems to affect both fans, regardless of the state
6507 * of this bit.
6509 * So far, only the firmware for the X60/X61 non-tablet versions
6510 * seem to support this (firmware TP-7M).
6512 * TPACPI_FAN_WR_ACPI_FANS:
6513 * ThinkPad X31, X40, X41. Not available in the X60.
6515 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6516 * high speed. ACPI DSDT seems to map these three speeds to levels
6517 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6518 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6520 * The speeds are stored on handles
6521 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6523 * There are three default speed sets, acessible as handles:
6524 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6526 * ACPI DSDT switches which set is in use depending on various
6527 * factors.
6529 * TPACPI_FAN_WR_TPEC is also available and should be used to
6530 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6531 * but the ACPI tables just mention level 7.
6534 enum { /* Fan control constants */
6535 fan_status_offset = 0x2f, /* EC register 0x2f */
6536 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6537 * 0x84 must be read before 0x85 */
6538 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6539 bit 0 selects which fan is active */
6541 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6542 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6544 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6547 enum fan_status_access_mode {
6548 TPACPI_FAN_NONE = 0, /* No fan status or control */
6549 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6550 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6553 enum fan_control_access_mode {
6554 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6555 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6556 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6557 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6560 enum fan_control_commands {
6561 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6562 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6563 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6564 * and also watchdog cmd */
6567 static int fan_control_allowed;
6569 static enum fan_status_access_mode fan_status_access_mode;
6570 static enum fan_control_access_mode fan_control_access_mode;
6571 static enum fan_control_commands fan_control_commands;
6573 static u8 fan_control_initial_status;
6574 static u8 fan_control_desired_level;
6575 static u8 fan_control_resume_level;
6576 static int fan_watchdog_maxinterval;
6578 static struct mutex fan_mutex;
6580 static void fan_watchdog_fire(struct work_struct *ignored);
6581 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6583 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6584 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6585 "\\FSPD", /* 600e/x, 770e, 770x */
6586 ); /* all others */
6587 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6588 "JFNS", /* 770x-JL */
6589 ); /* all others */
6592 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6593 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6594 * be in auto mode (0x80).
6596 * This is corrected by any write to HFSP either by the driver, or
6597 * by the firmware.
6599 * We assume 0x07 really means auto mode while this quirk is active,
6600 * as this is far more likely than the ThinkPad being in level 7,
6601 * which is only used by the firmware during thermal emergencies.
6603 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6604 * TP-70 (T43, R52), which are known to be buggy.
6607 static void fan_quirk1_setup(void)
6609 if (fan_control_initial_status == 0x07) {
6610 printk(TPACPI_NOTICE
6611 "fan_init: initial fan status is unknown, "
6612 "assuming it is in auto mode\n");
6613 tp_features.fan_ctrl_status_undef = 1;
6617 static void fan_quirk1_handle(u8 *fan_status)
6619 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6620 if (*fan_status != fan_control_initial_status) {
6621 /* something changed the HFSP regisnter since
6622 * driver init time, so it is not undefined
6623 * anymore */
6624 tp_features.fan_ctrl_status_undef = 0;
6625 } else {
6626 /* Return most likely status. In fact, it
6627 * might be the only possible status */
6628 *fan_status = TP_EC_FAN_AUTO;
6633 /* Select main fan on X60/X61, NOOP on others */
6634 static bool fan_select_fan1(void)
6636 if (tp_features.second_fan) {
6637 u8 val;
6639 if (ec_read(fan_select_offset, &val) < 0)
6640 return false;
6641 val &= 0xFEU;
6642 if (ec_write(fan_select_offset, val) < 0)
6643 return false;
6645 return true;
6648 /* Select secondary fan on X60/X61 */
6649 static bool fan_select_fan2(void)
6651 u8 val;
6653 if (!tp_features.second_fan)
6654 return false;
6656 if (ec_read(fan_select_offset, &val) < 0)
6657 return false;
6658 val |= 0x01U;
6659 if (ec_write(fan_select_offset, val) < 0)
6660 return false;
6662 return true;
6666 * Call with fan_mutex held
6668 static void fan_update_desired_level(u8 status)
6670 if ((status &
6671 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6672 if (status > 7)
6673 fan_control_desired_level = 7;
6674 else
6675 fan_control_desired_level = status;
6679 static int fan_get_status(u8 *status)
6681 u8 s;
6683 /* TODO:
6684 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6686 switch (fan_status_access_mode) {
6687 case TPACPI_FAN_RD_ACPI_GFAN:
6688 /* 570, 600e/x, 770e, 770x */
6690 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6691 return -EIO;
6693 if (likely(status))
6694 *status = s & 0x07;
6696 break;
6698 case TPACPI_FAN_RD_TPEC:
6699 /* all except 570, 600e/x, 770e, 770x */
6700 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6701 return -EIO;
6703 if (likely(status)) {
6704 *status = s;
6705 fan_quirk1_handle(status);
6708 break;
6710 default:
6711 return -ENXIO;
6714 return 0;
6717 static int fan_get_status_safe(u8 *status)
6719 int rc;
6720 u8 s;
6722 if (mutex_lock_killable(&fan_mutex))
6723 return -ERESTARTSYS;
6724 rc = fan_get_status(&s);
6725 if (!rc)
6726 fan_update_desired_level(s);
6727 mutex_unlock(&fan_mutex);
6729 if (status)
6730 *status = s;
6732 return rc;
6735 static int fan_get_speed(unsigned int *speed)
6737 u8 hi, lo;
6739 switch (fan_status_access_mode) {
6740 case TPACPI_FAN_RD_TPEC:
6741 /* all except 570, 600e/x, 770e, 770x */
6742 if (unlikely(!fan_select_fan1()))
6743 return -EIO;
6744 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6745 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6746 return -EIO;
6748 if (likely(speed))
6749 *speed = (hi << 8) | lo;
6751 break;
6753 default:
6754 return -ENXIO;
6757 return 0;
6760 static int fan2_get_speed(unsigned int *speed)
6762 u8 hi, lo;
6763 bool rc;
6765 switch (fan_status_access_mode) {
6766 case TPACPI_FAN_RD_TPEC:
6767 /* all except 570, 600e/x, 770e, 770x */
6768 if (unlikely(!fan_select_fan2()))
6769 return -EIO;
6770 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6771 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6772 fan_select_fan1(); /* play it safe */
6773 if (rc)
6774 return -EIO;
6776 if (likely(speed))
6777 *speed = (hi << 8) | lo;
6779 break;
6781 default:
6782 return -ENXIO;
6785 return 0;
6788 static int fan_set_level(int level)
6790 if (!fan_control_allowed)
6791 return -EPERM;
6793 switch (fan_control_access_mode) {
6794 case TPACPI_FAN_WR_ACPI_SFAN:
6795 if (level >= 0 && level <= 7) {
6796 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6797 return -EIO;
6798 } else
6799 return -EINVAL;
6800 break;
6802 case TPACPI_FAN_WR_ACPI_FANS:
6803 case TPACPI_FAN_WR_TPEC:
6804 if (!(level & TP_EC_FAN_AUTO) &&
6805 !(level & TP_EC_FAN_FULLSPEED) &&
6806 ((level < 0) || (level > 7)))
6807 return -EINVAL;
6809 /* safety net should the EC not support AUTO
6810 * or FULLSPEED mode bits and just ignore them */
6811 if (level & TP_EC_FAN_FULLSPEED)
6812 level |= 7; /* safety min speed 7 */
6813 else if (level & TP_EC_FAN_AUTO)
6814 level |= 4; /* safety min speed 4 */
6816 if (!acpi_ec_write(fan_status_offset, level))
6817 return -EIO;
6818 else
6819 tp_features.fan_ctrl_status_undef = 0;
6820 break;
6822 default:
6823 return -ENXIO;
6826 vdbg_printk(TPACPI_DBG_FAN,
6827 "fan control: set fan control register to 0x%02x\n", level);
6828 return 0;
6831 static int fan_set_level_safe(int level)
6833 int rc;
6835 if (!fan_control_allowed)
6836 return -EPERM;
6838 if (mutex_lock_killable(&fan_mutex))
6839 return -ERESTARTSYS;
6841 if (level == TPACPI_FAN_LAST_LEVEL)
6842 level = fan_control_desired_level;
6844 rc = fan_set_level(level);
6845 if (!rc)
6846 fan_update_desired_level(level);
6848 mutex_unlock(&fan_mutex);
6849 return rc;
6852 static int fan_set_enable(void)
6854 u8 s;
6855 int rc;
6857 if (!fan_control_allowed)
6858 return -EPERM;
6860 if (mutex_lock_killable(&fan_mutex))
6861 return -ERESTARTSYS;
6863 switch (fan_control_access_mode) {
6864 case TPACPI_FAN_WR_ACPI_FANS:
6865 case TPACPI_FAN_WR_TPEC:
6866 rc = fan_get_status(&s);
6867 if (rc < 0)
6868 break;
6870 /* Don't go out of emergency fan mode */
6871 if (s != 7) {
6872 s &= 0x07;
6873 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6876 if (!acpi_ec_write(fan_status_offset, s))
6877 rc = -EIO;
6878 else {
6879 tp_features.fan_ctrl_status_undef = 0;
6880 rc = 0;
6882 break;
6884 case TPACPI_FAN_WR_ACPI_SFAN:
6885 rc = fan_get_status(&s);
6886 if (rc < 0)
6887 break;
6889 s &= 0x07;
6891 /* Set fan to at least level 4 */
6892 s |= 4;
6894 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6895 rc = -EIO;
6896 else
6897 rc = 0;
6898 break;
6900 default:
6901 rc = -ENXIO;
6904 mutex_unlock(&fan_mutex);
6906 if (!rc)
6907 vdbg_printk(TPACPI_DBG_FAN,
6908 "fan control: set fan control register to 0x%02x\n",
6910 return rc;
6913 static int fan_set_disable(void)
6915 int rc;
6917 if (!fan_control_allowed)
6918 return -EPERM;
6920 if (mutex_lock_killable(&fan_mutex))
6921 return -ERESTARTSYS;
6923 rc = 0;
6924 switch (fan_control_access_mode) {
6925 case TPACPI_FAN_WR_ACPI_FANS:
6926 case TPACPI_FAN_WR_TPEC:
6927 if (!acpi_ec_write(fan_status_offset, 0x00))
6928 rc = -EIO;
6929 else {
6930 fan_control_desired_level = 0;
6931 tp_features.fan_ctrl_status_undef = 0;
6933 break;
6935 case TPACPI_FAN_WR_ACPI_SFAN:
6936 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6937 rc = -EIO;
6938 else
6939 fan_control_desired_level = 0;
6940 break;
6942 default:
6943 rc = -ENXIO;
6946 if (!rc)
6947 vdbg_printk(TPACPI_DBG_FAN,
6948 "fan control: set fan control register to 0\n");
6950 mutex_unlock(&fan_mutex);
6951 return rc;
6954 static int fan_set_speed(int speed)
6956 int rc;
6958 if (!fan_control_allowed)
6959 return -EPERM;
6961 if (mutex_lock_killable(&fan_mutex))
6962 return -ERESTARTSYS;
6964 rc = 0;
6965 switch (fan_control_access_mode) {
6966 case TPACPI_FAN_WR_ACPI_FANS:
6967 if (speed >= 0 && speed <= 65535) {
6968 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6969 speed, speed, speed))
6970 rc = -EIO;
6971 } else
6972 rc = -EINVAL;
6973 break;
6975 default:
6976 rc = -ENXIO;
6979 mutex_unlock(&fan_mutex);
6980 return rc;
6983 static void fan_watchdog_reset(void)
6985 static int fan_watchdog_active;
6987 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6988 return;
6990 if (fan_watchdog_active)
6991 cancel_delayed_work(&fan_watchdog_task);
6993 if (fan_watchdog_maxinterval > 0 &&
6994 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6995 fan_watchdog_active = 1;
6996 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6997 msecs_to_jiffies(fan_watchdog_maxinterval
6998 * 1000))) {
6999 printk(TPACPI_ERR
7000 "failed to queue the fan watchdog, "
7001 "watchdog will not trigger\n");
7003 } else
7004 fan_watchdog_active = 0;
7007 static void fan_watchdog_fire(struct work_struct *ignored)
7009 int rc;
7011 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7012 return;
7014 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7015 rc = fan_set_enable();
7016 if (rc < 0) {
7017 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7018 "will try again later...\n", -rc);
7019 /* reschedule for later */
7020 fan_watchdog_reset();
7025 * SYSFS fan layout: hwmon compatible (device)
7027 * pwm*_enable:
7028 * 0: "disengaged" mode
7029 * 1: manual mode
7030 * 2: native EC "auto" mode (recommended, hardware default)
7032 * pwm*: set speed in manual mode, ignored otherwise.
7033 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7034 * interpolation.
7036 * fan*_input: tachometer reading, RPM
7039 * SYSFS fan layout: extensions
7041 * fan_watchdog (driver):
7042 * fan watchdog interval in seconds, 0 disables (default), max 120
7045 /* sysfs fan pwm1_enable ----------------------------------------------- */
7046 static ssize_t fan_pwm1_enable_show(struct device *dev,
7047 struct device_attribute *attr,
7048 char *buf)
7050 int res, mode;
7051 u8 status;
7053 res = fan_get_status_safe(&status);
7054 if (res)
7055 return res;
7057 if (status & TP_EC_FAN_FULLSPEED) {
7058 mode = 0;
7059 } else if (status & TP_EC_FAN_AUTO) {
7060 mode = 2;
7061 } else
7062 mode = 1;
7064 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7067 static ssize_t fan_pwm1_enable_store(struct device *dev,
7068 struct device_attribute *attr,
7069 const char *buf, size_t count)
7071 unsigned long t;
7072 int res, level;
7074 if (parse_strtoul(buf, 2, &t))
7075 return -EINVAL;
7077 tpacpi_disclose_usertask("hwmon pwm1_enable",
7078 "set fan mode to %lu\n", t);
7080 switch (t) {
7081 case 0:
7082 level = TP_EC_FAN_FULLSPEED;
7083 break;
7084 case 1:
7085 level = TPACPI_FAN_LAST_LEVEL;
7086 break;
7087 case 2:
7088 level = TP_EC_FAN_AUTO;
7089 break;
7090 case 3:
7091 /* reserved for software-controlled auto mode */
7092 return -ENOSYS;
7093 default:
7094 return -EINVAL;
7097 res = fan_set_level_safe(level);
7098 if (res == -ENXIO)
7099 return -EINVAL;
7100 else if (res < 0)
7101 return res;
7103 fan_watchdog_reset();
7105 return count;
7108 static struct device_attribute dev_attr_fan_pwm1_enable =
7109 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7110 fan_pwm1_enable_show, fan_pwm1_enable_store);
7112 /* sysfs fan pwm1 ------------------------------------------------------ */
7113 static ssize_t fan_pwm1_show(struct device *dev,
7114 struct device_attribute *attr,
7115 char *buf)
7117 int res;
7118 u8 status;
7120 res = fan_get_status_safe(&status);
7121 if (res)
7122 return res;
7124 if ((status &
7125 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7126 status = fan_control_desired_level;
7128 if (status > 7)
7129 status = 7;
7131 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7134 static ssize_t fan_pwm1_store(struct device *dev,
7135 struct device_attribute *attr,
7136 const char *buf, size_t count)
7138 unsigned long s;
7139 int rc;
7140 u8 status, newlevel;
7142 if (parse_strtoul(buf, 255, &s))
7143 return -EINVAL;
7145 tpacpi_disclose_usertask("hwmon pwm1",
7146 "set fan speed to %lu\n", s);
7148 /* scale down from 0-255 to 0-7 */
7149 newlevel = (s >> 5) & 0x07;
7151 if (mutex_lock_killable(&fan_mutex))
7152 return -ERESTARTSYS;
7154 rc = fan_get_status(&status);
7155 if (!rc && (status &
7156 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7157 rc = fan_set_level(newlevel);
7158 if (rc == -ENXIO)
7159 rc = -EINVAL;
7160 else if (!rc) {
7161 fan_update_desired_level(newlevel);
7162 fan_watchdog_reset();
7166 mutex_unlock(&fan_mutex);
7167 return (rc)? rc : count;
7170 static struct device_attribute dev_attr_fan_pwm1 =
7171 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7172 fan_pwm1_show, fan_pwm1_store);
7174 /* sysfs fan fan1_input ------------------------------------------------ */
7175 static ssize_t fan_fan1_input_show(struct device *dev,
7176 struct device_attribute *attr,
7177 char *buf)
7179 int res;
7180 unsigned int speed;
7182 res = fan_get_speed(&speed);
7183 if (res < 0)
7184 return res;
7186 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7189 static struct device_attribute dev_attr_fan_fan1_input =
7190 __ATTR(fan1_input, S_IRUGO,
7191 fan_fan1_input_show, NULL);
7193 /* sysfs fan fan2_input ------------------------------------------------ */
7194 static ssize_t fan_fan2_input_show(struct device *dev,
7195 struct device_attribute *attr,
7196 char *buf)
7198 int res;
7199 unsigned int speed;
7201 res = fan2_get_speed(&speed);
7202 if (res < 0)
7203 return res;
7205 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7208 static struct device_attribute dev_attr_fan_fan2_input =
7209 __ATTR(fan2_input, S_IRUGO,
7210 fan_fan2_input_show, NULL);
7212 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7213 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7214 char *buf)
7216 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7219 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7220 const char *buf, size_t count)
7222 unsigned long t;
7224 if (parse_strtoul(buf, 120, &t))
7225 return -EINVAL;
7227 if (!fan_control_allowed)
7228 return -EPERM;
7230 fan_watchdog_maxinterval = t;
7231 fan_watchdog_reset();
7233 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7235 return count;
7238 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7239 fan_fan_watchdog_show, fan_fan_watchdog_store);
7241 /* --------------------------------------------------------------------- */
7242 static struct attribute *fan_attributes[] = {
7243 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7244 &dev_attr_fan_fan1_input.attr,
7245 NULL, /* for fan2_input */
7246 NULL
7249 static const struct attribute_group fan_attr_group = {
7250 .attrs = fan_attributes,
7253 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7254 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7256 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7257 { .vendor = PCI_VENDOR_ID_IBM, \
7258 .bios = TPACPI_MATCH_ANY, \
7259 .ec = TPID(__id1, __id2), \
7260 .quirks = __quirks }
7262 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7263 { .vendor = PCI_VENDOR_ID_LENOVO, \
7264 .bios = TPACPI_MATCH_ANY, \
7265 .ec = TPID(__id1, __id2), \
7266 .quirks = __quirks }
7268 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7269 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7270 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7271 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7272 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7273 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7276 #undef TPACPI_FAN_QL
7277 #undef TPACPI_FAN_QI
7279 static int __init fan_init(struct ibm_init_struct *iibm)
7281 int rc;
7282 unsigned long quirks;
7284 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7285 "initializing fan subdriver\n");
7287 mutex_init(&fan_mutex);
7288 fan_status_access_mode = TPACPI_FAN_NONE;
7289 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7290 fan_control_commands = 0;
7291 fan_watchdog_maxinterval = 0;
7292 tp_features.fan_ctrl_status_undef = 0;
7293 tp_features.second_fan = 0;
7294 fan_control_desired_level = 7;
7296 TPACPI_ACPIHANDLE_INIT(fans);
7297 TPACPI_ACPIHANDLE_INIT(gfan);
7298 TPACPI_ACPIHANDLE_INIT(sfan);
7300 quirks = tpacpi_check_quirks(fan_quirk_table,
7301 ARRAY_SIZE(fan_quirk_table));
7303 if (gfan_handle) {
7304 /* 570, 600e/x, 770e, 770x */
7305 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7306 } else {
7307 /* all other ThinkPads: note that even old-style
7308 * ThinkPad ECs supports the fan control register */
7309 if (likely(acpi_ec_read(fan_status_offset,
7310 &fan_control_initial_status))) {
7311 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7312 if (quirks & TPACPI_FAN_Q1)
7313 fan_quirk1_setup();
7314 if (quirks & TPACPI_FAN_2FAN) {
7315 tp_features.second_fan = 1;
7316 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7317 "secondary fan support enabled\n");
7319 } else {
7320 printk(TPACPI_ERR
7321 "ThinkPad ACPI EC access misbehaving, "
7322 "fan status and control unavailable\n");
7323 return 1;
7327 if (sfan_handle) {
7328 /* 570, 770x-JL */
7329 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7330 fan_control_commands |=
7331 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7332 } else {
7333 if (!gfan_handle) {
7334 /* gfan without sfan means no fan control */
7335 /* all other models implement TP EC 0x2f control */
7337 if (fans_handle) {
7338 /* X31, X40, X41 */
7339 fan_control_access_mode =
7340 TPACPI_FAN_WR_ACPI_FANS;
7341 fan_control_commands |=
7342 TPACPI_FAN_CMD_SPEED |
7343 TPACPI_FAN_CMD_LEVEL |
7344 TPACPI_FAN_CMD_ENABLE;
7345 } else {
7346 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7347 fan_control_commands |=
7348 TPACPI_FAN_CMD_LEVEL |
7349 TPACPI_FAN_CMD_ENABLE;
7354 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7355 "fan is %s, modes %d, %d\n",
7356 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7357 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7358 fan_status_access_mode, fan_control_access_mode);
7360 /* fan control master switch */
7361 if (!fan_control_allowed) {
7362 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7363 fan_control_commands = 0;
7364 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7365 "fan control features disabled by parameter\n");
7368 /* update fan_control_desired_level */
7369 if (fan_status_access_mode != TPACPI_FAN_NONE)
7370 fan_get_status_safe(NULL);
7372 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7373 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7374 if (tp_features.second_fan) {
7375 /* attach second fan tachometer */
7376 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7377 &dev_attr_fan_fan2_input.attr;
7379 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7380 &fan_attr_group);
7381 if (rc < 0)
7382 return rc;
7384 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7385 &driver_attr_fan_watchdog);
7386 if (rc < 0) {
7387 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7388 &fan_attr_group);
7389 return rc;
7391 return 0;
7392 } else
7393 return 1;
7396 static void fan_exit(void)
7398 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7399 "cancelling any pending fan watchdog tasks\n");
7401 /* FIXME: can we really do this unconditionally? */
7402 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7403 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7404 &driver_attr_fan_watchdog);
7406 cancel_delayed_work(&fan_watchdog_task);
7407 flush_workqueue(tpacpi_wq);
7410 static void fan_suspend(pm_message_t state)
7412 int rc;
7414 if (!fan_control_allowed)
7415 return;
7417 /* Store fan status in cache */
7418 fan_control_resume_level = 0;
7419 rc = fan_get_status_safe(&fan_control_resume_level);
7420 if (rc < 0)
7421 printk(TPACPI_NOTICE
7422 "failed to read fan level for later "
7423 "restore during resume: %d\n", rc);
7425 /* if it is undefined, don't attempt to restore it.
7426 * KEEP THIS LAST */
7427 if (tp_features.fan_ctrl_status_undef)
7428 fan_control_resume_level = 0;
7431 static void fan_resume(void)
7433 u8 current_level = 7;
7434 bool do_set = false;
7435 int rc;
7437 /* DSDT *always* updates status on resume */
7438 tp_features.fan_ctrl_status_undef = 0;
7440 if (!fan_control_allowed ||
7441 !fan_control_resume_level ||
7442 (fan_get_status_safe(&current_level) < 0))
7443 return;
7445 switch (fan_control_access_mode) {
7446 case TPACPI_FAN_WR_ACPI_SFAN:
7447 /* never decrease fan level */
7448 do_set = (fan_control_resume_level > current_level);
7449 break;
7450 case TPACPI_FAN_WR_ACPI_FANS:
7451 case TPACPI_FAN_WR_TPEC:
7452 /* never decrease fan level, scale is:
7453 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7455 * We expect the firmware to set either 7 or AUTO, but we
7456 * handle FULLSPEED out of paranoia.
7458 * So, we can safely only restore FULLSPEED or 7, anything
7459 * else could slow the fan. Restoring AUTO is useless, at
7460 * best that's exactly what the DSDT already set (it is the
7461 * slower it uses).
7463 * Always keep in mind that the DSDT *will* have set the
7464 * fans to what the vendor supposes is the best level. We
7465 * muck with it only to speed the fan up.
7467 if (fan_control_resume_level != 7 &&
7468 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7469 return;
7470 else
7471 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7472 (current_level != fan_control_resume_level);
7473 break;
7474 default:
7475 return;
7477 if (do_set) {
7478 printk(TPACPI_NOTICE
7479 "restoring fan level to 0x%02x\n",
7480 fan_control_resume_level);
7481 rc = fan_set_level_safe(fan_control_resume_level);
7482 if (rc < 0)
7483 printk(TPACPI_NOTICE
7484 "failed to restore fan level: %d\n", rc);
7488 static int fan_read(char *p)
7490 int len = 0;
7491 int rc;
7492 u8 status;
7493 unsigned int speed = 0;
7495 switch (fan_status_access_mode) {
7496 case TPACPI_FAN_RD_ACPI_GFAN:
7497 /* 570, 600e/x, 770e, 770x */
7498 rc = fan_get_status_safe(&status);
7499 if (rc < 0)
7500 return rc;
7502 len += sprintf(p + len, "status:\t\t%s\n"
7503 "level:\t\t%d\n",
7504 (status != 0) ? "enabled" : "disabled", status);
7505 break;
7507 case TPACPI_FAN_RD_TPEC:
7508 /* all except 570, 600e/x, 770e, 770x */
7509 rc = fan_get_status_safe(&status);
7510 if (rc < 0)
7511 return rc;
7513 len += sprintf(p + len, "status:\t\t%s\n",
7514 (status != 0) ? "enabled" : "disabled");
7516 rc = fan_get_speed(&speed);
7517 if (rc < 0)
7518 return rc;
7520 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7522 if (status & TP_EC_FAN_FULLSPEED)
7523 /* Disengaged mode takes precedence */
7524 len += sprintf(p + len, "level:\t\tdisengaged\n");
7525 else if (status & TP_EC_FAN_AUTO)
7526 len += sprintf(p + len, "level:\t\tauto\n");
7527 else
7528 len += sprintf(p + len, "level:\t\t%d\n", status);
7529 break;
7531 case TPACPI_FAN_NONE:
7532 default:
7533 len += sprintf(p + len, "status:\t\tnot supported\n");
7536 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7537 len += sprintf(p + len, "commands:\tlevel <level>");
7539 switch (fan_control_access_mode) {
7540 case TPACPI_FAN_WR_ACPI_SFAN:
7541 len += sprintf(p + len, " (<level> is 0-7)\n");
7542 break;
7544 default:
7545 len += sprintf(p + len, " (<level> is 0-7, "
7546 "auto, disengaged, full-speed)\n");
7547 break;
7551 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7552 len += sprintf(p + len, "commands:\tenable, disable\n"
7553 "commands:\twatchdog <timeout> (<timeout> "
7554 "is 0 (off), 1-120 (seconds))\n");
7556 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7557 len += sprintf(p + len, "commands:\tspeed <speed>"
7558 " (<speed> is 0-65535)\n");
7560 return len;
7563 static int fan_write_cmd_level(const char *cmd, int *rc)
7565 int level;
7567 if (strlencmp(cmd, "level auto") == 0)
7568 level = TP_EC_FAN_AUTO;
7569 else if ((strlencmp(cmd, "level disengaged") == 0) |
7570 (strlencmp(cmd, "level full-speed") == 0))
7571 level = TP_EC_FAN_FULLSPEED;
7572 else if (sscanf(cmd, "level %d", &level) != 1)
7573 return 0;
7575 *rc = fan_set_level_safe(level);
7576 if (*rc == -ENXIO)
7577 printk(TPACPI_ERR "level command accepted for unsupported "
7578 "access mode %d", fan_control_access_mode);
7579 else if (!*rc)
7580 tpacpi_disclose_usertask("procfs fan",
7581 "set level to %d\n", level);
7583 return 1;
7586 static int fan_write_cmd_enable(const char *cmd, int *rc)
7588 if (strlencmp(cmd, "enable") != 0)
7589 return 0;
7591 *rc = fan_set_enable();
7592 if (*rc == -ENXIO)
7593 printk(TPACPI_ERR "enable command accepted for unsupported "
7594 "access mode %d", fan_control_access_mode);
7595 else if (!*rc)
7596 tpacpi_disclose_usertask("procfs fan", "enable\n");
7598 return 1;
7601 static int fan_write_cmd_disable(const char *cmd, int *rc)
7603 if (strlencmp(cmd, "disable") != 0)
7604 return 0;
7606 *rc = fan_set_disable();
7607 if (*rc == -ENXIO)
7608 printk(TPACPI_ERR "disable command accepted for unsupported "
7609 "access mode %d", fan_control_access_mode);
7610 else if (!*rc)
7611 tpacpi_disclose_usertask("procfs fan", "disable\n");
7613 return 1;
7616 static int fan_write_cmd_speed(const char *cmd, int *rc)
7618 int speed;
7620 /* TODO:
7621 * Support speed <low> <medium> <high> ? */
7623 if (sscanf(cmd, "speed %d", &speed) != 1)
7624 return 0;
7626 *rc = fan_set_speed(speed);
7627 if (*rc == -ENXIO)
7628 printk(TPACPI_ERR "speed command accepted for unsupported "
7629 "access mode %d", fan_control_access_mode);
7630 else if (!*rc)
7631 tpacpi_disclose_usertask("procfs fan",
7632 "set speed to %d\n", speed);
7634 return 1;
7637 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7639 int interval;
7641 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7642 return 0;
7644 if (interval < 0 || interval > 120)
7645 *rc = -EINVAL;
7646 else {
7647 fan_watchdog_maxinterval = interval;
7648 tpacpi_disclose_usertask("procfs fan",
7649 "set watchdog timer to %d\n",
7650 interval);
7653 return 1;
7656 static int fan_write(char *buf)
7658 char *cmd;
7659 int rc = 0;
7661 while (!rc && (cmd = next_cmd(&buf))) {
7662 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7663 fan_write_cmd_level(cmd, &rc)) &&
7664 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7665 (fan_write_cmd_enable(cmd, &rc) ||
7666 fan_write_cmd_disable(cmd, &rc) ||
7667 fan_write_cmd_watchdog(cmd, &rc))) &&
7668 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7669 fan_write_cmd_speed(cmd, &rc))
7671 rc = -EINVAL;
7672 else if (!rc)
7673 fan_watchdog_reset();
7676 return rc;
7679 static struct ibm_struct fan_driver_data = {
7680 .name = "fan",
7681 .read = fan_read,
7682 .write = fan_write,
7683 .exit = fan_exit,
7684 .suspend = fan_suspend,
7685 .resume = fan_resume,
7688 /****************************************************************************
7689 ****************************************************************************
7691 * Infrastructure
7693 ****************************************************************************
7694 ****************************************************************************/
7697 * HKEY event callout for other subdrivers go here
7698 * (yes, it is ugly, but it is quick, safe, and gets the job done
7700 static void tpacpi_driver_event(const unsigned int hkey_event)
7702 if (ibm_backlight_device) {
7703 switch (hkey_event) {
7704 case TP_HKEY_EV_BRGHT_UP:
7705 case TP_HKEY_EV_BRGHT_DOWN:
7706 tpacpi_brightness_notify_change();
7713 static void hotkey_driver_event(const unsigned int scancode)
7715 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7718 /* sysfs name ---------------------------------------------------------- */
7719 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7720 struct device_attribute *attr,
7721 char *buf)
7723 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7726 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7727 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7729 /* --------------------------------------------------------------------- */
7731 /* /proc support */
7732 static struct proc_dir_entry *proc_dir;
7735 * Module and infrastructure proble, init and exit handling
7738 static int force_load;
7740 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7741 static const char * __init str_supported(int is_supported)
7743 static char text_unsupported[] __initdata = "not supported";
7745 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7747 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7749 static void ibm_exit(struct ibm_struct *ibm)
7751 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7753 list_del_init(&ibm->all_drivers);
7755 if (ibm->flags.acpi_notify_installed) {
7756 dbg_printk(TPACPI_DBG_EXIT,
7757 "%s: acpi_remove_notify_handler\n", ibm->name);
7758 BUG_ON(!ibm->acpi);
7759 acpi_remove_notify_handler(*ibm->acpi->handle,
7760 ibm->acpi->type,
7761 dispatch_acpi_notify);
7762 ibm->flags.acpi_notify_installed = 0;
7763 ibm->flags.acpi_notify_installed = 0;
7766 if (ibm->flags.proc_created) {
7767 dbg_printk(TPACPI_DBG_EXIT,
7768 "%s: remove_proc_entry\n", ibm->name);
7769 remove_proc_entry(ibm->name, proc_dir);
7770 ibm->flags.proc_created = 0;
7773 if (ibm->flags.acpi_driver_registered) {
7774 dbg_printk(TPACPI_DBG_EXIT,
7775 "%s: acpi_bus_unregister_driver\n", ibm->name);
7776 BUG_ON(!ibm->acpi);
7777 acpi_bus_unregister_driver(ibm->acpi->driver);
7778 kfree(ibm->acpi->driver);
7779 ibm->acpi->driver = NULL;
7780 ibm->flags.acpi_driver_registered = 0;
7783 if (ibm->flags.init_called && ibm->exit) {
7784 ibm->exit();
7785 ibm->flags.init_called = 0;
7788 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7791 static int __init ibm_init(struct ibm_init_struct *iibm)
7793 int ret;
7794 struct ibm_struct *ibm = iibm->data;
7795 struct proc_dir_entry *entry;
7797 BUG_ON(ibm == NULL);
7799 INIT_LIST_HEAD(&ibm->all_drivers);
7801 if (ibm->flags.experimental && !experimental)
7802 return 0;
7804 dbg_printk(TPACPI_DBG_INIT,
7805 "probing for %s\n", ibm->name);
7807 if (iibm->init) {
7808 ret = iibm->init(iibm);
7809 if (ret > 0)
7810 return 0; /* probe failed */
7811 if (ret)
7812 return ret;
7814 ibm->flags.init_called = 1;
7817 if (ibm->acpi) {
7818 if (ibm->acpi->hid) {
7819 ret = register_tpacpi_subdriver(ibm);
7820 if (ret)
7821 goto err_out;
7824 if (ibm->acpi->notify) {
7825 ret = setup_acpi_notify(ibm);
7826 if (ret == -ENODEV) {
7827 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7828 ibm->name);
7829 ret = 0;
7830 goto err_out;
7832 if (ret < 0)
7833 goto err_out;
7837 dbg_printk(TPACPI_DBG_INIT,
7838 "%s installed\n", ibm->name);
7840 if (ibm->read) {
7841 entry = create_proc_entry(ibm->name,
7842 S_IFREG | S_IRUGO | S_IWUSR,
7843 proc_dir);
7844 if (!entry) {
7845 printk(TPACPI_ERR "unable to create proc entry %s\n",
7846 ibm->name);
7847 ret = -ENODEV;
7848 goto err_out;
7850 entry->owner = THIS_MODULE;
7851 entry->data = ibm;
7852 entry->read_proc = &dispatch_procfs_read;
7853 if (ibm->write)
7854 entry->write_proc = &dispatch_procfs_write;
7855 ibm->flags.proc_created = 1;
7858 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7860 return 0;
7862 err_out:
7863 dbg_printk(TPACPI_DBG_INIT,
7864 "%s: at error exit path with result %d\n",
7865 ibm->name, ret);
7867 ibm_exit(ibm);
7868 return (ret < 0)? ret : 0;
7871 /* Probing */
7873 static bool __pure __init tpacpi_is_fw_digit(const char c)
7875 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7878 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7879 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7880 const char t)
7882 return s && strlen(s) >= 8 &&
7883 tpacpi_is_fw_digit(s[0]) &&
7884 tpacpi_is_fw_digit(s[1]) &&
7885 s[2] == t && s[3] == 'T' &&
7886 tpacpi_is_fw_digit(s[4]) &&
7887 tpacpi_is_fw_digit(s[5]) &&
7888 s[6] == 'W' && s[7] == 'W';
7891 /* returns 0 - probe ok, or < 0 - probe error.
7892 * Probe ok doesn't mean thinkpad found.
7893 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7894 static int __must_check __init get_thinkpad_model_data(
7895 struct thinkpad_id_data *tp)
7897 const struct dmi_device *dev = NULL;
7898 char ec_fw_string[18];
7899 char const *s;
7901 if (!tp)
7902 return -EINVAL;
7904 memset(tp, 0, sizeof(*tp));
7906 if (dmi_name_in_vendors("IBM"))
7907 tp->vendor = PCI_VENDOR_ID_IBM;
7908 else if (dmi_name_in_vendors("LENOVO"))
7909 tp->vendor = PCI_VENDOR_ID_LENOVO;
7910 else
7911 return 0;
7913 s = dmi_get_system_info(DMI_BIOS_VERSION);
7914 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7915 if (s && !tp->bios_version_str)
7916 return -ENOMEM;
7918 /* Really ancient ThinkPad 240X will fail this, which is fine */
7919 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7920 return 0;
7922 tp->bios_model = tp->bios_version_str[0]
7923 | (tp->bios_version_str[1] << 8);
7924 tp->bios_release = (tp->bios_version_str[4] << 8)
7925 | tp->bios_version_str[5];
7928 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7929 * X32 or newer, all Z series; Some models must have an
7930 * up-to-date BIOS or they will not be detected.
7932 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7934 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7935 if (sscanf(dev->name,
7936 "IBM ThinkPad Embedded Controller -[%17c",
7937 ec_fw_string) == 1) {
7938 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7939 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7941 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7942 if (!tp->ec_version_str)
7943 return -ENOMEM;
7945 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7946 tp->ec_model = ec_fw_string[0]
7947 | (ec_fw_string[1] << 8);
7948 tp->ec_release = (ec_fw_string[4] << 8)
7949 | ec_fw_string[5];
7950 } else {
7951 printk(TPACPI_NOTICE
7952 "ThinkPad firmware release %s "
7953 "doesn't match the known patterns\n",
7954 ec_fw_string);
7955 printk(TPACPI_NOTICE
7956 "please report this to %s\n",
7957 TPACPI_MAIL);
7959 break;
7963 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7964 if (s && !strnicmp(s, "ThinkPad", 8)) {
7965 tp->model_str = kstrdup(s, GFP_KERNEL);
7966 if (!tp->model_str)
7967 return -ENOMEM;
7970 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7971 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7972 if (s && !tp->nummodel_str)
7973 return -ENOMEM;
7975 return 0;
7978 static int __init probe_for_thinkpad(void)
7980 int is_thinkpad;
7982 if (acpi_disabled)
7983 return -ENODEV;
7986 * Non-ancient models have better DMI tagging, but very old models
7987 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7989 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7990 (thinkpad_id.ec_model != 0) ||
7991 tpacpi_is_fw_known();
7993 /* ec is required because many other handles are relative to it */
7994 TPACPI_ACPIHANDLE_INIT(ec);
7995 if (!ec_handle) {
7996 if (is_thinkpad)
7997 printk(TPACPI_ERR
7998 "Not yet supported ThinkPad detected!\n");
7999 return -ENODEV;
8002 if (!is_thinkpad && !force_load)
8003 return -ENODEV;
8005 return 0;
8009 /* Module init, exit, parameters */
8011 static struct ibm_init_struct ibms_init[] __initdata = {
8013 .init = thinkpad_acpi_driver_init,
8014 .data = &thinkpad_acpi_driver_data,
8017 .init = hotkey_init,
8018 .data = &hotkey_driver_data,
8021 .init = bluetooth_init,
8022 .data = &bluetooth_driver_data,
8025 .init = wan_init,
8026 .data = &wan_driver_data,
8029 .init = uwb_init,
8030 .data = &uwb_driver_data,
8032 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8034 .init = video_init,
8035 .data = &video_driver_data,
8037 #endif
8039 .init = light_init,
8040 .data = &light_driver_data,
8043 .init = cmos_init,
8044 .data = &cmos_driver_data,
8047 .init = led_init,
8048 .data = &led_driver_data,
8051 .init = beep_init,
8052 .data = &beep_driver_data,
8055 .init = thermal_init,
8056 .data = &thermal_driver_data,
8059 .data = &ecdump_driver_data,
8062 .init = brightness_init,
8063 .data = &brightness_driver_data,
8066 .data = &volume_driver_data,
8069 .init = fan_init,
8070 .data = &fan_driver_data,
8074 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8076 unsigned int i;
8077 struct ibm_struct *ibm;
8079 if (!kp || !kp->name || !val)
8080 return -EINVAL;
8082 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8083 ibm = ibms_init[i].data;
8084 WARN_ON(ibm == NULL);
8086 if (!ibm || !ibm->name)
8087 continue;
8089 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8090 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8091 return -ENOSPC;
8092 strcpy(ibms_init[i].param, val);
8093 strcat(ibms_init[i].param, ",");
8094 return 0;
8098 return -EINVAL;
8101 module_param(experimental, int, 0);
8102 MODULE_PARM_DESC(experimental,
8103 "Enables experimental features when non-zero");
8105 module_param_named(debug, dbg_level, uint, 0);
8106 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8108 module_param(force_load, bool, 0);
8109 MODULE_PARM_DESC(force_load,
8110 "Attempts to load the driver even on a "
8111 "mis-identified ThinkPad when true");
8113 module_param_named(fan_control, fan_control_allowed, bool, 0);
8114 MODULE_PARM_DESC(fan_control,
8115 "Enables setting fan parameters features when true");
8117 module_param_named(brightness_mode, brightness_mode, uint, 0);
8118 MODULE_PARM_DESC(brightness_mode,
8119 "Selects brightness control strategy: "
8120 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8122 module_param(brightness_enable, uint, 0);
8123 MODULE_PARM_DESC(brightness_enable,
8124 "Enables backlight control when 1, disables when 0");
8126 module_param(hotkey_report_mode, uint, 0);
8127 MODULE_PARM_DESC(hotkey_report_mode,
8128 "used for backwards compatibility with userspace, "
8129 "see documentation");
8131 #define TPACPI_PARAM(feature) \
8132 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8133 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8134 "at module load, see documentation")
8136 TPACPI_PARAM(hotkey);
8137 TPACPI_PARAM(bluetooth);
8138 TPACPI_PARAM(video);
8139 TPACPI_PARAM(light);
8140 TPACPI_PARAM(cmos);
8141 TPACPI_PARAM(led);
8142 TPACPI_PARAM(beep);
8143 TPACPI_PARAM(ecdump);
8144 TPACPI_PARAM(brightness);
8145 TPACPI_PARAM(volume);
8146 TPACPI_PARAM(fan);
8148 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8149 module_param(dbg_wlswemul, uint, 0);
8150 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8151 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8152 MODULE_PARM_DESC(wlsw_state,
8153 "Initial state of the emulated WLSW switch");
8155 module_param(dbg_bluetoothemul, uint, 0);
8156 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8157 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8158 MODULE_PARM_DESC(bluetooth_state,
8159 "Initial state of the emulated bluetooth switch");
8161 module_param(dbg_wwanemul, uint, 0);
8162 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8163 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8164 MODULE_PARM_DESC(wwan_state,
8165 "Initial state of the emulated WWAN switch");
8167 module_param(dbg_uwbemul, uint, 0);
8168 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8169 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8170 MODULE_PARM_DESC(uwb_state,
8171 "Initial state of the emulated UWB switch");
8172 #endif
8174 static void thinkpad_acpi_module_exit(void)
8176 struct ibm_struct *ibm, *itmp;
8178 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8180 list_for_each_entry_safe_reverse(ibm, itmp,
8181 &tpacpi_all_drivers,
8182 all_drivers) {
8183 ibm_exit(ibm);
8186 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8188 if (tpacpi_inputdev) {
8189 if (tp_features.input_device_registered)
8190 input_unregister_device(tpacpi_inputdev);
8191 else
8192 input_free_device(tpacpi_inputdev);
8195 if (tpacpi_hwmon)
8196 hwmon_device_unregister(tpacpi_hwmon);
8198 if (tp_features.sensors_pdev_attrs_registered)
8199 device_remove_file(&tpacpi_sensors_pdev->dev,
8200 &dev_attr_thinkpad_acpi_pdev_name);
8201 if (tpacpi_sensors_pdev)
8202 platform_device_unregister(tpacpi_sensors_pdev);
8203 if (tpacpi_pdev)
8204 platform_device_unregister(tpacpi_pdev);
8206 if (tp_features.sensors_pdrv_attrs_registered)
8207 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8208 if (tp_features.platform_drv_attrs_registered)
8209 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8211 if (tp_features.sensors_pdrv_registered)
8212 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8214 if (tp_features.platform_drv_registered)
8215 platform_driver_unregister(&tpacpi_pdriver);
8217 if (proc_dir)
8218 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8220 if (tpacpi_wq)
8221 destroy_workqueue(tpacpi_wq);
8223 kfree(thinkpad_id.bios_version_str);
8224 kfree(thinkpad_id.ec_version_str);
8225 kfree(thinkpad_id.model_str);
8229 static int __init thinkpad_acpi_module_init(void)
8231 int ret, i;
8233 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8235 /* Parameter checking */
8236 if (hotkey_report_mode > 2)
8237 return -EINVAL;
8239 /* Driver-level probe */
8241 ret = get_thinkpad_model_data(&thinkpad_id);
8242 if (ret) {
8243 printk(TPACPI_ERR
8244 "unable to get DMI data: %d\n", ret);
8245 thinkpad_acpi_module_exit();
8246 return ret;
8248 ret = probe_for_thinkpad();
8249 if (ret) {
8250 thinkpad_acpi_module_exit();
8251 return ret;
8254 /* Driver initialization */
8256 TPACPI_ACPIHANDLE_INIT(ecrd);
8257 TPACPI_ACPIHANDLE_INIT(ecwr);
8259 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8260 if (!tpacpi_wq) {
8261 thinkpad_acpi_module_exit();
8262 return -ENOMEM;
8265 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8266 if (!proc_dir) {
8267 printk(TPACPI_ERR
8268 "unable to create proc dir " TPACPI_PROC_DIR);
8269 thinkpad_acpi_module_exit();
8270 return -ENODEV;
8272 proc_dir->owner = THIS_MODULE;
8274 ret = platform_driver_register(&tpacpi_pdriver);
8275 if (ret) {
8276 printk(TPACPI_ERR
8277 "unable to register main platform driver\n");
8278 thinkpad_acpi_module_exit();
8279 return ret;
8281 tp_features.platform_drv_registered = 1;
8283 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8284 if (ret) {
8285 printk(TPACPI_ERR
8286 "unable to register hwmon platform driver\n");
8287 thinkpad_acpi_module_exit();
8288 return ret;
8290 tp_features.sensors_pdrv_registered = 1;
8292 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8293 if (!ret) {
8294 tp_features.platform_drv_attrs_registered = 1;
8295 ret = tpacpi_create_driver_attributes(
8296 &tpacpi_hwmon_pdriver.driver);
8298 if (ret) {
8299 printk(TPACPI_ERR
8300 "unable to create sysfs driver attributes\n");
8301 thinkpad_acpi_module_exit();
8302 return ret;
8304 tp_features.sensors_pdrv_attrs_registered = 1;
8307 /* Device initialization */
8308 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8309 NULL, 0);
8310 if (IS_ERR(tpacpi_pdev)) {
8311 ret = PTR_ERR(tpacpi_pdev);
8312 tpacpi_pdev = NULL;
8313 printk(TPACPI_ERR "unable to register platform device\n");
8314 thinkpad_acpi_module_exit();
8315 return ret;
8317 tpacpi_sensors_pdev = platform_device_register_simple(
8318 TPACPI_HWMON_DRVR_NAME,
8319 -1, NULL, 0);
8320 if (IS_ERR(tpacpi_sensors_pdev)) {
8321 ret = PTR_ERR(tpacpi_sensors_pdev);
8322 tpacpi_sensors_pdev = NULL;
8323 printk(TPACPI_ERR
8324 "unable to register hwmon platform device\n");
8325 thinkpad_acpi_module_exit();
8326 return ret;
8328 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8329 &dev_attr_thinkpad_acpi_pdev_name);
8330 if (ret) {
8331 printk(TPACPI_ERR
8332 "unable to create sysfs hwmon device attributes\n");
8333 thinkpad_acpi_module_exit();
8334 return ret;
8336 tp_features.sensors_pdev_attrs_registered = 1;
8337 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8338 if (IS_ERR(tpacpi_hwmon)) {
8339 ret = PTR_ERR(tpacpi_hwmon);
8340 tpacpi_hwmon = NULL;
8341 printk(TPACPI_ERR "unable to register hwmon device\n");
8342 thinkpad_acpi_module_exit();
8343 return ret;
8345 mutex_init(&tpacpi_inputdev_send_mutex);
8346 tpacpi_inputdev = input_allocate_device();
8347 if (!tpacpi_inputdev) {
8348 printk(TPACPI_ERR "unable to allocate input device\n");
8349 thinkpad_acpi_module_exit();
8350 return -ENOMEM;
8351 } else {
8352 /* Prepare input device, but don't register */
8353 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8354 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8355 tpacpi_inputdev->id.bustype = BUS_HOST;
8356 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8357 thinkpad_id.vendor :
8358 PCI_VENDOR_ID_IBM;
8359 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8360 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8361 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8363 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8364 ret = ibm_init(&ibms_init[i]);
8365 if (ret >= 0 && *ibms_init[i].param)
8366 ret = ibms_init[i].data->write(ibms_init[i].param);
8367 if (ret < 0) {
8368 thinkpad_acpi_module_exit();
8369 return ret;
8372 ret = input_register_device(tpacpi_inputdev);
8373 if (ret < 0) {
8374 printk(TPACPI_ERR "unable to register input device\n");
8375 thinkpad_acpi_module_exit();
8376 return ret;
8377 } else {
8378 tp_features.input_device_registered = 1;
8381 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8382 return 0;
8385 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8388 * This will autoload the driver in almost every ThinkPad
8389 * in widespread use.
8391 * Only _VERY_ old models, like the 240, 240x and 570 lack
8392 * the HKEY event interface.
8394 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8397 * DMI matching for module autoloading
8399 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8400 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8402 * Only models listed in thinkwiki will be supported, so add yours
8403 * if it is not there yet.
8405 #define IBM_BIOS_MODULE_ALIAS(__type) \
8406 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8408 /* Ancient thinkpad BIOSes have to be identified by
8409 * BIOS type or model number, and there are far less
8410 * BIOS types than model numbers... */
8411 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8413 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8414 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8415 MODULE_DESCRIPTION(TPACPI_DESC);
8416 MODULE_VERSION(TPACPI_VERSION);
8417 MODULE_LICENSE("GPL");
8419 module_init(thinkpad_acpi_module_init);
8420 module_exit(thinkpad_acpi_module_exit);