backlight/thinkpad-acpi: issue backlight class events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob758dc0b3ed91e65b3cec114b5397bf4c8865e8f0
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020600
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /* HKEY events */
149 enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
182 /* Thermal events */
183 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
189 /* Misc */
190 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
193 /****************************************************************************
194 * Main driver
197 #define TPACPI_NAME "thinkpad"
198 #define TPACPI_DESC "ThinkPad ACPI Extras"
199 #define TPACPI_FILE TPACPI_NAME "_acpi"
200 #define TPACPI_URL "http://ibm-acpi.sf.net/"
201 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
203 #define TPACPI_PROC_DIR "ibm"
204 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
205 #define TPACPI_DRVR_NAME TPACPI_FILE
206 #define TPACPI_DRVR_SHORTNAME "tpacpi"
207 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
209 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
210 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
212 #define TPACPI_MAX_ACPI_ARGS 3
214 /* printk headers */
215 #define TPACPI_LOG TPACPI_FILE ": "
216 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
217 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
218 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
219 #define TPACPI_ERR KERN_ERR TPACPI_LOG
220 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
221 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
222 #define TPACPI_INFO KERN_INFO TPACPI_LOG
223 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
225 /* Debugging printk groups */
226 #define TPACPI_DBG_ALL 0xffff
227 #define TPACPI_DBG_DISCLOSETASK 0x8000
228 #define TPACPI_DBG_INIT 0x0001
229 #define TPACPI_DBG_EXIT 0x0002
230 #define TPACPI_DBG_RFKILL 0x0004
231 #define TPACPI_DBG_HKEY 0x0008
232 #define TPACPI_DBG_FAN 0x0010
233 #define TPACPI_DBG_BRGHT 0x0020
235 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
236 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
237 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
240 /****************************************************************************
241 * Driver-wide structs and misc. variables
244 struct ibm_struct;
246 struct tp_acpi_drv_struct {
247 const struct acpi_device_id *hid;
248 struct acpi_driver *driver;
250 void (*notify) (struct ibm_struct *, u32);
251 acpi_handle *handle;
252 u32 type;
253 struct acpi_device *device;
256 struct ibm_struct {
257 char *name;
259 int (*read) (char *);
260 int (*write) (char *);
261 void (*exit) (void);
262 void (*resume) (void);
263 void (*suspend) (pm_message_t state);
264 void (*shutdown) (void);
266 struct list_head all_drivers;
268 struct tp_acpi_drv_struct *acpi;
270 struct {
271 u8 acpi_driver_registered:1;
272 u8 acpi_notify_installed:1;
273 u8 proc_created:1;
274 u8 init_called:1;
275 u8 experimental:1;
276 } flags;
279 struct ibm_init_struct {
280 char param[32];
282 int (*init) (struct ibm_init_struct *);
283 struct ibm_struct *data;
286 static struct {
287 u32 bluetooth:1;
288 u32 hotkey:1;
289 u32 hotkey_mask:1;
290 u32 hotkey_wlsw:1;
291 u32 hotkey_tablet:1;
292 u32 light:1;
293 u32 light_status:1;
294 u32 bright_16levels:1;
295 u32 bright_acpimode:1;
296 u32 wan:1;
297 u32 uwb:1;
298 u32 fan_ctrl_status_undef:1;
299 u32 second_fan:1;
300 u32 beep_needs_two_args:1;
301 u32 input_device_registered:1;
302 u32 platform_drv_registered:1;
303 u32 platform_drv_attrs_registered:1;
304 u32 sensors_pdrv_registered:1;
305 u32 sensors_pdrv_attrs_registered:1;
306 u32 sensors_pdev_attrs_registered:1;
307 u32 hotkey_poll_active:1;
308 } tp_features;
310 static struct {
311 u16 hotkey_mask_ff:1;
312 } tp_warned;
314 struct thinkpad_id_data {
315 unsigned int vendor; /* ThinkPad vendor:
316 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
318 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
319 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
321 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
322 u16 ec_model;
323 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
324 u16 ec_release;
326 char *model_str; /* ThinkPad T43 */
327 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
329 static struct thinkpad_id_data thinkpad_id;
331 static enum {
332 TPACPI_LIFE_INIT = 0,
333 TPACPI_LIFE_RUNNING,
334 TPACPI_LIFE_EXITING,
335 } tpacpi_lifecycle;
337 static int experimental;
338 static u32 dbg_level;
340 static struct workqueue_struct *tpacpi_wq;
342 enum led_status_t {
343 TPACPI_LED_OFF = 0,
344 TPACPI_LED_ON,
345 TPACPI_LED_BLINK,
348 /* Special LED class that can defer work */
349 struct tpacpi_led_classdev {
350 struct led_classdev led_classdev;
351 struct work_struct work;
352 enum led_status_t new_state;
353 unsigned int led;
356 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
357 static int dbg_wlswemul;
358 static int tpacpi_wlsw_emulstate;
359 static int dbg_bluetoothemul;
360 static int tpacpi_bluetooth_emulstate;
361 static int dbg_wwanemul;
362 static int tpacpi_wwan_emulstate;
363 static int dbg_uwbemul;
364 static int tpacpi_uwb_emulstate;
365 #endif
368 /*************************************************************************
369 * Debugging helpers
372 #define dbg_printk(a_dbg_level, format, arg...) \
373 do { if (dbg_level & (a_dbg_level)) \
374 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
375 } while (0)
377 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
378 #define vdbg_printk dbg_printk
379 static const char *str_supported(int is_supported);
380 #else
381 #define vdbg_printk(a_dbg_level, format, arg...) \
382 do { } while (0)
383 #endif
385 static void tpacpi_log_usertask(const char * const what)
387 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
388 what, task_tgid_vnr(current));
391 #define tpacpi_disclose_usertask(what, format, arg...) \
392 do { \
393 if (unlikely( \
394 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
395 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
396 printk(TPACPI_DEBUG "%s: PID %d: " format, \
397 what, task_tgid_vnr(current), ## arg); \
399 } while (0)
402 * Quirk handling helpers
404 * ThinkPad IDs and versions seen in the field so far
405 * are two-characters from the set [0-9A-Z], i.e. base 36.
407 * We use values well outside that range as specials.
410 #define TPACPI_MATCH_ANY 0xffffU
411 #define TPACPI_MATCH_UNKNOWN 0U
413 /* TPID('1', 'Y') == 0x5931 */
414 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
416 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
417 { .vendor = PCI_VENDOR_ID_IBM, \
418 .bios = TPID(__id1, __id2), \
419 .ec = TPACPI_MATCH_ANY, \
420 .quirks = (__quirk) }
422 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
423 { .vendor = PCI_VENDOR_ID_LENOVO, \
424 .bios = TPID(__id1, __id2), \
425 .ec = TPACPI_MATCH_ANY, \
426 .quirks = (__quirk) }
428 struct tpacpi_quirk {
429 unsigned int vendor;
430 u16 bios;
431 u16 ec;
432 unsigned long quirks;
436 * tpacpi_check_quirks() - search BIOS/EC version on a list
437 * @qlist: array of &struct tpacpi_quirk
438 * @qlist_size: number of elements in @qlist
440 * Iterates over a quirks list until one is found that matches the
441 * ThinkPad's vendor, BIOS and EC model.
443 * Returns 0 if nothing matches, otherwise returns the quirks field of
444 * the matching &struct tpacpi_quirk entry.
446 * The match criteria is: vendor, ec and bios much match.
448 static unsigned long __init tpacpi_check_quirks(
449 const struct tpacpi_quirk *qlist,
450 unsigned int qlist_size)
452 while (qlist_size) {
453 if ((qlist->vendor == thinkpad_id.vendor ||
454 qlist->vendor == TPACPI_MATCH_ANY) &&
455 (qlist->bios == thinkpad_id.bios_model ||
456 qlist->bios == TPACPI_MATCH_ANY) &&
457 (qlist->ec == thinkpad_id.ec_model ||
458 qlist->ec == TPACPI_MATCH_ANY))
459 return qlist->quirks;
461 qlist_size--;
462 qlist++;
464 return 0;
468 /****************************************************************************
469 ****************************************************************************
471 * ACPI Helpers and device model
473 ****************************************************************************
474 ****************************************************************************/
476 /*************************************************************************
477 * ACPI basic handles
480 static acpi_handle root_handle;
482 #define TPACPI_HANDLE(object, parent, paths...) \
483 static acpi_handle object##_handle; \
484 static acpi_handle *object##_parent = &parent##_handle; \
485 static char *object##_path; \
486 static char *object##_paths[] = { paths }
488 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
489 "\\_SB.PCI.ISA.EC", /* 570 */
490 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
491 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
492 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
493 "\\_SB.PCI0.ICH3.EC0", /* R31 */
494 "\\_SB.PCI0.LPC.EC", /* all others */
497 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
498 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
500 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
501 /* T4x, X31, X40 */
502 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
503 "\\CMS", /* R40, R40e */
504 ); /* all others */
506 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
507 "^HKEY", /* R30, R31 */
508 "HKEY", /* all others */
509 ); /* 570 */
511 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
512 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
513 "\\_SB.PCI0.VID0", /* 770e */
514 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
515 "\\_SB.PCI0.AGP.VID", /* all others */
516 ); /* R30, R31 */
519 /*************************************************************************
520 * ACPI helpers
523 static int acpi_evalf(acpi_handle handle,
524 void *res, char *method, char *fmt, ...)
526 char *fmt0 = fmt;
527 struct acpi_object_list params;
528 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
529 struct acpi_buffer result, *resultp;
530 union acpi_object out_obj;
531 acpi_status status;
532 va_list ap;
533 char res_type;
534 int success;
535 int quiet;
537 if (!*fmt) {
538 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
539 return 0;
542 if (*fmt == 'q') {
543 quiet = 1;
544 fmt++;
545 } else
546 quiet = 0;
548 res_type = *(fmt++);
550 params.count = 0;
551 params.pointer = &in_objs[0];
553 va_start(ap, fmt);
554 while (*fmt) {
555 char c = *(fmt++);
556 switch (c) {
557 case 'd': /* int */
558 in_objs[params.count].integer.value = va_arg(ap, int);
559 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
560 break;
561 /* add more types as needed */
562 default:
563 printk(TPACPI_ERR "acpi_evalf() called "
564 "with invalid format character '%c'\n", c);
565 return 0;
568 va_end(ap);
570 if (res_type != 'v') {
571 result.length = sizeof(out_obj);
572 result.pointer = &out_obj;
573 resultp = &result;
574 } else
575 resultp = NULL;
577 status = acpi_evaluate_object(handle, method, &params, resultp);
579 switch (res_type) {
580 case 'd': /* int */
581 if (res)
582 *(int *)res = out_obj.integer.value;
583 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
584 break;
585 case 'v': /* void */
586 success = status == AE_OK;
587 break;
588 /* add more types as needed */
589 default:
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", res_type);
592 return 0;
595 if (!success && !quiet)
596 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
597 method, fmt0, status);
599 return success;
602 static int acpi_ec_read(int i, u8 *p)
604 int v;
606 if (ecrd_handle) {
607 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
608 return 0;
609 *p = v;
610 } else {
611 if (ec_read(i, p) < 0)
612 return 0;
615 return 1;
618 static int acpi_ec_write(int i, u8 v)
620 if (ecwr_handle) {
621 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
622 return 0;
623 } else {
624 if (ec_write(i, v) < 0)
625 return 0;
628 return 1;
631 static int issue_thinkpad_cmos_command(int cmos_cmd)
633 if (!cmos_handle)
634 return -ENXIO;
636 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
637 return -EIO;
639 return 0;
642 /*************************************************************************
643 * ACPI device model
646 #define TPACPI_ACPIHANDLE_INIT(object) \
647 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
648 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
650 static void drv_acpi_handle_init(char *name,
651 acpi_handle *handle, acpi_handle parent,
652 char **paths, int num_paths, char **path)
654 int i;
655 acpi_status status;
657 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
658 name);
660 for (i = 0; i < num_paths; i++) {
661 status = acpi_get_handle(parent, paths[i], handle);
662 if (ACPI_SUCCESS(status)) {
663 *path = paths[i];
664 dbg_printk(TPACPI_DBG_INIT,
665 "Found ACPI handle %s for %s\n",
666 *path, name);
667 return;
671 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
672 name);
673 *handle = NULL;
676 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
678 struct ibm_struct *ibm = data;
680 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
681 return;
683 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
684 return;
686 ibm->acpi->notify(ibm, event);
689 static int __init setup_acpi_notify(struct ibm_struct *ibm)
691 acpi_status status;
692 int rc;
694 BUG_ON(!ibm->acpi);
696 if (!*ibm->acpi->handle)
697 return 0;
699 vdbg_printk(TPACPI_DBG_INIT,
700 "setting up ACPI notify for %s\n", ibm->name);
702 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
703 if (rc < 0) {
704 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
705 ibm->name, rc);
706 return -ENODEV;
709 ibm->acpi->device->driver_data = ibm;
710 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
711 TPACPI_ACPI_EVENT_PREFIX,
712 ibm->name);
714 status = acpi_install_notify_handler(*ibm->acpi->handle,
715 ibm->acpi->type, dispatch_acpi_notify, ibm);
716 if (ACPI_FAILURE(status)) {
717 if (status == AE_ALREADY_EXISTS) {
718 printk(TPACPI_NOTICE
719 "another device driver is already "
720 "handling %s events\n", ibm->name);
721 } else {
722 printk(TPACPI_ERR
723 "acpi_install_notify_handler(%s) failed: %d\n",
724 ibm->name, status);
726 return -ENODEV;
728 ibm->flags.acpi_notify_installed = 1;
729 return 0;
732 static int __init tpacpi_device_add(struct acpi_device *device)
734 return 0;
737 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
739 int rc;
741 dbg_printk(TPACPI_DBG_INIT,
742 "registering %s as an ACPI driver\n", ibm->name);
744 BUG_ON(!ibm->acpi);
746 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
747 if (!ibm->acpi->driver) {
748 printk(TPACPI_ERR
749 "failed to allocate memory for ibm->acpi->driver\n");
750 return -ENOMEM;
753 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
754 ibm->acpi->driver->ids = ibm->acpi->hid;
756 ibm->acpi->driver->ops.add = &tpacpi_device_add;
758 rc = acpi_bus_register_driver(ibm->acpi->driver);
759 if (rc < 0) {
760 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
761 ibm->name, rc);
762 kfree(ibm->acpi->driver);
763 ibm->acpi->driver = NULL;
764 } else if (!rc)
765 ibm->flags.acpi_driver_registered = 1;
767 return rc;
771 /****************************************************************************
772 ****************************************************************************
774 * Procfs Helpers
776 ****************************************************************************
777 ****************************************************************************/
779 static int dispatch_procfs_read(char *page, char **start, off_t off,
780 int count, int *eof, void *data)
782 struct ibm_struct *ibm = data;
783 int len;
785 if (!ibm || !ibm->read)
786 return -EINVAL;
788 len = ibm->read(page);
789 if (len < 0)
790 return len;
792 if (len <= off + count)
793 *eof = 1;
794 *start = page + off;
795 len -= off;
796 if (len > count)
797 len = count;
798 if (len < 0)
799 len = 0;
801 return len;
804 static int dispatch_procfs_write(struct file *file,
805 const char __user *userbuf,
806 unsigned long count, void *data)
808 struct ibm_struct *ibm = data;
809 char *kernbuf;
810 int ret;
812 if (!ibm || !ibm->write)
813 return -EINVAL;
814 if (count > PAGE_SIZE - 2)
815 return -EINVAL;
817 kernbuf = kmalloc(count + 2, GFP_KERNEL);
818 if (!kernbuf)
819 return -ENOMEM;
821 if (copy_from_user(kernbuf, userbuf, count)) {
822 kfree(kernbuf);
823 return -EFAULT;
826 kernbuf[count] = 0;
827 strcat(kernbuf, ",");
828 ret = ibm->write(kernbuf);
829 if (ret == 0)
830 ret = count;
832 kfree(kernbuf);
834 return ret;
837 static char *next_cmd(char **cmds)
839 char *start = *cmds;
840 char *end;
842 while ((end = strchr(start, ',')) && end == start)
843 start = end + 1;
845 if (!end)
846 return NULL;
848 *end = 0;
849 *cmds = end + 1;
850 return start;
854 /****************************************************************************
855 ****************************************************************************
857 * Device model: input, hwmon and platform
859 ****************************************************************************
860 ****************************************************************************/
862 static struct platform_device *tpacpi_pdev;
863 static struct platform_device *tpacpi_sensors_pdev;
864 static struct device *tpacpi_hwmon;
865 static struct input_dev *tpacpi_inputdev;
866 static struct mutex tpacpi_inputdev_send_mutex;
867 static LIST_HEAD(tpacpi_all_drivers);
869 static int tpacpi_suspend_handler(struct platform_device *pdev,
870 pm_message_t state)
872 struct ibm_struct *ibm, *itmp;
874 list_for_each_entry_safe(ibm, itmp,
875 &tpacpi_all_drivers,
876 all_drivers) {
877 if (ibm->suspend)
878 (ibm->suspend)(state);
881 return 0;
884 static int tpacpi_resume_handler(struct platform_device *pdev)
886 struct ibm_struct *ibm, *itmp;
888 list_for_each_entry_safe(ibm, itmp,
889 &tpacpi_all_drivers,
890 all_drivers) {
891 if (ibm->resume)
892 (ibm->resume)();
895 return 0;
898 static void tpacpi_shutdown_handler(struct platform_device *pdev)
900 struct ibm_struct *ibm, *itmp;
902 list_for_each_entry_safe(ibm, itmp,
903 &tpacpi_all_drivers,
904 all_drivers) {
905 if (ibm->shutdown)
906 (ibm->shutdown)();
910 static struct platform_driver tpacpi_pdriver = {
911 .driver = {
912 .name = TPACPI_DRVR_NAME,
913 .owner = THIS_MODULE,
915 .suspend = tpacpi_suspend_handler,
916 .resume = tpacpi_resume_handler,
917 .shutdown = tpacpi_shutdown_handler,
920 static struct platform_driver tpacpi_hwmon_pdriver = {
921 .driver = {
922 .name = TPACPI_HWMON_DRVR_NAME,
923 .owner = THIS_MODULE,
927 /*************************************************************************
928 * sysfs support helpers
931 struct attribute_set {
932 unsigned int members, max_members;
933 struct attribute_group group;
936 struct attribute_set_obj {
937 struct attribute_set s;
938 struct attribute *a;
939 } __attribute__((packed));
941 static struct attribute_set *create_attr_set(unsigned int max_members,
942 const char *name)
944 struct attribute_set_obj *sobj;
946 if (max_members == 0)
947 return NULL;
949 /* Allocates space for implicit NULL at the end too */
950 sobj = kzalloc(sizeof(struct attribute_set_obj) +
951 max_members * sizeof(struct attribute *),
952 GFP_KERNEL);
953 if (!sobj)
954 return NULL;
955 sobj->s.max_members = max_members;
956 sobj->s.group.attrs = &sobj->a;
957 sobj->s.group.name = name;
959 return &sobj->s;
962 #define destroy_attr_set(_set) \
963 kfree(_set);
965 /* not multi-threaded safe, use it in a single thread per set */
966 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
968 if (!s || !attr)
969 return -EINVAL;
971 if (s->members >= s->max_members)
972 return -ENOMEM;
974 s->group.attrs[s->members] = attr;
975 s->members++;
977 return 0;
980 static int add_many_to_attr_set(struct attribute_set *s,
981 struct attribute **attr,
982 unsigned int count)
984 int i, res;
986 for (i = 0; i < count; i++) {
987 res = add_to_attr_set(s, attr[i]);
988 if (res)
989 return res;
992 return 0;
995 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
997 sysfs_remove_group(kobj, &s->group);
998 destroy_attr_set(s);
1001 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1002 sysfs_create_group(_kobj, &_attr_set->group)
1004 static int parse_strtoul(const char *buf,
1005 unsigned long max, unsigned long *value)
1007 char *endp;
1009 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1010 endp = skip_spaces(endp);
1011 if (*endp || *value > max)
1012 return -EINVAL;
1014 return 0;
1017 static void tpacpi_disable_brightness_delay(void)
1019 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1020 printk(TPACPI_NOTICE
1021 "ACPI backlight control delay disabled\n");
1024 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1026 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1027 union acpi_object *obj;
1028 int rc;
1030 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1031 obj = (union acpi_object *)buffer.pointer;
1032 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1033 printk(TPACPI_ERR "Unknown _BCL data, "
1034 "please report this to %s\n", TPACPI_MAIL);
1035 rc = 0;
1036 } else {
1037 rc = obj->package.count;
1039 } else {
1040 return 0;
1043 kfree(buffer.pointer);
1044 return rc;
1047 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1048 u32 lvl, void *context, void **rv)
1050 char name[ACPI_PATH_SEGMENT_LENGTH];
1051 struct acpi_buffer buffer = { sizeof(name), &name };
1053 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1054 !strncmp("_BCL", name, sizeof(name) - 1)) {
1055 BUG_ON(!rv || !*rv);
1056 **(int **)rv = tpacpi_query_bcl_levels(handle);
1057 return AE_CTRL_TERMINATE;
1058 } else {
1059 return AE_OK;
1064 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1066 static int __init tpacpi_check_std_acpi_brightness_support(void)
1068 int status;
1069 int bcl_levels = 0;
1070 void *bcl_ptr = &bcl_levels;
1072 if (!vid_handle) {
1073 TPACPI_ACPIHANDLE_INIT(vid);
1075 if (!vid_handle)
1076 return 0;
1079 * Search for a _BCL method, and execute it. This is safe on all
1080 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1081 * BIOS in ACPI backlight control mode. We do NOT have to care
1082 * about calling the _BCL method in an enabled video device, any
1083 * will do for our purposes.
1086 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1087 tpacpi_acpi_walk_find_bcl, NULL, NULL,
1088 &bcl_ptr);
1090 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1091 tp_features.bright_acpimode = 1;
1092 return (bcl_levels - 2);
1095 return 0;
1098 static void printk_deprecated_attribute(const char * const what,
1099 const char * const details)
1101 tpacpi_log_usertask("deprecated sysfs attribute");
1102 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1103 "will be removed. %s\n",
1104 what, details);
1107 /*************************************************************************
1108 * rfkill and radio control support helpers
1112 * ThinkPad-ACPI firmware handling model:
1114 * WLSW (master wireless switch) is event-driven, and is common to all
1115 * firmware-controlled radios. It cannot be controlled, just monitored,
1116 * as expected. It overrides all radio state in firmware
1118 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1119 * (TODO: verify how WLSW interacts with the returned radio state).
1121 * The only time there are shadow radio state changes, is when
1122 * masked-off hotkeys are used.
1126 * Internal driver API for radio state:
1128 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1129 * bool: true means radio blocked (off)
1131 enum tpacpi_rfkill_state {
1132 TPACPI_RFK_RADIO_OFF = 0,
1133 TPACPI_RFK_RADIO_ON
1136 /* rfkill switches */
1137 enum tpacpi_rfk_id {
1138 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1139 TPACPI_RFK_WWAN_SW_ID,
1140 TPACPI_RFK_UWB_SW_ID,
1141 TPACPI_RFK_SW_MAX
1144 static const char *tpacpi_rfkill_names[] = {
1145 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1146 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1147 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1148 [TPACPI_RFK_SW_MAX] = NULL
1151 /* ThinkPad-ACPI rfkill subdriver */
1152 struct tpacpi_rfk {
1153 struct rfkill *rfkill;
1154 enum tpacpi_rfk_id id;
1155 const struct tpacpi_rfk_ops *ops;
1158 struct tpacpi_rfk_ops {
1159 /* firmware interface */
1160 int (*get_status)(void);
1161 int (*set_status)(const enum tpacpi_rfkill_state);
1164 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1166 /* Query FW and update rfkill sw state for a given rfkill switch */
1167 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1169 int status;
1171 if (!tp_rfk)
1172 return -ENODEV;
1174 status = (tp_rfk->ops->get_status)();
1175 if (status < 0)
1176 return status;
1178 rfkill_set_sw_state(tp_rfk->rfkill,
1179 (status == TPACPI_RFK_RADIO_OFF));
1181 return status;
1184 /* Query FW and update rfkill sw state for all rfkill switches */
1185 static void tpacpi_rfk_update_swstate_all(void)
1187 unsigned int i;
1189 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1190 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1194 * Sync the HW-blocking state of all rfkill switches,
1195 * do notice it causes the rfkill core to schedule uevents
1197 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1199 unsigned int i;
1200 struct tpacpi_rfk *tp_rfk;
1202 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1203 tp_rfk = tpacpi_rfkill_switches[i];
1204 if (tp_rfk) {
1205 if (rfkill_set_hw_state(tp_rfk->rfkill,
1206 blocked)) {
1207 /* ignore -- we track sw block */
1213 /* Call to get the WLSW state from the firmware */
1214 static int hotkey_get_wlsw(void);
1216 /* Call to query WLSW state and update all rfkill switches */
1217 static bool tpacpi_rfk_check_hwblock_state(void)
1219 int res = hotkey_get_wlsw();
1220 int hw_blocked;
1222 /* When unknown or unsupported, we have to assume it is unblocked */
1223 if (res < 0)
1224 return false;
1226 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1227 tpacpi_rfk_update_hwblock_state(hw_blocked);
1229 return hw_blocked;
1232 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1234 struct tpacpi_rfk *tp_rfk = data;
1235 int res;
1237 dbg_printk(TPACPI_DBG_RFKILL,
1238 "request to change radio state to %s\n",
1239 blocked ? "blocked" : "unblocked");
1241 /* try to set radio state */
1242 res = (tp_rfk->ops->set_status)(blocked ?
1243 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1245 /* and update the rfkill core with whatever the FW really did */
1246 tpacpi_rfk_update_swstate(tp_rfk);
1248 return (res < 0) ? res : 0;
1251 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1252 .set_block = tpacpi_rfk_hook_set_block,
1255 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1256 const struct tpacpi_rfk_ops *tp_rfkops,
1257 const enum rfkill_type rfktype,
1258 const char *name,
1259 const bool set_default)
1261 struct tpacpi_rfk *atp_rfk;
1262 int res;
1263 bool sw_state = false;
1264 int sw_status;
1266 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1268 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1269 if (atp_rfk)
1270 atp_rfk->rfkill = rfkill_alloc(name,
1271 &tpacpi_pdev->dev,
1272 rfktype,
1273 &tpacpi_rfk_rfkill_ops,
1274 atp_rfk);
1275 if (!atp_rfk || !atp_rfk->rfkill) {
1276 printk(TPACPI_ERR
1277 "failed to allocate memory for rfkill class\n");
1278 kfree(atp_rfk);
1279 return -ENOMEM;
1282 atp_rfk->id = id;
1283 atp_rfk->ops = tp_rfkops;
1285 sw_status = (tp_rfkops->get_status)();
1286 if (sw_status < 0) {
1287 printk(TPACPI_ERR
1288 "failed to read initial state for %s, error %d\n",
1289 name, sw_status);
1290 } else {
1291 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1292 if (set_default) {
1293 /* try to keep the initial state, since we ask the
1294 * firmware to preserve it across S5 in NVRAM */
1295 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1298 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
1300 res = rfkill_register(atp_rfk->rfkill);
1301 if (res < 0) {
1302 printk(TPACPI_ERR
1303 "failed to register %s rfkill switch: %d\n",
1304 name, res);
1305 rfkill_destroy(atp_rfk->rfkill);
1306 kfree(atp_rfk);
1307 return res;
1310 tpacpi_rfkill_switches[id] = atp_rfk;
1311 return 0;
1314 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1316 struct tpacpi_rfk *tp_rfk;
1318 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1320 tp_rfk = tpacpi_rfkill_switches[id];
1321 if (tp_rfk) {
1322 rfkill_unregister(tp_rfk->rfkill);
1323 rfkill_destroy(tp_rfk->rfkill);
1324 tpacpi_rfkill_switches[id] = NULL;
1325 kfree(tp_rfk);
1329 static void printk_deprecated_rfkill_attribute(const char * const what)
1331 printk_deprecated_attribute(what,
1332 "Please switch to generic rfkill before year 2010");
1335 /* sysfs <radio> enable ------------------------------------------------ */
1336 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1337 struct device_attribute *attr,
1338 char *buf)
1340 int status;
1342 printk_deprecated_rfkill_attribute(attr->attr.name);
1344 /* This is in the ABI... */
1345 if (tpacpi_rfk_check_hwblock_state()) {
1346 status = TPACPI_RFK_RADIO_OFF;
1347 } else {
1348 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1349 if (status < 0)
1350 return status;
1353 return snprintf(buf, PAGE_SIZE, "%d\n",
1354 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1357 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1358 struct device_attribute *attr,
1359 const char *buf, size_t count)
1361 unsigned long t;
1362 int res;
1364 printk_deprecated_rfkill_attribute(attr->attr.name);
1366 if (parse_strtoul(buf, 1, &t))
1367 return -EINVAL;
1369 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1371 /* This is in the ABI... */
1372 if (tpacpi_rfk_check_hwblock_state() && !!t)
1373 return -EPERM;
1375 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1376 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1377 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1379 return (res < 0) ? res : count;
1382 /* procfs -------------------------------------------------------------- */
1383 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1385 int len = 0;
1387 if (id >= TPACPI_RFK_SW_MAX)
1388 len += sprintf(p + len, "status:\t\tnot supported\n");
1389 else {
1390 int status;
1392 /* This is in the ABI... */
1393 if (tpacpi_rfk_check_hwblock_state()) {
1394 status = TPACPI_RFK_RADIO_OFF;
1395 } else {
1396 status = tpacpi_rfk_update_swstate(
1397 tpacpi_rfkill_switches[id]);
1398 if (status < 0)
1399 return status;
1402 len += sprintf(p + len, "status:\t\t%s\n",
1403 (status == TPACPI_RFK_RADIO_ON) ?
1404 "enabled" : "disabled");
1405 len += sprintf(p + len, "commands:\tenable, disable\n");
1408 return len;
1411 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1413 char *cmd;
1414 int status = -1;
1415 int res = 0;
1417 if (id >= TPACPI_RFK_SW_MAX)
1418 return -ENODEV;
1420 while ((cmd = next_cmd(&buf))) {
1421 if (strlencmp(cmd, "enable") == 0)
1422 status = TPACPI_RFK_RADIO_ON;
1423 else if (strlencmp(cmd, "disable") == 0)
1424 status = TPACPI_RFK_RADIO_OFF;
1425 else
1426 return -EINVAL;
1429 if (status != -1) {
1430 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1431 (status == TPACPI_RFK_RADIO_ON) ?
1432 "enable" : "disable",
1433 tpacpi_rfkill_names[id]);
1434 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1435 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1438 return res;
1441 /*************************************************************************
1442 * thinkpad-acpi driver attributes
1445 /* interface_version --------------------------------------------------- */
1446 static ssize_t tpacpi_driver_interface_version_show(
1447 struct device_driver *drv,
1448 char *buf)
1450 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1453 static DRIVER_ATTR(interface_version, S_IRUGO,
1454 tpacpi_driver_interface_version_show, NULL);
1456 /* debug_level --------------------------------------------------------- */
1457 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1458 char *buf)
1460 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1463 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1464 const char *buf, size_t count)
1466 unsigned long t;
1468 if (parse_strtoul(buf, 0xffff, &t))
1469 return -EINVAL;
1471 dbg_level = t;
1473 return count;
1476 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1477 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1479 /* version ------------------------------------------------------------- */
1480 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1481 char *buf)
1483 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1484 TPACPI_DESC, TPACPI_VERSION);
1487 static DRIVER_ATTR(version, S_IRUGO,
1488 tpacpi_driver_version_show, NULL);
1490 /* --------------------------------------------------------------------- */
1492 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1494 /* wlsw_emulstate ------------------------------------------------------ */
1495 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1496 char *buf)
1498 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1501 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1502 const char *buf, size_t count)
1504 unsigned long t;
1506 if (parse_strtoul(buf, 1, &t))
1507 return -EINVAL;
1509 if (tpacpi_wlsw_emulstate != !!t) {
1510 tpacpi_wlsw_emulstate = !!t;
1511 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1514 return count;
1517 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1518 tpacpi_driver_wlsw_emulstate_show,
1519 tpacpi_driver_wlsw_emulstate_store);
1521 /* bluetooth_emulstate ------------------------------------------------- */
1522 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1523 struct device_driver *drv,
1524 char *buf)
1526 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1529 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1530 struct device_driver *drv,
1531 const char *buf, size_t count)
1533 unsigned long t;
1535 if (parse_strtoul(buf, 1, &t))
1536 return -EINVAL;
1538 tpacpi_bluetooth_emulstate = !!t;
1540 return count;
1543 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1544 tpacpi_driver_bluetooth_emulstate_show,
1545 tpacpi_driver_bluetooth_emulstate_store);
1547 /* wwan_emulstate ------------------------------------------------- */
1548 static ssize_t tpacpi_driver_wwan_emulstate_show(
1549 struct device_driver *drv,
1550 char *buf)
1552 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1555 static ssize_t tpacpi_driver_wwan_emulstate_store(
1556 struct device_driver *drv,
1557 const char *buf, size_t count)
1559 unsigned long t;
1561 if (parse_strtoul(buf, 1, &t))
1562 return -EINVAL;
1564 tpacpi_wwan_emulstate = !!t;
1566 return count;
1569 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1570 tpacpi_driver_wwan_emulstate_show,
1571 tpacpi_driver_wwan_emulstate_store);
1573 /* uwb_emulstate ------------------------------------------------- */
1574 static ssize_t tpacpi_driver_uwb_emulstate_show(
1575 struct device_driver *drv,
1576 char *buf)
1578 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1581 static ssize_t tpacpi_driver_uwb_emulstate_store(
1582 struct device_driver *drv,
1583 const char *buf, size_t count)
1585 unsigned long t;
1587 if (parse_strtoul(buf, 1, &t))
1588 return -EINVAL;
1590 tpacpi_uwb_emulstate = !!t;
1592 return count;
1595 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1596 tpacpi_driver_uwb_emulstate_show,
1597 tpacpi_driver_uwb_emulstate_store);
1598 #endif
1600 /* --------------------------------------------------------------------- */
1602 static struct driver_attribute *tpacpi_driver_attributes[] = {
1603 &driver_attr_debug_level, &driver_attr_version,
1604 &driver_attr_interface_version,
1607 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1609 int i, res;
1611 i = 0;
1612 res = 0;
1613 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1614 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1615 i++;
1618 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1619 if (!res && dbg_wlswemul)
1620 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1621 if (!res && dbg_bluetoothemul)
1622 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1623 if (!res && dbg_wwanemul)
1624 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1625 if (!res && dbg_uwbemul)
1626 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1627 #endif
1629 return res;
1632 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1634 int i;
1636 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1637 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1639 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1640 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1641 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1642 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1643 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1644 #endif
1647 /*************************************************************************
1648 * Firmware Data
1652 * Table of recommended minimum BIOS versions
1654 * Reasons for listing:
1655 * 1. Stable BIOS, listed because the unknown ammount of
1656 * bugs and bad ACPI behaviour on older versions
1658 * 2. BIOS or EC fw with known bugs that trigger on Linux
1660 * 3. BIOS with known reduced functionality in older versions
1662 * We recommend the latest BIOS and EC version.
1663 * We only support the latest BIOS and EC fw version as a rule.
1665 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1666 * Information from users in ThinkWiki
1668 * WARNING: we use this table also to detect that the machine is
1669 * a ThinkPad in some cases, so don't remove entries lightly.
1672 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1673 { .vendor = (__v), \
1674 .bios = TPID(__id1, __id2), \
1675 .ec = TPACPI_MATCH_ANY, \
1676 .quirks = TPACPI_MATCH_ANY << 16 \
1677 | (__bv1) << 8 | (__bv2) }
1679 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1680 __eid, __ev1, __ev2) \
1681 { .vendor = (__v), \
1682 .bios = TPID(__bid1, __bid2), \
1683 .ec = __eid, \
1684 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1685 | (__bv1) << 8 | (__bv2) }
1687 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1688 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1690 /* Outdated IBM BIOSes often lack the EC id string */
1691 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1692 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1693 __bv1, __bv2, TPID(__id1, __id2), \
1694 __ev1, __ev2), \
1695 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1696 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1697 __ev1, __ev2)
1699 /* Outdated IBM BIOSes often lack the EC id string */
1700 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1701 __eid1, __eid2, __ev1, __ev2) \
1702 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1703 __bv1, __bv2, TPID(__eid1, __eid2), \
1704 __ev1, __ev2), \
1705 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1706 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1707 __ev1, __ev2)
1709 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1710 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1712 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1713 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1714 __bv1, __bv2, TPID(__id1, __id2), \
1715 __ev1, __ev2)
1717 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1718 __eid1, __eid2, __ev1, __ev2) \
1719 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1720 __bv1, __bv2, TPID(__eid1, __eid2), \
1721 __ev1, __ev2)
1723 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1724 /* Numeric models ------------------ */
1725 /* FW MODEL BIOS VERS */
1726 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1727 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1728 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1729 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1730 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1731 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1732 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1733 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1734 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1736 /* A-series ------------------------- */
1737 /* FW MODEL BIOS VERS EC VERS */
1738 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1739 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1740 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1741 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1742 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1743 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1744 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1745 TPV_QI0('1', '3', '2', '0'), /* A22m */
1746 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1747 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1748 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1750 /* G-series ------------------------- */
1751 /* FW MODEL BIOS VERS */
1752 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1753 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1755 /* R-series, T-series --------------- */
1756 /* FW MODEL BIOS VERS EC VERS */
1757 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1758 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1759 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1760 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1761 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1762 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1763 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1764 T40/p, T41/p, T42/p (1) */
1765 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1766 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1767 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1768 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1770 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1771 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1772 TPV_QI0('1', '6', '3', '2'), /* T22 */
1773 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1774 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1775 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1777 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1778 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1779 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1781 /* BIOS FW BIOS VERS EC FW EC VERS */
1782 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1783 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1785 /* X-series ------------------------- */
1786 /* FW MODEL BIOS VERS EC VERS */
1787 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1788 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1789 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1790 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1791 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1792 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1793 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1795 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1796 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1798 /* (0) - older versions lack DMI EC fw string and functionality */
1799 /* (1) - older versions known to lack functionality */
1802 #undef TPV_QL1
1803 #undef TPV_QL0
1804 #undef TPV_QI2
1805 #undef TPV_QI1
1806 #undef TPV_QI0
1807 #undef TPV_Q_X
1808 #undef TPV_Q
1810 static void __init tpacpi_check_outdated_fw(void)
1812 unsigned long fwvers;
1813 u16 ec_version, bios_version;
1815 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1816 ARRAY_SIZE(tpacpi_bios_version_qtable));
1818 if (!fwvers)
1819 return;
1821 bios_version = fwvers & 0xffffU;
1822 ec_version = (fwvers >> 16) & 0xffffU;
1824 /* note that unknown versions are set to 0x0000 and we use that */
1825 if ((bios_version > thinkpad_id.bios_release) ||
1826 (ec_version > thinkpad_id.ec_release &&
1827 ec_version != TPACPI_MATCH_ANY)) {
1829 * The changelogs would let us track down the exact
1830 * reason, but it is just too much of a pain to track
1831 * it. We only list BIOSes that are either really
1832 * broken, or really stable to begin with, so it is
1833 * best if the user upgrades the firmware anyway.
1835 printk(TPACPI_WARN
1836 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1837 printk(TPACPI_WARN
1838 "WARNING: This firmware may be missing critical bug "
1839 "fixes and/or important features\n");
1843 static bool __init tpacpi_is_fw_known(void)
1845 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1846 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1849 /****************************************************************************
1850 ****************************************************************************
1852 * Subdrivers
1854 ****************************************************************************
1855 ****************************************************************************/
1857 /*************************************************************************
1858 * thinkpad-acpi init subdriver
1861 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1863 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1864 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1866 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1867 (thinkpad_id.bios_version_str) ?
1868 thinkpad_id.bios_version_str : "unknown",
1869 (thinkpad_id.ec_version_str) ?
1870 thinkpad_id.ec_version_str : "unknown");
1872 if (thinkpad_id.vendor && thinkpad_id.model_str)
1873 printk(TPACPI_INFO "%s %s, model %s\n",
1874 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1875 "IBM" : ((thinkpad_id.vendor ==
1876 PCI_VENDOR_ID_LENOVO) ?
1877 "Lenovo" : "Unknown vendor"),
1878 thinkpad_id.model_str,
1879 (thinkpad_id.nummodel_str) ?
1880 thinkpad_id.nummodel_str : "unknown");
1882 tpacpi_check_outdated_fw();
1883 return 0;
1886 static int thinkpad_acpi_driver_read(char *p)
1888 int len = 0;
1890 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1891 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1893 return len;
1896 static struct ibm_struct thinkpad_acpi_driver_data = {
1897 .name = "driver",
1898 .read = thinkpad_acpi_driver_read,
1901 /*************************************************************************
1902 * Hotkey subdriver
1906 * ThinkPad firmware event model
1908 * The ThinkPad firmware has two main event interfaces: normal ACPI
1909 * notifications (which follow the ACPI standard), and a private event
1910 * interface.
1912 * The private event interface also issues events for the hotkeys. As
1913 * the driver gained features, the event handling code ended up being
1914 * built around the hotkey subdriver. This will need to be refactored
1915 * to a more formal event API eventually.
1917 * Some "hotkeys" are actually supposed to be used as event reports,
1918 * such as "brightness has changed", "volume has changed", depending on
1919 * the ThinkPad model and how the firmware is operating.
1921 * Unlike other classes, hotkey-class events have mask/unmask control on
1922 * non-ancient firmware. However, how it behaves changes a lot with the
1923 * firmware model and version.
1926 enum { /* hot key scan codes (derived from ACPI DSDT) */
1927 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1928 TP_ACPI_HOTKEYSCAN_FNF2,
1929 TP_ACPI_HOTKEYSCAN_FNF3,
1930 TP_ACPI_HOTKEYSCAN_FNF4,
1931 TP_ACPI_HOTKEYSCAN_FNF5,
1932 TP_ACPI_HOTKEYSCAN_FNF6,
1933 TP_ACPI_HOTKEYSCAN_FNF7,
1934 TP_ACPI_HOTKEYSCAN_FNF8,
1935 TP_ACPI_HOTKEYSCAN_FNF9,
1936 TP_ACPI_HOTKEYSCAN_FNF10,
1937 TP_ACPI_HOTKEYSCAN_FNF11,
1938 TP_ACPI_HOTKEYSCAN_FNF12,
1939 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1940 TP_ACPI_HOTKEYSCAN_FNINSERT,
1941 TP_ACPI_HOTKEYSCAN_FNDELETE,
1942 TP_ACPI_HOTKEYSCAN_FNHOME,
1943 TP_ACPI_HOTKEYSCAN_FNEND,
1944 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1945 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1946 TP_ACPI_HOTKEYSCAN_FNSPACE,
1947 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1948 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1949 TP_ACPI_HOTKEYSCAN_MUTE,
1950 TP_ACPI_HOTKEYSCAN_THINKPAD,
1953 enum { /* Keys/events available through NVRAM polling */
1954 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1955 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1958 enum { /* Positions of some of the keys in hotkey masks */
1959 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1960 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1961 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1962 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1963 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1964 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1965 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1966 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1967 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1968 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1969 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1972 enum { /* NVRAM to ACPI HKEY group map */
1973 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1974 TP_ACPI_HKEY_ZOOM_MASK |
1975 TP_ACPI_HKEY_DISPSWTCH_MASK |
1976 TP_ACPI_HKEY_HIBERNATE_MASK,
1977 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1978 TP_ACPI_HKEY_BRGHTDWN_MASK,
1979 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1980 TP_ACPI_HKEY_VOLDWN_MASK |
1981 TP_ACPI_HKEY_MUTE_MASK,
1984 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1985 struct tp_nvram_state {
1986 u16 thinkpad_toggle:1;
1987 u16 zoom_toggle:1;
1988 u16 display_toggle:1;
1989 u16 thinklight_toggle:1;
1990 u16 hibernate_toggle:1;
1991 u16 displayexp_toggle:1;
1992 u16 display_state:1;
1993 u16 brightness_toggle:1;
1994 u16 volume_toggle:1;
1995 u16 mute:1;
1997 u8 brightness_level;
1998 u8 volume_level;
2001 /* kthread for the hotkey poller */
2002 static struct task_struct *tpacpi_hotkey_task;
2004 /* Acquired while the poller kthread is running, use to sync start/stop */
2005 static struct mutex hotkey_thread_mutex;
2008 * Acquire mutex to write poller control variables as an
2009 * atomic block.
2011 * Increment hotkey_config_change when changing them if you
2012 * want the kthread to forget old state.
2014 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2016 static struct mutex hotkey_thread_data_mutex;
2017 static unsigned int hotkey_config_change;
2020 * hotkey poller control variables
2022 * Must be atomic or readers will also need to acquire mutex
2024 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2025 * should be used only when the changes need to be taken as
2026 * a block, OR when one needs to force the kthread to forget
2027 * old state.
2029 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2030 static unsigned int hotkey_poll_freq = 10; /* Hz */
2032 #define HOTKEY_CONFIG_CRITICAL_START \
2033 do { \
2034 mutex_lock(&hotkey_thread_data_mutex); \
2035 hotkey_config_change++; \
2036 } while (0);
2037 #define HOTKEY_CONFIG_CRITICAL_END \
2038 mutex_unlock(&hotkey_thread_data_mutex);
2040 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2042 #define hotkey_source_mask 0U
2043 #define HOTKEY_CONFIG_CRITICAL_START
2044 #define HOTKEY_CONFIG_CRITICAL_END
2046 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2048 static struct mutex hotkey_mutex;
2050 static enum { /* Reasons for waking up */
2051 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2052 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2053 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2054 } hotkey_wakeup_reason;
2056 static int hotkey_autosleep_ack;
2058 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2059 static u32 hotkey_all_mask; /* all events supported in fw */
2060 static u32 hotkey_reserved_mask; /* events better left disabled */
2061 static u32 hotkey_driver_mask; /* events needed by the driver */
2062 static u32 hotkey_user_mask; /* events visible to userspace */
2063 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2065 static unsigned int hotkey_report_mode;
2067 static u16 *hotkey_keycode_map;
2069 static struct attribute_set *hotkey_dev_attributes;
2071 static void tpacpi_driver_event(const unsigned int hkey_event);
2072 static void hotkey_driver_event(const unsigned int scancode);
2074 /* HKEY.MHKG() return bits */
2075 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2077 static int hotkey_get_wlsw(void)
2079 int status;
2081 if (!tp_features.hotkey_wlsw)
2082 return -ENODEV;
2084 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2085 if (dbg_wlswemul)
2086 return (tpacpi_wlsw_emulstate) ?
2087 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2088 #endif
2090 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2091 return -EIO;
2093 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2096 static int hotkey_get_tablet_mode(int *status)
2098 int s;
2100 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2101 return -EIO;
2103 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2104 return 0;
2108 * Reads current event mask from firmware, and updates
2109 * hotkey_acpi_mask accordingly. Also resets any bits
2110 * from hotkey_user_mask that are unavailable to be
2111 * delivered (shadow requirement of the userspace ABI).
2113 * Call with hotkey_mutex held
2115 static int hotkey_mask_get(void)
2117 if (tp_features.hotkey_mask) {
2118 u32 m = 0;
2120 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2121 return -EIO;
2123 hotkey_acpi_mask = m;
2124 } else {
2125 /* no mask support doesn't mean no event support... */
2126 hotkey_acpi_mask = hotkey_all_mask;
2129 /* sync userspace-visible mask */
2130 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2132 return 0;
2135 void static hotkey_mask_warn_incomplete_mask(void)
2137 /* log only what the user can fix... */
2138 const u32 wantedmask = hotkey_driver_mask &
2139 ~(hotkey_acpi_mask | hotkey_source_mask) &
2140 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2142 if (wantedmask)
2143 printk(TPACPI_NOTICE
2144 "required events 0x%08x not enabled!\n",
2145 wantedmask);
2149 * Set the firmware mask when supported
2151 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2153 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2155 * Call with hotkey_mutex held
2157 static int hotkey_mask_set(u32 mask)
2159 int i;
2160 int rc = 0;
2162 const u32 fwmask = mask & ~hotkey_source_mask;
2164 if (tp_features.hotkey_mask) {
2165 for (i = 0; i < 32; i++) {
2166 if (!acpi_evalf(hkey_handle,
2167 NULL, "MHKM", "vdd", i + 1,
2168 !!(mask & (1 << i)))) {
2169 rc = -EIO;
2170 break;
2176 * We *must* make an inconditional call to hotkey_mask_get to
2177 * refresh hotkey_acpi_mask and update hotkey_user_mask
2179 * Take the opportunity to also log when we cannot _enable_
2180 * a given event.
2182 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2183 printk(TPACPI_NOTICE
2184 "asked for hotkey mask 0x%08x, but "
2185 "firmware forced it to 0x%08x\n",
2186 fwmask, hotkey_acpi_mask);
2189 hotkey_mask_warn_incomplete_mask();
2191 return rc;
2195 * Sets hotkey_user_mask and tries to set the firmware mask
2197 * Call with hotkey_mutex held
2199 static int hotkey_user_mask_set(const u32 mask)
2201 int rc;
2203 /* Give people a chance to notice they are doing something that
2204 * is bound to go boom on their users sooner or later */
2205 if (!tp_warned.hotkey_mask_ff &&
2206 (mask == 0xffff || mask == 0xffffff ||
2207 mask == 0xffffffff)) {
2208 tp_warned.hotkey_mask_ff = 1;
2209 printk(TPACPI_NOTICE
2210 "setting the hotkey mask to 0x%08x is likely "
2211 "not the best way to go about it\n", mask);
2212 printk(TPACPI_NOTICE
2213 "please consider using the driver defaults, "
2214 "and refer to up-to-date thinkpad-acpi "
2215 "documentation\n");
2218 /* Try to enable what the user asked for, plus whatever we need.
2219 * this syncs everything but won't enable bits in hotkey_user_mask */
2220 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2222 /* Enable the available bits in hotkey_user_mask */
2223 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2225 return rc;
2229 * Sets the driver hotkey mask.
2231 * Can be called even if the hotkey subdriver is inactive
2233 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2235 int rc;
2237 /* Do the right thing if hotkey_init has not been called yet */
2238 if (!tp_features.hotkey) {
2239 hotkey_driver_mask = mask;
2240 return 0;
2243 mutex_lock(&hotkey_mutex);
2245 HOTKEY_CONFIG_CRITICAL_START
2246 hotkey_driver_mask = mask;
2247 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2248 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2249 #endif
2250 HOTKEY_CONFIG_CRITICAL_END
2252 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2253 ~hotkey_source_mask);
2254 mutex_unlock(&hotkey_mutex);
2256 return rc;
2259 static int hotkey_status_get(int *status)
2261 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2262 return -EIO;
2264 return 0;
2267 static int hotkey_status_set(bool enable)
2269 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2270 return -EIO;
2272 return 0;
2275 static void tpacpi_input_send_tabletsw(void)
2277 int state;
2279 if (tp_features.hotkey_tablet &&
2280 !hotkey_get_tablet_mode(&state)) {
2281 mutex_lock(&tpacpi_inputdev_send_mutex);
2283 input_report_switch(tpacpi_inputdev,
2284 SW_TABLET_MODE, !!state);
2285 input_sync(tpacpi_inputdev);
2287 mutex_unlock(&tpacpi_inputdev_send_mutex);
2291 /* Do NOT call without validating scancode first */
2292 static void tpacpi_input_send_key(const unsigned int scancode)
2294 const unsigned int keycode = hotkey_keycode_map[scancode];
2296 if (keycode != KEY_RESERVED) {
2297 mutex_lock(&tpacpi_inputdev_send_mutex);
2299 input_report_key(tpacpi_inputdev, keycode, 1);
2300 if (keycode == KEY_UNKNOWN)
2301 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2302 scancode);
2303 input_sync(tpacpi_inputdev);
2305 input_report_key(tpacpi_inputdev, keycode, 0);
2306 if (keycode == KEY_UNKNOWN)
2307 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2308 scancode);
2309 input_sync(tpacpi_inputdev);
2311 mutex_unlock(&tpacpi_inputdev_send_mutex);
2315 /* Do NOT call without validating scancode first */
2316 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2318 hotkey_driver_event(scancode);
2319 if (hotkey_user_mask & (1 << scancode))
2320 tpacpi_input_send_key(scancode);
2323 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2324 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2326 /* Do NOT call without validating scancode first */
2327 static void tpacpi_hotkey_send_key(unsigned int scancode)
2329 tpacpi_input_send_key_masked(scancode);
2330 if (hotkey_report_mode < 2) {
2331 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2332 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2336 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2338 u8 d;
2340 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2341 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2342 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2343 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2344 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2345 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2347 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2348 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2349 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2351 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2352 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2353 n->displayexp_toggle =
2354 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2356 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2358 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2359 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2360 n->brightness_toggle =
2361 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2363 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2365 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2366 >> TP_NVRAM_POS_LEVEL_VOLUME;
2367 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2368 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2372 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2373 struct tp_nvram_state *newn,
2374 const u32 event_mask)
2377 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2378 do { \
2379 if ((event_mask & (1 << __scancode)) && \
2380 oldn->__member != newn->__member) \
2381 tpacpi_hotkey_send_key(__scancode); \
2382 } while (0)
2384 #define TPACPI_MAY_SEND_KEY(__scancode) \
2385 do { \
2386 if (event_mask & (1 << __scancode)) \
2387 tpacpi_hotkey_send_key(__scancode); \
2388 } while (0)
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2399 /* handle volume */
2400 if (oldn->volume_toggle != newn->volume_toggle) {
2401 if (oldn->mute != newn->mute) {
2402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2404 if (oldn->volume_level > newn->volume_level) {
2405 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2406 } else if (oldn->volume_level < newn->volume_level) {
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2408 } else if (oldn->mute == newn->mute) {
2409 /* repeated key presses that didn't change state */
2410 if (newn->mute) {
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2412 } else if (newn->volume_level != 0) {
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2414 } else {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2420 /* handle brightness */
2421 if (oldn->brightness_toggle != newn->brightness_toggle) {
2422 if (oldn->brightness_level < newn->brightness_level) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2424 } else if (oldn->brightness_level > newn->brightness_level) {
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2426 } else {
2427 /* repeated key presses that didn't change state */
2428 if (newn->brightness_level != 0) {
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2430 } else {
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2436 #undef TPACPI_COMPARE_KEY
2437 #undef TPACPI_MAY_SEND_KEY
2441 * Polling driver
2443 * We track all events in hotkey_source_mask all the time, since
2444 * most of them are edge-based. We only issue those requested by
2445 * hotkey_user_mask or hotkey_driver_mask, though.
2447 static int hotkey_kthread(void *data)
2449 struct tp_nvram_state s[2];
2450 u32 poll_mask, event_mask;
2451 unsigned int si, so;
2452 unsigned long t;
2453 unsigned int change_detector, must_reset;
2454 unsigned int poll_freq;
2456 mutex_lock(&hotkey_thread_mutex);
2458 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2459 goto exit;
2461 set_freezable();
2463 so = 0;
2464 si = 1;
2465 t = 0;
2467 /* Initial state for compares */
2468 mutex_lock(&hotkey_thread_data_mutex);
2469 change_detector = hotkey_config_change;
2470 poll_mask = hotkey_source_mask;
2471 event_mask = hotkey_source_mask &
2472 (hotkey_driver_mask | hotkey_user_mask);
2473 poll_freq = hotkey_poll_freq;
2474 mutex_unlock(&hotkey_thread_data_mutex);
2475 hotkey_read_nvram(&s[so], poll_mask);
2477 while (!kthread_should_stop()) {
2478 if (t == 0) {
2479 if (likely(poll_freq))
2480 t = 1000/poll_freq;
2481 else
2482 t = 100; /* should never happen... */
2484 t = msleep_interruptible(t);
2485 if (unlikely(kthread_should_stop()))
2486 break;
2487 must_reset = try_to_freeze();
2488 if (t > 0 && !must_reset)
2489 continue;
2491 mutex_lock(&hotkey_thread_data_mutex);
2492 if (must_reset || hotkey_config_change != change_detector) {
2493 /* forget old state on thaw or config change */
2494 si = so;
2495 t = 0;
2496 change_detector = hotkey_config_change;
2498 poll_mask = hotkey_source_mask;
2499 event_mask = hotkey_source_mask &
2500 (hotkey_driver_mask | hotkey_user_mask);
2501 poll_freq = hotkey_poll_freq;
2502 mutex_unlock(&hotkey_thread_data_mutex);
2504 if (likely(poll_mask)) {
2505 hotkey_read_nvram(&s[si], poll_mask);
2506 if (likely(si != so)) {
2507 hotkey_compare_and_issue_event(&s[so], &s[si],
2508 event_mask);
2512 so = si;
2513 si ^= 1;
2516 exit:
2517 mutex_unlock(&hotkey_thread_mutex);
2518 return 0;
2521 /* call with hotkey_mutex held */
2522 static void hotkey_poll_stop_sync(void)
2524 if (tpacpi_hotkey_task) {
2525 if (frozen(tpacpi_hotkey_task) ||
2526 freezing(tpacpi_hotkey_task))
2527 thaw_process(tpacpi_hotkey_task);
2529 kthread_stop(tpacpi_hotkey_task);
2530 tpacpi_hotkey_task = NULL;
2531 mutex_lock(&hotkey_thread_mutex);
2532 /* at this point, the thread did exit */
2533 mutex_unlock(&hotkey_thread_mutex);
2537 /* call with hotkey_mutex held */
2538 static void hotkey_poll_setup(bool may_warn)
2540 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2541 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2543 if (hotkey_poll_freq > 0 &&
2544 (poll_driver_mask ||
2545 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2546 if (!tpacpi_hotkey_task) {
2547 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2548 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2549 if (IS_ERR(tpacpi_hotkey_task)) {
2550 tpacpi_hotkey_task = NULL;
2551 printk(TPACPI_ERR
2552 "could not create kernel thread "
2553 "for hotkey polling\n");
2556 } else {
2557 hotkey_poll_stop_sync();
2558 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2559 hotkey_poll_freq == 0) {
2560 printk(TPACPI_NOTICE
2561 "hot keys 0x%08x and/or events 0x%08x "
2562 "require polling, which is currently "
2563 "disabled\n",
2564 poll_user_mask, poll_driver_mask);
2569 static void hotkey_poll_setup_safe(bool may_warn)
2571 mutex_lock(&hotkey_mutex);
2572 hotkey_poll_setup(may_warn);
2573 mutex_unlock(&hotkey_mutex);
2576 /* call with hotkey_mutex held */
2577 static void hotkey_poll_set_freq(unsigned int freq)
2579 if (!freq)
2580 hotkey_poll_stop_sync();
2582 hotkey_poll_freq = freq;
2585 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2587 static void hotkey_poll_setup_safe(bool __unused)
2591 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2593 static int hotkey_inputdev_open(struct input_dev *dev)
2595 switch (tpacpi_lifecycle) {
2596 case TPACPI_LIFE_INIT:
2598 * hotkey_init will call hotkey_poll_setup_safe
2599 * at the appropriate moment
2601 return 0;
2602 case TPACPI_LIFE_EXITING:
2603 return -EBUSY;
2604 case TPACPI_LIFE_RUNNING:
2605 hotkey_poll_setup_safe(false);
2606 return 0;
2609 /* Should only happen if tpacpi_lifecycle is corrupt */
2610 BUG();
2611 return -EBUSY;
2614 static void hotkey_inputdev_close(struct input_dev *dev)
2616 /* disable hotkey polling when possible */
2617 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2618 !(hotkey_source_mask & hotkey_driver_mask))
2619 hotkey_poll_setup_safe(false);
2622 /* sysfs hotkey enable ------------------------------------------------- */
2623 static ssize_t hotkey_enable_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2627 int res, status;
2629 printk_deprecated_attribute("hotkey_enable",
2630 "Hotkey reporting is always enabled");
2632 res = hotkey_status_get(&status);
2633 if (res)
2634 return res;
2636 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2639 static ssize_t hotkey_enable_store(struct device *dev,
2640 struct device_attribute *attr,
2641 const char *buf, size_t count)
2643 unsigned long t;
2645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkeys can be disabled through hotkey_mask");
2648 if (parse_strtoul(buf, 1, &t))
2649 return -EINVAL;
2651 if (t == 0)
2652 return -EPERM;
2654 return count;
2657 static struct device_attribute dev_attr_hotkey_enable =
2658 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2659 hotkey_enable_show, hotkey_enable_store);
2661 /* sysfs hotkey mask --------------------------------------------------- */
2662 static ssize_t hotkey_mask_show(struct device *dev,
2663 struct device_attribute *attr,
2664 char *buf)
2666 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2669 static ssize_t hotkey_mask_store(struct device *dev,
2670 struct device_attribute *attr,
2671 const char *buf, size_t count)
2673 unsigned long t;
2674 int res;
2676 if (parse_strtoul(buf, 0xffffffffUL, &t))
2677 return -EINVAL;
2679 if (mutex_lock_killable(&hotkey_mutex))
2680 return -ERESTARTSYS;
2682 res = hotkey_user_mask_set(t);
2684 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2685 hotkey_poll_setup(true);
2686 #endif
2688 mutex_unlock(&hotkey_mutex);
2690 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2692 return (res) ? res : count;
2695 static struct device_attribute dev_attr_hotkey_mask =
2696 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2697 hotkey_mask_show, hotkey_mask_store);
2699 /* sysfs hotkey bios_enabled ------------------------------------------- */
2700 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2701 struct device_attribute *attr,
2702 char *buf)
2704 return sprintf(buf, "0\n");
2707 static struct device_attribute dev_attr_hotkey_bios_enabled =
2708 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2710 /* sysfs hotkey bios_mask ---------------------------------------------- */
2711 static ssize_t hotkey_bios_mask_show(struct device *dev,
2712 struct device_attribute *attr,
2713 char *buf)
2715 printk_deprecated_attribute("hotkey_bios_mask",
2716 "This attribute is useless.");
2717 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2720 static struct device_attribute dev_attr_hotkey_bios_mask =
2721 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2723 /* sysfs hotkey all_mask ----------------------------------------------- */
2724 static ssize_t hotkey_all_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2728 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2729 hotkey_all_mask | hotkey_source_mask);
2732 static struct device_attribute dev_attr_hotkey_all_mask =
2733 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2735 /* sysfs hotkey recommended_mask --------------------------------------- */
2736 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2737 struct device_attribute *attr,
2738 char *buf)
2740 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2741 (hotkey_all_mask | hotkey_source_mask)
2742 & ~hotkey_reserved_mask);
2745 static struct device_attribute dev_attr_hotkey_recommended_mask =
2746 __ATTR(hotkey_recommended_mask, S_IRUGO,
2747 hotkey_recommended_mask_show, NULL);
2749 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2751 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2752 static ssize_t hotkey_source_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2759 static ssize_t hotkey_source_mask_store(struct device *dev,
2760 struct device_attribute *attr,
2761 const char *buf, size_t count)
2763 unsigned long t;
2764 u32 r_ev;
2765 int rc;
2767 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2768 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2769 return -EINVAL;
2771 if (mutex_lock_killable(&hotkey_mutex))
2772 return -ERESTARTSYS;
2774 HOTKEY_CONFIG_CRITICAL_START
2775 hotkey_source_mask = t;
2776 HOTKEY_CONFIG_CRITICAL_END
2778 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2779 ~hotkey_source_mask);
2780 hotkey_poll_setup(true);
2782 /* check if events needed by the driver got disabled */
2783 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2784 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2786 mutex_unlock(&hotkey_mutex);
2788 if (rc < 0)
2789 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2790 "firmware event mask!\n");
2792 if (r_ev)
2793 printk(TPACPI_NOTICE "hotkey_source_mask: "
2794 "some important events were disabled: "
2795 "0x%04x\n", r_ev);
2797 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2799 return (rc < 0) ? rc : count;
2802 static struct device_attribute dev_attr_hotkey_source_mask =
2803 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2804 hotkey_source_mask_show, hotkey_source_mask_store);
2806 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2807 static ssize_t hotkey_poll_freq_show(struct device *dev,
2808 struct device_attribute *attr,
2809 char *buf)
2811 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2814 static ssize_t hotkey_poll_freq_store(struct device *dev,
2815 struct device_attribute *attr,
2816 const char *buf, size_t count)
2818 unsigned long t;
2820 if (parse_strtoul(buf, 25, &t))
2821 return -EINVAL;
2823 if (mutex_lock_killable(&hotkey_mutex))
2824 return -ERESTARTSYS;
2826 hotkey_poll_set_freq(t);
2827 hotkey_poll_setup(true);
2829 mutex_unlock(&hotkey_mutex);
2831 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2833 return count;
2836 static struct device_attribute dev_attr_hotkey_poll_freq =
2837 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2838 hotkey_poll_freq_show, hotkey_poll_freq_store);
2840 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2842 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2843 static ssize_t hotkey_radio_sw_show(struct device *dev,
2844 struct device_attribute *attr,
2845 char *buf)
2847 int res;
2848 res = hotkey_get_wlsw();
2849 if (res < 0)
2850 return res;
2852 /* Opportunistic update */
2853 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2855 return snprintf(buf, PAGE_SIZE, "%d\n",
2856 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2859 static struct device_attribute dev_attr_hotkey_radio_sw =
2860 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2862 static void hotkey_radio_sw_notify_change(void)
2864 if (tp_features.hotkey_wlsw)
2865 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2866 "hotkey_radio_sw");
2869 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2870 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2871 struct device_attribute *attr,
2872 char *buf)
2874 int res, s;
2875 res = hotkey_get_tablet_mode(&s);
2876 if (res < 0)
2877 return res;
2879 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2882 static struct device_attribute dev_attr_hotkey_tablet_mode =
2883 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2885 static void hotkey_tablet_mode_notify_change(void)
2887 if (tp_features.hotkey_tablet)
2888 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2889 "hotkey_tablet_mode");
2892 /* sysfs hotkey report_mode -------------------------------------------- */
2893 static ssize_t hotkey_report_mode_show(struct device *dev,
2894 struct device_attribute *attr,
2895 char *buf)
2897 return snprintf(buf, PAGE_SIZE, "%d\n",
2898 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2901 static struct device_attribute dev_attr_hotkey_report_mode =
2902 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2904 /* sysfs wakeup reason (pollable) -------------------------------------- */
2905 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2906 struct device_attribute *attr,
2907 char *buf)
2909 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2912 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2913 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2915 static void hotkey_wakeup_reason_notify_change(void)
2917 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2918 "wakeup_reason");
2921 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2922 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2923 struct device_attribute *attr,
2924 char *buf)
2926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2929 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2930 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2931 hotkey_wakeup_hotunplug_complete_show, NULL);
2933 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2935 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2936 "wakeup_hotunplug_complete");
2939 /* --------------------------------------------------------------------- */
2941 static struct attribute *hotkey_attributes[] __initdata = {
2942 &dev_attr_hotkey_enable.attr,
2943 &dev_attr_hotkey_bios_enabled.attr,
2944 &dev_attr_hotkey_bios_mask.attr,
2945 &dev_attr_hotkey_report_mode.attr,
2946 &dev_attr_hotkey_wakeup_reason.attr,
2947 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2948 &dev_attr_hotkey_mask.attr,
2949 &dev_attr_hotkey_all_mask.attr,
2950 &dev_attr_hotkey_recommended_mask.attr,
2951 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2952 &dev_attr_hotkey_source_mask.attr,
2953 &dev_attr_hotkey_poll_freq.attr,
2954 #endif
2958 * Sync both the hw and sw blocking state of all switches
2960 static void tpacpi_send_radiosw_update(void)
2962 int wlsw;
2965 * We must sync all rfkill controllers *before* issuing any
2966 * rfkill input events, or we will race the rfkill core input
2967 * handler.
2969 * tpacpi_inputdev_send_mutex works as a syncronization point
2970 * for the above.
2972 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2975 wlsw = hotkey_get_wlsw();
2977 /* Sync hw blocking state first if it is hw-blocked */
2978 if (wlsw == TPACPI_RFK_RADIO_OFF)
2979 tpacpi_rfk_update_hwblock_state(true);
2981 /* Sync sw blocking state */
2982 tpacpi_rfk_update_swstate_all();
2984 /* Sync hw blocking state last if it is hw-unblocked */
2985 if (wlsw == TPACPI_RFK_RADIO_ON)
2986 tpacpi_rfk_update_hwblock_state(false);
2988 /* Issue rfkill input event for WLSW switch */
2989 if (!(wlsw < 0)) {
2990 mutex_lock(&tpacpi_inputdev_send_mutex);
2992 input_report_switch(tpacpi_inputdev,
2993 SW_RFKILL_ALL, (wlsw > 0));
2994 input_sync(tpacpi_inputdev);
2996 mutex_unlock(&tpacpi_inputdev_send_mutex);
3000 * this can be unconditional, as we will poll state again
3001 * if userspace uses the notify to read data
3003 hotkey_radio_sw_notify_change();
3006 static void hotkey_exit(void)
3008 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3009 mutex_lock(&hotkey_mutex);
3010 hotkey_poll_stop_sync();
3011 mutex_unlock(&hotkey_mutex);
3012 #endif
3014 if (hotkey_dev_attributes)
3015 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3017 kfree(hotkey_keycode_map);
3019 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3020 "restoring original HKEY status and mask\n");
3021 /* yes, there is a bitwise or below, we want the
3022 * functions to be called even if one of them fail */
3023 if (((tp_features.hotkey_mask &&
3024 hotkey_mask_set(hotkey_orig_mask)) |
3025 hotkey_status_set(false)) != 0)
3026 printk(TPACPI_ERR
3027 "failed to restore hot key mask "
3028 "to BIOS defaults\n");
3031 static void __init hotkey_unmap(const unsigned int scancode)
3033 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3034 clear_bit(hotkey_keycode_map[scancode],
3035 tpacpi_inputdev->keybit);
3036 hotkey_keycode_map[scancode] = KEY_RESERVED;
3041 * HKEY quirks:
3042 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3045 #define TPACPI_HK_Q_INIMASK 0x0001
3047 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3048 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3049 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3050 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3051 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3052 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3053 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3054 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3055 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3056 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3057 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3058 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3059 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3060 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3061 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3062 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3063 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3064 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3065 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3066 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3069 static int __init hotkey_init(struct ibm_init_struct *iibm)
3071 /* Requirements for changing the default keymaps:
3073 * 1. Many of the keys are mapped to KEY_RESERVED for very
3074 * good reasons. Do not change them unless you have deep
3075 * knowledge on the IBM and Lenovo ThinkPad firmware for
3076 * the various ThinkPad models. The driver behaves
3077 * differently for KEY_RESERVED: such keys have their
3078 * hot key mask *unset* in mask_recommended, and also
3079 * in the initial hot key mask programmed into the
3080 * firmware at driver load time, which means the firm-
3081 * ware may react very differently if you change them to
3082 * something else;
3084 * 2. You must be subscribed to the linux-thinkpad and
3085 * ibm-acpi-devel mailing lists, and you should read the
3086 * list archives since 2007 if you want to change the
3087 * keymaps. This requirement exists so that you will
3088 * know the past history of problems with the thinkpad-
3089 * acpi driver keymaps, and also that you will be
3090 * listening to any bug reports;
3092 * 3. Do not send thinkpad-acpi specific patches directly to
3093 * for merging, *ever*. Send them to the linux-acpi
3094 * mailinglist for comments. Merging is to be done only
3095 * through acpi-test and the ACPI maintainer.
3097 * If the above is too much to ask, don't change the keymap.
3098 * Ask the thinkpad-acpi maintainer to do it, instead.
3100 static u16 ibm_keycode_map[] __initdata = {
3101 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3102 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3103 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3104 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3106 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3107 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3108 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3109 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3111 /* brightness: firmware always reacts to them */
3112 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3113 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3115 /* Thinklight: firmware always react to it */
3116 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3118 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3119 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3121 /* Volume: firmware always react to it and reprograms
3122 * the built-in *extra* mixer. Never map it to control
3123 * another mixer by default. */
3124 KEY_RESERVED, /* 0x14: VOLUME UP */
3125 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3126 KEY_RESERVED, /* 0x16: MUTE */
3128 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3130 /* (assignments unknown, please report if found) */
3131 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3132 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3134 static u16 lenovo_keycode_map[] __initdata = {
3135 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3136 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3137 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3138 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3140 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3141 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3142 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3143 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3145 /* These should be enabled --only-- when ACPI video
3146 * is disabled (i.e. in "vendor" mode), and are handled
3147 * in a special way by the init code */
3148 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3149 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3151 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3153 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3154 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3156 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3157 * react to it and reprograms the built-in *extra* mixer.
3158 * Never map it to control another mixer by default.
3160 * T60?, T61, R60?, R61: firmware and EC tries to send
3161 * these over the regular keyboard, so these are no-ops,
3162 * but there are still weird bugs re. MUTE, so do not
3163 * change unless you get test reports from all Lenovo
3164 * models. May cause the BIOS to interfere with the
3165 * HDA mixer.
3167 KEY_RESERVED, /* 0x14: VOLUME UP */
3168 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3169 KEY_RESERVED, /* 0x16: MUTE */
3171 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3173 /* (assignments unknown, please report if found) */
3174 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3175 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3178 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3179 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3180 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3182 int res, i;
3183 int status;
3184 int hkeyv;
3186 unsigned long quirks;
3188 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3189 "initializing hotkey subdriver\n");
3191 BUG_ON(!tpacpi_inputdev);
3192 BUG_ON(tpacpi_inputdev->open != NULL ||
3193 tpacpi_inputdev->close != NULL);
3195 TPACPI_ACPIHANDLE_INIT(hkey);
3196 mutex_init(&hotkey_mutex);
3198 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3199 mutex_init(&hotkey_thread_mutex);
3200 mutex_init(&hotkey_thread_data_mutex);
3201 #endif
3203 /* hotkey not supported on 570 */
3204 tp_features.hotkey = hkey_handle != NULL;
3206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "hotkeys are %s\n",
3208 str_supported(tp_features.hotkey));
3210 if (!tp_features.hotkey)
3211 return 1;
3213 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3214 ARRAY_SIZE(tpacpi_hotkey_qtable));
3216 tpacpi_disable_brightness_delay();
3218 /* MUST have enough space for all attributes to be added to
3219 * hotkey_dev_attributes */
3220 hotkey_dev_attributes = create_attr_set(
3221 ARRAY_SIZE(hotkey_attributes) + 2,
3222 NULL);
3223 if (!hotkey_dev_attributes)
3224 return -ENOMEM;
3225 res = add_many_to_attr_set(hotkey_dev_attributes,
3226 hotkey_attributes,
3227 ARRAY_SIZE(hotkey_attributes));
3228 if (res)
3229 goto err_exit;
3231 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3232 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3233 for HKEY interface version 0x100 */
3234 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3235 if ((hkeyv >> 8) != 1) {
3236 printk(TPACPI_ERR "unknown version of the "
3237 "HKEY interface: 0x%x\n", hkeyv);
3238 printk(TPACPI_ERR "please report this to %s\n",
3239 TPACPI_MAIL);
3240 } else {
3242 * MHKV 0x100 in A31, R40, R40e,
3243 * T4x, X31, and later
3245 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3246 "firmware HKEY interface version: 0x%x\n",
3247 hkeyv);
3249 /* Paranoia check AND init hotkey_all_mask */
3250 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3251 "MHKA", "qd")) {
3252 printk(TPACPI_ERR
3253 "missing MHKA handler, "
3254 "please report this to %s\n",
3255 TPACPI_MAIL);
3256 /* Fallback: pre-init for FN+F3,F4,F12 */
3257 hotkey_all_mask = 0x080cU;
3258 } else {
3259 tp_features.hotkey_mask = 1;
3264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "hotkey masks are %s\n",
3266 str_supported(tp_features.hotkey_mask));
3268 /* Init hotkey_all_mask if not initialized yet */
3269 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3270 (quirks & TPACPI_HK_Q_INIMASK))
3271 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3273 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3274 if (tp_features.hotkey_mask) {
3275 /* hotkey_source_mask *must* be zero for
3276 * the first hotkey_mask_get to return hotkey_orig_mask */
3277 res = hotkey_mask_get();
3278 if (res)
3279 goto err_exit;
3281 hotkey_orig_mask = hotkey_acpi_mask;
3282 } else {
3283 hotkey_orig_mask = hotkey_all_mask;
3284 hotkey_acpi_mask = hotkey_all_mask;
3287 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3288 if (dbg_wlswemul) {
3289 tp_features.hotkey_wlsw = 1;
3290 printk(TPACPI_INFO
3291 "radio switch emulation enabled\n");
3292 } else
3293 #endif
3294 /* Not all thinkpads have a hardware radio switch */
3295 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3296 tp_features.hotkey_wlsw = 1;
3297 printk(TPACPI_INFO
3298 "radio switch found; radios are %s\n",
3299 enabled(status, 0));
3301 if (tp_features.hotkey_wlsw)
3302 res = add_to_attr_set(hotkey_dev_attributes,
3303 &dev_attr_hotkey_radio_sw.attr);
3305 /* For X41t, X60t, X61t Tablets... */
3306 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3307 tp_features.hotkey_tablet = 1;
3308 printk(TPACPI_INFO
3309 "possible tablet mode switch found; "
3310 "ThinkPad in %s mode\n",
3311 (status & TP_HOTKEY_TABLET_MASK)?
3312 "tablet" : "laptop");
3313 res = add_to_attr_set(hotkey_dev_attributes,
3314 &dev_attr_hotkey_tablet_mode.attr);
3317 if (!res)
3318 res = register_attr_set_with_sysfs(
3319 hotkey_dev_attributes,
3320 &tpacpi_pdev->dev.kobj);
3321 if (res)
3322 goto err_exit;
3324 /* Set up key map */
3326 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3327 GFP_KERNEL);
3328 if (!hotkey_keycode_map) {
3329 printk(TPACPI_ERR
3330 "failed to allocate memory for key map\n");
3331 res = -ENOMEM;
3332 goto err_exit;
3335 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3336 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3337 "using Lenovo default hot key map\n");
3338 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3339 TPACPI_HOTKEY_MAP_SIZE);
3340 } else {
3341 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3342 "using IBM default hot key map\n");
3343 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3344 TPACPI_HOTKEY_MAP_SIZE);
3347 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3348 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3349 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3350 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3351 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3352 tpacpi_inputdev->keycode = hotkey_keycode_map;
3353 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3354 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3355 set_bit(hotkey_keycode_map[i],
3356 tpacpi_inputdev->keybit);
3357 } else {
3358 if (i < sizeof(hotkey_reserved_mask)*8)
3359 hotkey_reserved_mask |= 1 << i;
3363 if (tp_features.hotkey_wlsw) {
3364 set_bit(EV_SW, tpacpi_inputdev->evbit);
3365 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3367 if (tp_features.hotkey_tablet) {
3368 set_bit(EV_SW, tpacpi_inputdev->evbit);
3369 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3372 /* Do not issue duplicate brightness change events to
3373 * userspace */
3374 if (!tp_features.bright_acpimode)
3375 /* update bright_acpimode... */
3376 tpacpi_check_std_acpi_brightness_support();
3378 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3379 printk(TPACPI_INFO
3380 "This ThinkPad has standard ACPI backlight "
3381 "brightness control, supported by the ACPI "
3382 "video driver\n");
3383 printk(TPACPI_NOTICE
3384 "Disabling thinkpad-acpi brightness events "
3385 "by default...\n");
3387 /* Disable brightness up/down on Lenovo thinkpads when
3388 * ACPI is handling them, otherwise it is plain impossible
3389 * for userspace to do something even remotely sane */
3390 hotkey_reserved_mask |=
3391 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3392 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3393 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3394 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3397 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3398 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3399 & ~hotkey_all_mask
3400 & ~hotkey_reserved_mask;
3402 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3403 "hotkey source mask 0x%08x, polling freq %u\n",
3404 hotkey_source_mask, hotkey_poll_freq);
3405 #endif
3407 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3408 "enabling firmware HKEY event interface...\n");
3409 res = hotkey_status_set(true);
3410 if (res) {
3411 hotkey_exit();
3412 return res;
3414 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3415 | hotkey_driver_mask)
3416 & ~hotkey_source_mask);
3417 if (res < 0 && res != -ENXIO) {
3418 hotkey_exit();
3419 return res;
3421 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3422 & ~hotkey_reserved_mask;
3423 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3424 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3425 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3427 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3428 "legacy ibm/hotkey event reporting over procfs %s\n",
3429 (hotkey_report_mode < 2) ?
3430 "enabled" : "disabled");
3432 tpacpi_inputdev->open = &hotkey_inputdev_open;
3433 tpacpi_inputdev->close = &hotkey_inputdev_close;
3435 hotkey_poll_setup_safe(true);
3436 tpacpi_send_radiosw_update();
3437 tpacpi_input_send_tabletsw();
3439 return 0;
3441 err_exit:
3442 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3443 hotkey_dev_attributes = NULL;
3445 return (res < 0)? res : 1;
3448 static bool hotkey_notify_hotkey(const u32 hkey,
3449 bool *send_acpi_ev,
3450 bool *ignore_acpi_ev)
3452 /* 0x1000-0x1FFF: key presses */
3453 unsigned int scancode = hkey & 0xfff;
3454 *send_acpi_ev = true;
3455 *ignore_acpi_ev = false;
3457 if (scancode > 0 && scancode < 0x21) {
3458 scancode--;
3459 if (!(hotkey_source_mask & (1 << scancode))) {
3460 tpacpi_input_send_key_masked(scancode);
3461 *send_acpi_ev = false;
3462 } else {
3463 *ignore_acpi_ev = true;
3465 return true;
3467 return false;
3470 static bool hotkey_notify_wakeup(const u32 hkey,
3471 bool *send_acpi_ev,
3472 bool *ignore_acpi_ev)
3474 /* 0x2000-0x2FFF: Wakeup reason */
3475 *send_acpi_ev = true;
3476 *ignore_acpi_ev = false;
3478 switch (hkey) {
3479 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3480 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3481 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3482 *ignore_acpi_ev = true;
3483 break;
3485 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3486 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3487 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3488 *ignore_acpi_ev = true;
3489 break;
3491 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3492 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3493 printk(TPACPI_ALERT
3494 "EMERGENCY WAKEUP: battery almost empty\n");
3495 /* how to auto-heal: */
3496 /* 2313: woke up from S3, go to S4/S5 */
3497 /* 2413: woke up from S4, go to S5 */
3498 break;
3500 default:
3501 return false;
3504 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3505 printk(TPACPI_INFO
3506 "woke up due to a hot-unplug "
3507 "request...\n");
3508 hotkey_wakeup_reason_notify_change();
3510 return true;
3513 static bool hotkey_notify_usrevent(const u32 hkey,
3514 bool *send_acpi_ev,
3515 bool *ignore_acpi_ev)
3517 /* 0x5000-0x5FFF: human interface helpers */
3518 *send_acpi_ev = true;
3519 *ignore_acpi_ev = false;
3521 switch (hkey) {
3522 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3523 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3524 return true;
3526 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3527 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3528 tpacpi_input_send_tabletsw();
3529 hotkey_tablet_mode_notify_change();
3530 *send_acpi_ev = false;
3531 return true;
3533 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3534 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3535 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3536 /* do not propagate these events */
3537 *ignore_acpi_ev = true;
3538 return true;
3540 default:
3541 return false;
3545 static bool hotkey_notify_thermal(const u32 hkey,
3546 bool *send_acpi_ev,
3547 bool *ignore_acpi_ev)
3549 /* 0x6000-0x6FFF: thermal alarms */
3550 *send_acpi_ev = true;
3551 *ignore_acpi_ev = false;
3553 switch (hkey) {
3554 case TP_HKEY_EV_ALARM_BAT_HOT:
3555 printk(TPACPI_CRIT
3556 "THERMAL ALARM: battery is too hot!\n");
3557 /* recommended action: warn user through gui */
3558 return true;
3559 case TP_HKEY_EV_ALARM_BAT_XHOT:
3560 printk(TPACPI_ALERT
3561 "THERMAL EMERGENCY: battery is extremely hot!\n");
3562 /* recommended action: immediate sleep/hibernate */
3563 return true;
3564 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3565 printk(TPACPI_CRIT
3566 "THERMAL ALARM: "
3567 "a sensor reports something is too hot!\n");
3568 /* recommended action: warn user through gui, that */
3569 /* some internal component is too hot */
3570 return true;
3571 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3572 printk(TPACPI_ALERT
3573 "THERMAL EMERGENCY: "
3574 "a sensor reports something is extremely hot!\n");
3575 /* recommended action: immediate sleep/hibernate */
3576 return true;
3577 case TP_HKEY_EV_THM_TABLE_CHANGED:
3578 printk(TPACPI_INFO
3579 "EC reports that Thermal Table has changed\n");
3580 /* recommended action: do nothing, we don't have
3581 * Lenovo ATM information */
3582 return true;
3583 default:
3584 printk(TPACPI_ALERT
3585 "THERMAL ALERT: unknown thermal alarm received\n");
3586 return false;
3590 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3592 u32 hkey;
3593 bool send_acpi_ev;
3594 bool ignore_acpi_ev;
3595 bool known_ev;
3597 if (event != 0x80) {
3598 printk(TPACPI_ERR
3599 "unknown HKEY notification event %d\n", event);
3600 /* forward it to userspace, maybe it knows how to handle it */
3601 acpi_bus_generate_netlink_event(
3602 ibm->acpi->device->pnp.device_class,
3603 dev_name(&ibm->acpi->device->dev),
3604 event, 0);
3605 return;
3608 while (1) {
3609 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3610 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3611 return;
3614 if (hkey == 0) {
3615 /* queue empty */
3616 return;
3619 send_acpi_ev = true;
3620 ignore_acpi_ev = false;
3622 switch (hkey >> 12) {
3623 case 1:
3624 /* 0x1000-0x1FFF: key presses */
3625 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3626 &ignore_acpi_ev);
3627 break;
3628 case 2:
3629 /* 0x2000-0x2FFF: Wakeup reason */
3630 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3631 &ignore_acpi_ev);
3632 break;
3633 case 3:
3634 /* 0x3000-0x3FFF: bay-related wakeups */
3635 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3636 hotkey_autosleep_ack = 1;
3637 printk(TPACPI_INFO
3638 "bay ejected\n");
3639 hotkey_wakeup_hotunplug_complete_notify_change();
3640 known_ev = true;
3641 } else {
3642 known_ev = false;
3644 break;
3645 case 4:
3646 /* 0x4000-0x4FFF: dock-related wakeups */
3647 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3648 hotkey_autosleep_ack = 1;
3649 printk(TPACPI_INFO
3650 "undocked\n");
3651 hotkey_wakeup_hotunplug_complete_notify_change();
3652 known_ev = true;
3653 } else {
3654 known_ev = false;
3656 break;
3657 case 5:
3658 /* 0x5000-0x5FFF: human interface helpers */
3659 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3660 &ignore_acpi_ev);
3661 break;
3662 case 6:
3663 /* 0x6000-0x6FFF: thermal alarms */
3664 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3665 &ignore_acpi_ev);
3666 break;
3667 case 7:
3668 /* 0x7000-0x7FFF: misc */
3669 if (tp_features.hotkey_wlsw &&
3670 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3671 tpacpi_send_radiosw_update();
3672 send_acpi_ev = 0;
3673 known_ev = true;
3674 break;
3676 /* fallthrough to default */
3677 default:
3678 known_ev = false;
3680 if (!known_ev) {
3681 printk(TPACPI_NOTICE
3682 "unhandled HKEY event 0x%04x\n", hkey);
3683 printk(TPACPI_NOTICE
3684 "please report the conditions when this "
3685 "event happened to %s\n", TPACPI_MAIL);
3688 /* Legacy events */
3689 if (!ignore_acpi_ev &&
3690 (send_acpi_ev || hotkey_report_mode < 2)) {
3691 acpi_bus_generate_proc_event(ibm->acpi->device,
3692 event, hkey);
3695 /* netlink events */
3696 if (!ignore_acpi_ev && send_acpi_ev) {
3697 acpi_bus_generate_netlink_event(
3698 ibm->acpi->device->pnp.device_class,
3699 dev_name(&ibm->acpi->device->dev),
3700 event, hkey);
3705 static void hotkey_suspend(pm_message_t state)
3707 /* Do these on suspend, we get the events on early resume! */
3708 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3709 hotkey_autosleep_ack = 0;
3712 static void hotkey_resume(void)
3714 tpacpi_disable_brightness_delay();
3716 if (hotkey_status_set(true) < 0 ||
3717 hotkey_mask_set(hotkey_acpi_mask) < 0)
3718 printk(TPACPI_ERR
3719 "error while attempting to reset the event "
3720 "firmware interface\n");
3722 tpacpi_send_radiosw_update();
3723 hotkey_tablet_mode_notify_change();
3724 hotkey_wakeup_reason_notify_change();
3725 hotkey_wakeup_hotunplug_complete_notify_change();
3726 hotkey_poll_setup_safe(false);
3729 /* procfs -------------------------------------------------------------- */
3730 static int hotkey_read(char *p)
3732 int res, status;
3733 int len = 0;
3735 if (!tp_features.hotkey) {
3736 len += sprintf(p + len, "status:\t\tnot supported\n");
3737 return len;
3740 if (mutex_lock_killable(&hotkey_mutex))
3741 return -ERESTARTSYS;
3742 res = hotkey_status_get(&status);
3743 if (!res)
3744 res = hotkey_mask_get();
3745 mutex_unlock(&hotkey_mutex);
3746 if (res)
3747 return res;
3749 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3750 if (hotkey_all_mask) {
3751 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3752 len += sprintf(p + len,
3753 "commands:\tenable, disable, reset, <mask>\n");
3754 } else {
3755 len += sprintf(p + len, "mask:\t\tnot supported\n");
3756 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3759 return len;
3762 static void hotkey_enabledisable_warn(bool enable)
3764 tpacpi_log_usertask("procfs hotkey enable/disable");
3765 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3766 TPACPI_WARN
3767 "hotkey enable/disable functionality has been "
3768 "removed from the driver. Hotkeys are always "
3769 "enabled\n"))
3770 printk(TPACPI_ERR
3771 "Please remove the hotkey=enable module "
3772 "parameter, it is deprecated. Hotkeys are always "
3773 "enabled\n");
3776 static int hotkey_write(char *buf)
3778 int res;
3779 u32 mask;
3780 char *cmd;
3782 if (!tp_features.hotkey)
3783 return -ENODEV;
3785 if (mutex_lock_killable(&hotkey_mutex))
3786 return -ERESTARTSYS;
3788 mask = hotkey_user_mask;
3790 res = 0;
3791 while ((cmd = next_cmd(&buf))) {
3792 if (strlencmp(cmd, "enable") == 0) {
3793 hotkey_enabledisable_warn(1);
3794 } else if (strlencmp(cmd, "disable") == 0) {
3795 hotkey_enabledisable_warn(0);
3796 res = -EPERM;
3797 } else if (strlencmp(cmd, "reset") == 0) {
3798 mask = (hotkey_all_mask | hotkey_source_mask)
3799 & ~hotkey_reserved_mask;
3800 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3801 /* mask set */
3802 } else if (sscanf(cmd, "%x", &mask) == 1) {
3803 /* mask set */
3804 } else {
3805 res = -EINVAL;
3806 goto errexit;
3810 if (!res) {
3811 tpacpi_disclose_usertask("procfs hotkey",
3812 "set mask to 0x%08x\n", mask);
3813 res = hotkey_user_mask_set(mask);
3816 errexit:
3817 mutex_unlock(&hotkey_mutex);
3818 return res;
3821 static const struct acpi_device_id ibm_htk_device_ids[] = {
3822 {TPACPI_ACPI_HKEY_HID, 0},
3823 {"", 0},
3826 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3827 .hid = ibm_htk_device_ids,
3828 .notify = hotkey_notify,
3829 .handle = &hkey_handle,
3830 .type = ACPI_DEVICE_NOTIFY,
3833 static struct ibm_struct hotkey_driver_data = {
3834 .name = "hotkey",
3835 .read = hotkey_read,
3836 .write = hotkey_write,
3837 .exit = hotkey_exit,
3838 .resume = hotkey_resume,
3839 .suspend = hotkey_suspend,
3840 .acpi = &ibm_hotkey_acpidriver,
3843 /*************************************************************************
3844 * Bluetooth subdriver
3847 enum {
3848 /* ACPI GBDC/SBDC bits */
3849 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3850 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3851 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3852 off / last state */
3855 enum {
3856 /* ACPI \BLTH commands */
3857 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3858 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3859 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3860 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3861 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3864 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3866 static void bluetooth_suspend(pm_message_t state)
3868 /* Try to make sure radio will resume powered off */
3869 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3870 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3871 vdbg_printk(TPACPI_DBG_RFKILL,
3872 "bluetooth power down on resume request failed\n");
3875 static int bluetooth_get_status(void)
3877 int status;
3879 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3880 if (dbg_bluetoothemul)
3881 return (tpacpi_bluetooth_emulstate) ?
3882 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3883 #endif
3885 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3886 return -EIO;
3888 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3889 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3892 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3894 int status;
3896 vdbg_printk(TPACPI_DBG_RFKILL,
3897 "will attempt to %s bluetooth\n",
3898 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3900 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3901 if (dbg_bluetoothemul) {
3902 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3903 return 0;
3905 #endif
3907 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3908 if (state == TPACPI_RFK_RADIO_ON)
3909 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3910 else
3911 status = 0;
3913 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3914 return -EIO;
3916 return 0;
3919 /* sysfs bluetooth enable ---------------------------------------------- */
3920 static ssize_t bluetooth_enable_show(struct device *dev,
3921 struct device_attribute *attr,
3922 char *buf)
3924 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3925 attr, buf);
3928 static ssize_t bluetooth_enable_store(struct device *dev,
3929 struct device_attribute *attr,
3930 const char *buf, size_t count)
3932 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3933 attr, buf, count);
3936 static struct device_attribute dev_attr_bluetooth_enable =
3937 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3938 bluetooth_enable_show, bluetooth_enable_store);
3940 /* --------------------------------------------------------------------- */
3942 static struct attribute *bluetooth_attributes[] = {
3943 &dev_attr_bluetooth_enable.attr,
3944 NULL
3947 static const struct attribute_group bluetooth_attr_group = {
3948 .attrs = bluetooth_attributes,
3951 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3952 .get_status = bluetooth_get_status,
3953 .set_status = bluetooth_set_status,
3956 static void bluetooth_shutdown(void)
3958 /* Order firmware to save current state to NVRAM */
3959 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3960 TP_ACPI_BLTH_SAVE_STATE))
3961 printk(TPACPI_NOTICE
3962 "failed to save bluetooth state to NVRAM\n");
3963 else
3964 vdbg_printk(TPACPI_DBG_RFKILL,
3965 "bluestooth state saved to NVRAM\n");
3968 static void bluetooth_exit(void)
3970 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3971 &bluetooth_attr_group);
3973 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3975 bluetooth_shutdown();
3978 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3980 int res;
3981 int status = 0;
3983 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3984 "initializing bluetooth subdriver\n");
3986 TPACPI_ACPIHANDLE_INIT(hkey);
3988 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3989 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3990 tp_features.bluetooth = hkey_handle &&
3991 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3993 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3994 "bluetooth is %s, status 0x%02x\n",
3995 str_supported(tp_features.bluetooth),
3996 status);
3998 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3999 if (dbg_bluetoothemul) {
4000 tp_features.bluetooth = 1;
4001 printk(TPACPI_INFO
4002 "bluetooth switch emulation enabled\n");
4003 } else
4004 #endif
4005 if (tp_features.bluetooth &&
4006 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4007 /* no bluetooth hardware present in system */
4008 tp_features.bluetooth = 0;
4009 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4010 "bluetooth hardware not installed\n");
4013 if (!tp_features.bluetooth)
4014 return 1;
4016 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4017 &bluetooth_tprfk_ops,
4018 RFKILL_TYPE_BLUETOOTH,
4019 TPACPI_RFK_BLUETOOTH_SW_NAME,
4020 true);
4021 if (res)
4022 return res;
4024 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4025 &bluetooth_attr_group);
4026 if (res) {
4027 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4028 return res;
4031 return 0;
4034 /* procfs -------------------------------------------------------------- */
4035 static int bluetooth_read(char *p)
4037 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
4040 static int bluetooth_write(char *buf)
4042 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4045 static struct ibm_struct bluetooth_driver_data = {
4046 .name = "bluetooth",
4047 .read = bluetooth_read,
4048 .write = bluetooth_write,
4049 .exit = bluetooth_exit,
4050 .suspend = bluetooth_suspend,
4051 .shutdown = bluetooth_shutdown,
4054 /*************************************************************************
4055 * Wan subdriver
4058 enum {
4059 /* ACPI GWAN/SWAN bits */
4060 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4061 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4062 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4063 off / last state */
4066 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4068 static void wan_suspend(pm_message_t state)
4070 /* Try to make sure radio will resume powered off */
4071 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
4072 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
4073 vdbg_printk(TPACPI_DBG_RFKILL,
4074 "WWAN power down on resume request failed\n");
4077 static int wan_get_status(void)
4079 int status;
4081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4082 if (dbg_wwanemul)
4083 return (tpacpi_wwan_emulstate) ?
4084 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4085 #endif
4087 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4088 return -EIO;
4090 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4094 static int wan_set_status(enum tpacpi_rfkill_state state)
4096 int status;
4098 vdbg_printk(TPACPI_DBG_RFKILL,
4099 "will attempt to %s wwan\n",
4100 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4102 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4103 if (dbg_wwanemul) {
4104 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4105 return 0;
4107 #endif
4109 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
4110 if (state == TPACPI_RFK_RADIO_ON)
4111 status = TP_ACPI_WANCARD_RADIOSSW;
4112 else
4113 status = 0;
4115 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4116 return -EIO;
4118 return 0;
4121 /* sysfs wan enable ---------------------------------------------------- */
4122 static ssize_t wan_enable_show(struct device *dev,
4123 struct device_attribute *attr,
4124 char *buf)
4126 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4127 attr, buf);
4130 static ssize_t wan_enable_store(struct device *dev,
4131 struct device_attribute *attr,
4132 const char *buf, size_t count)
4134 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf, count);
4138 static struct device_attribute dev_attr_wan_enable =
4139 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4140 wan_enable_show, wan_enable_store);
4142 /* --------------------------------------------------------------------- */
4144 static struct attribute *wan_attributes[] = {
4145 &dev_attr_wan_enable.attr,
4146 NULL
4149 static const struct attribute_group wan_attr_group = {
4150 .attrs = wan_attributes,
4153 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4154 .get_status = wan_get_status,
4155 .set_status = wan_set_status,
4158 static void wan_shutdown(void)
4160 /* Order firmware to save current state to NVRAM */
4161 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4162 TP_ACPI_WGSV_SAVE_STATE))
4163 printk(TPACPI_NOTICE
4164 "failed to save WWAN state to NVRAM\n");
4165 else
4166 vdbg_printk(TPACPI_DBG_RFKILL,
4167 "WWAN state saved to NVRAM\n");
4170 static void wan_exit(void)
4172 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4173 &wan_attr_group);
4175 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4177 wan_shutdown();
4180 static int __init wan_init(struct ibm_init_struct *iibm)
4182 int res;
4183 int status = 0;
4185 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4186 "initializing wan subdriver\n");
4188 TPACPI_ACPIHANDLE_INIT(hkey);
4190 tp_features.wan = hkey_handle &&
4191 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "wan is %s, status 0x%02x\n",
4195 str_supported(tp_features.wan),
4196 status);
4198 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4199 if (dbg_wwanemul) {
4200 tp_features.wan = 1;
4201 printk(TPACPI_INFO
4202 "wwan switch emulation enabled\n");
4203 } else
4204 #endif
4205 if (tp_features.wan &&
4206 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4207 /* no wan hardware present in system */
4208 tp_features.wan = 0;
4209 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4210 "wan hardware not installed\n");
4213 if (!tp_features.wan)
4214 return 1;
4216 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4217 &wan_tprfk_ops,
4218 RFKILL_TYPE_WWAN,
4219 TPACPI_RFK_WWAN_SW_NAME,
4220 true);
4221 if (res)
4222 return res;
4224 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4225 &wan_attr_group);
4227 if (res) {
4228 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4229 return res;
4232 return 0;
4235 /* procfs -------------------------------------------------------------- */
4236 static int wan_read(char *p)
4238 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
4241 static int wan_write(char *buf)
4243 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4246 static struct ibm_struct wan_driver_data = {
4247 .name = "wan",
4248 .read = wan_read,
4249 .write = wan_write,
4250 .exit = wan_exit,
4251 .suspend = wan_suspend,
4252 .shutdown = wan_shutdown,
4255 /*************************************************************************
4256 * UWB subdriver
4259 enum {
4260 /* ACPI GUWB/SUWB bits */
4261 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4262 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4265 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4267 static int uwb_get_status(void)
4269 int status;
4271 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4272 if (dbg_uwbemul)
4273 return (tpacpi_uwb_emulstate) ?
4274 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4275 #endif
4277 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4278 return -EIO;
4280 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4284 static int uwb_set_status(enum tpacpi_rfkill_state state)
4286 int status;
4288 vdbg_printk(TPACPI_DBG_RFKILL,
4289 "will attempt to %s UWB\n",
4290 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4292 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4293 if (dbg_uwbemul) {
4294 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4295 return 0;
4297 #endif
4299 if (state == TPACPI_RFK_RADIO_ON)
4300 status = TP_ACPI_UWB_RADIOSSW;
4301 else
4302 status = 0;
4304 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4305 return -EIO;
4307 return 0;
4310 /* --------------------------------------------------------------------- */
4312 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4313 .get_status = uwb_get_status,
4314 .set_status = uwb_set_status,
4317 static void uwb_exit(void)
4319 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4322 static int __init uwb_init(struct ibm_init_struct *iibm)
4324 int res;
4325 int status = 0;
4327 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4328 "initializing uwb subdriver\n");
4330 TPACPI_ACPIHANDLE_INIT(hkey);
4332 tp_features.uwb = hkey_handle &&
4333 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4335 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4336 "uwb is %s, status 0x%02x\n",
4337 str_supported(tp_features.uwb),
4338 status);
4340 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4341 if (dbg_uwbemul) {
4342 tp_features.uwb = 1;
4343 printk(TPACPI_INFO
4344 "uwb switch emulation enabled\n");
4345 } else
4346 #endif
4347 if (tp_features.uwb &&
4348 !(status & TP_ACPI_UWB_HWPRESENT)) {
4349 /* no uwb hardware present in system */
4350 tp_features.uwb = 0;
4351 dbg_printk(TPACPI_DBG_INIT,
4352 "uwb hardware not installed\n");
4355 if (!tp_features.uwb)
4356 return 1;
4358 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4359 &uwb_tprfk_ops,
4360 RFKILL_TYPE_UWB,
4361 TPACPI_RFK_UWB_SW_NAME,
4362 false);
4363 return res;
4366 static struct ibm_struct uwb_driver_data = {
4367 .name = "uwb",
4368 .exit = uwb_exit,
4369 .flags.experimental = 1,
4372 /*************************************************************************
4373 * Video subdriver
4376 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4378 enum video_access_mode {
4379 TPACPI_VIDEO_NONE = 0,
4380 TPACPI_VIDEO_570, /* 570 */
4381 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4382 TPACPI_VIDEO_NEW, /* all others */
4385 enum { /* video status flags, based on VIDEO_570 */
4386 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4387 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4388 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4391 enum { /* TPACPI_VIDEO_570 constants */
4392 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4393 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4394 * video_status_flags */
4395 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4396 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4399 static enum video_access_mode video_supported;
4400 static int video_orig_autosw;
4402 static int video_autosw_get(void);
4403 static int video_autosw_set(int enable);
4405 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4407 static int __init video_init(struct ibm_init_struct *iibm)
4409 int ivga;
4411 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4413 TPACPI_ACPIHANDLE_INIT(vid);
4414 TPACPI_ACPIHANDLE_INIT(vid2);
4416 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4417 /* G41, assume IVGA doesn't change */
4418 vid_handle = vid2_handle;
4420 if (!vid_handle)
4421 /* video switching not supported on R30, R31 */
4422 video_supported = TPACPI_VIDEO_NONE;
4423 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4424 /* 570 */
4425 video_supported = TPACPI_VIDEO_570;
4426 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4427 /* 600e/x, 770e, 770x */
4428 video_supported = TPACPI_VIDEO_770;
4429 else
4430 /* all others */
4431 video_supported = TPACPI_VIDEO_NEW;
4433 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4434 str_supported(video_supported != TPACPI_VIDEO_NONE),
4435 video_supported);
4437 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4440 static void video_exit(void)
4442 dbg_printk(TPACPI_DBG_EXIT,
4443 "restoring original video autoswitch mode\n");
4444 if (video_autosw_set(video_orig_autosw))
4445 printk(TPACPI_ERR "error while trying to restore original "
4446 "video autoswitch mode\n");
4449 static int video_outputsw_get(void)
4451 int status = 0;
4452 int i;
4454 switch (video_supported) {
4455 case TPACPI_VIDEO_570:
4456 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4457 TP_ACPI_VIDEO_570_PHSCMD))
4458 return -EIO;
4459 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4460 break;
4461 case TPACPI_VIDEO_770:
4462 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_LCD;
4466 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_CRT;
4470 break;
4471 case TPACPI_VIDEO_NEW:
4472 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4473 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
4478 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4479 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_LCD;
4483 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_DVI;
4487 break;
4488 default:
4489 return -ENOSYS;
4492 return status;
4495 static int video_outputsw_set(int status)
4497 int autosw;
4498 int res = 0;
4500 switch (video_supported) {
4501 case TPACPI_VIDEO_570:
4502 res = acpi_evalf(NULL, NULL,
4503 "\\_SB.PHS2", "vdd",
4504 TP_ACPI_VIDEO_570_PHS2CMD,
4505 status | TP_ACPI_VIDEO_570_PHS2SET);
4506 break;
4507 case TPACPI_VIDEO_770:
4508 autosw = video_autosw_get();
4509 if (autosw < 0)
4510 return autosw;
4512 res = video_autosw_set(1);
4513 if (res)
4514 return res;
4515 res = acpi_evalf(vid_handle, NULL,
4516 "ASWT", "vdd", status * 0x100, 0);
4517 if (!autosw && video_autosw_set(autosw)) {
4518 printk(TPACPI_ERR
4519 "video auto-switch left enabled due to error\n");
4520 return -EIO;
4522 break;
4523 case TPACPI_VIDEO_NEW:
4524 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4525 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4526 break;
4527 default:
4528 return -ENOSYS;
4531 return (res)? 0 : -EIO;
4534 static int video_autosw_get(void)
4536 int autosw = 0;
4538 switch (video_supported) {
4539 case TPACPI_VIDEO_570:
4540 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4541 return -EIO;
4542 break;
4543 case TPACPI_VIDEO_770:
4544 case TPACPI_VIDEO_NEW:
4545 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4546 return -EIO;
4547 break;
4548 default:
4549 return -ENOSYS;
4552 return autosw & 1;
4555 static int video_autosw_set(int enable)
4557 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4558 return -EIO;
4559 return 0;
4562 static int video_outputsw_cycle(void)
4564 int autosw = video_autosw_get();
4565 int res;
4567 if (autosw < 0)
4568 return autosw;
4570 switch (video_supported) {
4571 case TPACPI_VIDEO_570:
4572 res = video_autosw_set(1);
4573 if (res)
4574 return res;
4575 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4576 break;
4577 case TPACPI_VIDEO_770:
4578 case TPACPI_VIDEO_NEW:
4579 res = video_autosw_set(1);
4580 if (res)
4581 return res;
4582 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4583 break;
4584 default:
4585 return -ENOSYS;
4587 if (!autosw && video_autosw_set(autosw)) {
4588 printk(TPACPI_ERR
4589 "video auto-switch left enabled due to error\n");
4590 return -EIO;
4593 return (res)? 0 : -EIO;
4596 static int video_expand_toggle(void)
4598 switch (video_supported) {
4599 case TPACPI_VIDEO_570:
4600 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4601 0 : -EIO;
4602 case TPACPI_VIDEO_770:
4603 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4604 0 : -EIO;
4605 case TPACPI_VIDEO_NEW:
4606 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4607 0 : -EIO;
4608 default:
4609 return -ENOSYS;
4611 /* not reached */
4614 static int video_read(char *p)
4616 int status, autosw;
4617 int len = 0;
4619 if (video_supported == TPACPI_VIDEO_NONE) {
4620 len += sprintf(p + len, "status:\t\tnot supported\n");
4621 return len;
4624 status = video_outputsw_get();
4625 if (status < 0)
4626 return status;
4628 autosw = video_autosw_get();
4629 if (autosw < 0)
4630 return autosw;
4632 len += sprintf(p + len, "status:\t\tsupported\n");
4633 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4634 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4635 if (video_supported == TPACPI_VIDEO_NEW)
4636 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4637 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4638 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4639 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4640 if (video_supported == TPACPI_VIDEO_NEW)
4641 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4642 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4643 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4645 return len;
4648 static int video_write(char *buf)
4650 char *cmd;
4651 int enable, disable, status;
4652 int res;
4654 if (video_supported == TPACPI_VIDEO_NONE)
4655 return -ENODEV;
4657 enable = 0;
4658 disable = 0;
4660 while ((cmd = next_cmd(&buf))) {
4661 if (strlencmp(cmd, "lcd_enable") == 0) {
4662 enable |= TP_ACPI_VIDEO_S_LCD;
4663 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4664 disable |= TP_ACPI_VIDEO_S_LCD;
4665 } else if (strlencmp(cmd, "crt_enable") == 0) {
4666 enable |= TP_ACPI_VIDEO_S_CRT;
4667 } else if (strlencmp(cmd, "crt_disable") == 0) {
4668 disable |= TP_ACPI_VIDEO_S_CRT;
4669 } else if (video_supported == TPACPI_VIDEO_NEW &&
4670 strlencmp(cmd, "dvi_enable") == 0) {
4671 enable |= TP_ACPI_VIDEO_S_DVI;
4672 } else if (video_supported == TPACPI_VIDEO_NEW &&
4673 strlencmp(cmd, "dvi_disable") == 0) {
4674 disable |= TP_ACPI_VIDEO_S_DVI;
4675 } else if (strlencmp(cmd, "auto_enable") == 0) {
4676 res = video_autosw_set(1);
4677 if (res)
4678 return res;
4679 } else if (strlencmp(cmd, "auto_disable") == 0) {
4680 res = video_autosw_set(0);
4681 if (res)
4682 return res;
4683 } else if (strlencmp(cmd, "video_switch") == 0) {
4684 res = video_outputsw_cycle();
4685 if (res)
4686 return res;
4687 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4688 res = video_expand_toggle();
4689 if (res)
4690 return res;
4691 } else
4692 return -EINVAL;
4695 if (enable || disable) {
4696 status = video_outputsw_get();
4697 if (status < 0)
4698 return status;
4699 res = video_outputsw_set((status & ~disable) | enable);
4700 if (res)
4701 return res;
4704 return 0;
4707 static struct ibm_struct video_driver_data = {
4708 .name = "video",
4709 .read = video_read,
4710 .write = video_write,
4711 .exit = video_exit,
4714 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4716 /*************************************************************************
4717 * Light (thinklight) subdriver
4720 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4721 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4723 static int light_get_status(void)
4725 int status = 0;
4727 if (tp_features.light_status) {
4728 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4729 return -EIO;
4730 return (!!status);
4733 return -ENXIO;
4736 static int light_set_status(int status)
4738 int rc;
4740 if (tp_features.light) {
4741 if (cmos_handle) {
4742 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4743 (status)?
4744 TP_CMOS_THINKLIGHT_ON :
4745 TP_CMOS_THINKLIGHT_OFF);
4746 } else {
4747 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4748 (status)? 1 : 0);
4750 return (rc)? 0 : -EIO;
4753 return -ENXIO;
4756 static void light_set_status_worker(struct work_struct *work)
4758 struct tpacpi_led_classdev *data =
4759 container_of(work, struct tpacpi_led_classdev, work);
4761 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4762 light_set_status((data->new_state != TPACPI_LED_OFF));
4765 static void light_sysfs_set(struct led_classdev *led_cdev,
4766 enum led_brightness brightness)
4768 struct tpacpi_led_classdev *data =
4769 container_of(led_cdev,
4770 struct tpacpi_led_classdev,
4771 led_classdev);
4772 data->new_state = (brightness != LED_OFF) ?
4773 TPACPI_LED_ON : TPACPI_LED_OFF;
4774 queue_work(tpacpi_wq, &data->work);
4777 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4779 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4782 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4783 .led_classdev = {
4784 .name = "tpacpi::thinklight",
4785 .brightness_set = &light_sysfs_set,
4786 .brightness_get = &light_sysfs_get,
4790 static int __init light_init(struct ibm_init_struct *iibm)
4792 int rc;
4794 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4796 TPACPI_ACPIHANDLE_INIT(ledb);
4797 TPACPI_ACPIHANDLE_INIT(lght);
4798 TPACPI_ACPIHANDLE_INIT(cmos);
4799 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4801 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4802 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4804 if (tp_features.light)
4805 /* light status not supported on
4806 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4807 tp_features.light_status =
4808 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4810 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4811 str_supported(tp_features.light),
4812 str_supported(tp_features.light_status));
4814 if (!tp_features.light)
4815 return 1;
4817 rc = led_classdev_register(&tpacpi_pdev->dev,
4818 &tpacpi_led_thinklight.led_classdev);
4820 if (rc < 0) {
4821 tp_features.light = 0;
4822 tp_features.light_status = 0;
4823 } else {
4824 rc = 0;
4827 return rc;
4830 static void light_exit(void)
4832 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4833 if (work_pending(&tpacpi_led_thinklight.work))
4834 flush_workqueue(tpacpi_wq);
4837 static int light_read(char *p)
4839 int len = 0;
4840 int status;
4842 if (!tp_features.light) {
4843 len += sprintf(p + len, "status:\t\tnot supported\n");
4844 } else if (!tp_features.light_status) {
4845 len += sprintf(p + len, "status:\t\tunknown\n");
4846 len += sprintf(p + len, "commands:\ton, off\n");
4847 } else {
4848 status = light_get_status();
4849 if (status < 0)
4850 return status;
4851 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4852 len += sprintf(p + len, "commands:\ton, off\n");
4855 return len;
4858 static int light_write(char *buf)
4860 char *cmd;
4861 int newstatus = 0;
4863 if (!tp_features.light)
4864 return -ENODEV;
4866 while ((cmd = next_cmd(&buf))) {
4867 if (strlencmp(cmd, "on") == 0) {
4868 newstatus = 1;
4869 } else if (strlencmp(cmd, "off") == 0) {
4870 newstatus = 0;
4871 } else
4872 return -EINVAL;
4875 return light_set_status(newstatus);
4878 static struct ibm_struct light_driver_data = {
4879 .name = "light",
4880 .read = light_read,
4881 .write = light_write,
4882 .exit = light_exit,
4885 /*************************************************************************
4886 * CMOS subdriver
4889 /* sysfs cmos_command -------------------------------------------------- */
4890 static ssize_t cmos_command_store(struct device *dev,
4891 struct device_attribute *attr,
4892 const char *buf, size_t count)
4894 unsigned long cmos_cmd;
4895 int res;
4897 if (parse_strtoul(buf, 21, &cmos_cmd))
4898 return -EINVAL;
4900 res = issue_thinkpad_cmos_command(cmos_cmd);
4901 return (res)? res : count;
4904 static struct device_attribute dev_attr_cmos_command =
4905 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4907 /* --------------------------------------------------------------------- */
4909 static int __init cmos_init(struct ibm_init_struct *iibm)
4911 int res;
4913 vdbg_printk(TPACPI_DBG_INIT,
4914 "initializing cmos commands subdriver\n");
4916 TPACPI_ACPIHANDLE_INIT(cmos);
4918 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4919 str_supported(cmos_handle != NULL));
4921 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4922 if (res)
4923 return res;
4925 return (cmos_handle)? 0 : 1;
4928 static void cmos_exit(void)
4930 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4933 static int cmos_read(char *p)
4935 int len = 0;
4937 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4938 R30, R31, T20-22, X20-21 */
4939 if (!cmos_handle)
4940 len += sprintf(p + len, "status:\t\tnot supported\n");
4941 else {
4942 len += sprintf(p + len, "status:\t\tsupported\n");
4943 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4946 return len;
4949 static int cmos_write(char *buf)
4951 char *cmd;
4952 int cmos_cmd, res;
4954 while ((cmd = next_cmd(&buf))) {
4955 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4956 cmos_cmd >= 0 && cmos_cmd <= 21) {
4957 /* cmos_cmd set */
4958 } else
4959 return -EINVAL;
4961 res = issue_thinkpad_cmos_command(cmos_cmd);
4962 if (res)
4963 return res;
4966 return 0;
4969 static struct ibm_struct cmos_driver_data = {
4970 .name = "cmos",
4971 .read = cmos_read,
4972 .write = cmos_write,
4973 .exit = cmos_exit,
4976 /*************************************************************************
4977 * LED subdriver
4980 enum led_access_mode {
4981 TPACPI_LED_NONE = 0,
4982 TPACPI_LED_570, /* 570 */
4983 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4984 TPACPI_LED_NEW, /* all others */
4987 enum { /* For TPACPI_LED_OLD */
4988 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4989 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4990 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4993 static enum led_access_mode led_supported;
4995 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4996 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4997 /* T20-22, X20-21 */
4998 "LED", /* all others */
4999 ); /* R30, R31 */
5001 #define TPACPI_LED_NUMLEDS 16
5002 static struct tpacpi_led_classdev *tpacpi_leds;
5003 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5004 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5005 /* there's a limit of 19 chars + NULL before 2.6.26 */
5006 "tpacpi::power",
5007 "tpacpi:orange:batt",
5008 "tpacpi:green:batt",
5009 "tpacpi::dock_active",
5010 "tpacpi::bay_active",
5011 "tpacpi::dock_batt",
5012 "tpacpi::unknown_led",
5013 "tpacpi::standby",
5014 "tpacpi::dock_status1",
5015 "tpacpi::dock_status2",
5016 "tpacpi::unknown_led2",
5017 "tpacpi::unknown_led3",
5018 "tpacpi::thinkvantage",
5020 #define TPACPI_SAFE_LEDS 0x1081U
5022 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5024 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5025 return false;
5026 #else
5027 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5028 #endif
5031 static int led_get_status(const unsigned int led)
5033 int status;
5034 enum led_status_t led_s;
5036 switch (led_supported) {
5037 case TPACPI_LED_570:
5038 if (!acpi_evalf(ec_handle,
5039 &status, "GLED", "dd", 1 << led))
5040 return -EIO;
5041 led_s = (status == 0)?
5042 TPACPI_LED_OFF :
5043 ((status == 1)?
5044 TPACPI_LED_ON :
5045 TPACPI_LED_BLINK);
5046 tpacpi_led_state_cache[led] = led_s;
5047 return led_s;
5048 default:
5049 return -ENXIO;
5052 /* not reached */
5055 static int led_set_status(const unsigned int led,
5056 const enum led_status_t ledstatus)
5058 /* off, on, blink. Index is led_status_t */
5059 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5060 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5062 int rc = 0;
5064 switch (led_supported) {
5065 case TPACPI_LED_570:
5066 /* 570 */
5067 if (unlikely(led > 7))
5068 return -EINVAL;
5069 if (unlikely(tpacpi_is_led_restricted(led)))
5070 return -EPERM;
5071 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5072 (1 << led), led_sled_arg1[ledstatus]))
5073 rc = -EIO;
5074 break;
5075 case TPACPI_LED_OLD:
5076 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5077 if (unlikely(led > 7))
5078 return -EINVAL;
5079 if (unlikely(tpacpi_is_led_restricted(led)))
5080 return -EPERM;
5081 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5082 if (rc >= 0)
5083 rc = ec_write(TPACPI_LED_EC_HLBL,
5084 (ledstatus == TPACPI_LED_BLINK) << led);
5085 if (rc >= 0)
5086 rc = ec_write(TPACPI_LED_EC_HLCL,
5087 (ledstatus != TPACPI_LED_OFF) << led);
5088 break;
5089 case TPACPI_LED_NEW:
5090 /* all others */
5091 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5092 return -EINVAL;
5093 if (unlikely(tpacpi_is_led_restricted(led)))
5094 return -EPERM;
5095 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5096 led, led_led_arg1[ledstatus]))
5097 rc = -EIO;
5098 break;
5099 default:
5100 rc = -ENXIO;
5103 if (!rc)
5104 tpacpi_led_state_cache[led] = ledstatus;
5106 return rc;
5109 static void led_set_status_worker(struct work_struct *work)
5111 struct tpacpi_led_classdev *data =
5112 container_of(work, struct tpacpi_led_classdev, work);
5114 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5115 led_set_status(data->led, data->new_state);
5118 static void led_sysfs_set(struct led_classdev *led_cdev,
5119 enum led_brightness brightness)
5121 struct tpacpi_led_classdev *data = container_of(led_cdev,
5122 struct tpacpi_led_classdev, led_classdev);
5124 if (brightness == LED_OFF)
5125 data->new_state = TPACPI_LED_OFF;
5126 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5127 data->new_state = TPACPI_LED_ON;
5128 else
5129 data->new_state = TPACPI_LED_BLINK;
5131 queue_work(tpacpi_wq, &data->work);
5134 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5135 unsigned long *delay_on, unsigned long *delay_off)
5137 struct tpacpi_led_classdev *data = container_of(led_cdev,
5138 struct tpacpi_led_classdev, led_classdev);
5140 /* Can we choose the flash rate? */
5141 if (*delay_on == 0 && *delay_off == 0) {
5142 /* yes. set them to the hardware blink rate (1 Hz) */
5143 *delay_on = 500; /* ms */
5144 *delay_off = 500; /* ms */
5145 } else if ((*delay_on != 500) || (*delay_off != 500))
5146 return -EINVAL;
5148 data->new_state = TPACPI_LED_BLINK;
5149 queue_work(tpacpi_wq, &data->work);
5151 return 0;
5154 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5156 int rc;
5158 struct tpacpi_led_classdev *data = container_of(led_cdev,
5159 struct tpacpi_led_classdev, led_classdev);
5161 rc = led_get_status(data->led);
5163 if (rc == TPACPI_LED_OFF || rc < 0)
5164 rc = LED_OFF; /* no error handling in led class :( */
5165 else
5166 rc = LED_FULL;
5168 return rc;
5171 static void led_exit(void)
5173 unsigned int i;
5175 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5176 if (tpacpi_leds[i].led_classdev.name)
5177 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5180 kfree(tpacpi_leds);
5183 static int __init tpacpi_init_led(unsigned int led)
5185 int rc;
5187 tpacpi_leds[led].led = led;
5189 /* LEDs with no name don't get registered */
5190 if (!tpacpi_led_names[led])
5191 return 0;
5193 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5194 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5195 if (led_supported == TPACPI_LED_570)
5196 tpacpi_leds[led].led_classdev.brightness_get =
5197 &led_sysfs_get;
5199 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5201 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5203 rc = led_classdev_register(&tpacpi_pdev->dev,
5204 &tpacpi_leds[led].led_classdev);
5205 if (rc < 0)
5206 tpacpi_leds[led].led_classdev.name = NULL;
5208 return rc;
5211 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5212 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5213 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5214 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5216 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5217 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5218 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5219 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5220 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5221 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5222 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5223 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5225 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5226 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5227 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5228 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5229 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5231 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5232 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5233 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5234 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5236 /* (1) - may have excess leds enabled on MSB */
5238 /* Defaults (order matters, keep last, don't reorder!) */
5239 { /* Lenovo */
5240 .vendor = PCI_VENDOR_ID_LENOVO,
5241 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5242 .quirks = 0x1fffU,
5244 { /* IBM ThinkPads with no EC version string */
5245 .vendor = PCI_VENDOR_ID_IBM,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5247 .quirks = 0x00ffU,
5249 { /* IBM ThinkPads with EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5252 .quirks = 0x00bfU,
5256 #undef TPACPI_LEDQ_IBM
5257 #undef TPACPI_LEDQ_LNV
5259 static int __init led_init(struct ibm_init_struct *iibm)
5261 unsigned int i;
5262 int rc;
5263 unsigned long useful_leds;
5265 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5267 TPACPI_ACPIHANDLE_INIT(led);
5269 if (!led_handle)
5270 /* led not supported on R30, R31 */
5271 led_supported = TPACPI_LED_NONE;
5272 else if (strlencmp(led_path, "SLED") == 0)
5273 /* 570 */
5274 led_supported = TPACPI_LED_570;
5275 else if (strlencmp(led_path, "SYSL") == 0)
5276 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5277 led_supported = TPACPI_LED_OLD;
5278 else
5279 /* all others */
5280 led_supported = TPACPI_LED_NEW;
5282 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5283 str_supported(led_supported), led_supported);
5285 if (led_supported == TPACPI_LED_NONE)
5286 return 1;
5288 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5289 GFP_KERNEL);
5290 if (!tpacpi_leds) {
5291 printk(TPACPI_ERR "Out of memory for LED data\n");
5292 return -ENOMEM;
5295 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5296 ARRAY_SIZE(led_useful_qtable));
5298 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5299 if (!tpacpi_is_led_restricted(i) &&
5300 test_bit(i, &useful_leds)) {
5301 rc = tpacpi_init_led(i);
5302 if (rc < 0) {
5303 led_exit();
5304 return rc;
5309 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5310 printk(TPACPI_NOTICE
5311 "warning: userspace override of important "
5312 "firmware LEDs is enabled\n");
5313 #endif
5314 return 0;
5317 #define str_led_status(s) \
5318 ((s) == TPACPI_LED_OFF ? "off" : \
5319 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5321 static int led_read(char *p)
5323 int len = 0;
5325 if (!led_supported) {
5326 len += sprintf(p + len, "status:\t\tnot supported\n");
5327 return len;
5329 len += sprintf(p + len, "status:\t\tsupported\n");
5331 if (led_supported == TPACPI_LED_570) {
5332 /* 570 */
5333 int i, status;
5334 for (i = 0; i < 8; i++) {
5335 status = led_get_status(i);
5336 if (status < 0)
5337 return -EIO;
5338 len += sprintf(p + len, "%d:\t\t%s\n",
5339 i, str_led_status(status));
5343 len += sprintf(p + len, "commands:\t"
5344 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5346 return len;
5349 static int led_write(char *buf)
5351 char *cmd;
5352 int led, rc;
5353 enum led_status_t s;
5355 if (!led_supported)
5356 return -ENODEV;
5358 while ((cmd = next_cmd(&buf))) {
5359 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5360 return -EINVAL;
5362 if (strstr(cmd, "off")) {
5363 s = TPACPI_LED_OFF;
5364 } else if (strstr(cmd, "on")) {
5365 s = TPACPI_LED_ON;
5366 } else if (strstr(cmd, "blink")) {
5367 s = TPACPI_LED_BLINK;
5368 } else {
5369 return -EINVAL;
5372 rc = led_set_status(led, s);
5373 if (rc < 0)
5374 return rc;
5377 return 0;
5380 static struct ibm_struct led_driver_data = {
5381 .name = "led",
5382 .read = led_read,
5383 .write = led_write,
5384 .exit = led_exit,
5387 /*************************************************************************
5388 * Beep subdriver
5391 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5393 #define TPACPI_BEEP_Q1 0x0001
5395 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5396 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5397 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5400 static int __init beep_init(struct ibm_init_struct *iibm)
5402 unsigned long quirks;
5404 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5406 TPACPI_ACPIHANDLE_INIT(beep);
5408 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5409 str_supported(beep_handle != NULL));
5411 quirks = tpacpi_check_quirks(beep_quirk_table,
5412 ARRAY_SIZE(beep_quirk_table));
5414 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5416 return (beep_handle)? 0 : 1;
5419 static int beep_read(char *p)
5421 int len = 0;
5423 if (!beep_handle)
5424 len += sprintf(p + len, "status:\t\tnot supported\n");
5425 else {
5426 len += sprintf(p + len, "status:\t\tsupported\n");
5427 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5430 return len;
5433 static int beep_write(char *buf)
5435 char *cmd;
5436 int beep_cmd;
5438 if (!beep_handle)
5439 return -ENODEV;
5441 while ((cmd = next_cmd(&buf))) {
5442 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5443 beep_cmd >= 0 && beep_cmd <= 17) {
5444 /* beep_cmd set */
5445 } else
5446 return -EINVAL;
5447 if (tp_features.beep_needs_two_args) {
5448 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5449 beep_cmd, 0))
5450 return -EIO;
5451 } else {
5452 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5453 beep_cmd))
5454 return -EIO;
5458 return 0;
5461 static struct ibm_struct beep_driver_data = {
5462 .name = "beep",
5463 .read = beep_read,
5464 .write = beep_write,
5467 /*************************************************************************
5468 * Thermal subdriver
5471 enum thermal_access_mode {
5472 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5473 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5474 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5475 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5476 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5479 enum { /* TPACPI_THERMAL_TPEC_* */
5480 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5481 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5482 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5485 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5486 struct ibm_thermal_sensors_struct {
5487 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5490 static enum thermal_access_mode thermal_read_mode;
5492 /* idx is zero-based */
5493 static int thermal_get_sensor(int idx, s32 *value)
5495 int t;
5496 s8 tmp;
5497 char tmpi[5];
5499 t = TP_EC_THERMAL_TMP0;
5501 switch (thermal_read_mode) {
5502 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5503 case TPACPI_THERMAL_TPEC_16:
5504 if (idx >= 8 && idx <= 15) {
5505 t = TP_EC_THERMAL_TMP8;
5506 idx -= 8;
5508 /* fallthrough */
5509 #endif
5510 case TPACPI_THERMAL_TPEC_8:
5511 if (idx <= 7) {
5512 if (!acpi_ec_read(t + idx, &tmp))
5513 return -EIO;
5514 *value = tmp * 1000;
5515 return 0;
5517 break;
5519 case TPACPI_THERMAL_ACPI_UPDT:
5520 if (idx <= 7) {
5521 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5522 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5523 return -EIO;
5524 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5525 return -EIO;
5526 *value = (t - 2732) * 100;
5527 return 0;
5529 break;
5531 case TPACPI_THERMAL_ACPI_TMP07:
5532 if (idx <= 7) {
5533 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5534 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5535 return -EIO;
5536 if (t > 127 || t < -127)
5537 t = TP_EC_THERMAL_TMP_NA;
5538 *value = t * 1000;
5539 return 0;
5541 break;
5543 case TPACPI_THERMAL_NONE:
5544 default:
5545 return -ENOSYS;
5548 return -EINVAL;
5551 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5553 int res, i;
5554 int n;
5556 n = 8;
5557 i = 0;
5559 if (!s)
5560 return -EINVAL;
5562 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5563 n = 16;
5565 for (i = 0 ; i < n; i++) {
5566 res = thermal_get_sensor(i, &s->temp[i]);
5567 if (res)
5568 return res;
5571 return n;
5574 /* sysfs temp##_input -------------------------------------------------- */
5576 static ssize_t thermal_temp_input_show(struct device *dev,
5577 struct device_attribute *attr,
5578 char *buf)
5580 struct sensor_device_attribute *sensor_attr =
5581 to_sensor_dev_attr(attr);
5582 int idx = sensor_attr->index;
5583 s32 value;
5584 int res;
5586 res = thermal_get_sensor(idx, &value);
5587 if (res)
5588 return res;
5589 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5590 return -ENXIO;
5592 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5595 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5596 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5597 thermal_temp_input_show, NULL, _idxB)
5599 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5600 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5601 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5602 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5603 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5604 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5605 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5606 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5607 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5608 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5609 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5610 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5611 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5612 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5613 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5614 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5615 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5618 #define THERMAL_ATTRS(X) \
5619 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5621 static struct attribute *thermal_temp_input_attr[] = {
5622 THERMAL_ATTRS(8),
5623 THERMAL_ATTRS(9),
5624 THERMAL_ATTRS(10),
5625 THERMAL_ATTRS(11),
5626 THERMAL_ATTRS(12),
5627 THERMAL_ATTRS(13),
5628 THERMAL_ATTRS(14),
5629 THERMAL_ATTRS(15),
5630 THERMAL_ATTRS(0),
5631 THERMAL_ATTRS(1),
5632 THERMAL_ATTRS(2),
5633 THERMAL_ATTRS(3),
5634 THERMAL_ATTRS(4),
5635 THERMAL_ATTRS(5),
5636 THERMAL_ATTRS(6),
5637 THERMAL_ATTRS(7),
5638 NULL
5641 static const struct attribute_group thermal_temp_input16_group = {
5642 .attrs = thermal_temp_input_attr
5645 static const struct attribute_group thermal_temp_input8_group = {
5646 .attrs = &thermal_temp_input_attr[8]
5649 #undef THERMAL_SENSOR_ATTR_TEMP
5650 #undef THERMAL_ATTRS
5652 /* --------------------------------------------------------------------- */
5654 static int __init thermal_init(struct ibm_init_struct *iibm)
5656 u8 t, ta1, ta2;
5657 int i;
5658 int acpi_tmp7;
5659 int res;
5661 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5663 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5665 if (thinkpad_id.ec_model) {
5667 * Direct EC access mode: sensors at registers
5668 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5669 * non-implemented, thermal sensors return 0x80 when
5670 * not available
5673 ta1 = ta2 = 0;
5674 for (i = 0; i < 8; i++) {
5675 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5676 ta1 |= t;
5677 } else {
5678 ta1 = 0;
5679 break;
5681 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5682 ta2 |= t;
5683 } else {
5684 ta1 = 0;
5685 break;
5688 if (ta1 == 0) {
5689 /* This is sheer paranoia, but we handle it anyway */
5690 if (acpi_tmp7) {
5691 printk(TPACPI_ERR
5692 "ThinkPad ACPI EC access misbehaving, "
5693 "falling back to ACPI TMPx access "
5694 "mode\n");
5695 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5696 } else {
5697 printk(TPACPI_ERR
5698 "ThinkPad ACPI EC access misbehaving, "
5699 "disabling thermal sensors access\n");
5700 thermal_read_mode = TPACPI_THERMAL_NONE;
5702 } else {
5703 thermal_read_mode =
5704 (ta2 != 0) ?
5705 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5707 } else if (acpi_tmp7) {
5708 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5709 /* 600e/x, 770e, 770x */
5710 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5711 } else {
5712 /* Standard ACPI TMPx access, max 8 sensors */
5713 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5715 } else {
5716 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5717 thermal_read_mode = TPACPI_THERMAL_NONE;
5720 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5721 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5722 thermal_read_mode);
5724 switch (thermal_read_mode) {
5725 case TPACPI_THERMAL_TPEC_16:
5726 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5727 &thermal_temp_input16_group);
5728 if (res)
5729 return res;
5730 break;
5731 case TPACPI_THERMAL_TPEC_8:
5732 case TPACPI_THERMAL_ACPI_TMP07:
5733 case TPACPI_THERMAL_ACPI_UPDT:
5734 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5735 &thermal_temp_input8_group);
5736 if (res)
5737 return res;
5738 break;
5739 case TPACPI_THERMAL_NONE:
5740 default:
5741 return 1;
5744 return 0;
5747 static void thermal_exit(void)
5749 switch (thermal_read_mode) {
5750 case TPACPI_THERMAL_TPEC_16:
5751 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5752 &thermal_temp_input16_group);
5753 break;
5754 case TPACPI_THERMAL_TPEC_8:
5755 case TPACPI_THERMAL_ACPI_TMP07:
5756 case TPACPI_THERMAL_ACPI_UPDT:
5757 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5758 &thermal_temp_input16_group);
5759 break;
5760 case TPACPI_THERMAL_NONE:
5761 default:
5762 break;
5766 static int thermal_read(char *p)
5768 int len = 0;
5769 int n, i;
5770 struct ibm_thermal_sensors_struct t;
5772 n = thermal_get_sensors(&t);
5773 if (unlikely(n < 0))
5774 return n;
5776 len += sprintf(p + len, "temperatures:\t");
5778 if (n > 0) {
5779 for (i = 0; i < (n - 1); i++)
5780 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5781 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5782 } else
5783 len += sprintf(p + len, "not supported\n");
5785 return len;
5788 static struct ibm_struct thermal_driver_data = {
5789 .name = "thermal",
5790 .read = thermal_read,
5791 .exit = thermal_exit,
5794 /*************************************************************************
5795 * EC Dump subdriver
5798 static u8 ecdump_regs[256];
5800 static int ecdump_read(char *p)
5802 int len = 0;
5803 int i, j;
5804 u8 v;
5806 len += sprintf(p + len, "EC "
5807 " +00 +01 +02 +03 +04 +05 +06 +07"
5808 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5809 for (i = 0; i < 256; i += 16) {
5810 len += sprintf(p + len, "EC 0x%02x:", i);
5811 for (j = 0; j < 16; j++) {
5812 if (!acpi_ec_read(i + j, &v))
5813 break;
5814 if (v != ecdump_regs[i + j])
5815 len += sprintf(p + len, " *%02x", v);
5816 else
5817 len += sprintf(p + len, " %02x", v);
5818 ecdump_regs[i + j] = v;
5820 len += sprintf(p + len, "\n");
5821 if (j != 16)
5822 break;
5825 /* These are way too dangerous to advertise openly... */
5826 #if 0
5827 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5828 " (<offset> is 00-ff, <value> is 00-ff)\n");
5829 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5830 " (<offset> is 00-ff, <value> is 0-255)\n");
5831 #endif
5832 return len;
5835 static int ecdump_write(char *buf)
5837 char *cmd;
5838 int i, v;
5840 while ((cmd = next_cmd(&buf))) {
5841 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5842 /* i and v set */
5843 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5844 /* i and v set */
5845 } else
5846 return -EINVAL;
5847 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5848 if (!acpi_ec_write(i, v))
5849 return -EIO;
5850 } else
5851 return -EINVAL;
5854 return 0;
5857 static struct ibm_struct ecdump_driver_data = {
5858 .name = "ecdump",
5859 .read = ecdump_read,
5860 .write = ecdump_write,
5861 .flags.experimental = 1,
5864 /*************************************************************************
5865 * Backlight/brightness subdriver
5868 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5871 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5872 * CMOS NVRAM byte 0x5E, bits 0-3.
5874 * EC HBRV (0x31) has the following layout
5875 * Bit 7: unknown function
5876 * Bit 6: unknown function
5877 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5878 * Bit 4: must be set to zero to avoid problems
5879 * Bit 3-0: backlight brightness level
5881 * brightness_get_raw returns status data in the HBRV layout
5883 * WARNING: The X61 has been verified to use HBRV for something else, so
5884 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5885 * testing on the very early *60 Lenovo models...
5888 enum {
5889 TP_EC_BACKLIGHT = 0x31,
5891 /* TP_EC_BACKLIGHT bitmasks */
5892 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5893 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5894 TP_EC_BACKLIGHT_MAPSW = 0x20,
5897 enum tpacpi_brightness_access_mode {
5898 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5899 TPACPI_BRGHT_MODE_EC, /* EC control */
5900 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5901 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5902 TPACPI_BRGHT_MODE_MAX
5905 static struct backlight_device *ibm_backlight_device;
5907 static enum tpacpi_brightness_access_mode brightness_mode =
5908 TPACPI_BRGHT_MODE_MAX;
5910 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5912 static struct mutex brightness_mutex;
5914 /* NVRAM brightness access,
5915 * call with brightness_mutex held! */
5916 static unsigned int tpacpi_brightness_nvram_get(void)
5918 u8 lnvram;
5920 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5921 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5922 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5923 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5925 return lnvram;
5928 static void tpacpi_brightness_checkpoint_nvram(void)
5930 u8 lec = 0;
5931 u8 b_nvram;
5933 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5934 return;
5936 vdbg_printk(TPACPI_DBG_BRGHT,
5937 "trying to checkpoint backlight level to NVRAM...\n");
5939 if (mutex_lock_killable(&brightness_mutex) < 0)
5940 return;
5942 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5943 goto unlock;
5944 lec &= TP_EC_BACKLIGHT_LVLMSK;
5945 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5947 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5948 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5949 /* NVRAM needs update */
5950 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5951 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5952 b_nvram |= lec;
5953 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5954 dbg_printk(TPACPI_DBG_BRGHT,
5955 "updated NVRAM backlight level to %u (0x%02x)\n",
5956 (unsigned int) lec, (unsigned int) b_nvram);
5957 } else
5958 vdbg_printk(TPACPI_DBG_BRGHT,
5959 "NVRAM backlight level already is %u (0x%02x)\n",
5960 (unsigned int) lec, (unsigned int) b_nvram);
5962 unlock:
5963 mutex_unlock(&brightness_mutex);
5967 /* call with brightness_mutex held! */
5968 static int tpacpi_brightness_get_raw(int *status)
5970 u8 lec = 0;
5972 switch (brightness_mode) {
5973 case TPACPI_BRGHT_MODE_UCMS_STEP:
5974 *status = tpacpi_brightness_nvram_get();
5975 return 0;
5976 case TPACPI_BRGHT_MODE_EC:
5977 case TPACPI_BRGHT_MODE_ECNVRAM:
5978 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5979 return -EIO;
5980 *status = lec;
5981 return 0;
5982 default:
5983 return -ENXIO;
5987 /* call with brightness_mutex held! */
5988 /* do NOT call with illegal backlight level value */
5989 static int tpacpi_brightness_set_ec(unsigned int value)
5991 u8 lec = 0;
5993 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5994 return -EIO;
5996 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5997 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5998 (value & TP_EC_BACKLIGHT_LVLMSK))))
5999 return -EIO;
6001 return 0;
6004 /* call with brightness_mutex held! */
6005 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6007 int cmos_cmd, inc;
6008 unsigned int current_value, i;
6010 current_value = tpacpi_brightness_nvram_get();
6012 if (value == current_value)
6013 return 0;
6015 cmos_cmd = (value > current_value) ?
6016 TP_CMOS_BRIGHTNESS_UP :
6017 TP_CMOS_BRIGHTNESS_DOWN;
6018 inc = (value > current_value) ? 1 : -1;
6020 for (i = current_value; i != value; i += inc)
6021 if (issue_thinkpad_cmos_command(cmos_cmd))
6022 return -EIO;
6024 return 0;
6027 /* May return EINTR which can always be mapped to ERESTARTSYS */
6028 static int brightness_set(unsigned int value)
6030 int res;
6032 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6033 value < 0)
6034 return -EINVAL;
6036 vdbg_printk(TPACPI_DBG_BRGHT,
6037 "set backlight level to %d\n", value);
6039 res = mutex_lock_killable(&brightness_mutex);
6040 if (res < 0)
6041 return res;
6043 switch (brightness_mode) {
6044 case TPACPI_BRGHT_MODE_EC:
6045 case TPACPI_BRGHT_MODE_ECNVRAM:
6046 res = tpacpi_brightness_set_ec(value);
6047 break;
6048 case TPACPI_BRGHT_MODE_UCMS_STEP:
6049 res = tpacpi_brightness_set_ucmsstep(value);
6050 break;
6051 default:
6052 res = -ENXIO;
6055 mutex_unlock(&brightness_mutex);
6056 return res;
6059 /* sysfs backlight class ----------------------------------------------- */
6061 static int brightness_update_status(struct backlight_device *bd)
6063 unsigned int level =
6064 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6065 bd->props.power == FB_BLANK_UNBLANK) ?
6066 bd->props.brightness : 0;
6068 dbg_printk(TPACPI_DBG_BRGHT,
6069 "backlight: attempt to set level to %d\n",
6070 level);
6072 /* it is the backlight class's job (caller) to handle
6073 * EINTR and other errors properly */
6074 return brightness_set(level);
6077 static int brightness_get(struct backlight_device *bd)
6079 int status, res;
6081 res = mutex_lock_killable(&brightness_mutex);
6082 if (res < 0)
6083 return 0;
6085 res = tpacpi_brightness_get_raw(&status);
6087 mutex_unlock(&brightness_mutex);
6089 if (res < 0)
6090 return 0;
6092 return status & TP_EC_BACKLIGHT_LVLMSK;
6095 static void tpacpi_brightness_notify_change(void)
6097 backlight_force_update(ibm_backlight_device,
6098 BACKLIGHT_UPDATE_HOTKEY);
6101 static struct backlight_ops ibm_backlight_data = {
6102 .get_brightness = brightness_get,
6103 .update_status = brightness_update_status,
6106 /* --------------------------------------------------------------------- */
6109 * These are only useful for models that have only one possibility
6110 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6111 * these quirks.
6113 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6114 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6115 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6117 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6118 /* Models with ATI GPUs known to require ECNVRAM mode */
6119 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6121 /* Models with ATI GPUs that can use ECNVRAM */
6122 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6123 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6124 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6125 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6127 /* Models with Intel Extreme Graphics 2 */
6128 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6129 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6130 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6132 /* Models with Intel GMA900 */
6133 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6134 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6135 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6138 static int __init brightness_init(struct ibm_init_struct *iibm)
6140 int b;
6141 unsigned long quirks;
6143 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6145 mutex_init(&brightness_mutex);
6147 quirks = tpacpi_check_quirks(brightness_quirk_table,
6148 ARRAY_SIZE(brightness_quirk_table));
6151 * We always attempt to detect acpi support, so as to switch
6152 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6153 * going to publish a backlight interface
6155 b = tpacpi_check_std_acpi_brightness_support();
6156 if (b > 0) {
6158 if (acpi_video_backlight_support()) {
6159 if (brightness_enable > 1) {
6160 printk(TPACPI_NOTICE
6161 "Standard ACPI backlight interface "
6162 "available, not loading native one.\n");
6163 return 1;
6164 } else if (brightness_enable == 1) {
6165 printk(TPACPI_NOTICE
6166 "Backlight control force enabled, even if standard "
6167 "ACPI backlight interface is available\n");
6169 } else {
6170 if (brightness_enable > 1) {
6171 printk(TPACPI_NOTICE
6172 "Standard ACPI backlight interface not "
6173 "available, thinkpad_acpi native "
6174 "brightness control enabled\n");
6179 if (!brightness_enable) {
6180 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6181 "brightness support disabled by "
6182 "module parameter\n");
6183 return 1;
6186 if (b > 16) {
6187 printk(TPACPI_ERR
6188 "Unsupported brightness interface, "
6189 "please contact %s\n", TPACPI_MAIL);
6190 return 1;
6192 if (b == 16)
6193 tp_features.bright_16levels = 1;
6196 * Check for module parameter bogosity, note that we
6197 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6198 * able to detect "unspecified"
6200 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6201 return -EINVAL;
6203 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6204 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6205 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6206 if (quirks & TPACPI_BRGHT_Q_EC)
6207 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6208 else
6209 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6211 dbg_printk(TPACPI_DBG_BRGHT,
6212 "driver auto-selected brightness_mode=%d\n",
6213 brightness_mode);
6216 /* Safety */
6217 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6218 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6219 brightness_mode == TPACPI_BRGHT_MODE_EC))
6220 return -EINVAL;
6222 if (tpacpi_brightness_get_raw(&b) < 0)
6223 return 1;
6225 if (tp_features.bright_16levels)
6226 printk(TPACPI_INFO
6227 "detected a 16-level brightness capable ThinkPad\n");
6229 ibm_backlight_device = backlight_device_register(
6230 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6231 &ibm_backlight_data);
6232 if (IS_ERR(ibm_backlight_device)) {
6233 int rc = PTR_ERR(ibm_backlight_device);
6234 ibm_backlight_device = NULL;
6235 printk(TPACPI_ERR "Could not register backlight device\n");
6236 return rc;
6238 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6239 "brightness is supported\n");
6241 if (quirks & TPACPI_BRGHT_Q_ASK) {
6242 printk(TPACPI_NOTICE
6243 "brightness: will use unverified default: "
6244 "brightness_mode=%d\n", brightness_mode);
6245 printk(TPACPI_NOTICE
6246 "brightness: please report to %s whether it works well "
6247 "or not on your ThinkPad\n", TPACPI_MAIL);
6250 ibm_backlight_device->props.max_brightness =
6251 (tp_features.bright_16levels)? 15 : 7;
6252 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6253 backlight_update_status(ibm_backlight_device);
6255 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6256 "brightness: registering brightness hotkeys "
6257 "as change notification\n");
6258 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6259 | TP_ACPI_HKEY_BRGHTUP_MASK
6260 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6261 return 0;
6264 static void brightness_suspend(pm_message_t state)
6266 tpacpi_brightness_checkpoint_nvram();
6269 static void brightness_shutdown(void)
6271 tpacpi_brightness_checkpoint_nvram();
6274 static void brightness_exit(void)
6276 if (ibm_backlight_device) {
6277 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6278 "calling backlight_device_unregister()\n");
6279 backlight_device_unregister(ibm_backlight_device);
6282 tpacpi_brightness_checkpoint_nvram();
6285 static int brightness_read(char *p)
6287 int len = 0;
6288 int level;
6290 level = brightness_get(NULL);
6291 if (level < 0) {
6292 len += sprintf(p + len, "level:\t\tunreadable\n");
6293 } else {
6294 len += sprintf(p + len, "level:\t\t%d\n", level);
6295 len += sprintf(p + len, "commands:\tup, down\n");
6296 len += sprintf(p + len, "commands:\tlevel <level>"
6297 " (<level> is 0-%d)\n",
6298 (tp_features.bright_16levels) ? 15 : 7);
6301 return len;
6304 static int brightness_write(char *buf)
6306 int level;
6307 int rc;
6308 char *cmd;
6309 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6311 level = brightness_get(NULL);
6312 if (level < 0)
6313 return level;
6315 while ((cmd = next_cmd(&buf))) {
6316 if (strlencmp(cmd, "up") == 0) {
6317 if (level < max_level)
6318 level++;
6319 } else if (strlencmp(cmd, "down") == 0) {
6320 if (level > 0)
6321 level--;
6322 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6323 level >= 0 && level <= max_level) {
6324 /* new level set */
6325 } else
6326 return -EINVAL;
6329 tpacpi_disclose_usertask("procfs brightness",
6330 "set level to %d\n", level);
6333 * Now we know what the final level should be, so we try to set it.
6334 * Doing it this way makes the syscall restartable in case of EINTR
6336 rc = brightness_set(level);
6337 if (!rc && ibm_backlight_device)
6338 backlight_force_update(ibm_backlight_device,
6339 BACKLIGHT_UPDATE_SYSFS);
6340 return (rc == -EINTR)? -ERESTARTSYS : rc;
6343 static struct ibm_struct brightness_driver_data = {
6344 .name = "brightness",
6345 .read = brightness_read,
6346 .write = brightness_write,
6347 .exit = brightness_exit,
6348 .suspend = brightness_suspend,
6349 .shutdown = brightness_shutdown,
6352 /*************************************************************************
6353 * Volume subdriver
6356 static int volume_offset = 0x30;
6358 static int volume_read(char *p)
6360 int len = 0;
6361 u8 level;
6363 if (!acpi_ec_read(volume_offset, &level)) {
6364 len += sprintf(p + len, "level:\t\tunreadable\n");
6365 } else {
6366 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6367 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6368 len += sprintf(p + len, "commands:\tup, down, mute\n");
6369 len += sprintf(p + len, "commands:\tlevel <level>"
6370 " (<level> is 0-15)\n");
6373 return len;
6376 static int volume_write(char *buf)
6378 int cmos_cmd, inc, i;
6379 u8 level, mute;
6380 int new_level, new_mute;
6381 char *cmd;
6383 while ((cmd = next_cmd(&buf))) {
6384 if (!acpi_ec_read(volume_offset, &level))
6385 return -EIO;
6386 new_mute = mute = level & 0x40;
6387 new_level = level = level & 0xf;
6389 if (strlencmp(cmd, "up") == 0) {
6390 if (mute)
6391 new_mute = 0;
6392 else
6393 new_level = level == 15 ? 15 : level + 1;
6394 } else if (strlencmp(cmd, "down") == 0) {
6395 if (mute)
6396 new_mute = 0;
6397 else
6398 new_level = level == 0 ? 0 : level - 1;
6399 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6400 new_level >= 0 && new_level <= 15) {
6401 /* new_level set */
6402 } else if (strlencmp(cmd, "mute") == 0) {
6403 new_mute = 0x40;
6404 } else
6405 return -EINVAL;
6407 if (new_level != level) {
6408 /* mute doesn't change */
6410 cmos_cmd = (new_level > level) ?
6411 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6412 inc = new_level > level ? 1 : -1;
6414 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6415 !acpi_ec_write(volume_offset, level)))
6416 return -EIO;
6418 for (i = level; i != new_level; i += inc)
6419 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6420 !acpi_ec_write(volume_offset, i + inc))
6421 return -EIO;
6423 if (mute &&
6424 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6425 !acpi_ec_write(volume_offset, new_level + mute))) {
6426 return -EIO;
6430 if (new_mute != mute) {
6431 /* level doesn't change */
6433 cmos_cmd = (new_mute) ?
6434 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6436 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6437 !acpi_ec_write(volume_offset, level + new_mute))
6438 return -EIO;
6442 return 0;
6445 static struct ibm_struct volume_driver_data = {
6446 .name = "volume",
6447 .read = volume_read,
6448 .write = volume_write,
6451 /*************************************************************************
6452 * Fan subdriver
6456 * FAN ACCESS MODES
6458 * TPACPI_FAN_RD_ACPI_GFAN:
6459 * ACPI GFAN method: returns fan level
6461 * see TPACPI_FAN_WR_ACPI_SFAN
6462 * EC 0x2f (HFSP) not available if GFAN exists
6464 * TPACPI_FAN_WR_ACPI_SFAN:
6465 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6467 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6468 * it for writing.
6470 * TPACPI_FAN_WR_TPEC:
6471 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6472 * Supported on almost all ThinkPads
6474 * Fan speed changes of any sort (including those caused by the
6475 * disengaged mode) are usually done slowly by the firmware as the
6476 * maximum ammount of fan duty cycle change per second seems to be
6477 * limited.
6479 * Reading is not available if GFAN exists.
6480 * Writing is not available if SFAN exists.
6482 * Bits
6483 * 7 automatic mode engaged;
6484 * (default operation mode of the ThinkPad)
6485 * fan level is ignored in this mode.
6486 * 6 full speed mode (takes precedence over bit 7);
6487 * not available on all thinkpads. May disable
6488 * the tachometer while the fan controller ramps up
6489 * the speed (which can take up to a few *minutes*).
6490 * Speeds up fan to 100% duty-cycle, which is far above
6491 * the standard RPM levels. It is not impossible that
6492 * it could cause hardware damage.
6493 * 5-3 unused in some models. Extra bits for fan level
6494 * in others, but still useless as all values above
6495 * 7 map to the same speed as level 7 in these models.
6496 * 2-0 fan level (0..7 usually)
6497 * 0x00 = stop
6498 * 0x07 = max (set when temperatures critical)
6499 * Some ThinkPads may have other levels, see
6500 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6502 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6503 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6504 * does so, its initial value is meaningless (0x07).
6506 * For firmware bugs, refer to:
6507 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6509 * ----
6511 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6512 * Main fan tachometer reading (in RPM)
6514 * This register is present on all ThinkPads with a new-style EC, and
6515 * it is known not to be present on the A21m/e, and T22, as there is
6516 * something else in offset 0x84 according to the ACPI DSDT. Other
6517 * ThinkPads from this same time period (and earlier) probably lack the
6518 * tachometer as well.
6520 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6521 * was never fixed by IBM to report the EC firmware version string
6522 * probably support the tachometer (like the early X models), so
6523 * detecting it is quite hard. We need more data to know for sure.
6525 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6526 * might result.
6528 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6529 * mode.
6531 * For firmware bugs, refer to:
6532 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6534 * ----
6536 * ThinkPad EC register 0x31 bit 0 (only on select models)
6538 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6539 * show the speed of the main fan. When bit 0 of EC register 0x31
6540 * is one, the tachometer registers show the speed of the auxiliary
6541 * fan.
6543 * Fan control seems to affect both fans, regardless of the state
6544 * of this bit.
6546 * So far, only the firmware for the X60/X61 non-tablet versions
6547 * seem to support this (firmware TP-7M).
6549 * TPACPI_FAN_WR_ACPI_FANS:
6550 * ThinkPad X31, X40, X41. Not available in the X60.
6552 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6553 * high speed. ACPI DSDT seems to map these three speeds to levels
6554 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6555 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6557 * The speeds are stored on handles
6558 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6560 * There are three default speed sets, accessible as handles:
6561 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6563 * ACPI DSDT switches which set is in use depending on various
6564 * factors.
6566 * TPACPI_FAN_WR_TPEC is also available and should be used to
6567 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6568 * but the ACPI tables just mention level 7.
6571 enum { /* Fan control constants */
6572 fan_status_offset = 0x2f, /* EC register 0x2f */
6573 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6574 * 0x84 must be read before 0x85 */
6575 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6576 bit 0 selects which fan is active */
6578 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6579 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6581 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6584 enum fan_status_access_mode {
6585 TPACPI_FAN_NONE = 0, /* No fan status or control */
6586 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6587 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6590 enum fan_control_access_mode {
6591 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6592 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6593 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6594 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6597 enum fan_control_commands {
6598 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6599 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6600 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6601 * and also watchdog cmd */
6604 static int fan_control_allowed;
6606 static enum fan_status_access_mode fan_status_access_mode;
6607 static enum fan_control_access_mode fan_control_access_mode;
6608 static enum fan_control_commands fan_control_commands;
6610 static u8 fan_control_initial_status;
6611 static u8 fan_control_desired_level;
6612 static u8 fan_control_resume_level;
6613 static int fan_watchdog_maxinterval;
6615 static struct mutex fan_mutex;
6617 static void fan_watchdog_fire(struct work_struct *ignored);
6618 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6620 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6621 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6622 "\\FSPD", /* 600e/x, 770e, 770x */
6623 ); /* all others */
6624 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6625 "JFNS", /* 770x-JL */
6626 ); /* all others */
6629 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6630 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6631 * be in auto mode (0x80).
6633 * This is corrected by any write to HFSP either by the driver, or
6634 * by the firmware.
6636 * We assume 0x07 really means auto mode while this quirk is active,
6637 * as this is far more likely than the ThinkPad being in level 7,
6638 * which is only used by the firmware during thermal emergencies.
6640 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6641 * TP-70 (T43, R52), which are known to be buggy.
6644 static void fan_quirk1_setup(void)
6646 if (fan_control_initial_status == 0x07) {
6647 printk(TPACPI_NOTICE
6648 "fan_init: initial fan status is unknown, "
6649 "assuming it is in auto mode\n");
6650 tp_features.fan_ctrl_status_undef = 1;
6654 static void fan_quirk1_handle(u8 *fan_status)
6656 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6657 if (*fan_status != fan_control_initial_status) {
6658 /* something changed the HFSP regisnter since
6659 * driver init time, so it is not undefined
6660 * anymore */
6661 tp_features.fan_ctrl_status_undef = 0;
6662 } else {
6663 /* Return most likely status. In fact, it
6664 * might be the only possible status */
6665 *fan_status = TP_EC_FAN_AUTO;
6670 /* Select main fan on X60/X61, NOOP on others */
6671 static bool fan_select_fan1(void)
6673 if (tp_features.second_fan) {
6674 u8 val;
6676 if (ec_read(fan_select_offset, &val) < 0)
6677 return false;
6678 val &= 0xFEU;
6679 if (ec_write(fan_select_offset, val) < 0)
6680 return false;
6682 return true;
6685 /* Select secondary fan on X60/X61 */
6686 static bool fan_select_fan2(void)
6688 u8 val;
6690 if (!tp_features.second_fan)
6691 return false;
6693 if (ec_read(fan_select_offset, &val) < 0)
6694 return false;
6695 val |= 0x01U;
6696 if (ec_write(fan_select_offset, val) < 0)
6697 return false;
6699 return true;
6703 * Call with fan_mutex held
6705 static void fan_update_desired_level(u8 status)
6707 if ((status &
6708 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6709 if (status > 7)
6710 fan_control_desired_level = 7;
6711 else
6712 fan_control_desired_level = status;
6716 static int fan_get_status(u8 *status)
6718 u8 s;
6720 /* TODO:
6721 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6723 switch (fan_status_access_mode) {
6724 case TPACPI_FAN_RD_ACPI_GFAN:
6725 /* 570, 600e/x, 770e, 770x */
6727 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6728 return -EIO;
6730 if (likely(status))
6731 *status = s & 0x07;
6733 break;
6735 case TPACPI_FAN_RD_TPEC:
6736 /* all except 570, 600e/x, 770e, 770x */
6737 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6738 return -EIO;
6740 if (likely(status)) {
6741 *status = s;
6742 fan_quirk1_handle(status);
6745 break;
6747 default:
6748 return -ENXIO;
6751 return 0;
6754 static int fan_get_status_safe(u8 *status)
6756 int rc;
6757 u8 s;
6759 if (mutex_lock_killable(&fan_mutex))
6760 return -ERESTARTSYS;
6761 rc = fan_get_status(&s);
6762 if (!rc)
6763 fan_update_desired_level(s);
6764 mutex_unlock(&fan_mutex);
6766 if (status)
6767 *status = s;
6769 return rc;
6772 static int fan_get_speed(unsigned int *speed)
6774 u8 hi, lo;
6776 switch (fan_status_access_mode) {
6777 case TPACPI_FAN_RD_TPEC:
6778 /* all except 570, 600e/x, 770e, 770x */
6779 if (unlikely(!fan_select_fan1()))
6780 return -EIO;
6781 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6782 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6783 return -EIO;
6785 if (likely(speed))
6786 *speed = (hi << 8) | lo;
6788 break;
6790 default:
6791 return -ENXIO;
6794 return 0;
6797 static int fan2_get_speed(unsigned int *speed)
6799 u8 hi, lo;
6800 bool rc;
6802 switch (fan_status_access_mode) {
6803 case TPACPI_FAN_RD_TPEC:
6804 /* all except 570, 600e/x, 770e, 770x */
6805 if (unlikely(!fan_select_fan2()))
6806 return -EIO;
6807 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6808 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6809 fan_select_fan1(); /* play it safe */
6810 if (rc)
6811 return -EIO;
6813 if (likely(speed))
6814 *speed = (hi << 8) | lo;
6816 break;
6818 default:
6819 return -ENXIO;
6822 return 0;
6825 static int fan_set_level(int level)
6827 if (!fan_control_allowed)
6828 return -EPERM;
6830 switch (fan_control_access_mode) {
6831 case TPACPI_FAN_WR_ACPI_SFAN:
6832 if (level >= 0 && level <= 7) {
6833 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6834 return -EIO;
6835 } else
6836 return -EINVAL;
6837 break;
6839 case TPACPI_FAN_WR_ACPI_FANS:
6840 case TPACPI_FAN_WR_TPEC:
6841 if (!(level & TP_EC_FAN_AUTO) &&
6842 !(level & TP_EC_FAN_FULLSPEED) &&
6843 ((level < 0) || (level > 7)))
6844 return -EINVAL;
6846 /* safety net should the EC not support AUTO
6847 * or FULLSPEED mode bits and just ignore them */
6848 if (level & TP_EC_FAN_FULLSPEED)
6849 level |= 7; /* safety min speed 7 */
6850 else if (level & TP_EC_FAN_AUTO)
6851 level |= 4; /* safety min speed 4 */
6853 if (!acpi_ec_write(fan_status_offset, level))
6854 return -EIO;
6855 else
6856 tp_features.fan_ctrl_status_undef = 0;
6857 break;
6859 default:
6860 return -ENXIO;
6863 vdbg_printk(TPACPI_DBG_FAN,
6864 "fan control: set fan control register to 0x%02x\n", level);
6865 return 0;
6868 static int fan_set_level_safe(int level)
6870 int rc;
6872 if (!fan_control_allowed)
6873 return -EPERM;
6875 if (mutex_lock_killable(&fan_mutex))
6876 return -ERESTARTSYS;
6878 if (level == TPACPI_FAN_LAST_LEVEL)
6879 level = fan_control_desired_level;
6881 rc = fan_set_level(level);
6882 if (!rc)
6883 fan_update_desired_level(level);
6885 mutex_unlock(&fan_mutex);
6886 return rc;
6889 static int fan_set_enable(void)
6891 u8 s;
6892 int rc;
6894 if (!fan_control_allowed)
6895 return -EPERM;
6897 if (mutex_lock_killable(&fan_mutex))
6898 return -ERESTARTSYS;
6900 switch (fan_control_access_mode) {
6901 case TPACPI_FAN_WR_ACPI_FANS:
6902 case TPACPI_FAN_WR_TPEC:
6903 rc = fan_get_status(&s);
6904 if (rc < 0)
6905 break;
6907 /* Don't go out of emergency fan mode */
6908 if (s != 7) {
6909 s &= 0x07;
6910 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6913 if (!acpi_ec_write(fan_status_offset, s))
6914 rc = -EIO;
6915 else {
6916 tp_features.fan_ctrl_status_undef = 0;
6917 rc = 0;
6919 break;
6921 case TPACPI_FAN_WR_ACPI_SFAN:
6922 rc = fan_get_status(&s);
6923 if (rc < 0)
6924 break;
6926 s &= 0x07;
6928 /* Set fan to at least level 4 */
6929 s |= 4;
6931 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6932 rc = -EIO;
6933 else
6934 rc = 0;
6935 break;
6937 default:
6938 rc = -ENXIO;
6941 mutex_unlock(&fan_mutex);
6943 if (!rc)
6944 vdbg_printk(TPACPI_DBG_FAN,
6945 "fan control: set fan control register to 0x%02x\n",
6947 return rc;
6950 static int fan_set_disable(void)
6952 int rc;
6954 if (!fan_control_allowed)
6955 return -EPERM;
6957 if (mutex_lock_killable(&fan_mutex))
6958 return -ERESTARTSYS;
6960 rc = 0;
6961 switch (fan_control_access_mode) {
6962 case TPACPI_FAN_WR_ACPI_FANS:
6963 case TPACPI_FAN_WR_TPEC:
6964 if (!acpi_ec_write(fan_status_offset, 0x00))
6965 rc = -EIO;
6966 else {
6967 fan_control_desired_level = 0;
6968 tp_features.fan_ctrl_status_undef = 0;
6970 break;
6972 case TPACPI_FAN_WR_ACPI_SFAN:
6973 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6974 rc = -EIO;
6975 else
6976 fan_control_desired_level = 0;
6977 break;
6979 default:
6980 rc = -ENXIO;
6983 if (!rc)
6984 vdbg_printk(TPACPI_DBG_FAN,
6985 "fan control: set fan control register to 0\n");
6987 mutex_unlock(&fan_mutex);
6988 return rc;
6991 static int fan_set_speed(int speed)
6993 int rc;
6995 if (!fan_control_allowed)
6996 return -EPERM;
6998 if (mutex_lock_killable(&fan_mutex))
6999 return -ERESTARTSYS;
7001 rc = 0;
7002 switch (fan_control_access_mode) {
7003 case TPACPI_FAN_WR_ACPI_FANS:
7004 if (speed >= 0 && speed <= 65535) {
7005 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7006 speed, speed, speed))
7007 rc = -EIO;
7008 } else
7009 rc = -EINVAL;
7010 break;
7012 default:
7013 rc = -ENXIO;
7016 mutex_unlock(&fan_mutex);
7017 return rc;
7020 static void fan_watchdog_reset(void)
7022 static int fan_watchdog_active;
7024 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7025 return;
7027 if (fan_watchdog_active)
7028 cancel_delayed_work(&fan_watchdog_task);
7030 if (fan_watchdog_maxinterval > 0 &&
7031 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7032 fan_watchdog_active = 1;
7033 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7034 msecs_to_jiffies(fan_watchdog_maxinterval
7035 * 1000))) {
7036 printk(TPACPI_ERR
7037 "failed to queue the fan watchdog, "
7038 "watchdog will not trigger\n");
7040 } else
7041 fan_watchdog_active = 0;
7044 static void fan_watchdog_fire(struct work_struct *ignored)
7046 int rc;
7048 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7049 return;
7051 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7052 rc = fan_set_enable();
7053 if (rc < 0) {
7054 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7055 "will try again later...\n", -rc);
7056 /* reschedule for later */
7057 fan_watchdog_reset();
7062 * SYSFS fan layout: hwmon compatible (device)
7064 * pwm*_enable:
7065 * 0: "disengaged" mode
7066 * 1: manual mode
7067 * 2: native EC "auto" mode (recommended, hardware default)
7069 * pwm*: set speed in manual mode, ignored otherwise.
7070 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7071 * interpolation.
7073 * fan*_input: tachometer reading, RPM
7076 * SYSFS fan layout: extensions
7078 * fan_watchdog (driver):
7079 * fan watchdog interval in seconds, 0 disables (default), max 120
7082 /* sysfs fan pwm1_enable ----------------------------------------------- */
7083 static ssize_t fan_pwm1_enable_show(struct device *dev,
7084 struct device_attribute *attr,
7085 char *buf)
7087 int res, mode;
7088 u8 status;
7090 res = fan_get_status_safe(&status);
7091 if (res)
7092 return res;
7094 if (status & TP_EC_FAN_FULLSPEED) {
7095 mode = 0;
7096 } else if (status & TP_EC_FAN_AUTO) {
7097 mode = 2;
7098 } else
7099 mode = 1;
7101 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7104 static ssize_t fan_pwm1_enable_store(struct device *dev,
7105 struct device_attribute *attr,
7106 const char *buf, size_t count)
7108 unsigned long t;
7109 int res, level;
7111 if (parse_strtoul(buf, 2, &t))
7112 return -EINVAL;
7114 tpacpi_disclose_usertask("hwmon pwm1_enable",
7115 "set fan mode to %lu\n", t);
7117 switch (t) {
7118 case 0:
7119 level = TP_EC_FAN_FULLSPEED;
7120 break;
7121 case 1:
7122 level = TPACPI_FAN_LAST_LEVEL;
7123 break;
7124 case 2:
7125 level = TP_EC_FAN_AUTO;
7126 break;
7127 case 3:
7128 /* reserved for software-controlled auto mode */
7129 return -ENOSYS;
7130 default:
7131 return -EINVAL;
7134 res = fan_set_level_safe(level);
7135 if (res == -ENXIO)
7136 return -EINVAL;
7137 else if (res < 0)
7138 return res;
7140 fan_watchdog_reset();
7142 return count;
7145 static struct device_attribute dev_attr_fan_pwm1_enable =
7146 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7147 fan_pwm1_enable_show, fan_pwm1_enable_store);
7149 /* sysfs fan pwm1 ------------------------------------------------------ */
7150 static ssize_t fan_pwm1_show(struct device *dev,
7151 struct device_attribute *attr,
7152 char *buf)
7154 int res;
7155 u8 status;
7157 res = fan_get_status_safe(&status);
7158 if (res)
7159 return res;
7161 if ((status &
7162 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7163 status = fan_control_desired_level;
7165 if (status > 7)
7166 status = 7;
7168 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7171 static ssize_t fan_pwm1_store(struct device *dev,
7172 struct device_attribute *attr,
7173 const char *buf, size_t count)
7175 unsigned long s;
7176 int rc;
7177 u8 status, newlevel;
7179 if (parse_strtoul(buf, 255, &s))
7180 return -EINVAL;
7182 tpacpi_disclose_usertask("hwmon pwm1",
7183 "set fan speed to %lu\n", s);
7185 /* scale down from 0-255 to 0-7 */
7186 newlevel = (s >> 5) & 0x07;
7188 if (mutex_lock_killable(&fan_mutex))
7189 return -ERESTARTSYS;
7191 rc = fan_get_status(&status);
7192 if (!rc && (status &
7193 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7194 rc = fan_set_level(newlevel);
7195 if (rc == -ENXIO)
7196 rc = -EINVAL;
7197 else if (!rc) {
7198 fan_update_desired_level(newlevel);
7199 fan_watchdog_reset();
7203 mutex_unlock(&fan_mutex);
7204 return (rc)? rc : count;
7207 static struct device_attribute dev_attr_fan_pwm1 =
7208 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7209 fan_pwm1_show, fan_pwm1_store);
7211 /* sysfs fan fan1_input ------------------------------------------------ */
7212 static ssize_t fan_fan1_input_show(struct device *dev,
7213 struct device_attribute *attr,
7214 char *buf)
7216 int res;
7217 unsigned int speed;
7219 res = fan_get_speed(&speed);
7220 if (res < 0)
7221 return res;
7223 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7226 static struct device_attribute dev_attr_fan_fan1_input =
7227 __ATTR(fan1_input, S_IRUGO,
7228 fan_fan1_input_show, NULL);
7230 /* sysfs fan fan2_input ------------------------------------------------ */
7231 static ssize_t fan_fan2_input_show(struct device *dev,
7232 struct device_attribute *attr,
7233 char *buf)
7235 int res;
7236 unsigned int speed;
7238 res = fan2_get_speed(&speed);
7239 if (res < 0)
7240 return res;
7242 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7245 static struct device_attribute dev_attr_fan_fan2_input =
7246 __ATTR(fan2_input, S_IRUGO,
7247 fan_fan2_input_show, NULL);
7249 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7250 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7251 char *buf)
7253 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7256 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7257 const char *buf, size_t count)
7259 unsigned long t;
7261 if (parse_strtoul(buf, 120, &t))
7262 return -EINVAL;
7264 if (!fan_control_allowed)
7265 return -EPERM;
7267 fan_watchdog_maxinterval = t;
7268 fan_watchdog_reset();
7270 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7272 return count;
7275 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7276 fan_fan_watchdog_show, fan_fan_watchdog_store);
7278 /* --------------------------------------------------------------------- */
7279 static struct attribute *fan_attributes[] = {
7280 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7281 &dev_attr_fan_fan1_input.attr,
7282 NULL, /* for fan2_input */
7283 NULL
7286 static const struct attribute_group fan_attr_group = {
7287 .attrs = fan_attributes,
7290 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7291 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7293 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7294 { .vendor = PCI_VENDOR_ID_IBM, \
7295 .bios = TPACPI_MATCH_ANY, \
7296 .ec = TPID(__id1, __id2), \
7297 .quirks = __quirks }
7299 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7300 { .vendor = PCI_VENDOR_ID_LENOVO, \
7301 .bios = TPACPI_MATCH_ANY, \
7302 .ec = TPID(__id1, __id2), \
7303 .quirks = __quirks }
7305 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7306 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7307 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7308 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7309 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7310 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7313 #undef TPACPI_FAN_QL
7314 #undef TPACPI_FAN_QI
7316 static int __init fan_init(struct ibm_init_struct *iibm)
7318 int rc;
7319 unsigned long quirks;
7321 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7322 "initializing fan subdriver\n");
7324 mutex_init(&fan_mutex);
7325 fan_status_access_mode = TPACPI_FAN_NONE;
7326 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7327 fan_control_commands = 0;
7328 fan_watchdog_maxinterval = 0;
7329 tp_features.fan_ctrl_status_undef = 0;
7330 tp_features.second_fan = 0;
7331 fan_control_desired_level = 7;
7333 TPACPI_ACPIHANDLE_INIT(fans);
7334 TPACPI_ACPIHANDLE_INIT(gfan);
7335 TPACPI_ACPIHANDLE_INIT(sfan);
7337 quirks = tpacpi_check_quirks(fan_quirk_table,
7338 ARRAY_SIZE(fan_quirk_table));
7340 if (gfan_handle) {
7341 /* 570, 600e/x, 770e, 770x */
7342 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7343 } else {
7344 /* all other ThinkPads: note that even old-style
7345 * ThinkPad ECs supports the fan control register */
7346 if (likely(acpi_ec_read(fan_status_offset,
7347 &fan_control_initial_status))) {
7348 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7349 if (quirks & TPACPI_FAN_Q1)
7350 fan_quirk1_setup();
7351 if (quirks & TPACPI_FAN_2FAN) {
7352 tp_features.second_fan = 1;
7353 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7354 "secondary fan support enabled\n");
7356 } else {
7357 printk(TPACPI_ERR
7358 "ThinkPad ACPI EC access misbehaving, "
7359 "fan status and control unavailable\n");
7360 return 1;
7364 if (sfan_handle) {
7365 /* 570, 770x-JL */
7366 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7367 fan_control_commands |=
7368 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7369 } else {
7370 if (!gfan_handle) {
7371 /* gfan without sfan means no fan control */
7372 /* all other models implement TP EC 0x2f control */
7374 if (fans_handle) {
7375 /* X31, X40, X41 */
7376 fan_control_access_mode =
7377 TPACPI_FAN_WR_ACPI_FANS;
7378 fan_control_commands |=
7379 TPACPI_FAN_CMD_SPEED |
7380 TPACPI_FAN_CMD_LEVEL |
7381 TPACPI_FAN_CMD_ENABLE;
7382 } else {
7383 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7384 fan_control_commands |=
7385 TPACPI_FAN_CMD_LEVEL |
7386 TPACPI_FAN_CMD_ENABLE;
7391 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7392 "fan is %s, modes %d, %d\n",
7393 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7394 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7395 fan_status_access_mode, fan_control_access_mode);
7397 /* fan control master switch */
7398 if (!fan_control_allowed) {
7399 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7400 fan_control_commands = 0;
7401 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7402 "fan control features disabled by parameter\n");
7405 /* update fan_control_desired_level */
7406 if (fan_status_access_mode != TPACPI_FAN_NONE)
7407 fan_get_status_safe(NULL);
7409 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7410 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7411 if (tp_features.second_fan) {
7412 /* attach second fan tachometer */
7413 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7414 &dev_attr_fan_fan2_input.attr;
7416 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7417 &fan_attr_group);
7418 if (rc < 0)
7419 return rc;
7421 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7422 &driver_attr_fan_watchdog);
7423 if (rc < 0) {
7424 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7425 &fan_attr_group);
7426 return rc;
7428 return 0;
7429 } else
7430 return 1;
7433 static void fan_exit(void)
7435 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7436 "cancelling any pending fan watchdog tasks\n");
7438 /* FIXME: can we really do this unconditionally? */
7439 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7440 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7441 &driver_attr_fan_watchdog);
7443 cancel_delayed_work(&fan_watchdog_task);
7444 flush_workqueue(tpacpi_wq);
7447 static void fan_suspend(pm_message_t state)
7449 int rc;
7451 if (!fan_control_allowed)
7452 return;
7454 /* Store fan status in cache */
7455 fan_control_resume_level = 0;
7456 rc = fan_get_status_safe(&fan_control_resume_level);
7457 if (rc < 0)
7458 printk(TPACPI_NOTICE
7459 "failed to read fan level for later "
7460 "restore during resume: %d\n", rc);
7462 /* if it is undefined, don't attempt to restore it.
7463 * KEEP THIS LAST */
7464 if (tp_features.fan_ctrl_status_undef)
7465 fan_control_resume_level = 0;
7468 static void fan_resume(void)
7470 u8 current_level = 7;
7471 bool do_set = false;
7472 int rc;
7474 /* DSDT *always* updates status on resume */
7475 tp_features.fan_ctrl_status_undef = 0;
7477 if (!fan_control_allowed ||
7478 !fan_control_resume_level ||
7479 (fan_get_status_safe(&current_level) < 0))
7480 return;
7482 switch (fan_control_access_mode) {
7483 case TPACPI_FAN_WR_ACPI_SFAN:
7484 /* never decrease fan level */
7485 do_set = (fan_control_resume_level > current_level);
7486 break;
7487 case TPACPI_FAN_WR_ACPI_FANS:
7488 case TPACPI_FAN_WR_TPEC:
7489 /* never decrease fan level, scale is:
7490 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7492 * We expect the firmware to set either 7 or AUTO, but we
7493 * handle FULLSPEED out of paranoia.
7495 * So, we can safely only restore FULLSPEED or 7, anything
7496 * else could slow the fan. Restoring AUTO is useless, at
7497 * best that's exactly what the DSDT already set (it is the
7498 * slower it uses).
7500 * Always keep in mind that the DSDT *will* have set the
7501 * fans to what the vendor supposes is the best level. We
7502 * muck with it only to speed the fan up.
7504 if (fan_control_resume_level != 7 &&
7505 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7506 return;
7507 else
7508 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7509 (current_level != fan_control_resume_level);
7510 break;
7511 default:
7512 return;
7514 if (do_set) {
7515 printk(TPACPI_NOTICE
7516 "restoring fan level to 0x%02x\n",
7517 fan_control_resume_level);
7518 rc = fan_set_level_safe(fan_control_resume_level);
7519 if (rc < 0)
7520 printk(TPACPI_NOTICE
7521 "failed to restore fan level: %d\n", rc);
7525 static int fan_read(char *p)
7527 int len = 0;
7528 int rc;
7529 u8 status;
7530 unsigned int speed = 0;
7532 switch (fan_status_access_mode) {
7533 case TPACPI_FAN_RD_ACPI_GFAN:
7534 /* 570, 600e/x, 770e, 770x */
7535 rc = fan_get_status_safe(&status);
7536 if (rc < 0)
7537 return rc;
7539 len += sprintf(p + len, "status:\t\t%s\n"
7540 "level:\t\t%d\n",
7541 (status != 0) ? "enabled" : "disabled", status);
7542 break;
7544 case TPACPI_FAN_RD_TPEC:
7545 /* all except 570, 600e/x, 770e, 770x */
7546 rc = fan_get_status_safe(&status);
7547 if (rc < 0)
7548 return rc;
7550 len += sprintf(p + len, "status:\t\t%s\n",
7551 (status != 0) ? "enabled" : "disabled");
7553 rc = fan_get_speed(&speed);
7554 if (rc < 0)
7555 return rc;
7557 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7559 if (status & TP_EC_FAN_FULLSPEED)
7560 /* Disengaged mode takes precedence */
7561 len += sprintf(p + len, "level:\t\tdisengaged\n");
7562 else if (status & TP_EC_FAN_AUTO)
7563 len += sprintf(p + len, "level:\t\tauto\n");
7564 else
7565 len += sprintf(p + len, "level:\t\t%d\n", status);
7566 break;
7568 case TPACPI_FAN_NONE:
7569 default:
7570 len += sprintf(p + len, "status:\t\tnot supported\n");
7573 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7574 len += sprintf(p + len, "commands:\tlevel <level>");
7576 switch (fan_control_access_mode) {
7577 case TPACPI_FAN_WR_ACPI_SFAN:
7578 len += sprintf(p + len, " (<level> is 0-7)\n");
7579 break;
7581 default:
7582 len += sprintf(p + len, " (<level> is 0-7, "
7583 "auto, disengaged, full-speed)\n");
7584 break;
7588 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7589 len += sprintf(p + len, "commands:\tenable, disable\n"
7590 "commands:\twatchdog <timeout> (<timeout> "
7591 "is 0 (off), 1-120 (seconds))\n");
7593 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7594 len += sprintf(p + len, "commands:\tspeed <speed>"
7595 " (<speed> is 0-65535)\n");
7597 return len;
7600 static int fan_write_cmd_level(const char *cmd, int *rc)
7602 int level;
7604 if (strlencmp(cmd, "level auto") == 0)
7605 level = TP_EC_FAN_AUTO;
7606 else if ((strlencmp(cmd, "level disengaged") == 0) |
7607 (strlencmp(cmd, "level full-speed") == 0))
7608 level = TP_EC_FAN_FULLSPEED;
7609 else if (sscanf(cmd, "level %d", &level) != 1)
7610 return 0;
7612 *rc = fan_set_level_safe(level);
7613 if (*rc == -ENXIO)
7614 printk(TPACPI_ERR "level command accepted for unsupported "
7615 "access mode %d", fan_control_access_mode);
7616 else if (!*rc)
7617 tpacpi_disclose_usertask("procfs fan",
7618 "set level to %d\n", level);
7620 return 1;
7623 static int fan_write_cmd_enable(const char *cmd, int *rc)
7625 if (strlencmp(cmd, "enable") != 0)
7626 return 0;
7628 *rc = fan_set_enable();
7629 if (*rc == -ENXIO)
7630 printk(TPACPI_ERR "enable command accepted for unsupported "
7631 "access mode %d", fan_control_access_mode);
7632 else if (!*rc)
7633 tpacpi_disclose_usertask("procfs fan", "enable\n");
7635 return 1;
7638 static int fan_write_cmd_disable(const char *cmd, int *rc)
7640 if (strlencmp(cmd, "disable") != 0)
7641 return 0;
7643 *rc = fan_set_disable();
7644 if (*rc == -ENXIO)
7645 printk(TPACPI_ERR "disable command accepted for unsupported "
7646 "access mode %d", fan_control_access_mode);
7647 else if (!*rc)
7648 tpacpi_disclose_usertask("procfs fan", "disable\n");
7650 return 1;
7653 static int fan_write_cmd_speed(const char *cmd, int *rc)
7655 int speed;
7657 /* TODO:
7658 * Support speed <low> <medium> <high> ? */
7660 if (sscanf(cmd, "speed %d", &speed) != 1)
7661 return 0;
7663 *rc = fan_set_speed(speed);
7664 if (*rc == -ENXIO)
7665 printk(TPACPI_ERR "speed command accepted for unsupported "
7666 "access mode %d", fan_control_access_mode);
7667 else if (!*rc)
7668 tpacpi_disclose_usertask("procfs fan",
7669 "set speed to %d\n", speed);
7671 return 1;
7674 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7676 int interval;
7678 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7679 return 0;
7681 if (interval < 0 || interval > 120)
7682 *rc = -EINVAL;
7683 else {
7684 fan_watchdog_maxinterval = interval;
7685 tpacpi_disclose_usertask("procfs fan",
7686 "set watchdog timer to %d\n",
7687 interval);
7690 return 1;
7693 static int fan_write(char *buf)
7695 char *cmd;
7696 int rc = 0;
7698 while (!rc && (cmd = next_cmd(&buf))) {
7699 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7700 fan_write_cmd_level(cmd, &rc)) &&
7701 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7702 (fan_write_cmd_enable(cmd, &rc) ||
7703 fan_write_cmd_disable(cmd, &rc) ||
7704 fan_write_cmd_watchdog(cmd, &rc))) &&
7705 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7706 fan_write_cmd_speed(cmd, &rc))
7708 rc = -EINVAL;
7709 else if (!rc)
7710 fan_watchdog_reset();
7713 return rc;
7716 static struct ibm_struct fan_driver_data = {
7717 .name = "fan",
7718 .read = fan_read,
7719 .write = fan_write,
7720 .exit = fan_exit,
7721 .suspend = fan_suspend,
7722 .resume = fan_resume,
7725 /****************************************************************************
7726 ****************************************************************************
7728 * Infrastructure
7730 ****************************************************************************
7731 ****************************************************************************/
7734 * HKEY event callout for other subdrivers go here
7735 * (yes, it is ugly, but it is quick, safe, and gets the job done
7737 static void tpacpi_driver_event(const unsigned int hkey_event)
7739 if (ibm_backlight_device) {
7740 switch (hkey_event) {
7741 case TP_HKEY_EV_BRGHT_UP:
7742 case TP_HKEY_EV_BRGHT_DOWN:
7743 tpacpi_brightness_notify_change();
7750 static void hotkey_driver_event(const unsigned int scancode)
7752 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7755 /* sysfs name ---------------------------------------------------------- */
7756 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7757 struct device_attribute *attr,
7758 char *buf)
7760 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7763 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7764 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7766 /* --------------------------------------------------------------------- */
7768 /* /proc support */
7769 static struct proc_dir_entry *proc_dir;
7772 * Module and infrastructure proble, init and exit handling
7775 static int force_load;
7777 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7778 static const char * __init str_supported(int is_supported)
7780 static char text_unsupported[] __initdata = "not supported";
7782 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7784 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7786 static void ibm_exit(struct ibm_struct *ibm)
7788 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7790 list_del_init(&ibm->all_drivers);
7792 if (ibm->flags.acpi_notify_installed) {
7793 dbg_printk(TPACPI_DBG_EXIT,
7794 "%s: acpi_remove_notify_handler\n", ibm->name);
7795 BUG_ON(!ibm->acpi);
7796 acpi_remove_notify_handler(*ibm->acpi->handle,
7797 ibm->acpi->type,
7798 dispatch_acpi_notify);
7799 ibm->flags.acpi_notify_installed = 0;
7800 ibm->flags.acpi_notify_installed = 0;
7803 if (ibm->flags.proc_created) {
7804 dbg_printk(TPACPI_DBG_EXIT,
7805 "%s: remove_proc_entry\n", ibm->name);
7806 remove_proc_entry(ibm->name, proc_dir);
7807 ibm->flags.proc_created = 0;
7810 if (ibm->flags.acpi_driver_registered) {
7811 dbg_printk(TPACPI_DBG_EXIT,
7812 "%s: acpi_bus_unregister_driver\n", ibm->name);
7813 BUG_ON(!ibm->acpi);
7814 acpi_bus_unregister_driver(ibm->acpi->driver);
7815 kfree(ibm->acpi->driver);
7816 ibm->acpi->driver = NULL;
7817 ibm->flags.acpi_driver_registered = 0;
7820 if (ibm->flags.init_called && ibm->exit) {
7821 ibm->exit();
7822 ibm->flags.init_called = 0;
7825 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7828 static int __init ibm_init(struct ibm_init_struct *iibm)
7830 int ret;
7831 struct ibm_struct *ibm = iibm->data;
7832 struct proc_dir_entry *entry;
7834 BUG_ON(ibm == NULL);
7836 INIT_LIST_HEAD(&ibm->all_drivers);
7838 if (ibm->flags.experimental && !experimental)
7839 return 0;
7841 dbg_printk(TPACPI_DBG_INIT,
7842 "probing for %s\n", ibm->name);
7844 if (iibm->init) {
7845 ret = iibm->init(iibm);
7846 if (ret > 0)
7847 return 0; /* probe failed */
7848 if (ret)
7849 return ret;
7851 ibm->flags.init_called = 1;
7854 if (ibm->acpi) {
7855 if (ibm->acpi->hid) {
7856 ret = register_tpacpi_subdriver(ibm);
7857 if (ret)
7858 goto err_out;
7861 if (ibm->acpi->notify) {
7862 ret = setup_acpi_notify(ibm);
7863 if (ret == -ENODEV) {
7864 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7865 ibm->name);
7866 ret = 0;
7867 goto err_out;
7869 if (ret < 0)
7870 goto err_out;
7874 dbg_printk(TPACPI_DBG_INIT,
7875 "%s installed\n", ibm->name);
7877 if (ibm->read) {
7878 entry = create_proc_entry(ibm->name,
7879 S_IFREG | S_IRUGO | S_IWUSR,
7880 proc_dir);
7881 if (!entry) {
7882 printk(TPACPI_ERR "unable to create proc entry %s\n",
7883 ibm->name);
7884 ret = -ENODEV;
7885 goto err_out;
7887 entry->data = ibm;
7888 entry->read_proc = &dispatch_procfs_read;
7889 if (ibm->write)
7890 entry->write_proc = &dispatch_procfs_write;
7891 ibm->flags.proc_created = 1;
7894 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7896 return 0;
7898 err_out:
7899 dbg_printk(TPACPI_DBG_INIT,
7900 "%s: at error exit path with result %d\n",
7901 ibm->name, ret);
7903 ibm_exit(ibm);
7904 return (ret < 0)? ret : 0;
7907 /* Probing */
7909 static bool __pure __init tpacpi_is_fw_digit(const char c)
7911 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7914 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7915 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7916 const char t)
7918 return s && strlen(s) >= 8 &&
7919 tpacpi_is_fw_digit(s[0]) &&
7920 tpacpi_is_fw_digit(s[1]) &&
7921 s[2] == t && s[3] == 'T' &&
7922 tpacpi_is_fw_digit(s[4]) &&
7923 tpacpi_is_fw_digit(s[5]) &&
7924 s[6] == 'W' && s[7] == 'W';
7927 /* returns 0 - probe ok, or < 0 - probe error.
7928 * Probe ok doesn't mean thinkpad found.
7929 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7930 static int __must_check __init get_thinkpad_model_data(
7931 struct thinkpad_id_data *tp)
7933 const struct dmi_device *dev = NULL;
7934 char ec_fw_string[18];
7935 char const *s;
7937 if (!tp)
7938 return -EINVAL;
7940 memset(tp, 0, sizeof(*tp));
7942 if (dmi_name_in_vendors("IBM"))
7943 tp->vendor = PCI_VENDOR_ID_IBM;
7944 else if (dmi_name_in_vendors("LENOVO"))
7945 tp->vendor = PCI_VENDOR_ID_LENOVO;
7946 else
7947 return 0;
7949 s = dmi_get_system_info(DMI_BIOS_VERSION);
7950 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7951 if (s && !tp->bios_version_str)
7952 return -ENOMEM;
7954 /* Really ancient ThinkPad 240X will fail this, which is fine */
7955 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7956 return 0;
7958 tp->bios_model = tp->bios_version_str[0]
7959 | (tp->bios_version_str[1] << 8);
7960 tp->bios_release = (tp->bios_version_str[4] << 8)
7961 | tp->bios_version_str[5];
7964 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7965 * X32 or newer, all Z series; Some models must have an
7966 * up-to-date BIOS or they will not be detected.
7968 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7970 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7971 if (sscanf(dev->name,
7972 "IBM ThinkPad Embedded Controller -[%17c",
7973 ec_fw_string) == 1) {
7974 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7975 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7977 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7978 if (!tp->ec_version_str)
7979 return -ENOMEM;
7981 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7982 tp->ec_model = ec_fw_string[0]
7983 | (ec_fw_string[1] << 8);
7984 tp->ec_release = (ec_fw_string[4] << 8)
7985 | ec_fw_string[5];
7986 } else {
7987 printk(TPACPI_NOTICE
7988 "ThinkPad firmware release %s "
7989 "doesn't match the known patterns\n",
7990 ec_fw_string);
7991 printk(TPACPI_NOTICE
7992 "please report this to %s\n",
7993 TPACPI_MAIL);
7995 break;
7999 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8000 if (s && !strnicmp(s, "ThinkPad", 8)) {
8001 tp->model_str = kstrdup(s, GFP_KERNEL);
8002 if (!tp->model_str)
8003 return -ENOMEM;
8006 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8007 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8008 if (s && !tp->nummodel_str)
8009 return -ENOMEM;
8011 return 0;
8014 static int __init probe_for_thinkpad(void)
8016 int is_thinkpad;
8018 if (acpi_disabled)
8019 return -ENODEV;
8022 * Non-ancient models have better DMI tagging, but very old models
8023 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8025 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8026 (thinkpad_id.ec_model != 0) ||
8027 tpacpi_is_fw_known();
8029 /* ec is required because many other handles are relative to it */
8030 TPACPI_ACPIHANDLE_INIT(ec);
8031 if (!ec_handle) {
8032 if (is_thinkpad)
8033 printk(TPACPI_ERR
8034 "Not yet supported ThinkPad detected!\n");
8035 return -ENODEV;
8038 if (!is_thinkpad && !force_load)
8039 return -ENODEV;
8041 return 0;
8045 /* Module init, exit, parameters */
8047 static struct ibm_init_struct ibms_init[] __initdata = {
8049 .init = thinkpad_acpi_driver_init,
8050 .data = &thinkpad_acpi_driver_data,
8053 .init = hotkey_init,
8054 .data = &hotkey_driver_data,
8057 .init = bluetooth_init,
8058 .data = &bluetooth_driver_data,
8061 .init = wan_init,
8062 .data = &wan_driver_data,
8065 .init = uwb_init,
8066 .data = &uwb_driver_data,
8068 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8070 .init = video_init,
8071 .data = &video_driver_data,
8073 #endif
8075 .init = light_init,
8076 .data = &light_driver_data,
8079 .init = cmos_init,
8080 .data = &cmos_driver_data,
8083 .init = led_init,
8084 .data = &led_driver_data,
8087 .init = beep_init,
8088 .data = &beep_driver_data,
8091 .init = thermal_init,
8092 .data = &thermal_driver_data,
8095 .data = &ecdump_driver_data,
8098 .init = brightness_init,
8099 .data = &brightness_driver_data,
8102 .data = &volume_driver_data,
8105 .init = fan_init,
8106 .data = &fan_driver_data,
8110 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8112 unsigned int i;
8113 struct ibm_struct *ibm;
8115 if (!kp || !kp->name || !val)
8116 return -EINVAL;
8118 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8119 ibm = ibms_init[i].data;
8120 WARN_ON(ibm == NULL);
8122 if (!ibm || !ibm->name)
8123 continue;
8125 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8126 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8127 return -ENOSPC;
8128 strcpy(ibms_init[i].param, val);
8129 strcat(ibms_init[i].param, ",");
8130 return 0;
8134 return -EINVAL;
8137 module_param(experimental, int, 0);
8138 MODULE_PARM_DESC(experimental,
8139 "Enables experimental features when non-zero");
8141 module_param_named(debug, dbg_level, uint, 0);
8142 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8144 module_param(force_load, bool, 0);
8145 MODULE_PARM_DESC(force_load,
8146 "Attempts to load the driver even on a "
8147 "mis-identified ThinkPad when true");
8149 module_param_named(fan_control, fan_control_allowed, bool, 0);
8150 MODULE_PARM_DESC(fan_control,
8151 "Enables setting fan parameters features when true");
8153 module_param_named(brightness_mode, brightness_mode, uint, 0);
8154 MODULE_PARM_DESC(brightness_mode,
8155 "Selects brightness control strategy: "
8156 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8158 module_param(brightness_enable, uint, 0);
8159 MODULE_PARM_DESC(brightness_enable,
8160 "Enables backlight control when 1, disables when 0");
8162 module_param(hotkey_report_mode, uint, 0);
8163 MODULE_PARM_DESC(hotkey_report_mode,
8164 "used for backwards compatibility with userspace, "
8165 "see documentation");
8167 #define TPACPI_PARAM(feature) \
8168 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8169 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8170 "at module load, see documentation")
8172 TPACPI_PARAM(hotkey);
8173 TPACPI_PARAM(bluetooth);
8174 TPACPI_PARAM(video);
8175 TPACPI_PARAM(light);
8176 TPACPI_PARAM(cmos);
8177 TPACPI_PARAM(led);
8178 TPACPI_PARAM(beep);
8179 TPACPI_PARAM(ecdump);
8180 TPACPI_PARAM(brightness);
8181 TPACPI_PARAM(volume);
8182 TPACPI_PARAM(fan);
8184 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8185 module_param(dbg_wlswemul, uint, 0);
8186 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8187 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8188 MODULE_PARM_DESC(wlsw_state,
8189 "Initial state of the emulated WLSW switch");
8191 module_param(dbg_bluetoothemul, uint, 0);
8192 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8193 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8194 MODULE_PARM_DESC(bluetooth_state,
8195 "Initial state of the emulated bluetooth switch");
8197 module_param(dbg_wwanemul, uint, 0);
8198 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8199 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8200 MODULE_PARM_DESC(wwan_state,
8201 "Initial state of the emulated WWAN switch");
8203 module_param(dbg_uwbemul, uint, 0);
8204 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8205 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8206 MODULE_PARM_DESC(uwb_state,
8207 "Initial state of the emulated UWB switch");
8208 #endif
8210 static void thinkpad_acpi_module_exit(void)
8212 struct ibm_struct *ibm, *itmp;
8214 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8216 list_for_each_entry_safe_reverse(ibm, itmp,
8217 &tpacpi_all_drivers,
8218 all_drivers) {
8219 ibm_exit(ibm);
8222 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8224 if (tpacpi_inputdev) {
8225 if (tp_features.input_device_registered)
8226 input_unregister_device(tpacpi_inputdev);
8227 else
8228 input_free_device(tpacpi_inputdev);
8231 if (tpacpi_hwmon)
8232 hwmon_device_unregister(tpacpi_hwmon);
8234 if (tp_features.sensors_pdev_attrs_registered)
8235 device_remove_file(&tpacpi_sensors_pdev->dev,
8236 &dev_attr_thinkpad_acpi_pdev_name);
8237 if (tpacpi_sensors_pdev)
8238 platform_device_unregister(tpacpi_sensors_pdev);
8239 if (tpacpi_pdev)
8240 platform_device_unregister(tpacpi_pdev);
8242 if (tp_features.sensors_pdrv_attrs_registered)
8243 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8244 if (tp_features.platform_drv_attrs_registered)
8245 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8247 if (tp_features.sensors_pdrv_registered)
8248 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8250 if (tp_features.platform_drv_registered)
8251 platform_driver_unregister(&tpacpi_pdriver);
8253 if (proc_dir)
8254 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8256 if (tpacpi_wq)
8257 destroy_workqueue(tpacpi_wq);
8259 kfree(thinkpad_id.bios_version_str);
8260 kfree(thinkpad_id.ec_version_str);
8261 kfree(thinkpad_id.model_str);
8265 static int __init thinkpad_acpi_module_init(void)
8267 int ret, i;
8269 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8271 /* Parameter checking */
8272 if (hotkey_report_mode > 2)
8273 return -EINVAL;
8275 /* Driver-level probe */
8277 ret = get_thinkpad_model_data(&thinkpad_id);
8278 if (ret) {
8279 printk(TPACPI_ERR
8280 "unable to get DMI data: %d\n", ret);
8281 thinkpad_acpi_module_exit();
8282 return ret;
8284 ret = probe_for_thinkpad();
8285 if (ret) {
8286 thinkpad_acpi_module_exit();
8287 return ret;
8290 /* Driver initialization */
8292 TPACPI_ACPIHANDLE_INIT(ecrd);
8293 TPACPI_ACPIHANDLE_INIT(ecwr);
8295 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8296 if (!tpacpi_wq) {
8297 thinkpad_acpi_module_exit();
8298 return -ENOMEM;
8301 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8302 if (!proc_dir) {
8303 printk(TPACPI_ERR
8304 "unable to create proc dir " TPACPI_PROC_DIR);
8305 thinkpad_acpi_module_exit();
8306 return -ENODEV;
8309 ret = platform_driver_register(&tpacpi_pdriver);
8310 if (ret) {
8311 printk(TPACPI_ERR
8312 "unable to register main platform driver\n");
8313 thinkpad_acpi_module_exit();
8314 return ret;
8316 tp_features.platform_drv_registered = 1;
8318 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8319 if (ret) {
8320 printk(TPACPI_ERR
8321 "unable to register hwmon platform driver\n");
8322 thinkpad_acpi_module_exit();
8323 return ret;
8325 tp_features.sensors_pdrv_registered = 1;
8327 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8328 if (!ret) {
8329 tp_features.platform_drv_attrs_registered = 1;
8330 ret = tpacpi_create_driver_attributes(
8331 &tpacpi_hwmon_pdriver.driver);
8333 if (ret) {
8334 printk(TPACPI_ERR
8335 "unable to create sysfs driver attributes\n");
8336 thinkpad_acpi_module_exit();
8337 return ret;
8339 tp_features.sensors_pdrv_attrs_registered = 1;
8342 /* Device initialization */
8343 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8344 NULL, 0);
8345 if (IS_ERR(tpacpi_pdev)) {
8346 ret = PTR_ERR(tpacpi_pdev);
8347 tpacpi_pdev = NULL;
8348 printk(TPACPI_ERR "unable to register platform device\n");
8349 thinkpad_acpi_module_exit();
8350 return ret;
8352 tpacpi_sensors_pdev = platform_device_register_simple(
8353 TPACPI_HWMON_DRVR_NAME,
8354 -1, NULL, 0);
8355 if (IS_ERR(tpacpi_sensors_pdev)) {
8356 ret = PTR_ERR(tpacpi_sensors_pdev);
8357 tpacpi_sensors_pdev = NULL;
8358 printk(TPACPI_ERR
8359 "unable to register hwmon platform device\n");
8360 thinkpad_acpi_module_exit();
8361 return ret;
8363 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8364 &dev_attr_thinkpad_acpi_pdev_name);
8365 if (ret) {
8366 printk(TPACPI_ERR
8367 "unable to create sysfs hwmon device attributes\n");
8368 thinkpad_acpi_module_exit();
8369 return ret;
8371 tp_features.sensors_pdev_attrs_registered = 1;
8372 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8373 if (IS_ERR(tpacpi_hwmon)) {
8374 ret = PTR_ERR(tpacpi_hwmon);
8375 tpacpi_hwmon = NULL;
8376 printk(TPACPI_ERR "unable to register hwmon device\n");
8377 thinkpad_acpi_module_exit();
8378 return ret;
8380 mutex_init(&tpacpi_inputdev_send_mutex);
8381 tpacpi_inputdev = input_allocate_device();
8382 if (!tpacpi_inputdev) {
8383 printk(TPACPI_ERR "unable to allocate input device\n");
8384 thinkpad_acpi_module_exit();
8385 return -ENOMEM;
8386 } else {
8387 /* Prepare input device, but don't register */
8388 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8389 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8390 tpacpi_inputdev->id.bustype = BUS_HOST;
8391 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8392 thinkpad_id.vendor :
8393 PCI_VENDOR_ID_IBM;
8394 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8395 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8397 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8398 ret = ibm_init(&ibms_init[i]);
8399 if (ret >= 0 && *ibms_init[i].param)
8400 ret = ibms_init[i].data->write(ibms_init[i].param);
8401 if (ret < 0) {
8402 thinkpad_acpi_module_exit();
8403 return ret;
8406 ret = input_register_device(tpacpi_inputdev);
8407 if (ret < 0) {
8408 printk(TPACPI_ERR "unable to register input device\n");
8409 thinkpad_acpi_module_exit();
8410 return ret;
8411 } else {
8412 tp_features.input_device_registered = 1;
8415 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8416 return 0;
8419 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8422 * This will autoload the driver in almost every ThinkPad
8423 * in widespread use.
8425 * Only _VERY_ old models, like the 240, 240x and 570 lack
8426 * the HKEY event interface.
8428 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8431 * DMI matching for module autoloading
8433 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8434 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8436 * Only models listed in thinkwiki will be supported, so add yours
8437 * if it is not there yet.
8439 #define IBM_BIOS_MODULE_ALIAS(__type) \
8440 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8442 /* Ancient thinkpad BIOSes have to be identified by
8443 * BIOS type or model number, and there are far less
8444 * BIOS types than model numbers... */
8445 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8447 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8448 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8449 MODULE_DESCRIPTION(TPACPI_DESC);
8450 MODULE_VERSION(TPACPI_VERSION);
8451 MODULE_LICENSE("GPL");
8453 module_init(thinkpad_acpi_module_init);
8454 module_exit(thinkpad_acpi_module_exit);