thinkpad-acpi: log initial state of rfkill switches
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob37d18720aff8c9885644e8e059db55a510708095
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 while (*buf && isspace(*buf))
1010 buf++;
1011 *value = simple_strtoul(buf, &endp, 0);
1012 while (*endp && isspace(*endp))
1013 endp++;
1014 if (*endp || *value > max)
1015 return -EINVAL;
1017 return 0;
1020 static void tpacpi_disable_brightness_delay(void)
1022 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1023 printk(TPACPI_NOTICE
1024 "ACPI backlight control delay disabled\n");
1027 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1029 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1030 union acpi_object *obj;
1031 int rc;
1033 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1034 obj = (union acpi_object *)buffer.pointer;
1035 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1036 printk(TPACPI_ERR "Unknown _BCL data, "
1037 "please report this to %s\n", TPACPI_MAIL);
1038 rc = 0;
1039 } else {
1040 rc = obj->package.count;
1042 } else {
1043 return 0;
1046 kfree(buffer.pointer);
1047 return rc;
1050 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1051 u32 lvl, void *context, void **rv)
1053 char name[ACPI_PATH_SEGMENT_LENGTH];
1054 struct acpi_buffer buffer = { sizeof(name), &name };
1056 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1057 !strncmp("_BCL", name, sizeof(name) - 1)) {
1058 BUG_ON(!rv || !*rv);
1059 **(int **)rv = tpacpi_query_bcl_levels(handle);
1060 return AE_CTRL_TERMINATE;
1061 } else {
1062 return AE_OK;
1067 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1069 static int __init tpacpi_check_std_acpi_brightness_support(void)
1071 int status;
1072 int bcl_levels = 0;
1073 void *bcl_ptr = &bcl_levels;
1075 if (!vid_handle) {
1076 TPACPI_ACPIHANDLE_INIT(vid);
1078 if (!vid_handle)
1079 return 0;
1082 * Search for a _BCL method, and execute it. This is safe on all
1083 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1084 * BIOS in ACPI backlight control mode. We do NOT have to care
1085 * about calling the _BCL method in an enabled video device, any
1086 * will do for our purposes.
1089 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1090 tpacpi_acpi_walk_find_bcl, NULL,
1091 &bcl_ptr);
1093 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1094 tp_features.bright_acpimode = 1;
1095 return (bcl_levels - 2);
1098 return 0;
1101 static void printk_deprecated_attribute(const char * const what,
1102 const char * const details)
1104 tpacpi_log_usertask("deprecated sysfs attribute");
1105 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1106 "will be removed. %s\n",
1107 what, details);
1110 /*************************************************************************
1111 * rfkill and radio control support helpers
1115 * ThinkPad-ACPI firmware handling model:
1117 * WLSW (master wireless switch) is event-driven, and is common to all
1118 * firmware-controlled radios. It cannot be controlled, just monitored,
1119 * as expected. It overrides all radio state in firmware
1121 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1122 * (TODO: verify how WLSW interacts with the returned radio state).
1124 * The only time there are shadow radio state changes, is when
1125 * masked-off hotkeys are used.
1129 * Internal driver API for radio state:
1131 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1132 * bool: true means radio blocked (off)
1134 enum tpacpi_rfkill_state {
1135 TPACPI_RFK_RADIO_OFF = 0,
1136 TPACPI_RFK_RADIO_ON
1139 /* rfkill switches */
1140 enum tpacpi_rfk_id {
1141 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1142 TPACPI_RFK_WWAN_SW_ID,
1143 TPACPI_RFK_UWB_SW_ID,
1144 TPACPI_RFK_SW_MAX
1147 static const char *tpacpi_rfkill_names[] = {
1148 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1149 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1150 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1151 [TPACPI_RFK_SW_MAX] = NULL
1154 /* ThinkPad-ACPI rfkill subdriver */
1155 struct tpacpi_rfk {
1156 struct rfkill *rfkill;
1157 enum tpacpi_rfk_id id;
1158 const struct tpacpi_rfk_ops *ops;
1161 struct tpacpi_rfk_ops {
1162 /* firmware interface */
1163 int (*get_status)(void);
1164 int (*set_status)(const enum tpacpi_rfkill_state);
1167 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1169 /* Query FW and update rfkill sw state for a given rfkill switch */
1170 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1172 int status;
1174 if (!tp_rfk)
1175 return -ENODEV;
1177 status = (tp_rfk->ops->get_status)();
1178 if (status < 0)
1179 return status;
1181 rfkill_set_sw_state(tp_rfk->rfkill,
1182 (status == TPACPI_RFK_RADIO_OFF));
1184 return status;
1187 /* Query FW and update rfkill sw state for all rfkill switches */
1188 static void tpacpi_rfk_update_swstate_all(void)
1190 unsigned int i;
1192 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1193 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1197 * Sync the HW-blocking state of all rfkill switches,
1198 * do notice it causes the rfkill core to schedule uevents
1200 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1202 unsigned int i;
1203 struct tpacpi_rfk *tp_rfk;
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1206 tp_rfk = tpacpi_rfkill_switches[i];
1207 if (tp_rfk) {
1208 if (rfkill_set_hw_state(tp_rfk->rfkill,
1209 blocked)) {
1210 /* ignore -- we track sw block */
1216 /* Call to get the WLSW state from the firmware */
1217 static int hotkey_get_wlsw(void);
1219 /* Call to query WLSW state and update all rfkill switches */
1220 static bool tpacpi_rfk_check_hwblock_state(void)
1222 int res = hotkey_get_wlsw();
1223 int hw_blocked;
1225 /* When unknown or unsupported, we have to assume it is unblocked */
1226 if (res < 0)
1227 return false;
1229 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1230 tpacpi_rfk_update_hwblock_state(hw_blocked);
1232 return hw_blocked;
1235 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1237 struct tpacpi_rfk *tp_rfk = data;
1238 int res;
1240 dbg_printk(TPACPI_DBG_RFKILL,
1241 "request to change radio state to %s\n",
1242 blocked ? "blocked" : "unblocked");
1244 /* try to set radio state */
1245 res = (tp_rfk->ops->set_status)(blocked ?
1246 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1248 /* and update the rfkill core with whatever the FW really did */
1249 tpacpi_rfk_update_swstate(tp_rfk);
1251 return (res < 0) ? res : 0;
1254 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1255 .set_block = tpacpi_rfk_hook_set_block,
1258 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1259 const struct tpacpi_rfk_ops *tp_rfkops,
1260 const enum rfkill_type rfktype,
1261 const char *name,
1262 const bool set_default)
1264 struct tpacpi_rfk *atp_rfk;
1265 int res;
1266 bool sw_state = false;
1267 bool hw_state;
1268 int sw_status;
1270 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1272 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1273 if (atp_rfk)
1274 atp_rfk->rfkill = rfkill_alloc(name,
1275 &tpacpi_pdev->dev,
1276 rfktype,
1277 &tpacpi_rfk_rfkill_ops,
1278 atp_rfk);
1279 if (!atp_rfk || !atp_rfk->rfkill) {
1280 printk(TPACPI_ERR
1281 "failed to allocate memory for rfkill class\n");
1282 kfree(atp_rfk);
1283 return -ENOMEM;
1286 atp_rfk->id = id;
1287 atp_rfk->ops = tp_rfkops;
1289 sw_status = (tp_rfkops->get_status)();
1290 if (sw_status < 0) {
1291 printk(TPACPI_ERR
1292 "failed to read initial state for %s, error %d\n",
1293 name, sw_status);
1294 } else {
1295 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1296 if (set_default) {
1297 /* try to keep the initial state, since we ask the
1298 * firmware to preserve it across S5 in NVRAM */
1299 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1302 hw_state = tpacpi_rfk_check_hwblock_state();
1303 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1305 res = rfkill_register(atp_rfk->rfkill);
1306 if (res < 0) {
1307 printk(TPACPI_ERR
1308 "failed to register %s rfkill switch: %d\n",
1309 name, res);
1310 rfkill_destroy(atp_rfk->rfkill);
1311 kfree(atp_rfk);
1312 return res;
1315 tpacpi_rfkill_switches[id] = atp_rfk;
1317 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1318 name, (sw_state || hw_state) ? "" : "un");
1319 return 0;
1322 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1324 struct tpacpi_rfk *tp_rfk;
1326 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1328 tp_rfk = tpacpi_rfkill_switches[id];
1329 if (tp_rfk) {
1330 rfkill_unregister(tp_rfk->rfkill);
1331 rfkill_destroy(tp_rfk->rfkill);
1332 tpacpi_rfkill_switches[id] = NULL;
1333 kfree(tp_rfk);
1337 static void printk_deprecated_rfkill_attribute(const char * const what)
1339 printk_deprecated_attribute(what,
1340 "Please switch to generic rfkill before year 2010");
1343 /* sysfs <radio> enable ------------------------------------------------ */
1344 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1345 struct device_attribute *attr,
1346 char *buf)
1348 int status;
1350 printk_deprecated_rfkill_attribute(attr->attr.name);
1352 /* This is in the ABI... */
1353 if (tpacpi_rfk_check_hwblock_state()) {
1354 status = TPACPI_RFK_RADIO_OFF;
1355 } else {
1356 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1357 if (status < 0)
1358 return status;
1361 return snprintf(buf, PAGE_SIZE, "%d\n",
1362 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1365 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1366 struct device_attribute *attr,
1367 const char *buf, size_t count)
1369 unsigned long t;
1370 int res;
1372 printk_deprecated_rfkill_attribute(attr->attr.name);
1374 if (parse_strtoul(buf, 1, &t))
1375 return -EINVAL;
1377 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1379 /* This is in the ABI... */
1380 if (tpacpi_rfk_check_hwblock_state() && !!t)
1381 return -EPERM;
1383 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1384 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1385 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1387 return (res < 0) ? res : count;
1390 /* procfs -------------------------------------------------------------- */
1391 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1393 int len = 0;
1395 if (id >= TPACPI_RFK_SW_MAX)
1396 len += sprintf(p + len, "status:\t\tnot supported\n");
1397 else {
1398 int status;
1400 /* This is in the ABI... */
1401 if (tpacpi_rfk_check_hwblock_state()) {
1402 status = TPACPI_RFK_RADIO_OFF;
1403 } else {
1404 status = tpacpi_rfk_update_swstate(
1405 tpacpi_rfkill_switches[id]);
1406 if (status < 0)
1407 return status;
1410 len += sprintf(p + len, "status:\t\t%s\n",
1411 (status == TPACPI_RFK_RADIO_ON) ?
1412 "enabled" : "disabled");
1413 len += sprintf(p + len, "commands:\tenable, disable\n");
1416 return len;
1419 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1421 char *cmd;
1422 int status = -1;
1423 int res = 0;
1425 if (id >= TPACPI_RFK_SW_MAX)
1426 return -ENODEV;
1428 while ((cmd = next_cmd(&buf))) {
1429 if (strlencmp(cmd, "enable") == 0)
1430 status = TPACPI_RFK_RADIO_ON;
1431 else if (strlencmp(cmd, "disable") == 0)
1432 status = TPACPI_RFK_RADIO_OFF;
1433 else
1434 return -EINVAL;
1437 if (status != -1) {
1438 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1439 (status == TPACPI_RFK_RADIO_ON) ?
1440 "enable" : "disable",
1441 tpacpi_rfkill_names[id]);
1442 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1443 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1446 return res;
1449 /*************************************************************************
1450 * thinkpad-acpi driver attributes
1453 /* interface_version --------------------------------------------------- */
1454 static ssize_t tpacpi_driver_interface_version_show(
1455 struct device_driver *drv,
1456 char *buf)
1458 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1461 static DRIVER_ATTR(interface_version, S_IRUGO,
1462 tpacpi_driver_interface_version_show, NULL);
1464 /* debug_level --------------------------------------------------------- */
1465 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1466 char *buf)
1468 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1471 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1472 const char *buf, size_t count)
1474 unsigned long t;
1476 if (parse_strtoul(buf, 0xffff, &t))
1477 return -EINVAL;
1479 dbg_level = t;
1481 return count;
1484 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1485 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1487 /* version ------------------------------------------------------------- */
1488 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1489 char *buf)
1491 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1492 TPACPI_DESC, TPACPI_VERSION);
1495 static DRIVER_ATTR(version, S_IRUGO,
1496 tpacpi_driver_version_show, NULL);
1498 /* --------------------------------------------------------------------- */
1500 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1502 /* wlsw_emulstate ------------------------------------------------------ */
1503 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1504 char *buf)
1506 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1509 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1510 const char *buf, size_t count)
1512 unsigned long t;
1514 if (parse_strtoul(buf, 1, &t))
1515 return -EINVAL;
1517 if (tpacpi_wlsw_emulstate != !!t) {
1518 tpacpi_wlsw_emulstate = !!t;
1519 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1522 return count;
1525 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1526 tpacpi_driver_wlsw_emulstate_show,
1527 tpacpi_driver_wlsw_emulstate_store);
1529 /* bluetooth_emulstate ------------------------------------------------- */
1530 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1531 struct device_driver *drv,
1532 char *buf)
1534 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1537 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1538 struct device_driver *drv,
1539 const char *buf, size_t count)
1541 unsigned long t;
1543 if (parse_strtoul(buf, 1, &t))
1544 return -EINVAL;
1546 tpacpi_bluetooth_emulstate = !!t;
1548 return count;
1551 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1552 tpacpi_driver_bluetooth_emulstate_show,
1553 tpacpi_driver_bluetooth_emulstate_store);
1555 /* wwan_emulstate ------------------------------------------------- */
1556 static ssize_t tpacpi_driver_wwan_emulstate_show(
1557 struct device_driver *drv,
1558 char *buf)
1560 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1563 static ssize_t tpacpi_driver_wwan_emulstate_store(
1564 struct device_driver *drv,
1565 const char *buf, size_t count)
1567 unsigned long t;
1569 if (parse_strtoul(buf, 1, &t))
1570 return -EINVAL;
1572 tpacpi_wwan_emulstate = !!t;
1574 return count;
1577 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1578 tpacpi_driver_wwan_emulstate_show,
1579 tpacpi_driver_wwan_emulstate_store);
1581 /* uwb_emulstate ------------------------------------------------- */
1582 static ssize_t tpacpi_driver_uwb_emulstate_show(
1583 struct device_driver *drv,
1584 char *buf)
1586 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1589 static ssize_t tpacpi_driver_uwb_emulstate_store(
1590 struct device_driver *drv,
1591 const char *buf, size_t count)
1593 unsigned long t;
1595 if (parse_strtoul(buf, 1, &t))
1596 return -EINVAL;
1598 tpacpi_uwb_emulstate = !!t;
1600 return count;
1603 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1604 tpacpi_driver_uwb_emulstate_show,
1605 tpacpi_driver_uwb_emulstate_store);
1606 #endif
1608 /* --------------------------------------------------------------------- */
1610 static struct driver_attribute *tpacpi_driver_attributes[] = {
1611 &driver_attr_debug_level, &driver_attr_version,
1612 &driver_attr_interface_version,
1615 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1617 int i, res;
1619 i = 0;
1620 res = 0;
1621 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1622 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1623 i++;
1626 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1627 if (!res && dbg_wlswemul)
1628 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1629 if (!res && dbg_bluetoothemul)
1630 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1631 if (!res && dbg_wwanemul)
1632 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1633 if (!res && dbg_uwbemul)
1634 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1635 #endif
1637 return res;
1640 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1642 int i;
1644 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1645 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1647 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1648 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1649 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1650 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1651 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1652 #endif
1655 /*************************************************************************
1656 * Firmware Data
1660 * Table of recommended minimum BIOS versions
1662 * Reasons for listing:
1663 * 1. Stable BIOS, listed because the unknown ammount of
1664 * bugs and bad ACPI behaviour on older versions
1666 * 2. BIOS or EC fw with known bugs that trigger on Linux
1668 * 3. BIOS with known reduced functionality in older versions
1670 * We recommend the latest BIOS and EC version.
1671 * We only support the latest BIOS and EC fw version as a rule.
1673 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1674 * Information from users in ThinkWiki
1676 * WARNING: we use this table also to detect that the machine is
1677 * a ThinkPad in some cases, so don't remove entries lightly.
1680 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1681 { .vendor = (__v), \
1682 .bios = TPID(__id1, __id2), \
1683 .ec = TPACPI_MATCH_ANY, \
1684 .quirks = TPACPI_MATCH_ANY << 16 \
1685 | (__bv1) << 8 | (__bv2) }
1687 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1688 __eid, __ev1, __ev2) \
1689 { .vendor = (__v), \
1690 .bios = TPID(__bid1, __bid2), \
1691 .ec = __eid, \
1692 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1693 | (__bv1) << 8 | (__bv2) }
1695 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1696 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1698 /* Outdated IBM BIOSes often lack the EC id string */
1699 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1701 __bv1, __bv2, TPID(__id1, __id2), \
1702 __ev1, __ev2), \
1703 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1704 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1705 __ev1, __ev2)
1707 /* Outdated IBM BIOSes often lack the EC id string */
1708 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1709 __eid1, __eid2, __ev1, __ev2) \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1711 __bv1, __bv2, TPID(__eid1, __eid2), \
1712 __ev1, __ev2), \
1713 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1714 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1715 __ev1, __ev2)
1717 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1718 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1720 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1721 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1722 __bv1, __bv2, TPID(__id1, __id2), \
1723 __ev1, __ev2)
1725 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1726 __eid1, __eid2, __ev1, __ev2) \
1727 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1728 __bv1, __bv2, TPID(__eid1, __eid2), \
1729 __ev1, __ev2)
1731 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1732 /* Numeric models ------------------ */
1733 /* FW MODEL BIOS VERS */
1734 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1735 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1736 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1737 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1738 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1739 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1740 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1741 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1742 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1744 /* A-series ------------------------- */
1745 /* FW MODEL BIOS VERS EC VERS */
1746 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1747 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1748 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1749 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1750 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1751 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1752 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1753 TPV_QI0('1', '3', '2', '0'), /* A22m */
1754 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1755 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1756 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1758 /* G-series ------------------------- */
1759 /* FW MODEL BIOS VERS */
1760 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1761 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1763 /* R-series, T-series --------------- */
1764 /* FW MODEL BIOS VERS EC VERS */
1765 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1766 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1767 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1768 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1769 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1770 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1771 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1772 T40/p, T41/p, T42/p (1) */
1773 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1774 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1775 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1776 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1778 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1779 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1780 TPV_QI0('1', '6', '3', '2'), /* T22 */
1781 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1782 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1783 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1785 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1786 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1787 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1789 /* BIOS FW BIOS VERS EC FW EC VERS */
1790 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1791 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1793 /* X-series ------------------------- */
1794 /* FW MODEL BIOS VERS EC VERS */
1795 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1796 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1797 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1798 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1799 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1800 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1801 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1803 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1804 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1806 /* (0) - older versions lack DMI EC fw string and functionality */
1807 /* (1) - older versions known to lack functionality */
1810 #undef TPV_QL1
1811 #undef TPV_QL0
1812 #undef TPV_QI2
1813 #undef TPV_QI1
1814 #undef TPV_QI0
1815 #undef TPV_Q_X
1816 #undef TPV_Q
1818 static void __init tpacpi_check_outdated_fw(void)
1820 unsigned long fwvers;
1821 u16 ec_version, bios_version;
1823 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1824 ARRAY_SIZE(tpacpi_bios_version_qtable));
1826 if (!fwvers)
1827 return;
1829 bios_version = fwvers & 0xffffU;
1830 ec_version = (fwvers >> 16) & 0xffffU;
1832 /* note that unknown versions are set to 0x0000 and we use that */
1833 if ((bios_version > thinkpad_id.bios_release) ||
1834 (ec_version > thinkpad_id.ec_release &&
1835 ec_version != TPACPI_MATCH_ANY)) {
1837 * The changelogs would let us track down the exact
1838 * reason, but it is just too much of a pain to track
1839 * it. We only list BIOSes that are either really
1840 * broken, or really stable to begin with, so it is
1841 * best if the user upgrades the firmware anyway.
1843 printk(TPACPI_WARN
1844 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1845 printk(TPACPI_WARN
1846 "WARNING: This firmware may be missing critical bug "
1847 "fixes and/or important features\n");
1851 static bool __init tpacpi_is_fw_known(void)
1853 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1854 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1857 /****************************************************************************
1858 ****************************************************************************
1860 * Subdrivers
1862 ****************************************************************************
1863 ****************************************************************************/
1865 /*************************************************************************
1866 * thinkpad-acpi init subdriver
1869 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1871 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1872 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1874 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1875 (thinkpad_id.bios_version_str) ?
1876 thinkpad_id.bios_version_str : "unknown",
1877 (thinkpad_id.ec_version_str) ?
1878 thinkpad_id.ec_version_str : "unknown");
1880 if (thinkpad_id.vendor && thinkpad_id.model_str)
1881 printk(TPACPI_INFO "%s %s, model %s\n",
1882 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1883 "IBM" : ((thinkpad_id.vendor ==
1884 PCI_VENDOR_ID_LENOVO) ?
1885 "Lenovo" : "Unknown vendor"),
1886 thinkpad_id.model_str,
1887 (thinkpad_id.nummodel_str) ?
1888 thinkpad_id.nummodel_str : "unknown");
1890 tpacpi_check_outdated_fw();
1891 return 0;
1894 static int thinkpad_acpi_driver_read(char *p)
1896 int len = 0;
1898 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1899 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1901 return len;
1904 static struct ibm_struct thinkpad_acpi_driver_data = {
1905 .name = "driver",
1906 .read = thinkpad_acpi_driver_read,
1909 /*************************************************************************
1910 * Hotkey subdriver
1914 * ThinkPad firmware event model
1916 * The ThinkPad firmware has two main event interfaces: normal ACPI
1917 * notifications (which follow the ACPI standard), and a private event
1918 * interface.
1920 * The private event interface also issues events for the hotkeys. As
1921 * the driver gained features, the event handling code ended up being
1922 * built around the hotkey subdriver. This will need to be refactored
1923 * to a more formal event API eventually.
1925 * Some "hotkeys" are actually supposed to be used as event reports,
1926 * such as "brightness has changed", "volume has changed", depending on
1927 * the ThinkPad model and how the firmware is operating.
1929 * Unlike other classes, hotkey-class events have mask/unmask control on
1930 * non-ancient firmware. However, how it behaves changes a lot with the
1931 * firmware model and version.
1934 enum { /* hot key scan codes (derived from ACPI DSDT) */
1935 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1936 TP_ACPI_HOTKEYSCAN_FNF2,
1937 TP_ACPI_HOTKEYSCAN_FNF3,
1938 TP_ACPI_HOTKEYSCAN_FNF4,
1939 TP_ACPI_HOTKEYSCAN_FNF5,
1940 TP_ACPI_HOTKEYSCAN_FNF6,
1941 TP_ACPI_HOTKEYSCAN_FNF7,
1942 TP_ACPI_HOTKEYSCAN_FNF8,
1943 TP_ACPI_HOTKEYSCAN_FNF9,
1944 TP_ACPI_HOTKEYSCAN_FNF10,
1945 TP_ACPI_HOTKEYSCAN_FNF11,
1946 TP_ACPI_HOTKEYSCAN_FNF12,
1947 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1948 TP_ACPI_HOTKEYSCAN_FNINSERT,
1949 TP_ACPI_HOTKEYSCAN_FNDELETE,
1950 TP_ACPI_HOTKEYSCAN_FNHOME,
1951 TP_ACPI_HOTKEYSCAN_FNEND,
1952 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1953 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1954 TP_ACPI_HOTKEYSCAN_FNSPACE,
1955 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1956 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1957 TP_ACPI_HOTKEYSCAN_MUTE,
1958 TP_ACPI_HOTKEYSCAN_THINKPAD,
1961 enum { /* Keys/events available through NVRAM polling */
1962 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1963 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1966 enum { /* Positions of some of the keys in hotkey masks */
1967 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1968 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1969 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1970 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1971 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1972 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1973 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1974 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1975 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1976 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1977 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1980 enum { /* NVRAM to ACPI HKEY group map */
1981 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1982 TP_ACPI_HKEY_ZOOM_MASK |
1983 TP_ACPI_HKEY_DISPSWTCH_MASK |
1984 TP_ACPI_HKEY_HIBERNATE_MASK,
1985 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1986 TP_ACPI_HKEY_BRGHTDWN_MASK,
1987 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1988 TP_ACPI_HKEY_VOLDWN_MASK |
1989 TP_ACPI_HKEY_MUTE_MASK,
1992 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1993 struct tp_nvram_state {
1994 u16 thinkpad_toggle:1;
1995 u16 zoom_toggle:1;
1996 u16 display_toggle:1;
1997 u16 thinklight_toggle:1;
1998 u16 hibernate_toggle:1;
1999 u16 displayexp_toggle:1;
2000 u16 display_state:1;
2001 u16 brightness_toggle:1;
2002 u16 volume_toggle:1;
2003 u16 mute:1;
2005 u8 brightness_level;
2006 u8 volume_level;
2009 /* kthread for the hotkey poller */
2010 static struct task_struct *tpacpi_hotkey_task;
2012 /* Acquired while the poller kthread is running, use to sync start/stop */
2013 static struct mutex hotkey_thread_mutex;
2016 * Acquire mutex to write poller control variables as an
2017 * atomic block.
2019 * Increment hotkey_config_change when changing them if you
2020 * want the kthread to forget old state.
2022 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2024 static struct mutex hotkey_thread_data_mutex;
2025 static unsigned int hotkey_config_change;
2028 * hotkey poller control variables
2030 * Must be atomic or readers will also need to acquire mutex
2032 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2033 * should be used only when the changes need to be taken as
2034 * a block, OR when one needs to force the kthread to forget
2035 * old state.
2037 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2038 static unsigned int hotkey_poll_freq = 10; /* Hz */
2040 #define HOTKEY_CONFIG_CRITICAL_START \
2041 do { \
2042 mutex_lock(&hotkey_thread_data_mutex); \
2043 hotkey_config_change++; \
2044 } while (0);
2045 #define HOTKEY_CONFIG_CRITICAL_END \
2046 mutex_unlock(&hotkey_thread_data_mutex);
2048 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2050 #define hotkey_source_mask 0U
2051 #define HOTKEY_CONFIG_CRITICAL_START
2052 #define HOTKEY_CONFIG_CRITICAL_END
2054 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2056 static struct mutex hotkey_mutex;
2058 static enum { /* Reasons for waking up */
2059 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2060 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2061 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2062 } hotkey_wakeup_reason;
2064 static int hotkey_autosleep_ack;
2066 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2067 static u32 hotkey_all_mask; /* all events supported in fw */
2068 static u32 hotkey_reserved_mask; /* events better left disabled */
2069 static u32 hotkey_driver_mask; /* events needed by the driver */
2070 static u32 hotkey_user_mask; /* events visible to userspace */
2071 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2073 static unsigned int hotkey_report_mode;
2075 static u16 *hotkey_keycode_map;
2077 static struct attribute_set *hotkey_dev_attributes;
2079 static void tpacpi_driver_event(const unsigned int hkey_event);
2080 static void hotkey_driver_event(const unsigned int scancode);
2081 static void hotkey_poll_setup(const bool may_warn);
2083 /* HKEY.MHKG() return bits */
2084 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2086 static int hotkey_get_wlsw(void)
2088 int status;
2090 if (!tp_features.hotkey_wlsw)
2091 return -ENODEV;
2093 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2094 if (dbg_wlswemul)
2095 return (tpacpi_wlsw_emulstate) ?
2096 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2097 #endif
2099 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2100 return -EIO;
2102 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2105 static int hotkey_get_tablet_mode(int *status)
2107 int s;
2109 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2110 return -EIO;
2112 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2113 return 0;
2117 * Reads current event mask from firmware, and updates
2118 * hotkey_acpi_mask accordingly. Also resets any bits
2119 * from hotkey_user_mask that are unavailable to be
2120 * delivered (shadow requirement of the userspace ABI).
2122 * Call with hotkey_mutex held
2124 static int hotkey_mask_get(void)
2126 if (tp_features.hotkey_mask) {
2127 u32 m = 0;
2129 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2130 return -EIO;
2132 hotkey_acpi_mask = m;
2133 } else {
2134 /* no mask support doesn't mean no event support... */
2135 hotkey_acpi_mask = hotkey_all_mask;
2138 /* sync userspace-visible mask */
2139 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2141 return 0;
2144 void static hotkey_mask_warn_incomplete_mask(void)
2146 /* log only what the user can fix... */
2147 const u32 wantedmask = hotkey_driver_mask &
2148 ~(hotkey_acpi_mask | hotkey_source_mask) &
2149 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2151 if (wantedmask)
2152 printk(TPACPI_NOTICE
2153 "required events 0x%08x not enabled!\n",
2154 wantedmask);
2158 * Set the firmware mask when supported
2160 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2162 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2164 * Call with hotkey_mutex held
2166 static int hotkey_mask_set(u32 mask)
2168 int i;
2169 int rc = 0;
2171 const u32 fwmask = mask & ~hotkey_source_mask;
2173 if (tp_features.hotkey_mask) {
2174 for (i = 0; i < 32; i++) {
2175 if (!acpi_evalf(hkey_handle,
2176 NULL, "MHKM", "vdd", i + 1,
2177 !!(mask & (1 << i)))) {
2178 rc = -EIO;
2179 break;
2185 * We *must* make an inconditional call to hotkey_mask_get to
2186 * refresh hotkey_acpi_mask and update hotkey_user_mask
2188 * Take the opportunity to also log when we cannot _enable_
2189 * a given event.
2191 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2192 printk(TPACPI_NOTICE
2193 "asked for hotkey mask 0x%08x, but "
2194 "firmware forced it to 0x%08x\n",
2195 fwmask, hotkey_acpi_mask);
2198 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2199 hotkey_mask_warn_incomplete_mask();
2201 return rc;
2205 * Sets hotkey_user_mask and tries to set the firmware mask
2207 * Call with hotkey_mutex held
2209 static int hotkey_user_mask_set(const u32 mask)
2211 int rc;
2213 /* Give people a chance to notice they are doing something that
2214 * is bound to go boom on their users sooner or later */
2215 if (!tp_warned.hotkey_mask_ff &&
2216 (mask == 0xffff || mask == 0xffffff ||
2217 mask == 0xffffffff)) {
2218 tp_warned.hotkey_mask_ff = 1;
2219 printk(TPACPI_NOTICE
2220 "setting the hotkey mask to 0x%08x is likely "
2221 "not the best way to go about it\n", mask);
2222 printk(TPACPI_NOTICE
2223 "please consider using the driver defaults, "
2224 "and refer to up-to-date thinkpad-acpi "
2225 "documentation\n");
2228 /* Try to enable what the user asked for, plus whatever we need.
2229 * this syncs everything but won't enable bits in hotkey_user_mask */
2230 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2232 /* Enable the available bits in hotkey_user_mask */
2233 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2235 return rc;
2239 * Sets the driver hotkey mask.
2241 * Can be called even if the hotkey subdriver is inactive
2243 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2245 int rc;
2247 /* Do the right thing if hotkey_init has not been called yet */
2248 if (!tp_features.hotkey) {
2249 hotkey_driver_mask = mask;
2250 return 0;
2253 mutex_lock(&hotkey_mutex);
2255 HOTKEY_CONFIG_CRITICAL_START
2256 hotkey_driver_mask = mask;
2257 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2258 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2259 #endif
2260 HOTKEY_CONFIG_CRITICAL_END
2262 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2263 ~hotkey_source_mask);
2264 hotkey_poll_setup(true);
2266 mutex_unlock(&hotkey_mutex);
2268 return rc;
2271 static int hotkey_status_get(int *status)
2273 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2274 return -EIO;
2276 return 0;
2279 static int hotkey_status_set(bool enable)
2281 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2282 return -EIO;
2284 return 0;
2287 static void tpacpi_input_send_tabletsw(void)
2289 int state;
2291 if (tp_features.hotkey_tablet &&
2292 !hotkey_get_tablet_mode(&state)) {
2293 mutex_lock(&tpacpi_inputdev_send_mutex);
2295 input_report_switch(tpacpi_inputdev,
2296 SW_TABLET_MODE, !!state);
2297 input_sync(tpacpi_inputdev);
2299 mutex_unlock(&tpacpi_inputdev_send_mutex);
2303 /* Do NOT call without validating scancode first */
2304 static void tpacpi_input_send_key(const unsigned int scancode)
2306 const unsigned int keycode = hotkey_keycode_map[scancode];
2308 if (keycode != KEY_RESERVED) {
2309 mutex_lock(&tpacpi_inputdev_send_mutex);
2311 input_report_key(tpacpi_inputdev, keycode, 1);
2312 if (keycode == KEY_UNKNOWN)
2313 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2314 scancode);
2315 input_sync(tpacpi_inputdev);
2317 input_report_key(tpacpi_inputdev, keycode, 0);
2318 if (keycode == KEY_UNKNOWN)
2319 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2320 scancode);
2321 input_sync(tpacpi_inputdev);
2323 mutex_unlock(&tpacpi_inputdev_send_mutex);
2327 /* Do NOT call without validating scancode first */
2328 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2330 hotkey_driver_event(scancode);
2331 if (hotkey_user_mask & (1 << scancode))
2332 tpacpi_input_send_key(scancode);
2335 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2336 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2338 /* Do NOT call without validating scancode first */
2339 static void tpacpi_hotkey_send_key(unsigned int scancode)
2341 tpacpi_input_send_key_masked(scancode);
2342 if (hotkey_report_mode < 2) {
2343 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2344 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2348 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2350 u8 d;
2352 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2353 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2354 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2355 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2356 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2357 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2359 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2360 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2361 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2363 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2365 n->displayexp_toggle =
2366 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2368 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2369 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2370 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2371 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2372 n->brightness_toggle =
2373 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2375 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2376 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2377 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2378 >> TP_NVRAM_POS_LEVEL_VOLUME;
2379 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2380 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2384 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2385 struct tp_nvram_state *newn,
2386 const u32 event_mask)
2389 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2390 do { \
2391 if ((event_mask & (1 << __scancode)) && \
2392 oldn->__member != newn->__member) \
2393 tpacpi_hotkey_send_key(__scancode); \
2394 } while (0)
2396 #define TPACPI_MAY_SEND_KEY(__scancode) \
2397 do { \
2398 if (event_mask & (1 << __scancode)) \
2399 tpacpi_hotkey_send_key(__scancode); \
2400 } while (0)
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2411 /* handle volume */
2412 if (oldn->volume_toggle != newn->volume_toggle) {
2413 if (oldn->mute != newn->mute) {
2414 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2416 if (oldn->volume_level > newn->volume_level) {
2417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2418 } else if (oldn->volume_level < newn->volume_level) {
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2420 } else if (oldn->mute == newn->mute) {
2421 /* repeated key presses that didn't change state */
2422 if (newn->mute) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2424 } else if (newn->volume_level != 0) {
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2426 } else {
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2432 /* handle brightness */
2433 if (oldn->brightness_toggle != newn->brightness_toggle) {
2434 if (oldn->brightness_level < newn->brightness_level) {
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2436 } else if (oldn->brightness_level > newn->brightness_level) {
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2438 } else {
2439 /* repeated key presses that didn't change state */
2440 if (newn->brightness_level != 0) {
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2442 } else {
2443 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2448 #undef TPACPI_COMPARE_KEY
2449 #undef TPACPI_MAY_SEND_KEY
2453 * Polling driver
2455 * We track all events in hotkey_source_mask all the time, since
2456 * most of them are edge-based. We only issue those requested by
2457 * hotkey_user_mask or hotkey_driver_mask, though.
2459 static int hotkey_kthread(void *data)
2461 struct tp_nvram_state s[2];
2462 u32 poll_mask, event_mask;
2463 unsigned int si, so;
2464 unsigned long t;
2465 unsigned int change_detector, must_reset;
2466 unsigned int poll_freq;
2468 mutex_lock(&hotkey_thread_mutex);
2470 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2471 goto exit;
2473 set_freezable();
2475 so = 0;
2476 si = 1;
2477 t = 0;
2479 /* Initial state for compares */
2480 mutex_lock(&hotkey_thread_data_mutex);
2481 change_detector = hotkey_config_change;
2482 poll_mask = hotkey_source_mask;
2483 event_mask = hotkey_source_mask &
2484 (hotkey_driver_mask | hotkey_user_mask);
2485 poll_freq = hotkey_poll_freq;
2486 mutex_unlock(&hotkey_thread_data_mutex);
2487 hotkey_read_nvram(&s[so], poll_mask);
2489 while (!kthread_should_stop()) {
2490 if (t == 0) {
2491 if (likely(poll_freq))
2492 t = 1000/poll_freq;
2493 else
2494 t = 100; /* should never happen... */
2496 t = msleep_interruptible(t);
2497 if (unlikely(kthread_should_stop()))
2498 break;
2499 must_reset = try_to_freeze();
2500 if (t > 0 && !must_reset)
2501 continue;
2503 mutex_lock(&hotkey_thread_data_mutex);
2504 if (must_reset || hotkey_config_change != change_detector) {
2505 /* forget old state on thaw or config change */
2506 si = so;
2507 t = 0;
2508 change_detector = hotkey_config_change;
2510 poll_mask = hotkey_source_mask;
2511 event_mask = hotkey_source_mask &
2512 (hotkey_driver_mask | hotkey_user_mask);
2513 poll_freq = hotkey_poll_freq;
2514 mutex_unlock(&hotkey_thread_data_mutex);
2516 if (likely(poll_mask)) {
2517 hotkey_read_nvram(&s[si], poll_mask);
2518 if (likely(si != so)) {
2519 hotkey_compare_and_issue_event(&s[so], &s[si],
2520 event_mask);
2524 so = si;
2525 si ^= 1;
2528 exit:
2529 mutex_unlock(&hotkey_thread_mutex);
2530 return 0;
2533 /* call with hotkey_mutex held */
2534 static void hotkey_poll_stop_sync(void)
2536 if (tpacpi_hotkey_task) {
2537 if (frozen(tpacpi_hotkey_task) ||
2538 freezing(tpacpi_hotkey_task))
2539 thaw_process(tpacpi_hotkey_task);
2541 kthread_stop(tpacpi_hotkey_task);
2542 tpacpi_hotkey_task = NULL;
2543 mutex_lock(&hotkey_thread_mutex);
2544 /* at this point, the thread did exit */
2545 mutex_unlock(&hotkey_thread_mutex);
2549 /* call with hotkey_mutex held */
2550 static void hotkey_poll_setup(const bool may_warn)
2552 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2553 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2555 if (hotkey_poll_freq > 0 &&
2556 (poll_driver_mask ||
2557 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2558 if (!tpacpi_hotkey_task) {
2559 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2560 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2561 if (IS_ERR(tpacpi_hotkey_task)) {
2562 tpacpi_hotkey_task = NULL;
2563 printk(TPACPI_ERR
2564 "could not create kernel thread "
2565 "for hotkey polling\n");
2568 } else {
2569 hotkey_poll_stop_sync();
2570 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2571 hotkey_poll_freq == 0) {
2572 printk(TPACPI_NOTICE
2573 "hot keys 0x%08x and/or events 0x%08x "
2574 "require polling, which is currently "
2575 "disabled\n",
2576 poll_user_mask, poll_driver_mask);
2581 static void hotkey_poll_setup_safe(const bool may_warn)
2583 mutex_lock(&hotkey_mutex);
2584 hotkey_poll_setup(may_warn);
2585 mutex_unlock(&hotkey_mutex);
2588 /* call with hotkey_mutex held */
2589 static void hotkey_poll_set_freq(unsigned int freq)
2591 if (!freq)
2592 hotkey_poll_stop_sync();
2594 hotkey_poll_freq = freq;
2597 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2599 static void hotkey_poll_setup(const bool __unused)
2603 static void hotkey_poll_setup_safe(const bool __unused)
2607 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2609 static int hotkey_inputdev_open(struct input_dev *dev)
2611 switch (tpacpi_lifecycle) {
2612 case TPACPI_LIFE_INIT:
2613 case TPACPI_LIFE_RUNNING:
2614 hotkey_poll_setup_safe(false);
2615 return 0;
2616 case TPACPI_LIFE_EXITING:
2617 return -EBUSY;
2620 /* Should only happen if tpacpi_lifecycle is corrupt */
2621 BUG();
2622 return -EBUSY;
2625 static void hotkey_inputdev_close(struct input_dev *dev)
2627 /* disable hotkey polling when possible */
2628 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2629 !(hotkey_source_mask & hotkey_driver_mask))
2630 hotkey_poll_setup_safe(false);
2633 /* sysfs hotkey enable ------------------------------------------------- */
2634 static ssize_t hotkey_enable_show(struct device *dev,
2635 struct device_attribute *attr,
2636 char *buf)
2638 int res, status;
2640 printk_deprecated_attribute("hotkey_enable",
2641 "Hotkey reporting is always enabled");
2643 res = hotkey_status_get(&status);
2644 if (res)
2645 return res;
2647 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2650 static ssize_t hotkey_enable_store(struct device *dev,
2651 struct device_attribute *attr,
2652 const char *buf, size_t count)
2654 unsigned long t;
2656 printk_deprecated_attribute("hotkey_enable",
2657 "Hotkeys can be disabled through hotkey_mask");
2659 if (parse_strtoul(buf, 1, &t))
2660 return -EINVAL;
2662 if (t == 0)
2663 return -EPERM;
2665 return count;
2668 static struct device_attribute dev_attr_hotkey_enable =
2669 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2670 hotkey_enable_show, hotkey_enable_store);
2672 /* sysfs hotkey mask --------------------------------------------------- */
2673 static ssize_t hotkey_mask_show(struct device *dev,
2674 struct device_attribute *attr,
2675 char *buf)
2677 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2680 static ssize_t hotkey_mask_store(struct device *dev,
2681 struct device_attribute *attr,
2682 const char *buf, size_t count)
2684 unsigned long t;
2685 int res;
2687 if (parse_strtoul(buf, 0xffffffffUL, &t))
2688 return -EINVAL;
2690 if (mutex_lock_killable(&hotkey_mutex))
2691 return -ERESTARTSYS;
2693 res = hotkey_user_mask_set(t);
2695 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2696 hotkey_poll_setup(true);
2697 #endif
2699 mutex_unlock(&hotkey_mutex);
2701 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2703 return (res) ? res : count;
2706 static struct device_attribute dev_attr_hotkey_mask =
2707 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2708 hotkey_mask_show, hotkey_mask_store);
2710 /* sysfs hotkey bios_enabled ------------------------------------------- */
2711 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2712 struct device_attribute *attr,
2713 char *buf)
2715 return sprintf(buf, "0\n");
2718 static struct device_attribute dev_attr_hotkey_bios_enabled =
2719 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2721 /* sysfs hotkey bios_mask ---------------------------------------------- */
2722 static ssize_t hotkey_bios_mask_show(struct device *dev,
2723 struct device_attribute *attr,
2724 char *buf)
2726 printk_deprecated_attribute("hotkey_bios_mask",
2727 "This attribute is useless.");
2728 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2731 static struct device_attribute dev_attr_hotkey_bios_mask =
2732 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2734 /* sysfs hotkey all_mask ----------------------------------------------- */
2735 static ssize_t hotkey_all_mask_show(struct device *dev,
2736 struct device_attribute *attr,
2737 char *buf)
2739 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2740 hotkey_all_mask | hotkey_source_mask);
2743 static struct device_attribute dev_attr_hotkey_all_mask =
2744 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2746 /* sysfs hotkey recommended_mask --------------------------------------- */
2747 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2748 struct device_attribute *attr,
2749 char *buf)
2751 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2752 (hotkey_all_mask | hotkey_source_mask)
2753 & ~hotkey_reserved_mask);
2756 static struct device_attribute dev_attr_hotkey_recommended_mask =
2757 __ATTR(hotkey_recommended_mask, S_IRUGO,
2758 hotkey_recommended_mask_show, NULL);
2760 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2762 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2763 static ssize_t hotkey_source_mask_show(struct device *dev,
2764 struct device_attribute *attr,
2765 char *buf)
2767 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2770 static ssize_t hotkey_source_mask_store(struct device *dev,
2771 struct device_attribute *attr,
2772 const char *buf, size_t count)
2774 unsigned long t;
2775 u32 r_ev;
2776 int rc;
2778 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2779 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2780 return -EINVAL;
2782 if (mutex_lock_killable(&hotkey_mutex))
2783 return -ERESTARTSYS;
2785 HOTKEY_CONFIG_CRITICAL_START
2786 hotkey_source_mask = t;
2787 HOTKEY_CONFIG_CRITICAL_END
2789 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2790 ~hotkey_source_mask);
2791 hotkey_poll_setup(true);
2793 /* check if events needed by the driver got disabled */
2794 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2795 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2797 mutex_unlock(&hotkey_mutex);
2799 if (rc < 0)
2800 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2801 "firmware event mask!\n");
2803 if (r_ev)
2804 printk(TPACPI_NOTICE "hotkey_source_mask: "
2805 "some important events were disabled: "
2806 "0x%04x\n", r_ev);
2808 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2810 return (rc < 0) ? rc : count;
2813 static struct device_attribute dev_attr_hotkey_source_mask =
2814 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2815 hotkey_source_mask_show, hotkey_source_mask_store);
2817 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2818 static ssize_t hotkey_poll_freq_show(struct device *dev,
2819 struct device_attribute *attr,
2820 char *buf)
2822 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2825 static ssize_t hotkey_poll_freq_store(struct device *dev,
2826 struct device_attribute *attr,
2827 const char *buf, size_t count)
2829 unsigned long t;
2831 if (parse_strtoul(buf, 25, &t))
2832 return -EINVAL;
2834 if (mutex_lock_killable(&hotkey_mutex))
2835 return -ERESTARTSYS;
2837 hotkey_poll_set_freq(t);
2838 hotkey_poll_setup(true);
2840 mutex_unlock(&hotkey_mutex);
2842 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2844 return count;
2847 static struct device_attribute dev_attr_hotkey_poll_freq =
2848 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2849 hotkey_poll_freq_show, hotkey_poll_freq_store);
2851 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2853 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2854 static ssize_t hotkey_radio_sw_show(struct device *dev,
2855 struct device_attribute *attr,
2856 char *buf)
2858 int res;
2859 res = hotkey_get_wlsw();
2860 if (res < 0)
2861 return res;
2863 /* Opportunistic update */
2864 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2866 return snprintf(buf, PAGE_SIZE, "%d\n",
2867 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2870 static struct device_attribute dev_attr_hotkey_radio_sw =
2871 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2873 static void hotkey_radio_sw_notify_change(void)
2875 if (tp_features.hotkey_wlsw)
2876 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2877 "hotkey_radio_sw");
2880 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2881 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2882 struct device_attribute *attr,
2883 char *buf)
2885 int res, s;
2886 res = hotkey_get_tablet_mode(&s);
2887 if (res < 0)
2888 return res;
2890 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2893 static struct device_attribute dev_attr_hotkey_tablet_mode =
2894 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2896 static void hotkey_tablet_mode_notify_change(void)
2898 if (tp_features.hotkey_tablet)
2899 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2900 "hotkey_tablet_mode");
2903 /* sysfs hotkey report_mode -------------------------------------------- */
2904 static ssize_t hotkey_report_mode_show(struct device *dev,
2905 struct device_attribute *attr,
2906 char *buf)
2908 return snprintf(buf, PAGE_SIZE, "%d\n",
2909 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2912 static struct device_attribute dev_attr_hotkey_report_mode =
2913 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2915 /* sysfs wakeup reason (pollable) -------------------------------------- */
2916 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2917 struct device_attribute *attr,
2918 char *buf)
2920 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2923 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2924 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2926 static void hotkey_wakeup_reason_notify_change(void)
2928 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2929 "wakeup_reason");
2932 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2933 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2934 struct device_attribute *attr,
2935 char *buf)
2937 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2940 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2941 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2942 hotkey_wakeup_hotunplug_complete_show, NULL);
2944 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2946 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2947 "wakeup_hotunplug_complete");
2950 /* --------------------------------------------------------------------- */
2952 static struct attribute *hotkey_attributes[] __initdata = {
2953 &dev_attr_hotkey_enable.attr,
2954 &dev_attr_hotkey_bios_enabled.attr,
2955 &dev_attr_hotkey_bios_mask.attr,
2956 &dev_attr_hotkey_report_mode.attr,
2957 &dev_attr_hotkey_wakeup_reason.attr,
2958 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2959 &dev_attr_hotkey_mask.attr,
2960 &dev_attr_hotkey_all_mask.attr,
2961 &dev_attr_hotkey_recommended_mask.attr,
2962 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2963 &dev_attr_hotkey_source_mask.attr,
2964 &dev_attr_hotkey_poll_freq.attr,
2965 #endif
2969 * Sync both the hw and sw blocking state of all switches
2971 static void tpacpi_send_radiosw_update(void)
2973 int wlsw;
2976 * We must sync all rfkill controllers *before* issuing any
2977 * rfkill input events, or we will race the rfkill core input
2978 * handler.
2980 * tpacpi_inputdev_send_mutex works as a syncronization point
2981 * for the above.
2983 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2986 wlsw = hotkey_get_wlsw();
2988 /* Sync hw blocking state first if it is hw-blocked */
2989 if (wlsw == TPACPI_RFK_RADIO_OFF)
2990 tpacpi_rfk_update_hwblock_state(true);
2992 /* Sync sw blocking state */
2993 tpacpi_rfk_update_swstate_all();
2995 /* Sync hw blocking state last if it is hw-unblocked */
2996 if (wlsw == TPACPI_RFK_RADIO_ON)
2997 tpacpi_rfk_update_hwblock_state(false);
2999 /* Issue rfkill input event for WLSW switch */
3000 if (!(wlsw < 0)) {
3001 mutex_lock(&tpacpi_inputdev_send_mutex);
3003 input_report_switch(tpacpi_inputdev,
3004 SW_RFKILL_ALL, (wlsw > 0));
3005 input_sync(tpacpi_inputdev);
3007 mutex_unlock(&tpacpi_inputdev_send_mutex);
3011 * this can be unconditional, as we will poll state again
3012 * if userspace uses the notify to read data
3014 hotkey_radio_sw_notify_change();
3017 static void hotkey_exit(void)
3019 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3020 mutex_lock(&hotkey_mutex);
3021 hotkey_poll_stop_sync();
3022 mutex_unlock(&hotkey_mutex);
3023 #endif
3025 if (hotkey_dev_attributes)
3026 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3028 kfree(hotkey_keycode_map);
3030 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3031 "restoring original HKEY status and mask\n");
3032 /* yes, there is a bitwise or below, we want the
3033 * functions to be called even if one of them fail */
3034 if (((tp_features.hotkey_mask &&
3035 hotkey_mask_set(hotkey_orig_mask)) |
3036 hotkey_status_set(false)) != 0)
3037 printk(TPACPI_ERR
3038 "failed to restore hot key mask "
3039 "to BIOS defaults\n");
3042 static void __init hotkey_unmap(const unsigned int scancode)
3044 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3045 clear_bit(hotkey_keycode_map[scancode],
3046 tpacpi_inputdev->keybit);
3047 hotkey_keycode_map[scancode] = KEY_RESERVED;
3052 * HKEY quirks:
3053 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3056 #define TPACPI_HK_Q_INIMASK 0x0001
3058 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3059 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3060 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3061 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3062 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3063 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3064 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3065 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3066 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3067 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3068 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3069 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3070 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3071 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3072 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3073 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3074 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3075 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3076 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3077 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3080 static int __init hotkey_init(struct ibm_init_struct *iibm)
3082 /* Requirements for changing the default keymaps:
3084 * 1. Many of the keys are mapped to KEY_RESERVED for very
3085 * good reasons. Do not change them unless you have deep
3086 * knowledge on the IBM and Lenovo ThinkPad firmware for
3087 * the various ThinkPad models. The driver behaves
3088 * differently for KEY_RESERVED: such keys have their
3089 * hot key mask *unset* in mask_recommended, and also
3090 * in the initial hot key mask programmed into the
3091 * firmware at driver load time, which means the firm-
3092 * ware may react very differently if you change them to
3093 * something else;
3095 * 2. You must be subscribed to the linux-thinkpad and
3096 * ibm-acpi-devel mailing lists, and you should read the
3097 * list archives since 2007 if you want to change the
3098 * keymaps. This requirement exists so that you will
3099 * know the past history of problems with the thinkpad-
3100 * acpi driver keymaps, and also that you will be
3101 * listening to any bug reports;
3103 * 3. Do not send thinkpad-acpi specific patches directly to
3104 * for merging, *ever*. Send them to the linux-acpi
3105 * mailinglist for comments. Merging is to be done only
3106 * through acpi-test and the ACPI maintainer.
3108 * If the above is too much to ask, don't change the keymap.
3109 * Ask the thinkpad-acpi maintainer to do it, instead.
3111 static u16 ibm_keycode_map[] __initdata = {
3112 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3113 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3114 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3115 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3117 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3118 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3119 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3120 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3122 /* brightness: firmware always reacts to them */
3123 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3124 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3126 /* Thinklight: firmware always react to it */
3127 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3129 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3130 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3132 /* Volume: firmware always react to it and reprograms
3133 * the built-in *extra* mixer. Never map it to control
3134 * another mixer by default. */
3135 KEY_RESERVED, /* 0x14: VOLUME UP */
3136 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3137 KEY_RESERVED, /* 0x16: MUTE */
3139 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3141 /* (assignments unknown, please report if found) */
3142 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3143 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3145 static u16 lenovo_keycode_map[] __initdata = {
3146 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3147 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3148 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3149 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3151 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3152 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3153 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3154 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3156 /* These should be enabled --only-- when ACPI video
3157 * is disabled (i.e. in "vendor" mode), and are handled
3158 * in a special way by the init code */
3159 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3160 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3162 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3164 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3165 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3167 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3168 * react to it and reprograms the built-in *extra* mixer.
3169 * Never map it to control another mixer by default.
3171 * T60?, T61, R60?, R61: firmware and EC tries to send
3172 * these over the regular keyboard, so these are no-ops,
3173 * but there are still weird bugs re. MUTE, so do not
3174 * change unless you get test reports from all Lenovo
3175 * models. May cause the BIOS to interfere with the
3176 * HDA mixer.
3178 KEY_RESERVED, /* 0x14: VOLUME UP */
3179 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3180 KEY_RESERVED, /* 0x16: MUTE */
3182 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3184 /* (assignments unknown, please report if found) */
3185 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3186 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3189 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3190 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3191 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3193 int res, i;
3194 int status;
3195 int hkeyv;
3196 bool radiosw_state = false;
3197 bool tabletsw_state = false;
3199 unsigned long quirks;
3201 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3202 "initializing hotkey subdriver\n");
3204 BUG_ON(!tpacpi_inputdev);
3205 BUG_ON(tpacpi_inputdev->open != NULL ||
3206 tpacpi_inputdev->close != NULL);
3208 TPACPI_ACPIHANDLE_INIT(hkey);
3209 mutex_init(&hotkey_mutex);
3211 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3212 mutex_init(&hotkey_thread_mutex);
3213 mutex_init(&hotkey_thread_data_mutex);
3214 #endif
3216 /* hotkey not supported on 570 */
3217 tp_features.hotkey = hkey_handle != NULL;
3219 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3220 "hotkeys are %s\n",
3221 str_supported(tp_features.hotkey));
3223 if (!tp_features.hotkey)
3224 return 1;
3226 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3227 ARRAY_SIZE(tpacpi_hotkey_qtable));
3229 tpacpi_disable_brightness_delay();
3231 /* MUST have enough space for all attributes to be added to
3232 * hotkey_dev_attributes */
3233 hotkey_dev_attributes = create_attr_set(
3234 ARRAY_SIZE(hotkey_attributes) + 2,
3235 NULL);
3236 if (!hotkey_dev_attributes)
3237 return -ENOMEM;
3238 res = add_many_to_attr_set(hotkey_dev_attributes,
3239 hotkey_attributes,
3240 ARRAY_SIZE(hotkey_attributes));
3241 if (res)
3242 goto err_exit;
3244 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3245 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3246 for HKEY interface version 0x100 */
3247 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3248 if ((hkeyv >> 8) != 1) {
3249 printk(TPACPI_ERR "unknown version of the "
3250 "HKEY interface: 0x%x\n", hkeyv);
3251 printk(TPACPI_ERR "please report this to %s\n",
3252 TPACPI_MAIL);
3253 } else {
3255 * MHKV 0x100 in A31, R40, R40e,
3256 * T4x, X31, and later
3258 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3259 "firmware HKEY interface version: 0x%x\n",
3260 hkeyv);
3262 /* Paranoia check AND init hotkey_all_mask */
3263 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3264 "MHKA", "qd")) {
3265 printk(TPACPI_ERR
3266 "missing MHKA handler, "
3267 "please report this to %s\n",
3268 TPACPI_MAIL);
3269 /* Fallback: pre-init for FN+F3,F4,F12 */
3270 hotkey_all_mask = 0x080cU;
3271 } else {
3272 tp_features.hotkey_mask = 1;
3277 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3278 "hotkey masks are %s\n",
3279 str_supported(tp_features.hotkey_mask));
3281 /* Init hotkey_all_mask if not initialized yet */
3282 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3283 (quirks & TPACPI_HK_Q_INIMASK))
3284 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3286 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3287 if (tp_features.hotkey_mask) {
3288 /* hotkey_source_mask *must* be zero for
3289 * the first hotkey_mask_get to return hotkey_orig_mask */
3290 res = hotkey_mask_get();
3291 if (res)
3292 goto err_exit;
3294 hotkey_orig_mask = hotkey_acpi_mask;
3295 } else {
3296 hotkey_orig_mask = hotkey_all_mask;
3297 hotkey_acpi_mask = hotkey_all_mask;
3300 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3301 if (dbg_wlswemul) {
3302 tp_features.hotkey_wlsw = 1;
3303 radiosw_state = !!tpacpi_wlsw_emulstate;
3304 printk(TPACPI_INFO
3305 "radio switch emulation enabled\n");
3306 } else
3307 #endif
3308 /* Not all thinkpads have a hardware radio switch */
3309 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3310 tp_features.hotkey_wlsw = 1;
3311 radiosw_state = !!status;
3312 printk(TPACPI_INFO
3313 "radio switch found; radios are %s\n",
3314 enabled(status, 0));
3316 if (tp_features.hotkey_wlsw)
3317 res = add_to_attr_set(hotkey_dev_attributes,
3318 &dev_attr_hotkey_radio_sw.attr);
3320 /* For X41t, X60t, X61t Tablets... */
3321 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3322 tp_features.hotkey_tablet = 1;
3323 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3324 printk(TPACPI_INFO
3325 "possible tablet mode switch found; "
3326 "ThinkPad in %s mode\n",
3327 (tabletsw_state) ? "tablet" : "laptop");
3328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_tablet_mode.attr);
3332 if (!res)
3333 res = register_attr_set_with_sysfs(
3334 hotkey_dev_attributes,
3335 &tpacpi_pdev->dev.kobj);
3336 if (res)
3337 goto err_exit;
3339 /* Set up key map */
3341 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3342 GFP_KERNEL);
3343 if (!hotkey_keycode_map) {
3344 printk(TPACPI_ERR
3345 "failed to allocate memory for key map\n");
3346 res = -ENOMEM;
3347 goto err_exit;
3350 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3351 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3352 "using Lenovo default hot key map\n");
3353 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3354 TPACPI_HOTKEY_MAP_SIZE);
3355 } else {
3356 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3357 "using IBM default hot key map\n");
3358 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3359 TPACPI_HOTKEY_MAP_SIZE);
3362 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3363 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3364 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3365 tpacpi_inputdev->keycode = hotkey_keycode_map;
3366 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3367 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3368 input_set_capability(tpacpi_inputdev, EV_KEY,
3369 hotkey_keycode_map[i]);
3370 } else {
3371 if (i < sizeof(hotkey_reserved_mask)*8)
3372 hotkey_reserved_mask |= 1 << i;
3376 if (tp_features.hotkey_wlsw) {
3377 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3378 input_report_switch(tpacpi_inputdev,
3379 SW_RFKILL_ALL, radiosw_state);
3381 if (tp_features.hotkey_tablet) {
3382 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3383 input_report_switch(tpacpi_inputdev,
3384 SW_TABLET_MODE, tabletsw_state);
3387 /* Do not issue duplicate brightness change events to
3388 * userspace */
3389 if (!tp_features.bright_acpimode)
3390 /* update bright_acpimode... */
3391 tpacpi_check_std_acpi_brightness_support();
3393 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3394 printk(TPACPI_INFO
3395 "This ThinkPad has standard ACPI backlight "
3396 "brightness control, supported by the ACPI "
3397 "video driver\n");
3398 printk(TPACPI_NOTICE
3399 "Disabling thinkpad-acpi brightness events "
3400 "by default...\n");
3402 /* Disable brightness up/down on Lenovo thinkpads when
3403 * ACPI is handling them, otherwise it is plain impossible
3404 * for userspace to do something even remotely sane */
3405 hotkey_reserved_mask |=
3406 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3407 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3408 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3409 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3412 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3413 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3414 & ~hotkey_all_mask
3415 & ~hotkey_reserved_mask;
3417 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "hotkey source mask 0x%08x, polling freq %u\n",
3419 hotkey_source_mask, hotkey_poll_freq);
3420 #endif
3422 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "enabling firmware HKEY event interface...\n");
3424 res = hotkey_status_set(true);
3425 if (res) {
3426 hotkey_exit();
3427 return res;
3429 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3430 | hotkey_driver_mask)
3431 & ~hotkey_source_mask);
3432 if (res < 0 && res != -ENXIO) {
3433 hotkey_exit();
3434 return res;
3436 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3437 & ~hotkey_reserved_mask;
3438 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3439 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3440 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3442 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "legacy ibm/hotkey event reporting over procfs %s\n",
3444 (hotkey_report_mode < 2) ?
3445 "enabled" : "disabled");
3447 tpacpi_inputdev->open = &hotkey_inputdev_open;
3448 tpacpi_inputdev->close = &hotkey_inputdev_close;
3450 hotkey_poll_setup_safe(true);
3452 return 0;
3454 err_exit:
3455 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3456 hotkey_dev_attributes = NULL;
3458 return (res < 0)? res : 1;
3461 static bool hotkey_notify_hotkey(const u32 hkey,
3462 bool *send_acpi_ev,
3463 bool *ignore_acpi_ev)
3465 /* 0x1000-0x1FFF: key presses */
3466 unsigned int scancode = hkey & 0xfff;
3467 *send_acpi_ev = true;
3468 *ignore_acpi_ev = false;
3470 if (scancode > 0 && scancode < 0x21) {
3471 scancode--;
3472 if (!(hotkey_source_mask & (1 << scancode))) {
3473 tpacpi_input_send_key_masked(scancode);
3474 *send_acpi_ev = false;
3475 } else {
3476 *ignore_acpi_ev = true;
3478 return true;
3480 return false;
3483 static bool hotkey_notify_wakeup(const u32 hkey,
3484 bool *send_acpi_ev,
3485 bool *ignore_acpi_ev)
3487 /* 0x2000-0x2FFF: Wakeup reason */
3488 *send_acpi_ev = true;
3489 *ignore_acpi_ev = false;
3491 switch (hkey) {
3492 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3493 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3494 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3495 *ignore_acpi_ev = true;
3496 break;
3498 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3499 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3500 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3501 *ignore_acpi_ev = true;
3502 break;
3504 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3505 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3506 printk(TPACPI_ALERT
3507 "EMERGENCY WAKEUP: battery almost empty\n");
3508 /* how to auto-heal: */
3509 /* 2313: woke up from S3, go to S4/S5 */
3510 /* 2413: woke up from S4, go to S5 */
3511 break;
3513 default:
3514 return false;
3517 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3518 printk(TPACPI_INFO
3519 "woke up due to a hot-unplug "
3520 "request...\n");
3521 hotkey_wakeup_reason_notify_change();
3523 return true;
3526 static bool hotkey_notify_usrevent(const u32 hkey,
3527 bool *send_acpi_ev,
3528 bool *ignore_acpi_ev)
3530 /* 0x5000-0x5FFF: human interface helpers */
3531 *send_acpi_ev = true;
3532 *ignore_acpi_ev = false;
3534 switch (hkey) {
3535 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3536 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3537 return true;
3539 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3540 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3541 tpacpi_input_send_tabletsw();
3542 hotkey_tablet_mode_notify_change();
3543 *send_acpi_ev = false;
3544 return true;
3546 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3547 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3548 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3549 /* do not propagate these events */
3550 *ignore_acpi_ev = true;
3551 return true;
3553 default:
3554 return false;
3558 static void thermal_dump_all_sensors(void);
3560 static bool hotkey_notify_thermal(const u32 hkey,
3561 bool *send_acpi_ev,
3562 bool *ignore_acpi_ev)
3564 bool known = true;
3566 /* 0x6000-0x6FFF: thermal alarms */
3567 *send_acpi_ev = true;
3568 *ignore_acpi_ev = false;
3570 switch (hkey) {
3571 case TP_HKEY_EV_THM_TABLE_CHANGED:
3572 printk(TPACPI_INFO
3573 "EC reports that Thermal Table has changed\n");
3574 /* recommended action: do nothing, we don't have
3575 * Lenovo ATM information */
3576 return true;
3577 case TP_HKEY_EV_ALARM_BAT_HOT:
3578 printk(TPACPI_CRIT
3579 "THERMAL ALARM: battery is too hot!\n");
3580 /* recommended action: warn user through gui */
3581 break;
3582 case TP_HKEY_EV_ALARM_BAT_XHOT:
3583 printk(TPACPI_ALERT
3584 "THERMAL EMERGENCY: battery is extremely hot!\n");
3585 /* recommended action: immediate sleep/hibernate */
3586 break;
3587 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3588 printk(TPACPI_CRIT
3589 "THERMAL ALARM: "
3590 "a sensor reports something is too hot!\n");
3591 /* recommended action: warn user through gui, that */
3592 /* some internal component is too hot */
3593 break;
3594 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3595 printk(TPACPI_ALERT
3596 "THERMAL EMERGENCY: "
3597 "a sensor reports something is extremely hot!\n");
3598 /* recommended action: immediate sleep/hibernate */
3599 break;
3600 default:
3601 printk(TPACPI_ALERT
3602 "THERMAL ALERT: unknown thermal alarm received\n");
3603 known = false;
3606 thermal_dump_all_sensors();
3608 return known;
3611 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3613 u32 hkey;
3614 bool send_acpi_ev;
3615 bool ignore_acpi_ev;
3616 bool known_ev;
3618 if (event != 0x80) {
3619 printk(TPACPI_ERR
3620 "unknown HKEY notification event %d\n", event);
3621 /* forward it to userspace, maybe it knows how to handle it */
3622 acpi_bus_generate_netlink_event(
3623 ibm->acpi->device->pnp.device_class,
3624 dev_name(&ibm->acpi->device->dev),
3625 event, 0);
3626 return;
3629 while (1) {
3630 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3631 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3632 return;
3635 if (hkey == 0) {
3636 /* queue empty */
3637 return;
3640 send_acpi_ev = true;
3641 ignore_acpi_ev = false;
3643 switch (hkey >> 12) {
3644 case 1:
3645 /* 0x1000-0x1FFF: key presses */
3646 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3647 &ignore_acpi_ev);
3648 break;
3649 case 2:
3650 /* 0x2000-0x2FFF: Wakeup reason */
3651 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3652 &ignore_acpi_ev);
3653 break;
3654 case 3:
3655 /* 0x3000-0x3FFF: bay-related wakeups */
3656 switch (hkey) {
3657 case TP_HKEY_EV_BAYEJ_ACK:
3658 hotkey_autosleep_ack = 1;
3659 printk(TPACPI_INFO
3660 "bay ejected\n");
3661 hotkey_wakeup_hotunplug_complete_notify_change();
3662 known_ev = true;
3663 break;
3664 case TP_HKEY_EV_OPTDRV_EJ:
3665 /* FIXME: kick libata if SATA link offline */
3666 known_ev = true;
3667 break;
3668 default:
3669 known_ev = false;
3671 break;
3672 case 4:
3673 /* 0x4000-0x4FFF: dock-related wakeups */
3674 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3675 hotkey_autosleep_ack = 1;
3676 printk(TPACPI_INFO
3677 "undocked\n");
3678 hotkey_wakeup_hotunplug_complete_notify_change();
3679 known_ev = true;
3680 } else {
3681 known_ev = false;
3683 break;
3684 case 5:
3685 /* 0x5000-0x5FFF: human interface helpers */
3686 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3687 &ignore_acpi_ev);
3688 break;
3689 case 6:
3690 /* 0x6000-0x6FFF: thermal alarms */
3691 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3692 &ignore_acpi_ev);
3693 break;
3694 case 7:
3695 /* 0x7000-0x7FFF: misc */
3696 if (tp_features.hotkey_wlsw &&
3697 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3698 tpacpi_send_radiosw_update();
3699 send_acpi_ev = 0;
3700 known_ev = true;
3701 break;
3703 /* fallthrough to default */
3704 default:
3705 known_ev = false;
3707 if (!known_ev) {
3708 printk(TPACPI_NOTICE
3709 "unhandled HKEY event 0x%04x\n", hkey);
3710 printk(TPACPI_NOTICE
3711 "please report the conditions when this "
3712 "event happened to %s\n", TPACPI_MAIL);
3715 /* Legacy events */
3716 if (!ignore_acpi_ev &&
3717 (send_acpi_ev || hotkey_report_mode < 2)) {
3718 acpi_bus_generate_proc_event(ibm->acpi->device,
3719 event, hkey);
3722 /* netlink events */
3723 if (!ignore_acpi_ev && send_acpi_ev) {
3724 acpi_bus_generate_netlink_event(
3725 ibm->acpi->device->pnp.device_class,
3726 dev_name(&ibm->acpi->device->dev),
3727 event, hkey);
3732 static void hotkey_suspend(pm_message_t state)
3734 /* Do these on suspend, we get the events on early resume! */
3735 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3736 hotkey_autosleep_ack = 0;
3739 static void hotkey_resume(void)
3741 tpacpi_disable_brightness_delay();
3743 if (hotkey_status_set(true) < 0 ||
3744 hotkey_mask_set(hotkey_acpi_mask) < 0)
3745 printk(TPACPI_ERR
3746 "error while attempting to reset the event "
3747 "firmware interface\n");
3749 tpacpi_send_radiosw_update();
3750 hotkey_tablet_mode_notify_change();
3751 hotkey_wakeup_reason_notify_change();
3752 hotkey_wakeup_hotunplug_complete_notify_change();
3753 hotkey_poll_setup_safe(false);
3756 /* procfs -------------------------------------------------------------- */
3757 static int hotkey_read(char *p)
3759 int res, status;
3760 int len = 0;
3762 if (!tp_features.hotkey) {
3763 len += sprintf(p + len, "status:\t\tnot supported\n");
3764 return len;
3767 if (mutex_lock_killable(&hotkey_mutex))
3768 return -ERESTARTSYS;
3769 res = hotkey_status_get(&status);
3770 if (!res)
3771 res = hotkey_mask_get();
3772 mutex_unlock(&hotkey_mutex);
3773 if (res)
3774 return res;
3776 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3777 if (hotkey_all_mask) {
3778 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3779 len += sprintf(p + len,
3780 "commands:\tenable, disable, reset, <mask>\n");
3781 } else {
3782 len += sprintf(p + len, "mask:\t\tnot supported\n");
3783 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3786 return len;
3789 static void hotkey_enabledisable_warn(bool enable)
3791 tpacpi_log_usertask("procfs hotkey enable/disable");
3792 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3793 TPACPI_WARN
3794 "hotkey enable/disable functionality has been "
3795 "removed from the driver. Hotkeys are always "
3796 "enabled\n"))
3797 printk(TPACPI_ERR
3798 "Please remove the hotkey=enable module "
3799 "parameter, it is deprecated. Hotkeys are always "
3800 "enabled\n");
3803 static int hotkey_write(char *buf)
3805 int res;
3806 u32 mask;
3807 char *cmd;
3809 if (!tp_features.hotkey)
3810 return -ENODEV;
3812 if (mutex_lock_killable(&hotkey_mutex))
3813 return -ERESTARTSYS;
3815 mask = hotkey_user_mask;
3817 res = 0;
3818 while ((cmd = next_cmd(&buf))) {
3819 if (strlencmp(cmd, "enable") == 0) {
3820 hotkey_enabledisable_warn(1);
3821 } else if (strlencmp(cmd, "disable") == 0) {
3822 hotkey_enabledisable_warn(0);
3823 res = -EPERM;
3824 } else if (strlencmp(cmd, "reset") == 0) {
3825 mask = (hotkey_all_mask | hotkey_source_mask)
3826 & ~hotkey_reserved_mask;
3827 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3828 /* mask set */
3829 } else if (sscanf(cmd, "%x", &mask) == 1) {
3830 /* mask set */
3831 } else {
3832 res = -EINVAL;
3833 goto errexit;
3837 if (!res) {
3838 tpacpi_disclose_usertask("procfs hotkey",
3839 "set mask to 0x%08x\n", mask);
3840 res = hotkey_user_mask_set(mask);
3843 errexit:
3844 mutex_unlock(&hotkey_mutex);
3845 return res;
3848 static const struct acpi_device_id ibm_htk_device_ids[] = {
3849 {TPACPI_ACPI_HKEY_HID, 0},
3850 {"", 0},
3853 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3854 .hid = ibm_htk_device_ids,
3855 .notify = hotkey_notify,
3856 .handle = &hkey_handle,
3857 .type = ACPI_DEVICE_NOTIFY,
3860 static struct ibm_struct hotkey_driver_data = {
3861 .name = "hotkey",
3862 .read = hotkey_read,
3863 .write = hotkey_write,
3864 .exit = hotkey_exit,
3865 .resume = hotkey_resume,
3866 .suspend = hotkey_suspend,
3867 .acpi = &ibm_hotkey_acpidriver,
3870 /*************************************************************************
3871 * Bluetooth subdriver
3874 enum {
3875 /* ACPI GBDC/SBDC bits */
3876 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3877 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3878 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3879 0 = disable, 1 = enable */
3882 enum {
3883 /* ACPI \BLTH commands */
3884 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3885 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3886 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3887 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3888 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3891 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3893 static int bluetooth_get_status(void)
3895 int status;
3897 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3898 if (dbg_bluetoothemul)
3899 return (tpacpi_bluetooth_emulstate) ?
3900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3901 #endif
3903 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3904 return -EIO;
3906 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3907 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3910 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3912 int status;
3914 vdbg_printk(TPACPI_DBG_RFKILL,
3915 "will attempt to %s bluetooth\n",
3916 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3918 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3919 if (dbg_bluetoothemul) {
3920 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3921 return 0;
3923 #endif
3925 if (state == TPACPI_RFK_RADIO_ON)
3926 status = TP_ACPI_BLUETOOTH_RADIOSSW
3927 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3928 else
3929 status = 0;
3931 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3932 return -EIO;
3934 return 0;
3937 /* sysfs bluetooth enable ---------------------------------------------- */
3938 static ssize_t bluetooth_enable_show(struct device *dev,
3939 struct device_attribute *attr,
3940 char *buf)
3942 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3943 attr, buf);
3946 static ssize_t bluetooth_enable_store(struct device *dev,
3947 struct device_attribute *attr,
3948 const char *buf, size_t count)
3950 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3951 attr, buf, count);
3954 static struct device_attribute dev_attr_bluetooth_enable =
3955 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3956 bluetooth_enable_show, bluetooth_enable_store);
3958 /* --------------------------------------------------------------------- */
3960 static struct attribute *bluetooth_attributes[] = {
3961 &dev_attr_bluetooth_enable.attr,
3962 NULL
3965 static const struct attribute_group bluetooth_attr_group = {
3966 .attrs = bluetooth_attributes,
3969 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3970 .get_status = bluetooth_get_status,
3971 .set_status = bluetooth_set_status,
3974 static void bluetooth_shutdown(void)
3976 /* Order firmware to save current state to NVRAM */
3977 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3978 TP_ACPI_BLTH_SAVE_STATE))
3979 printk(TPACPI_NOTICE
3980 "failed to save bluetooth state to NVRAM\n");
3981 else
3982 vdbg_printk(TPACPI_DBG_RFKILL,
3983 "bluestooth state saved to NVRAM\n");
3986 static void bluetooth_exit(void)
3988 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3989 &bluetooth_attr_group);
3991 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3993 bluetooth_shutdown();
3996 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3998 int res;
3999 int status = 0;
4001 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4002 "initializing bluetooth subdriver\n");
4004 TPACPI_ACPIHANDLE_INIT(hkey);
4006 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4007 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4008 tp_features.bluetooth = hkey_handle &&
4009 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4011 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4012 "bluetooth is %s, status 0x%02x\n",
4013 str_supported(tp_features.bluetooth),
4014 status);
4016 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4017 if (dbg_bluetoothemul) {
4018 tp_features.bluetooth = 1;
4019 printk(TPACPI_INFO
4020 "bluetooth switch emulation enabled\n");
4021 } else
4022 #endif
4023 if (tp_features.bluetooth &&
4024 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4025 /* no bluetooth hardware present in system */
4026 tp_features.bluetooth = 0;
4027 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4028 "bluetooth hardware not installed\n");
4031 if (!tp_features.bluetooth)
4032 return 1;
4034 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4035 &bluetooth_tprfk_ops,
4036 RFKILL_TYPE_BLUETOOTH,
4037 TPACPI_RFK_BLUETOOTH_SW_NAME,
4038 true);
4039 if (res)
4040 return res;
4042 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4043 &bluetooth_attr_group);
4044 if (res) {
4045 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4046 return res;
4049 return 0;
4052 /* procfs -------------------------------------------------------------- */
4053 static int bluetooth_read(char *p)
4055 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
4058 static int bluetooth_write(char *buf)
4060 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4063 static struct ibm_struct bluetooth_driver_data = {
4064 .name = "bluetooth",
4065 .read = bluetooth_read,
4066 .write = bluetooth_write,
4067 .exit = bluetooth_exit,
4068 .shutdown = bluetooth_shutdown,
4071 /*************************************************************************
4072 * Wan subdriver
4075 enum {
4076 /* ACPI GWAN/SWAN bits */
4077 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4078 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4079 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4080 0 = disable, 1 = enable */
4083 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4085 static int wan_get_status(void)
4087 int status;
4089 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4090 if (dbg_wwanemul)
4091 return (tpacpi_wwan_emulstate) ?
4092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4093 #endif
4095 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4096 return -EIO;
4098 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4099 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4102 static int wan_set_status(enum tpacpi_rfkill_state state)
4104 int status;
4106 vdbg_printk(TPACPI_DBG_RFKILL,
4107 "will attempt to %s wwan\n",
4108 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4110 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4111 if (dbg_wwanemul) {
4112 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4113 return 0;
4115 #endif
4117 if (state == TPACPI_RFK_RADIO_ON)
4118 status = TP_ACPI_WANCARD_RADIOSSW
4119 | TP_ACPI_WANCARD_RESUMECTRL;
4120 else
4121 status = 0;
4123 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4124 return -EIO;
4126 return 0;
4129 /* sysfs wan enable ---------------------------------------------------- */
4130 static ssize_t wan_enable_show(struct device *dev,
4131 struct device_attribute *attr,
4132 char *buf)
4134 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf);
4138 static ssize_t wan_enable_store(struct device *dev,
4139 struct device_attribute *attr,
4140 const char *buf, size_t count)
4142 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4143 attr, buf, count);
4146 static struct device_attribute dev_attr_wan_enable =
4147 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4148 wan_enable_show, wan_enable_store);
4150 /* --------------------------------------------------------------------- */
4152 static struct attribute *wan_attributes[] = {
4153 &dev_attr_wan_enable.attr,
4154 NULL
4157 static const struct attribute_group wan_attr_group = {
4158 .attrs = wan_attributes,
4161 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4162 .get_status = wan_get_status,
4163 .set_status = wan_set_status,
4166 static void wan_shutdown(void)
4168 /* Order firmware to save current state to NVRAM */
4169 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4170 TP_ACPI_WGSV_SAVE_STATE))
4171 printk(TPACPI_NOTICE
4172 "failed to save WWAN state to NVRAM\n");
4173 else
4174 vdbg_printk(TPACPI_DBG_RFKILL,
4175 "WWAN state saved to NVRAM\n");
4178 static void wan_exit(void)
4180 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4181 &wan_attr_group);
4183 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4185 wan_shutdown();
4188 static int __init wan_init(struct ibm_init_struct *iibm)
4190 int res;
4191 int status = 0;
4193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "initializing wan subdriver\n");
4196 TPACPI_ACPIHANDLE_INIT(hkey);
4198 tp_features.wan = hkey_handle &&
4199 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4201 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4202 "wan is %s, status 0x%02x\n",
4203 str_supported(tp_features.wan),
4204 status);
4206 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4207 if (dbg_wwanemul) {
4208 tp_features.wan = 1;
4209 printk(TPACPI_INFO
4210 "wwan switch emulation enabled\n");
4211 } else
4212 #endif
4213 if (tp_features.wan &&
4214 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4215 /* no wan hardware present in system */
4216 tp_features.wan = 0;
4217 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4218 "wan hardware not installed\n");
4221 if (!tp_features.wan)
4222 return 1;
4224 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4225 &wan_tprfk_ops,
4226 RFKILL_TYPE_WWAN,
4227 TPACPI_RFK_WWAN_SW_NAME,
4228 true);
4229 if (res)
4230 return res;
4232 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4233 &wan_attr_group);
4235 if (res) {
4236 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4237 return res;
4240 return 0;
4243 /* procfs -------------------------------------------------------------- */
4244 static int wan_read(char *p)
4246 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
4249 static int wan_write(char *buf)
4251 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4254 static struct ibm_struct wan_driver_data = {
4255 .name = "wan",
4256 .read = wan_read,
4257 .write = wan_write,
4258 .exit = wan_exit,
4259 .shutdown = wan_shutdown,
4262 /*************************************************************************
4263 * UWB subdriver
4266 enum {
4267 /* ACPI GUWB/SUWB bits */
4268 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4269 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4272 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4274 static int uwb_get_status(void)
4276 int status;
4278 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4279 if (dbg_uwbemul)
4280 return (tpacpi_uwb_emulstate) ?
4281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4282 #endif
4284 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4285 return -EIO;
4287 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4288 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4291 static int uwb_set_status(enum tpacpi_rfkill_state state)
4293 int status;
4295 vdbg_printk(TPACPI_DBG_RFKILL,
4296 "will attempt to %s UWB\n",
4297 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4299 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4300 if (dbg_uwbemul) {
4301 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4302 return 0;
4304 #endif
4306 if (state == TPACPI_RFK_RADIO_ON)
4307 status = TP_ACPI_UWB_RADIOSSW;
4308 else
4309 status = 0;
4311 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4312 return -EIO;
4314 return 0;
4317 /* --------------------------------------------------------------------- */
4319 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4320 .get_status = uwb_get_status,
4321 .set_status = uwb_set_status,
4324 static void uwb_exit(void)
4326 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4329 static int __init uwb_init(struct ibm_init_struct *iibm)
4331 int res;
4332 int status = 0;
4334 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4335 "initializing uwb subdriver\n");
4337 TPACPI_ACPIHANDLE_INIT(hkey);
4339 tp_features.uwb = hkey_handle &&
4340 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4342 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4343 "uwb is %s, status 0x%02x\n",
4344 str_supported(tp_features.uwb),
4345 status);
4347 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4348 if (dbg_uwbemul) {
4349 tp_features.uwb = 1;
4350 printk(TPACPI_INFO
4351 "uwb switch emulation enabled\n");
4352 } else
4353 #endif
4354 if (tp_features.uwb &&
4355 !(status & TP_ACPI_UWB_HWPRESENT)) {
4356 /* no uwb hardware present in system */
4357 tp_features.uwb = 0;
4358 dbg_printk(TPACPI_DBG_INIT,
4359 "uwb hardware not installed\n");
4362 if (!tp_features.uwb)
4363 return 1;
4365 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4366 &uwb_tprfk_ops,
4367 RFKILL_TYPE_UWB,
4368 TPACPI_RFK_UWB_SW_NAME,
4369 false);
4370 return res;
4373 static struct ibm_struct uwb_driver_data = {
4374 .name = "uwb",
4375 .exit = uwb_exit,
4376 .flags.experimental = 1,
4379 /*************************************************************************
4380 * Video subdriver
4383 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4385 enum video_access_mode {
4386 TPACPI_VIDEO_NONE = 0,
4387 TPACPI_VIDEO_570, /* 570 */
4388 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4389 TPACPI_VIDEO_NEW, /* all others */
4392 enum { /* video status flags, based on VIDEO_570 */
4393 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4394 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4395 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4398 enum { /* TPACPI_VIDEO_570 constants */
4399 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4400 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4401 * video_status_flags */
4402 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4403 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4406 static enum video_access_mode video_supported;
4407 static int video_orig_autosw;
4409 static int video_autosw_get(void);
4410 static int video_autosw_set(int enable);
4412 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4414 static int __init video_init(struct ibm_init_struct *iibm)
4416 int ivga;
4418 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4420 TPACPI_ACPIHANDLE_INIT(vid);
4421 TPACPI_ACPIHANDLE_INIT(vid2);
4423 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4424 /* G41, assume IVGA doesn't change */
4425 vid_handle = vid2_handle;
4427 if (!vid_handle)
4428 /* video switching not supported on R30, R31 */
4429 video_supported = TPACPI_VIDEO_NONE;
4430 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4431 /* 570 */
4432 video_supported = TPACPI_VIDEO_570;
4433 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4434 /* 600e/x, 770e, 770x */
4435 video_supported = TPACPI_VIDEO_770;
4436 else
4437 /* all others */
4438 video_supported = TPACPI_VIDEO_NEW;
4440 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4441 str_supported(video_supported != TPACPI_VIDEO_NONE),
4442 video_supported);
4444 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4447 static void video_exit(void)
4449 dbg_printk(TPACPI_DBG_EXIT,
4450 "restoring original video autoswitch mode\n");
4451 if (video_autosw_set(video_orig_autosw))
4452 printk(TPACPI_ERR "error while trying to restore original "
4453 "video autoswitch mode\n");
4456 static int video_outputsw_get(void)
4458 int status = 0;
4459 int i;
4461 switch (video_supported) {
4462 case TPACPI_VIDEO_570:
4463 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4464 TP_ACPI_VIDEO_570_PHSCMD))
4465 return -EIO;
4466 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4467 break;
4468 case TPACPI_VIDEO_770:
4469 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4470 return -EIO;
4471 if (i)
4472 status |= TP_ACPI_VIDEO_S_LCD;
4473 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
4477 break;
4478 case TPACPI_VIDEO_NEW:
4479 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4480 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4481 return -EIO;
4482 if (i)
4483 status |= TP_ACPI_VIDEO_S_CRT;
4485 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4486 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4487 return -EIO;
4488 if (i)
4489 status |= TP_ACPI_VIDEO_S_LCD;
4490 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4491 return -EIO;
4492 if (i)
4493 status |= TP_ACPI_VIDEO_S_DVI;
4494 break;
4495 default:
4496 return -ENOSYS;
4499 return status;
4502 static int video_outputsw_set(int status)
4504 int autosw;
4505 int res = 0;
4507 switch (video_supported) {
4508 case TPACPI_VIDEO_570:
4509 res = acpi_evalf(NULL, NULL,
4510 "\\_SB.PHS2", "vdd",
4511 TP_ACPI_VIDEO_570_PHS2CMD,
4512 status | TP_ACPI_VIDEO_570_PHS2SET);
4513 break;
4514 case TPACPI_VIDEO_770:
4515 autosw = video_autosw_get();
4516 if (autosw < 0)
4517 return autosw;
4519 res = video_autosw_set(1);
4520 if (res)
4521 return res;
4522 res = acpi_evalf(vid_handle, NULL,
4523 "ASWT", "vdd", status * 0x100, 0);
4524 if (!autosw && video_autosw_set(autosw)) {
4525 printk(TPACPI_ERR
4526 "video auto-switch left enabled due to error\n");
4527 return -EIO;
4529 break;
4530 case TPACPI_VIDEO_NEW:
4531 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4532 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4533 break;
4534 default:
4535 return -ENOSYS;
4538 return (res)? 0 : -EIO;
4541 static int video_autosw_get(void)
4543 int autosw = 0;
4545 switch (video_supported) {
4546 case TPACPI_VIDEO_570:
4547 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4548 return -EIO;
4549 break;
4550 case TPACPI_VIDEO_770:
4551 case TPACPI_VIDEO_NEW:
4552 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4553 return -EIO;
4554 break;
4555 default:
4556 return -ENOSYS;
4559 return autosw & 1;
4562 static int video_autosw_set(int enable)
4564 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4565 return -EIO;
4566 return 0;
4569 static int video_outputsw_cycle(void)
4571 int autosw = video_autosw_get();
4572 int res;
4574 if (autosw < 0)
4575 return autosw;
4577 switch (video_supported) {
4578 case TPACPI_VIDEO_570:
4579 res = video_autosw_set(1);
4580 if (res)
4581 return res;
4582 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4583 break;
4584 case TPACPI_VIDEO_770:
4585 case TPACPI_VIDEO_NEW:
4586 res = video_autosw_set(1);
4587 if (res)
4588 return res;
4589 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4590 break;
4591 default:
4592 return -ENOSYS;
4594 if (!autosw && video_autosw_set(autosw)) {
4595 printk(TPACPI_ERR
4596 "video auto-switch left enabled due to error\n");
4597 return -EIO;
4600 return (res)? 0 : -EIO;
4603 static int video_expand_toggle(void)
4605 switch (video_supported) {
4606 case TPACPI_VIDEO_570:
4607 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4608 0 : -EIO;
4609 case TPACPI_VIDEO_770:
4610 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4611 0 : -EIO;
4612 case TPACPI_VIDEO_NEW:
4613 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4614 0 : -EIO;
4615 default:
4616 return -ENOSYS;
4618 /* not reached */
4621 static int video_read(char *p)
4623 int status, autosw;
4624 int len = 0;
4626 if (video_supported == TPACPI_VIDEO_NONE) {
4627 len += sprintf(p + len, "status:\t\tnot supported\n");
4628 return len;
4631 status = video_outputsw_get();
4632 if (status < 0)
4633 return status;
4635 autosw = video_autosw_get();
4636 if (autosw < 0)
4637 return autosw;
4639 len += sprintf(p + len, "status:\t\tsupported\n");
4640 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4641 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4642 if (video_supported == TPACPI_VIDEO_NEW)
4643 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4644 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4645 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4646 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4647 if (video_supported == TPACPI_VIDEO_NEW)
4648 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4649 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4650 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4652 return len;
4655 static int video_write(char *buf)
4657 char *cmd;
4658 int enable, disable, status;
4659 int res;
4661 if (video_supported == TPACPI_VIDEO_NONE)
4662 return -ENODEV;
4664 enable = 0;
4665 disable = 0;
4667 while ((cmd = next_cmd(&buf))) {
4668 if (strlencmp(cmd, "lcd_enable") == 0) {
4669 enable |= TP_ACPI_VIDEO_S_LCD;
4670 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4671 disable |= TP_ACPI_VIDEO_S_LCD;
4672 } else if (strlencmp(cmd, "crt_enable") == 0) {
4673 enable |= TP_ACPI_VIDEO_S_CRT;
4674 } else if (strlencmp(cmd, "crt_disable") == 0) {
4675 disable |= TP_ACPI_VIDEO_S_CRT;
4676 } else if (video_supported == TPACPI_VIDEO_NEW &&
4677 strlencmp(cmd, "dvi_enable") == 0) {
4678 enable |= TP_ACPI_VIDEO_S_DVI;
4679 } else if (video_supported == TPACPI_VIDEO_NEW &&
4680 strlencmp(cmd, "dvi_disable") == 0) {
4681 disable |= TP_ACPI_VIDEO_S_DVI;
4682 } else if (strlencmp(cmd, "auto_enable") == 0) {
4683 res = video_autosw_set(1);
4684 if (res)
4685 return res;
4686 } else if (strlencmp(cmd, "auto_disable") == 0) {
4687 res = video_autosw_set(0);
4688 if (res)
4689 return res;
4690 } else if (strlencmp(cmd, "video_switch") == 0) {
4691 res = video_outputsw_cycle();
4692 if (res)
4693 return res;
4694 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4695 res = video_expand_toggle();
4696 if (res)
4697 return res;
4698 } else
4699 return -EINVAL;
4702 if (enable || disable) {
4703 status = video_outputsw_get();
4704 if (status < 0)
4705 return status;
4706 res = video_outputsw_set((status & ~disable) | enable);
4707 if (res)
4708 return res;
4711 return 0;
4714 static struct ibm_struct video_driver_data = {
4715 .name = "video",
4716 .read = video_read,
4717 .write = video_write,
4718 .exit = video_exit,
4721 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4723 /*************************************************************************
4724 * Light (thinklight) subdriver
4727 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4728 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4730 static int light_get_status(void)
4732 int status = 0;
4734 if (tp_features.light_status) {
4735 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4736 return -EIO;
4737 return (!!status);
4740 return -ENXIO;
4743 static int light_set_status(int status)
4745 int rc;
4747 if (tp_features.light) {
4748 if (cmos_handle) {
4749 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4750 (status)?
4751 TP_CMOS_THINKLIGHT_ON :
4752 TP_CMOS_THINKLIGHT_OFF);
4753 } else {
4754 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4755 (status)? 1 : 0);
4757 return (rc)? 0 : -EIO;
4760 return -ENXIO;
4763 static void light_set_status_worker(struct work_struct *work)
4765 struct tpacpi_led_classdev *data =
4766 container_of(work, struct tpacpi_led_classdev, work);
4768 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4769 light_set_status((data->new_state != TPACPI_LED_OFF));
4772 static void light_sysfs_set(struct led_classdev *led_cdev,
4773 enum led_brightness brightness)
4775 struct tpacpi_led_classdev *data =
4776 container_of(led_cdev,
4777 struct tpacpi_led_classdev,
4778 led_classdev);
4779 data->new_state = (brightness != LED_OFF) ?
4780 TPACPI_LED_ON : TPACPI_LED_OFF;
4781 queue_work(tpacpi_wq, &data->work);
4784 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4786 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4789 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4790 .led_classdev = {
4791 .name = "tpacpi::thinklight",
4792 .brightness_set = &light_sysfs_set,
4793 .brightness_get = &light_sysfs_get,
4797 static int __init light_init(struct ibm_init_struct *iibm)
4799 int rc;
4801 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4803 TPACPI_ACPIHANDLE_INIT(ledb);
4804 TPACPI_ACPIHANDLE_INIT(lght);
4805 TPACPI_ACPIHANDLE_INIT(cmos);
4806 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4808 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4809 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4811 if (tp_features.light)
4812 /* light status not supported on
4813 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4814 tp_features.light_status =
4815 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4817 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4818 str_supported(tp_features.light),
4819 str_supported(tp_features.light_status));
4821 if (!tp_features.light)
4822 return 1;
4824 rc = led_classdev_register(&tpacpi_pdev->dev,
4825 &tpacpi_led_thinklight.led_classdev);
4827 if (rc < 0) {
4828 tp_features.light = 0;
4829 tp_features.light_status = 0;
4830 } else {
4831 rc = 0;
4834 return rc;
4837 static void light_exit(void)
4839 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4840 if (work_pending(&tpacpi_led_thinklight.work))
4841 flush_workqueue(tpacpi_wq);
4844 static int light_read(char *p)
4846 int len = 0;
4847 int status;
4849 if (!tp_features.light) {
4850 len += sprintf(p + len, "status:\t\tnot supported\n");
4851 } else if (!tp_features.light_status) {
4852 len += sprintf(p + len, "status:\t\tunknown\n");
4853 len += sprintf(p + len, "commands:\ton, off\n");
4854 } else {
4855 status = light_get_status();
4856 if (status < 0)
4857 return status;
4858 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4859 len += sprintf(p + len, "commands:\ton, off\n");
4862 return len;
4865 static int light_write(char *buf)
4867 char *cmd;
4868 int newstatus = 0;
4870 if (!tp_features.light)
4871 return -ENODEV;
4873 while ((cmd = next_cmd(&buf))) {
4874 if (strlencmp(cmd, "on") == 0) {
4875 newstatus = 1;
4876 } else if (strlencmp(cmd, "off") == 0) {
4877 newstatus = 0;
4878 } else
4879 return -EINVAL;
4882 return light_set_status(newstatus);
4885 static struct ibm_struct light_driver_data = {
4886 .name = "light",
4887 .read = light_read,
4888 .write = light_write,
4889 .exit = light_exit,
4892 /*************************************************************************
4893 * CMOS subdriver
4896 /* sysfs cmos_command -------------------------------------------------- */
4897 static ssize_t cmos_command_store(struct device *dev,
4898 struct device_attribute *attr,
4899 const char *buf, size_t count)
4901 unsigned long cmos_cmd;
4902 int res;
4904 if (parse_strtoul(buf, 21, &cmos_cmd))
4905 return -EINVAL;
4907 res = issue_thinkpad_cmos_command(cmos_cmd);
4908 return (res)? res : count;
4911 static struct device_attribute dev_attr_cmos_command =
4912 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4914 /* --------------------------------------------------------------------- */
4916 static int __init cmos_init(struct ibm_init_struct *iibm)
4918 int res;
4920 vdbg_printk(TPACPI_DBG_INIT,
4921 "initializing cmos commands subdriver\n");
4923 TPACPI_ACPIHANDLE_INIT(cmos);
4925 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4926 str_supported(cmos_handle != NULL));
4928 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4929 if (res)
4930 return res;
4932 return (cmos_handle)? 0 : 1;
4935 static void cmos_exit(void)
4937 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4940 static int cmos_read(char *p)
4942 int len = 0;
4944 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4945 R30, R31, T20-22, X20-21 */
4946 if (!cmos_handle)
4947 len += sprintf(p + len, "status:\t\tnot supported\n");
4948 else {
4949 len += sprintf(p + len, "status:\t\tsupported\n");
4950 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4953 return len;
4956 static int cmos_write(char *buf)
4958 char *cmd;
4959 int cmos_cmd, res;
4961 while ((cmd = next_cmd(&buf))) {
4962 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4963 cmos_cmd >= 0 && cmos_cmd <= 21) {
4964 /* cmos_cmd set */
4965 } else
4966 return -EINVAL;
4968 res = issue_thinkpad_cmos_command(cmos_cmd);
4969 if (res)
4970 return res;
4973 return 0;
4976 static struct ibm_struct cmos_driver_data = {
4977 .name = "cmos",
4978 .read = cmos_read,
4979 .write = cmos_write,
4980 .exit = cmos_exit,
4983 /*************************************************************************
4984 * LED subdriver
4987 enum led_access_mode {
4988 TPACPI_LED_NONE = 0,
4989 TPACPI_LED_570, /* 570 */
4990 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4991 TPACPI_LED_NEW, /* all others */
4994 enum { /* For TPACPI_LED_OLD */
4995 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4996 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4997 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5000 static enum led_access_mode led_supported;
5002 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5003 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5004 /* T20-22, X20-21 */
5005 "LED", /* all others */
5006 ); /* R30, R31 */
5008 #define TPACPI_LED_NUMLEDS 16
5009 static struct tpacpi_led_classdev *tpacpi_leds;
5010 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5011 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5012 /* there's a limit of 19 chars + NULL before 2.6.26 */
5013 "tpacpi::power",
5014 "tpacpi:orange:batt",
5015 "tpacpi:green:batt",
5016 "tpacpi::dock_active",
5017 "tpacpi::bay_active",
5018 "tpacpi::dock_batt",
5019 "tpacpi::unknown_led",
5020 "tpacpi::standby",
5021 "tpacpi::dock_status1",
5022 "tpacpi::dock_status2",
5023 "tpacpi::unknown_led2",
5024 "tpacpi::unknown_led3",
5025 "tpacpi::thinkvantage",
5027 #define TPACPI_SAFE_LEDS 0x1081U
5029 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5031 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5032 return false;
5033 #else
5034 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5035 #endif
5038 static int led_get_status(const unsigned int led)
5040 int status;
5041 enum led_status_t led_s;
5043 switch (led_supported) {
5044 case TPACPI_LED_570:
5045 if (!acpi_evalf(ec_handle,
5046 &status, "GLED", "dd", 1 << led))
5047 return -EIO;
5048 led_s = (status == 0)?
5049 TPACPI_LED_OFF :
5050 ((status == 1)?
5051 TPACPI_LED_ON :
5052 TPACPI_LED_BLINK);
5053 tpacpi_led_state_cache[led] = led_s;
5054 return led_s;
5055 default:
5056 return -ENXIO;
5059 /* not reached */
5062 static int led_set_status(const unsigned int led,
5063 const enum led_status_t ledstatus)
5065 /* off, on, blink. Index is led_status_t */
5066 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5067 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5069 int rc = 0;
5071 switch (led_supported) {
5072 case TPACPI_LED_570:
5073 /* 570 */
5074 if (unlikely(led > 7))
5075 return -EINVAL;
5076 if (unlikely(tpacpi_is_led_restricted(led)))
5077 return -EPERM;
5078 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5079 (1 << led), led_sled_arg1[ledstatus]))
5080 rc = -EIO;
5081 break;
5082 case TPACPI_LED_OLD:
5083 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5084 if (unlikely(led > 7))
5085 return -EINVAL;
5086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
5088 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5089 if (rc >= 0)
5090 rc = ec_write(TPACPI_LED_EC_HLBL,
5091 (ledstatus == TPACPI_LED_BLINK) << led);
5092 if (rc >= 0)
5093 rc = ec_write(TPACPI_LED_EC_HLCL,
5094 (ledstatus != TPACPI_LED_OFF) << led);
5095 break;
5096 case TPACPI_LED_NEW:
5097 /* all others */
5098 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5099 return -EINVAL;
5100 if (unlikely(tpacpi_is_led_restricted(led)))
5101 return -EPERM;
5102 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5103 led, led_led_arg1[ledstatus]))
5104 rc = -EIO;
5105 break;
5106 default:
5107 rc = -ENXIO;
5110 if (!rc)
5111 tpacpi_led_state_cache[led] = ledstatus;
5113 return rc;
5116 static void led_set_status_worker(struct work_struct *work)
5118 struct tpacpi_led_classdev *data =
5119 container_of(work, struct tpacpi_led_classdev, work);
5121 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5122 led_set_status(data->led, data->new_state);
5125 static void led_sysfs_set(struct led_classdev *led_cdev,
5126 enum led_brightness brightness)
5128 struct tpacpi_led_classdev *data = container_of(led_cdev,
5129 struct tpacpi_led_classdev, led_classdev);
5131 if (brightness == LED_OFF)
5132 data->new_state = TPACPI_LED_OFF;
5133 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5134 data->new_state = TPACPI_LED_ON;
5135 else
5136 data->new_state = TPACPI_LED_BLINK;
5138 queue_work(tpacpi_wq, &data->work);
5141 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5142 unsigned long *delay_on, unsigned long *delay_off)
5144 struct tpacpi_led_classdev *data = container_of(led_cdev,
5145 struct tpacpi_led_classdev, led_classdev);
5147 /* Can we choose the flash rate? */
5148 if (*delay_on == 0 && *delay_off == 0) {
5149 /* yes. set them to the hardware blink rate (1 Hz) */
5150 *delay_on = 500; /* ms */
5151 *delay_off = 500; /* ms */
5152 } else if ((*delay_on != 500) || (*delay_off != 500))
5153 return -EINVAL;
5155 data->new_state = TPACPI_LED_BLINK;
5156 queue_work(tpacpi_wq, &data->work);
5158 return 0;
5161 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5163 int rc;
5165 struct tpacpi_led_classdev *data = container_of(led_cdev,
5166 struct tpacpi_led_classdev, led_classdev);
5168 rc = led_get_status(data->led);
5170 if (rc == TPACPI_LED_OFF || rc < 0)
5171 rc = LED_OFF; /* no error handling in led class :( */
5172 else
5173 rc = LED_FULL;
5175 return rc;
5178 static void led_exit(void)
5180 unsigned int i;
5182 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5183 if (tpacpi_leds[i].led_classdev.name)
5184 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5187 kfree(tpacpi_leds);
5190 static int __init tpacpi_init_led(unsigned int led)
5192 int rc;
5194 tpacpi_leds[led].led = led;
5196 /* LEDs with no name don't get registered */
5197 if (!tpacpi_led_names[led])
5198 return 0;
5200 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5201 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5202 if (led_supported == TPACPI_LED_570)
5203 tpacpi_leds[led].led_classdev.brightness_get =
5204 &led_sysfs_get;
5206 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5208 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5210 rc = led_classdev_register(&tpacpi_pdev->dev,
5211 &tpacpi_leds[led].led_classdev);
5212 if (rc < 0)
5213 tpacpi_leds[led].led_classdev.name = NULL;
5215 return rc;
5218 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5219 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5220 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5221 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5223 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5224 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5225 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5226 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5227 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5228 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5229 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5230 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5232 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5233 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5234 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5235 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5236 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5238 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5239 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5240 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5241 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5243 /* (1) - may have excess leds enabled on MSB */
5245 /* Defaults (order matters, keep last, don't reorder!) */
5246 { /* Lenovo */
5247 .vendor = PCI_VENDOR_ID_LENOVO,
5248 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5249 .quirks = 0x1fffU,
5251 { /* IBM ThinkPads with no EC version string */
5252 .vendor = PCI_VENDOR_ID_IBM,
5253 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5254 .quirks = 0x00ffU,
5256 { /* IBM ThinkPads with EC version string */
5257 .vendor = PCI_VENDOR_ID_IBM,
5258 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5259 .quirks = 0x00bfU,
5263 #undef TPACPI_LEDQ_IBM
5264 #undef TPACPI_LEDQ_LNV
5266 static int __init led_init(struct ibm_init_struct *iibm)
5268 unsigned int i;
5269 int rc;
5270 unsigned long useful_leds;
5272 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5274 TPACPI_ACPIHANDLE_INIT(led);
5276 if (!led_handle)
5277 /* led not supported on R30, R31 */
5278 led_supported = TPACPI_LED_NONE;
5279 else if (strlencmp(led_path, "SLED") == 0)
5280 /* 570 */
5281 led_supported = TPACPI_LED_570;
5282 else if (strlencmp(led_path, "SYSL") == 0)
5283 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5284 led_supported = TPACPI_LED_OLD;
5285 else
5286 /* all others */
5287 led_supported = TPACPI_LED_NEW;
5289 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5290 str_supported(led_supported), led_supported);
5292 if (led_supported == TPACPI_LED_NONE)
5293 return 1;
5295 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5296 GFP_KERNEL);
5297 if (!tpacpi_leds) {
5298 printk(TPACPI_ERR "Out of memory for LED data\n");
5299 return -ENOMEM;
5302 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5303 ARRAY_SIZE(led_useful_qtable));
5305 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5306 if (!tpacpi_is_led_restricted(i) &&
5307 test_bit(i, &useful_leds)) {
5308 rc = tpacpi_init_led(i);
5309 if (rc < 0) {
5310 led_exit();
5311 return rc;
5316 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5317 printk(TPACPI_NOTICE
5318 "warning: userspace override of important "
5319 "firmware LEDs is enabled\n");
5320 #endif
5321 return 0;
5324 #define str_led_status(s) \
5325 ((s) == TPACPI_LED_OFF ? "off" : \
5326 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5328 static int led_read(char *p)
5330 int len = 0;
5332 if (!led_supported) {
5333 len += sprintf(p + len, "status:\t\tnot supported\n");
5334 return len;
5336 len += sprintf(p + len, "status:\t\tsupported\n");
5338 if (led_supported == TPACPI_LED_570) {
5339 /* 570 */
5340 int i, status;
5341 for (i = 0; i < 8; i++) {
5342 status = led_get_status(i);
5343 if (status < 0)
5344 return -EIO;
5345 len += sprintf(p + len, "%d:\t\t%s\n",
5346 i, str_led_status(status));
5350 len += sprintf(p + len, "commands:\t"
5351 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5353 return len;
5356 static int led_write(char *buf)
5358 char *cmd;
5359 int led, rc;
5360 enum led_status_t s;
5362 if (!led_supported)
5363 return -ENODEV;
5365 while ((cmd = next_cmd(&buf))) {
5366 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5367 return -EINVAL;
5369 if (strstr(cmd, "off")) {
5370 s = TPACPI_LED_OFF;
5371 } else if (strstr(cmd, "on")) {
5372 s = TPACPI_LED_ON;
5373 } else if (strstr(cmd, "blink")) {
5374 s = TPACPI_LED_BLINK;
5375 } else {
5376 return -EINVAL;
5379 rc = led_set_status(led, s);
5380 if (rc < 0)
5381 return rc;
5384 return 0;
5387 static struct ibm_struct led_driver_data = {
5388 .name = "led",
5389 .read = led_read,
5390 .write = led_write,
5391 .exit = led_exit,
5394 /*************************************************************************
5395 * Beep subdriver
5398 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5400 #define TPACPI_BEEP_Q1 0x0001
5402 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5403 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5404 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5407 static int __init beep_init(struct ibm_init_struct *iibm)
5409 unsigned long quirks;
5411 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5413 TPACPI_ACPIHANDLE_INIT(beep);
5415 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5416 str_supported(beep_handle != NULL));
5418 quirks = tpacpi_check_quirks(beep_quirk_table,
5419 ARRAY_SIZE(beep_quirk_table));
5421 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5423 return (beep_handle)? 0 : 1;
5426 static int beep_read(char *p)
5428 int len = 0;
5430 if (!beep_handle)
5431 len += sprintf(p + len, "status:\t\tnot supported\n");
5432 else {
5433 len += sprintf(p + len, "status:\t\tsupported\n");
5434 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5437 return len;
5440 static int beep_write(char *buf)
5442 char *cmd;
5443 int beep_cmd;
5445 if (!beep_handle)
5446 return -ENODEV;
5448 while ((cmd = next_cmd(&buf))) {
5449 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5450 beep_cmd >= 0 && beep_cmd <= 17) {
5451 /* beep_cmd set */
5452 } else
5453 return -EINVAL;
5454 if (tp_features.beep_needs_two_args) {
5455 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5456 beep_cmd, 0))
5457 return -EIO;
5458 } else {
5459 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5460 beep_cmd))
5461 return -EIO;
5465 return 0;
5468 static struct ibm_struct beep_driver_data = {
5469 .name = "beep",
5470 .read = beep_read,
5471 .write = beep_write,
5474 /*************************************************************************
5475 * Thermal subdriver
5478 enum thermal_access_mode {
5479 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5480 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5481 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5482 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5483 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5486 enum { /* TPACPI_THERMAL_TPEC_* */
5487 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5488 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5489 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5491 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5495 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5496 struct ibm_thermal_sensors_struct {
5497 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5500 static enum thermal_access_mode thermal_read_mode;
5502 /* idx is zero-based */
5503 static int thermal_get_sensor(int idx, s32 *value)
5505 int t;
5506 s8 tmp;
5507 char tmpi[5];
5509 t = TP_EC_THERMAL_TMP0;
5511 switch (thermal_read_mode) {
5512 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5513 case TPACPI_THERMAL_TPEC_16:
5514 if (idx >= 8 && idx <= 15) {
5515 t = TP_EC_THERMAL_TMP8;
5516 idx -= 8;
5518 /* fallthrough */
5519 #endif
5520 case TPACPI_THERMAL_TPEC_8:
5521 if (idx <= 7) {
5522 if (!acpi_ec_read(t + idx, &tmp))
5523 return -EIO;
5524 *value = tmp * 1000;
5525 return 0;
5527 break;
5529 case TPACPI_THERMAL_ACPI_UPDT:
5530 if (idx <= 7) {
5531 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5532 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5533 return -EIO;
5534 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5535 return -EIO;
5536 *value = (t - 2732) * 100;
5537 return 0;
5539 break;
5541 case TPACPI_THERMAL_ACPI_TMP07:
5542 if (idx <= 7) {
5543 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5544 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5545 return -EIO;
5546 if (t > 127 || t < -127)
5547 t = TP_EC_THERMAL_TMP_NA;
5548 *value = t * 1000;
5549 return 0;
5551 break;
5553 case TPACPI_THERMAL_NONE:
5554 default:
5555 return -ENOSYS;
5558 return -EINVAL;
5561 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5563 int res, i;
5564 int n;
5566 n = 8;
5567 i = 0;
5569 if (!s)
5570 return -EINVAL;
5572 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5573 n = 16;
5575 for (i = 0 ; i < n; i++) {
5576 res = thermal_get_sensor(i, &s->temp[i]);
5577 if (res)
5578 return res;
5581 return n;
5584 static void thermal_dump_all_sensors(void)
5586 int n, i;
5587 struct ibm_thermal_sensors_struct t;
5589 n = thermal_get_sensors(&t);
5590 if (n <= 0)
5591 return;
5593 printk(TPACPI_NOTICE
5594 "temperatures (Celsius):");
5596 for (i = 0; i < n; i++) {
5597 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5598 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5599 else
5600 printk(KERN_CONT " N/A");
5603 printk(KERN_CONT "\n");
5606 /* sysfs temp##_input -------------------------------------------------- */
5608 static ssize_t thermal_temp_input_show(struct device *dev,
5609 struct device_attribute *attr,
5610 char *buf)
5612 struct sensor_device_attribute *sensor_attr =
5613 to_sensor_dev_attr(attr);
5614 int idx = sensor_attr->index;
5615 s32 value;
5616 int res;
5618 res = thermal_get_sensor(idx, &value);
5619 if (res)
5620 return res;
5621 if (value == TPACPI_THERMAL_SENSOR_NA)
5622 return -ENXIO;
5624 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5627 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5628 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5629 thermal_temp_input_show, NULL, _idxB)
5631 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5632 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5633 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5634 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5635 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5636 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5637 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5638 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5639 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5640 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5641 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5642 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5643 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5644 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5645 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5646 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5647 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5650 #define THERMAL_ATTRS(X) \
5651 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5653 static struct attribute *thermal_temp_input_attr[] = {
5654 THERMAL_ATTRS(8),
5655 THERMAL_ATTRS(9),
5656 THERMAL_ATTRS(10),
5657 THERMAL_ATTRS(11),
5658 THERMAL_ATTRS(12),
5659 THERMAL_ATTRS(13),
5660 THERMAL_ATTRS(14),
5661 THERMAL_ATTRS(15),
5662 THERMAL_ATTRS(0),
5663 THERMAL_ATTRS(1),
5664 THERMAL_ATTRS(2),
5665 THERMAL_ATTRS(3),
5666 THERMAL_ATTRS(4),
5667 THERMAL_ATTRS(5),
5668 THERMAL_ATTRS(6),
5669 THERMAL_ATTRS(7),
5670 NULL
5673 static const struct attribute_group thermal_temp_input16_group = {
5674 .attrs = thermal_temp_input_attr
5677 static const struct attribute_group thermal_temp_input8_group = {
5678 .attrs = &thermal_temp_input_attr[8]
5681 #undef THERMAL_SENSOR_ATTR_TEMP
5682 #undef THERMAL_ATTRS
5684 /* --------------------------------------------------------------------- */
5686 static int __init thermal_init(struct ibm_init_struct *iibm)
5688 u8 t, ta1, ta2;
5689 int i;
5690 int acpi_tmp7;
5691 int res;
5693 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5695 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5697 if (thinkpad_id.ec_model) {
5699 * Direct EC access mode: sensors at registers
5700 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5701 * non-implemented, thermal sensors return 0x80 when
5702 * not available
5705 ta1 = ta2 = 0;
5706 for (i = 0; i < 8; i++) {
5707 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5708 ta1 |= t;
5709 } else {
5710 ta1 = 0;
5711 break;
5713 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5714 ta2 |= t;
5715 } else {
5716 ta1 = 0;
5717 break;
5720 if (ta1 == 0) {
5721 /* This is sheer paranoia, but we handle it anyway */
5722 if (acpi_tmp7) {
5723 printk(TPACPI_ERR
5724 "ThinkPad ACPI EC access misbehaving, "
5725 "falling back to ACPI TMPx access "
5726 "mode\n");
5727 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5728 } else {
5729 printk(TPACPI_ERR
5730 "ThinkPad ACPI EC access misbehaving, "
5731 "disabling thermal sensors access\n");
5732 thermal_read_mode = TPACPI_THERMAL_NONE;
5734 } else {
5735 thermal_read_mode =
5736 (ta2 != 0) ?
5737 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5739 } else if (acpi_tmp7) {
5740 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5741 /* 600e/x, 770e, 770x */
5742 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5743 } else {
5744 /* Standard ACPI TMPx access, max 8 sensors */
5745 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5747 } else {
5748 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5749 thermal_read_mode = TPACPI_THERMAL_NONE;
5752 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5753 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5754 thermal_read_mode);
5756 switch (thermal_read_mode) {
5757 case TPACPI_THERMAL_TPEC_16:
5758 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5759 &thermal_temp_input16_group);
5760 if (res)
5761 return res;
5762 break;
5763 case TPACPI_THERMAL_TPEC_8:
5764 case TPACPI_THERMAL_ACPI_TMP07:
5765 case TPACPI_THERMAL_ACPI_UPDT:
5766 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5767 &thermal_temp_input8_group);
5768 if (res)
5769 return res;
5770 break;
5771 case TPACPI_THERMAL_NONE:
5772 default:
5773 return 1;
5776 return 0;
5779 static void thermal_exit(void)
5781 switch (thermal_read_mode) {
5782 case TPACPI_THERMAL_TPEC_16:
5783 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5784 &thermal_temp_input16_group);
5785 break;
5786 case TPACPI_THERMAL_TPEC_8:
5787 case TPACPI_THERMAL_ACPI_TMP07:
5788 case TPACPI_THERMAL_ACPI_UPDT:
5789 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5790 &thermal_temp_input8_group);
5791 break;
5792 case TPACPI_THERMAL_NONE:
5793 default:
5794 break;
5798 static int thermal_read(char *p)
5800 int len = 0;
5801 int n, i;
5802 struct ibm_thermal_sensors_struct t;
5804 n = thermal_get_sensors(&t);
5805 if (unlikely(n < 0))
5806 return n;
5808 len += sprintf(p + len, "temperatures:\t");
5810 if (n > 0) {
5811 for (i = 0; i < (n - 1); i++)
5812 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5813 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5814 } else
5815 len += sprintf(p + len, "not supported\n");
5817 return len;
5820 static struct ibm_struct thermal_driver_data = {
5821 .name = "thermal",
5822 .read = thermal_read,
5823 .exit = thermal_exit,
5826 /*************************************************************************
5827 * EC Dump subdriver
5830 static u8 ecdump_regs[256];
5832 static int ecdump_read(char *p)
5834 int len = 0;
5835 int i, j;
5836 u8 v;
5838 len += sprintf(p + len, "EC "
5839 " +00 +01 +02 +03 +04 +05 +06 +07"
5840 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5841 for (i = 0; i < 256; i += 16) {
5842 len += sprintf(p + len, "EC 0x%02x:", i);
5843 for (j = 0; j < 16; j++) {
5844 if (!acpi_ec_read(i + j, &v))
5845 break;
5846 if (v != ecdump_regs[i + j])
5847 len += sprintf(p + len, " *%02x", v);
5848 else
5849 len += sprintf(p + len, " %02x", v);
5850 ecdump_regs[i + j] = v;
5852 len += sprintf(p + len, "\n");
5853 if (j != 16)
5854 break;
5857 /* These are way too dangerous to advertise openly... */
5858 #if 0
5859 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5860 " (<offset> is 00-ff, <value> is 00-ff)\n");
5861 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5862 " (<offset> is 00-ff, <value> is 0-255)\n");
5863 #endif
5864 return len;
5867 static int ecdump_write(char *buf)
5869 char *cmd;
5870 int i, v;
5872 while ((cmd = next_cmd(&buf))) {
5873 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5874 /* i and v set */
5875 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5876 /* i and v set */
5877 } else
5878 return -EINVAL;
5879 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5880 if (!acpi_ec_write(i, v))
5881 return -EIO;
5882 } else
5883 return -EINVAL;
5886 return 0;
5889 static struct ibm_struct ecdump_driver_data = {
5890 .name = "ecdump",
5891 .read = ecdump_read,
5892 .write = ecdump_write,
5893 .flags.experimental = 1,
5896 /*************************************************************************
5897 * Backlight/brightness subdriver
5900 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5903 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5904 * CMOS NVRAM byte 0x5E, bits 0-3.
5906 * EC HBRV (0x31) has the following layout
5907 * Bit 7: unknown function
5908 * Bit 6: unknown function
5909 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5910 * Bit 4: must be set to zero to avoid problems
5911 * Bit 3-0: backlight brightness level
5913 * brightness_get_raw returns status data in the HBRV layout
5915 * WARNING: The X61 has been verified to use HBRV for something else, so
5916 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5917 * testing on the very early *60 Lenovo models...
5920 enum {
5921 TP_EC_BACKLIGHT = 0x31,
5923 /* TP_EC_BACKLIGHT bitmasks */
5924 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5925 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5926 TP_EC_BACKLIGHT_MAPSW = 0x20,
5929 enum tpacpi_brightness_access_mode {
5930 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5931 TPACPI_BRGHT_MODE_EC, /* EC control */
5932 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5933 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5934 TPACPI_BRGHT_MODE_MAX
5937 static struct backlight_device *ibm_backlight_device;
5939 static enum tpacpi_brightness_access_mode brightness_mode =
5940 TPACPI_BRGHT_MODE_MAX;
5942 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5944 static struct mutex brightness_mutex;
5946 /* NVRAM brightness access,
5947 * call with brightness_mutex held! */
5948 static unsigned int tpacpi_brightness_nvram_get(void)
5950 u8 lnvram;
5952 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5953 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5954 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5955 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5957 return lnvram;
5960 static void tpacpi_brightness_checkpoint_nvram(void)
5962 u8 lec = 0;
5963 u8 b_nvram;
5965 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5966 return;
5968 vdbg_printk(TPACPI_DBG_BRGHT,
5969 "trying to checkpoint backlight level to NVRAM...\n");
5971 if (mutex_lock_killable(&brightness_mutex) < 0)
5972 return;
5974 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5975 goto unlock;
5976 lec &= TP_EC_BACKLIGHT_LVLMSK;
5977 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5979 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5980 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5981 /* NVRAM needs update */
5982 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5983 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5984 b_nvram |= lec;
5985 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5986 dbg_printk(TPACPI_DBG_BRGHT,
5987 "updated NVRAM backlight level to %u (0x%02x)\n",
5988 (unsigned int) lec, (unsigned int) b_nvram);
5989 } else
5990 vdbg_printk(TPACPI_DBG_BRGHT,
5991 "NVRAM backlight level already is %u (0x%02x)\n",
5992 (unsigned int) lec, (unsigned int) b_nvram);
5994 unlock:
5995 mutex_unlock(&brightness_mutex);
5999 /* call with brightness_mutex held! */
6000 static int tpacpi_brightness_get_raw(int *status)
6002 u8 lec = 0;
6004 switch (brightness_mode) {
6005 case TPACPI_BRGHT_MODE_UCMS_STEP:
6006 *status = tpacpi_brightness_nvram_get();
6007 return 0;
6008 case TPACPI_BRGHT_MODE_EC:
6009 case TPACPI_BRGHT_MODE_ECNVRAM:
6010 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6011 return -EIO;
6012 *status = lec;
6013 return 0;
6014 default:
6015 return -ENXIO;
6019 /* call with brightness_mutex held! */
6020 /* do NOT call with illegal backlight level value */
6021 static int tpacpi_brightness_set_ec(unsigned int value)
6023 u8 lec = 0;
6025 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6026 return -EIO;
6028 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6029 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6030 (value & TP_EC_BACKLIGHT_LVLMSK))))
6031 return -EIO;
6033 return 0;
6036 /* call with brightness_mutex held! */
6037 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6039 int cmos_cmd, inc;
6040 unsigned int current_value, i;
6042 current_value = tpacpi_brightness_nvram_get();
6044 if (value == current_value)
6045 return 0;
6047 cmos_cmd = (value > current_value) ?
6048 TP_CMOS_BRIGHTNESS_UP :
6049 TP_CMOS_BRIGHTNESS_DOWN;
6050 inc = (value > current_value) ? 1 : -1;
6052 for (i = current_value; i != value; i += inc)
6053 if (issue_thinkpad_cmos_command(cmos_cmd))
6054 return -EIO;
6056 return 0;
6059 /* May return EINTR which can always be mapped to ERESTARTSYS */
6060 static int brightness_set(unsigned int value)
6062 int res;
6064 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6065 value < 0)
6066 return -EINVAL;
6068 vdbg_printk(TPACPI_DBG_BRGHT,
6069 "set backlight level to %d\n", value);
6071 res = mutex_lock_killable(&brightness_mutex);
6072 if (res < 0)
6073 return res;
6075 switch (brightness_mode) {
6076 case TPACPI_BRGHT_MODE_EC:
6077 case TPACPI_BRGHT_MODE_ECNVRAM:
6078 res = tpacpi_brightness_set_ec(value);
6079 break;
6080 case TPACPI_BRGHT_MODE_UCMS_STEP:
6081 res = tpacpi_brightness_set_ucmsstep(value);
6082 break;
6083 default:
6084 res = -ENXIO;
6087 mutex_unlock(&brightness_mutex);
6088 return res;
6091 /* sysfs backlight class ----------------------------------------------- */
6093 static int brightness_update_status(struct backlight_device *bd)
6095 unsigned int level =
6096 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6097 bd->props.power == FB_BLANK_UNBLANK) ?
6098 bd->props.brightness : 0;
6100 dbg_printk(TPACPI_DBG_BRGHT,
6101 "backlight: attempt to set level to %d\n",
6102 level);
6104 /* it is the backlight class's job (caller) to handle
6105 * EINTR and other errors properly */
6106 return brightness_set(level);
6109 static int brightness_get(struct backlight_device *bd)
6111 int status, res;
6113 res = mutex_lock_killable(&brightness_mutex);
6114 if (res < 0)
6115 return 0;
6117 res = tpacpi_brightness_get_raw(&status);
6119 mutex_unlock(&brightness_mutex);
6121 if (res < 0)
6122 return 0;
6124 return status & TP_EC_BACKLIGHT_LVLMSK;
6127 static void tpacpi_brightness_notify_change(void)
6129 backlight_force_update(ibm_backlight_device,
6130 BACKLIGHT_UPDATE_HOTKEY);
6133 static struct backlight_ops ibm_backlight_data = {
6134 .get_brightness = brightness_get,
6135 .update_status = brightness_update_status,
6138 /* --------------------------------------------------------------------- */
6141 * These are only useful for models that have only one possibility
6142 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6143 * these quirks.
6145 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6146 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6147 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6149 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6150 /* Models with ATI GPUs known to require ECNVRAM mode */
6151 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6153 /* Models with ATI GPUs that can use ECNVRAM */
6154 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6155 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6156 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6157 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6159 /* Models with Intel Extreme Graphics 2 */
6160 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6161 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6162 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6164 /* Models with Intel GMA900 */
6165 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6166 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6167 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6170 static int __init brightness_init(struct ibm_init_struct *iibm)
6172 int b;
6173 unsigned long quirks;
6175 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6177 mutex_init(&brightness_mutex);
6179 quirks = tpacpi_check_quirks(brightness_quirk_table,
6180 ARRAY_SIZE(brightness_quirk_table));
6183 * We always attempt to detect acpi support, so as to switch
6184 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6185 * going to publish a backlight interface
6187 b = tpacpi_check_std_acpi_brightness_support();
6188 if (b > 0) {
6190 if (acpi_video_backlight_support()) {
6191 if (brightness_enable > 1) {
6192 printk(TPACPI_NOTICE
6193 "Standard ACPI backlight interface "
6194 "available, not loading native one.\n");
6195 return 1;
6196 } else if (brightness_enable == 1) {
6197 printk(TPACPI_NOTICE
6198 "Backlight control force enabled, even if standard "
6199 "ACPI backlight interface is available\n");
6201 } else {
6202 if (brightness_enable > 1) {
6203 printk(TPACPI_NOTICE
6204 "Standard ACPI backlight interface not "
6205 "available, thinkpad_acpi native "
6206 "brightness control enabled\n");
6211 if (!brightness_enable) {
6212 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6213 "brightness support disabled by "
6214 "module parameter\n");
6215 return 1;
6218 if (b > 16) {
6219 printk(TPACPI_ERR
6220 "Unsupported brightness interface, "
6221 "please contact %s\n", TPACPI_MAIL);
6222 return 1;
6224 if (b == 16)
6225 tp_features.bright_16levels = 1;
6228 * Check for module parameter bogosity, note that we
6229 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6230 * able to detect "unspecified"
6232 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6233 return -EINVAL;
6235 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6236 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6237 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6238 if (quirks & TPACPI_BRGHT_Q_EC)
6239 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6240 else
6241 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6243 dbg_printk(TPACPI_DBG_BRGHT,
6244 "driver auto-selected brightness_mode=%d\n",
6245 brightness_mode);
6248 /* Safety */
6249 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6250 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6251 brightness_mode == TPACPI_BRGHT_MODE_EC))
6252 return -EINVAL;
6254 if (tpacpi_brightness_get_raw(&b) < 0)
6255 return 1;
6257 if (tp_features.bright_16levels)
6258 printk(TPACPI_INFO
6259 "detected a 16-level brightness capable ThinkPad\n");
6261 ibm_backlight_device = backlight_device_register(
6262 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6263 &ibm_backlight_data);
6264 if (IS_ERR(ibm_backlight_device)) {
6265 int rc = PTR_ERR(ibm_backlight_device);
6266 ibm_backlight_device = NULL;
6267 printk(TPACPI_ERR "Could not register backlight device\n");
6268 return rc;
6270 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6271 "brightness is supported\n");
6273 if (quirks & TPACPI_BRGHT_Q_ASK) {
6274 printk(TPACPI_NOTICE
6275 "brightness: will use unverified default: "
6276 "brightness_mode=%d\n", brightness_mode);
6277 printk(TPACPI_NOTICE
6278 "brightness: please report to %s whether it works well "
6279 "or not on your ThinkPad\n", TPACPI_MAIL);
6282 ibm_backlight_device->props.max_brightness =
6283 (tp_features.bright_16levels)? 15 : 7;
6284 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6285 backlight_update_status(ibm_backlight_device);
6287 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6288 "brightness: registering brightness hotkeys "
6289 "as change notification\n");
6290 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6291 | TP_ACPI_HKEY_BRGHTUP_MASK
6292 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6293 return 0;
6296 static void brightness_suspend(pm_message_t state)
6298 tpacpi_brightness_checkpoint_nvram();
6301 static void brightness_shutdown(void)
6303 tpacpi_brightness_checkpoint_nvram();
6306 static void brightness_exit(void)
6308 if (ibm_backlight_device) {
6309 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6310 "calling backlight_device_unregister()\n");
6311 backlight_device_unregister(ibm_backlight_device);
6314 tpacpi_brightness_checkpoint_nvram();
6317 static int brightness_read(char *p)
6319 int len = 0;
6320 int level;
6322 level = brightness_get(NULL);
6323 if (level < 0) {
6324 len += sprintf(p + len, "level:\t\tunreadable\n");
6325 } else {
6326 len += sprintf(p + len, "level:\t\t%d\n", level);
6327 len += sprintf(p + len, "commands:\tup, down\n");
6328 len += sprintf(p + len, "commands:\tlevel <level>"
6329 " (<level> is 0-%d)\n",
6330 (tp_features.bright_16levels) ? 15 : 7);
6333 return len;
6336 static int brightness_write(char *buf)
6338 int level;
6339 int rc;
6340 char *cmd;
6341 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6343 level = brightness_get(NULL);
6344 if (level < 0)
6345 return level;
6347 while ((cmd = next_cmd(&buf))) {
6348 if (strlencmp(cmd, "up") == 0) {
6349 if (level < max_level)
6350 level++;
6351 } else if (strlencmp(cmd, "down") == 0) {
6352 if (level > 0)
6353 level--;
6354 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6355 level >= 0 && level <= max_level) {
6356 /* new level set */
6357 } else
6358 return -EINVAL;
6361 tpacpi_disclose_usertask("procfs brightness",
6362 "set level to %d\n", level);
6365 * Now we know what the final level should be, so we try to set it.
6366 * Doing it this way makes the syscall restartable in case of EINTR
6368 rc = brightness_set(level);
6369 if (!rc && ibm_backlight_device)
6370 backlight_force_update(ibm_backlight_device,
6371 BACKLIGHT_UPDATE_SYSFS);
6372 return (rc == -EINTR)? -ERESTARTSYS : rc;
6375 static struct ibm_struct brightness_driver_data = {
6376 .name = "brightness",
6377 .read = brightness_read,
6378 .write = brightness_write,
6379 .exit = brightness_exit,
6380 .suspend = brightness_suspend,
6381 .shutdown = brightness_shutdown,
6384 /*************************************************************************
6385 * Volume subdriver
6388 static int volume_offset = 0x30;
6390 static int volume_read(char *p)
6392 int len = 0;
6393 u8 level;
6395 if (!acpi_ec_read(volume_offset, &level)) {
6396 len += sprintf(p + len, "level:\t\tunreadable\n");
6397 } else {
6398 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6399 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6400 len += sprintf(p + len, "commands:\tup, down, mute\n");
6401 len += sprintf(p + len, "commands:\tlevel <level>"
6402 " (<level> is 0-15)\n");
6405 return len;
6408 static int volume_write(char *buf)
6410 int cmos_cmd, inc, i;
6411 u8 level, mute;
6412 int new_level, new_mute;
6413 char *cmd;
6415 while ((cmd = next_cmd(&buf))) {
6416 if (!acpi_ec_read(volume_offset, &level))
6417 return -EIO;
6418 new_mute = mute = level & 0x40;
6419 new_level = level = level & 0xf;
6421 if (strlencmp(cmd, "up") == 0) {
6422 if (mute)
6423 new_mute = 0;
6424 else
6425 new_level = level == 15 ? 15 : level + 1;
6426 } else if (strlencmp(cmd, "down") == 0) {
6427 if (mute)
6428 new_mute = 0;
6429 else
6430 new_level = level == 0 ? 0 : level - 1;
6431 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6432 new_level >= 0 && new_level <= 15) {
6433 /* new_level set */
6434 } else if (strlencmp(cmd, "mute") == 0) {
6435 new_mute = 0x40;
6436 } else
6437 return -EINVAL;
6439 if (new_level != level) {
6440 /* mute doesn't change */
6442 cmos_cmd = (new_level > level) ?
6443 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6444 inc = new_level > level ? 1 : -1;
6446 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6447 !acpi_ec_write(volume_offset, level)))
6448 return -EIO;
6450 for (i = level; i != new_level; i += inc)
6451 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6452 !acpi_ec_write(volume_offset, i + inc))
6453 return -EIO;
6455 if (mute &&
6456 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6457 !acpi_ec_write(volume_offset, new_level + mute))) {
6458 return -EIO;
6462 if (new_mute != mute) {
6463 /* level doesn't change */
6465 cmos_cmd = (new_mute) ?
6466 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6468 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6469 !acpi_ec_write(volume_offset, level + new_mute))
6470 return -EIO;
6474 return 0;
6477 static struct ibm_struct volume_driver_data = {
6478 .name = "volume",
6479 .read = volume_read,
6480 .write = volume_write,
6483 /*************************************************************************
6484 * Fan subdriver
6488 * FAN ACCESS MODES
6490 * TPACPI_FAN_RD_ACPI_GFAN:
6491 * ACPI GFAN method: returns fan level
6493 * see TPACPI_FAN_WR_ACPI_SFAN
6494 * EC 0x2f (HFSP) not available if GFAN exists
6496 * TPACPI_FAN_WR_ACPI_SFAN:
6497 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6499 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6500 * it for writing.
6502 * TPACPI_FAN_WR_TPEC:
6503 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6504 * Supported on almost all ThinkPads
6506 * Fan speed changes of any sort (including those caused by the
6507 * disengaged mode) are usually done slowly by the firmware as the
6508 * maximum ammount of fan duty cycle change per second seems to be
6509 * limited.
6511 * Reading is not available if GFAN exists.
6512 * Writing is not available if SFAN exists.
6514 * Bits
6515 * 7 automatic mode engaged;
6516 * (default operation mode of the ThinkPad)
6517 * fan level is ignored in this mode.
6518 * 6 full speed mode (takes precedence over bit 7);
6519 * not available on all thinkpads. May disable
6520 * the tachometer while the fan controller ramps up
6521 * the speed (which can take up to a few *minutes*).
6522 * Speeds up fan to 100% duty-cycle, which is far above
6523 * the standard RPM levels. It is not impossible that
6524 * it could cause hardware damage.
6525 * 5-3 unused in some models. Extra bits for fan level
6526 * in others, but still useless as all values above
6527 * 7 map to the same speed as level 7 in these models.
6528 * 2-0 fan level (0..7 usually)
6529 * 0x00 = stop
6530 * 0x07 = max (set when temperatures critical)
6531 * Some ThinkPads may have other levels, see
6532 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6534 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6535 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6536 * does so, its initial value is meaningless (0x07).
6538 * For firmware bugs, refer to:
6539 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6541 * ----
6543 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6544 * Main fan tachometer reading (in RPM)
6546 * This register is present on all ThinkPads with a new-style EC, and
6547 * it is known not to be present on the A21m/e, and T22, as there is
6548 * something else in offset 0x84 according to the ACPI DSDT. Other
6549 * ThinkPads from this same time period (and earlier) probably lack the
6550 * tachometer as well.
6552 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6553 * was never fixed by IBM to report the EC firmware version string
6554 * probably support the tachometer (like the early X models), so
6555 * detecting it is quite hard. We need more data to know for sure.
6557 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6558 * might result.
6560 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6561 * mode.
6563 * For firmware bugs, refer to:
6564 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6566 * ----
6568 * ThinkPad EC register 0x31 bit 0 (only on select models)
6570 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6571 * show the speed of the main fan. When bit 0 of EC register 0x31
6572 * is one, the tachometer registers show the speed of the auxiliary
6573 * fan.
6575 * Fan control seems to affect both fans, regardless of the state
6576 * of this bit.
6578 * So far, only the firmware for the X60/X61 non-tablet versions
6579 * seem to support this (firmware TP-7M).
6581 * TPACPI_FAN_WR_ACPI_FANS:
6582 * ThinkPad X31, X40, X41. Not available in the X60.
6584 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6585 * high speed. ACPI DSDT seems to map these three speeds to levels
6586 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6587 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6589 * The speeds are stored on handles
6590 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6592 * There are three default speed sets, acessible as handles:
6593 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6595 * ACPI DSDT switches which set is in use depending on various
6596 * factors.
6598 * TPACPI_FAN_WR_TPEC is also available and should be used to
6599 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6600 * but the ACPI tables just mention level 7.
6603 enum { /* Fan control constants */
6604 fan_status_offset = 0x2f, /* EC register 0x2f */
6605 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6606 * 0x84 must be read before 0x85 */
6607 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6608 bit 0 selects which fan is active */
6610 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6611 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6613 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6616 enum fan_status_access_mode {
6617 TPACPI_FAN_NONE = 0, /* No fan status or control */
6618 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6619 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6622 enum fan_control_access_mode {
6623 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6624 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6625 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6626 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6629 enum fan_control_commands {
6630 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6631 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6632 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6633 * and also watchdog cmd */
6636 static int fan_control_allowed;
6638 static enum fan_status_access_mode fan_status_access_mode;
6639 static enum fan_control_access_mode fan_control_access_mode;
6640 static enum fan_control_commands fan_control_commands;
6642 static u8 fan_control_initial_status;
6643 static u8 fan_control_desired_level;
6644 static u8 fan_control_resume_level;
6645 static int fan_watchdog_maxinterval;
6647 static struct mutex fan_mutex;
6649 static void fan_watchdog_fire(struct work_struct *ignored);
6650 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6652 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6653 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6654 "\\FSPD", /* 600e/x, 770e, 770x */
6655 ); /* all others */
6656 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6657 "JFNS", /* 770x-JL */
6658 ); /* all others */
6661 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6662 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6663 * be in auto mode (0x80).
6665 * This is corrected by any write to HFSP either by the driver, or
6666 * by the firmware.
6668 * We assume 0x07 really means auto mode while this quirk is active,
6669 * as this is far more likely than the ThinkPad being in level 7,
6670 * which is only used by the firmware during thermal emergencies.
6672 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6673 * TP-70 (T43, R52), which are known to be buggy.
6676 static void fan_quirk1_setup(void)
6678 if (fan_control_initial_status == 0x07) {
6679 printk(TPACPI_NOTICE
6680 "fan_init: initial fan status is unknown, "
6681 "assuming it is in auto mode\n");
6682 tp_features.fan_ctrl_status_undef = 1;
6686 static void fan_quirk1_handle(u8 *fan_status)
6688 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6689 if (*fan_status != fan_control_initial_status) {
6690 /* something changed the HFSP regisnter since
6691 * driver init time, so it is not undefined
6692 * anymore */
6693 tp_features.fan_ctrl_status_undef = 0;
6694 } else {
6695 /* Return most likely status. In fact, it
6696 * might be the only possible status */
6697 *fan_status = TP_EC_FAN_AUTO;
6702 /* Select main fan on X60/X61, NOOP on others */
6703 static bool fan_select_fan1(void)
6705 if (tp_features.second_fan) {
6706 u8 val;
6708 if (ec_read(fan_select_offset, &val) < 0)
6709 return false;
6710 val &= 0xFEU;
6711 if (ec_write(fan_select_offset, val) < 0)
6712 return false;
6714 return true;
6717 /* Select secondary fan on X60/X61 */
6718 static bool fan_select_fan2(void)
6720 u8 val;
6722 if (!tp_features.second_fan)
6723 return false;
6725 if (ec_read(fan_select_offset, &val) < 0)
6726 return false;
6727 val |= 0x01U;
6728 if (ec_write(fan_select_offset, val) < 0)
6729 return false;
6731 return true;
6735 * Call with fan_mutex held
6737 static void fan_update_desired_level(u8 status)
6739 if ((status &
6740 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6741 if (status > 7)
6742 fan_control_desired_level = 7;
6743 else
6744 fan_control_desired_level = status;
6748 static int fan_get_status(u8 *status)
6750 u8 s;
6752 /* TODO:
6753 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6755 switch (fan_status_access_mode) {
6756 case TPACPI_FAN_RD_ACPI_GFAN:
6757 /* 570, 600e/x, 770e, 770x */
6759 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6760 return -EIO;
6762 if (likely(status))
6763 *status = s & 0x07;
6765 break;
6767 case TPACPI_FAN_RD_TPEC:
6768 /* all except 570, 600e/x, 770e, 770x */
6769 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6770 return -EIO;
6772 if (likely(status)) {
6773 *status = s;
6774 fan_quirk1_handle(status);
6777 break;
6779 default:
6780 return -ENXIO;
6783 return 0;
6786 static int fan_get_status_safe(u8 *status)
6788 int rc;
6789 u8 s;
6791 if (mutex_lock_killable(&fan_mutex))
6792 return -ERESTARTSYS;
6793 rc = fan_get_status(&s);
6794 if (!rc)
6795 fan_update_desired_level(s);
6796 mutex_unlock(&fan_mutex);
6798 if (status)
6799 *status = s;
6801 return rc;
6804 static int fan_get_speed(unsigned int *speed)
6806 u8 hi, lo;
6808 switch (fan_status_access_mode) {
6809 case TPACPI_FAN_RD_TPEC:
6810 /* all except 570, 600e/x, 770e, 770x */
6811 if (unlikely(!fan_select_fan1()))
6812 return -EIO;
6813 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6814 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6815 return -EIO;
6817 if (likely(speed))
6818 *speed = (hi << 8) | lo;
6820 break;
6822 default:
6823 return -ENXIO;
6826 return 0;
6829 static int fan2_get_speed(unsigned int *speed)
6831 u8 hi, lo;
6832 bool rc;
6834 switch (fan_status_access_mode) {
6835 case TPACPI_FAN_RD_TPEC:
6836 /* all except 570, 600e/x, 770e, 770x */
6837 if (unlikely(!fan_select_fan2()))
6838 return -EIO;
6839 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6840 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6841 fan_select_fan1(); /* play it safe */
6842 if (rc)
6843 return -EIO;
6845 if (likely(speed))
6846 *speed = (hi << 8) | lo;
6848 break;
6850 default:
6851 return -ENXIO;
6854 return 0;
6857 static int fan_set_level(int level)
6859 if (!fan_control_allowed)
6860 return -EPERM;
6862 switch (fan_control_access_mode) {
6863 case TPACPI_FAN_WR_ACPI_SFAN:
6864 if (level >= 0 && level <= 7) {
6865 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6866 return -EIO;
6867 } else
6868 return -EINVAL;
6869 break;
6871 case TPACPI_FAN_WR_ACPI_FANS:
6872 case TPACPI_FAN_WR_TPEC:
6873 if (!(level & TP_EC_FAN_AUTO) &&
6874 !(level & TP_EC_FAN_FULLSPEED) &&
6875 ((level < 0) || (level > 7)))
6876 return -EINVAL;
6878 /* safety net should the EC not support AUTO
6879 * or FULLSPEED mode bits and just ignore them */
6880 if (level & TP_EC_FAN_FULLSPEED)
6881 level |= 7; /* safety min speed 7 */
6882 else if (level & TP_EC_FAN_AUTO)
6883 level |= 4; /* safety min speed 4 */
6885 if (!acpi_ec_write(fan_status_offset, level))
6886 return -EIO;
6887 else
6888 tp_features.fan_ctrl_status_undef = 0;
6889 break;
6891 default:
6892 return -ENXIO;
6895 vdbg_printk(TPACPI_DBG_FAN,
6896 "fan control: set fan control register to 0x%02x\n", level);
6897 return 0;
6900 static int fan_set_level_safe(int level)
6902 int rc;
6904 if (!fan_control_allowed)
6905 return -EPERM;
6907 if (mutex_lock_killable(&fan_mutex))
6908 return -ERESTARTSYS;
6910 if (level == TPACPI_FAN_LAST_LEVEL)
6911 level = fan_control_desired_level;
6913 rc = fan_set_level(level);
6914 if (!rc)
6915 fan_update_desired_level(level);
6917 mutex_unlock(&fan_mutex);
6918 return rc;
6921 static int fan_set_enable(void)
6923 u8 s;
6924 int rc;
6926 if (!fan_control_allowed)
6927 return -EPERM;
6929 if (mutex_lock_killable(&fan_mutex))
6930 return -ERESTARTSYS;
6932 switch (fan_control_access_mode) {
6933 case TPACPI_FAN_WR_ACPI_FANS:
6934 case TPACPI_FAN_WR_TPEC:
6935 rc = fan_get_status(&s);
6936 if (rc < 0)
6937 break;
6939 /* Don't go out of emergency fan mode */
6940 if (s != 7) {
6941 s &= 0x07;
6942 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6945 if (!acpi_ec_write(fan_status_offset, s))
6946 rc = -EIO;
6947 else {
6948 tp_features.fan_ctrl_status_undef = 0;
6949 rc = 0;
6951 break;
6953 case TPACPI_FAN_WR_ACPI_SFAN:
6954 rc = fan_get_status(&s);
6955 if (rc < 0)
6956 break;
6958 s &= 0x07;
6960 /* Set fan to at least level 4 */
6961 s |= 4;
6963 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6964 rc = -EIO;
6965 else
6966 rc = 0;
6967 break;
6969 default:
6970 rc = -ENXIO;
6973 mutex_unlock(&fan_mutex);
6975 if (!rc)
6976 vdbg_printk(TPACPI_DBG_FAN,
6977 "fan control: set fan control register to 0x%02x\n",
6979 return rc;
6982 static int fan_set_disable(void)
6984 int rc;
6986 if (!fan_control_allowed)
6987 return -EPERM;
6989 if (mutex_lock_killable(&fan_mutex))
6990 return -ERESTARTSYS;
6992 rc = 0;
6993 switch (fan_control_access_mode) {
6994 case TPACPI_FAN_WR_ACPI_FANS:
6995 case TPACPI_FAN_WR_TPEC:
6996 if (!acpi_ec_write(fan_status_offset, 0x00))
6997 rc = -EIO;
6998 else {
6999 fan_control_desired_level = 0;
7000 tp_features.fan_ctrl_status_undef = 0;
7002 break;
7004 case TPACPI_FAN_WR_ACPI_SFAN:
7005 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7006 rc = -EIO;
7007 else
7008 fan_control_desired_level = 0;
7009 break;
7011 default:
7012 rc = -ENXIO;
7015 if (!rc)
7016 vdbg_printk(TPACPI_DBG_FAN,
7017 "fan control: set fan control register to 0\n");
7019 mutex_unlock(&fan_mutex);
7020 return rc;
7023 static int fan_set_speed(int speed)
7025 int rc;
7027 if (!fan_control_allowed)
7028 return -EPERM;
7030 if (mutex_lock_killable(&fan_mutex))
7031 return -ERESTARTSYS;
7033 rc = 0;
7034 switch (fan_control_access_mode) {
7035 case TPACPI_FAN_WR_ACPI_FANS:
7036 if (speed >= 0 && speed <= 65535) {
7037 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7038 speed, speed, speed))
7039 rc = -EIO;
7040 } else
7041 rc = -EINVAL;
7042 break;
7044 default:
7045 rc = -ENXIO;
7048 mutex_unlock(&fan_mutex);
7049 return rc;
7052 static void fan_watchdog_reset(void)
7054 static int fan_watchdog_active;
7056 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7057 return;
7059 if (fan_watchdog_active)
7060 cancel_delayed_work(&fan_watchdog_task);
7062 if (fan_watchdog_maxinterval > 0 &&
7063 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7064 fan_watchdog_active = 1;
7065 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7066 msecs_to_jiffies(fan_watchdog_maxinterval
7067 * 1000))) {
7068 printk(TPACPI_ERR
7069 "failed to queue the fan watchdog, "
7070 "watchdog will not trigger\n");
7072 } else
7073 fan_watchdog_active = 0;
7076 static void fan_watchdog_fire(struct work_struct *ignored)
7078 int rc;
7080 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7081 return;
7083 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7084 rc = fan_set_enable();
7085 if (rc < 0) {
7086 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7087 "will try again later...\n", -rc);
7088 /* reschedule for later */
7089 fan_watchdog_reset();
7094 * SYSFS fan layout: hwmon compatible (device)
7096 * pwm*_enable:
7097 * 0: "disengaged" mode
7098 * 1: manual mode
7099 * 2: native EC "auto" mode (recommended, hardware default)
7101 * pwm*: set speed in manual mode, ignored otherwise.
7102 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7103 * interpolation.
7105 * fan*_input: tachometer reading, RPM
7108 * SYSFS fan layout: extensions
7110 * fan_watchdog (driver):
7111 * fan watchdog interval in seconds, 0 disables (default), max 120
7114 /* sysfs fan pwm1_enable ----------------------------------------------- */
7115 static ssize_t fan_pwm1_enable_show(struct device *dev,
7116 struct device_attribute *attr,
7117 char *buf)
7119 int res, mode;
7120 u8 status;
7122 res = fan_get_status_safe(&status);
7123 if (res)
7124 return res;
7126 if (status & TP_EC_FAN_FULLSPEED) {
7127 mode = 0;
7128 } else if (status & TP_EC_FAN_AUTO) {
7129 mode = 2;
7130 } else
7131 mode = 1;
7133 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7136 static ssize_t fan_pwm1_enable_store(struct device *dev,
7137 struct device_attribute *attr,
7138 const char *buf, size_t count)
7140 unsigned long t;
7141 int res, level;
7143 if (parse_strtoul(buf, 2, &t))
7144 return -EINVAL;
7146 tpacpi_disclose_usertask("hwmon pwm1_enable",
7147 "set fan mode to %lu\n", t);
7149 switch (t) {
7150 case 0:
7151 level = TP_EC_FAN_FULLSPEED;
7152 break;
7153 case 1:
7154 level = TPACPI_FAN_LAST_LEVEL;
7155 break;
7156 case 2:
7157 level = TP_EC_FAN_AUTO;
7158 break;
7159 case 3:
7160 /* reserved for software-controlled auto mode */
7161 return -ENOSYS;
7162 default:
7163 return -EINVAL;
7166 res = fan_set_level_safe(level);
7167 if (res == -ENXIO)
7168 return -EINVAL;
7169 else if (res < 0)
7170 return res;
7172 fan_watchdog_reset();
7174 return count;
7177 static struct device_attribute dev_attr_fan_pwm1_enable =
7178 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7179 fan_pwm1_enable_show, fan_pwm1_enable_store);
7181 /* sysfs fan pwm1 ------------------------------------------------------ */
7182 static ssize_t fan_pwm1_show(struct device *dev,
7183 struct device_attribute *attr,
7184 char *buf)
7186 int res;
7187 u8 status;
7189 res = fan_get_status_safe(&status);
7190 if (res)
7191 return res;
7193 if ((status &
7194 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7195 status = fan_control_desired_level;
7197 if (status > 7)
7198 status = 7;
7200 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7203 static ssize_t fan_pwm1_store(struct device *dev,
7204 struct device_attribute *attr,
7205 const char *buf, size_t count)
7207 unsigned long s;
7208 int rc;
7209 u8 status, newlevel;
7211 if (parse_strtoul(buf, 255, &s))
7212 return -EINVAL;
7214 tpacpi_disclose_usertask("hwmon pwm1",
7215 "set fan speed to %lu\n", s);
7217 /* scale down from 0-255 to 0-7 */
7218 newlevel = (s >> 5) & 0x07;
7220 if (mutex_lock_killable(&fan_mutex))
7221 return -ERESTARTSYS;
7223 rc = fan_get_status(&status);
7224 if (!rc && (status &
7225 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7226 rc = fan_set_level(newlevel);
7227 if (rc == -ENXIO)
7228 rc = -EINVAL;
7229 else if (!rc) {
7230 fan_update_desired_level(newlevel);
7231 fan_watchdog_reset();
7235 mutex_unlock(&fan_mutex);
7236 return (rc)? rc : count;
7239 static struct device_attribute dev_attr_fan_pwm1 =
7240 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7241 fan_pwm1_show, fan_pwm1_store);
7243 /* sysfs fan fan1_input ------------------------------------------------ */
7244 static ssize_t fan_fan1_input_show(struct device *dev,
7245 struct device_attribute *attr,
7246 char *buf)
7248 int res;
7249 unsigned int speed;
7251 res = fan_get_speed(&speed);
7252 if (res < 0)
7253 return res;
7255 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7258 static struct device_attribute dev_attr_fan_fan1_input =
7259 __ATTR(fan1_input, S_IRUGO,
7260 fan_fan1_input_show, NULL);
7262 /* sysfs fan fan2_input ------------------------------------------------ */
7263 static ssize_t fan_fan2_input_show(struct device *dev,
7264 struct device_attribute *attr,
7265 char *buf)
7267 int res;
7268 unsigned int speed;
7270 res = fan2_get_speed(&speed);
7271 if (res < 0)
7272 return res;
7274 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7277 static struct device_attribute dev_attr_fan_fan2_input =
7278 __ATTR(fan2_input, S_IRUGO,
7279 fan_fan2_input_show, NULL);
7281 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7282 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7283 char *buf)
7285 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7288 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7289 const char *buf, size_t count)
7291 unsigned long t;
7293 if (parse_strtoul(buf, 120, &t))
7294 return -EINVAL;
7296 if (!fan_control_allowed)
7297 return -EPERM;
7299 fan_watchdog_maxinterval = t;
7300 fan_watchdog_reset();
7302 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7304 return count;
7307 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7308 fan_fan_watchdog_show, fan_fan_watchdog_store);
7310 /* --------------------------------------------------------------------- */
7311 static struct attribute *fan_attributes[] = {
7312 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7313 &dev_attr_fan_fan1_input.attr,
7314 NULL, /* for fan2_input */
7315 NULL
7318 static const struct attribute_group fan_attr_group = {
7319 .attrs = fan_attributes,
7322 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7323 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7325 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7326 { .vendor = PCI_VENDOR_ID_IBM, \
7327 .bios = TPACPI_MATCH_ANY, \
7328 .ec = TPID(__id1, __id2), \
7329 .quirks = __quirks }
7331 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7332 { .vendor = PCI_VENDOR_ID_LENOVO, \
7333 .bios = TPACPI_MATCH_ANY, \
7334 .ec = TPID(__id1, __id2), \
7335 .quirks = __quirks }
7337 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7338 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7339 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7340 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7341 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7342 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7345 #undef TPACPI_FAN_QL
7346 #undef TPACPI_FAN_QI
7348 static int __init fan_init(struct ibm_init_struct *iibm)
7350 int rc;
7351 unsigned long quirks;
7353 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7354 "initializing fan subdriver\n");
7356 mutex_init(&fan_mutex);
7357 fan_status_access_mode = TPACPI_FAN_NONE;
7358 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7359 fan_control_commands = 0;
7360 fan_watchdog_maxinterval = 0;
7361 tp_features.fan_ctrl_status_undef = 0;
7362 tp_features.second_fan = 0;
7363 fan_control_desired_level = 7;
7365 TPACPI_ACPIHANDLE_INIT(fans);
7366 TPACPI_ACPIHANDLE_INIT(gfan);
7367 TPACPI_ACPIHANDLE_INIT(sfan);
7369 quirks = tpacpi_check_quirks(fan_quirk_table,
7370 ARRAY_SIZE(fan_quirk_table));
7372 if (gfan_handle) {
7373 /* 570, 600e/x, 770e, 770x */
7374 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7375 } else {
7376 /* all other ThinkPads: note that even old-style
7377 * ThinkPad ECs supports the fan control register */
7378 if (likely(acpi_ec_read(fan_status_offset,
7379 &fan_control_initial_status))) {
7380 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7381 if (quirks & TPACPI_FAN_Q1)
7382 fan_quirk1_setup();
7383 if (quirks & TPACPI_FAN_2FAN) {
7384 tp_features.second_fan = 1;
7385 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7386 "secondary fan support enabled\n");
7388 } else {
7389 printk(TPACPI_ERR
7390 "ThinkPad ACPI EC access misbehaving, "
7391 "fan status and control unavailable\n");
7392 return 1;
7396 if (sfan_handle) {
7397 /* 570, 770x-JL */
7398 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7399 fan_control_commands |=
7400 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7401 } else {
7402 if (!gfan_handle) {
7403 /* gfan without sfan means no fan control */
7404 /* all other models implement TP EC 0x2f control */
7406 if (fans_handle) {
7407 /* X31, X40, X41 */
7408 fan_control_access_mode =
7409 TPACPI_FAN_WR_ACPI_FANS;
7410 fan_control_commands |=
7411 TPACPI_FAN_CMD_SPEED |
7412 TPACPI_FAN_CMD_LEVEL |
7413 TPACPI_FAN_CMD_ENABLE;
7414 } else {
7415 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7416 fan_control_commands |=
7417 TPACPI_FAN_CMD_LEVEL |
7418 TPACPI_FAN_CMD_ENABLE;
7423 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7424 "fan is %s, modes %d, %d\n",
7425 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7426 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7427 fan_status_access_mode, fan_control_access_mode);
7429 /* fan control master switch */
7430 if (!fan_control_allowed) {
7431 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7432 fan_control_commands = 0;
7433 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7434 "fan control features disabled by parameter\n");
7437 /* update fan_control_desired_level */
7438 if (fan_status_access_mode != TPACPI_FAN_NONE)
7439 fan_get_status_safe(NULL);
7441 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7442 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7443 if (tp_features.second_fan) {
7444 /* attach second fan tachometer */
7445 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7446 &dev_attr_fan_fan2_input.attr;
7448 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7449 &fan_attr_group);
7450 if (rc < 0)
7451 return rc;
7453 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7454 &driver_attr_fan_watchdog);
7455 if (rc < 0) {
7456 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7457 &fan_attr_group);
7458 return rc;
7460 return 0;
7461 } else
7462 return 1;
7465 static void fan_exit(void)
7467 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7468 "cancelling any pending fan watchdog tasks\n");
7470 /* FIXME: can we really do this unconditionally? */
7471 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7472 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7473 &driver_attr_fan_watchdog);
7475 cancel_delayed_work(&fan_watchdog_task);
7476 flush_workqueue(tpacpi_wq);
7479 static void fan_suspend(pm_message_t state)
7481 int rc;
7483 if (!fan_control_allowed)
7484 return;
7486 /* Store fan status in cache */
7487 fan_control_resume_level = 0;
7488 rc = fan_get_status_safe(&fan_control_resume_level);
7489 if (rc < 0)
7490 printk(TPACPI_NOTICE
7491 "failed to read fan level for later "
7492 "restore during resume: %d\n", rc);
7494 /* if it is undefined, don't attempt to restore it.
7495 * KEEP THIS LAST */
7496 if (tp_features.fan_ctrl_status_undef)
7497 fan_control_resume_level = 0;
7500 static void fan_resume(void)
7502 u8 current_level = 7;
7503 bool do_set = false;
7504 int rc;
7506 /* DSDT *always* updates status on resume */
7507 tp_features.fan_ctrl_status_undef = 0;
7509 if (!fan_control_allowed ||
7510 !fan_control_resume_level ||
7511 (fan_get_status_safe(&current_level) < 0))
7512 return;
7514 switch (fan_control_access_mode) {
7515 case TPACPI_FAN_WR_ACPI_SFAN:
7516 /* never decrease fan level */
7517 do_set = (fan_control_resume_level > current_level);
7518 break;
7519 case TPACPI_FAN_WR_ACPI_FANS:
7520 case TPACPI_FAN_WR_TPEC:
7521 /* never decrease fan level, scale is:
7522 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7524 * We expect the firmware to set either 7 or AUTO, but we
7525 * handle FULLSPEED out of paranoia.
7527 * So, we can safely only restore FULLSPEED or 7, anything
7528 * else could slow the fan. Restoring AUTO is useless, at
7529 * best that's exactly what the DSDT already set (it is the
7530 * slower it uses).
7532 * Always keep in mind that the DSDT *will* have set the
7533 * fans to what the vendor supposes is the best level. We
7534 * muck with it only to speed the fan up.
7536 if (fan_control_resume_level != 7 &&
7537 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7538 return;
7539 else
7540 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7541 (current_level != fan_control_resume_level);
7542 break;
7543 default:
7544 return;
7546 if (do_set) {
7547 printk(TPACPI_NOTICE
7548 "restoring fan level to 0x%02x\n",
7549 fan_control_resume_level);
7550 rc = fan_set_level_safe(fan_control_resume_level);
7551 if (rc < 0)
7552 printk(TPACPI_NOTICE
7553 "failed to restore fan level: %d\n", rc);
7557 static int fan_read(char *p)
7559 int len = 0;
7560 int rc;
7561 u8 status;
7562 unsigned int speed = 0;
7564 switch (fan_status_access_mode) {
7565 case TPACPI_FAN_RD_ACPI_GFAN:
7566 /* 570, 600e/x, 770e, 770x */
7567 rc = fan_get_status_safe(&status);
7568 if (rc < 0)
7569 return rc;
7571 len += sprintf(p + len, "status:\t\t%s\n"
7572 "level:\t\t%d\n",
7573 (status != 0) ? "enabled" : "disabled", status);
7574 break;
7576 case TPACPI_FAN_RD_TPEC:
7577 /* all except 570, 600e/x, 770e, 770x */
7578 rc = fan_get_status_safe(&status);
7579 if (rc < 0)
7580 return rc;
7582 len += sprintf(p + len, "status:\t\t%s\n",
7583 (status != 0) ? "enabled" : "disabled");
7585 rc = fan_get_speed(&speed);
7586 if (rc < 0)
7587 return rc;
7589 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7591 if (status & TP_EC_FAN_FULLSPEED)
7592 /* Disengaged mode takes precedence */
7593 len += sprintf(p + len, "level:\t\tdisengaged\n");
7594 else if (status & TP_EC_FAN_AUTO)
7595 len += sprintf(p + len, "level:\t\tauto\n");
7596 else
7597 len += sprintf(p + len, "level:\t\t%d\n", status);
7598 break;
7600 case TPACPI_FAN_NONE:
7601 default:
7602 len += sprintf(p + len, "status:\t\tnot supported\n");
7605 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7606 len += sprintf(p + len, "commands:\tlevel <level>");
7608 switch (fan_control_access_mode) {
7609 case TPACPI_FAN_WR_ACPI_SFAN:
7610 len += sprintf(p + len, " (<level> is 0-7)\n");
7611 break;
7613 default:
7614 len += sprintf(p + len, " (<level> is 0-7, "
7615 "auto, disengaged, full-speed)\n");
7616 break;
7620 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7621 len += sprintf(p + len, "commands:\tenable, disable\n"
7622 "commands:\twatchdog <timeout> (<timeout> "
7623 "is 0 (off), 1-120 (seconds))\n");
7625 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7626 len += sprintf(p + len, "commands:\tspeed <speed>"
7627 " (<speed> is 0-65535)\n");
7629 return len;
7632 static int fan_write_cmd_level(const char *cmd, int *rc)
7634 int level;
7636 if (strlencmp(cmd, "level auto") == 0)
7637 level = TP_EC_FAN_AUTO;
7638 else if ((strlencmp(cmd, "level disengaged") == 0) |
7639 (strlencmp(cmd, "level full-speed") == 0))
7640 level = TP_EC_FAN_FULLSPEED;
7641 else if (sscanf(cmd, "level %d", &level) != 1)
7642 return 0;
7644 *rc = fan_set_level_safe(level);
7645 if (*rc == -ENXIO)
7646 printk(TPACPI_ERR "level command accepted for unsupported "
7647 "access mode %d", fan_control_access_mode);
7648 else if (!*rc)
7649 tpacpi_disclose_usertask("procfs fan",
7650 "set level to %d\n", level);
7652 return 1;
7655 static int fan_write_cmd_enable(const char *cmd, int *rc)
7657 if (strlencmp(cmd, "enable") != 0)
7658 return 0;
7660 *rc = fan_set_enable();
7661 if (*rc == -ENXIO)
7662 printk(TPACPI_ERR "enable command accepted for unsupported "
7663 "access mode %d", fan_control_access_mode);
7664 else if (!*rc)
7665 tpacpi_disclose_usertask("procfs fan", "enable\n");
7667 return 1;
7670 static int fan_write_cmd_disable(const char *cmd, int *rc)
7672 if (strlencmp(cmd, "disable") != 0)
7673 return 0;
7675 *rc = fan_set_disable();
7676 if (*rc == -ENXIO)
7677 printk(TPACPI_ERR "disable command accepted for unsupported "
7678 "access mode %d", fan_control_access_mode);
7679 else if (!*rc)
7680 tpacpi_disclose_usertask("procfs fan", "disable\n");
7682 return 1;
7685 static int fan_write_cmd_speed(const char *cmd, int *rc)
7687 int speed;
7689 /* TODO:
7690 * Support speed <low> <medium> <high> ? */
7692 if (sscanf(cmd, "speed %d", &speed) != 1)
7693 return 0;
7695 *rc = fan_set_speed(speed);
7696 if (*rc == -ENXIO)
7697 printk(TPACPI_ERR "speed command accepted for unsupported "
7698 "access mode %d", fan_control_access_mode);
7699 else if (!*rc)
7700 tpacpi_disclose_usertask("procfs fan",
7701 "set speed to %d\n", speed);
7703 return 1;
7706 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7708 int interval;
7710 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7711 return 0;
7713 if (interval < 0 || interval > 120)
7714 *rc = -EINVAL;
7715 else {
7716 fan_watchdog_maxinterval = interval;
7717 tpacpi_disclose_usertask("procfs fan",
7718 "set watchdog timer to %d\n",
7719 interval);
7722 return 1;
7725 static int fan_write(char *buf)
7727 char *cmd;
7728 int rc = 0;
7730 while (!rc && (cmd = next_cmd(&buf))) {
7731 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7732 fan_write_cmd_level(cmd, &rc)) &&
7733 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7734 (fan_write_cmd_enable(cmd, &rc) ||
7735 fan_write_cmd_disable(cmd, &rc) ||
7736 fan_write_cmd_watchdog(cmd, &rc))) &&
7737 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7738 fan_write_cmd_speed(cmd, &rc))
7740 rc = -EINVAL;
7741 else if (!rc)
7742 fan_watchdog_reset();
7745 return rc;
7748 static struct ibm_struct fan_driver_data = {
7749 .name = "fan",
7750 .read = fan_read,
7751 .write = fan_write,
7752 .exit = fan_exit,
7753 .suspend = fan_suspend,
7754 .resume = fan_resume,
7757 /****************************************************************************
7758 ****************************************************************************
7760 * Infrastructure
7762 ****************************************************************************
7763 ****************************************************************************/
7766 * HKEY event callout for other subdrivers go here
7767 * (yes, it is ugly, but it is quick, safe, and gets the job done
7769 static void tpacpi_driver_event(const unsigned int hkey_event)
7771 if (ibm_backlight_device) {
7772 switch (hkey_event) {
7773 case TP_HKEY_EV_BRGHT_UP:
7774 case TP_HKEY_EV_BRGHT_DOWN:
7775 tpacpi_brightness_notify_change();
7782 static void hotkey_driver_event(const unsigned int scancode)
7784 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7787 /* sysfs name ---------------------------------------------------------- */
7788 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7789 struct device_attribute *attr,
7790 char *buf)
7792 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7795 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7796 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7798 /* --------------------------------------------------------------------- */
7800 /* /proc support */
7801 static struct proc_dir_entry *proc_dir;
7804 * Module and infrastructure proble, init and exit handling
7807 static int force_load;
7809 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7810 static const char * __init str_supported(int is_supported)
7812 static char text_unsupported[] __initdata = "not supported";
7814 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7816 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7818 static void ibm_exit(struct ibm_struct *ibm)
7820 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7822 list_del_init(&ibm->all_drivers);
7824 if (ibm->flags.acpi_notify_installed) {
7825 dbg_printk(TPACPI_DBG_EXIT,
7826 "%s: acpi_remove_notify_handler\n", ibm->name);
7827 BUG_ON(!ibm->acpi);
7828 acpi_remove_notify_handler(*ibm->acpi->handle,
7829 ibm->acpi->type,
7830 dispatch_acpi_notify);
7831 ibm->flags.acpi_notify_installed = 0;
7832 ibm->flags.acpi_notify_installed = 0;
7835 if (ibm->flags.proc_created) {
7836 dbg_printk(TPACPI_DBG_EXIT,
7837 "%s: remove_proc_entry\n", ibm->name);
7838 remove_proc_entry(ibm->name, proc_dir);
7839 ibm->flags.proc_created = 0;
7842 if (ibm->flags.acpi_driver_registered) {
7843 dbg_printk(TPACPI_DBG_EXIT,
7844 "%s: acpi_bus_unregister_driver\n", ibm->name);
7845 BUG_ON(!ibm->acpi);
7846 acpi_bus_unregister_driver(ibm->acpi->driver);
7847 kfree(ibm->acpi->driver);
7848 ibm->acpi->driver = NULL;
7849 ibm->flags.acpi_driver_registered = 0;
7852 if (ibm->flags.init_called && ibm->exit) {
7853 ibm->exit();
7854 ibm->flags.init_called = 0;
7857 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7860 static int __init ibm_init(struct ibm_init_struct *iibm)
7862 int ret;
7863 struct ibm_struct *ibm = iibm->data;
7864 struct proc_dir_entry *entry;
7866 BUG_ON(ibm == NULL);
7868 INIT_LIST_HEAD(&ibm->all_drivers);
7870 if (ibm->flags.experimental && !experimental)
7871 return 0;
7873 dbg_printk(TPACPI_DBG_INIT,
7874 "probing for %s\n", ibm->name);
7876 if (iibm->init) {
7877 ret = iibm->init(iibm);
7878 if (ret > 0)
7879 return 0; /* probe failed */
7880 if (ret)
7881 return ret;
7883 ibm->flags.init_called = 1;
7886 if (ibm->acpi) {
7887 if (ibm->acpi->hid) {
7888 ret = register_tpacpi_subdriver(ibm);
7889 if (ret)
7890 goto err_out;
7893 if (ibm->acpi->notify) {
7894 ret = setup_acpi_notify(ibm);
7895 if (ret == -ENODEV) {
7896 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7897 ibm->name);
7898 ret = 0;
7899 goto err_out;
7901 if (ret < 0)
7902 goto err_out;
7906 dbg_printk(TPACPI_DBG_INIT,
7907 "%s installed\n", ibm->name);
7909 if (ibm->read) {
7910 entry = create_proc_entry(ibm->name,
7911 S_IFREG | S_IRUGO | S_IWUSR,
7912 proc_dir);
7913 if (!entry) {
7914 printk(TPACPI_ERR "unable to create proc entry %s\n",
7915 ibm->name);
7916 ret = -ENODEV;
7917 goto err_out;
7919 entry->data = ibm;
7920 entry->read_proc = &dispatch_procfs_read;
7921 if (ibm->write)
7922 entry->write_proc = &dispatch_procfs_write;
7923 ibm->flags.proc_created = 1;
7926 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7928 return 0;
7930 err_out:
7931 dbg_printk(TPACPI_DBG_INIT,
7932 "%s: at error exit path with result %d\n",
7933 ibm->name, ret);
7935 ibm_exit(ibm);
7936 return (ret < 0)? ret : 0;
7939 /* Probing */
7941 static bool __pure __init tpacpi_is_fw_digit(const char c)
7943 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7946 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7947 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7948 const char t)
7950 return s && strlen(s) >= 8 &&
7951 tpacpi_is_fw_digit(s[0]) &&
7952 tpacpi_is_fw_digit(s[1]) &&
7953 s[2] == t && s[3] == 'T' &&
7954 tpacpi_is_fw_digit(s[4]) &&
7955 tpacpi_is_fw_digit(s[5]) &&
7956 s[6] == 'W' && s[7] == 'W';
7959 /* returns 0 - probe ok, or < 0 - probe error.
7960 * Probe ok doesn't mean thinkpad found.
7961 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7962 static int __must_check __init get_thinkpad_model_data(
7963 struct thinkpad_id_data *tp)
7965 const struct dmi_device *dev = NULL;
7966 char ec_fw_string[18];
7967 char const *s;
7969 if (!tp)
7970 return -EINVAL;
7972 memset(tp, 0, sizeof(*tp));
7974 if (dmi_name_in_vendors("IBM"))
7975 tp->vendor = PCI_VENDOR_ID_IBM;
7976 else if (dmi_name_in_vendors("LENOVO"))
7977 tp->vendor = PCI_VENDOR_ID_LENOVO;
7978 else
7979 return 0;
7981 s = dmi_get_system_info(DMI_BIOS_VERSION);
7982 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7983 if (s && !tp->bios_version_str)
7984 return -ENOMEM;
7986 /* Really ancient ThinkPad 240X will fail this, which is fine */
7987 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7988 return 0;
7990 tp->bios_model = tp->bios_version_str[0]
7991 | (tp->bios_version_str[1] << 8);
7992 tp->bios_release = (tp->bios_version_str[4] << 8)
7993 | tp->bios_version_str[5];
7996 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7997 * X32 or newer, all Z series; Some models must have an
7998 * up-to-date BIOS or they will not be detected.
8000 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8002 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8003 if (sscanf(dev->name,
8004 "IBM ThinkPad Embedded Controller -[%17c",
8005 ec_fw_string) == 1) {
8006 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8007 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8009 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8010 if (!tp->ec_version_str)
8011 return -ENOMEM;
8013 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8014 tp->ec_model = ec_fw_string[0]
8015 | (ec_fw_string[1] << 8);
8016 tp->ec_release = (ec_fw_string[4] << 8)
8017 | ec_fw_string[5];
8018 } else {
8019 printk(TPACPI_NOTICE
8020 "ThinkPad firmware release %s "
8021 "doesn't match the known patterns\n",
8022 ec_fw_string);
8023 printk(TPACPI_NOTICE
8024 "please report this to %s\n",
8025 TPACPI_MAIL);
8027 break;
8031 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8032 if (s && !strnicmp(s, "ThinkPad", 8)) {
8033 tp->model_str = kstrdup(s, GFP_KERNEL);
8034 if (!tp->model_str)
8035 return -ENOMEM;
8038 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8039 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8040 if (s && !tp->nummodel_str)
8041 return -ENOMEM;
8043 return 0;
8046 static int __init probe_for_thinkpad(void)
8048 int is_thinkpad;
8050 if (acpi_disabled)
8051 return -ENODEV;
8054 * Non-ancient models have better DMI tagging, but very old models
8055 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8057 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8058 (thinkpad_id.ec_model != 0) ||
8059 tpacpi_is_fw_known();
8061 /* ec is required because many other handles are relative to it */
8062 TPACPI_ACPIHANDLE_INIT(ec);
8063 if (!ec_handle) {
8064 if (is_thinkpad)
8065 printk(TPACPI_ERR
8066 "Not yet supported ThinkPad detected!\n");
8067 return -ENODEV;
8070 if (!is_thinkpad && !force_load)
8071 return -ENODEV;
8073 return 0;
8077 /* Module init, exit, parameters */
8079 static struct ibm_init_struct ibms_init[] __initdata = {
8081 .init = thinkpad_acpi_driver_init,
8082 .data = &thinkpad_acpi_driver_data,
8085 .init = hotkey_init,
8086 .data = &hotkey_driver_data,
8089 .init = bluetooth_init,
8090 .data = &bluetooth_driver_data,
8093 .init = wan_init,
8094 .data = &wan_driver_data,
8097 .init = uwb_init,
8098 .data = &uwb_driver_data,
8100 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8102 .init = video_init,
8103 .data = &video_driver_data,
8105 #endif
8107 .init = light_init,
8108 .data = &light_driver_data,
8111 .init = cmos_init,
8112 .data = &cmos_driver_data,
8115 .init = led_init,
8116 .data = &led_driver_data,
8119 .init = beep_init,
8120 .data = &beep_driver_data,
8123 .init = thermal_init,
8124 .data = &thermal_driver_data,
8127 .data = &ecdump_driver_data,
8130 .init = brightness_init,
8131 .data = &brightness_driver_data,
8134 .data = &volume_driver_data,
8137 .init = fan_init,
8138 .data = &fan_driver_data,
8142 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8144 unsigned int i;
8145 struct ibm_struct *ibm;
8147 if (!kp || !kp->name || !val)
8148 return -EINVAL;
8150 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8151 ibm = ibms_init[i].data;
8152 WARN_ON(ibm == NULL);
8154 if (!ibm || !ibm->name)
8155 continue;
8157 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8158 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8159 return -ENOSPC;
8160 strcpy(ibms_init[i].param, val);
8161 strcat(ibms_init[i].param, ",");
8162 return 0;
8166 return -EINVAL;
8169 module_param(experimental, int, 0444);
8170 MODULE_PARM_DESC(experimental,
8171 "Enables experimental features when non-zero");
8173 module_param_named(debug, dbg_level, uint, 0);
8174 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8176 module_param(force_load, bool, 0444);
8177 MODULE_PARM_DESC(force_load,
8178 "Attempts to load the driver even on a "
8179 "mis-identified ThinkPad when true");
8181 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8182 MODULE_PARM_DESC(fan_control,
8183 "Enables setting fan parameters features when true");
8185 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8186 MODULE_PARM_DESC(brightness_mode,
8187 "Selects brightness control strategy: "
8188 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8190 module_param(brightness_enable, uint, 0444);
8191 MODULE_PARM_DESC(brightness_enable,
8192 "Enables backlight control when 1, disables when 0");
8194 module_param(hotkey_report_mode, uint, 0444);
8195 MODULE_PARM_DESC(hotkey_report_mode,
8196 "used for backwards compatibility with userspace, "
8197 "see documentation");
8199 #define TPACPI_PARAM(feature) \
8200 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8201 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8202 "at module load, see documentation")
8204 TPACPI_PARAM(hotkey);
8205 TPACPI_PARAM(bluetooth);
8206 TPACPI_PARAM(video);
8207 TPACPI_PARAM(light);
8208 TPACPI_PARAM(cmos);
8209 TPACPI_PARAM(led);
8210 TPACPI_PARAM(beep);
8211 TPACPI_PARAM(ecdump);
8212 TPACPI_PARAM(brightness);
8213 TPACPI_PARAM(volume);
8214 TPACPI_PARAM(fan);
8216 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8217 module_param(dbg_wlswemul, uint, 0444);
8218 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8219 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8220 MODULE_PARM_DESC(wlsw_state,
8221 "Initial state of the emulated WLSW switch");
8223 module_param(dbg_bluetoothemul, uint, 0444);
8224 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8225 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8226 MODULE_PARM_DESC(bluetooth_state,
8227 "Initial state of the emulated bluetooth switch");
8229 module_param(dbg_wwanemul, uint, 0444);
8230 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8231 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8232 MODULE_PARM_DESC(wwan_state,
8233 "Initial state of the emulated WWAN switch");
8235 module_param(dbg_uwbemul, uint, 0444);
8236 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8237 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8238 MODULE_PARM_DESC(uwb_state,
8239 "Initial state of the emulated UWB switch");
8240 #endif
8242 static void thinkpad_acpi_module_exit(void)
8244 struct ibm_struct *ibm, *itmp;
8246 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8248 list_for_each_entry_safe_reverse(ibm, itmp,
8249 &tpacpi_all_drivers,
8250 all_drivers) {
8251 ibm_exit(ibm);
8254 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8256 if (tpacpi_inputdev) {
8257 if (tp_features.input_device_registered)
8258 input_unregister_device(tpacpi_inputdev);
8259 else
8260 input_free_device(tpacpi_inputdev);
8263 if (tpacpi_hwmon)
8264 hwmon_device_unregister(tpacpi_hwmon);
8266 if (tp_features.sensors_pdev_attrs_registered)
8267 device_remove_file(&tpacpi_sensors_pdev->dev,
8268 &dev_attr_thinkpad_acpi_pdev_name);
8269 if (tpacpi_sensors_pdev)
8270 platform_device_unregister(tpacpi_sensors_pdev);
8271 if (tpacpi_pdev)
8272 platform_device_unregister(tpacpi_pdev);
8274 if (tp_features.sensors_pdrv_attrs_registered)
8275 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8276 if (tp_features.platform_drv_attrs_registered)
8277 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8279 if (tp_features.sensors_pdrv_registered)
8280 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8282 if (tp_features.platform_drv_registered)
8283 platform_driver_unregister(&tpacpi_pdriver);
8285 if (proc_dir)
8286 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8288 if (tpacpi_wq)
8289 destroy_workqueue(tpacpi_wq);
8291 kfree(thinkpad_id.bios_version_str);
8292 kfree(thinkpad_id.ec_version_str);
8293 kfree(thinkpad_id.model_str);
8297 static int __init thinkpad_acpi_module_init(void)
8299 int ret, i;
8301 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8303 /* Parameter checking */
8304 if (hotkey_report_mode > 2)
8305 return -EINVAL;
8307 /* Driver-level probe */
8309 ret = get_thinkpad_model_data(&thinkpad_id);
8310 if (ret) {
8311 printk(TPACPI_ERR
8312 "unable to get DMI data: %d\n", ret);
8313 thinkpad_acpi_module_exit();
8314 return ret;
8316 ret = probe_for_thinkpad();
8317 if (ret) {
8318 thinkpad_acpi_module_exit();
8319 return ret;
8322 /* Driver initialization */
8324 TPACPI_ACPIHANDLE_INIT(ecrd);
8325 TPACPI_ACPIHANDLE_INIT(ecwr);
8327 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8328 if (!tpacpi_wq) {
8329 thinkpad_acpi_module_exit();
8330 return -ENOMEM;
8333 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8334 if (!proc_dir) {
8335 printk(TPACPI_ERR
8336 "unable to create proc dir " TPACPI_PROC_DIR);
8337 thinkpad_acpi_module_exit();
8338 return -ENODEV;
8341 ret = platform_driver_register(&tpacpi_pdriver);
8342 if (ret) {
8343 printk(TPACPI_ERR
8344 "unable to register main platform driver\n");
8345 thinkpad_acpi_module_exit();
8346 return ret;
8348 tp_features.platform_drv_registered = 1;
8350 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8351 if (ret) {
8352 printk(TPACPI_ERR
8353 "unable to register hwmon platform driver\n");
8354 thinkpad_acpi_module_exit();
8355 return ret;
8357 tp_features.sensors_pdrv_registered = 1;
8359 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8360 if (!ret) {
8361 tp_features.platform_drv_attrs_registered = 1;
8362 ret = tpacpi_create_driver_attributes(
8363 &tpacpi_hwmon_pdriver.driver);
8365 if (ret) {
8366 printk(TPACPI_ERR
8367 "unable to create sysfs driver attributes\n");
8368 thinkpad_acpi_module_exit();
8369 return ret;
8371 tp_features.sensors_pdrv_attrs_registered = 1;
8374 /* Device initialization */
8375 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8376 NULL, 0);
8377 if (IS_ERR(tpacpi_pdev)) {
8378 ret = PTR_ERR(tpacpi_pdev);
8379 tpacpi_pdev = NULL;
8380 printk(TPACPI_ERR "unable to register platform device\n");
8381 thinkpad_acpi_module_exit();
8382 return ret;
8384 tpacpi_sensors_pdev = platform_device_register_simple(
8385 TPACPI_HWMON_DRVR_NAME,
8386 -1, NULL, 0);
8387 if (IS_ERR(tpacpi_sensors_pdev)) {
8388 ret = PTR_ERR(tpacpi_sensors_pdev);
8389 tpacpi_sensors_pdev = NULL;
8390 printk(TPACPI_ERR
8391 "unable to register hwmon platform device\n");
8392 thinkpad_acpi_module_exit();
8393 return ret;
8395 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8396 &dev_attr_thinkpad_acpi_pdev_name);
8397 if (ret) {
8398 printk(TPACPI_ERR
8399 "unable to create sysfs hwmon device attributes\n");
8400 thinkpad_acpi_module_exit();
8401 return ret;
8403 tp_features.sensors_pdev_attrs_registered = 1;
8404 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8405 if (IS_ERR(tpacpi_hwmon)) {
8406 ret = PTR_ERR(tpacpi_hwmon);
8407 tpacpi_hwmon = NULL;
8408 printk(TPACPI_ERR "unable to register hwmon device\n");
8409 thinkpad_acpi_module_exit();
8410 return ret;
8412 mutex_init(&tpacpi_inputdev_send_mutex);
8413 tpacpi_inputdev = input_allocate_device();
8414 if (!tpacpi_inputdev) {
8415 printk(TPACPI_ERR "unable to allocate input device\n");
8416 thinkpad_acpi_module_exit();
8417 return -ENOMEM;
8418 } else {
8419 /* Prepare input device, but don't register */
8420 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8421 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8422 tpacpi_inputdev->id.bustype = BUS_HOST;
8423 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8424 thinkpad_id.vendor :
8425 PCI_VENDOR_ID_IBM;
8426 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8427 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8428 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8430 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8431 ret = ibm_init(&ibms_init[i]);
8432 if (ret >= 0 && *ibms_init[i].param)
8433 ret = ibms_init[i].data->write(ibms_init[i].param);
8434 if (ret < 0) {
8435 thinkpad_acpi_module_exit();
8436 return ret;
8440 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8442 ret = input_register_device(tpacpi_inputdev);
8443 if (ret < 0) {
8444 printk(TPACPI_ERR "unable to register input device\n");
8445 thinkpad_acpi_module_exit();
8446 return ret;
8447 } else {
8448 tp_features.input_device_registered = 1;
8451 return 0;
8454 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8457 * This will autoload the driver in almost every ThinkPad
8458 * in widespread use.
8460 * Only _VERY_ old models, like the 240, 240x and 570 lack
8461 * the HKEY event interface.
8463 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8466 * DMI matching for module autoloading
8468 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8469 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8471 * Only models listed in thinkwiki will be supported, so add yours
8472 * if it is not there yet.
8474 #define IBM_BIOS_MODULE_ALIAS(__type) \
8475 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8477 /* Ancient thinkpad BIOSes have to be identified by
8478 * BIOS type or model number, and there are far less
8479 * BIOS types than model numbers... */
8480 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8482 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8483 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8484 MODULE_DESCRIPTION(TPACPI_DESC);
8485 MODULE_VERSION(TPACPI_VERSION);
8486 MODULE_LICENSE("GPL");
8488 module_init(thinkpad_acpi_module_init);
8489 module_exit(thinkpad_acpi_module_exit);