thinkpad-acpi: fix incorrect use of TPACPI_BRGHT_MODE_ECNVRAM
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob0acc0c9e962c4cc07d41256a05e3dbc4a44f2378
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 0x020400
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 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
195 #define TPACPI_DBG_BRGHT 0x0020
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
226 void (*shutdown) (void);
228 struct list_head all_drivers;
230 struct tp_acpi_drv_struct *acpi;
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
241 struct ibm_init_struct {
242 char param[32];
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
248 static struct {
249 u32 bluetooth:1;
250 u32 hotkey:1;
251 u32 hotkey_mask:1;
252 u32 hotkey_wlsw:1;
253 u32 hotkey_tablet:1;
254 u32 light:1;
255 u32 light_status:1;
256 u32 bright_16levels:1;
257 u32 bright_acpimode:1;
258 u32 wan:1;
259 u32 uwb:1;
260 u32 fan_ctrl_status_undef:1;
261 u32 second_fan:1;
262 u32 beep_needs_two_args:1;
263 u32 input_device_registered:1;
264 u32 platform_drv_registered:1;
265 u32 platform_drv_attrs_registered:1;
266 u32 sensors_pdrv_registered:1;
267 u32 sensors_pdrv_attrs_registered:1;
268 u32 sensors_pdev_attrs_registered:1;
269 u32 hotkey_poll_active:1;
270 } tp_features;
272 static struct {
273 u16 hotkey_mask_ff:1;
274 } tp_warned;
276 struct thinkpad_id_data {
277 unsigned int vendor; /* ThinkPad vendor:
278 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
280 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
281 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
283 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
284 u16 ec_model;
285 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
286 u16 ec_release;
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
293 static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 enum led_status_t {
305 TPACPI_LED_OFF = 0,
306 TPACPI_LED_ON,
307 TPACPI_LED_BLINK,
310 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev {
312 struct led_classdev led_classdev;
313 struct work_struct work;
314 enum led_status_t new_state;
315 unsigned int led;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul;
320 static int tpacpi_wlsw_emulstate;
321 static int dbg_bluetoothemul;
322 static int tpacpi_bluetooth_emulstate;
323 static int dbg_wwanemul;
324 static int tpacpi_wwan_emulstate;
325 static int dbg_uwbemul;
326 static int tpacpi_uwb_emulstate;
327 #endif
330 /*************************************************************************
331 * Debugging helpers
334 #define dbg_printk(a_dbg_level, format, arg...) \
335 do { if (dbg_level & (a_dbg_level)) \
336 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
337 } while (0)
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 static const char *str_supported(int is_supported);
342 #else
343 #define vdbg_printk(a_dbg_level, format, arg...) \
344 do { } while (0)
345 #endif
347 static void tpacpi_log_usertask(const char * const what)
349 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
350 what, task_tgid_vnr(current));
353 #define tpacpi_disclose_usertask(what, format, arg...) \
354 do { \
355 if (unlikely( \
356 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
357 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
358 printk(TPACPI_DEBUG "%s: PID %d: " format, \
359 what, task_tgid_vnr(current), ## arg); \
361 } while (0)
364 * Quirk handling helpers
366 * ThinkPad IDs and versions seen in the field so far
367 * are two-characters from the set [0-9A-Z], i.e. base 36.
369 * We use values well outside that range as specials.
372 #define TPACPI_MATCH_ANY 0xffffU
373 #define TPACPI_MATCH_UNKNOWN 0U
375 /* TPID('1', 'Y') == 0x5931 */
376 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
378 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
379 { .vendor = PCI_VENDOR_ID_IBM, \
380 .bios = TPID(__id1, __id2), \
381 .ec = TPACPI_MATCH_ANY, \
382 .quirks = (__quirk) }
384 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_LENOVO, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
390 struct tpacpi_quirk {
391 unsigned int vendor;
392 u16 bios;
393 u16 ec;
394 unsigned long quirks;
398 * tpacpi_check_quirks() - search BIOS/EC version on a list
399 * @qlist: array of &struct tpacpi_quirk
400 * @qlist_size: number of elements in @qlist
402 * Iterates over a quirks list until one is found that matches the
403 * ThinkPad's vendor, BIOS and EC model.
405 * Returns 0 if nothing matches, otherwise returns the quirks field of
406 * the matching &struct tpacpi_quirk entry.
408 * The match criteria is: vendor, ec and bios much match.
410 static unsigned long __init tpacpi_check_quirks(
411 const struct tpacpi_quirk *qlist,
412 unsigned int qlist_size)
414 while (qlist_size) {
415 if ((qlist->vendor == thinkpad_id.vendor ||
416 qlist->vendor == TPACPI_MATCH_ANY) &&
417 (qlist->bios == thinkpad_id.bios_model ||
418 qlist->bios == TPACPI_MATCH_ANY) &&
419 (qlist->ec == thinkpad_id.ec_model ||
420 qlist->ec == TPACPI_MATCH_ANY))
421 return qlist->quirks;
423 qlist_size--;
424 qlist++;
426 return 0;
430 /****************************************************************************
431 ****************************************************************************
433 * ACPI Helpers and device model
435 ****************************************************************************
436 ****************************************************************************/
438 /*************************************************************************
439 * ACPI basic handles
442 static acpi_handle root_handle;
444 #define TPACPI_HANDLE(object, parent, paths...) \
445 static acpi_handle object##_handle; \
446 static acpi_handle *object##_parent = &parent##_handle; \
447 static char *object##_path; \
448 static char *object##_paths[] = { paths }
450 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
451 "\\_SB.PCI.ISA.EC", /* 570 */
452 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
453 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
454 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
455 "\\_SB.PCI0.ICH3.EC0", /* R31 */
456 "\\_SB.PCI0.LPC.EC", /* all others */
459 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
460 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
462 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
463 /* T4x, X31, X40 */
464 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
465 "\\CMS", /* R40, R40e */
466 ); /* all others */
468 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
469 "^HKEY", /* R30, R31 */
470 "HKEY", /* all others */
471 ); /* 570 */
473 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
474 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
475 "\\_SB.PCI0.VID0", /* 770e */
476 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
477 "\\_SB.PCI0.AGP.VID", /* all others */
478 ); /* R30, R31 */
481 /*************************************************************************
482 * ACPI helpers
485 static int acpi_evalf(acpi_handle handle,
486 void *res, char *method, char *fmt, ...)
488 char *fmt0 = fmt;
489 struct acpi_object_list params;
490 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
491 struct acpi_buffer result, *resultp;
492 union acpi_object out_obj;
493 acpi_status status;
494 va_list ap;
495 char res_type;
496 int success;
497 int quiet;
499 if (!*fmt) {
500 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
501 return 0;
504 if (*fmt == 'q') {
505 quiet = 1;
506 fmt++;
507 } else
508 quiet = 0;
510 res_type = *(fmt++);
512 params.count = 0;
513 params.pointer = &in_objs[0];
515 va_start(ap, fmt);
516 while (*fmt) {
517 char c = *(fmt++);
518 switch (c) {
519 case 'd': /* int */
520 in_objs[params.count].integer.value = va_arg(ap, int);
521 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
522 break;
523 /* add more types as needed */
524 default:
525 printk(TPACPI_ERR "acpi_evalf() called "
526 "with invalid format character '%c'\n", c);
527 return 0;
530 va_end(ap);
532 if (res_type != 'v') {
533 result.length = sizeof(out_obj);
534 result.pointer = &out_obj;
535 resultp = &result;
536 } else
537 resultp = NULL;
539 status = acpi_evaluate_object(handle, method, &params, resultp);
541 switch (res_type) {
542 case 'd': /* int */
543 if (res)
544 *(int *)res = out_obj.integer.value;
545 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
546 break;
547 case 'v': /* void */
548 success = status == AE_OK;
549 break;
550 /* add more types as needed */
551 default:
552 printk(TPACPI_ERR "acpi_evalf() called "
553 "with invalid format character '%c'\n", res_type);
554 return 0;
557 if (!success && !quiet)
558 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
559 method, fmt0, status);
561 return success;
564 static int acpi_ec_read(int i, u8 *p)
566 int v;
568 if (ecrd_handle) {
569 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
570 return 0;
571 *p = v;
572 } else {
573 if (ec_read(i, p) < 0)
574 return 0;
577 return 1;
580 static int acpi_ec_write(int i, u8 v)
582 if (ecwr_handle) {
583 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
584 return 0;
585 } else {
586 if (ec_write(i, v) < 0)
587 return 0;
590 return 1;
593 static int issue_thinkpad_cmos_command(int cmos_cmd)
595 if (!cmos_handle)
596 return -ENXIO;
598 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
599 return -EIO;
601 return 0;
604 /*************************************************************************
605 * ACPI device model
608 #define TPACPI_ACPIHANDLE_INIT(object) \
609 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
610 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
612 static void drv_acpi_handle_init(char *name,
613 acpi_handle *handle, acpi_handle parent,
614 char **paths, int num_paths, char **path)
616 int i;
617 acpi_status status;
619 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
620 name);
622 for (i = 0; i < num_paths; i++) {
623 status = acpi_get_handle(parent, paths[i], handle);
624 if (ACPI_SUCCESS(status)) {
625 *path = paths[i];
626 dbg_printk(TPACPI_DBG_INIT,
627 "Found ACPI handle %s for %s\n",
628 *path, name);
629 return;
633 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
634 name);
635 *handle = NULL;
638 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
640 struct ibm_struct *ibm = data;
642 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
643 return;
645 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
646 return;
648 ibm->acpi->notify(ibm, event);
651 static int __init setup_acpi_notify(struct ibm_struct *ibm)
653 acpi_status status;
654 int rc;
656 BUG_ON(!ibm->acpi);
658 if (!*ibm->acpi->handle)
659 return 0;
661 vdbg_printk(TPACPI_DBG_INIT,
662 "setting up ACPI notify for %s\n", ibm->name);
664 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
665 if (rc < 0) {
666 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
667 ibm->name, rc);
668 return -ENODEV;
671 ibm->acpi->device->driver_data = ibm;
672 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
673 TPACPI_ACPI_EVENT_PREFIX,
674 ibm->name);
676 status = acpi_install_notify_handler(*ibm->acpi->handle,
677 ibm->acpi->type, dispatch_acpi_notify, ibm);
678 if (ACPI_FAILURE(status)) {
679 if (status == AE_ALREADY_EXISTS) {
680 printk(TPACPI_NOTICE
681 "another device driver is already "
682 "handling %s events\n", ibm->name);
683 } else {
684 printk(TPACPI_ERR
685 "acpi_install_notify_handler(%s) failed: %d\n",
686 ibm->name, status);
688 return -ENODEV;
690 ibm->flags.acpi_notify_installed = 1;
691 return 0;
694 static int __init tpacpi_device_add(struct acpi_device *device)
696 return 0;
699 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
701 int rc;
703 dbg_printk(TPACPI_DBG_INIT,
704 "registering %s as an ACPI driver\n", ibm->name);
706 BUG_ON(!ibm->acpi);
708 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
709 if (!ibm->acpi->driver) {
710 printk(TPACPI_ERR
711 "failed to allocate memory for ibm->acpi->driver\n");
712 return -ENOMEM;
715 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
716 ibm->acpi->driver->ids = ibm->acpi->hid;
718 ibm->acpi->driver->ops.add = &tpacpi_device_add;
720 rc = acpi_bus_register_driver(ibm->acpi->driver);
721 if (rc < 0) {
722 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
723 ibm->name, rc);
724 kfree(ibm->acpi->driver);
725 ibm->acpi->driver = NULL;
726 } else if (!rc)
727 ibm->flags.acpi_driver_registered = 1;
729 return rc;
733 /****************************************************************************
734 ****************************************************************************
736 * Procfs Helpers
738 ****************************************************************************
739 ****************************************************************************/
741 static int dispatch_procfs_read(char *page, char **start, off_t off,
742 int count, int *eof, void *data)
744 struct ibm_struct *ibm = data;
745 int len;
747 if (!ibm || !ibm->read)
748 return -EINVAL;
750 len = ibm->read(page);
751 if (len < 0)
752 return len;
754 if (len <= off + count)
755 *eof = 1;
756 *start = page + off;
757 len -= off;
758 if (len > count)
759 len = count;
760 if (len < 0)
761 len = 0;
763 return len;
766 static int dispatch_procfs_write(struct file *file,
767 const char __user *userbuf,
768 unsigned long count, void *data)
770 struct ibm_struct *ibm = data;
771 char *kernbuf;
772 int ret;
774 if (!ibm || !ibm->write)
775 return -EINVAL;
776 if (count > PAGE_SIZE - 2)
777 return -EINVAL;
779 kernbuf = kmalloc(count + 2, GFP_KERNEL);
780 if (!kernbuf)
781 return -ENOMEM;
783 if (copy_from_user(kernbuf, userbuf, count)) {
784 kfree(kernbuf);
785 return -EFAULT;
788 kernbuf[count] = 0;
789 strcat(kernbuf, ",");
790 ret = ibm->write(kernbuf);
791 if (ret == 0)
792 ret = count;
794 kfree(kernbuf);
796 return ret;
799 static char *next_cmd(char **cmds)
801 char *start = *cmds;
802 char *end;
804 while ((end = strchr(start, ',')) && end == start)
805 start = end + 1;
807 if (!end)
808 return NULL;
810 *end = 0;
811 *cmds = end + 1;
812 return start;
816 /****************************************************************************
817 ****************************************************************************
819 * Device model: input, hwmon and platform
821 ****************************************************************************
822 ****************************************************************************/
824 static struct platform_device *tpacpi_pdev;
825 static struct platform_device *tpacpi_sensors_pdev;
826 static struct device *tpacpi_hwmon;
827 static struct input_dev *tpacpi_inputdev;
828 static struct mutex tpacpi_inputdev_send_mutex;
829 static LIST_HEAD(tpacpi_all_drivers);
831 static int tpacpi_suspend_handler(struct platform_device *pdev,
832 pm_message_t state)
834 struct ibm_struct *ibm, *itmp;
836 list_for_each_entry_safe(ibm, itmp,
837 &tpacpi_all_drivers,
838 all_drivers) {
839 if (ibm->suspend)
840 (ibm->suspend)(state);
843 return 0;
846 static int tpacpi_resume_handler(struct platform_device *pdev)
848 struct ibm_struct *ibm, *itmp;
850 list_for_each_entry_safe(ibm, itmp,
851 &tpacpi_all_drivers,
852 all_drivers) {
853 if (ibm->resume)
854 (ibm->resume)();
857 return 0;
860 static void tpacpi_shutdown_handler(struct platform_device *pdev)
862 struct ibm_struct *ibm, *itmp;
864 list_for_each_entry_safe(ibm, itmp,
865 &tpacpi_all_drivers,
866 all_drivers) {
867 if (ibm->shutdown)
868 (ibm->shutdown)();
872 static struct platform_driver tpacpi_pdriver = {
873 .driver = {
874 .name = TPACPI_DRVR_NAME,
875 .owner = THIS_MODULE,
877 .suspend = tpacpi_suspend_handler,
878 .resume = tpacpi_resume_handler,
879 .shutdown = tpacpi_shutdown_handler,
882 static struct platform_driver tpacpi_hwmon_pdriver = {
883 .driver = {
884 .name = TPACPI_HWMON_DRVR_NAME,
885 .owner = THIS_MODULE,
889 /*************************************************************************
890 * sysfs support helpers
893 struct attribute_set {
894 unsigned int members, max_members;
895 struct attribute_group group;
898 struct attribute_set_obj {
899 struct attribute_set s;
900 struct attribute *a;
901 } __attribute__((packed));
903 static struct attribute_set *create_attr_set(unsigned int max_members,
904 const char *name)
906 struct attribute_set_obj *sobj;
908 if (max_members == 0)
909 return NULL;
911 /* Allocates space for implicit NULL at the end too */
912 sobj = kzalloc(sizeof(struct attribute_set_obj) +
913 max_members * sizeof(struct attribute *),
914 GFP_KERNEL);
915 if (!sobj)
916 return NULL;
917 sobj->s.max_members = max_members;
918 sobj->s.group.attrs = &sobj->a;
919 sobj->s.group.name = name;
921 return &sobj->s;
924 #define destroy_attr_set(_set) \
925 kfree(_set);
927 /* not multi-threaded safe, use it in a single thread per set */
928 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
930 if (!s || !attr)
931 return -EINVAL;
933 if (s->members >= s->max_members)
934 return -ENOMEM;
936 s->group.attrs[s->members] = attr;
937 s->members++;
939 return 0;
942 static int add_many_to_attr_set(struct attribute_set *s,
943 struct attribute **attr,
944 unsigned int count)
946 int i, res;
948 for (i = 0; i < count; i++) {
949 res = add_to_attr_set(s, attr[i]);
950 if (res)
951 return res;
954 return 0;
957 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
959 sysfs_remove_group(kobj, &s->group);
960 destroy_attr_set(s);
963 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
964 sysfs_create_group(_kobj, &_attr_set->group)
966 static int parse_strtoul(const char *buf,
967 unsigned long max, unsigned long *value)
969 char *endp;
971 while (*buf && isspace(*buf))
972 buf++;
973 *value = simple_strtoul(buf, &endp, 0);
974 while (*endp && isspace(*endp))
975 endp++;
976 if (*endp || *value > max)
977 return -EINVAL;
979 return 0;
982 static void tpacpi_disable_brightness_delay(void)
984 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
985 printk(TPACPI_NOTICE
986 "ACPI backlight control delay disabled\n");
989 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
991 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
992 union acpi_object *obj;
993 int rc;
995 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
996 obj = (union acpi_object *)buffer.pointer;
997 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
998 printk(TPACPI_ERR "Unknown _BCL data, "
999 "please report this to %s\n", TPACPI_MAIL);
1000 rc = 0;
1001 } else {
1002 rc = obj->package.count;
1004 } else {
1005 return 0;
1008 kfree(buffer.pointer);
1009 return rc;
1012 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1013 u32 lvl, void *context, void **rv)
1015 char name[ACPI_PATH_SEGMENT_LENGTH];
1016 struct acpi_buffer buffer = { sizeof(name), &name };
1018 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1019 !strncmp("_BCL", name, sizeof(name) - 1)) {
1020 BUG_ON(!rv || !*rv);
1021 **(int **)rv = tpacpi_query_bcl_levels(handle);
1022 return AE_CTRL_TERMINATE;
1023 } else {
1024 return AE_OK;
1029 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1031 static int __init tpacpi_check_std_acpi_brightness_support(void)
1033 int status;
1034 int bcl_levels = 0;
1035 void *bcl_ptr = &bcl_levels;
1037 if (!vid_handle) {
1038 TPACPI_ACPIHANDLE_INIT(vid);
1040 if (!vid_handle)
1041 return 0;
1044 * Search for a _BCL method, and execute it. This is safe on all
1045 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1046 * BIOS in ACPI backlight control mode. We do NOT have to care
1047 * about calling the _BCL method in an enabled video device, any
1048 * will do for our purposes.
1051 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1052 tpacpi_acpi_walk_find_bcl, NULL,
1053 &bcl_ptr);
1055 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1056 tp_features.bright_acpimode = 1;
1057 return (bcl_levels - 2);
1060 return 0;
1063 static int __init tpacpi_new_rfkill(const unsigned int id,
1064 struct rfkill **rfk,
1065 const enum rfkill_type rfktype,
1066 const char *name,
1067 const bool set_default,
1068 int (*toggle_radio)(void *, enum rfkill_state),
1069 int (*get_state)(void *, enum rfkill_state *))
1071 int res;
1072 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1074 res = get_state(NULL, &initial_state);
1075 if (res < 0) {
1076 printk(TPACPI_ERR
1077 "failed to read initial state for %s, error %d; "
1078 "will turn radio off\n", name, res);
1079 } else if (set_default) {
1080 /* try to set the initial state as the default for the rfkill
1081 * type, since we ask the firmware to preserve it across S5 in
1082 * NVRAM */
1083 if (rfkill_set_default(rfktype,
1084 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1085 RFKILL_STATE_UNBLOCKED :
1086 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1087 vdbg_printk(TPACPI_DBG_RFKILL,
1088 "Default state for %s cannot be changed\n",
1089 name);
1092 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1093 if (!*rfk) {
1094 printk(TPACPI_ERR
1095 "failed to allocate memory for rfkill class\n");
1096 return -ENOMEM;
1099 (*rfk)->name = name;
1100 (*rfk)->get_state = get_state;
1101 (*rfk)->toggle_radio = toggle_radio;
1102 (*rfk)->state = initial_state;
1104 res = rfkill_register(*rfk);
1105 if (res < 0) {
1106 printk(TPACPI_ERR
1107 "failed to register %s rfkill switch: %d\n",
1108 name, res);
1109 rfkill_free(*rfk);
1110 *rfk = NULL;
1111 return res;
1114 return 0;
1117 static void printk_deprecated_attribute(const char * const what,
1118 const char * const details)
1120 tpacpi_log_usertask("deprecated sysfs attribute");
1121 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1122 "will be removed. %s\n",
1123 what, details);
1126 static void printk_deprecated_rfkill_attribute(const char * const what)
1128 printk_deprecated_attribute(what,
1129 "Please switch to generic rfkill before year 2010");
1132 /*************************************************************************
1133 * thinkpad-acpi driver attributes
1136 /* interface_version --------------------------------------------------- */
1137 static ssize_t tpacpi_driver_interface_version_show(
1138 struct device_driver *drv,
1139 char *buf)
1141 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1144 static DRIVER_ATTR(interface_version, S_IRUGO,
1145 tpacpi_driver_interface_version_show, NULL);
1147 /* debug_level --------------------------------------------------------- */
1148 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1149 char *buf)
1151 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1154 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1155 const char *buf, size_t count)
1157 unsigned long t;
1159 if (parse_strtoul(buf, 0xffff, &t))
1160 return -EINVAL;
1162 dbg_level = t;
1164 return count;
1167 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1168 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1170 /* version ------------------------------------------------------------- */
1171 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1172 char *buf)
1174 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1175 TPACPI_DESC, TPACPI_VERSION);
1178 static DRIVER_ATTR(version, S_IRUGO,
1179 tpacpi_driver_version_show, NULL);
1181 /* --------------------------------------------------------------------- */
1183 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1185 static void tpacpi_send_radiosw_update(void);
1187 /* wlsw_emulstate ------------------------------------------------------ */
1188 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1189 char *buf)
1191 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1194 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1195 const char *buf, size_t count)
1197 unsigned long t;
1199 if (parse_strtoul(buf, 1, &t))
1200 return -EINVAL;
1202 if (tpacpi_wlsw_emulstate != t) {
1203 tpacpi_wlsw_emulstate = !!t;
1204 tpacpi_send_radiosw_update();
1205 } else
1206 tpacpi_wlsw_emulstate = !!t;
1208 return count;
1211 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1212 tpacpi_driver_wlsw_emulstate_show,
1213 tpacpi_driver_wlsw_emulstate_store);
1215 /* bluetooth_emulstate ------------------------------------------------- */
1216 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1217 struct device_driver *drv,
1218 char *buf)
1220 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1223 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1224 struct device_driver *drv,
1225 const char *buf, size_t count)
1227 unsigned long t;
1229 if (parse_strtoul(buf, 1, &t))
1230 return -EINVAL;
1232 tpacpi_bluetooth_emulstate = !!t;
1234 return count;
1237 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1238 tpacpi_driver_bluetooth_emulstate_show,
1239 tpacpi_driver_bluetooth_emulstate_store);
1241 /* wwan_emulstate ------------------------------------------------- */
1242 static ssize_t tpacpi_driver_wwan_emulstate_show(
1243 struct device_driver *drv,
1244 char *buf)
1246 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1249 static ssize_t tpacpi_driver_wwan_emulstate_store(
1250 struct device_driver *drv,
1251 const char *buf, size_t count)
1253 unsigned long t;
1255 if (parse_strtoul(buf, 1, &t))
1256 return -EINVAL;
1258 tpacpi_wwan_emulstate = !!t;
1260 return count;
1263 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1264 tpacpi_driver_wwan_emulstate_show,
1265 tpacpi_driver_wwan_emulstate_store);
1267 /* uwb_emulstate ------------------------------------------------- */
1268 static ssize_t tpacpi_driver_uwb_emulstate_show(
1269 struct device_driver *drv,
1270 char *buf)
1272 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1275 static ssize_t tpacpi_driver_uwb_emulstate_store(
1276 struct device_driver *drv,
1277 const char *buf, size_t count)
1279 unsigned long t;
1281 if (parse_strtoul(buf, 1, &t))
1282 return -EINVAL;
1284 tpacpi_uwb_emulstate = !!t;
1286 return count;
1289 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1290 tpacpi_driver_uwb_emulstate_show,
1291 tpacpi_driver_uwb_emulstate_store);
1292 #endif
1294 /* --------------------------------------------------------------------- */
1296 static struct driver_attribute *tpacpi_driver_attributes[] = {
1297 &driver_attr_debug_level, &driver_attr_version,
1298 &driver_attr_interface_version,
1301 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1303 int i, res;
1305 i = 0;
1306 res = 0;
1307 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1308 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1309 i++;
1312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1313 if (!res && dbg_wlswemul)
1314 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1315 if (!res && dbg_bluetoothemul)
1316 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1317 if (!res && dbg_wwanemul)
1318 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1319 if (!res && dbg_uwbemul)
1320 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1321 #endif
1323 return res;
1326 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1328 int i;
1330 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1331 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1333 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1334 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1335 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1336 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1337 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1338 #endif
1341 /****************************************************************************
1342 ****************************************************************************
1344 * Subdrivers
1346 ****************************************************************************
1347 ****************************************************************************/
1349 /*************************************************************************
1350 * thinkpad-acpi init subdriver
1353 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1355 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1356 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1358 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1359 (thinkpad_id.bios_version_str) ?
1360 thinkpad_id.bios_version_str : "unknown",
1361 (thinkpad_id.ec_version_str) ?
1362 thinkpad_id.ec_version_str : "unknown");
1364 if (thinkpad_id.vendor && thinkpad_id.model_str)
1365 printk(TPACPI_INFO "%s %s, model %s\n",
1366 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1367 "IBM" : ((thinkpad_id.vendor ==
1368 PCI_VENDOR_ID_LENOVO) ?
1369 "Lenovo" : "Unknown vendor"),
1370 thinkpad_id.model_str,
1371 (thinkpad_id.nummodel_str) ?
1372 thinkpad_id.nummodel_str : "unknown");
1374 return 0;
1377 static int thinkpad_acpi_driver_read(char *p)
1379 int len = 0;
1381 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1382 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1384 return len;
1387 static struct ibm_struct thinkpad_acpi_driver_data = {
1388 .name = "driver",
1389 .read = thinkpad_acpi_driver_read,
1392 /*************************************************************************
1393 * Hotkey subdriver
1396 enum { /* hot key scan codes (derived from ACPI DSDT) */
1397 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1398 TP_ACPI_HOTKEYSCAN_FNF2,
1399 TP_ACPI_HOTKEYSCAN_FNF3,
1400 TP_ACPI_HOTKEYSCAN_FNF4,
1401 TP_ACPI_HOTKEYSCAN_FNF5,
1402 TP_ACPI_HOTKEYSCAN_FNF6,
1403 TP_ACPI_HOTKEYSCAN_FNF7,
1404 TP_ACPI_HOTKEYSCAN_FNF8,
1405 TP_ACPI_HOTKEYSCAN_FNF9,
1406 TP_ACPI_HOTKEYSCAN_FNF10,
1407 TP_ACPI_HOTKEYSCAN_FNF11,
1408 TP_ACPI_HOTKEYSCAN_FNF12,
1409 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1410 TP_ACPI_HOTKEYSCAN_FNINSERT,
1411 TP_ACPI_HOTKEYSCAN_FNDELETE,
1412 TP_ACPI_HOTKEYSCAN_FNHOME,
1413 TP_ACPI_HOTKEYSCAN_FNEND,
1414 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1415 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1416 TP_ACPI_HOTKEYSCAN_FNSPACE,
1417 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1418 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1419 TP_ACPI_HOTKEYSCAN_MUTE,
1420 TP_ACPI_HOTKEYSCAN_THINKPAD,
1423 enum { /* Keys available through NVRAM polling */
1424 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1425 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1428 enum { /* Positions of some of the keys in hotkey masks */
1429 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1430 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1431 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1432 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1433 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1434 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1435 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1436 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1437 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1438 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1439 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1442 enum { /* NVRAM to ACPI HKEY group map */
1443 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1444 TP_ACPI_HKEY_ZOOM_MASK |
1445 TP_ACPI_HKEY_DISPSWTCH_MASK |
1446 TP_ACPI_HKEY_HIBERNATE_MASK,
1447 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1448 TP_ACPI_HKEY_BRGHTDWN_MASK,
1449 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1450 TP_ACPI_HKEY_VOLDWN_MASK |
1451 TP_ACPI_HKEY_MUTE_MASK,
1454 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1455 struct tp_nvram_state {
1456 u16 thinkpad_toggle:1;
1457 u16 zoom_toggle:1;
1458 u16 display_toggle:1;
1459 u16 thinklight_toggle:1;
1460 u16 hibernate_toggle:1;
1461 u16 displayexp_toggle:1;
1462 u16 display_state:1;
1463 u16 brightness_toggle:1;
1464 u16 volume_toggle:1;
1465 u16 mute:1;
1467 u8 brightness_level;
1468 u8 volume_level;
1471 static struct task_struct *tpacpi_hotkey_task;
1472 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1473 static int hotkey_poll_freq = 10; /* Hz */
1474 static struct mutex hotkey_thread_mutex;
1475 static struct mutex hotkey_thread_data_mutex;
1476 static unsigned int hotkey_config_change;
1478 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1480 #define hotkey_source_mask 0U
1482 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1484 static struct mutex hotkey_mutex;
1486 static enum { /* Reasons for waking up */
1487 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1488 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1489 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1490 } hotkey_wakeup_reason;
1492 static int hotkey_autosleep_ack;
1494 static u32 hotkey_orig_mask;
1495 static u32 hotkey_all_mask;
1496 static u32 hotkey_reserved_mask;
1497 static u32 hotkey_mask;
1499 static unsigned int hotkey_report_mode;
1501 static u16 *hotkey_keycode_map;
1503 static struct attribute_set *hotkey_dev_attributes;
1505 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1506 #define HOTKEY_CONFIG_CRITICAL_START \
1507 do { \
1508 mutex_lock(&hotkey_thread_data_mutex); \
1509 hotkey_config_change++; \
1510 } while (0);
1511 #define HOTKEY_CONFIG_CRITICAL_END \
1512 mutex_unlock(&hotkey_thread_data_mutex);
1513 #else
1514 #define HOTKEY_CONFIG_CRITICAL_START
1515 #define HOTKEY_CONFIG_CRITICAL_END
1516 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1518 /* HKEY.MHKG() return bits */
1519 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1521 static int hotkey_get_wlsw(int *status)
1523 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1524 if (dbg_wlswemul) {
1525 *status = !!tpacpi_wlsw_emulstate;
1526 return 0;
1528 #endif
1529 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1530 return -EIO;
1531 return 0;
1534 static int hotkey_get_tablet_mode(int *status)
1536 int s;
1538 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1539 return -EIO;
1541 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1542 return 0;
1546 * Call with hotkey_mutex held
1548 static int hotkey_mask_get(void)
1550 u32 m = 0;
1552 if (tp_features.hotkey_mask) {
1553 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1554 return -EIO;
1556 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1558 return 0;
1562 * Call with hotkey_mutex held
1564 static int hotkey_mask_set(u32 mask)
1566 int i;
1567 int rc = 0;
1569 if (tp_features.hotkey_mask) {
1570 if (!tp_warned.hotkey_mask_ff &&
1571 (mask == 0xffff || mask == 0xffffff ||
1572 mask == 0xffffffff)) {
1573 tp_warned.hotkey_mask_ff = 1;
1574 printk(TPACPI_NOTICE
1575 "setting the hotkey mask to 0x%08x is likely "
1576 "not the best way to go about it\n", mask);
1577 printk(TPACPI_NOTICE
1578 "please consider using the driver defaults, "
1579 "and refer to up-to-date thinkpad-acpi "
1580 "documentation\n");
1583 HOTKEY_CONFIG_CRITICAL_START
1584 for (i = 0; i < 32; i++) {
1585 u32 m = 1 << i;
1586 /* enable in firmware mask only keys not in NVRAM
1587 * mode, but enable the key in the cached hotkey_mask
1588 * regardless of mode, or the key will end up
1589 * disabled by hotkey_mask_get() */
1590 if (!acpi_evalf(hkey_handle,
1591 NULL, "MHKM", "vdd", i + 1,
1592 !!((mask & ~hotkey_source_mask) & m))) {
1593 rc = -EIO;
1594 break;
1595 } else {
1596 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1599 HOTKEY_CONFIG_CRITICAL_END
1601 /* hotkey_mask_get must be called unconditionally below */
1602 if (!hotkey_mask_get() && !rc &&
1603 (hotkey_mask & ~hotkey_source_mask) !=
1604 (mask & ~hotkey_source_mask)) {
1605 printk(TPACPI_NOTICE
1606 "requested hot key mask 0x%08x, but "
1607 "firmware forced it to 0x%08x\n",
1608 mask, hotkey_mask);
1610 } else {
1611 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1612 HOTKEY_CONFIG_CRITICAL_START
1613 hotkey_mask = mask & hotkey_source_mask;
1614 HOTKEY_CONFIG_CRITICAL_END
1615 hotkey_mask_get();
1616 if (hotkey_mask != mask) {
1617 printk(TPACPI_NOTICE
1618 "requested hot key mask 0x%08x, "
1619 "forced to 0x%08x (NVRAM poll mask is "
1620 "0x%08x): no firmware mask support\n",
1621 mask, hotkey_mask, hotkey_source_mask);
1623 #else
1624 hotkey_mask_get();
1625 rc = -ENXIO;
1626 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1629 return rc;
1632 static int hotkey_status_get(int *status)
1634 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1635 return -EIO;
1637 return 0;
1640 static int hotkey_status_set(bool enable)
1642 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1643 return -EIO;
1645 return 0;
1648 static void tpacpi_input_send_tabletsw(void)
1650 int state;
1652 if (tp_features.hotkey_tablet &&
1653 !hotkey_get_tablet_mode(&state)) {
1654 mutex_lock(&tpacpi_inputdev_send_mutex);
1656 input_report_switch(tpacpi_inputdev,
1657 SW_TABLET_MODE, !!state);
1658 input_sync(tpacpi_inputdev);
1660 mutex_unlock(&tpacpi_inputdev_send_mutex);
1664 static void tpacpi_input_send_key(unsigned int scancode)
1666 unsigned int keycode;
1668 keycode = hotkey_keycode_map[scancode];
1670 if (keycode != KEY_RESERVED) {
1671 mutex_lock(&tpacpi_inputdev_send_mutex);
1673 input_report_key(tpacpi_inputdev, keycode, 1);
1674 if (keycode == KEY_UNKNOWN)
1675 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1676 scancode);
1677 input_sync(tpacpi_inputdev);
1679 input_report_key(tpacpi_inputdev, keycode, 0);
1680 if (keycode == KEY_UNKNOWN)
1681 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1682 scancode);
1683 input_sync(tpacpi_inputdev);
1685 mutex_unlock(&tpacpi_inputdev_send_mutex);
1689 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1690 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1692 static void tpacpi_hotkey_send_key(unsigned int scancode)
1694 tpacpi_input_send_key(scancode);
1695 if (hotkey_report_mode < 2) {
1696 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1697 0x80, 0x1001 + scancode);
1701 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1703 u8 d;
1705 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1706 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1707 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1708 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1709 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1710 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1712 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1713 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1714 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1716 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1717 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1718 n->displayexp_toggle =
1719 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1721 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1722 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1723 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1724 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1725 n->brightness_toggle =
1726 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1728 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1729 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1730 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1731 >> TP_NVRAM_POS_LEVEL_VOLUME;
1732 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1733 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1737 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1738 do { \
1739 if ((mask & (1 << __scancode)) && \
1740 oldn->__member != newn->__member) \
1741 tpacpi_hotkey_send_key(__scancode); \
1742 } while (0)
1744 #define TPACPI_MAY_SEND_KEY(__scancode) \
1745 do { if (mask & (1 << __scancode)) \
1746 tpacpi_hotkey_send_key(__scancode); } while (0)
1748 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1749 struct tp_nvram_state *newn,
1750 u32 mask)
1752 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1753 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1754 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1755 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1757 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1759 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1761 /* handle volume */
1762 if (oldn->volume_toggle != newn->volume_toggle) {
1763 if (oldn->mute != newn->mute) {
1764 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1766 if (oldn->volume_level > newn->volume_level) {
1767 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1768 } else if (oldn->volume_level < newn->volume_level) {
1769 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1770 } else if (oldn->mute == newn->mute) {
1771 /* repeated key presses that didn't change state */
1772 if (newn->mute) {
1773 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1774 } else if (newn->volume_level != 0) {
1775 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1776 } else {
1777 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1782 /* handle brightness */
1783 if (oldn->brightness_toggle != newn->brightness_toggle) {
1784 if (oldn->brightness_level < newn->brightness_level) {
1785 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1786 } else if (oldn->brightness_level > newn->brightness_level) {
1787 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1788 } else {
1789 /* repeated key presses that didn't change state */
1790 if (newn->brightness_level != 0) {
1791 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1792 } else {
1793 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1799 #undef TPACPI_COMPARE_KEY
1800 #undef TPACPI_MAY_SEND_KEY
1802 static int hotkey_kthread(void *data)
1804 struct tp_nvram_state s[2];
1805 u32 mask;
1806 unsigned int si, so;
1807 unsigned long t;
1808 unsigned int change_detector, must_reset;
1810 mutex_lock(&hotkey_thread_mutex);
1812 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1813 goto exit;
1815 set_freezable();
1817 so = 0;
1818 si = 1;
1819 t = 0;
1821 /* Initial state for compares */
1822 mutex_lock(&hotkey_thread_data_mutex);
1823 change_detector = hotkey_config_change;
1824 mask = hotkey_source_mask & hotkey_mask;
1825 mutex_unlock(&hotkey_thread_data_mutex);
1826 hotkey_read_nvram(&s[so], mask);
1828 while (!kthread_should_stop() && hotkey_poll_freq) {
1829 if (t == 0)
1830 t = 1000/hotkey_poll_freq;
1831 t = msleep_interruptible(t);
1832 if (unlikely(kthread_should_stop()))
1833 break;
1834 must_reset = try_to_freeze();
1835 if (t > 0 && !must_reset)
1836 continue;
1838 mutex_lock(&hotkey_thread_data_mutex);
1839 if (must_reset || hotkey_config_change != change_detector) {
1840 /* forget old state on thaw or config change */
1841 si = so;
1842 t = 0;
1843 change_detector = hotkey_config_change;
1845 mask = hotkey_source_mask & hotkey_mask;
1846 mutex_unlock(&hotkey_thread_data_mutex);
1848 if (likely(mask)) {
1849 hotkey_read_nvram(&s[si], mask);
1850 if (likely(si != so)) {
1851 hotkey_compare_and_issue_event(&s[so], &s[si],
1852 mask);
1856 so = si;
1857 si ^= 1;
1860 exit:
1861 mutex_unlock(&hotkey_thread_mutex);
1862 return 0;
1865 static void hotkey_poll_stop_sync(void)
1867 if (tpacpi_hotkey_task) {
1868 if (frozen(tpacpi_hotkey_task) ||
1869 freezing(tpacpi_hotkey_task))
1870 thaw_process(tpacpi_hotkey_task);
1872 kthread_stop(tpacpi_hotkey_task);
1873 tpacpi_hotkey_task = NULL;
1874 mutex_lock(&hotkey_thread_mutex);
1875 /* at this point, the thread did exit */
1876 mutex_unlock(&hotkey_thread_mutex);
1880 /* call with hotkey_mutex held */
1881 static void hotkey_poll_setup(int may_warn)
1883 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1884 hotkey_poll_freq > 0 &&
1885 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1886 if (!tpacpi_hotkey_task) {
1887 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1888 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1889 if (IS_ERR(tpacpi_hotkey_task)) {
1890 tpacpi_hotkey_task = NULL;
1891 printk(TPACPI_ERR
1892 "could not create kernel thread "
1893 "for hotkey polling\n");
1896 } else {
1897 hotkey_poll_stop_sync();
1898 if (may_warn &&
1899 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1900 printk(TPACPI_NOTICE
1901 "hot keys 0x%08x require polling, "
1902 "which is currently disabled\n",
1903 hotkey_source_mask);
1908 static void hotkey_poll_setup_safe(int may_warn)
1910 mutex_lock(&hotkey_mutex);
1911 hotkey_poll_setup(may_warn);
1912 mutex_unlock(&hotkey_mutex);
1915 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1917 static void hotkey_poll_setup_safe(int __unused)
1921 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1923 static int hotkey_inputdev_open(struct input_dev *dev)
1925 switch (tpacpi_lifecycle) {
1926 case TPACPI_LIFE_INIT:
1928 * hotkey_init will call hotkey_poll_setup_safe
1929 * at the appropriate moment
1931 return 0;
1932 case TPACPI_LIFE_EXITING:
1933 return -EBUSY;
1934 case TPACPI_LIFE_RUNNING:
1935 hotkey_poll_setup_safe(0);
1936 return 0;
1939 /* Should only happen if tpacpi_lifecycle is corrupt */
1940 BUG();
1941 return -EBUSY;
1944 static void hotkey_inputdev_close(struct input_dev *dev)
1946 /* disable hotkey polling when possible */
1947 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1948 hotkey_poll_setup_safe(0);
1951 /* sysfs hotkey enable ------------------------------------------------- */
1952 static ssize_t hotkey_enable_show(struct device *dev,
1953 struct device_attribute *attr,
1954 char *buf)
1956 int res, status;
1958 printk_deprecated_attribute("hotkey_enable",
1959 "Hotkey reporting is always enabled");
1961 res = hotkey_status_get(&status);
1962 if (res)
1963 return res;
1965 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1968 static ssize_t hotkey_enable_store(struct device *dev,
1969 struct device_attribute *attr,
1970 const char *buf, size_t count)
1972 unsigned long t;
1974 printk_deprecated_attribute("hotkey_enable",
1975 "Hotkeys can be disabled through hotkey_mask");
1977 if (parse_strtoul(buf, 1, &t))
1978 return -EINVAL;
1980 if (t == 0)
1981 return -EPERM;
1983 return count;
1986 static struct device_attribute dev_attr_hotkey_enable =
1987 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1988 hotkey_enable_show, hotkey_enable_store);
1990 /* sysfs hotkey mask --------------------------------------------------- */
1991 static ssize_t hotkey_mask_show(struct device *dev,
1992 struct device_attribute *attr,
1993 char *buf)
1995 int res;
1997 if (mutex_lock_killable(&hotkey_mutex))
1998 return -ERESTARTSYS;
1999 res = hotkey_mask_get();
2000 mutex_unlock(&hotkey_mutex);
2002 return (res)?
2003 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2006 static ssize_t hotkey_mask_store(struct device *dev,
2007 struct device_attribute *attr,
2008 const char *buf, size_t count)
2010 unsigned long t;
2011 int res;
2013 if (parse_strtoul(buf, 0xffffffffUL, &t))
2014 return -EINVAL;
2016 if (mutex_lock_killable(&hotkey_mutex))
2017 return -ERESTARTSYS;
2019 res = hotkey_mask_set(t);
2021 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2022 hotkey_poll_setup(1);
2023 #endif
2025 mutex_unlock(&hotkey_mutex);
2027 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2029 return (res) ? res : count;
2032 static struct device_attribute dev_attr_hotkey_mask =
2033 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2034 hotkey_mask_show, hotkey_mask_store);
2036 /* sysfs hotkey bios_enabled ------------------------------------------- */
2037 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2038 struct device_attribute *attr,
2039 char *buf)
2041 return sprintf(buf, "0\n");
2044 static struct device_attribute dev_attr_hotkey_bios_enabled =
2045 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2047 /* sysfs hotkey bios_mask ---------------------------------------------- */
2048 static ssize_t hotkey_bios_mask_show(struct device *dev,
2049 struct device_attribute *attr,
2050 char *buf)
2052 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2055 static struct device_attribute dev_attr_hotkey_bios_mask =
2056 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2058 /* sysfs hotkey all_mask ----------------------------------------------- */
2059 static ssize_t hotkey_all_mask_show(struct device *dev,
2060 struct device_attribute *attr,
2061 char *buf)
2063 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2064 hotkey_all_mask | hotkey_source_mask);
2067 static struct device_attribute dev_attr_hotkey_all_mask =
2068 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2070 /* sysfs hotkey recommended_mask --------------------------------------- */
2071 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2072 struct device_attribute *attr,
2073 char *buf)
2075 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2076 (hotkey_all_mask | hotkey_source_mask)
2077 & ~hotkey_reserved_mask);
2080 static struct device_attribute dev_attr_hotkey_recommended_mask =
2081 __ATTR(hotkey_recommended_mask, S_IRUGO,
2082 hotkey_recommended_mask_show, NULL);
2084 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2086 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2087 static ssize_t hotkey_source_mask_show(struct device *dev,
2088 struct device_attribute *attr,
2089 char *buf)
2091 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2094 static ssize_t hotkey_source_mask_store(struct device *dev,
2095 struct device_attribute *attr,
2096 const char *buf, size_t count)
2098 unsigned long t;
2100 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2101 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2102 return -EINVAL;
2104 if (mutex_lock_killable(&hotkey_mutex))
2105 return -ERESTARTSYS;
2107 HOTKEY_CONFIG_CRITICAL_START
2108 hotkey_source_mask = t;
2109 HOTKEY_CONFIG_CRITICAL_END
2111 hotkey_poll_setup(1);
2113 mutex_unlock(&hotkey_mutex);
2115 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2117 return count;
2120 static struct device_attribute dev_attr_hotkey_source_mask =
2121 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2122 hotkey_source_mask_show, hotkey_source_mask_store);
2124 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2125 static ssize_t hotkey_poll_freq_show(struct device *dev,
2126 struct device_attribute *attr,
2127 char *buf)
2129 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2132 static ssize_t hotkey_poll_freq_store(struct device *dev,
2133 struct device_attribute *attr,
2134 const char *buf, size_t count)
2136 unsigned long t;
2138 if (parse_strtoul(buf, 25, &t))
2139 return -EINVAL;
2141 if (mutex_lock_killable(&hotkey_mutex))
2142 return -ERESTARTSYS;
2144 hotkey_poll_freq = t;
2146 hotkey_poll_setup(1);
2147 mutex_unlock(&hotkey_mutex);
2149 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2151 return count;
2154 static struct device_attribute dev_attr_hotkey_poll_freq =
2155 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2156 hotkey_poll_freq_show, hotkey_poll_freq_store);
2158 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2160 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2161 static ssize_t hotkey_radio_sw_show(struct device *dev,
2162 struct device_attribute *attr,
2163 char *buf)
2165 int res, s;
2166 res = hotkey_get_wlsw(&s);
2167 if (res < 0)
2168 return res;
2170 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2173 static struct device_attribute dev_attr_hotkey_radio_sw =
2174 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2176 static void hotkey_radio_sw_notify_change(void)
2178 if (tp_features.hotkey_wlsw)
2179 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2180 "hotkey_radio_sw");
2183 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2184 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2185 struct device_attribute *attr,
2186 char *buf)
2188 int res, s;
2189 res = hotkey_get_tablet_mode(&s);
2190 if (res < 0)
2191 return res;
2193 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2196 static struct device_attribute dev_attr_hotkey_tablet_mode =
2197 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2199 static void hotkey_tablet_mode_notify_change(void)
2201 if (tp_features.hotkey_tablet)
2202 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2203 "hotkey_tablet_mode");
2206 /* sysfs hotkey report_mode -------------------------------------------- */
2207 static ssize_t hotkey_report_mode_show(struct device *dev,
2208 struct device_attribute *attr,
2209 char *buf)
2211 return snprintf(buf, PAGE_SIZE, "%d\n",
2212 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2215 static struct device_attribute dev_attr_hotkey_report_mode =
2216 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2218 /* sysfs wakeup reason (pollable) -------------------------------------- */
2219 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2220 struct device_attribute *attr,
2221 char *buf)
2223 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2226 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2227 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2229 static void hotkey_wakeup_reason_notify_change(void)
2231 if (tp_features.hotkey_mask)
2232 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2233 "wakeup_reason");
2236 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2237 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2238 struct device_attribute *attr,
2239 char *buf)
2241 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2244 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2245 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2246 hotkey_wakeup_hotunplug_complete_show, NULL);
2248 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2250 if (tp_features.hotkey_mask)
2251 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2252 "wakeup_hotunplug_complete");
2255 /* --------------------------------------------------------------------- */
2257 static struct attribute *hotkey_attributes[] __initdata = {
2258 &dev_attr_hotkey_enable.attr,
2259 &dev_attr_hotkey_bios_enabled.attr,
2260 &dev_attr_hotkey_report_mode.attr,
2261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 &dev_attr_hotkey_mask.attr,
2263 &dev_attr_hotkey_all_mask.attr,
2264 &dev_attr_hotkey_recommended_mask.attr,
2265 &dev_attr_hotkey_source_mask.attr,
2266 &dev_attr_hotkey_poll_freq.attr,
2267 #endif
2270 static struct attribute *hotkey_mask_attributes[] __initdata = {
2271 &dev_attr_hotkey_bios_mask.attr,
2272 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2273 &dev_attr_hotkey_mask.attr,
2274 &dev_attr_hotkey_all_mask.attr,
2275 &dev_attr_hotkey_recommended_mask.attr,
2276 #endif
2277 &dev_attr_hotkey_wakeup_reason.attr,
2278 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2281 static void bluetooth_update_rfk(void);
2282 static void wan_update_rfk(void);
2283 static void uwb_update_rfk(void);
2284 static void tpacpi_send_radiosw_update(void)
2286 int wlsw;
2288 /* Sync these BEFORE sending any rfkill events */
2289 if (tp_features.bluetooth)
2290 bluetooth_update_rfk();
2291 if (tp_features.wan)
2292 wan_update_rfk();
2293 if (tp_features.uwb)
2294 uwb_update_rfk();
2296 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2297 mutex_lock(&tpacpi_inputdev_send_mutex);
2299 input_report_switch(tpacpi_inputdev,
2300 SW_RFKILL_ALL, !!wlsw);
2301 input_sync(tpacpi_inputdev);
2303 mutex_unlock(&tpacpi_inputdev_send_mutex);
2305 hotkey_radio_sw_notify_change();
2308 static void hotkey_exit(void)
2310 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2311 hotkey_poll_stop_sync();
2312 #endif
2314 if (hotkey_dev_attributes)
2315 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2317 kfree(hotkey_keycode_map);
2319 if (tp_features.hotkey) {
2320 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2321 "restoring original hot key mask\n");
2322 /* no short-circuit boolean operator below! */
2323 if ((hotkey_mask_set(hotkey_orig_mask) |
2324 hotkey_status_set(false)) != 0)
2325 printk(TPACPI_ERR
2326 "failed to restore hot key mask "
2327 "to BIOS defaults\n");
2331 static int __init hotkey_init(struct ibm_init_struct *iibm)
2333 /* Requirements for changing the default keymaps:
2335 * 1. Many of the keys are mapped to KEY_RESERVED for very
2336 * good reasons. Do not change them unless you have deep
2337 * knowledge on the IBM and Lenovo ThinkPad firmware for
2338 * the various ThinkPad models. The driver behaves
2339 * differently for KEY_RESERVED: such keys have their
2340 * hot key mask *unset* in mask_recommended, and also
2341 * in the initial hot key mask programmed into the
2342 * firmware at driver load time, which means the firm-
2343 * ware may react very differently if you change them to
2344 * something else;
2346 * 2. You must be subscribed to the linux-thinkpad and
2347 * ibm-acpi-devel mailing lists, and you should read the
2348 * list archives since 2007 if you want to change the
2349 * keymaps. This requirement exists so that you will
2350 * know the past history of problems with the thinkpad-
2351 * acpi driver keymaps, and also that you will be
2352 * listening to any bug reports;
2354 * 3. Do not send thinkpad-acpi specific patches directly to
2355 * for merging, *ever*. Send them to the linux-acpi
2356 * mailinglist for comments. Merging is to be done only
2357 * through acpi-test and the ACPI maintainer.
2359 * If the above is too much to ask, don't change the keymap.
2360 * Ask the thinkpad-acpi maintainer to do it, instead.
2362 static u16 ibm_keycode_map[] __initdata = {
2363 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2364 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2365 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2366 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2368 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2369 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2370 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2371 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2373 /* brightness: firmware always reacts to them, unless
2374 * X.org did some tricks in the radeon BIOS scratch
2375 * registers of *some* models */
2376 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2377 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2379 /* Thinklight: firmware always react to it */
2380 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2382 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2383 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2385 /* Volume: firmware always react to it and reprograms
2386 * the built-in *extra* mixer. Never map it to control
2387 * another mixer by default. */
2388 KEY_RESERVED, /* 0x14: VOLUME UP */
2389 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2390 KEY_RESERVED, /* 0x16: MUTE */
2392 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2394 /* (assignments unknown, please report if found) */
2395 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2396 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2398 static u16 lenovo_keycode_map[] __initdata = {
2399 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2400 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2401 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2402 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2404 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2405 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2406 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2407 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2409 /* These either have to go through ACPI video, or
2410 * act like in the IBM ThinkPads, so don't ever
2411 * enable them by default */
2412 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2413 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2415 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2417 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2418 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2420 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2421 * react to it and reprograms the built-in *extra* mixer.
2422 * Never map it to control another mixer by default.
2424 * T60?, T61, R60?, R61: firmware and EC tries to send
2425 * these over the regular keyboard, so these are no-ops,
2426 * but there are still weird bugs re. MUTE, so do not
2427 * change unless you get test reports from all Lenovo
2428 * models. May cause the BIOS to interfere with the
2429 * HDA mixer.
2431 KEY_RESERVED, /* 0x14: VOLUME UP */
2432 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2433 KEY_RESERVED, /* 0x16: MUTE */
2435 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2437 /* (assignments unknown, please report if found) */
2438 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2439 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2442 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2443 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2444 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2446 int res, i;
2447 int status;
2448 int hkeyv;
2450 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2451 "initializing hotkey subdriver\n");
2453 BUG_ON(!tpacpi_inputdev);
2454 BUG_ON(tpacpi_inputdev->open != NULL ||
2455 tpacpi_inputdev->close != NULL);
2457 TPACPI_ACPIHANDLE_INIT(hkey);
2458 mutex_init(&hotkey_mutex);
2460 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2461 mutex_init(&hotkey_thread_mutex);
2462 mutex_init(&hotkey_thread_data_mutex);
2463 #endif
2465 /* hotkey not supported on 570 */
2466 tp_features.hotkey = hkey_handle != NULL;
2468 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2469 "hotkeys are %s\n",
2470 str_supported(tp_features.hotkey));
2472 if (!tp_features.hotkey)
2473 return 1;
2475 tpacpi_disable_brightness_delay();
2477 hotkey_dev_attributes = create_attr_set(13, NULL);
2478 if (!hotkey_dev_attributes)
2479 return -ENOMEM;
2480 res = add_many_to_attr_set(hotkey_dev_attributes,
2481 hotkey_attributes,
2482 ARRAY_SIZE(hotkey_attributes));
2483 if (res)
2484 goto err_exit;
2486 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2487 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2488 for HKEY interface version 0x100 */
2489 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2490 if ((hkeyv >> 8) != 1) {
2491 printk(TPACPI_ERR "unknown version of the "
2492 "HKEY interface: 0x%x\n", hkeyv);
2493 printk(TPACPI_ERR "please report this to %s\n",
2494 TPACPI_MAIL);
2495 } else {
2497 * MHKV 0x100 in A31, R40, R40e,
2498 * T4x, X31, and later
2500 tp_features.hotkey_mask = 1;
2501 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2502 "firmware HKEY interface version: 0x%x\n",
2503 hkeyv);
2507 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2508 "hotkey masks are %s\n",
2509 str_supported(tp_features.hotkey_mask));
2511 if (tp_features.hotkey_mask) {
2512 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2513 "MHKA", "qd")) {
2514 printk(TPACPI_ERR
2515 "missing MHKA handler, "
2516 "please report this to %s\n",
2517 TPACPI_MAIL);
2518 /* FN+F12, FN+F4, FN+F3 */
2519 hotkey_all_mask = 0x080cU;
2523 /* hotkey_source_mask *must* be zero for
2524 * the first hotkey_mask_get */
2525 if (tp_features.hotkey_mask) {
2526 res = hotkey_mask_get();
2527 if (res)
2528 goto err_exit;
2530 hotkey_orig_mask = hotkey_mask;
2531 res = add_many_to_attr_set(
2532 hotkey_dev_attributes,
2533 hotkey_mask_attributes,
2534 ARRAY_SIZE(hotkey_mask_attributes));
2535 if (res)
2536 goto err_exit;
2539 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2540 if (tp_features.hotkey_mask) {
2541 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2542 & ~hotkey_all_mask;
2543 } else {
2544 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2547 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2548 "hotkey source mask 0x%08x, polling freq %d\n",
2549 hotkey_source_mask, hotkey_poll_freq);
2550 #endif
2552 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2553 if (dbg_wlswemul) {
2554 tp_features.hotkey_wlsw = 1;
2555 printk(TPACPI_INFO
2556 "radio switch emulation enabled\n");
2557 } else
2558 #endif
2559 /* Not all thinkpads have a hardware radio switch */
2560 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2561 tp_features.hotkey_wlsw = 1;
2562 printk(TPACPI_INFO
2563 "radio switch found; radios are %s\n",
2564 enabled(status, 0));
2566 if (tp_features.hotkey_wlsw)
2567 res = add_to_attr_set(hotkey_dev_attributes,
2568 &dev_attr_hotkey_radio_sw.attr);
2570 /* For X41t, X60t, X61t Tablets... */
2571 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2572 tp_features.hotkey_tablet = 1;
2573 printk(TPACPI_INFO
2574 "possible tablet mode switch found; "
2575 "ThinkPad in %s mode\n",
2576 (status & TP_HOTKEY_TABLET_MASK)?
2577 "tablet" : "laptop");
2578 res = add_to_attr_set(hotkey_dev_attributes,
2579 &dev_attr_hotkey_tablet_mode.attr);
2582 if (!res)
2583 res = register_attr_set_with_sysfs(
2584 hotkey_dev_attributes,
2585 &tpacpi_pdev->dev.kobj);
2586 if (res)
2587 goto err_exit;
2589 /* Set up key map */
2591 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2592 GFP_KERNEL);
2593 if (!hotkey_keycode_map) {
2594 printk(TPACPI_ERR
2595 "failed to allocate memory for key map\n");
2596 res = -ENOMEM;
2597 goto err_exit;
2600 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2601 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2602 "using Lenovo default hot key map\n");
2603 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2604 TPACPI_HOTKEY_MAP_SIZE);
2605 } else {
2606 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2607 "using IBM default hot key map\n");
2608 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2609 TPACPI_HOTKEY_MAP_SIZE);
2612 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2613 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2614 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2615 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2616 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2617 tpacpi_inputdev->keycode = hotkey_keycode_map;
2618 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2619 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2620 set_bit(hotkey_keycode_map[i],
2621 tpacpi_inputdev->keybit);
2622 } else {
2623 if (i < sizeof(hotkey_reserved_mask)*8)
2624 hotkey_reserved_mask |= 1 << i;
2628 if (tp_features.hotkey_wlsw) {
2629 set_bit(EV_SW, tpacpi_inputdev->evbit);
2630 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2632 if (tp_features.hotkey_tablet) {
2633 set_bit(EV_SW, tpacpi_inputdev->evbit);
2634 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2637 /* Do not issue duplicate brightness change events to
2638 * userspace */
2639 if (!tp_features.bright_acpimode)
2640 /* update bright_acpimode... */
2641 tpacpi_check_std_acpi_brightness_support();
2643 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2644 printk(TPACPI_INFO
2645 "This ThinkPad has standard ACPI backlight "
2646 "brightness control, supported by the ACPI "
2647 "video driver\n");
2648 printk(TPACPI_NOTICE
2649 "Disabling thinkpad-acpi brightness events "
2650 "by default...\n");
2652 /* The hotkey_reserved_mask change below is not
2653 * necessary while the keys are at KEY_RESERVED in the
2654 * default map, but better safe than sorry, leave it
2655 * here as a marker of what we have to do, especially
2656 * when we finally become able to set this at runtime
2657 * on response to X.org requests */
2658 hotkey_reserved_mask |=
2659 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2660 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2663 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2664 "enabling firmware HKEY event interface...\n");
2665 res = hotkey_status_set(true);
2666 if (res) {
2667 hotkey_exit();
2668 return res;
2670 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2671 & ~hotkey_reserved_mask)
2672 | hotkey_orig_mask);
2673 if (res < 0 && res != -ENXIO) {
2674 hotkey_exit();
2675 return res;
2678 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2679 "legacy ibm/hotkey event reporting over procfs %s\n",
2680 (hotkey_report_mode < 2) ?
2681 "enabled" : "disabled");
2683 tpacpi_inputdev->open = &hotkey_inputdev_open;
2684 tpacpi_inputdev->close = &hotkey_inputdev_close;
2686 hotkey_poll_setup_safe(1);
2687 tpacpi_send_radiosw_update();
2688 tpacpi_input_send_tabletsw();
2690 return 0;
2692 err_exit:
2693 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2694 hotkey_dev_attributes = NULL;
2696 return (res < 0)? res : 1;
2699 static bool hotkey_notify_hotkey(const u32 hkey,
2700 bool *send_acpi_ev,
2701 bool *ignore_acpi_ev)
2703 /* 0x1000-0x1FFF: key presses */
2704 unsigned int scancode = hkey & 0xfff;
2705 *send_acpi_ev = true;
2706 *ignore_acpi_ev = false;
2708 if (scancode > 0 && scancode < 0x21) {
2709 scancode--;
2710 if (!(hotkey_source_mask & (1 << scancode))) {
2711 tpacpi_input_send_key(scancode);
2712 *send_acpi_ev = false;
2713 } else {
2714 *ignore_acpi_ev = true;
2716 return true;
2718 return false;
2721 static bool hotkey_notify_wakeup(const u32 hkey,
2722 bool *send_acpi_ev,
2723 bool *ignore_acpi_ev)
2725 /* 0x2000-0x2FFF: Wakeup reason */
2726 *send_acpi_ev = true;
2727 *ignore_acpi_ev = false;
2729 switch (hkey) {
2730 case 0x2304: /* suspend, undock */
2731 case 0x2404: /* hibernation, undock */
2732 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2733 *ignore_acpi_ev = true;
2734 break;
2736 case 0x2305: /* suspend, bay eject */
2737 case 0x2405: /* hibernation, bay eject */
2738 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2739 *ignore_acpi_ev = true;
2740 break;
2742 case 0x2313: /* Battery on critical low level (S3) */
2743 case 0x2413: /* Battery on critical low level (S4) */
2744 printk(TPACPI_ALERT
2745 "EMERGENCY WAKEUP: battery almost empty\n");
2746 /* how to auto-heal: */
2747 /* 2313: woke up from S3, go to S4/S5 */
2748 /* 2413: woke up from S4, go to S5 */
2749 break;
2751 default:
2752 return false;
2755 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2756 printk(TPACPI_INFO
2757 "woke up due to a hot-unplug "
2758 "request...\n");
2759 hotkey_wakeup_reason_notify_change();
2761 return true;
2764 static bool hotkey_notify_usrevent(const u32 hkey,
2765 bool *send_acpi_ev,
2766 bool *ignore_acpi_ev)
2768 /* 0x5000-0x5FFF: human interface helpers */
2769 *send_acpi_ev = true;
2770 *ignore_acpi_ev = false;
2772 switch (hkey) {
2773 case 0x5010: /* Lenovo new BIOS: brightness changed */
2774 case 0x500b: /* X61t: tablet pen inserted into bay */
2775 case 0x500c: /* X61t: tablet pen removed from bay */
2776 return true;
2778 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2779 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2780 tpacpi_input_send_tabletsw();
2781 hotkey_tablet_mode_notify_change();
2782 *send_acpi_ev = false;
2783 return true;
2785 case 0x5001:
2786 case 0x5002:
2787 /* LID switch events. Do not propagate */
2788 *ignore_acpi_ev = true;
2789 return true;
2791 default:
2792 return false;
2796 static bool hotkey_notify_thermal(const u32 hkey,
2797 bool *send_acpi_ev,
2798 bool *ignore_acpi_ev)
2800 /* 0x6000-0x6FFF: thermal alarms */
2801 *send_acpi_ev = true;
2802 *ignore_acpi_ev = false;
2804 switch (hkey) {
2805 case 0x6011:
2806 printk(TPACPI_CRIT
2807 "THERMAL ALARM: battery is too hot!\n");
2808 /* recommended action: warn user through gui */
2809 return true;
2810 case 0x6012:
2811 printk(TPACPI_ALERT
2812 "THERMAL EMERGENCY: battery is extremely hot!\n");
2813 /* recommended action: immediate sleep/hibernate */
2814 return true;
2815 case 0x6021:
2816 printk(TPACPI_CRIT
2817 "THERMAL ALARM: "
2818 "a sensor reports something is too hot!\n");
2819 /* recommended action: warn user through gui, that */
2820 /* some internal component is too hot */
2821 return true;
2822 case 0x6022:
2823 printk(TPACPI_ALERT
2824 "THERMAL EMERGENCY: "
2825 "a sensor reports something is extremely hot!\n");
2826 /* recommended action: immediate sleep/hibernate */
2827 return true;
2828 case 0x6030:
2829 printk(TPACPI_INFO
2830 "EC reports that Thermal Table has changed\n");
2831 /* recommended action: do nothing, we don't have
2832 * Lenovo ATM information */
2833 return true;
2834 default:
2835 printk(TPACPI_ALERT
2836 "THERMAL ALERT: unknown thermal alarm received\n");
2837 return false;
2841 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2843 u32 hkey;
2844 bool send_acpi_ev;
2845 bool ignore_acpi_ev;
2846 bool known_ev;
2848 if (event != 0x80) {
2849 printk(TPACPI_ERR
2850 "unknown HKEY notification event %d\n", event);
2851 /* forward it to userspace, maybe it knows how to handle it */
2852 acpi_bus_generate_netlink_event(
2853 ibm->acpi->device->pnp.device_class,
2854 dev_name(&ibm->acpi->device->dev),
2855 event, 0);
2856 return;
2859 while (1) {
2860 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2861 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2862 return;
2865 if (hkey == 0) {
2866 /* queue empty */
2867 return;
2870 send_acpi_ev = true;
2871 ignore_acpi_ev = false;
2873 switch (hkey >> 12) {
2874 case 1:
2875 /* 0x1000-0x1FFF: key presses */
2876 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2877 &ignore_acpi_ev);
2878 break;
2879 case 2:
2880 /* 0x2000-0x2FFF: Wakeup reason */
2881 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2882 &ignore_acpi_ev);
2883 break;
2884 case 3:
2885 /* 0x3000-0x3FFF: bay-related wakeups */
2886 if (hkey == 0x3003) {
2887 hotkey_autosleep_ack = 1;
2888 printk(TPACPI_INFO
2889 "bay ejected\n");
2890 hotkey_wakeup_hotunplug_complete_notify_change();
2891 known_ev = true;
2892 } else {
2893 known_ev = false;
2895 break;
2896 case 4:
2897 /* 0x4000-0x4FFF: dock-related wakeups */
2898 if (hkey == 0x4003) {
2899 hotkey_autosleep_ack = 1;
2900 printk(TPACPI_INFO
2901 "undocked\n");
2902 hotkey_wakeup_hotunplug_complete_notify_change();
2903 known_ev = true;
2904 } else {
2905 known_ev = false;
2907 break;
2908 case 5:
2909 /* 0x5000-0x5FFF: human interface helpers */
2910 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2911 &ignore_acpi_ev);
2912 break;
2913 case 6:
2914 /* 0x6000-0x6FFF: thermal alarms */
2915 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2916 &ignore_acpi_ev);
2917 break;
2918 case 7:
2919 /* 0x7000-0x7FFF: misc */
2920 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2921 tpacpi_send_radiosw_update();
2922 send_acpi_ev = 0;
2923 known_ev = true;
2924 break;
2926 /* fallthrough to default */
2927 default:
2928 known_ev = false;
2930 if (!known_ev) {
2931 printk(TPACPI_NOTICE
2932 "unhandled HKEY event 0x%04x\n", hkey);
2933 printk(TPACPI_NOTICE
2934 "please report the conditions when this "
2935 "event happened to %s\n", TPACPI_MAIL);
2938 /* Legacy events */
2939 if (!ignore_acpi_ev &&
2940 (send_acpi_ev || hotkey_report_mode < 2)) {
2941 acpi_bus_generate_proc_event(ibm->acpi->device,
2942 event, hkey);
2945 /* netlink events */
2946 if (!ignore_acpi_ev && send_acpi_ev) {
2947 acpi_bus_generate_netlink_event(
2948 ibm->acpi->device->pnp.device_class,
2949 dev_name(&ibm->acpi->device->dev),
2950 event, hkey);
2955 static void hotkey_suspend(pm_message_t state)
2957 /* Do these on suspend, we get the events on early resume! */
2958 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2959 hotkey_autosleep_ack = 0;
2962 static void hotkey_resume(void)
2964 tpacpi_disable_brightness_delay();
2966 if (hotkey_mask_get())
2967 printk(TPACPI_ERR
2968 "error while trying to read hot key mask "
2969 "from firmware\n");
2970 tpacpi_send_radiosw_update();
2971 hotkey_tablet_mode_notify_change();
2972 hotkey_wakeup_reason_notify_change();
2973 hotkey_wakeup_hotunplug_complete_notify_change();
2974 hotkey_poll_setup_safe(0);
2977 /* procfs -------------------------------------------------------------- */
2978 static int hotkey_read(char *p)
2980 int res, status;
2981 int len = 0;
2983 if (!tp_features.hotkey) {
2984 len += sprintf(p + len, "status:\t\tnot supported\n");
2985 return len;
2988 if (mutex_lock_killable(&hotkey_mutex))
2989 return -ERESTARTSYS;
2990 res = hotkey_status_get(&status);
2991 if (!res)
2992 res = hotkey_mask_get();
2993 mutex_unlock(&hotkey_mutex);
2994 if (res)
2995 return res;
2997 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2998 if (tp_features.hotkey_mask) {
2999 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3000 len += sprintf(p + len,
3001 "commands:\tenable, disable, reset, <mask>\n");
3002 } else {
3003 len += sprintf(p + len, "mask:\t\tnot supported\n");
3004 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3007 return len;
3010 static void hotkey_enabledisable_warn(bool enable)
3012 tpacpi_log_usertask("procfs hotkey enable/disable");
3013 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3014 TPACPI_WARN
3015 "hotkey enable/disable functionality has been "
3016 "removed from the driver. Hotkeys are always "
3017 "enabled\n"))
3018 printk(TPACPI_ERR
3019 "Please remove the hotkey=enable module "
3020 "parameter, it is deprecated. Hotkeys are always "
3021 "enabled\n");
3024 static int hotkey_write(char *buf)
3026 int res;
3027 u32 mask;
3028 char *cmd;
3030 if (!tp_features.hotkey)
3031 return -ENODEV;
3033 if (mutex_lock_killable(&hotkey_mutex))
3034 return -ERESTARTSYS;
3036 mask = hotkey_mask;
3038 res = 0;
3039 while ((cmd = next_cmd(&buf))) {
3040 if (strlencmp(cmd, "enable") == 0) {
3041 hotkey_enabledisable_warn(1);
3042 } else if (strlencmp(cmd, "disable") == 0) {
3043 hotkey_enabledisable_warn(0);
3044 res = -EPERM;
3045 } else if (strlencmp(cmd, "reset") == 0) {
3046 mask = hotkey_orig_mask;
3047 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3048 /* mask set */
3049 } else if (sscanf(cmd, "%x", &mask) == 1) {
3050 /* mask set */
3051 } else {
3052 res = -EINVAL;
3053 goto errexit;
3057 if (!res)
3058 tpacpi_disclose_usertask("procfs hotkey",
3059 "set mask to 0x%08x\n", mask);
3061 if (!res && mask != hotkey_mask)
3062 res = hotkey_mask_set(mask);
3064 errexit:
3065 mutex_unlock(&hotkey_mutex);
3066 return res;
3069 static const struct acpi_device_id ibm_htk_device_ids[] = {
3070 {TPACPI_ACPI_HKEY_HID, 0},
3071 {"", 0},
3074 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3075 .hid = ibm_htk_device_ids,
3076 .notify = hotkey_notify,
3077 .handle = &hkey_handle,
3078 .type = ACPI_DEVICE_NOTIFY,
3081 static struct ibm_struct hotkey_driver_data = {
3082 .name = "hotkey",
3083 .read = hotkey_read,
3084 .write = hotkey_write,
3085 .exit = hotkey_exit,
3086 .resume = hotkey_resume,
3087 .suspend = hotkey_suspend,
3088 .acpi = &ibm_hotkey_acpidriver,
3091 /*************************************************************************
3092 * Bluetooth subdriver
3095 enum {
3096 /* ACPI GBDC/SBDC bits */
3097 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3098 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3099 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3100 off / last state */
3103 enum {
3104 /* ACPI \BLTH commands */
3105 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3106 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3107 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3108 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3109 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3112 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3114 static struct rfkill *tpacpi_bluetooth_rfkill;
3116 static void bluetooth_suspend(pm_message_t state)
3118 /* Try to make sure radio will resume powered off */
3119 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3120 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3121 vdbg_printk(TPACPI_DBG_RFKILL,
3122 "bluetooth power down on resume request failed\n");
3125 static int bluetooth_get_radiosw(void)
3127 int status;
3129 if (!tp_features.bluetooth)
3130 return -ENODEV;
3132 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3133 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3134 return RFKILL_STATE_HARD_BLOCKED;
3136 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3137 if (dbg_bluetoothemul)
3138 return (tpacpi_bluetooth_emulstate) ?
3139 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3140 #endif
3142 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3143 return -EIO;
3145 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3146 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3149 static void bluetooth_update_rfk(void)
3151 int status;
3153 if (!tpacpi_bluetooth_rfkill)
3154 return;
3156 status = bluetooth_get_radiosw();
3157 if (status < 0)
3158 return;
3159 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3161 vdbg_printk(TPACPI_DBG_RFKILL,
3162 "forced rfkill state to %d\n",
3163 status);
3166 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3168 int status;
3170 if (!tp_features.bluetooth)
3171 return -ENODEV;
3173 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3174 * reason to risk weird behaviour. */
3175 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3176 && radio_on)
3177 return -EPERM;
3179 vdbg_printk(TPACPI_DBG_RFKILL,
3180 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3182 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3183 if (dbg_bluetoothemul) {
3184 tpacpi_bluetooth_emulstate = !!radio_on;
3185 if (update_rfk)
3186 bluetooth_update_rfk();
3187 return 0;
3189 #endif
3191 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3192 if (radio_on)
3193 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3194 else
3195 status = 0;
3196 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3197 return -EIO;
3199 if (update_rfk)
3200 bluetooth_update_rfk();
3202 return 0;
3205 /* sysfs bluetooth enable ---------------------------------------------- */
3206 static ssize_t bluetooth_enable_show(struct device *dev,
3207 struct device_attribute *attr,
3208 char *buf)
3210 int status;
3212 printk_deprecated_rfkill_attribute("bluetooth_enable");
3214 status = bluetooth_get_radiosw();
3215 if (status < 0)
3216 return status;
3218 return snprintf(buf, PAGE_SIZE, "%d\n",
3219 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3222 static ssize_t bluetooth_enable_store(struct device *dev,
3223 struct device_attribute *attr,
3224 const char *buf, size_t count)
3226 unsigned long t;
3227 int res;
3229 printk_deprecated_rfkill_attribute("bluetooth_enable");
3231 if (parse_strtoul(buf, 1, &t))
3232 return -EINVAL;
3234 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3236 res = bluetooth_set_radiosw(t, 1);
3238 return (res) ? res : count;
3241 static struct device_attribute dev_attr_bluetooth_enable =
3242 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3243 bluetooth_enable_show, bluetooth_enable_store);
3245 /* --------------------------------------------------------------------- */
3247 static struct attribute *bluetooth_attributes[] = {
3248 &dev_attr_bluetooth_enable.attr,
3249 NULL
3252 static const struct attribute_group bluetooth_attr_group = {
3253 .attrs = bluetooth_attributes,
3256 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3258 int bts = bluetooth_get_radiosw();
3260 if (bts < 0)
3261 return bts;
3263 *state = bts;
3264 return 0;
3267 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3269 dbg_printk(TPACPI_DBG_RFKILL,
3270 "request to change radio state to %d\n", state);
3271 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3274 static void bluetooth_shutdown(void)
3276 /* Order firmware to save current state to NVRAM */
3277 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3278 TP_ACPI_BLTH_SAVE_STATE))
3279 printk(TPACPI_NOTICE
3280 "failed to save bluetooth state to NVRAM\n");
3281 else
3282 vdbg_printk(TPACPI_DBG_RFKILL,
3283 "bluestooth state saved to NVRAM\n");
3286 static void bluetooth_exit(void)
3288 bluetooth_shutdown();
3290 if (tpacpi_bluetooth_rfkill)
3291 rfkill_unregister(tpacpi_bluetooth_rfkill);
3293 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3294 &bluetooth_attr_group);
3297 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3299 int res;
3300 int status = 0;
3302 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3303 "initializing bluetooth subdriver\n");
3305 TPACPI_ACPIHANDLE_INIT(hkey);
3307 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3308 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3309 tp_features.bluetooth = hkey_handle &&
3310 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3312 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3313 "bluetooth is %s, status 0x%02x\n",
3314 str_supported(tp_features.bluetooth),
3315 status);
3317 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3318 if (dbg_bluetoothemul) {
3319 tp_features.bluetooth = 1;
3320 printk(TPACPI_INFO
3321 "bluetooth switch emulation enabled\n");
3322 } else
3323 #endif
3324 if (tp_features.bluetooth &&
3325 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3326 /* no bluetooth hardware present in system */
3327 tp_features.bluetooth = 0;
3328 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3329 "bluetooth hardware not installed\n");
3332 if (!tp_features.bluetooth)
3333 return 1;
3335 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3336 &bluetooth_attr_group);
3337 if (res)
3338 return res;
3340 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3341 &tpacpi_bluetooth_rfkill,
3342 RFKILL_TYPE_BLUETOOTH,
3343 TPACPI_RFK_BLUETOOTH_SW_NAME,
3344 true,
3345 tpacpi_bluetooth_rfk_set,
3346 tpacpi_bluetooth_rfk_get);
3347 if (res) {
3348 bluetooth_exit();
3349 return res;
3352 return 0;
3355 /* procfs -------------------------------------------------------------- */
3356 static int bluetooth_read(char *p)
3358 int len = 0;
3359 int status = bluetooth_get_radiosw();
3361 if (!tp_features.bluetooth)
3362 len += sprintf(p + len, "status:\t\tnot supported\n");
3363 else {
3364 len += sprintf(p + len, "status:\t\t%s\n",
3365 (status == RFKILL_STATE_UNBLOCKED) ?
3366 "enabled" : "disabled");
3367 len += sprintf(p + len, "commands:\tenable, disable\n");
3370 return len;
3373 static int bluetooth_write(char *buf)
3375 char *cmd;
3376 int state = -1;
3378 if (!tp_features.bluetooth)
3379 return -ENODEV;
3381 while ((cmd = next_cmd(&buf))) {
3382 if (strlencmp(cmd, "enable") == 0) {
3383 state = 1;
3384 } else if (strlencmp(cmd, "disable") == 0) {
3385 state = 0;
3386 } else
3387 return -EINVAL;
3390 if (state != -1) {
3391 tpacpi_disclose_usertask("procfs bluetooth",
3392 "attempt to %s\n",
3393 state ? "enable" : "disable");
3394 bluetooth_set_radiosw(state, 1);
3397 return 0;
3400 static struct ibm_struct bluetooth_driver_data = {
3401 .name = "bluetooth",
3402 .read = bluetooth_read,
3403 .write = bluetooth_write,
3404 .exit = bluetooth_exit,
3405 .suspend = bluetooth_suspend,
3406 .shutdown = bluetooth_shutdown,
3409 /*************************************************************************
3410 * Wan subdriver
3413 enum {
3414 /* ACPI GWAN/SWAN bits */
3415 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3416 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3417 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3418 off / last state */
3421 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3423 static struct rfkill *tpacpi_wan_rfkill;
3425 static void wan_suspend(pm_message_t state)
3427 /* Try to make sure radio will resume powered off */
3428 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3429 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3430 vdbg_printk(TPACPI_DBG_RFKILL,
3431 "WWAN power down on resume request failed\n");
3434 static int wan_get_radiosw(void)
3436 int status;
3438 if (!tp_features.wan)
3439 return -ENODEV;
3441 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3442 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3443 return RFKILL_STATE_HARD_BLOCKED;
3445 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3446 if (dbg_wwanemul)
3447 return (tpacpi_wwan_emulstate) ?
3448 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3449 #endif
3451 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3452 return -EIO;
3454 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3455 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3458 static void wan_update_rfk(void)
3460 int status;
3462 if (!tpacpi_wan_rfkill)
3463 return;
3465 status = wan_get_radiosw();
3466 if (status < 0)
3467 return;
3468 rfkill_force_state(tpacpi_wan_rfkill, status);
3470 vdbg_printk(TPACPI_DBG_RFKILL,
3471 "forced rfkill state to %d\n",
3472 status);
3475 static int wan_set_radiosw(int radio_on, int update_rfk)
3477 int status;
3479 if (!tp_features.wan)
3480 return -ENODEV;
3482 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3483 * reason to risk weird behaviour. */
3484 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3485 && radio_on)
3486 return -EPERM;
3488 vdbg_printk(TPACPI_DBG_RFKILL,
3489 "will %s WWAN\n", radio_on ? "enable" : "disable");
3491 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3492 if (dbg_wwanemul) {
3493 tpacpi_wwan_emulstate = !!radio_on;
3494 if (update_rfk)
3495 wan_update_rfk();
3496 return 0;
3498 #endif
3500 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3501 if (radio_on)
3502 status = TP_ACPI_WANCARD_RADIOSSW;
3503 else
3504 status = 0;
3505 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3506 return -EIO;
3508 if (update_rfk)
3509 wan_update_rfk();
3511 return 0;
3514 /* sysfs wan enable ---------------------------------------------------- */
3515 static ssize_t wan_enable_show(struct device *dev,
3516 struct device_attribute *attr,
3517 char *buf)
3519 int status;
3521 printk_deprecated_rfkill_attribute("wwan_enable");
3523 status = wan_get_radiosw();
3524 if (status < 0)
3525 return status;
3527 return snprintf(buf, PAGE_SIZE, "%d\n",
3528 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3531 static ssize_t wan_enable_store(struct device *dev,
3532 struct device_attribute *attr,
3533 const char *buf, size_t count)
3535 unsigned long t;
3536 int res;
3538 printk_deprecated_rfkill_attribute("wwan_enable");
3540 if (parse_strtoul(buf, 1, &t))
3541 return -EINVAL;
3543 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3545 res = wan_set_radiosw(t, 1);
3547 return (res) ? res : count;
3550 static struct device_attribute dev_attr_wan_enable =
3551 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3552 wan_enable_show, wan_enable_store);
3554 /* --------------------------------------------------------------------- */
3556 static struct attribute *wan_attributes[] = {
3557 &dev_attr_wan_enable.attr,
3558 NULL
3561 static const struct attribute_group wan_attr_group = {
3562 .attrs = wan_attributes,
3565 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3567 int wans = wan_get_radiosw();
3569 if (wans < 0)
3570 return wans;
3572 *state = wans;
3573 return 0;
3576 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3578 dbg_printk(TPACPI_DBG_RFKILL,
3579 "request to change radio state to %d\n", state);
3580 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3583 static void wan_shutdown(void)
3585 /* Order firmware to save current state to NVRAM */
3586 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3587 TP_ACPI_WGSV_SAVE_STATE))
3588 printk(TPACPI_NOTICE
3589 "failed to save WWAN state to NVRAM\n");
3590 else
3591 vdbg_printk(TPACPI_DBG_RFKILL,
3592 "WWAN state saved to NVRAM\n");
3595 static void wan_exit(void)
3597 wan_shutdown();
3599 if (tpacpi_wan_rfkill)
3600 rfkill_unregister(tpacpi_wan_rfkill);
3602 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3603 &wan_attr_group);
3606 static int __init wan_init(struct ibm_init_struct *iibm)
3608 int res;
3609 int status = 0;
3611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3612 "initializing wan subdriver\n");
3614 TPACPI_ACPIHANDLE_INIT(hkey);
3616 tp_features.wan = hkey_handle &&
3617 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3619 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3620 "wan is %s, status 0x%02x\n",
3621 str_supported(tp_features.wan),
3622 status);
3624 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3625 if (dbg_wwanemul) {
3626 tp_features.wan = 1;
3627 printk(TPACPI_INFO
3628 "wwan switch emulation enabled\n");
3629 } else
3630 #endif
3631 if (tp_features.wan &&
3632 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3633 /* no wan hardware present in system */
3634 tp_features.wan = 0;
3635 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3636 "wan hardware not installed\n");
3639 if (!tp_features.wan)
3640 return 1;
3642 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3643 &wan_attr_group);
3644 if (res)
3645 return res;
3647 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3648 &tpacpi_wan_rfkill,
3649 RFKILL_TYPE_WWAN,
3650 TPACPI_RFK_WWAN_SW_NAME,
3651 true,
3652 tpacpi_wan_rfk_set,
3653 tpacpi_wan_rfk_get);
3654 if (res) {
3655 wan_exit();
3656 return res;
3659 return 0;
3662 /* procfs -------------------------------------------------------------- */
3663 static int wan_read(char *p)
3665 int len = 0;
3666 int status = wan_get_radiosw();
3668 tpacpi_disclose_usertask("procfs wan", "read");
3670 if (!tp_features.wan)
3671 len += sprintf(p + len, "status:\t\tnot supported\n");
3672 else {
3673 len += sprintf(p + len, "status:\t\t%s\n",
3674 (status == RFKILL_STATE_UNBLOCKED) ?
3675 "enabled" : "disabled");
3676 len += sprintf(p + len, "commands:\tenable, disable\n");
3679 return len;
3682 static int wan_write(char *buf)
3684 char *cmd;
3685 int state = -1;
3687 if (!tp_features.wan)
3688 return -ENODEV;
3690 while ((cmd = next_cmd(&buf))) {
3691 if (strlencmp(cmd, "enable") == 0) {
3692 state = 1;
3693 } else if (strlencmp(cmd, "disable") == 0) {
3694 state = 0;
3695 } else
3696 return -EINVAL;
3699 if (state != -1) {
3700 tpacpi_disclose_usertask("procfs wan",
3701 "attempt to %s\n",
3702 state ? "enable" : "disable");
3703 wan_set_radiosw(state, 1);
3706 return 0;
3709 static struct ibm_struct wan_driver_data = {
3710 .name = "wan",
3711 .read = wan_read,
3712 .write = wan_write,
3713 .exit = wan_exit,
3714 .suspend = wan_suspend,
3715 .shutdown = wan_shutdown,
3718 /*************************************************************************
3719 * UWB subdriver
3722 enum {
3723 /* ACPI GUWB/SUWB bits */
3724 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3725 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3728 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3730 static struct rfkill *tpacpi_uwb_rfkill;
3732 static int uwb_get_radiosw(void)
3734 int status;
3736 if (!tp_features.uwb)
3737 return -ENODEV;
3739 /* WLSW overrides UWB in firmware/hardware, reflect that */
3740 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3741 return RFKILL_STATE_HARD_BLOCKED;
3743 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3744 if (dbg_uwbemul)
3745 return (tpacpi_uwb_emulstate) ?
3746 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3747 #endif
3749 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3750 return -EIO;
3752 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3753 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3756 static void uwb_update_rfk(void)
3758 int status;
3760 if (!tpacpi_uwb_rfkill)
3761 return;
3763 status = uwb_get_radiosw();
3764 if (status < 0)
3765 return;
3766 rfkill_force_state(tpacpi_uwb_rfkill, status);
3768 vdbg_printk(TPACPI_DBG_RFKILL,
3769 "forced rfkill state to %d\n",
3770 status);
3773 static int uwb_set_radiosw(int radio_on, int update_rfk)
3775 int status;
3777 if (!tp_features.uwb)
3778 return -ENODEV;
3780 /* WLSW overrides UWB in firmware/hardware, but there is no
3781 * reason to risk weird behaviour. */
3782 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3783 && radio_on)
3784 return -EPERM;
3786 vdbg_printk(TPACPI_DBG_RFKILL,
3787 "will %s UWB\n", radio_on ? "enable" : "disable");
3789 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3790 if (dbg_uwbemul) {
3791 tpacpi_uwb_emulstate = !!radio_on;
3792 if (update_rfk)
3793 uwb_update_rfk();
3794 return 0;
3796 #endif
3798 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3799 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3800 return -EIO;
3802 if (update_rfk)
3803 uwb_update_rfk();
3805 return 0;
3808 /* --------------------------------------------------------------------- */
3810 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3812 int uwbs = uwb_get_radiosw();
3814 if (uwbs < 0)
3815 return uwbs;
3817 *state = uwbs;
3818 return 0;
3821 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3823 dbg_printk(TPACPI_DBG_RFKILL,
3824 "request to change radio state to %d\n", state);
3825 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3828 static void uwb_exit(void)
3830 if (tpacpi_uwb_rfkill)
3831 rfkill_unregister(tpacpi_uwb_rfkill);
3834 static int __init uwb_init(struct ibm_init_struct *iibm)
3836 int res;
3837 int status = 0;
3839 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3840 "initializing uwb subdriver\n");
3842 TPACPI_ACPIHANDLE_INIT(hkey);
3844 tp_features.uwb = hkey_handle &&
3845 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3847 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3848 "uwb is %s, status 0x%02x\n",
3849 str_supported(tp_features.uwb),
3850 status);
3852 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3853 if (dbg_uwbemul) {
3854 tp_features.uwb = 1;
3855 printk(TPACPI_INFO
3856 "uwb switch emulation enabled\n");
3857 } else
3858 #endif
3859 if (tp_features.uwb &&
3860 !(status & TP_ACPI_UWB_HWPRESENT)) {
3861 /* no uwb hardware present in system */
3862 tp_features.uwb = 0;
3863 dbg_printk(TPACPI_DBG_INIT,
3864 "uwb hardware not installed\n");
3867 if (!tp_features.uwb)
3868 return 1;
3870 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3871 &tpacpi_uwb_rfkill,
3872 RFKILL_TYPE_UWB,
3873 TPACPI_RFK_UWB_SW_NAME,
3874 false,
3875 tpacpi_uwb_rfk_set,
3876 tpacpi_uwb_rfk_get);
3878 return res;
3881 static struct ibm_struct uwb_driver_data = {
3882 .name = "uwb",
3883 .exit = uwb_exit,
3884 .flags.experimental = 1,
3887 /*************************************************************************
3888 * Video subdriver
3891 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3893 enum video_access_mode {
3894 TPACPI_VIDEO_NONE = 0,
3895 TPACPI_VIDEO_570, /* 570 */
3896 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3897 TPACPI_VIDEO_NEW, /* all others */
3900 enum { /* video status flags, based on VIDEO_570 */
3901 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3902 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3903 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3906 enum { /* TPACPI_VIDEO_570 constants */
3907 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3908 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3909 * video_status_flags */
3910 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3911 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3914 static enum video_access_mode video_supported;
3915 static int video_orig_autosw;
3917 static int video_autosw_get(void);
3918 static int video_autosw_set(int enable);
3920 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3922 static int __init video_init(struct ibm_init_struct *iibm)
3924 int ivga;
3926 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3928 TPACPI_ACPIHANDLE_INIT(vid);
3929 TPACPI_ACPIHANDLE_INIT(vid2);
3931 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3932 /* G41, assume IVGA doesn't change */
3933 vid_handle = vid2_handle;
3935 if (!vid_handle)
3936 /* video switching not supported on R30, R31 */
3937 video_supported = TPACPI_VIDEO_NONE;
3938 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3939 /* 570 */
3940 video_supported = TPACPI_VIDEO_570;
3941 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3942 /* 600e/x, 770e, 770x */
3943 video_supported = TPACPI_VIDEO_770;
3944 else
3945 /* all others */
3946 video_supported = TPACPI_VIDEO_NEW;
3948 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3949 str_supported(video_supported != TPACPI_VIDEO_NONE),
3950 video_supported);
3952 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3955 static void video_exit(void)
3957 dbg_printk(TPACPI_DBG_EXIT,
3958 "restoring original video autoswitch mode\n");
3959 if (video_autosw_set(video_orig_autosw))
3960 printk(TPACPI_ERR "error while trying to restore original "
3961 "video autoswitch mode\n");
3964 static int video_outputsw_get(void)
3966 int status = 0;
3967 int i;
3969 switch (video_supported) {
3970 case TPACPI_VIDEO_570:
3971 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3972 TP_ACPI_VIDEO_570_PHSCMD))
3973 return -EIO;
3974 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3975 break;
3976 case TPACPI_VIDEO_770:
3977 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3978 return -EIO;
3979 if (i)
3980 status |= TP_ACPI_VIDEO_S_LCD;
3981 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3982 return -EIO;
3983 if (i)
3984 status |= TP_ACPI_VIDEO_S_CRT;
3985 break;
3986 case TPACPI_VIDEO_NEW:
3987 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3988 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3989 return -EIO;
3990 if (i)
3991 status |= TP_ACPI_VIDEO_S_CRT;
3993 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3994 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3995 return -EIO;
3996 if (i)
3997 status |= TP_ACPI_VIDEO_S_LCD;
3998 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3999 return -EIO;
4000 if (i)
4001 status |= TP_ACPI_VIDEO_S_DVI;
4002 break;
4003 default:
4004 return -ENOSYS;
4007 return status;
4010 static int video_outputsw_set(int status)
4012 int autosw;
4013 int res = 0;
4015 switch (video_supported) {
4016 case TPACPI_VIDEO_570:
4017 res = acpi_evalf(NULL, NULL,
4018 "\\_SB.PHS2", "vdd",
4019 TP_ACPI_VIDEO_570_PHS2CMD,
4020 status | TP_ACPI_VIDEO_570_PHS2SET);
4021 break;
4022 case TPACPI_VIDEO_770:
4023 autosw = video_autosw_get();
4024 if (autosw < 0)
4025 return autosw;
4027 res = video_autosw_set(1);
4028 if (res)
4029 return res;
4030 res = acpi_evalf(vid_handle, NULL,
4031 "ASWT", "vdd", status * 0x100, 0);
4032 if (!autosw && video_autosw_set(autosw)) {
4033 printk(TPACPI_ERR
4034 "video auto-switch left enabled due to error\n");
4035 return -EIO;
4037 break;
4038 case TPACPI_VIDEO_NEW:
4039 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4040 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4041 break;
4042 default:
4043 return -ENOSYS;
4046 return (res)? 0 : -EIO;
4049 static int video_autosw_get(void)
4051 int autosw = 0;
4053 switch (video_supported) {
4054 case TPACPI_VIDEO_570:
4055 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4056 return -EIO;
4057 break;
4058 case TPACPI_VIDEO_770:
4059 case TPACPI_VIDEO_NEW:
4060 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4061 return -EIO;
4062 break;
4063 default:
4064 return -ENOSYS;
4067 return autosw & 1;
4070 static int video_autosw_set(int enable)
4072 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4073 return -EIO;
4074 return 0;
4077 static int video_outputsw_cycle(void)
4079 int autosw = video_autosw_get();
4080 int res;
4082 if (autosw < 0)
4083 return autosw;
4085 switch (video_supported) {
4086 case TPACPI_VIDEO_570:
4087 res = video_autosw_set(1);
4088 if (res)
4089 return res;
4090 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4091 break;
4092 case TPACPI_VIDEO_770:
4093 case TPACPI_VIDEO_NEW:
4094 res = video_autosw_set(1);
4095 if (res)
4096 return res;
4097 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4098 break;
4099 default:
4100 return -ENOSYS;
4102 if (!autosw && video_autosw_set(autosw)) {
4103 printk(TPACPI_ERR
4104 "video auto-switch left enabled due to error\n");
4105 return -EIO;
4108 return (res)? 0 : -EIO;
4111 static int video_expand_toggle(void)
4113 switch (video_supported) {
4114 case TPACPI_VIDEO_570:
4115 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4116 0 : -EIO;
4117 case TPACPI_VIDEO_770:
4118 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4119 0 : -EIO;
4120 case TPACPI_VIDEO_NEW:
4121 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4122 0 : -EIO;
4123 default:
4124 return -ENOSYS;
4126 /* not reached */
4129 static int video_read(char *p)
4131 int status, autosw;
4132 int len = 0;
4134 if (video_supported == TPACPI_VIDEO_NONE) {
4135 len += sprintf(p + len, "status:\t\tnot supported\n");
4136 return len;
4139 status = video_outputsw_get();
4140 if (status < 0)
4141 return status;
4143 autosw = video_autosw_get();
4144 if (autosw < 0)
4145 return autosw;
4147 len += sprintf(p + len, "status:\t\tsupported\n");
4148 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4149 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4150 if (video_supported == TPACPI_VIDEO_NEW)
4151 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4152 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4153 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4154 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4155 if (video_supported == TPACPI_VIDEO_NEW)
4156 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4157 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4158 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4160 return len;
4163 static int video_write(char *buf)
4165 char *cmd;
4166 int enable, disable, status;
4167 int res;
4169 if (video_supported == TPACPI_VIDEO_NONE)
4170 return -ENODEV;
4172 enable = 0;
4173 disable = 0;
4175 while ((cmd = next_cmd(&buf))) {
4176 if (strlencmp(cmd, "lcd_enable") == 0) {
4177 enable |= TP_ACPI_VIDEO_S_LCD;
4178 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4179 disable |= TP_ACPI_VIDEO_S_LCD;
4180 } else if (strlencmp(cmd, "crt_enable") == 0) {
4181 enable |= TP_ACPI_VIDEO_S_CRT;
4182 } else if (strlencmp(cmd, "crt_disable") == 0) {
4183 disable |= TP_ACPI_VIDEO_S_CRT;
4184 } else if (video_supported == TPACPI_VIDEO_NEW &&
4185 strlencmp(cmd, "dvi_enable") == 0) {
4186 enable |= TP_ACPI_VIDEO_S_DVI;
4187 } else if (video_supported == TPACPI_VIDEO_NEW &&
4188 strlencmp(cmd, "dvi_disable") == 0) {
4189 disable |= TP_ACPI_VIDEO_S_DVI;
4190 } else if (strlencmp(cmd, "auto_enable") == 0) {
4191 res = video_autosw_set(1);
4192 if (res)
4193 return res;
4194 } else if (strlencmp(cmd, "auto_disable") == 0) {
4195 res = video_autosw_set(0);
4196 if (res)
4197 return res;
4198 } else if (strlencmp(cmd, "video_switch") == 0) {
4199 res = video_outputsw_cycle();
4200 if (res)
4201 return res;
4202 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4203 res = video_expand_toggle();
4204 if (res)
4205 return res;
4206 } else
4207 return -EINVAL;
4210 if (enable || disable) {
4211 status = video_outputsw_get();
4212 if (status < 0)
4213 return status;
4214 res = video_outputsw_set((status & ~disable) | enable);
4215 if (res)
4216 return res;
4219 return 0;
4222 static struct ibm_struct video_driver_data = {
4223 .name = "video",
4224 .read = video_read,
4225 .write = video_write,
4226 .exit = video_exit,
4229 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4231 /*************************************************************************
4232 * Light (thinklight) subdriver
4235 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4236 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4238 static int light_get_status(void)
4240 int status = 0;
4242 if (tp_features.light_status) {
4243 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4244 return -EIO;
4245 return (!!status);
4248 return -ENXIO;
4251 static int light_set_status(int status)
4253 int rc;
4255 if (tp_features.light) {
4256 if (cmos_handle) {
4257 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4258 (status)?
4259 TP_CMOS_THINKLIGHT_ON :
4260 TP_CMOS_THINKLIGHT_OFF);
4261 } else {
4262 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4263 (status)? 1 : 0);
4265 return (rc)? 0 : -EIO;
4268 return -ENXIO;
4271 static void light_set_status_worker(struct work_struct *work)
4273 struct tpacpi_led_classdev *data =
4274 container_of(work, struct tpacpi_led_classdev, work);
4276 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4277 light_set_status((data->new_state != TPACPI_LED_OFF));
4280 static void light_sysfs_set(struct led_classdev *led_cdev,
4281 enum led_brightness brightness)
4283 struct tpacpi_led_classdev *data =
4284 container_of(led_cdev,
4285 struct tpacpi_led_classdev,
4286 led_classdev);
4287 data->new_state = (brightness != LED_OFF) ?
4288 TPACPI_LED_ON : TPACPI_LED_OFF;
4289 queue_work(tpacpi_wq, &data->work);
4292 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4294 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4297 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4298 .led_classdev = {
4299 .name = "tpacpi::thinklight",
4300 .brightness_set = &light_sysfs_set,
4301 .brightness_get = &light_sysfs_get,
4305 static int __init light_init(struct ibm_init_struct *iibm)
4307 int rc;
4309 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4311 TPACPI_ACPIHANDLE_INIT(ledb);
4312 TPACPI_ACPIHANDLE_INIT(lght);
4313 TPACPI_ACPIHANDLE_INIT(cmos);
4314 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4316 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4317 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4319 if (tp_features.light)
4320 /* light status not supported on
4321 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4322 tp_features.light_status =
4323 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4325 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4326 str_supported(tp_features.light),
4327 str_supported(tp_features.light_status));
4329 if (!tp_features.light)
4330 return 1;
4332 rc = led_classdev_register(&tpacpi_pdev->dev,
4333 &tpacpi_led_thinklight.led_classdev);
4335 if (rc < 0) {
4336 tp_features.light = 0;
4337 tp_features.light_status = 0;
4338 } else {
4339 rc = 0;
4342 return rc;
4345 static void light_exit(void)
4347 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4348 if (work_pending(&tpacpi_led_thinklight.work))
4349 flush_workqueue(tpacpi_wq);
4352 static int light_read(char *p)
4354 int len = 0;
4355 int status;
4357 if (!tp_features.light) {
4358 len += sprintf(p + len, "status:\t\tnot supported\n");
4359 } else if (!tp_features.light_status) {
4360 len += sprintf(p + len, "status:\t\tunknown\n");
4361 len += sprintf(p + len, "commands:\ton, off\n");
4362 } else {
4363 status = light_get_status();
4364 if (status < 0)
4365 return status;
4366 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4367 len += sprintf(p + len, "commands:\ton, off\n");
4370 return len;
4373 static int light_write(char *buf)
4375 char *cmd;
4376 int newstatus = 0;
4378 if (!tp_features.light)
4379 return -ENODEV;
4381 while ((cmd = next_cmd(&buf))) {
4382 if (strlencmp(cmd, "on") == 0) {
4383 newstatus = 1;
4384 } else if (strlencmp(cmd, "off") == 0) {
4385 newstatus = 0;
4386 } else
4387 return -EINVAL;
4390 return light_set_status(newstatus);
4393 static struct ibm_struct light_driver_data = {
4394 .name = "light",
4395 .read = light_read,
4396 .write = light_write,
4397 .exit = light_exit,
4400 /*************************************************************************
4401 * CMOS subdriver
4404 /* sysfs cmos_command -------------------------------------------------- */
4405 static ssize_t cmos_command_store(struct device *dev,
4406 struct device_attribute *attr,
4407 const char *buf, size_t count)
4409 unsigned long cmos_cmd;
4410 int res;
4412 if (parse_strtoul(buf, 21, &cmos_cmd))
4413 return -EINVAL;
4415 res = issue_thinkpad_cmos_command(cmos_cmd);
4416 return (res)? res : count;
4419 static struct device_attribute dev_attr_cmos_command =
4420 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4422 /* --------------------------------------------------------------------- */
4424 static int __init cmos_init(struct ibm_init_struct *iibm)
4426 int res;
4428 vdbg_printk(TPACPI_DBG_INIT,
4429 "initializing cmos commands subdriver\n");
4431 TPACPI_ACPIHANDLE_INIT(cmos);
4433 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4434 str_supported(cmos_handle != NULL));
4436 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4437 if (res)
4438 return res;
4440 return (cmos_handle)? 0 : 1;
4443 static void cmos_exit(void)
4445 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4448 static int cmos_read(char *p)
4450 int len = 0;
4452 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4453 R30, R31, T20-22, X20-21 */
4454 if (!cmos_handle)
4455 len += sprintf(p + len, "status:\t\tnot supported\n");
4456 else {
4457 len += sprintf(p + len, "status:\t\tsupported\n");
4458 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4461 return len;
4464 static int cmos_write(char *buf)
4466 char *cmd;
4467 int cmos_cmd, res;
4469 while ((cmd = next_cmd(&buf))) {
4470 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4471 cmos_cmd >= 0 && cmos_cmd <= 21) {
4472 /* cmos_cmd set */
4473 } else
4474 return -EINVAL;
4476 res = issue_thinkpad_cmos_command(cmos_cmd);
4477 if (res)
4478 return res;
4481 return 0;
4484 static struct ibm_struct cmos_driver_data = {
4485 .name = "cmos",
4486 .read = cmos_read,
4487 .write = cmos_write,
4488 .exit = cmos_exit,
4491 /*************************************************************************
4492 * LED subdriver
4495 enum led_access_mode {
4496 TPACPI_LED_NONE = 0,
4497 TPACPI_LED_570, /* 570 */
4498 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4499 TPACPI_LED_NEW, /* all others */
4502 enum { /* For TPACPI_LED_OLD */
4503 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4504 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4505 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4508 static enum led_access_mode led_supported;
4510 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4511 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4512 /* T20-22, X20-21 */
4513 "LED", /* all others */
4514 ); /* R30, R31 */
4516 #define TPACPI_LED_NUMLEDS 16
4517 static struct tpacpi_led_classdev *tpacpi_leds;
4518 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4519 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4520 /* there's a limit of 19 chars + NULL before 2.6.26 */
4521 "tpacpi::power",
4522 "tpacpi:orange:batt",
4523 "tpacpi:green:batt",
4524 "tpacpi::dock_active",
4525 "tpacpi::bay_active",
4526 "tpacpi::dock_batt",
4527 "tpacpi::unknown_led",
4528 "tpacpi::standby",
4529 "tpacpi::dock_status1",
4530 "tpacpi::dock_status2",
4531 "tpacpi::unknown_led2",
4532 "tpacpi::unknown_led3",
4533 "tpacpi::thinkvantage",
4535 #define TPACPI_SAFE_LEDS 0x1081U
4537 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4539 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4540 return false;
4541 #else
4542 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4543 #endif
4546 static int led_get_status(const unsigned int led)
4548 int status;
4549 enum led_status_t led_s;
4551 switch (led_supported) {
4552 case TPACPI_LED_570:
4553 if (!acpi_evalf(ec_handle,
4554 &status, "GLED", "dd", 1 << led))
4555 return -EIO;
4556 led_s = (status == 0)?
4557 TPACPI_LED_OFF :
4558 ((status == 1)?
4559 TPACPI_LED_ON :
4560 TPACPI_LED_BLINK);
4561 tpacpi_led_state_cache[led] = led_s;
4562 return led_s;
4563 default:
4564 return -ENXIO;
4567 /* not reached */
4570 static int led_set_status(const unsigned int led,
4571 const enum led_status_t ledstatus)
4573 /* off, on, blink. Index is led_status_t */
4574 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4575 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4577 int rc = 0;
4579 switch (led_supported) {
4580 case TPACPI_LED_570:
4581 /* 570 */
4582 if (unlikely(led > 7))
4583 return -EINVAL;
4584 if (unlikely(tpacpi_is_led_restricted(led)))
4585 return -EPERM;
4586 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4587 (1 << led), led_sled_arg1[ledstatus]))
4588 rc = -EIO;
4589 break;
4590 case TPACPI_LED_OLD:
4591 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4592 if (unlikely(led > 7))
4593 return -EINVAL;
4594 if (unlikely(tpacpi_is_led_restricted(led)))
4595 return -EPERM;
4596 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4597 if (rc >= 0)
4598 rc = ec_write(TPACPI_LED_EC_HLBL,
4599 (ledstatus == TPACPI_LED_BLINK) << led);
4600 if (rc >= 0)
4601 rc = ec_write(TPACPI_LED_EC_HLCL,
4602 (ledstatus != TPACPI_LED_OFF) << led);
4603 break;
4604 case TPACPI_LED_NEW:
4605 /* all others */
4606 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4607 return -EINVAL;
4608 if (unlikely(tpacpi_is_led_restricted(led)))
4609 return -EPERM;
4610 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4611 led, led_led_arg1[ledstatus]))
4612 rc = -EIO;
4613 break;
4614 default:
4615 rc = -ENXIO;
4618 if (!rc)
4619 tpacpi_led_state_cache[led] = ledstatus;
4621 return rc;
4624 static void led_set_status_worker(struct work_struct *work)
4626 struct tpacpi_led_classdev *data =
4627 container_of(work, struct tpacpi_led_classdev, work);
4629 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4630 led_set_status(data->led, data->new_state);
4633 static void led_sysfs_set(struct led_classdev *led_cdev,
4634 enum led_brightness brightness)
4636 struct tpacpi_led_classdev *data = container_of(led_cdev,
4637 struct tpacpi_led_classdev, led_classdev);
4639 if (brightness == LED_OFF)
4640 data->new_state = TPACPI_LED_OFF;
4641 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4642 data->new_state = TPACPI_LED_ON;
4643 else
4644 data->new_state = TPACPI_LED_BLINK;
4646 queue_work(tpacpi_wq, &data->work);
4649 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4650 unsigned long *delay_on, unsigned long *delay_off)
4652 struct tpacpi_led_classdev *data = container_of(led_cdev,
4653 struct tpacpi_led_classdev, led_classdev);
4655 /* Can we choose the flash rate? */
4656 if (*delay_on == 0 && *delay_off == 0) {
4657 /* yes. set them to the hardware blink rate (1 Hz) */
4658 *delay_on = 500; /* ms */
4659 *delay_off = 500; /* ms */
4660 } else if ((*delay_on != 500) || (*delay_off != 500))
4661 return -EINVAL;
4663 data->new_state = TPACPI_LED_BLINK;
4664 queue_work(tpacpi_wq, &data->work);
4666 return 0;
4669 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4671 int rc;
4673 struct tpacpi_led_classdev *data = container_of(led_cdev,
4674 struct tpacpi_led_classdev, led_classdev);
4676 rc = led_get_status(data->led);
4678 if (rc == TPACPI_LED_OFF || rc < 0)
4679 rc = LED_OFF; /* no error handling in led class :( */
4680 else
4681 rc = LED_FULL;
4683 return rc;
4686 static void led_exit(void)
4688 unsigned int i;
4690 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4691 if (tpacpi_leds[i].led_classdev.name)
4692 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4695 kfree(tpacpi_leds);
4698 static int __init tpacpi_init_led(unsigned int led)
4700 int rc;
4702 tpacpi_leds[led].led = led;
4704 /* LEDs with no name don't get registered */
4705 if (!tpacpi_led_names[led])
4706 return 0;
4708 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4709 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4710 if (led_supported == TPACPI_LED_570)
4711 tpacpi_leds[led].led_classdev.brightness_get =
4712 &led_sysfs_get;
4714 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4716 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4718 rc = led_classdev_register(&tpacpi_pdev->dev,
4719 &tpacpi_leds[led].led_classdev);
4720 if (rc < 0)
4721 tpacpi_leds[led].led_classdev.name = NULL;
4723 return rc;
4726 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4727 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4728 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4729 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4731 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4732 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4733 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4734 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4735 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4736 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4737 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4738 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4740 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4741 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4742 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4743 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4744 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4746 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4747 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4748 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4749 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4751 /* (1) - may have excess leds enabled on MSB */
4753 /* Defaults (order matters, keep last, don't reorder!) */
4754 { /* Lenovo */
4755 .vendor = PCI_VENDOR_ID_LENOVO,
4756 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4757 .quirks = 0x1fffU,
4759 { /* IBM ThinkPads with no EC version string */
4760 .vendor = PCI_VENDOR_ID_IBM,
4761 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4762 .quirks = 0x00ffU,
4764 { /* IBM ThinkPads with EC version string */
4765 .vendor = PCI_VENDOR_ID_IBM,
4766 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4767 .quirks = 0x00bfU,
4771 #undef TPACPI_LEDQ_IBM
4772 #undef TPACPI_LEDQ_LNV
4774 static int __init led_init(struct ibm_init_struct *iibm)
4776 unsigned int i;
4777 int rc;
4778 unsigned long useful_leds;
4780 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4782 TPACPI_ACPIHANDLE_INIT(led);
4784 if (!led_handle)
4785 /* led not supported on R30, R31 */
4786 led_supported = TPACPI_LED_NONE;
4787 else if (strlencmp(led_path, "SLED") == 0)
4788 /* 570 */
4789 led_supported = TPACPI_LED_570;
4790 else if (strlencmp(led_path, "SYSL") == 0)
4791 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4792 led_supported = TPACPI_LED_OLD;
4793 else
4794 /* all others */
4795 led_supported = TPACPI_LED_NEW;
4797 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4798 str_supported(led_supported), led_supported);
4800 if (led_supported == TPACPI_LED_NONE)
4801 return 1;
4803 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4804 GFP_KERNEL);
4805 if (!tpacpi_leds) {
4806 printk(TPACPI_ERR "Out of memory for LED data\n");
4807 return -ENOMEM;
4810 useful_leds = tpacpi_check_quirks(led_useful_qtable,
4811 ARRAY_SIZE(led_useful_qtable));
4813 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4814 if (!tpacpi_is_led_restricted(i) &&
4815 test_bit(i, &useful_leds)) {
4816 rc = tpacpi_init_led(i);
4817 if (rc < 0) {
4818 led_exit();
4819 return rc;
4824 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4825 printk(TPACPI_NOTICE
4826 "warning: userspace override of important "
4827 "firmware LEDs is enabled\n");
4828 #endif
4829 return 0;
4832 #define str_led_status(s) \
4833 ((s) == TPACPI_LED_OFF ? "off" : \
4834 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4836 static int led_read(char *p)
4838 int len = 0;
4840 if (!led_supported) {
4841 len += sprintf(p + len, "status:\t\tnot supported\n");
4842 return len;
4844 len += sprintf(p + len, "status:\t\tsupported\n");
4846 if (led_supported == TPACPI_LED_570) {
4847 /* 570 */
4848 int i, status;
4849 for (i = 0; i < 8; i++) {
4850 status = led_get_status(i);
4851 if (status < 0)
4852 return -EIO;
4853 len += sprintf(p + len, "%d:\t\t%s\n",
4854 i, str_led_status(status));
4858 len += sprintf(p + len, "commands:\t"
4859 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
4861 return len;
4864 static int led_write(char *buf)
4866 char *cmd;
4867 int led, rc;
4868 enum led_status_t s;
4870 if (!led_supported)
4871 return -ENODEV;
4873 while ((cmd = next_cmd(&buf))) {
4874 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
4875 return -EINVAL;
4877 if (strstr(cmd, "off")) {
4878 s = TPACPI_LED_OFF;
4879 } else if (strstr(cmd, "on")) {
4880 s = TPACPI_LED_ON;
4881 } else if (strstr(cmd, "blink")) {
4882 s = TPACPI_LED_BLINK;
4883 } else {
4884 return -EINVAL;
4887 rc = led_set_status(led, s);
4888 if (rc < 0)
4889 return rc;
4892 return 0;
4895 static struct ibm_struct led_driver_data = {
4896 .name = "led",
4897 .read = led_read,
4898 .write = led_write,
4899 .exit = led_exit,
4902 /*************************************************************************
4903 * Beep subdriver
4906 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4908 #define TPACPI_BEEP_Q1 0x0001
4910 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
4911 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
4912 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
4915 static int __init beep_init(struct ibm_init_struct *iibm)
4917 unsigned long quirks;
4919 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4921 TPACPI_ACPIHANDLE_INIT(beep);
4923 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4924 str_supported(beep_handle != NULL));
4926 quirks = tpacpi_check_quirks(beep_quirk_table,
4927 ARRAY_SIZE(beep_quirk_table));
4929 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
4931 return (beep_handle)? 0 : 1;
4934 static int beep_read(char *p)
4936 int len = 0;
4938 if (!beep_handle)
4939 len += sprintf(p + len, "status:\t\tnot supported\n");
4940 else {
4941 len += sprintf(p + len, "status:\t\tsupported\n");
4942 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4945 return len;
4948 static int beep_write(char *buf)
4950 char *cmd;
4951 int beep_cmd;
4953 if (!beep_handle)
4954 return -ENODEV;
4956 while ((cmd = next_cmd(&buf))) {
4957 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4958 beep_cmd >= 0 && beep_cmd <= 17) {
4959 /* beep_cmd set */
4960 } else
4961 return -EINVAL;
4962 if (tp_features.beep_needs_two_args) {
4963 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
4964 beep_cmd, 0))
4965 return -EIO;
4966 } else {
4967 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
4968 beep_cmd))
4969 return -EIO;
4973 return 0;
4976 static struct ibm_struct beep_driver_data = {
4977 .name = "beep",
4978 .read = beep_read,
4979 .write = beep_write,
4982 /*************************************************************************
4983 * Thermal subdriver
4986 enum thermal_access_mode {
4987 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4988 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4989 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4990 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4991 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4994 enum { /* TPACPI_THERMAL_TPEC_* */
4995 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4996 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4997 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5000 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5001 struct ibm_thermal_sensors_struct {
5002 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5005 static enum thermal_access_mode thermal_read_mode;
5007 /* idx is zero-based */
5008 static int thermal_get_sensor(int idx, s32 *value)
5010 int t;
5011 s8 tmp;
5012 char tmpi[5];
5014 t = TP_EC_THERMAL_TMP0;
5016 switch (thermal_read_mode) {
5017 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5018 case TPACPI_THERMAL_TPEC_16:
5019 if (idx >= 8 && idx <= 15) {
5020 t = TP_EC_THERMAL_TMP8;
5021 idx -= 8;
5023 /* fallthrough */
5024 #endif
5025 case TPACPI_THERMAL_TPEC_8:
5026 if (idx <= 7) {
5027 if (!acpi_ec_read(t + idx, &tmp))
5028 return -EIO;
5029 *value = tmp * 1000;
5030 return 0;
5032 break;
5034 case TPACPI_THERMAL_ACPI_UPDT:
5035 if (idx <= 7) {
5036 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5037 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5038 return -EIO;
5039 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5040 return -EIO;
5041 *value = (t - 2732) * 100;
5042 return 0;
5044 break;
5046 case TPACPI_THERMAL_ACPI_TMP07:
5047 if (idx <= 7) {
5048 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5049 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5050 return -EIO;
5051 if (t > 127 || t < -127)
5052 t = TP_EC_THERMAL_TMP_NA;
5053 *value = t * 1000;
5054 return 0;
5056 break;
5058 case TPACPI_THERMAL_NONE:
5059 default:
5060 return -ENOSYS;
5063 return -EINVAL;
5066 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5068 int res, i;
5069 int n;
5071 n = 8;
5072 i = 0;
5074 if (!s)
5075 return -EINVAL;
5077 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5078 n = 16;
5080 for (i = 0 ; i < n; i++) {
5081 res = thermal_get_sensor(i, &s->temp[i]);
5082 if (res)
5083 return res;
5086 return n;
5089 /* sysfs temp##_input -------------------------------------------------- */
5091 static ssize_t thermal_temp_input_show(struct device *dev,
5092 struct device_attribute *attr,
5093 char *buf)
5095 struct sensor_device_attribute *sensor_attr =
5096 to_sensor_dev_attr(attr);
5097 int idx = sensor_attr->index;
5098 s32 value;
5099 int res;
5101 res = thermal_get_sensor(idx, &value);
5102 if (res)
5103 return res;
5104 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5105 return -ENXIO;
5107 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5110 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5111 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5112 thermal_temp_input_show, NULL, _idxB)
5114 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5115 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5116 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5117 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5118 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5119 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5120 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5121 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5122 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5123 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5124 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5125 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5126 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5127 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5128 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5129 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5130 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5133 #define THERMAL_ATTRS(X) \
5134 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5136 static struct attribute *thermal_temp_input_attr[] = {
5137 THERMAL_ATTRS(8),
5138 THERMAL_ATTRS(9),
5139 THERMAL_ATTRS(10),
5140 THERMAL_ATTRS(11),
5141 THERMAL_ATTRS(12),
5142 THERMAL_ATTRS(13),
5143 THERMAL_ATTRS(14),
5144 THERMAL_ATTRS(15),
5145 THERMAL_ATTRS(0),
5146 THERMAL_ATTRS(1),
5147 THERMAL_ATTRS(2),
5148 THERMAL_ATTRS(3),
5149 THERMAL_ATTRS(4),
5150 THERMAL_ATTRS(5),
5151 THERMAL_ATTRS(6),
5152 THERMAL_ATTRS(7),
5153 NULL
5156 static const struct attribute_group thermal_temp_input16_group = {
5157 .attrs = thermal_temp_input_attr
5160 static const struct attribute_group thermal_temp_input8_group = {
5161 .attrs = &thermal_temp_input_attr[8]
5164 #undef THERMAL_SENSOR_ATTR_TEMP
5165 #undef THERMAL_ATTRS
5167 /* --------------------------------------------------------------------- */
5169 static int __init thermal_init(struct ibm_init_struct *iibm)
5171 u8 t, ta1, ta2;
5172 int i;
5173 int acpi_tmp7;
5174 int res;
5176 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5178 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5180 if (thinkpad_id.ec_model) {
5182 * Direct EC access mode: sensors at registers
5183 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5184 * non-implemented, thermal sensors return 0x80 when
5185 * not available
5188 ta1 = ta2 = 0;
5189 for (i = 0; i < 8; i++) {
5190 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5191 ta1 |= t;
5192 } else {
5193 ta1 = 0;
5194 break;
5196 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5197 ta2 |= t;
5198 } else {
5199 ta1 = 0;
5200 break;
5203 if (ta1 == 0) {
5204 /* This is sheer paranoia, but we handle it anyway */
5205 if (acpi_tmp7) {
5206 printk(TPACPI_ERR
5207 "ThinkPad ACPI EC access misbehaving, "
5208 "falling back to ACPI TMPx access "
5209 "mode\n");
5210 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5211 } else {
5212 printk(TPACPI_ERR
5213 "ThinkPad ACPI EC access misbehaving, "
5214 "disabling thermal sensors access\n");
5215 thermal_read_mode = TPACPI_THERMAL_NONE;
5217 } else {
5218 thermal_read_mode =
5219 (ta2 != 0) ?
5220 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5222 } else if (acpi_tmp7) {
5223 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5224 /* 600e/x, 770e, 770x */
5225 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5226 } else {
5227 /* Standard ACPI TMPx access, max 8 sensors */
5228 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5230 } else {
5231 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5232 thermal_read_mode = TPACPI_THERMAL_NONE;
5235 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5236 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5237 thermal_read_mode);
5239 switch (thermal_read_mode) {
5240 case TPACPI_THERMAL_TPEC_16:
5241 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5242 &thermal_temp_input16_group);
5243 if (res)
5244 return res;
5245 break;
5246 case TPACPI_THERMAL_TPEC_8:
5247 case TPACPI_THERMAL_ACPI_TMP07:
5248 case TPACPI_THERMAL_ACPI_UPDT:
5249 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5250 &thermal_temp_input8_group);
5251 if (res)
5252 return res;
5253 break;
5254 case TPACPI_THERMAL_NONE:
5255 default:
5256 return 1;
5259 return 0;
5262 static void thermal_exit(void)
5264 switch (thermal_read_mode) {
5265 case TPACPI_THERMAL_TPEC_16:
5266 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5267 &thermal_temp_input16_group);
5268 break;
5269 case TPACPI_THERMAL_TPEC_8:
5270 case TPACPI_THERMAL_ACPI_TMP07:
5271 case TPACPI_THERMAL_ACPI_UPDT:
5272 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5273 &thermal_temp_input16_group);
5274 break;
5275 case TPACPI_THERMAL_NONE:
5276 default:
5277 break;
5281 static int thermal_read(char *p)
5283 int len = 0;
5284 int n, i;
5285 struct ibm_thermal_sensors_struct t;
5287 n = thermal_get_sensors(&t);
5288 if (unlikely(n < 0))
5289 return n;
5291 len += sprintf(p + len, "temperatures:\t");
5293 if (n > 0) {
5294 for (i = 0; i < (n - 1); i++)
5295 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5296 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5297 } else
5298 len += sprintf(p + len, "not supported\n");
5300 return len;
5303 static struct ibm_struct thermal_driver_data = {
5304 .name = "thermal",
5305 .read = thermal_read,
5306 .exit = thermal_exit,
5309 /*************************************************************************
5310 * EC Dump subdriver
5313 static u8 ecdump_regs[256];
5315 static int ecdump_read(char *p)
5317 int len = 0;
5318 int i, j;
5319 u8 v;
5321 len += sprintf(p + len, "EC "
5322 " +00 +01 +02 +03 +04 +05 +06 +07"
5323 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5324 for (i = 0; i < 256; i += 16) {
5325 len += sprintf(p + len, "EC 0x%02x:", i);
5326 for (j = 0; j < 16; j++) {
5327 if (!acpi_ec_read(i + j, &v))
5328 break;
5329 if (v != ecdump_regs[i + j])
5330 len += sprintf(p + len, " *%02x", v);
5331 else
5332 len += sprintf(p + len, " %02x", v);
5333 ecdump_regs[i + j] = v;
5335 len += sprintf(p + len, "\n");
5336 if (j != 16)
5337 break;
5340 /* These are way too dangerous to advertise openly... */
5341 #if 0
5342 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5343 " (<offset> is 00-ff, <value> is 00-ff)\n");
5344 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5345 " (<offset> is 00-ff, <value> is 0-255)\n");
5346 #endif
5347 return len;
5350 static int ecdump_write(char *buf)
5352 char *cmd;
5353 int i, v;
5355 while ((cmd = next_cmd(&buf))) {
5356 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5357 /* i and v set */
5358 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5359 /* i and v set */
5360 } else
5361 return -EINVAL;
5362 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5363 if (!acpi_ec_write(i, v))
5364 return -EIO;
5365 } else
5366 return -EINVAL;
5369 return 0;
5372 static struct ibm_struct ecdump_driver_data = {
5373 .name = "ecdump",
5374 .read = ecdump_read,
5375 .write = ecdump_write,
5376 .flags.experimental = 1,
5379 /*************************************************************************
5380 * Backlight/brightness subdriver
5383 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5386 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5387 * CMOS NVRAM byte 0x5E, bits 0-3.
5389 * EC HBRV (0x31) has the following layout
5390 * Bit 7: unknown function
5391 * Bit 6: unknown function
5392 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5393 * Bit 4: must be set to zero to avoid problems
5394 * Bit 3-0: backlight brightness level
5396 * brightness_get_raw returns status data in the HBRV layout
5398 * WARNING: The X61 has been verified to use HBRV for something else, so
5399 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5400 * testing on the very early *60 Lenovo models...
5403 enum {
5404 TP_EC_BACKLIGHT = 0x31,
5406 /* TP_EC_BACKLIGHT bitmasks */
5407 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5408 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5409 TP_EC_BACKLIGHT_MAPSW = 0x20,
5412 enum tpacpi_brightness_access_mode {
5413 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5414 TPACPI_BRGHT_MODE_EC, /* EC control */
5415 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5416 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5417 TPACPI_BRGHT_MODE_MAX
5420 static struct backlight_device *ibm_backlight_device;
5422 static enum tpacpi_brightness_access_mode brightness_mode =
5423 TPACPI_BRGHT_MODE_MAX;
5425 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5427 static struct mutex brightness_mutex;
5429 /* NVRAM brightness access,
5430 * call with brightness_mutex held! */
5431 static unsigned int tpacpi_brightness_nvram_get(void)
5433 u8 lnvram;
5435 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5436 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5437 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5438 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5440 return lnvram;
5443 static void tpacpi_brightness_checkpoint_nvram(void)
5445 u8 lec = 0;
5446 u8 b_nvram;
5448 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5449 return;
5451 vdbg_printk(TPACPI_DBG_BRGHT,
5452 "trying to checkpoint backlight level to NVRAM...\n");
5454 if (mutex_lock_killable(&brightness_mutex) < 0)
5455 return;
5457 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5458 goto unlock;
5459 lec &= TP_EC_BACKLIGHT_LVLMSK;
5460 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5462 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5463 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5464 /* NVRAM needs update */
5465 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5466 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5467 b_nvram |= lec;
5468 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5469 dbg_printk(TPACPI_DBG_BRGHT,
5470 "updated NVRAM backlight level to %u (0x%02x)\n",
5471 (unsigned int) lec, (unsigned int) b_nvram);
5472 } else
5473 vdbg_printk(TPACPI_DBG_BRGHT,
5474 "NVRAM backlight level already is %u (0x%02x)\n",
5475 (unsigned int) lec, (unsigned int) b_nvram);
5477 unlock:
5478 mutex_unlock(&brightness_mutex);
5482 /* call with brightness_mutex held! */
5483 static int tpacpi_brightness_get_raw(int *status)
5485 u8 lec = 0;
5487 switch (brightness_mode) {
5488 case TPACPI_BRGHT_MODE_UCMS_STEP:
5489 *status = tpacpi_brightness_nvram_get();
5490 return 0;
5491 case TPACPI_BRGHT_MODE_EC:
5492 case TPACPI_BRGHT_MODE_ECNVRAM:
5493 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5494 return -EIO;
5495 *status = lec;
5496 return 0;
5497 default:
5498 return -ENXIO;
5502 /* call with brightness_mutex held! */
5503 /* do NOT call with illegal backlight level value */
5504 static int tpacpi_brightness_set_ec(unsigned int value)
5506 u8 lec = 0;
5508 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5509 return -EIO;
5511 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5512 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5513 (value & TP_EC_BACKLIGHT_LVLMSK))))
5514 return -EIO;
5516 return 0;
5519 /* call with brightness_mutex held! */
5520 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5522 int cmos_cmd, inc;
5523 unsigned int current_value, i;
5525 current_value = tpacpi_brightness_nvram_get();
5527 if (value == current_value)
5528 return 0;
5530 cmos_cmd = (value > current_value) ?
5531 TP_CMOS_BRIGHTNESS_UP :
5532 TP_CMOS_BRIGHTNESS_DOWN;
5533 inc = (value > current_value) ? 1 : -1;
5535 for (i = current_value; i != value; i += inc)
5536 if (issue_thinkpad_cmos_command(cmos_cmd))
5537 return -EIO;
5539 return 0;
5542 /* May return EINTR which can always be mapped to ERESTARTSYS */
5543 static int brightness_set(unsigned int value)
5545 int res;
5547 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5548 value < 0)
5549 return -EINVAL;
5551 vdbg_printk(TPACPI_DBG_BRGHT,
5552 "set backlight level to %d\n", value);
5554 res = mutex_lock_killable(&brightness_mutex);
5555 if (res < 0)
5556 return res;
5558 switch (brightness_mode) {
5559 case TPACPI_BRGHT_MODE_EC:
5560 case TPACPI_BRGHT_MODE_ECNVRAM:
5561 res = tpacpi_brightness_set_ec(value);
5562 break;
5563 case TPACPI_BRGHT_MODE_UCMS_STEP:
5564 res = tpacpi_brightness_set_ucmsstep(value);
5565 break;
5566 default:
5567 res = -ENXIO;
5570 mutex_unlock(&brightness_mutex);
5571 return res;
5574 /* sysfs backlight class ----------------------------------------------- */
5576 static int brightness_update_status(struct backlight_device *bd)
5578 unsigned int level =
5579 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5580 bd->props.power == FB_BLANK_UNBLANK) ?
5581 bd->props.brightness : 0;
5583 dbg_printk(TPACPI_DBG_BRGHT,
5584 "backlight: attempt to set level to %d\n",
5585 level);
5587 /* it is the backlight class's job (caller) to handle
5588 * EINTR and other errors properly */
5589 return brightness_set(level);
5592 static int brightness_get(struct backlight_device *bd)
5594 int status, res;
5596 res = mutex_lock_killable(&brightness_mutex);
5597 if (res < 0)
5598 return 0;
5600 res = tpacpi_brightness_get_raw(&status);
5602 mutex_unlock(&brightness_mutex);
5604 if (res < 0)
5605 return 0;
5607 return status & TP_EC_BACKLIGHT_LVLMSK;
5610 static struct backlight_ops ibm_backlight_data = {
5611 .get_brightness = brightness_get,
5612 .update_status = brightness_update_status,
5615 /* --------------------------------------------------------------------- */
5618 * These are only useful for models that have only one possibility
5619 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5620 * these quirks.
5622 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5623 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5624 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5626 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5627 /* Models with ATI GPUs known to require ECNVRAM mode */
5628 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5630 /* Models with ATI GPUs (waiting confirmation) */
5631 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5632 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5633 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5634 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5636 /* Models with Intel Extreme Graphics 2 (waiting confirmation) */
5637 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5638 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5639 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5641 /* Models with Intel GMA900 */
5642 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5643 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5644 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5647 static int __init brightness_init(struct ibm_init_struct *iibm)
5649 int b;
5650 unsigned long quirks;
5652 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5654 mutex_init(&brightness_mutex);
5656 quirks = tpacpi_check_quirks(brightness_quirk_table,
5657 ARRAY_SIZE(brightness_quirk_table));
5660 * We always attempt to detect acpi support, so as to switch
5661 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5662 * going to publish a backlight interface
5664 b = tpacpi_check_std_acpi_brightness_support();
5665 if (b > 0) {
5667 if (acpi_video_backlight_support()) {
5668 if (brightness_enable > 1) {
5669 printk(TPACPI_NOTICE
5670 "Standard ACPI backlight interface "
5671 "available, not loading native one.\n");
5672 return 1;
5673 } else if (brightness_enable == 1) {
5674 printk(TPACPI_NOTICE
5675 "Backlight control force enabled, even if standard "
5676 "ACPI backlight interface is available\n");
5678 } else {
5679 if (brightness_enable > 1) {
5680 printk(TPACPI_NOTICE
5681 "Standard ACPI backlight interface not "
5682 "available, thinkpad_acpi native "
5683 "brightness control enabled\n");
5688 if (!brightness_enable) {
5689 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5690 "brightness support disabled by "
5691 "module parameter\n");
5692 return 1;
5695 if (b > 16) {
5696 printk(TPACPI_ERR
5697 "Unsupported brightness interface, "
5698 "please contact %s\n", TPACPI_MAIL);
5699 return 1;
5701 if (b == 16)
5702 tp_features.bright_16levels = 1;
5705 * Check for module parameter bogosity, note that we
5706 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5707 * able to detect "unspecified"
5709 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5710 return -EINVAL;
5712 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5713 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5714 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5715 if (quirks & TPACPI_BRGHT_Q_EC)
5716 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5717 else
5718 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5720 dbg_printk(TPACPI_DBG_BRGHT,
5721 "driver auto-selected brightness_mode=%d\n",
5722 brightness_mode);
5725 /* Safety */
5726 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5727 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5728 brightness_mode == TPACPI_BRGHT_MODE_EC))
5729 return -EINVAL;
5731 if (tpacpi_brightness_get_raw(&b) < 0)
5732 return 1;
5734 if (tp_features.bright_16levels)
5735 printk(TPACPI_INFO
5736 "detected a 16-level brightness capable ThinkPad\n");
5738 ibm_backlight_device = backlight_device_register(
5739 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5740 &ibm_backlight_data);
5741 if (IS_ERR(ibm_backlight_device)) {
5742 printk(TPACPI_ERR "Could not register backlight device\n");
5743 return PTR_ERR(ibm_backlight_device);
5745 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5746 "brightness is supported\n");
5748 if (quirks & TPACPI_BRGHT_Q_ASK) {
5749 printk(TPACPI_NOTICE
5750 "brightness: will use unverified default: "
5751 "brightness_mode=%d\n", brightness_mode);
5752 printk(TPACPI_NOTICE
5753 "brightness: please report to %s whether it works well "
5754 "or not on your ThinkPad\n", TPACPI_MAIL);
5757 ibm_backlight_device->props.max_brightness =
5758 (tp_features.bright_16levels)? 15 : 7;
5759 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5760 backlight_update_status(ibm_backlight_device);
5762 return 0;
5765 static void brightness_suspend(pm_message_t state)
5767 tpacpi_brightness_checkpoint_nvram();
5770 static void brightness_shutdown(void)
5772 tpacpi_brightness_checkpoint_nvram();
5775 static void brightness_exit(void)
5777 if (ibm_backlight_device) {
5778 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5779 "calling backlight_device_unregister()\n");
5780 backlight_device_unregister(ibm_backlight_device);
5783 tpacpi_brightness_checkpoint_nvram();
5786 static int brightness_read(char *p)
5788 int len = 0;
5789 int level;
5791 level = brightness_get(NULL);
5792 if (level < 0) {
5793 len += sprintf(p + len, "level:\t\tunreadable\n");
5794 } else {
5795 len += sprintf(p + len, "level:\t\t%d\n", level);
5796 len += sprintf(p + len, "commands:\tup, down\n");
5797 len += sprintf(p + len, "commands:\tlevel <level>"
5798 " (<level> is 0-%d)\n",
5799 (tp_features.bright_16levels) ? 15 : 7);
5802 return len;
5805 static int brightness_write(char *buf)
5807 int level;
5808 int rc;
5809 char *cmd;
5810 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5812 level = brightness_get(NULL);
5813 if (level < 0)
5814 return level;
5816 while ((cmd = next_cmd(&buf))) {
5817 if (strlencmp(cmd, "up") == 0) {
5818 if (level < max_level)
5819 level++;
5820 } else if (strlencmp(cmd, "down") == 0) {
5821 if (level > 0)
5822 level--;
5823 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5824 level >= 0 && level <= max_level) {
5825 /* new level set */
5826 } else
5827 return -EINVAL;
5830 tpacpi_disclose_usertask("procfs brightness",
5831 "set level to %d\n", level);
5834 * Now we know what the final level should be, so we try to set it.
5835 * Doing it this way makes the syscall restartable in case of EINTR
5837 rc = brightness_set(level);
5838 return (rc == -EINTR)? ERESTARTSYS : rc;
5841 static struct ibm_struct brightness_driver_data = {
5842 .name = "brightness",
5843 .read = brightness_read,
5844 .write = brightness_write,
5845 .exit = brightness_exit,
5846 .suspend = brightness_suspend,
5847 .shutdown = brightness_shutdown,
5850 /*************************************************************************
5851 * Volume subdriver
5854 static int volume_offset = 0x30;
5856 static int volume_read(char *p)
5858 int len = 0;
5859 u8 level;
5861 if (!acpi_ec_read(volume_offset, &level)) {
5862 len += sprintf(p + len, "level:\t\tunreadable\n");
5863 } else {
5864 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5865 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5866 len += sprintf(p + len, "commands:\tup, down, mute\n");
5867 len += sprintf(p + len, "commands:\tlevel <level>"
5868 " (<level> is 0-15)\n");
5871 return len;
5874 static int volume_write(char *buf)
5876 int cmos_cmd, inc, i;
5877 u8 level, mute;
5878 int new_level, new_mute;
5879 char *cmd;
5881 while ((cmd = next_cmd(&buf))) {
5882 if (!acpi_ec_read(volume_offset, &level))
5883 return -EIO;
5884 new_mute = mute = level & 0x40;
5885 new_level = level = level & 0xf;
5887 if (strlencmp(cmd, "up") == 0) {
5888 if (mute)
5889 new_mute = 0;
5890 else
5891 new_level = level == 15 ? 15 : level + 1;
5892 } else if (strlencmp(cmd, "down") == 0) {
5893 if (mute)
5894 new_mute = 0;
5895 else
5896 new_level = level == 0 ? 0 : level - 1;
5897 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5898 new_level >= 0 && new_level <= 15) {
5899 /* new_level set */
5900 } else if (strlencmp(cmd, "mute") == 0) {
5901 new_mute = 0x40;
5902 } else
5903 return -EINVAL;
5905 if (new_level != level) {
5906 /* mute doesn't change */
5908 cmos_cmd = (new_level > level) ?
5909 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5910 inc = new_level > level ? 1 : -1;
5912 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5913 !acpi_ec_write(volume_offset, level)))
5914 return -EIO;
5916 for (i = level; i != new_level; i += inc)
5917 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5918 !acpi_ec_write(volume_offset, i + inc))
5919 return -EIO;
5921 if (mute &&
5922 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5923 !acpi_ec_write(volume_offset, new_level + mute))) {
5924 return -EIO;
5928 if (new_mute != mute) {
5929 /* level doesn't change */
5931 cmos_cmd = (new_mute) ?
5932 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5934 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5935 !acpi_ec_write(volume_offset, level + new_mute))
5936 return -EIO;
5940 return 0;
5943 static struct ibm_struct volume_driver_data = {
5944 .name = "volume",
5945 .read = volume_read,
5946 .write = volume_write,
5949 /*************************************************************************
5950 * Fan subdriver
5954 * FAN ACCESS MODES
5956 * TPACPI_FAN_RD_ACPI_GFAN:
5957 * ACPI GFAN method: returns fan level
5959 * see TPACPI_FAN_WR_ACPI_SFAN
5960 * EC 0x2f (HFSP) not available if GFAN exists
5962 * TPACPI_FAN_WR_ACPI_SFAN:
5963 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5965 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5966 * it for writing.
5968 * TPACPI_FAN_WR_TPEC:
5969 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5970 * Supported on almost all ThinkPads
5972 * Fan speed changes of any sort (including those caused by the
5973 * disengaged mode) are usually done slowly by the firmware as the
5974 * maximum ammount of fan duty cycle change per second seems to be
5975 * limited.
5977 * Reading is not available if GFAN exists.
5978 * Writing is not available if SFAN exists.
5980 * Bits
5981 * 7 automatic mode engaged;
5982 * (default operation mode of the ThinkPad)
5983 * fan level is ignored in this mode.
5984 * 6 full speed mode (takes precedence over bit 7);
5985 * not available on all thinkpads. May disable
5986 * the tachometer while the fan controller ramps up
5987 * the speed (which can take up to a few *minutes*).
5988 * Speeds up fan to 100% duty-cycle, which is far above
5989 * the standard RPM levels. It is not impossible that
5990 * it could cause hardware damage.
5991 * 5-3 unused in some models. Extra bits for fan level
5992 * in others, but still useless as all values above
5993 * 7 map to the same speed as level 7 in these models.
5994 * 2-0 fan level (0..7 usually)
5995 * 0x00 = stop
5996 * 0x07 = max (set when temperatures critical)
5997 * Some ThinkPads may have other levels, see
5998 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6000 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6001 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6002 * does so, its initial value is meaningless (0x07).
6004 * For firmware bugs, refer to:
6005 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6007 * ----
6009 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6010 * Main fan tachometer reading (in RPM)
6012 * This register is present on all ThinkPads with a new-style EC, and
6013 * it is known not to be present on the A21m/e, and T22, as there is
6014 * something else in offset 0x84 according to the ACPI DSDT. Other
6015 * ThinkPads from this same time period (and earlier) probably lack the
6016 * tachometer as well.
6018 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6019 * was never fixed by IBM to report the EC firmware version string
6020 * probably support the tachometer (like the early X models), so
6021 * detecting it is quite hard. We need more data to know for sure.
6023 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6024 * might result.
6026 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6027 * mode.
6029 * For firmware bugs, refer to:
6030 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6032 * ----
6034 * ThinkPad EC register 0x31 bit 0 (only on select models)
6036 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6037 * show the speed of the main fan. When bit 0 of EC register 0x31
6038 * is one, the tachometer registers show the speed of the auxiliary
6039 * fan.
6041 * Fan control seems to affect both fans, regardless of the state
6042 * of this bit.
6044 * So far, only the firmware for the X60/X61 non-tablet versions
6045 * seem to support this (firmware TP-7M).
6047 * TPACPI_FAN_WR_ACPI_FANS:
6048 * ThinkPad X31, X40, X41. Not available in the X60.
6050 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6051 * high speed. ACPI DSDT seems to map these three speeds to levels
6052 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6053 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6055 * The speeds are stored on handles
6056 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6058 * There are three default speed sets, acessible as handles:
6059 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6061 * ACPI DSDT switches which set is in use depending on various
6062 * factors.
6064 * TPACPI_FAN_WR_TPEC is also available and should be used to
6065 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6066 * but the ACPI tables just mention level 7.
6069 enum { /* Fan control constants */
6070 fan_status_offset = 0x2f, /* EC register 0x2f */
6071 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6072 * 0x84 must be read before 0x85 */
6073 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6074 bit 0 selects which fan is active */
6076 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6077 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6079 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6082 enum fan_status_access_mode {
6083 TPACPI_FAN_NONE = 0, /* No fan status or control */
6084 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6085 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6088 enum fan_control_access_mode {
6089 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6090 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6091 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6092 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6095 enum fan_control_commands {
6096 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6097 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6098 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6099 * and also watchdog cmd */
6102 static int fan_control_allowed;
6104 static enum fan_status_access_mode fan_status_access_mode;
6105 static enum fan_control_access_mode fan_control_access_mode;
6106 static enum fan_control_commands fan_control_commands;
6108 static u8 fan_control_initial_status;
6109 static u8 fan_control_desired_level;
6110 static u8 fan_control_resume_level;
6111 static int fan_watchdog_maxinterval;
6113 static struct mutex fan_mutex;
6115 static void fan_watchdog_fire(struct work_struct *ignored);
6116 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6118 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6119 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6120 "\\FSPD", /* 600e/x, 770e, 770x */
6121 ); /* all others */
6122 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6123 "JFNS", /* 770x-JL */
6124 ); /* all others */
6127 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6128 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6129 * be in auto mode (0x80).
6131 * This is corrected by any write to HFSP either by the driver, or
6132 * by the firmware.
6134 * We assume 0x07 really means auto mode while this quirk is active,
6135 * as this is far more likely than the ThinkPad being in level 7,
6136 * which is only used by the firmware during thermal emergencies.
6138 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6139 * TP-70 (T43, R52), which are known to be buggy.
6142 static void fan_quirk1_setup(void)
6144 if (fan_control_initial_status == 0x07) {
6145 printk(TPACPI_NOTICE
6146 "fan_init: initial fan status is unknown, "
6147 "assuming it is in auto mode\n");
6148 tp_features.fan_ctrl_status_undef = 1;
6152 static void fan_quirk1_handle(u8 *fan_status)
6154 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6155 if (*fan_status != fan_control_initial_status) {
6156 /* something changed the HFSP regisnter since
6157 * driver init time, so it is not undefined
6158 * anymore */
6159 tp_features.fan_ctrl_status_undef = 0;
6160 } else {
6161 /* Return most likely status. In fact, it
6162 * might be the only possible status */
6163 *fan_status = TP_EC_FAN_AUTO;
6168 /* Select main fan on X60/X61, NOOP on others */
6169 static bool fan_select_fan1(void)
6171 if (tp_features.second_fan) {
6172 u8 val;
6174 if (ec_read(fan_select_offset, &val) < 0)
6175 return false;
6176 val &= 0xFEU;
6177 if (ec_write(fan_select_offset, val) < 0)
6178 return false;
6180 return true;
6183 /* Select secondary fan on X60/X61 */
6184 static bool fan_select_fan2(void)
6186 u8 val;
6188 if (!tp_features.second_fan)
6189 return false;
6191 if (ec_read(fan_select_offset, &val) < 0)
6192 return false;
6193 val |= 0x01U;
6194 if (ec_write(fan_select_offset, val) < 0)
6195 return false;
6197 return true;
6201 * Call with fan_mutex held
6203 static void fan_update_desired_level(u8 status)
6205 if ((status &
6206 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6207 if (status > 7)
6208 fan_control_desired_level = 7;
6209 else
6210 fan_control_desired_level = status;
6214 static int fan_get_status(u8 *status)
6216 u8 s;
6218 /* TODO:
6219 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6221 switch (fan_status_access_mode) {
6222 case TPACPI_FAN_RD_ACPI_GFAN:
6223 /* 570, 600e/x, 770e, 770x */
6225 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6226 return -EIO;
6228 if (likely(status))
6229 *status = s & 0x07;
6231 break;
6233 case TPACPI_FAN_RD_TPEC:
6234 /* all except 570, 600e/x, 770e, 770x */
6235 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6236 return -EIO;
6238 if (likely(status)) {
6239 *status = s;
6240 fan_quirk1_handle(status);
6243 break;
6245 default:
6246 return -ENXIO;
6249 return 0;
6252 static int fan_get_status_safe(u8 *status)
6254 int rc;
6255 u8 s;
6257 if (mutex_lock_killable(&fan_mutex))
6258 return -ERESTARTSYS;
6259 rc = fan_get_status(&s);
6260 if (!rc)
6261 fan_update_desired_level(s);
6262 mutex_unlock(&fan_mutex);
6264 if (status)
6265 *status = s;
6267 return rc;
6270 static int fan_get_speed(unsigned int *speed)
6272 u8 hi, lo;
6274 switch (fan_status_access_mode) {
6275 case TPACPI_FAN_RD_TPEC:
6276 /* all except 570, 600e/x, 770e, 770x */
6277 if (unlikely(!fan_select_fan1()))
6278 return -EIO;
6279 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6280 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6281 return -EIO;
6283 if (likely(speed))
6284 *speed = (hi << 8) | lo;
6286 break;
6288 default:
6289 return -ENXIO;
6292 return 0;
6295 static int fan2_get_speed(unsigned int *speed)
6297 u8 hi, lo;
6298 bool rc;
6300 switch (fan_status_access_mode) {
6301 case TPACPI_FAN_RD_TPEC:
6302 /* all except 570, 600e/x, 770e, 770x */
6303 if (unlikely(!fan_select_fan2()))
6304 return -EIO;
6305 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6306 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6307 fan_select_fan1(); /* play it safe */
6308 if (rc)
6309 return -EIO;
6311 if (likely(speed))
6312 *speed = (hi << 8) | lo;
6314 break;
6316 default:
6317 return -ENXIO;
6320 return 0;
6323 static int fan_set_level(int level)
6325 if (!fan_control_allowed)
6326 return -EPERM;
6328 switch (fan_control_access_mode) {
6329 case TPACPI_FAN_WR_ACPI_SFAN:
6330 if (level >= 0 && level <= 7) {
6331 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6332 return -EIO;
6333 } else
6334 return -EINVAL;
6335 break;
6337 case TPACPI_FAN_WR_ACPI_FANS:
6338 case TPACPI_FAN_WR_TPEC:
6339 if (!(level & TP_EC_FAN_AUTO) &&
6340 !(level & TP_EC_FAN_FULLSPEED) &&
6341 ((level < 0) || (level > 7)))
6342 return -EINVAL;
6344 /* safety net should the EC not support AUTO
6345 * or FULLSPEED mode bits and just ignore them */
6346 if (level & TP_EC_FAN_FULLSPEED)
6347 level |= 7; /* safety min speed 7 */
6348 else if (level & TP_EC_FAN_AUTO)
6349 level |= 4; /* safety min speed 4 */
6351 if (!acpi_ec_write(fan_status_offset, level))
6352 return -EIO;
6353 else
6354 tp_features.fan_ctrl_status_undef = 0;
6355 break;
6357 default:
6358 return -ENXIO;
6361 vdbg_printk(TPACPI_DBG_FAN,
6362 "fan control: set fan control register to 0x%02x\n", level);
6363 return 0;
6366 static int fan_set_level_safe(int level)
6368 int rc;
6370 if (!fan_control_allowed)
6371 return -EPERM;
6373 if (mutex_lock_killable(&fan_mutex))
6374 return -ERESTARTSYS;
6376 if (level == TPACPI_FAN_LAST_LEVEL)
6377 level = fan_control_desired_level;
6379 rc = fan_set_level(level);
6380 if (!rc)
6381 fan_update_desired_level(level);
6383 mutex_unlock(&fan_mutex);
6384 return rc;
6387 static int fan_set_enable(void)
6389 u8 s;
6390 int rc;
6392 if (!fan_control_allowed)
6393 return -EPERM;
6395 if (mutex_lock_killable(&fan_mutex))
6396 return -ERESTARTSYS;
6398 switch (fan_control_access_mode) {
6399 case TPACPI_FAN_WR_ACPI_FANS:
6400 case TPACPI_FAN_WR_TPEC:
6401 rc = fan_get_status(&s);
6402 if (rc < 0)
6403 break;
6405 /* Don't go out of emergency fan mode */
6406 if (s != 7) {
6407 s &= 0x07;
6408 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6411 if (!acpi_ec_write(fan_status_offset, s))
6412 rc = -EIO;
6413 else {
6414 tp_features.fan_ctrl_status_undef = 0;
6415 rc = 0;
6417 break;
6419 case TPACPI_FAN_WR_ACPI_SFAN:
6420 rc = fan_get_status(&s);
6421 if (rc < 0)
6422 break;
6424 s &= 0x07;
6426 /* Set fan to at least level 4 */
6427 s |= 4;
6429 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6430 rc = -EIO;
6431 else
6432 rc = 0;
6433 break;
6435 default:
6436 rc = -ENXIO;
6439 mutex_unlock(&fan_mutex);
6441 if (!rc)
6442 vdbg_printk(TPACPI_DBG_FAN,
6443 "fan control: set fan control register to 0x%02x\n",
6445 return rc;
6448 static int fan_set_disable(void)
6450 int rc;
6452 if (!fan_control_allowed)
6453 return -EPERM;
6455 if (mutex_lock_killable(&fan_mutex))
6456 return -ERESTARTSYS;
6458 rc = 0;
6459 switch (fan_control_access_mode) {
6460 case TPACPI_FAN_WR_ACPI_FANS:
6461 case TPACPI_FAN_WR_TPEC:
6462 if (!acpi_ec_write(fan_status_offset, 0x00))
6463 rc = -EIO;
6464 else {
6465 fan_control_desired_level = 0;
6466 tp_features.fan_ctrl_status_undef = 0;
6468 break;
6470 case TPACPI_FAN_WR_ACPI_SFAN:
6471 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6472 rc = -EIO;
6473 else
6474 fan_control_desired_level = 0;
6475 break;
6477 default:
6478 rc = -ENXIO;
6481 if (!rc)
6482 vdbg_printk(TPACPI_DBG_FAN,
6483 "fan control: set fan control register to 0\n");
6485 mutex_unlock(&fan_mutex);
6486 return rc;
6489 static int fan_set_speed(int speed)
6491 int rc;
6493 if (!fan_control_allowed)
6494 return -EPERM;
6496 if (mutex_lock_killable(&fan_mutex))
6497 return -ERESTARTSYS;
6499 rc = 0;
6500 switch (fan_control_access_mode) {
6501 case TPACPI_FAN_WR_ACPI_FANS:
6502 if (speed >= 0 && speed <= 65535) {
6503 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6504 speed, speed, speed))
6505 rc = -EIO;
6506 } else
6507 rc = -EINVAL;
6508 break;
6510 default:
6511 rc = -ENXIO;
6514 mutex_unlock(&fan_mutex);
6515 return rc;
6518 static void fan_watchdog_reset(void)
6520 static int fan_watchdog_active;
6522 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6523 return;
6525 if (fan_watchdog_active)
6526 cancel_delayed_work(&fan_watchdog_task);
6528 if (fan_watchdog_maxinterval > 0 &&
6529 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6530 fan_watchdog_active = 1;
6531 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6532 msecs_to_jiffies(fan_watchdog_maxinterval
6533 * 1000))) {
6534 printk(TPACPI_ERR
6535 "failed to queue the fan watchdog, "
6536 "watchdog will not trigger\n");
6538 } else
6539 fan_watchdog_active = 0;
6542 static void fan_watchdog_fire(struct work_struct *ignored)
6544 int rc;
6546 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6547 return;
6549 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6550 rc = fan_set_enable();
6551 if (rc < 0) {
6552 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6553 "will try again later...\n", -rc);
6554 /* reschedule for later */
6555 fan_watchdog_reset();
6560 * SYSFS fan layout: hwmon compatible (device)
6562 * pwm*_enable:
6563 * 0: "disengaged" mode
6564 * 1: manual mode
6565 * 2: native EC "auto" mode (recommended, hardware default)
6567 * pwm*: set speed in manual mode, ignored otherwise.
6568 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6569 * interpolation.
6571 * fan*_input: tachometer reading, RPM
6574 * SYSFS fan layout: extensions
6576 * fan_watchdog (driver):
6577 * fan watchdog interval in seconds, 0 disables (default), max 120
6580 /* sysfs fan pwm1_enable ----------------------------------------------- */
6581 static ssize_t fan_pwm1_enable_show(struct device *dev,
6582 struct device_attribute *attr,
6583 char *buf)
6585 int res, mode;
6586 u8 status;
6588 res = fan_get_status_safe(&status);
6589 if (res)
6590 return res;
6592 if (status & TP_EC_FAN_FULLSPEED) {
6593 mode = 0;
6594 } else if (status & TP_EC_FAN_AUTO) {
6595 mode = 2;
6596 } else
6597 mode = 1;
6599 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6602 static ssize_t fan_pwm1_enable_store(struct device *dev,
6603 struct device_attribute *attr,
6604 const char *buf, size_t count)
6606 unsigned long t;
6607 int res, level;
6609 if (parse_strtoul(buf, 2, &t))
6610 return -EINVAL;
6612 tpacpi_disclose_usertask("hwmon pwm1_enable",
6613 "set fan mode to %lu\n", t);
6615 switch (t) {
6616 case 0:
6617 level = TP_EC_FAN_FULLSPEED;
6618 break;
6619 case 1:
6620 level = TPACPI_FAN_LAST_LEVEL;
6621 break;
6622 case 2:
6623 level = TP_EC_FAN_AUTO;
6624 break;
6625 case 3:
6626 /* reserved for software-controlled auto mode */
6627 return -ENOSYS;
6628 default:
6629 return -EINVAL;
6632 res = fan_set_level_safe(level);
6633 if (res == -ENXIO)
6634 return -EINVAL;
6635 else if (res < 0)
6636 return res;
6638 fan_watchdog_reset();
6640 return count;
6643 static struct device_attribute dev_attr_fan_pwm1_enable =
6644 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6645 fan_pwm1_enable_show, fan_pwm1_enable_store);
6647 /* sysfs fan pwm1 ------------------------------------------------------ */
6648 static ssize_t fan_pwm1_show(struct device *dev,
6649 struct device_attribute *attr,
6650 char *buf)
6652 int res;
6653 u8 status;
6655 res = fan_get_status_safe(&status);
6656 if (res)
6657 return res;
6659 if ((status &
6660 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6661 status = fan_control_desired_level;
6663 if (status > 7)
6664 status = 7;
6666 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6669 static ssize_t fan_pwm1_store(struct device *dev,
6670 struct device_attribute *attr,
6671 const char *buf, size_t count)
6673 unsigned long s;
6674 int rc;
6675 u8 status, newlevel;
6677 if (parse_strtoul(buf, 255, &s))
6678 return -EINVAL;
6680 tpacpi_disclose_usertask("hwmon pwm1",
6681 "set fan speed to %lu\n", s);
6683 /* scale down from 0-255 to 0-7 */
6684 newlevel = (s >> 5) & 0x07;
6686 if (mutex_lock_killable(&fan_mutex))
6687 return -ERESTARTSYS;
6689 rc = fan_get_status(&status);
6690 if (!rc && (status &
6691 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6692 rc = fan_set_level(newlevel);
6693 if (rc == -ENXIO)
6694 rc = -EINVAL;
6695 else if (!rc) {
6696 fan_update_desired_level(newlevel);
6697 fan_watchdog_reset();
6701 mutex_unlock(&fan_mutex);
6702 return (rc)? rc : count;
6705 static struct device_attribute dev_attr_fan_pwm1 =
6706 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6707 fan_pwm1_show, fan_pwm1_store);
6709 /* sysfs fan fan1_input ------------------------------------------------ */
6710 static ssize_t fan_fan1_input_show(struct device *dev,
6711 struct device_attribute *attr,
6712 char *buf)
6714 int res;
6715 unsigned int speed;
6717 res = fan_get_speed(&speed);
6718 if (res < 0)
6719 return res;
6721 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6724 static struct device_attribute dev_attr_fan_fan1_input =
6725 __ATTR(fan1_input, S_IRUGO,
6726 fan_fan1_input_show, NULL);
6728 /* sysfs fan fan2_input ------------------------------------------------ */
6729 static ssize_t fan_fan2_input_show(struct device *dev,
6730 struct device_attribute *attr,
6731 char *buf)
6733 int res;
6734 unsigned int speed;
6736 res = fan2_get_speed(&speed);
6737 if (res < 0)
6738 return res;
6740 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6743 static struct device_attribute dev_attr_fan_fan2_input =
6744 __ATTR(fan2_input, S_IRUGO,
6745 fan_fan2_input_show, NULL);
6747 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6748 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6749 char *buf)
6751 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6754 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6755 const char *buf, size_t count)
6757 unsigned long t;
6759 if (parse_strtoul(buf, 120, &t))
6760 return -EINVAL;
6762 if (!fan_control_allowed)
6763 return -EPERM;
6765 fan_watchdog_maxinterval = t;
6766 fan_watchdog_reset();
6768 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6770 return count;
6773 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6774 fan_fan_watchdog_show, fan_fan_watchdog_store);
6776 /* --------------------------------------------------------------------- */
6777 static struct attribute *fan_attributes[] = {
6778 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6779 &dev_attr_fan_fan1_input.attr,
6780 NULL, /* for fan2_input */
6781 NULL
6784 static const struct attribute_group fan_attr_group = {
6785 .attrs = fan_attributes,
6788 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6789 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
6791 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6792 { .vendor = PCI_VENDOR_ID_IBM, \
6793 .bios = TPACPI_MATCH_ANY, \
6794 .ec = TPID(__id1, __id2), \
6795 .quirks = __quirks }
6797 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
6798 { .vendor = PCI_VENDOR_ID_LENOVO, \
6799 .bios = TPACPI_MATCH_ANY, \
6800 .ec = TPID(__id1, __id2), \
6801 .quirks = __quirks }
6803 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6804 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6805 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6806 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6807 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
6808 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
6811 #undef TPACPI_FAN_QL
6812 #undef TPACPI_FAN_QI
6814 static int __init fan_init(struct ibm_init_struct *iibm)
6816 int rc;
6817 unsigned long quirks;
6819 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6820 "initializing fan subdriver\n");
6822 mutex_init(&fan_mutex);
6823 fan_status_access_mode = TPACPI_FAN_NONE;
6824 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6825 fan_control_commands = 0;
6826 fan_watchdog_maxinterval = 0;
6827 tp_features.fan_ctrl_status_undef = 0;
6828 tp_features.second_fan = 0;
6829 fan_control_desired_level = 7;
6831 TPACPI_ACPIHANDLE_INIT(fans);
6832 TPACPI_ACPIHANDLE_INIT(gfan);
6833 TPACPI_ACPIHANDLE_INIT(sfan);
6835 quirks = tpacpi_check_quirks(fan_quirk_table,
6836 ARRAY_SIZE(fan_quirk_table));
6838 if (gfan_handle) {
6839 /* 570, 600e/x, 770e, 770x */
6840 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6841 } else {
6842 /* all other ThinkPads: note that even old-style
6843 * ThinkPad ECs supports the fan control register */
6844 if (likely(acpi_ec_read(fan_status_offset,
6845 &fan_control_initial_status))) {
6846 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6847 if (quirks & TPACPI_FAN_Q1)
6848 fan_quirk1_setup();
6849 if (quirks & TPACPI_FAN_2FAN) {
6850 tp_features.second_fan = 1;
6851 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6852 "secondary fan support enabled\n");
6854 } else {
6855 printk(TPACPI_ERR
6856 "ThinkPad ACPI EC access misbehaving, "
6857 "fan status and control unavailable\n");
6858 return 1;
6862 if (sfan_handle) {
6863 /* 570, 770x-JL */
6864 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6865 fan_control_commands |=
6866 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6867 } else {
6868 if (!gfan_handle) {
6869 /* gfan without sfan means no fan control */
6870 /* all other models implement TP EC 0x2f control */
6872 if (fans_handle) {
6873 /* X31, X40, X41 */
6874 fan_control_access_mode =
6875 TPACPI_FAN_WR_ACPI_FANS;
6876 fan_control_commands |=
6877 TPACPI_FAN_CMD_SPEED |
6878 TPACPI_FAN_CMD_LEVEL |
6879 TPACPI_FAN_CMD_ENABLE;
6880 } else {
6881 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6882 fan_control_commands |=
6883 TPACPI_FAN_CMD_LEVEL |
6884 TPACPI_FAN_CMD_ENABLE;
6889 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6890 "fan is %s, modes %d, %d\n",
6891 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6892 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6893 fan_status_access_mode, fan_control_access_mode);
6895 /* fan control master switch */
6896 if (!fan_control_allowed) {
6897 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6898 fan_control_commands = 0;
6899 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6900 "fan control features disabled by parameter\n");
6903 /* update fan_control_desired_level */
6904 if (fan_status_access_mode != TPACPI_FAN_NONE)
6905 fan_get_status_safe(NULL);
6907 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6908 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6909 if (tp_features.second_fan) {
6910 /* attach second fan tachometer */
6911 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
6912 &dev_attr_fan_fan2_input.attr;
6914 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6915 &fan_attr_group);
6916 if (rc < 0)
6917 return rc;
6919 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6920 &driver_attr_fan_watchdog);
6921 if (rc < 0) {
6922 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6923 &fan_attr_group);
6924 return rc;
6926 return 0;
6927 } else
6928 return 1;
6931 static void fan_exit(void)
6933 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
6934 "cancelling any pending fan watchdog tasks\n");
6936 /* FIXME: can we really do this unconditionally? */
6937 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6938 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6939 &driver_attr_fan_watchdog);
6941 cancel_delayed_work(&fan_watchdog_task);
6942 flush_workqueue(tpacpi_wq);
6945 static void fan_suspend(pm_message_t state)
6947 int rc;
6949 if (!fan_control_allowed)
6950 return;
6952 /* Store fan status in cache */
6953 fan_control_resume_level = 0;
6954 rc = fan_get_status_safe(&fan_control_resume_level);
6955 if (rc < 0)
6956 printk(TPACPI_NOTICE
6957 "failed to read fan level for later "
6958 "restore during resume: %d\n", rc);
6960 /* if it is undefined, don't attempt to restore it.
6961 * KEEP THIS LAST */
6962 if (tp_features.fan_ctrl_status_undef)
6963 fan_control_resume_level = 0;
6966 static void fan_resume(void)
6968 u8 current_level = 7;
6969 bool do_set = false;
6970 int rc;
6972 /* DSDT *always* updates status on resume */
6973 tp_features.fan_ctrl_status_undef = 0;
6975 if (!fan_control_allowed ||
6976 !fan_control_resume_level ||
6977 (fan_get_status_safe(&current_level) < 0))
6978 return;
6980 switch (fan_control_access_mode) {
6981 case TPACPI_FAN_WR_ACPI_SFAN:
6982 /* never decrease fan level */
6983 do_set = (fan_control_resume_level > current_level);
6984 break;
6985 case TPACPI_FAN_WR_ACPI_FANS:
6986 case TPACPI_FAN_WR_TPEC:
6987 /* never decrease fan level, scale is:
6988 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6990 * We expect the firmware to set either 7 or AUTO, but we
6991 * handle FULLSPEED out of paranoia.
6993 * So, we can safely only restore FULLSPEED or 7, anything
6994 * else could slow the fan. Restoring AUTO is useless, at
6995 * best that's exactly what the DSDT already set (it is the
6996 * slower it uses).
6998 * Always keep in mind that the DSDT *will* have set the
6999 * fans to what the vendor supposes is the best level. We
7000 * muck with it only to speed the fan up.
7002 if (fan_control_resume_level != 7 &&
7003 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7004 return;
7005 else
7006 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7007 (current_level != fan_control_resume_level);
7008 break;
7009 default:
7010 return;
7012 if (do_set) {
7013 printk(TPACPI_NOTICE
7014 "restoring fan level to 0x%02x\n",
7015 fan_control_resume_level);
7016 rc = fan_set_level_safe(fan_control_resume_level);
7017 if (rc < 0)
7018 printk(TPACPI_NOTICE
7019 "failed to restore fan level: %d\n", rc);
7023 static int fan_read(char *p)
7025 int len = 0;
7026 int rc;
7027 u8 status;
7028 unsigned int speed = 0;
7030 switch (fan_status_access_mode) {
7031 case TPACPI_FAN_RD_ACPI_GFAN:
7032 /* 570, 600e/x, 770e, 770x */
7033 rc = fan_get_status_safe(&status);
7034 if (rc < 0)
7035 return rc;
7037 len += sprintf(p + len, "status:\t\t%s\n"
7038 "level:\t\t%d\n",
7039 (status != 0) ? "enabled" : "disabled", status);
7040 break;
7042 case TPACPI_FAN_RD_TPEC:
7043 /* all except 570, 600e/x, 770e, 770x */
7044 rc = fan_get_status_safe(&status);
7045 if (rc < 0)
7046 return rc;
7048 len += sprintf(p + len, "status:\t\t%s\n",
7049 (status != 0) ? "enabled" : "disabled");
7051 rc = fan_get_speed(&speed);
7052 if (rc < 0)
7053 return rc;
7055 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7057 if (status & TP_EC_FAN_FULLSPEED)
7058 /* Disengaged mode takes precedence */
7059 len += sprintf(p + len, "level:\t\tdisengaged\n");
7060 else if (status & TP_EC_FAN_AUTO)
7061 len += sprintf(p + len, "level:\t\tauto\n");
7062 else
7063 len += sprintf(p + len, "level:\t\t%d\n", status);
7064 break;
7066 case TPACPI_FAN_NONE:
7067 default:
7068 len += sprintf(p + len, "status:\t\tnot supported\n");
7071 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7072 len += sprintf(p + len, "commands:\tlevel <level>");
7074 switch (fan_control_access_mode) {
7075 case TPACPI_FAN_WR_ACPI_SFAN:
7076 len += sprintf(p + len, " (<level> is 0-7)\n");
7077 break;
7079 default:
7080 len += sprintf(p + len, " (<level> is 0-7, "
7081 "auto, disengaged, full-speed)\n");
7082 break;
7086 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7087 len += sprintf(p + len, "commands:\tenable, disable\n"
7088 "commands:\twatchdog <timeout> (<timeout> "
7089 "is 0 (off), 1-120 (seconds))\n");
7091 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7092 len += sprintf(p + len, "commands:\tspeed <speed>"
7093 " (<speed> is 0-65535)\n");
7095 return len;
7098 static int fan_write_cmd_level(const char *cmd, int *rc)
7100 int level;
7102 if (strlencmp(cmd, "level auto") == 0)
7103 level = TP_EC_FAN_AUTO;
7104 else if ((strlencmp(cmd, "level disengaged") == 0) |
7105 (strlencmp(cmd, "level full-speed") == 0))
7106 level = TP_EC_FAN_FULLSPEED;
7107 else if (sscanf(cmd, "level %d", &level) != 1)
7108 return 0;
7110 *rc = fan_set_level_safe(level);
7111 if (*rc == -ENXIO)
7112 printk(TPACPI_ERR "level command accepted for unsupported "
7113 "access mode %d", fan_control_access_mode);
7114 else if (!*rc)
7115 tpacpi_disclose_usertask("procfs fan",
7116 "set level to %d\n", level);
7118 return 1;
7121 static int fan_write_cmd_enable(const char *cmd, int *rc)
7123 if (strlencmp(cmd, "enable") != 0)
7124 return 0;
7126 *rc = fan_set_enable();
7127 if (*rc == -ENXIO)
7128 printk(TPACPI_ERR "enable command accepted for unsupported "
7129 "access mode %d", fan_control_access_mode);
7130 else if (!*rc)
7131 tpacpi_disclose_usertask("procfs fan", "enable\n");
7133 return 1;
7136 static int fan_write_cmd_disable(const char *cmd, int *rc)
7138 if (strlencmp(cmd, "disable") != 0)
7139 return 0;
7141 *rc = fan_set_disable();
7142 if (*rc == -ENXIO)
7143 printk(TPACPI_ERR "disable command accepted for unsupported "
7144 "access mode %d", fan_control_access_mode);
7145 else if (!*rc)
7146 tpacpi_disclose_usertask("procfs fan", "disable\n");
7148 return 1;
7151 static int fan_write_cmd_speed(const char *cmd, int *rc)
7153 int speed;
7155 /* TODO:
7156 * Support speed <low> <medium> <high> ? */
7158 if (sscanf(cmd, "speed %d", &speed) != 1)
7159 return 0;
7161 *rc = fan_set_speed(speed);
7162 if (*rc == -ENXIO)
7163 printk(TPACPI_ERR "speed command accepted for unsupported "
7164 "access mode %d", fan_control_access_mode);
7165 else if (!*rc)
7166 tpacpi_disclose_usertask("procfs fan",
7167 "set speed to %d\n", speed);
7169 return 1;
7172 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7174 int interval;
7176 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7177 return 0;
7179 if (interval < 0 || interval > 120)
7180 *rc = -EINVAL;
7181 else {
7182 fan_watchdog_maxinterval = interval;
7183 tpacpi_disclose_usertask("procfs fan",
7184 "set watchdog timer to %d\n",
7185 interval);
7188 return 1;
7191 static int fan_write(char *buf)
7193 char *cmd;
7194 int rc = 0;
7196 while (!rc && (cmd = next_cmd(&buf))) {
7197 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7198 fan_write_cmd_level(cmd, &rc)) &&
7199 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7200 (fan_write_cmd_enable(cmd, &rc) ||
7201 fan_write_cmd_disable(cmd, &rc) ||
7202 fan_write_cmd_watchdog(cmd, &rc))) &&
7203 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7204 fan_write_cmd_speed(cmd, &rc))
7206 rc = -EINVAL;
7207 else if (!rc)
7208 fan_watchdog_reset();
7211 return rc;
7214 static struct ibm_struct fan_driver_data = {
7215 .name = "fan",
7216 .read = fan_read,
7217 .write = fan_write,
7218 .exit = fan_exit,
7219 .suspend = fan_suspend,
7220 .resume = fan_resume,
7223 /****************************************************************************
7224 ****************************************************************************
7226 * Infrastructure
7228 ****************************************************************************
7229 ****************************************************************************/
7231 /* sysfs name ---------------------------------------------------------- */
7232 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7233 struct device_attribute *attr,
7234 char *buf)
7236 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7239 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7240 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7242 /* --------------------------------------------------------------------- */
7244 /* /proc support */
7245 static struct proc_dir_entry *proc_dir;
7248 * Module and infrastructure proble, init and exit handling
7251 static int force_load;
7253 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7254 static const char * __init str_supported(int is_supported)
7256 static char text_unsupported[] __initdata = "not supported";
7258 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7260 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7262 static void ibm_exit(struct ibm_struct *ibm)
7264 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7266 list_del_init(&ibm->all_drivers);
7268 if (ibm->flags.acpi_notify_installed) {
7269 dbg_printk(TPACPI_DBG_EXIT,
7270 "%s: acpi_remove_notify_handler\n", ibm->name);
7271 BUG_ON(!ibm->acpi);
7272 acpi_remove_notify_handler(*ibm->acpi->handle,
7273 ibm->acpi->type,
7274 dispatch_acpi_notify);
7275 ibm->flags.acpi_notify_installed = 0;
7276 ibm->flags.acpi_notify_installed = 0;
7279 if (ibm->flags.proc_created) {
7280 dbg_printk(TPACPI_DBG_EXIT,
7281 "%s: remove_proc_entry\n", ibm->name);
7282 remove_proc_entry(ibm->name, proc_dir);
7283 ibm->flags.proc_created = 0;
7286 if (ibm->flags.acpi_driver_registered) {
7287 dbg_printk(TPACPI_DBG_EXIT,
7288 "%s: acpi_bus_unregister_driver\n", ibm->name);
7289 BUG_ON(!ibm->acpi);
7290 acpi_bus_unregister_driver(ibm->acpi->driver);
7291 kfree(ibm->acpi->driver);
7292 ibm->acpi->driver = NULL;
7293 ibm->flags.acpi_driver_registered = 0;
7296 if (ibm->flags.init_called && ibm->exit) {
7297 ibm->exit();
7298 ibm->flags.init_called = 0;
7301 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7304 static int __init ibm_init(struct ibm_init_struct *iibm)
7306 int ret;
7307 struct ibm_struct *ibm = iibm->data;
7308 struct proc_dir_entry *entry;
7310 BUG_ON(ibm == NULL);
7312 INIT_LIST_HEAD(&ibm->all_drivers);
7314 if (ibm->flags.experimental && !experimental)
7315 return 0;
7317 dbg_printk(TPACPI_DBG_INIT,
7318 "probing for %s\n", ibm->name);
7320 if (iibm->init) {
7321 ret = iibm->init(iibm);
7322 if (ret > 0)
7323 return 0; /* probe failed */
7324 if (ret)
7325 return ret;
7327 ibm->flags.init_called = 1;
7330 if (ibm->acpi) {
7331 if (ibm->acpi->hid) {
7332 ret = register_tpacpi_subdriver(ibm);
7333 if (ret)
7334 goto err_out;
7337 if (ibm->acpi->notify) {
7338 ret = setup_acpi_notify(ibm);
7339 if (ret == -ENODEV) {
7340 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7341 ibm->name);
7342 ret = 0;
7343 goto err_out;
7345 if (ret < 0)
7346 goto err_out;
7350 dbg_printk(TPACPI_DBG_INIT,
7351 "%s installed\n", ibm->name);
7353 if (ibm->read) {
7354 entry = create_proc_entry(ibm->name,
7355 S_IFREG | S_IRUGO | S_IWUSR,
7356 proc_dir);
7357 if (!entry) {
7358 printk(TPACPI_ERR "unable to create proc entry %s\n",
7359 ibm->name);
7360 ret = -ENODEV;
7361 goto err_out;
7363 entry->data = ibm;
7364 entry->read_proc = &dispatch_procfs_read;
7365 if (ibm->write)
7366 entry->write_proc = &dispatch_procfs_write;
7367 ibm->flags.proc_created = 1;
7370 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7372 return 0;
7374 err_out:
7375 dbg_printk(TPACPI_DBG_INIT,
7376 "%s: at error exit path with result %d\n",
7377 ibm->name, ret);
7379 ibm_exit(ibm);
7380 return (ret < 0)? ret : 0;
7383 /* Probing */
7385 static bool __pure __init tpacpi_is_fw_digit(const char c)
7387 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7390 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7391 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7392 const char t)
7394 return s && strlen(s) >= 8 &&
7395 tpacpi_is_fw_digit(s[0]) &&
7396 tpacpi_is_fw_digit(s[1]) &&
7397 s[2] == t && s[3] == 'T' &&
7398 tpacpi_is_fw_digit(s[4]) &&
7399 tpacpi_is_fw_digit(s[5]) &&
7400 s[6] == 'W' && s[7] == 'W';
7403 /* returns 0 - probe ok, or < 0 - probe error.
7404 * Probe ok doesn't mean thinkpad found.
7405 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7406 static int __must_check __init get_thinkpad_model_data(
7407 struct thinkpad_id_data *tp)
7409 const struct dmi_device *dev = NULL;
7410 char ec_fw_string[18];
7411 char const *s;
7413 if (!tp)
7414 return -EINVAL;
7416 memset(tp, 0, sizeof(*tp));
7418 if (dmi_name_in_vendors("IBM"))
7419 tp->vendor = PCI_VENDOR_ID_IBM;
7420 else if (dmi_name_in_vendors("LENOVO"))
7421 tp->vendor = PCI_VENDOR_ID_LENOVO;
7422 else
7423 return 0;
7425 s = dmi_get_system_info(DMI_BIOS_VERSION);
7426 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7427 if (s && !tp->bios_version_str)
7428 return -ENOMEM;
7430 /* Really ancient ThinkPad 240X will fail this, which is fine */
7431 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7432 return 0;
7434 tp->bios_model = tp->bios_version_str[0]
7435 | (tp->bios_version_str[1] << 8);
7436 tp->bios_release = (tp->bios_version_str[4] << 8)
7437 | tp->bios_version_str[5];
7440 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7441 * X32 or newer, all Z series; Some models must have an
7442 * up-to-date BIOS or they will not be detected.
7444 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7446 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7447 if (sscanf(dev->name,
7448 "IBM ThinkPad Embedded Controller -[%17c",
7449 ec_fw_string) == 1) {
7450 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7451 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7453 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7454 if (!tp->ec_version_str)
7455 return -ENOMEM;
7457 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7458 tp->ec_model = ec_fw_string[0]
7459 | (ec_fw_string[1] << 8);
7460 tp->ec_release = (ec_fw_string[4] << 8)
7461 | ec_fw_string[5];
7462 } else {
7463 printk(TPACPI_NOTICE
7464 "ThinkPad firmware release %s "
7465 "doesn't match the known patterns\n",
7466 ec_fw_string);
7467 printk(TPACPI_NOTICE
7468 "please report this to %s\n",
7469 TPACPI_MAIL);
7471 break;
7475 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7476 if (s && !strnicmp(s, "ThinkPad", 8)) {
7477 tp->model_str = kstrdup(s, GFP_KERNEL);
7478 if (!tp->model_str)
7479 return -ENOMEM;
7482 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7483 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7484 if (s && !tp->nummodel_str)
7485 return -ENOMEM;
7487 return 0;
7490 static int __init probe_for_thinkpad(void)
7492 int is_thinkpad;
7494 if (acpi_disabled)
7495 return -ENODEV;
7498 * Non-ancient models have better DMI tagging, but very old models
7499 * don't.
7501 is_thinkpad = (thinkpad_id.model_str != NULL);
7503 /* ec is required because many other handles are relative to it */
7504 TPACPI_ACPIHANDLE_INIT(ec);
7505 if (!ec_handle) {
7506 if (is_thinkpad)
7507 printk(TPACPI_ERR
7508 "Not yet supported ThinkPad detected!\n");
7509 return -ENODEV;
7513 * Risks a regression on very old machines, but reduces potential
7514 * false positives a damn great deal
7516 if (!is_thinkpad)
7517 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7519 if (!is_thinkpad && !force_load)
7520 return -ENODEV;
7522 return 0;
7526 /* Module init, exit, parameters */
7528 static struct ibm_init_struct ibms_init[] __initdata = {
7530 .init = thinkpad_acpi_driver_init,
7531 .data = &thinkpad_acpi_driver_data,
7534 .init = hotkey_init,
7535 .data = &hotkey_driver_data,
7538 .init = bluetooth_init,
7539 .data = &bluetooth_driver_data,
7542 .init = wan_init,
7543 .data = &wan_driver_data,
7546 .init = uwb_init,
7547 .data = &uwb_driver_data,
7549 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7551 .init = video_init,
7552 .data = &video_driver_data,
7554 #endif
7556 .init = light_init,
7557 .data = &light_driver_data,
7560 .init = cmos_init,
7561 .data = &cmos_driver_data,
7564 .init = led_init,
7565 .data = &led_driver_data,
7568 .init = beep_init,
7569 .data = &beep_driver_data,
7572 .init = thermal_init,
7573 .data = &thermal_driver_data,
7576 .data = &ecdump_driver_data,
7579 .init = brightness_init,
7580 .data = &brightness_driver_data,
7583 .data = &volume_driver_data,
7586 .init = fan_init,
7587 .data = &fan_driver_data,
7591 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7593 unsigned int i;
7594 struct ibm_struct *ibm;
7596 if (!kp || !kp->name || !val)
7597 return -EINVAL;
7599 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7600 ibm = ibms_init[i].data;
7601 WARN_ON(ibm == NULL);
7603 if (!ibm || !ibm->name)
7604 continue;
7606 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7607 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7608 return -ENOSPC;
7609 strcpy(ibms_init[i].param, val);
7610 strcat(ibms_init[i].param, ",");
7611 return 0;
7615 return -EINVAL;
7618 module_param(experimental, int, 0);
7619 MODULE_PARM_DESC(experimental,
7620 "Enables experimental features when non-zero");
7622 module_param_named(debug, dbg_level, uint, 0);
7623 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7625 module_param(force_load, bool, 0);
7626 MODULE_PARM_DESC(force_load,
7627 "Attempts to load the driver even on a "
7628 "mis-identified ThinkPad when true");
7630 module_param_named(fan_control, fan_control_allowed, bool, 0);
7631 MODULE_PARM_DESC(fan_control,
7632 "Enables setting fan parameters features when true");
7634 module_param_named(brightness_mode, brightness_mode, uint, 0);
7635 MODULE_PARM_DESC(brightness_mode,
7636 "Selects brightness control strategy: "
7637 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7639 module_param(brightness_enable, uint, 0);
7640 MODULE_PARM_DESC(brightness_enable,
7641 "Enables backlight control when 1, disables when 0");
7643 module_param(hotkey_report_mode, uint, 0);
7644 MODULE_PARM_DESC(hotkey_report_mode,
7645 "used for backwards compatibility with userspace, "
7646 "see documentation");
7648 #define TPACPI_PARAM(feature) \
7649 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7650 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7651 "at module load, see documentation")
7653 TPACPI_PARAM(hotkey);
7654 TPACPI_PARAM(bluetooth);
7655 TPACPI_PARAM(video);
7656 TPACPI_PARAM(light);
7657 TPACPI_PARAM(cmos);
7658 TPACPI_PARAM(led);
7659 TPACPI_PARAM(beep);
7660 TPACPI_PARAM(ecdump);
7661 TPACPI_PARAM(brightness);
7662 TPACPI_PARAM(volume);
7663 TPACPI_PARAM(fan);
7665 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7666 module_param(dbg_wlswemul, uint, 0);
7667 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7668 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7669 MODULE_PARM_DESC(wlsw_state,
7670 "Initial state of the emulated WLSW switch");
7672 module_param(dbg_bluetoothemul, uint, 0);
7673 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7674 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7675 MODULE_PARM_DESC(bluetooth_state,
7676 "Initial state of the emulated bluetooth switch");
7678 module_param(dbg_wwanemul, uint, 0);
7679 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7680 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7681 MODULE_PARM_DESC(wwan_state,
7682 "Initial state of the emulated WWAN switch");
7684 module_param(dbg_uwbemul, uint, 0);
7685 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7686 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7687 MODULE_PARM_DESC(uwb_state,
7688 "Initial state of the emulated UWB switch");
7689 #endif
7691 static void thinkpad_acpi_module_exit(void)
7693 struct ibm_struct *ibm, *itmp;
7695 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7697 list_for_each_entry_safe_reverse(ibm, itmp,
7698 &tpacpi_all_drivers,
7699 all_drivers) {
7700 ibm_exit(ibm);
7703 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7705 if (tpacpi_inputdev) {
7706 if (tp_features.input_device_registered)
7707 input_unregister_device(tpacpi_inputdev);
7708 else
7709 input_free_device(tpacpi_inputdev);
7712 if (tpacpi_hwmon)
7713 hwmon_device_unregister(tpacpi_hwmon);
7715 if (tp_features.sensors_pdev_attrs_registered)
7716 device_remove_file(&tpacpi_sensors_pdev->dev,
7717 &dev_attr_thinkpad_acpi_pdev_name);
7718 if (tpacpi_sensors_pdev)
7719 platform_device_unregister(tpacpi_sensors_pdev);
7720 if (tpacpi_pdev)
7721 platform_device_unregister(tpacpi_pdev);
7723 if (tp_features.sensors_pdrv_attrs_registered)
7724 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7725 if (tp_features.platform_drv_attrs_registered)
7726 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7728 if (tp_features.sensors_pdrv_registered)
7729 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7731 if (tp_features.platform_drv_registered)
7732 platform_driver_unregister(&tpacpi_pdriver);
7734 if (proc_dir)
7735 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7737 if (tpacpi_wq)
7738 destroy_workqueue(tpacpi_wq);
7740 kfree(thinkpad_id.bios_version_str);
7741 kfree(thinkpad_id.ec_version_str);
7742 kfree(thinkpad_id.model_str);
7746 static int __init thinkpad_acpi_module_init(void)
7748 int ret, i;
7750 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7752 /* Parameter checking */
7753 if (hotkey_report_mode > 2)
7754 return -EINVAL;
7756 /* Driver-level probe */
7758 ret = get_thinkpad_model_data(&thinkpad_id);
7759 if (ret) {
7760 printk(TPACPI_ERR
7761 "unable to get DMI data: %d\n", ret);
7762 thinkpad_acpi_module_exit();
7763 return ret;
7765 ret = probe_for_thinkpad();
7766 if (ret) {
7767 thinkpad_acpi_module_exit();
7768 return ret;
7771 /* Driver initialization */
7773 TPACPI_ACPIHANDLE_INIT(ecrd);
7774 TPACPI_ACPIHANDLE_INIT(ecwr);
7776 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7777 if (!tpacpi_wq) {
7778 thinkpad_acpi_module_exit();
7779 return -ENOMEM;
7782 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7783 if (!proc_dir) {
7784 printk(TPACPI_ERR
7785 "unable to create proc dir " TPACPI_PROC_DIR);
7786 thinkpad_acpi_module_exit();
7787 return -ENODEV;
7790 ret = platform_driver_register(&tpacpi_pdriver);
7791 if (ret) {
7792 printk(TPACPI_ERR
7793 "unable to register main platform driver\n");
7794 thinkpad_acpi_module_exit();
7795 return ret;
7797 tp_features.platform_drv_registered = 1;
7799 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7800 if (ret) {
7801 printk(TPACPI_ERR
7802 "unable to register hwmon platform driver\n");
7803 thinkpad_acpi_module_exit();
7804 return ret;
7806 tp_features.sensors_pdrv_registered = 1;
7808 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7809 if (!ret) {
7810 tp_features.platform_drv_attrs_registered = 1;
7811 ret = tpacpi_create_driver_attributes(
7812 &tpacpi_hwmon_pdriver.driver);
7814 if (ret) {
7815 printk(TPACPI_ERR
7816 "unable to create sysfs driver attributes\n");
7817 thinkpad_acpi_module_exit();
7818 return ret;
7820 tp_features.sensors_pdrv_attrs_registered = 1;
7823 /* Device initialization */
7824 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7825 NULL, 0);
7826 if (IS_ERR(tpacpi_pdev)) {
7827 ret = PTR_ERR(tpacpi_pdev);
7828 tpacpi_pdev = NULL;
7829 printk(TPACPI_ERR "unable to register platform device\n");
7830 thinkpad_acpi_module_exit();
7831 return ret;
7833 tpacpi_sensors_pdev = platform_device_register_simple(
7834 TPACPI_HWMON_DRVR_NAME,
7835 -1, NULL, 0);
7836 if (IS_ERR(tpacpi_sensors_pdev)) {
7837 ret = PTR_ERR(tpacpi_sensors_pdev);
7838 tpacpi_sensors_pdev = NULL;
7839 printk(TPACPI_ERR
7840 "unable to register hwmon platform device\n");
7841 thinkpad_acpi_module_exit();
7842 return ret;
7844 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7845 &dev_attr_thinkpad_acpi_pdev_name);
7846 if (ret) {
7847 printk(TPACPI_ERR
7848 "unable to create sysfs hwmon device attributes\n");
7849 thinkpad_acpi_module_exit();
7850 return ret;
7852 tp_features.sensors_pdev_attrs_registered = 1;
7853 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7854 if (IS_ERR(tpacpi_hwmon)) {
7855 ret = PTR_ERR(tpacpi_hwmon);
7856 tpacpi_hwmon = NULL;
7857 printk(TPACPI_ERR "unable to register hwmon device\n");
7858 thinkpad_acpi_module_exit();
7859 return ret;
7861 mutex_init(&tpacpi_inputdev_send_mutex);
7862 tpacpi_inputdev = input_allocate_device();
7863 if (!tpacpi_inputdev) {
7864 printk(TPACPI_ERR "unable to allocate input device\n");
7865 thinkpad_acpi_module_exit();
7866 return -ENOMEM;
7867 } else {
7868 /* Prepare input device, but don't register */
7869 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7870 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7871 tpacpi_inputdev->id.bustype = BUS_HOST;
7872 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7873 thinkpad_id.vendor :
7874 PCI_VENDOR_ID_IBM;
7875 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7876 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7878 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7879 ret = ibm_init(&ibms_init[i]);
7880 if (ret >= 0 && *ibms_init[i].param)
7881 ret = ibms_init[i].data->write(ibms_init[i].param);
7882 if (ret < 0) {
7883 thinkpad_acpi_module_exit();
7884 return ret;
7887 ret = input_register_device(tpacpi_inputdev);
7888 if (ret < 0) {
7889 printk(TPACPI_ERR "unable to register input device\n");
7890 thinkpad_acpi_module_exit();
7891 return ret;
7892 } else {
7893 tp_features.input_device_registered = 1;
7896 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7897 return 0;
7900 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7903 * This will autoload the driver in almost every ThinkPad
7904 * in widespread use.
7906 * Only _VERY_ old models, like the 240, 240x and 570 lack
7907 * the HKEY event interface.
7909 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7912 * DMI matching for module autoloading
7914 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7915 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7917 * Only models listed in thinkwiki will be supported, so add yours
7918 * if it is not there yet.
7920 #define IBM_BIOS_MODULE_ALIAS(__type) \
7921 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7923 /* Ancient thinkpad BIOSes have to be identified by
7924 * BIOS type or model number, and there are far less
7925 * BIOS types than model numbers... */
7926 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
7928 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7929 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
7930 MODULE_DESCRIPTION(TPACPI_DESC);
7931 MODULE_VERSION(TPACPI_VERSION);
7932 MODULE_LICENSE("GPL");
7934 module_init(thinkpad_acpi_module_init);
7935 module_exit(thinkpad_acpi_module_exit);