thinkpad-acpi: issue backlight class events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob0fb9daa2888e7c0611e994b16ba6cccd7dd13655
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020600
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /* HKEY events */
149 enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
182 /* Thermal events */
183 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
189 /* Misc */
190 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
193 /****************************************************************************
194 * Main driver
197 #define TPACPI_NAME "thinkpad"
198 #define TPACPI_DESC "ThinkPad ACPI Extras"
199 #define TPACPI_FILE TPACPI_NAME "_acpi"
200 #define TPACPI_URL "http://ibm-acpi.sf.net/"
201 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
203 #define TPACPI_PROC_DIR "ibm"
204 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
205 #define TPACPI_DRVR_NAME TPACPI_FILE
206 #define TPACPI_DRVR_SHORTNAME "tpacpi"
207 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
209 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
210 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
212 #define TPACPI_MAX_ACPI_ARGS 3
214 /* rfkill switches */
215 enum {
216 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
217 TPACPI_RFK_WWAN_SW_ID,
218 TPACPI_RFK_UWB_SW_ID,
221 /* printk headers */
222 #define TPACPI_LOG TPACPI_FILE ": "
223 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
224 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
225 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
226 #define TPACPI_ERR KERN_ERR TPACPI_LOG
227 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
228 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
229 #define TPACPI_INFO KERN_INFO TPACPI_LOG
230 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
232 /* Debugging printk groups */
233 #define TPACPI_DBG_ALL 0xffff
234 #define TPACPI_DBG_DISCLOSETASK 0x8000
235 #define TPACPI_DBG_INIT 0x0001
236 #define TPACPI_DBG_EXIT 0x0002
237 #define TPACPI_DBG_RFKILL 0x0004
238 #define TPACPI_DBG_HKEY 0x0008
239 #define TPACPI_DBG_FAN 0x0010
240 #define TPACPI_DBG_BRGHT 0x0020
242 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
243 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
244 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
247 /****************************************************************************
248 * Driver-wide structs and misc. variables
251 struct ibm_struct;
253 struct tp_acpi_drv_struct {
254 const struct acpi_device_id *hid;
255 struct acpi_driver *driver;
257 void (*notify) (struct ibm_struct *, u32);
258 acpi_handle *handle;
259 u32 type;
260 struct acpi_device *device;
263 struct ibm_struct {
264 char *name;
266 int (*read) (char *);
267 int (*write) (char *);
268 void (*exit) (void);
269 void (*resume) (void);
270 void (*suspend) (pm_message_t state);
271 void (*shutdown) (void);
273 struct list_head all_drivers;
275 struct tp_acpi_drv_struct *acpi;
277 struct {
278 u8 acpi_driver_registered:1;
279 u8 acpi_notify_installed:1;
280 u8 proc_created:1;
281 u8 init_called:1;
282 u8 experimental:1;
283 } flags;
286 struct ibm_init_struct {
287 char param[32];
289 int (*init) (struct ibm_init_struct *);
290 struct ibm_struct *data;
293 static struct {
294 u32 bluetooth:1;
295 u32 hotkey:1;
296 u32 hotkey_mask:1;
297 u32 hotkey_wlsw:1;
298 u32 hotkey_tablet:1;
299 u32 light:1;
300 u32 light_status:1;
301 u32 bright_16levels:1;
302 u32 bright_acpimode:1;
303 u32 wan:1;
304 u32 uwb:1;
305 u32 fan_ctrl_status_undef:1;
306 u32 second_fan:1;
307 u32 beep_needs_two_args:1;
308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315 } tp_features;
317 static struct {
318 u16 hotkey_mask_ff:1;
319 } tp_warned;
321 struct thinkpad_id_data {
322 unsigned int vendor; /* ThinkPad vendor:
323 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
325 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
326 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
328 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
329 u16 ec_model;
330 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
331 u16 ec_release;
333 char *model_str; /* ThinkPad T43 */
334 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
336 static struct thinkpad_id_data thinkpad_id;
338 static enum {
339 TPACPI_LIFE_INIT = 0,
340 TPACPI_LIFE_RUNNING,
341 TPACPI_LIFE_EXITING,
342 } tpacpi_lifecycle;
344 static int experimental;
345 static u32 dbg_level;
347 static struct workqueue_struct *tpacpi_wq;
349 enum led_status_t {
350 TPACPI_LED_OFF = 0,
351 TPACPI_LED_ON,
352 TPACPI_LED_BLINK,
355 /* Special LED class that can defer work */
356 struct tpacpi_led_classdev {
357 struct led_classdev led_classdev;
358 struct work_struct work;
359 enum led_status_t new_state;
360 unsigned int led;
363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
364 static int dbg_wlswemul;
365 static int tpacpi_wlsw_emulstate;
366 static int dbg_bluetoothemul;
367 static int tpacpi_bluetooth_emulstate;
368 static int dbg_wwanemul;
369 static int tpacpi_wwan_emulstate;
370 static int dbg_uwbemul;
371 static int tpacpi_uwb_emulstate;
372 #endif
375 /*************************************************************************
376 * Debugging helpers
379 #define dbg_printk(a_dbg_level, format, arg...) \
380 do { if (dbg_level & (a_dbg_level)) \
381 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
382 } while (0)
384 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
385 #define vdbg_printk dbg_printk
386 static const char *str_supported(int is_supported);
387 #else
388 #define vdbg_printk(a_dbg_level, format, arg...) \
389 do { } while (0)
390 #endif
392 static void tpacpi_log_usertask(const char * const what)
394 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
395 what, task_tgid_vnr(current));
398 #define tpacpi_disclose_usertask(what, format, arg...) \
399 do { \
400 if (unlikely( \
401 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
402 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
403 printk(TPACPI_DEBUG "%s: PID %d: " format, \
404 what, task_tgid_vnr(current), ## arg); \
406 } while (0)
409 * Quirk handling helpers
411 * ThinkPad IDs and versions seen in the field so far
412 * are two-characters from the set [0-9A-Z], i.e. base 36.
414 * We use values well outside that range as specials.
417 #define TPACPI_MATCH_ANY 0xffffU
418 #define TPACPI_MATCH_UNKNOWN 0U
420 /* TPID('1', 'Y') == 0x5931 */
421 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
423 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
424 { .vendor = PCI_VENDOR_ID_IBM, \
425 .bios = TPID(__id1, __id2), \
426 .ec = TPACPI_MATCH_ANY, \
427 .quirks = (__quirk) }
429 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
430 { .vendor = PCI_VENDOR_ID_LENOVO, \
431 .bios = TPID(__id1, __id2), \
432 .ec = TPACPI_MATCH_ANY, \
433 .quirks = (__quirk) }
435 struct tpacpi_quirk {
436 unsigned int vendor;
437 u16 bios;
438 u16 ec;
439 unsigned long quirks;
443 * tpacpi_check_quirks() - search BIOS/EC version on a list
444 * @qlist: array of &struct tpacpi_quirk
445 * @qlist_size: number of elements in @qlist
447 * Iterates over a quirks list until one is found that matches the
448 * ThinkPad's vendor, BIOS and EC model.
450 * Returns 0 if nothing matches, otherwise returns the quirks field of
451 * the matching &struct tpacpi_quirk entry.
453 * The match criteria is: vendor, ec and bios much match.
455 static unsigned long __init tpacpi_check_quirks(
456 const struct tpacpi_quirk *qlist,
457 unsigned int qlist_size)
459 while (qlist_size) {
460 if ((qlist->vendor == thinkpad_id.vendor ||
461 qlist->vendor == TPACPI_MATCH_ANY) &&
462 (qlist->bios == thinkpad_id.bios_model ||
463 qlist->bios == TPACPI_MATCH_ANY) &&
464 (qlist->ec == thinkpad_id.ec_model ||
465 qlist->ec == TPACPI_MATCH_ANY))
466 return qlist->quirks;
468 qlist_size--;
469 qlist++;
471 return 0;
475 /****************************************************************************
476 ****************************************************************************
478 * ACPI Helpers and device model
480 ****************************************************************************
481 ****************************************************************************/
483 /*************************************************************************
484 * ACPI basic handles
487 static acpi_handle root_handle;
489 #define TPACPI_HANDLE(object, parent, paths...) \
490 static acpi_handle object##_handle; \
491 static acpi_handle *object##_parent = &parent##_handle; \
492 static char *object##_path; \
493 static char *object##_paths[] = { paths }
495 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
496 "\\_SB.PCI.ISA.EC", /* 570 */
497 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
498 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
499 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
500 "\\_SB.PCI0.ICH3.EC0", /* R31 */
501 "\\_SB.PCI0.LPC.EC", /* all others */
504 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
505 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
507 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
508 /* T4x, X31, X40 */
509 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
510 "\\CMS", /* R40, R40e */
511 ); /* all others */
513 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
514 "^HKEY", /* R30, R31 */
515 "HKEY", /* all others */
516 ); /* 570 */
518 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
519 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
520 "\\_SB.PCI0.VID0", /* 770e */
521 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
522 "\\_SB.PCI0.AGP.VID", /* all others */
523 ); /* R30, R31 */
526 /*************************************************************************
527 * ACPI helpers
530 static int acpi_evalf(acpi_handle handle,
531 void *res, char *method, char *fmt, ...)
533 char *fmt0 = fmt;
534 struct acpi_object_list params;
535 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
536 struct acpi_buffer result, *resultp;
537 union acpi_object out_obj;
538 acpi_status status;
539 va_list ap;
540 char res_type;
541 int success;
542 int quiet;
544 if (!*fmt) {
545 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
546 return 0;
549 if (*fmt == 'q') {
550 quiet = 1;
551 fmt++;
552 } else
553 quiet = 0;
555 res_type = *(fmt++);
557 params.count = 0;
558 params.pointer = &in_objs[0];
560 va_start(ap, fmt);
561 while (*fmt) {
562 char c = *(fmt++);
563 switch (c) {
564 case 'd': /* int */
565 in_objs[params.count].integer.value = va_arg(ap, int);
566 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
567 break;
568 /* add more types as needed */
569 default:
570 printk(TPACPI_ERR "acpi_evalf() called "
571 "with invalid format character '%c'\n", c);
572 return 0;
575 va_end(ap);
577 if (res_type != 'v') {
578 result.length = sizeof(out_obj);
579 result.pointer = &out_obj;
580 resultp = &result;
581 } else
582 resultp = NULL;
584 status = acpi_evaluate_object(handle, method, &params, resultp);
586 switch (res_type) {
587 case 'd': /* int */
588 if (res)
589 *(int *)res = out_obj.integer.value;
590 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
591 break;
592 case 'v': /* void */
593 success = status == AE_OK;
594 break;
595 /* add more types as needed */
596 default:
597 printk(TPACPI_ERR "acpi_evalf() called "
598 "with invalid format character '%c'\n", res_type);
599 return 0;
602 if (!success && !quiet)
603 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
604 method, fmt0, status);
606 return success;
609 static int acpi_ec_read(int i, u8 *p)
611 int v;
613 if (ecrd_handle) {
614 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
615 return 0;
616 *p = v;
617 } else {
618 if (ec_read(i, p) < 0)
619 return 0;
622 return 1;
625 static int acpi_ec_write(int i, u8 v)
627 if (ecwr_handle) {
628 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
629 return 0;
630 } else {
631 if (ec_write(i, v) < 0)
632 return 0;
635 return 1;
638 static int issue_thinkpad_cmos_command(int cmos_cmd)
640 if (!cmos_handle)
641 return -ENXIO;
643 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
644 return -EIO;
646 return 0;
649 /*************************************************************************
650 * ACPI device model
653 #define TPACPI_ACPIHANDLE_INIT(object) \
654 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
655 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
657 static void drv_acpi_handle_init(char *name,
658 acpi_handle *handle, acpi_handle parent,
659 char **paths, int num_paths, char **path)
661 int i;
662 acpi_status status;
664 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
665 name);
667 for (i = 0; i < num_paths; i++) {
668 status = acpi_get_handle(parent, paths[i], handle);
669 if (ACPI_SUCCESS(status)) {
670 *path = paths[i];
671 dbg_printk(TPACPI_DBG_INIT,
672 "Found ACPI handle %s for %s\n",
673 *path, name);
674 return;
678 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
679 name);
680 *handle = NULL;
683 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
685 struct ibm_struct *ibm = data;
687 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
688 return;
690 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
691 return;
693 ibm->acpi->notify(ibm, event);
696 static int __init setup_acpi_notify(struct ibm_struct *ibm)
698 acpi_status status;
699 int rc;
701 BUG_ON(!ibm->acpi);
703 if (!*ibm->acpi->handle)
704 return 0;
706 vdbg_printk(TPACPI_DBG_INIT,
707 "setting up ACPI notify for %s\n", ibm->name);
709 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
710 if (rc < 0) {
711 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
712 ibm->name, rc);
713 return -ENODEV;
716 acpi_driver_data(ibm->acpi->device) = ibm;
717 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
718 TPACPI_ACPI_EVENT_PREFIX,
719 ibm->name);
721 status = acpi_install_notify_handler(*ibm->acpi->handle,
722 ibm->acpi->type, dispatch_acpi_notify, ibm);
723 if (ACPI_FAILURE(status)) {
724 if (status == AE_ALREADY_EXISTS) {
725 printk(TPACPI_NOTICE
726 "another device driver is already "
727 "handling %s events\n", ibm->name);
728 } else {
729 printk(TPACPI_ERR
730 "acpi_install_notify_handler(%s) failed: %d\n",
731 ibm->name, status);
733 return -ENODEV;
735 ibm->flags.acpi_notify_installed = 1;
736 return 0;
739 static int __init tpacpi_device_add(struct acpi_device *device)
741 return 0;
744 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
746 int rc;
748 dbg_printk(TPACPI_DBG_INIT,
749 "registering %s as an ACPI driver\n", ibm->name);
751 BUG_ON(!ibm->acpi);
753 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
754 if (!ibm->acpi->driver) {
755 printk(TPACPI_ERR
756 "failed to allocate memory for ibm->acpi->driver\n");
757 return -ENOMEM;
760 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
761 ibm->acpi->driver->ids = ibm->acpi->hid;
763 ibm->acpi->driver->ops.add = &tpacpi_device_add;
765 rc = acpi_bus_register_driver(ibm->acpi->driver);
766 if (rc < 0) {
767 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
768 ibm->name, rc);
769 kfree(ibm->acpi->driver);
770 ibm->acpi->driver = NULL;
771 } else if (!rc)
772 ibm->flags.acpi_driver_registered = 1;
774 return rc;
778 /****************************************************************************
779 ****************************************************************************
781 * Procfs Helpers
783 ****************************************************************************
784 ****************************************************************************/
786 static int dispatch_procfs_read(char *page, char **start, off_t off,
787 int count, int *eof, void *data)
789 struct ibm_struct *ibm = data;
790 int len;
792 if (!ibm || !ibm->read)
793 return -EINVAL;
795 len = ibm->read(page);
796 if (len < 0)
797 return len;
799 if (len <= off + count)
800 *eof = 1;
801 *start = page + off;
802 len -= off;
803 if (len > count)
804 len = count;
805 if (len < 0)
806 len = 0;
808 return len;
811 static int dispatch_procfs_write(struct file *file,
812 const char __user *userbuf,
813 unsigned long count, void *data)
815 struct ibm_struct *ibm = data;
816 char *kernbuf;
817 int ret;
819 if (!ibm || !ibm->write)
820 return -EINVAL;
821 if (count > PAGE_SIZE - 2)
822 return -EINVAL;
824 kernbuf = kmalloc(count + 2, GFP_KERNEL);
825 if (!kernbuf)
826 return -ENOMEM;
828 if (copy_from_user(kernbuf, userbuf, count)) {
829 kfree(kernbuf);
830 return -EFAULT;
833 kernbuf[count] = 0;
834 strcat(kernbuf, ",");
835 ret = ibm->write(kernbuf);
836 if (ret == 0)
837 ret = count;
839 kfree(kernbuf);
841 return ret;
844 static char *next_cmd(char **cmds)
846 char *start = *cmds;
847 char *end;
849 while ((end = strchr(start, ',')) && end == start)
850 start = end + 1;
852 if (!end)
853 return NULL;
855 *end = 0;
856 *cmds = end + 1;
857 return start;
861 /****************************************************************************
862 ****************************************************************************
864 * Device model: input, hwmon and platform
866 ****************************************************************************
867 ****************************************************************************/
869 static struct platform_device *tpacpi_pdev;
870 static struct platform_device *tpacpi_sensors_pdev;
871 static struct device *tpacpi_hwmon;
872 static struct input_dev *tpacpi_inputdev;
873 static struct mutex tpacpi_inputdev_send_mutex;
874 static LIST_HEAD(tpacpi_all_drivers);
876 static int tpacpi_suspend_handler(struct platform_device *pdev,
877 pm_message_t state)
879 struct ibm_struct *ibm, *itmp;
881 list_for_each_entry_safe(ibm, itmp,
882 &tpacpi_all_drivers,
883 all_drivers) {
884 if (ibm->suspend)
885 (ibm->suspend)(state);
888 return 0;
891 static int tpacpi_resume_handler(struct platform_device *pdev)
893 struct ibm_struct *ibm, *itmp;
895 list_for_each_entry_safe(ibm, itmp,
896 &tpacpi_all_drivers,
897 all_drivers) {
898 if (ibm->resume)
899 (ibm->resume)();
902 return 0;
905 static void tpacpi_shutdown_handler(struct platform_device *pdev)
907 struct ibm_struct *ibm, *itmp;
909 list_for_each_entry_safe(ibm, itmp,
910 &tpacpi_all_drivers,
911 all_drivers) {
912 if (ibm->shutdown)
913 (ibm->shutdown)();
917 static struct platform_driver tpacpi_pdriver = {
918 .driver = {
919 .name = TPACPI_DRVR_NAME,
920 .owner = THIS_MODULE,
922 .suspend = tpacpi_suspend_handler,
923 .resume = tpacpi_resume_handler,
924 .shutdown = tpacpi_shutdown_handler,
927 static struct platform_driver tpacpi_hwmon_pdriver = {
928 .driver = {
929 .name = TPACPI_HWMON_DRVR_NAME,
930 .owner = THIS_MODULE,
934 /*************************************************************************
935 * sysfs support helpers
938 struct attribute_set {
939 unsigned int members, max_members;
940 struct attribute_group group;
943 struct attribute_set_obj {
944 struct attribute_set s;
945 struct attribute *a;
946 } __attribute__((packed));
948 static struct attribute_set *create_attr_set(unsigned int max_members,
949 const char *name)
951 struct attribute_set_obj *sobj;
953 if (max_members == 0)
954 return NULL;
956 /* Allocates space for implicit NULL at the end too */
957 sobj = kzalloc(sizeof(struct attribute_set_obj) +
958 max_members * sizeof(struct attribute *),
959 GFP_KERNEL);
960 if (!sobj)
961 return NULL;
962 sobj->s.max_members = max_members;
963 sobj->s.group.attrs = &sobj->a;
964 sobj->s.group.name = name;
966 return &sobj->s;
969 #define destroy_attr_set(_set) \
970 kfree(_set);
972 /* not multi-threaded safe, use it in a single thread per set */
973 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
975 if (!s || !attr)
976 return -EINVAL;
978 if (s->members >= s->max_members)
979 return -ENOMEM;
981 s->group.attrs[s->members] = attr;
982 s->members++;
984 return 0;
987 static int add_many_to_attr_set(struct attribute_set *s,
988 struct attribute **attr,
989 unsigned int count)
991 int i, res;
993 for (i = 0; i < count; i++) {
994 res = add_to_attr_set(s, attr[i]);
995 if (res)
996 return res;
999 return 0;
1002 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1004 sysfs_remove_group(kobj, &s->group);
1005 destroy_attr_set(s);
1008 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1009 sysfs_create_group(_kobj, &_attr_set->group)
1011 static int parse_strtoul(const char *buf,
1012 unsigned long max, unsigned long *value)
1014 char *endp;
1016 while (*buf && isspace(*buf))
1017 buf++;
1018 *value = simple_strtoul(buf, &endp, 0);
1019 while (*endp && isspace(*endp))
1020 endp++;
1021 if (*endp || *value > max)
1022 return -EINVAL;
1024 return 0;
1027 static void tpacpi_disable_brightness_delay(void)
1029 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1030 printk(TPACPI_NOTICE
1031 "ACPI backlight control delay disabled\n");
1034 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1036 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1037 union acpi_object *obj;
1038 int rc;
1040 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1041 obj = (union acpi_object *)buffer.pointer;
1042 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1043 printk(TPACPI_ERR "Unknown _BCL data, "
1044 "please report this to %s\n", TPACPI_MAIL);
1045 rc = 0;
1046 } else {
1047 rc = obj->package.count;
1049 } else {
1050 return 0;
1053 kfree(buffer.pointer);
1054 return rc;
1057 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1058 u32 lvl, void *context, void **rv)
1060 char name[ACPI_PATH_SEGMENT_LENGTH];
1061 struct acpi_buffer buffer = { sizeof(name), &name };
1063 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1064 !strncmp("_BCL", name, sizeof(name) - 1)) {
1065 BUG_ON(!rv || !*rv);
1066 **(int **)rv = tpacpi_query_bcl_levels(handle);
1067 return AE_CTRL_TERMINATE;
1068 } else {
1069 return AE_OK;
1074 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1076 static int __init tpacpi_check_std_acpi_brightness_support(void)
1078 int status;
1079 int bcl_levels = 0;
1080 void *bcl_ptr = &bcl_levels;
1082 if (!vid_handle) {
1083 TPACPI_ACPIHANDLE_INIT(vid);
1085 if (!vid_handle)
1086 return 0;
1089 * Search for a _BCL method, and execute it. This is safe on all
1090 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1091 * BIOS in ACPI backlight control mode. We do NOT have to care
1092 * about calling the _BCL method in an enabled video device, any
1093 * will do for our purposes.
1096 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1097 tpacpi_acpi_walk_find_bcl, NULL,
1098 &bcl_ptr);
1100 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1101 tp_features.bright_acpimode = 1;
1102 return (bcl_levels - 2);
1105 return 0;
1108 static int __init tpacpi_new_rfkill(const unsigned int id,
1109 struct rfkill **rfk,
1110 const enum rfkill_type rfktype,
1111 const char *name,
1112 const bool set_default,
1113 int (*toggle_radio)(void *, enum rfkill_state),
1114 int (*get_state)(void *, enum rfkill_state *))
1116 int res;
1117 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1119 res = get_state(NULL, &initial_state);
1120 if (res < 0) {
1121 printk(TPACPI_ERR
1122 "failed to read initial state for %s, error %d; "
1123 "will turn radio off\n", name, res);
1124 } else if (set_default) {
1125 /* try to set the initial state as the default for the rfkill
1126 * type, since we ask the firmware to preserve it across S5 in
1127 * NVRAM */
1128 if (rfkill_set_default(rfktype,
1129 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1130 RFKILL_STATE_UNBLOCKED :
1131 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1132 vdbg_printk(TPACPI_DBG_RFKILL,
1133 "Default state for %s cannot be changed\n",
1134 name);
1137 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1138 if (!*rfk) {
1139 printk(TPACPI_ERR
1140 "failed to allocate memory for rfkill class\n");
1141 return -ENOMEM;
1144 (*rfk)->name = name;
1145 (*rfk)->get_state = get_state;
1146 (*rfk)->toggle_radio = toggle_radio;
1147 (*rfk)->state = initial_state;
1149 res = rfkill_register(*rfk);
1150 if (res < 0) {
1151 printk(TPACPI_ERR
1152 "failed to register %s rfkill switch: %d\n",
1153 name, res);
1154 rfkill_free(*rfk);
1155 *rfk = NULL;
1156 return res;
1159 return 0;
1162 static void printk_deprecated_attribute(const char * const what,
1163 const char * const details)
1165 tpacpi_log_usertask("deprecated sysfs attribute");
1166 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1167 "will be removed. %s\n",
1168 what, details);
1171 static void printk_deprecated_rfkill_attribute(const char * const what)
1173 printk_deprecated_attribute(what,
1174 "Please switch to generic rfkill before year 2010");
1177 /*************************************************************************
1178 * thinkpad-acpi driver attributes
1181 /* interface_version --------------------------------------------------- */
1182 static ssize_t tpacpi_driver_interface_version_show(
1183 struct device_driver *drv,
1184 char *buf)
1186 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1189 static DRIVER_ATTR(interface_version, S_IRUGO,
1190 tpacpi_driver_interface_version_show, NULL);
1192 /* debug_level --------------------------------------------------------- */
1193 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1194 char *buf)
1196 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1199 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1200 const char *buf, size_t count)
1202 unsigned long t;
1204 if (parse_strtoul(buf, 0xffff, &t))
1205 return -EINVAL;
1207 dbg_level = t;
1209 return count;
1212 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1213 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1215 /* version ------------------------------------------------------------- */
1216 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1217 char *buf)
1219 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1220 TPACPI_DESC, TPACPI_VERSION);
1223 static DRIVER_ATTR(version, S_IRUGO,
1224 tpacpi_driver_version_show, NULL);
1226 /* --------------------------------------------------------------------- */
1228 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1230 static void tpacpi_send_radiosw_update(void);
1232 /* wlsw_emulstate ------------------------------------------------------ */
1233 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1234 char *buf)
1236 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1239 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1240 const char *buf, size_t count)
1242 unsigned long t;
1244 if (parse_strtoul(buf, 1, &t))
1245 return -EINVAL;
1247 if (tpacpi_wlsw_emulstate != t) {
1248 tpacpi_wlsw_emulstate = !!t;
1249 tpacpi_send_radiosw_update();
1250 } else
1251 tpacpi_wlsw_emulstate = !!t;
1253 return count;
1256 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1257 tpacpi_driver_wlsw_emulstate_show,
1258 tpacpi_driver_wlsw_emulstate_store);
1260 /* bluetooth_emulstate ------------------------------------------------- */
1261 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1262 struct device_driver *drv,
1263 char *buf)
1265 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1268 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1269 struct device_driver *drv,
1270 const char *buf, size_t count)
1272 unsigned long t;
1274 if (parse_strtoul(buf, 1, &t))
1275 return -EINVAL;
1277 tpacpi_bluetooth_emulstate = !!t;
1279 return count;
1282 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1283 tpacpi_driver_bluetooth_emulstate_show,
1284 tpacpi_driver_bluetooth_emulstate_store);
1286 /* wwan_emulstate ------------------------------------------------- */
1287 static ssize_t tpacpi_driver_wwan_emulstate_show(
1288 struct device_driver *drv,
1289 char *buf)
1291 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1294 static ssize_t tpacpi_driver_wwan_emulstate_store(
1295 struct device_driver *drv,
1296 const char *buf, size_t count)
1298 unsigned long t;
1300 if (parse_strtoul(buf, 1, &t))
1301 return -EINVAL;
1303 tpacpi_wwan_emulstate = !!t;
1305 return count;
1308 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1309 tpacpi_driver_wwan_emulstate_show,
1310 tpacpi_driver_wwan_emulstate_store);
1312 /* uwb_emulstate ------------------------------------------------- */
1313 static ssize_t tpacpi_driver_uwb_emulstate_show(
1314 struct device_driver *drv,
1315 char *buf)
1317 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1320 static ssize_t tpacpi_driver_uwb_emulstate_store(
1321 struct device_driver *drv,
1322 const char *buf, size_t count)
1324 unsigned long t;
1326 if (parse_strtoul(buf, 1, &t))
1327 return -EINVAL;
1329 tpacpi_uwb_emulstate = !!t;
1331 return count;
1334 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1335 tpacpi_driver_uwb_emulstate_show,
1336 tpacpi_driver_uwb_emulstate_store);
1337 #endif
1339 /* --------------------------------------------------------------------- */
1341 static struct driver_attribute *tpacpi_driver_attributes[] = {
1342 &driver_attr_debug_level, &driver_attr_version,
1343 &driver_attr_interface_version,
1346 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1348 int i, res;
1350 i = 0;
1351 res = 0;
1352 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1353 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1354 i++;
1357 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1358 if (!res && dbg_wlswemul)
1359 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1360 if (!res && dbg_bluetoothemul)
1361 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1362 if (!res && dbg_wwanemul)
1363 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1364 if (!res && dbg_uwbemul)
1365 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1366 #endif
1368 return res;
1371 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1373 int i;
1375 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1376 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1378 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1379 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1380 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1381 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1382 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1383 #endif
1386 /*************************************************************************
1387 * Firmware Data
1391 * Table of recommended minimum BIOS versions
1393 * Reasons for listing:
1394 * 1. Stable BIOS, listed because the unknown ammount of
1395 * bugs and bad ACPI behaviour on older versions
1397 * 2. BIOS or EC fw with known bugs that trigger on Linux
1399 * 3. BIOS with known reduced functionality in older versions
1401 * We recommend the latest BIOS and EC version.
1402 * We only support the latest BIOS and EC fw version as a rule.
1404 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1405 * Information from users in ThinkWiki
1407 * WARNING: we use this table also to detect that the machine is
1408 * a ThinkPad in some cases, so don't remove entries lightly.
1411 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1412 { .vendor = (__v), \
1413 .bios = TPID(__id1, __id2), \
1414 .ec = TPACPI_MATCH_ANY, \
1415 .quirks = TPACPI_MATCH_ANY << 16 \
1416 | (__bv1) << 8 | (__bv2) }
1418 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1419 __eid, __ev1, __ev2) \
1420 { .vendor = (__v), \
1421 .bios = TPID(__bid1, __bid2), \
1422 .ec = __eid, \
1423 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1424 | (__bv1) << 8 | (__bv2) }
1426 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1427 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1429 /* Outdated IBM BIOSes often lack the EC id string */
1430 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1431 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1432 __bv1, __bv2, TPID(__id1, __id2), \
1433 __ev1, __ev2), \
1434 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1435 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1436 __ev1, __ev2)
1438 /* Outdated IBM BIOSes often lack the EC id string */
1439 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1440 __eid1, __eid2, __ev1, __ev2) \
1441 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1442 __bv1, __bv2, TPID(__eid1, __eid2), \
1443 __ev1, __ev2), \
1444 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1445 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1446 __ev1, __ev2)
1448 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1449 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1451 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1452 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1453 __bv1, __bv2, TPID(__id1, __id2), \
1454 __ev1, __ev2)
1456 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1457 __eid1, __eid2, __ev1, __ev2) \
1458 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1459 __bv1, __bv2, TPID(__eid1, __eid2), \
1460 __ev1, __ev2)
1462 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1463 /* Numeric models ------------------ */
1464 /* FW MODEL BIOS VERS */
1465 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1466 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1467 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1468 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1469 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1470 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1471 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1472 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1473 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1475 /* A-series ------------------------- */
1476 /* FW MODEL BIOS VERS EC VERS */
1477 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1478 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1479 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1480 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1481 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1482 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1483 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1484 TPV_QI0('1', '3', '2', '0'), /* A22m */
1485 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1486 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1487 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1489 /* G-series ------------------------- */
1490 /* FW MODEL BIOS VERS */
1491 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1492 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1494 /* R-series, T-series --------------- */
1495 /* FW MODEL BIOS VERS EC VERS */
1496 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1497 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1498 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1499 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1500 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1501 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1502 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1503 T40/p, T41/p, T42/p (1) */
1504 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1505 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1506 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1507 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1509 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1510 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1511 TPV_QI0('1', '6', '3', '2'), /* T22 */
1512 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1513 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1514 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1516 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1517 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1518 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1520 /* BIOS FW BIOS VERS EC FW EC VERS */
1521 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1522 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1524 /* X-series ------------------------- */
1525 /* FW MODEL BIOS VERS EC VERS */
1526 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1527 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1528 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1529 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1530 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1531 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1532 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1534 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1535 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1537 /* (0) - older versions lack DMI EC fw string and functionality */
1538 /* (1) - older versions known to lack functionality */
1541 #undef TPV_QL1
1542 #undef TPV_QL0
1543 #undef TPV_QI2
1544 #undef TPV_QI1
1545 #undef TPV_QI0
1546 #undef TPV_Q_X
1547 #undef TPV_Q
1549 static void __init tpacpi_check_outdated_fw(void)
1551 unsigned long fwvers;
1552 u16 ec_version, bios_version;
1554 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1555 ARRAY_SIZE(tpacpi_bios_version_qtable));
1557 if (!fwvers)
1558 return;
1560 bios_version = fwvers & 0xffffU;
1561 ec_version = (fwvers >> 16) & 0xffffU;
1563 /* note that unknown versions are set to 0x0000 and we use that */
1564 if ((bios_version > thinkpad_id.bios_release) ||
1565 (ec_version > thinkpad_id.ec_release &&
1566 ec_version != TPACPI_MATCH_ANY)) {
1568 * The changelogs would let us track down the exact
1569 * reason, but it is just too much of a pain to track
1570 * it. We only list BIOSes that are either really
1571 * broken, or really stable to begin with, so it is
1572 * best if the user upgrades the firmware anyway.
1574 printk(TPACPI_WARN
1575 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1576 printk(TPACPI_WARN
1577 "WARNING: This firmware may be missing critical bug "
1578 "fixes and/or important features\n");
1582 static bool __init tpacpi_is_fw_known(void)
1584 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1585 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1588 /****************************************************************************
1589 ****************************************************************************
1591 * Subdrivers
1593 ****************************************************************************
1594 ****************************************************************************/
1596 /*************************************************************************
1597 * thinkpad-acpi init subdriver
1600 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1602 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1603 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1605 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1606 (thinkpad_id.bios_version_str) ?
1607 thinkpad_id.bios_version_str : "unknown",
1608 (thinkpad_id.ec_version_str) ?
1609 thinkpad_id.ec_version_str : "unknown");
1611 if (thinkpad_id.vendor && thinkpad_id.model_str)
1612 printk(TPACPI_INFO "%s %s, model %s\n",
1613 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1614 "IBM" : ((thinkpad_id.vendor ==
1615 PCI_VENDOR_ID_LENOVO) ?
1616 "Lenovo" : "Unknown vendor"),
1617 thinkpad_id.model_str,
1618 (thinkpad_id.nummodel_str) ?
1619 thinkpad_id.nummodel_str : "unknown");
1621 tpacpi_check_outdated_fw();
1622 return 0;
1625 static int thinkpad_acpi_driver_read(char *p)
1627 int len = 0;
1629 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1630 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1632 return len;
1635 static struct ibm_struct thinkpad_acpi_driver_data = {
1636 .name = "driver",
1637 .read = thinkpad_acpi_driver_read,
1640 /*************************************************************************
1641 * Hotkey subdriver
1645 * ThinkPad firmware event model
1647 * The ThinkPad firmware has two main event interfaces: normal ACPI
1648 * notifications (which follow the ACPI standard), and a private event
1649 * interface.
1651 * The private event interface also issues events for the hotkeys. As
1652 * the driver gained features, the event handling code ended up being
1653 * built around the hotkey subdriver. This will need to be refactored
1654 * to a more formal event API eventually.
1656 * Some "hotkeys" are actually supposed to be used as event reports,
1657 * such as "brightness has changed", "volume has changed", depending on
1658 * the ThinkPad model and how the firmware is operating.
1660 * Unlike other classes, hotkey-class events have mask/unmask control on
1661 * non-ancient firmware. However, how it behaves changes a lot with the
1662 * firmware model and version.
1665 enum { /* hot key scan codes (derived from ACPI DSDT) */
1666 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1667 TP_ACPI_HOTKEYSCAN_FNF2,
1668 TP_ACPI_HOTKEYSCAN_FNF3,
1669 TP_ACPI_HOTKEYSCAN_FNF4,
1670 TP_ACPI_HOTKEYSCAN_FNF5,
1671 TP_ACPI_HOTKEYSCAN_FNF6,
1672 TP_ACPI_HOTKEYSCAN_FNF7,
1673 TP_ACPI_HOTKEYSCAN_FNF8,
1674 TP_ACPI_HOTKEYSCAN_FNF9,
1675 TP_ACPI_HOTKEYSCAN_FNF10,
1676 TP_ACPI_HOTKEYSCAN_FNF11,
1677 TP_ACPI_HOTKEYSCAN_FNF12,
1678 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1679 TP_ACPI_HOTKEYSCAN_FNINSERT,
1680 TP_ACPI_HOTKEYSCAN_FNDELETE,
1681 TP_ACPI_HOTKEYSCAN_FNHOME,
1682 TP_ACPI_HOTKEYSCAN_FNEND,
1683 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1684 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1685 TP_ACPI_HOTKEYSCAN_FNSPACE,
1686 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1687 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1688 TP_ACPI_HOTKEYSCAN_MUTE,
1689 TP_ACPI_HOTKEYSCAN_THINKPAD,
1692 enum { /* Keys/events available through NVRAM polling */
1693 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1694 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1697 enum { /* Positions of some of the keys in hotkey masks */
1698 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1699 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1700 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1701 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1702 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1703 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1704 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1705 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1706 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1707 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1708 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1711 enum { /* NVRAM to ACPI HKEY group map */
1712 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1713 TP_ACPI_HKEY_ZOOM_MASK |
1714 TP_ACPI_HKEY_DISPSWTCH_MASK |
1715 TP_ACPI_HKEY_HIBERNATE_MASK,
1716 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1717 TP_ACPI_HKEY_BRGHTDWN_MASK,
1718 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1719 TP_ACPI_HKEY_VOLDWN_MASK |
1720 TP_ACPI_HKEY_MUTE_MASK,
1723 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1724 struct tp_nvram_state {
1725 u16 thinkpad_toggle:1;
1726 u16 zoom_toggle:1;
1727 u16 display_toggle:1;
1728 u16 thinklight_toggle:1;
1729 u16 hibernate_toggle:1;
1730 u16 displayexp_toggle:1;
1731 u16 display_state:1;
1732 u16 brightness_toggle:1;
1733 u16 volume_toggle:1;
1734 u16 mute:1;
1736 u8 brightness_level;
1737 u8 volume_level;
1740 /* kthread for the hotkey poller */
1741 static struct task_struct *tpacpi_hotkey_task;
1743 /* Acquired while the poller kthread is running, use to sync start/stop */
1744 static struct mutex hotkey_thread_mutex;
1747 * Acquire mutex to write poller control variables as an
1748 * atomic block.
1750 * Increment hotkey_config_change when changing them if you
1751 * want the kthread to forget old state.
1753 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1755 static struct mutex hotkey_thread_data_mutex;
1756 static unsigned int hotkey_config_change;
1759 * hotkey poller control variables
1761 * Must be atomic or readers will also need to acquire mutex
1763 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1764 * should be used only when the changes need to be taken as
1765 * a block, OR when one needs to force the kthread to forget
1766 * old state.
1768 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1769 static unsigned int hotkey_poll_freq = 10; /* Hz */
1771 #define HOTKEY_CONFIG_CRITICAL_START \
1772 do { \
1773 mutex_lock(&hotkey_thread_data_mutex); \
1774 hotkey_config_change++; \
1775 } while (0);
1776 #define HOTKEY_CONFIG_CRITICAL_END \
1777 mutex_unlock(&hotkey_thread_data_mutex);
1779 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1781 #define hotkey_source_mask 0U
1782 #define HOTKEY_CONFIG_CRITICAL_START
1783 #define HOTKEY_CONFIG_CRITICAL_END
1785 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1787 static struct mutex hotkey_mutex;
1789 static enum { /* Reasons for waking up */
1790 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1791 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1792 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1793 } hotkey_wakeup_reason;
1795 static int hotkey_autosleep_ack;
1797 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1798 static u32 hotkey_all_mask; /* all events supported in fw */
1799 static u32 hotkey_reserved_mask; /* events better left disabled */
1800 static u32 hotkey_driver_mask; /* events needed by the driver */
1801 static u32 hotkey_user_mask; /* events visible to userspace */
1802 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1804 static unsigned int hotkey_report_mode;
1806 static u16 *hotkey_keycode_map;
1808 static struct attribute_set *hotkey_dev_attributes;
1810 static void tpacpi_driver_event(const unsigned int hkey_event);
1811 static void hotkey_driver_event(const unsigned int scancode);
1813 /* HKEY.MHKG() return bits */
1814 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1816 static int hotkey_get_wlsw(int *status)
1818 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1819 if (dbg_wlswemul) {
1820 *status = !!tpacpi_wlsw_emulstate;
1821 return 0;
1823 #endif
1824 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1825 return -EIO;
1826 return 0;
1829 static int hotkey_get_tablet_mode(int *status)
1831 int s;
1833 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1834 return -EIO;
1836 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1837 return 0;
1841 * Reads current event mask from firmware, and updates
1842 * hotkey_acpi_mask accordingly. Also resets any bits
1843 * from hotkey_user_mask that are unavailable to be
1844 * delivered (shadow requirement of the userspace ABI).
1846 * Call with hotkey_mutex held
1848 static int hotkey_mask_get(void)
1850 if (tp_features.hotkey_mask) {
1851 u32 m = 0;
1853 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1854 return -EIO;
1856 hotkey_acpi_mask = m;
1857 } else {
1858 /* no mask support doesn't mean no event support... */
1859 hotkey_acpi_mask = hotkey_all_mask;
1862 /* sync userspace-visible mask */
1863 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1865 return 0;
1868 void static hotkey_mask_warn_incomplete_mask(void)
1870 /* log only what the user can fix... */
1871 const u32 wantedmask = hotkey_driver_mask &
1872 ~(hotkey_acpi_mask | hotkey_source_mask) &
1873 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1875 if (wantedmask)
1876 printk(TPACPI_NOTICE
1877 "required events 0x%08x not enabled!\n",
1878 wantedmask);
1882 * Set the firmware mask when supported
1884 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1886 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1888 * Call with hotkey_mutex held
1890 static int hotkey_mask_set(u32 mask)
1892 int i;
1893 int rc = 0;
1895 const u32 fwmask = mask & ~hotkey_source_mask;
1897 if (tp_features.hotkey_mask) {
1898 for (i = 0; i < 32; i++) {
1899 if (!acpi_evalf(hkey_handle,
1900 NULL, "MHKM", "vdd", i + 1,
1901 !!(mask & (1 << i)))) {
1902 rc = -EIO;
1903 break;
1909 * We *must* make an inconditional call to hotkey_mask_get to
1910 * refresh hotkey_acpi_mask and update hotkey_user_mask
1912 * Take the opportunity to also log when we cannot _enable_
1913 * a given event.
1915 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1916 printk(TPACPI_NOTICE
1917 "asked for hotkey mask 0x%08x, but "
1918 "firmware forced it to 0x%08x\n",
1919 fwmask, hotkey_acpi_mask);
1922 hotkey_mask_warn_incomplete_mask();
1924 return rc;
1928 * Sets hotkey_user_mask and tries to set the firmware mask
1930 * Call with hotkey_mutex held
1932 static int hotkey_user_mask_set(const u32 mask)
1934 int rc;
1936 /* Give people a chance to notice they are doing something that
1937 * is bound to go boom on their users sooner or later */
1938 if (!tp_warned.hotkey_mask_ff &&
1939 (mask == 0xffff || mask == 0xffffff ||
1940 mask == 0xffffffff)) {
1941 tp_warned.hotkey_mask_ff = 1;
1942 printk(TPACPI_NOTICE
1943 "setting the hotkey mask to 0x%08x is likely "
1944 "not the best way to go about it\n", mask);
1945 printk(TPACPI_NOTICE
1946 "please consider using the driver defaults, "
1947 "and refer to up-to-date thinkpad-acpi "
1948 "documentation\n");
1951 /* Try to enable what the user asked for, plus whatever we need.
1952 * this syncs everything but won't enable bits in hotkey_user_mask */
1953 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1955 /* Enable the available bits in hotkey_user_mask */
1956 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1958 return rc;
1962 * Sets the driver hotkey mask.
1964 * Can be called even if the hotkey subdriver is inactive
1966 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1968 int rc;
1970 /* Do the right thing if hotkey_init has not been called yet */
1971 if (!tp_features.hotkey) {
1972 hotkey_driver_mask = mask;
1973 return 0;
1976 mutex_lock(&hotkey_mutex);
1978 HOTKEY_CONFIG_CRITICAL_START
1979 hotkey_driver_mask = mask;
1980 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1981 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1982 #endif
1983 HOTKEY_CONFIG_CRITICAL_END
1985 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1986 ~hotkey_source_mask);
1987 mutex_unlock(&hotkey_mutex);
1989 return rc;
1992 static int hotkey_status_get(int *status)
1994 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1995 return -EIO;
1997 return 0;
2000 static int hotkey_status_set(bool enable)
2002 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2003 return -EIO;
2005 return 0;
2008 static void tpacpi_input_send_tabletsw(void)
2010 int state;
2012 if (tp_features.hotkey_tablet &&
2013 !hotkey_get_tablet_mode(&state)) {
2014 mutex_lock(&tpacpi_inputdev_send_mutex);
2016 input_report_switch(tpacpi_inputdev,
2017 SW_TABLET_MODE, !!state);
2018 input_sync(tpacpi_inputdev);
2020 mutex_unlock(&tpacpi_inputdev_send_mutex);
2024 /* Do NOT call without validating scancode first */
2025 static void tpacpi_input_send_key(const unsigned int scancode)
2027 const unsigned int keycode = hotkey_keycode_map[scancode];
2029 if (keycode != KEY_RESERVED) {
2030 mutex_lock(&tpacpi_inputdev_send_mutex);
2032 input_report_key(tpacpi_inputdev, keycode, 1);
2033 if (keycode == KEY_UNKNOWN)
2034 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2035 scancode);
2036 input_sync(tpacpi_inputdev);
2038 input_report_key(tpacpi_inputdev, keycode, 0);
2039 if (keycode == KEY_UNKNOWN)
2040 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2041 scancode);
2042 input_sync(tpacpi_inputdev);
2044 mutex_unlock(&tpacpi_inputdev_send_mutex);
2048 /* Do NOT call without validating scancode first */
2049 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2051 hotkey_driver_event(scancode);
2052 if (hotkey_user_mask & (1 << scancode))
2053 tpacpi_input_send_key(scancode);
2056 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2057 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2059 /* Do NOT call without validating scancode first */
2060 static void tpacpi_hotkey_send_key(unsigned int scancode)
2062 tpacpi_input_send_key_masked(scancode);
2063 if (hotkey_report_mode < 2) {
2064 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2065 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2069 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2071 u8 d;
2073 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2074 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2075 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2076 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2077 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2078 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2080 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2081 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2082 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2084 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2085 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2086 n->displayexp_toggle =
2087 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2089 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2090 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2091 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2092 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2093 n->brightness_toggle =
2094 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2096 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2097 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2098 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2099 >> TP_NVRAM_POS_LEVEL_VOLUME;
2100 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2101 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2105 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2106 struct tp_nvram_state *newn,
2107 const u32 event_mask)
2110 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2111 do { \
2112 if ((event_mask & (1 << __scancode)) && \
2113 oldn->__member != newn->__member) \
2114 tpacpi_hotkey_send_key(__scancode); \
2115 } while (0)
2117 #define TPACPI_MAY_SEND_KEY(__scancode) \
2118 do { \
2119 if (event_mask & (1 << __scancode)) \
2120 tpacpi_hotkey_send_key(__scancode); \
2121 } while (0)
2123 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2124 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2125 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2126 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2128 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2130 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2132 /* handle volume */
2133 if (oldn->volume_toggle != newn->volume_toggle) {
2134 if (oldn->mute != newn->mute) {
2135 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2137 if (oldn->volume_level > newn->volume_level) {
2138 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2139 } else if (oldn->volume_level < newn->volume_level) {
2140 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2141 } else if (oldn->mute == newn->mute) {
2142 /* repeated key presses that didn't change state */
2143 if (newn->mute) {
2144 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2145 } else if (newn->volume_level != 0) {
2146 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2147 } else {
2148 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2153 /* handle brightness */
2154 if (oldn->brightness_toggle != newn->brightness_toggle) {
2155 if (oldn->brightness_level < newn->brightness_level) {
2156 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2157 } else if (oldn->brightness_level > newn->brightness_level) {
2158 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2159 } else {
2160 /* repeated key presses that didn't change state */
2161 if (newn->brightness_level != 0) {
2162 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2163 } else {
2164 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2169 #undef TPACPI_COMPARE_KEY
2170 #undef TPACPI_MAY_SEND_KEY
2174 * Polling driver
2176 * We track all events in hotkey_source_mask all the time, since
2177 * most of them are edge-based. We only issue those requested by
2178 * hotkey_user_mask or hotkey_driver_mask, though.
2180 static int hotkey_kthread(void *data)
2182 struct tp_nvram_state s[2];
2183 u32 poll_mask, event_mask;
2184 unsigned int si, so;
2185 unsigned long t;
2186 unsigned int change_detector, must_reset;
2187 unsigned int poll_freq;
2189 mutex_lock(&hotkey_thread_mutex);
2191 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2192 goto exit;
2194 set_freezable();
2196 so = 0;
2197 si = 1;
2198 t = 0;
2200 /* Initial state for compares */
2201 mutex_lock(&hotkey_thread_data_mutex);
2202 change_detector = hotkey_config_change;
2203 poll_mask = hotkey_source_mask;
2204 event_mask = hotkey_source_mask &
2205 (hotkey_driver_mask | hotkey_user_mask);
2206 poll_freq = hotkey_poll_freq;
2207 mutex_unlock(&hotkey_thread_data_mutex);
2208 hotkey_read_nvram(&s[so], poll_mask);
2210 while (!kthread_should_stop()) {
2211 if (t == 0) {
2212 if (likely(poll_freq))
2213 t = 1000/poll_freq;
2214 else
2215 t = 100; /* should never happen... */
2217 t = msleep_interruptible(t);
2218 if (unlikely(kthread_should_stop()))
2219 break;
2220 must_reset = try_to_freeze();
2221 if (t > 0 && !must_reset)
2222 continue;
2224 mutex_lock(&hotkey_thread_data_mutex);
2225 if (must_reset || hotkey_config_change != change_detector) {
2226 /* forget old state on thaw or config change */
2227 si = so;
2228 t = 0;
2229 change_detector = hotkey_config_change;
2231 poll_mask = hotkey_source_mask;
2232 event_mask = hotkey_source_mask &
2233 (hotkey_driver_mask | hotkey_user_mask);
2234 poll_freq = hotkey_poll_freq;
2235 mutex_unlock(&hotkey_thread_data_mutex);
2237 if (likely(poll_mask)) {
2238 hotkey_read_nvram(&s[si], poll_mask);
2239 if (likely(si != so)) {
2240 hotkey_compare_and_issue_event(&s[so], &s[si],
2241 event_mask);
2245 so = si;
2246 si ^= 1;
2249 exit:
2250 mutex_unlock(&hotkey_thread_mutex);
2251 return 0;
2254 /* call with hotkey_mutex held */
2255 static void hotkey_poll_stop_sync(void)
2257 if (tpacpi_hotkey_task) {
2258 if (frozen(tpacpi_hotkey_task) ||
2259 freezing(tpacpi_hotkey_task))
2260 thaw_process(tpacpi_hotkey_task);
2262 kthread_stop(tpacpi_hotkey_task);
2263 tpacpi_hotkey_task = NULL;
2264 mutex_lock(&hotkey_thread_mutex);
2265 /* at this point, the thread did exit */
2266 mutex_unlock(&hotkey_thread_mutex);
2270 /* call with hotkey_mutex held */
2271 static void hotkey_poll_setup(bool may_warn)
2273 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2274 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2276 if (hotkey_poll_freq > 0 &&
2277 (poll_driver_mask ||
2278 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2279 if (!tpacpi_hotkey_task) {
2280 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2281 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2282 if (IS_ERR(tpacpi_hotkey_task)) {
2283 tpacpi_hotkey_task = NULL;
2284 printk(TPACPI_ERR
2285 "could not create kernel thread "
2286 "for hotkey polling\n");
2289 } else {
2290 hotkey_poll_stop_sync();
2291 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2292 hotkey_poll_freq == 0) {
2293 printk(TPACPI_NOTICE
2294 "hot keys 0x%08x and/or events 0x%08x "
2295 "require polling, which is currently "
2296 "disabled\n",
2297 poll_user_mask, poll_driver_mask);
2302 static void hotkey_poll_setup_safe(bool may_warn)
2304 mutex_lock(&hotkey_mutex);
2305 hotkey_poll_setup(may_warn);
2306 mutex_unlock(&hotkey_mutex);
2309 /* call with hotkey_mutex held */
2310 static void hotkey_poll_set_freq(unsigned int freq)
2312 if (!freq)
2313 hotkey_poll_stop_sync();
2315 hotkey_poll_freq = freq;
2318 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2320 static void hotkey_poll_setup_safe(bool __unused)
2324 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2326 static int hotkey_inputdev_open(struct input_dev *dev)
2328 switch (tpacpi_lifecycle) {
2329 case TPACPI_LIFE_INIT:
2331 * hotkey_init will call hotkey_poll_setup_safe
2332 * at the appropriate moment
2334 return 0;
2335 case TPACPI_LIFE_EXITING:
2336 return -EBUSY;
2337 case TPACPI_LIFE_RUNNING:
2338 hotkey_poll_setup_safe(false);
2339 return 0;
2342 /* Should only happen if tpacpi_lifecycle is corrupt */
2343 BUG();
2344 return -EBUSY;
2347 static void hotkey_inputdev_close(struct input_dev *dev)
2349 /* disable hotkey polling when possible */
2350 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2351 !(hotkey_source_mask & hotkey_driver_mask))
2352 hotkey_poll_setup_safe(false);
2355 /* sysfs hotkey enable ------------------------------------------------- */
2356 static ssize_t hotkey_enable_show(struct device *dev,
2357 struct device_attribute *attr,
2358 char *buf)
2360 int res, status;
2362 printk_deprecated_attribute("hotkey_enable",
2363 "Hotkey reporting is always enabled");
2365 res = hotkey_status_get(&status);
2366 if (res)
2367 return res;
2369 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2372 static ssize_t hotkey_enable_store(struct device *dev,
2373 struct device_attribute *attr,
2374 const char *buf, size_t count)
2376 unsigned long t;
2378 printk_deprecated_attribute("hotkey_enable",
2379 "Hotkeys can be disabled through hotkey_mask");
2381 if (parse_strtoul(buf, 1, &t))
2382 return -EINVAL;
2384 if (t == 0)
2385 return -EPERM;
2387 return count;
2390 static struct device_attribute dev_attr_hotkey_enable =
2391 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2392 hotkey_enable_show, hotkey_enable_store);
2394 /* sysfs hotkey mask --------------------------------------------------- */
2395 static ssize_t hotkey_mask_show(struct device *dev,
2396 struct device_attribute *attr,
2397 char *buf)
2399 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2402 static ssize_t hotkey_mask_store(struct device *dev,
2403 struct device_attribute *attr,
2404 const char *buf, size_t count)
2406 unsigned long t;
2407 int res;
2409 if (parse_strtoul(buf, 0xffffffffUL, &t))
2410 return -EINVAL;
2412 if (mutex_lock_killable(&hotkey_mutex))
2413 return -ERESTARTSYS;
2415 res = hotkey_user_mask_set(t);
2417 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2418 hotkey_poll_setup(true);
2419 #endif
2421 mutex_unlock(&hotkey_mutex);
2423 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2425 return (res) ? res : count;
2428 static struct device_attribute dev_attr_hotkey_mask =
2429 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2430 hotkey_mask_show, hotkey_mask_store);
2432 /* sysfs hotkey bios_enabled ------------------------------------------- */
2433 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2434 struct device_attribute *attr,
2435 char *buf)
2437 return sprintf(buf, "0\n");
2440 static struct device_attribute dev_attr_hotkey_bios_enabled =
2441 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2443 /* sysfs hotkey bios_mask ---------------------------------------------- */
2444 static ssize_t hotkey_bios_mask_show(struct device *dev,
2445 struct device_attribute *attr,
2446 char *buf)
2448 printk_deprecated_attribute("hotkey_bios_mask",
2449 "This attribute is useless.");
2450 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2453 static struct device_attribute dev_attr_hotkey_bios_mask =
2454 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2456 /* sysfs hotkey all_mask ----------------------------------------------- */
2457 static ssize_t hotkey_all_mask_show(struct device *dev,
2458 struct device_attribute *attr,
2459 char *buf)
2461 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2462 hotkey_all_mask | hotkey_source_mask);
2465 static struct device_attribute dev_attr_hotkey_all_mask =
2466 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2468 /* sysfs hotkey recommended_mask --------------------------------------- */
2469 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2470 struct device_attribute *attr,
2471 char *buf)
2473 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2474 (hotkey_all_mask | hotkey_source_mask)
2475 & ~hotkey_reserved_mask);
2478 static struct device_attribute dev_attr_hotkey_recommended_mask =
2479 __ATTR(hotkey_recommended_mask, S_IRUGO,
2480 hotkey_recommended_mask_show, NULL);
2482 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2484 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2485 static ssize_t hotkey_source_mask_show(struct device *dev,
2486 struct device_attribute *attr,
2487 char *buf)
2489 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2492 static ssize_t hotkey_source_mask_store(struct device *dev,
2493 struct device_attribute *attr,
2494 const char *buf, size_t count)
2496 unsigned long t;
2497 u32 r_ev;
2498 int rc;
2500 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2501 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2502 return -EINVAL;
2504 if (mutex_lock_killable(&hotkey_mutex))
2505 return -ERESTARTSYS;
2507 HOTKEY_CONFIG_CRITICAL_START
2508 hotkey_source_mask = t;
2509 HOTKEY_CONFIG_CRITICAL_END
2511 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2512 ~hotkey_source_mask);
2513 hotkey_poll_setup(true);
2515 /* check if events needed by the driver got disabled */
2516 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2517 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2519 mutex_unlock(&hotkey_mutex);
2521 if (rc < 0)
2522 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2523 "firmware event mask!\n");
2525 if (r_ev)
2526 printk(TPACPI_NOTICE "hotkey_source_mask: "
2527 "some important events were disabled: "
2528 "0x%04x\n", r_ev);
2530 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2532 return (rc < 0) ? rc : count;
2535 static struct device_attribute dev_attr_hotkey_source_mask =
2536 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2537 hotkey_source_mask_show, hotkey_source_mask_store);
2539 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2540 static ssize_t hotkey_poll_freq_show(struct device *dev,
2541 struct device_attribute *attr,
2542 char *buf)
2544 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2547 static ssize_t hotkey_poll_freq_store(struct device *dev,
2548 struct device_attribute *attr,
2549 const char *buf, size_t count)
2551 unsigned long t;
2553 if (parse_strtoul(buf, 25, &t))
2554 return -EINVAL;
2556 if (mutex_lock_killable(&hotkey_mutex))
2557 return -ERESTARTSYS;
2559 hotkey_poll_set_freq(t);
2560 hotkey_poll_setup(true);
2562 mutex_unlock(&hotkey_mutex);
2564 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2566 return count;
2569 static struct device_attribute dev_attr_hotkey_poll_freq =
2570 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2571 hotkey_poll_freq_show, hotkey_poll_freq_store);
2573 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2575 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2576 static ssize_t hotkey_radio_sw_show(struct device *dev,
2577 struct device_attribute *attr,
2578 char *buf)
2580 int res, s;
2581 res = hotkey_get_wlsw(&s);
2582 if (res < 0)
2583 return res;
2585 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2588 static struct device_attribute dev_attr_hotkey_radio_sw =
2589 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2591 static void hotkey_radio_sw_notify_change(void)
2593 if (tp_features.hotkey_wlsw)
2594 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2595 "hotkey_radio_sw");
2598 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2599 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2600 struct device_attribute *attr,
2601 char *buf)
2603 int res, s;
2604 res = hotkey_get_tablet_mode(&s);
2605 if (res < 0)
2606 return res;
2608 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2611 static struct device_attribute dev_attr_hotkey_tablet_mode =
2612 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2614 static void hotkey_tablet_mode_notify_change(void)
2616 if (tp_features.hotkey_tablet)
2617 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2618 "hotkey_tablet_mode");
2621 /* sysfs hotkey report_mode -------------------------------------------- */
2622 static ssize_t hotkey_report_mode_show(struct device *dev,
2623 struct device_attribute *attr,
2624 char *buf)
2626 return snprintf(buf, PAGE_SIZE, "%d\n",
2627 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2630 static struct device_attribute dev_attr_hotkey_report_mode =
2631 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2633 /* sysfs wakeup reason (pollable) -------------------------------------- */
2634 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2635 struct device_attribute *attr,
2636 char *buf)
2638 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2641 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2642 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2644 static void hotkey_wakeup_reason_notify_change(void)
2646 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2647 "wakeup_reason");
2650 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2651 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2652 struct device_attribute *attr,
2653 char *buf)
2655 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2658 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2659 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2660 hotkey_wakeup_hotunplug_complete_show, NULL);
2662 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2664 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2665 "wakeup_hotunplug_complete");
2668 /* --------------------------------------------------------------------- */
2670 static struct attribute *hotkey_attributes[] __initdata = {
2671 &dev_attr_hotkey_enable.attr,
2672 &dev_attr_hotkey_bios_enabled.attr,
2673 &dev_attr_hotkey_bios_mask.attr,
2674 &dev_attr_hotkey_report_mode.attr,
2675 &dev_attr_hotkey_wakeup_reason.attr,
2676 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2677 &dev_attr_hotkey_mask.attr,
2678 &dev_attr_hotkey_all_mask.attr,
2679 &dev_attr_hotkey_recommended_mask.attr,
2680 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2681 &dev_attr_hotkey_source_mask.attr,
2682 &dev_attr_hotkey_poll_freq.attr,
2683 #endif
2686 static void bluetooth_update_rfk(void);
2687 static void wan_update_rfk(void);
2688 static void uwb_update_rfk(void);
2689 static void tpacpi_send_radiosw_update(void)
2691 int wlsw;
2693 /* Sync these BEFORE sending any rfkill events */
2694 if (tp_features.bluetooth)
2695 bluetooth_update_rfk();
2696 if (tp_features.wan)
2697 wan_update_rfk();
2698 if (tp_features.uwb)
2699 uwb_update_rfk();
2701 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2702 mutex_lock(&tpacpi_inputdev_send_mutex);
2704 input_report_switch(tpacpi_inputdev,
2705 SW_RFKILL_ALL, !!wlsw);
2706 input_sync(tpacpi_inputdev);
2708 mutex_unlock(&tpacpi_inputdev_send_mutex);
2710 hotkey_radio_sw_notify_change();
2713 static void hotkey_exit(void)
2715 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2716 mutex_lock(&hotkey_mutex);
2717 hotkey_poll_stop_sync();
2718 mutex_unlock(&hotkey_mutex);
2719 #endif
2721 if (hotkey_dev_attributes)
2722 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2724 kfree(hotkey_keycode_map);
2726 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2727 "restoring original HKEY status and mask\n");
2728 /* yes, there is a bitwise or below, we want the
2729 * functions to be called even if one of them fail */
2730 if (((tp_features.hotkey_mask &&
2731 hotkey_mask_set(hotkey_orig_mask)) |
2732 hotkey_status_set(false)) != 0)
2733 printk(TPACPI_ERR
2734 "failed to restore hot key mask "
2735 "to BIOS defaults\n");
2738 static void __init hotkey_unmap(const unsigned int scancode)
2740 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2741 clear_bit(hotkey_keycode_map[scancode],
2742 tpacpi_inputdev->keybit);
2743 hotkey_keycode_map[scancode] = KEY_RESERVED;
2748 * HKEY quirks:
2749 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2752 #define TPACPI_HK_Q_INIMASK 0x0001
2754 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2755 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2756 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2757 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2758 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2759 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2760 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2761 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2762 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2763 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2764 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2765 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2766 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2767 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2768 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2769 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2770 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2771 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2772 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2773 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2776 static int __init hotkey_init(struct ibm_init_struct *iibm)
2778 /* Requirements for changing the default keymaps:
2780 * 1. Many of the keys are mapped to KEY_RESERVED for very
2781 * good reasons. Do not change them unless you have deep
2782 * knowledge on the IBM and Lenovo ThinkPad firmware for
2783 * the various ThinkPad models. The driver behaves
2784 * differently for KEY_RESERVED: such keys have their
2785 * hot key mask *unset* in mask_recommended, and also
2786 * in the initial hot key mask programmed into the
2787 * firmware at driver load time, which means the firm-
2788 * ware may react very differently if you change them to
2789 * something else;
2791 * 2. You must be subscribed to the linux-thinkpad and
2792 * ibm-acpi-devel mailing lists, and you should read the
2793 * list archives since 2007 if you want to change the
2794 * keymaps. This requirement exists so that you will
2795 * know the past history of problems with the thinkpad-
2796 * acpi driver keymaps, and also that you will be
2797 * listening to any bug reports;
2799 * 3. Do not send thinkpad-acpi specific patches directly to
2800 * for merging, *ever*. Send them to the linux-acpi
2801 * mailinglist for comments. Merging is to be done only
2802 * through acpi-test and the ACPI maintainer.
2804 * If the above is too much to ask, don't change the keymap.
2805 * Ask the thinkpad-acpi maintainer to do it, instead.
2807 static u16 ibm_keycode_map[] __initdata = {
2808 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2809 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2810 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2811 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2813 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2814 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2815 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2816 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2818 /* brightness: firmware always reacts to them */
2819 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2820 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2822 /* Thinklight: firmware always react to it */
2823 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2825 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2826 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2828 /* Volume: firmware always react to it and reprograms
2829 * the built-in *extra* mixer. Never map it to control
2830 * another mixer by default. */
2831 KEY_RESERVED, /* 0x14: VOLUME UP */
2832 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2833 KEY_RESERVED, /* 0x16: MUTE */
2835 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2837 /* (assignments unknown, please report if found) */
2838 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2839 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2841 static u16 lenovo_keycode_map[] __initdata = {
2842 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2843 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2844 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2845 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2847 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2848 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2849 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2850 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2852 /* These should be enabled --only-- when ACPI video
2853 * is disabled (i.e. in "vendor" mode), and are handled
2854 * in a special way by the init code */
2855 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2856 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2858 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2860 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2861 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2863 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2864 * react to it and reprograms the built-in *extra* mixer.
2865 * Never map it to control another mixer by default.
2867 * T60?, T61, R60?, R61: firmware and EC tries to send
2868 * these over the regular keyboard, so these are no-ops,
2869 * but there are still weird bugs re. MUTE, so do not
2870 * change unless you get test reports from all Lenovo
2871 * models. May cause the BIOS to interfere with the
2872 * HDA mixer.
2874 KEY_RESERVED, /* 0x14: VOLUME UP */
2875 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2876 KEY_RESERVED, /* 0x16: MUTE */
2878 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2880 /* (assignments unknown, please report if found) */
2881 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2882 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2885 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2886 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2887 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2889 int res, i;
2890 int status;
2891 int hkeyv;
2893 unsigned long quirks;
2895 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2896 "initializing hotkey subdriver\n");
2898 BUG_ON(!tpacpi_inputdev);
2899 BUG_ON(tpacpi_inputdev->open != NULL ||
2900 tpacpi_inputdev->close != NULL);
2902 TPACPI_ACPIHANDLE_INIT(hkey);
2903 mutex_init(&hotkey_mutex);
2905 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2906 mutex_init(&hotkey_thread_mutex);
2907 mutex_init(&hotkey_thread_data_mutex);
2908 #endif
2910 /* hotkey not supported on 570 */
2911 tp_features.hotkey = hkey_handle != NULL;
2913 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2914 "hotkeys are %s\n",
2915 str_supported(tp_features.hotkey));
2917 if (!tp_features.hotkey)
2918 return 1;
2920 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2921 ARRAY_SIZE(tpacpi_hotkey_qtable));
2923 tpacpi_disable_brightness_delay();
2925 /* MUST have enough space for all attributes to be added to
2926 * hotkey_dev_attributes */
2927 hotkey_dev_attributes = create_attr_set(
2928 ARRAY_SIZE(hotkey_attributes) + 2,
2929 NULL);
2930 if (!hotkey_dev_attributes)
2931 return -ENOMEM;
2932 res = add_many_to_attr_set(hotkey_dev_attributes,
2933 hotkey_attributes,
2934 ARRAY_SIZE(hotkey_attributes));
2935 if (res)
2936 goto err_exit;
2938 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2939 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2940 for HKEY interface version 0x100 */
2941 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2942 if ((hkeyv >> 8) != 1) {
2943 printk(TPACPI_ERR "unknown version of the "
2944 "HKEY interface: 0x%x\n", hkeyv);
2945 printk(TPACPI_ERR "please report this to %s\n",
2946 TPACPI_MAIL);
2947 } else {
2949 * MHKV 0x100 in A31, R40, R40e,
2950 * T4x, X31, and later
2952 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2953 "firmware HKEY interface version: 0x%x\n",
2954 hkeyv);
2956 /* Paranoia check AND init hotkey_all_mask */
2957 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2958 "MHKA", "qd")) {
2959 printk(TPACPI_ERR
2960 "missing MHKA handler, "
2961 "please report this to %s\n",
2962 TPACPI_MAIL);
2963 /* Fallback: pre-init for FN+F3,F4,F12 */
2964 hotkey_all_mask = 0x080cU;
2965 } else {
2966 tp_features.hotkey_mask = 1;
2971 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2972 "hotkey masks are %s\n",
2973 str_supported(tp_features.hotkey_mask));
2975 /* Init hotkey_all_mask if not initialized yet */
2976 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2977 (quirks & TPACPI_HK_Q_INIMASK))
2978 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2980 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2981 if (tp_features.hotkey_mask) {
2982 /* hotkey_source_mask *must* be zero for
2983 * the first hotkey_mask_get to return hotkey_orig_mask */
2984 res = hotkey_mask_get();
2985 if (res)
2986 goto err_exit;
2988 hotkey_orig_mask = hotkey_acpi_mask;
2989 } else {
2990 hotkey_orig_mask = hotkey_all_mask;
2991 hotkey_acpi_mask = hotkey_all_mask;
2994 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2995 if (dbg_wlswemul) {
2996 tp_features.hotkey_wlsw = 1;
2997 printk(TPACPI_INFO
2998 "radio switch emulation enabled\n");
2999 } else
3000 #endif
3001 /* Not all thinkpads have a hardware radio switch */
3002 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3003 tp_features.hotkey_wlsw = 1;
3004 printk(TPACPI_INFO
3005 "radio switch found; radios are %s\n",
3006 enabled(status, 0));
3008 if (tp_features.hotkey_wlsw)
3009 res = add_to_attr_set(hotkey_dev_attributes,
3010 &dev_attr_hotkey_radio_sw.attr);
3012 /* For X41t, X60t, X61t Tablets... */
3013 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3014 tp_features.hotkey_tablet = 1;
3015 printk(TPACPI_INFO
3016 "possible tablet mode switch found; "
3017 "ThinkPad in %s mode\n",
3018 (status & TP_HOTKEY_TABLET_MASK)?
3019 "tablet" : "laptop");
3020 res = add_to_attr_set(hotkey_dev_attributes,
3021 &dev_attr_hotkey_tablet_mode.attr);
3024 if (!res)
3025 res = register_attr_set_with_sysfs(
3026 hotkey_dev_attributes,
3027 &tpacpi_pdev->dev.kobj);
3028 if (res)
3029 goto err_exit;
3031 /* Set up key map */
3033 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3034 GFP_KERNEL);
3035 if (!hotkey_keycode_map) {
3036 printk(TPACPI_ERR
3037 "failed to allocate memory for key map\n");
3038 res = -ENOMEM;
3039 goto err_exit;
3042 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3043 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3044 "using Lenovo default hot key map\n");
3045 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3046 TPACPI_HOTKEY_MAP_SIZE);
3047 } else {
3048 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3049 "using IBM default hot key map\n");
3050 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3051 TPACPI_HOTKEY_MAP_SIZE);
3054 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3055 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3056 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3057 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3058 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3059 tpacpi_inputdev->keycode = hotkey_keycode_map;
3060 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3061 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3062 set_bit(hotkey_keycode_map[i],
3063 tpacpi_inputdev->keybit);
3064 } else {
3065 if (i < sizeof(hotkey_reserved_mask)*8)
3066 hotkey_reserved_mask |= 1 << i;
3070 if (tp_features.hotkey_wlsw) {
3071 set_bit(EV_SW, tpacpi_inputdev->evbit);
3072 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3074 if (tp_features.hotkey_tablet) {
3075 set_bit(EV_SW, tpacpi_inputdev->evbit);
3076 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3079 /* Do not issue duplicate brightness change events to
3080 * userspace */
3081 if (!tp_features.bright_acpimode)
3082 /* update bright_acpimode... */
3083 tpacpi_check_std_acpi_brightness_support();
3085 if (tp_features.bright_acpimode) {
3086 printk(TPACPI_INFO
3087 "This ThinkPad has standard ACPI backlight "
3088 "brightness control, supported by the ACPI "
3089 "video driver\n");
3090 printk(TPACPI_NOTICE
3091 "Disabling thinkpad-acpi brightness events "
3092 "by default...\n");
3094 /* Disable brightness up/down on Lenovo thinkpads when
3095 * ACPI is handling them, otherwise it is plain impossible
3096 * for userspace to do something even remotely sane */
3097 hotkey_reserved_mask |=
3098 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3099 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3100 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3101 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3104 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3105 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3106 & ~hotkey_all_mask
3107 & ~hotkey_reserved_mask;
3109 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3110 "hotkey source mask 0x%08x, polling freq %u\n",
3111 hotkey_source_mask, hotkey_poll_freq);
3112 #endif
3114 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3115 "enabling firmware HKEY event interface...\n");
3116 res = hotkey_status_set(true);
3117 if (res) {
3118 hotkey_exit();
3119 return res;
3121 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3122 | hotkey_driver_mask)
3123 & ~hotkey_source_mask);
3124 if (res < 0 && res != -ENXIO) {
3125 hotkey_exit();
3126 return res;
3128 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3129 & ~hotkey_reserved_mask;
3130 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3131 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3132 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3134 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3135 "legacy ibm/hotkey event reporting over procfs %s\n",
3136 (hotkey_report_mode < 2) ?
3137 "enabled" : "disabled");
3139 tpacpi_inputdev->open = &hotkey_inputdev_open;
3140 tpacpi_inputdev->close = &hotkey_inputdev_close;
3142 hotkey_poll_setup_safe(true);
3143 tpacpi_send_radiosw_update();
3144 tpacpi_input_send_tabletsw();
3146 return 0;
3148 err_exit:
3149 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3150 hotkey_dev_attributes = NULL;
3152 return (res < 0)? res : 1;
3155 static bool hotkey_notify_hotkey(const u32 hkey,
3156 bool *send_acpi_ev,
3157 bool *ignore_acpi_ev)
3159 /* 0x1000-0x1FFF: key presses */
3160 unsigned int scancode = hkey & 0xfff;
3161 *send_acpi_ev = true;
3162 *ignore_acpi_ev = false;
3164 if (scancode > 0 && scancode < 0x21) {
3165 scancode--;
3166 if (!(hotkey_source_mask & (1 << scancode))) {
3167 tpacpi_input_send_key_masked(scancode);
3168 *send_acpi_ev = false;
3169 } else {
3170 *ignore_acpi_ev = true;
3172 return true;
3174 return false;
3177 static bool hotkey_notify_wakeup(const u32 hkey,
3178 bool *send_acpi_ev,
3179 bool *ignore_acpi_ev)
3181 /* 0x2000-0x2FFF: Wakeup reason */
3182 *send_acpi_ev = true;
3183 *ignore_acpi_ev = false;
3185 switch (hkey) {
3186 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3187 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3188 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3189 *ignore_acpi_ev = true;
3190 break;
3192 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3193 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3194 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3195 *ignore_acpi_ev = true;
3196 break;
3198 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3199 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3200 printk(TPACPI_ALERT
3201 "EMERGENCY WAKEUP: battery almost empty\n");
3202 /* how to auto-heal: */
3203 /* 2313: woke up from S3, go to S4/S5 */
3204 /* 2413: woke up from S4, go to S5 */
3205 break;
3207 default:
3208 return false;
3211 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3212 printk(TPACPI_INFO
3213 "woke up due to a hot-unplug "
3214 "request...\n");
3215 hotkey_wakeup_reason_notify_change();
3217 return true;
3220 static bool hotkey_notify_usrevent(const u32 hkey,
3221 bool *send_acpi_ev,
3222 bool *ignore_acpi_ev)
3224 /* 0x5000-0x5FFF: human interface helpers */
3225 *send_acpi_ev = true;
3226 *ignore_acpi_ev = false;
3228 switch (hkey) {
3229 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3230 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3231 return true;
3233 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3234 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3235 tpacpi_input_send_tabletsw();
3236 hotkey_tablet_mode_notify_change();
3237 *send_acpi_ev = false;
3238 return true;
3240 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3241 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3242 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3243 /* do not propagate these events */
3244 *ignore_acpi_ev = true;
3245 return true;
3247 default:
3248 return false;
3252 static bool hotkey_notify_thermal(const u32 hkey,
3253 bool *send_acpi_ev,
3254 bool *ignore_acpi_ev)
3256 /* 0x6000-0x6FFF: thermal alarms */
3257 *send_acpi_ev = true;
3258 *ignore_acpi_ev = false;
3260 switch (hkey) {
3261 case TP_HKEY_EV_ALARM_BAT_HOT:
3262 printk(TPACPI_CRIT
3263 "THERMAL ALARM: battery is too hot!\n");
3264 /* recommended action: warn user through gui */
3265 return true;
3266 case TP_HKEY_EV_ALARM_BAT_XHOT:
3267 printk(TPACPI_ALERT
3268 "THERMAL EMERGENCY: battery is extremely hot!\n");
3269 /* recommended action: immediate sleep/hibernate */
3270 return true;
3271 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3272 printk(TPACPI_CRIT
3273 "THERMAL ALARM: "
3274 "a sensor reports something is too hot!\n");
3275 /* recommended action: warn user through gui, that */
3276 /* some internal component is too hot */
3277 return true;
3278 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3279 printk(TPACPI_ALERT
3280 "THERMAL EMERGENCY: "
3281 "a sensor reports something is extremely hot!\n");
3282 /* recommended action: immediate sleep/hibernate */
3283 return true;
3284 case TP_HKEY_EV_THM_TABLE_CHANGED:
3285 printk(TPACPI_INFO
3286 "EC reports that Thermal Table has changed\n");
3287 /* recommended action: do nothing, we don't have
3288 * Lenovo ATM information */
3289 return true;
3290 default:
3291 printk(TPACPI_ALERT
3292 "THERMAL ALERT: unknown thermal alarm received\n");
3293 return false;
3297 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3299 u32 hkey;
3300 bool send_acpi_ev;
3301 bool ignore_acpi_ev;
3302 bool known_ev;
3304 if (event != 0x80) {
3305 printk(TPACPI_ERR
3306 "unknown HKEY notification event %d\n", event);
3307 /* forward it to userspace, maybe it knows how to handle it */
3308 acpi_bus_generate_netlink_event(
3309 ibm->acpi->device->pnp.device_class,
3310 dev_name(&ibm->acpi->device->dev),
3311 event, 0);
3312 return;
3315 while (1) {
3316 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3317 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3318 return;
3321 if (hkey == 0) {
3322 /* queue empty */
3323 return;
3326 send_acpi_ev = true;
3327 ignore_acpi_ev = false;
3329 switch (hkey >> 12) {
3330 case 1:
3331 /* 0x1000-0x1FFF: key presses */
3332 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3333 &ignore_acpi_ev);
3334 break;
3335 case 2:
3336 /* 0x2000-0x2FFF: Wakeup reason */
3337 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3338 &ignore_acpi_ev);
3339 break;
3340 case 3:
3341 /* 0x3000-0x3FFF: bay-related wakeups */
3342 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3343 hotkey_autosleep_ack = 1;
3344 printk(TPACPI_INFO
3345 "bay ejected\n");
3346 hotkey_wakeup_hotunplug_complete_notify_change();
3347 known_ev = true;
3348 } else {
3349 known_ev = false;
3351 break;
3352 case 4:
3353 /* 0x4000-0x4FFF: dock-related wakeups */
3354 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3355 hotkey_autosleep_ack = 1;
3356 printk(TPACPI_INFO
3357 "undocked\n");
3358 hotkey_wakeup_hotunplug_complete_notify_change();
3359 known_ev = true;
3360 } else {
3361 known_ev = false;
3363 break;
3364 case 5:
3365 /* 0x5000-0x5FFF: human interface helpers */
3366 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3367 &ignore_acpi_ev);
3368 break;
3369 case 6:
3370 /* 0x6000-0x6FFF: thermal alarms */
3371 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3372 &ignore_acpi_ev);
3373 break;
3374 case 7:
3375 /* 0x7000-0x7FFF: misc */
3376 if (tp_features.hotkey_wlsw &&
3377 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3378 tpacpi_send_radiosw_update();
3379 send_acpi_ev = 0;
3380 known_ev = true;
3381 break;
3383 /* fallthrough to default */
3384 default:
3385 known_ev = false;
3387 if (!known_ev) {
3388 printk(TPACPI_NOTICE
3389 "unhandled HKEY event 0x%04x\n", hkey);
3390 printk(TPACPI_NOTICE
3391 "please report the conditions when this "
3392 "event happened to %s\n", TPACPI_MAIL);
3395 /* Legacy events */
3396 if (!ignore_acpi_ev &&
3397 (send_acpi_ev || hotkey_report_mode < 2)) {
3398 acpi_bus_generate_proc_event(ibm->acpi->device,
3399 event, hkey);
3402 /* netlink events */
3403 if (!ignore_acpi_ev && send_acpi_ev) {
3404 acpi_bus_generate_netlink_event(
3405 ibm->acpi->device->pnp.device_class,
3406 dev_name(&ibm->acpi->device->dev),
3407 event, hkey);
3412 static void hotkey_suspend(pm_message_t state)
3414 /* Do these on suspend, we get the events on early resume! */
3415 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3416 hotkey_autosleep_ack = 0;
3419 static void hotkey_resume(void)
3421 tpacpi_disable_brightness_delay();
3423 if (hotkey_status_set(true) < 0 ||
3424 hotkey_mask_set(hotkey_acpi_mask) < 0)
3425 printk(TPACPI_ERR
3426 "error while attempting to reset the event "
3427 "firmware interface\n");
3429 tpacpi_send_radiosw_update();
3430 hotkey_tablet_mode_notify_change();
3431 hotkey_wakeup_reason_notify_change();
3432 hotkey_wakeup_hotunplug_complete_notify_change();
3433 hotkey_poll_setup_safe(false);
3436 /* procfs -------------------------------------------------------------- */
3437 static int hotkey_read(char *p)
3439 int res, status;
3440 int len = 0;
3442 if (!tp_features.hotkey) {
3443 len += sprintf(p + len, "status:\t\tnot supported\n");
3444 return len;
3447 if (mutex_lock_killable(&hotkey_mutex))
3448 return -ERESTARTSYS;
3449 res = hotkey_status_get(&status);
3450 if (!res)
3451 res = hotkey_mask_get();
3452 mutex_unlock(&hotkey_mutex);
3453 if (res)
3454 return res;
3456 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3457 if (hotkey_all_mask) {
3458 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3459 len += sprintf(p + len,
3460 "commands:\tenable, disable, reset, <mask>\n");
3461 } else {
3462 len += sprintf(p + len, "mask:\t\tnot supported\n");
3463 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3466 return len;
3469 static void hotkey_enabledisable_warn(bool enable)
3471 tpacpi_log_usertask("procfs hotkey enable/disable");
3472 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3473 TPACPI_WARN
3474 "hotkey enable/disable functionality has been "
3475 "removed from the driver. Hotkeys are always "
3476 "enabled\n"))
3477 printk(TPACPI_ERR
3478 "Please remove the hotkey=enable module "
3479 "parameter, it is deprecated. Hotkeys are always "
3480 "enabled\n");
3483 static int hotkey_write(char *buf)
3485 int res;
3486 u32 mask;
3487 char *cmd;
3489 if (!tp_features.hotkey)
3490 return -ENODEV;
3492 if (mutex_lock_killable(&hotkey_mutex))
3493 return -ERESTARTSYS;
3495 mask = hotkey_user_mask;
3497 res = 0;
3498 while ((cmd = next_cmd(&buf))) {
3499 if (strlencmp(cmd, "enable") == 0) {
3500 hotkey_enabledisable_warn(1);
3501 } else if (strlencmp(cmd, "disable") == 0) {
3502 hotkey_enabledisable_warn(0);
3503 res = -EPERM;
3504 } else if (strlencmp(cmd, "reset") == 0) {
3505 mask = (hotkey_all_mask | hotkey_source_mask)
3506 & ~hotkey_reserved_mask;
3507 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3508 /* mask set */
3509 } else if (sscanf(cmd, "%x", &mask) == 1) {
3510 /* mask set */
3511 } else {
3512 res = -EINVAL;
3513 goto errexit;
3517 if (!res) {
3518 tpacpi_disclose_usertask("procfs hotkey",
3519 "set mask to 0x%08x\n", mask);
3520 res = hotkey_user_mask_set(mask);
3523 errexit:
3524 mutex_unlock(&hotkey_mutex);
3525 return res;
3528 static const struct acpi_device_id ibm_htk_device_ids[] = {
3529 {TPACPI_ACPI_HKEY_HID, 0},
3530 {"", 0},
3533 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3534 .hid = ibm_htk_device_ids,
3535 .notify = hotkey_notify,
3536 .handle = &hkey_handle,
3537 .type = ACPI_DEVICE_NOTIFY,
3540 static struct ibm_struct hotkey_driver_data = {
3541 .name = "hotkey",
3542 .read = hotkey_read,
3543 .write = hotkey_write,
3544 .exit = hotkey_exit,
3545 .resume = hotkey_resume,
3546 .suspend = hotkey_suspend,
3547 .acpi = &ibm_hotkey_acpidriver,
3550 /*************************************************************************
3551 * Bluetooth subdriver
3554 enum {
3555 /* ACPI GBDC/SBDC bits */
3556 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3557 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3558 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3559 off / last state */
3562 enum {
3563 /* ACPI \BLTH commands */
3564 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3565 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3566 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3567 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3568 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3571 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3573 static struct rfkill *tpacpi_bluetooth_rfkill;
3575 static void bluetooth_suspend(pm_message_t state)
3577 /* Try to make sure radio will resume powered off */
3578 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3579 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3580 vdbg_printk(TPACPI_DBG_RFKILL,
3581 "bluetooth power down on resume request failed\n");
3584 static int bluetooth_get_radiosw(void)
3586 int status;
3588 if (!tp_features.bluetooth)
3589 return -ENODEV;
3591 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3592 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3593 return RFKILL_STATE_HARD_BLOCKED;
3595 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3596 if (dbg_bluetoothemul)
3597 return (tpacpi_bluetooth_emulstate) ?
3598 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3599 #endif
3601 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3602 return -EIO;
3604 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3605 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3608 static void bluetooth_update_rfk(void)
3610 int status;
3612 if (!tpacpi_bluetooth_rfkill)
3613 return;
3615 status = bluetooth_get_radiosw();
3616 if (status < 0)
3617 return;
3618 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3620 vdbg_printk(TPACPI_DBG_RFKILL,
3621 "forced rfkill state to %d\n",
3622 status);
3625 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3627 int status;
3629 if (!tp_features.bluetooth)
3630 return -ENODEV;
3632 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3633 * reason to risk weird behaviour. */
3634 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3635 && radio_on)
3636 return -EPERM;
3638 vdbg_printk(TPACPI_DBG_RFKILL,
3639 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3641 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3642 if (dbg_bluetoothemul) {
3643 tpacpi_bluetooth_emulstate = !!radio_on;
3644 if (update_rfk)
3645 bluetooth_update_rfk();
3646 return 0;
3648 #endif
3650 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3651 if (radio_on)
3652 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3653 else
3654 status = 0;
3655 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3656 return -EIO;
3658 if (update_rfk)
3659 bluetooth_update_rfk();
3661 return 0;
3664 /* sysfs bluetooth enable ---------------------------------------------- */
3665 static ssize_t bluetooth_enable_show(struct device *dev,
3666 struct device_attribute *attr,
3667 char *buf)
3669 int status;
3671 printk_deprecated_rfkill_attribute("bluetooth_enable");
3673 status = bluetooth_get_radiosw();
3674 if (status < 0)
3675 return status;
3677 return snprintf(buf, PAGE_SIZE, "%d\n",
3678 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3681 static ssize_t bluetooth_enable_store(struct device *dev,
3682 struct device_attribute *attr,
3683 const char *buf, size_t count)
3685 unsigned long t;
3686 int res;
3688 printk_deprecated_rfkill_attribute("bluetooth_enable");
3690 if (parse_strtoul(buf, 1, &t))
3691 return -EINVAL;
3693 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3695 res = bluetooth_set_radiosw(t, 1);
3697 return (res) ? res : count;
3700 static struct device_attribute dev_attr_bluetooth_enable =
3701 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3702 bluetooth_enable_show, bluetooth_enable_store);
3704 /* --------------------------------------------------------------------- */
3706 static struct attribute *bluetooth_attributes[] = {
3707 &dev_attr_bluetooth_enable.attr,
3708 NULL
3711 static const struct attribute_group bluetooth_attr_group = {
3712 .attrs = bluetooth_attributes,
3715 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3717 int bts = bluetooth_get_radiosw();
3719 if (bts < 0)
3720 return bts;
3722 *state = bts;
3723 return 0;
3726 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3728 dbg_printk(TPACPI_DBG_RFKILL,
3729 "request to change radio state to %d\n", state);
3730 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3733 static void bluetooth_shutdown(void)
3735 /* Order firmware to save current state to NVRAM */
3736 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3737 TP_ACPI_BLTH_SAVE_STATE))
3738 printk(TPACPI_NOTICE
3739 "failed to save bluetooth state to NVRAM\n");
3740 else
3741 vdbg_printk(TPACPI_DBG_RFKILL,
3742 "bluestooth state saved to NVRAM\n");
3745 static void bluetooth_exit(void)
3747 bluetooth_shutdown();
3749 if (tpacpi_bluetooth_rfkill)
3750 rfkill_unregister(tpacpi_bluetooth_rfkill);
3752 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3753 &bluetooth_attr_group);
3756 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3758 int res;
3759 int status = 0;
3761 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3762 "initializing bluetooth subdriver\n");
3764 TPACPI_ACPIHANDLE_INIT(hkey);
3766 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3767 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3768 tp_features.bluetooth = hkey_handle &&
3769 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3771 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3772 "bluetooth is %s, status 0x%02x\n",
3773 str_supported(tp_features.bluetooth),
3774 status);
3776 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3777 if (dbg_bluetoothemul) {
3778 tp_features.bluetooth = 1;
3779 printk(TPACPI_INFO
3780 "bluetooth switch emulation enabled\n");
3781 } else
3782 #endif
3783 if (tp_features.bluetooth &&
3784 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3785 /* no bluetooth hardware present in system */
3786 tp_features.bluetooth = 0;
3787 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3788 "bluetooth hardware not installed\n");
3791 if (!tp_features.bluetooth)
3792 return 1;
3794 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3795 &bluetooth_attr_group);
3796 if (res)
3797 return res;
3799 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3800 &tpacpi_bluetooth_rfkill,
3801 RFKILL_TYPE_BLUETOOTH,
3802 TPACPI_RFK_BLUETOOTH_SW_NAME,
3803 true,
3804 tpacpi_bluetooth_rfk_set,
3805 tpacpi_bluetooth_rfk_get);
3806 if (res) {
3807 bluetooth_exit();
3808 return res;
3811 return 0;
3814 /* procfs -------------------------------------------------------------- */
3815 static int bluetooth_read(char *p)
3817 int len = 0;
3818 int status = bluetooth_get_radiosw();
3820 if (!tp_features.bluetooth)
3821 len += sprintf(p + len, "status:\t\tnot supported\n");
3822 else {
3823 len += sprintf(p + len, "status:\t\t%s\n",
3824 (status == RFKILL_STATE_UNBLOCKED) ?
3825 "enabled" : "disabled");
3826 len += sprintf(p + len, "commands:\tenable, disable\n");
3829 return len;
3832 static int bluetooth_write(char *buf)
3834 char *cmd;
3835 int state = -1;
3837 if (!tp_features.bluetooth)
3838 return -ENODEV;
3840 while ((cmd = next_cmd(&buf))) {
3841 if (strlencmp(cmd, "enable") == 0) {
3842 state = 1;
3843 } else if (strlencmp(cmd, "disable") == 0) {
3844 state = 0;
3845 } else
3846 return -EINVAL;
3849 if (state != -1) {
3850 tpacpi_disclose_usertask("procfs bluetooth",
3851 "attempt to %s\n",
3852 state ? "enable" : "disable");
3853 bluetooth_set_radiosw(state, 1);
3856 return 0;
3859 static struct ibm_struct bluetooth_driver_data = {
3860 .name = "bluetooth",
3861 .read = bluetooth_read,
3862 .write = bluetooth_write,
3863 .exit = bluetooth_exit,
3864 .suspend = bluetooth_suspend,
3865 .shutdown = bluetooth_shutdown,
3868 /*************************************************************************
3869 * Wan subdriver
3872 enum {
3873 /* ACPI GWAN/SWAN bits */
3874 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3875 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3876 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3877 off / last state */
3880 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3882 static struct rfkill *tpacpi_wan_rfkill;
3884 static void wan_suspend(pm_message_t state)
3886 /* Try to make sure radio will resume powered off */
3887 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3888 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3889 vdbg_printk(TPACPI_DBG_RFKILL,
3890 "WWAN power down on resume request failed\n");
3893 static int wan_get_radiosw(void)
3895 int status;
3897 if (!tp_features.wan)
3898 return -ENODEV;
3900 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3901 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3902 return RFKILL_STATE_HARD_BLOCKED;
3904 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3905 if (dbg_wwanemul)
3906 return (tpacpi_wwan_emulstate) ?
3907 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3908 #endif
3910 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3911 return -EIO;
3913 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3914 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3917 static void wan_update_rfk(void)
3919 int status;
3921 if (!tpacpi_wan_rfkill)
3922 return;
3924 status = wan_get_radiosw();
3925 if (status < 0)
3926 return;
3927 rfkill_force_state(tpacpi_wan_rfkill, status);
3929 vdbg_printk(TPACPI_DBG_RFKILL,
3930 "forced rfkill state to %d\n",
3931 status);
3934 static int wan_set_radiosw(int radio_on, int update_rfk)
3936 int status;
3938 if (!tp_features.wan)
3939 return -ENODEV;
3941 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3942 * reason to risk weird behaviour. */
3943 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3944 && radio_on)
3945 return -EPERM;
3947 vdbg_printk(TPACPI_DBG_RFKILL,
3948 "will %s WWAN\n", radio_on ? "enable" : "disable");
3950 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3951 if (dbg_wwanemul) {
3952 tpacpi_wwan_emulstate = !!radio_on;
3953 if (update_rfk)
3954 wan_update_rfk();
3955 return 0;
3957 #endif
3959 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3960 if (radio_on)
3961 status = TP_ACPI_WANCARD_RADIOSSW;
3962 else
3963 status = 0;
3964 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3965 return -EIO;
3967 if (update_rfk)
3968 wan_update_rfk();
3970 return 0;
3973 /* sysfs wan enable ---------------------------------------------------- */
3974 static ssize_t wan_enable_show(struct device *dev,
3975 struct device_attribute *attr,
3976 char *buf)
3978 int status;
3980 printk_deprecated_rfkill_attribute("wwan_enable");
3982 status = wan_get_radiosw();
3983 if (status < 0)
3984 return status;
3986 return snprintf(buf, PAGE_SIZE, "%d\n",
3987 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3990 static ssize_t wan_enable_store(struct device *dev,
3991 struct device_attribute *attr,
3992 const char *buf, size_t count)
3994 unsigned long t;
3995 int res;
3997 printk_deprecated_rfkill_attribute("wwan_enable");
3999 if (parse_strtoul(buf, 1, &t))
4000 return -EINVAL;
4002 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4004 res = wan_set_radiosw(t, 1);
4006 return (res) ? res : count;
4009 static struct device_attribute dev_attr_wan_enable =
4010 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4011 wan_enable_show, wan_enable_store);
4013 /* --------------------------------------------------------------------- */
4015 static struct attribute *wan_attributes[] = {
4016 &dev_attr_wan_enable.attr,
4017 NULL
4020 static const struct attribute_group wan_attr_group = {
4021 .attrs = wan_attributes,
4024 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4026 int wans = wan_get_radiosw();
4028 if (wans < 0)
4029 return wans;
4031 *state = wans;
4032 return 0;
4035 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4037 dbg_printk(TPACPI_DBG_RFKILL,
4038 "request to change radio state to %d\n", state);
4039 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4042 static void wan_shutdown(void)
4044 /* Order firmware to save current state to NVRAM */
4045 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4046 TP_ACPI_WGSV_SAVE_STATE))
4047 printk(TPACPI_NOTICE
4048 "failed to save WWAN state to NVRAM\n");
4049 else
4050 vdbg_printk(TPACPI_DBG_RFKILL,
4051 "WWAN state saved to NVRAM\n");
4054 static void wan_exit(void)
4056 wan_shutdown();
4058 if (tpacpi_wan_rfkill)
4059 rfkill_unregister(tpacpi_wan_rfkill);
4061 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4062 &wan_attr_group);
4065 static int __init wan_init(struct ibm_init_struct *iibm)
4067 int res;
4068 int status = 0;
4070 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4071 "initializing wan subdriver\n");
4073 TPACPI_ACPIHANDLE_INIT(hkey);
4075 tp_features.wan = hkey_handle &&
4076 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4078 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4079 "wan is %s, status 0x%02x\n",
4080 str_supported(tp_features.wan),
4081 status);
4083 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4084 if (dbg_wwanemul) {
4085 tp_features.wan = 1;
4086 printk(TPACPI_INFO
4087 "wwan switch emulation enabled\n");
4088 } else
4089 #endif
4090 if (tp_features.wan &&
4091 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4092 /* no wan hardware present in system */
4093 tp_features.wan = 0;
4094 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4095 "wan hardware not installed\n");
4098 if (!tp_features.wan)
4099 return 1;
4101 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4102 &wan_attr_group);
4103 if (res)
4104 return res;
4106 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4107 &tpacpi_wan_rfkill,
4108 RFKILL_TYPE_WWAN,
4109 TPACPI_RFK_WWAN_SW_NAME,
4110 true,
4111 tpacpi_wan_rfk_set,
4112 tpacpi_wan_rfk_get);
4113 if (res) {
4114 wan_exit();
4115 return res;
4118 return 0;
4121 /* procfs -------------------------------------------------------------- */
4122 static int wan_read(char *p)
4124 int len = 0;
4125 int status = wan_get_radiosw();
4127 tpacpi_disclose_usertask("procfs wan", "read");
4129 if (!tp_features.wan)
4130 len += sprintf(p + len, "status:\t\tnot supported\n");
4131 else {
4132 len += sprintf(p + len, "status:\t\t%s\n",
4133 (status == RFKILL_STATE_UNBLOCKED) ?
4134 "enabled" : "disabled");
4135 len += sprintf(p + len, "commands:\tenable, disable\n");
4138 return len;
4141 static int wan_write(char *buf)
4143 char *cmd;
4144 int state = -1;
4146 if (!tp_features.wan)
4147 return -ENODEV;
4149 while ((cmd = next_cmd(&buf))) {
4150 if (strlencmp(cmd, "enable") == 0) {
4151 state = 1;
4152 } else if (strlencmp(cmd, "disable") == 0) {
4153 state = 0;
4154 } else
4155 return -EINVAL;
4158 if (state != -1) {
4159 tpacpi_disclose_usertask("procfs wan",
4160 "attempt to %s\n",
4161 state ? "enable" : "disable");
4162 wan_set_radiosw(state, 1);
4165 return 0;
4168 static struct ibm_struct wan_driver_data = {
4169 .name = "wan",
4170 .read = wan_read,
4171 .write = wan_write,
4172 .exit = wan_exit,
4173 .suspend = wan_suspend,
4174 .shutdown = wan_shutdown,
4177 /*************************************************************************
4178 * UWB subdriver
4181 enum {
4182 /* ACPI GUWB/SUWB bits */
4183 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4184 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4187 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4189 static struct rfkill *tpacpi_uwb_rfkill;
4191 static int uwb_get_radiosw(void)
4193 int status;
4195 if (!tp_features.uwb)
4196 return -ENODEV;
4198 /* WLSW overrides UWB in firmware/hardware, reflect that */
4199 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4200 return RFKILL_STATE_HARD_BLOCKED;
4202 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4203 if (dbg_uwbemul)
4204 return (tpacpi_uwb_emulstate) ?
4205 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4206 #endif
4208 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4209 return -EIO;
4211 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4212 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4215 static void uwb_update_rfk(void)
4217 int status;
4219 if (!tpacpi_uwb_rfkill)
4220 return;
4222 status = uwb_get_radiosw();
4223 if (status < 0)
4224 return;
4225 rfkill_force_state(tpacpi_uwb_rfkill, status);
4227 vdbg_printk(TPACPI_DBG_RFKILL,
4228 "forced rfkill state to %d\n",
4229 status);
4232 static int uwb_set_radiosw(int radio_on, int update_rfk)
4234 int status;
4236 if (!tp_features.uwb)
4237 return -ENODEV;
4239 /* WLSW overrides UWB in firmware/hardware, but there is no
4240 * reason to risk weird behaviour. */
4241 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4242 && radio_on)
4243 return -EPERM;
4245 vdbg_printk(TPACPI_DBG_RFKILL,
4246 "will %s UWB\n", radio_on ? "enable" : "disable");
4248 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4249 if (dbg_uwbemul) {
4250 tpacpi_uwb_emulstate = !!radio_on;
4251 if (update_rfk)
4252 uwb_update_rfk();
4253 return 0;
4255 #endif
4257 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4258 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4259 return -EIO;
4261 if (update_rfk)
4262 uwb_update_rfk();
4264 return 0;
4267 /* --------------------------------------------------------------------- */
4269 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4271 int uwbs = uwb_get_radiosw();
4273 if (uwbs < 0)
4274 return uwbs;
4276 *state = uwbs;
4277 return 0;
4280 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4282 dbg_printk(TPACPI_DBG_RFKILL,
4283 "request to change radio state to %d\n", state);
4284 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4287 static void uwb_exit(void)
4289 if (tpacpi_uwb_rfkill)
4290 rfkill_unregister(tpacpi_uwb_rfkill);
4293 static int __init uwb_init(struct ibm_init_struct *iibm)
4295 int res;
4296 int status = 0;
4298 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4299 "initializing uwb subdriver\n");
4301 TPACPI_ACPIHANDLE_INIT(hkey);
4303 tp_features.uwb = hkey_handle &&
4304 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4306 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4307 "uwb is %s, status 0x%02x\n",
4308 str_supported(tp_features.uwb),
4309 status);
4311 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4312 if (dbg_uwbemul) {
4313 tp_features.uwb = 1;
4314 printk(TPACPI_INFO
4315 "uwb switch emulation enabled\n");
4316 } else
4317 #endif
4318 if (tp_features.uwb &&
4319 !(status & TP_ACPI_UWB_HWPRESENT)) {
4320 /* no uwb hardware present in system */
4321 tp_features.uwb = 0;
4322 dbg_printk(TPACPI_DBG_INIT,
4323 "uwb hardware not installed\n");
4326 if (!tp_features.uwb)
4327 return 1;
4329 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4330 &tpacpi_uwb_rfkill,
4331 RFKILL_TYPE_UWB,
4332 TPACPI_RFK_UWB_SW_NAME,
4333 false,
4334 tpacpi_uwb_rfk_set,
4335 tpacpi_uwb_rfk_get);
4337 return res;
4340 static struct ibm_struct uwb_driver_data = {
4341 .name = "uwb",
4342 .exit = uwb_exit,
4343 .flags.experimental = 1,
4346 /*************************************************************************
4347 * Video subdriver
4350 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4352 enum video_access_mode {
4353 TPACPI_VIDEO_NONE = 0,
4354 TPACPI_VIDEO_570, /* 570 */
4355 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4356 TPACPI_VIDEO_NEW, /* all others */
4359 enum { /* video status flags, based on VIDEO_570 */
4360 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4361 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4362 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4365 enum { /* TPACPI_VIDEO_570 constants */
4366 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4367 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4368 * video_status_flags */
4369 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4370 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4373 static enum video_access_mode video_supported;
4374 static int video_orig_autosw;
4376 static int video_autosw_get(void);
4377 static int video_autosw_set(int enable);
4379 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4381 static int __init video_init(struct ibm_init_struct *iibm)
4383 int ivga;
4385 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4387 TPACPI_ACPIHANDLE_INIT(vid);
4388 TPACPI_ACPIHANDLE_INIT(vid2);
4390 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4391 /* G41, assume IVGA doesn't change */
4392 vid_handle = vid2_handle;
4394 if (!vid_handle)
4395 /* video switching not supported on R30, R31 */
4396 video_supported = TPACPI_VIDEO_NONE;
4397 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4398 /* 570 */
4399 video_supported = TPACPI_VIDEO_570;
4400 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4401 /* 600e/x, 770e, 770x */
4402 video_supported = TPACPI_VIDEO_770;
4403 else
4404 /* all others */
4405 video_supported = TPACPI_VIDEO_NEW;
4407 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4408 str_supported(video_supported != TPACPI_VIDEO_NONE),
4409 video_supported);
4411 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4414 static void video_exit(void)
4416 dbg_printk(TPACPI_DBG_EXIT,
4417 "restoring original video autoswitch mode\n");
4418 if (video_autosw_set(video_orig_autosw))
4419 printk(TPACPI_ERR "error while trying to restore original "
4420 "video autoswitch mode\n");
4423 static int video_outputsw_get(void)
4425 int status = 0;
4426 int i;
4428 switch (video_supported) {
4429 case TPACPI_VIDEO_570:
4430 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4431 TP_ACPI_VIDEO_570_PHSCMD))
4432 return -EIO;
4433 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4434 break;
4435 case TPACPI_VIDEO_770:
4436 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4437 return -EIO;
4438 if (i)
4439 status |= TP_ACPI_VIDEO_S_LCD;
4440 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4441 return -EIO;
4442 if (i)
4443 status |= TP_ACPI_VIDEO_S_CRT;
4444 break;
4445 case TPACPI_VIDEO_NEW:
4446 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4447 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4448 return -EIO;
4449 if (i)
4450 status |= TP_ACPI_VIDEO_S_CRT;
4452 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4453 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4454 return -EIO;
4455 if (i)
4456 status |= TP_ACPI_VIDEO_S_LCD;
4457 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4458 return -EIO;
4459 if (i)
4460 status |= TP_ACPI_VIDEO_S_DVI;
4461 break;
4462 default:
4463 return -ENOSYS;
4466 return status;
4469 static int video_outputsw_set(int status)
4471 int autosw;
4472 int res = 0;
4474 switch (video_supported) {
4475 case TPACPI_VIDEO_570:
4476 res = acpi_evalf(NULL, NULL,
4477 "\\_SB.PHS2", "vdd",
4478 TP_ACPI_VIDEO_570_PHS2CMD,
4479 status | TP_ACPI_VIDEO_570_PHS2SET);
4480 break;
4481 case TPACPI_VIDEO_770:
4482 autosw = video_autosw_get();
4483 if (autosw < 0)
4484 return autosw;
4486 res = video_autosw_set(1);
4487 if (res)
4488 return res;
4489 res = acpi_evalf(vid_handle, NULL,
4490 "ASWT", "vdd", status * 0x100, 0);
4491 if (!autosw && video_autosw_set(autosw)) {
4492 printk(TPACPI_ERR
4493 "video auto-switch left enabled due to error\n");
4494 return -EIO;
4496 break;
4497 case TPACPI_VIDEO_NEW:
4498 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4499 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4500 break;
4501 default:
4502 return -ENOSYS;
4505 return (res)? 0 : -EIO;
4508 static int video_autosw_get(void)
4510 int autosw = 0;
4512 switch (video_supported) {
4513 case TPACPI_VIDEO_570:
4514 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4515 return -EIO;
4516 break;
4517 case TPACPI_VIDEO_770:
4518 case TPACPI_VIDEO_NEW:
4519 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4520 return -EIO;
4521 break;
4522 default:
4523 return -ENOSYS;
4526 return autosw & 1;
4529 static int video_autosw_set(int enable)
4531 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4532 return -EIO;
4533 return 0;
4536 static int video_outputsw_cycle(void)
4538 int autosw = video_autosw_get();
4539 int res;
4541 if (autosw < 0)
4542 return autosw;
4544 switch (video_supported) {
4545 case TPACPI_VIDEO_570:
4546 res = video_autosw_set(1);
4547 if (res)
4548 return res;
4549 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4550 break;
4551 case TPACPI_VIDEO_770:
4552 case TPACPI_VIDEO_NEW:
4553 res = video_autosw_set(1);
4554 if (res)
4555 return res;
4556 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4557 break;
4558 default:
4559 return -ENOSYS;
4561 if (!autosw && video_autosw_set(autosw)) {
4562 printk(TPACPI_ERR
4563 "video auto-switch left enabled due to error\n");
4564 return -EIO;
4567 return (res)? 0 : -EIO;
4570 static int video_expand_toggle(void)
4572 switch (video_supported) {
4573 case TPACPI_VIDEO_570:
4574 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4575 0 : -EIO;
4576 case TPACPI_VIDEO_770:
4577 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4578 0 : -EIO;
4579 case TPACPI_VIDEO_NEW:
4580 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4581 0 : -EIO;
4582 default:
4583 return -ENOSYS;
4585 /* not reached */
4588 static int video_read(char *p)
4590 int status, autosw;
4591 int len = 0;
4593 if (video_supported == TPACPI_VIDEO_NONE) {
4594 len += sprintf(p + len, "status:\t\tnot supported\n");
4595 return len;
4598 status = video_outputsw_get();
4599 if (status < 0)
4600 return status;
4602 autosw = video_autosw_get();
4603 if (autosw < 0)
4604 return autosw;
4606 len += sprintf(p + len, "status:\t\tsupported\n");
4607 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4608 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4609 if (video_supported == TPACPI_VIDEO_NEW)
4610 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4611 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4612 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4613 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4614 if (video_supported == TPACPI_VIDEO_NEW)
4615 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4616 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4617 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4619 return len;
4622 static int video_write(char *buf)
4624 char *cmd;
4625 int enable, disable, status;
4626 int res;
4628 if (video_supported == TPACPI_VIDEO_NONE)
4629 return -ENODEV;
4631 enable = 0;
4632 disable = 0;
4634 while ((cmd = next_cmd(&buf))) {
4635 if (strlencmp(cmd, "lcd_enable") == 0) {
4636 enable |= TP_ACPI_VIDEO_S_LCD;
4637 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4638 disable |= TP_ACPI_VIDEO_S_LCD;
4639 } else if (strlencmp(cmd, "crt_enable") == 0) {
4640 enable |= TP_ACPI_VIDEO_S_CRT;
4641 } else if (strlencmp(cmd, "crt_disable") == 0) {
4642 disable |= TP_ACPI_VIDEO_S_CRT;
4643 } else if (video_supported == TPACPI_VIDEO_NEW &&
4644 strlencmp(cmd, "dvi_enable") == 0) {
4645 enable |= TP_ACPI_VIDEO_S_DVI;
4646 } else if (video_supported == TPACPI_VIDEO_NEW &&
4647 strlencmp(cmd, "dvi_disable") == 0) {
4648 disable |= TP_ACPI_VIDEO_S_DVI;
4649 } else if (strlencmp(cmd, "auto_enable") == 0) {
4650 res = video_autosw_set(1);
4651 if (res)
4652 return res;
4653 } else if (strlencmp(cmd, "auto_disable") == 0) {
4654 res = video_autosw_set(0);
4655 if (res)
4656 return res;
4657 } else if (strlencmp(cmd, "video_switch") == 0) {
4658 res = video_outputsw_cycle();
4659 if (res)
4660 return res;
4661 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4662 res = video_expand_toggle();
4663 if (res)
4664 return res;
4665 } else
4666 return -EINVAL;
4669 if (enable || disable) {
4670 status = video_outputsw_get();
4671 if (status < 0)
4672 return status;
4673 res = video_outputsw_set((status & ~disable) | enable);
4674 if (res)
4675 return res;
4678 return 0;
4681 static struct ibm_struct video_driver_data = {
4682 .name = "video",
4683 .read = video_read,
4684 .write = video_write,
4685 .exit = video_exit,
4688 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4690 /*************************************************************************
4691 * Light (thinklight) subdriver
4694 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4695 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4697 static int light_get_status(void)
4699 int status = 0;
4701 if (tp_features.light_status) {
4702 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4703 return -EIO;
4704 return (!!status);
4707 return -ENXIO;
4710 static int light_set_status(int status)
4712 int rc;
4714 if (tp_features.light) {
4715 if (cmos_handle) {
4716 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4717 (status)?
4718 TP_CMOS_THINKLIGHT_ON :
4719 TP_CMOS_THINKLIGHT_OFF);
4720 } else {
4721 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4722 (status)? 1 : 0);
4724 return (rc)? 0 : -EIO;
4727 return -ENXIO;
4730 static void light_set_status_worker(struct work_struct *work)
4732 struct tpacpi_led_classdev *data =
4733 container_of(work, struct tpacpi_led_classdev, work);
4735 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4736 light_set_status((data->new_state != TPACPI_LED_OFF));
4739 static void light_sysfs_set(struct led_classdev *led_cdev,
4740 enum led_brightness brightness)
4742 struct tpacpi_led_classdev *data =
4743 container_of(led_cdev,
4744 struct tpacpi_led_classdev,
4745 led_classdev);
4746 data->new_state = (brightness != LED_OFF) ?
4747 TPACPI_LED_ON : TPACPI_LED_OFF;
4748 queue_work(tpacpi_wq, &data->work);
4751 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4753 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4756 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4757 .led_classdev = {
4758 .name = "tpacpi::thinklight",
4759 .brightness_set = &light_sysfs_set,
4760 .brightness_get = &light_sysfs_get,
4764 static int __init light_init(struct ibm_init_struct *iibm)
4766 int rc;
4768 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4770 TPACPI_ACPIHANDLE_INIT(ledb);
4771 TPACPI_ACPIHANDLE_INIT(lght);
4772 TPACPI_ACPIHANDLE_INIT(cmos);
4773 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4775 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4776 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4778 if (tp_features.light)
4779 /* light status not supported on
4780 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4781 tp_features.light_status =
4782 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4784 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4785 str_supported(tp_features.light),
4786 str_supported(tp_features.light_status));
4788 if (!tp_features.light)
4789 return 1;
4791 rc = led_classdev_register(&tpacpi_pdev->dev,
4792 &tpacpi_led_thinklight.led_classdev);
4794 if (rc < 0) {
4795 tp_features.light = 0;
4796 tp_features.light_status = 0;
4797 } else {
4798 rc = 0;
4801 return rc;
4804 static void light_exit(void)
4806 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4807 if (work_pending(&tpacpi_led_thinklight.work))
4808 flush_workqueue(tpacpi_wq);
4811 static int light_read(char *p)
4813 int len = 0;
4814 int status;
4816 if (!tp_features.light) {
4817 len += sprintf(p + len, "status:\t\tnot supported\n");
4818 } else if (!tp_features.light_status) {
4819 len += sprintf(p + len, "status:\t\tunknown\n");
4820 len += sprintf(p + len, "commands:\ton, off\n");
4821 } else {
4822 status = light_get_status();
4823 if (status < 0)
4824 return status;
4825 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4826 len += sprintf(p + len, "commands:\ton, off\n");
4829 return len;
4832 static int light_write(char *buf)
4834 char *cmd;
4835 int newstatus = 0;
4837 if (!tp_features.light)
4838 return -ENODEV;
4840 while ((cmd = next_cmd(&buf))) {
4841 if (strlencmp(cmd, "on") == 0) {
4842 newstatus = 1;
4843 } else if (strlencmp(cmd, "off") == 0) {
4844 newstatus = 0;
4845 } else
4846 return -EINVAL;
4849 return light_set_status(newstatus);
4852 static struct ibm_struct light_driver_data = {
4853 .name = "light",
4854 .read = light_read,
4855 .write = light_write,
4856 .exit = light_exit,
4859 /*************************************************************************
4860 * CMOS subdriver
4863 /* sysfs cmos_command -------------------------------------------------- */
4864 static ssize_t cmos_command_store(struct device *dev,
4865 struct device_attribute *attr,
4866 const char *buf, size_t count)
4868 unsigned long cmos_cmd;
4869 int res;
4871 if (parse_strtoul(buf, 21, &cmos_cmd))
4872 return -EINVAL;
4874 res = issue_thinkpad_cmos_command(cmos_cmd);
4875 return (res)? res : count;
4878 static struct device_attribute dev_attr_cmos_command =
4879 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4881 /* --------------------------------------------------------------------- */
4883 static int __init cmos_init(struct ibm_init_struct *iibm)
4885 int res;
4887 vdbg_printk(TPACPI_DBG_INIT,
4888 "initializing cmos commands subdriver\n");
4890 TPACPI_ACPIHANDLE_INIT(cmos);
4892 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4893 str_supported(cmos_handle != NULL));
4895 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4896 if (res)
4897 return res;
4899 return (cmos_handle)? 0 : 1;
4902 static void cmos_exit(void)
4904 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4907 static int cmos_read(char *p)
4909 int len = 0;
4911 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4912 R30, R31, T20-22, X20-21 */
4913 if (!cmos_handle)
4914 len += sprintf(p + len, "status:\t\tnot supported\n");
4915 else {
4916 len += sprintf(p + len, "status:\t\tsupported\n");
4917 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4920 return len;
4923 static int cmos_write(char *buf)
4925 char *cmd;
4926 int cmos_cmd, res;
4928 while ((cmd = next_cmd(&buf))) {
4929 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4930 cmos_cmd >= 0 && cmos_cmd <= 21) {
4931 /* cmos_cmd set */
4932 } else
4933 return -EINVAL;
4935 res = issue_thinkpad_cmos_command(cmos_cmd);
4936 if (res)
4937 return res;
4940 return 0;
4943 static struct ibm_struct cmos_driver_data = {
4944 .name = "cmos",
4945 .read = cmos_read,
4946 .write = cmos_write,
4947 .exit = cmos_exit,
4950 /*************************************************************************
4951 * LED subdriver
4954 enum led_access_mode {
4955 TPACPI_LED_NONE = 0,
4956 TPACPI_LED_570, /* 570 */
4957 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4958 TPACPI_LED_NEW, /* all others */
4961 enum { /* For TPACPI_LED_OLD */
4962 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4963 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4964 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4967 static enum led_access_mode led_supported;
4969 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4970 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4971 /* T20-22, X20-21 */
4972 "LED", /* all others */
4973 ); /* R30, R31 */
4975 #define TPACPI_LED_NUMLEDS 16
4976 static struct tpacpi_led_classdev *tpacpi_leds;
4977 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4978 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4979 /* there's a limit of 19 chars + NULL before 2.6.26 */
4980 "tpacpi::power",
4981 "tpacpi:orange:batt",
4982 "tpacpi:green:batt",
4983 "tpacpi::dock_active",
4984 "tpacpi::bay_active",
4985 "tpacpi::dock_batt",
4986 "tpacpi::unknown_led",
4987 "tpacpi::standby",
4988 "tpacpi::dock_status1",
4989 "tpacpi::dock_status2",
4990 "tpacpi::unknown_led2",
4991 "tpacpi::unknown_led3",
4992 "tpacpi::thinkvantage",
4994 #define TPACPI_SAFE_LEDS 0x1081U
4996 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4998 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4999 return false;
5000 #else
5001 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5002 #endif
5005 static int led_get_status(const unsigned int led)
5007 int status;
5008 enum led_status_t led_s;
5010 switch (led_supported) {
5011 case TPACPI_LED_570:
5012 if (!acpi_evalf(ec_handle,
5013 &status, "GLED", "dd", 1 << led))
5014 return -EIO;
5015 led_s = (status == 0)?
5016 TPACPI_LED_OFF :
5017 ((status == 1)?
5018 TPACPI_LED_ON :
5019 TPACPI_LED_BLINK);
5020 tpacpi_led_state_cache[led] = led_s;
5021 return led_s;
5022 default:
5023 return -ENXIO;
5026 /* not reached */
5029 static int led_set_status(const unsigned int led,
5030 const enum led_status_t ledstatus)
5032 /* off, on, blink. Index is led_status_t */
5033 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5034 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5036 int rc = 0;
5038 switch (led_supported) {
5039 case TPACPI_LED_570:
5040 /* 570 */
5041 if (unlikely(led > 7))
5042 return -EINVAL;
5043 if (unlikely(tpacpi_is_led_restricted(led)))
5044 return -EPERM;
5045 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5046 (1 << led), led_sled_arg1[ledstatus]))
5047 rc = -EIO;
5048 break;
5049 case TPACPI_LED_OLD:
5050 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5051 if (unlikely(led > 7))
5052 return -EINVAL;
5053 if (unlikely(tpacpi_is_led_restricted(led)))
5054 return -EPERM;
5055 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5056 if (rc >= 0)
5057 rc = ec_write(TPACPI_LED_EC_HLBL,
5058 (ledstatus == TPACPI_LED_BLINK) << led);
5059 if (rc >= 0)
5060 rc = ec_write(TPACPI_LED_EC_HLCL,
5061 (ledstatus != TPACPI_LED_OFF) << led);
5062 break;
5063 case TPACPI_LED_NEW:
5064 /* all others */
5065 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5066 return -EINVAL;
5067 if (unlikely(tpacpi_is_led_restricted(led)))
5068 return -EPERM;
5069 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5070 led, led_led_arg1[ledstatus]))
5071 rc = -EIO;
5072 break;
5073 default:
5074 rc = -ENXIO;
5077 if (!rc)
5078 tpacpi_led_state_cache[led] = ledstatus;
5080 return rc;
5083 static void led_set_status_worker(struct work_struct *work)
5085 struct tpacpi_led_classdev *data =
5086 container_of(work, struct tpacpi_led_classdev, work);
5088 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5089 led_set_status(data->led, data->new_state);
5092 static void led_sysfs_set(struct led_classdev *led_cdev,
5093 enum led_brightness brightness)
5095 struct tpacpi_led_classdev *data = container_of(led_cdev,
5096 struct tpacpi_led_classdev, led_classdev);
5098 if (brightness == LED_OFF)
5099 data->new_state = TPACPI_LED_OFF;
5100 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5101 data->new_state = TPACPI_LED_ON;
5102 else
5103 data->new_state = TPACPI_LED_BLINK;
5105 queue_work(tpacpi_wq, &data->work);
5108 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5109 unsigned long *delay_on, unsigned long *delay_off)
5111 struct tpacpi_led_classdev *data = container_of(led_cdev,
5112 struct tpacpi_led_classdev, led_classdev);
5114 /* Can we choose the flash rate? */
5115 if (*delay_on == 0 && *delay_off == 0) {
5116 /* yes. set them to the hardware blink rate (1 Hz) */
5117 *delay_on = 500; /* ms */
5118 *delay_off = 500; /* ms */
5119 } else if ((*delay_on != 500) || (*delay_off != 500))
5120 return -EINVAL;
5122 data->new_state = TPACPI_LED_BLINK;
5123 queue_work(tpacpi_wq, &data->work);
5125 return 0;
5128 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5130 int rc;
5132 struct tpacpi_led_classdev *data = container_of(led_cdev,
5133 struct tpacpi_led_classdev, led_classdev);
5135 rc = led_get_status(data->led);
5137 if (rc == TPACPI_LED_OFF || rc < 0)
5138 rc = LED_OFF; /* no error handling in led class :( */
5139 else
5140 rc = LED_FULL;
5142 return rc;
5145 static void led_exit(void)
5147 unsigned int i;
5149 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5150 if (tpacpi_leds[i].led_classdev.name)
5151 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5154 kfree(tpacpi_leds);
5157 static int __init tpacpi_init_led(unsigned int led)
5159 int rc;
5161 tpacpi_leds[led].led = led;
5163 /* LEDs with no name don't get registered */
5164 if (!tpacpi_led_names[led])
5165 return 0;
5167 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5168 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5169 if (led_supported == TPACPI_LED_570)
5170 tpacpi_leds[led].led_classdev.brightness_get =
5171 &led_sysfs_get;
5173 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5175 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5177 rc = led_classdev_register(&tpacpi_pdev->dev,
5178 &tpacpi_leds[led].led_classdev);
5179 if (rc < 0)
5180 tpacpi_leds[led].led_classdev.name = NULL;
5182 return rc;
5185 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5186 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5187 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5188 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5190 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5191 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5192 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5193 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5194 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5195 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5196 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5197 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5199 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5200 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5201 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5202 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5203 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5205 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5206 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5207 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5208 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5210 /* (1) - may have excess leds enabled on MSB */
5212 /* Defaults (order matters, keep last, don't reorder!) */
5213 { /* Lenovo */
5214 .vendor = PCI_VENDOR_ID_LENOVO,
5215 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5216 .quirks = 0x1fffU,
5218 { /* IBM ThinkPads with no EC version string */
5219 .vendor = PCI_VENDOR_ID_IBM,
5220 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5221 .quirks = 0x00ffU,
5223 { /* IBM ThinkPads with EC version string */
5224 .vendor = PCI_VENDOR_ID_IBM,
5225 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5226 .quirks = 0x00bfU,
5230 #undef TPACPI_LEDQ_IBM
5231 #undef TPACPI_LEDQ_LNV
5233 static int __init led_init(struct ibm_init_struct *iibm)
5235 unsigned int i;
5236 int rc;
5237 unsigned long useful_leds;
5239 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5241 TPACPI_ACPIHANDLE_INIT(led);
5243 if (!led_handle)
5244 /* led not supported on R30, R31 */
5245 led_supported = TPACPI_LED_NONE;
5246 else if (strlencmp(led_path, "SLED") == 0)
5247 /* 570 */
5248 led_supported = TPACPI_LED_570;
5249 else if (strlencmp(led_path, "SYSL") == 0)
5250 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5251 led_supported = TPACPI_LED_OLD;
5252 else
5253 /* all others */
5254 led_supported = TPACPI_LED_NEW;
5256 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5257 str_supported(led_supported), led_supported);
5259 if (led_supported == TPACPI_LED_NONE)
5260 return 1;
5262 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5263 GFP_KERNEL);
5264 if (!tpacpi_leds) {
5265 printk(TPACPI_ERR "Out of memory for LED data\n");
5266 return -ENOMEM;
5269 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5270 ARRAY_SIZE(led_useful_qtable));
5272 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5273 if (!tpacpi_is_led_restricted(i) &&
5274 test_bit(i, &useful_leds)) {
5275 rc = tpacpi_init_led(i);
5276 if (rc < 0) {
5277 led_exit();
5278 return rc;
5283 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5284 printk(TPACPI_NOTICE
5285 "warning: userspace override of important "
5286 "firmware LEDs is enabled\n");
5287 #endif
5288 return 0;
5291 #define str_led_status(s) \
5292 ((s) == TPACPI_LED_OFF ? "off" : \
5293 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5295 static int led_read(char *p)
5297 int len = 0;
5299 if (!led_supported) {
5300 len += sprintf(p + len, "status:\t\tnot supported\n");
5301 return len;
5303 len += sprintf(p + len, "status:\t\tsupported\n");
5305 if (led_supported == TPACPI_LED_570) {
5306 /* 570 */
5307 int i, status;
5308 for (i = 0; i < 8; i++) {
5309 status = led_get_status(i);
5310 if (status < 0)
5311 return -EIO;
5312 len += sprintf(p + len, "%d:\t\t%s\n",
5313 i, str_led_status(status));
5317 len += sprintf(p + len, "commands:\t"
5318 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5320 return len;
5323 static int led_write(char *buf)
5325 char *cmd;
5326 int led, rc;
5327 enum led_status_t s;
5329 if (!led_supported)
5330 return -ENODEV;
5332 while ((cmd = next_cmd(&buf))) {
5333 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5334 return -EINVAL;
5336 if (strstr(cmd, "off")) {
5337 s = TPACPI_LED_OFF;
5338 } else if (strstr(cmd, "on")) {
5339 s = TPACPI_LED_ON;
5340 } else if (strstr(cmd, "blink")) {
5341 s = TPACPI_LED_BLINK;
5342 } else {
5343 return -EINVAL;
5346 rc = led_set_status(led, s);
5347 if (rc < 0)
5348 return rc;
5351 return 0;
5354 static struct ibm_struct led_driver_data = {
5355 .name = "led",
5356 .read = led_read,
5357 .write = led_write,
5358 .exit = led_exit,
5361 /*************************************************************************
5362 * Beep subdriver
5365 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5367 #define TPACPI_BEEP_Q1 0x0001
5369 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5370 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5371 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5374 static int __init beep_init(struct ibm_init_struct *iibm)
5376 unsigned long quirks;
5378 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5380 TPACPI_ACPIHANDLE_INIT(beep);
5382 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5383 str_supported(beep_handle != NULL));
5385 quirks = tpacpi_check_quirks(beep_quirk_table,
5386 ARRAY_SIZE(beep_quirk_table));
5388 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5390 return (beep_handle)? 0 : 1;
5393 static int beep_read(char *p)
5395 int len = 0;
5397 if (!beep_handle)
5398 len += sprintf(p + len, "status:\t\tnot supported\n");
5399 else {
5400 len += sprintf(p + len, "status:\t\tsupported\n");
5401 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5404 return len;
5407 static int beep_write(char *buf)
5409 char *cmd;
5410 int beep_cmd;
5412 if (!beep_handle)
5413 return -ENODEV;
5415 while ((cmd = next_cmd(&buf))) {
5416 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5417 beep_cmd >= 0 && beep_cmd <= 17) {
5418 /* beep_cmd set */
5419 } else
5420 return -EINVAL;
5421 if (tp_features.beep_needs_two_args) {
5422 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5423 beep_cmd, 0))
5424 return -EIO;
5425 } else {
5426 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5427 beep_cmd))
5428 return -EIO;
5432 return 0;
5435 static struct ibm_struct beep_driver_data = {
5436 .name = "beep",
5437 .read = beep_read,
5438 .write = beep_write,
5441 /*************************************************************************
5442 * Thermal subdriver
5445 enum thermal_access_mode {
5446 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5447 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5448 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5449 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5450 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5453 enum { /* TPACPI_THERMAL_TPEC_* */
5454 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5455 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5456 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5459 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5460 struct ibm_thermal_sensors_struct {
5461 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5464 static enum thermal_access_mode thermal_read_mode;
5466 /* idx is zero-based */
5467 static int thermal_get_sensor(int idx, s32 *value)
5469 int t;
5470 s8 tmp;
5471 char tmpi[5];
5473 t = TP_EC_THERMAL_TMP0;
5475 switch (thermal_read_mode) {
5476 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5477 case TPACPI_THERMAL_TPEC_16:
5478 if (idx >= 8 && idx <= 15) {
5479 t = TP_EC_THERMAL_TMP8;
5480 idx -= 8;
5482 /* fallthrough */
5483 #endif
5484 case TPACPI_THERMAL_TPEC_8:
5485 if (idx <= 7) {
5486 if (!acpi_ec_read(t + idx, &tmp))
5487 return -EIO;
5488 *value = tmp * 1000;
5489 return 0;
5491 break;
5493 case TPACPI_THERMAL_ACPI_UPDT:
5494 if (idx <= 7) {
5495 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5496 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5497 return -EIO;
5498 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5499 return -EIO;
5500 *value = (t - 2732) * 100;
5501 return 0;
5503 break;
5505 case TPACPI_THERMAL_ACPI_TMP07:
5506 if (idx <= 7) {
5507 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5508 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5509 return -EIO;
5510 if (t > 127 || t < -127)
5511 t = TP_EC_THERMAL_TMP_NA;
5512 *value = t * 1000;
5513 return 0;
5515 break;
5517 case TPACPI_THERMAL_NONE:
5518 default:
5519 return -ENOSYS;
5522 return -EINVAL;
5525 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5527 int res, i;
5528 int n;
5530 n = 8;
5531 i = 0;
5533 if (!s)
5534 return -EINVAL;
5536 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5537 n = 16;
5539 for (i = 0 ; i < n; i++) {
5540 res = thermal_get_sensor(i, &s->temp[i]);
5541 if (res)
5542 return res;
5545 return n;
5548 /* sysfs temp##_input -------------------------------------------------- */
5550 static ssize_t thermal_temp_input_show(struct device *dev,
5551 struct device_attribute *attr,
5552 char *buf)
5554 struct sensor_device_attribute *sensor_attr =
5555 to_sensor_dev_attr(attr);
5556 int idx = sensor_attr->index;
5557 s32 value;
5558 int res;
5560 res = thermal_get_sensor(idx, &value);
5561 if (res)
5562 return res;
5563 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5564 return -ENXIO;
5566 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5569 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5570 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5571 thermal_temp_input_show, NULL, _idxB)
5573 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5574 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5575 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5576 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5577 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5578 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5579 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5580 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5581 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5582 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5583 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5584 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5585 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5586 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5587 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5588 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5589 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5592 #define THERMAL_ATTRS(X) \
5593 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5595 static struct attribute *thermal_temp_input_attr[] = {
5596 THERMAL_ATTRS(8),
5597 THERMAL_ATTRS(9),
5598 THERMAL_ATTRS(10),
5599 THERMAL_ATTRS(11),
5600 THERMAL_ATTRS(12),
5601 THERMAL_ATTRS(13),
5602 THERMAL_ATTRS(14),
5603 THERMAL_ATTRS(15),
5604 THERMAL_ATTRS(0),
5605 THERMAL_ATTRS(1),
5606 THERMAL_ATTRS(2),
5607 THERMAL_ATTRS(3),
5608 THERMAL_ATTRS(4),
5609 THERMAL_ATTRS(5),
5610 THERMAL_ATTRS(6),
5611 THERMAL_ATTRS(7),
5612 NULL
5615 static const struct attribute_group thermal_temp_input16_group = {
5616 .attrs = thermal_temp_input_attr
5619 static const struct attribute_group thermal_temp_input8_group = {
5620 .attrs = &thermal_temp_input_attr[8]
5623 #undef THERMAL_SENSOR_ATTR_TEMP
5624 #undef THERMAL_ATTRS
5626 /* --------------------------------------------------------------------- */
5628 static int __init thermal_init(struct ibm_init_struct *iibm)
5630 u8 t, ta1, ta2;
5631 int i;
5632 int acpi_tmp7;
5633 int res;
5635 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5637 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5639 if (thinkpad_id.ec_model) {
5641 * Direct EC access mode: sensors at registers
5642 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5643 * non-implemented, thermal sensors return 0x80 when
5644 * not available
5647 ta1 = ta2 = 0;
5648 for (i = 0; i < 8; i++) {
5649 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5650 ta1 |= t;
5651 } else {
5652 ta1 = 0;
5653 break;
5655 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5656 ta2 |= t;
5657 } else {
5658 ta1 = 0;
5659 break;
5662 if (ta1 == 0) {
5663 /* This is sheer paranoia, but we handle it anyway */
5664 if (acpi_tmp7) {
5665 printk(TPACPI_ERR
5666 "ThinkPad ACPI EC access misbehaving, "
5667 "falling back to ACPI TMPx access "
5668 "mode\n");
5669 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5670 } else {
5671 printk(TPACPI_ERR
5672 "ThinkPad ACPI EC access misbehaving, "
5673 "disabling thermal sensors access\n");
5674 thermal_read_mode = TPACPI_THERMAL_NONE;
5676 } else {
5677 thermal_read_mode =
5678 (ta2 != 0) ?
5679 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5681 } else if (acpi_tmp7) {
5682 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5683 /* 600e/x, 770e, 770x */
5684 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5685 } else {
5686 /* Standard ACPI TMPx access, max 8 sensors */
5687 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5689 } else {
5690 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5691 thermal_read_mode = TPACPI_THERMAL_NONE;
5694 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5695 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5696 thermal_read_mode);
5698 switch (thermal_read_mode) {
5699 case TPACPI_THERMAL_TPEC_16:
5700 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5701 &thermal_temp_input16_group);
5702 if (res)
5703 return res;
5704 break;
5705 case TPACPI_THERMAL_TPEC_8:
5706 case TPACPI_THERMAL_ACPI_TMP07:
5707 case TPACPI_THERMAL_ACPI_UPDT:
5708 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5709 &thermal_temp_input8_group);
5710 if (res)
5711 return res;
5712 break;
5713 case TPACPI_THERMAL_NONE:
5714 default:
5715 return 1;
5718 return 0;
5721 static void thermal_exit(void)
5723 switch (thermal_read_mode) {
5724 case TPACPI_THERMAL_TPEC_16:
5725 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5726 &thermal_temp_input16_group);
5727 break;
5728 case TPACPI_THERMAL_TPEC_8:
5729 case TPACPI_THERMAL_ACPI_TMP07:
5730 case TPACPI_THERMAL_ACPI_UPDT:
5731 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5732 &thermal_temp_input16_group);
5733 break;
5734 case TPACPI_THERMAL_NONE:
5735 default:
5736 break;
5740 static int thermal_read(char *p)
5742 int len = 0;
5743 int n, i;
5744 struct ibm_thermal_sensors_struct t;
5746 n = thermal_get_sensors(&t);
5747 if (unlikely(n < 0))
5748 return n;
5750 len += sprintf(p + len, "temperatures:\t");
5752 if (n > 0) {
5753 for (i = 0; i < (n - 1); i++)
5754 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5755 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5756 } else
5757 len += sprintf(p + len, "not supported\n");
5759 return len;
5762 static struct ibm_struct thermal_driver_data = {
5763 .name = "thermal",
5764 .read = thermal_read,
5765 .exit = thermal_exit,
5768 /*************************************************************************
5769 * EC Dump subdriver
5772 static u8 ecdump_regs[256];
5774 static int ecdump_read(char *p)
5776 int len = 0;
5777 int i, j;
5778 u8 v;
5780 len += sprintf(p + len, "EC "
5781 " +00 +01 +02 +03 +04 +05 +06 +07"
5782 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5783 for (i = 0; i < 256; i += 16) {
5784 len += sprintf(p + len, "EC 0x%02x:", i);
5785 for (j = 0; j < 16; j++) {
5786 if (!acpi_ec_read(i + j, &v))
5787 break;
5788 if (v != ecdump_regs[i + j])
5789 len += sprintf(p + len, " *%02x", v);
5790 else
5791 len += sprintf(p + len, " %02x", v);
5792 ecdump_regs[i + j] = v;
5794 len += sprintf(p + len, "\n");
5795 if (j != 16)
5796 break;
5799 /* These are way too dangerous to advertise openly... */
5800 #if 0
5801 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5802 " (<offset> is 00-ff, <value> is 00-ff)\n");
5803 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5804 " (<offset> is 00-ff, <value> is 0-255)\n");
5805 #endif
5806 return len;
5809 static int ecdump_write(char *buf)
5811 char *cmd;
5812 int i, v;
5814 while ((cmd = next_cmd(&buf))) {
5815 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5816 /* i and v set */
5817 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5818 /* i and v set */
5819 } else
5820 return -EINVAL;
5821 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5822 if (!acpi_ec_write(i, v))
5823 return -EIO;
5824 } else
5825 return -EINVAL;
5828 return 0;
5831 static struct ibm_struct ecdump_driver_data = {
5832 .name = "ecdump",
5833 .read = ecdump_read,
5834 .write = ecdump_write,
5835 .flags.experimental = 1,
5838 /*************************************************************************
5839 * Backlight/brightness subdriver
5842 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5845 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5846 * CMOS NVRAM byte 0x5E, bits 0-3.
5848 * EC HBRV (0x31) has the following layout
5849 * Bit 7: unknown function
5850 * Bit 6: unknown function
5851 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5852 * Bit 4: must be set to zero to avoid problems
5853 * Bit 3-0: backlight brightness level
5855 * brightness_get_raw returns status data in the HBRV layout
5857 * WARNING: The X61 has been verified to use HBRV for something else, so
5858 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5859 * testing on the very early *60 Lenovo models...
5862 enum {
5863 TP_EC_BACKLIGHT = 0x31,
5865 /* TP_EC_BACKLIGHT bitmasks */
5866 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5867 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5868 TP_EC_BACKLIGHT_MAPSW = 0x20,
5871 enum tpacpi_brightness_access_mode {
5872 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5873 TPACPI_BRGHT_MODE_EC, /* EC control */
5874 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5875 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5876 TPACPI_BRGHT_MODE_MAX
5879 static struct backlight_device *ibm_backlight_device;
5881 static enum tpacpi_brightness_access_mode brightness_mode =
5882 TPACPI_BRGHT_MODE_MAX;
5884 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5886 static struct mutex brightness_mutex;
5888 /* NVRAM brightness access,
5889 * call with brightness_mutex held! */
5890 static unsigned int tpacpi_brightness_nvram_get(void)
5892 u8 lnvram;
5894 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5895 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5896 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5897 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5899 return lnvram;
5902 static void tpacpi_brightness_checkpoint_nvram(void)
5904 u8 lec = 0;
5905 u8 b_nvram;
5907 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5908 return;
5910 vdbg_printk(TPACPI_DBG_BRGHT,
5911 "trying to checkpoint backlight level to NVRAM...\n");
5913 if (mutex_lock_killable(&brightness_mutex) < 0)
5914 return;
5916 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5917 goto unlock;
5918 lec &= TP_EC_BACKLIGHT_LVLMSK;
5919 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5921 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5922 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5923 /* NVRAM needs update */
5924 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5925 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5926 b_nvram |= lec;
5927 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5928 dbg_printk(TPACPI_DBG_BRGHT,
5929 "updated NVRAM backlight level to %u (0x%02x)\n",
5930 (unsigned int) lec, (unsigned int) b_nvram);
5931 } else
5932 vdbg_printk(TPACPI_DBG_BRGHT,
5933 "NVRAM backlight level already is %u (0x%02x)\n",
5934 (unsigned int) lec, (unsigned int) b_nvram);
5936 unlock:
5937 mutex_unlock(&brightness_mutex);
5941 /* call with brightness_mutex held! */
5942 static int tpacpi_brightness_get_raw(int *status)
5944 u8 lec = 0;
5946 switch (brightness_mode) {
5947 case TPACPI_BRGHT_MODE_UCMS_STEP:
5948 *status = tpacpi_brightness_nvram_get();
5949 return 0;
5950 case TPACPI_BRGHT_MODE_EC:
5951 case TPACPI_BRGHT_MODE_ECNVRAM:
5952 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5953 return -EIO;
5954 *status = lec;
5955 return 0;
5956 default:
5957 return -ENXIO;
5961 /* call with brightness_mutex held! */
5962 /* do NOT call with illegal backlight level value */
5963 static int tpacpi_brightness_set_ec(unsigned int value)
5965 u8 lec = 0;
5967 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5968 return -EIO;
5970 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5971 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5972 (value & TP_EC_BACKLIGHT_LVLMSK))))
5973 return -EIO;
5975 return 0;
5978 /* call with brightness_mutex held! */
5979 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5981 int cmos_cmd, inc;
5982 unsigned int current_value, i;
5984 current_value = tpacpi_brightness_nvram_get();
5986 if (value == current_value)
5987 return 0;
5989 cmos_cmd = (value > current_value) ?
5990 TP_CMOS_BRIGHTNESS_UP :
5991 TP_CMOS_BRIGHTNESS_DOWN;
5992 inc = (value > current_value) ? 1 : -1;
5994 for (i = current_value; i != value; i += inc)
5995 if (issue_thinkpad_cmos_command(cmos_cmd))
5996 return -EIO;
5998 return 0;
6001 /* May return EINTR which can always be mapped to ERESTARTSYS */
6002 static int brightness_set(unsigned int value)
6004 int res;
6006 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6007 value < 0)
6008 return -EINVAL;
6010 vdbg_printk(TPACPI_DBG_BRGHT,
6011 "set backlight level to %d\n", value);
6013 res = mutex_lock_killable(&brightness_mutex);
6014 if (res < 0)
6015 return res;
6017 switch (brightness_mode) {
6018 case TPACPI_BRGHT_MODE_EC:
6019 case TPACPI_BRGHT_MODE_ECNVRAM:
6020 res = tpacpi_brightness_set_ec(value);
6021 break;
6022 case TPACPI_BRGHT_MODE_UCMS_STEP:
6023 res = tpacpi_brightness_set_ucmsstep(value);
6024 break;
6025 default:
6026 res = -ENXIO;
6029 mutex_unlock(&brightness_mutex);
6030 return res;
6033 /* sysfs backlight class ----------------------------------------------- */
6035 static int brightness_update_status(struct backlight_device *bd)
6037 unsigned int level =
6038 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6039 bd->props.power == FB_BLANK_UNBLANK) ?
6040 bd->props.brightness : 0;
6042 dbg_printk(TPACPI_DBG_BRGHT,
6043 "backlight: attempt to set level to %d\n",
6044 level);
6046 /* it is the backlight class's job (caller) to handle
6047 * EINTR and other errors properly */
6048 return brightness_set(level);
6051 static int brightness_get(struct backlight_device *bd)
6053 int status, res;
6055 res = mutex_lock_killable(&brightness_mutex);
6056 if (res < 0)
6057 return 0;
6059 res = tpacpi_brightness_get_raw(&status);
6061 mutex_unlock(&brightness_mutex);
6063 if (res < 0)
6064 return 0;
6066 return status & TP_EC_BACKLIGHT_LVLMSK;
6069 static void tpacpi_brightness_notify_change(void)
6071 backlight_force_update(ibm_backlight_device,
6072 BACKLIGHT_UPDATE_HOTKEY);
6075 static struct backlight_ops ibm_backlight_data = {
6076 .get_brightness = brightness_get,
6077 .update_status = brightness_update_status,
6080 /* --------------------------------------------------------------------- */
6083 * These are only useful for models that have only one possibility
6084 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6085 * these quirks.
6087 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6088 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6089 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6091 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6092 /* Models with ATI GPUs known to require ECNVRAM mode */
6093 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6095 /* Models with ATI GPUs that can use ECNVRAM */
6096 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6097 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6098 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6099 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6101 /* Models with Intel Extreme Graphics 2 */
6102 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6103 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6104 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6106 /* Models with Intel GMA900 */
6107 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6108 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6109 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6112 static int __init brightness_init(struct ibm_init_struct *iibm)
6114 int b;
6115 unsigned long quirks;
6117 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6119 mutex_init(&brightness_mutex);
6121 quirks = tpacpi_check_quirks(brightness_quirk_table,
6122 ARRAY_SIZE(brightness_quirk_table));
6125 * We always attempt to detect acpi support, so as to switch
6126 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6127 * going to publish a backlight interface
6129 b = tpacpi_check_std_acpi_brightness_support();
6130 if (b > 0) {
6131 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
6132 printk(TPACPI_NOTICE
6133 "Lenovo BIOS switched to ACPI backlight "
6134 "control mode\n");
6136 if (brightness_enable > 1) {
6137 printk(TPACPI_NOTICE
6138 "standard ACPI backlight interface "
6139 "available, not loading native one...\n");
6140 return 1;
6144 if (!brightness_enable) {
6145 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6146 "brightness support disabled by "
6147 "module parameter\n");
6148 return 1;
6151 if (b > 16) {
6152 printk(TPACPI_ERR
6153 "Unsupported brightness interface, "
6154 "please contact %s\n", TPACPI_MAIL);
6155 return 1;
6157 if (b == 16)
6158 tp_features.bright_16levels = 1;
6161 * Check for module parameter bogosity, note that we
6162 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6163 * able to detect "unspecified"
6165 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6166 return -EINVAL;
6168 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6169 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6170 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6171 if (quirks & TPACPI_BRGHT_Q_EC)
6172 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6173 else
6174 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6176 dbg_printk(TPACPI_DBG_BRGHT,
6177 "driver auto-selected brightness_mode=%d\n",
6178 brightness_mode);
6181 /* Safety */
6182 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6183 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6184 brightness_mode == TPACPI_BRGHT_MODE_EC))
6185 return -EINVAL;
6187 if (tpacpi_brightness_get_raw(&b) < 0)
6188 return 1;
6190 if (tp_features.bright_16levels)
6191 printk(TPACPI_INFO
6192 "detected a 16-level brightness capable ThinkPad\n");
6194 ibm_backlight_device = backlight_device_register(
6195 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6196 &ibm_backlight_data);
6197 if (IS_ERR(ibm_backlight_device)) {
6198 int rc = PTR_ERR(ibm_backlight_device);
6199 ibm_backlight_device = NULL;
6200 printk(TPACPI_ERR "Could not register backlight device\n");
6201 return rc;
6203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6204 "brightness is supported\n");
6206 if (quirks & TPACPI_BRGHT_Q_ASK) {
6207 printk(TPACPI_NOTICE
6208 "brightness: will use unverified default: "
6209 "brightness_mode=%d\n", brightness_mode);
6210 printk(TPACPI_NOTICE
6211 "brightness: please report to %s whether it works well "
6212 "or not on your ThinkPad\n", TPACPI_MAIL);
6215 ibm_backlight_device->props.max_brightness =
6216 (tp_features.bright_16levels)? 15 : 7;
6217 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6218 backlight_update_status(ibm_backlight_device);
6220 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6221 "brightness: registering brightness hotkeys "
6222 "as change notification\n");
6223 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6224 | TP_ACPI_HKEY_BRGHTUP_MASK
6225 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6226 return 0;
6229 static void brightness_suspend(pm_message_t state)
6231 tpacpi_brightness_checkpoint_nvram();
6234 static void brightness_shutdown(void)
6236 tpacpi_brightness_checkpoint_nvram();
6239 static void brightness_exit(void)
6241 if (ibm_backlight_device) {
6242 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6243 "calling backlight_device_unregister()\n");
6244 backlight_device_unregister(ibm_backlight_device);
6247 tpacpi_brightness_checkpoint_nvram();
6250 static int brightness_read(char *p)
6252 int len = 0;
6253 int level;
6255 level = brightness_get(NULL);
6256 if (level < 0) {
6257 len += sprintf(p + len, "level:\t\tunreadable\n");
6258 } else {
6259 len += sprintf(p + len, "level:\t\t%d\n", level);
6260 len += sprintf(p + len, "commands:\tup, down\n");
6261 len += sprintf(p + len, "commands:\tlevel <level>"
6262 " (<level> is 0-%d)\n",
6263 (tp_features.bright_16levels) ? 15 : 7);
6266 return len;
6269 static int brightness_write(char *buf)
6271 int level;
6272 int rc;
6273 char *cmd;
6274 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6276 level = brightness_get(NULL);
6277 if (level < 0)
6278 return level;
6280 while ((cmd = next_cmd(&buf))) {
6281 if (strlencmp(cmd, "up") == 0) {
6282 if (level < max_level)
6283 level++;
6284 } else if (strlencmp(cmd, "down") == 0) {
6285 if (level > 0)
6286 level--;
6287 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6288 level >= 0 && level <= max_level) {
6289 /* new level set */
6290 } else
6291 return -EINVAL;
6294 tpacpi_disclose_usertask("procfs brightness",
6295 "set level to %d\n", level);
6298 * Now we know what the final level should be, so we try to set it.
6299 * Doing it this way makes the syscall restartable in case of EINTR
6301 rc = brightness_set(level);
6302 if (!rc && ibm_backlight_device)
6303 backlight_force_update(ibm_backlight_device,
6304 BACKLIGHT_UPDATE_SYSFS);
6305 return (rc == -EINTR)? -ERESTARTSYS : rc;
6308 static struct ibm_struct brightness_driver_data = {
6309 .name = "brightness",
6310 .read = brightness_read,
6311 .write = brightness_write,
6312 .exit = brightness_exit,
6313 .suspend = brightness_suspend,
6314 .shutdown = brightness_shutdown,
6317 /*************************************************************************
6318 * Volume subdriver
6321 static int volume_offset = 0x30;
6323 static int volume_read(char *p)
6325 int len = 0;
6326 u8 level;
6328 if (!acpi_ec_read(volume_offset, &level)) {
6329 len += sprintf(p + len, "level:\t\tunreadable\n");
6330 } else {
6331 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6332 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6333 len += sprintf(p + len, "commands:\tup, down, mute\n");
6334 len += sprintf(p + len, "commands:\tlevel <level>"
6335 " (<level> is 0-15)\n");
6338 return len;
6341 static int volume_write(char *buf)
6343 int cmos_cmd, inc, i;
6344 u8 level, mute;
6345 int new_level, new_mute;
6346 char *cmd;
6348 while ((cmd = next_cmd(&buf))) {
6349 if (!acpi_ec_read(volume_offset, &level))
6350 return -EIO;
6351 new_mute = mute = level & 0x40;
6352 new_level = level = level & 0xf;
6354 if (strlencmp(cmd, "up") == 0) {
6355 if (mute)
6356 new_mute = 0;
6357 else
6358 new_level = level == 15 ? 15 : level + 1;
6359 } else if (strlencmp(cmd, "down") == 0) {
6360 if (mute)
6361 new_mute = 0;
6362 else
6363 new_level = level == 0 ? 0 : level - 1;
6364 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6365 new_level >= 0 && new_level <= 15) {
6366 /* new_level set */
6367 } else if (strlencmp(cmd, "mute") == 0) {
6368 new_mute = 0x40;
6369 } else
6370 return -EINVAL;
6372 if (new_level != level) {
6373 /* mute doesn't change */
6375 cmos_cmd = (new_level > level) ?
6376 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6377 inc = new_level > level ? 1 : -1;
6379 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6380 !acpi_ec_write(volume_offset, level)))
6381 return -EIO;
6383 for (i = level; i != new_level; i += inc)
6384 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6385 !acpi_ec_write(volume_offset, i + inc))
6386 return -EIO;
6388 if (mute &&
6389 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6390 !acpi_ec_write(volume_offset, new_level + mute))) {
6391 return -EIO;
6395 if (new_mute != mute) {
6396 /* level doesn't change */
6398 cmos_cmd = (new_mute) ?
6399 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6401 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6402 !acpi_ec_write(volume_offset, level + new_mute))
6403 return -EIO;
6407 return 0;
6410 static struct ibm_struct volume_driver_data = {
6411 .name = "volume",
6412 .read = volume_read,
6413 .write = volume_write,
6416 /*************************************************************************
6417 * Fan subdriver
6421 * FAN ACCESS MODES
6423 * TPACPI_FAN_RD_ACPI_GFAN:
6424 * ACPI GFAN method: returns fan level
6426 * see TPACPI_FAN_WR_ACPI_SFAN
6427 * EC 0x2f (HFSP) not available if GFAN exists
6429 * TPACPI_FAN_WR_ACPI_SFAN:
6430 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6432 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6433 * it for writing.
6435 * TPACPI_FAN_WR_TPEC:
6436 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6437 * Supported on almost all ThinkPads
6439 * Fan speed changes of any sort (including those caused by the
6440 * disengaged mode) are usually done slowly by the firmware as the
6441 * maximum ammount of fan duty cycle change per second seems to be
6442 * limited.
6444 * Reading is not available if GFAN exists.
6445 * Writing is not available if SFAN exists.
6447 * Bits
6448 * 7 automatic mode engaged;
6449 * (default operation mode of the ThinkPad)
6450 * fan level is ignored in this mode.
6451 * 6 full speed mode (takes precedence over bit 7);
6452 * not available on all thinkpads. May disable
6453 * the tachometer while the fan controller ramps up
6454 * the speed (which can take up to a few *minutes*).
6455 * Speeds up fan to 100% duty-cycle, which is far above
6456 * the standard RPM levels. It is not impossible that
6457 * it could cause hardware damage.
6458 * 5-3 unused in some models. Extra bits for fan level
6459 * in others, but still useless as all values above
6460 * 7 map to the same speed as level 7 in these models.
6461 * 2-0 fan level (0..7 usually)
6462 * 0x00 = stop
6463 * 0x07 = max (set when temperatures critical)
6464 * Some ThinkPads may have other levels, see
6465 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6467 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6468 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6469 * does so, its initial value is meaningless (0x07).
6471 * For firmware bugs, refer to:
6472 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6474 * ----
6476 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6477 * Main fan tachometer reading (in RPM)
6479 * This register is present on all ThinkPads with a new-style EC, and
6480 * it is known not to be present on the A21m/e, and T22, as there is
6481 * something else in offset 0x84 according to the ACPI DSDT. Other
6482 * ThinkPads from this same time period (and earlier) probably lack the
6483 * tachometer as well.
6485 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6486 * was never fixed by IBM to report the EC firmware version string
6487 * probably support the tachometer (like the early X models), so
6488 * detecting it is quite hard. We need more data to know for sure.
6490 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6491 * might result.
6493 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6494 * mode.
6496 * For firmware bugs, refer to:
6497 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6499 * ----
6501 * ThinkPad EC register 0x31 bit 0 (only on select models)
6503 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6504 * show the speed of the main fan. When bit 0 of EC register 0x31
6505 * is one, the tachometer registers show the speed of the auxiliary
6506 * fan.
6508 * Fan control seems to affect both fans, regardless of the state
6509 * of this bit.
6511 * So far, only the firmware for the X60/X61 non-tablet versions
6512 * seem to support this (firmware TP-7M).
6514 * TPACPI_FAN_WR_ACPI_FANS:
6515 * ThinkPad X31, X40, X41. Not available in the X60.
6517 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6518 * high speed. ACPI DSDT seems to map these three speeds to levels
6519 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6520 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6522 * The speeds are stored on handles
6523 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6525 * There are three default speed sets, acessible as handles:
6526 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6528 * ACPI DSDT switches which set is in use depending on various
6529 * factors.
6531 * TPACPI_FAN_WR_TPEC is also available and should be used to
6532 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6533 * but the ACPI tables just mention level 7.
6536 enum { /* Fan control constants */
6537 fan_status_offset = 0x2f, /* EC register 0x2f */
6538 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6539 * 0x84 must be read before 0x85 */
6540 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6541 bit 0 selects which fan is active */
6543 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6544 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6546 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6549 enum fan_status_access_mode {
6550 TPACPI_FAN_NONE = 0, /* No fan status or control */
6551 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6552 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6555 enum fan_control_access_mode {
6556 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6557 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6558 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6559 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6562 enum fan_control_commands {
6563 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6564 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6565 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6566 * and also watchdog cmd */
6569 static int fan_control_allowed;
6571 static enum fan_status_access_mode fan_status_access_mode;
6572 static enum fan_control_access_mode fan_control_access_mode;
6573 static enum fan_control_commands fan_control_commands;
6575 static u8 fan_control_initial_status;
6576 static u8 fan_control_desired_level;
6577 static u8 fan_control_resume_level;
6578 static int fan_watchdog_maxinterval;
6580 static struct mutex fan_mutex;
6582 static void fan_watchdog_fire(struct work_struct *ignored);
6583 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6585 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6586 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6587 "\\FSPD", /* 600e/x, 770e, 770x */
6588 ); /* all others */
6589 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6590 "JFNS", /* 770x-JL */
6591 ); /* all others */
6594 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6595 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6596 * be in auto mode (0x80).
6598 * This is corrected by any write to HFSP either by the driver, or
6599 * by the firmware.
6601 * We assume 0x07 really means auto mode while this quirk is active,
6602 * as this is far more likely than the ThinkPad being in level 7,
6603 * which is only used by the firmware during thermal emergencies.
6605 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6606 * TP-70 (T43, R52), which are known to be buggy.
6609 static void fan_quirk1_setup(void)
6611 if (fan_control_initial_status == 0x07) {
6612 printk(TPACPI_NOTICE
6613 "fan_init: initial fan status is unknown, "
6614 "assuming it is in auto mode\n");
6615 tp_features.fan_ctrl_status_undef = 1;
6619 static void fan_quirk1_handle(u8 *fan_status)
6621 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6622 if (*fan_status != fan_control_initial_status) {
6623 /* something changed the HFSP regisnter since
6624 * driver init time, so it is not undefined
6625 * anymore */
6626 tp_features.fan_ctrl_status_undef = 0;
6627 } else {
6628 /* Return most likely status. In fact, it
6629 * might be the only possible status */
6630 *fan_status = TP_EC_FAN_AUTO;
6635 /* Select main fan on X60/X61, NOOP on others */
6636 static bool fan_select_fan1(void)
6638 if (tp_features.second_fan) {
6639 u8 val;
6641 if (ec_read(fan_select_offset, &val) < 0)
6642 return false;
6643 val &= 0xFEU;
6644 if (ec_write(fan_select_offset, val) < 0)
6645 return false;
6647 return true;
6650 /* Select secondary fan on X60/X61 */
6651 static bool fan_select_fan2(void)
6653 u8 val;
6655 if (!tp_features.second_fan)
6656 return false;
6658 if (ec_read(fan_select_offset, &val) < 0)
6659 return false;
6660 val |= 0x01U;
6661 if (ec_write(fan_select_offset, val) < 0)
6662 return false;
6664 return true;
6668 * Call with fan_mutex held
6670 static void fan_update_desired_level(u8 status)
6672 if ((status &
6673 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6674 if (status > 7)
6675 fan_control_desired_level = 7;
6676 else
6677 fan_control_desired_level = status;
6681 static int fan_get_status(u8 *status)
6683 u8 s;
6685 /* TODO:
6686 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6688 switch (fan_status_access_mode) {
6689 case TPACPI_FAN_RD_ACPI_GFAN:
6690 /* 570, 600e/x, 770e, 770x */
6692 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6693 return -EIO;
6695 if (likely(status))
6696 *status = s & 0x07;
6698 break;
6700 case TPACPI_FAN_RD_TPEC:
6701 /* all except 570, 600e/x, 770e, 770x */
6702 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6703 return -EIO;
6705 if (likely(status)) {
6706 *status = s;
6707 fan_quirk1_handle(status);
6710 break;
6712 default:
6713 return -ENXIO;
6716 return 0;
6719 static int fan_get_status_safe(u8 *status)
6721 int rc;
6722 u8 s;
6724 if (mutex_lock_killable(&fan_mutex))
6725 return -ERESTARTSYS;
6726 rc = fan_get_status(&s);
6727 if (!rc)
6728 fan_update_desired_level(s);
6729 mutex_unlock(&fan_mutex);
6731 if (status)
6732 *status = s;
6734 return rc;
6737 static int fan_get_speed(unsigned int *speed)
6739 u8 hi, lo;
6741 switch (fan_status_access_mode) {
6742 case TPACPI_FAN_RD_TPEC:
6743 /* all except 570, 600e/x, 770e, 770x */
6744 if (unlikely(!fan_select_fan1()))
6745 return -EIO;
6746 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6747 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6748 return -EIO;
6750 if (likely(speed))
6751 *speed = (hi << 8) | lo;
6753 break;
6755 default:
6756 return -ENXIO;
6759 return 0;
6762 static int fan2_get_speed(unsigned int *speed)
6764 u8 hi, lo;
6765 bool rc;
6767 switch (fan_status_access_mode) {
6768 case TPACPI_FAN_RD_TPEC:
6769 /* all except 570, 600e/x, 770e, 770x */
6770 if (unlikely(!fan_select_fan2()))
6771 return -EIO;
6772 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6773 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6774 fan_select_fan1(); /* play it safe */
6775 if (rc)
6776 return -EIO;
6778 if (likely(speed))
6779 *speed = (hi << 8) | lo;
6781 break;
6783 default:
6784 return -ENXIO;
6787 return 0;
6790 static int fan_set_level(int level)
6792 if (!fan_control_allowed)
6793 return -EPERM;
6795 switch (fan_control_access_mode) {
6796 case TPACPI_FAN_WR_ACPI_SFAN:
6797 if (level >= 0 && level <= 7) {
6798 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6799 return -EIO;
6800 } else
6801 return -EINVAL;
6802 break;
6804 case TPACPI_FAN_WR_ACPI_FANS:
6805 case TPACPI_FAN_WR_TPEC:
6806 if (!(level & TP_EC_FAN_AUTO) &&
6807 !(level & TP_EC_FAN_FULLSPEED) &&
6808 ((level < 0) || (level > 7)))
6809 return -EINVAL;
6811 /* safety net should the EC not support AUTO
6812 * or FULLSPEED mode bits and just ignore them */
6813 if (level & TP_EC_FAN_FULLSPEED)
6814 level |= 7; /* safety min speed 7 */
6815 else if (level & TP_EC_FAN_AUTO)
6816 level |= 4; /* safety min speed 4 */
6818 if (!acpi_ec_write(fan_status_offset, level))
6819 return -EIO;
6820 else
6821 tp_features.fan_ctrl_status_undef = 0;
6822 break;
6824 default:
6825 return -ENXIO;
6828 vdbg_printk(TPACPI_DBG_FAN,
6829 "fan control: set fan control register to 0x%02x\n", level);
6830 return 0;
6833 static int fan_set_level_safe(int level)
6835 int rc;
6837 if (!fan_control_allowed)
6838 return -EPERM;
6840 if (mutex_lock_killable(&fan_mutex))
6841 return -ERESTARTSYS;
6843 if (level == TPACPI_FAN_LAST_LEVEL)
6844 level = fan_control_desired_level;
6846 rc = fan_set_level(level);
6847 if (!rc)
6848 fan_update_desired_level(level);
6850 mutex_unlock(&fan_mutex);
6851 return rc;
6854 static int fan_set_enable(void)
6856 u8 s;
6857 int rc;
6859 if (!fan_control_allowed)
6860 return -EPERM;
6862 if (mutex_lock_killable(&fan_mutex))
6863 return -ERESTARTSYS;
6865 switch (fan_control_access_mode) {
6866 case TPACPI_FAN_WR_ACPI_FANS:
6867 case TPACPI_FAN_WR_TPEC:
6868 rc = fan_get_status(&s);
6869 if (rc < 0)
6870 break;
6872 /* Don't go out of emergency fan mode */
6873 if (s != 7) {
6874 s &= 0x07;
6875 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6878 if (!acpi_ec_write(fan_status_offset, s))
6879 rc = -EIO;
6880 else {
6881 tp_features.fan_ctrl_status_undef = 0;
6882 rc = 0;
6884 break;
6886 case TPACPI_FAN_WR_ACPI_SFAN:
6887 rc = fan_get_status(&s);
6888 if (rc < 0)
6889 break;
6891 s &= 0x07;
6893 /* Set fan to at least level 4 */
6894 s |= 4;
6896 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6897 rc = -EIO;
6898 else
6899 rc = 0;
6900 break;
6902 default:
6903 rc = -ENXIO;
6906 mutex_unlock(&fan_mutex);
6908 if (!rc)
6909 vdbg_printk(TPACPI_DBG_FAN,
6910 "fan control: set fan control register to 0x%02x\n",
6912 return rc;
6915 static int fan_set_disable(void)
6917 int rc;
6919 if (!fan_control_allowed)
6920 return -EPERM;
6922 if (mutex_lock_killable(&fan_mutex))
6923 return -ERESTARTSYS;
6925 rc = 0;
6926 switch (fan_control_access_mode) {
6927 case TPACPI_FAN_WR_ACPI_FANS:
6928 case TPACPI_FAN_WR_TPEC:
6929 if (!acpi_ec_write(fan_status_offset, 0x00))
6930 rc = -EIO;
6931 else {
6932 fan_control_desired_level = 0;
6933 tp_features.fan_ctrl_status_undef = 0;
6935 break;
6937 case TPACPI_FAN_WR_ACPI_SFAN:
6938 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6939 rc = -EIO;
6940 else
6941 fan_control_desired_level = 0;
6942 break;
6944 default:
6945 rc = -ENXIO;
6948 if (!rc)
6949 vdbg_printk(TPACPI_DBG_FAN,
6950 "fan control: set fan control register to 0\n");
6952 mutex_unlock(&fan_mutex);
6953 return rc;
6956 static int fan_set_speed(int speed)
6958 int rc;
6960 if (!fan_control_allowed)
6961 return -EPERM;
6963 if (mutex_lock_killable(&fan_mutex))
6964 return -ERESTARTSYS;
6966 rc = 0;
6967 switch (fan_control_access_mode) {
6968 case TPACPI_FAN_WR_ACPI_FANS:
6969 if (speed >= 0 && speed <= 65535) {
6970 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6971 speed, speed, speed))
6972 rc = -EIO;
6973 } else
6974 rc = -EINVAL;
6975 break;
6977 default:
6978 rc = -ENXIO;
6981 mutex_unlock(&fan_mutex);
6982 return rc;
6985 static void fan_watchdog_reset(void)
6987 static int fan_watchdog_active;
6989 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6990 return;
6992 if (fan_watchdog_active)
6993 cancel_delayed_work(&fan_watchdog_task);
6995 if (fan_watchdog_maxinterval > 0 &&
6996 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6997 fan_watchdog_active = 1;
6998 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6999 msecs_to_jiffies(fan_watchdog_maxinterval
7000 * 1000))) {
7001 printk(TPACPI_ERR
7002 "failed to queue the fan watchdog, "
7003 "watchdog will not trigger\n");
7005 } else
7006 fan_watchdog_active = 0;
7009 static void fan_watchdog_fire(struct work_struct *ignored)
7011 int rc;
7013 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7014 return;
7016 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7017 rc = fan_set_enable();
7018 if (rc < 0) {
7019 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7020 "will try again later...\n", -rc);
7021 /* reschedule for later */
7022 fan_watchdog_reset();
7027 * SYSFS fan layout: hwmon compatible (device)
7029 * pwm*_enable:
7030 * 0: "disengaged" mode
7031 * 1: manual mode
7032 * 2: native EC "auto" mode (recommended, hardware default)
7034 * pwm*: set speed in manual mode, ignored otherwise.
7035 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7036 * interpolation.
7038 * fan*_input: tachometer reading, RPM
7041 * SYSFS fan layout: extensions
7043 * fan_watchdog (driver):
7044 * fan watchdog interval in seconds, 0 disables (default), max 120
7047 /* sysfs fan pwm1_enable ----------------------------------------------- */
7048 static ssize_t fan_pwm1_enable_show(struct device *dev,
7049 struct device_attribute *attr,
7050 char *buf)
7052 int res, mode;
7053 u8 status;
7055 res = fan_get_status_safe(&status);
7056 if (res)
7057 return res;
7059 if (status & TP_EC_FAN_FULLSPEED) {
7060 mode = 0;
7061 } else if (status & TP_EC_FAN_AUTO) {
7062 mode = 2;
7063 } else
7064 mode = 1;
7066 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7069 static ssize_t fan_pwm1_enable_store(struct device *dev,
7070 struct device_attribute *attr,
7071 const char *buf, size_t count)
7073 unsigned long t;
7074 int res, level;
7076 if (parse_strtoul(buf, 2, &t))
7077 return -EINVAL;
7079 tpacpi_disclose_usertask("hwmon pwm1_enable",
7080 "set fan mode to %lu\n", t);
7082 switch (t) {
7083 case 0:
7084 level = TP_EC_FAN_FULLSPEED;
7085 break;
7086 case 1:
7087 level = TPACPI_FAN_LAST_LEVEL;
7088 break;
7089 case 2:
7090 level = TP_EC_FAN_AUTO;
7091 break;
7092 case 3:
7093 /* reserved for software-controlled auto mode */
7094 return -ENOSYS;
7095 default:
7096 return -EINVAL;
7099 res = fan_set_level_safe(level);
7100 if (res == -ENXIO)
7101 return -EINVAL;
7102 else if (res < 0)
7103 return res;
7105 fan_watchdog_reset();
7107 return count;
7110 static struct device_attribute dev_attr_fan_pwm1_enable =
7111 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7112 fan_pwm1_enable_show, fan_pwm1_enable_store);
7114 /* sysfs fan pwm1 ------------------------------------------------------ */
7115 static ssize_t fan_pwm1_show(struct device *dev,
7116 struct device_attribute *attr,
7117 char *buf)
7119 int res;
7120 u8 status;
7122 res = fan_get_status_safe(&status);
7123 if (res)
7124 return res;
7126 if ((status &
7127 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7128 status = fan_control_desired_level;
7130 if (status > 7)
7131 status = 7;
7133 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7136 static ssize_t fan_pwm1_store(struct device *dev,
7137 struct device_attribute *attr,
7138 const char *buf, size_t count)
7140 unsigned long s;
7141 int rc;
7142 u8 status, newlevel;
7144 if (parse_strtoul(buf, 255, &s))
7145 return -EINVAL;
7147 tpacpi_disclose_usertask("hwmon pwm1",
7148 "set fan speed to %lu\n", s);
7150 /* scale down from 0-255 to 0-7 */
7151 newlevel = (s >> 5) & 0x07;
7153 if (mutex_lock_killable(&fan_mutex))
7154 return -ERESTARTSYS;
7156 rc = fan_get_status(&status);
7157 if (!rc && (status &
7158 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7159 rc = fan_set_level(newlevel);
7160 if (rc == -ENXIO)
7161 rc = -EINVAL;
7162 else if (!rc) {
7163 fan_update_desired_level(newlevel);
7164 fan_watchdog_reset();
7168 mutex_unlock(&fan_mutex);
7169 return (rc)? rc : count;
7172 static struct device_attribute dev_attr_fan_pwm1 =
7173 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7174 fan_pwm1_show, fan_pwm1_store);
7176 /* sysfs fan fan1_input ------------------------------------------------ */
7177 static ssize_t fan_fan1_input_show(struct device *dev,
7178 struct device_attribute *attr,
7179 char *buf)
7181 int res;
7182 unsigned int speed;
7184 res = fan_get_speed(&speed);
7185 if (res < 0)
7186 return res;
7188 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7191 static struct device_attribute dev_attr_fan_fan1_input =
7192 __ATTR(fan1_input, S_IRUGO,
7193 fan_fan1_input_show, NULL);
7195 /* sysfs fan fan2_input ------------------------------------------------ */
7196 static ssize_t fan_fan2_input_show(struct device *dev,
7197 struct device_attribute *attr,
7198 char *buf)
7200 int res;
7201 unsigned int speed;
7203 res = fan2_get_speed(&speed);
7204 if (res < 0)
7205 return res;
7207 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7210 static struct device_attribute dev_attr_fan_fan2_input =
7211 __ATTR(fan2_input, S_IRUGO,
7212 fan_fan2_input_show, NULL);
7214 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7215 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7216 char *buf)
7218 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7221 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7222 const char *buf, size_t count)
7224 unsigned long t;
7226 if (parse_strtoul(buf, 120, &t))
7227 return -EINVAL;
7229 if (!fan_control_allowed)
7230 return -EPERM;
7232 fan_watchdog_maxinterval = t;
7233 fan_watchdog_reset();
7235 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7237 return count;
7240 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7241 fan_fan_watchdog_show, fan_fan_watchdog_store);
7243 /* --------------------------------------------------------------------- */
7244 static struct attribute *fan_attributes[] = {
7245 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7246 &dev_attr_fan_fan1_input.attr,
7247 NULL, /* for fan2_input */
7248 NULL
7251 static const struct attribute_group fan_attr_group = {
7252 .attrs = fan_attributes,
7255 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7256 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7258 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7259 { .vendor = PCI_VENDOR_ID_IBM, \
7260 .bios = TPACPI_MATCH_ANY, \
7261 .ec = TPID(__id1, __id2), \
7262 .quirks = __quirks }
7264 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7265 { .vendor = PCI_VENDOR_ID_LENOVO, \
7266 .bios = TPACPI_MATCH_ANY, \
7267 .ec = TPID(__id1, __id2), \
7268 .quirks = __quirks }
7270 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7271 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7272 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7273 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7274 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7275 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7278 #undef TPACPI_FAN_QL
7279 #undef TPACPI_FAN_QI
7281 static int __init fan_init(struct ibm_init_struct *iibm)
7283 int rc;
7284 unsigned long quirks;
7286 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7287 "initializing fan subdriver\n");
7289 mutex_init(&fan_mutex);
7290 fan_status_access_mode = TPACPI_FAN_NONE;
7291 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7292 fan_control_commands = 0;
7293 fan_watchdog_maxinterval = 0;
7294 tp_features.fan_ctrl_status_undef = 0;
7295 tp_features.second_fan = 0;
7296 fan_control_desired_level = 7;
7298 TPACPI_ACPIHANDLE_INIT(fans);
7299 TPACPI_ACPIHANDLE_INIT(gfan);
7300 TPACPI_ACPIHANDLE_INIT(sfan);
7302 quirks = tpacpi_check_quirks(fan_quirk_table,
7303 ARRAY_SIZE(fan_quirk_table));
7305 if (gfan_handle) {
7306 /* 570, 600e/x, 770e, 770x */
7307 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7308 } else {
7309 /* all other ThinkPads: note that even old-style
7310 * ThinkPad ECs supports the fan control register */
7311 if (likely(acpi_ec_read(fan_status_offset,
7312 &fan_control_initial_status))) {
7313 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7314 if (quirks & TPACPI_FAN_Q1)
7315 fan_quirk1_setup();
7316 if (quirks & TPACPI_FAN_2FAN) {
7317 tp_features.second_fan = 1;
7318 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7319 "secondary fan support enabled\n");
7321 } else {
7322 printk(TPACPI_ERR
7323 "ThinkPad ACPI EC access misbehaving, "
7324 "fan status and control unavailable\n");
7325 return 1;
7329 if (sfan_handle) {
7330 /* 570, 770x-JL */
7331 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7332 fan_control_commands |=
7333 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7334 } else {
7335 if (!gfan_handle) {
7336 /* gfan without sfan means no fan control */
7337 /* all other models implement TP EC 0x2f control */
7339 if (fans_handle) {
7340 /* X31, X40, X41 */
7341 fan_control_access_mode =
7342 TPACPI_FAN_WR_ACPI_FANS;
7343 fan_control_commands |=
7344 TPACPI_FAN_CMD_SPEED |
7345 TPACPI_FAN_CMD_LEVEL |
7346 TPACPI_FAN_CMD_ENABLE;
7347 } else {
7348 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7349 fan_control_commands |=
7350 TPACPI_FAN_CMD_LEVEL |
7351 TPACPI_FAN_CMD_ENABLE;
7356 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7357 "fan is %s, modes %d, %d\n",
7358 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7359 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7360 fan_status_access_mode, fan_control_access_mode);
7362 /* fan control master switch */
7363 if (!fan_control_allowed) {
7364 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7365 fan_control_commands = 0;
7366 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7367 "fan control features disabled by parameter\n");
7370 /* update fan_control_desired_level */
7371 if (fan_status_access_mode != TPACPI_FAN_NONE)
7372 fan_get_status_safe(NULL);
7374 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7375 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7376 if (tp_features.second_fan) {
7377 /* attach second fan tachometer */
7378 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7379 &dev_attr_fan_fan2_input.attr;
7381 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7382 &fan_attr_group);
7383 if (rc < 0)
7384 return rc;
7386 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7387 &driver_attr_fan_watchdog);
7388 if (rc < 0) {
7389 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7390 &fan_attr_group);
7391 return rc;
7393 return 0;
7394 } else
7395 return 1;
7398 static void fan_exit(void)
7400 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7401 "cancelling any pending fan watchdog tasks\n");
7403 /* FIXME: can we really do this unconditionally? */
7404 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7405 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7406 &driver_attr_fan_watchdog);
7408 cancel_delayed_work(&fan_watchdog_task);
7409 flush_workqueue(tpacpi_wq);
7412 static void fan_suspend(pm_message_t state)
7414 int rc;
7416 if (!fan_control_allowed)
7417 return;
7419 /* Store fan status in cache */
7420 fan_control_resume_level = 0;
7421 rc = fan_get_status_safe(&fan_control_resume_level);
7422 if (rc < 0)
7423 printk(TPACPI_NOTICE
7424 "failed to read fan level for later "
7425 "restore during resume: %d\n", rc);
7427 /* if it is undefined, don't attempt to restore it.
7428 * KEEP THIS LAST */
7429 if (tp_features.fan_ctrl_status_undef)
7430 fan_control_resume_level = 0;
7433 static void fan_resume(void)
7435 u8 current_level = 7;
7436 bool do_set = false;
7437 int rc;
7439 /* DSDT *always* updates status on resume */
7440 tp_features.fan_ctrl_status_undef = 0;
7442 if (!fan_control_allowed ||
7443 !fan_control_resume_level ||
7444 (fan_get_status_safe(&current_level) < 0))
7445 return;
7447 switch (fan_control_access_mode) {
7448 case TPACPI_FAN_WR_ACPI_SFAN:
7449 /* never decrease fan level */
7450 do_set = (fan_control_resume_level > current_level);
7451 break;
7452 case TPACPI_FAN_WR_ACPI_FANS:
7453 case TPACPI_FAN_WR_TPEC:
7454 /* never decrease fan level, scale is:
7455 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7457 * We expect the firmware to set either 7 or AUTO, but we
7458 * handle FULLSPEED out of paranoia.
7460 * So, we can safely only restore FULLSPEED or 7, anything
7461 * else could slow the fan. Restoring AUTO is useless, at
7462 * best that's exactly what the DSDT already set (it is the
7463 * slower it uses).
7465 * Always keep in mind that the DSDT *will* have set the
7466 * fans to what the vendor supposes is the best level. We
7467 * muck with it only to speed the fan up.
7469 if (fan_control_resume_level != 7 &&
7470 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7471 return;
7472 else
7473 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7474 (current_level != fan_control_resume_level);
7475 break;
7476 default:
7477 return;
7479 if (do_set) {
7480 printk(TPACPI_NOTICE
7481 "restoring fan level to 0x%02x\n",
7482 fan_control_resume_level);
7483 rc = fan_set_level_safe(fan_control_resume_level);
7484 if (rc < 0)
7485 printk(TPACPI_NOTICE
7486 "failed to restore fan level: %d\n", rc);
7490 static int fan_read(char *p)
7492 int len = 0;
7493 int rc;
7494 u8 status;
7495 unsigned int speed = 0;
7497 switch (fan_status_access_mode) {
7498 case TPACPI_FAN_RD_ACPI_GFAN:
7499 /* 570, 600e/x, 770e, 770x */
7500 rc = fan_get_status_safe(&status);
7501 if (rc < 0)
7502 return rc;
7504 len += sprintf(p + len, "status:\t\t%s\n"
7505 "level:\t\t%d\n",
7506 (status != 0) ? "enabled" : "disabled", status);
7507 break;
7509 case TPACPI_FAN_RD_TPEC:
7510 /* all except 570, 600e/x, 770e, 770x */
7511 rc = fan_get_status_safe(&status);
7512 if (rc < 0)
7513 return rc;
7515 len += sprintf(p + len, "status:\t\t%s\n",
7516 (status != 0) ? "enabled" : "disabled");
7518 rc = fan_get_speed(&speed);
7519 if (rc < 0)
7520 return rc;
7522 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7524 if (status & TP_EC_FAN_FULLSPEED)
7525 /* Disengaged mode takes precedence */
7526 len += sprintf(p + len, "level:\t\tdisengaged\n");
7527 else if (status & TP_EC_FAN_AUTO)
7528 len += sprintf(p + len, "level:\t\tauto\n");
7529 else
7530 len += sprintf(p + len, "level:\t\t%d\n", status);
7531 break;
7533 case TPACPI_FAN_NONE:
7534 default:
7535 len += sprintf(p + len, "status:\t\tnot supported\n");
7538 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7539 len += sprintf(p + len, "commands:\tlevel <level>");
7541 switch (fan_control_access_mode) {
7542 case TPACPI_FAN_WR_ACPI_SFAN:
7543 len += sprintf(p + len, " (<level> is 0-7)\n");
7544 break;
7546 default:
7547 len += sprintf(p + len, " (<level> is 0-7, "
7548 "auto, disengaged, full-speed)\n");
7549 break;
7553 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7554 len += sprintf(p + len, "commands:\tenable, disable\n"
7555 "commands:\twatchdog <timeout> (<timeout> "
7556 "is 0 (off), 1-120 (seconds))\n");
7558 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7559 len += sprintf(p + len, "commands:\tspeed <speed>"
7560 " (<speed> is 0-65535)\n");
7562 return len;
7565 static int fan_write_cmd_level(const char *cmd, int *rc)
7567 int level;
7569 if (strlencmp(cmd, "level auto") == 0)
7570 level = TP_EC_FAN_AUTO;
7571 else if ((strlencmp(cmd, "level disengaged") == 0) |
7572 (strlencmp(cmd, "level full-speed") == 0))
7573 level = TP_EC_FAN_FULLSPEED;
7574 else if (sscanf(cmd, "level %d", &level) != 1)
7575 return 0;
7577 *rc = fan_set_level_safe(level);
7578 if (*rc == -ENXIO)
7579 printk(TPACPI_ERR "level command accepted for unsupported "
7580 "access mode %d", fan_control_access_mode);
7581 else if (!*rc)
7582 tpacpi_disclose_usertask("procfs fan",
7583 "set level to %d\n", level);
7585 return 1;
7588 static int fan_write_cmd_enable(const char *cmd, int *rc)
7590 if (strlencmp(cmd, "enable") != 0)
7591 return 0;
7593 *rc = fan_set_enable();
7594 if (*rc == -ENXIO)
7595 printk(TPACPI_ERR "enable command accepted for unsupported "
7596 "access mode %d", fan_control_access_mode);
7597 else if (!*rc)
7598 tpacpi_disclose_usertask("procfs fan", "enable\n");
7600 return 1;
7603 static int fan_write_cmd_disable(const char *cmd, int *rc)
7605 if (strlencmp(cmd, "disable") != 0)
7606 return 0;
7608 *rc = fan_set_disable();
7609 if (*rc == -ENXIO)
7610 printk(TPACPI_ERR "disable command accepted for unsupported "
7611 "access mode %d", fan_control_access_mode);
7612 else if (!*rc)
7613 tpacpi_disclose_usertask("procfs fan", "disable\n");
7615 return 1;
7618 static int fan_write_cmd_speed(const char *cmd, int *rc)
7620 int speed;
7622 /* TODO:
7623 * Support speed <low> <medium> <high> ? */
7625 if (sscanf(cmd, "speed %d", &speed) != 1)
7626 return 0;
7628 *rc = fan_set_speed(speed);
7629 if (*rc == -ENXIO)
7630 printk(TPACPI_ERR "speed command accepted for unsupported "
7631 "access mode %d", fan_control_access_mode);
7632 else if (!*rc)
7633 tpacpi_disclose_usertask("procfs fan",
7634 "set speed to %d\n", speed);
7636 return 1;
7639 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7641 int interval;
7643 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7644 return 0;
7646 if (interval < 0 || interval > 120)
7647 *rc = -EINVAL;
7648 else {
7649 fan_watchdog_maxinterval = interval;
7650 tpacpi_disclose_usertask("procfs fan",
7651 "set watchdog timer to %d\n",
7652 interval);
7655 return 1;
7658 static int fan_write(char *buf)
7660 char *cmd;
7661 int rc = 0;
7663 while (!rc && (cmd = next_cmd(&buf))) {
7664 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7665 fan_write_cmd_level(cmd, &rc)) &&
7666 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7667 (fan_write_cmd_enable(cmd, &rc) ||
7668 fan_write_cmd_disable(cmd, &rc) ||
7669 fan_write_cmd_watchdog(cmd, &rc))) &&
7670 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7671 fan_write_cmd_speed(cmd, &rc))
7673 rc = -EINVAL;
7674 else if (!rc)
7675 fan_watchdog_reset();
7678 return rc;
7681 static struct ibm_struct fan_driver_data = {
7682 .name = "fan",
7683 .read = fan_read,
7684 .write = fan_write,
7685 .exit = fan_exit,
7686 .suspend = fan_suspend,
7687 .resume = fan_resume,
7690 /****************************************************************************
7691 ****************************************************************************
7693 * Infrastructure
7695 ****************************************************************************
7696 ****************************************************************************/
7699 * HKEY event callout for other subdrivers go here
7700 * (yes, it is ugly, but it is quick, safe, and gets the job done
7702 static void tpacpi_driver_event(const unsigned int hkey_event)
7704 if (ibm_backlight_device) {
7705 switch (hkey_event) {
7706 case TP_HKEY_EV_BRGHT_UP:
7707 case TP_HKEY_EV_BRGHT_DOWN:
7708 tpacpi_brightness_notify_change();
7715 static void hotkey_driver_event(const unsigned int scancode)
7717 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7720 /* sysfs name ---------------------------------------------------------- */
7721 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7722 struct device_attribute *attr,
7723 char *buf)
7725 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7728 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7729 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7731 /* --------------------------------------------------------------------- */
7733 /* /proc support */
7734 static struct proc_dir_entry *proc_dir;
7737 * Module and infrastructure proble, init and exit handling
7740 static int force_load;
7742 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7743 static const char * __init str_supported(int is_supported)
7745 static char text_unsupported[] __initdata = "not supported";
7747 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7749 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7751 static void ibm_exit(struct ibm_struct *ibm)
7753 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7755 list_del_init(&ibm->all_drivers);
7757 if (ibm->flags.acpi_notify_installed) {
7758 dbg_printk(TPACPI_DBG_EXIT,
7759 "%s: acpi_remove_notify_handler\n", ibm->name);
7760 BUG_ON(!ibm->acpi);
7761 acpi_remove_notify_handler(*ibm->acpi->handle,
7762 ibm->acpi->type,
7763 dispatch_acpi_notify);
7764 ibm->flags.acpi_notify_installed = 0;
7765 ibm->flags.acpi_notify_installed = 0;
7768 if (ibm->flags.proc_created) {
7769 dbg_printk(TPACPI_DBG_EXIT,
7770 "%s: remove_proc_entry\n", ibm->name);
7771 remove_proc_entry(ibm->name, proc_dir);
7772 ibm->flags.proc_created = 0;
7775 if (ibm->flags.acpi_driver_registered) {
7776 dbg_printk(TPACPI_DBG_EXIT,
7777 "%s: acpi_bus_unregister_driver\n", ibm->name);
7778 BUG_ON(!ibm->acpi);
7779 acpi_bus_unregister_driver(ibm->acpi->driver);
7780 kfree(ibm->acpi->driver);
7781 ibm->acpi->driver = NULL;
7782 ibm->flags.acpi_driver_registered = 0;
7785 if (ibm->flags.init_called && ibm->exit) {
7786 ibm->exit();
7787 ibm->flags.init_called = 0;
7790 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7793 static int __init ibm_init(struct ibm_init_struct *iibm)
7795 int ret;
7796 struct ibm_struct *ibm = iibm->data;
7797 struct proc_dir_entry *entry;
7799 BUG_ON(ibm == NULL);
7801 INIT_LIST_HEAD(&ibm->all_drivers);
7803 if (ibm->flags.experimental && !experimental)
7804 return 0;
7806 dbg_printk(TPACPI_DBG_INIT,
7807 "probing for %s\n", ibm->name);
7809 if (iibm->init) {
7810 ret = iibm->init(iibm);
7811 if (ret > 0)
7812 return 0; /* probe failed */
7813 if (ret)
7814 return ret;
7816 ibm->flags.init_called = 1;
7819 if (ibm->acpi) {
7820 if (ibm->acpi->hid) {
7821 ret = register_tpacpi_subdriver(ibm);
7822 if (ret)
7823 goto err_out;
7826 if (ibm->acpi->notify) {
7827 ret = setup_acpi_notify(ibm);
7828 if (ret == -ENODEV) {
7829 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7830 ibm->name);
7831 ret = 0;
7832 goto err_out;
7834 if (ret < 0)
7835 goto err_out;
7839 dbg_printk(TPACPI_DBG_INIT,
7840 "%s installed\n", ibm->name);
7842 if (ibm->read) {
7843 entry = create_proc_entry(ibm->name,
7844 S_IFREG | S_IRUGO | S_IWUSR,
7845 proc_dir);
7846 if (!entry) {
7847 printk(TPACPI_ERR "unable to create proc entry %s\n",
7848 ibm->name);
7849 ret = -ENODEV;
7850 goto err_out;
7852 entry->owner = THIS_MODULE;
7853 entry->data = ibm;
7854 entry->read_proc = &dispatch_procfs_read;
7855 if (ibm->write)
7856 entry->write_proc = &dispatch_procfs_write;
7857 ibm->flags.proc_created = 1;
7860 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7862 return 0;
7864 err_out:
7865 dbg_printk(TPACPI_DBG_INIT,
7866 "%s: at error exit path with result %d\n",
7867 ibm->name, ret);
7869 ibm_exit(ibm);
7870 return (ret < 0)? ret : 0;
7873 /* Probing */
7875 static bool __pure __init tpacpi_is_fw_digit(const char c)
7877 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7880 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7881 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7882 const char t)
7884 return s && strlen(s) >= 8 &&
7885 tpacpi_is_fw_digit(s[0]) &&
7886 tpacpi_is_fw_digit(s[1]) &&
7887 s[2] == t && s[3] == 'T' &&
7888 tpacpi_is_fw_digit(s[4]) &&
7889 tpacpi_is_fw_digit(s[5]) &&
7890 s[6] == 'W' && s[7] == 'W';
7893 /* returns 0 - probe ok, or < 0 - probe error.
7894 * Probe ok doesn't mean thinkpad found.
7895 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7896 static int __must_check __init get_thinkpad_model_data(
7897 struct thinkpad_id_data *tp)
7899 const struct dmi_device *dev = NULL;
7900 char ec_fw_string[18];
7901 char const *s;
7903 if (!tp)
7904 return -EINVAL;
7906 memset(tp, 0, sizeof(*tp));
7908 if (dmi_name_in_vendors("IBM"))
7909 tp->vendor = PCI_VENDOR_ID_IBM;
7910 else if (dmi_name_in_vendors("LENOVO"))
7911 tp->vendor = PCI_VENDOR_ID_LENOVO;
7912 else
7913 return 0;
7915 s = dmi_get_system_info(DMI_BIOS_VERSION);
7916 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7917 if (s && !tp->bios_version_str)
7918 return -ENOMEM;
7920 /* Really ancient ThinkPad 240X will fail this, which is fine */
7921 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7922 return 0;
7924 tp->bios_model = tp->bios_version_str[0]
7925 | (tp->bios_version_str[1] << 8);
7926 tp->bios_release = (tp->bios_version_str[4] << 8)
7927 | tp->bios_version_str[5];
7930 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7931 * X32 or newer, all Z series; Some models must have an
7932 * up-to-date BIOS or they will not be detected.
7934 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7936 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7937 if (sscanf(dev->name,
7938 "IBM ThinkPad Embedded Controller -[%17c",
7939 ec_fw_string) == 1) {
7940 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7941 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7943 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7944 if (!tp->ec_version_str)
7945 return -ENOMEM;
7947 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7948 tp->ec_model = ec_fw_string[0]
7949 | (ec_fw_string[1] << 8);
7950 tp->ec_release = (ec_fw_string[4] << 8)
7951 | ec_fw_string[5];
7952 } else {
7953 printk(TPACPI_NOTICE
7954 "ThinkPad firmware release %s "
7955 "doesn't match the known patterns\n",
7956 ec_fw_string);
7957 printk(TPACPI_NOTICE
7958 "please report this to %s\n",
7959 TPACPI_MAIL);
7961 break;
7965 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7966 if (s && !strnicmp(s, "ThinkPad", 8)) {
7967 tp->model_str = kstrdup(s, GFP_KERNEL);
7968 if (!tp->model_str)
7969 return -ENOMEM;
7972 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7973 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7974 if (s && !tp->nummodel_str)
7975 return -ENOMEM;
7977 return 0;
7980 static int __init probe_for_thinkpad(void)
7982 int is_thinkpad;
7984 if (acpi_disabled)
7985 return -ENODEV;
7988 * Non-ancient models have better DMI tagging, but very old models
7989 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7991 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7992 (thinkpad_id.ec_model != 0) ||
7993 tpacpi_is_fw_known();
7995 /* ec is required because many other handles are relative to it */
7996 TPACPI_ACPIHANDLE_INIT(ec);
7997 if (!ec_handle) {
7998 if (is_thinkpad)
7999 printk(TPACPI_ERR
8000 "Not yet supported ThinkPad detected!\n");
8001 return -ENODEV;
8004 if (!is_thinkpad && !force_load)
8005 return -ENODEV;
8007 return 0;
8011 /* Module init, exit, parameters */
8013 static struct ibm_init_struct ibms_init[] __initdata = {
8015 .init = thinkpad_acpi_driver_init,
8016 .data = &thinkpad_acpi_driver_data,
8019 .init = hotkey_init,
8020 .data = &hotkey_driver_data,
8023 .init = bluetooth_init,
8024 .data = &bluetooth_driver_data,
8027 .init = wan_init,
8028 .data = &wan_driver_data,
8031 .init = uwb_init,
8032 .data = &uwb_driver_data,
8034 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8036 .init = video_init,
8037 .data = &video_driver_data,
8039 #endif
8041 .init = light_init,
8042 .data = &light_driver_data,
8045 .init = cmos_init,
8046 .data = &cmos_driver_data,
8049 .init = led_init,
8050 .data = &led_driver_data,
8053 .init = beep_init,
8054 .data = &beep_driver_data,
8057 .init = thermal_init,
8058 .data = &thermal_driver_data,
8061 .data = &ecdump_driver_data,
8064 .init = brightness_init,
8065 .data = &brightness_driver_data,
8068 .data = &volume_driver_data,
8071 .init = fan_init,
8072 .data = &fan_driver_data,
8076 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8078 unsigned int i;
8079 struct ibm_struct *ibm;
8081 if (!kp || !kp->name || !val)
8082 return -EINVAL;
8084 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8085 ibm = ibms_init[i].data;
8086 WARN_ON(ibm == NULL);
8088 if (!ibm || !ibm->name)
8089 continue;
8091 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8092 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8093 return -ENOSPC;
8094 strcpy(ibms_init[i].param, val);
8095 strcat(ibms_init[i].param, ",");
8096 return 0;
8100 return -EINVAL;
8103 module_param(experimental, int, 0);
8104 MODULE_PARM_DESC(experimental,
8105 "Enables experimental features when non-zero");
8107 module_param_named(debug, dbg_level, uint, 0);
8108 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8110 module_param(force_load, bool, 0);
8111 MODULE_PARM_DESC(force_load,
8112 "Attempts to load the driver even on a "
8113 "mis-identified ThinkPad when true");
8115 module_param_named(fan_control, fan_control_allowed, bool, 0);
8116 MODULE_PARM_DESC(fan_control,
8117 "Enables setting fan parameters features when true");
8119 module_param_named(brightness_mode, brightness_mode, uint, 0);
8120 MODULE_PARM_DESC(brightness_mode,
8121 "Selects brightness control strategy: "
8122 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8124 module_param(brightness_enable, uint, 0);
8125 MODULE_PARM_DESC(brightness_enable,
8126 "Enables backlight control when 1, disables when 0");
8128 module_param(hotkey_report_mode, uint, 0);
8129 MODULE_PARM_DESC(hotkey_report_mode,
8130 "used for backwards compatibility with userspace, "
8131 "see documentation");
8133 #define TPACPI_PARAM(feature) \
8134 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8135 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8136 "at module load, see documentation")
8138 TPACPI_PARAM(hotkey);
8139 TPACPI_PARAM(bluetooth);
8140 TPACPI_PARAM(video);
8141 TPACPI_PARAM(light);
8142 TPACPI_PARAM(cmos);
8143 TPACPI_PARAM(led);
8144 TPACPI_PARAM(beep);
8145 TPACPI_PARAM(ecdump);
8146 TPACPI_PARAM(brightness);
8147 TPACPI_PARAM(volume);
8148 TPACPI_PARAM(fan);
8150 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8151 module_param(dbg_wlswemul, uint, 0);
8152 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8153 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8154 MODULE_PARM_DESC(wlsw_state,
8155 "Initial state of the emulated WLSW switch");
8157 module_param(dbg_bluetoothemul, uint, 0);
8158 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8159 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8160 MODULE_PARM_DESC(bluetooth_state,
8161 "Initial state of the emulated bluetooth switch");
8163 module_param(dbg_wwanemul, uint, 0);
8164 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8165 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8166 MODULE_PARM_DESC(wwan_state,
8167 "Initial state of the emulated WWAN switch");
8169 module_param(dbg_uwbemul, uint, 0);
8170 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8171 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8172 MODULE_PARM_DESC(uwb_state,
8173 "Initial state of the emulated UWB switch");
8174 #endif
8176 static void thinkpad_acpi_module_exit(void)
8178 struct ibm_struct *ibm, *itmp;
8180 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8182 list_for_each_entry_safe_reverse(ibm, itmp,
8183 &tpacpi_all_drivers,
8184 all_drivers) {
8185 ibm_exit(ibm);
8188 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8190 if (tpacpi_inputdev) {
8191 if (tp_features.input_device_registered)
8192 input_unregister_device(tpacpi_inputdev);
8193 else
8194 input_free_device(tpacpi_inputdev);
8197 if (tpacpi_hwmon)
8198 hwmon_device_unregister(tpacpi_hwmon);
8200 if (tp_features.sensors_pdev_attrs_registered)
8201 device_remove_file(&tpacpi_sensors_pdev->dev,
8202 &dev_attr_thinkpad_acpi_pdev_name);
8203 if (tpacpi_sensors_pdev)
8204 platform_device_unregister(tpacpi_sensors_pdev);
8205 if (tpacpi_pdev)
8206 platform_device_unregister(tpacpi_pdev);
8208 if (tp_features.sensors_pdrv_attrs_registered)
8209 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8210 if (tp_features.platform_drv_attrs_registered)
8211 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8213 if (tp_features.sensors_pdrv_registered)
8214 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8216 if (tp_features.platform_drv_registered)
8217 platform_driver_unregister(&tpacpi_pdriver);
8219 if (proc_dir)
8220 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8222 if (tpacpi_wq)
8223 destroy_workqueue(tpacpi_wq);
8225 kfree(thinkpad_id.bios_version_str);
8226 kfree(thinkpad_id.ec_version_str);
8227 kfree(thinkpad_id.model_str);
8231 static int __init thinkpad_acpi_module_init(void)
8233 int ret, i;
8235 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8237 /* Parameter checking */
8238 if (hotkey_report_mode > 2)
8239 return -EINVAL;
8241 /* Driver-level probe */
8243 ret = get_thinkpad_model_data(&thinkpad_id);
8244 if (ret) {
8245 printk(TPACPI_ERR
8246 "unable to get DMI data: %d\n", ret);
8247 thinkpad_acpi_module_exit();
8248 return ret;
8250 ret = probe_for_thinkpad();
8251 if (ret) {
8252 thinkpad_acpi_module_exit();
8253 return ret;
8256 /* Driver initialization */
8258 TPACPI_ACPIHANDLE_INIT(ecrd);
8259 TPACPI_ACPIHANDLE_INIT(ecwr);
8261 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8262 if (!tpacpi_wq) {
8263 thinkpad_acpi_module_exit();
8264 return -ENOMEM;
8267 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8268 if (!proc_dir) {
8269 printk(TPACPI_ERR
8270 "unable to create proc dir " TPACPI_PROC_DIR);
8271 thinkpad_acpi_module_exit();
8272 return -ENODEV;
8274 proc_dir->owner = THIS_MODULE;
8276 ret = platform_driver_register(&tpacpi_pdriver);
8277 if (ret) {
8278 printk(TPACPI_ERR
8279 "unable to register main platform driver\n");
8280 thinkpad_acpi_module_exit();
8281 return ret;
8283 tp_features.platform_drv_registered = 1;
8285 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8286 if (ret) {
8287 printk(TPACPI_ERR
8288 "unable to register hwmon platform driver\n");
8289 thinkpad_acpi_module_exit();
8290 return ret;
8292 tp_features.sensors_pdrv_registered = 1;
8294 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8295 if (!ret) {
8296 tp_features.platform_drv_attrs_registered = 1;
8297 ret = tpacpi_create_driver_attributes(
8298 &tpacpi_hwmon_pdriver.driver);
8300 if (ret) {
8301 printk(TPACPI_ERR
8302 "unable to create sysfs driver attributes\n");
8303 thinkpad_acpi_module_exit();
8304 return ret;
8306 tp_features.sensors_pdrv_attrs_registered = 1;
8309 /* Device initialization */
8310 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8311 NULL, 0);
8312 if (IS_ERR(tpacpi_pdev)) {
8313 ret = PTR_ERR(tpacpi_pdev);
8314 tpacpi_pdev = NULL;
8315 printk(TPACPI_ERR "unable to register platform device\n");
8316 thinkpad_acpi_module_exit();
8317 return ret;
8319 tpacpi_sensors_pdev = platform_device_register_simple(
8320 TPACPI_HWMON_DRVR_NAME,
8321 -1, NULL, 0);
8322 if (IS_ERR(tpacpi_sensors_pdev)) {
8323 ret = PTR_ERR(tpacpi_sensors_pdev);
8324 tpacpi_sensors_pdev = NULL;
8325 printk(TPACPI_ERR
8326 "unable to register hwmon platform device\n");
8327 thinkpad_acpi_module_exit();
8328 return ret;
8330 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8331 &dev_attr_thinkpad_acpi_pdev_name);
8332 if (ret) {
8333 printk(TPACPI_ERR
8334 "unable to create sysfs hwmon device attributes\n");
8335 thinkpad_acpi_module_exit();
8336 return ret;
8338 tp_features.sensors_pdev_attrs_registered = 1;
8339 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8340 if (IS_ERR(tpacpi_hwmon)) {
8341 ret = PTR_ERR(tpacpi_hwmon);
8342 tpacpi_hwmon = NULL;
8343 printk(TPACPI_ERR "unable to register hwmon device\n");
8344 thinkpad_acpi_module_exit();
8345 return ret;
8347 mutex_init(&tpacpi_inputdev_send_mutex);
8348 tpacpi_inputdev = input_allocate_device();
8349 if (!tpacpi_inputdev) {
8350 printk(TPACPI_ERR "unable to allocate input device\n");
8351 thinkpad_acpi_module_exit();
8352 return -ENOMEM;
8353 } else {
8354 /* Prepare input device, but don't register */
8355 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8356 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8357 tpacpi_inputdev->id.bustype = BUS_HOST;
8358 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8359 thinkpad_id.vendor :
8360 PCI_VENDOR_ID_IBM;
8361 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8362 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8364 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8365 ret = ibm_init(&ibms_init[i]);
8366 if (ret >= 0 && *ibms_init[i].param)
8367 ret = ibms_init[i].data->write(ibms_init[i].param);
8368 if (ret < 0) {
8369 thinkpad_acpi_module_exit();
8370 return ret;
8373 ret = input_register_device(tpacpi_inputdev);
8374 if (ret < 0) {
8375 printk(TPACPI_ERR "unable to register input device\n");
8376 thinkpad_acpi_module_exit();
8377 return ret;
8378 } else {
8379 tp_features.input_device_registered = 1;
8382 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8383 return 0;
8386 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8389 * This will autoload the driver in almost every ThinkPad
8390 * in widespread use.
8392 * Only _VERY_ old models, like the 240, 240x and 570 lack
8393 * the HKEY event interface.
8395 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8398 * DMI matching for module autoloading
8400 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8401 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8403 * Only models listed in thinkwiki will be supported, so add yours
8404 * if it is not there yet.
8406 #define IBM_BIOS_MODULE_ALIAS(__type) \
8407 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8409 /* Ancient thinkpad BIOSes have to be identified by
8410 * BIOS type or model number, and there are far less
8411 * BIOS types than model numbers... */
8412 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8414 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8415 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8416 MODULE_DESCRIPTION(TPACPI_DESC);
8417 MODULE_VERSION(TPACPI_VERSION);
8418 MODULE_LICENSE("GPL");
8420 module_init(thinkpad_acpi_module_init);
8421 module_exit(thinkpad_acpi_module_exit);