thinkpad-acpi: add quirklist engine
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob3981b060b7d5f39cdb9a8460a7307e53b11515e2
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 0x020300
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 #ifdef CONFIG_THINKPAD_ACPI_BAY
250 u32 bay_status:1;
251 u32 bay_eject:1;
252 u32 bay_status2:1;
253 u32 bay_eject2:1;
254 #endif
255 u32 bluetooth:1;
256 u32 hotkey:1;
257 u32 hotkey_mask:1;
258 u32 hotkey_wlsw:1;
259 u32 hotkey_tablet:1;
260 u32 light:1;
261 u32 light_status:1;
262 u32 bright_16levels:1;
263 u32 bright_acpimode:1;
264 u32 wan:1;
265 u32 uwb:1;
266 u32 fan_ctrl_status_undef:1;
267 u32 input_device_registered:1;
268 u32 platform_drv_registered:1;
269 u32 platform_drv_attrs_registered:1;
270 u32 sensors_pdrv_registered:1;
271 u32 sensors_pdrv_attrs_registered:1;
272 u32 sensors_pdev_attrs_registered:1;
273 u32 hotkey_poll_active:1;
274 } tp_features;
276 static struct {
277 u16 hotkey_mask_ff:1;
278 } tp_warned;
280 struct thinkpad_id_data {
281 unsigned int vendor; /* ThinkPad vendor:
282 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
284 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
285 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
287 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
288 u16 ec_model;
289 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
290 u16 ec_release;
292 char *model_str; /* ThinkPad T43 */
293 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
295 static struct thinkpad_id_data thinkpad_id;
297 static enum {
298 TPACPI_LIFE_INIT = 0,
299 TPACPI_LIFE_RUNNING,
300 TPACPI_LIFE_EXITING,
301 } tpacpi_lifecycle;
303 static int experimental;
304 static u32 dbg_level;
306 static struct workqueue_struct *tpacpi_wq;
308 enum led_status_t {
309 TPACPI_LED_OFF = 0,
310 TPACPI_LED_ON,
311 TPACPI_LED_BLINK,
314 /* Special LED class that can defer work */
315 struct tpacpi_led_classdev {
316 struct led_classdev led_classdev;
317 struct work_struct work;
318 enum led_status_t new_state;
319 unsigned int led;
322 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
323 static int dbg_wlswemul;
324 static int tpacpi_wlsw_emulstate;
325 static int dbg_bluetoothemul;
326 static int tpacpi_bluetooth_emulstate;
327 static int dbg_wwanemul;
328 static int tpacpi_wwan_emulstate;
329 static int dbg_uwbemul;
330 static int tpacpi_uwb_emulstate;
331 #endif
334 /*************************************************************************
335 * Debugging helpers
338 #define dbg_printk(a_dbg_level, format, arg...) \
339 do { if (dbg_level & (a_dbg_level)) \
340 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
341 } while (0)
343 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
344 #define vdbg_printk dbg_printk
345 static const char *str_supported(int is_supported);
346 #else
347 #define vdbg_printk(a_dbg_level, format, arg...) \
348 do { } while (0)
349 #endif
351 static void tpacpi_log_usertask(const char * const what)
353 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
354 what, task_tgid_vnr(current));
357 #define tpacpi_disclose_usertask(what, format, arg...) \
358 do { \
359 if (unlikely( \
360 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
361 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
362 printk(TPACPI_DEBUG "%s: PID %d: " format, \
363 what, task_tgid_vnr(current), ## arg); \
365 } while (0)
368 * Quirk handling helpers
370 * ThinkPad IDs and versions seen in the field so far
371 * are two-characters from the set [0-9A-Z], i.e. base 36.
373 * We use values well outside that range as specials.
376 #define TPACPI_MATCH_ANY 0xffffU
377 #define TPACPI_MATCH_UNKNOWN 0U
379 /* TPID('1', 'Y') == 0x5931 */
380 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
382 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
383 { .vendor = PCI_VENDOR_ID_IBM, \
384 .bios = TPID(__id1, __id2), \
385 .ec = TPACPI_MATCH_ANY, \
386 .quirks = (__quirk) }
388 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
389 { .vendor = PCI_VENDOR_ID_LENOVO, \
390 .bios = TPID(__id1, __id2), \
391 .ec = TPACPI_MATCH_ANY, \
392 .quirks = (__quirk) }
394 struct tpacpi_quirk {
395 unsigned int vendor;
396 u16 bios;
397 u16 ec;
398 unsigned long quirks;
402 * tpacpi_check_quirks() - search BIOS/EC version on a list
403 * @qlist: array of &struct tpacpi_quirk
404 * @qlist_size: number of elements in @qlist
406 * Iterates over a quirks list until one is found that matches the
407 * ThinkPad's vendor, BIOS and EC model.
409 * Returns 0 if nothing matches, otherwise returns the quirks field of
410 * the matching &struct tpacpi_quirk entry.
412 * The match criteria is: vendor, ec and bios much match.
414 static unsigned long __init tpacpi_check_quirks(
415 const struct tpacpi_quirk *qlist,
416 unsigned int qlist_size)
418 while (qlist_size) {
419 if ((qlist->vendor == thinkpad_id.vendor ||
420 qlist->vendor == TPACPI_MATCH_ANY) &&
421 (qlist->bios == thinkpad_id.bios_model ||
422 qlist->bios == TPACPI_MATCH_ANY) &&
423 (qlist->ec == thinkpad_id.ec_model ||
424 qlist->ec == TPACPI_MATCH_ANY))
425 return qlist->quirks;
427 qlist_size--;
428 qlist++;
430 return 0;
434 /****************************************************************************
435 ****************************************************************************
437 * ACPI Helpers and device model
439 ****************************************************************************
440 ****************************************************************************/
442 /*************************************************************************
443 * ACPI basic handles
446 static acpi_handle root_handle;
448 #define TPACPI_HANDLE(object, parent, paths...) \
449 static acpi_handle object##_handle; \
450 static acpi_handle *object##_parent = &parent##_handle; \
451 static char *object##_path; \
452 static char *object##_paths[] = { paths }
454 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
455 "\\_SB.PCI.ISA.EC", /* 570 */
456 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
457 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
458 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
459 "\\_SB.PCI0.ICH3.EC0", /* R31 */
460 "\\_SB.PCI0.LPC.EC", /* all others */
463 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
464 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
466 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
467 /* T4x, X31, X40 */
468 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
469 "\\CMS", /* R40, R40e */
470 ); /* all others */
472 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
473 "^HKEY", /* R30, R31 */
474 "HKEY", /* all others */
475 ); /* 570 */
477 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
478 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
479 "\\_SB.PCI0.VID0", /* 770e */
480 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
481 "\\_SB.PCI0.AGP.VID", /* all others */
482 ); /* R30, R31 */
485 /*************************************************************************
486 * ACPI helpers
489 static int acpi_evalf(acpi_handle handle,
490 void *res, char *method, char *fmt, ...)
492 char *fmt0 = fmt;
493 struct acpi_object_list params;
494 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
495 struct acpi_buffer result, *resultp;
496 union acpi_object out_obj;
497 acpi_status status;
498 va_list ap;
499 char res_type;
500 int success;
501 int quiet;
503 if (!*fmt) {
504 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
505 return 0;
508 if (*fmt == 'q') {
509 quiet = 1;
510 fmt++;
511 } else
512 quiet = 0;
514 res_type = *(fmt++);
516 params.count = 0;
517 params.pointer = &in_objs[0];
519 va_start(ap, fmt);
520 while (*fmt) {
521 char c = *(fmt++);
522 switch (c) {
523 case 'd': /* int */
524 in_objs[params.count].integer.value = va_arg(ap, int);
525 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
526 break;
527 /* add more types as needed */
528 default:
529 printk(TPACPI_ERR "acpi_evalf() called "
530 "with invalid format character '%c'\n", c);
531 return 0;
534 va_end(ap);
536 if (res_type != 'v') {
537 result.length = sizeof(out_obj);
538 result.pointer = &out_obj;
539 resultp = &result;
540 } else
541 resultp = NULL;
543 status = acpi_evaluate_object(handle, method, &params, resultp);
545 switch (res_type) {
546 case 'd': /* int */
547 if (res)
548 *(int *)res = out_obj.integer.value;
549 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
550 break;
551 case 'v': /* void */
552 success = status == AE_OK;
553 break;
554 /* add more types as needed */
555 default:
556 printk(TPACPI_ERR "acpi_evalf() called "
557 "with invalid format character '%c'\n", res_type);
558 return 0;
561 if (!success && !quiet)
562 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
563 method, fmt0, status);
565 return success;
568 static int acpi_ec_read(int i, u8 *p)
570 int v;
572 if (ecrd_handle) {
573 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
574 return 0;
575 *p = v;
576 } else {
577 if (ec_read(i, p) < 0)
578 return 0;
581 return 1;
584 static int acpi_ec_write(int i, u8 v)
586 if (ecwr_handle) {
587 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
588 return 0;
589 } else {
590 if (ec_write(i, v) < 0)
591 return 0;
594 return 1;
597 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
598 static int _sta(acpi_handle handle)
600 int status;
602 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
603 status = 0;
605 return status;
607 #endif
609 static int issue_thinkpad_cmos_command(int cmos_cmd)
611 if (!cmos_handle)
612 return -ENXIO;
614 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
615 return -EIO;
617 return 0;
620 /*************************************************************************
621 * ACPI device model
624 #define TPACPI_ACPIHANDLE_INIT(object) \
625 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
626 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
628 static void drv_acpi_handle_init(char *name,
629 acpi_handle *handle, acpi_handle parent,
630 char **paths, int num_paths, char **path)
632 int i;
633 acpi_status status;
635 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
636 name);
638 for (i = 0; i < num_paths; i++) {
639 status = acpi_get_handle(parent, paths[i], handle);
640 if (ACPI_SUCCESS(status)) {
641 *path = paths[i];
642 dbg_printk(TPACPI_DBG_INIT,
643 "Found ACPI handle %s for %s\n",
644 *path, name);
645 return;
649 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
650 name);
651 *handle = NULL;
654 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
656 struct ibm_struct *ibm = data;
658 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
659 return;
661 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
662 return;
664 ibm->acpi->notify(ibm, event);
667 static int __init setup_acpi_notify(struct ibm_struct *ibm)
669 acpi_status status;
670 int rc;
672 BUG_ON(!ibm->acpi);
674 if (!*ibm->acpi->handle)
675 return 0;
677 vdbg_printk(TPACPI_DBG_INIT,
678 "setting up ACPI notify for %s\n", ibm->name);
680 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
681 if (rc < 0) {
682 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
683 ibm->name, rc);
684 return -ENODEV;
687 ibm->acpi->device->driver_data = ibm;
688 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
689 TPACPI_ACPI_EVENT_PREFIX,
690 ibm->name);
692 status = acpi_install_notify_handler(*ibm->acpi->handle,
693 ibm->acpi->type, dispatch_acpi_notify, ibm);
694 if (ACPI_FAILURE(status)) {
695 if (status == AE_ALREADY_EXISTS) {
696 printk(TPACPI_NOTICE
697 "another device driver is already "
698 "handling %s events\n", ibm->name);
699 } else {
700 printk(TPACPI_ERR
701 "acpi_install_notify_handler(%s) failed: %d\n",
702 ibm->name, status);
704 return -ENODEV;
706 ibm->flags.acpi_notify_installed = 1;
707 return 0;
710 static int __init tpacpi_device_add(struct acpi_device *device)
712 return 0;
715 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
717 int rc;
719 dbg_printk(TPACPI_DBG_INIT,
720 "registering %s as an ACPI driver\n", ibm->name);
722 BUG_ON(!ibm->acpi);
724 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
725 if (!ibm->acpi->driver) {
726 printk(TPACPI_ERR
727 "failed to allocate memory for ibm->acpi->driver\n");
728 return -ENOMEM;
731 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
732 ibm->acpi->driver->ids = ibm->acpi->hid;
734 ibm->acpi->driver->ops.add = &tpacpi_device_add;
736 rc = acpi_bus_register_driver(ibm->acpi->driver);
737 if (rc < 0) {
738 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
739 ibm->name, rc);
740 kfree(ibm->acpi->driver);
741 ibm->acpi->driver = NULL;
742 } else if (!rc)
743 ibm->flags.acpi_driver_registered = 1;
745 return rc;
749 /****************************************************************************
750 ****************************************************************************
752 * Procfs Helpers
754 ****************************************************************************
755 ****************************************************************************/
757 static int dispatch_procfs_read(char *page, char **start, off_t off,
758 int count, int *eof, void *data)
760 struct ibm_struct *ibm = data;
761 int len;
763 if (!ibm || !ibm->read)
764 return -EINVAL;
766 len = ibm->read(page);
767 if (len < 0)
768 return len;
770 if (len <= off + count)
771 *eof = 1;
772 *start = page + off;
773 len -= off;
774 if (len > count)
775 len = count;
776 if (len < 0)
777 len = 0;
779 return len;
782 static int dispatch_procfs_write(struct file *file,
783 const char __user *userbuf,
784 unsigned long count, void *data)
786 struct ibm_struct *ibm = data;
787 char *kernbuf;
788 int ret;
790 if (!ibm || !ibm->write)
791 return -EINVAL;
793 kernbuf = kmalloc(count + 2, GFP_KERNEL);
794 if (!kernbuf)
795 return -ENOMEM;
797 if (copy_from_user(kernbuf, userbuf, count)) {
798 kfree(kernbuf);
799 return -EFAULT;
802 kernbuf[count] = 0;
803 strcat(kernbuf, ",");
804 ret = ibm->write(kernbuf);
805 if (ret == 0)
806 ret = count;
808 kfree(kernbuf);
810 return ret;
813 static char *next_cmd(char **cmds)
815 char *start = *cmds;
816 char *end;
818 while ((end = strchr(start, ',')) && end == start)
819 start = end + 1;
821 if (!end)
822 return NULL;
824 *end = 0;
825 *cmds = end + 1;
826 return start;
830 /****************************************************************************
831 ****************************************************************************
833 * Device model: input, hwmon and platform
835 ****************************************************************************
836 ****************************************************************************/
838 static struct platform_device *tpacpi_pdev;
839 static struct platform_device *tpacpi_sensors_pdev;
840 static struct device *tpacpi_hwmon;
841 static struct input_dev *tpacpi_inputdev;
842 static struct mutex tpacpi_inputdev_send_mutex;
843 static LIST_HEAD(tpacpi_all_drivers);
845 static int tpacpi_suspend_handler(struct platform_device *pdev,
846 pm_message_t state)
848 struct ibm_struct *ibm, *itmp;
850 list_for_each_entry_safe(ibm, itmp,
851 &tpacpi_all_drivers,
852 all_drivers) {
853 if (ibm->suspend)
854 (ibm->suspend)(state);
857 return 0;
860 static int tpacpi_resume_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->resume)
868 (ibm->resume)();
871 return 0;
874 static void tpacpi_shutdown_handler(struct platform_device *pdev)
876 struct ibm_struct *ibm, *itmp;
878 list_for_each_entry_safe(ibm, itmp,
879 &tpacpi_all_drivers,
880 all_drivers) {
881 if (ibm->shutdown)
882 (ibm->shutdown)();
886 static struct platform_driver tpacpi_pdriver = {
887 .driver = {
888 .name = TPACPI_DRVR_NAME,
889 .owner = THIS_MODULE,
891 .suspend = tpacpi_suspend_handler,
892 .resume = tpacpi_resume_handler,
893 .shutdown = tpacpi_shutdown_handler,
896 static struct platform_driver tpacpi_hwmon_pdriver = {
897 .driver = {
898 .name = TPACPI_HWMON_DRVR_NAME,
899 .owner = THIS_MODULE,
903 /*************************************************************************
904 * sysfs support helpers
907 struct attribute_set {
908 unsigned int members, max_members;
909 struct attribute_group group;
912 struct attribute_set_obj {
913 struct attribute_set s;
914 struct attribute *a;
915 } __attribute__((packed));
917 static struct attribute_set *create_attr_set(unsigned int max_members,
918 const char *name)
920 struct attribute_set_obj *sobj;
922 if (max_members == 0)
923 return NULL;
925 /* Allocates space for implicit NULL at the end too */
926 sobj = kzalloc(sizeof(struct attribute_set_obj) +
927 max_members * sizeof(struct attribute *),
928 GFP_KERNEL);
929 if (!sobj)
930 return NULL;
931 sobj->s.max_members = max_members;
932 sobj->s.group.attrs = &sobj->a;
933 sobj->s.group.name = name;
935 return &sobj->s;
938 #define destroy_attr_set(_set) \
939 kfree(_set);
941 /* not multi-threaded safe, use it in a single thread per set */
942 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
944 if (!s || !attr)
945 return -EINVAL;
947 if (s->members >= s->max_members)
948 return -ENOMEM;
950 s->group.attrs[s->members] = attr;
951 s->members++;
953 return 0;
956 static int add_many_to_attr_set(struct attribute_set *s,
957 struct attribute **attr,
958 unsigned int count)
960 int i, res;
962 for (i = 0; i < count; i++) {
963 res = add_to_attr_set(s, attr[i]);
964 if (res)
965 return res;
968 return 0;
971 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
973 sysfs_remove_group(kobj, &s->group);
974 destroy_attr_set(s);
977 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
978 sysfs_create_group(_kobj, &_attr_set->group)
980 static int parse_strtoul(const char *buf,
981 unsigned long max, unsigned long *value)
983 char *endp;
985 while (*buf && isspace(*buf))
986 buf++;
987 *value = simple_strtoul(buf, &endp, 0);
988 while (*endp && isspace(*endp))
989 endp++;
990 if (*endp || *value > max)
991 return -EINVAL;
993 return 0;
996 static void tpacpi_disable_brightness_delay(void)
998 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
999 printk(TPACPI_NOTICE
1000 "ACPI backlight control delay disabled\n");
1003 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1005 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1006 union acpi_object *obj;
1007 int rc;
1009 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1010 obj = (union acpi_object *)buffer.pointer;
1011 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1012 printk(TPACPI_ERR "Unknown _BCL data, "
1013 "please report this to %s\n", TPACPI_MAIL);
1014 rc = 0;
1015 } else {
1016 rc = obj->package.count;
1018 } else {
1019 return 0;
1022 kfree(buffer.pointer);
1023 return rc;
1026 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1027 u32 lvl, void *context, void **rv)
1029 char name[ACPI_PATH_SEGMENT_LENGTH];
1030 struct acpi_buffer buffer = { sizeof(name), &name };
1032 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1033 !strncmp("_BCL", name, sizeof(name) - 1)) {
1034 BUG_ON(!rv || !*rv);
1035 **(int **)rv = tpacpi_query_bcl_levels(handle);
1036 return AE_CTRL_TERMINATE;
1037 } else {
1038 return AE_OK;
1043 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1045 static int __init tpacpi_check_std_acpi_brightness_support(void)
1047 int status;
1048 int bcl_levels = 0;
1049 void *bcl_ptr = &bcl_levels;
1051 if (!vid_handle) {
1052 TPACPI_ACPIHANDLE_INIT(vid);
1054 if (!vid_handle)
1055 return 0;
1058 * Search for a _BCL method, and execute it. This is safe on all
1059 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1060 * BIOS in ACPI backlight control mode. We do NOT have to care
1061 * about calling the _BCL method in an enabled video device, any
1062 * will do for our purposes.
1065 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1066 tpacpi_acpi_walk_find_bcl, NULL,
1067 &bcl_ptr);
1069 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1070 tp_features.bright_acpimode = 1;
1071 return (bcl_levels - 2);
1074 return 0;
1077 static int __init tpacpi_new_rfkill(const unsigned int id,
1078 struct rfkill **rfk,
1079 const enum rfkill_type rfktype,
1080 const char *name,
1081 const bool set_default,
1082 int (*toggle_radio)(void *, enum rfkill_state),
1083 int (*get_state)(void *, enum rfkill_state *))
1085 int res;
1086 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1088 res = get_state(NULL, &initial_state);
1089 if (res < 0) {
1090 printk(TPACPI_ERR
1091 "failed to read initial state for %s, error %d; "
1092 "will turn radio off\n", name, res);
1093 } else if (set_default) {
1094 /* try to set the initial state as the default for the rfkill
1095 * type, since we ask the firmware to preserve it across S5 in
1096 * NVRAM */
1097 if (rfkill_set_default(rfktype,
1098 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1099 RFKILL_STATE_UNBLOCKED :
1100 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1101 vdbg_printk(TPACPI_DBG_RFKILL,
1102 "Default state for %s cannot be changed\n",
1103 name);
1106 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1107 if (!*rfk) {
1108 printk(TPACPI_ERR
1109 "failed to allocate memory for rfkill class\n");
1110 return -ENOMEM;
1113 (*rfk)->name = name;
1114 (*rfk)->get_state = get_state;
1115 (*rfk)->toggle_radio = toggle_radio;
1116 (*rfk)->state = initial_state;
1118 res = rfkill_register(*rfk);
1119 if (res < 0) {
1120 printk(TPACPI_ERR
1121 "failed to register %s rfkill switch: %d\n",
1122 name, res);
1123 rfkill_free(*rfk);
1124 *rfk = NULL;
1125 return res;
1128 return 0;
1131 static void printk_deprecated_attribute(const char * const what,
1132 const char * const details)
1134 tpacpi_log_usertask("deprecated sysfs attribute");
1135 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1136 "will be removed. %s\n",
1137 what, details);
1140 static void printk_deprecated_rfkill_attribute(const char * const what)
1142 printk_deprecated_attribute(what,
1143 "Please switch to generic rfkill before year 2010");
1146 /*************************************************************************
1147 * thinkpad-acpi driver attributes
1150 /* interface_version --------------------------------------------------- */
1151 static ssize_t tpacpi_driver_interface_version_show(
1152 struct device_driver *drv,
1153 char *buf)
1155 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1158 static DRIVER_ATTR(interface_version, S_IRUGO,
1159 tpacpi_driver_interface_version_show, NULL);
1161 /* debug_level --------------------------------------------------------- */
1162 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1163 char *buf)
1165 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1168 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1169 const char *buf, size_t count)
1171 unsigned long t;
1173 if (parse_strtoul(buf, 0xffff, &t))
1174 return -EINVAL;
1176 dbg_level = t;
1178 return count;
1181 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1182 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1184 /* version ------------------------------------------------------------- */
1185 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1186 char *buf)
1188 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1189 TPACPI_DESC, TPACPI_VERSION);
1192 static DRIVER_ATTR(version, S_IRUGO,
1193 tpacpi_driver_version_show, NULL);
1195 /* --------------------------------------------------------------------- */
1197 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1199 static void tpacpi_send_radiosw_update(void);
1201 /* wlsw_emulstate ------------------------------------------------------ */
1202 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1203 char *buf)
1205 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1208 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1209 const char *buf, size_t count)
1211 unsigned long t;
1213 if (parse_strtoul(buf, 1, &t))
1214 return -EINVAL;
1216 if (tpacpi_wlsw_emulstate != t) {
1217 tpacpi_wlsw_emulstate = !!t;
1218 tpacpi_send_radiosw_update();
1219 } else
1220 tpacpi_wlsw_emulstate = !!t;
1222 return count;
1225 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1226 tpacpi_driver_wlsw_emulstate_show,
1227 tpacpi_driver_wlsw_emulstate_store);
1229 /* bluetooth_emulstate ------------------------------------------------- */
1230 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1231 struct device_driver *drv,
1232 char *buf)
1234 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1237 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1238 struct device_driver *drv,
1239 const char *buf, size_t count)
1241 unsigned long t;
1243 if (parse_strtoul(buf, 1, &t))
1244 return -EINVAL;
1246 tpacpi_bluetooth_emulstate = !!t;
1248 return count;
1251 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1252 tpacpi_driver_bluetooth_emulstate_show,
1253 tpacpi_driver_bluetooth_emulstate_store);
1255 /* wwan_emulstate ------------------------------------------------- */
1256 static ssize_t tpacpi_driver_wwan_emulstate_show(
1257 struct device_driver *drv,
1258 char *buf)
1260 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1263 static ssize_t tpacpi_driver_wwan_emulstate_store(
1264 struct device_driver *drv,
1265 const char *buf, size_t count)
1267 unsigned long t;
1269 if (parse_strtoul(buf, 1, &t))
1270 return -EINVAL;
1272 tpacpi_wwan_emulstate = !!t;
1274 return count;
1277 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1278 tpacpi_driver_wwan_emulstate_show,
1279 tpacpi_driver_wwan_emulstate_store);
1281 /* uwb_emulstate ------------------------------------------------- */
1282 static ssize_t tpacpi_driver_uwb_emulstate_show(
1283 struct device_driver *drv,
1284 char *buf)
1286 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1289 static ssize_t tpacpi_driver_uwb_emulstate_store(
1290 struct device_driver *drv,
1291 const char *buf, size_t count)
1293 unsigned long t;
1295 if (parse_strtoul(buf, 1, &t))
1296 return -EINVAL;
1298 tpacpi_uwb_emulstate = !!t;
1300 return count;
1303 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1304 tpacpi_driver_uwb_emulstate_show,
1305 tpacpi_driver_uwb_emulstate_store);
1306 #endif
1308 /* --------------------------------------------------------------------- */
1310 static struct driver_attribute *tpacpi_driver_attributes[] = {
1311 &driver_attr_debug_level, &driver_attr_version,
1312 &driver_attr_interface_version,
1315 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1317 int i, res;
1319 i = 0;
1320 res = 0;
1321 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1322 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1323 i++;
1326 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1327 if (!res && dbg_wlswemul)
1328 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1329 if (!res && dbg_bluetoothemul)
1330 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1331 if (!res && dbg_wwanemul)
1332 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1333 if (!res && dbg_uwbemul)
1334 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1335 #endif
1337 return res;
1340 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1342 int i;
1344 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1345 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1347 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1348 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1349 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1350 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1351 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1352 #endif
1355 /****************************************************************************
1356 ****************************************************************************
1358 * Subdrivers
1360 ****************************************************************************
1361 ****************************************************************************/
1363 /*************************************************************************
1364 * thinkpad-acpi init subdriver
1367 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1369 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1370 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1372 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1373 (thinkpad_id.bios_version_str) ?
1374 thinkpad_id.bios_version_str : "unknown",
1375 (thinkpad_id.ec_version_str) ?
1376 thinkpad_id.ec_version_str : "unknown");
1378 if (thinkpad_id.vendor && thinkpad_id.model_str)
1379 printk(TPACPI_INFO "%s %s, model %s\n",
1380 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1381 "IBM" : ((thinkpad_id.vendor ==
1382 PCI_VENDOR_ID_LENOVO) ?
1383 "Lenovo" : "Unknown vendor"),
1384 thinkpad_id.model_str,
1385 (thinkpad_id.nummodel_str) ?
1386 thinkpad_id.nummodel_str : "unknown");
1388 return 0;
1391 static int thinkpad_acpi_driver_read(char *p)
1393 int len = 0;
1395 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1396 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1398 return len;
1401 static struct ibm_struct thinkpad_acpi_driver_data = {
1402 .name = "driver",
1403 .read = thinkpad_acpi_driver_read,
1406 /*************************************************************************
1407 * Hotkey subdriver
1410 enum { /* hot key scan codes (derived from ACPI DSDT) */
1411 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1412 TP_ACPI_HOTKEYSCAN_FNF2,
1413 TP_ACPI_HOTKEYSCAN_FNF3,
1414 TP_ACPI_HOTKEYSCAN_FNF4,
1415 TP_ACPI_HOTKEYSCAN_FNF5,
1416 TP_ACPI_HOTKEYSCAN_FNF6,
1417 TP_ACPI_HOTKEYSCAN_FNF7,
1418 TP_ACPI_HOTKEYSCAN_FNF8,
1419 TP_ACPI_HOTKEYSCAN_FNF9,
1420 TP_ACPI_HOTKEYSCAN_FNF10,
1421 TP_ACPI_HOTKEYSCAN_FNF11,
1422 TP_ACPI_HOTKEYSCAN_FNF12,
1423 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1424 TP_ACPI_HOTKEYSCAN_FNINSERT,
1425 TP_ACPI_HOTKEYSCAN_FNDELETE,
1426 TP_ACPI_HOTKEYSCAN_FNHOME,
1427 TP_ACPI_HOTKEYSCAN_FNEND,
1428 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1429 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1430 TP_ACPI_HOTKEYSCAN_FNSPACE,
1431 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1432 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1433 TP_ACPI_HOTKEYSCAN_MUTE,
1434 TP_ACPI_HOTKEYSCAN_THINKPAD,
1437 enum { /* Keys available through NVRAM polling */
1438 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1439 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1442 enum { /* Positions of some of the keys in hotkey masks */
1443 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1444 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1445 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1446 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1447 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1448 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1449 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1450 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1451 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1452 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1453 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1456 enum { /* NVRAM to ACPI HKEY group map */
1457 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1458 TP_ACPI_HKEY_ZOOM_MASK |
1459 TP_ACPI_HKEY_DISPSWTCH_MASK |
1460 TP_ACPI_HKEY_HIBERNATE_MASK,
1461 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1462 TP_ACPI_HKEY_BRGHTDWN_MASK,
1463 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1464 TP_ACPI_HKEY_VOLDWN_MASK |
1465 TP_ACPI_HKEY_MUTE_MASK,
1468 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1469 struct tp_nvram_state {
1470 u16 thinkpad_toggle:1;
1471 u16 zoom_toggle:1;
1472 u16 display_toggle:1;
1473 u16 thinklight_toggle:1;
1474 u16 hibernate_toggle:1;
1475 u16 displayexp_toggle:1;
1476 u16 display_state:1;
1477 u16 brightness_toggle:1;
1478 u16 volume_toggle:1;
1479 u16 mute:1;
1481 u8 brightness_level;
1482 u8 volume_level;
1485 static struct task_struct *tpacpi_hotkey_task;
1486 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1487 static int hotkey_poll_freq = 10; /* Hz */
1488 static struct mutex hotkey_thread_mutex;
1489 static struct mutex hotkey_thread_data_mutex;
1490 static unsigned int hotkey_config_change;
1492 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1494 #define hotkey_source_mask 0U
1496 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1498 static struct mutex hotkey_mutex;
1500 static enum { /* Reasons for waking up */
1501 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1502 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1503 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1504 } hotkey_wakeup_reason;
1506 static int hotkey_autosleep_ack;
1508 static u32 hotkey_orig_mask;
1509 static u32 hotkey_all_mask;
1510 static u32 hotkey_reserved_mask;
1511 static u32 hotkey_mask;
1513 static unsigned int hotkey_report_mode;
1515 static u16 *hotkey_keycode_map;
1517 static struct attribute_set *hotkey_dev_attributes;
1519 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1520 #define HOTKEY_CONFIG_CRITICAL_START \
1521 do { \
1522 mutex_lock(&hotkey_thread_data_mutex); \
1523 hotkey_config_change++; \
1524 } while (0);
1525 #define HOTKEY_CONFIG_CRITICAL_END \
1526 mutex_unlock(&hotkey_thread_data_mutex);
1527 #else
1528 #define HOTKEY_CONFIG_CRITICAL_START
1529 #define HOTKEY_CONFIG_CRITICAL_END
1530 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1532 /* HKEY.MHKG() return bits */
1533 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1535 static int hotkey_get_wlsw(int *status)
1537 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1538 if (dbg_wlswemul) {
1539 *status = !!tpacpi_wlsw_emulstate;
1540 return 0;
1542 #endif
1543 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1544 return -EIO;
1545 return 0;
1548 static int hotkey_get_tablet_mode(int *status)
1550 int s;
1552 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1553 return -EIO;
1555 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1556 return 0;
1560 * Call with hotkey_mutex held
1562 static int hotkey_mask_get(void)
1564 u32 m = 0;
1566 if (tp_features.hotkey_mask) {
1567 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1568 return -EIO;
1570 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1572 return 0;
1576 * Call with hotkey_mutex held
1578 static int hotkey_mask_set(u32 mask)
1580 int i;
1581 int rc = 0;
1583 if (tp_features.hotkey_mask) {
1584 if (!tp_warned.hotkey_mask_ff &&
1585 (mask == 0xffff || mask == 0xffffff ||
1586 mask == 0xffffffff)) {
1587 tp_warned.hotkey_mask_ff = 1;
1588 printk(TPACPI_NOTICE
1589 "setting the hotkey mask to 0x%08x is likely "
1590 "not the best way to go about it\n", mask);
1591 printk(TPACPI_NOTICE
1592 "please consider using the driver defaults, "
1593 "and refer to up-to-date thinkpad-acpi "
1594 "documentation\n");
1597 HOTKEY_CONFIG_CRITICAL_START
1598 for (i = 0; i < 32; i++) {
1599 u32 m = 1 << i;
1600 /* enable in firmware mask only keys not in NVRAM
1601 * mode, but enable the key in the cached hotkey_mask
1602 * regardless of mode, or the key will end up
1603 * disabled by hotkey_mask_get() */
1604 if (!acpi_evalf(hkey_handle,
1605 NULL, "MHKM", "vdd", i + 1,
1606 !!((mask & ~hotkey_source_mask) & m))) {
1607 rc = -EIO;
1608 break;
1609 } else {
1610 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1613 HOTKEY_CONFIG_CRITICAL_END
1615 /* hotkey_mask_get must be called unconditionally below */
1616 if (!hotkey_mask_get() && !rc &&
1617 (hotkey_mask & ~hotkey_source_mask) !=
1618 (mask & ~hotkey_source_mask)) {
1619 printk(TPACPI_NOTICE
1620 "requested hot key mask 0x%08x, but "
1621 "firmware forced it to 0x%08x\n",
1622 mask, hotkey_mask);
1624 } else {
1625 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1626 HOTKEY_CONFIG_CRITICAL_START
1627 hotkey_mask = mask & hotkey_source_mask;
1628 HOTKEY_CONFIG_CRITICAL_END
1629 hotkey_mask_get();
1630 if (hotkey_mask != mask) {
1631 printk(TPACPI_NOTICE
1632 "requested hot key mask 0x%08x, "
1633 "forced to 0x%08x (NVRAM poll mask is "
1634 "0x%08x): no firmware mask support\n",
1635 mask, hotkey_mask, hotkey_source_mask);
1637 #else
1638 hotkey_mask_get();
1639 rc = -ENXIO;
1640 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1643 return rc;
1646 static int hotkey_status_get(int *status)
1648 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1649 return -EIO;
1651 return 0;
1654 static int hotkey_status_set(bool enable)
1656 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1657 return -EIO;
1659 return 0;
1662 static void tpacpi_input_send_tabletsw(void)
1664 int state;
1666 if (tp_features.hotkey_tablet &&
1667 !hotkey_get_tablet_mode(&state)) {
1668 mutex_lock(&tpacpi_inputdev_send_mutex);
1670 input_report_switch(tpacpi_inputdev,
1671 SW_TABLET_MODE, !!state);
1672 input_sync(tpacpi_inputdev);
1674 mutex_unlock(&tpacpi_inputdev_send_mutex);
1678 static void tpacpi_input_send_key(unsigned int scancode)
1680 unsigned int keycode;
1682 keycode = hotkey_keycode_map[scancode];
1684 if (keycode != KEY_RESERVED) {
1685 mutex_lock(&tpacpi_inputdev_send_mutex);
1687 input_report_key(tpacpi_inputdev, keycode, 1);
1688 if (keycode == KEY_UNKNOWN)
1689 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1690 scancode);
1691 input_sync(tpacpi_inputdev);
1693 input_report_key(tpacpi_inputdev, keycode, 0);
1694 if (keycode == KEY_UNKNOWN)
1695 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1696 scancode);
1697 input_sync(tpacpi_inputdev);
1699 mutex_unlock(&tpacpi_inputdev_send_mutex);
1703 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1704 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1706 static void tpacpi_hotkey_send_key(unsigned int scancode)
1708 tpacpi_input_send_key(scancode);
1709 if (hotkey_report_mode < 2) {
1710 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1711 0x80, 0x1001 + scancode);
1715 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1717 u8 d;
1719 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1720 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1721 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1722 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1723 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1724 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1726 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1727 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1728 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1730 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1731 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1732 n->displayexp_toggle =
1733 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1735 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1736 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1737 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1738 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1739 n->brightness_toggle =
1740 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1742 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1743 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1744 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1745 >> TP_NVRAM_POS_LEVEL_VOLUME;
1746 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1747 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1751 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1752 do { \
1753 if ((mask & (1 << __scancode)) && \
1754 oldn->__member != newn->__member) \
1755 tpacpi_hotkey_send_key(__scancode); \
1756 } while (0)
1758 #define TPACPI_MAY_SEND_KEY(__scancode) \
1759 do { if (mask & (1 << __scancode)) \
1760 tpacpi_hotkey_send_key(__scancode); } while (0)
1762 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1763 struct tp_nvram_state *newn,
1764 u32 mask)
1766 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1767 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1768 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1769 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1771 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1773 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1775 /* handle volume */
1776 if (oldn->volume_toggle != newn->volume_toggle) {
1777 if (oldn->mute != newn->mute) {
1778 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1780 if (oldn->volume_level > newn->volume_level) {
1781 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1782 } else if (oldn->volume_level < newn->volume_level) {
1783 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1784 } else if (oldn->mute == newn->mute) {
1785 /* repeated key presses that didn't change state */
1786 if (newn->mute) {
1787 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1788 } else if (newn->volume_level != 0) {
1789 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1790 } else {
1791 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1796 /* handle brightness */
1797 if (oldn->brightness_toggle != newn->brightness_toggle) {
1798 if (oldn->brightness_level < newn->brightness_level) {
1799 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1800 } else if (oldn->brightness_level > newn->brightness_level) {
1801 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1802 } else {
1803 /* repeated key presses that didn't change state */
1804 if (newn->brightness_level != 0) {
1805 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1806 } else {
1807 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1813 #undef TPACPI_COMPARE_KEY
1814 #undef TPACPI_MAY_SEND_KEY
1816 static int hotkey_kthread(void *data)
1818 struct tp_nvram_state s[2];
1819 u32 mask;
1820 unsigned int si, so;
1821 unsigned long t;
1822 unsigned int change_detector, must_reset;
1824 mutex_lock(&hotkey_thread_mutex);
1826 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1827 goto exit;
1829 set_freezable();
1831 so = 0;
1832 si = 1;
1833 t = 0;
1835 /* Initial state for compares */
1836 mutex_lock(&hotkey_thread_data_mutex);
1837 change_detector = hotkey_config_change;
1838 mask = hotkey_source_mask & hotkey_mask;
1839 mutex_unlock(&hotkey_thread_data_mutex);
1840 hotkey_read_nvram(&s[so], mask);
1842 while (!kthread_should_stop() && hotkey_poll_freq) {
1843 if (t == 0)
1844 t = 1000/hotkey_poll_freq;
1845 t = msleep_interruptible(t);
1846 if (unlikely(kthread_should_stop()))
1847 break;
1848 must_reset = try_to_freeze();
1849 if (t > 0 && !must_reset)
1850 continue;
1852 mutex_lock(&hotkey_thread_data_mutex);
1853 if (must_reset || hotkey_config_change != change_detector) {
1854 /* forget old state on thaw or config change */
1855 si = so;
1856 t = 0;
1857 change_detector = hotkey_config_change;
1859 mask = hotkey_source_mask & hotkey_mask;
1860 mutex_unlock(&hotkey_thread_data_mutex);
1862 if (likely(mask)) {
1863 hotkey_read_nvram(&s[si], mask);
1864 if (likely(si != so)) {
1865 hotkey_compare_and_issue_event(&s[so], &s[si],
1866 mask);
1870 so = si;
1871 si ^= 1;
1874 exit:
1875 mutex_unlock(&hotkey_thread_mutex);
1876 return 0;
1879 static void hotkey_poll_stop_sync(void)
1881 if (tpacpi_hotkey_task) {
1882 if (frozen(tpacpi_hotkey_task) ||
1883 freezing(tpacpi_hotkey_task))
1884 thaw_process(tpacpi_hotkey_task);
1886 kthread_stop(tpacpi_hotkey_task);
1887 tpacpi_hotkey_task = NULL;
1888 mutex_lock(&hotkey_thread_mutex);
1889 /* at this point, the thread did exit */
1890 mutex_unlock(&hotkey_thread_mutex);
1894 /* call with hotkey_mutex held */
1895 static void hotkey_poll_setup(int may_warn)
1897 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1898 hotkey_poll_freq > 0 &&
1899 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1900 if (!tpacpi_hotkey_task) {
1901 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1902 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1903 if (IS_ERR(tpacpi_hotkey_task)) {
1904 tpacpi_hotkey_task = NULL;
1905 printk(TPACPI_ERR
1906 "could not create kernel thread "
1907 "for hotkey polling\n");
1910 } else {
1911 hotkey_poll_stop_sync();
1912 if (may_warn &&
1913 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1914 printk(TPACPI_NOTICE
1915 "hot keys 0x%08x require polling, "
1916 "which is currently disabled\n",
1917 hotkey_source_mask);
1922 static void hotkey_poll_setup_safe(int may_warn)
1924 mutex_lock(&hotkey_mutex);
1925 hotkey_poll_setup(may_warn);
1926 mutex_unlock(&hotkey_mutex);
1929 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1931 static void hotkey_poll_setup_safe(int __unused)
1935 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1937 static int hotkey_inputdev_open(struct input_dev *dev)
1939 switch (tpacpi_lifecycle) {
1940 case TPACPI_LIFE_INIT:
1942 * hotkey_init will call hotkey_poll_setup_safe
1943 * at the appropriate moment
1945 return 0;
1946 case TPACPI_LIFE_EXITING:
1947 return -EBUSY;
1948 case TPACPI_LIFE_RUNNING:
1949 hotkey_poll_setup_safe(0);
1950 return 0;
1953 /* Should only happen if tpacpi_lifecycle is corrupt */
1954 BUG();
1955 return -EBUSY;
1958 static void hotkey_inputdev_close(struct input_dev *dev)
1960 /* disable hotkey polling when possible */
1961 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1962 hotkey_poll_setup_safe(0);
1965 /* sysfs hotkey enable ------------------------------------------------- */
1966 static ssize_t hotkey_enable_show(struct device *dev,
1967 struct device_attribute *attr,
1968 char *buf)
1970 int res, status;
1972 printk_deprecated_attribute("hotkey_enable",
1973 "Hotkey reporting is always enabled");
1975 res = hotkey_status_get(&status);
1976 if (res)
1977 return res;
1979 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1982 static ssize_t hotkey_enable_store(struct device *dev,
1983 struct device_attribute *attr,
1984 const char *buf, size_t count)
1986 unsigned long t;
1988 printk_deprecated_attribute("hotkey_enable",
1989 "Hotkeys can be disabled through hotkey_mask");
1991 if (parse_strtoul(buf, 1, &t))
1992 return -EINVAL;
1994 if (t == 0)
1995 return -EPERM;
1997 return count;
2000 static struct device_attribute dev_attr_hotkey_enable =
2001 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2002 hotkey_enable_show, hotkey_enable_store);
2004 /* sysfs hotkey mask --------------------------------------------------- */
2005 static ssize_t hotkey_mask_show(struct device *dev,
2006 struct device_attribute *attr,
2007 char *buf)
2009 int res;
2011 if (mutex_lock_killable(&hotkey_mutex))
2012 return -ERESTARTSYS;
2013 res = hotkey_mask_get();
2014 mutex_unlock(&hotkey_mutex);
2016 return (res)?
2017 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2020 static ssize_t hotkey_mask_store(struct device *dev,
2021 struct device_attribute *attr,
2022 const char *buf, size_t count)
2024 unsigned long t;
2025 int res;
2027 if (parse_strtoul(buf, 0xffffffffUL, &t))
2028 return -EINVAL;
2030 if (mutex_lock_killable(&hotkey_mutex))
2031 return -ERESTARTSYS;
2033 res = hotkey_mask_set(t);
2035 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2036 hotkey_poll_setup(1);
2037 #endif
2039 mutex_unlock(&hotkey_mutex);
2041 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2043 return (res) ? res : count;
2046 static struct device_attribute dev_attr_hotkey_mask =
2047 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2048 hotkey_mask_show, hotkey_mask_store);
2050 /* sysfs hotkey bios_enabled ------------------------------------------- */
2051 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2052 struct device_attribute *attr,
2053 char *buf)
2055 return sprintf(buf, "0\n");
2058 static struct device_attribute dev_attr_hotkey_bios_enabled =
2059 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2061 /* sysfs hotkey bios_mask ---------------------------------------------- */
2062 static ssize_t hotkey_bios_mask_show(struct device *dev,
2063 struct device_attribute *attr,
2064 char *buf)
2066 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2069 static struct device_attribute dev_attr_hotkey_bios_mask =
2070 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2072 /* sysfs hotkey all_mask ----------------------------------------------- */
2073 static ssize_t hotkey_all_mask_show(struct device *dev,
2074 struct device_attribute *attr,
2075 char *buf)
2077 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2078 hotkey_all_mask | hotkey_source_mask);
2081 static struct device_attribute dev_attr_hotkey_all_mask =
2082 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2084 /* sysfs hotkey recommended_mask --------------------------------------- */
2085 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2086 struct device_attribute *attr,
2087 char *buf)
2089 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2090 (hotkey_all_mask | hotkey_source_mask)
2091 & ~hotkey_reserved_mask);
2094 static struct device_attribute dev_attr_hotkey_recommended_mask =
2095 __ATTR(hotkey_recommended_mask, S_IRUGO,
2096 hotkey_recommended_mask_show, NULL);
2098 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2100 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2101 static ssize_t hotkey_source_mask_show(struct device *dev,
2102 struct device_attribute *attr,
2103 char *buf)
2105 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2108 static ssize_t hotkey_source_mask_store(struct device *dev,
2109 struct device_attribute *attr,
2110 const char *buf, size_t count)
2112 unsigned long t;
2114 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2115 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2116 return -EINVAL;
2118 if (mutex_lock_killable(&hotkey_mutex))
2119 return -ERESTARTSYS;
2121 HOTKEY_CONFIG_CRITICAL_START
2122 hotkey_source_mask = t;
2123 HOTKEY_CONFIG_CRITICAL_END
2125 hotkey_poll_setup(1);
2127 mutex_unlock(&hotkey_mutex);
2129 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2131 return count;
2134 static struct device_attribute dev_attr_hotkey_source_mask =
2135 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2136 hotkey_source_mask_show, hotkey_source_mask_store);
2138 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2139 static ssize_t hotkey_poll_freq_show(struct device *dev,
2140 struct device_attribute *attr,
2141 char *buf)
2143 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2146 static ssize_t hotkey_poll_freq_store(struct device *dev,
2147 struct device_attribute *attr,
2148 const char *buf, size_t count)
2150 unsigned long t;
2152 if (parse_strtoul(buf, 25, &t))
2153 return -EINVAL;
2155 if (mutex_lock_killable(&hotkey_mutex))
2156 return -ERESTARTSYS;
2158 hotkey_poll_freq = t;
2160 hotkey_poll_setup(1);
2161 mutex_unlock(&hotkey_mutex);
2163 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2165 return count;
2168 static struct device_attribute dev_attr_hotkey_poll_freq =
2169 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2170 hotkey_poll_freq_show, hotkey_poll_freq_store);
2172 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2174 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2175 static ssize_t hotkey_radio_sw_show(struct device *dev,
2176 struct device_attribute *attr,
2177 char *buf)
2179 int res, s;
2180 res = hotkey_get_wlsw(&s);
2181 if (res < 0)
2182 return res;
2184 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2187 static struct device_attribute dev_attr_hotkey_radio_sw =
2188 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2190 static void hotkey_radio_sw_notify_change(void)
2192 if (tp_features.hotkey_wlsw)
2193 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2194 "hotkey_radio_sw");
2197 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2198 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2199 struct device_attribute *attr,
2200 char *buf)
2202 int res, s;
2203 res = hotkey_get_tablet_mode(&s);
2204 if (res < 0)
2205 return res;
2207 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2210 static struct device_attribute dev_attr_hotkey_tablet_mode =
2211 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2213 static void hotkey_tablet_mode_notify_change(void)
2215 if (tp_features.hotkey_tablet)
2216 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2217 "hotkey_tablet_mode");
2220 /* sysfs hotkey report_mode -------------------------------------------- */
2221 static ssize_t hotkey_report_mode_show(struct device *dev,
2222 struct device_attribute *attr,
2223 char *buf)
2225 return snprintf(buf, PAGE_SIZE, "%d\n",
2226 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2229 static struct device_attribute dev_attr_hotkey_report_mode =
2230 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2232 /* sysfs wakeup reason (pollable) -------------------------------------- */
2233 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2234 struct device_attribute *attr,
2235 char *buf)
2237 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2240 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2241 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2243 static void hotkey_wakeup_reason_notify_change(void)
2245 if (tp_features.hotkey_mask)
2246 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2247 "wakeup_reason");
2250 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2251 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2252 struct device_attribute *attr,
2253 char *buf)
2255 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2258 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2259 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2260 hotkey_wakeup_hotunplug_complete_show, NULL);
2262 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2264 if (tp_features.hotkey_mask)
2265 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2266 "wakeup_hotunplug_complete");
2269 /* --------------------------------------------------------------------- */
2271 static struct attribute *hotkey_attributes[] __initdata = {
2272 &dev_attr_hotkey_enable.attr,
2273 &dev_attr_hotkey_bios_enabled.attr,
2274 &dev_attr_hotkey_report_mode.attr,
2275 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2276 &dev_attr_hotkey_mask.attr,
2277 &dev_attr_hotkey_all_mask.attr,
2278 &dev_attr_hotkey_recommended_mask.attr,
2279 &dev_attr_hotkey_source_mask.attr,
2280 &dev_attr_hotkey_poll_freq.attr,
2281 #endif
2284 static struct attribute *hotkey_mask_attributes[] __initdata = {
2285 &dev_attr_hotkey_bios_mask.attr,
2286 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2287 &dev_attr_hotkey_mask.attr,
2288 &dev_attr_hotkey_all_mask.attr,
2289 &dev_attr_hotkey_recommended_mask.attr,
2290 #endif
2291 &dev_attr_hotkey_wakeup_reason.attr,
2292 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2295 static void bluetooth_update_rfk(void);
2296 static void wan_update_rfk(void);
2297 static void uwb_update_rfk(void);
2298 static void tpacpi_send_radiosw_update(void)
2300 int wlsw;
2302 /* Sync these BEFORE sending any rfkill events */
2303 if (tp_features.bluetooth)
2304 bluetooth_update_rfk();
2305 if (tp_features.wan)
2306 wan_update_rfk();
2307 if (tp_features.uwb)
2308 uwb_update_rfk();
2310 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2311 mutex_lock(&tpacpi_inputdev_send_mutex);
2313 input_report_switch(tpacpi_inputdev,
2314 SW_RFKILL_ALL, !!wlsw);
2315 input_sync(tpacpi_inputdev);
2317 mutex_unlock(&tpacpi_inputdev_send_mutex);
2319 hotkey_radio_sw_notify_change();
2322 static void hotkey_exit(void)
2324 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2325 hotkey_poll_stop_sync();
2326 #endif
2328 if (hotkey_dev_attributes)
2329 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2331 kfree(hotkey_keycode_map);
2333 if (tp_features.hotkey) {
2334 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2335 "restoring original hot key mask\n");
2336 /* no short-circuit boolean operator below! */
2337 if ((hotkey_mask_set(hotkey_orig_mask) |
2338 hotkey_status_set(false)) != 0)
2339 printk(TPACPI_ERR
2340 "failed to restore hot key mask "
2341 "to BIOS defaults\n");
2345 static int __init hotkey_init(struct ibm_init_struct *iibm)
2347 /* Requirements for changing the default keymaps:
2349 * 1. Many of the keys are mapped to KEY_RESERVED for very
2350 * good reasons. Do not change them unless you have deep
2351 * knowledge on the IBM and Lenovo ThinkPad firmware for
2352 * the various ThinkPad models. The driver behaves
2353 * differently for KEY_RESERVED: such keys have their
2354 * hot key mask *unset* in mask_recommended, and also
2355 * in the initial hot key mask programmed into the
2356 * firmware at driver load time, which means the firm-
2357 * ware may react very differently if you change them to
2358 * something else;
2360 * 2. You must be subscribed to the linux-thinkpad and
2361 * ibm-acpi-devel mailing lists, and you should read the
2362 * list archives since 2007 if you want to change the
2363 * keymaps. This requirement exists so that you will
2364 * know the past history of problems with the thinkpad-
2365 * acpi driver keymaps, and also that you will be
2366 * listening to any bug reports;
2368 * 3. Do not send thinkpad-acpi specific patches directly to
2369 * for merging, *ever*. Send them to the linux-acpi
2370 * mailinglist for comments. Merging is to be done only
2371 * through acpi-test and the ACPI maintainer.
2373 * If the above is too much to ask, don't change the keymap.
2374 * Ask the thinkpad-acpi maintainer to do it, instead.
2376 static u16 ibm_keycode_map[] __initdata = {
2377 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2378 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2379 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2380 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2382 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2383 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2384 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2385 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2387 /* brightness: firmware always reacts to them, unless
2388 * X.org did some tricks in the radeon BIOS scratch
2389 * registers of *some* models */
2390 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2391 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2393 /* Thinklight: firmware always react to it */
2394 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2396 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2397 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2399 /* Volume: firmware always react to it and reprograms
2400 * the built-in *extra* mixer. Never map it to control
2401 * another mixer by default. */
2402 KEY_RESERVED, /* 0x14: VOLUME UP */
2403 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2404 KEY_RESERVED, /* 0x16: MUTE */
2406 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2408 /* (assignments unknown, please report if found) */
2409 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2410 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2412 static u16 lenovo_keycode_map[] __initdata = {
2413 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2414 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2415 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2416 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2418 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2419 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2420 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2421 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2423 /* These either have to go through ACPI video, or
2424 * act like in the IBM ThinkPads, so don't ever
2425 * enable them by default */
2426 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2427 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2429 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2431 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2432 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2434 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2435 * react to it and reprograms the built-in *extra* mixer.
2436 * Never map it to control another mixer by default.
2438 * T60?, T61, R60?, R61: firmware and EC tries to send
2439 * these over the regular keyboard, so these are no-ops,
2440 * but there are still weird bugs re. MUTE, so do not
2441 * change unless you get test reports from all Lenovo
2442 * models. May cause the BIOS to interfere with the
2443 * HDA mixer.
2445 KEY_RESERVED, /* 0x14: VOLUME UP */
2446 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2447 KEY_RESERVED, /* 0x16: MUTE */
2449 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2451 /* (assignments unknown, please report if found) */
2452 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2453 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2456 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2457 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2458 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2460 int res, i;
2461 int status;
2462 int hkeyv;
2464 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2465 "initializing hotkey subdriver\n");
2467 BUG_ON(!tpacpi_inputdev);
2468 BUG_ON(tpacpi_inputdev->open != NULL ||
2469 tpacpi_inputdev->close != NULL);
2471 TPACPI_ACPIHANDLE_INIT(hkey);
2472 mutex_init(&hotkey_mutex);
2474 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2475 mutex_init(&hotkey_thread_mutex);
2476 mutex_init(&hotkey_thread_data_mutex);
2477 #endif
2479 /* hotkey not supported on 570 */
2480 tp_features.hotkey = hkey_handle != NULL;
2482 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2483 "hotkeys are %s\n",
2484 str_supported(tp_features.hotkey));
2486 if (!tp_features.hotkey)
2487 return 1;
2489 tpacpi_disable_brightness_delay();
2491 hotkey_dev_attributes = create_attr_set(13, NULL);
2492 if (!hotkey_dev_attributes)
2493 return -ENOMEM;
2494 res = add_many_to_attr_set(hotkey_dev_attributes,
2495 hotkey_attributes,
2496 ARRAY_SIZE(hotkey_attributes));
2497 if (res)
2498 goto err_exit;
2500 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2501 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2502 for HKEY interface version 0x100 */
2503 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2504 if ((hkeyv >> 8) != 1) {
2505 printk(TPACPI_ERR "unknown version of the "
2506 "HKEY interface: 0x%x\n", hkeyv);
2507 printk(TPACPI_ERR "please report this to %s\n",
2508 TPACPI_MAIL);
2509 } else {
2511 * MHKV 0x100 in A31, R40, R40e,
2512 * T4x, X31, and later
2514 tp_features.hotkey_mask = 1;
2515 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2516 "firmware HKEY interface version: 0x%x\n",
2517 hkeyv);
2521 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2522 "hotkey masks are %s\n",
2523 str_supported(tp_features.hotkey_mask));
2525 if (tp_features.hotkey_mask) {
2526 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2527 "MHKA", "qd")) {
2528 printk(TPACPI_ERR
2529 "missing MHKA handler, "
2530 "please report this to %s\n",
2531 TPACPI_MAIL);
2532 /* FN+F12, FN+F4, FN+F3 */
2533 hotkey_all_mask = 0x080cU;
2537 /* hotkey_source_mask *must* be zero for
2538 * the first hotkey_mask_get */
2539 if (tp_features.hotkey_mask) {
2540 res = hotkey_mask_get();
2541 if (res)
2542 goto err_exit;
2544 hotkey_orig_mask = hotkey_mask;
2545 res = add_many_to_attr_set(
2546 hotkey_dev_attributes,
2547 hotkey_mask_attributes,
2548 ARRAY_SIZE(hotkey_mask_attributes));
2549 if (res)
2550 goto err_exit;
2553 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2554 if (tp_features.hotkey_mask) {
2555 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2556 & ~hotkey_all_mask;
2557 } else {
2558 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2561 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2562 "hotkey source mask 0x%08x, polling freq %d\n",
2563 hotkey_source_mask, hotkey_poll_freq);
2564 #endif
2566 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2567 if (dbg_wlswemul) {
2568 tp_features.hotkey_wlsw = 1;
2569 printk(TPACPI_INFO
2570 "radio switch emulation enabled\n");
2571 } else
2572 #endif
2573 /* Not all thinkpads have a hardware radio switch */
2574 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2575 tp_features.hotkey_wlsw = 1;
2576 printk(TPACPI_INFO
2577 "radio switch found; radios are %s\n",
2578 enabled(status, 0));
2580 if (tp_features.hotkey_wlsw)
2581 res = add_to_attr_set(hotkey_dev_attributes,
2582 &dev_attr_hotkey_radio_sw.attr);
2584 /* For X41t, X60t, X61t Tablets... */
2585 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2586 tp_features.hotkey_tablet = 1;
2587 printk(TPACPI_INFO
2588 "possible tablet mode switch found; "
2589 "ThinkPad in %s mode\n",
2590 (status & TP_HOTKEY_TABLET_MASK)?
2591 "tablet" : "laptop");
2592 res = add_to_attr_set(hotkey_dev_attributes,
2593 &dev_attr_hotkey_tablet_mode.attr);
2596 if (!res)
2597 res = register_attr_set_with_sysfs(
2598 hotkey_dev_attributes,
2599 &tpacpi_pdev->dev.kobj);
2600 if (res)
2601 goto err_exit;
2603 /* Set up key map */
2605 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2606 GFP_KERNEL);
2607 if (!hotkey_keycode_map) {
2608 printk(TPACPI_ERR
2609 "failed to allocate memory for key map\n");
2610 res = -ENOMEM;
2611 goto err_exit;
2614 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2615 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2616 "using Lenovo default hot key map\n");
2617 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2618 TPACPI_HOTKEY_MAP_SIZE);
2619 } else {
2620 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2621 "using IBM default hot key map\n");
2622 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2623 TPACPI_HOTKEY_MAP_SIZE);
2626 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2627 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2628 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2629 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2630 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2631 tpacpi_inputdev->keycode = hotkey_keycode_map;
2632 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2633 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2634 set_bit(hotkey_keycode_map[i],
2635 tpacpi_inputdev->keybit);
2636 } else {
2637 if (i < sizeof(hotkey_reserved_mask)*8)
2638 hotkey_reserved_mask |= 1 << i;
2642 if (tp_features.hotkey_wlsw) {
2643 set_bit(EV_SW, tpacpi_inputdev->evbit);
2644 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2646 if (tp_features.hotkey_tablet) {
2647 set_bit(EV_SW, tpacpi_inputdev->evbit);
2648 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2651 /* Do not issue duplicate brightness change events to
2652 * userspace */
2653 if (!tp_features.bright_acpimode)
2654 /* update bright_acpimode... */
2655 tpacpi_check_std_acpi_brightness_support();
2657 if (tp_features.bright_acpimode) {
2658 printk(TPACPI_INFO
2659 "This ThinkPad has standard ACPI backlight "
2660 "brightness control, supported by the ACPI "
2661 "video driver\n");
2662 printk(TPACPI_NOTICE
2663 "Disabling thinkpad-acpi brightness events "
2664 "by default...\n");
2666 /* The hotkey_reserved_mask change below is not
2667 * necessary while the keys are at KEY_RESERVED in the
2668 * default map, but better safe than sorry, leave it
2669 * here as a marker of what we have to do, especially
2670 * when we finally become able to set this at runtime
2671 * on response to X.org requests */
2672 hotkey_reserved_mask |=
2673 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2674 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2677 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2678 "enabling firmware HKEY event interface...\n");
2679 res = hotkey_status_set(true);
2680 if (res) {
2681 hotkey_exit();
2682 return res;
2684 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2685 & ~hotkey_reserved_mask)
2686 | hotkey_orig_mask);
2687 if (res < 0 && res != -ENXIO) {
2688 hotkey_exit();
2689 return res;
2692 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2693 "legacy ibm/hotkey event reporting over procfs %s\n",
2694 (hotkey_report_mode < 2) ?
2695 "enabled" : "disabled");
2697 tpacpi_inputdev->open = &hotkey_inputdev_open;
2698 tpacpi_inputdev->close = &hotkey_inputdev_close;
2700 hotkey_poll_setup_safe(1);
2701 tpacpi_send_radiosw_update();
2702 tpacpi_input_send_tabletsw();
2704 return 0;
2706 err_exit:
2707 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2708 hotkey_dev_attributes = NULL;
2710 return (res < 0)? res : 1;
2713 static bool hotkey_notify_hotkey(const u32 hkey,
2714 bool *send_acpi_ev,
2715 bool *ignore_acpi_ev)
2717 /* 0x1000-0x1FFF: key presses */
2718 unsigned int scancode = hkey & 0xfff;
2719 *send_acpi_ev = true;
2720 *ignore_acpi_ev = false;
2722 if (scancode > 0 && scancode < 0x21) {
2723 scancode--;
2724 if (!(hotkey_source_mask & (1 << scancode))) {
2725 tpacpi_input_send_key(scancode);
2726 *send_acpi_ev = false;
2727 } else {
2728 *ignore_acpi_ev = true;
2730 return true;
2732 return false;
2735 static bool hotkey_notify_wakeup(const u32 hkey,
2736 bool *send_acpi_ev,
2737 bool *ignore_acpi_ev)
2739 /* 0x2000-0x2FFF: Wakeup reason */
2740 *send_acpi_ev = true;
2741 *ignore_acpi_ev = false;
2743 switch (hkey) {
2744 case 0x2304: /* suspend, undock */
2745 case 0x2404: /* hibernation, undock */
2746 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2747 *ignore_acpi_ev = true;
2748 break;
2750 case 0x2305: /* suspend, bay eject */
2751 case 0x2405: /* hibernation, bay eject */
2752 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2753 *ignore_acpi_ev = true;
2754 break;
2756 case 0x2313: /* Battery on critical low level (S3) */
2757 case 0x2413: /* Battery on critical low level (S4) */
2758 printk(TPACPI_ALERT
2759 "EMERGENCY WAKEUP: battery almost empty\n");
2760 /* how to auto-heal: */
2761 /* 2313: woke up from S3, go to S4/S5 */
2762 /* 2413: woke up from S4, go to S5 */
2763 break;
2765 default:
2766 return false;
2769 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2770 printk(TPACPI_INFO
2771 "woke up due to a hot-unplug "
2772 "request...\n");
2773 hotkey_wakeup_reason_notify_change();
2775 return true;
2778 static bool hotkey_notify_usrevent(const u32 hkey,
2779 bool *send_acpi_ev,
2780 bool *ignore_acpi_ev)
2782 /* 0x5000-0x5FFF: human interface helpers */
2783 *send_acpi_ev = true;
2784 *ignore_acpi_ev = false;
2786 switch (hkey) {
2787 case 0x5010: /* Lenovo new BIOS: brightness changed */
2788 case 0x500b: /* X61t: tablet pen inserted into bay */
2789 case 0x500c: /* X61t: tablet pen removed from bay */
2790 return true;
2792 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2793 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2794 tpacpi_input_send_tabletsw();
2795 hotkey_tablet_mode_notify_change();
2796 *send_acpi_ev = false;
2797 return true;
2799 case 0x5001:
2800 case 0x5002:
2801 /* LID switch events. Do not propagate */
2802 *ignore_acpi_ev = true;
2803 return true;
2805 default:
2806 return false;
2810 static bool hotkey_notify_thermal(const u32 hkey,
2811 bool *send_acpi_ev,
2812 bool *ignore_acpi_ev)
2814 /* 0x6000-0x6FFF: thermal alarms */
2815 *send_acpi_ev = true;
2816 *ignore_acpi_ev = false;
2818 switch (hkey) {
2819 case 0x6011:
2820 printk(TPACPI_CRIT
2821 "THERMAL ALARM: battery is too hot!\n");
2822 /* recommended action: warn user through gui */
2823 return true;
2824 case 0x6012:
2825 printk(TPACPI_ALERT
2826 "THERMAL EMERGENCY: battery is extremely hot!\n");
2827 /* recommended action: immediate sleep/hibernate */
2828 return true;
2829 case 0x6021:
2830 printk(TPACPI_CRIT
2831 "THERMAL ALARM: "
2832 "a sensor reports something is too hot!\n");
2833 /* recommended action: warn user through gui, that */
2834 /* some internal component is too hot */
2835 return true;
2836 case 0x6022:
2837 printk(TPACPI_ALERT
2838 "THERMAL EMERGENCY: "
2839 "a sensor reports something is extremely hot!\n");
2840 /* recommended action: immediate sleep/hibernate */
2841 return true;
2842 case 0x6030:
2843 printk(TPACPI_INFO
2844 "EC reports that Thermal Table has changed\n");
2845 /* recommended action: do nothing, we don't have
2846 * Lenovo ATM information */
2847 return true;
2848 default:
2849 printk(TPACPI_ALERT
2850 "THERMAL ALERT: unknown thermal alarm received\n");
2851 return false;
2855 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2857 u32 hkey;
2858 bool send_acpi_ev;
2859 bool ignore_acpi_ev;
2860 bool known_ev;
2862 if (event != 0x80) {
2863 printk(TPACPI_ERR
2864 "unknown HKEY notification event %d\n", event);
2865 /* forward it to userspace, maybe it knows how to handle it */
2866 acpi_bus_generate_netlink_event(
2867 ibm->acpi->device->pnp.device_class,
2868 dev_name(&ibm->acpi->device->dev),
2869 event, 0);
2870 return;
2873 while (1) {
2874 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2875 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2876 return;
2879 if (hkey == 0) {
2880 /* queue empty */
2881 return;
2884 send_acpi_ev = true;
2885 ignore_acpi_ev = false;
2887 switch (hkey >> 12) {
2888 case 1:
2889 /* 0x1000-0x1FFF: key presses */
2890 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2891 &ignore_acpi_ev);
2892 break;
2893 case 2:
2894 /* 0x2000-0x2FFF: Wakeup reason */
2895 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2896 &ignore_acpi_ev);
2897 break;
2898 case 3:
2899 /* 0x3000-0x3FFF: bay-related wakeups */
2900 if (hkey == 0x3003) {
2901 hotkey_autosleep_ack = 1;
2902 printk(TPACPI_INFO
2903 "bay ejected\n");
2904 hotkey_wakeup_hotunplug_complete_notify_change();
2905 known_ev = true;
2906 } else {
2907 known_ev = false;
2909 break;
2910 case 4:
2911 /* 0x4000-0x4FFF: dock-related wakeups */
2912 if (hkey == 0x4003) {
2913 hotkey_autosleep_ack = 1;
2914 printk(TPACPI_INFO
2915 "undocked\n");
2916 hotkey_wakeup_hotunplug_complete_notify_change();
2917 known_ev = true;
2918 } else {
2919 known_ev = false;
2921 break;
2922 case 5:
2923 /* 0x5000-0x5FFF: human interface helpers */
2924 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2925 &ignore_acpi_ev);
2926 break;
2927 case 6:
2928 /* 0x6000-0x6FFF: thermal alarms */
2929 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2930 &ignore_acpi_ev);
2931 break;
2932 case 7:
2933 /* 0x7000-0x7FFF: misc */
2934 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2935 tpacpi_send_radiosw_update();
2936 send_acpi_ev = 0;
2937 known_ev = true;
2938 break;
2940 /* fallthrough to default */
2941 default:
2942 known_ev = false;
2944 if (!known_ev) {
2945 printk(TPACPI_NOTICE
2946 "unhandled HKEY event 0x%04x\n", hkey);
2947 printk(TPACPI_NOTICE
2948 "please report the conditions when this "
2949 "event happened to %s\n", TPACPI_MAIL);
2952 /* Legacy events */
2953 if (!ignore_acpi_ev &&
2954 (send_acpi_ev || hotkey_report_mode < 2)) {
2955 acpi_bus_generate_proc_event(ibm->acpi->device,
2956 event, hkey);
2959 /* netlink events */
2960 if (!ignore_acpi_ev && send_acpi_ev) {
2961 acpi_bus_generate_netlink_event(
2962 ibm->acpi->device->pnp.device_class,
2963 dev_name(&ibm->acpi->device->dev),
2964 event, hkey);
2969 static void hotkey_suspend(pm_message_t state)
2971 /* Do these on suspend, we get the events on early resume! */
2972 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2973 hotkey_autosleep_ack = 0;
2976 static void hotkey_resume(void)
2978 tpacpi_disable_brightness_delay();
2980 if (hotkey_mask_get())
2981 printk(TPACPI_ERR
2982 "error while trying to read hot key mask "
2983 "from firmware\n");
2984 tpacpi_send_radiosw_update();
2985 hotkey_tablet_mode_notify_change();
2986 hotkey_wakeup_reason_notify_change();
2987 hotkey_wakeup_hotunplug_complete_notify_change();
2988 hotkey_poll_setup_safe(0);
2991 /* procfs -------------------------------------------------------------- */
2992 static int hotkey_read(char *p)
2994 int res, status;
2995 int len = 0;
2997 if (!tp_features.hotkey) {
2998 len += sprintf(p + len, "status:\t\tnot supported\n");
2999 return len;
3002 if (mutex_lock_killable(&hotkey_mutex))
3003 return -ERESTARTSYS;
3004 res = hotkey_status_get(&status);
3005 if (!res)
3006 res = hotkey_mask_get();
3007 mutex_unlock(&hotkey_mutex);
3008 if (res)
3009 return res;
3011 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3012 if (tp_features.hotkey_mask) {
3013 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3014 len += sprintf(p + len,
3015 "commands:\tenable, disable, reset, <mask>\n");
3016 } else {
3017 len += sprintf(p + len, "mask:\t\tnot supported\n");
3018 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3021 return len;
3024 static void hotkey_enabledisable_warn(bool enable)
3026 tpacpi_log_usertask("procfs hotkey enable/disable");
3027 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3028 TPACPI_WARN
3029 "hotkey enable/disable functionality has been "
3030 "removed from the driver. Hotkeys are always "
3031 "enabled\n"))
3032 printk(TPACPI_ERR
3033 "Please remove the hotkey=enable module "
3034 "parameter, it is deprecated. Hotkeys are always "
3035 "enabled\n");
3038 static int hotkey_write(char *buf)
3040 int res;
3041 u32 mask;
3042 char *cmd;
3044 if (!tp_features.hotkey)
3045 return -ENODEV;
3047 if (mutex_lock_killable(&hotkey_mutex))
3048 return -ERESTARTSYS;
3050 mask = hotkey_mask;
3052 res = 0;
3053 while ((cmd = next_cmd(&buf))) {
3054 if (strlencmp(cmd, "enable") == 0) {
3055 hotkey_enabledisable_warn(1);
3056 } else if (strlencmp(cmd, "disable") == 0) {
3057 hotkey_enabledisable_warn(0);
3058 res = -EPERM;
3059 } else if (strlencmp(cmd, "reset") == 0) {
3060 mask = hotkey_orig_mask;
3061 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3062 /* mask set */
3063 } else if (sscanf(cmd, "%x", &mask) == 1) {
3064 /* mask set */
3065 } else {
3066 res = -EINVAL;
3067 goto errexit;
3071 if (!res)
3072 tpacpi_disclose_usertask("procfs hotkey",
3073 "set mask to 0x%08x\n", mask);
3075 if (!res && mask != hotkey_mask)
3076 res = hotkey_mask_set(mask);
3078 errexit:
3079 mutex_unlock(&hotkey_mutex);
3080 return res;
3083 static const struct acpi_device_id ibm_htk_device_ids[] = {
3084 {TPACPI_ACPI_HKEY_HID, 0},
3085 {"", 0},
3088 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3089 .hid = ibm_htk_device_ids,
3090 .notify = hotkey_notify,
3091 .handle = &hkey_handle,
3092 .type = ACPI_DEVICE_NOTIFY,
3095 static struct ibm_struct hotkey_driver_data = {
3096 .name = "hotkey",
3097 .read = hotkey_read,
3098 .write = hotkey_write,
3099 .exit = hotkey_exit,
3100 .resume = hotkey_resume,
3101 .suspend = hotkey_suspend,
3102 .acpi = &ibm_hotkey_acpidriver,
3105 /*************************************************************************
3106 * Bluetooth subdriver
3109 enum {
3110 /* ACPI GBDC/SBDC bits */
3111 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3112 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3113 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3114 off / last state */
3117 enum {
3118 /* ACPI \BLTH commands */
3119 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3120 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3121 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3122 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3123 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3126 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3128 static struct rfkill *tpacpi_bluetooth_rfkill;
3130 static void bluetooth_suspend(pm_message_t state)
3132 /* Try to make sure radio will resume powered off */
3133 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3134 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3135 vdbg_printk(TPACPI_DBG_RFKILL,
3136 "bluetooth power down on resume request failed\n");
3139 static int bluetooth_get_radiosw(void)
3141 int status;
3143 if (!tp_features.bluetooth)
3144 return -ENODEV;
3146 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3147 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3148 return RFKILL_STATE_HARD_BLOCKED;
3150 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3151 if (dbg_bluetoothemul)
3152 return (tpacpi_bluetooth_emulstate) ?
3153 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3154 #endif
3156 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3157 return -EIO;
3159 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3160 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3163 static void bluetooth_update_rfk(void)
3165 int status;
3167 if (!tpacpi_bluetooth_rfkill)
3168 return;
3170 status = bluetooth_get_radiosw();
3171 if (status < 0)
3172 return;
3173 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3175 vdbg_printk(TPACPI_DBG_RFKILL,
3176 "forced rfkill state to %d\n",
3177 status);
3180 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3182 int status;
3184 if (!tp_features.bluetooth)
3185 return -ENODEV;
3187 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3188 * reason to risk weird behaviour. */
3189 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3190 && radio_on)
3191 return -EPERM;
3193 vdbg_printk(TPACPI_DBG_RFKILL,
3194 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3196 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3197 if (dbg_bluetoothemul) {
3198 tpacpi_bluetooth_emulstate = !!radio_on;
3199 if (update_rfk)
3200 bluetooth_update_rfk();
3201 return 0;
3203 #endif
3205 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3206 if (radio_on)
3207 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3208 else
3209 status = 0;
3210 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3211 return -EIO;
3213 if (update_rfk)
3214 bluetooth_update_rfk();
3216 return 0;
3219 /* sysfs bluetooth enable ---------------------------------------------- */
3220 static ssize_t bluetooth_enable_show(struct device *dev,
3221 struct device_attribute *attr,
3222 char *buf)
3224 int status;
3226 printk_deprecated_rfkill_attribute("bluetooth_enable");
3228 status = bluetooth_get_radiosw();
3229 if (status < 0)
3230 return status;
3232 return snprintf(buf, PAGE_SIZE, "%d\n",
3233 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3236 static ssize_t bluetooth_enable_store(struct device *dev,
3237 struct device_attribute *attr,
3238 const char *buf, size_t count)
3240 unsigned long t;
3241 int res;
3243 printk_deprecated_rfkill_attribute("bluetooth_enable");
3245 if (parse_strtoul(buf, 1, &t))
3246 return -EINVAL;
3248 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3250 res = bluetooth_set_radiosw(t, 1);
3252 return (res) ? res : count;
3255 static struct device_attribute dev_attr_bluetooth_enable =
3256 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3257 bluetooth_enable_show, bluetooth_enable_store);
3259 /* --------------------------------------------------------------------- */
3261 static struct attribute *bluetooth_attributes[] = {
3262 &dev_attr_bluetooth_enable.attr,
3263 NULL
3266 static const struct attribute_group bluetooth_attr_group = {
3267 .attrs = bluetooth_attributes,
3270 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3272 int bts = bluetooth_get_radiosw();
3274 if (bts < 0)
3275 return bts;
3277 *state = bts;
3278 return 0;
3281 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3283 dbg_printk(TPACPI_DBG_RFKILL,
3284 "request to change radio state to %d\n", state);
3285 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3288 static void bluetooth_shutdown(void)
3290 /* Order firmware to save current state to NVRAM */
3291 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3292 TP_ACPI_BLTH_SAVE_STATE))
3293 printk(TPACPI_NOTICE
3294 "failed to save bluetooth state to NVRAM\n");
3295 else
3296 vdbg_printk(TPACPI_DBG_RFKILL,
3297 "bluestooth state saved to NVRAM\n");
3300 static void bluetooth_exit(void)
3302 bluetooth_shutdown();
3304 if (tpacpi_bluetooth_rfkill)
3305 rfkill_unregister(tpacpi_bluetooth_rfkill);
3307 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3308 &bluetooth_attr_group);
3311 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3313 int res;
3314 int status = 0;
3316 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3317 "initializing bluetooth subdriver\n");
3319 TPACPI_ACPIHANDLE_INIT(hkey);
3321 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3322 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3323 tp_features.bluetooth = hkey_handle &&
3324 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3326 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3327 "bluetooth is %s, status 0x%02x\n",
3328 str_supported(tp_features.bluetooth),
3329 status);
3331 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3332 if (dbg_bluetoothemul) {
3333 tp_features.bluetooth = 1;
3334 printk(TPACPI_INFO
3335 "bluetooth switch emulation enabled\n");
3336 } else
3337 #endif
3338 if (tp_features.bluetooth &&
3339 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3340 /* no bluetooth hardware present in system */
3341 tp_features.bluetooth = 0;
3342 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3343 "bluetooth hardware not installed\n");
3346 if (!tp_features.bluetooth)
3347 return 1;
3349 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3350 &bluetooth_attr_group);
3351 if (res)
3352 return res;
3354 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3355 &tpacpi_bluetooth_rfkill,
3356 RFKILL_TYPE_BLUETOOTH,
3357 TPACPI_RFK_BLUETOOTH_SW_NAME,
3358 true,
3359 tpacpi_bluetooth_rfk_set,
3360 tpacpi_bluetooth_rfk_get);
3361 if (res) {
3362 bluetooth_exit();
3363 return res;
3366 return 0;
3369 /* procfs -------------------------------------------------------------- */
3370 static int bluetooth_read(char *p)
3372 int len = 0;
3373 int status = bluetooth_get_radiosw();
3375 if (!tp_features.bluetooth)
3376 len += sprintf(p + len, "status:\t\tnot supported\n");
3377 else {
3378 len += sprintf(p + len, "status:\t\t%s\n",
3379 (status == RFKILL_STATE_UNBLOCKED) ?
3380 "enabled" : "disabled");
3381 len += sprintf(p + len, "commands:\tenable, disable\n");
3384 return len;
3387 static int bluetooth_write(char *buf)
3389 char *cmd;
3390 int state = -1;
3392 if (!tp_features.bluetooth)
3393 return -ENODEV;
3395 while ((cmd = next_cmd(&buf))) {
3396 if (strlencmp(cmd, "enable") == 0) {
3397 state = 1;
3398 } else if (strlencmp(cmd, "disable") == 0) {
3399 state = 0;
3400 } else
3401 return -EINVAL;
3404 if (state != -1) {
3405 tpacpi_disclose_usertask("procfs bluetooth",
3406 "attempt to %s\n",
3407 state ? "enable" : "disable");
3408 bluetooth_set_radiosw(state, 1);
3411 return 0;
3414 static struct ibm_struct bluetooth_driver_data = {
3415 .name = "bluetooth",
3416 .read = bluetooth_read,
3417 .write = bluetooth_write,
3418 .exit = bluetooth_exit,
3419 .suspend = bluetooth_suspend,
3420 .shutdown = bluetooth_shutdown,
3423 /*************************************************************************
3424 * Wan subdriver
3427 enum {
3428 /* ACPI GWAN/SWAN bits */
3429 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3430 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3431 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3432 off / last state */
3435 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3437 static struct rfkill *tpacpi_wan_rfkill;
3439 static void wan_suspend(pm_message_t state)
3441 /* Try to make sure radio will resume powered off */
3442 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3443 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3444 vdbg_printk(TPACPI_DBG_RFKILL,
3445 "WWAN power down on resume request failed\n");
3448 static int wan_get_radiosw(void)
3450 int status;
3452 if (!tp_features.wan)
3453 return -ENODEV;
3455 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3456 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3457 return RFKILL_STATE_HARD_BLOCKED;
3459 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3460 if (dbg_wwanemul)
3461 return (tpacpi_wwan_emulstate) ?
3462 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3463 #endif
3465 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3466 return -EIO;
3468 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3469 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3472 static void wan_update_rfk(void)
3474 int status;
3476 if (!tpacpi_wan_rfkill)
3477 return;
3479 status = wan_get_radiosw();
3480 if (status < 0)
3481 return;
3482 rfkill_force_state(tpacpi_wan_rfkill, status);
3484 vdbg_printk(TPACPI_DBG_RFKILL,
3485 "forced rfkill state to %d\n",
3486 status);
3489 static int wan_set_radiosw(int radio_on, int update_rfk)
3491 int status;
3493 if (!tp_features.wan)
3494 return -ENODEV;
3496 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3497 * reason to risk weird behaviour. */
3498 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3499 && radio_on)
3500 return -EPERM;
3502 vdbg_printk(TPACPI_DBG_RFKILL,
3503 "will %s WWAN\n", radio_on ? "enable" : "disable");
3505 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3506 if (dbg_wwanemul) {
3507 tpacpi_wwan_emulstate = !!radio_on;
3508 if (update_rfk)
3509 wan_update_rfk();
3510 return 0;
3512 #endif
3514 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3515 if (radio_on)
3516 status = TP_ACPI_WANCARD_RADIOSSW;
3517 else
3518 status = 0;
3519 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3520 return -EIO;
3522 if (update_rfk)
3523 wan_update_rfk();
3525 return 0;
3528 /* sysfs wan enable ---------------------------------------------------- */
3529 static ssize_t wan_enable_show(struct device *dev,
3530 struct device_attribute *attr,
3531 char *buf)
3533 int status;
3535 printk_deprecated_rfkill_attribute("wwan_enable");
3537 status = wan_get_radiosw();
3538 if (status < 0)
3539 return status;
3541 return snprintf(buf, PAGE_SIZE, "%d\n",
3542 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3545 static ssize_t wan_enable_store(struct device *dev,
3546 struct device_attribute *attr,
3547 const char *buf, size_t count)
3549 unsigned long t;
3550 int res;
3552 printk_deprecated_rfkill_attribute("wwan_enable");
3554 if (parse_strtoul(buf, 1, &t))
3555 return -EINVAL;
3557 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3559 res = wan_set_radiosw(t, 1);
3561 return (res) ? res : count;
3564 static struct device_attribute dev_attr_wan_enable =
3565 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3566 wan_enable_show, wan_enable_store);
3568 /* --------------------------------------------------------------------- */
3570 static struct attribute *wan_attributes[] = {
3571 &dev_attr_wan_enable.attr,
3572 NULL
3575 static const struct attribute_group wan_attr_group = {
3576 .attrs = wan_attributes,
3579 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3581 int wans = wan_get_radiosw();
3583 if (wans < 0)
3584 return wans;
3586 *state = wans;
3587 return 0;
3590 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3592 dbg_printk(TPACPI_DBG_RFKILL,
3593 "request to change radio state to %d\n", state);
3594 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3597 static void wan_shutdown(void)
3599 /* Order firmware to save current state to NVRAM */
3600 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3601 TP_ACPI_WGSV_SAVE_STATE))
3602 printk(TPACPI_NOTICE
3603 "failed to save WWAN state to NVRAM\n");
3604 else
3605 vdbg_printk(TPACPI_DBG_RFKILL,
3606 "WWAN state saved to NVRAM\n");
3609 static void wan_exit(void)
3611 wan_shutdown();
3613 if (tpacpi_wan_rfkill)
3614 rfkill_unregister(tpacpi_wan_rfkill);
3616 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3617 &wan_attr_group);
3620 static int __init wan_init(struct ibm_init_struct *iibm)
3622 int res;
3623 int status = 0;
3625 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3626 "initializing wan subdriver\n");
3628 TPACPI_ACPIHANDLE_INIT(hkey);
3630 tp_features.wan = hkey_handle &&
3631 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3633 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3634 "wan is %s, status 0x%02x\n",
3635 str_supported(tp_features.wan),
3636 status);
3638 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3639 if (dbg_wwanemul) {
3640 tp_features.wan = 1;
3641 printk(TPACPI_INFO
3642 "wwan switch emulation enabled\n");
3643 } else
3644 #endif
3645 if (tp_features.wan &&
3646 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3647 /* no wan hardware present in system */
3648 tp_features.wan = 0;
3649 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3650 "wan hardware not installed\n");
3653 if (!tp_features.wan)
3654 return 1;
3656 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3657 &wan_attr_group);
3658 if (res)
3659 return res;
3661 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3662 &tpacpi_wan_rfkill,
3663 RFKILL_TYPE_WWAN,
3664 TPACPI_RFK_WWAN_SW_NAME,
3665 true,
3666 tpacpi_wan_rfk_set,
3667 tpacpi_wan_rfk_get);
3668 if (res) {
3669 wan_exit();
3670 return res;
3673 return 0;
3676 /* procfs -------------------------------------------------------------- */
3677 static int wan_read(char *p)
3679 int len = 0;
3680 int status = wan_get_radiosw();
3682 tpacpi_disclose_usertask("procfs wan", "read");
3684 if (!tp_features.wan)
3685 len += sprintf(p + len, "status:\t\tnot supported\n");
3686 else {
3687 len += sprintf(p + len, "status:\t\t%s\n",
3688 (status == RFKILL_STATE_UNBLOCKED) ?
3689 "enabled" : "disabled");
3690 len += sprintf(p + len, "commands:\tenable, disable\n");
3693 return len;
3696 static int wan_write(char *buf)
3698 char *cmd;
3699 int state = -1;
3701 if (!tp_features.wan)
3702 return -ENODEV;
3704 while ((cmd = next_cmd(&buf))) {
3705 if (strlencmp(cmd, "enable") == 0) {
3706 state = 1;
3707 } else if (strlencmp(cmd, "disable") == 0) {
3708 state = 0;
3709 } else
3710 return -EINVAL;
3713 if (state != -1) {
3714 tpacpi_disclose_usertask("procfs wan",
3715 "attempt to %s\n",
3716 state ? "enable" : "disable");
3717 wan_set_radiosw(state, 1);
3720 return 0;
3723 static struct ibm_struct wan_driver_data = {
3724 .name = "wan",
3725 .read = wan_read,
3726 .write = wan_write,
3727 .exit = wan_exit,
3728 .suspend = wan_suspend,
3729 .shutdown = wan_shutdown,
3732 /*************************************************************************
3733 * UWB subdriver
3736 enum {
3737 /* ACPI GUWB/SUWB bits */
3738 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3739 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3742 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3744 static struct rfkill *tpacpi_uwb_rfkill;
3746 static int uwb_get_radiosw(void)
3748 int status;
3750 if (!tp_features.uwb)
3751 return -ENODEV;
3753 /* WLSW overrides UWB in firmware/hardware, reflect that */
3754 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3755 return RFKILL_STATE_HARD_BLOCKED;
3757 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3758 if (dbg_uwbemul)
3759 return (tpacpi_uwb_emulstate) ?
3760 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3761 #endif
3763 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3764 return -EIO;
3766 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3767 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3770 static void uwb_update_rfk(void)
3772 int status;
3774 if (!tpacpi_uwb_rfkill)
3775 return;
3777 status = uwb_get_radiosw();
3778 if (status < 0)
3779 return;
3780 rfkill_force_state(tpacpi_uwb_rfkill, status);
3782 vdbg_printk(TPACPI_DBG_RFKILL,
3783 "forced rfkill state to %d\n",
3784 status);
3787 static int uwb_set_radiosw(int radio_on, int update_rfk)
3789 int status;
3791 if (!tp_features.uwb)
3792 return -ENODEV;
3794 /* WLSW overrides UWB in firmware/hardware, but there is no
3795 * reason to risk weird behaviour. */
3796 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3797 && radio_on)
3798 return -EPERM;
3800 vdbg_printk(TPACPI_DBG_RFKILL,
3801 "will %s UWB\n", radio_on ? "enable" : "disable");
3803 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3804 if (dbg_uwbemul) {
3805 tpacpi_uwb_emulstate = !!radio_on;
3806 if (update_rfk)
3807 uwb_update_rfk();
3808 return 0;
3810 #endif
3812 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3813 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3814 return -EIO;
3816 if (update_rfk)
3817 uwb_update_rfk();
3819 return 0;
3822 /* --------------------------------------------------------------------- */
3824 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3826 int uwbs = uwb_get_radiosw();
3828 if (uwbs < 0)
3829 return uwbs;
3831 *state = uwbs;
3832 return 0;
3835 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3837 dbg_printk(TPACPI_DBG_RFKILL,
3838 "request to change radio state to %d\n", state);
3839 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3842 static void uwb_exit(void)
3844 if (tpacpi_uwb_rfkill)
3845 rfkill_unregister(tpacpi_uwb_rfkill);
3848 static int __init uwb_init(struct ibm_init_struct *iibm)
3850 int res;
3851 int status = 0;
3853 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3854 "initializing uwb subdriver\n");
3856 TPACPI_ACPIHANDLE_INIT(hkey);
3858 tp_features.uwb = hkey_handle &&
3859 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3861 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3862 "uwb is %s, status 0x%02x\n",
3863 str_supported(tp_features.uwb),
3864 status);
3866 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3867 if (dbg_uwbemul) {
3868 tp_features.uwb = 1;
3869 printk(TPACPI_INFO
3870 "uwb switch emulation enabled\n");
3871 } else
3872 #endif
3873 if (tp_features.uwb &&
3874 !(status & TP_ACPI_UWB_HWPRESENT)) {
3875 /* no uwb hardware present in system */
3876 tp_features.uwb = 0;
3877 dbg_printk(TPACPI_DBG_INIT,
3878 "uwb hardware not installed\n");
3881 if (!tp_features.uwb)
3882 return 1;
3884 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3885 &tpacpi_uwb_rfkill,
3886 RFKILL_TYPE_UWB,
3887 TPACPI_RFK_UWB_SW_NAME,
3888 false,
3889 tpacpi_uwb_rfk_set,
3890 tpacpi_uwb_rfk_get);
3892 return res;
3895 static struct ibm_struct uwb_driver_data = {
3896 .name = "uwb",
3897 .exit = uwb_exit,
3898 .flags.experimental = 1,
3901 /*************************************************************************
3902 * Video subdriver
3905 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3907 enum video_access_mode {
3908 TPACPI_VIDEO_NONE = 0,
3909 TPACPI_VIDEO_570, /* 570 */
3910 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3911 TPACPI_VIDEO_NEW, /* all others */
3914 enum { /* video status flags, based on VIDEO_570 */
3915 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3916 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3917 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3920 enum { /* TPACPI_VIDEO_570 constants */
3921 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3922 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3923 * video_status_flags */
3924 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3925 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3928 static enum video_access_mode video_supported;
3929 static int video_orig_autosw;
3931 static int video_autosw_get(void);
3932 static int video_autosw_set(int enable);
3934 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3936 static int __init video_init(struct ibm_init_struct *iibm)
3938 int ivga;
3940 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3942 TPACPI_ACPIHANDLE_INIT(vid);
3943 TPACPI_ACPIHANDLE_INIT(vid2);
3945 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3946 /* G41, assume IVGA doesn't change */
3947 vid_handle = vid2_handle;
3949 if (!vid_handle)
3950 /* video switching not supported on R30, R31 */
3951 video_supported = TPACPI_VIDEO_NONE;
3952 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3953 /* 570 */
3954 video_supported = TPACPI_VIDEO_570;
3955 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3956 /* 600e/x, 770e, 770x */
3957 video_supported = TPACPI_VIDEO_770;
3958 else
3959 /* all others */
3960 video_supported = TPACPI_VIDEO_NEW;
3962 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3963 str_supported(video_supported != TPACPI_VIDEO_NONE),
3964 video_supported);
3966 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3969 static void video_exit(void)
3971 dbg_printk(TPACPI_DBG_EXIT,
3972 "restoring original video autoswitch mode\n");
3973 if (video_autosw_set(video_orig_autosw))
3974 printk(TPACPI_ERR "error while trying to restore original "
3975 "video autoswitch mode\n");
3978 static int video_outputsw_get(void)
3980 int status = 0;
3981 int i;
3983 switch (video_supported) {
3984 case TPACPI_VIDEO_570:
3985 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3986 TP_ACPI_VIDEO_570_PHSCMD))
3987 return -EIO;
3988 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3989 break;
3990 case TPACPI_VIDEO_770:
3991 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3992 return -EIO;
3993 if (i)
3994 status |= TP_ACPI_VIDEO_S_LCD;
3995 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3996 return -EIO;
3997 if (i)
3998 status |= TP_ACPI_VIDEO_S_CRT;
3999 break;
4000 case TPACPI_VIDEO_NEW:
4001 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4002 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4003 return -EIO;
4004 if (i)
4005 status |= TP_ACPI_VIDEO_S_CRT;
4007 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4008 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4009 return -EIO;
4010 if (i)
4011 status |= TP_ACPI_VIDEO_S_LCD;
4012 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4013 return -EIO;
4014 if (i)
4015 status |= TP_ACPI_VIDEO_S_DVI;
4016 break;
4017 default:
4018 return -ENOSYS;
4021 return status;
4024 static int video_outputsw_set(int status)
4026 int autosw;
4027 int res = 0;
4029 switch (video_supported) {
4030 case TPACPI_VIDEO_570:
4031 res = acpi_evalf(NULL, NULL,
4032 "\\_SB.PHS2", "vdd",
4033 TP_ACPI_VIDEO_570_PHS2CMD,
4034 status | TP_ACPI_VIDEO_570_PHS2SET);
4035 break;
4036 case TPACPI_VIDEO_770:
4037 autosw = video_autosw_get();
4038 if (autosw < 0)
4039 return autosw;
4041 res = video_autosw_set(1);
4042 if (res)
4043 return res;
4044 res = acpi_evalf(vid_handle, NULL,
4045 "ASWT", "vdd", status * 0x100, 0);
4046 if (!autosw && video_autosw_set(autosw)) {
4047 printk(TPACPI_ERR
4048 "video auto-switch left enabled due to error\n");
4049 return -EIO;
4051 break;
4052 case TPACPI_VIDEO_NEW:
4053 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4054 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4055 break;
4056 default:
4057 return -ENOSYS;
4060 return (res)? 0 : -EIO;
4063 static int video_autosw_get(void)
4065 int autosw = 0;
4067 switch (video_supported) {
4068 case TPACPI_VIDEO_570:
4069 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4070 return -EIO;
4071 break;
4072 case TPACPI_VIDEO_770:
4073 case TPACPI_VIDEO_NEW:
4074 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4075 return -EIO;
4076 break;
4077 default:
4078 return -ENOSYS;
4081 return autosw & 1;
4084 static int video_autosw_set(int enable)
4086 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4087 return -EIO;
4088 return 0;
4091 static int video_outputsw_cycle(void)
4093 int autosw = video_autosw_get();
4094 int res;
4096 if (autosw < 0)
4097 return autosw;
4099 switch (video_supported) {
4100 case TPACPI_VIDEO_570:
4101 res = video_autosw_set(1);
4102 if (res)
4103 return res;
4104 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4105 break;
4106 case TPACPI_VIDEO_770:
4107 case TPACPI_VIDEO_NEW:
4108 res = video_autosw_set(1);
4109 if (res)
4110 return res;
4111 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4112 break;
4113 default:
4114 return -ENOSYS;
4116 if (!autosw && video_autosw_set(autosw)) {
4117 printk(TPACPI_ERR
4118 "video auto-switch left enabled due to error\n");
4119 return -EIO;
4122 return (res)? 0 : -EIO;
4125 static int video_expand_toggle(void)
4127 switch (video_supported) {
4128 case TPACPI_VIDEO_570:
4129 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4130 0 : -EIO;
4131 case TPACPI_VIDEO_770:
4132 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4133 0 : -EIO;
4134 case TPACPI_VIDEO_NEW:
4135 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4136 0 : -EIO;
4137 default:
4138 return -ENOSYS;
4140 /* not reached */
4143 static int video_read(char *p)
4145 int status, autosw;
4146 int len = 0;
4148 if (video_supported == TPACPI_VIDEO_NONE) {
4149 len += sprintf(p + len, "status:\t\tnot supported\n");
4150 return len;
4153 status = video_outputsw_get();
4154 if (status < 0)
4155 return status;
4157 autosw = video_autosw_get();
4158 if (autosw < 0)
4159 return autosw;
4161 len += sprintf(p + len, "status:\t\tsupported\n");
4162 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4163 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4164 if (video_supported == TPACPI_VIDEO_NEW)
4165 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4166 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4167 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4168 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4169 if (video_supported == TPACPI_VIDEO_NEW)
4170 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4171 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4172 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4174 return len;
4177 static int video_write(char *buf)
4179 char *cmd;
4180 int enable, disable, status;
4181 int res;
4183 if (video_supported == TPACPI_VIDEO_NONE)
4184 return -ENODEV;
4186 enable = 0;
4187 disable = 0;
4189 while ((cmd = next_cmd(&buf))) {
4190 if (strlencmp(cmd, "lcd_enable") == 0) {
4191 enable |= TP_ACPI_VIDEO_S_LCD;
4192 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4193 disable |= TP_ACPI_VIDEO_S_LCD;
4194 } else if (strlencmp(cmd, "crt_enable") == 0) {
4195 enable |= TP_ACPI_VIDEO_S_CRT;
4196 } else if (strlencmp(cmd, "crt_disable") == 0) {
4197 disable |= TP_ACPI_VIDEO_S_CRT;
4198 } else if (video_supported == TPACPI_VIDEO_NEW &&
4199 strlencmp(cmd, "dvi_enable") == 0) {
4200 enable |= TP_ACPI_VIDEO_S_DVI;
4201 } else if (video_supported == TPACPI_VIDEO_NEW &&
4202 strlencmp(cmd, "dvi_disable") == 0) {
4203 disable |= TP_ACPI_VIDEO_S_DVI;
4204 } else if (strlencmp(cmd, "auto_enable") == 0) {
4205 res = video_autosw_set(1);
4206 if (res)
4207 return res;
4208 } else if (strlencmp(cmd, "auto_disable") == 0) {
4209 res = video_autosw_set(0);
4210 if (res)
4211 return res;
4212 } else if (strlencmp(cmd, "video_switch") == 0) {
4213 res = video_outputsw_cycle();
4214 if (res)
4215 return res;
4216 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4217 res = video_expand_toggle();
4218 if (res)
4219 return res;
4220 } else
4221 return -EINVAL;
4224 if (enable || disable) {
4225 status = video_outputsw_get();
4226 if (status < 0)
4227 return status;
4228 res = video_outputsw_set((status & ~disable) | enable);
4229 if (res)
4230 return res;
4233 return 0;
4236 static struct ibm_struct video_driver_data = {
4237 .name = "video",
4238 .read = video_read,
4239 .write = video_write,
4240 .exit = video_exit,
4243 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4245 /*************************************************************************
4246 * Light (thinklight) subdriver
4249 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4250 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4252 static int light_get_status(void)
4254 int status = 0;
4256 if (tp_features.light_status) {
4257 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4258 return -EIO;
4259 return (!!status);
4262 return -ENXIO;
4265 static int light_set_status(int status)
4267 int rc;
4269 if (tp_features.light) {
4270 if (cmos_handle) {
4271 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4272 (status)?
4273 TP_CMOS_THINKLIGHT_ON :
4274 TP_CMOS_THINKLIGHT_OFF);
4275 } else {
4276 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4277 (status)? 1 : 0);
4279 return (rc)? 0 : -EIO;
4282 return -ENXIO;
4285 static void light_set_status_worker(struct work_struct *work)
4287 struct tpacpi_led_classdev *data =
4288 container_of(work, struct tpacpi_led_classdev, work);
4290 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4291 light_set_status((data->new_state != TPACPI_LED_OFF));
4294 static void light_sysfs_set(struct led_classdev *led_cdev,
4295 enum led_brightness brightness)
4297 struct tpacpi_led_classdev *data =
4298 container_of(led_cdev,
4299 struct tpacpi_led_classdev,
4300 led_classdev);
4301 data->new_state = (brightness != LED_OFF) ?
4302 TPACPI_LED_ON : TPACPI_LED_OFF;
4303 queue_work(tpacpi_wq, &data->work);
4306 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4308 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4311 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4312 .led_classdev = {
4313 .name = "tpacpi::thinklight",
4314 .brightness_set = &light_sysfs_set,
4315 .brightness_get = &light_sysfs_get,
4319 static int __init light_init(struct ibm_init_struct *iibm)
4321 int rc;
4323 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4325 TPACPI_ACPIHANDLE_INIT(ledb);
4326 TPACPI_ACPIHANDLE_INIT(lght);
4327 TPACPI_ACPIHANDLE_INIT(cmos);
4328 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4330 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4331 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4333 if (tp_features.light)
4334 /* light status not supported on
4335 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4336 tp_features.light_status =
4337 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4339 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4340 str_supported(tp_features.light),
4341 str_supported(tp_features.light_status));
4343 if (!tp_features.light)
4344 return 1;
4346 rc = led_classdev_register(&tpacpi_pdev->dev,
4347 &tpacpi_led_thinklight.led_classdev);
4349 if (rc < 0) {
4350 tp_features.light = 0;
4351 tp_features.light_status = 0;
4352 } else {
4353 rc = 0;
4356 return rc;
4359 static void light_exit(void)
4361 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4362 if (work_pending(&tpacpi_led_thinklight.work))
4363 flush_workqueue(tpacpi_wq);
4366 static int light_read(char *p)
4368 int len = 0;
4369 int status;
4371 if (!tp_features.light) {
4372 len += sprintf(p + len, "status:\t\tnot supported\n");
4373 } else if (!tp_features.light_status) {
4374 len += sprintf(p + len, "status:\t\tunknown\n");
4375 len += sprintf(p + len, "commands:\ton, off\n");
4376 } else {
4377 status = light_get_status();
4378 if (status < 0)
4379 return status;
4380 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4381 len += sprintf(p + len, "commands:\ton, off\n");
4384 return len;
4387 static int light_write(char *buf)
4389 char *cmd;
4390 int newstatus = 0;
4392 if (!tp_features.light)
4393 return -ENODEV;
4395 while ((cmd = next_cmd(&buf))) {
4396 if (strlencmp(cmd, "on") == 0) {
4397 newstatus = 1;
4398 } else if (strlencmp(cmd, "off") == 0) {
4399 newstatus = 0;
4400 } else
4401 return -EINVAL;
4404 return light_set_status(newstatus);
4407 static struct ibm_struct light_driver_data = {
4408 .name = "light",
4409 .read = light_read,
4410 .write = light_write,
4411 .exit = light_exit,
4414 /*************************************************************************
4415 * Dock subdriver
4418 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4420 static void dock_notify(struct ibm_struct *ibm, u32 event);
4421 static int dock_read(char *p);
4422 static int dock_write(char *buf);
4424 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4425 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4426 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4427 "\\_SB.PCI.ISA.SLCE", /* 570 */
4428 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4430 /* don't list other alternatives as we install a notify handler on the 570 */
4431 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4433 static const struct acpi_device_id ibm_pci_device_ids[] = {
4434 {PCI_ROOT_HID_STRING, 0},
4435 {"", 0},
4438 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4440 .notify = dock_notify,
4441 .handle = &dock_handle,
4442 .type = ACPI_SYSTEM_NOTIFY,
4445 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4446 * We just use it to get notifications of dock hotplug
4447 * in very old thinkpads */
4448 .hid = ibm_pci_device_ids,
4449 .notify = dock_notify,
4450 .handle = &pci_handle,
4451 .type = ACPI_SYSTEM_NOTIFY,
4455 static struct ibm_struct dock_driver_data[2] = {
4457 .name = "dock",
4458 .read = dock_read,
4459 .write = dock_write,
4460 .acpi = &ibm_dock_acpidriver[0],
4463 .name = "dock",
4464 .acpi = &ibm_dock_acpidriver[1],
4468 #define dock_docked() (_sta(dock_handle) & 1)
4470 static int __init dock_init(struct ibm_init_struct *iibm)
4472 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4474 TPACPI_ACPIHANDLE_INIT(dock);
4476 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4477 str_supported(dock_handle != NULL));
4479 return (dock_handle)? 0 : 1;
4482 static int __init dock_init2(struct ibm_init_struct *iibm)
4484 int dock2_needed;
4486 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4488 if (dock_driver_data[0].flags.acpi_driver_registered &&
4489 dock_driver_data[0].flags.acpi_notify_installed) {
4490 TPACPI_ACPIHANDLE_INIT(pci);
4491 dock2_needed = (pci_handle != NULL);
4492 vdbg_printk(TPACPI_DBG_INIT,
4493 "dock PCI handler for the TP 570 is %s\n",
4494 str_supported(dock2_needed));
4495 } else {
4496 vdbg_printk(TPACPI_DBG_INIT,
4497 "dock subdriver part 2 not required\n");
4498 dock2_needed = 0;
4501 return (dock2_needed)? 0 : 1;
4504 static void dock_notify(struct ibm_struct *ibm, u32 event)
4506 int docked = dock_docked();
4507 int pci = ibm->acpi->hid && ibm->acpi->device &&
4508 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4509 int data;
4511 if (event == 1 && !pci) /* 570 */
4512 data = 1; /* button */
4513 else if (event == 1 && pci) /* 570 */
4514 data = 3; /* dock */
4515 else if (event == 3 && docked)
4516 data = 1; /* button */
4517 else if (event == 3 && !docked)
4518 data = 2; /* undock */
4519 else if (event == 0 && docked)
4520 data = 3; /* dock */
4521 else {
4522 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4523 event, _sta(dock_handle));
4524 data = 0; /* unknown */
4526 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4527 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4528 dev_name(&ibm->acpi->device->dev),
4529 event, data);
4532 static int dock_read(char *p)
4534 int len = 0;
4535 int docked = dock_docked();
4537 if (!dock_handle)
4538 len += sprintf(p + len, "status:\t\tnot supported\n");
4539 else if (!docked)
4540 len += sprintf(p + len, "status:\t\tundocked\n");
4541 else {
4542 len += sprintf(p + len, "status:\t\tdocked\n");
4543 len += sprintf(p + len, "commands:\tdock, undock\n");
4546 return len;
4549 static int dock_write(char *buf)
4551 char *cmd;
4553 if (!dock_docked())
4554 return -ENODEV;
4556 while ((cmd = next_cmd(&buf))) {
4557 if (strlencmp(cmd, "undock") == 0) {
4558 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4559 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4560 return -EIO;
4561 } else if (strlencmp(cmd, "dock") == 0) {
4562 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4563 return -EIO;
4564 } else
4565 return -EINVAL;
4568 return 0;
4571 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4573 /*************************************************************************
4574 * Bay subdriver
4577 #ifdef CONFIG_THINKPAD_ACPI_BAY
4579 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4580 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4581 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4582 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4583 ); /* A21e, R30, R31 */
4584 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4585 "_EJ0", /* all others */
4586 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4587 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4588 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4589 ); /* all others */
4590 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4591 "_EJ0", /* 770x */
4592 ); /* all others */
4594 static int __init bay_init(struct ibm_init_struct *iibm)
4596 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4598 TPACPI_ACPIHANDLE_INIT(bay);
4599 if (bay_handle)
4600 TPACPI_ACPIHANDLE_INIT(bay_ej);
4601 TPACPI_ACPIHANDLE_INIT(bay2);
4602 if (bay2_handle)
4603 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4605 tp_features.bay_status = bay_handle &&
4606 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4607 tp_features.bay_status2 = bay2_handle &&
4608 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4610 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4611 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4612 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4613 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4615 vdbg_printk(TPACPI_DBG_INIT,
4616 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4617 str_supported(tp_features.bay_status),
4618 str_supported(tp_features.bay_eject),
4619 str_supported(tp_features.bay_status2),
4620 str_supported(tp_features.bay_eject2));
4622 return (tp_features.bay_status || tp_features.bay_eject ||
4623 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4626 static void bay_notify(struct ibm_struct *ibm, u32 event)
4628 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4629 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4630 dev_name(&ibm->acpi->device->dev),
4631 event, 0);
4634 #define bay_occupied(b) (_sta(b##_handle) & 1)
4636 static int bay_read(char *p)
4638 int len = 0;
4639 int occupied = bay_occupied(bay);
4640 int occupied2 = bay_occupied(bay2);
4641 int eject, eject2;
4643 len += sprintf(p + len, "status:\t\t%s\n",
4644 tp_features.bay_status ?
4645 (occupied ? "occupied" : "unoccupied") :
4646 "not supported");
4647 if (tp_features.bay_status2)
4648 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4649 "occupied" : "unoccupied");
4651 eject = tp_features.bay_eject && occupied;
4652 eject2 = tp_features.bay_eject2 && occupied2;
4654 if (eject && eject2)
4655 len += sprintf(p + len, "commands:\teject, eject2\n");
4656 else if (eject)
4657 len += sprintf(p + len, "commands:\teject\n");
4658 else if (eject2)
4659 len += sprintf(p + len, "commands:\teject2\n");
4661 return len;
4664 static int bay_write(char *buf)
4666 char *cmd;
4668 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4669 return -ENODEV;
4671 while ((cmd = next_cmd(&buf))) {
4672 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4673 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4674 return -EIO;
4675 } else if (tp_features.bay_eject2 &&
4676 strlencmp(cmd, "eject2") == 0) {
4677 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4678 return -EIO;
4679 } else
4680 return -EINVAL;
4683 return 0;
4686 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4687 .notify = bay_notify,
4688 .handle = &bay_handle,
4689 .type = ACPI_SYSTEM_NOTIFY,
4692 static struct ibm_struct bay_driver_data = {
4693 .name = "bay",
4694 .read = bay_read,
4695 .write = bay_write,
4696 .acpi = &ibm_bay_acpidriver,
4699 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4701 /*************************************************************************
4702 * CMOS subdriver
4705 /* sysfs cmos_command -------------------------------------------------- */
4706 static ssize_t cmos_command_store(struct device *dev,
4707 struct device_attribute *attr,
4708 const char *buf, size_t count)
4710 unsigned long cmos_cmd;
4711 int res;
4713 if (parse_strtoul(buf, 21, &cmos_cmd))
4714 return -EINVAL;
4716 res = issue_thinkpad_cmos_command(cmos_cmd);
4717 return (res)? res : count;
4720 static struct device_attribute dev_attr_cmos_command =
4721 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4723 /* --------------------------------------------------------------------- */
4725 static int __init cmos_init(struct ibm_init_struct *iibm)
4727 int res;
4729 vdbg_printk(TPACPI_DBG_INIT,
4730 "initializing cmos commands subdriver\n");
4732 TPACPI_ACPIHANDLE_INIT(cmos);
4734 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4735 str_supported(cmos_handle != NULL));
4737 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4738 if (res)
4739 return res;
4741 return (cmos_handle)? 0 : 1;
4744 static void cmos_exit(void)
4746 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4749 static int cmos_read(char *p)
4751 int len = 0;
4753 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4754 R30, R31, T20-22, X20-21 */
4755 if (!cmos_handle)
4756 len += sprintf(p + len, "status:\t\tnot supported\n");
4757 else {
4758 len += sprintf(p + len, "status:\t\tsupported\n");
4759 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4762 return len;
4765 static int cmos_write(char *buf)
4767 char *cmd;
4768 int cmos_cmd, res;
4770 while ((cmd = next_cmd(&buf))) {
4771 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4772 cmos_cmd >= 0 && cmos_cmd <= 21) {
4773 /* cmos_cmd set */
4774 } else
4775 return -EINVAL;
4777 res = issue_thinkpad_cmos_command(cmos_cmd);
4778 if (res)
4779 return res;
4782 return 0;
4785 static struct ibm_struct cmos_driver_data = {
4786 .name = "cmos",
4787 .read = cmos_read,
4788 .write = cmos_write,
4789 .exit = cmos_exit,
4792 /*************************************************************************
4793 * LED subdriver
4796 enum led_access_mode {
4797 TPACPI_LED_NONE = 0,
4798 TPACPI_LED_570, /* 570 */
4799 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4800 TPACPI_LED_NEW, /* all others */
4803 enum { /* For TPACPI_LED_OLD */
4804 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4805 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4806 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4809 static enum led_access_mode led_supported;
4811 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4812 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4813 /* T20-22, X20-21 */
4814 "LED", /* all others */
4815 ); /* R30, R31 */
4817 #define TPACPI_LED_NUMLEDS 8
4818 static struct tpacpi_led_classdev *tpacpi_leds;
4819 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4820 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4821 /* there's a limit of 19 chars + NULL before 2.6.26 */
4822 "tpacpi::power",
4823 "tpacpi:orange:batt",
4824 "tpacpi:green:batt",
4825 "tpacpi::dock_active",
4826 "tpacpi::bay_active",
4827 "tpacpi::dock_batt",
4828 "tpacpi::unknown_led",
4829 "tpacpi::standby",
4831 #define TPACPI_SAFE_LEDS 0x0081U
4833 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4835 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4836 return false;
4837 #else
4838 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4839 #endif
4842 static int led_get_status(const unsigned int led)
4844 int status;
4845 enum led_status_t led_s;
4847 switch (led_supported) {
4848 case TPACPI_LED_570:
4849 if (!acpi_evalf(ec_handle,
4850 &status, "GLED", "dd", 1 << led))
4851 return -EIO;
4852 led_s = (status == 0)?
4853 TPACPI_LED_OFF :
4854 ((status == 1)?
4855 TPACPI_LED_ON :
4856 TPACPI_LED_BLINK);
4857 tpacpi_led_state_cache[led] = led_s;
4858 return led_s;
4859 default:
4860 return -ENXIO;
4863 /* not reached */
4866 static int led_set_status(const unsigned int led,
4867 const enum led_status_t ledstatus)
4869 /* off, on, blink. Index is led_status_t */
4870 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4871 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4873 int rc = 0;
4875 switch (led_supported) {
4876 case TPACPI_LED_570:
4877 /* 570 */
4878 if (unlikely(led > 7))
4879 return -EINVAL;
4880 if (unlikely(tpacpi_is_led_restricted(led)))
4881 return -EPERM;
4882 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4883 (1 << led), led_sled_arg1[ledstatus]))
4884 rc = -EIO;
4885 break;
4886 case TPACPI_LED_OLD:
4887 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4888 if (unlikely(led > 7))
4889 return -EINVAL;
4890 if (unlikely(tpacpi_is_led_restricted(led)))
4891 return -EPERM;
4892 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4893 if (rc >= 0)
4894 rc = ec_write(TPACPI_LED_EC_HLBL,
4895 (ledstatus == TPACPI_LED_BLINK) << led);
4896 if (rc >= 0)
4897 rc = ec_write(TPACPI_LED_EC_HLCL,
4898 (ledstatus != TPACPI_LED_OFF) << led);
4899 break;
4900 case TPACPI_LED_NEW:
4901 /* all others */
4902 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4903 return -EINVAL;
4904 if (unlikely(tpacpi_is_led_restricted(led)))
4905 return -EPERM;
4906 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4907 led, led_led_arg1[ledstatus]))
4908 rc = -EIO;
4909 break;
4910 default:
4911 rc = -ENXIO;
4914 if (!rc)
4915 tpacpi_led_state_cache[led] = ledstatus;
4917 return rc;
4920 static void led_set_status_worker(struct work_struct *work)
4922 struct tpacpi_led_classdev *data =
4923 container_of(work, struct tpacpi_led_classdev, work);
4925 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4926 led_set_status(data->led, data->new_state);
4929 static void led_sysfs_set(struct led_classdev *led_cdev,
4930 enum led_brightness brightness)
4932 struct tpacpi_led_classdev *data = container_of(led_cdev,
4933 struct tpacpi_led_classdev, led_classdev);
4935 if (brightness == LED_OFF)
4936 data->new_state = TPACPI_LED_OFF;
4937 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4938 data->new_state = TPACPI_LED_ON;
4939 else
4940 data->new_state = TPACPI_LED_BLINK;
4942 queue_work(tpacpi_wq, &data->work);
4945 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4946 unsigned long *delay_on, unsigned long *delay_off)
4948 struct tpacpi_led_classdev *data = container_of(led_cdev,
4949 struct tpacpi_led_classdev, led_classdev);
4951 /* Can we choose the flash rate? */
4952 if (*delay_on == 0 && *delay_off == 0) {
4953 /* yes. set them to the hardware blink rate (1 Hz) */
4954 *delay_on = 500; /* ms */
4955 *delay_off = 500; /* ms */
4956 } else if ((*delay_on != 500) || (*delay_off != 500))
4957 return -EINVAL;
4959 data->new_state = TPACPI_LED_BLINK;
4960 queue_work(tpacpi_wq, &data->work);
4962 return 0;
4965 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4967 int rc;
4969 struct tpacpi_led_classdev *data = container_of(led_cdev,
4970 struct tpacpi_led_classdev, led_classdev);
4972 rc = led_get_status(data->led);
4974 if (rc == TPACPI_LED_OFF || rc < 0)
4975 rc = LED_OFF; /* no error handling in led class :( */
4976 else
4977 rc = LED_FULL;
4979 return rc;
4982 static void led_exit(void)
4984 unsigned int i;
4986 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4987 if (tpacpi_leds[i].led_classdev.name)
4988 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4991 kfree(tpacpi_leds);
4994 static int __init tpacpi_init_led(unsigned int led)
4996 int rc;
4998 tpacpi_leds[led].led = led;
5000 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5001 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5002 if (led_supported == TPACPI_LED_570)
5003 tpacpi_leds[led].led_classdev.brightness_get =
5004 &led_sysfs_get;
5006 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5008 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5010 rc = led_classdev_register(&tpacpi_pdev->dev,
5011 &tpacpi_leds[led].led_classdev);
5012 if (rc < 0)
5013 tpacpi_leds[led].led_classdev.name = NULL;
5015 return rc;
5018 static int __init led_init(struct ibm_init_struct *iibm)
5020 unsigned int i;
5021 int rc;
5023 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5025 TPACPI_ACPIHANDLE_INIT(led);
5027 if (!led_handle)
5028 /* led not supported on R30, R31 */
5029 led_supported = TPACPI_LED_NONE;
5030 else if (strlencmp(led_path, "SLED") == 0)
5031 /* 570 */
5032 led_supported = TPACPI_LED_570;
5033 else if (strlencmp(led_path, "SYSL") == 0)
5034 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5035 led_supported = TPACPI_LED_OLD;
5036 else
5037 /* all others */
5038 led_supported = TPACPI_LED_NEW;
5040 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5041 str_supported(led_supported), led_supported);
5043 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5044 GFP_KERNEL);
5045 if (!tpacpi_leds) {
5046 printk(TPACPI_ERR "Out of memory for LED data\n");
5047 return -ENOMEM;
5050 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5051 if (!tpacpi_is_led_restricted(i)) {
5052 rc = tpacpi_init_led(i);
5053 if (rc < 0) {
5054 led_exit();
5055 return rc;
5060 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5061 if (led_supported != TPACPI_LED_NONE)
5062 printk(TPACPI_NOTICE
5063 "warning: userspace override of important "
5064 "firmware LEDs is enabled\n");
5065 #endif
5066 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
5069 #define str_led_status(s) \
5070 ((s) == TPACPI_LED_OFF ? "off" : \
5071 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5073 static int led_read(char *p)
5075 int len = 0;
5077 if (!led_supported) {
5078 len += sprintf(p + len, "status:\t\tnot supported\n");
5079 return len;
5081 len += sprintf(p + len, "status:\t\tsupported\n");
5083 if (led_supported == TPACPI_LED_570) {
5084 /* 570 */
5085 int i, status;
5086 for (i = 0; i < 8; i++) {
5087 status = led_get_status(i);
5088 if (status < 0)
5089 return -EIO;
5090 len += sprintf(p + len, "%d:\t\t%s\n",
5091 i, str_led_status(status));
5095 len += sprintf(p + len, "commands:\t"
5096 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5098 return len;
5101 static int led_write(char *buf)
5103 char *cmd;
5104 int led, rc;
5105 enum led_status_t s;
5107 if (!led_supported)
5108 return -ENODEV;
5110 while ((cmd = next_cmd(&buf))) {
5111 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5112 return -EINVAL;
5114 if (strstr(cmd, "off")) {
5115 s = TPACPI_LED_OFF;
5116 } else if (strstr(cmd, "on")) {
5117 s = TPACPI_LED_ON;
5118 } else if (strstr(cmd, "blink")) {
5119 s = TPACPI_LED_BLINK;
5120 } else {
5121 return -EINVAL;
5124 rc = led_set_status(led, s);
5125 if (rc < 0)
5126 return rc;
5129 return 0;
5132 static struct ibm_struct led_driver_data = {
5133 .name = "led",
5134 .read = led_read,
5135 .write = led_write,
5136 .exit = led_exit,
5139 /*************************************************************************
5140 * Beep subdriver
5143 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5145 static int __init beep_init(struct ibm_init_struct *iibm)
5147 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5149 TPACPI_ACPIHANDLE_INIT(beep);
5151 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5152 str_supported(beep_handle != NULL));
5154 return (beep_handle)? 0 : 1;
5157 static int beep_read(char *p)
5159 int len = 0;
5161 if (!beep_handle)
5162 len += sprintf(p + len, "status:\t\tnot supported\n");
5163 else {
5164 len += sprintf(p + len, "status:\t\tsupported\n");
5165 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5168 return len;
5171 static int beep_write(char *buf)
5173 char *cmd;
5174 int beep_cmd;
5176 if (!beep_handle)
5177 return -ENODEV;
5179 while ((cmd = next_cmd(&buf))) {
5180 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5181 beep_cmd >= 0 && beep_cmd <= 17) {
5182 /* beep_cmd set */
5183 } else
5184 return -EINVAL;
5185 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5186 return -EIO;
5189 return 0;
5192 static struct ibm_struct beep_driver_data = {
5193 .name = "beep",
5194 .read = beep_read,
5195 .write = beep_write,
5198 /*************************************************************************
5199 * Thermal subdriver
5202 enum thermal_access_mode {
5203 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5204 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5205 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5206 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5207 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5210 enum { /* TPACPI_THERMAL_TPEC_* */
5211 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5212 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5213 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5216 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5217 struct ibm_thermal_sensors_struct {
5218 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5221 static enum thermal_access_mode thermal_read_mode;
5223 /* idx is zero-based */
5224 static int thermal_get_sensor(int idx, s32 *value)
5226 int t;
5227 s8 tmp;
5228 char tmpi[5];
5230 t = TP_EC_THERMAL_TMP0;
5232 switch (thermal_read_mode) {
5233 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5234 case TPACPI_THERMAL_TPEC_16:
5235 if (idx >= 8 && idx <= 15) {
5236 t = TP_EC_THERMAL_TMP8;
5237 idx -= 8;
5239 /* fallthrough */
5240 #endif
5241 case TPACPI_THERMAL_TPEC_8:
5242 if (idx <= 7) {
5243 if (!acpi_ec_read(t + idx, &tmp))
5244 return -EIO;
5245 *value = tmp * 1000;
5246 return 0;
5248 break;
5250 case TPACPI_THERMAL_ACPI_UPDT:
5251 if (idx <= 7) {
5252 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5253 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5254 return -EIO;
5255 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5256 return -EIO;
5257 *value = (t - 2732) * 100;
5258 return 0;
5260 break;
5262 case TPACPI_THERMAL_ACPI_TMP07:
5263 if (idx <= 7) {
5264 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5265 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5266 return -EIO;
5267 if (t > 127 || t < -127)
5268 t = TP_EC_THERMAL_TMP_NA;
5269 *value = t * 1000;
5270 return 0;
5272 break;
5274 case TPACPI_THERMAL_NONE:
5275 default:
5276 return -ENOSYS;
5279 return -EINVAL;
5282 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5284 int res, i;
5285 int n;
5287 n = 8;
5288 i = 0;
5290 if (!s)
5291 return -EINVAL;
5293 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5294 n = 16;
5296 for (i = 0 ; i < n; i++) {
5297 res = thermal_get_sensor(i, &s->temp[i]);
5298 if (res)
5299 return res;
5302 return n;
5305 /* sysfs temp##_input -------------------------------------------------- */
5307 static ssize_t thermal_temp_input_show(struct device *dev,
5308 struct device_attribute *attr,
5309 char *buf)
5311 struct sensor_device_attribute *sensor_attr =
5312 to_sensor_dev_attr(attr);
5313 int idx = sensor_attr->index;
5314 s32 value;
5315 int res;
5317 res = thermal_get_sensor(idx, &value);
5318 if (res)
5319 return res;
5320 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5321 return -ENXIO;
5323 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5326 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5327 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5328 thermal_temp_input_show, NULL, _idxB)
5330 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5331 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5332 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5333 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5334 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5335 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5336 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5337 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5338 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5339 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5340 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5341 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5342 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5343 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5344 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5345 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5346 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5349 #define THERMAL_ATTRS(X) \
5350 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5352 static struct attribute *thermal_temp_input_attr[] = {
5353 THERMAL_ATTRS(8),
5354 THERMAL_ATTRS(9),
5355 THERMAL_ATTRS(10),
5356 THERMAL_ATTRS(11),
5357 THERMAL_ATTRS(12),
5358 THERMAL_ATTRS(13),
5359 THERMAL_ATTRS(14),
5360 THERMAL_ATTRS(15),
5361 THERMAL_ATTRS(0),
5362 THERMAL_ATTRS(1),
5363 THERMAL_ATTRS(2),
5364 THERMAL_ATTRS(3),
5365 THERMAL_ATTRS(4),
5366 THERMAL_ATTRS(5),
5367 THERMAL_ATTRS(6),
5368 THERMAL_ATTRS(7),
5369 NULL
5372 static const struct attribute_group thermal_temp_input16_group = {
5373 .attrs = thermal_temp_input_attr
5376 static const struct attribute_group thermal_temp_input8_group = {
5377 .attrs = &thermal_temp_input_attr[8]
5380 #undef THERMAL_SENSOR_ATTR_TEMP
5381 #undef THERMAL_ATTRS
5383 /* --------------------------------------------------------------------- */
5385 static int __init thermal_init(struct ibm_init_struct *iibm)
5387 u8 t, ta1, ta2;
5388 int i;
5389 int acpi_tmp7;
5390 int res;
5392 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5394 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5396 if (thinkpad_id.ec_model) {
5398 * Direct EC access mode: sensors at registers
5399 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5400 * non-implemented, thermal sensors return 0x80 when
5401 * not available
5404 ta1 = ta2 = 0;
5405 for (i = 0; i < 8; i++) {
5406 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5407 ta1 |= t;
5408 } else {
5409 ta1 = 0;
5410 break;
5412 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5413 ta2 |= t;
5414 } else {
5415 ta1 = 0;
5416 break;
5419 if (ta1 == 0) {
5420 /* This is sheer paranoia, but we handle it anyway */
5421 if (acpi_tmp7) {
5422 printk(TPACPI_ERR
5423 "ThinkPad ACPI EC access misbehaving, "
5424 "falling back to ACPI TMPx access "
5425 "mode\n");
5426 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5427 } else {
5428 printk(TPACPI_ERR
5429 "ThinkPad ACPI EC access misbehaving, "
5430 "disabling thermal sensors access\n");
5431 thermal_read_mode = TPACPI_THERMAL_NONE;
5433 } else {
5434 thermal_read_mode =
5435 (ta2 != 0) ?
5436 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5438 } else if (acpi_tmp7) {
5439 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5440 /* 600e/x, 770e, 770x */
5441 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5442 } else {
5443 /* Standard ACPI TMPx access, max 8 sensors */
5444 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5446 } else {
5447 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5448 thermal_read_mode = TPACPI_THERMAL_NONE;
5451 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5452 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5453 thermal_read_mode);
5455 switch (thermal_read_mode) {
5456 case TPACPI_THERMAL_TPEC_16:
5457 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5458 &thermal_temp_input16_group);
5459 if (res)
5460 return res;
5461 break;
5462 case TPACPI_THERMAL_TPEC_8:
5463 case TPACPI_THERMAL_ACPI_TMP07:
5464 case TPACPI_THERMAL_ACPI_UPDT:
5465 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5466 &thermal_temp_input8_group);
5467 if (res)
5468 return res;
5469 break;
5470 case TPACPI_THERMAL_NONE:
5471 default:
5472 return 1;
5475 return 0;
5478 static void thermal_exit(void)
5480 switch (thermal_read_mode) {
5481 case TPACPI_THERMAL_TPEC_16:
5482 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5483 &thermal_temp_input16_group);
5484 break;
5485 case TPACPI_THERMAL_TPEC_8:
5486 case TPACPI_THERMAL_ACPI_TMP07:
5487 case TPACPI_THERMAL_ACPI_UPDT:
5488 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5489 &thermal_temp_input16_group);
5490 break;
5491 case TPACPI_THERMAL_NONE:
5492 default:
5493 break;
5497 static int thermal_read(char *p)
5499 int len = 0;
5500 int n, i;
5501 struct ibm_thermal_sensors_struct t;
5503 n = thermal_get_sensors(&t);
5504 if (unlikely(n < 0))
5505 return n;
5507 len += sprintf(p + len, "temperatures:\t");
5509 if (n > 0) {
5510 for (i = 0; i < (n - 1); i++)
5511 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5512 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5513 } else
5514 len += sprintf(p + len, "not supported\n");
5516 return len;
5519 static struct ibm_struct thermal_driver_data = {
5520 .name = "thermal",
5521 .read = thermal_read,
5522 .exit = thermal_exit,
5525 /*************************************************************************
5526 * EC Dump subdriver
5529 static u8 ecdump_regs[256];
5531 static int ecdump_read(char *p)
5533 int len = 0;
5534 int i, j;
5535 u8 v;
5537 len += sprintf(p + len, "EC "
5538 " +00 +01 +02 +03 +04 +05 +06 +07"
5539 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5540 for (i = 0; i < 256; i += 16) {
5541 len += sprintf(p + len, "EC 0x%02x:", i);
5542 for (j = 0; j < 16; j++) {
5543 if (!acpi_ec_read(i + j, &v))
5544 break;
5545 if (v != ecdump_regs[i + j])
5546 len += sprintf(p + len, " *%02x", v);
5547 else
5548 len += sprintf(p + len, " %02x", v);
5549 ecdump_regs[i + j] = v;
5551 len += sprintf(p + len, "\n");
5552 if (j != 16)
5553 break;
5556 /* These are way too dangerous to advertise openly... */
5557 #if 0
5558 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5559 " (<offset> is 00-ff, <value> is 00-ff)\n");
5560 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5561 " (<offset> is 00-ff, <value> is 0-255)\n");
5562 #endif
5563 return len;
5566 static int ecdump_write(char *buf)
5568 char *cmd;
5569 int i, v;
5571 while ((cmd = next_cmd(&buf))) {
5572 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5573 /* i and v set */
5574 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5575 /* i and v set */
5576 } else
5577 return -EINVAL;
5578 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5579 if (!acpi_ec_write(i, v))
5580 return -EIO;
5581 } else
5582 return -EINVAL;
5585 return 0;
5588 static struct ibm_struct ecdump_driver_data = {
5589 .name = "ecdump",
5590 .read = ecdump_read,
5591 .write = ecdump_write,
5592 .flags.experimental = 1,
5595 /*************************************************************************
5596 * Backlight/brightness subdriver
5599 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5602 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5603 * CMOS NVRAM byte 0x5E, bits 0-3.
5605 * EC HBRV (0x31) has the following layout
5606 * Bit 7: unknown function
5607 * Bit 6: unknown function
5608 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5609 * Bit 4: must be set to zero to avoid problems
5610 * Bit 3-0: backlight brightness level
5612 * brightness_get_raw returns status data in the HBRV layout
5615 enum {
5616 TP_EC_BACKLIGHT = 0x31,
5618 /* TP_EC_BACKLIGHT bitmasks */
5619 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5620 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5621 TP_EC_BACKLIGHT_MAPSW = 0x20,
5624 enum tpacpi_brightness_access_mode {
5625 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5626 TPACPI_BRGHT_MODE_EC, /* EC control */
5627 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5628 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5629 TPACPI_BRGHT_MODE_MAX
5632 static struct backlight_device *ibm_backlight_device;
5634 static enum tpacpi_brightness_access_mode brightness_mode =
5635 TPACPI_BRGHT_MODE_MAX;
5637 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5639 static struct mutex brightness_mutex;
5641 /* NVRAM brightness access,
5642 * call with brightness_mutex held! */
5643 static unsigned int tpacpi_brightness_nvram_get(void)
5645 u8 lnvram;
5647 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5648 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5649 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5650 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5652 return lnvram;
5655 static void tpacpi_brightness_checkpoint_nvram(void)
5657 u8 lec = 0;
5658 u8 b_nvram;
5660 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5661 return;
5663 vdbg_printk(TPACPI_DBG_BRGHT,
5664 "trying to checkpoint backlight level to NVRAM...\n");
5666 if (mutex_lock_killable(&brightness_mutex) < 0)
5667 return;
5669 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5670 goto unlock;
5671 lec &= TP_EC_BACKLIGHT_LVLMSK;
5672 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5674 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5675 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5676 /* NVRAM needs update */
5677 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5678 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5679 b_nvram |= lec;
5680 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5681 dbg_printk(TPACPI_DBG_BRGHT,
5682 "updated NVRAM backlight level to %u (0x%02x)\n",
5683 (unsigned int) lec, (unsigned int) b_nvram);
5684 } else
5685 vdbg_printk(TPACPI_DBG_BRGHT,
5686 "NVRAM backlight level already is %u (0x%02x)\n",
5687 (unsigned int) lec, (unsigned int) b_nvram);
5689 unlock:
5690 mutex_unlock(&brightness_mutex);
5694 /* call with brightness_mutex held! */
5695 static int tpacpi_brightness_get_raw(int *status)
5697 u8 lec = 0;
5699 switch (brightness_mode) {
5700 case TPACPI_BRGHT_MODE_UCMS_STEP:
5701 *status = tpacpi_brightness_nvram_get();
5702 return 0;
5703 case TPACPI_BRGHT_MODE_EC:
5704 case TPACPI_BRGHT_MODE_ECNVRAM:
5705 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5706 return -EIO;
5707 *status = lec;
5708 return 0;
5709 default:
5710 return -ENXIO;
5714 /* call with brightness_mutex held! */
5715 /* do NOT call with illegal backlight level value */
5716 static int tpacpi_brightness_set_ec(unsigned int value)
5718 u8 lec = 0;
5720 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5721 return -EIO;
5723 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5724 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5725 (value & TP_EC_BACKLIGHT_LVLMSK))))
5726 return -EIO;
5728 return 0;
5731 /* call with brightness_mutex held! */
5732 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5734 int cmos_cmd, inc;
5735 unsigned int current_value, i;
5737 current_value = tpacpi_brightness_nvram_get();
5739 if (value == current_value)
5740 return 0;
5742 cmos_cmd = (value > current_value) ?
5743 TP_CMOS_BRIGHTNESS_UP :
5744 TP_CMOS_BRIGHTNESS_DOWN;
5745 inc = (value > current_value) ? 1 : -1;
5747 for (i = current_value; i != value; i += inc)
5748 if (issue_thinkpad_cmos_command(cmos_cmd))
5749 return -EIO;
5751 return 0;
5754 /* May return EINTR which can always be mapped to ERESTARTSYS */
5755 static int brightness_set(unsigned int value)
5757 int res;
5759 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5760 value < 0)
5761 return -EINVAL;
5763 vdbg_printk(TPACPI_DBG_BRGHT,
5764 "set backlight level to %d\n", value);
5766 res = mutex_lock_killable(&brightness_mutex);
5767 if (res < 0)
5768 return res;
5770 switch (brightness_mode) {
5771 case TPACPI_BRGHT_MODE_EC:
5772 case TPACPI_BRGHT_MODE_ECNVRAM:
5773 res = tpacpi_brightness_set_ec(value);
5774 break;
5775 case TPACPI_BRGHT_MODE_UCMS_STEP:
5776 res = tpacpi_brightness_set_ucmsstep(value);
5777 break;
5778 default:
5779 res = -ENXIO;
5782 mutex_unlock(&brightness_mutex);
5783 return res;
5786 /* sysfs backlight class ----------------------------------------------- */
5788 static int brightness_update_status(struct backlight_device *bd)
5790 unsigned int level =
5791 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5792 bd->props.power == FB_BLANK_UNBLANK) ?
5793 bd->props.brightness : 0;
5795 dbg_printk(TPACPI_DBG_BRGHT,
5796 "backlight: attempt to set level to %d\n",
5797 level);
5799 /* it is the backlight class's job (caller) to handle
5800 * EINTR and other errors properly */
5801 return brightness_set(level);
5804 static int brightness_get(struct backlight_device *bd)
5806 int status, res;
5808 res = mutex_lock_killable(&brightness_mutex);
5809 if (res < 0)
5810 return 0;
5812 res = tpacpi_brightness_get_raw(&status);
5814 mutex_unlock(&brightness_mutex);
5816 if (res < 0)
5817 return 0;
5819 return status & TP_EC_BACKLIGHT_LVLMSK;
5822 static struct backlight_ops ibm_backlight_data = {
5823 .get_brightness = brightness_get,
5824 .update_status = brightness_update_status,
5827 /* --------------------------------------------------------------------- */
5829 static int __init brightness_init(struct ibm_init_struct *iibm)
5831 int b;
5833 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5835 mutex_init(&brightness_mutex);
5838 * We always attempt to detect acpi support, so as to switch
5839 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5840 * going to publish a backlight interface
5842 b = tpacpi_check_std_acpi_brightness_support();
5843 if (b > 0) {
5845 if (acpi_video_backlight_support()) {
5846 if (brightness_enable > 1) {
5847 printk(TPACPI_NOTICE
5848 "Standard ACPI backlight interface "
5849 "available, not loading native one.\n");
5850 return 1;
5851 } else if (brightness_enable == 1) {
5852 printk(TPACPI_NOTICE
5853 "Backlight control force enabled, even if standard "
5854 "ACPI backlight interface is available\n");
5856 } else {
5857 if (brightness_enable > 1) {
5858 printk(TPACPI_NOTICE
5859 "Standard ACPI backlight interface not "
5860 "available, thinkpad_acpi native "
5861 "brightness control enabled\n");
5866 if (!brightness_enable) {
5867 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5868 "brightness support disabled by "
5869 "module parameter\n");
5870 return 1;
5873 if (b > 16) {
5874 printk(TPACPI_ERR
5875 "Unsupported brightness interface, "
5876 "please contact %s\n", TPACPI_MAIL);
5877 return 1;
5879 if (b == 16)
5880 tp_features.bright_16levels = 1;
5883 * Check for module parameter bogosity, note that we
5884 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5885 * able to detect "unspecified"
5887 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5888 return -EINVAL;
5890 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5891 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5892 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5893 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5895 * IBM models that define HBRV probably have
5896 * EC-based backlight level control
5898 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5899 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5900 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5901 else
5902 /* all other IBM ThinkPads */
5903 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5904 } else
5905 /* All Lenovo ThinkPads */
5906 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5908 dbg_printk(TPACPI_DBG_BRGHT,
5909 "selected brightness_mode=%d\n",
5910 brightness_mode);
5913 if (tpacpi_brightness_get_raw(&b) < 0)
5914 return 1;
5916 if (tp_features.bright_16levels)
5917 printk(TPACPI_INFO
5918 "detected a 16-level brightness capable ThinkPad\n");
5920 ibm_backlight_device = backlight_device_register(
5921 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5922 &ibm_backlight_data);
5923 if (IS_ERR(ibm_backlight_device)) {
5924 printk(TPACPI_ERR "Could not register backlight device\n");
5925 return PTR_ERR(ibm_backlight_device);
5927 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5928 "brightness is supported\n");
5930 ibm_backlight_device->props.max_brightness =
5931 (tp_features.bright_16levels)? 15 : 7;
5932 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5933 backlight_update_status(ibm_backlight_device);
5935 return 0;
5938 static void brightness_suspend(pm_message_t state)
5940 tpacpi_brightness_checkpoint_nvram();
5943 static void brightness_shutdown(void)
5945 tpacpi_brightness_checkpoint_nvram();
5948 static void brightness_exit(void)
5950 if (ibm_backlight_device) {
5951 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5952 "calling backlight_device_unregister()\n");
5953 backlight_device_unregister(ibm_backlight_device);
5956 tpacpi_brightness_checkpoint_nvram();
5959 static int brightness_read(char *p)
5961 int len = 0;
5962 int level;
5964 level = brightness_get(NULL);
5965 if (level < 0) {
5966 len += sprintf(p + len, "level:\t\tunreadable\n");
5967 } else {
5968 len += sprintf(p + len, "level:\t\t%d\n", level);
5969 len += sprintf(p + len, "commands:\tup, down\n");
5970 len += sprintf(p + len, "commands:\tlevel <level>"
5971 " (<level> is 0-%d)\n",
5972 (tp_features.bright_16levels) ? 15 : 7);
5975 return len;
5978 static int brightness_write(char *buf)
5980 int level;
5981 int rc;
5982 char *cmd;
5983 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5985 level = brightness_get(NULL);
5986 if (level < 0)
5987 return level;
5989 while ((cmd = next_cmd(&buf))) {
5990 if (strlencmp(cmd, "up") == 0) {
5991 if (level < max_level)
5992 level++;
5993 } else if (strlencmp(cmd, "down") == 0) {
5994 if (level > 0)
5995 level--;
5996 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5997 level >= 0 && level <= max_level) {
5998 /* new level set */
5999 } else
6000 return -EINVAL;
6003 tpacpi_disclose_usertask("procfs brightness",
6004 "set level to %d\n", level);
6007 * Now we know what the final level should be, so we try to set it.
6008 * Doing it this way makes the syscall restartable in case of EINTR
6010 rc = brightness_set(level);
6011 return (rc == -EINTR)? ERESTARTSYS : rc;
6014 static struct ibm_struct brightness_driver_data = {
6015 .name = "brightness",
6016 .read = brightness_read,
6017 .write = brightness_write,
6018 .exit = brightness_exit,
6019 .suspend = brightness_suspend,
6020 .shutdown = brightness_shutdown,
6023 /*************************************************************************
6024 * Volume subdriver
6027 static int volume_offset = 0x30;
6029 static int volume_read(char *p)
6031 int len = 0;
6032 u8 level;
6034 if (!acpi_ec_read(volume_offset, &level)) {
6035 len += sprintf(p + len, "level:\t\tunreadable\n");
6036 } else {
6037 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6038 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6039 len += sprintf(p + len, "commands:\tup, down, mute\n");
6040 len += sprintf(p + len, "commands:\tlevel <level>"
6041 " (<level> is 0-15)\n");
6044 return len;
6047 static int volume_write(char *buf)
6049 int cmos_cmd, inc, i;
6050 u8 level, mute;
6051 int new_level, new_mute;
6052 char *cmd;
6054 while ((cmd = next_cmd(&buf))) {
6055 if (!acpi_ec_read(volume_offset, &level))
6056 return -EIO;
6057 new_mute = mute = level & 0x40;
6058 new_level = level = level & 0xf;
6060 if (strlencmp(cmd, "up") == 0) {
6061 if (mute)
6062 new_mute = 0;
6063 else
6064 new_level = level == 15 ? 15 : level + 1;
6065 } else if (strlencmp(cmd, "down") == 0) {
6066 if (mute)
6067 new_mute = 0;
6068 else
6069 new_level = level == 0 ? 0 : level - 1;
6070 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6071 new_level >= 0 && new_level <= 15) {
6072 /* new_level set */
6073 } else if (strlencmp(cmd, "mute") == 0) {
6074 new_mute = 0x40;
6075 } else
6076 return -EINVAL;
6078 if (new_level != level) {
6079 /* mute doesn't change */
6081 cmos_cmd = (new_level > level) ?
6082 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6083 inc = new_level > level ? 1 : -1;
6085 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6086 !acpi_ec_write(volume_offset, level)))
6087 return -EIO;
6089 for (i = level; i != new_level; i += inc)
6090 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6091 !acpi_ec_write(volume_offset, i + inc))
6092 return -EIO;
6094 if (mute &&
6095 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6096 !acpi_ec_write(volume_offset, new_level + mute))) {
6097 return -EIO;
6101 if (new_mute != mute) {
6102 /* level doesn't change */
6104 cmos_cmd = (new_mute) ?
6105 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6107 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6108 !acpi_ec_write(volume_offset, level + new_mute))
6109 return -EIO;
6113 return 0;
6116 static struct ibm_struct volume_driver_data = {
6117 .name = "volume",
6118 .read = volume_read,
6119 .write = volume_write,
6122 /*************************************************************************
6123 * Fan subdriver
6127 * FAN ACCESS MODES
6129 * TPACPI_FAN_RD_ACPI_GFAN:
6130 * ACPI GFAN method: returns fan level
6132 * see TPACPI_FAN_WR_ACPI_SFAN
6133 * EC 0x2f (HFSP) not available if GFAN exists
6135 * TPACPI_FAN_WR_ACPI_SFAN:
6136 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6138 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6139 * it for writing.
6141 * TPACPI_FAN_WR_TPEC:
6142 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6143 * Supported on almost all ThinkPads
6145 * Fan speed changes of any sort (including those caused by the
6146 * disengaged mode) are usually done slowly by the firmware as the
6147 * maximum ammount of fan duty cycle change per second seems to be
6148 * limited.
6150 * Reading is not available if GFAN exists.
6151 * Writing is not available if SFAN exists.
6153 * Bits
6154 * 7 automatic mode engaged;
6155 * (default operation mode of the ThinkPad)
6156 * fan level is ignored in this mode.
6157 * 6 full speed mode (takes precedence over bit 7);
6158 * not available on all thinkpads. May disable
6159 * the tachometer while the fan controller ramps up
6160 * the speed (which can take up to a few *minutes*).
6161 * Speeds up fan to 100% duty-cycle, which is far above
6162 * the standard RPM levels. It is not impossible that
6163 * it could cause hardware damage.
6164 * 5-3 unused in some models. Extra bits for fan level
6165 * in others, but still useless as all values above
6166 * 7 map to the same speed as level 7 in these models.
6167 * 2-0 fan level (0..7 usually)
6168 * 0x00 = stop
6169 * 0x07 = max (set when temperatures critical)
6170 * Some ThinkPads may have other levels, see
6171 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6173 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6174 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6175 * does so, its initial value is meaningless (0x07).
6177 * For firmware bugs, refer to:
6178 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6180 * ----
6182 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6183 * Main fan tachometer reading (in RPM)
6185 * This register is present on all ThinkPads with a new-style EC, and
6186 * it is known not to be present on the A21m/e, and T22, as there is
6187 * something else in offset 0x84 according to the ACPI DSDT. Other
6188 * ThinkPads from this same time period (and earlier) probably lack the
6189 * tachometer as well.
6191 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6192 * was never fixed by IBM to report the EC firmware version string
6193 * probably support the tachometer (like the early X models), so
6194 * detecting it is quite hard. We need more data to know for sure.
6196 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6197 * might result.
6199 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6200 * mode.
6202 * For firmware bugs, refer to:
6203 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6205 * TPACPI_FAN_WR_ACPI_FANS:
6206 * ThinkPad X31, X40, X41. Not available in the X60.
6208 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6209 * high speed. ACPI DSDT seems to map these three speeds to levels
6210 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6211 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6213 * The speeds are stored on handles
6214 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6216 * There are three default speed sets, acessible as handles:
6217 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6219 * ACPI DSDT switches which set is in use depending on various
6220 * factors.
6222 * TPACPI_FAN_WR_TPEC is also available and should be used to
6223 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6224 * but the ACPI tables just mention level 7.
6227 enum { /* Fan control constants */
6228 fan_status_offset = 0x2f, /* EC register 0x2f */
6229 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6230 * 0x84 must be read before 0x85 */
6232 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6233 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6235 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6238 enum fan_status_access_mode {
6239 TPACPI_FAN_NONE = 0, /* No fan status or control */
6240 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6241 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6244 enum fan_control_access_mode {
6245 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6246 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6247 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6248 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6251 enum fan_control_commands {
6252 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6253 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6254 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6255 * and also watchdog cmd */
6258 static int fan_control_allowed;
6260 static enum fan_status_access_mode fan_status_access_mode;
6261 static enum fan_control_access_mode fan_control_access_mode;
6262 static enum fan_control_commands fan_control_commands;
6264 static u8 fan_control_initial_status;
6265 static u8 fan_control_desired_level;
6266 static u8 fan_control_resume_level;
6267 static int fan_watchdog_maxinterval;
6269 static struct mutex fan_mutex;
6271 static void fan_watchdog_fire(struct work_struct *ignored);
6272 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6274 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6275 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6276 "\\FSPD", /* 600e/x, 770e, 770x */
6277 ); /* all others */
6278 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6279 "JFNS", /* 770x-JL */
6280 ); /* all others */
6283 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6284 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6285 * be in auto mode (0x80).
6287 * This is corrected by any write to HFSP either by the driver, or
6288 * by the firmware.
6290 * We assume 0x07 really means auto mode while this quirk is active,
6291 * as this is far more likely than the ThinkPad being in level 7,
6292 * which is only used by the firmware during thermal emergencies.
6294 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6295 * TP-70 (T43, R52), which are known to be buggy.
6298 static void fan_quirk1_setup(void)
6300 if (fan_control_initial_status == 0x07) {
6301 printk(TPACPI_NOTICE
6302 "fan_init: initial fan status is unknown, "
6303 "assuming it is in auto mode\n");
6304 tp_features.fan_ctrl_status_undef = 1;
6308 static void fan_quirk1_handle(u8 *fan_status)
6310 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6311 if (*fan_status != fan_control_initial_status) {
6312 /* something changed the HFSP regisnter since
6313 * driver init time, so it is not undefined
6314 * anymore */
6315 tp_features.fan_ctrl_status_undef = 0;
6316 } else {
6317 /* Return most likely status. In fact, it
6318 * might be the only possible status */
6319 *fan_status = TP_EC_FAN_AUTO;
6325 * Call with fan_mutex held
6327 static void fan_update_desired_level(u8 status)
6329 if ((status &
6330 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6331 if (status > 7)
6332 fan_control_desired_level = 7;
6333 else
6334 fan_control_desired_level = status;
6338 static int fan_get_status(u8 *status)
6340 u8 s;
6342 /* TODO:
6343 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6345 switch (fan_status_access_mode) {
6346 case TPACPI_FAN_RD_ACPI_GFAN:
6347 /* 570, 600e/x, 770e, 770x */
6349 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6350 return -EIO;
6352 if (likely(status))
6353 *status = s & 0x07;
6355 break;
6357 case TPACPI_FAN_RD_TPEC:
6358 /* all except 570, 600e/x, 770e, 770x */
6359 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6360 return -EIO;
6362 if (likely(status)) {
6363 *status = s;
6364 fan_quirk1_handle(status);
6367 break;
6369 default:
6370 return -ENXIO;
6373 return 0;
6376 static int fan_get_status_safe(u8 *status)
6378 int rc;
6379 u8 s;
6381 if (mutex_lock_killable(&fan_mutex))
6382 return -ERESTARTSYS;
6383 rc = fan_get_status(&s);
6384 if (!rc)
6385 fan_update_desired_level(s);
6386 mutex_unlock(&fan_mutex);
6388 if (status)
6389 *status = s;
6391 return rc;
6394 static int fan_get_speed(unsigned int *speed)
6396 u8 hi, lo;
6398 switch (fan_status_access_mode) {
6399 case TPACPI_FAN_RD_TPEC:
6400 /* all except 570, 600e/x, 770e, 770x */
6401 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6402 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6403 return -EIO;
6405 if (likely(speed))
6406 *speed = (hi << 8) | lo;
6408 break;
6410 default:
6411 return -ENXIO;
6414 return 0;
6417 static int fan_set_level(int level)
6419 if (!fan_control_allowed)
6420 return -EPERM;
6422 switch (fan_control_access_mode) {
6423 case TPACPI_FAN_WR_ACPI_SFAN:
6424 if (level >= 0 && level <= 7) {
6425 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6426 return -EIO;
6427 } else
6428 return -EINVAL;
6429 break;
6431 case TPACPI_FAN_WR_ACPI_FANS:
6432 case TPACPI_FAN_WR_TPEC:
6433 if (!(level & TP_EC_FAN_AUTO) &&
6434 !(level & TP_EC_FAN_FULLSPEED) &&
6435 ((level < 0) || (level > 7)))
6436 return -EINVAL;
6438 /* safety net should the EC not support AUTO
6439 * or FULLSPEED mode bits and just ignore them */
6440 if (level & TP_EC_FAN_FULLSPEED)
6441 level |= 7; /* safety min speed 7 */
6442 else if (level & TP_EC_FAN_AUTO)
6443 level |= 4; /* safety min speed 4 */
6445 if (!acpi_ec_write(fan_status_offset, level))
6446 return -EIO;
6447 else
6448 tp_features.fan_ctrl_status_undef = 0;
6449 break;
6451 default:
6452 return -ENXIO;
6455 vdbg_printk(TPACPI_DBG_FAN,
6456 "fan control: set fan control register to 0x%02x\n", level);
6457 return 0;
6460 static int fan_set_level_safe(int level)
6462 int rc;
6464 if (!fan_control_allowed)
6465 return -EPERM;
6467 if (mutex_lock_killable(&fan_mutex))
6468 return -ERESTARTSYS;
6470 if (level == TPACPI_FAN_LAST_LEVEL)
6471 level = fan_control_desired_level;
6473 rc = fan_set_level(level);
6474 if (!rc)
6475 fan_update_desired_level(level);
6477 mutex_unlock(&fan_mutex);
6478 return rc;
6481 static int fan_set_enable(void)
6483 u8 s;
6484 int rc;
6486 if (!fan_control_allowed)
6487 return -EPERM;
6489 if (mutex_lock_killable(&fan_mutex))
6490 return -ERESTARTSYS;
6492 switch (fan_control_access_mode) {
6493 case TPACPI_FAN_WR_ACPI_FANS:
6494 case TPACPI_FAN_WR_TPEC:
6495 rc = fan_get_status(&s);
6496 if (rc < 0)
6497 break;
6499 /* Don't go out of emergency fan mode */
6500 if (s != 7) {
6501 s &= 0x07;
6502 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6505 if (!acpi_ec_write(fan_status_offset, s))
6506 rc = -EIO;
6507 else {
6508 tp_features.fan_ctrl_status_undef = 0;
6509 rc = 0;
6511 break;
6513 case TPACPI_FAN_WR_ACPI_SFAN:
6514 rc = fan_get_status(&s);
6515 if (rc < 0)
6516 break;
6518 s &= 0x07;
6520 /* Set fan to at least level 4 */
6521 s |= 4;
6523 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6524 rc = -EIO;
6525 else
6526 rc = 0;
6527 break;
6529 default:
6530 rc = -ENXIO;
6533 mutex_unlock(&fan_mutex);
6535 if (!rc)
6536 vdbg_printk(TPACPI_DBG_FAN,
6537 "fan control: set fan control register to 0x%02x\n",
6539 return rc;
6542 static int fan_set_disable(void)
6544 int rc;
6546 if (!fan_control_allowed)
6547 return -EPERM;
6549 if (mutex_lock_killable(&fan_mutex))
6550 return -ERESTARTSYS;
6552 rc = 0;
6553 switch (fan_control_access_mode) {
6554 case TPACPI_FAN_WR_ACPI_FANS:
6555 case TPACPI_FAN_WR_TPEC:
6556 if (!acpi_ec_write(fan_status_offset, 0x00))
6557 rc = -EIO;
6558 else {
6559 fan_control_desired_level = 0;
6560 tp_features.fan_ctrl_status_undef = 0;
6562 break;
6564 case TPACPI_FAN_WR_ACPI_SFAN:
6565 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6566 rc = -EIO;
6567 else
6568 fan_control_desired_level = 0;
6569 break;
6571 default:
6572 rc = -ENXIO;
6575 if (!rc)
6576 vdbg_printk(TPACPI_DBG_FAN,
6577 "fan control: set fan control register to 0\n");
6579 mutex_unlock(&fan_mutex);
6580 return rc;
6583 static int fan_set_speed(int speed)
6585 int rc;
6587 if (!fan_control_allowed)
6588 return -EPERM;
6590 if (mutex_lock_killable(&fan_mutex))
6591 return -ERESTARTSYS;
6593 rc = 0;
6594 switch (fan_control_access_mode) {
6595 case TPACPI_FAN_WR_ACPI_FANS:
6596 if (speed >= 0 && speed <= 65535) {
6597 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6598 speed, speed, speed))
6599 rc = -EIO;
6600 } else
6601 rc = -EINVAL;
6602 break;
6604 default:
6605 rc = -ENXIO;
6608 mutex_unlock(&fan_mutex);
6609 return rc;
6612 static void fan_watchdog_reset(void)
6614 static int fan_watchdog_active;
6616 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6617 return;
6619 if (fan_watchdog_active)
6620 cancel_delayed_work(&fan_watchdog_task);
6622 if (fan_watchdog_maxinterval > 0 &&
6623 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6624 fan_watchdog_active = 1;
6625 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6626 msecs_to_jiffies(fan_watchdog_maxinterval
6627 * 1000))) {
6628 printk(TPACPI_ERR
6629 "failed to queue the fan watchdog, "
6630 "watchdog will not trigger\n");
6632 } else
6633 fan_watchdog_active = 0;
6636 static void fan_watchdog_fire(struct work_struct *ignored)
6638 int rc;
6640 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6641 return;
6643 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6644 rc = fan_set_enable();
6645 if (rc < 0) {
6646 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6647 "will try again later...\n", -rc);
6648 /* reschedule for later */
6649 fan_watchdog_reset();
6654 * SYSFS fan layout: hwmon compatible (device)
6656 * pwm*_enable:
6657 * 0: "disengaged" mode
6658 * 1: manual mode
6659 * 2: native EC "auto" mode (recommended, hardware default)
6661 * pwm*: set speed in manual mode, ignored otherwise.
6662 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6663 * interpolation.
6665 * fan*_input: tachometer reading, RPM
6668 * SYSFS fan layout: extensions
6670 * fan_watchdog (driver):
6671 * fan watchdog interval in seconds, 0 disables (default), max 120
6674 /* sysfs fan pwm1_enable ----------------------------------------------- */
6675 static ssize_t fan_pwm1_enable_show(struct device *dev,
6676 struct device_attribute *attr,
6677 char *buf)
6679 int res, mode;
6680 u8 status;
6682 res = fan_get_status_safe(&status);
6683 if (res)
6684 return res;
6686 if (status & TP_EC_FAN_FULLSPEED) {
6687 mode = 0;
6688 } else if (status & TP_EC_FAN_AUTO) {
6689 mode = 2;
6690 } else
6691 mode = 1;
6693 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6696 static ssize_t fan_pwm1_enable_store(struct device *dev,
6697 struct device_attribute *attr,
6698 const char *buf, size_t count)
6700 unsigned long t;
6701 int res, level;
6703 if (parse_strtoul(buf, 2, &t))
6704 return -EINVAL;
6706 tpacpi_disclose_usertask("hwmon pwm1_enable",
6707 "set fan mode to %lu\n", t);
6709 switch (t) {
6710 case 0:
6711 level = TP_EC_FAN_FULLSPEED;
6712 break;
6713 case 1:
6714 level = TPACPI_FAN_LAST_LEVEL;
6715 break;
6716 case 2:
6717 level = TP_EC_FAN_AUTO;
6718 break;
6719 case 3:
6720 /* reserved for software-controlled auto mode */
6721 return -ENOSYS;
6722 default:
6723 return -EINVAL;
6726 res = fan_set_level_safe(level);
6727 if (res == -ENXIO)
6728 return -EINVAL;
6729 else if (res < 0)
6730 return res;
6732 fan_watchdog_reset();
6734 return count;
6737 static struct device_attribute dev_attr_fan_pwm1_enable =
6738 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6739 fan_pwm1_enable_show, fan_pwm1_enable_store);
6741 /* sysfs fan pwm1 ------------------------------------------------------ */
6742 static ssize_t fan_pwm1_show(struct device *dev,
6743 struct device_attribute *attr,
6744 char *buf)
6746 int res;
6747 u8 status;
6749 res = fan_get_status_safe(&status);
6750 if (res)
6751 return res;
6753 if ((status &
6754 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6755 status = fan_control_desired_level;
6757 if (status > 7)
6758 status = 7;
6760 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6763 static ssize_t fan_pwm1_store(struct device *dev,
6764 struct device_attribute *attr,
6765 const char *buf, size_t count)
6767 unsigned long s;
6768 int rc;
6769 u8 status, newlevel;
6771 if (parse_strtoul(buf, 255, &s))
6772 return -EINVAL;
6774 tpacpi_disclose_usertask("hwmon pwm1",
6775 "set fan speed to %lu\n", s);
6777 /* scale down from 0-255 to 0-7 */
6778 newlevel = (s >> 5) & 0x07;
6780 if (mutex_lock_killable(&fan_mutex))
6781 return -ERESTARTSYS;
6783 rc = fan_get_status(&status);
6784 if (!rc && (status &
6785 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6786 rc = fan_set_level(newlevel);
6787 if (rc == -ENXIO)
6788 rc = -EINVAL;
6789 else if (!rc) {
6790 fan_update_desired_level(newlevel);
6791 fan_watchdog_reset();
6795 mutex_unlock(&fan_mutex);
6796 return (rc)? rc : count;
6799 static struct device_attribute dev_attr_fan_pwm1 =
6800 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6801 fan_pwm1_show, fan_pwm1_store);
6803 /* sysfs fan fan1_input ------------------------------------------------ */
6804 static ssize_t fan_fan1_input_show(struct device *dev,
6805 struct device_attribute *attr,
6806 char *buf)
6808 int res;
6809 unsigned int speed;
6811 res = fan_get_speed(&speed);
6812 if (res < 0)
6813 return res;
6815 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6818 static struct device_attribute dev_attr_fan_fan1_input =
6819 __ATTR(fan1_input, S_IRUGO,
6820 fan_fan1_input_show, NULL);
6822 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6823 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6824 char *buf)
6826 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6829 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6830 const char *buf, size_t count)
6832 unsigned long t;
6834 if (parse_strtoul(buf, 120, &t))
6835 return -EINVAL;
6837 if (!fan_control_allowed)
6838 return -EPERM;
6840 fan_watchdog_maxinterval = t;
6841 fan_watchdog_reset();
6843 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6845 return count;
6848 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6849 fan_fan_watchdog_show, fan_fan_watchdog_store);
6851 /* --------------------------------------------------------------------- */
6852 static struct attribute *fan_attributes[] = {
6853 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6854 &dev_attr_fan_fan1_input.attr,
6855 NULL
6858 static const struct attribute_group fan_attr_group = {
6859 .attrs = fan_attributes,
6862 #define TPACPI_FAN_Q1 0x0001
6864 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6865 { .vendor = PCI_VENDOR_ID_IBM, \
6866 .bios = TPACPI_MATCH_ANY, \
6867 .ec = TPID(__id1, __id2), \
6868 .quirks = __quirks }
6870 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6871 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6872 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6873 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6874 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
6877 #undef TPACPI_FAN_QI
6879 static int __init fan_init(struct ibm_init_struct *iibm)
6881 int rc;
6882 unsigned long quirks;
6884 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6885 "initializing fan subdriver\n");
6887 mutex_init(&fan_mutex);
6888 fan_status_access_mode = TPACPI_FAN_NONE;
6889 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6890 fan_control_commands = 0;
6891 fan_watchdog_maxinterval = 0;
6892 tp_features.fan_ctrl_status_undef = 0;
6893 fan_control_desired_level = 7;
6895 TPACPI_ACPIHANDLE_INIT(fans);
6896 TPACPI_ACPIHANDLE_INIT(gfan);
6897 TPACPI_ACPIHANDLE_INIT(sfan);
6899 quirks = tpacpi_check_quirks(fan_quirk_table,
6900 ARRAY_SIZE(fan_quirk_table));
6902 if (gfan_handle) {
6903 /* 570, 600e/x, 770e, 770x */
6904 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6905 } else {
6906 /* all other ThinkPads: note that even old-style
6907 * ThinkPad ECs supports the fan control register */
6908 if (likely(acpi_ec_read(fan_status_offset,
6909 &fan_control_initial_status))) {
6910 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6911 if (quirks & TPACPI_FAN_Q1)
6912 fan_quirk1_setup();
6913 } else {
6914 printk(TPACPI_ERR
6915 "ThinkPad ACPI EC access misbehaving, "
6916 "fan status and control unavailable\n");
6917 return 1;
6921 if (sfan_handle) {
6922 /* 570, 770x-JL */
6923 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6924 fan_control_commands |=
6925 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6926 } else {
6927 if (!gfan_handle) {
6928 /* gfan without sfan means no fan control */
6929 /* all other models implement TP EC 0x2f control */
6931 if (fans_handle) {
6932 /* X31, X40, X41 */
6933 fan_control_access_mode =
6934 TPACPI_FAN_WR_ACPI_FANS;
6935 fan_control_commands |=
6936 TPACPI_FAN_CMD_SPEED |
6937 TPACPI_FAN_CMD_LEVEL |
6938 TPACPI_FAN_CMD_ENABLE;
6939 } else {
6940 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6941 fan_control_commands |=
6942 TPACPI_FAN_CMD_LEVEL |
6943 TPACPI_FAN_CMD_ENABLE;
6948 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6949 "fan is %s, modes %d, %d\n",
6950 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6951 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6952 fan_status_access_mode, fan_control_access_mode);
6954 /* fan control master switch */
6955 if (!fan_control_allowed) {
6956 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6957 fan_control_commands = 0;
6958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6959 "fan control features disabled by parameter\n");
6962 /* update fan_control_desired_level */
6963 if (fan_status_access_mode != TPACPI_FAN_NONE)
6964 fan_get_status_safe(NULL);
6966 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6967 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6968 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6969 &fan_attr_group);
6970 if (rc < 0)
6971 return rc;
6973 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6974 &driver_attr_fan_watchdog);
6975 if (rc < 0) {
6976 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6977 &fan_attr_group);
6978 return rc;
6980 return 0;
6981 } else
6982 return 1;
6985 static void fan_exit(void)
6987 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
6988 "cancelling any pending fan watchdog tasks\n");
6990 /* FIXME: can we really do this unconditionally? */
6991 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6992 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6993 &driver_attr_fan_watchdog);
6995 cancel_delayed_work(&fan_watchdog_task);
6996 flush_workqueue(tpacpi_wq);
6999 static void fan_suspend(pm_message_t state)
7001 int rc;
7003 if (!fan_control_allowed)
7004 return;
7006 /* Store fan status in cache */
7007 fan_control_resume_level = 0;
7008 rc = fan_get_status_safe(&fan_control_resume_level);
7009 if (rc < 0)
7010 printk(TPACPI_NOTICE
7011 "failed to read fan level for later "
7012 "restore during resume: %d\n", rc);
7014 /* if it is undefined, don't attempt to restore it.
7015 * KEEP THIS LAST */
7016 if (tp_features.fan_ctrl_status_undef)
7017 fan_control_resume_level = 0;
7020 static void fan_resume(void)
7022 u8 current_level = 7;
7023 bool do_set = false;
7024 int rc;
7026 /* DSDT *always* updates status on resume */
7027 tp_features.fan_ctrl_status_undef = 0;
7029 if (!fan_control_allowed ||
7030 !fan_control_resume_level ||
7031 (fan_get_status_safe(&current_level) < 0))
7032 return;
7034 switch (fan_control_access_mode) {
7035 case TPACPI_FAN_WR_ACPI_SFAN:
7036 /* never decrease fan level */
7037 do_set = (fan_control_resume_level > current_level);
7038 break;
7039 case TPACPI_FAN_WR_ACPI_FANS:
7040 case TPACPI_FAN_WR_TPEC:
7041 /* never decrease fan level, scale is:
7042 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7044 * We expect the firmware to set either 7 or AUTO, but we
7045 * handle FULLSPEED out of paranoia.
7047 * So, we can safely only restore FULLSPEED or 7, anything
7048 * else could slow the fan. Restoring AUTO is useless, at
7049 * best that's exactly what the DSDT already set (it is the
7050 * slower it uses).
7052 * Always keep in mind that the DSDT *will* have set the
7053 * fans to what the vendor supposes is the best level. We
7054 * muck with it only to speed the fan up.
7056 if (fan_control_resume_level != 7 &&
7057 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7058 return;
7059 else
7060 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7061 (current_level != fan_control_resume_level);
7062 break;
7063 default:
7064 return;
7066 if (do_set) {
7067 printk(TPACPI_NOTICE
7068 "restoring fan level to 0x%02x\n",
7069 fan_control_resume_level);
7070 rc = fan_set_level_safe(fan_control_resume_level);
7071 if (rc < 0)
7072 printk(TPACPI_NOTICE
7073 "failed to restore fan level: %d\n", rc);
7077 static int fan_read(char *p)
7079 int len = 0;
7080 int rc;
7081 u8 status;
7082 unsigned int speed = 0;
7084 switch (fan_status_access_mode) {
7085 case TPACPI_FAN_RD_ACPI_GFAN:
7086 /* 570, 600e/x, 770e, 770x */
7087 rc = fan_get_status_safe(&status);
7088 if (rc < 0)
7089 return rc;
7091 len += sprintf(p + len, "status:\t\t%s\n"
7092 "level:\t\t%d\n",
7093 (status != 0) ? "enabled" : "disabled", status);
7094 break;
7096 case TPACPI_FAN_RD_TPEC:
7097 /* all except 570, 600e/x, 770e, 770x */
7098 rc = fan_get_status_safe(&status);
7099 if (rc < 0)
7100 return rc;
7102 len += sprintf(p + len, "status:\t\t%s\n",
7103 (status != 0) ? "enabled" : "disabled");
7105 rc = fan_get_speed(&speed);
7106 if (rc < 0)
7107 return rc;
7109 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7111 if (status & TP_EC_FAN_FULLSPEED)
7112 /* Disengaged mode takes precedence */
7113 len += sprintf(p + len, "level:\t\tdisengaged\n");
7114 else if (status & TP_EC_FAN_AUTO)
7115 len += sprintf(p + len, "level:\t\tauto\n");
7116 else
7117 len += sprintf(p + len, "level:\t\t%d\n", status);
7118 break;
7120 case TPACPI_FAN_NONE:
7121 default:
7122 len += sprintf(p + len, "status:\t\tnot supported\n");
7125 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7126 len += sprintf(p + len, "commands:\tlevel <level>");
7128 switch (fan_control_access_mode) {
7129 case TPACPI_FAN_WR_ACPI_SFAN:
7130 len += sprintf(p + len, " (<level> is 0-7)\n");
7131 break;
7133 default:
7134 len += sprintf(p + len, " (<level> is 0-7, "
7135 "auto, disengaged, full-speed)\n");
7136 break;
7140 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7141 len += sprintf(p + len, "commands:\tenable, disable\n"
7142 "commands:\twatchdog <timeout> (<timeout> "
7143 "is 0 (off), 1-120 (seconds))\n");
7145 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7146 len += sprintf(p + len, "commands:\tspeed <speed>"
7147 " (<speed> is 0-65535)\n");
7149 return len;
7152 static int fan_write_cmd_level(const char *cmd, int *rc)
7154 int level;
7156 if (strlencmp(cmd, "level auto") == 0)
7157 level = TP_EC_FAN_AUTO;
7158 else if ((strlencmp(cmd, "level disengaged") == 0) |
7159 (strlencmp(cmd, "level full-speed") == 0))
7160 level = TP_EC_FAN_FULLSPEED;
7161 else if (sscanf(cmd, "level %d", &level) != 1)
7162 return 0;
7164 *rc = fan_set_level_safe(level);
7165 if (*rc == -ENXIO)
7166 printk(TPACPI_ERR "level command accepted for unsupported "
7167 "access mode %d", fan_control_access_mode);
7168 else if (!*rc)
7169 tpacpi_disclose_usertask("procfs fan",
7170 "set level to %d\n", level);
7172 return 1;
7175 static int fan_write_cmd_enable(const char *cmd, int *rc)
7177 if (strlencmp(cmd, "enable") != 0)
7178 return 0;
7180 *rc = fan_set_enable();
7181 if (*rc == -ENXIO)
7182 printk(TPACPI_ERR "enable command accepted for unsupported "
7183 "access mode %d", fan_control_access_mode);
7184 else if (!*rc)
7185 tpacpi_disclose_usertask("procfs fan", "enable\n");
7187 return 1;
7190 static int fan_write_cmd_disable(const char *cmd, int *rc)
7192 if (strlencmp(cmd, "disable") != 0)
7193 return 0;
7195 *rc = fan_set_disable();
7196 if (*rc == -ENXIO)
7197 printk(TPACPI_ERR "disable command accepted for unsupported "
7198 "access mode %d", fan_control_access_mode);
7199 else if (!*rc)
7200 tpacpi_disclose_usertask("procfs fan", "disable\n");
7202 return 1;
7205 static int fan_write_cmd_speed(const char *cmd, int *rc)
7207 int speed;
7209 /* TODO:
7210 * Support speed <low> <medium> <high> ? */
7212 if (sscanf(cmd, "speed %d", &speed) != 1)
7213 return 0;
7215 *rc = fan_set_speed(speed);
7216 if (*rc == -ENXIO)
7217 printk(TPACPI_ERR "speed command accepted for unsupported "
7218 "access mode %d", fan_control_access_mode);
7219 else if (!*rc)
7220 tpacpi_disclose_usertask("procfs fan",
7221 "set speed to %d\n", speed);
7223 return 1;
7226 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7228 int interval;
7230 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7231 return 0;
7233 if (interval < 0 || interval > 120)
7234 *rc = -EINVAL;
7235 else {
7236 fan_watchdog_maxinterval = interval;
7237 tpacpi_disclose_usertask("procfs fan",
7238 "set watchdog timer to %d\n",
7239 interval);
7242 return 1;
7245 static int fan_write(char *buf)
7247 char *cmd;
7248 int rc = 0;
7250 while (!rc && (cmd = next_cmd(&buf))) {
7251 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7252 fan_write_cmd_level(cmd, &rc)) &&
7253 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7254 (fan_write_cmd_enable(cmd, &rc) ||
7255 fan_write_cmd_disable(cmd, &rc) ||
7256 fan_write_cmd_watchdog(cmd, &rc))) &&
7257 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7258 fan_write_cmd_speed(cmd, &rc))
7260 rc = -EINVAL;
7261 else if (!rc)
7262 fan_watchdog_reset();
7265 return rc;
7268 static struct ibm_struct fan_driver_data = {
7269 .name = "fan",
7270 .read = fan_read,
7271 .write = fan_write,
7272 .exit = fan_exit,
7273 .suspend = fan_suspend,
7274 .resume = fan_resume,
7277 /****************************************************************************
7278 ****************************************************************************
7280 * Infrastructure
7282 ****************************************************************************
7283 ****************************************************************************/
7285 /* sysfs name ---------------------------------------------------------- */
7286 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7287 struct device_attribute *attr,
7288 char *buf)
7290 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7293 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7294 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7296 /* --------------------------------------------------------------------- */
7298 /* /proc support */
7299 static struct proc_dir_entry *proc_dir;
7302 * Module and infrastructure proble, init and exit handling
7305 static int force_load;
7307 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7308 static const char * __init str_supported(int is_supported)
7310 static char text_unsupported[] __initdata = "not supported";
7312 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7314 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7316 static void ibm_exit(struct ibm_struct *ibm)
7318 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7320 list_del_init(&ibm->all_drivers);
7322 if (ibm->flags.acpi_notify_installed) {
7323 dbg_printk(TPACPI_DBG_EXIT,
7324 "%s: acpi_remove_notify_handler\n", ibm->name);
7325 BUG_ON(!ibm->acpi);
7326 acpi_remove_notify_handler(*ibm->acpi->handle,
7327 ibm->acpi->type,
7328 dispatch_acpi_notify);
7329 ibm->flags.acpi_notify_installed = 0;
7330 ibm->flags.acpi_notify_installed = 0;
7333 if (ibm->flags.proc_created) {
7334 dbg_printk(TPACPI_DBG_EXIT,
7335 "%s: remove_proc_entry\n", ibm->name);
7336 remove_proc_entry(ibm->name, proc_dir);
7337 ibm->flags.proc_created = 0;
7340 if (ibm->flags.acpi_driver_registered) {
7341 dbg_printk(TPACPI_DBG_EXIT,
7342 "%s: acpi_bus_unregister_driver\n", ibm->name);
7343 BUG_ON(!ibm->acpi);
7344 acpi_bus_unregister_driver(ibm->acpi->driver);
7345 kfree(ibm->acpi->driver);
7346 ibm->acpi->driver = NULL;
7347 ibm->flags.acpi_driver_registered = 0;
7350 if (ibm->flags.init_called && ibm->exit) {
7351 ibm->exit();
7352 ibm->flags.init_called = 0;
7355 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7358 static int __init ibm_init(struct ibm_init_struct *iibm)
7360 int ret;
7361 struct ibm_struct *ibm = iibm->data;
7362 struct proc_dir_entry *entry;
7364 BUG_ON(ibm == NULL);
7366 INIT_LIST_HEAD(&ibm->all_drivers);
7368 if (ibm->flags.experimental && !experimental)
7369 return 0;
7371 dbg_printk(TPACPI_DBG_INIT,
7372 "probing for %s\n", ibm->name);
7374 if (iibm->init) {
7375 ret = iibm->init(iibm);
7376 if (ret > 0)
7377 return 0; /* probe failed */
7378 if (ret)
7379 return ret;
7381 ibm->flags.init_called = 1;
7384 if (ibm->acpi) {
7385 if (ibm->acpi->hid) {
7386 ret = register_tpacpi_subdriver(ibm);
7387 if (ret)
7388 goto err_out;
7391 if (ibm->acpi->notify) {
7392 ret = setup_acpi_notify(ibm);
7393 if (ret == -ENODEV) {
7394 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7395 ibm->name);
7396 ret = 0;
7397 goto err_out;
7399 if (ret < 0)
7400 goto err_out;
7404 dbg_printk(TPACPI_DBG_INIT,
7405 "%s installed\n", ibm->name);
7407 if (ibm->read) {
7408 entry = create_proc_entry(ibm->name,
7409 S_IFREG | S_IRUGO | S_IWUSR,
7410 proc_dir);
7411 if (!entry) {
7412 printk(TPACPI_ERR "unable to create proc entry %s\n",
7413 ibm->name);
7414 ret = -ENODEV;
7415 goto err_out;
7417 entry->data = ibm;
7418 entry->read_proc = &dispatch_procfs_read;
7419 if (ibm->write)
7420 entry->write_proc = &dispatch_procfs_write;
7421 ibm->flags.proc_created = 1;
7424 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7426 return 0;
7428 err_out:
7429 dbg_printk(TPACPI_DBG_INIT,
7430 "%s: at error exit path with result %d\n",
7431 ibm->name, ret);
7433 ibm_exit(ibm);
7434 return (ret < 0)? ret : 0;
7437 /* Probing */
7439 static bool __pure __init tpacpi_is_fw_digit(const char c)
7441 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7444 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7445 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7446 const char t)
7448 return s && strlen(s) >= 8 &&
7449 tpacpi_is_fw_digit(s[0]) &&
7450 tpacpi_is_fw_digit(s[1]) &&
7451 s[2] == t && s[3] == 'T' &&
7452 tpacpi_is_fw_digit(s[4]) &&
7453 tpacpi_is_fw_digit(s[5]) &&
7454 s[6] == 'W' && s[7] == 'W';
7457 /* returns 0 - probe ok, or < 0 - probe error.
7458 * Probe ok doesn't mean thinkpad found.
7459 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7460 static int __must_check __init get_thinkpad_model_data(
7461 struct thinkpad_id_data *tp)
7463 const struct dmi_device *dev = NULL;
7464 char ec_fw_string[18];
7465 char const *s;
7467 if (!tp)
7468 return -EINVAL;
7470 memset(tp, 0, sizeof(*tp));
7472 if (dmi_name_in_vendors("IBM"))
7473 tp->vendor = PCI_VENDOR_ID_IBM;
7474 else if (dmi_name_in_vendors("LENOVO"))
7475 tp->vendor = PCI_VENDOR_ID_LENOVO;
7476 else
7477 return 0;
7479 s = dmi_get_system_info(DMI_BIOS_VERSION);
7480 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7481 if (s && !tp->bios_version_str)
7482 return -ENOMEM;
7484 /* Really ancient ThinkPad 240X will fail this, which is fine */
7485 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7486 return 0;
7488 tp->bios_model = tp->bios_version_str[0]
7489 | (tp->bios_version_str[1] << 8);
7490 tp->bios_release = (tp->bios_version_str[4] << 8)
7491 | tp->bios_version_str[5];
7494 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7495 * X32 or newer, all Z series; Some models must have an
7496 * up-to-date BIOS or they will not be detected.
7498 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7500 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7501 if (sscanf(dev->name,
7502 "IBM ThinkPad Embedded Controller -[%17c",
7503 ec_fw_string) == 1) {
7504 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7505 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7507 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7508 if (!tp->ec_version_str)
7509 return -ENOMEM;
7511 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7512 tp->ec_model = ec_fw_string[0]
7513 | (ec_fw_string[1] << 8);
7514 tp->ec_release = (ec_fw_string[4] << 8)
7515 | ec_fw_string[5];
7516 } else {
7517 printk(TPACPI_NOTICE
7518 "ThinkPad firmware release %s "
7519 "doesn't match the known patterns\n",
7520 ec_fw_string);
7521 printk(TPACPI_NOTICE
7522 "please report this to %s\n",
7523 TPACPI_MAIL);
7525 break;
7529 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7530 if (s && !strnicmp(s, "ThinkPad", 8)) {
7531 tp->model_str = kstrdup(s, GFP_KERNEL);
7532 if (!tp->model_str)
7533 return -ENOMEM;
7536 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7537 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7538 if (s && !tp->nummodel_str)
7539 return -ENOMEM;
7541 return 0;
7544 static int __init probe_for_thinkpad(void)
7546 int is_thinkpad;
7548 if (acpi_disabled)
7549 return -ENODEV;
7552 * Non-ancient models have better DMI tagging, but very old models
7553 * don't.
7555 is_thinkpad = (thinkpad_id.model_str != NULL);
7557 /* ec is required because many other handles are relative to it */
7558 TPACPI_ACPIHANDLE_INIT(ec);
7559 if (!ec_handle) {
7560 if (is_thinkpad)
7561 printk(TPACPI_ERR
7562 "Not yet supported ThinkPad detected!\n");
7563 return -ENODEV;
7567 * Risks a regression on very old machines, but reduces potential
7568 * false positives a damn great deal
7570 if (!is_thinkpad)
7571 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7573 if (!is_thinkpad && !force_load)
7574 return -ENODEV;
7576 return 0;
7580 /* Module init, exit, parameters */
7582 static struct ibm_init_struct ibms_init[] __initdata = {
7584 .init = thinkpad_acpi_driver_init,
7585 .data = &thinkpad_acpi_driver_data,
7588 .init = hotkey_init,
7589 .data = &hotkey_driver_data,
7592 .init = bluetooth_init,
7593 .data = &bluetooth_driver_data,
7596 .init = wan_init,
7597 .data = &wan_driver_data,
7600 .init = uwb_init,
7601 .data = &uwb_driver_data,
7603 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7605 .init = video_init,
7606 .data = &video_driver_data,
7608 #endif
7610 .init = light_init,
7611 .data = &light_driver_data,
7613 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7615 .init = dock_init,
7616 .data = &dock_driver_data[0],
7619 .init = dock_init2,
7620 .data = &dock_driver_data[1],
7622 #endif
7623 #ifdef CONFIG_THINKPAD_ACPI_BAY
7625 .init = bay_init,
7626 .data = &bay_driver_data,
7628 #endif
7630 .init = cmos_init,
7631 .data = &cmos_driver_data,
7634 .init = led_init,
7635 .data = &led_driver_data,
7638 .init = beep_init,
7639 .data = &beep_driver_data,
7642 .init = thermal_init,
7643 .data = &thermal_driver_data,
7646 .data = &ecdump_driver_data,
7649 .init = brightness_init,
7650 .data = &brightness_driver_data,
7653 .data = &volume_driver_data,
7656 .init = fan_init,
7657 .data = &fan_driver_data,
7661 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7663 unsigned int i;
7664 struct ibm_struct *ibm;
7666 if (!kp || !kp->name || !val)
7667 return -EINVAL;
7669 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7670 ibm = ibms_init[i].data;
7671 WARN_ON(ibm == NULL);
7673 if (!ibm || !ibm->name)
7674 continue;
7676 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7677 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7678 return -ENOSPC;
7679 strcpy(ibms_init[i].param, val);
7680 strcat(ibms_init[i].param, ",");
7681 return 0;
7685 return -EINVAL;
7688 module_param(experimental, int, 0);
7689 MODULE_PARM_DESC(experimental,
7690 "Enables experimental features when non-zero");
7692 module_param_named(debug, dbg_level, uint, 0);
7693 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7695 module_param(force_load, bool, 0);
7696 MODULE_PARM_DESC(force_load,
7697 "Attempts to load the driver even on a "
7698 "mis-identified ThinkPad when true");
7700 module_param_named(fan_control, fan_control_allowed, bool, 0);
7701 MODULE_PARM_DESC(fan_control,
7702 "Enables setting fan parameters features when true");
7704 module_param_named(brightness_mode, brightness_mode, uint, 0);
7705 MODULE_PARM_DESC(brightness_mode,
7706 "Selects brightness control strategy: "
7707 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7709 module_param(brightness_enable, uint, 0);
7710 MODULE_PARM_DESC(brightness_enable,
7711 "Enables backlight control when 1, disables when 0");
7713 module_param(hotkey_report_mode, uint, 0);
7714 MODULE_PARM_DESC(hotkey_report_mode,
7715 "used for backwards compatibility with userspace, "
7716 "see documentation");
7718 #define TPACPI_PARAM(feature) \
7719 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7720 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7721 "at module load, see documentation")
7723 TPACPI_PARAM(hotkey);
7724 TPACPI_PARAM(bluetooth);
7725 TPACPI_PARAM(video);
7726 TPACPI_PARAM(light);
7727 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7728 TPACPI_PARAM(dock);
7729 #endif
7730 #ifdef CONFIG_THINKPAD_ACPI_BAY
7731 TPACPI_PARAM(bay);
7732 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7733 TPACPI_PARAM(cmos);
7734 TPACPI_PARAM(led);
7735 TPACPI_PARAM(beep);
7736 TPACPI_PARAM(ecdump);
7737 TPACPI_PARAM(brightness);
7738 TPACPI_PARAM(volume);
7739 TPACPI_PARAM(fan);
7741 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7742 module_param(dbg_wlswemul, uint, 0);
7743 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7744 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7745 MODULE_PARM_DESC(wlsw_state,
7746 "Initial state of the emulated WLSW switch");
7748 module_param(dbg_bluetoothemul, uint, 0);
7749 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7750 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7751 MODULE_PARM_DESC(bluetooth_state,
7752 "Initial state of the emulated bluetooth switch");
7754 module_param(dbg_wwanemul, uint, 0);
7755 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7756 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7757 MODULE_PARM_DESC(wwan_state,
7758 "Initial state of the emulated WWAN switch");
7760 module_param(dbg_uwbemul, uint, 0);
7761 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7762 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7763 MODULE_PARM_DESC(uwb_state,
7764 "Initial state of the emulated UWB switch");
7765 #endif
7767 static void thinkpad_acpi_module_exit(void)
7769 struct ibm_struct *ibm, *itmp;
7771 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7773 list_for_each_entry_safe_reverse(ibm, itmp,
7774 &tpacpi_all_drivers,
7775 all_drivers) {
7776 ibm_exit(ibm);
7779 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7781 if (tpacpi_inputdev) {
7782 if (tp_features.input_device_registered)
7783 input_unregister_device(tpacpi_inputdev);
7784 else
7785 input_free_device(tpacpi_inputdev);
7788 if (tpacpi_hwmon)
7789 hwmon_device_unregister(tpacpi_hwmon);
7791 if (tp_features.sensors_pdev_attrs_registered)
7792 device_remove_file(&tpacpi_sensors_pdev->dev,
7793 &dev_attr_thinkpad_acpi_pdev_name);
7794 if (tpacpi_sensors_pdev)
7795 platform_device_unregister(tpacpi_sensors_pdev);
7796 if (tpacpi_pdev)
7797 platform_device_unregister(tpacpi_pdev);
7799 if (tp_features.sensors_pdrv_attrs_registered)
7800 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7801 if (tp_features.platform_drv_attrs_registered)
7802 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7804 if (tp_features.sensors_pdrv_registered)
7805 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7807 if (tp_features.platform_drv_registered)
7808 platform_driver_unregister(&tpacpi_pdriver);
7810 if (proc_dir)
7811 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7813 if (tpacpi_wq)
7814 destroy_workqueue(tpacpi_wq);
7816 kfree(thinkpad_id.bios_version_str);
7817 kfree(thinkpad_id.ec_version_str);
7818 kfree(thinkpad_id.model_str);
7822 static int __init thinkpad_acpi_module_init(void)
7824 int ret, i;
7826 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7828 /* Parameter checking */
7829 if (hotkey_report_mode > 2)
7830 return -EINVAL;
7832 /* Driver-level probe */
7834 ret = get_thinkpad_model_data(&thinkpad_id);
7835 if (ret) {
7836 printk(TPACPI_ERR
7837 "unable to get DMI data: %d\n", ret);
7838 thinkpad_acpi_module_exit();
7839 return ret;
7841 ret = probe_for_thinkpad();
7842 if (ret) {
7843 thinkpad_acpi_module_exit();
7844 return ret;
7847 /* Driver initialization */
7849 TPACPI_ACPIHANDLE_INIT(ecrd);
7850 TPACPI_ACPIHANDLE_INIT(ecwr);
7852 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7853 if (!tpacpi_wq) {
7854 thinkpad_acpi_module_exit();
7855 return -ENOMEM;
7858 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7859 if (!proc_dir) {
7860 printk(TPACPI_ERR
7861 "unable to create proc dir " TPACPI_PROC_DIR);
7862 thinkpad_acpi_module_exit();
7863 return -ENODEV;
7866 ret = platform_driver_register(&tpacpi_pdriver);
7867 if (ret) {
7868 printk(TPACPI_ERR
7869 "unable to register main platform driver\n");
7870 thinkpad_acpi_module_exit();
7871 return ret;
7873 tp_features.platform_drv_registered = 1;
7875 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7876 if (ret) {
7877 printk(TPACPI_ERR
7878 "unable to register hwmon platform driver\n");
7879 thinkpad_acpi_module_exit();
7880 return ret;
7882 tp_features.sensors_pdrv_registered = 1;
7884 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7885 if (!ret) {
7886 tp_features.platform_drv_attrs_registered = 1;
7887 ret = tpacpi_create_driver_attributes(
7888 &tpacpi_hwmon_pdriver.driver);
7890 if (ret) {
7891 printk(TPACPI_ERR
7892 "unable to create sysfs driver attributes\n");
7893 thinkpad_acpi_module_exit();
7894 return ret;
7896 tp_features.sensors_pdrv_attrs_registered = 1;
7899 /* Device initialization */
7900 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7901 NULL, 0);
7902 if (IS_ERR(tpacpi_pdev)) {
7903 ret = PTR_ERR(tpacpi_pdev);
7904 tpacpi_pdev = NULL;
7905 printk(TPACPI_ERR "unable to register platform device\n");
7906 thinkpad_acpi_module_exit();
7907 return ret;
7909 tpacpi_sensors_pdev = platform_device_register_simple(
7910 TPACPI_HWMON_DRVR_NAME,
7911 -1, NULL, 0);
7912 if (IS_ERR(tpacpi_sensors_pdev)) {
7913 ret = PTR_ERR(tpacpi_sensors_pdev);
7914 tpacpi_sensors_pdev = NULL;
7915 printk(TPACPI_ERR
7916 "unable to register hwmon platform device\n");
7917 thinkpad_acpi_module_exit();
7918 return ret;
7920 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7921 &dev_attr_thinkpad_acpi_pdev_name);
7922 if (ret) {
7923 printk(TPACPI_ERR
7924 "unable to create sysfs hwmon device attributes\n");
7925 thinkpad_acpi_module_exit();
7926 return ret;
7928 tp_features.sensors_pdev_attrs_registered = 1;
7929 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7930 if (IS_ERR(tpacpi_hwmon)) {
7931 ret = PTR_ERR(tpacpi_hwmon);
7932 tpacpi_hwmon = NULL;
7933 printk(TPACPI_ERR "unable to register hwmon device\n");
7934 thinkpad_acpi_module_exit();
7935 return ret;
7937 mutex_init(&tpacpi_inputdev_send_mutex);
7938 tpacpi_inputdev = input_allocate_device();
7939 if (!tpacpi_inputdev) {
7940 printk(TPACPI_ERR "unable to allocate input device\n");
7941 thinkpad_acpi_module_exit();
7942 return -ENOMEM;
7943 } else {
7944 /* Prepare input device, but don't register */
7945 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7946 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7947 tpacpi_inputdev->id.bustype = BUS_HOST;
7948 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7949 thinkpad_id.vendor :
7950 PCI_VENDOR_ID_IBM;
7951 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7952 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7954 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7955 ret = ibm_init(&ibms_init[i]);
7956 if (ret >= 0 && *ibms_init[i].param)
7957 ret = ibms_init[i].data->write(ibms_init[i].param);
7958 if (ret < 0) {
7959 thinkpad_acpi_module_exit();
7960 return ret;
7963 ret = input_register_device(tpacpi_inputdev);
7964 if (ret < 0) {
7965 printk(TPACPI_ERR "unable to register input device\n");
7966 thinkpad_acpi_module_exit();
7967 return ret;
7968 } else {
7969 tp_features.input_device_registered = 1;
7972 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7973 return 0;
7976 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7979 * This will autoload the driver in almost every ThinkPad
7980 * in widespread use.
7982 * Only _VERY_ old models, like the 240, 240x and 570 lack
7983 * the HKEY event interface.
7985 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7988 * DMI matching for module autoloading
7990 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7991 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7993 * Only models listed in thinkwiki will be supported, so add yours
7994 * if it is not there yet.
7996 #define IBM_BIOS_MODULE_ALIAS(__type) \
7997 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7999 /* Ancient thinkpad BIOSes have to be identified by
8000 * BIOS type or model number, and there are far less
8001 * BIOS types than model numbers... */
8002 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8004 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8005 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8006 MODULE_DESCRIPTION(TPACPI_DESC);
8007 MODULE_VERSION(TPACPI_VERSION);
8008 MODULE_LICENSE("GPL");
8010 module_init(thinkpad_acpi_module_init);
8011 module_exit(thinkpad_acpi_module_exit);