thinkpad-acpi: volume subdriver rewrite
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobf2a6d0faaf01ba1a3cf9a56785c49e635c5a666d
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
241 #define TPACPI_DBG_MIXER 0x0040
243 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
244 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
245 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
248 /****************************************************************************
249 * Driver-wide structs and misc. variables
252 struct ibm_struct;
254 struct tp_acpi_drv_struct {
255 const struct acpi_device_id *hid;
256 struct acpi_driver *driver;
258 void (*notify) (struct ibm_struct *, u32);
259 acpi_handle *handle;
260 u32 type;
261 struct acpi_device *device;
264 struct ibm_struct {
265 char *name;
267 int (*read) (char *);
268 int (*write) (char *);
269 void (*exit) (void);
270 void (*resume) (void);
271 void (*suspend) (pm_message_t state);
272 void (*shutdown) (void);
274 struct list_head all_drivers;
276 struct tp_acpi_drv_struct *acpi;
278 struct {
279 u8 acpi_driver_registered:1;
280 u8 acpi_notify_installed:1;
281 u8 proc_created:1;
282 u8 init_called:1;
283 u8 experimental:1;
284 } flags;
287 struct ibm_init_struct {
288 char param[32];
290 int (*init) (struct ibm_init_struct *);
291 struct ibm_struct *data;
294 static struct {
295 u32 bluetooth:1;
296 u32 hotkey:1;
297 u32 hotkey_mask:1;
298 u32 hotkey_wlsw:1;
299 u32 hotkey_tablet:1;
300 u32 light:1;
301 u32 light_status:1;
302 u32 bright_16levels:1;
303 u32 bright_acpimode:1;
304 u32 wan:1;
305 u32 uwb:1;
306 u32 fan_ctrl_status_undef:1;
307 u32 second_fan:1;
308 u32 beep_needs_two_args:1;
309 u32 input_device_registered:1;
310 u32 platform_drv_registered:1;
311 u32 platform_drv_attrs_registered:1;
312 u32 sensors_pdrv_registered:1;
313 u32 sensors_pdrv_attrs_registered:1;
314 u32 sensors_pdev_attrs_registered:1;
315 u32 hotkey_poll_active:1;
316 } tp_features;
318 static struct {
319 u16 hotkey_mask_ff:1;
320 } tp_warned;
322 struct thinkpad_id_data {
323 unsigned int vendor; /* ThinkPad vendor:
324 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
326 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
327 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
329 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
330 u16 ec_model;
331 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
332 u16 ec_release;
334 char *model_str; /* ThinkPad T43 */
335 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
337 static struct thinkpad_id_data thinkpad_id;
339 static enum {
340 TPACPI_LIFE_INIT = 0,
341 TPACPI_LIFE_RUNNING,
342 TPACPI_LIFE_EXITING,
343 } tpacpi_lifecycle;
345 static int experimental;
346 static u32 dbg_level;
348 static struct workqueue_struct *tpacpi_wq;
350 enum led_status_t {
351 TPACPI_LED_OFF = 0,
352 TPACPI_LED_ON,
353 TPACPI_LED_BLINK,
356 /* Special LED class that can defer work */
357 struct tpacpi_led_classdev {
358 struct led_classdev led_classdev;
359 struct work_struct work;
360 enum led_status_t new_state;
361 unsigned int led;
364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
365 static int dbg_wlswemul;
366 static int tpacpi_wlsw_emulstate;
367 static int dbg_bluetoothemul;
368 static int tpacpi_bluetooth_emulstate;
369 static int dbg_wwanemul;
370 static int tpacpi_wwan_emulstate;
371 static int dbg_uwbemul;
372 static int tpacpi_uwb_emulstate;
373 #endif
376 /*************************************************************************
377 * Debugging helpers
380 #define dbg_printk(a_dbg_level, format, arg...) \
381 do { if (dbg_level & (a_dbg_level)) \
382 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
383 } while (0)
385 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
386 #define vdbg_printk dbg_printk
387 static const char *str_supported(int is_supported);
388 #else
389 #define vdbg_printk(a_dbg_level, format, arg...) \
390 do { } while (0)
391 #endif
393 static void tpacpi_log_usertask(const char * const what)
395 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
396 what, task_tgid_vnr(current));
399 #define tpacpi_disclose_usertask(what, format, arg...) \
400 do { \
401 if (unlikely( \
402 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
403 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
404 printk(TPACPI_DEBUG "%s: PID %d: " format, \
405 what, task_tgid_vnr(current), ## arg); \
407 } while (0)
410 * Quirk handling helpers
412 * ThinkPad IDs and versions seen in the field so far
413 * are two-characters from the set [0-9A-Z], i.e. base 36.
415 * We use values well outside that range as specials.
418 #define TPACPI_MATCH_ANY 0xffffU
419 #define TPACPI_MATCH_UNKNOWN 0U
421 /* TPID('1', 'Y') == 0x5931 */
422 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
424 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
425 { .vendor = PCI_VENDOR_ID_IBM, \
426 .bios = TPID(__id1, __id2), \
427 .ec = TPACPI_MATCH_ANY, \
428 .quirks = (__quirk) }
430 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
431 { .vendor = PCI_VENDOR_ID_LENOVO, \
432 .bios = TPID(__id1, __id2), \
433 .ec = TPACPI_MATCH_ANY, \
434 .quirks = (__quirk) }
436 struct tpacpi_quirk {
437 unsigned int vendor;
438 u16 bios;
439 u16 ec;
440 unsigned long quirks;
444 * tpacpi_check_quirks() - search BIOS/EC version on a list
445 * @qlist: array of &struct tpacpi_quirk
446 * @qlist_size: number of elements in @qlist
448 * Iterates over a quirks list until one is found that matches the
449 * ThinkPad's vendor, BIOS and EC model.
451 * Returns 0 if nothing matches, otherwise returns the quirks field of
452 * the matching &struct tpacpi_quirk entry.
454 * The match criteria is: vendor, ec and bios much match.
456 static unsigned long __init tpacpi_check_quirks(
457 const struct tpacpi_quirk *qlist,
458 unsigned int qlist_size)
460 while (qlist_size) {
461 if ((qlist->vendor == thinkpad_id.vendor ||
462 qlist->vendor == TPACPI_MATCH_ANY) &&
463 (qlist->bios == thinkpad_id.bios_model ||
464 qlist->bios == TPACPI_MATCH_ANY) &&
465 (qlist->ec == thinkpad_id.ec_model ||
466 qlist->ec == TPACPI_MATCH_ANY))
467 return qlist->quirks;
469 qlist_size--;
470 qlist++;
472 return 0;
476 /****************************************************************************
477 ****************************************************************************
479 * ACPI Helpers and device model
481 ****************************************************************************
482 ****************************************************************************/
484 /*************************************************************************
485 * ACPI basic handles
488 static acpi_handle root_handle;
490 #define TPACPI_HANDLE(object, parent, paths...) \
491 static acpi_handle object##_handle; \
492 static acpi_handle *object##_parent = &parent##_handle; \
493 static char *object##_path; \
494 static char *object##_paths[] = { paths }
496 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
497 "\\_SB.PCI.ISA.EC", /* 570 */
498 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
499 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
500 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
501 "\\_SB.PCI0.ICH3.EC0", /* R31 */
502 "\\_SB.PCI0.LPC.EC", /* all others */
505 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
506 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
508 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
509 /* T4x, X31, X40 */
510 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
511 "\\CMS", /* R40, R40e */
512 ); /* all others */
514 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
515 "^HKEY", /* R30, R31 */
516 "HKEY", /* all others */
517 ); /* 570 */
519 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
520 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
521 "\\_SB.PCI0.VID0", /* 770e */
522 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
523 "\\_SB.PCI0.AGP.VID", /* all others */
524 ); /* R30, R31 */
527 /*************************************************************************
528 * ACPI helpers
531 static int acpi_evalf(acpi_handle handle,
532 void *res, char *method, char *fmt, ...)
534 char *fmt0 = fmt;
535 struct acpi_object_list params;
536 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
537 struct acpi_buffer result, *resultp;
538 union acpi_object out_obj;
539 acpi_status status;
540 va_list ap;
541 char res_type;
542 int success;
543 int quiet;
545 if (!*fmt) {
546 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
547 return 0;
550 if (*fmt == 'q') {
551 quiet = 1;
552 fmt++;
553 } else
554 quiet = 0;
556 res_type = *(fmt++);
558 params.count = 0;
559 params.pointer = &in_objs[0];
561 va_start(ap, fmt);
562 while (*fmt) {
563 char c = *(fmt++);
564 switch (c) {
565 case 'd': /* int */
566 in_objs[params.count].integer.value = va_arg(ap, int);
567 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
568 break;
569 /* add more types as needed */
570 default:
571 printk(TPACPI_ERR "acpi_evalf() called "
572 "with invalid format character '%c'\n", c);
573 return 0;
576 va_end(ap);
578 if (res_type != 'v') {
579 result.length = sizeof(out_obj);
580 result.pointer = &out_obj;
581 resultp = &result;
582 } else
583 resultp = NULL;
585 status = acpi_evaluate_object(handle, method, &params, resultp);
587 switch (res_type) {
588 case 'd': /* int */
589 if (res)
590 *(int *)res = out_obj.integer.value;
591 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
592 break;
593 case 'v': /* void */
594 success = status == AE_OK;
595 break;
596 /* add more types as needed */
597 default:
598 printk(TPACPI_ERR "acpi_evalf() called "
599 "with invalid format character '%c'\n", res_type);
600 return 0;
603 if (!success && !quiet)
604 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
605 method, fmt0, status);
607 return success;
610 static int acpi_ec_read(int i, u8 *p)
612 int v;
614 if (ecrd_handle) {
615 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
616 return 0;
617 *p = v;
618 } else {
619 if (ec_read(i, p) < 0)
620 return 0;
623 return 1;
626 static int acpi_ec_write(int i, u8 v)
628 if (ecwr_handle) {
629 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
630 return 0;
631 } else {
632 if (ec_write(i, v) < 0)
633 return 0;
636 return 1;
639 static int issue_thinkpad_cmos_command(int cmos_cmd)
641 if (!cmos_handle)
642 return -ENXIO;
644 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
645 return -EIO;
647 return 0;
650 /*************************************************************************
651 * ACPI device model
654 #define TPACPI_ACPIHANDLE_INIT(object) \
655 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
656 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
658 static void drv_acpi_handle_init(char *name,
659 acpi_handle *handle, acpi_handle parent,
660 char **paths, int num_paths, char **path)
662 int i;
663 acpi_status status;
665 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
666 name);
668 for (i = 0; i < num_paths; i++) {
669 status = acpi_get_handle(parent, paths[i], handle);
670 if (ACPI_SUCCESS(status)) {
671 *path = paths[i];
672 dbg_printk(TPACPI_DBG_INIT,
673 "Found ACPI handle %s for %s\n",
674 *path, name);
675 return;
679 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
680 name);
681 *handle = NULL;
684 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
686 struct ibm_struct *ibm = data;
688 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
689 return;
691 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
692 return;
694 ibm->acpi->notify(ibm, event);
697 static int __init setup_acpi_notify(struct ibm_struct *ibm)
699 acpi_status status;
700 int rc;
702 BUG_ON(!ibm->acpi);
704 if (!*ibm->acpi->handle)
705 return 0;
707 vdbg_printk(TPACPI_DBG_INIT,
708 "setting up ACPI notify for %s\n", ibm->name);
710 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
711 if (rc < 0) {
712 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
713 ibm->name, rc);
714 return -ENODEV;
717 ibm->acpi->device->driver_data = ibm;
718 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
719 TPACPI_ACPI_EVENT_PREFIX,
720 ibm->name);
722 status = acpi_install_notify_handler(*ibm->acpi->handle,
723 ibm->acpi->type, dispatch_acpi_notify, ibm);
724 if (ACPI_FAILURE(status)) {
725 if (status == AE_ALREADY_EXISTS) {
726 printk(TPACPI_NOTICE
727 "another device driver is already "
728 "handling %s events\n", ibm->name);
729 } else {
730 printk(TPACPI_ERR
731 "acpi_install_notify_handler(%s) failed: %d\n",
732 ibm->name, status);
734 return -ENODEV;
736 ibm->flags.acpi_notify_installed = 1;
737 return 0;
740 static int __init tpacpi_device_add(struct acpi_device *device)
742 return 0;
745 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
747 int rc;
749 dbg_printk(TPACPI_DBG_INIT,
750 "registering %s as an ACPI driver\n", ibm->name);
752 BUG_ON(!ibm->acpi);
754 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
755 if (!ibm->acpi->driver) {
756 printk(TPACPI_ERR
757 "failed to allocate memory for ibm->acpi->driver\n");
758 return -ENOMEM;
761 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
762 ibm->acpi->driver->ids = ibm->acpi->hid;
764 ibm->acpi->driver->ops.add = &tpacpi_device_add;
766 rc = acpi_bus_register_driver(ibm->acpi->driver);
767 if (rc < 0) {
768 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
769 ibm->name, rc);
770 kfree(ibm->acpi->driver);
771 ibm->acpi->driver = NULL;
772 } else if (!rc)
773 ibm->flags.acpi_driver_registered = 1;
775 return rc;
779 /****************************************************************************
780 ****************************************************************************
782 * Procfs Helpers
784 ****************************************************************************
785 ****************************************************************************/
787 static int dispatch_procfs_read(char *page, char **start, off_t off,
788 int count, int *eof, void *data)
790 struct ibm_struct *ibm = data;
791 int len;
793 if (!ibm || !ibm->read)
794 return -EINVAL;
796 len = ibm->read(page);
797 if (len < 0)
798 return len;
800 if (len <= off + count)
801 *eof = 1;
802 *start = page + off;
803 len -= off;
804 if (len > count)
805 len = count;
806 if (len < 0)
807 len = 0;
809 return len;
812 static int dispatch_procfs_write(struct file *file,
813 const char __user *userbuf,
814 unsigned long count, void *data)
816 struct ibm_struct *ibm = data;
817 char *kernbuf;
818 int ret;
820 if (!ibm || !ibm->write)
821 return -EINVAL;
822 if (count > PAGE_SIZE - 2)
823 return -EINVAL;
825 kernbuf = kmalloc(count + 2, GFP_KERNEL);
826 if (!kernbuf)
827 return -ENOMEM;
829 if (copy_from_user(kernbuf, userbuf, count)) {
830 kfree(kernbuf);
831 return -EFAULT;
834 kernbuf[count] = 0;
835 strcat(kernbuf, ",");
836 ret = ibm->write(kernbuf);
837 if (ret == 0)
838 ret = count;
840 kfree(kernbuf);
842 return ret;
845 static char *next_cmd(char **cmds)
847 char *start = *cmds;
848 char *end;
850 while ((end = strchr(start, ',')) && end == start)
851 start = end + 1;
853 if (!end)
854 return NULL;
856 *end = 0;
857 *cmds = end + 1;
858 return start;
862 /****************************************************************************
863 ****************************************************************************
865 * Device model: input, hwmon and platform
867 ****************************************************************************
868 ****************************************************************************/
870 static struct platform_device *tpacpi_pdev;
871 static struct platform_device *tpacpi_sensors_pdev;
872 static struct device *tpacpi_hwmon;
873 static struct input_dev *tpacpi_inputdev;
874 static struct mutex tpacpi_inputdev_send_mutex;
875 static LIST_HEAD(tpacpi_all_drivers);
877 static int tpacpi_suspend_handler(struct platform_device *pdev,
878 pm_message_t state)
880 struct ibm_struct *ibm, *itmp;
882 list_for_each_entry_safe(ibm, itmp,
883 &tpacpi_all_drivers,
884 all_drivers) {
885 if (ibm->suspend)
886 (ibm->suspend)(state);
889 return 0;
892 static int tpacpi_resume_handler(struct platform_device *pdev)
894 struct ibm_struct *ibm, *itmp;
896 list_for_each_entry_safe(ibm, itmp,
897 &tpacpi_all_drivers,
898 all_drivers) {
899 if (ibm->resume)
900 (ibm->resume)();
903 return 0;
906 static void tpacpi_shutdown_handler(struct platform_device *pdev)
908 struct ibm_struct *ibm, *itmp;
910 list_for_each_entry_safe(ibm, itmp,
911 &tpacpi_all_drivers,
912 all_drivers) {
913 if (ibm->shutdown)
914 (ibm->shutdown)();
918 static struct platform_driver tpacpi_pdriver = {
919 .driver = {
920 .name = TPACPI_DRVR_NAME,
921 .owner = THIS_MODULE,
923 .suspend = tpacpi_suspend_handler,
924 .resume = tpacpi_resume_handler,
925 .shutdown = tpacpi_shutdown_handler,
928 static struct platform_driver tpacpi_hwmon_pdriver = {
929 .driver = {
930 .name = TPACPI_HWMON_DRVR_NAME,
931 .owner = THIS_MODULE,
935 /*************************************************************************
936 * sysfs support helpers
939 struct attribute_set {
940 unsigned int members, max_members;
941 struct attribute_group group;
944 struct attribute_set_obj {
945 struct attribute_set s;
946 struct attribute *a;
947 } __attribute__((packed));
949 static struct attribute_set *create_attr_set(unsigned int max_members,
950 const char *name)
952 struct attribute_set_obj *sobj;
954 if (max_members == 0)
955 return NULL;
957 /* Allocates space for implicit NULL at the end too */
958 sobj = kzalloc(sizeof(struct attribute_set_obj) +
959 max_members * sizeof(struct attribute *),
960 GFP_KERNEL);
961 if (!sobj)
962 return NULL;
963 sobj->s.max_members = max_members;
964 sobj->s.group.attrs = &sobj->a;
965 sobj->s.group.name = name;
967 return &sobj->s;
970 #define destroy_attr_set(_set) \
971 kfree(_set);
973 /* not multi-threaded safe, use it in a single thread per set */
974 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
976 if (!s || !attr)
977 return -EINVAL;
979 if (s->members >= s->max_members)
980 return -ENOMEM;
982 s->group.attrs[s->members] = attr;
983 s->members++;
985 return 0;
988 static int add_many_to_attr_set(struct attribute_set *s,
989 struct attribute **attr,
990 unsigned int count)
992 int i, res;
994 for (i = 0; i < count; i++) {
995 res = add_to_attr_set(s, attr[i]);
996 if (res)
997 return res;
1000 return 0;
1003 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1005 sysfs_remove_group(kobj, &s->group);
1006 destroy_attr_set(s);
1009 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1010 sysfs_create_group(_kobj, &_attr_set->group)
1012 static int parse_strtoul(const char *buf,
1013 unsigned long max, unsigned long *value)
1015 char *endp;
1017 while (*buf && isspace(*buf))
1018 buf++;
1019 *value = simple_strtoul(buf, &endp, 0);
1020 while (*endp && isspace(*endp))
1021 endp++;
1022 if (*endp || *value > max)
1023 return -EINVAL;
1025 return 0;
1028 static void tpacpi_disable_brightness_delay(void)
1030 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1031 printk(TPACPI_NOTICE
1032 "ACPI backlight control delay disabled\n");
1035 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1037 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1038 union acpi_object *obj;
1039 int rc;
1041 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1042 obj = (union acpi_object *)buffer.pointer;
1043 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1044 printk(TPACPI_ERR "Unknown _BCL data, "
1045 "please report this to %s\n", TPACPI_MAIL);
1046 rc = 0;
1047 } else {
1048 rc = obj->package.count;
1050 } else {
1051 return 0;
1054 kfree(buffer.pointer);
1055 return rc;
1058 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1059 u32 lvl, void *context, void **rv)
1061 char name[ACPI_PATH_SEGMENT_LENGTH];
1062 struct acpi_buffer buffer = { sizeof(name), &name };
1064 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1065 !strncmp("_BCL", name, sizeof(name) - 1)) {
1066 BUG_ON(!rv || !*rv);
1067 **(int **)rv = tpacpi_query_bcl_levels(handle);
1068 return AE_CTRL_TERMINATE;
1069 } else {
1070 return AE_OK;
1075 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1077 static int __init tpacpi_check_std_acpi_brightness_support(void)
1079 int status;
1080 int bcl_levels = 0;
1081 void *bcl_ptr = &bcl_levels;
1083 if (!vid_handle) {
1084 TPACPI_ACPIHANDLE_INIT(vid);
1086 if (!vid_handle)
1087 return 0;
1090 * Search for a _BCL method, and execute it. This is safe on all
1091 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1092 * BIOS in ACPI backlight control mode. We do NOT have to care
1093 * about calling the _BCL method in an enabled video device, any
1094 * will do for our purposes.
1097 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1098 tpacpi_acpi_walk_find_bcl, NULL,
1099 &bcl_ptr);
1101 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1102 tp_features.bright_acpimode = 1;
1103 return (bcl_levels - 2);
1106 return 0;
1109 static int __init tpacpi_new_rfkill(const unsigned int id,
1110 struct rfkill **rfk,
1111 const enum rfkill_type rfktype,
1112 const char *name,
1113 const bool set_default,
1114 int (*toggle_radio)(void *, enum rfkill_state),
1115 int (*get_state)(void *, enum rfkill_state *))
1117 int res;
1118 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1120 res = get_state(NULL, &initial_state);
1121 if (res < 0) {
1122 printk(TPACPI_ERR
1123 "failed to read initial state for %s, error %d; "
1124 "will turn radio off\n", name, res);
1125 } else if (set_default) {
1126 /* try to set the initial state as the default for the rfkill
1127 * type, since we ask the firmware to preserve it across S5 in
1128 * NVRAM */
1129 if (rfkill_set_default(rfktype,
1130 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1131 RFKILL_STATE_UNBLOCKED :
1132 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1133 vdbg_printk(TPACPI_DBG_RFKILL,
1134 "Default state for %s cannot be changed\n",
1135 name);
1138 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1139 if (!*rfk) {
1140 printk(TPACPI_ERR
1141 "failed to allocate memory for rfkill class\n");
1142 return -ENOMEM;
1145 (*rfk)->name = name;
1146 (*rfk)->get_state = get_state;
1147 (*rfk)->toggle_radio = toggle_radio;
1148 (*rfk)->state = initial_state;
1150 res = rfkill_register(*rfk);
1151 if (res < 0) {
1152 printk(TPACPI_ERR
1153 "failed to register %s rfkill switch: %d\n",
1154 name, res);
1155 rfkill_free(*rfk);
1156 *rfk = NULL;
1157 return res;
1160 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1161 name, (initial_state == RFKILL_STATE_UNBLOCKED) ?
1162 "un" : (initial_state == RFKILL_STATE_SOFT_BLOCKED) ?
1163 "soft-" : "hard-");
1164 return 0;
1167 static void printk_deprecated_attribute(const char * const what,
1168 const char * const details)
1170 tpacpi_log_usertask("deprecated sysfs attribute");
1171 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1172 "will be removed. %s\n",
1173 what, details);
1176 static void printk_deprecated_rfkill_attribute(const char * const what)
1178 printk_deprecated_attribute(what,
1179 "Please switch to generic rfkill before year 2010");
1182 /*************************************************************************
1183 * thinkpad-acpi driver attributes
1186 /* interface_version --------------------------------------------------- */
1187 static ssize_t tpacpi_driver_interface_version_show(
1188 struct device_driver *drv,
1189 char *buf)
1191 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1194 static DRIVER_ATTR(interface_version, S_IRUGO,
1195 tpacpi_driver_interface_version_show, NULL);
1197 /* debug_level --------------------------------------------------------- */
1198 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1199 char *buf)
1201 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1204 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1205 const char *buf, size_t count)
1207 unsigned long t;
1209 if (parse_strtoul(buf, 0xffff, &t))
1210 return -EINVAL;
1212 dbg_level = t;
1214 return count;
1217 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1218 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1220 /* version ------------------------------------------------------------- */
1221 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1222 char *buf)
1224 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1225 TPACPI_DESC, TPACPI_VERSION);
1228 static DRIVER_ATTR(version, S_IRUGO,
1229 tpacpi_driver_version_show, NULL);
1231 /* --------------------------------------------------------------------- */
1233 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1235 static void tpacpi_send_radiosw_update(void);
1237 /* wlsw_emulstate ------------------------------------------------------ */
1238 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1239 char *buf)
1241 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1244 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1245 const char *buf, size_t count)
1247 unsigned long t;
1249 if (parse_strtoul(buf, 1, &t))
1250 return -EINVAL;
1252 if (tpacpi_wlsw_emulstate != t) {
1253 tpacpi_wlsw_emulstate = !!t;
1254 tpacpi_send_radiosw_update();
1255 } else
1256 tpacpi_wlsw_emulstate = !!t;
1258 return count;
1261 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1262 tpacpi_driver_wlsw_emulstate_show,
1263 tpacpi_driver_wlsw_emulstate_store);
1265 /* bluetooth_emulstate ------------------------------------------------- */
1266 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1267 struct device_driver *drv,
1268 char *buf)
1270 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1273 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1274 struct device_driver *drv,
1275 const char *buf, size_t count)
1277 unsigned long t;
1279 if (parse_strtoul(buf, 1, &t))
1280 return -EINVAL;
1282 tpacpi_bluetooth_emulstate = !!t;
1284 return count;
1287 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1288 tpacpi_driver_bluetooth_emulstate_show,
1289 tpacpi_driver_bluetooth_emulstate_store);
1291 /* wwan_emulstate ------------------------------------------------- */
1292 static ssize_t tpacpi_driver_wwan_emulstate_show(
1293 struct device_driver *drv,
1294 char *buf)
1296 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1299 static ssize_t tpacpi_driver_wwan_emulstate_store(
1300 struct device_driver *drv,
1301 const char *buf, size_t count)
1303 unsigned long t;
1305 if (parse_strtoul(buf, 1, &t))
1306 return -EINVAL;
1308 tpacpi_wwan_emulstate = !!t;
1310 return count;
1313 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1314 tpacpi_driver_wwan_emulstate_show,
1315 tpacpi_driver_wwan_emulstate_store);
1317 /* uwb_emulstate ------------------------------------------------- */
1318 static ssize_t tpacpi_driver_uwb_emulstate_show(
1319 struct device_driver *drv,
1320 char *buf)
1322 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1325 static ssize_t tpacpi_driver_uwb_emulstate_store(
1326 struct device_driver *drv,
1327 const char *buf, size_t count)
1329 unsigned long t;
1331 if (parse_strtoul(buf, 1, &t))
1332 return -EINVAL;
1334 tpacpi_uwb_emulstate = !!t;
1336 return count;
1339 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1340 tpacpi_driver_uwb_emulstate_show,
1341 tpacpi_driver_uwb_emulstate_store);
1342 #endif
1344 /* --------------------------------------------------------------------- */
1346 static struct driver_attribute *tpacpi_driver_attributes[] = {
1347 &driver_attr_debug_level, &driver_attr_version,
1348 &driver_attr_interface_version,
1351 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1353 int i, res;
1355 i = 0;
1356 res = 0;
1357 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1358 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1359 i++;
1362 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1363 if (!res && dbg_wlswemul)
1364 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1365 if (!res && dbg_bluetoothemul)
1366 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1367 if (!res && dbg_wwanemul)
1368 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1369 if (!res && dbg_uwbemul)
1370 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1371 #endif
1373 return res;
1376 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1378 int i;
1380 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1381 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1383 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1384 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1385 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1386 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1387 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1388 #endif
1391 /*************************************************************************
1392 * Firmware Data
1396 * Table of recommended minimum BIOS versions
1398 * Reasons for listing:
1399 * 1. Stable BIOS, listed because the unknown ammount of
1400 * bugs and bad ACPI behaviour on older versions
1402 * 2. BIOS or EC fw with known bugs that trigger on Linux
1404 * 3. BIOS with known reduced functionality in older versions
1406 * We recommend the latest BIOS and EC version.
1407 * We only support the latest BIOS and EC fw version as a rule.
1409 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1410 * Information from users in ThinkWiki
1412 * WARNING: we use this table also to detect that the machine is
1413 * a ThinkPad in some cases, so don't remove entries lightly.
1416 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1417 { .vendor = (__v), \
1418 .bios = TPID(__id1, __id2), \
1419 .ec = TPACPI_MATCH_ANY, \
1420 .quirks = TPACPI_MATCH_ANY << 16 \
1421 | (__bv1) << 8 | (__bv2) }
1423 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1424 __eid, __ev1, __ev2) \
1425 { .vendor = (__v), \
1426 .bios = TPID(__bid1, __bid2), \
1427 .ec = __eid, \
1428 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1429 | (__bv1) << 8 | (__bv2) }
1431 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1432 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1434 /* Outdated IBM BIOSes often lack the EC id string */
1435 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1436 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1437 __bv1, __bv2, TPID(__id1, __id2), \
1438 __ev1, __ev2), \
1439 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1440 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1441 __ev1, __ev2)
1443 /* Outdated IBM BIOSes often lack the EC id string */
1444 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1445 __eid1, __eid2, __ev1, __ev2) \
1446 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1447 __bv1, __bv2, TPID(__eid1, __eid2), \
1448 __ev1, __ev2), \
1449 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1450 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1451 __ev1, __ev2)
1453 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1454 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1456 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1457 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1458 __bv1, __bv2, TPID(__id1, __id2), \
1459 __ev1, __ev2)
1461 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1462 __eid1, __eid2, __ev1, __ev2) \
1463 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1464 __bv1, __bv2, TPID(__eid1, __eid2), \
1465 __ev1, __ev2)
1467 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1468 /* Numeric models ------------------ */
1469 /* FW MODEL BIOS VERS */
1470 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1471 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1472 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1473 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1474 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1475 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1476 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1477 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1478 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1480 /* A-series ------------------------- */
1481 /* FW MODEL BIOS VERS EC VERS */
1482 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1483 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1484 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1485 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1486 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1487 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1488 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1489 TPV_QI0('1', '3', '2', '0'), /* A22m */
1490 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1491 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1492 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1494 /* G-series ------------------------- */
1495 /* FW MODEL BIOS VERS */
1496 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1497 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1499 /* R-series, T-series --------------- */
1500 /* FW MODEL BIOS VERS EC VERS */
1501 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1502 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1503 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1504 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1505 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1506 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1507 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1508 T40/p, T41/p, T42/p (1) */
1509 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1510 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1511 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1512 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1514 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1515 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1516 TPV_QI0('1', '6', '3', '2'), /* T22 */
1517 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1518 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1519 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1521 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1522 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1523 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1525 /* BIOS FW BIOS VERS EC FW EC VERS */
1526 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1527 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1529 /* X-series ------------------------- */
1530 /* FW MODEL BIOS VERS EC VERS */
1531 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1532 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1533 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1534 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1535 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1536 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1537 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1539 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1540 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1542 /* (0) - older versions lack DMI EC fw string and functionality */
1543 /* (1) - older versions known to lack functionality */
1546 #undef TPV_QL1
1547 #undef TPV_QL0
1548 #undef TPV_QI2
1549 #undef TPV_QI1
1550 #undef TPV_QI0
1551 #undef TPV_Q_X
1552 #undef TPV_Q
1554 static void __init tpacpi_check_outdated_fw(void)
1556 unsigned long fwvers;
1557 u16 ec_version, bios_version;
1559 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1560 ARRAY_SIZE(tpacpi_bios_version_qtable));
1562 if (!fwvers)
1563 return;
1565 bios_version = fwvers & 0xffffU;
1566 ec_version = (fwvers >> 16) & 0xffffU;
1568 /* note that unknown versions are set to 0x0000 and we use that */
1569 if ((bios_version > thinkpad_id.bios_release) ||
1570 (ec_version > thinkpad_id.ec_release &&
1571 ec_version != TPACPI_MATCH_ANY)) {
1573 * The changelogs would let us track down the exact
1574 * reason, but it is just too much of a pain to track
1575 * it. We only list BIOSes that are either really
1576 * broken, or really stable to begin with, so it is
1577 * best if the user upgrades the firmware anyway.
1579 printk(TPACPI_WARN
1580 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1581 printk(TPACPI_WARN
1582 "WARNING: This firmware may be missing critical bug "
1583 "fixes and/or important features\n");
1587 static bool __init tpacpi_is_fw_known(void)
1589 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1590 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1593 /****************************************************************************
1594 ****************************************************************************
1596 * Subdrivers
1598 ****************************************************************************
1599 ****************************************************************************/
1601 /*************************************************************************
1602 * thinkpad-acpi init subdriver
1605 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1607 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1608 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1610 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1611 (thinkpad_id.bios_version_str) ?
1612 thinkpad_id.bios_version_str : "unknown",
1613 (thinkpad_id.ec_version_str) ?
1614 thinkpad_id.ec_version_str : "unknown");
1616 if (thinkpad_id.vendor && thinkpad_id.model_str)
1617 printk(TPACPI_INFO "%s %s, model %s\n",
1618 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1619 "IBM" : ((thinkpad_id.vendor ==
1620 PCI_VENDOR_ID_LENOVO) ?
1621 "Lenovo" : "Unknown vendor"),
1622 thinkpad_id.model_str,
1623 (thinkpad_id.nummodel_str) ?
1624 thinkpad_id.nummodel_str : "unknown");
1626 tpacpi_check_outdated_fw();
1627 return 0;
1630 static int thinkpad_acpi_driver_read(char *p)
1632 int len = 0;
1634 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1635 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1637 return len;
1640 static struct ibm_struct thinkpad_acpi_driver_data = {
1641 .name = "driver",
1642 .read = thinkpad_acpi_driver_read,
1645 /*************************************************************************
1646 * Hotkey subdriver
1650 * ThinkPad firmware event model
1652 * The ThinkPad firmware has two main event interfaces: normal ACPI
1653 * notifications (which follow the ACPI standard), and a private event
1654 * interface.
1656 * The private event interface also issues events for the hotkeys. As
1657 * the driver gained features, the event handling code ended up being
1658 * built around the hotkey subdriver. This will need to be refactored
1659 * to a more formal event API eventually.
1661 * Some "hotkeys" are actually supposed to be used as event reports,
1662 * such as "brightness has changed", "volume has changed", depending on
1663 * the ThinkPad model and how the firmware is operating.
1665 * Unlike other classes, hotkey-class events have mask/unmask control on
1666 * non-ancient firmware. However, how it behaves changes a lot with the
1667 * firmware model and version.
1670 enum { /* hot key scan codes (derived from ACPI DSDT) */
1671 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1672 TP_ACPI_HOTKEYSCAN_FNF2,
1673 TP_ACPI_HOTKEYSCAN_FNF3,
1674 TP_ACPI_HOTKEYSCAN_FNF4,
1675 TP_ACPI_HOTKEYSCAN_FNF5,
1676 TP_ACPI_HOTKEYSCAN_FNF6,
1677 TP_ACPI_HOTKEYSCAN_FNF7,
1678 TP_ACPI_HOTKEYSCAN_FNF8,
1679 TP_ACPI_HOTKEYSCAN_FNF9,
1680 TP_ACPI_HOTKEYSCAN_FNF10,
1681 TP_ACPI_HOTKEYSCAN_FNF11,
1682 TP_ACPI_HOTKEYSCAN_FNF12,
1683 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1684 TP_ACPI_HOTKEYSCAN_FNINSERT,
1685 TP_ACPI_HOTKEYSCAN_FNDELETE,
1686 TP_ACPI_HOTKEYSCAN_FNHOME,
1687 TP_ACPI_HOTKEYSCAN_FNEND,
1688 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1689 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1690 TP_ACPI_HOTKEYSCAN_FNSPACE,
1691 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1692 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1693 TP_ACPI_HOTKEYSCAN_MUTE,
1694 TP_ACPI_HOTKEYSCAN_THINKPAD,
1697 enum { /* Keys/events available through NVRAM polling */
1698 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1699 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1702 enum { /* Positions of some of the keys in hotkey masks */
1703 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1704 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1705 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1706 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1707 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1708 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1709 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1710 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1711 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1712 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1713 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1716 enum { /* NVRAM to ACPI HKEY group map */
1717 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1718 TP_ACPI_HKEY_ZOOM_MASK |
1719 TP_ACPI_HKEY_DISPSWTCH_MASK |
1720 TP_ACPI_HKEY_HIBERNATE_MASK,
1721 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1722 TP_ACPI_HKEY_BRGHTDWN_MASK,
1723 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1724 TP_ACPI_HKEY_VOLDWN_MASK |
1725 TP_ACPI_HKEY_MUTE_MASK,
1728 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1729 struct tp_nvram_state {
1730 u16 thinkpad_toggle:1;
1731 u16 zoom_toggle:1;
1732 u16 display_toggle:1;
1733 u16 thinklight_toggle:1;
1734 u16 hibernate_toggle:1;
1735 u16 displayexp_toggle:1;
1736 u16 display_state:1;
1737 u16 brightness_toggle:1;
1738 u16 volume_toggle:1;
1739 u16 mute:1;
1741 u8 brightness_level;
1742 u8 volume_level;
1745 /* kthread for the hotkey poller */
1746 static struct task_struct *tpacpi_hotkey_task;
1748 /* Acquired while the poller kthread is running, use to sync start/stop */
1749 static struct mutex hotkey_thread_mutex;
1752 * Acquire mutex to write poller control variables as an
1753 * atomic block.
1755 * Increment hotkey_config_change when changing them if you
1756 * want the kthread to forget old state.
1758 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1760 static struct mutex hotkey_thread_data_mutex;
1761 static unsigned int hotkey_config_change;
1764 * hotkey poller control variables
1766 * Must be atomic or readers will also need to acquire mutex
1768 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1769 * should be used only when the changes need to be taken as
1770 * a block, OR when one needs to force the kthread to forget
1771 * old state.
1773 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1774 static unsigned int hotkey_poll_freq = 10; /* Hz */
1776 #define HOTKEY_CONFIG_CRITICAL_START \
1777 do { \
1778 mutex_lock(&hotkey_thread_data_mutex); \
1779 hotkey_config_change++; \
1780 } while (0);
1781 #define HOTKEY_CONFIG_CRITICAL_END \
1782 mutex_unlock(&hotkey_thread_data_mutex);
1784 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1786 #define hotkey_source_mask 0U
1787 #define HOTKEY_CONFIG_CRITICAL_START
1788 #define HOTKEY_CONFIG_CRITICAL_END
1790 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1792 static struct mutex hotkey_mutex;
1794 static enum { /* Reasons for waking up */
1795 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1796 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1797 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1798 } hotkey_wakeup_reason;
1800 static int hotkey_autosleep_ack;
1802 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1803 static u32 hotkey_all_mask; /* all events supported in fw */
1804 static u32 hotkey_reserved_mask; /* events better left disabled */
1805 static u32 hotkey_driver_mask; /* events needed by the driver */
1806 static u32 hotkey_user_mask; /* events visible to userspace */
1807 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1809 static unsigned int hotkey_report_mode;
1811 static u16 *hotkey_keycode_map;
1813 static struct attribute_set *hotkey_dev_attributes;
1815 static void tpacpi_driver_event(const unsigned int hkey_event);
1816 static void hotkey_driver_event(const unsigned int scancode);
1818 /* HKEY.MHKG() return bits */
1819 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1821 static int hotkey_get_wlsw(int *status)
1823 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1824 if (dbg_wlswemul) {
1825 *status = !!tpacpi_wlsw_emulstate;
1826 return 0;
1828 #endif
1829 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1830 return -EIO;
1831 return 0;
1834 static int hotkey_get_tablet_mode(int *status)
1836 int s;
1838 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1839 return -EIO;
1841 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1842 return 0;
1846 * Reads current event mask from firmware, and updates
1847 * hotkey_acpi_mask accordingly. Also resets any bits
1848 * from hotkey_user_mask that are unavailable to be
1849 * delivered (shadow requirement of the userspace ABI).
1851 * Call with hotkey_mutex held
1853 static int hotkey_mask_get(void)
1855 if (tp_features.hotkey_mask) {
1856 u32 m = 0;
1858 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1859 return -EIO;
1861 hotkey_acpi_mask = m;
1862 } else {
1863 /* no mask support doesn't mean no event support... */
1864 hotkey_acpi_mask = hotkey_all_mask;
1867 /* sync userspace-visible mask */
1868 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1870 return 0;
1873 void static hotkey_mask_warn_incomplete_mask(void)
1875 /* log only what the user can fix... */
1876 const u32 wantedmask = hotkey_driver_mask &
1877 ~(hotkey_acpi_mask | hotkey_source_mask) &
1878 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1880 if (wantedmask)
1881 printk(TPACPI_NOTICE
1882 "required events 0x%08x not enabled!\n",
1883 wantedmask);
1887 * Set the firmware mask when supported
1889 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1891 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1893 * Call with hotkey_mutex held
1895 static int hotkey_mask_set(u32 mask)
1897 int i;
1898 int rc = 0;
1900 const u32 fwmask = mask & ~hotkey_source_mask;
1902 if (tp_features.hotkey_mask) {
1903 for (i = 0; i < 32; i++) {
1904 if (!acpi_evalf(hkey_handle,
1905 NULL, "MHKM", "vdd", i + 1,
1906 !!(mask & (1 << i)))) {
1907 rc = -EIO;
1908 break;
1914 * We *must* make an inconditional call to hotkey_mask_get to
1915 * refresh hotkey_acpi_mask and update hotkey_user_mask
1917 * Take the opportunity to also log when we cannot _enable_
1918 * a given event.
1920 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1921 printk(TPACPI_NOTICE
1922 "asked for hotkey mask 0x%08x, but "
1923 "firmware forced it to 0x%08x\n",
1924 fwmask, hotkey_acpi_mask);
1927 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1928 hotkey_mask_warn_incomplete_mask();
1930 return rc;
1934 * Sets hotkey_user_mask and tries to set the firmware mask
1936 * Call with hotkey_mutex held
1938 static int hotkey_user_mask_set(const u32 mask)
1940 int rc;
1942 /* Give people a chance to notice they are doing something that
1943 * is bound to go boom on their users sooner or later */
1944 if (!tp_warned.hotkey_mask_ff &&
1945 (mask == 0xffff || mask == 0xffffff ||
1946 mask == 0xffffffff)) {
1947 tp_warned.hotkey_mask_ff = 1;
1948 printk(TPACPI_NOTICE
1949 "setting the hotkey mask to 0x%08x is likely "
1950 "not the best way to go about it\n", mask);
1951 printk(TPACPI_NOTICE
1952 "please consider using the driver defaults, "
1953 "and refer to up-to-date thinkpad-acpi "
1954 "documentation\n");
1957 /* Try to enable what the user asked for, plus whatever we need.
1958 * this syncs everything but won't enable bits in hotkey_user_mask */
1959 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1961 /* Enable the available bits in hotkey_user_mask */
1962 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1964 return rc;
1968 * Sets the driver hotkey mask.
1970 * Can be called even if the hotkey subdriver is inactive
1972 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1974 int rc;
1976 /* Do the right thing if hotkey_init has not been called yet */
1977 if (!tp_features.hotkey) {
1978 hotkey_driver_mask = mask;
1979 return 0;
1982 mutex_lock(&hotkey_mutex);
1984 HOTKEY_CONFIG_CRITICAL_START
1985 hotkey_driver_mask = mask;
1986 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1987 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1988 #endif
1989 HOTKEY_CONFIG_CRITICAL_END
1991 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1992 ~hotkey_source_mask);
1993 mutex_unlock(&hotkey_mutex);
1995 return rc;
1998 static int hotkey_status_get(int *status)
2000 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2001 return -EIO;
2003 return 0;
2006 static int hotkey_status_set(bool enable)
2008 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2009 return -EIO;
2011 return 0;
2014 static void tpacpi_input_send_tabletsw(void)
2016 int state;
2018 if (tp_features.hotkey_tablet &&
2019 !hotkey_get_tablet_mode(&state)) {
2020 mutex_lock(&tpacpi_inputdev_send_mutex);
2022 input_report_switch(tpacpi_inputdev,
2023 SW_TABLET_MODE, !!state);
2024 input_sync(tpacpi_inputdev);
2026 mutex_unlock(&tpacpi_inputdev_send_mutex);
2030 /* Do NOT call without validating scancode first */
2031 static void tpacpi_input_send_key(const unsigned int scancode)
2033 const unsigned int keycode = hotkey_keycode_map[scancode];
2035 if (keycode != KEY_RESERVED) {
2036 mutex_lock(&tpacpi_inputdev_send_mutex);
2038 input_report_key(tpacpi_inputdev, keycode, 1);
2039 if (keycode == KEY_UNKNOWN)
2040 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2041 scancode);
2042 input_sync(tpacpi_inputdev);
2044 input_report_key(tpacpi_inputdev, keycode, 0);
2045 if (keycode == KEY_UNKNOWN)
2046 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2047 scancode);
2048 input_sync(tpacpi_inputdev);
2050 mutex_unlock(&tpacpi_inputdev_send_mutex);
2054 /* Do NOT call without validating scancode first */
2055 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2057 hotkey_driver_event(scancode);
2058 if (hotkey_user_mask & (1 << scancode))
2059 tpacpi_input_send_key(scancode);
2062 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2063 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2065 /* Do NOT call without validating scancode first */
2066 static void tpacpi_hotkey_send_key(unsigned int scancode)
2068 tpacpi_input_send_key_masked(scancode);
2069 if (hotkey_report_mode < 2) {
2070 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2071 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2075 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2077 u8 d;
2079 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2080 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2081 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2082 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2083 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2084 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2086 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2087 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2088 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2090 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2091 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2092 n->displayexp_toggle =
2093 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2095 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2096 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2097 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2098 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2099 n->brightness_toggle =
2100 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2102 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2103 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2104 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2105 >> TP_NVRAM_POS_LEVEL_VOLUME;
2106 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2107 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2111 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2112 struct tp_nvram_state *newn,
2113 const u32 event_mask)
2116 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2117 do { \
2118 if ((event_mask & (1 << __scancode)) && \
2119 oldn->__member != newn->__member) \
2120 tpacpi_hotkey_send_key(__scancode); \
2121 } while (0)
2123 #define TPACPI_MAY_SEND_KEY(__scancode) \
2124 do { \
2125 if (event_mask & (1 << __scancode)) \
2126 tpacpi_hotkey_send_key(__scancode); \
2127 } while (0)
2129 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2130 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2131 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2132 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2134 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2136 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2138 /* handle volume */
2139 if (oldn->volume_toggle != newn->volume_toggle) {
2140 if (oldn->mute != newn->mute) {
2141 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2143 if (oldn->volume_level > newn->volume_level) {
2144 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2145 } else if (oldn->volume_level < newn->volume_level) {
2146 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2147 } else if (oldn->mute == newn->mute) {
2148 /* repeated key presses that didn't change state */
2149 if (newn->mute) {
2150 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2151 } else if (newn->volume_level != 0) {
2152 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2153 } else {
2154 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2159 /* handle brightness */
2160 if (oldn->brightness_toggle != newn->brightness_toggle) {
2161 if (oldn->brightness_level < newn->brightness_level) {
2162 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2163 } else if (oldn->brightness_level > newn->brightness_level) {
2164 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2165 } else {
2166 /* repeated key presses that didn't change state */
2167 if (newn->brightness_level != 0) {
2168 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2169 } else {
2170 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2175 #undef TPACPI_COMPARE_KEY
2176 #undef TPACPI_MAY_SEND_KEY
2180 * Polling driver
2182 * We track all events in hotkey_source_mask all the time, since
2183 * most of them are edge-based. We only issue those requested by
2184 * hotkey_user_mask or hotkey_driver_mask, though.
2186 static int hotkey_kthread(void *data)
2188 struct tp_nvram_state s[2];
2189 u32 poll_mask, event_mask;
2190 unsigned int si, so;
2191 unsigned long t;
2192 unsigned int change_detector, must_reset;
2193 unsigned int poll_freq;
2195 mutex_lock(&hotkey_thread_mutex);
2197 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2198 goto exit;
2200 set_freezable();
2202 so = 0;
2203 si = 1;
2204 t = 0;
2206 /* Initial state for compares */
2207 mutex_lock(&hotkey_thread_data_mutex);
2208 change_detector = hotkey_config_change;
2209 poll_mask = hotkey_source_mask;
2210 event_mask = hotkey_source_mask &
2211 (hotkey_driver_mask | hotkey_user_mask);
2212 poll_freq = hotkey_poll_freq;
2213 mutex_unlock(&hotkey_thread_data_mutex);
2214 hotkey_read_nvram(&s[so], poll_mask);
2216 while (!kthread_should_stop()) {
2217 if (t == 0) {
2218 if (likely(poll_freq))
2219 t = 1000/poll_freq;
2220 else
2221 t = 100; /* should never happen... */
2223 t = msleep_interruptible(t);
2224 if (unlikely(kthread_should_stop()))
2225 break;
2226 must_reset = try_to_freeze();
2227 if (t > 0 && !must_reset)
2228 continue;
2230 mutex_lock(&hotkey_thread_data_mutex);
2231 if (must_reset || hotkey_config_change != change_detector) {
2232 /* forget old state on thaw or config change */
2233 si = so;
2234 t = 0;
2235 change_detector = hotkey_config_change;
2237 poll_mask = hotkey_source_mask;
2238 event_mask = hotkey_source_mask &
2239 (hotkey_driver_mask | hotkey_user_mask);
2240 poll_freq = hotkey_poll_freq;
2241 mutex_unlock(&hotkey_thread_data_mutex);
2243 if (likely(poll_mask)) {
2244 hotkey_read_nvram(&s[si], poll_mask);
2245 if (likely(si != so)) {
2246 hotkey_compare_and_issue_event(&s[so], &s[si],
2247 event_mask);
2251 so = si;
2252 si ^= 1;
2255 exit:
2256 mutex_unlock(&hotkey_thread_mutex);
2257 return 0;
2260 /* call with hotkey_mutex held */
2261 static void hotkey_poll_stop_sync(void)
2263 if (tpacpi_hotkey_task) {
2264 if (frozen(tpacpi_hotkey_task) ||
2265 freezing(tpacpi_hotkey_task))
2266 thaw_process(tpacpi_hotkey_task);
2268 kthread_stop(tpacpi_hotkey_task);
2269 tpacpi_hotkey_task = NULL;
2270 mutex_lock(&hotkey_thread_mutex);
2271 /* at this point, the thread did exit */
2272 mutex_unlock(&hotkey_thread_mutex);
2276 /* call with hotkey_mutex held */
2277 static void hotkey_poll_setup(bool may_warn)
2279 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2280 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2282 if (hotkey_poll_freq > 0 &&
2283 (poll_driver_mask ||
2284 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2285 if (!tpacpi_hotkey_task) {
2286 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2287 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2288 if (IS_ERR(tpacpi_hotkey_task)) {
2289 tpacpi_hotkey_task = NULL;
2290 printk(TPACPI_ERR
2291 "could not create kernel thread "
2292 "for hotkey polling\n");
2295 } else {
2296 hotkey_poll_stop_sync();
2297 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2298 hotkey_poll_freq == 0) {
2299 printk(TPACPI_NOTICE
2300 "hot keys 0x%08x and/or events 0x%08x "
2301 "require polling, which is currently "
2302 "disabled\n",
2303 poll_user_mask, poll_driver_mask);
2308 static void hotkey_poll_setup_safe(bool may_warn)
2310 mutex_lock(&hotkey_mutex);
2311 hotkey_poll_setup(may_warn);
2312 mutex_unlock(&hotkey_mutex);
2315 /* call with hotkey_mutex held */
2316 static void hotkey_poll_set_freq(unsigned int freq)
2318 if (!freq)
2319 hotkey_poll_stop_sync();
2321 hotkey_poll_freq = freq;
2324 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2326 static void hotkey_poll_setup_safe(bool __unused)
2330 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2332 static int hotkey_inputdev_open(struct input_dev *dev)
2334 switch (tpacpi_lifecycle) {
2335 case TPACPI_LIFE_INIT:
2337 * hotkey_init will call hotkey_poll_setup_safe
2338 * at the appropriate moment
2340 return 0;
2341 case TPACPI_LIFE_EXITING:
2342 return -EBUSY;
2343 case TPACPI_LIFE_RUNNING:
2344 hotkey_poll_setup_safe(false);
2345 return 0;
2348 /* Should only happen if tpacpi_lifecycle is corrupt */
2349 BUG();
2350 return -EBUSY;
2353 static void hotkey_inputdev_close(struct input_dev *dev)
2355 /* disable hotkey polling when possible */
2356 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2357 !(hotkey_source_mask & hotkey_driver_mask))
2358 hotkey_poll_setup_safe(false);
2361 /* sysfs hotkey enable ------------------------------------------------- */
2362 static ssize_t hotkey_enable_show(struct device *dev,
2363 struct device_attribute *attr,
2364 char *buf)
2366 int res, status;
2368 printk_deprecated_attribute("hotkey_enable",
2369 "Hotkey reporting is always enabled");
2371 res = hotkey_status_get(&status);
2372 if (res)
2373 return res;
2375 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2378 static ssize_t hotkey_enable_store(struct device *dev,
2379 struct device_attribute *attr,
2380 const char *buf, size_t count)
2382 unsigned long t;
2384 printk_deprecated_attribute("hotkey_enable",
2385 "Hotkeys can be disabled through hotkey_mask");
2387 if (parse_strtoul(buf, 1, &t))
2388 return -EINVAL;
2390 if (t == 0)
2391 return -EPERM;
2393 return count;
2396 static struct device_attribute dev_attr_hotkey_enable =
2397 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2398 hotkey_enable_show, hotkey_enable_store);
2400 /* sysfs hotkey mask --------------------------------------------------- */
2401 static ssize_t hotkey_mask_show(struct device *dev,
2402 struct device_attribute *attr,
2403 char *buf)
2405 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2408 static ssize_t hotkey_mask_store(struct device *dev,
2409 struct device_attribute *attr,
2410 const char *buf, size_t count)
2412 unsigned long t;
2413 int res;
2415 if (parse_strtoul(buf, 0xffffffffUL, &t))
2416 return -EINVAL;
2418 if (mutex_lock_killable(&hotkey_mutex))
2419 return -ERESTARTSYS;
2421 res = hotkey_user_mask_set(t);
2423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2424 hotkey_poll_setup(true);
2425 #endif
2427 mutex_unlock(&hotkey_mutex);
2429 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2431 return (res) ? res : count;
2434 static struct device_attribute dev_attr_hotkey_mask =
2435 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2436 hotkey_mask_show, hotkey_mask_store);
2438 /* sysfs hotkey bios_enabled ------------------------------------------- */
2439 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2440 struct device_attribute *attr,
2441 char *buf)
2443 return sprintf(buf, "0\n");
2446 static struct device_attribute dev_attr_hotkey_bios_enabled =
2447 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2449 /* sysfs hotkey bios_mask ---------------------------------------------- */
2450 static ssize_t hotkey_bios_mask_show(struct device *dev,
2451 struct device_attribute *attr,
2452 char *buf)
2454 printk_deprecated_attribute("hotkey_bios_mask",
2455 "This attribute is useless.");
2456 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2459 static struct device_attribute dev_attr_hotkey_bios_mask =
2460 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2462 /* sysfs hotkey all_mask ----------------------------------------------- */
2463 static ssize_t hotkey_all_mask_show(struct device *dev,
2464 struct device_attribute *attr,
2465 char *buf)
2467 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2468 hotkey_all_mask | hotkey_source_mask);
2471 static struct device_attribute dev_attr_hotkey_all_mask =
2472 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2474 /* sysfs hotkey recommended_mask --------------------------------------- */
2475 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2476 struct device_attribute *attr,
2477 char *buf)
2479 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2480 (hotkey_all_mask | hotkey_source_mask)
2481 & ~hotkey_reserved_mask);
2484 static struct device_attribute dev_attr_hotkey_recommended_mask =
2485 __ATTR(hotkey_recommended_mask, S_IRUGO,
2486 hotkey_recommended_mask_show, NULL);
2488 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2490 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2491 static ssize_t hotkey_source_mask_show(struct device *dev,
2492 struct device_attribute *attr,
2493 char *buf)
2495 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2498 static ssize_t hotkey_source_mask_store(struct device *dev,
2499 struct device_attribute *attr,
2500 const char *buf, size_t count)
2502 unsigned long t;
2503 u32 r_ev;
2504 int rc;
2506 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2507 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2508 return -EINVAL;
2510 if (mutex_lock_killable(&hotkey_mutex))
2511 return -ERESTARTSYS;
2513 HOTKEY_CONFIG_CRITICAL_START
2514 hotkey_source_mask = t;
2515 HOTKEY_CONFIG_CRITICAL_END
2517 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2518 ~hotkey_source_mask);
2519 hotkey_poll_setup(true);
2521 /* check if events needed by the driver got disabled */
2522 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2523 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2525 mutex_unlock(&hotkey_mutex);
2527 if (rc < 0)
2528 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2529 "firmware event mask!\n");
2531 if (r_ev)
2532 printk(TPACPI_NOTICE "hotkey_source_mask: "
2533 "some important events were disabled: "
2534 "0x%04x\n", r_ev);
2536 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2538 return (rc < 0) ? rc : count;
2541 static struct device_attribute dev_attr_hotkey_source_mask =
2542 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2543 hotkey_source_mask_show, hotkey_source_mask_store);
2545 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2546 static ssize_t hotkey_poll_freq_show(struct device *dev,
2547 struct device_attribute *attr,
2548 char *buf)
2550 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2553 static ssize_t hotkey_poll_freq_store(struct device *dev,
2554 struct device_attribute *attr,
2555 const char *buf, size_t count)
2557 unsigned long t;
2559 if (parse_strtoul(buf, 25, &t))
2560 return -EINVAL;
2562 if (mutex_lock_killable(&hotkey_mutex))
2563 return -ERESTARTSYS;
2565 hotkey_poll_set_freq(t);
2566 hotkey_poll_setup(true);
2568 mutex_unlock(&hotkey_mutex);
2570 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2572 return count;
2575 static struct device_attribute dev_attr_hotkey_poll_freq =
2576 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2577 hotkey_poll_freq_show, hotkey_poll_freq_store);
2579 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2581 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2582 static ssize_t hotkey_radio_sw_show(struct device *dev,
2583 struct device_attribute *attr,
2584 char *buf)
2586 int res, s;
2587 res = hotkey_get_wlsw(&s);
2588 if (res < 0)
2589 return res;
2591 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2594 static struct device_attribute dev_attr_hotkey_radio_sw =
2595 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2597 static void hotkey_radio_sw_notify_change(void)
2599 if (tp_features.hotkey_wlsw)
2600 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2601 "hotkey_radio_sw");
2604 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2605 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2606 struct device_attribute *attr,
2607 char *buf)
2609 int res, s;
2610 res = hotkey_get_tablet_mode(&s);
2611 if (res < 0)
2612 return res;
2614 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2617 static struct device_attribute dev_attr_hotkey_tablet_mode =
2618 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2620 static void hotkey_tablet_mode_notify_change(void)
2622 if (tp_features.hotkey_tablet)
2623 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2624 "hotkey_tablet_mode");
2627 /* sysfs hotkey report_mode -------------------------------------------- */
2628 static ssize_t hotkey_report_mode_show(struct device *dev,
2629 struct device_attribute *attr,
2630 char *buf)
2632 return snprintf(buf, PAGE_SIZE, "%d\n",
2633 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2636 static struct device_attribute dev_attr_hotkey_report_mode =
2637 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2639 /* sysfs wakeup reason (pollable) -------------------------------------- */
2640 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2641 struct device_attribute *attr,
2642 char *buf)
2644 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2647 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2648 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2650 static void hotkey_wakeup_reason_notify_change(void)
2652 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2653 "wakeup_reason");
2656 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2657 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2658 struct device_attribute *attr,
2659 char *buf)
2661 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2664 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2665 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2666 hotkey_wakeup_hotunplug_complete_show, NULL);
2668 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2670 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2671 "wakeup_hotunplug_complete");
2674 /* --------------------------------------------------------------------- */
2676 static struct attribute *hotkey_attributes[] __initdata = {
2677 &dev_attr_hotkey_enable.attr,
2678 &dev_attr_hotkey_bios_enabled.attr,
2679 &dev_attr_hotkey_bios_mask.attr,
2680 &dev_attr_hotkey_report_mode.attr,
2681 &dev_attr_hotkey_wakeup_reason.attr,
2682 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2683 &dev_attr_hotkey_mask.attr,
2684 &dev_attr_hotkey_all_mask.attr,
2685 &dev_attr_hotkey_recommended_mask.attr,
2686 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2687 &dev_attr_hotkey_source_mask.attr,
2688 &dev_attr_hotkey_poll_freq.attr,
2689 #endif
2692 static void bluetooth_update_rfk(void);
2693 static void wan_update_rfk(void);
2694 static void uwb_update_rfk(void);
2695 static void tpacpi_send_radiosw_update(void)
2697 int wlsw;
2699 /* Sync these BEFORE sending any rfkill events */
2700 if (tp_features.bluetooth)
2701 bluetooth_update_rfk();
2702 if (tp_features.wan)
2703 wan_update_rfk();
2704 if (tp_features.uwb)
2705 uwb_update_rfk();
2707 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2708 mutex_lock(&tpacpi_inputdev_send_mutex);
2710 input_report_switch(tpacpi_inputdev,
2711 SW_RFKILL_ALL, !!wlsw);
2712 input_sync(tpacpi_inputdev);
2714 mutex_unlock(&tpacpi_inputdev_send_mutex);
2716 hotkey_radio_sw_notify_change();
2719 static void hotkey_exit(void)
2721 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2722 mutex_lock(&hotkey_mutex);
2723 hotkey_poll_stop_sync();
2724 mutex_unlock(&hotkey_mutex);
2725 #endif
2727 if (hotkey_dev_attributes)
2728 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2730 kfree(hotkey_keycode_map);
2732 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2733 "restoring original HKEY status and mask\n");
2734 /* yes, there is a bitwise or below, we want the
2735 * functions to be called even if one of them fail */
2736 if (((tp_features.hotkey_mask &&
2737 hotkey_mask_set(hotkey_orig_mask)) |
2738 hotkey_status_set(false)) != 0)
2739 printk(TPACPI_ERR
2740 "failed to restore hot key mask "
2741 "to BIOS defaults\n");
2744 static void __init hotkey_unmap(const unsigned int scancode)
2746 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2747 clear_bit(hotkey_keycode_map[scancode],
2748 tpacpi_inputdev->keybit);
2749 hotkey_keycode_map[scancode] = KEY_RESERVED;
2754 * HKEY quirks:
2755 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2758 #define TPACPI_HK_Q_INIMASK 0x0001
2760 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2761 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2762 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2763 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2764 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2765 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2766 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2767 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2768 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2769 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2770 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2771 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2772 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2773 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2774 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2775 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2776 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2777 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2778 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2779 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2782 static int __init hotkey_init(struct ibm_init_struct *iibm)
2784 /* Requirements for changing the default keymaps:
2786 * 1. Many of the keys are mapped to KEY_RESERVED for very
2787 * good reasons. Do not change them unless you have deep
2788 * knowledge on the IBM and Lenovo ThinkPad firmware for
2789 * the various ThinkPad models. The driver behaves
2790 * differently for KEY_RESERVED: such keys have their
2791 * hot key mask *unset* in mask_recommended, and also
2792 * in the initial hot key mask programmed into the
2793 * firmware at driver load time, which means the firm-
2794 * ware may react very differently if you change them to
2795 * something else;
2797 * 2. You must be subscribed to the linux-thinkpad and
2798 * ibm-acpi-devel mailing lists, and you should read the
2799 * list archives since 2007 if you want to change the
2800 * keymaps. This requirement exists so that you will
2801 * know the past history of problems with the thinkpad-
2802 * acpi driver keymaps, and also that you will be
2803 * listening to any bug reports;
2805 * 3. Do not send thinkpad-acpi specific patches directly to
2806 * for merging, *ever*. Send them to the linux-acpi
2807 * mailinglist for comments. Merging is to be done only
2808 * through acpi-test and the ACPI maintainer.
2810 * If the above is too much to ask, don't change the keymap.
2811 * Ask the thinkpad-acpi maintainer to do it, instead.
2813 static u16 ibm_keycode_map[] __initdata = {
2814 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2815 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2816 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2817 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2819 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2820 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2821 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2822 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2824 /* brightness: firmware always reacts to them */
2825 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2826 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2828 /* Thinklight: firmware always react to it */
2829 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2831 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2832 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2834 /* Volume: firmware always react to it and reprograms
2835 * the built-in *extra* mixer. Never map it to control
2836 * another mixer by default. */
2837 KEY_RESERVED, /* 0x14: VOLUME UP */
2838 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2839 KEY_RESERVED, /* 0x16: MUTE */
2841 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2843 /* (assignments unknown, please report if found) */
2844 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2845 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2847 static u16 lenovo_keycode_map[] __initdata = {
2848 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2849 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2850 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2851 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2853 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2854 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2855 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2856 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2858 /* These should be enabled --only-- when ACPI video
2859 * is disabled (i.e. in "vendor" mode), and are handled
2860 * in a special way by the init code */
2861 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2862 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2864 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2866 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2867 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2869 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2870 * react to it and reprograms the built-in *extra* mixer.
2871 * Never map it to control another mixer by default.
2873 * T60?, T61, R60?, R61: firmware and EC tries to send
2874 * these over the regular keyboard, so these are no-ops,
2875 * but there are still weird bugs re. MUTE, so do not
2876 * change unless you get test reports from all Lenovo
2877 * models. May cause the BIOS to interfere with the
2878 * HDA mixer.
2880 KEY_RESERVED, /* 0x14: VOLUME UP */
2881 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2882 KEY_RESERVED, /* 0x16: MUTE */
2884 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2886 /* (assignments unknown, please report if found) */
2887 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2888 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2891 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2892 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2893 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2895 int res, i;
2896 int status;
2897 int hkeyv;
2898 bool radiosw_state = false;
2899 bool tabletsw_state = false;
2901 unsigned long quirks;
2903 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2904 "initializing hotkey subdriver\n");
2906 BUG_ON(!tpacpi_inputdev);
2907 BUG_ON(tpacpi_inputdev->open != NULL ||
2908 tpacpi_inputdev->close != NULL);
2910 TPACPI_ACPIHANDLE_INIT(hkey);
2911 mutex_init(&hotkey_mutex);
2913 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2914 mutex_init(&hotkey_thread_mutex);
2915 mutex_init(&hotkey_thread_data_mutex);
2916 #endif
2918 /* hotkey not supported on 570 */
2919 tp_features.hotkey = hkey_handle != NULL;
2921 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2922 "hotkeys are %s\n",
2923 str_supported(tp_features.hotkey));
2925 if (!tp_features.hotkey)
2926 return 1;
2928 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2929 ARRAY_SIZE(tpacpi_hotkey_qtable));
2931 tpacpi_disable_brightness_delay();
2933 /* MUST have enough space for all attributes to be added to
2934 * hotkey_dev_attributes */
2935 hotkey_dev_attributes = create_attr_set(
2936 ARRAY_SIZE(hotkey_attributes) + 2,
2937 NULL);
2938 if (!hotkey_dev_attributes)
2939 return -ENOMEM;
2940 res = add_many_to_attr_set(hotkey_dev_attributes,
2941 hotkey_attributes,
2942 ARRAY_SIZE(hotkey_attributes));
2943 if (res)
2944 goto err_exit;
2946 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2947 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2948 for HKEY interface version 0x100 */
2949 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2950 if ((hkeyv >> 8) != 1) {
2951 printk(TPACPI_ERR "unknown version of the "
2952 "HKEY interface: 0x%x\n", hkeyv);
2953 printk(TPACPI_ERR "please report this to %s\n",
2954 TPACPI_MAIL);
2955 } else {
2957 * MHKV 0x100 in A31, R40, R40e,
2958 * T4x, X31, and later
2960 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2961 "firmware HKEY interface version: 0x%x\n",
2962 hkeyv);
2964 /* Paranoia check AND init hotkey_all_mask */
2965 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2966 "MHKA", "qd")) {
2967 printk(TPACPI_ERR
2968 "missing MHKA handler, "
2969 "please report this to %s\n",
2970 TPACPI_MAIL);
2971 /* Fallback: pre-init for FN+F3,F4,F12 */
2972 hotkey_all_mask = 0x080cU;
2973 } else {
2974 tp_features.hotkey_mask = 1;
2979 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2980 "hotkey masks are %s\n",
2981 str_supported(tp_features.hotkey_mask));
2983 /* Init hotkey_all_mask if not initialized yet */
2984 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2985 (quirks & TPACPI_HK_Q_INIMASK))
2986 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2988 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2989 if (tp_features.hotkey_mask) {
2990 /* hotkey_source_mask *must* be zero for
2991 * the first hotkey_mask_get to return hotkey_orig_mask */
2992 res = hotkey_mask_get();
2993 if (res)
2994 goto err_exit;
2996 hotkey_orig_mask = hotkey_acpi_mask;
2997 } else {
2998 hotkey_orig_mask = hotkey_all_mask;
2999 hotkey_acpi_mask = hotkey_all_mask;
3002 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3003 if (dbg_wlswemul) {
3004 tp_features.hotkey_wlsw = 1;
3005 radiosw_state = !!tpacpi_wlsw_emulstate;
3006 printk(TPACPI_INFO
3007 "radio switch emulation enabled\n");
3008 } else
3009 #endif
3010 /* Not all thinkpads have a hardware radio switch */
3011 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3012 tp_features.hotkey_wlsw = 1;
3013 radiosw_state = !!status;
3014 printk(TPACPI_INFO
3015 "radio switch found; radios are %s\n",
3016 enabled(status, 0));
3018 if (tp_features.hotkey_wlsw)
3019 res = add_to_attr_set(hotkey_dev_attributes,
3020 &dev_attr_hotkey_radio_sw.attr);
3022 /* For X41t, X60t, X61t Tablets... */
3023 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3024 tp_features.hotkey_tablet = 1;
3025 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3026 printk(TPACPI_INFO
3027 "possible tablet mode switch found; "
3028 "ThinkPad in %s mode\n",
3029 (tabletsw_state) ? "tablet" : "laptop");
3030 res = add_to_attr_set(hotkey_dev_attributes,
3031 &dev_attr_hotkey_tablet_mode.attr);
3034 if (!res)
3035 res = register_attr_set_with_sysfs(
3036 hotkey_dev_attributes,
3037 &tpacpi_pdev->dev.kobj);
3038 if (res)
3039 goto err_exit;
3041 /* Set up key map */
3043 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3044 GFP_KERNEL);
3045 if (!hotkey_keycode_map) {
3046 printk(TPACPI_ERR
3047 "failed to allocate memory for key map\n");
3048 res = -ENOMEM;
3049 goto err_exit;
3052 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3053 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3054 "using Lenovo default hot key map\n");
3055 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3056 TPACPI_HOTKEY_MAP_SIZE);
3057 } else {
3058 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3059 "using IBM default hot key map\n");
3060 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3061 TPACPI_HOTKEY_MAP_SIZE);
3064 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3065 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3066 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3067 tpacpi_inputdev->keycode = hotkey_keycode_map;
3068 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3069 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3070 input_set_capability(tpacpi_inputdev, EV_KEY,
3071 hotkey_keycode_map[i]);
3072 } else {
3073 if (i < sizeof(hotkey_reserved_mask)*8)
3074 hotkey_reserved_mask |= 1 << i;
3078 if (tp_features.hotkey_wlsw) {
3079 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3080 input_report_switch(tpacpi_inputdev,
3081 SW_RFKILL_ALL, radiosw_state);
3083 if (tp_features.hotkey_tablet) {
3084 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3085 input_report_switch(tpacpi_inputdev,
3086 SW_TABLET_MODE, tabletsw_state);
3089 /* Do not issue duplicate brightness change events to
3090 * userspace */
3091 if (!tp_features.bright_acpimode)
3092 /* update bright_acpimode... */
3093 tpacpi_check_std_acpi_brightness_support();
3095 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3096 printk(TPACPI_INFO
3097 "This ThinkPad has standard ACPI backlight "
3098 "brightness control, supported by the ACPI "
3099 "video driver\n");
3100 printk(TPACPI_NOTICE
3101 "Disabling thinkpad-acpi brightness events "
3102 "by default...\n");
3104 /* Disable brightness up/down on Lenovo thinkpads when
3105 * ACPI is handling them, otherwise it is plain impossible
3106 * for userspace to do something even remotely sane */
3107 hotkey_reserved_mask |=
3108 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3109 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3110 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3111 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3114 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3115 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3116 & ~hotkey_all_mask
3117 & ~hotkey_reserved_mask;
3119 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3120 "hotkey source mask 0x%08x, polling freq %u\n",
3121 hotkey_source_mask, hotkey_poll_freq);
3122 #endif
3124 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3125 "enabling firmware HKEY event interface...\n");
3126 res = hotkey_status_set(true);
3127 if (res) {
3128 hotkey_exit();
3129 return res;
3131 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3132 | hotkey_driver_mask)
3133 & ~hotkey_source_mask);
3134 if (res < 0 && res != -ENXIO) {
3135 hotkey_exit();
3136 return res;
3138 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3139 & ~hotkey_reserved_mask;
3140 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3141 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3142 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3144 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3145 "legacy ibm/hotkey event reporting over procfs %s\n",
3146 (hotkey_report_mode < 2) ?
3147 "enabled" : "disabled");
3149 tpacpi_inputdev->open = &hotkey_inputdev_open;
3150 tpacpi_inputdev->close = &hotkey_inputdev_close;
3152 hotkey_poll_setup_safe(true);
3154 return 0;
3156 err_exit:
3157 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3158 hotkey_dev_attributes = NULL;
3160 return (res < 0)? res : 1;
3163 static bool hotkey_notify_hotkey(const u32 hkey,
3164 bool *send_acpi_ev,
3165 bool *ignore_acpi_ev)
3167 /* 0x1000-0x1FFF: key presses */
3168 unsigned int scancode = hkey & 0xfff;
3169 *send_acpi_ev = true;
3170 *ignore_acpi_ev = false;
3172 if (scancode > 0 && scancode < 0x21) {
3173 scancode--;
3174 if (!(hotkey_source_mask & (1 << scancode))) {
3175 tpacpi_input_send_key_masked(scancode);
3176 *send_acpi_ev = false;
3177 } else {
3178 *ignore_acpi_ev = true;
3180 return true;
3182 return false;
3185 static bool hotkey_notify_wakeup(const u32 hkey,
3186 bool *send_acpi_ev,
3187 bool *ignore_acpi_ev)
3189 /* 0x2000-0x2FFF: Wakeup reason */
3190 *send_acpi_ev = true;
3191 *ignore_acpi_ev = false;
3193 switch (hkey) {
3194 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3195 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3196 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3197 *ignore_acpi_ev = true;
3198 break;
3200 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3201 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3202 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3203 *ignore_acpi_ev = true;
3204 break;
3206 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3207 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3208 printk(TPACPI_ALERT
3209 "EMERGENCY WAKEUP: battery almost empty\n");
3210 /* how to auto-heal: */
3211 /* 2313: woke up from S3, go to S4/S5 */
3212 /* 2413: woke up from S4, go to S5 */
3213 break;
3215 default:
3216 return false;
3219 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3220 printk(TPACPI_INFO
3221 "woke up due to a hot-unplug "
3222 "request...\n");
3223 hotkey_wakeup_reason_notify_change();
3225 return true;
3228 static bool hotkey_notify_usrevent(const u32 hkey,
3229 bool *send_acpi_ev,
3230 bool *ignore_acpi_ev)
3232 /* 0x5000-0x5FFF: human interface helpers */
3233 *send_acpi_ev = true;
3234 *ignore_acpi_ev = false;
3236 switch (hkey) {
3237 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3238 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3239 return true;
3241 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3242 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3243 tpacpi_input_send_tabletsw();
3244 hotkey_tablet_mode_notify_change();
3245 *send_acpi_ev = false;
3246 return true;
3248 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3249 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3250 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3251 /* do not propagate these events */
3252 *ignore_acpi_ev = true;
3253 return true;
3255 default:
3256 return false;
3260 static void thermal_dump_all_sensors(void);
3262 static bool hotkey_notify_thermal(const u32 hkey,
3263 bool *send_acpi_ev,
3264 bool *ignore_acpi_ev)
3266 bool known = true;
3268 /* 0x6000-0x6FFF: thermal alarms */
3269 *send_acpi_ev = true;
3270 *ignore_acpi_ev = false;
3272 switch (hkey) {
3273 case TP_HKEY_EV_THM_TABLE_CHANGED:
3274 printk(TPACPI_INFO
3275 "EC reports that Thermal Table has changed\n");
3276 /* recommended action: do nothing, we don't have
3277 * Lenovo ATM information */
3278 return true;
3279 case TP_HKEY_EV_ALARM_BAT_HOT:
3280 printk(TPACPI_CRIT
3281 "THERMAL ALARM: battery is too hot!\n");
3282 /* recommended action: warn user through gui */
3283 break;
3284 case TP_HKEY_EV_ALARM_BAT_XHOT:
3285 printk(TPACPI_ALERT
3286 "THERMAL EMERGENCY: battery is extremely hot!\n");
3287 /* recommended action: immediate sleep/hibernate */
3288 break;
3289 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3290 printk(TPACPI_CRIT
3291 "THERMAL ALARM: "
3292 "a sensor reports something is too hot!\n");
3293 /* recommended action: warn user through gui, that */
3294 /* some internal component is too hot */
3295 break;
3296 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3297 printk(TPACPI_ALERT
3298 "THERMAL EMERGENCY: "
3299 "a sensor reports something is extremely hot!\n");
3300 /* recommended action: immediate sleep/hibernate */
3301 break;
3302 default:
3303 printk(TPACPI_ALERT
3304 "THERMAL ALERT: unknown thermal alarm received\n");
3305 known = false;
3308 thermal_dump_all_sensors();
3310 return known;
3313 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3315 u32 hkey;
3316 bool send_acpi_ev;
3317 bool ignore_acpi_ev;
3318 bool known_ev;
3320 if (event != 0x80) {
3321 printk(TPACPI_ERR
3322 "unknown HKEY notification event %d\n", event);
3323 /* forward it to userspace, maybe it knows how to handle it */
3324 acpi_bus_generate_netlink_event(
3325 ibm->acpi->device->pnp.device_class,
3326 dev_name(&ibm->acpi->device->dev),
3327 event, 0);
3328 return;
3331 while (1) {
3332 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3333 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3334 return;
3337 if (hkey == 0) {
3338 /* queue empty */
3339 return;
3342 send_acpi_ev = true;
3343 ignore_acpi_ev = false;
3345 switch (hkey >> 12) {
3346 case 1:
3347 /* 0x1000-0x1FFF: key presses */
3348 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3349 &ignore_acpi_ev);
3350 break;
3351 case 2:
3352 /* 0x2000-0x2FFF: Wakeup reason */
3353 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3354 &ignore_acpi_ev);
3355 break;
3356 case 3:
3357 /* 0x3000-0x3FFF: bay-related wakeups */
3358 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3359 hotkey_autosleep_ack = 1;
3360 printk(TPACPI_INFO
3361 "bay ejected\n");
3362 hotkey_wakeup_hotunplug_complete_notify_change();
3363 known_ev = true;
3364 } else {
3365 known_ev = false;
3367 break;
3368 case 4:
3369 /* 0x4000-0x4FFF: dock-related wakeups */
3370 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3371 hotkey_autosleep_ack = 1;
3372 printk(TPACPI_INFO
3373 "undocked\n");
3374 hotkey_wakeup_hotunplug_complete_notify_change();
3375 known_ev = true;
3376 } else {
3377 known_ev = false;
3379 break;
3380 case 5:
3381 /* 0x5000-0x5FFF: human interface helpers */
3382 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3383 &ignore_acpi_ev);
3384 break;
3385 case 6:
3386 /* 0x6000-0x6FFF: thermal alarms */
3387 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3388 &ignore_acpi_ev);
3389 break;
3390 case 7:
3391 /* 0x7000-0x7FFF: misc */
3392 if (tp_features.hotkey_wlsw &&
3393 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3394 tpacpi_send_radiosw_update();
3395 send_acpi_ev = 0;
3396 known_ev = true;
3397 break;
3399 /* fallthrough to default */
3400 default:
3401 known_ev = false;
3403 if (!known_ev) {
3404 printk(TPACPI_NOTICE
3405 "unhandled HKEY event 0x%04x\n", hkey);
3406 printk(TPACPI_NOTICE
3407 "please report the conditions when this "
3408 "event happened to %s\n", TPACPI_MAIL);
3411 /* Legacy events */
3412 if (!ignore_acpi_ev &&
3413 (send_acpi_ev || hotkey_report_mode < 2)) {
3414 acpi_bus_generate_proc_event(ibm->acpi->device,
3415 event, hkey);
3418 /* netlink events */
3419 if (!ignore_acpi_ev && send_acpi_ev) {
3420 acpi_bus_generate_netlink_event(
3421 ibm->acpi->device->pnp.device_class,
3422 dev_name(&ibm->acpi->device->dev),
3423 event, hkey);
3428 static void hotkey_suspend(pm_message_t state)
3430 /* Do these on suspend, we get the events on early resume! */
3431 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3432 hotkey_autosleep_ack = 0;
3435 static void hotkey_resume(void)
3437 tpacpi_disable_brightness_delay();
3439 if (hotkey_status_set(true) < 0 ||
3440 hotkey_mask_set(hotkey_acpi_mask) < 0)
3441 printk(TPACPI_ERR
3442 "error while attempting to reset the event "
3443 "firmware interface\n");
3445 tpacpi_send_radiosw_update();
3446 hotkey_tablet_mode_notify_change();
3447 hotkey_wakeup_reason_notify_change();
3448 hotkey_wakeup_hotunplug_complete_notify_change();
3449 hotkey_poll_setup_safe(false);
3452 /* procfs -------------------------------------------------------------- */
3453 static int hotkey_read(char *p)
3455 int res, status;
3456 int len = 0;
3458 if (!tp_features.hotkey) {
3459 len += sprintf(p + len, "status:\t\tnot supported\n");
3460 return len;
3463 if (mutex_lock_killable(&hotkey_mutex))
3464 return -ERESTARTSYS;
3465 res = hotkey_status_get(&status);
3466 if (!res)
3467 res = hotkey_mask_get();
3468 mutex_unlock(&hotkey_mutex);
3469 if (res)
3470 return res;
3472 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3473 if (hotkey_all_mask) {
3474 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3475 len += sprintf(p + len,
3476 "commands:\tenable, disable, reset, <mask>\n");
3477 } else {
3478 len += sprintf(p + len, "mask:\t\tnot supported\n");
3479 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3482 return len;
3485 static void hotkey_enabledisable_warn(bool enable)
3487 tpacpi_log_usertask("procfs hotkey enable/disable");
3488 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3489 TPACPI_WARN
3490 "hotkey enable/disable functionality has been "
3491 "removed from the driver. Hotkeys are always "
3492 "enabled\n"))
3493 printk(TPACPI_ERR
3494 "Please remove the hotkey=enable module "
3495 "parameter, it is deprecated. Hotkeys are always "
3496 "enabled\n");
3499 static int hotkey_write(char *buf)
3501 int res;
3502 u32 mask;
3503 char *cmd;
3505 if (!tp_features.hotkey)
3506 return -ENODEV;
3508 if (mutex_lock_killable(&hotkey_mutex))
3509 return -ERESTARTSYS;
3511 mask = hotkey_user_mask;
3513 res = 0;
3514 while ((cmd = next_cmd(&buf))) {
3515 if (strlencmp(cmd, "enable") == 0) {
3516 hotkey_enabledisable_warn(1);
3517 } else if (strlencmp(cmd, "disable") == 0) {
3518 hotkey_enabledisable_warn(0);
3519 res = -EPERM;
3520 } else if (strlencmp(cmd, "reset") == 0) {
3521 mask = (hotkey_all_mask | hotkey_source_mask)
3522 & ~hotkey_reserved_mask;
3523 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3524 /* mask set */
3525 } else if (sscanf(cmd, "%x", &mask) == 1) {
3526 /* mask set */
3527 } else {
3528 res = -EINVAL;
3529 goto errexit;
3533 if (!res) {
3534 tpacpi_disclose_usertask("procfs hotkey",
3535 "set mask to 0x%08x\n", mask);
3536 res = hotkey_user_mask_set(mask);
3539 errexit:
3540 mutex_unlock(&hotkey_mutex);
3541 return res;
3544 static const struct acpi_device_id ibm_htk_device_ids[] = {
3545 {TPACPI_ACPI_HKEY_HID, 0},
3546 {"", 0},
3549 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3550 .hid = ibm_htk_device_ids,
3551 .notify = hotkey_notify,
3552 .handle = &hkey_handle,
3553 .type = ACPI_DEVICE_NOTIFY,
3556 static struct ibm_struct hotkey_driver_data = {
3557 .name = "hotkey",
3558 .read = hotkey_read,
3559 .write = hotkey_write,
3560 .exit = hotkey_exit,
3561 .resume = hotkey_resume,
3562 .suspend = hotkey_suspend,
3563 .acpi = &ibm_hotkey_acpidriver,
3566 /*************************************************************************
3567 * Bluetooth subdriver
3570 enum {
3571 /* ACPI GBDC/SBDC bits */
3572 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3573 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3574 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3575 off / last state */
3578 enum {
3579 /* ACPI \BLTH commands */
3580 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3581 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3582 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3583 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3584 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3587 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3589 static struct rfkill *tpacpi_bluetooth_rfkill;
3591 static void bluetooth_suspend(pm_message_t state)
3593 /* Try to make sure radio will resume powered off */
3594 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3595 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3596 vdbg_printk(TPACPI_DBG_RFKILL,
3597 "bluetooth power down on resume request failed\n");
3600 static int bluetooth_get_radiosw(void)
3602 int status;
3604 if (!tp_features.bluetooth)
3605 return -ENODEV;
3607 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3608 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3609 return RFKILL_STATE_HARD_BLOCKED;
3611 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3612 if (dbg_bluetoothemul)
3613 return (tpacpi_bluetooth_emulstate) ?
3614 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3615 #endif
3617 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3618 return -EIO;
3620 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3621 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3624 static void bluetooth_update_rfk(void)
3626 int status;
3628 if (!tpacpi_bluetooth_rfkill)
3629 return;
3631 status = bluetooth_get_radiosw();
3632 if (status < 0)
3633 return;
3634 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3636 vdbg_printk(TPACPI_DBG_RFKILL,
3637 "forced rfkill state to %d\n",
3638 status);
3641 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3643 int status;
3645 if (!tp_features.bluetooth)
3646 return -ENODEV;
3648 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3649 * reason to risk weird behaviour. */
3650 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3651 && radio_on)
3652 return -EPERM;
3654 vdbg_printk(TPACPI_DBG_RFKILL,
3655 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3657 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3658 if (dbg_bluetoothemul) {
3659 tpacpi_bluetooth_emulstate = !!radio_on;
3660 if (update_rfk)
3661 bluetooth_update_rfk();
3662 return 0;
3664 #endif
3666 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3667 if (radio_on)
3668 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3669 else
3670 status = 0;
3671 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3672 return -EIO;
3674 if (update_rfk)
3675 bluetooth_update_rfk();
3677 return 0;
3680 /* sysfs bluetooth enable ---------------------------------------------- */
3681 static ssize_t bluetooth_enable_show(struct device *dev,
3682 struct device_attribute *attr,
3683 char *buf)
3685 int status;
3687 printk_deprecated_rfkill_attribute("bluetooth_enable");
3689 status = bluetooth_get_radiosw();
3690 if (status < 0)
3691 return status;
3693 return snprintf(buf, PAGE_SIZE, "%d\n",
3694 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3697 static ssize_t bluetooth_enable_store(struct device *dev,
3698 struct device_attribute *attr,
3699 const char *buf, size_t count)
3701 unsigned long t;
3702 int res;
3704 printk_deprecated_rfkill_attribute("bluetooth_enable");
3706 if (parse_strtoul(buf, 1, &t))
3707 return -EINVAL;
3709 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3711 res = bluetooth_set_radiosw(t, 1);
3713 return (res) ? res : count;
3716 static struct device_attribute dev_attr_bluetooth_enable =
3717 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3718 bluetooth_enable_show, bluetooth_enable_store);
3720 /* --------------------------------------------------------------------- */
3722 static struct attribute *bluetooth_attributes[] = {
3723 &dev_attr_bluetooth_enable.attr,
3724 NULL
3727 static const struct attribute_group bluetooth_attr_group = {
3728 .attrs = bluetooth_attributes,
3731 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3733 int bts = bluetooth_get_radiosw();
3735 if (bts < 0)
3736 return bts;
3738 *state = bts;
3739 return 0;
3742 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3744 dbg_printk(TPACPI_DBG_RFKILL,
3745 "request to change radio state to %d\n", state);
3746 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3749 static void bluetooth_shutdown(void)
3751 /* Order firmware to save current state to NVRAM */
3752 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3753 TP_ACPI_BLTH_SAVE_STATE))
3754 printk(TPACPI_NOTICE
3755 "failed to save bluetooth state to NVRAM\n");
3756 else
3757 vdbg_printk(TPACPI_DBG_RFKILL,
3758 "bluestooth state saved to NVRAM\n");
3761 static void bluetooth_exit(void)
3763 bluetooth_shutdown();
3765 if (tpacpi_bluetooth_rfkill)
3766 rfkill_unregister(tpacpi_bluetooth_rfkill);
3768 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3769 &bluetooth_attr_group);
3772 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3774 int res;
3775 int status = 0;
3777 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3778 "initializing bluetooth subdriver\n");
3780 TPACPI_ACPIHANDLE_INIT(hkey);
3782 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3783 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3784 tp_features.bluetooth = hkey_handle &&
3785 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3787 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3788 "bluetooth is %s, status 0x%02x\n",
3789 str_supported(tp_features.bluetooth),
3790 status);
3792 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3793 if (dbg_bluetoothemul) {
3794 tp_features.bluetooth = 1;
3795 printk(TPACPI_INFO
3796 "bluetooth switch emulation enabled\n");
3797 } else
3798 #endif
3799 if (tp_features.bluetooth &&
3800 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3801 /* no bluetooth hardware present in system */
3802 tp_features.bluetooth = 0;
3803 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3804 "bluetooth hardware not installed\n");
3807 if (!tp_features.bluetooth)
3808 return 1;
3810 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3811 &bluetooth_attr_group);
3812 if (res)
3813 return res;
3815 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3816 &tpacpi_bluetooth_rfkill,
3817 RFKILL_TYPE_BLUETOOTH,
3818 TPACPI_RFK_BLUETOOTH_SW_NAME,
3819 true,
3820 tpacpi_bluetooth_rfk_set,
3821 tpacpi_bluetooth_rfk_get);
3822 if (res) {
3823 bluetooth_exit();
3824 return res;
3827 return 0;
3830 /* procfs -------------------------------------------------------------- */
3831 static int bluetooth_read(char *p)
3833 int len = 0;
3834 int status = bluetooth_get_radiosw();
3836 if (!tp_features.bluetooth)
3837 len += sprintf(p + len, "status:\t\tnot supported\n");
3838 else {
3839 len += sprintf(p + len, "status:\t\t%s\n",
3840 (status == RFKILL_STATE_UNBLOCKED) ?
3841 "enabled" : "disabled");
3842 len += sprintf(p + len, "commands:\tenable, disable\n");
3845 return len;
3848 static int bluetooth_write(char *buf)
3850 char *cmd;
3851 int state = -1;
3853 if (!tp_features.bluetooth)
3854 return -ENODEV;
3856 while ((cmd = next_cmd(&buf))) {
3857 if (strlencmp(cmd, "enable") == 0) {
3858 state = 1;
3859 } else if (strlencmp(cmd, "disable") == 0) {
3860 state = 0;
3861 } else
3862 return -EINVAL;
3865 if (state != -1) {
3866 tpacpi_disclose_usertask("procfs bluetooth",
3867 "attempt to %s\n",
3868 state ? "enable" : "disable");
3869 bluetooth_set_radiosw(state, 1);
3872 return 0;
3875 static struct ibm_struct bluetooth_driver_data = {
3876 .name = "bluetooth",
3877 .read = bluetooth_read,
3878 .write = bluetooth_write,
3879 .exit = bluetooth_exit,
3880 .suspend = bluetooth_suspend,
3881 .shutdown = bluetooth_shutdown,
3884 /*************************************************************************
3885 * Wan subdriver
3888 enum {
3889 /* ACPI GWAN/SWAN bits */
3890 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3891 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3892 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3893 off / last state */
3896 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3898 static struct rfkill *tpacpi_wan_rfkill;
3900 static void wan_suspend(pm_message_t state)
3902 /* Try to make sure radio will resume powered off */
3903 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3904 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3905 vdbg_printk(TPACPI_DBG_RFKILL,
3906 "WWAN power down on resume request failed\n");
3909 static int wan_get_radiosw(void)
3911 int status;
3913 if (!tp_features.wan)
3914 return -ENODEV;
3916 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3917 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3918 return RFKILL_STATE_HARD_BLOCKED;
3920 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3921 if (dbg_wwanemul)
3922 return (tpacpi_wwan_emulstate) ?
3923 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3924 #endif
3926 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3927 return -EIO;
3929 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3930 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3933 static void wan_update_rfk(void)
3935 int status;
3937 if (!tpacpi_wan_rfkill)
3938 return;
3940 status = wan_get_radiosw();
3941 if (status < 0)
3942 return;
3943 rfkill_force_state(tpacpi_wan_rfkill, status);
3945 vdbg_printk(TPACPI_DBG_RFKILL,
3946 "forced rfkill state to %d\n",
3947 status);
3950 static int wan_set_radiosw(int radio_on, int update_rfk)
3952 int status;
3954 if (!tp_features.wan)
3955 return -ENODEV;
3957 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3958 * reason to risk weird behaviour. */
3959 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3960 && radio_on)
3961 return -EPERM;
3963 vdbg_printk(TPACPI_DBG_RFKILL,
3964 "will %s WWAN\n", radio_on ? "enable" : "disable");
3966 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3967 if (dbg_wwanemul) {
3968 tpacpi_wwan_emulstate = !!radio_on;
3969 if (update_rfk)
3970 wan_update_rfk();
3971 return 0;
3973 #endif
3975 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3976 if (radio_on)
3977 status = TP_ACPI_WANCARD_RADIOSSW;
3978 else
3979 status = 0;
3980 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3981 return -EIO;
3983 if (update_rfk)
3984 wan_update_rfk();
3986 return 0;
3989 /* sysfs wan enable ---------------------------------------------------- */
3990 static ssize_t wan_enable_show(struct device *dev,
3991 struct device_attribute *attr,
3992 char *buf)
3994 int status;
3996 printk_deprecated_rfkill_attribute("wwan_enable");
3998 status = wan_get_radiosw();
3999 if (status < 0)
4000 return status;
4002 return snprintf(buf, PAGE_SIZE, "%d\n",
4003 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
4006 static ssize_t wan_enable_store(struct device *dev,
4007 struct device_attribute *attr,
4008 const char *buf, size_t count)
4010 unsigned long t;
4011 int res;
4013 printk_deprecated_rfkill_attribute("wwan_enable");
4015 if (parse_strtoul(buf, 1, &t))
4016 return -EINVAL;
4018 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
4020 res = wan_set_radiosw(t, 1);
4022 return (res) ? res : count;
4025 static struct device_attribute dev_attr_wan_enable =
4026 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4027 wan_enable_show, wan_enable_store);
4029 /* --------------------------------------------------------------------- */
4031 static struct attribute *wan_attributes[] = {
4032 &dev_attr_wan_enable.attr,
4033 NULL
4036 static const struct attribute_group wan_attr_group = {
4037 .attrs = wan_attributes,
4040 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
4042 int wans = wan_get_radiosw();
4044 if (wans < 0)
4045 return wans;
4047 *state = wans;
4048 return 0;
4051 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
4053 dbg_printk(TPACPI_DBG_RFKILL,
4054 "request to change radio state to %d\n", state);
4055 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4058 static void wan_shutdown(void)
4060 /* Order firmware to save current state to NVRAM */
4061 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4062 TP_ACPI_WGSV_SAVE_STATE))
4063 printk(TPACPI_NOTICE
4064 "failed to save WWAN state to NVRAM\n");
4065 else
4066 vdbg_printk(TPACPI_DBG_RFKILL,
4067 "WWAN state saved to NVRAM\n");
4070 static void wan_exit(void)
4072 wan_shutdown();
4074 if (tpacpi_wan_rfkill)
4075 rfkill_unregister(tpacpi_wan_rfkill);
4077 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4078 &wan_attr_group);
4081 static int __init wan_init(struct ibm_init_struct *iibm)
4083 int res;
4084 int status = 0;
4086 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4087 "initializing wan subdriver\n");
4089 TPACPI_ACPIHANDLE_INIT(hkey);
4091 tp_features.wan = hkey_handle &&
4092 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4094 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4095 "wan is %s, status 0x%02x\n",
4096 str_supported(tp_features.wan),
4097 status);
4099 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4100 if (dbg_wwanemul) {
4101 tp_features.wan = 1;
4102 printk(TPACPI_INFO
4103 "wwan switch emulation enabled\n");
4104 } else
4105 #endif
4106 if (tp_features.wan &&
4107 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4108 /* no wan hardware present in system */
4109 tp_features.wan = 0;
4110 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4111 "wan hardware not installed\n");
4114 if (!tp_features.wan)
4115 return 1;
4117 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4118 &wan_attr_group);
4119 if (res)
4120 return res;
4122 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4123 &tpacpi_wan_rfkill,
4124 RFKILL_TYPE_WWAN,
4125 TPACPI_RFK_WWAN_SW_NAME,
4126 true,
4127 tpacpi_wan_rfk_set,
4128 tpacpi_wan_rfk_get);
4129 if (res) {
4130 wan_exit();
4131 return res;
4134 return 0;
4137 /* procfs -------------------------------------------------------------- */
4138 static int wan_read(char *p)
4140 int len = 0;
4141 int status = wan_get_radiosw();
4143 tpacpi_disclose_usertask("procfs wan", "read");
4145 if (!tp_features.wan)
4146 len += sprintf(p + len, "status:\t\tnot supported\n");
4147 else {
4148 len += sprintf(p + len, "status:\t\t%s\n",
4149 (status == RFKILL_STATE_UNBLOCKED) ?
4150 "enabled" : "disabled");
4151 len += sprintf(p + len, "commands:\tenable, disable\n");
4154 return len;
4157 static int wan_write(char *buf)
4159 char *cmd;
4160 int state = -1;
4162 if (!tp_features.wan)
4163 return -ENODEV;
4165 while ((cmd = next_cmd(&buf))) {
4166 if (strlencmp(cmd, "enable") == 0) {
4167 state = 1;
4168 } else if (strlencmp(cmd, "disable") == 0) {
4169 state = 0;
4170 } else
4171 return -EINVAL;
4174 if (state != -1) {
4175 tpacpi_disclose_usertask("procfs wan",
4176 "attempt to %s\n",
4177 state ? "enable" : "disable");
4178 wan_set_radiosw(state, 1);
4181 return 0;
4184 static struct ibm_struct wan_driver_data = {
4185 .name = "wan",
4186 .read = wan_read,
4187 .write = wan_write,
4188 .exit = wan_exit,
4189 .suspend = wan_suspend,
4190 .shutdown = wan_shutdown,
4193 /*************************************************************************
4194 * UWB subdriver
4197 enum {
4198 /* ACPI GUWB/SUWB bits */
4199 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4200 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4203 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4205 static struct rfkill *tpacpi_uwb_rfkill;
4207 static int uwb_get_radiosw(void)
4209 int status;
4211 if (!tp_features.uwb)
4212 return -ENODEV;
4214 /* WLSW overrides UWB in firmware/hardware, reflect that */
4215 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4216 return RFKILL_STATE_HARD_BLOCKED;
4218 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4219 if (dbg_uwbemul)
4220 return (tpacpi_uwb_emulstate) ?
4221 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4222 #endif
4224 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4225 return -EIO;
4227 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4228 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4231 static void uwb_update_rfk(void)
4233 int status;
4235 if (!tpacpi_uwb_rfkill)
4236 return;
4238 status = uwb_get_radiosw();
4239 if (status < 0)
4240 return;
4241 rfkill_force_state(tpacpi_uwb_rfkill, status);
4243 vdbg_printk(TPACPI_DBG_RFKILL,
4244 "forced rfkill state to %d\n",
4245 status);
4248 static int uwb_set_radiosw(int radio_on, int update_rfk)
4250 int status;
4252 if (!tp_features.uwb)
4253 return -ENODEV;
4255 /* WLSW overrides UWB in firmware/hardware, but there is no
4256 * reason to risk weird behaviour. */
4257 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4258 && radio_on)
4259 return -EPERM;
4261 vdbg_printk(TPACPI_DBG_RFKILL,
4262 "will %s UWB\n", radio_on ? "enable" : "disable");
4264 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4265 if (dbg_uwbemul) {
4266 tpacpi_uwb_emulstate = !!radio_on;
4267 if (update_rfk)
4268 uwb_update_rfk();
4269 return 0;
4271 #endif
4273 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4274 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4275 return -EIO;
4277 if (update_rfk)
4278 uwb_update_rfk();
4280 return 0;
4283 /* --------------------------------------------------------------------- */
4285 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4287 int uwbs = uwb_get_radiosw();
4289 if (uwbs < 0)
4290 return uwbs;
4292 *state = uwbs;
4293 return 0;
4296 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4298 dbg_printk(TPACPI_DBG_RFKILL,
4299 "request to change radio state to %d\n", state);
4300 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4303 static void uwb_exit(void)
4305 if (tpacpi_uwb_rfkill)
4306 rfkill_unregister(tpacpi_uwb_rfkill);
4309 static int __init uwb_init(struct ibm_init_struct *iibm)
4311 int res;
4312 int status = 0;
4314 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4315 "initializing uwb subdriver\n");
4317 TPACPI_ACPIHANDLE_INIT(hkey);
4319 tp_features.uwb = hkey_handle &&
4320 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4322 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4323 "uwb is %s, status 0x%02x\n",
4324 str_supported(tp_features.uwb),
4325 status);
4327 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4328 if (dbg_uwbemul) {
4329 tp_features.uwb = 1;
4330 printk(TPACPI_INFO
4331 "uwb switch emulation enabled\n");
4332 } else
4333 #endif
4334 if (tp_features.uwb &&
4335 !(status & TP_ACPI_UWB_HWPRESENT)) {
4336 /* no uwb hardware present in system */
4337 tp_features.uwb = 0;
4338 dbg_printk(TPACPI_DBG_INIT,
4339 "uwb hardware not installed\n");
4342 if (!tp_features.uwb)
4343 return 1;
4345 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4346 &tpacpi_uwb_rfkill,
4347 RFKILL_TYPE_UWB,
4348 TPACPI_RFK_UWB_SW_NAME,
4349 false,
4350 tpacpi_uwb_rfk_set,
4351 tpacpi_uwb_rfk_get);
4353 return res;
4356 static struct ibm_struct uwb_driver_data = {
4357 .name = "uwb",
4358 .exit = uwb_exit,
4359 .flags.experimental = 1,
4362 /*************************************************************************
4363 * Video subdriver
4366 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4368 enum video_access_mode {
4369 TPACPI_VIDEO_NONE = 0,
4370 TPACPI_VIDEO_570, /* 570 */
4371 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4372 TPACPI_VIDEO_NEW, /* all others */
4375 enum { /* video status flags, based on VIDEO_570 */
4376 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4377 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4378 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4381 enum { /* TPACPI_VIDEO_570 constants */
4382 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4383 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4384 * video_status_flags */
4385 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4386 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4389 static enum video_access_mode video_supported;
4390 static int video_orig_autosw;
4392 static int video_autosw_get(void);
4393 static int video_autosw_set(int enable);
4395 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4397 static int __init video_init(struct ibm_init_struct *iibm)
4399 int ivga;
4401 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4403 TPACPI_ACPIHANDLE_INIT(vid);
4404 TPACPI_ACPIHANDLE_INIT(vid2);
4406 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4407 /* G41, assume IVGA doesn't change */
4408 vid_handle = vid2_handle;
4410 if (!vid_handle)
4411 /* video switching not supported on R30, R31 */
4412 video_supported = TPACPI_VIDEO_NONE;
4413 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4414 /* 570 */
4415 video_supported = TPACPI_VIDEO_570;
4416 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4417 /* 600e/x, 770e, 770x */
4418 video_supported = TPACPI_VIDEO_770;
4419 else
4420 /* all others */
4421 video_supported = TPACPI_VIDEO_NEW;
4423 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4424 str_supported(video_supported != TPACPI_VIDEO_NONE),
4425 video_supported);
4427 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4430 static void video_exit(void)
4432 dbg_printk(TPACPI_DBG_EXIT,
4433 "restoring original video autoswitch mode\n");
4434 if (video_autosw_set(video_orig_autosw))
4435 printk(TPACPI_ERR "error while trying to restore original "
4436 "video autoswitch mode\n");
4439 static int video_outputsw_get(void)
4441 int status = 0;
4442 int i;
4444 switch (video_supported) {
4445 case TPACPI_VIDEO_570:
4446 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4447 TP_ACPI_VIDEO_570_PHSCMD))
4448 return -EIO;
4449 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4450 break;
4451 case TPACPI_VIDEO_770:
4452 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4453 return -EIO;
4454 if (i)
4455 status |= TP_ACPI_VIDEO_S_LCD;
4456 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4457 return -EIO;
4458 if (i)
4459 status |= TP_ACPI_VIDEO_S_CRT;
4460 break;
4461 case TPACPI_VIDEO_NEW:
4462 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4463 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4464 return -EIO;
4465 if (i)
4466 status |= TP_ACPI_VIDEO_S_CRT;
4468 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4469 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4470 return -EIO;
4471 if (i)
4472 status |= TP_ACPI_VIDEO_S_LCD;
4473 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_DVI;
4477 break;
4478 default:
4479 return -ENOSYS;
4482 return status;
4485 static int video_outputsw_set(int status)
4487 int autosw;
4488 int res = 0;
4490 switch (video_supported) {
4491 case TPACPI_VIDEO_570:
4492 res = acpi_evalf(NULL, NULL,
4493 "\\_SB.PHS2", "vdd",
4494 TP_ACPI_VIDEO_570_PHS2CMD,
4495 status | TP_ACPI_VIDEO_570_PHS2SET);
4496 break;
4497 case TPACPI_VIDEO_770:
4498 autosw = video_autosw_get();
4499 if (autosw < 0)
4500 return autosw;
4502 res = video_autosw_set(1);
4503 if (res)
4504 return res;
4505 res = acpi_evalf(vid_handle, NULL,
4506 "ASWT", "vdd", status * 0x100, 0);
4507 if (!autosw && video_autosw_set(autosw)) {
4508 printk(TPACPI_ERR
4509 "video auto-switch left enabled due to error\n");
4510 return -EIO;
4512 break;
4513 case TPACPI_VIDEO_NEW:
4514 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4515 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4516 break;
4517 default:
4518 return -ENOSYS;
4521 return (res)? 0 : -EIO;
4524 static int video_autosw_get(void)
4526 int autosw = 0;
4528 switch (video_supported) {
4529 case TPACPI_VIDEO_570:
4530 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4531 return -EIO;
4532 break;
4533 case TPACPI_VIDEO_770:
4534 case TPACPI_VIDEO_NEW:
4535 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4536 return -EIO;
4537 break;
4538 default:
4539 return -ENOSYS;
4542 return autosw & 1;
4545 static int video_autosw_set(int enable)
4547 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4548 return -EIO;
4549 return 0;
4552 static int video_outputsw_cycle(void)
4554 int autosw = video_autosw_get();
4555 int res;
4557 if (autosw < 0)
4558 return autosw;
4560 switch (video_supported) {
4561 case TPACPI_VIDEO_570:
4562 res = video_autosw_set(1);
4563 if (res)
4564 return res;
4565 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4566 break;
4567 case TPACPI_VIDEO_770:
4568 case TPACPI_VIDEO_NEW:
4569 res = video_autosw_set(1);
4570 if (res)
4571 return res;
4572 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4573 break;
4574 default:
4575 return -ENOSYS;
4577 if (!autosw && video_autosw_set(autosw)) {
4578 printk(TPACPI_ERR
4579 "video auto-switch left enabled due to error\n");
4580 return -EIO;
4583 return (res)? 0 : -EIO;
4586 static int video_expand_toggle(void)
4588 switch (video_supported) {
4589 case TPACPI_VIDEO_570:
4590 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4591 0 : -EIO;
4592 case TPACPI_VIDEO_770:
4593 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4594 0 : -EIO;
4595 case TPACPI_VIDEO_NEW:
4596 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4597 0 : -EIO;
4598 default:
4599 return -ENOSYS;
4601 /* not reached */
4604 static int video_read(char *p)
4606 int status, autosw;
4607 int len = 0;
4609 if (video_supported == TPACPI_VIDEO_NONE) {
4610 len += sprintf(p + len, "status:\t\tnot supported\n");
4611 return len;
4614 status = video_outputsw_get();
4615 if (status < 0)
4616 return status;
4618 autosw = video_autosw_get();
4619 if (autosw < 0)
4620 return autosw;
4622 len += sprintf(p + len, "status:\t\tsupported\n");
4623 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4624 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4625 if (video_supported == TPACPI_VIDEO_NEW)
4626 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4627 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4628 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4629 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4630 if (video_supported == TPACPI_VIDEO_NEW)
4631 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4632 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4633 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4635 return len;
4638 static int video_write(char *buf)
4640 char *cmd;
4641 int enable, disable, status;
4642 int res;
4644 if (video_supported == TPACPI_VIDEO_NONE)
4645 return -ENODEV;
4647 enable = 0;
4648 disable = 0;
4650 while ((cmd = next_cmd(&buf))) {
4651 if (strlencmp(cmd, "lcd_enable") == 0) {
4652 enable |= TP_ACPI_VIDEO_S_LCD;
4653 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4654 disable |= TP_ACPI_VIDEO_S_LCD;
4655 } else if (strlencmp(cmd, "crt_enable") == 0) {
4656 enable |= TP_ACPI_VIDEO_S_CRT;
4657 } else if (strlencmp(cmd, "crt_disable") == 0) {
4658 disable |= TP_ACPI_VIDEO_S_CRT;
4659 } else if (video_supported == TPACPI_VIDEO_NEW &&
4660 strlencmp(cmd, "dvi_enable") == 0) {
4661 enable |= TP_ACPI_VIDEO_S_DVI;
4662 } else if (video_supported == TPACPI_VIDEO_NEW &&
4663 strlencmp(cmd, "dvi_disable") == 0) {
4664 disable |= TP_ACPI_VIDEO_S_DVI;
4665 } else if (strlencmp(cmd, "auto_enable") == 0) {
4666 res = video_autosw_set(1);
4667 if (res)
4668 return res;
4669 } else if (strlencmp(cmd, "auto_disable") == 0) {
4670 res = video_autosw_set(0);
4671 if (res)
4672 return res;
4673 } else if (strlencmp(cmd, "video_switch") == 0) {
4674 res = video_outputsw_cycle();
4675 if (res)
4676 return res;
4677 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4678 res = video_expand_toggle();
4679 if (res)
4680 return res;
4681 } else
4682 return -EINVAL;
4685 if (enable || disable) {
4686 status = video_outputsw_get();
4687 if (status < 0)
4688 return status;
4689 res = video_outputsw_set((status & ~disable) | enable);
4690 if (res)
4691 return res;
4694 return 0;
4697 static struct ibm_struct video_driver_data = {
4698 .name = "video",
4699 .read = video_read,
4700 .write = video_write,
4701 .exit = video_exit,
4704 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4706 /*************************************************************************
4707 * Light (thinklight) subdriver
4710 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4711 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4713 static int light_get_status(void)
4715 int status = 0;
4717 if (tp_features.light_status) {
4718 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4719 return -EIO;
4720 return (!!status);
4723 return -ENXIO;
4726 static int light_set_status(int status)
4728 int rc;
4730 if (tp_features.light) {
4731 if (cmos_handle) {
4732 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4733 (status)?
4734 TP_CMOS_THINKLIGHT_ON :
4735 TP_CMOS_THINKLIGHT_OFF);
4736 } else {
4737 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4738 (status)? 1 : 0);
4740 return (rc)? 0 : -EIO;
4743 return -ENXIO;
4746 static void light_set_status_worker(struct work_struct *work)
4748 struct tpacpi_led_classdev *data =
4749 container_of(work, struct tpacpi_led_classdev, work);
4751 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4752 light_set_status((data->new_state != TPACPI_LED_OFF));
4755 static void light_sysfs_set(struct led_classdev *led_cdev,
4756 enum led_brightness brightness)
4758 struct tpacpi_led_classdev *data =
4759 container_of(led_cdev,
4760 struct tpacpi_led_classdev,
4761 led_classdev);
4762 data->new_state = (brightness != LED_OFF) ?
4763 TPACPI_LED_ON : TPACPI_LED_OFF;
4764 queue_work(tpacpi_wq, &data->work);
4767 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4769 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4772 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4773 .led_classdev = {
4774 .name = "tpacpi::thinklight",
4775 .brightness_set = &light_sysfs_set,
4776 .brightness_get = &light_sysfs_get,
4780 static int __init light_init(struct ibm_init_struct *iibm)
4782 int rc;
4784 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4786 TPACPI_ACPIHANDLE_INIT(ledb);
4787 TPACPI_ACPIHANDLE_INIT(lght);
4788 TPACPI_ACPIHANDLE_INIT(cmos);
4789 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4791 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4792 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4794 if (tp_features.light)
4795 /* light status not supported on
4796 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4797 tp_features.light_status =
4798 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4800 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4801 str_supported(tp_features.light),
4802 str_supported(tp_features.light_status));
4804 if (!tp_features.light)
4805 return 1;
4807 rc = led_classdev_register(&tpacpi_pdev->dev,
4808 &tpacpi_led_thinklight.led_classdev);
4810 if (rc < 0) {
4811 tp_features.light = 0;
4812 tp_features.light_status = 0;
4813 } else {
4814 rc = 0;
4817 return rc;
4820 static void light_exit(void)
4822 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4823 if (work_pending(&tpacpi_led_thinklight.work))
4824 flush_workqueue(tpacpi_wq);
4827 static int light_read(char *p)
4829 int len = 0;
4830 int status;
4832 if (!tp_features.light) {
4833 len += sprintf(p + len, "status:\t\tnot supported\n");
4834 } else if (!tp_features.light_status) {
4835 len += sprintf(p + len, "status:\t\tunknown\n");
4836 len += sprintf(p + len, "commands:\ton, off\n");
4837 } else {
4838 status = light_get_status();
4839 if (status < 0)
4840 return status;
4841 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4842 len += sprintf(p + len, "commands:\ton, off\n");
4845 return len;
4848 static int light_write(char *buf)
4850 char *cmd;
4851 int newstatus = 0;
4853 if (!tp_features.light)
4854 return -ENODEV;
4856 while ((cmd = next_cmd(&buf))) {
4857 if (strlencmp(cmd, "on") == 0) {
4858 newstatus = 1;
4859 } else if (strlencmp(cmd, "off") == 0) {
4860 newstatus = 0;
4861 } else
4862 return -EINVAL;
4865 return light_set_status(newstatus);
4868 static struct ibm_struct light_driver_data = {
4869 .name = "light",
4870 .read = light_read,
4871 .write = light_write,
4872 .exit = light_exit,
4875 /*************************************************************************
4876 * CMOS subdriver
4879 /* sysfs cmos_command -------------------------------------------------- */
4880 static ssize_t cmos_command_store(struct device *dev,
4881 struct device_attribute *attr,
4882 const char *buf, size_t count)
4884 unsigned long cmos_cmd;
4885 int res;
4887 if (parse_strtoul(buf, 21, &cmos_cmd))
4888 return -EINVAL;
4890 res = issue_thinkpad_cmos_command(cmos_cmd);
4891 return (res)? res : count;
4894 static struct device_attribute dev_attr_cmos_command =
4895 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4897 /* --------------------------------------------------------------------- */
4899 static int __init cmos_init(struct ibm_init_struct *iibm)
4901 int res;
4903 vdbg_printk(TPACPI_DBG_INIT,
4904 "initializing cmos commands subdriver\n");
4906 TPACPI_ACPIHANDLE_INIT(cmos);
4908 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4909 str_supported(cmos_handle != NULL));
4911 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4912 if (res)
4913 return res;
4915 return (cmos_handle)? 0 : 1;
4918 static void cmos_exit(void)
4920 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4923 static int cmos_read(char *p)
4925 int len = 0;
4927 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4928 R30, R31, T20-22, X20-21 */
4929 if (!cmos_handle)
4930 len += sprintf(p + len, "status:\t\tnot supported\n");
4931 else {
4932 len += sprintf(p + len, "status:\t\tsupported\n");
4933 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4936 return len;
4939 static int cmos_write(char *buf)
4941 char *cmd;
4942 int cmos_cmd, res;
4944 while ((cmd = next_cmd(&buf))) {
4945 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4946 cmos_cmd >= 0 && cmos_cmd <= 21) {
4947 /* cmos_cmd set */
4948 } else
4949 return -EINVAL;
4951 res = issue_thinkpad_cmos_command(cmos_cmd);
4952 if (res)
4953 return res;
4956 return 0;
4959 static struct ibm_struct cmos_driver_data = {
4960 .name = "cmos",
4961 .read = cmos_read,
4962 .write = cmos_write,
4963 .exit = cmos_exit,
4966 /*************************************************************************
4967 * LED subdriver
4970 enum led_access_mode {
4971 TPACPI_LED_NONE = 0,
4972 TPACPI_LED_570, /* 570 */
4973 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4974 TPACPI_LED_NEW, /* all others */
4977 enum { /* For TPACPI_LED_OLD */
4978 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4979 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4980 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4983 static enum led_access_mode led_supported;
4985 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4986 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4987 /* T20-22, X20-21 */
4988 "LED", /* all others */
4989 ); /* R30, R31 */
4991 #define TPACPI_LED_NUMLEDS 16
4992 static struct tpacpi_led_classdev *tpacpi_leds;
4993 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4994 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4995 /* there's a limit of 19 chars + NULL before 2.6.26 */
4996 "tpacpi::power",
4997 "tpacpi:orange:batt",
4998 "tpacpi:green:batt",
4999 "tpacpi::dock_active",
5000 "tpacpi::bay_active",
5001 "tpacpi::dock_batt",
5002 "tpacpi::unknown_led",
5003 "tpacpi::standby",
5004 "tpacpi::dock_status1",
5005 "tpacpi::dock_status2",
5006 "tpacpi::unknown_led2",
5007 "tpacpi::unknown_led3",
5008 "tpacpi::thinkvantage",
5010 #define TPACPI_SAFE_LEDS 0x1081U
5012 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5014 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5015 return false;
5016 #else
5017 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5018 #endif
5021 static int led_get_status(const unsigned int led)
5023 int status;
5024 enum led_status_t led_s;
5026 switch (led_supported) {
5027 case TPACPI_LED_570:
5028 if (!acpi_evalf(ec_handle,
5029 &status, "GLED", "dd", 1 << led))
5030 return -EIO;
5031 led_s = (status == 0)?
5032 TPACPI_LED_OFF :
5033 ((status == 1)?
5034 TPACPI_LED_ON :
5035 TPACPI_LED_BLINK);
5036 tpacpi_led_state_cache[led] = led_s;
5037 return led_s;
5038 default:
5039 return -ENXIO;
5042 /* not reached */
5045 static int led_set_status(const unsigned int led,
5046 const enum led_status_t ledstatus)
5048 /* off, on, blink. Index is led_status_t */
5049 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5050 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5052 int rc = 0;
5054 switch (led_supported) {
5055 case TPACPI_LED_570:
5056 /* 570 */
5057 if (unlikely(led > 7))
5058 return -EINVAL;
5059 if (unlikely(tpacpi_is_led_restricted(led)))
5060 return -EPERM;
5061 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5062 (1 << led), led_sled_arg1[ledstatus]))
5063 rc = -EIO;
5064 break;
5065 case TPACPI_LED_OLD:
5066 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5067 if (unlikely(led > 7))
5068 return -EINVAL;
5069 if (unlikely(tpacpi_is_led_restricted(led)))
5070 return -EPERM;
5071 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5072 if (rc >= 0)
5073 rc = ec_write(TPACPI_LED_EC_HLBL,
5074 (ledstatus == TPACPI_LED_BLINK) << led);
5075 if (rc >= 0)
5076 rc = ec_write(TPACPI_LED_EC_HLCL,
5077 (ledstatus != TPACPI_LED_OFF) << led);
5078 break;
5079 case TPACPI_LED_NEW:
5080 /* all others */
5081 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5082 return -EINVAL;
5083 if (unlikely(tpacpi_is_led_restricted(led)))
5084 return -EPERM;
5085 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5086 led, led_led_arg1[ledstatus]))
5087 rc = -EIO;
5088 break;
5089 default:
5090 rc = -ENXIO;
5093 if (!rc)
5094 tpacpi_led_state_cache[led] = ledstatus;
5096 return rc;
5099 static void led_set_status_worker(struct work_struct *work)
5101 struct tpacpi_led_classdev *data =
5102 container_of(work, struct tpacpi_led_classdev, work);
5104 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5105 led_set_status(data->led, data->new_state);
5108 static void led_sysfs_set(struct led_classdev *led_cdev,
5109 enum led_brightness brightness)
5111 struct tpacpi_led_classdev *data = container_of(led_cdev,
5112 struct tpacpi_led_classdev, led_classdev);
5114 if (brightness == LED_OFF)
5115 data->new_state = TPACPI_LED_OFF;
5116 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5117 data->new_state = TPACPI_LED_ON;
5118 else
5119 data->new_state = TPACPI_LED_BLINK;
5121 queue_work(tpacpi_wq, &data->work);
5124 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5125 unsigned long *delay_on, unsigned long *delay_off)
5127 struct tpacpi_led_classdev *data = container_of(led_cdev,
5128 struct tpacpi_led_classdev, led_classdev);
5130 /* Can we choose the flash rate? */
5131 if (*delay_on == 0 && *delay_off == 0) {
5132 /* yes. set them to the hardware blink rate (1 Hz) */
5133 *delay_on = 500; /* ms */
5134 *delay_off = 500; /* ms */
5135 } else if ((*delay_on != 500) || (*delay_off != 500))
5136 return -EINVAL;
5138 data->new_state = TPACPI_LED_BLINK;
5139 queue_work(tpacpi_wq, &data->work);
5141 return 0;
5144 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5146 int rc;
5148 struct tpacpi_led_classdev *data = container_of(led_cdev,
5149 struct tpacpi_led_classdev, led_classdev);
5151 rc = led_get_status(data->led);
5153 if (rc == TPACPI_LED_OFF || rc < 0)
5154 rc = LED_OFF; /* no error handling in led class :( */
5155 else
5156 rc = LED_FULL;
5158 return rc;
5161 static void led_exit(void)
5163 unsigned int i;
5165 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5166 if (tpacpi_leds[i].led_classdev.name)
5167 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5170 kfree(tpacpi_leds);
5173 static int __init tpacpi_init_led(unsigned int led)
5175 int rc;
5177 tpacpi_leds[led].led = led;
5179 /* LEDs with no name don't get registered */
5180 if (!tpacpi_led_names[led])
5181 return 0;
5183 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5184 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5185 if (led_supported == TPACPI_LED_570)
5186 tpacpi_leds[led].led_classdev.brightness_get =
5187 &led_sysfs_get;
5189 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5191 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5193 rc = led_classdev_register(&tpacpi_pdev->dev,
5194 &tpacpi_leds[led].led_classdev);
5195 if (rc < 0)
5196 tpacpi_leds[led].led_classdev.name = NULL;
5198 return rc;
5201 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5202 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5203 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5204 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5206 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5207 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5208 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5209 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5210 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5211 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5212 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5213 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5215 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5216 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5217 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5218 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5219 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5221 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5222 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5223 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5224 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5226 /* (1) - may have excess leds enabled on MSB */
5228 /* Defaults (order matters, keep last, don't reorder!) */
5229 { /* Lenovo */
5230 .vendor = PCI_VENDOR_ID_LENOVO,
5231 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5232 .quirks = 0x1fffU,
5234 { /* IBM ThinkPads with no EC version string */
5235 .vendor = PCI_VENDOR_ID_IBM,
5236 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5237 .quirks = 0x00ffU,
5239 { /* IBM ThinkPads with EC version string */
5240 .vendor = PCI_VENDOR_ID_IBM,
5241 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5242 .quirks = 0x00bfU,
5246 #undef TPACPI_LEDQ_IBM
5247 #undef TPACPI_LEDQ_LNV
5249 static int __init led_init(struct ibm_init_struct *iibm)
5251 unsigned int i;
5252 int rc;
5253 unsigned long useful_leds;
5255 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5257 TPACPI_ACPIHANDLE_INIT(led);
5259 if (!led_handle)
5260 /* led not supported on R30, R31 */
5261 led_supported = TPACPI_LED_NONE;
5262 else if (strlencmp(led_path, "SLED") == 0)
5263 /* 570 */
5264 led_supported = TPACPI_LED_570;
5265 else if (strlencmp(led_path, "SYSL") == 0)
5266 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5267 led_supported = TPACPI_LED_OLD;
5268 else
5269 /* all others */
5270 led_supported = TPACPI_LED_NEW;
5272 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5273 str_supported(led_supported), led_supported);
5275 if (led_supported == TPACPI_LED_NONE)
5276 return 1;
5278 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5279 GFP_KERNEL);
5280 if (!tpacpi_leds) {
5281 printk(TPACPI_ERR "Out of memory for LED data\n");
5282 return -ENOMEM;
5285 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5286 ARRAY_SIZE(led_useful_qtable));
5288 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5289 if (!tpacpi_is_led_restricted(i) &&
5290 test_bit(i, &useful_leds)) {
5291 rc = tpacpi_init_led(i);
5292 if (rc < 0) {
5293 led_exit();
5294 return rc;
5299 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5300 printk(TPACPI_NOTICE
5301 "warning: userspace override of important "
5302 "firmware LEDs is enabled\n");
5303 #endif
5304 return 0;
5307 #define str_led_status(s) \
5308 ((s) == TPACPI_LED_OFF ? "off" : \
5309 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5311 static int led_read(char *p)
5313 int len = 0;
5315 if (!led_supported) {
5316 len += sprintf(p + len, "status:\t\tnot supported\n");
5317 return len;
5319 len += sprintf(p + len, "status:\t\tsupported\n");
5321 if (led_supported == TPACPI_LED_570) {
5322 /* 570 */
5323 int i, status;
5324 for (i = 0; i < 8; i++) {
5325 status = led_get_status(i);
5326 if (status < 0)
5327 return -EIO;
5328 len += sprintf(p + len, "%d:\t\t%s\n",
5329 i, str_led_status(status));
5333 len += sprintf(p + len, "commands:\t"
5334 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5336 return len;
5339 static int led_write(char *buf)
5341 char *cmd;
5342 int led, rc;
5343 enum led_status_t s;
5345 if (!led_supported)
5346 return -ENODEV;
5348 while ((cmd = next_cmd(&buf))) {
5349 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5350 return -EINVAL;
5352 if (strstr(cmd, "off")) {
5353 s = TPACPI_LED_OFF;
5354 } else if (strstr(cmd, "on")) {
5355 s = TPACPI_LED_ON;
5356 } else if (strstr(cmd, "blink")) {
5357 s = TPACPI_LED_BLINK;
5358 } else {
5359 return -EINVAL;
5362 rc = led_set_status(led, s);
5363 if (rc < 0)
5364 return rc;
5367 return 0;
5370 static struct ibm_struct led_driver_data = {
5371 .name = "led",
5372 .read = led_read,
5373 .write = led_write,
5374 .exit = led_exit,
5377 /*************************************************************************
5378 * Beep subdriver
5381 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5383 #define TPACPI_BEEP_Q1 0x0001
5385 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5386 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5387 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5390 static int __init beep_init(struct ibm_init_struct *iibm)
5392 unsigned long quirks;
5394 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5396 TPACPI_ACPIHANDLE_INIT(beep);
5398 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5399 str_supported(beep_handle != NULL));
5401 quirks = tpacpi_check_quirks(beep_quirk_table,
5402 ARRAY_SIZE(beep_quirk_table));
5404 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5406 return (beep_handle)? 0 : 1;
5409 static int beep_read(char *p)
5411 int len = 0;
5413 if (!beep_handle)
5414 len += sprintf(p + len, "status:\t\tnot supported\n");
5415 else {
5416 len += sprintf(p + len, "status:\t\tsupported\n");
5417 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5420 return len;
5423 static int beep_write(char *buf)
5425 char *cmd;
5426 int beep_cmd;
5428 if (!beep_handle)
5429 return -ENODEV;
5431 while ((cmd = next_cmd(&buf))) {
5432 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5433 beep_cmd >= 0 && beep_cmd <= 17) {
5434 /* beep_cmd set */
5435 } else
5436 return -EINVAL;
5437 if (tp_features.beep_needs_two_args) {
5438 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5439 beep_cmd, 0))
5440 return -EIO;
5441 } else {
5442 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5443 beep_cmd))
5444 return -EIO;
5448 return 0;
5451 static struct ibm_struct beep_driver_data = {
5452 .name = "beep",
5453 .read = beep_read,
5454 .write = beep_write,
5457 /*************************************************************************
5458 * Thermal subdriver
5461 enum thermal_access_mode {
5462 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5463 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5464 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5465 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5466 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5469 enum { /* TPACPI_THERMAL_TPEC_* */
5470 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5471 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5472 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5474 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5478 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5479 struct ibm_thermal_sensors_struct {
5480 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5483 static enum thermal_access_mode thermal_read_mode;
5485 /* idx is zero-based */
5486 static int thermal_get_sensor(int idx, s32 *value)
5488 int t;
5489 s8 tmp;
5490 char tmpi[5];
5492 t = TP_EC_THERMAL_TMP0;
5494 switch (thermal_read_mode) {
5495 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5496 case TPACPI_THERMAL_TPEC_16:
5497 if (idx >= 8 && idx <= 15) {
5498 t = TP_EC_THERMAL_TMP8;
5499 idx -= 8;
5501 /* fallthrough */
5502 #endif
5503 case TPACPI_THERMAL_TPEC_8:
5504 if (idx <= 7) {
5505 if (!acpi_ec_read(t + idx, &tmp))
5506 return -EIO;
5507 *value = tmp * 1000;
5508 return 0;
5510 break;
5512 case TPACPI_THERMAL_ACPI_UPDT:
5513 if (idx <= 7) {
5514 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5515 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5516 return -EIO;
5517 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5518 return -EIO;
5519 *value = (t - 2732) * 100;
5520 return 0;
5522 break;
5524 case TPACPI_THERMAL_ACPI_TMP07:
5525 if (idx <= 7) {
5526 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5527 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5528 return -EIO;
5529 if (t > 127 || t < -127)
5530 t = TP_EC_THERMAL_TMP_NA;
5531 *value = t * 1000;
5532 return 0;
5534 break;
5536 case TPACPI_THERMAL_NONE:
5537 default:
5538 return -ENOSYS;
5541 return -EINVAL;
5544 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5546 int res, i;
5547 int n;
5549 n = 8;
5550 i = 0;
5552 if (!s)
5553 return -EINVAL;
5555 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5556 n = 16;
5558 for (i = 0 ; i < n; i++) {
5559 res = thermal_get_sensor(i, &s->temp[i]);
5560 if (res)
5561 return res;
5564 return n;
5567 static void thermal_dump_all_sensors(void)
5569 int n, i;
5570 struct ibm_thermal_sensors_struct t;
5572 n = thermal_get_sensors(&t);
5573 if (n <= 0)
5574 return;
5576 printk(TPACPI_NOTICE
5577 "temperatures (Celsius):");
5579 for (i = 0; i < n; i++) {
5580 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5581 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5582 else
5583 printk(KERN_CONT " N/A");
5586 printk(KERN_CONT "\n");
5589 /* sysfs temp##_input -------------------------------------------------- */
5591 static ssize_t thermal_temp_input_show(struct device *dev,
5592 struct device_attribute *attr,
5593 char *buf)
5595 struct sensor_device_attribute *sensor_attr =
5596 to_sensor_dev_attr(attr);
5597 int idx = sensor_attr->index;
5598 s32 value;
5599 int res;
5601 res = thermal_get_sensor(idx, &value);
5602 if (res)
5603 return res;
5604 if (value == TPACPI_THERMAL_SENSOR_NA)
5605 return -ENXIO;
5607 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5610 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5611 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5612 thermal_temp_input_show, NULL, _idxB)
5614 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5615 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5616 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5617 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5618 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5619 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5620 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5621 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5622 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5623 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5624 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5625 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5626 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5627 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5628 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5629 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5630 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5633 #define THERMAL_ATTRS(X) \
5634 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5636 static struct attribute *thermal_temp_input_attr[] = {
5637 THERMAL_ATTRS(8),
5638 THERMAL_ATTRS(9),
5639 THERMAL_ATTRS(10),
5640 THERMAL_ATTRS(11),
5641 THERMAL_ATTRS(12),
5642 THERMAL_ATTRS(13),
5643 THERMAL_ATTRS(14),
5644 THERMAL_ATTRS(15),
5645 THERMAL_ATTRS(0),
5646 THERMAL_ATTRS(1),
5647 THERMAL_ATTRS(2),
5648 THERMAL_ATTRS(3),
5649 THERMAL_ATTRS(4),
5650 THERMAL_ATTRS(5),
5651 THERMAL_ATTRS(6),
5652 THERMAL_ATTRS(7),
5653 NULL
5656 static const struct attribute_group thermal_temp_input16_group = {
5657 .attrs = thermal_temp_input_attr
5660 static const struct attribute_group thermal_temp_input8_group = {
5661 .attrs = &thermal_temp_input_attr[8]
5664 #undef THERMAL_SENSOR_ATTR_TEMP
5665 #undef THERMAL_ATTRS
5667 /* --------------------------------------------------------------------- */
5669 static int __init thermal_init(struct ibm_init_struct *iibm)
5671 u8 t, ta1, ta2;
5672 int i;
5673 int acpi_tmp7;
5674 int res;
5676 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5678 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5680 if (thinkpad_id.ec_model) {
5682 * Direct EC access mode: sensors at registers
5683 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5684 * non-implemented, thermal sensors return 0x80 when
5685 * not available
5688 ta1 = ta2 = 0;
5689 for (i = 0; i < 8; i++) {
5690 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5691 ta1 |= t;
5692 } else {
5693 ta1 = 0;
5694 break;
5696 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5697 ta2 |= t;
5698 } else {
5699 ta1 = 0;
5700 break;
5703 if (ta1 == 0) {
5704 /* This is sheer paranoia, but we handle it anyway */
5705 if (acpi_tmp7) {
5706 printk(TPACPI_ERR
5707 "ThinkPad ACPI EC access misbehaving, "
5708 "falling back to ACPI TMPx access "
5709 "mode\n");
5710 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5711 } else {
5712 printk(TPACPI_ERR
5713 "ThinkPad ACPI EC access misbehaving, "
5714 "disabling thermal sensors access\n");
5715 thermal_read_mode = TPACPI_THERMAL_NONE;
5717 } else {
5718 thermal_read_mode =
5719 (ta2 != 0) ?
5720 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5722 } else if (acpi_tmp7) {
5723 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5724 /* 600e/x, 770e, 770x */
5725 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5726 } else {
5727 /* Standard ACPI TMPx access, max 8 sensors */
5728 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5730 } else {
5731 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5732 thermal_read_mode = TPACPI_THERMAL_NONE;
5735 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5736 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5737 thermal_read_mode);
5739 switch (thermal_read_mode) {
5740 case TPACPI_THERMAL_TPEC_16:
5741 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5742 &thermal_temp_input16_group);
5743 if (res)
5744 return res;
5745 break;
5746 case TPACPI_THERMAL_TPEC_8:
5747 case TPACPI_THERMAL_ACPI_TMP07:
5748 case TPACPI_THERMAL_ACPI_UPDT:
5749 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5750 &thermal_temp_input8_group);
5751 if (res)
5752 return res;
5753 break;
5754 case TPACPI_THERMAL_NONE:
5755 default:
5756 return 1;
5759 return 0;
5762 static void thermal_exit(void)
5764 switch (thermal_read_mode) {
5765 case TPACPI_THERMAL_TPEC_16:
5766 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5767 &thermal_temp_input16_group);
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
5772 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5773 &thermal_temp_input16_group);
5774 break;
5775 case TPACPI_THERMAL_NONE:
5776 default:
5777 break;
5781 static int thermal_read(char *p)
5783 int len = 0;
5784 int n, i;
5785 struct ibm_thermal_sensors_struct t;
5787 n = thermal_get_sensors(&t);
5788 if (unlikely(n < 0))
5789 return n;
5791 len += sprintf(p + len, "temperatures:\t");
5793 if (n > 0) {
5794 for (i = 0; i < (n - 1); i++)
5795 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5796 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5797 } else
5798 len += sprintf(p + len, "not supported\n");
5800 return len;
5803 static struct ibm_struct thermal_driver_data = {
5804 .name = "thermal",
5805 .read = thermal_read,
5806 .exit = thermal_exit,
5809 /*************************************************************************
5810 * EC Dump subdriver
5813 static u8 ecdump_regs[256];
5815 static int ecdump_read(char *p)
5817 int len = 0;
5818 int i, j;
5819 u8 v;
5821 len += sprintf(p + len, "EC "
5822 " +00 +01 +02 +03 +04 +05 +06 +07"
5823 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5824 for (i = 0; i < 256; i += 16) {
5825 len += sprintf(p + len, "EC 0x%02x:", i);
5826 for (j = 0; j < 16; j++) {
5827 if (!acpi_ec_read(i + j, &v))
5828 break;
5829 if (v != ecdump_regs[i + j])
5830 len += sprintf(p + len, " *%02x", v);
5831 else
5832 len += sprintf(p + len, " %02x", v);
5833 ecdump_regs[i + j] = v;
5835 len += sprintf(p + len, "\n");
5836 if (j != 16)
5837 break;
5840 /* These are way too dangerous to advertise openly... */
5841 #if 0
5842 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5843 " (<offset> is 00-ff, <value> is 00-ff)\n");
5844 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5845 " (<offset> is 00-ff, <value> is 0-255)\n");
5846 #endif
5847 return len;
5850 static int ecdump_write(char *buf)
5852 char *cmd;
5853 int i, v;
5855 while ((cmd = next_cmd(&buf))) {
5856 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5857 /* i and v set */
5858 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5859 /* i and v set */
5860 } else
5861 return -EINVAL;
5862 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5863 if (!acpi_ec_write(i, v))
5864 return -EIO;
5865 } else
5866 return -EINVAL;
5869 return 0;
5872 static struct ibm_struct ecdump_driver_data = {
5873 .name = "ecdump",
5874 .read = ecdump_read,
5875 .write = ecdump_write,
5876 .flags.experimental = 1,
5879 /*************************************************************************
5880 * Backlight/brightness subdriver
5883 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5886 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5887 * CMOS NVRAM byte 0x5E, bits 0-3.
5889 * EC HBRV (0x31) has the following layout
5890 * Bit 7: unknown function
5891 * Bit 6: unknown function
5892 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5893 * Bit 4: must be set to zero to avoid problems
5894 * Bit 3-0: backlight brightness level
5896 * brightness_get_raw returns status data in the HBRV layout
5898 * WARNING: The X61 has been verified to use HBRV for something else, so
5899 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5900 * testing on the very early *60 Lenovo models...
5903 enum {
5904 TP_EC_BACKLIGHT = 0x31,
5906 /* TP_EC_BACKLIGHT bitmasks */
5907 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5908 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5909 TP_EC_BACKLIGHT_MAPSW = 0x20,
5912 enum tpacpi_brightness_access_mode {
5913 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5914 TPACPI_BRGHT_MODE_EC, /* EC control */
5915 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5916 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5917 TPACPI_BRGHT_MODE_MAX
5920 static struct backlight_device *ibm_backlight_device;
5922 static enum tpacpi_brightness_access_mode brightness_mode =
5923 TPACPI_BRGHT_MODE_MAX;
5925 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5927 static struct mutex brightness_mutex;
5929 /* NVRAM brightness access,
5930 * call with brightness_mutex held! */
5931 static unsigned int tpacpi_brightness_nvram_get(void)
5933 u8 lnvram;
5935 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5936 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5937 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5938 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5940 return lnvram;
5943 static void tpacpi_brightness_checkpoint_nvram(void)
5945 u8 lec = 0;
5946 u8 b_nvram;
5948 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5949 return;
5951 vdbg_printk(TPACPI_DBG_BRGHT,
5952 "trying to checkpoint backlight level to NVRAM...\n");
5954 if (mutex_lock_killable(&brightness_mutex) < 0)
5955 return;
5957 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5958 goto unlock;
5959 lec &= TP_EC_BACKLIGHT_LVLMSK;
5960 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5962 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5963 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5964 /* NVRAM needs update */
5965 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5966 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5967 b_nvram |= lec;
5968 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5969 dbg_printk(TPACPI_DBG_BRGHT,
5970 "updated NVRAM backlight level to %u (0x%02x)\n",
5971 (unsigned int) lec, (unsigned int) b_nvram);
5972 } else
5973 vdbg_printk(TPACPI_DBG_BRGHT,
5974 "NVRAM backlight level already is %u (0x%02x)\n",
5975 (unsigned int) lec, (unsigned int) b_nvram);
5977 unlock:
5978 mutex_unlock(&brightness_mutex);
5982 /* call with brightness_mutex held! */
5983 static int tpacpi_brightness_get_raw(int *status)
5985 u8 lec = 0;
5987 switch (brightness_mode) {
5988 case TPACPI_BRGHT_MODE_UCMS_STEP:
5989 *status = tpacpi_brightness_nvram_get();
5990 return 0;
5991 case TPACPI_BRGHT_MODE_EC:
5992 case TPACPI_BRGHT_MODE_ECNVRAM:
5993 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5994 return -EIO;
5995 *status = lec;
5996 return 0;
5997 default:
5998 return -ENXIO;
6002 /* call with brightness_mutex held! */
6003 /* do NOT call with illegal backlight level value */
6004 static int tpacpi_brightness_set_ec(unsigned int value)
6006 u8 lec = 0;
6008 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6009 return -EIO;
6011 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6012 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6013 (value & TP_EC_BACKLIGHT_LVLMSK))))
6014 return -EIO;
6016 return 0;
6019 /* call with brightness_mutex held! */
6020 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6022 int cmos_cmd, inc;
6023 unsigned int current_value, i;
6025 current_value = tpacpi_brightness_nvram_get();
6027 if (value == current_value)
6028 return 0;
6030 cmos_cmd = (value > current_value) ?
6031 TP_CMOS_BRIGHTNESS_UP :
6032 TP_CMOS_BRIGHTNESS_DOWN;
6033 inc = (value > current_value) ? 1 : -1;
6035 for (i = current_value; i != value; i += inc)
6036 if (issue_thinkpad_cmos_command(cmos_cmd))
6037 return -EIO;
6039 return 0;
6042 /* May return EINTR which can always be mapped to ERESTARTSYS */
6043 static int brightness_set(unsigned int value)
6045 int res;
6047 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6048 value < 0)
6049 return -EINVAL;
6051 vdbg_printk(TPACPI_DBG_BRGHT,
6052 "set backlight level to %d\n", value);
6054 res = mutex_lock_killable(&brightness_mutex);
6055 if (res < 0)
6056 return res;
6058 switch (brightness_mode) {
6059 case TPACPI_BRGHT_MODE_EC:
6060 case TPACPI_BRGHT_MODE_ECNVRAM:
6061 res = tpacpi_brightness_set_ec(value);
6062 break;
6063 case TPACPI_BRGHT_MODE_UCMS_STEP:
6064 res = tpacpi_brightness_set_ucmsstep(value);
6065 break;
6066 default:
6067 res = -ENXIO;
6070 mutex_unlock(&brightness_mutex);
6071 return res;
6074 /* sysfs backlight class ----------------------------------------------- */
6076 static int brightness_update_status(struct backlight_device *bd)
6078 unsigned int level =
6079 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6080 bd->props.power == FB_BLANK_UNBLANK) ?
6081 bd->props.brightness : 0;
6083 dbg_printk(TPACPI_DBG_BRGHT,
6084 "backlight: attempt to set level to %d\n",
6085 level);
6087 /* it is the backlight class's job (caller) to handle
6088 * EINTR and other errors properly */
6089 return brightness_set(level);
6092 static int brightness_get(struct backlight_device *bd)
6094 int status, res;
6096 res = mutex_lock_killable(&brightness_mutex);
6097 if (res < 0)
6098 return 0;
6100 res = tpacpi_brightness_get_raw(&status);
6102 mutex_unlock(&brightness_mutex);
6104 if (res < 0)
6105 return 0;
6107 return status & TP_EC_BACKLIGHT_LVLMSK;
6110 static void tpacpi_brightness_notify_change(void)
6112 backlight_force_update(ibm_backlight_device,
6113 BACKLIGHT_UPDATE_HOTKEY);
6116 static struct backlight_ops ibm_backlight_data = {
6117 .get_brightness = brightness_get,
6118 .update_status = brightness_update_status,
6121 /* --------------------------------------------------------------------- */
6124 * These are only useful for models that have only one possibility
6125 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6126 * these quirks.
6128 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6129 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6130 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6132 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6133 /* Models with ATI GPUs known to require ECNVRAM mode */
6134 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6136 /* Models with ATI GPUs that can use ECNVRAM */
6137 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6138 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6139 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6140 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 /* Models with Intel Extreme Graphics 2 */
6143 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6144 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6145 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6147 /* Models with Intel GMA900 */
6148 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6149 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6150 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6153 static int __init brightness_init(struct ibm_init_struct *iibm)
6155 int b;
6156 unsigned long quirks;
6158 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6160 mutex_init(&brightness_mutex);
6162 quirks = tpacpi_check_quirks(brightness_quirk_table,
6163 ARRAY_SIZE(brightness_quirk_table));
6166 * We always attempt to detect acpi support, so as to switch
6167 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6168 * going to publish a backlight interface
6170 b = tpacpi_check_std_acpi_brightness_support();
6171 if (b > 0) {
6173 if (acpi_video_backlight_support()) {
6174 if (brightness_enable > 1) {
6175 printk(TPACPI_NOTICE
6176 "Standard ACPI backlight interface "
6177 "available, not loading native one.\n");
6178 return 1;
6179 } else if (brightness_enable == 1) {
6180 printk(TPACPI_NOTICE
6181 "Backlight control force enabled, even if standard "
6182 "ACPI backlight interface is available\n");
6184 } else {
6185 if (brightness_enable > 1) {
6186 printk(TPACPI_NOTICE
6187 "Standard ACPI backlight interface not "
6188 "available, thinkpad_acpi native "
6189 "brightness control enabled\n");
6194 if (!brightness_enable) {
6195 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6196 "brightness support disabled by "
6197 "module parameter\n");
6198 return 1;
6201 if (b > 16) {
6202 printk(TPACPI_ERR
6203 "Unsupported brightness interface, "
6204 "please contact %s\n", TPACPI_MAIL);
6205 return 1;
6207 if (b == 16)
6208 tp_features.bright_16levels = 1;
6211 * Check for module parameter bogosity, note that we
6212 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6213 * able to detect "unspecified"
6215 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6216 return -EINVAL;
6218 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6219 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6220 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6221 if (quirks & TPACPI_BRGHT_Q_EC)
6222 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6223 else
6224 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6226 dbg_printk(TPACPI_DBG_BRGHT,
6227 "driver auto-selected brightness_mode=%d\n",
6228 brightness_mode);
6231 /* Safety */
6232 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6233 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6234 brightness_mode == TPACPI_BRGHT_MODE_EC))
6235 return -EINVAL;
6237 if (tpacpi_brightness_get_raw(&b) < 0)
6238 return 1;
6240 if (tp_features.bright_16levels)
6241 printk(TPACPI_INFO
6242 "detected a 16-level brightness capable ThinkPad\n");
6244 ibm_backlight_device = backlight_device_register(
6245 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6246 &ibm_backlight_data);
6247 if (IS_ERR(ibm_backlight_device)) {
6248 int rc = PTR_ERR(ibm_backlight_device);
6249 ibm_backlight_device = NULL;
6250 printk(TPACPI_ERR "Could not register backlight device\n");
6251 return rc;
6253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6254 "brightness is supported\n");
6256 if (quirks & TPACPI_BRGHT_Q_ASK) {
6257 printk(TPACPI_NOTICE
6258 "brightness: will use unverified default: "
6259 "brightness_mode=%d\n", brightness_mode);
6260 printk(TPACPI_NOTICE
6261 "brightness: please report to %s whether it works well "
6262 "or not on your ThinkPad\n", TPACPI_MAIL);
6265 ibm_backlight_device->props.max_brightness =
6266 (tp_features.bright_16levels)? 15 : 7;
6267 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6268 backlight_update_status(ibm_backlight_device);
6270 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6271 "brightness: registering brightness hotkeys "
6272 "as change notification\n");
6273 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6274 | TP_ACPI_HKEY_BRGHTUP_MASK
6275 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6276 return 0;
6279 static void brightness_suspend(pm_message_t state)
6281 tpacpi_brightness_checkpoint_nvram();
6284 static void brightness_shutdown(void)
6286 tpacpi_brightness_checkpoint_nvram();
6289 static void brightness_exit(void)
6291 if (ibm_backlight_device) {
6292 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6293 "calling backlight_device_unregister()\n");
6294 backlight_device_unregister(ibm_backlight_device);
6297 tpacpi_brightness_checkpoint_nvram();
6300 static int brightness_read(char *p)
6302 int len = 0;
6303 int level;
6305 level = brightness_get(NULL);
6306 if (level < 0) {
6307 len += sprintf(p + len, "level:\t\tunreadable\n");
6308 } else {
6309 len += sprintf(p + len, "level:\t\t%d\n", level);
6310 len += sprintf(p + len, "commands:\tup, down\n");
6311 len += sprintf(p + len, "commands:\tlevel <level>"
6312 " (<level> is 0-%d)\n",
6313 (tp_features.bright_16levels) ? 15 : 7);
6316 return len;
6319 static int brightness_write(char *buf)
6321 int level;
6322 int rc;
6323 char *cmd;
6324 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6326 level = brightness_get(NULL);
6327 if (level < 0)
6328 return level;
6330 while ((cmd = next_cmd(&buf))) {
6331 if (strlencmp(cmd, "up") == 0) {
6332 if (level < max_level)
6333 level++;
6334 } else if (strlencmp(cmd, "down") == 0) {
6335 if (level > 0)
6336 level--;
6337 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6338 level >= 0 && level <= max_level) {
6339 /* new level set */
6340 } else
6341 return -EINVAL;
6344 tpacpi_disclose_usertask("procfs brightness",
6345 "set level to %d\n", level);
6348 * Now we know what the final level should be, so we try to set it.
6349 * Doing it this way makes the syscall restartable in case of EINTR
6351 rc = brightness_set(level);
6352 if (!rc && ibm_backlight_device)
6353 backlight_force_update(ibm_backlight_device,
6354 BACKLIGHT_UPDATE_SYSFS);
6355 return (rc == -EINTR)? -ERESTARTSYS : rc;
6358 static struct ibm_struct brightness_driver_data = {
6359 .name = "brightness",
6360 .read = brightness_read,
6361 .write = brightness_write,
6362 .exit = brightness_exit,
6363 .suspend = brightness_suspend,
6364 .shutdown = brightness_shutdown,
6367 /*************************************************************************
6368 * Volume subdriver
6372 * IBM ThinkPads have a simple volume controller with MUTE gating.
6373 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6375 * Since the *61 series (and probably also the later *60 series), Lenovo
6376 * ThinkPads only implement the MUTE gate.
6378 * EC register 0x30
6379 * Bit 6: MUTE (1 mutes sound)
6380 * Bit 3-0: Volume
6381 * Other bits should be zero as far as we know.
6383 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6384 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6385 * such as bit 7 which is used to detect repeated presses of MUTE,
6386 * and we leave them unchanged.
6389 enum {
6390 TP_EC_AUDIO = 0x30,
6392 /* TP_EC_AUDIO bits */
6393 TP_EC_AUDIO_MUTESW = 6,
6395 /* TP_EC_AUDIO bitmasks */
6396 TP_EC_AUDIO_LVL_MSK = 0x0F,
6397 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6399 /* Maximum volume */
6400 TP_EC_VOLUME_MAX = 14,
6403 enum tpacpi_volume_access_mode {
6404 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6405 TPACPI_VOL_MODE_EC, /* Pure EC control */
6406 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6407 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6408 TPACPI_VOL_MODE_MAX
6411 static enum tpacpi_volume_access_mode volume_mode =
6412 TPACPI_VOL_MODE_MAX;
6416 * Used to syncronize writers to TP_EC_AUDIO and
6417 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6419 static struct mutex volume_mutex;
6421 static void tpacpi_volume_checkpoint_nvram(void)
6423 u8 lec = 0;
6424 u8 b_nvram;
6425 const u8 ec_mask = TP_EC_AUDIO_LVL_MSK | TP_EC_AUDIO_MUTESW_MSK;
6427 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6428 return;
6430 vdbg_printk(TPACPI_DBG_MIXER,
6431 "trying to checkpoint mixer state to NVRAM...\n");
6433 if (mutex_lock_killable(&volume_mutex) < 0)
6434 return;
6436 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6437 goto unlock;
6438 lec &= ec_mask;
6439 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6441 if (lec != (b_nvram & ec_mask)) {
6442 /* NVRAM needs update */
6443 b_nvram &= ~ec_mask;
6444 b_nvram |= lec;
6445 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6446 dbg_printk(TPACPI_DBG_MIXER,
6447 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6448 (unsigned int) lec, (unsigned int) b_nvram);
6449 } else {
6450 vdbg_printk(TPACPI_DBG_MIXER,
6451 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6452 (unsigned int) lec, (unsigned int) b_nvram);
6455 unlock:
6456 mutex_unlock(&volume_mutex);
6459 static int volume_get_status_ec(u8 *status)
6461 u8 s;
6463 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6464 return -EIO;
6466 *status = s;
6468 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6470 return 0;
6473 static int volume_get_status(u8 *status)
6475 return volume_get_status_ec(status);
6478 static int volume_set_status_ec(const u8 status)
6480 if (!acpi_ec_write(TP_EC_AUDIO, status))
6481 return -EIO;
6483 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6485 return 0;
6488 static int volume_set_status(const u8 status)
6490 return volume_set_status_ec(status);
6493 static int volume_set_mute_ec(const bool mute)
6495 int rc;
6496 u8 s, n;
6498 if (mutex_lock_killable(&volume_mutex) < 0)
6499 return -EINTR;
6501 rc = volume_get_status_ec(&s);
6502 if (rc)
6503 goto unlock;
6505 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6506 s & ~TP_EC_AUDIO_MUTESW_MSK;
6508 if (n != s)
6509 rc = volume_set_status_ec(n);
6511 unlock:
6512 mutex_unlock(&volume_mutex);
6513 return rc;
6516 static int volume_set_mute(const bool mute)
6518 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6519 (mute) ? "" : "un");
6520 return volume_set_mute_ec(mute);
6523 static int volume_set_volume_ec(const u8 vol)
6525 int rc;
6526 u8 s, n;
6528 if (vol > TP_EC_VOLUME_MAX)
6529 return -EINVAL;
6531 if (mutex_lock_killable(&volume_mutex) < 0)
6532 return -EINTR;
6534 rc = volume_get_status_ec(&s);
6535 if (rc)
6536 goto unlock;
6538 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6540 if (n != s)
6541 rc = volume_set_status_ec(n);
6543 unlock:
6544 mutex_unlock(&volume_mutex);
6545 return rc;
6548 static int volume_set_volume(const u8 vol)
6550 dbg_printk(TPACPI_DBG_MIXER,
6551 "trying to set volume level to %hu\n", vol);
6552 return volume_set_volume_ec(vol);
6555 static void volume_suspend(pm_message_t state)
6557 tpacpi_volume_checkpoint_nvram();
6560 static void volume_shutdown(void)
6562 tpacpi_volume_checkpoint_nvram();
6565 static void volume_exit(void)
6567 tpacpi_volume_checkpoint_nvram();
6570 static int __init volume_init(struct ibm_init_struct *iibm)
6572 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6574 mutex_init(&volume_mutex);
6577 * Check for module parameter bogosity, note that we
6578 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6579 * able to detect "unspecified"
6581 if (volume_mode > TPACPI_VOL_MODE_MAX)
6582 return -EINVAL;
6584 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6585 printk(TPACPI_ERR
6586 "UCMS step volume mode not implemented, "
6587 "please contact %s\n", TPACPI_MAIL);
6588 return 1;
6591 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6592 volume_mode == TPACPI_VOL_MODE_MAX) {
6593 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6595 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6596 "driver auto-selected volume_mode=%d\n",
6597 volume_mode);
6598 } else {
6599 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6600 "using user-supplied volume_mode=%d\n",
6601 volume_mode);
6604 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6605 "volume is supported\n");
6607 return 0;
6610 static int volume_read(char *p)
6612 int len = 0;
6613 u8 status;
6615 if (volume_get_status(&status) < 0) {
6616 len += sprintf(p + len, "level:\t\tunreadable\n");
6617 } else {
6618 len += sprintf(p + len, "level:\t\t%d\n",
6619 status & TP_EC_AUDIO_LVL_MSK);
6620 len += sprintf(p + len, "mute:\t\t%s\n",
6621 onoff(status, TP_EC_AUDIO_MUTESW));
6622 len += sprintf(p + len, "commands:\tup, down, mute\n");
6623 len += sprintf(p + len, "commands:\tlevel <level>"
6624 " (<level> is 0-%d)\n", TP_EC_VOLUME_MAX);
6627 return len;
6630 static int volume_write(char *buf)
6632 u8 s;
6633 u8 new_level, new_mute;
6634 int l;
6635 char *cmd;
6636 int rc;
6638 rc = volume_get_status(&s);
6639 if (rc < 0)
6640 return rc;
6642 new_level = s & TP_EC_AUDIO_LVL_MSK;
6643 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6645 while ((cmd = next_cmd(&buf))) {
6646 if (strlencmp(cmd, "up") == 0) {
6647 if (new_mute)
6648 new_mute = 0;
6649 else if (new_level < TP_EC_VOLUME_MAX)
6650 new_level++;
6651 } else if (strlencmp(cmd, "down") == 0) {
6652 if (new_mute)
6653 new_mute = 0;
6654 else if (new_level > 0)
6655 new_level--;
6656 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6657 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6658 new_level = l;
6659 } else if (strlencmp(cmd, "mute") == 0) {
6660 new_mute = TP_EC_AUDIO_MUTESW_MSK;
6661 } else
6662 return -EINVAL;
6665 tpacpi_disclose_usertask("procfs volume",
6666 "%smute and set level to %d\n",
6667 new_mute ? "" : "un", new_level);
6669 rc = volume_set_status(new_mute | new_level);
6671 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6674 static struct ibm_struct volume_driver_data = {
6675 .name = "volume",
6676 .read = volume_read,
6677 .write = volume_write,
6678 .exit = volume_exit,
6679 .suspend = volume_suspend,
6680 .shutdown = volume_shutdown,
6683 /*************************************************************************
6684 * Fan subdriver
6688 * FAN ACCESS MODES
6690 * TPACPI_FAN_RD_ACPI_GFAN:
6691 * ACPI GFAN method: returns fan level
6693 * see TPACPI_FAN_WR_ACPI_SFAN
6694 * EC 0x2f (HFSP) not available if GFAN exists
6696 * TPACPI_FAN_WR_ACPI_SFAN:
6697 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6699 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6700 * it for writing.
6702 * TPACPI_FAN_WR_TPEC:
6703 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6704 * Supported on almost all ThinkPads
6706 * Fan speed changes of any sort (including those caused by the
6707 * disengaged mode) are usually done slowly by the firmware as the
6708 * maximum ammount of fan duty cycle change per second seems to be
6709 * limited.
6711 * Reading is not available if GFAN exists.
6712 * Writing is not available if SFAN exists.
6714 * Bits
6715 * 7 automatic mode engaged;
6716 * (default operation mode of the ThinkPad)
6717 * fan level is ignored in this mode.
6718 * 6 full speed mode (takes precedence over bit 7);
6719 * not available on all thinkpads. May disable
6720 * the tachometer while the fan controller ramps up
6721 * the speed (which can take up to a few *minutes*).
6722 * Speeds up fan to 100% duty-cycle, which is far above
6723 * the standard RPM levels. It is not impossible that
6724 * it could cause hardware damage.
6725 * 5-3 unused in some models. Extra bits for fan level
6726 * in others, but still useless as all values above
6727 * 7 map to the same speed as level 7 in these models.
6728 * 2-0 fan level (0..7 usually)
6729 * 0x00 = stop
6730 * 0x07 = max (set when temperatures critical)
6731 * Some ThinkPads may have other levels, see
6732 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6734 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6735 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6736 * does so, its initial value is meaningless (0x07).
6738 * For firmware bugs, refer to:
6739 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6741 * ----
6743 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6744 * Main fan tachometer reading (in RPM)
6746 * This register is present on all ThinkPads with a new-style EC, and
6747 * it is known not to be present on the A21m/e, and T22, as there is
6748 * something else in offset 0x84 according to the ACPI DSDT. Other
6749 * ThinkPads from this same time period (and earlier) probably lack the
6750 * tachometer as well.
6752 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6753 * was never fixed by IBM to report the EC firmware version string
6754 * probably support the tachometer (like the early X models), so
6755 * detecting it is quite hard. We need more data to know for sure.
6757 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6758 * might result.
6760 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6761 * mode.
6763 * For firmware bugs, refer to:
6764 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6766 * ----
6768 * ThinkPad EC register 0x31 bit 0 (only on select models)
6770 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6771 * show the speed of the main fan. When bit 0 of EC register 0x31
6772 * is one, the tachometer registers show the speed of the auxiliary
6773 * fan.
6775 * Fan control seems to affect both fans, regardless of the state
6776 * of this bit.
6778 * So far, only the firmware for the X60/X61 non-tablet versions
6779 * seem to support this (firmware TP-7M).
6781 * TPACPI_FAN_WR_ACPI_FANS:
6782 * ThinkPad X31, X40, X41. Not available in the X60.
6784 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6785 * high speed. ACPI DSDT seems to map these three speeds to levels
6786 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6787 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6789 * The speeds are stored on handles
6790 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6792 * There are three default speed sets, acessible as handles:
6793 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6795 * ACPI DSDT switches which set is in use depending on various
6796 * factors.
6798 * TPACPI_FAN_WR_TPEC is also available and should be used to
6799 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6800 * but the ACPI tables just mention level 7.
6803 enum { /* Fan control constants */
6804 fan_status_offset = 0x2f, /* EC register 0x2f */
6805 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6806 * 0x84 must be read before 0x85 */
6807 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6808 bit 0 selects which fan is active */
6810 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6811 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6813 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6816 enum fan_status_access_mode {
6817 TPACPI_FAN_NONE = 0, /* No fan status or control */
6818 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6819 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6822 enum fan_control_access_mode {
6823 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6824 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6825 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6826 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6829 enum fan_control_commands {
6830 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6831 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6832 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6833 * and also watchdog cmd */
6836 static int fan_control_allowed;
6838 static enum fan_status_access_mode fan_status_access_mode;
6839 static enum fan_control_access_mode fan_control_access_mode;
6840 static enum fan_control_commands fan_control_commands;
6842 static u8 fan_control_initial_status;
6843 static u8 fan_control_desired_level;
6844 static u8 fan_control_resume_level;
6845 static int fan_watchdog_maxinterval;
6847 static struct mutex fan_mutex;
6849 static void fan_watchdog_fire(struct work_struct *ignored);
6850 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6852 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6853 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6854 "\\FSPD", /* 600e/x, 770e, 770x */
6855 ); /* all others */
6856 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6857 "JFNS", /* 770x-JL */
6858 ); /* all others */
6861 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6862 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6863 * be in auto mode (0x80).
6865 * This is corrected by any write to HFSP either by the driver, or
6866 * by the firmware.
6868 * We assume 0x07 really means auto mode while this quirk is active,
6869 * as this is far more likely than the ThinkPad being in level 7,
6870 * which is only used by the firmware during thermal emergencies.
6872 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6873 * TP-70 (T43, R52), which are known to be buggy.
6876 static void fan_quirk1_setup(void)
6878 if (fan_control_initial_status == 0x07) {
6879 printk(TPACPI_NOTICE
6880 "fan_init: initial fan status is unknown, "
6881 "assuming it is in auto mode\n");
6882 tp_features.fan_ctrl_status_undef = 1;
6886 static void fan_quirk1_handle(u8 *fan_status)
6888 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6889 if (*fan_status != fan_control_initial_status) {
6890 /* something changed the HFSP regisnter since
6891 * driver init time, so it is not undefined
6892 * anymore */
6893 tp_features.fan_ctrl_status_undef = 0;
6894 } else {
6895 /* Return most likely status. In fact, it
6896 * might be the only possible status */
6897 *fan_status = TP_EC_FAN_AUTO;
6902 /* Select main fan on X60/X61, NOOP on others */
6903 static bool fan_select_fan1(void)
6905 if (tp_features.second_fan) {
6906 u8 val;
6908 if (ec_read(fan_select_offset, &val) < 0)
6909 return false;
6910 val &= 0xFEU;
6911 if (ec_write(fan_select_offset, val) < 0)
6912 return false;
6914 return true;
6917 /* Select secondary fan on X60/X61 */
6918 static bool fan_select_fan2(void)
6920 u8 val;
6922 if (!tp_features.second_fan)
6923 return false;
6925 if (ec_read(fan_select_offset, &val) < 0)
6926 return false;
6927 val |= 0x01U;
6928 if (ec_write(fan_select_offset, val) < 0)
6929 return false;
6931 return true;
6935 * Call with fan_mutex held
6937 static void fan_update_desired_level(u8 status)
6939 if ((status &
6940 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6941 if (status > 7)
6942 fan_control_desired_level = 7;
6943 else
6944 fan_control_desired_level = status;
6948 static int fan_get_status(u8 *status)
6950 u8 s;
6952 /* TODO:
6953 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6955 switch (fan_status_access_mode) {
6956 case TPACPI_FAN_RD_ACPI_GFAN:
6957 /* 570, 600e/x, 770e, 770x */
6959 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6960 return -EIO;
6962 if (likely(status))
6963 *status = s & 0x07;
6965 break;
6967 case TPACPI_FAN_RD_TPEC:
6968 /* all except 570, 600e/x, 770e, 770x */
6969 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6970 return -EIO;
6972 if (likely(status)) {
6973 *status = s;
6974 fan_quirk1_handle(status);
6977 break;
6979 default:
6980 return -ENXIO;
6983 return 0;
6986 static int fan_get_status_safe(u8 *status)
6988 int rc;
6989 u8 s;
6991 if (mutex_lock_killable(&fan_mutex))
6992 return -ERESTARTSYS;
6993 rc = fan_get_status(&s);
6994 if (!rc)
6995 fan_update_desired_level(s);
6996 mutex_unlock(&fan_mutex);
6998 if (status)
6999 *status = s;
7001 return rc;
7004 static int fan_get_speed(unsigned int *speed)
7006 u8 hi, lo;
7008 switch (fan_status_access_mode) {
7009 case TPACPI_FAN_RD_TPEC:
7010 /* all except 570, 600e/x, 770e, 770x */
7011 if (unlikely(!fan_select_fan1()))
7012 return -EIO;
7013 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7014 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7015 return -EIO;
7017 if (likely(speed))
7018 *speed = (hi << 8) | lo;
7020 break;
7022 default:
7023 return -ENXIO;
7026 return 0;
7029 static int fan2_get_speed(unsigned int *speed)
7031 u8 hi, lo;
7032 bool rc;
7034 switch (fan_status_access_mode) {
7035 case TPACPI_FAN_RD_TPEC:
7036 /* all except 570, 600e/x, 770e, 770x */
7037 if (unlikely(!fan_select_fan2()))
7038 return -EIO;
7039 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7040 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7041 fan_select_fan1(); /* play it safe */
7042 if (rc)
7043 return -EIO;
7045 if (likely(speed))
7046 *speed = (hi << 8) | lo;
7048 break;
7050 default:
7051 return -ENXIO;
7054 return 0;
7057 static int fan_set_level(int level)
7059 if (!fan_control_allowed)
7060 return -EPERM;
7062 switch (fan_control_access_mode) {
7063 case TPACPI_FAN_WR_ACPI_SFAN:
7064 if (level >= 0 && level <= 7) {
7065 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7066 return -EIO;
7067 } else
7068 return -EINVAL;
7069 break;
7071 case TPACPI_FAN_WR_ACPI_FANS:
7072 case TPACPI_FAN_WR_TPEC:
7073 if (!(level & TP_EC_FAN_AUTO) &&
7074 !(level & TP_EC_FAN_FULLSPEED) &&
7075 ((level < 0) || (level > 7)))
7076 return -EINVAL;
7078 /* safety net should the EC not support AUTO
7079 * or FULLSPEED mode bits and just ignore them */
7080 if (level & TP_EC_FAN_FULLSPEED)
7081 level |= 7; /* safety min speed 7 */
7082 else if (level & TP_EC_FAN_AUTO)
7083 level |= 4; /* safety min speed 4 */
7085 if (!acpi_ec_write(fan_status_offset, level))
7086 return -EIO;
7087 else
7088 tp_features.fan_ctrl_status_undef = 0;
7089 break;
7091 default:
7092 return -ENXIO;
7095 vdbg_printk(TPACPI_DBG_FAN,
7096 "fan control: set fan control register to 0x%02x\n", level);
7097 return 0;
7100 static int fan_set_level_safe(int level)
7102 int rc;
7104 if (!fan_control_allowed)
7105 return -EPERM;
7107 if (mutex_lock_killable(&fan_mutex))
7108 return -ERESTARTSYS;
7110 if (level == TPACPI_FAN_LAST_LEVEL)
7111 level = fan_control_desired_level;
7113 rc = fan_set_level(level);
7114 if (!rc)
7115 fan_update_desired_level(level);
7117 mutex_unlock(&fan_mutex);
7118 return rc;
7121 static int fan_set_enable(void)
7123 u8 s;
7124 int rc;
7126 if (!fan_control_allowed)
7127 return -EPERM;
7129 if (mutex_lock_killable(&fan_mutex))
7130 return -ERESTARTSYS;
7132 switch (fan_control_access_mode) {
7133 case TPACPI_FAN_WR_ACPI_FANS:
7134 case TPACPI_FAN_WR_TPEC:
7135 rc = fan_get_status(&s);
7136 if (rc < 0)
7137 break;
7139 /* Don't go out of emergency fan mode */
7140 if (s != 7) {
7141 s &= 0x07;
7142 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7145 if (!acpi_ec_write(fan_status_offset, s))
7146 rc = -EIO;
7147 else {
7148 tp_features.fan_ctrl_status_undef = 0;
7149 rc = 0;
7151 break;
7153 case TPACPI_FAN_WR_ACPI_SFAN:
7154 rc = fan_get_status(&s);
7155 if (rc < 0)
7156 break;
7158 s &= 0x07;
7160 /* Set fan to at least level 4 */
7161 s |= 4;
7163 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7164 rc = -EIO;
7165 else
7166 rc = 0;
7167 break;
7169 default:
7170 rc = -ENXIO;
7173 mutex_unlock(&fan_mutex);
7175 if (!rc)
7176 vdbg_printk(TPACPI_DBG_FAN,
7177 "fan control: set fan control register to 0x%02x\n",
7179 return rc;
7182 static int fan_set_disable(void)
7184 int rc;
7186 if (!fan_control_allowed)
7187 return -EPERM;
7189 if (mutex_lock_killable(&fan_mutex))
7190 return -ERESTARTSYS;
7192 rc = 0;
7193 switch (fan_control_access_mode) {
7194 case TPACPI_FAN_WR_ACPI_FANS:
7195 case TPACPI_FAN_WR_TPEC:
7196 if (!acpi_ec_write(fan_status_offset, 0x00))
7197 rc = -EIO;
7198 else {
7199 fan_control_desired_level = 0;
7200 tp_features.fan_ctrl_status_undef = 0;
7202 break;
7204 case TPACPI_FAN_WR_ACPI_SFAN:
7205 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7206 rc = -EIO;
7207 else
7208 fan_control_desired_level = 0;
7209 break;
7211 default:
7212 rc = -ENXIO;
7215 if (!rc)
7216 vdbg_printk(TPACPI_DBG_FAN,
7217 "fan control: set fan control register to 0\n");
7219 mutex_unlock(&fan_mutex);
7220 return rc;
7223 static int fan_set_speed(int speed)
7225 int rc;
7227 if (!fan_control_allowed)
7228 return -EPERM;
7230 if (mutex_lock_killable(&fan_mutex))
7231 return -ERESTARTSYS;
7233 rc = 0;
7234 switch (fan_control_access_mode) {
7235 case TPACPI_FAN_WR_ACPI_FANS:
7236 if (speed >= 0 && speed <= 65535) {
7237 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7238 speed, speed, speed))
7239 rc = -EIO;
7240 } else
7241 rc = -EINVAL;
7242 break;
7244 default:
7245 rc = -ENXIO;
7248 mutex_unlock(&fan_mutex);
7249 return rc;
7252 static void fan_watchdog_reset(void)
7254 static int fan_watchdog_active;
7256 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7257 return;
7259 if (fan_watchdog_active)
7260 cancel_delayed_work(&fan_watchdog_task);
7262 if (fan_watchdog_maxinterval > 0 &&
7263 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7264 fan_watchdog_active = 1;
7265 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7266 msecs_to_jiffies(fan_watchdog_maxinterval
7267 * 1000))) {
7268 printk(TPACPI_ERR
7269 "failed to queue the fan watchdog, "
7270 "watchdog will not trigger\n");
7272 } else
7273 fan_watchdog_active = 0;
7276 static void fan_watchdog_fire(struct work_struct *ignored)
7278 int rc;
7280 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7281 return;
7283 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7284 rc = fan_set_enable();
7285 if (rc < 0) {
7286 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7287 "will try again later...\n", -rc);
7288 /* reschedule for later */
7289 fan_watchdog_reset();
7294 * SYSFS fan layout: hwmon compatible (device)
7296 * pwm*_enable:
7297 * 0: "disengaged" mode
7298 * 1: manual mode
7299 * 2: native EC "auto" mode (recommended, hardware default)
7301 * pwm*: set speed in manual mode, ignored otherwise.
7302 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7303 * interpolation.
7305 * fan*_input: tachometer reading, RPM
7308 * SYSFS fan layout: extensions
7310 * fan_watchdog (driver):
7311 * fan watchdog interval in seconds, 0 disables (default), max 120
7314 /* sysfs fan pwm1_enable ----------------------------------------------- */
7315 static ssize_t fan_pwm1_enable_show(struct device *dev,
7316 struct device_attribute *attr,
7317 char *buf)
7319 int res, mode;
7320 u8 status;
7322 res = fan_get_status_safe(&status);
7323 if (res)
7324 return res;
7326 if (status & TP_EC_FAN_FULLSPEED) {
7327 mode = 0;
7328 } else if (status & TP_EC_FAN_AUTO) {
7329 mode = 2;
7330 } else
7331 mode = 1;
7333 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7336 static ssize_t fan_pwm1_enable_store(struct device *dev,
7337 struct device_attribute *attr,
7338 const char *buf, size_t count)
7340 unsigned long t;
7341 int res, level;
7343 if (parse_strtoul(buf, 2, &t))
7344 return -EINVAL;
7346 tpacpi_disclose_usertask("hwmon pwm1_enable",
7347 "set fan mode to %lu\n", t);
7349 switch (t) {
7350 case 0:
7351 level = TP_EC_FAN_FULLSPEED;
7352 break;
7353 case 1:
7354 level = TPACPI_FAN_LAST_LEVEL;
7355 break;
7356 case 2:
7357 level = TP_EC_FAN_AUTO;
7358 break;
7359 case 3:
7360 /* reserved for software-controlled auto mode */
7361 return -ENOSYS;
7362 default:
7363 return -EINVAL;
7366 res = fan_set_level_safe(level);
7367 if (res == -ENXIO)
7368 return -EINVAL;
7369 else if (res < 0)
7370 return res;
7372 fan_watchdog_reset();
7374 return count;
7377 static struct device_attribute dev_attr_fan_pwm1_enable =
7378 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7379 fan_pwm1_enable_show, fan_pwm1_enable_store);
7381 /* sysfs fan pwm1 ------------------------------------------------------ */
7382 static ssize_t fan_pwm1_show(struct device *dev,
7383 struct device_attribute *attr,
7384 char *buf)
7386 int res;
7387 u8 status;
7389 res = fan_get_status_safe(&status);
7390 if (res)
7391 return res;
7393 if ((status &
7394 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7395 status = fan_control_desired_level;
7397 if (status > 7)
7398 status = 7;
7400 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7403 static ssize_t fan_pwm1_store(struct device *dev,
7404 struct device_attribute *attr,
7405 const char *buf, size_t count)
7407 unsigned long s;
7408 int rc;
7409 u8 status, newlevel;
7411 if (parse_strtoul(buf, 255, &s))
7412 return -EINVAL;
7414 tpacpi_disclose_usertask("hwmon pwm1",
7415 "set fan speed to %lu\n", s);
7417 /* scale down from 0-255 to 0-7 */
7418 newlevel = (s >> 5) & 0x07;
7420 if (mutex_lock_killable(&fan_mutex))
7421 return -ERESTARTSYS;
7423 rc = fan_get_status(&status);
7424 if (!rc && (status &
7425 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7426 rc = fan_set_level(newlevel);
7427 if (rc == -ENXIO)
7428 rc = -EINVAL;
7429 else if (!rc) {
7430 fan_update_desired_level(newlevel);
7431 fan_watchdog_reset();
7435 mutex_unlock(&fan_mutex);
7436 return (rc)? rc : count;
7439 static struct device_attribute dev_attr_fan_pwm1 =
7440 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7441 fan_pwm1_show, fan_pwm1_store);
7443 /* sysfs fan fan1_input ------------------------------------------------ */
7444 static ssize_t fan_fan1_input_show(struct device *dev,
7445 struct device_attribute *attr,
7446 char *buf)
7448 int res;
7449 unsigned int speed;
7451 res = fan_get_speed(&speed);
7452 if (res < 0)
7453 return res;
7455 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7458 static struct device_attribute dev_attr_fan_fan1_input =
7459 __ATTR(fan1_input, S_IRUGO,
7460 fan_fan1_input_show, NULL);
7462 /* sysfs fan fan2_input ------------------------------------------------ */
7463 static ssize_t fan_fan2_input_show(struct device *dev,
7464 struct device_attribute *attr,
7465 char *buf)
7467 int res;
7468 unsigned int speed;
7470 res = fan2_get_speed(&speed);
7471 if (res < 0)
7472 return res;
7474 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7477 static struct device_attribute dev_attr_fan_fan2_input =
7478 __ATTR(fan2_input, S_IRUGO,
7479 fan_fan2_input_show, NULL);
7481 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7482 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7483 char *buf)
7485 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7488 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7489 const char *buf, size_t count)
7491 unsigned long t;
7493 if (parse_strtoul(buf, 120, &t))
7494 return -EINVAL;
7496 if (!fan_control_allowed)
7497 return -EPERM;
7499 fan_watchdog_maxinterval = t;
7500 fan_watchdog_reset();
7502 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7504 return count;
7507 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7508 fan_fan_watchdog_show, fan_fan_watchdog_store);
7510 /* --------------------------------------------------------------------- */
7511 static struct attribute *fan_attributes[] = {
7512 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7513 &dev_attr_fan_fan1_input.attr,
7514 NULL, /* for fan2_input */
7515 NULL
7518 static const struct attribute_group fan_attr_group = {
7519 .attrs = fan_attributes,
7522 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7523 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7525 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7526 { .vendor = PCI_VENDOR_ID_IBM, \
7527 .bios = TPACPI_MATCH_ANY, \
7528 .ec = TPID(__id1, __id2), \
7529 .quirks = __quirks }
7531 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7532 { .vendor = PCI_VENDOR_ID_LENOVO, \
7533 .bios = TPACPI_MATCH_ANY, \
7534 .ec = TPID(__id1, __id2), \
7535 .quirks = __quirks }
7537 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7538 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7539 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7540 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7541 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7542 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7545 #undef TPACPI_FAN_QL
7546 #undef TPACPI_FAN_QI
7548 static int __init fan_init(struct ibm_init_struct *iibm)
7550 int rc;
7551 unsigned long quirks;
7553 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7554 "initializing fan subdriver\n");
7556 mutex_init(&fan_mutex);
7557 fan_status_access_mode = TPACPI_FAN_NONE;
7558 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7559 fan_control_commands = 0;
7560 fan_watchdog_maxinterval = 0;
7561 tp_features.fan_ctrl_status_undef = 0;
7562 tp_features.second_fan = 0;
7563 fan_control_desired_level = 7;
7565 TPACPI_ACPIHANDLE_INIT(fans);
7566 TPACPI_ACPIHANDLE_INIT(gfan);
7567 TPACPI_ACPIHANDLE_INIT(sfan);
7569 quirks = tpacpi_check_quirks(fan_quirk_table,
7570 ARRAY_SIZE(fan_quirk_table));
7572 if (gfan_handle) {
7573 /* 570, 600e/x, 770e, 770x */
7574 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7575 } else {
7576 /* all other ThinkPads: note that even old-style
7577 * ThinkPad ECs supports the fan control register */
7578 if (likely(acpi_ec_read(fan_status_offset,
7579 &fan_control_initial_status))) {
7580 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7581 if (quirks & TPACPI_FAN_Q1)
7582 fan_quirk1_setup();
7583 if (quirks & TPACPI_FAN_2FAN) {
7584 tp_features.second_fan = 1;
7585 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7586 "secondary fan support enabled\n");
7588 } else {
7589 printk(TPACPI_ERR
7590 "ThinkPad ACPI EC access misbehaving, "
7591 "fan status and control unavailable\n");
7592 return 1;
7596 if (sfan_handle) {
7597 /* 570, 770x-JL */
7598 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7599 fan_control_commands |=
7600 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7601 } else {
7602 if (!gfan_handle) {
7603 /* gfan without sfan means no fan control */
7604 /* all other models implement TP EC 0x2f control */
7606 if (fans_handle) {
7607 /* X31, X40, X41 */
7608 fan_control_access_mode =
7609 TPACPI_FAN_WR_ACPI_FANS;
7610 fan_control_commands |=
7611 TPACPI_FAN_CMD_SPEED |
7612 TPACPI_FAN_CMD_LEVEL |
7613 TPACPI_FAN_CMD_ENABLE;
7614 } else {
7615 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7616 fan_control_commands |=
7617 TPACPI_FAN_CMD_LEVEL |
7618 TPACPI_FAN_CMD_ENABLE;
7623 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7624 "fan is %s, modes %d, %d\n",
7625 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7626 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7627 fan_status_access_mode, fan_control_access_mode);
7629 /* fan control master switch */
7630 if (!fan_control_allowed) {
7631 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7632 fan_control_commands = 0;
7633 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7634 "fan control features disabled by parameter\n");
7637 /* update fan_control_desired_level */
7638 if (fan_status_access_mode != TPACPI_FAN_NONE)
7639 fan_get_status_safe(NULL);
7641 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7642 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7643 if (tp_features.second_fan) {
7644 /* attach second fan tachometer */
7645 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7646 &dev_attr_fan_fan2_input.attr;
7648 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7649 &fan_attr_group);
7650 if (rc < 0)
7651 return rc;
7653 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7654 &driver_attr_fan_watchdog);
7655 if (rc < 0) {
7656 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7657 &fan_attr_group);
7658 return rc;
7660 return 0;
7661 } else
7662 return 1;
7665 static void fan_exit(void)
7667 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7668 "cancelling any pending fan watchdog tasks\n");
7670 /* FIXME: can we really do this unconditionally? */
7671 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7672 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7673 &driver_attr_fan_watchdog);
7675 cancel_delayed_work(&fan_watchdog_task);
7676 flush_workqueue(tpacpi_wq);
7679 static void fan_suspend(pm_message_t state)
7681 int rc;
7683 if (!fan_control_allowed)
7684 return;
7686 /* Store fan status in cache */
7687 fan_control_resume_level = 0;
7688 rc = fan_get_status_safe(&fan_control_resume_level);
7689 if (rc < 0)
7690 printk(TPACPI_NOTICE
7691 "failed to read fan level for later "
7692 "restore during resume: %d\n", rc);
7694 /* if it is undefined, don't attempt to restore it.
7695 * KEEP THIS LAST */
7696 if (tp_features.fan_ctrl_status_undef)
7697 fan_control_resume_level = 0;
7700 static void fan_resume(void)
7702 u8 current_level = 7;
7703 bool do_set = false;
7704 int rc;
7706 /* DSDT *always* updates status on resume */
7707 tp_features.fan_ctrl_status_undef = 0;
7709 if (!fan_control_allowed ||
7710 !fan_control_resume_level ||
7711 (fan_get_status_safe(&current_level) < 0))
7712 return;
7714 switch (fan_control_access_mode) {
7715 case TPACPI_FAN_WR_ACPI_SFAN:
7716 /* never decrease fan level */
7717 do_set = (fan_control_resume_level > current_level);
7718 break;
7719 case TPACPI_FAN_WR_ACPI_FANS:
7720 case TPACPI_FAN_WR_TPEC:
7721 /* never decrease fan level, scale is:
7722 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7724 * We expect the firmware to set either 7 or AUTO, but we
7725 * handle FULLSPEED out of paranoia.
7727 * So, we can safely only restore FULLSPEED or 7, anything
7728 * else could slow the fan. Restoring AUTO is useless, at
7729 * best that's exactly what the DSDT already set (it is the
7730 * slower it uses).
7732 * Always keep in mind that the DSDT *will* have set the
7733 * fans to what the vendor supposes is the best level. We
7734 * muck with it only to speed the fan up.
7736 if (fan_control_resume_level != 7 &&
7737 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7738 return;
7739 else
7740 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7741 (current_level != fan_control_resume_level);
7742 break;
7743 default:
7744 return;
7746 if (do_set) {
7747 printk(TPACPI_NOTICE
7748 "restoring fan level to 0x%02x\n",
7749 fan_control_resume_level);
7750 rc = fan_set_level_safe(fan_control_resume_level);
7751 if (rc < 0)
7752 printk(TPACPI_NOTICE
7753 "failed to restore fan level: %d\n", rc);
7757 static int fan_read(char *p)
7759 int len = 0;
7760 int rc;
7761 u8 status;
7762 unsigned int speed = 0;
7764 switch (fan_status_access_mode) {
7765 case TPACPI_FAN_RD_ACPI_GFAN:
7766 /* 570, 600e/x, 770e, 770x */
7767 rc = fan_get_status_safe(&status);
7768 if (rc < 0)
7769 return rc;
7771 len += sprintf(p + len, "status:\t\t%s\n"
7772 "level:\t\t%d\n",
7773 (status != 0) ? "enabled" : "disabled", status);
7774 break;
7776 case TPACPI_FAN_RD_TPEC:
7777 /* all except 570, 600e/x, 770e, 770x */
7778 rc = fan_get_status_safe(&status);
7779 if (rc < 0)
7780 return rc;
7782 len += sprintf(p + len, "status:\t\t%s\n",
7783 (status != 0) ? "enabled" : "disabled");
7785 rc = fan_get_speed(&speed);
7786 if (rc < 0)
7787 return rc;
7789 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7791 if (status & TP_EC_FAN_FULLSPEED)
7792 /* Disengaged mode takes precedence */
7793 len += sprintf(p + len, "level:\t\tdisengaged\n");
7794 else if (status & TP_EC_FAN_AUTO)
7795 len += sprintf(p + len, "level:\t\tauto\n");
7796 else
7797 len += sprintf(p + len, "level:\t\t%d\n", status);
7798 break;
7800 case TPACPI_FAN_NONE:
7801 default:
7802 len += sprintf(p + len, "status:\t\tnot supported\n");
7805 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7806 len += sprintf(p + len, "commands:\tlevel <level>");
7808 switch (fan_control_access_mode) {
7809 case TPACPI_FAN_WR_ACPI_SFAN:
7810 len += sprintf(p + len, " (<level> is 0-7)\n");
7811 break;
7813 default:
7814 len += sprintf(p + len, " (<level> is 0-7, "
7815 "auto, disengaged, full-speed)\n");
7816 break;
7820 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7821 len += sprintf(p + len, "commands:\tenable, disable\n"
7822 "commands:\twatchdog <timeout> (<timeout> "
7823 "is 0 (off), 1-120 (seconds))\n");
7825 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7826 len += sprintf(p + len, "commands:\tspeed <speed>"
7827 " (<speed> is 0-65535)\n");
7829 return len;
7832 static int fan_write_cmd_level(const char *cmd, int *rc)
7834 int level;
7836 if (strlencmp(cmd, "level auto") == 0)
7837 level = TP_EC_FAN_AUTO;
7838 else if ((strlencmp(cmd, "level disengaged") == 0) |
7839 (strlencmp(cmd, "level full-speed") == 0))
7840 level = TP_EC_FAN_FULLSPEED;
7841 else if (sscanf(cmd, "level %d", &level) != 1)
7842 return 0;
7844 *rc = fan_set_level_safe(level);
7845 if (*rc == -ENXIO)
7846 printk(TPACPI_ERR "level command accepted for unsupported "
7847 "access mode %d", fan_control_access_mode);
7848 else if (!*rc)
7849 tpacpi_disclose_usertask("procfs fan",
7850 "set level to %d\n", level);
7852 return 1;
7855 static int fan_write_cmd_enable(const char *cmd, int *rc)
7857 if (strlencmp(cmd, "enable") != 0)
7858 return 0;
7860 *rc = fan_set_enable();
7861 if (*rc == -ENXIO)
7862 printk(TPACPI_ERR "enable command accepted for unsupported "
7863 "access mode %d", fan_control_access_mode);
7864 else if (!*rc)
7865 tpacpi_disclose_usertask("procfs fan", "enable\n");
7867 return 1;
7870 static int fan_write_cmd_disable(const char *cmd, int *rc)
7872 if (strlencmp(cmd, "disable") != 0)
7873 return 0;
7875 *rc = fan_set_disable();
7876 if (*rc == -ENXIO)
7877 printk(TPACPI_ERR "disable command accepted for unsupported "
7878 "access mode %d", fan_control_access_mode);
7879 else if (!*rc)
7880 tpacpi_disclose_usertask("procfs fan", "disable\n");
7882 return 1;
7885 static int fan_write_cmd_speed(const char *cmd, int *rc)
7887 int speed;
7889 /* TODO:
7890 * Support speed <low> <medium> <high> ? */
7892 if (sscanf(cmd, "speed %d", &speed) != 1)
7893 return 0;
7895 *rc = fan_set_speed(speed);
7896 if (*rc == -ENXIO)
7897 printk(TPACPI_ERR "speed command accepted for unsupported "
7898 "access mode %d", fan_control_access_mode);
7899 else if (!*rc)
7900 tpacpi_disclose_usertask("procfs fan",
7901 "set speed to %d\n", speed);
7903 return 1;
7906 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7908 int interval;
7910 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7911 return 0;
7913 if (interval < 0 || interval > 120)
7914 *rc = -EINVAL;
7915 else {
7916 fan_watchdog_maxinterval = interval;
7917 tpacpi_disclose_usertask("procfs fan",
7918 "set watchdog timer to %d\n",
7919 interval);
7922 return 1;
7925 static int fan_write(char *buf)
7927 char *cmd;
7928 int rc = 0;
7930 while (!rc && (cmd = next_cmd(&buf))) {
7931 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7932 fan_write_cmd_level(cmd, &rc)) &&
7933 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7934 (fan_write_cmd_enable(cmd, &rc) ||
7935 fan_write_cmd_disable(cmd, &rc) ||
7936 fan_write_cmd_watchdog(cmd, &rc))) &&
7937 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7938 fan_write_cmd_speed(cmd, &rc))
7940 rc = -EINVAL;
7941 else if (!rc)
7942 fan_watchdog_reset();
7945 return rc;
7948 static struct ibm_struct fan_driver_data = {
7949 .name = "fan",
7950 .read = fan_read,
7951 .write = fan_write,
7952 .exit = fan_exit,
7953 .suspend = fan_suspend,
7954 .resume = fan_resume,
7957 /****************************************************************************
7958 ****************************************************************************
7960 * Infrastructure
7962 ****************************************************************************
7963 ****************************************************************************/
7966 * HKEY event callout for other subdrivers go here
7967 * (yes, it is ugly, but it is quick, safe, and gets the job done
7969 static void tpacpi_driver_event(const unsigned int hkey_event)
7971 if (ibm_backlight_device) {
7972 switch (hkey_event) {
7973 case TP_HKEY_EV_BRGHT_UP:
7974 case TP_HKEY_EV_BRGHT_DOWN:
7975 tpacpi_brightness_notify_change();
7982 static void hotkey_driver_event(const unsigned int scancode)
7984 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
7987 /* sysfs name ---------------------------------------------------------- */
7988 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7989 struct device_attribute *attr,
7990 char *buf)
7992 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7995 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7996 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7998 /* --------------------------------------------------------------------- */
8000 /* /proc support */
8001 static struct proc_dir_entry *proc_dir;
8004 * Module and infrastructure proble, init and exit handling
8007 static int force_load;
8009 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8010 static const char * __init str_supported(int is_supported)
8012 static char text_unsupported[] __initdata = "not supported";
8014 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8016 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8018 static void ibm_exit(struct ibm_struct *ibm)
8020 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8022 list_del_init(&ibm->all_drivers);
8024 if (ibm->flags.acpi_notify_installed) {
8025 dbg_printk(TPACPI_DBG_EXIT,
8026 "%s: acpi_remove_notify_handler\n", ibm->name);
8027 BUG_ON(!ibm->acpi);
8028 acpi_remove_notify_handler(*ibm->acpi->handle,
8029 ibm->acpi->type,
8030 dispatch_acpi_notify);
8031 ibm->flags.acpi_notify_installed = 0;
8032 ibm->flags.acpi_notify_installed = 0;
8035 if (ibm->flags.proc_created) {
8036 dbg_printk(TPACPI_DBG_EXIT,
8037 "%s: remove_proc_entry\n", ibm->name);
8038 remove_proc_entry(ibm->name, proc_dir);
8039 ibm->flags.proc_created = 0;
8042 if (ibm->flags.acpi_driver_registered) {
8043 dbg_printk(TPACPI_DBG_EXIT,
8044 "%s: acpi_bus_unregister_driver\n", ibm->name);
8045 BUG_ON(!ibm->acpi);
8046 acpi_bus_unregister_driver(ibm->acpi->driver);
8047 kfree(ibm->acpi->driver);
8048 ibm->acpi->driver = NULL;
8049 ibm->flags.acpi_driver_registered = 0;
8052 if (ibm->flags.init_called && ibm->exit) {
8053 ibm->exit();
8054 ibm->flags.init_called = 0;
8057 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8060 static int __init ibm_init(struct ibm_init_struct *iibm)
8062 int ret;
8063 struct ibm_struct *ibm = iibm->data;
8064 struct proc_dir_entry *entry;
8066 BUG_ON(ibm == NULL);
8068 INIT_LIST_HEAD(&ibm->all_drivers);
8070 if (ibm->flags.experimental && !experimental)
8071 return 0;
8073 dbg_printk(TPACPI_DBG_INIT,
8074 "probing for %s\n", ibm->name);
8076 if (iibm->init) {
8077 ret = iibm->init(iibm);
8078 if (ret > 0)
8079 return 0; /* probe failed */
8080 if (ret)
8081 return ret;
8083 ibm->flags.init_called = 1;
8086 if (ibm->acpi) {
8087 if (ibm->acpi->hid) {
8088 ret = register_tpacpi_subdriver(ibm);
8089 if (ret)
8090 goto err_out;
8093 if (ibm->acpi->notify) {
8094 ret = setup_acpi_notify(ibm);
8095 if (ret == -ENODEV) {
8096 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8097 ibm->name);
8098 ret = 0;
8099 goto err_out;
8101 if (ret < 0)
8102 goto err_out;
8106 dbg_printk(TPACPI_DBG_INIT,
8107 "%s installed\n", ibm->name);
8109 if (ibm->read) {
8110 entry = create_proc_entry(ibm->name,
8111 S_IFREG | S_IRUGO | S_IWUSR,
8112 proc_dir);
8113 if (!entry) {
8114 printk(TPACPI_ERR "unable to create proc entry %s\n",
8115 ibm->name);
8116 ret = -ENODEV;
8117 goto err_out;
8119 entry->owner = THIS_MODULE;
8120 entry->data = ibm;
8121 entry->read_proc = &dispatch_procfs_read;
8122 if (ibm->write)
8123 entry->write_proc = &dispatch_procfs_write;
8124 ibm->flags.proc_created = 1;
8127 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8129 return 0;
8131 err_out:
8132 dbg_printk(TPACPI_DBG_INIT,
8133 "%s: at error exit path with result %d\n",
8134 ibm->name, ret);
8136 ibm_exit(ibm);
8137 return (ret < 0)? ret : 0;
8140 /* Probing */
8142 static bool __pure __init tpacpi_is_fw_digit(const char c)
8144 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8147 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8148 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8149 const char t)
8151 return s && strlen(s) >= 8 &&
8152 tpacpi_is_fw_digit(s[0]) &&
8153 tpacpi_is_fw_digit(s[1]) &&
8154 s[2] == t && s[3] == 'T' &&
8155 tpacpi_is_fw_digit(s[4]) &&
8156 tpacpi_is_fw_digit(s[5]) &&
8157 s[6] == 'W' && s[7] == 'W';
8160 /* returns 0 - probe ok, or < 0 - probe error.
8161 * Probe ok doesn't mean thinkpad found.
8162 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8163 static int __must_check __init get_thinkpad_model_data(
8164 struct thinkpad_id_data *tp)
8166 const struct dmi_device *dev = NULL;
8167 char ec_fw_string[18];
8168 char const *s;
8170 if (!tp)
8171 return -EINVAL;
8173 memset(tp, 0, sizeof(*tp));
8175 if (dmi_name_in_vendors("IBM"))
8176 tp->vendor = PCI_VENDOR_ID_IBM;
8177 else if (dmi_name_in_vendors("LENOVO"))
8178 tp->vendor = PCI_VENDOR_ID_LENOVO;
8179 else
8180 return 0;
8182 s = dmi_get_system_info(DMI_BIOS_VERSION);
8183 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8184 if (s && !tp->bios_version_str)
8185 return -ENOMEM;
8187 /* Really ancient ThinkPad 240X will fail this, which is fine */
8188 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8189 return 0;
8191 tp->bios_model = tp->bios_version_str[0]
8192 | (tp->bios_version_str[1] << 8);
8193 tp->bios_release = (tp->bios_version_str[4] << 8)
8194 | tp->bios_version_str[5];
8197 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8198 * X32 or newer, all Z series; Some models must have an
8199 * up-to-date BIOS or they will not be detected.
8201 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8203 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8204 if (sscanf(dev->name,
8205 "IBM ThinkPad Embedded Controller -[%17c",
8206 ec_fw_string) == 1) {
8207 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8208 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8210 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8211 if (!tp->ec_version_str)
8212 return -ENOMEM;
8214 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8215 tp->ec_model = ec_fw_string[0]
8216 | (ec_fw_string[1] << 8);
8217 tp->ec_release = (ec_fw_string[4] << 8)
8218 | ec_fw_string[5];
8219 } else {
8220 printk(TPACPI_NOTICE
8221 "ThinkPad firmware release %s "
8222 "doesn't match the known patterns\n",
8223 ec_fw_string);
8224 printk(TPACPI_NOTICE
8225 "please report this to %s\n",
8226 TPACPI_MAIL);
8228 break;
8232 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8233 if (s && !strnicmp(s, "ThinkPad", 8)) {
8234 tp->model_str = kstrdup(s, GFP_KERNEL);
8235 if (!tp->model_str)
8236 return -ENOMEM;
8239 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8240 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8241 if (s && !tp->nummodel_str)
8242 return -ENOMEM;
8244 return 0;
8247 static int __init probe_for_thinkpad(void)
8249 int is_thinkpad;
8251 if (acpi_disabled)
8252 return -ENODEV;
8255 * Non-ancient models have better DMI tagging, but very old models
8256 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8258 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8259 (thinkpad_id.ec_model != 0) ||
8260 tpacpi_is_fw_known();
8262 /* ec is required because many other handles are relative to it */
8263 TPACPI_ACPIHANDLE_INIT(ec);
8264 if (!ec_handle) {
8265 if (is_thinkpad)
8266 printk(TPACPI_ERR
8267 "Not yet supported ThinkPad detected!\n");
8268 return -ENODEV;
8271 if (!is_thinkpad && !force_load)
8272 return -ENODEV;
8274 return 0;
8278 /* Module init, exit, parameters */
8280 static struct ibm_init_struct ibms_init[] __initdata = {
8282 .init = thinkpad_acpi_driver_init,
8283 .data = &thinkpad_acpi_driver_data,
8286 .init = hotkey_init,
8287 .data = &hotkey_driver_data,
8290 .init = bluetooth_init,
8291 .data = &bluetooth_driver_data,
8294 .init = wan_init,
8295 .data = &wan_driver_data,
8298 .init = uwb_init,
8299 .data = &uwb_driver_data,
8301 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8303 .init = video_init,
8304 .data = &video_driver_data,
8306 #endif
8308 .init = light_init,
8309 .data = &light_driver_data,
8312 .init = cmos_init,
8313 .data = &cmos_driver_data,
8316 .init = led_init,
8317 .data = &led_driver_data,
8320 .init = beep_init,
8321 .data = &beep_driver_data,
8324 .init = thermal_init,
8325 .data = &thermal_driver_data,
8328 .data = &ecdump_driver_data,
8331 .init = brightness_init,
8332 .data = &brightness_driver_data,
8335 .init = volume_init,
8336 .data = &volume_driver_data,
8339 .init = fan_init,
8340 .data = &fan_driver_data,
8344 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8346 unsigned int i;
8347 struct ibm_struct *ibm;
8349 if (!kp || !kp->name || !val)
8350 return -EINVAL;
8352 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8353 ibm = ibms_init[i].data;
8354 WARN_ON(ibm == NULL);
8356 if (!ibm || !ibm->name)
8357 continue;
8359 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8360 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8361 return -ENOSPC;
8362 strcpy(ibms_init[i].param, val);
8363 strcat(ibms_init[i].param, ",");
8364 return 0;
8368 return -EINVAL;
8371 module_param(experimental, int, 0444);
8372 MODULE_PARM_DESC(experimental,
8373 "Enables experimental features when non-zero");
8375 module_param_named(debug, dbg_level, uint, 0);
8376 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8378 module_param(force_load, bool, 0444);
8379 MODULE_PARM_DESC(force_load,
8380 "Attempts to load the driver even on a "
8381 "mis-identified ThinkPad when true");
8383 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8384 MODULE_PARM_DESC(fan_control,
8385 "Enables setting fan parameters features when true");
8387 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8388 MODULE_PARM_DESC(brightness_mode,
8389 "Selects brightness control strategy: "
8390 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8392 module_param(brightness_enable, uint, 0444);
8393 MODULE_PARM_DESC(brightness_enable,
8394 "Enables backlight control when 1, disables when 0");
8396 module_param(hotkey_report_mode, uint, 0444);
8397 MODULE_PARM_DESC(hotkey_report_mode,
8398 "used for backwards compatibility with userspace, "
8399 "see documentation");
8401 module_param_named(volume_mode, volume_mode, uint, 0444);
8402 MODULE_PARM_DESC(volume_mode,
8403 "Selects volume control strategy: "
8404 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8406 #define TPACPI_PARAM(feature) \
8407 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8408 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8409 "at module load, see documentation")
8411 TPACPI_PARAM(hotkey);
8412 TPACPI_PARAM(bluetooth);
8413 TPACPI_PARAM(video);
8414 TPACPI_PARAM(light);
8415 TPACPI_PARAM(cmos);
8416 TPACPI_PARAM(led);
8417 TPACPI_PARAM(beep);
8418 TPACPI_PARAM(ecdump);
8419 TPACPI_PARAM(brightness);
8420 TPACPI_PARAM(volume);
8421 TPACPI_PARAM(fan);
8423 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8424 module_param(dbg_wlswemul, uint, 0444);
8425 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8426 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8427 MODULE_PARM_DESC(wlsw_state,
8428 "Initial state of the emulated WLSW switch");
8430 module_param(dbg_bluetoothemul, uint, 0444);
8431 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8432 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8433 MODULE_PARM_DESC(bluetooth_state,
8434 "Initial state of the emulated bluetooth switch");
8436 module_param(dbg_wwanemul, uint, 0444);
8437 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8438 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8439 MODULE_PARM_DESC(wwan_state,
8440 "Initial state of the emulated WWAN switch");
8442 module_param(dbg_uwbemul, uint, 0444);
8443 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8444 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8445 MODULE_PARM_DESC(uwb_state,
8446 "Initial state of the emulated UWB switch");
8447 #endif
8449 static void thinkpad_acpi_module_exit(void)
8451 struct ibm_struct *ibm, *itmp;
8453 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8455 list_for_each_entry_safe_reverse(ibm, itmp,
8456 &tpacpi_all_drivers,
8457 all_drivers) {
8458 ibm_exit(ibm);
8461 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8463 if (tpacpi_inputdev) {
8464 if (tp_features.input_device_registered)
8465 input_unregister_device(tpacpi_inputdev);
8466 else
8467 input_free_device(tpacpi_inputdev);
8470 if (tpacpi_hwmon)
8471 hwmon_device_unregister(tpacpi_hwmon);
8473 if (tp_features.sensors_pdev_attrs_registered)
8474 device_remove_file(&tpacpi_sensors_pdev->dev,
8475 &dev_attr_thinkpad_acpi_pdev_name);
8476 if (tpacpi_sensors_pdev)
8477 platform_device_unregister(tpacpi_sensors_pdev);
8478 if (tpacpi_pdev)
8479 platform_device_unregister(tpacpi_pdev);
8481 if (tp_features.sensors_pdrv_attrs_registered)
8482 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8483 if (tp_features.platform_drv_attrs_registered)
8484 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8486 if (tp_features.sensors_pdrv_registered)
8487 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8489 if (tp_features.platform_drv_registered)
8490 platform_driver_unregister(&tpacpi_pdriver);
8492 if (proc_dir)
8493 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8495 if (tpacpi_wq)
8496 destroy_workqueue(tpacpi_wq);
8498 kfree(thinkpad_id.bios_version_str);
8499 kfree(thinkpad_id.ec_version_str);
8500 kfree(thinkpad_id.model_str);
8504 static int __init thinkpad_acpi_module_init(void)
8506 int ret, i;
8508 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8510 /* Parameter checking */
8511 if (hotkey_report_mode > 2)
8512 return -EINVAL;
8514 /* Driver-level probe */
8516 ret = get_thinkpad_model_data(&thinkpad_id);
8517 if (ret) {
8518 printk(TPACPI_ERR
8519 "unable to get DMI data: %d\n", ret);
8520 thinkpad_acpi_module_exit();
8521 return ret;
8523 ret = probe_for_thinkpad();
8524 if (ret) {
8525 thinkpad_acpi_module_exit();
8526 return ret;
8529 /* Driver initialization */
8531 TPACPI_ACPIHANDLE_INIT(ecrd);
8532 TPACPI_ACPIHANDLE_INIT(ecwr);
8534 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8535 if (!tpacpi_wq) {
8536 thinkpad_acpi_module_exit();
8537 return -ENOMEM;
8540 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8541 if (!proc_dir) {
8542 printk(TPACPI_ERR
8543 "unable to create proc dir " TPACPI_PROC_DIR);
8544 thinkpad_acpi_module_exit();
8545 return -ENODEV;
8547 proc_dir->owner = THIS_MODULE;
8549 ret = platform_driver_register(&tpacpi_pdriver);
8550 if (ret) {
8551 printk(TPACPI_ERR
8552 "unable to register main platform driver\n");
8553 thinkpad_acpi_module_exit();
8554 return ret;
8556 tp_features.platform_drv_registered = 1;
8558 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8559 if (ret) {
8560 printk(TPACPI_ERR
8561 "unable to register hwmon platform driver\n");
8562 thinkpad_acpi_module_exit();
8563 return ret;
8565 tp_features.sensors_pdrv_registered = 1;
8567 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8568 if (!ret) {
8569 tp_features.platform_drv_attrs_registered = 1;
8570 ret = tpacpi_create_driver_attributes(
8571 &tpacpi_hwmon_pdriver.driver);
8573 if (ret) {
8574 printk(TPACPI_ERR
8575 "unable to create sysfs driver attributes\n");
8576 thinkpad_acpi_module_exit();
8577 return ret;
8579 tp_features.sensors_pdrv_attrs_registered = 1;
8582 /* Device initialization */
8583 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8584 NULL, 0);
8585 if (IS_ERR(tpacpi_pdev)) {
8586 ret = PTR_ERR(tpacpi_pdev);
8587 tpacpi_pdev = NULL;
8588 printk(TPACPI_ERR "unable to register platform device\n");
8589 thinkpad_acpi_module_exit();
8590 return ret;
8592 tpacpi_sensors_pdev = platform_device_register_simple(
8593 TPACPI_HWMON_DRVR_NAME,
8594 -1, NULL, 0);
8595 if (IS_ERR(tpacpi_sensors_pdev)) {
8596 ret = PTR_ERR(tpacpi_sensors_pdev);
8597 tpacpi_sensors_pdev = NULL;
8598 printk(TPACPI_ERR
8599 "unable to register hwmon platform device\n");
8600 thinkpad_acpi_module_exit();
8601 return ret;
8603 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8604 &dev_attr_thinkpad_acpi_pdev_name);
8605 if (ret) {
8606 printk(TPACPI_ERR
8607 "unable to create sysfs hwmon device attributes\n");
8608 thinkpad_acpi_module_exit();
8609 return ret;
8611 tp_features.sensors_pdev_attrs_registered = 1;
8612 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8613 if (IS_ERR(tpacpi_hwmon)) {
8614 ret = PTR_ERR(tpacpi_hwmon);
8615 tpacpi_hwmon = NULL;
8616 printk(TPACPI_ERR "unable to register hwmon device\n");
8617 thinkpad_acpi_module_exit();
8618 return ret;
8620 mutex_init(&tpacpi_inputdev_send_mutex);
8621 tpacpi_inputdev = input_allocate_device();
8622 if (!tpacpi_inputdev) {
8623 printk(TPACPI_ERR "unable to allocate input device\n");
8624 thinkpad_acpi_module_exit();
8625 return -ENOMEM;
8626 } else {
8627 /* Prepare input device, but don't register */
8628 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8629 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8630 tpacpi_inputdev->id.bustype = BUS_HOST;
8631 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8632 thinkpad_id.vendor :
8633 PCI_VENDOR_ID_IBM;
8634 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8635 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8636 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8638 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8639 ret = ibm_init(&ibms_init[i]);
8640 if (ret >= 0 && *ibms_init[i].param)
8641 ret = ibms_init[i].data->write(ibms_init[i].param);
8642 if (ret < 0) {
8643 thinkpad_acpi_module_exit();
8644 return ret;
8647 ret = input_register_device(tpacpi_inputdev);
8648 if (ret < 0) {
8649 printk(TPACPI_ERR "unable to register input device\n");
8650 thinkpad_acpi_module_exit();
8651 return ret;
8652 } else {
8653 tp_features.input_device_registered = 1;
8656 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8657 return 0;
8660 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8663 * This will autoload the driver in almost every ThinkPad
8664 * in widespread use.
8666 * Only _VERY_ old models, like the 240, 240x and 570 lack
8667 * the HKEY event interface.
8669 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8672 * DMI matching for module autoloading
8674 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8675 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8677 * Only models listed in thinkwiki will be supported, so add yours
8678 * if it is not there yet.
8680 #define IBM_BIOS_MODULE_ALIAS(__type) \
8681 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8683 /* Ancient thinkpad BIOSes have to be identified by
8684 * BIOS type or model number, and there are far less
8685 * BIOS types than model numbers... */
8686 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8688 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8689 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8690 MODULE_DESCRIPTION(TPACPI_DESC);
8691 MODULE_VERSION(TPACPI_VERSION);
8692 MODULE_LICENSE("GPL");
8694 module_init(thinkpad_acpi_module_init);
8695 module_exit(thinkpad_acpi_module_exit);