thinkpad-acpi: support the second fan on the X61
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob27ca676a7092e1fe98288e8757489b95131e8951
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020400
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
195 #define TPACPI_DBG_BRGHT 0x0020
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
226 void (*shutdown) (void);
228 struct list_head all_drivers;
230 struct tp_acpi_drv_struct *acpi;
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
241 struct ibm_init_struct {
242 char param[32];
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
248 static struct {
249 #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 second_fan:1;
268 u32 beep_needs_two_args:1;
269 u32 input_device_registered:1;
270 u32 platform_drv_registered:1;
271 u32 platform_drv_attrs_registered:1;
272 u32 sensors_pdrv_registered:1;
273 u32 sensors_pdrv_attrs_registered:1;
274 u32 sensors_pdev_attrs_registered:1;
275 u32 hotkey_poll_active:1;
276 } tp_features;
278 static struct {
279 u16 hotkey_mask_ff:1;
280 } tp_warned;
282 struct thinkpad_id_data {
283 unsigned int vendor; /* ThinkPad vendor:
284 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
286 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
287 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
289 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
290 u16 ec_model;
291 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
292 u16 ec_release;
294 char *model_str; /* ThinkPad T43 */
295 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
297 static struct thinkpad_id_data thinkpad_id;
299 static enum {
300 TPACPI_LIFE_INIT = 0,
301 TPACPI_LIFE_RUNNING,
302 TPACPI_LIFE_EXITING,
303 } tpacpi_lifecycle;
305 static int experimental;
306 static u32 dbg_level;
308 static struct workqueue_struct *tpacpi_wq;
310 enum led_status_t {
311 TPACPI_LED_OFF = 0,
312 TPACPI_LED_ON,
313 TPACPI_LED_BLINK,
316 /* Special LED class that can defer work */
317 struct tpacpi_led_classdev {
318 struct led_classdev led_classdev;
319 struct work_struct work;
320 enum led_status_t new_state;
321 unsigned int led;
324 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
325 static int dbg_wlswemul;
326 static int tpacpi_wlsw_emulstate;
327 static int dbg_bluetoothemul;
328 static int tpacpi_bluetooth_emulstate;
329 static int dbg_wwanemul;
330 static int tpacpi_wwan_emulstate;
331 static int dbg_uwbemul;
332 static int tpacpi_uwb_emulstate;
333 #endif
336 /*************************************************************************
337 * Debugging helpers
340 #define dbg_printk(a_dbg_level, format, arg...) \
341 do { if (dbg_level & (a_dbg_level)) \
342 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
343 } while (0)
345 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
346 #define vdbg_printk dbg_printk
347 static const char *str_supported(int is_supported);
348 #else
349 #define vdbg_printk(a_dbg_level, format, arg...) \
350 do { } while (0)
351 #endif
353 static void tpacpi_log_usertask(const char * const what)
355 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
356 what, task_tgid_vnr(current));
359 #define tpacpi_disclose_usertask(what, format, arg...) \
360 do { \
361 if (unlikely( \
362 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
363 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
364 printk(TPACPI_DEBUG "%s: PID %d: " format, \
365 what, task_tgid_vnr(current), ## arg); \
367 } while (0)
370 * Quirk handling helpers
372 * ThinkPad IDs and versions seen in the field so far
373 * are two-characters from the set [0-9A-Z], i.e. base 36.
375 * We use values well outside that range as specials.
378 #define TPACPI_MATCH_ANY 0xffffU
379 #define TPACPI_MATCH_UNKNOWN 0U
381 /* TPID('1', 'Y') == 0x5931 */
382 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
384 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_IBM, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
390 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
391 { .vendor = PCI_VENDOR_ID_LENOVO, \
392 .bios = TPID(__id1, __id2), \
393 .ec = TPACPI_MATCH_ANY, \
394 .quirks = (__quirk) }
396 struct tpacpi_quirk {
397 unsigned int vendor;
398 u16 bios;
399 u16 ec;
400 unsigned long quirks;
404 * tpacpi_check_quirks() - search BIOS/EC version on a list
405 * @qlist: array of &struct tpacpi_quirk
406 * @qlist_size: number of elements in @qlist
408 * Iterates over a quirks list until one is found that matches the
409 * ThinkPad's vendor, BIOS and EC model.
411 * Returns 0 if nothing matches, otherwise returns the quirks field of
412 * the matching &struct tpacpi_quirk entry.
414 * The match criteria is: vendor, ec and bios much match.
416 static unsigned long __init tpacpi_check_quirks(
417 const struct tpacpi_quirk *qlist,
418 unsigned int qlist_size)
420 while (qlist_size) {
421 if ((qlist->vendor == thinkpad_id.vendor ||
422 qlist->vendor == TPACPI_MATCH_ANY) &&
423 (qlist->bios == thinkpad_id.bios_model ||
424 qlist->bios == TPACPI_MATCH_ANY) &&
425 (qlist->ec == thinkpad_id.ec_model ||
426 qlist->ec == TPACPI_MATCH_ANY))
427 return qlist->quirks;
429 qlist_size--;
430 qlist++;
432 return 0;
436 /****************************************************************************
437 ****************************************************************************
439 * ACPI Helpers and device model
441 ****************************************************************************
442 ****************************************************************************/
444 /*************************************************************************
445 * ACPI basic handles
448 static acpi_handle root_handle;
450 #define TPACPI_HANDLE(object, parent, paths...) \
451 static acpi_handle object##_handle; \
452 static acpi_handle *object##_parent = &parent##_handle; \
453 static char *object##_path; \
454 static char *object##_paths[] = { paths }
456 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
457 "\\_SB.PCI.ISA.EC", /* 570 */
458 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
459 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
460 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
461 "\\_SB.PCI0.ICH3.EC0", /* R31 */
462 "\\_SB.PCI0.LPC.EC", /* all others */
465 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
466 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
468 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
469 /* T4x, X31, X40 */
470 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
471 "\\CMS", /* R40, R40e */
472 ); /* all others */
474 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
475 "^HKEY", /* R30, R31 */
476 "HKEY", /* all others */
477 ); /* 570 */
479 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
480 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
481 "\\_SB.PCI0.VID0", /* 770e */
482 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
483 "\\_SB.PCI0.AGP.VID", /* all others */
484 ); /* R30, R31 */
487 /*************************************************************************
488 * ACPI helpers
491 static int acpi_evalf(acpi_handle handle,
492 void *res, char *method, char *fmt, ...)
494 char *fmt0 = fmt;
495 struct acpi_object_list params;
496 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
497 struct acpi_buffer result, *resultp;
498 union acpi_object out_obj;
499 acpi_status status;
500 va_list ap;
501 char res_type;
502 int success;
503 int quiet;
505 if (!*fmt) {
506 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
507 return 0;
510 if (*fmt == 'q') {
511 quiet = 1;
512 fmt++;
513 } else
514 quiet = 0;
516 res_type = *(fmt++);
518 params.count = 0;
519 params.pointer = &in_objs[0];
521 va_start(ap, fmt);
522 while (*fmt) {
523 char c = *(fmt++);
524 switch (c) {
525 case 'd': /* int */
526 in_objs[params.count].integer.value = va_arg(ap, int);
527 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
528 break;
529 /* add more types as needed */
530 default:
531 printk(TPACPI_ERR "acpi_evalf() called "
532 "with invalid format character '%c'\n", c);
533 return 0;
536 va_end(ap);
538 if (res_type != 'v') {
539 result.length = sizeof(out_obj);
540 result.pointer = &out_obj;
541 resultp = &result;
542 } else
543 resultp = NULL;
545 status = acpi_evaluate_object(handle, method, &params, resultp);
547 switch (res_type) {
548 case 'd': /* int */
549 if (res)
550 *(int *)res = out_obj.integer.value;
551 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
552 break;
553 case 'v': /* void */
554 success = status == AE_OK;
555 break;
556 /* add more types as needed */
557 default:
558 printk(TPACPI_ERR "acpi_evalf() called "
559 "with invalid format character '%c'\n", res_type);
560 return 0;
563 if (!success && !quiet)
564 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
565 method, fmt0, status);
567 return success;
570 static int acpi_ec_read(int i, u8 *p)
572 int v;
574 if (ecrd_handle) {
575 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
576 return 0;
577 *p = v;
578 } else {
579 if (ec_read(i, p) < 0)
580 return 0;
583 return 1;
586 static int acpi_ec_write(int i, u8 v)
588 if (ecwr_handle) {
589 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
590 return 0;
591 } else {
592 if (ec_write(i, v) < 0)
593 return 0;
596 return 1;
599 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
600 static int _sta(acpi_handle handle)
602 int status;
604 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
605 status = 0;
607 return status;
609 #endif
611 static int issue_thinkpad_cmos_command(int cmos_cmd)
613 if (!cmos_handle)
614 return -ENXIO;
616 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
617 return -EIO;
619 return 0;
622 /*************************************************************************
623 * ACPI device model
626 #define TPACPI_ACPIHANDLE_INIT(object) \
627 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
628 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
630 static void drv_acpi_handle_init(char *name,
631 acpi_handle *handle, acpi_handle parent,
632 char **paths, int num_paths, char **path)
634 int i;
635 acpi_status status;
637 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
638 name);
640 for (i = 0; i < num_paths; i++) {
641 status = acpi_get_handle(parent, paths[i], handle);
642 if (ACPI_SUCCESS(status)) {
643 *path = paths[i];
644 dbg_printk(TPACPI_DBG_INIT,
645 "Found ACPI handle %s for %s\n",
646 *path, name);
647 return;
651 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
652 name);
653 *handle = NULL;
656 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
658 struct ibm_struct *ibm = data;
660 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
661 return;
663 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
664 return;
666 ibm->acpi->notify(ibm, event);
669 static int __init setup_acpi_notify(struct ibm_struct *ibm)
671 acpi_status status;
672 int rc;
674 BUG_ON(!ibm->acpi);
676 if (!*ibm->acpi->handle)
677 return 0;
679 vdbg_printk(TPACPI_DBG_INIT,
680 "setting up ACPI notify for %s\n", ibm->name);
682 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
683 if (rc < 0) {
684 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
685 ibm->name, rc);
686 return -ENODEV;
689 ibm->acpi->device->driver_data = ibm;
690 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
691 TPACPI_ACPI_EVENT_PREFIX,
692 ibm->name);
694 status = acpi_install_notify_handler(*ibm->acpi->handle,
695 ibm->acpi->type, dispatch_acpi_notify, ibm);
696 if (ACPI_FAILURE(status)) {
697 if (status == AE_ALREADY_EXISTS) {
698 printk(TPACPI_NOTICE
699 "another device driver is already "
700 "handling %s events\n", ibm->name);
701 } else {
702 printk(TPACPI_ERR
703 "acpi_install_notify_handler(%s) failed: %d\n",
704 ibm->name, status);
706 return -ENODEV;
708 ibm->flags.acpi_notify_installed = 1;
709 return 0;
712 static int __init tpacpi_device_add(struct acpi_device *device)
714 return 0;
717 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
719 int rc;
721 dbg_printk(TPACPI_DBG_INIT,
722 "registering %s as an ACPI driver\n", ibm->name);
724 BUG_ON(!ibm->acpi);
726 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
727 if (!ibm->acpi->driver) {
728 printk(TPACPI_ERR
729 "failed to allocate memory for ibm->acpi->driver\n");
730 return -ENOMEM;
733 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
734 ibm->acpi->driver->ids = ibm->acpi->hid;
736 ibm->acpi->driver->ops.add = &tpacpi_device_add;
738 rc = acpi_bus_register_driver(ibm->acpi->driver);
739 if (rc < 0) {
740 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
741 ibm->name, rc);
742 kfree(ibm->acpi->driver);
743 ibm->acpi->driver = NULL;
744 } else if (!rc)
745 ibm->flags.acpi_driver_registered = 1;
747 return rc;
751 /****************************************************************************
752 ****************************************************************************
754 * Procfs Helpers
756 ****************************************************************************
757 ****************************************************************************/
759 static int dispatch_procfs_read(char *page, char **start, off_t off,
760 int count, int *eof, void *data)
762 struct ibm_struct *ibm = data;
763 int len;
765 if (!ibm || !ibm->read)
766 return -EINVAL;
768 len = ibm->read(page);
769 if (len < 0)
770 return len;
772 if (len <= off + count)
773 *eof = 1;
774 *start = page + off;
775 len -= off;
776 if (len > count)
777 len = count;
778 if (len < 0)
779 len = 0;
781 return len;
784 static int dispatch_procfs_write(struct file *file,
785 const char __user *userbuf,
786 unsigned long count, void *data)
788 struct ibm_struct *ibm = data;
789 char *kernbuf;
790 int ret;
792 if (!ibm || !ibm->write)
793 return -EINVAL;
795 kernbuf = kmalloc(count + 2, GFP_KERNEL);
796 if (!kernbuf)
797 return -ENOMEM;
799 if (copy_from_user(kernbuf, userbuf, count)) {
800 kfree(kernbuf);
801 return -EFAULT;
804 kernbuf[count] = 0;
805 strcat(kernbuf, ",");
806 ret = ibm->write(kernbuf);
807 if (ret == 0)
808 ret = count;
810 kfree(kernbuf);
812 return ret;
815 static char *next_cmd(char **cmds)
817 char *start = *cmds;
818 char *end;
820 while ((end = strchr(start, ',')) && end == start)
821 start = end + 1;
823 if (!end)
824 return NULL;
826 *end = 0;
827 *cmds = end + 1;
828 return start;
832 /****************************************************************************
833 ****************************************************************************
835 * Device model: input, hwmon and platform
837 ****************************************************************************
838 ****************************************************************************/
840 static struct platform_device *tpacpi_pdev;
841 static struct platform_device *tpacpi_sensors_pdev;
842 static struct device *tpacpi_hwmon;
843 static struct input_dev *tpacpi_inputdev;
844 static struct mutex tpacpi_inputdev_send_mutex;
845 static LIST_HEAD(tpacpi_all_drivers);
847 static int tpacpi_suspend_handler(struct platform_device *pdev,
848 pm_message_t state)
850 struct ibm_struct *ibm, *itmp;
852 list_for_each_entry_safe(ibm, itmp,
853 &tpacpi_all_drivers,
854 all_drivers) {
855 if (ibm->suspend)
856 (ibm->suspend)(state);
859 return 0;
862 static int tpacpi_resume_handler(struct platform_device *pdev)
864 struct ibm_struct *ibm, *itmp;
866 list_for_each_entry_safe(ibm, itmp,
867 &tpacpi_all_drivers,
868 all_drivers) {
869 if (ibm->resume)
870 (ibm->resume)();
873 return 0;
876 static void tpacpi_shutdown_handler(struct platform_device *pdev)
878 struct ibm_struct *ibm, *itmp;
880 list_for_each_entry_safe(ibm, itmp,
881 &tpacpi_all_drivers,
882 all_drivers) {
883 if (ibm->shutdown)
884 (ibm->shutdown)();
888 static struct platform_driver tpacpi_pdriver = {
889 .driver = {
890 .name = TPACPI_DRVR_NAME,
891 .owner = THIS_MODULE,
893 .suspend = tpacpi_suspend_handler,
894 .resume = tpacpi_resume_handler,
895 .shutdown = tpacpi_shutdown_handler,
898 static struct platform_driver tpacpi_hwmon_pdriver = {
899 .driver = {
900 .name = TPACPI_HWMON_DRVR_NAME,
901 .owner = THIS_MODULE,
905 /*************************************************************************
906 * sysfs support helpers
909 struct attribute_set {
910 unsigned int members, max_members;
911 struct attribute_group group;
914 struct attribute_set_obj {
915 struct attribute_set s;
916 struct attribute *a;
917 } __attribute__((packed));
919 static struct attribute_set *create_attr_set(unsigned int max_members,
920 const char *name)
922 struct attribute_set_obj *sobj;
924 if (max_members == 0)
925 return NULL;
927 /* Allocates space for implicit NULL at the end too */
928 sobj = kzalloc(sizeof(struct attribute_set_obj) +
929 max_members * sizeof(struct attribute *),
930 GFP_KERNEL);
931 if (!sobj)
932 return NULL;
933 sobj->s.max_members = max_members;
934 sobj->s.group.attrs = &sobj->a;
935 sobj->s.group.name = name;
937 return &sobj->s;
940 #define destroy_attr_set(_set) \
941 kfree(_set);
943 /* not multi-threaded safe, use it in a single thread per set */
944 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
946 if (!s || !attr)
947 return -EINVAL;
949 if (s->members >= s->max_members)
950 return -ENOMEM;
952 s->group.attrs[s->members] = attr;
953 s->members++;
955 return 0;
958 static int add_many_to_attr_set(struct attribute_set *s,
959 struct attribute **attr,
960 unsigned int count)
962 int i, res;
964 for (i = 0; i < count; i++) {
965 res = add_to_attr_set(s, attr[i]);
966 if (res)
967 return res;
970 return 0;
973 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
975 sysfs_remove_group(kobj, &s->group);
976 destroy_attr_set(s);
979 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
980 sysfs_create_group(_kobj, &_attr_set->group)
982 static int parse_strtoul(const char *buf,
983 unsigned long max, unsigned long *value)
985 char *endp;
987 while (*buf && isspace(*buf))
988 buf++;
989 *value = simple_strtoul(buf, &endp, 0);
990 while (*endp && isspace(*endp))
991 endp++;
992 if (*endp || *value > max)
993 return -EINVAL;
995 return 0;
998 static void tpacpi_disable_brightness_delay(void)
1000 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1001 printk(TPACPI_NOTICE
1002 "ACPI backlight control delay disabled\n");
1005 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1007 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1008 union acpi_object *obj;
1009 int rc;
1011 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1012 obj = (union acpi_object *)buffer.pointer;
1013 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1014 printk(TPACPI_ERR "Unknown _BCL data, "
1015 "please report this to %s\n", TPACPI_MAIL);
1016 rc = 0;
1017 } else {
1018 rc = obj->package.count;
1020 } else {
1021 return 0;
1024 kfree(buffer.pointer);
1025 return rc;
1028 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1029 u32 lvl, void *context, void **rv)
1031 char name[ACPI_PATH_SEGMENT_LENGTH];
1032 struct acpi_buffer buffer = { sizeof(name), &name };
1034 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1035 !strncmp("_BCL", name, sizeof(name) - 1)) {
1036 BUG_ON(!rv || !*rv);
1037 **(int **)rv = tpacpi_query_bcl_levels(handle);
1038 return AE_CTRL_TERMINATE;
1039 } else {
1040 return AE_OK;
1045 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1047 static int __init tpacpi_check_std_acpi_brightness_support(void)
1049 int status;
1050 int bcl_levels = 0;
1051 void *bcl_ptr = &bcl_levels;
1053 if (!vid_handle) {
1054 TPACPI_ACPIHANDLE_INIT(vid);
1056 if (!vid_handle)
1057 return 0;
1060 * Search for a _BCL method, and execute it. This is safe on all
1061 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1062 * BIOS in ACPI backlight control mode. We do NOT have to care
1063 * about calling the _BCL method in an enabled video device, any
1064 * will do for our purposes.
1067 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1068 tpacpi_acpi_walk_find_bcl, NULL,
1069 &bcl_ptr);
1071 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1072 tp_features.bright_acpimode = 1;
1073 return (bcl_levels - 2);
1076 return 0;
1079 static int __init tpacpi_new_rfkill(const unsigned int id,
1080 struct rfkill **rfk,
1081 const enum rfkill_type rfktype,
1082 const char *name,
1083 const bool set_default,
1084 int (*toggle_radio)(void *, enum rfkill_state),
1085 int (*get_state)(void *, enum rfkill_state *))
1087 int res;
1088 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1090 res = get_state(NULL, &initial_state);
1091 if (res < 0) {
1092 printk(TPACPI_ERR
1093 "failed to read initial state for %s, error %d; "
1094 "will turn radio off\n", name, res);
1095 } else if (set_default) {
1096 /* try to set the initial state as the default for the rfkill
1097 * type, since we ask the firmware to preserve it across S5 in
1098 * NVRAM */
1099 if (rfkill_set_default(rfktype,
1100 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1101 RFKILL_STATE_UNBLOCKED :
1102 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1103 vdbg_printk(TPACPI_DBG_RFKILL,
1104 "Default state for %s cannot be changed\n",
1105 name);
1108 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1109 if (!*rfk) {
1110 printk(TPACPI_ERR
1111 "failed to allocate memory for rfkill class\n");
1112 return -ENOMEM;
1115 (*rfk)->name = name;
1116 (*rfk)->get_state = get_state;
1117 (*rfk)->toggle_radio = toggle_radio;
1118 (*rfk)->state = initial_state;
1120 res = rfkill_register(*rfk);
1121 if (res < 0) {
1122 printk(TPACPI_ERR
1123 "failed to register %s rfkill switch: %d\n",
1124 name, res);
1125 rfkill_free(*rfk);
1126 *rfk = NULL;
1127 return res;
1130 return 0;
1133 static void printk_deprecated_attribute(const char * const what,
1134 const char * const details)
1136 tpacpi_log_usertask("deprecated sysfs attribute");
1137 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1138 "will be removed. %s\n",
1139 what, details);
1142 static void printk_deprecated_rfkill_attribute(const char * const what)
1144 printk_deprecated_attribute(what,
1145 "Please switch to generic rfkill before year 2010");
1148 /*************************************************************************
1149 * thinkpad-acpi driver attributes
1152 /* interface_version --------------------------------------------------- */
1153 static ssize_t tpacpi_driver_interface_version_show(
1154 struct device_driver *drv,
1155 char *buf)
1157 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1160 static DRIVER_ATTR(interface_version, S_IRUGO,
1161 tpacpi_driver_interface_version_show, NULL);
1163 /* debug_level --------------------------------------------------------- */
1164 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1165 char *buf)
1167 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1170 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1171 const char *buf, size_t count)
1173 unsigned long t;
1175 if (parse_strtoul(buf, 0xffff, &t))
1176 return -EINVAL;
1178 dbg_level = t;
1180 return count;
1183 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1184 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1186 /* version ------------------------------------------------------------- */
1187 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1188 char *buf)
1190 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1191 TPACPI_DESC, TPACPI_VERSION);
1194 static DRIVER_ATTR(version, S_IRUGO,
1195 tpacpi_driver_version_show, NULL);
1197 /* --------------------------------------------------------------------- */
1199 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1201 static void tpacpi_send_radiosw_update(void);
1203 /* wlsw_emulstate ------------------------------------------------------ */
1204 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1205 char *buf)
1207 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1210 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1211 const char *buf, size_t count)
1213 unsigned long t;
1215 if (parse_strtoul(buf, 1, &t))
1216 return -EINVAL;
1218 if (tpacpi_wlsw_emulstate != t) {
1219 tpacpi_wlsw_emulstate = !!t;
1220 tpacpi_send_radiosw_update();
1221 } else
1222 tpacpi_wlsw_emulstate = !!t;
1224 return count;
1227 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1228 tpacpi_driver_wlsw_emulstate_show,
1229 tpacpi_driver_wlsw_emulstate_store);
1231 /* bluetooth_emulstate ------------------------------------------------- */
1232 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1233 struct device_driver *drv,
1234 char *buf)
1236 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1239 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1240 struct device_driver *drv,
1241 const char *buf, size_t count)
1243 unsigned long t;
1245 if (parse_strtoul(buf, 1, &t))
1246 return -EINVAL;
1248 tpacpi_bluetooth_emulstate = !!t;
1250 return count;
1253 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1254 tpacpi_driver_bluetooth_emulstate_show,
1255 tpacpi_driver_bluetooth_emulstate_store);
1257 /* wwan_emulstate ------------------------------------------------- */
1258 static ssize_t tpacpi_driver_wwan_emulstate_show(
1259 struct device_driver *drv,
1260 char *buf)
1262 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1265 static ssize_t tpacpi_driver_wwan_emulstate_store(
1266 struct device_driver *drv,
1267 const char *buf, size_t count)
1269 unsigned long t;
1271 if (parse_strtoul(buf, 1, &t))
1272 return -EINVAL;
1274 tpacpi_wwan_emulstate = !!t;
1276 return count;
1279 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1280 tpacpi_driver_wwan_emulstate_show,
1281 tpacpi_driver_wwan_emulstate_store);
1283 /* uwb_emulstate ------------------------------------------------- */
1284 static ssize_t tpacpi_driver_uwb_emulstate_show(
1285 struct device_driver *drv,
1286 char *buf)
1288 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1291 static ssize_t tpacpi_driver_uwb_emulstate_store(
1292 struct device_driver *drv,
1293 const char *buf, size_t count)
1295 unsigned long t;
1297 if (parse_strtoul(buf, 1, &t))
1298 return -EINVAL;
1300 tpacpi_uwb_emulstate = !!t;
1302 return count;
1305 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1306 tpacpi_driver_uwb_emulstate_show,
1307 tpacpi_driver_uwb_emulstate_store);
1308 #endif
1310 /* --------------------------------------------------------------------- */
1312 static struct driver_attribute *tpacpi_driver_attributes[] = {
1313 &driver_attr_debug_level, &driver_attr_version,
1314 &driver_attr_interface_version,
1317 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1319 int i, res;
1321 i = 0;
1322 res = 0;
1323 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1324 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1325 i++;
1328 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1329 if (!res && dbg_wlswemul)
1330 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1331 if (!res && dbg_bluetoothemul)
1332 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1333 if (!res && dbg_wwanemul)
1334 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1335 if (!res && dbg_uwbemul)
1336 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1337 #endif
1339 return res;
1342 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1344 int i;
1346 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1347 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1349 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1350 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1351 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1352 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1353 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1354 #endif
1357 /****************************************************************************
1358 ****************************************************************************
1360 * Subdrivers
1362 ****************************************************************************
1363 ****************************************************************************/
1365 /*************************************************************************
1366 * thinkpad-acpi init subdriver
1369 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1371 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1372 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1374 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1375 (thinkpad_id.bios_version_str) ?
1376 thinkpad_id.bios_version_str : "unknown",
1377 (thinkpad_id.ec_version_str) ?
1378 thinkpad_id.ec_version_str : "unknown");
1380 if (thinkpad_id.vendor && thinkpad_id.model_str)
1381 printk(TPACPI_INFO "%s %s, model %s\n",
1382 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1383 "IBM" : ((thinkpad_id.vendor ==
1384 PCI_VENDOR_ID_LENOVO) ?
1385 "Lenovo" : "Unknown vendor"),
1386 thinkpad_id.model_str,
1387 (thinkpad_id.nummodel_str) ?
1388 thinkpad_id.nummodel_str : "unknown");
1390 return 0;
1393 static int thinkpad_acpi_driver_read(char *p)
1395 int len = 0;
1397 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1398 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1400 return len;
1403 static struct ibm_struct thinkpad_acpi_driver_data = {
1404 .name = "driver",
1405 .read = thinkpad_acpi_driver_read,
1408 /*************************************************************************
1409 * Hotkey subdriver
1412 enum { /* hot key scan codes (derived from ACPI DSDT) */
1413 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1414 TP_ACPI_HOTKEYSCAN_FNF2,
1415 TP_ACPI_HOTKEYSCAN_FNF3,
1416 TP_ACPI_HOTKEYSCAN_FNF4,
1417 TP_ACPI_HOTKEYSCAN_FNF5,
1418 TP_ACPI_HOTKEYSCAN_FNF6,
1419 TP_ACPI_HOTKEYSCAN_FNF7,
1420 TP_ACPI_HOTKEYSCAN_FNF8,
1421 TP_ACPI_HOTKEYSCAN_FNF9,
1422 TP_ACPI_HOTKEYSCAN_FNF10,
1423 TP_ACPI_HOTKEYSCAN_FNF11,
1424 TP_ACPI_HOTKEYSCAN_FNF12,
1425 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1426 TP_ACPI_HOTKEYSCAN_FNINSERT,
1427 TP_ACPI_HOTKEYSCAN_FNDELETE,
1428 TP_ACPI_HOTKEYSCAN_FNHOME,
1429 TP_ACPI_HOTKEYSCAN_FNEND,
1430 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1431 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1432 TP_ACPI_HOTKEYSCAN_FNSPACE,
1433 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1434 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1435 TP_ACPI_HOTKEYSCAN_MUTE,
1436 TP_ACPI_HOTKEYSCAN_THINKPAD,
1439 enum { /* Keys available through NVRAM polling */
1440 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1441 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1444 enum { /* Positions of some of the keys in hotkey masks */
1445 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1446 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1447 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1448 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1449 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1450 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1451 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1452 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1453 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1454 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1455 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1458 enum { /* NVRAM to ACPI HKEY group map */
1459 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1460 TP_ACPI_HKEY_ZOOM_MASK |
1461 TP_ACPI_HKEY_DISPSWTCH_MASK |
1462 TP_ACPI_HKEY_HIBERNATE_MASK,
1463 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1464 TP_ACPI_HKEY_BRGHTDWN_MASK,
1465 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1466 TP_ACPI_HKEY_VOLDWN_MASK |
1467 TP_ACPI_HKEY_MUTE_MASK,
1470 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1471 struct tp_nvram_state {
1472 u16 thinkpad_toggle:1;
1473 u16 zoom_toggle:1;
1474 u16 display_toggle:1;
1475 u16 thinklight_toggle:1;
1476 u16 hibernate_toggle:1;
1477 u16 displayexp_toggle:1;
1478 u16 display_state:1;
1479 u16 brightness_toggle:1;
1480 u16 volume_toggle:1;
1481 u16 mute:1;
1483 u8 brightness_level;
1484 u8 volume_level;
1487 static struct task_struct *tpacpi_hotkey_task;
1488 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1489 static int hotkey_poll_freq = 10; /* Hz */
1490 static struct mutex hotkey_thread_mutex;
1491 static struct mutex hotkey_thread_data_mutex;
1492 static unsigned int hotkey_config_change;
1494 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1496 #define hotkey_source_mask 0U
1498 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1500 static struct mutex hotkey_mutex;
1502 static enum { /* Reasons for waking up */
1503 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1504 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1505 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1506 } hotkey_wakeup_reason;
1508 static int hotkey_autosleep_ack;
1510 static u32 hotkey_orig_mask;
1511 static u32 hotkey_all_mask;
1512 static u32 hotkey_reserved_mask;
1513 static u32 hotkey_mask;
1515 static unsigned int hotkey_report_mode;
1517 static u16 *hotkey_keycode_map;
1519 static struct attribute_set *hotkey_dev_attributes;
1521 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1522 #define HOTKEY_CONFIG_CRITICAL_START \
1523 do { \
1524 mutex_lock(&hotkey_thread_data_mutex); \
1525 hotkey_config_change++; \
1526 } while (0);
1527 #define HOTKEY_CONFIG_CRITICAL_END \
1528 mutex_unlock(&hotkey_thread_data_mutex);
1529 #else
1530 #define HOTKEY_CONFIG_CRITICAL_START
1531 #define HOTKEY_CONFIG_CRITICAL_END
1532 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1534 /* HKEY.MHKG() return bits */
1535 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1537 static int hotkey_get_wlsw(int *status)
1539 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1540 if (dbg_wlswemul) {
1541 *status = !!tpacpi_wlsw_emulstate;
1542 return 0;
1544 #endif
1545 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1546 return -EIO;
1547 return 0;
1550 static int hotkey_get_tablet_mode(int *status)
1552 int s;
1554 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1555 return -EIO;
1557 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1558 return 0;
1562 * Call with hotkey_mutex held
1564 static int hotkey_mask_get(void)
1566 u32 m = 0;
1568 if (tp_features.hotkey_mask) {
1569 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1570 return -EIO;
1572 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1574 return 0;
1578 * Call with hotkey_mutex held
1580 static int hotkey_mask_set(u32 mask)
1582 int i;
1583 int rc = 0;
1585 if (tp_features.hotkey_mask) {
1586 if (!tp_warned.hotkey_mask_ff &&
1587 (mask == 0xffff || mask == 0xffffff ||
1588 mask == 0xffffffff)) {
1589 tp_warned.hotkey_mask_ff = 1;
1590 printk(TPACPI_NOTICE
1591 "setting the hotkey mask to 0x%08x is likely "
1592 "not the best way to go about it\n", mask);
1593 printk(TPACPI_NOTICE
1594 "please consider using the driver defaults, "
1595 "and refer to up-to-date thinkpad-acpi "
1596 "documentation\n");
1599 HOTKEY_CONFIG_CRITICAL_START
1600 for (i = 0; i < 32; i++) {
1601 u32 m = 1 << i;
1602 /* enable in firmware mask only keys not in NVRAM
1603 * mode, but enable the key in the cached hotkey_mask
1604 * regardless of mode, or the key will end up
1605 * disabled by hotkey_mask_get() */
1606 if (!acpi_evalf(hkey_handle,
1607 NULL, "MHKM", "vdd", i + 1,
1608 !!((mask & ~hotkey_source_mask) & m))) {
1609 rc = -EIO;
1610 break;
1611 } else {
1612 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1615 HOTKEY_CONFIG_CRITICAL_END
1617 /* hotkey_mask_get must be called unconditionally below */
1618 if (!hotkey_mask_get() && !rc &&
1619 (hotkey_mask & ~hotkey_source_mask) !=
1620 (mask & ~hotkey_source_mask)) {
1621 printk(TPACPI_NOTICE
1622 "requested hot key mask 0x%08x, but "
1623 "firmware forced it to 0x%08x\n",
1624 mask, hotkey_mask);
1626 } else {
1627 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1628 HOTKEY_CONFIG_CRITICAL_START
1629 hotkey_mask = mask & hotkey_source_mask;
1630 HOTKEY_CONFIG_CRITICAL_END
1631 hotkey_mask_get();
1632 if (hotkey_mask != mask) {
1633 printk(TPACPI_NOTICE
1634 "requested hot key mask 0x%08x, "
1635 "forced to 0x%08x (NVRAM poll mask is "
1636 "0x%08x): no firmware mask support\n",
1637 mask, hotkey_mask, hotkey_source_mask);
1639 #else
1640 hotkey_mask_get();
1641 rc = -ENXIO;
1642 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1645 return rc;
1648 static int hotkey_status_get(int *status)
1650 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1651 return -EIO;
1653 return 0;
1656 static int hotkey_status_set(bool enable)
1658 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1659 return -EIO;
1661 return 0;
1664 static void tpacpi_input_send_tabletsw(void)
1666 int state;
1668 if (tp_features.hotkey_tablet &&
1669 !hotkey_get_tablet_mode(&state)) {
1670 mutex_lock(&tpacpi_inputdev_send_mutex);
1672 input_report_switch(tpacpi_inputdev,
1673 SW_TABLET_MODE, !!state);
1674 input_sync(tpacpi_inputdev);
1676 mutex_unlock(&tpacpi_inputdev_send_mutex);
1680 static void tpacpi_input_send_key(unsigned int scancode)
1682 unsigned int keycode;
1684 keycode = hotkey_keycode_map[scancode];
1686 if (keycode != KEY_RESERVED) {
1687 mutex_lock(&tpacpi_inputdev_send_mutex);
1689 input_report_key(tpacpi_inputdev, keycode, 1);
1690 if (keycode == KEY_UNKNOWN)
1691 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1692 scancode);
1693 input_sync(tpacpi_inputdev);
1695 input_report_key(tpacpi_inputdev, keycode, 0);
1696 if (keycode == KEY_UNKNOWN)
1697 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1698 scancode);
1699 input_sync(tpacpi_inputdev);
1701 mutex_unlock(&tpacpi_inputdev_send_mutex);
1705 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1706 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1708 static void tpacpi_hotkey_send_key(unsigned int scancode)
1710 tpacpi_input_send_key(scancode);
1711 if (hotkey_report_mode < 2) {
1712 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1713 0x80, 0x1001 + scancode);
1717 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1719 u8 d;
1721 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1722 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1723 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1724 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1725 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1726 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1728 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1729 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1730 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1732 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1733 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1734 n->displayexp_toggle =
1735 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1737 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1738 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1739 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1740 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1741 n->brightness_toggle =
1742 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1744 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1745 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1746 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1747 >> TP_NVRAM_POS_LEVEL_VOLUME;
1748 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1749 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1753 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1754 do { \
1755 if ((mask & (1 << __scancode)) && \
1756 oldn->__member != newn->__member) \
1757 tpacpi_hotkey_send_key(__scancode); \
1758 } while (0)
1760 #define TPACPI_MAY_SEND_KEY(__scancode) \
1761 do { if (mask & (1 << __scancode)) \
1762 tpacpi_hotkey_send_key(__scancode); } while (0)
1764 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1765 struct tp_nvram_state *newn,
1766 u32 mask)
1768 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1769 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1770 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1771 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1773 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1775 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1777 /* handle volume */
1778 if (oldn->volume_toggle != newn->volume_toggle) {
1779 if (oldn->mute != newn->mute) {
1780 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1782 if (oldn->volume_level > newn->volume_level) {
1783 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1784 } else if (oldn->volume_level < newn->volume_level) {
1785 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1786 } else if (oldn->mute == newn->mute) {
1787 /* repeated key presses that didn't change state */
1788 if (newn->mute) {
1789 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1790 } else if (newn->volume_level != 0) {
1791 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1792 } else {
1793 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1798 /* handle brightness */
1799 if (oldn->brightness_toggle != newn->brightness_toggle) {
1800 if (oldn->brightness_level < newn->brightness_level) {
1801 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1802 } else if (oldn->brightness_level > newn->brightness_level) {
1803 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1804 } else {
1805 /* repeated key presses that didn't change state */
1806 if (newn->brightness_level != 0) {
1807 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1808 } else {
1809 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1815 #undef TPACPI_COMPARE_KEY
1816 #undef TPACPI_MAY_SEND_KEY
1818 static int hotkey_kthread(void *data)
1820 struct tp_nvram_state s[2];
1821 u32 mask;
1822 unsigned int si, so;
1823 unsigned long t;
1824 unsigned int change_detector, must_reset;
1826 mutex_lock(&hotkey_thread_mutex);
1828 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1829 goto exit;
1831 set_freezable();
1833 so = 0;
1834 si = 1;
1835 t = 0;
1837 /* Initial state for compares */
1838 mutex_lock(&hotkey_thread_data_mutex);
1839 change_detector = hotkey_config_change;
1840 mask = hotkey_source_mask & hotkey_mask;
1841 mutex_unlock(&hotkey_thread_data_mutex);
1842 hotkey_read_nvram(&s[so], mask);
1844 while (!kthread_should_stop() && hotkey_poll_freq) {
1845 if (t == 0)
1846 t = 1000/hotkey_poll_freq;
1847 t = msleep_interruptible(t);
1848 if (unlikely(kthread_should_stop()))
1849 break;
1850 must_reset = try_to_freeze();
1851 if (t > 0 && !must_reset)
1852 continue;
1854 mutex_lock(&hotkey_thread_data_mutex);
1855 if (must_reset || hotkey_config_change != change_detector) {
1856 /* forget old state on thaw or config change */
1857 si = so;
1858 t = 0;
1859 change_detector = hotkey_config_change;
1861 mask = hotkey_source_mask & hotkey_mask;
1862 mutex_unlock(&hotkey_thread_data_mutex);
1864 if (likely(mask)) {
1865 hotkey_read_nvram(&s[si], mask);
1866 if (likely(si != so)) {
1867 hotkey_compare_and_issue_event(&s[so], &s[si],
1868 mask);
1872 so = si;
1873 si ^= 1;
1876 exit:
1877 mutex_unlock(&hotkey_thread_mutex);
1878 return 0;
1881 static void hotkey_poll_stop_sync(void)
1883 if (tpacpi_hotkey_task) {
1884 if (frozen(tpacpi_hotkey_task) ||
1885 freezing(tpacpi_hotkey_task))
1886 thaw_process(tpacpi_hotkey_task);
1888 kthread_stop(tpacpi_hotkey_task);
1889 tpacpi_hotkey_task = NULL;
1890 mutex_lock(&hotkey_thread_mutex);
1891 /* at this point, the thread did exit */
1892 mutex_unlock(&hotkey_thread_mutex);
1896 /* call with hotkey_mutex held */
1897 static void hotkey_poll_setup(int may_warn)
1899 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1900 hotkey_poll_freq > 0 &&
1901 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1902 if (!tpacpi_hotkey_task) {
1903 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1904 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1905 if (IS_ERR(tpacpi_hotkey_task)) {
1906 tpacpi_hotkey_task = NULL;
1907 printk(TPACPI_ERR
1908 "could not create kernel thread "
1909 "for hotkey polling\n");
1912 } else {
1913 hotkey_poll_stop_sync();
1914 if (may_warn &&
1915 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1916 printk(TPACPI_NOTICE
1917 "hot keys 0x%08x require polling, "
1918 "which is currently disabled\n",
1919 hotkey_source_mask);
1924 static void hotkey_poll_setup_safe(int may_warn)
1926 mutex_lock(&hotkey_mutex);
1927 hotkey_poll_setup(may_warn);
1928 mutex_unlock(&hotkey_mutex);
1931 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1933 static void hotkey_poll_setup_safe(int __unused)
1937 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1939 static int hotkey_inputdev_open(struct input_dev *dev)
1941 switch (tpacpi_lifecycle) {
1942 case TPACPI_LIFE_INIT:
1944 * hotkey_init will call hotkey_poll_setup_safe
1945 * at the appropriate moment
1947 return 0;
1948 case TPACPI_LIFE_EXITING:
1949 return -EBUSY;
1950 case TPACPI_LIFE_RUNNING:
1951 hotkey_poll_setup_safe(0);
1952 return 0;
1955 /* Should only happen if tpacpi_lifecycle is corrupt */
1956 BUG();
1957 return -EBUSY;
1960 static void hotkey_inputdev_close(struct input_dev *dev)
1962 /* disable hotkey polling when possible */
1963 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1964 hotkey_poll_setup_safe(0);
1967 /* sysfs hotkey enable ------------------------------------------------- */
1968 static ssize_t hotkey_enable_show(struct device *dev,
1969 struct device_attribute *attr,
1970 char *buf)
1972 int res, status;
1974 printk_deprecated_attribute("hotkey_enable",
1975 "Hotkey reporting is always enabled");
1977 res = hotkey_status_get(&status);
1978 if (res)
1979 return res;
1981 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1984 static ssize_t hotkey_enable_store(struct device *dev,
1985 struct device_attribute *attr,
1986 const char *buf, size_t count)
1988 unsigned long t;
1990 printk_deprecated_attribute("hotkey_enable",
1991 "Hotkeys can be disabled through hotkey_mask");
1993 if (parse_strtoul(buf, 1, &t))
1994 return -EINVAL;
1996 if (t == 0)
1997 return -EPERM;
1999 return count;
2002 static struct device_attribute dev_attr_hotkey_enable =
2003 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2004 hotkey_enable_show, hotkey_enable_store);
2006 /* sysfs hotkey mask --------------------------------------------------- */
2007 static ssize_t hotkey_mask_show(struct device *dev,
2008 struct device_attribute *attr,
2009 char *buf)
2011 int res;
2013 if (mutex_lock_killable(&hotkey_mutex))
2014 return -ERESTARTSYS;
2015 res = hotkey_mask_get();
2016 mutex_unlock(&hotkey_mutex);
2018 return (res)?
2019 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2022 static ssize_t hotkey_mask_store(struct device *dev,
2023 struct device_attribute *attr,
2024 const char *buf, size_t count)
2026 unsigned long t;
2027 int res;
2029 if (parse_strtoul(buf, 0xffffffffUL, &t))
2030 return -EINVAL;
2032 if (mutex_lock_killable(&hotkey_mutex))
2033 return -ERESTARTSYS;
2035 res = hotkey_mask_set(t);
2037 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2038 hotkey_poll_setup(1);
2039 #endif
2041 mutex_unlock(&hotkey_mutex);
2043 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2045 return (res) ? res : count;
2048 static struct device_attribute dev_attr_hotkey_mask =
2049 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2050 hotkey_mask_show, hotkey_mask_store);
2052 /* sysfs hotkey bios_enabled ------------------------------------------- */
2053 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2054 struct device_attribute *attr,
2055 char *buf)
2057 return sprintf(buf, "0\n");
2060 static struct device_attribute dev_attr_hotkey_bios_enabled =
2061 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2063 /* sysfs hotkey bios_mask ---------------------------------------------- */
2064 static ssize_t hotkey_bios_mask_show(struct device *dev,
2065 struct device_attribute *attr,
2066 char *buf)
2068 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2071 static struct device_attribute dev_attr_hotkey_bios_mask =
2072 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2074 /* sysfs hotkey all_mask ----------------------------------------------- */
2075 static ssize_t hotkey_all_mask_show(struct device *dev,
2076 struct device_attribute *attr,
2077 char *buf)
2079 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2080 hotkey_all_mask | hotkey_source_mask);
2083 static struct device_attribute dev_attr_hotkey_all_mask =
2084 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2086 /* sysfs hotkey recommended_mask --------------------------------------- */
2087 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2088 struct device_attribute *attr,
2089 char *buf)
2091 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2092 (hotkey_all_mask | hotkey_source_mask)
2093 & ~hotkey_reserved_mask);
2096 static struct device_attribute dev_attr_hotkey_recommended_mask =
2097 __ATTR(hotkey_recommended_mask, S_IRUGO,
2098 hotkey_recommended_mask_show, NULL);
2100 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2102 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2103 static ssize_t hotkey_source_mask_show(struct device *dev,
2104 struct device_attribute *attr,
2105 char *buf)
2107 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2110 static ssize_t hotkey_source_mask_store(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t count)
2114 unsigned long t;
2116 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2117 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2118 return -EINVAL;
2120 if (mutex_lock_killable(&hotkey_mutex))
2121 return -ERESTARTSYS;
2123 HOTKEY_CONFIG_CRITICAL_START
2124 hotkey_source_mask = t;
2125 HOTKEY_CONFIG_CRITICAL_END
2127 hotkey_poll_setup(1);
2129 mutex_unlock(&hotkey_mutex);
2131 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2133 return count;
2136 static struct device_attribute dev_attr_hotkey_source_mask =
2137 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2138 hotkey_source_mask_show, hotkey_source_mask_store);
2140 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2141 static ssize_t hotkey_poll_freq_show(struct device *dev,
2142 struct device_attribute *attr,
2143 char *buf)
2145 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2148 static ssize_t hotkey_poll_freq_store(struct device *dev,
2149 struct device_attribute *attr,
2150 const char *buf, size_t count)
2152 unsigned long t;
2154 if (parse_strtoul(buf, 25, &t))
2155 return -EINVAL;
2157 if (mutex_lock_killable(&hotkey_mutex))
2158 return -ERESTARTSYS;
2160 hotkey_poll_freq = t;
2162 hotkey_poll_setup(1);
2163 mutex_unlock(&hotkey_mutex);
2165 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2167 return count;
2170 static struct device_attribute dev_attr_hotkey_poll_freq =
2171 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2172 hotkey_poll_freq_show, hotkey_poll_freq_store);
2174 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2176 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2177 static ssize_t hotkey_radio_sw_show(struct device *dev,
2178 struct device_attribute *attr,
2179 char *buf)
2181 int res, s;
2182 res = hotkey_get_wlsw(&s);
2183 if (res < 0)
2184 return res;
2186 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2189 static struct device_attribute dev_attr_hotkey_radio_sw =
2190 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2192 static void hotkey_radio_sw_notify_change(void)
2194 if (tp_features.hotkey_wlsw)
2195 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2196 "hotkey_radio_sw");
2199 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2200 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2201 struct device_attribute *attr,
2202 char *buf)
2204 int res, s;
2205 res = hotkey_get_tablet_mode(&s);
2206 if (res < 0)
2207 return res;
2209 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2212 static struct device_attribute dev_attr_hotkey_tablet_mode =
2213 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2215 static void hotkey_tablet_mode_notify_change(void)
2217 if (tp_features.hotkey_tablet)
2218 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2219 "hotkey_tablet_mode");
2222 /* sysfs hotkey report_mode -------------------------------------------- */
2223 static ssize_t hotkey_report_mode_show(struct device *dev,
2224 struct device_attribute *attr,
2225 char *buf)
2227 return snprintf(buf, PAGE_SIZE, "%d\n",
2228 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2231 static struct device_attribute dev_attr_hotkey_report_mode =
2232 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2234 /* sysfs wakeup reason (pollable) -------------------------------------- */
2235 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2236 struct device_attribute *attr,
2237 char *buf)
2239 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2242 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2243 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2245 static void hotkey_wakeup_reason_notify_change(void)
2247 if (tp_features.hotkey_mask)
2248 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2249 "wakeup_reason");
2252 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2253 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2254 struct device_attribute *attr,
2255 char *buf)
2257 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2260 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2261 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2262 hotkey_wakeup_hotunplug_complete_show, NULL);
2264 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2266 if (tp_features.hotkey_mask)
2267 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2268 "wakeup_hotunplug_complete");
2271 /* --------------------------------------------------------------------- */
2273 static struct attribute *hotkey_attributes[] __initdata = {
2274 &dev_attr_hotkey_enable.attr,
2275 &dev_attr_hotkey_bios_enabled.attr,
2276 &dev_attr_hotkey_report_mode.attr,
2277 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2278 &dev_attr_hotkey_mask.attr,
2279 &dev_attr_hotkey_all_mask.attr,
2280 &dev_attr_hotkey_recommended_mask.attr,
2281 &dev_attr_hotkey_source_mask.attr,
2282 &dev_attr_hotkey_poll_freq.attr,
2283 #endif
2286 static struct attribute *hotkey_mask_attributes[] __initdata = {
2287 &dev_attr_hotkey_bios_mask.attr,
2288 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2289 &dev_attr_hotkey_mask.attr,
2290 &dev_attr_hotkey_all_mask.attr,
2291 &dev_attr_hotkey_recommended_mask.attr,
2292 #endif
2293 &dev_attr_hotkey_wakeup_reason.attr,
2294 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2297 static void bluetooth_update_rfk(void);
2298 static void wan_update_rfk(void);
2299 static void uwb_update_rfk(void);
2300 static void tpacpi_send_radiosw_update(void)
2302 int wlsw;
2304 /* Sync these BEFORE sending any rfkill events */
2305 if (tp_features.bluetooth)
2306 bluetooth_update_rfk();
2307 if (tp_features.wan)
2308 wan_update_rfk();
2309 if (tp_features.uwb)
2310 uwb_update_rfk();
2312 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2313 mutex_lock(&tpacpi_inputdev_send_mutex);
2315 input_report_switch(tpacpi_inputdev,
2316 SW_RFKILL_ALL, !!wlsw);
2317 input_sync(tpacpi_inputdev);
2319 mutex_unlock(&tpacpi_inputdev_send_mutex);
2321 hotkey_radio_sw_notify_change();
2324 static void hotkey_exit(void)
2326 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2327 hotkey_poll_stop_sync();
2328 #endif
2330 if (hotkey_dev_attributes)
2331 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2333 kfree(hotkey_keycode_map);
2335 if (tp_features.hotkey) {
2336 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2337 "restoring original hot key mask\n");
2338 /* no short-circuit boolean operator below! */
2339 if ((hotkey_mask_set(hotkey_orig_mask) |
2340 hotkey_status_set(false)) != 0)
2341 printk(TPACPI_ERR
2342 "failed to restore hot key mask "
2343 "to BIOS defaults\n");
2347 static int __init hotkey_init(struct ibm_init_struct *iibm)
2349 /* Requirements for changing the default keymaps:
2351 * 1. Many of the keys are mapped to KEY_RESERVED for very
2352 * good reasons. Do not change them unless you have deep
2353 * knowledge on the IBM and Lenovo ThinkPad firmware for
2354 * the various ThinkPad models. The driver behaves
2355 * differently for KEY_RESERVED: such keys have their
2356 * hot key mask *unset* in mask_recommended, and also
2357 * in the initial hot key mask programmed into the
2358 * firmware at driver load time, which means the firm-
2359 * ware may react very differently if you change them to
2360 * something else;
2362 * 2. You must be subscribed to the linux-thinkpad and
2363 * ibm-acpi-devel mailing lists, and you should read the
2364 * list archives since 2007 if you want to change the
2365 * keymaps. This requirement exists so that you will
2366 * know the past history of problems with the thinkpad-
2367 * acpi driver keymaps, and also that you will be
2368 * listening to any bug reports;
2370 * 3. Do not send thinkpad-acpi specific patches directly to
2371 * for merging, *ever*. Send them to the linux-acpi
2372 * mailinglist for comments. Merging is to be done only
2373 * through acpi-test and the ACPI maintainer.
2375 * If the above is too much to ask, don't change the keymap.
2376 * Ask the thinkpad-acpi maintainer to do it, instead.
2378 static u16 ibm_keycode_map[] __initdata = {
2379 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2380 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2381 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2382 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2384 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2385 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2386 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2387 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2389 /* brightness: firmware always reacts to them, unless
2390 * X.org did some tricks in the radeon BIOS scratch
2391 * registers of *some* models */
2392 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2393 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2395 /* Thinklight: firmware always react to it */
2396 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2398 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2399 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2401 /* Volume: firmware always react to it and reprograms
2402 * the built-in *extra* mixer. Never map it to control
2403 * another mixer by default. */
2404 KEY_RESERVED, /* 0x14: VOLUME UP */
2405 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2406 KEY_RESERVED, /* 0x16: MUTE */
2408 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2410 /* (assignments unknown, please report if found) */
2411 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2412 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2414 static u16 lenovo_keycode_map[] __initdata = {
2415 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2416 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2417 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2418 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2420 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2421 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2422 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2423 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2425 /* These either have to go through ACPI video, or
2426 * act like in the IBM ThinkPads, so don't ever
2427 * enable them by default */
2428 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2429 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2431 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2433 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2434 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2436 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2437 * react to it and reprograms the built-in *extra* mixer.
2438 * Never map it to control another mixer by default.
2440 * T60?, T61, R60?, R61: firmware and EC tries to send
2441 * these over the regular keyboard, so these are no-ops,
2442 * but there are still weird bugs re. MUTE, so do not
2443 * change unless you get test reports from all Lenovo
2444 * models. May cause the BIOS to interfere with the
2445 * HDA mixer.
2447 KEY_RESERVED, /* 0x14: VOLUME UP */
2448 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2449 KEY_RESERVED, /* 0x16: MUTE */
2451 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2453 /* (assignments unknown, please report if found) */
2454 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2455 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2458 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2459 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2460 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2462 int res, i;
2463 int status;
2464 int hkeyv;
2466 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2467 "initializing hotkey subdriver\n");
2469 BUG_ON(!tpacpi_inputdev);
2470 BUG_ON(tpacpi_inputdev->open != NULL ||
2471 tpacpi_inputdev->close != NULL);
2473 TPACPI_ACPIHANDLE_INIT(hkey);
2474 mutex_init(&hotkey_mutex);
2476 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2477 mutex_init(&hotkey_thread_mutex);
2478 mutex_init(&hotkey_thread_data_mutex);
2479 #endif
2481 /* hotkey not supported on 570 */
2482 tp_features.hotkey = hkey_handle != NULL;
2484 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2485 "hotkeys are %s\n",
2486 str_supported(tp_features.hotkey));
2488 if (!tp_features.hotkey)
2489 return 1;
2491 tpacpi_disable_brightness_delay();
2493 hotkey_dev_attributes = create_attr_set(13, NULL);
2494 if (!hotkey_dev_attributes)
2495 return -ENOMEM;
2496 res = add_many_to_attr_set(hotkey_dev_attributes,
2497 hotkey_attributes,
2498 ARRAY_SIZE(hotkey_attributes));
2499 if (res)
2500 goto err_exit;
2502 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2503 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2504 for HKEY interface version 0x100 */
2505 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2506 if ((hkeyv >> 8) != 1) {
2507 printk(TPACPI_ERR "unknown version of the "
2508 "HKEY interface: 0x%x\n", hkeyv);
2509 printk(TPACPI_ERR "please report this to %s\n",
2510 TPACPI_MAIL);
2511 } else {
2513 * MHKV 0x100 in A31, R40, R40e,
2514 * T4x, X31, and later
2516 tp_features.hotkey_mask = 1;
2517 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2518 "firmware HKEY interface version: 0x%x\n",
2519 hkeyv);
2523 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2524 "hotkey masks are %s\n",
2525 str_supported(tp_features.hotkey_mask));
2527 if (tp_features.hotkey_mask) {
2528 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2529 "MHKA", "qd")) {
2530 printk(TPACPI_ERR
2531 "missing MHKA handler, "
2532 "please report this to %s\n",
2533 TPACPI_MAIL);
2534 /* FN+F12, FN+F4, FN+F3 */
2535 hotkey_all_mask = 0x080cU;
2539 /* hotkey_source_mask *must* be zero for
2540 * the first hotkey_mask_get */
2541 if (tp_features.hotkey_mask) {
2542 res = hotkey_mask_get();
2543 if (res)
2544 goto err_exit;
2546 hotkey_orig_mask = hotkey_mask;
2547 res = add_many_to_attr_set(
2548 hotkey_dev_attributes,
2549 hotkey_mask_attributes,
2550 ARRAY_SIZE(hotkey_mask_attributes));
2551 if (res)
2552 goto err_exit;
2555 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2556 if (tp_features.hotkey_mask) {
2557 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2558 & ~hotkey_all_mask;
2559 } else {
2560 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2563 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2564 "hotkey source mask 0x%08x, polling freq %d\n",
2565 hotkey_source_mask, hotkey_poll_freq);
2566 #endif
2568 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2569 if (dbg_wlswemul) {
2570 tp_features.hotkey_wlsw = 1;
2571 printk(TPACPI_INFO
2572 "radio switch emulation enabled\n");
2573 } else
2574 #endif
2575 /* Not all thinkpads have a hardware radio switch */
2576 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2577 tp_features.hotkey_wlsw = 1;
2578 printk(TPACPI_INFO
2579 "radio switch found; radios are %s\n",
2580 enabled(status, 0));
2582 if (tp_features.hotkey_wlsw)
2583 res = add_to_attr_set(hotkey_dev_attributes,
2584 &dev_attr_hotkey_radio_sw.attr);
2586 /* For X41t, X60t, X61t Tablets... */
2587 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2588 tp_features.hotkey_tablet = 1;
2589 printk(TPACPI_INFO
2590 "possible tablet mode switch found; "
2591 "ThinkPad in %s mode\n",
2592 (status & TP_HOTKEY_TABLET_MASK)?
2593 "tablet" : "laptop");
2594 res = add_to_attr_set(hotkey_dev_attributes,
2595 &dev_attr_hotkey_tablet_mode.attr);
2598 if (!res)
2599 res = register_attr_set_with_sysfs(
2600 hotkey_dev_attributes,
2601 &tpacpi_pdev->dev.kobj);
2602 if (res)
2603 goto err_exit;
2605 /* Set up key map */
2607 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2608 GFP_KERNEL);
2609 if (!hotkey_keycode_map) {
2610 printk(TPACPI_ERR
2611 "failed to allocate memory for key map\n");
2612 res = -ENOMEM;
2613 goto err_exit;
2616 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2617 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2618 "using Lenovo default hot key map\n");
2619 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2620 TPACPI_HOTKEY_MAP_SIZE);
2621 } else {
2622 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2623 "using IBM default hot key map\n");
2624 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2625 TPACPI_HOTKEY_MAP_SIZE);
2628 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2629 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2630 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2631 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2632 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2633 tpacpi_inputdev->keycode = hotkey_keycode_map;
2634 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2635 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2636 set_bit(hotkey_keycode_map[i],
2637 tpacpi_inputdev->keybit);
2638 } else {
2639 if (i < sizeof(hotkey_reserved_mask)*8)
2640 hotkey_reserved_mask |= 1 << i;
2644 if (tp_features.hotkey_wlsw) {
2645 set_bit(EV_SW, tpacpi_inputdev->evbit);
2646 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2648 if (tp_features.hotkey_tablet) {
2649 set_bit(EV_SW, tpacpi_inputdev->evbit);
2650 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2653 /* Do not issue duplicate brightness change events to
2654 * userspace */
2655 if (!tp_features.bright_acpimode)
2656 /* update bright_acpimode... */
2657 tpacpi_check_std_acpi_brightness_support();
2659 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2660 printk(TPACPI_INFO
2661 "This ThinkPad has standard ACPI backlight "
2662 "brightness control, supported by the ACPI "
2663 "video driver\n");
2664 printk(TPACPI_NOTICE
2665 "Disabling thinkpad-acpi brightness events "
2666 "by default...\n");
2668 /* The hotkey_reserved_mask change below is not
2669 * necessary while the keys are at KEY_RESERVED in the
2670 * default map, but better safe than sorry, leave it
2671 * here as a marker of what we have to do, especially
2672 * when we finally become able to set this at runtime
2673 * on response to X.org requests */
2674 hotkey_reserved_mask |=
2675 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2676 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2679 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2680 "enabling firmware HKEY event interface...\n");
2681 res = hotkey_status_set(true);
2682 if (res) {
2683 hotkey_exit();
2684 return res;
2686 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2687 & ~hotkey_reserved_mask)
2688 | hotkey_orig_mask);
2689 if (res < 0 && res != -ENXIO) {
2690 hotkey_exit();
2691 return res;
2694 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2695 "legacy ibm/hotkey event reporting over procfs %s\n",
2696 (hotkey_report_mode < 2) ?
2697 "enabled" : "disabled");
2699 tpacpi_inputdev->open = &hotkey_inputdev_open;
2700 tpacpi_inputdev->close = &hotkey_inputdev_close;
2702 hotkey_poll_setup_safe(1);
2703 tpacpi_send_radiosw_update();
2704 tpacpi_input_send_tabletsw();
2706 return 0;
2708 err_exit:
2709 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2710 hotkey_dev_attributes = NULL;
2712 return (res < 0)? res : 1;
2715 static bool hotkey_notify_hotkey(const u32 hkey,
2716 bool *send_acpi_ev,
2717 bool *ignore_acpi_ev)
2719 /* 0x1000-0x1FFF: key presses */
2720 unsigned int scancode = hkey & 0xfff;
2721 *send_acpi_ev = true;
2722 *ignore_acpi_ev = false;
2724 if (scancode > 0 && scancode < 0x21) {
2725 scancode--;
2726 if (!(hotkey_source_mask & (1 << scancode))) {
2727 tpacpi_input_send_key(scancode);
2728 *send_acpi_ev = false;
2729 } else {
2730 *ignore_acpi_ev = true;
2732 return true;
2734 return false;
2737 static bool hotkey_notify_wakeup(const u32 hkey,
2738 bool *send_acpi_ev,
2739 bool *ignore_acpi_ev)
2741 /* 0x2000-0x2FFF: Wakeup reason */
2742 *send_acpi_ev = true;
2743 *ignore_acpi_ev = false;
2745 switch (hkey) {
2746 case 0x2304: /* suspend, undock */
2747 case 0x2404: /* hibernation, undock */
2748 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2749 *ignore_acpi_ev = true;
2750 break;
2752 case 0x2305: /* suspend, bay eject */
2753 case 0x2405: /* hibernation, bay eject */
2754 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2755 *ignore_acpi_ev = true;
2756 break;
2758 case 0x2313: /* Battery on critical low level (S3) */
2759 case 0x2413: /* Battery on critical low level (S4) */
2760 printk(TPACPI_ALERT
2761 "EMERGENCY WAKEUP: battery almost empty\n");
2762 /* how to auto-heal: */
2763 /* 2313: woke up from S3, go to S4/S5 */
2764 /* 2413: woke up from S4, go to S5 */
2765 break;
2767 default:
2768 return false;
2771 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2772 printk(TPACPI_INFO
2773 "woke up due to a hot-unplug "
2774 "request...\n");
2775 hotkey_wakeup_reason_notify_change();
2777 return true;
2780 static bool hotkey_notify_usrevent(const u32 hkey,
2781 bool *send_acpi_ev,
2782 bool *ignore_acpi_ev)
2784 /* 0x5000-0x5FFF: human interface helpers */
2785 *send_acpi_ev = true;
2786 *ignore_acpi_ev = false;
2788 switch (hkey) {
2789 case 0x5010: /* Lenovo new BIOS: brightness changed */
2790 case 0x500b: /* X61t: tablet pen inserted into bay */
2791 case 0x500c: /* X61t: tablet pen removed from bay */
2792 return true;
2794 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2795 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2796 tpacpi_input_send_tabletsw();
2797 hotkey_tablet_mode_notify_change();
2798 *send_acpi_ev = false;
2799 return true;
2801 case 0x5001:
2802 case 0x5002:
2803 /* LID switch events. Do not propagate */
2804 *ignore_acpi_ev = true;
2805 return true;
2807 default:
2808 return false;
2812 static bool hotkey_notify_thermal(const u32 hkey,
2813 bool *send_acpi_ev,
2814 bool *ignore_acpi_ev)
2816 /* 0x6000-0x6FFF: thermal alarms */
2817 *send_acpi_ev = true;
2818 *ignore_acpi_ev = false;
2820 switch (hkey) {
2821 case 0x6011:
2822 printk(TPACPI_CRIT
2823 "THERMAL ALARM: battery is too hot!\n");
2824 /* recommended action: warn user through gui */
2825 return true;
2826 case 0x6012:
2827 printk(TPACPI_ALERT
2828 "THERMAL EMERGENCY: battery is extremely hot!\n");
2829 /* recommended action: immediate sleep/hibernate */
2830 return true;
2831 case 0x6021:
2832 printk(TPACPI_CRIT
2833 "THERMAL ALARM: "
2834 "a sensor reports something is too hot!\n");
2835 /* recommended action: warn user through gui, that */
2836 /* some internal component is too hot */
2837 return true;
2838 case 0x6022:
2839 printk(TPACPI_ALERT
2840 "THERMAL EMERGENCY: "
2841 "a sensor reports something is extremely hot!\n");
2842 /* recommended action: immediate sleep/hibernate */
2843 return true;
2844 case 0x6030:
2845 printk(TPACPI_INFO
2846 "EC reports that Thermal Table has changed\n");
2847 /* recommended action: do nothing, we don't have
2848 * Lenovo ATM information */
2849 return true;
2850 default:
2851 printk(TPACPI_ALERT
2852 "THERMAL ALERT: unknown thermal alarm received\n");
2853 return false;
2857 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2859 u32 hkey;
2860 bool send_acpi_ev;
2861 bool ignore_acpi_ev;
2862 bool known_ev;
2864 if (event != 0x80) {
2865 printk(TPACPI_ERR
2866 "unknown HKEY notification event %d\n", event);
2867 /* forward it to userspace, maybe it knows how to handle it */
2868 acpi_bus_generate_netlink_event(
2869 ibm->acpi->device->pnp.device_class,
2870 dev_name(&ibm->acpi->device->dev),
2871 event, 0);
2872 return;
2875 while (1) {
2876 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2877 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2878 return;
2881 if (hkey == 0) {
2882 /* queue empty */
2883 return;
2886 send_acpi_ev = true;
2887 ignore_acpi_ev = false;
2889 switch (hkey >> 12) {
2890 case 1:
2891 /* 0x1000-0x1FFF: key presses */
2892 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2893 &ignore_acpi_ev);
2894 break;
2895 case 2:
2896 /* 0x2000-0x2FFF: Wakeup reason */
2897 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2898 &ignore_acpi_ev);
2899 break;
2900 case 3:
2901 /* 0x3000-0x3FFF: bay-related wakeups */
2902 if (hkey == 0x3003) {
2903 hotkey_autosleep_ack = 1;
2904 printk(TPACPI_INFO
2905 "bay ejected\n");
2906 hotkey_wakeup_hotunplug_complete_notify_change();
2907 known_ev = true;
2908 } else {
2909 known_ev = false;
2911 break;
2912 case 4:
2913 /* 0x4000-0x4FFF: dock-related wakeups */
2914 if (hkey == 0x4003) {
2915 hotkey_autosleep_ack = 1;
2916 printk(TPACPI_INFO
2917 "undocked\n");
2918 hotkey_wakeup_hotunplug_complete_notify_change();
2919 known_ev = true;
2920 } else {
2921 known_ev = false;
2923 break;
2924 case 5:
2925 /* 0x5000-0x5FFF: human interface helpers */
2926 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2927 &ignore_acpi_ev);
2928 break;
2929 case 6:
2930 /* 0x6000-0x6FFF: thermal alarms */
2931 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2932 &ignore_acpi_ev);
2933 break;
2934 case 7:
2935 /* 0x7000-0x7FFF: misc */
2936 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2937 tpacpi_send_radiosw_update();
2938 send_acpi_ev = 0;
2939 known_ev = true;
2940 break;
2942 /* fallthrough to default */
2943 default:
2944 known_ev = false;
2946 if (!known_ev) {
2947 printk(TPACPI_NOTICE
2948 "unhandled HKEY event 0x%04x\n", hkey);
2949 printk(TPACPI_NOTICE
2950 "please report the conditions when this "
2951 "event happened to %s\n", TPACPI_MAIL);
2954 /* Legacy events */
2955 if (!ignore_acpi_ev &&
2956 (send_acpi_ev || hotkey_report_mode < 2)) {
2957 acpi_bus_generate_proc_event(ibm->acpi->device,
2958 event, hkey);
2961 /* netlink events */
2962 if (!ignore_acpi_ev && send_acpi_ev) {
2963 acpi_bus_generate_netlink_event(
2964 ibm->acpi->device->pnp.device_class,
2965 dev_name(&ibm->acpi->device->dev),
2966 event, hkey);
2971 static void hotkey_suspend(pm_message_t state)
2973 /* Do these on suspend, we get the events on early resume! */
2974 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2975 hotkey_autosleep_ack = 0;
2978 static void hotkey_resume(void)
2980 tpacpi_disable_brightness_delay();
2982 if (hotkey_mask_get())
2983 printk(TPACPI_ERR
2984 "error while trying to read hot key mask "
2985 "from firmware\n");
2986 tpacpi_send_radiosw_update();
2987 hotkey_tablet_mode_notify_change();
2988 hotkey_wakeup_reason_notify_change();
2989 hotkey_wakeup_hotunplug_complete_notify_change();
2990 hotkey_poll_setup_safe(0);
2993 /* procfs -------------------------------------------------------------- */
2994 static int hotkey_read(char *p)
2996 int res, status;
2997 int len = 0;
2999 if (!tp_features.hotkey) {
3000 len += sprintf(p + len, "status:\t\tnot supported\n");
3001 return len;
3004 if (mutex_lock_killable(&hotkey_mutex))
3005 return -ERESTARTSYS;
3006 res = hotkey_status_get(&status);
3007 if (!res)
3008 res = hotkey_mask_get();
3009 mutex_unlock(&hotkey_mutex);
3010 if (res)
3011 return res;
3013 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3014 if (tp_features.hotkey_mask) {
3015 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3016 len += sprintf(p + len,
3017 "commands:\tenable, disable, reset, <mask>\n");
3018 } else {
3019 len += sprintf(p + len, "mask:\t\tnot supported\n");
3020 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3023 return len;
3026 static void hotkey_enabledisable_warn(bool enable)
3028 tpacpi_log_usertask("procfs hotkey enable/disable");
3029 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3030 TPACPI_WARN
3031 "hotkey enable/disable functionality has been "
3032 "removed from the driver. Hotkeys are always "
3033 "enabled\n"))
3034 printk(TPACPI_ERR
3035 "Please remove the hotkey=enable module "
3036 "parameter, it is deprecated. Hotkeys are always "
3037 "enabled\n");
3040 static int hotkey_write(char *buf)
3042 int res;
3043 u32 mask;
3044 char *cmd;
3046 if (!tp_features.hotkey)
3047 return -ENODEV;
3049 if (mutex_lock_killable(&hotkey_mutex))
3050 return -ERESTARTSYS;
3052 mask = hotkey_mask;
3054 res = 0;
3055 while ((cmd = next_cmd(&buf))) {
3056 if (strlencmp(cmd, "enable") == 0) {
3057 hotkey_enabledisable_warn(1);
3058 } else if (strlencmp(cmd, "disable") == 0) {
3059 hotkey_enabledisable_warn(0);
3060 res = -EPERM;
3061 } else if (strlencmp(cmd, "reset") == 0) {
3062 mask = hotkey_orig_mask;
3063 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3064 /* mask set */
3065 } else if (sscanf(cmd, "%x", &mask) == 1) {
3066 /* mask set */
3067 } else {
3068 res = -EINVAL;
3069 goto errexit;
3073 if (!res)
3074 tpacpi_disclose_usertask("procfs hotkey",
3075 "set mask to 0x%08x\n", mask);
3077 if (!res && mask != hotkey_mask)
3078 res = hotkey_mask_set(mask);
3080 errexit:
3081 mutex_unlock(&hotkey_mutex);
3082 return res;
3085 static const struct acpi_device_id ibm_htk_device_ids[] = {
3086 {TPACPI_ACPI_HKEY_HID, 0},
3087 {"", 0},
3090 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3091 .hid = ibm_htk_device_ids,
3092 .notify = hotkey_notify,
3093 .handle = &hkey_handle,
3094 .type = ACPI_DEVICE_NOTIFY,
3097 static struct ibm_struct hotkey_driver_data = {
3098 .name = "hotkey",
3099 .read = hotkey_read,
3100 .write = hotkey_write,
3101 .exit = hotkey_exit,
3102 .resume = hotkey_resume,
3103 .suspend = hotkey_suspend,
3104 .acpi = &ibm_hotkey_acpidriver,
3107 /*************************************************************************
3108 * Bluetooth subdriver
3111 enum {
3112 /* ACPI GBDC/SBDC bits */
3113 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3114 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3115 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3116 off / last state */
3119 enum {
3120 /* ACPI \BLTH commands */
3121 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3122 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3123 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3124 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3125 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3128 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3130 static struct rfkill *tpacpi_bluetooth_rfkill;
3132 static void bluetooth_suspend(pm_message_t state)
3134 /* Try to make sure radio will resume powered off */
3135 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3136 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3137 vdbg_printk(TPACPI_DBG_RFKILL,
3138 "bluetooth power down on resume request failed\n");
3141 static int bluetooth_get_radiosw(void)
3143 int status;
3145 if (!tp_features.bluetooth)
3146 return -ENODEV;
3148 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3149 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3150 return RFKILL_STATE_HARD_BLOCKED;
3152 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3153 if (dbg_bluetoothemul)
3154 return (tpacpi_bluetooth_emulstate) ?
3155 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3156 #endif
3158 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3159 return -EIO;
3161 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3162 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3165 static void bluetooth_update_rfk(void)
3167 int status;
3169 if (!tpacpi_bluetooth_rfkill)
3170 return;
3172 status = bluetooth_get_radiosw();
3173 if (status < 0)
3174 return;
3175 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3177 vdbg_printk(TPACPI_DBG_RFKILL,
3178 "forced rfkill state to %d\n",
3179 status);
3182 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3184 int status;
3186 if (!tp_features.bluetooth)
3187 return -ENODEV;
3189 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3190 * reason to risk weird behaviour. */
3191 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3192 && radio_on)
3193 return -EPERM;
3195 vdbg_printk(TPACPI_DBG_RFKILL,
3196 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3198 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3199 if (dbg_bluetoothemul) {
3200 tpacpi_bluetooth_emulstate = !!radio_on;
3201 if (update_rfk)
3202 bluetooth_update_rfk();
3203 return 0;
3205 #endif
3207 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3208 if (radio_on)
3209 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3210 else
3211 status = 0;
3212 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3213 return -EIO;
3215 if (update_rfk)
3216 bluetooth_update_rfk();
3218 return 0;
3221 /* sysfs bluetooth enable ---------------------------------------------- */
3222 static ssize_t bluetooth_enable_show(struct device *dev,
3223 struct device_attribute *attr,
3224 char *buf)
3226 int status;
3228 printk_deprecated_rfkill_attribute("bluetooth_enable");
3230 status = bluetooth_get_radiosw();
3231 if (status < 0)
3232 return status;
3234 return snprintf(buf, PAGE_SIZE, "%d\n",
3235 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3238 static ssize_t bluetooth_enable_store(struct device *dev,
3239 struct device_attribute *attr,
3240 const char *buf, size_t count)
3242 unsigned long t;
3243 int res;
3245 printk_deprecated_rfkill_attribute("bluetooth_enable");
3247 if (parse_strtoul(buf, 1, &t))
3248 return -EINVAL;
3250 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3252 res = bluetooth_set_radiosw(t, 1);
3254 return (res) ? res : count;
3257 static struct device_attribute dev_attr_bluetooth_enable =
3258 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3259 bluetooth_enable_show, bluetooth_enable_store);
3261 /* --------------------------------------------------------------------- */
3263 static struct attribute *bluetooth_attributes[] = {
3264 &dev_attr_bluetooth_enable.attr,
3265 NULL
3268 static const struct attribute_group bluetooth_attr_group = {
3269 .attrs = bluetooth_attributes,
3272 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3274 int bts = bluetooth_get_radiosw();
3276 if (bts < 0)
3277 return bts;
3279 *state = bts;
3280 return 0;
3283 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3285 dbg_printk(TPACPI_DBG_RFKILL,
3286 "request to change radio state to %d\n", state);
3287 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3290 static void bluetooth_shutdown(void)
3292 /* Order firmware to save current state to NVRAM */
3293 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3294 TP_ACPI_BLTH_SAVE_STATE))
3295 printk(TPACPI_NOTICE
3296 "failed to save bluetooth state to NVRAM\n");
3297 else
3298 vdbg_printk(TPACPI_DBG_RFKILL,
3299 "bluestooth state saved to NVRAM\n");
3302 static void bluetooth_exit(void)
3304 bluetooth_shutdown();
3306 if (tpacpi_bluetooth_rfkill)
3307 rfkill_unregister(tpacpi_bluetooth_rfkill);
3309 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3310 &bluetooth_attr_group);
3313 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3315 int res;
3316 int status = 0;
3318 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3319 "initializing bluetooth subdriver\n");
3321 TPACPI_ACPIHANDLE_INIT(hkey);
3323 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3324 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3325 tp_features.bluetooth = hkey_handle &&
3326 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3328 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3329 "bluetooth is %s, status 0x%02x\n",
3330 str_supported(tp_features.bluetooth),
3331 status);
3333 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3334 if (dbg_bluetoothemul) {
3335 tp_features.bluetooth = 1;
3336 printk(TPACPI_INFO
3337 "bluetooth switch emulation enabled\n");
3338 } else
3339 #endif
3340 if (tp_features.bluetooth &&
3341 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3342 /* no bluetooth hardware present in system */
3343 tp_features.bluetooth = 0;
3344 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3345 "bluetooth hardware not installed\n");
3348 if (!tp_features.bluetooth)
3349 return 1;
3351 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3352 &bluetooth_attr_group);
3353 if (res)
3354 return res;
3356 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3357 &tpacpi_bluetooth_rfkill,
3358 RFKILL_TYPE_BLUETOOTH,
3359 TPACPI_RFK_BLUETOOTH_SW_NAME,
3360 true,
3361 tpacpi_bluetooth_rfk_set,
3362 tpacpi_bluetooth_rfk_get);
3363 if (res) {
3364 bluetooth_exit();
3365 return res;
3368 return 0;
3371 /* procfs -------------------------------------------------------------- */
3372 static int bluetooth_read(char *p)
3374 int len = 0;
3375 int status = bluetooth_get_radiosw();
3377 if (!tp_features.bluetooth)
3378 len += sprintf(p + len, "status:\t\tnot supported\n");
3379 else {
3380 len += sprintf(p + len, "status:\t\t%s\n",
3381 (status == RFKILL_STATE_UNBLOCKED) ?
3382 "enabled" : "disabled");
3383 len += sprintf(p + len, "commands:\tenable, disable\n");
3386 return len;
3389 static int bluetooth_write(char *buf)
3391 char *cmd;
3392 int state = -1;
3394 if (!tp_features.bluetooth)
3395 return -ENODEV;
3397 while ((cmd = next_cmd(&buf))) {
3398 if (strlencmp(cmd, "enable") == 0) {
3399 state = 1;
3400 } else if (strlencmp(cmd, "disable") == 0) {
3401 state = 0;
3402 } else
3403 return -EINVAL;
3406 if (state != -1) {
3407 tpacpi_disclose_usertask("procfs bluetooth",
3408 "attempt to %s\n",
3409 state ? "enable" : "disable");
3410 bluetooth_set_radiosw(state, 1);
3413 return 0;
3416 static struct ibm_struct bluetooth_driver_data = {
3417 .name = "bluetooth",
3418 .read = bluetooth_read,
3419 .write = bluetooth_write,
3420 .exit = bluetooth_exit,
3421 .suspend = bluetooth_suspend,
3422 .shutdown = bluetooth_shutdown,
3425 /*************************************************************************
3426 * Wan subdriver
3429 enum {
3430 /* ACPI GWAN/SWAN bits */
3431 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3432 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3433 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3434 off / last state */
3437 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3439 static struct rfkill *tpacpi_wan_rfkill;
3441 static void wan_suspend(pm_message_t state)
3443 /* Try to make sure radio will resume powered off */
3444 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3445 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3446 vdbg_printk(TPACPI_DBG_RFKILL,
3447 "WWAN power down on resume request failed\n");
3450 static int wan_get_radiosw(void)
3452 int status;
3454 if (!tp_features.wan)
3455 return -ENODEV;
3457 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3458 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3459 return RFKILL_STATE_HARD_BLOCKED;
3461 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3462 if (dbg_wwanemul)
3463 return (tpacpi_wwan_emulstate) ?
3464 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3465 #endif
3467 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3468 return -EIO;
3470 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3471 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3474 static void wan_update_rfk(void)
3476 int status;
3478 if (!tpacpi_wan_rfkill)
3479 return;
3481 status = wan_get_radiosw();
3482 if (status < 0)
3483 return;
3484 rfkill_force_state(tpacpi_wan_rfkill, status);
3486 vdbg_printk(TPACPI_DBG_RFKILL,
3487 "forced rfkill state to %d\n",
3488 status);
3491 static int wan_set_radiosw(int radio_on, int update_rfk)
3493 int status;
3495 if (!tp_features.wan)
3496 return -ENODEV;
3498 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3499 * reason to risk weird behaviour. */
3500 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3501 && radio_on)
3502 return -EPERM;
3504 vdbg_printk(TPACPI_DBG_RFKILL,
3505 "will %s WWAN\n", radio_on ? "enable" : "disable");
3507 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3508 if (dbg_wwanemul) {
3509 tpacpi_wwan_emulstate = !!radio_on;
3510 if (update_rfk)
3511 wan_update_rfk();
3512 return 0;
3514 #endif
3516 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3517 if (radio_on)
3518 status = TP_ACPI_WANCARD_RADIOSSW;
3519 else
3520 status = 0;
3521 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3522 return -EIO;
3524 if (update_rfk)
3525 wan_update_rfk();
3527 return 0;
3530 /* sysfs wan enable ---------------------------------------------------- */
3531 static ssize_t wan_enable_show(struct device *dev,
3532 struct device_attribute *attr,
3533 char *buf)
3535 int status;
3537 printk_deprecated_rfkill_attribute("wwan_enable");
3539 status = wan_get_radiosw();
3540 if (status < 0)
3541 return status;
3543 return snprintf(buf, PAGE_SIZE, "%d\n",
3544 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3547 static ssize_t wan_enable_store(struct device *dev,
3548 struct device_attribute *attr,
3549 const char *buf, size_t count)
3551 unsigned long t;
3552 int res;
3554 printk_deprecated_rfkill_attribute("wwan_enable");
3556 if (parse_strtoul(buf, 1, &t))
3557 return -EINVAL;
3559 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3561 res = wan_set_radiosw(t, 1);
3563 return (res) ? res : count;
3566 static struct device_attribute dev_attr_wan_enable =
3567 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3568 wan_enable_show, wan_enable_store);
3570 /* --------------------------------------------------------------------- */
3572 static struct attribute *wan_attributes[] = {
3573 &dev_attr_wan_enable.attr,
3574 NULL
3577 static const struct attribute_group wan_attr_group = {
3578 .attrs = wan_attributes,
3581 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3583 int wans = wan_get_radiosw();
3585 if (wans < 0)
3586 return wans;
3588 *state = wans;
3589 return 0;
3592 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3594 dbg_printk(TPACPI_DBG_RFKILL,
3595 "request to change radio state to %d\n", state);
3596 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3599 static void wan_shutdown(void)
3601 /* Order firmware to save current state to NVRAM */
3602 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3603 TP_ACPI_WGSV_SAVE_STATE))
3604 printk(TPACPI_NOTICE
3605 "failed to save WWAN state to NVRAM\n");
3606 else
3607 vdbg_printk(TPACPI_DBG_RFKILL,
3608 "WWAN state saved to NVRAM\n");
3611 static void wan_exit(void)
3613 wan_shutdown();
3615 if (tpacpi_wan_rfkill)
3616 rfkill_unregister(tpacpi_wan_rfkill);
3618 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3619 &wan_attr_group);
3622 static int __init wan_init(struct ibm_init_struct *iibm)
3624 int res;
3625 int status = 0;
3627 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3628 "initializing wan subdriver\n");
3630 TPACPI_ACPIHANDLE_INIT(hkey);
3632 tp_features.wan = hkey_handle &&
3633 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3635 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3636 "wan is %s, status 0x%02x\n",
3637 str_supported(tp_features.wan),
3638 status);
3640 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3641 if (dbg_wwanemul) {
3642 tp_features.wan = 1;
3643 printk(TPACPI_INFO
3644 "wwan switch emulation enabled\n");
3645 } else
3646 #endif
3647 if (tp_features.wan &&
3648 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3649 /* no wan hardware present in system */
3650 tp_features.wan = 0;
3651 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3652 "wan hardware not installed\n");
3655 if (!tp_features.wan)
3656 return 1;
3658 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3659 &wan_attr_group);
3660 if (res)
3661 return res;
3663 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3664 &tpacpi_wan_rfkill,
3665 RFKILL_TYPE_WWAN,
3666 TPACPI_RFK_WWAN_SW_NAME,
3667 true,
3668 tpacpi_wan_rfk_set,
3669 tpacpi_wan_rfk_get);
3670 if (res) {
3671 wan_exit();
3672 return res;
3675 return 0;
3678 /* procfs -------------------------------------------------------------- */
3679 static int wan_read(char *p)
3681 int len = 0;
3682 int status = wan_get_radiosw();
3684 tpacpi_disclose_usertask("procfs wan", "read");
3686 if (!tp_features.wan)
3687 len += sprintf(p + len, "status:\t\tnot supported\n");
3688 else {
3689 len += sprintf(p + len, "status:\t\t%s\n",
3690 (status == RFKILL_STATE_UNBLOCKED) ?
3691 "enabled" : "disabled");
3692 len += sprintf(p + len, "commands:\tenable, disable\n");
3695 return len;
3698 static int wan_write(char *buf)
3700 char *cmd;
3701 int state = -1;
3703 if (!tp_features.wan)
3704 return -ENODEV;
3706 while ((cmd = next_cmd(&buf))) {
3707 if (strlencmp(cmd, "enable") == 0) {
3708 state = 1;
3709 } else if (strlencmp(cmd, "disable") == 0) {
3710 state = 0;
3711 } else
3712 return -EINVAL;
3715 if (state != -1) {
3716 tpacpi_disclose_usertask("procfs wan",
3717 "attempt to %s\n",
3718 state ? "enable" : "disable");
3719 wan_set_radiosw(state, 1);
3722 return 0;
3725 static struct ibm_struct wan_driver_data = {
3726 .name = "wan",
3727 .read = wan_read,
3728 .write = wan_write,
3729 .exit = wan_exit,
3730 .suspend = wan_suspend,
3731 .shutdown = wan_shutdown,
3734 /*************************************************************************
3735 * UWB subdriver
3738 enum {
3739 /* ACPI GUWB/SUWB bits */
3740 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3741 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3744 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3746 static struct rfkill *tpacpi_uwb_rfkill;
3748 static int uwb_get_radiosw(void)
3750 int status;
3752 if (!tp_features.uwb)
3753 return -ENODEV;
3755 /* WLSW overrides UWB in firmware/hardware, reflect that */
3756 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3757 return RFKILL_STATE_HARD_BLOCKED;
3759 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3760 if (dbg_uwbemul)
3761 return (tpacpi_uwb_emulstate) ?
3762 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3763 #endif
3765 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3766 return -EIO;
3768 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3769 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3772 static void uwb_update_rfk(void)
3774 int status;
3776 if (!tpacpi_uwb_rfkill)
3777 return;
3779 status = uwb_get_radiosw();
3780 if (status < 0)
3781 return;
3782 rfkill_force_state(tpacpi_uwb_rfkill, status);
3784 vdbg_printk(TPACPI_DBG_RFKILL,
3785 "forced rfkill state to %d\n",
3786 status);
3789 static int uwb_set_radiosw(int radio_on, int update_rfk)
3791 int status;
3793 if (!tp_features.uwb)
3794 return -ENODEV;
3796 /* WLSW overrides UWB in firmware/hardware, but there is no
3797 * reason to risk weird behaviour. */
3798 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3799 && radio_on)
3800 return -EPERM;
3802 vdbg_printk(TPACPI_DBG_RFKILL,
3803 "will %s UWB\n", radio_on ? "enable" : "disable");
3805 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3806 if (dbg_uwbemul) {
3807 tpacpi_uwb_emulstate = !!radio_on;
3808 if (update_rfk)
3809 uwb_update_rfk();
3810 return 0;
3812 #endif
3814 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3815 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3816 return -EIO;
3818 if (update_rfk)
3819 uwb_update_rfk();
3821 return 0;
3824 /* --------------------------------------------------------------------- */
3826 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3828 int uwbs = uwb_get_radiosw();
3830 if (uwbs < 0)
3831 return uwbs;
3833 *state = uwbs;
3834 return 0;
3837 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3839 dbg_printk(TPACPI_DBG_RFKILL,
3840 "request to change radio state to %d\n", state);
3841 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3844 static void uwb_exit(void)
3846 if (tpacpi_uwb_rfkill)
3847 rfkill_unregister(tpacpi_uwb_rfkill);
3850 static int __init uwb_init(struct ibm_init_struct *iibm)
3852 int res;
3853 int status = 0;
3855 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3856 "initializing uwb subdriver\n");
3858 TPACPI_ACPIHANDLE_INIT(hkey);
3860 tp_features.uwb = hkey_handle &&
3861 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3863 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3864 "uwb is %s, status 0x%02x\n",
3865 str_supported(tp_features.uwb),
3866 status);
3868 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3869 if (dbg_uwbemul) {
3870 tp_features.uwb = 1;
3871 printk(TPACPI_INFO
3872 "uwb switch emulation enabled\n");
3873 } else
3874 #endif
3875 if (tp_features.uwb &&
3876 !(status & TP_ACPI_UWB_HWPRESENT)) {
3877 /* no uwb hardware present in system */
3878 tp_features.uwb = 0;
3879 dbg_printk(TPACPI_DBG_INIT,
3880 "uwb hardware not installed\n");
3883 if (!tp_features.uwb)
3884 return 1;
3886 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3887 &tpacpi_uwb_rfkill,
3888 RFKILL_TYPE_UWB,
3889 TPACPI_RFK_UWB_SW_NAME,
3890 false,
3891 tpacpi_uwb_rfk_set,
3892 tpacpi_uwb_rfk_get);
3894 return res;
3897 static struct ibm_struct uwb_driver_data = {
3898 .name = "uwb",
3899 .exit = uwb_exit,
3900 .flags.experimental = 1,
3903 /*************************************************************************
3904 * Video subdriver
3907 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3909 enum video_access_mode {
3910 TPACPI_VIDEO_NONE = 0,
3911 TPACPI_VIDEO_570, /* 570 */
3912 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3913 TPACPI_VIDEO_NEW, /* all others */
3916 enum { /* video status flags, based on VIDEO_570 */
3917 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3918 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3919 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3922 enum { /* TPACPI_VIDEO_570 constants */
3923 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3924 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3925 * video_status_flags */
3926 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3927 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3930 static enum video_access_mode video_supported;
3931 static int video_orig_autosw;
3933 static int video_autosw_get(void);
3934 static int video_autosw_set(int enable);
3936 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3938 static int __init video_init(struct ibm_init_struct *iibm)
3940 int ivga;
3942 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3944 TPACPI_ACPIHANDLE_INIT(vid);
3945 TPACPI_ACPIHANDLE_INIT(vid2);
3947 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3948 /* G41, assume IVGA doesn't change */
3949 vid_handle = vid2_handle;
3951 if (!vid_handle)
3952 /* video switching not supported on R30, R31 */
3953 video_supported = TPACPI_VIDEO_NONE;
3954 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3955 /* 570 */
3956 video_supported = TPACPI_VIDEO_570;
3957 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3958 /* 600e/x, 770e, 770x */
3959 video_supported = TPACPI_VIDEO_770;
3960 else
3961 /* all others */
3962 video_supported = TPACPI_VIDEO_NEW;
3964 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3965 str_supported(video_supported != TPACPI_VIDEO_NONE),
3966 video_supported);
3968 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3971 static void video_exit(void)
3973 dbg_printk(TPACPI_DBG_EXIT,
3974 "restoring original video autoswitch mode\n");
3975 if (video_autosw_set(video_orig_autosw))
3976 printk(TPACPI_ERR "error while trying to restore original "
3977 "video autoswitch mode\n");
3980 static int video_outputsw_get(void)
3982 int status = 0;
3983 int i;
3985 switch (video_supported) {
3986 case TPACPI_VIDEO_570:
3987 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3988 TP_ACPI_VIDEO_570_PHSCMD))
3989 return -EIO;
3990 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3991 break;
3992 case TPACPI_VIDEO_770:
3993 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3994 return -EIO;
3995 if (i)
3996 status |= TP_ACPI_VIDEO_S_LCD;
3997 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3998 return -EIO;
3999 if (i)
4000 status |= TP_ACPI_VIDEO_S_CRT;
4001 break;
4002 case TPACPI_VIDEO_NEW:
4003 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4004 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4005 return -EIO;
4006 if (i)
4007 status |= TP_ACPI_VIDEO_S_CRT;
4009 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4010 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4011 return -EIO;
4012 if (i)
4013 status |= TP_ACPI_VIDEO_S_LCD;
4014 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4015 return -EIO;
4016 if (i)
4017 status |= TP_ACPI_VIDEO_S_DVI;
4018 break;
4019 default:
4020 return -ENOSYS;
4023 return status;
4026 static int video_outputsw_set(int status)
4028 int autosw;
4029 int res = 0;
4031 switch (video_supported) {
4032 case TPACPI_VIDEO_570:
4033 res = acpi_evalf(NULL, NULL,
4034 "\\_SB.PHS2", "vdd",
4035 TP_ACPI_VIDEO_570_PHS2CMD,
4036 status | TP_ACPI_VIDEO_570_PHS2SET);
4037 break;
4038 case TPACPI_VIDEO_770:
4039 autosw = video_autosw_get();
4040 if (autosw < 0)
4041 return autosw;
4043 res = video_autosw_set(1);
4044 if (res)
4045 return res;
4046 res = acpi_evalf(vid_handle, NULL,
4047 "ASWT", "vdd", status * 0x100, 0);
4048 if (!autosw && video_autosw_set(autosw)) {
4049 printk(TPACPI_ERR
4050 "video auto-switch left enabled due to error\n");
4051 return -EIO;
4053 break;
4054 case TPACPI_VIDEO_NEW:
4055 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4056 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4057 break;
4058 default:
4059 return -ENOSYS;
4062 return (res)? 0 : -EIO;
4065 static int video_autosw_get(void)
4067 int autosw = 0;
4069 switch (video_supported) {
4070 case TPACPI_VIDEO_570:
4071 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4072 return -EIO;
4073 break;
4074 case TPACPI_VIDEO_770:
4075 case TPACPI_VIDEO_NEW:
4076 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4077 return -EIO;
4078 break;
4079 default:
4080 return -ENOSYS;
4083 return autosw & 1;
4086 static int video_autosw_set(int enable)
4088 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4089 return -EIO;
4090 return 0;
4093 static int video_outputsw_cycle(void)
4095 int autosw = video_autosw_get();
4096 int res;
4098 if (autosw < 0)
4099 return autosw;
4101 switch (video_supported) {
4102 case TPACPI_VIDEO_570:
4103 res = video_autosw_set(1);
4104 if (res)
4105 return res;
4106 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4107 break;
4108 case TPACPI_VIDEO_770:
4109 case TPACPI_VIDEO_NEW:
4110 res = video_autosw_set(1);
4111 if (res)
4112 return res;
4113 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4114 break;
4115 default:
4116 return -ENOSYS;
4118 if (!autosw && video_autosw_set(autosw)) {
4119 printk(TPACPI_ERR
4120 "video auto-switch left enabled due to error\n");
4121 return -EIO;
4124 return (res)? 0 : -EIO;
4127 static int video_expand_toggle(void)
4129 switch (video_supported) {
4130 case TPACPI_VIDEO_570:
4131 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4132 0 : -EIO;
4133 case TPACPI_VIDEO_770:
4134 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4135 0 : -EIO;
4136 case TPACPI_VIDEO_NEW:
4137 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4138 0 : -EIO;
4139 default:
4140 return -ENOSYS;
4142 /* not reached */
4145 static int video_read(char *p)
4147 int status, autosw;
4148 int len = 0;
4150 if (video_supported == TPACPI_VIDEO_NONE) {
4151 len += sprintf(p + len, "status:\t\tnot supported\n");
4152 return len;
4155 status = video_outputsw_get();
4156 if (status < 0)
4157 return status;
4159 autosw = video_autosw_get();
4160 if (autosw < 0)
4161 return autosw;
4163 len += sprintf(p + len, "status:\t\tsupported\n");
4164 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4165 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4166 if (video_supported == TPACPI_VIDEO_NEW)
4167 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4168 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4169 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4170 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4171 if (video_supported == TPACPI_VIDEO_NEW)
4172 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4173 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4174 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4176 return len;
4179 static int video_write(char *buf)
4181 char *cmd;
4182 int enable, disable, status;
4183 int res;
4185 if (video_supported == TPACPI_VIDEO_NONE)
4186 return -ENODEV;
4188 enable = 0;
4189 disable = 0;
4191 while ((cmd = next_cmd(&buf))) {
4192 if (strlencmp(cmd, "lcd_enable") == 0) {
4193 enable |= TP_ACPI_VIDEO_S_LCD;
4194 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4195 disable |= TP_ACPI_VIDEO_S_LCD;
4196 } else if (strlencmp(cmd, "crt_enable") == 0) {
4197 enable |= TP_ACPI_VIDEO_S_CRT;
4198 } else if (strlencmp(cmd, "crt_disable") == 0) {
4199 disable |= TP_ACPI_VIDEO_S_CRT;
4200 } else if (video_supported == TPACPI_VIDEO_NEW &&
4201 strlencmp(cmd, "dvi_enable") == 0) {
4202 enable |= TP_ACPI_VIDEO_S_DVI;
4203 } else if (video_supported == TPACPI_VIDEO_NEW &&
4204 strlencmp(cmd, "dvi_disable") == 0) {
4205 disable |= TP_ACPI_VIDEO_S_DVI;
4206 } else if (strlencmp(cmd, "auto_enable") == 0) {
4207 res = video_autosw_set(1);
4208 if (res)
4209 return res;
4210 } else if (strlencmp(cmd, "auto_disable") == 0) {
4211 res = video_autosw_set(0);
4212 if (res)
4213 return res;
4214 } else if (strlencmp(cmd, "video_switch") == 0) {
4215 res = video_outputsw_cycle();
4216 if (res)
4217 return res;
4218 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4219 res = video_expand_toggle();
4220 if (res)
4221 return res;
4222 } else
4223 return -EINVAL;
4226 if (enable || disable) {
4227 status = video_outputsw_get();
4228 if (status < 0)
4229 return status;
4230 res = video_outputsw_set((status & ~disable) | enable);
4231 if (res)
4232 return res;
4235 return 0;
4238 static struct ibm_struct video_driver_data = {
4239 .name = "video",
4240 .read = video_read,
4241 .write = video_write,
4242 .exit = video_exit,
4245 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4247 /*************************************************************************
4248 * Light (thinklight) subdriver
4251 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4252 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4254 static int light_get_status(void)
4256 int status = 0;
4258 if (tp_features.light_status) {
4259 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4260 return -EIO;
4261 return (!!status);
4264 return -ENXIO;
4267 static int light_set_status(int status)
4269 int rc;
4271 if (tp_features.light) {
4272 if (cmos_handle) {
4273 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4274 (status)?
4275 TP_CMOS_THINKLIGHT_ON :
4276 TP_CMOS_THINKLIGHT_OFF);
4277 } else {
4278 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4279 (status)? 1 : 0);
4281 return (rc)? 0 : -EIO;
4284 return -ENXIO;
4287 static void light_set_status_worker(struct work_struct *work)
4289 struct tpacpi_led_classdev *data =
4290 container_of(work, struct tpacpi_led_classdev, work);
4292 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4293 light_set_status((data->new_state != TPACPI_LED_OFF));
4296 static void light_sysfs_set(struct led_classdev *led_cdev,
4297 enum led_brightness brightness)
4299 struct tpacpi_led_classdev *data =
4300 container_of(led_cdev,
4301 struct tpacpi_led_classdev,
4302 led_classdev);
4303 data->new_state = (brightness != LED_OFF) ?
4304 TPACPI_LED_ON : TPACPI_LED_OFF;
4305 queue_work(tpacpi_wq, &data->work);
4308 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4310 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4313 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4314 .led_classdev = {
4315 .name = "tpacpi::thinklight",
4316 .brightness_set = &light_sysfs_set,
4317 .brightness_get = &light_sysfs_get,
4321 static int __init light_init(struct ibm_init_struct *iibm)
4323 int rc;
4325 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4327 TPACPI_ACPIHANDLE_INIT(ledb);
4328 TPACPI_ACPIHANDLE_INIT(lght);
4329 TPACPI_ACPIHANDLE_INIT(cmos);
4330 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4332 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4333 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4335 if (tp_features.light)
4336 /* light status not supported on
4337 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4338 tp_features.light_status =
4339 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4341 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4342 str_supported(tp_features.light),
4343 str_supported(tp_features.light_status));
4345 if (!tp_features.light)
4346 return 1;
4348 rc = led_classdev_register(&tpacpi_pdev->dev,
4349 &tpacpi_led_thinklight.led_classdev);
4351 if (rc < 0) {
4352 tp_features.light = 0;
4353 tp_features.light_status = 0;
4354 } else {
4355 rc = 0;
4358 return rc;
4361 static void light_exit(void)
4363 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4364 if (work_pending(&tpacpi_led_thinklight.work))
4365 flush_workqueue(tpacpi_wq);
4368 static int light_read(char *p)
4370 int len = 0;
4371 int status;
4373 if (!tp_features.light) {
4374 len += sprintf(p + len, "status:\t\tnot supported\n");
4375 } else if (!tp_features.light_status) {
4376 len += sprintf(p + len, "status:\t\tunknown\n");
4377 len += sprintf(p + len, "commands:\ton, off\n");
4378 } else {
4379 status = light_get_status();
4380 if (status < 0)
4381 return status;
4382 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4383 len += sprintf(p + len, "commands:\ton, off\n");
4386 return len;
4389 static int light_write(char *buf)
4391 char *cmd;
4392 int newstatus = 0;
4394 if (!tp_features.light)
4395 return -ENODEV;
4397 while ((cmd = next_cmd(&buf))) {
4398 if (strlencmp(cmd, "on") == 0) {
4399 newstatus = 1;
4400 } else if (strlencmp(cmd, "off") == 0) {
4401 newstatus = 0;
4402 } else
4403 return -EINVAL;
4406 return light_set_status(newstatus);
4409 static struct ibm_struct light_driver_data = {
4410 .name = "light",
4411 .read = light_read,
4412 .write = light_write,
4413 .exit = light_exit,
4416 /*************************************************************************
4417 * Dock subdriver
4420 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4422 static void dock_notify(struct ibm_struct *ibm, u32 event);
4423 static int dock_read(char *p);
4424 static int dock_write(char *buf);
4426 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4427 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4428 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4429 "\\_SB.PCI.ISA.SLCE", /* 570 */
4430 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4432 /* don't list other alternatives as we install a notify handler on the 570 */
4433 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4435 static const struct acpi_device_id ibm_pci_device_ids[] = {
4436 {PCI_ROOT_HID_STRING, 0},
4437 {"", 0},
4440 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4442 .notify = dock_notify,
4443 .handle = &dock_handle,
4444 .type = ACPI_SYSTEM_NOTIFY,
4447 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4448 * We just use it to get notifications of dock hotplug
4449 * in very old thinkpads */
4450 .hid = ibm_pci_device_ids,
4451 .notify = dock_notify,
4452 .handle = &pci_handle,
4453 .type = ACPI_SYSTEM_NOTIFY,
4457 static struct ibm_struct dock_driver_data[2] = {
4459 .name = "dock",
4460 .read = dock_read,
4461 .write = dock_write,
4462 .acpi = &ibm_dock_acpidriver[0],
4465 .name = "dock",
4466 .acpi = &ibm_dock_acpidriver[1],
4470 #define dock_docked() (_sta(dock_handle) & 1)
4472 static int __init dock_init(struct ibm_init_struct *iibm)
4474 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4476 TPACPI_ACPIHANDLE_INIT(dock);
4478 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4479 str_supported(dock_handle != NULL));
4481 return (dock_handle)? 0 : 1;
4484 static int __init dock_init2(struct ibm_init_struct *iibm)
4486 int dock2_needed;
4488 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4490 if (dock_driver_data[0].flags.acpi_driver_registered &&
4491 dock_driver_data[0].flags.acpi_notify_installed) {
4492 TPACPI_ACPIHANDLE_INIT(pci);
4493 dock2_needed = (pci_handle != NULL);
4494 vdbg_printk(TPACPI_DBG_INIT,
4495 "dock PCI handler for the TP 570 is %s\n",
4496 str_supported(dock2_needed));
4497 } else {
4498 vdbg_printk(TPACPI_DBG_INIT,
4499 "dock subdriver part 2 not required\n");
4500 dock2_needed = 0;
4503 return (dock2_needed)? 0 : 1;
4506 static void dock_notify(struct ibm_struct *ibm, u32 event)
4508 int docked = dock_docked();
4509 int pci = ibm->acpi->hid && ibm->acpi->device &&
4510 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4511 int data;
4513 if (event == 1 && !pci) /* 570 */
4514 data = 1; /* button */
4515 else if (event == 1 && pci) /* 570 */
4516 data = 3; /* dock */
4517 else if (event == 3 && docked)
4518 data = 1; /* button */
4519 else if (event == 3 && !docked)
4520 data = 2; /* undock */
4521 else if (event == 0 && docked)
4522 data = 3; /* dock */
4523 else {
4524 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4525 event, _sta(dock_handle));
4526 data = 0; /* unknown */
4528 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4529 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4530 dev_name(&ibm->acpi->device->dev),
4531 event, data);
4534 static int dock_read(char *p)
4536 int len = 0;
4537 int docked = dock_docked();
4539 if (!dock_handle)
4540 len += sprintf(p + len, "status:\t\tnot supported\n");
4541 else if (!docked)
4542 len += sprintf(p + len, "status:\t\tundocked\n");
4543 else {
4544 len += sprintf(p + len, "status:\t\tdocked\n");
4545 len += sprintf(p + len, "commands:\tdock, undock\n");
4548 return len;
4551 static int dock_write(char *buf)
4553 char *cmd;
4555 if (!dock_docked())
4556 return -ENODEV;
4558 while ((cmd = next_cmd(&buf))) {
4559 if (strlencmp(cmd, "undock") == 0) {
4560 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4561 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4562 return -EIO;
4563 } else if (strlencmp(cmd, "dock") == 0) {
4564 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4565 return -EIO;
4566 } else
4567 return -EINVAL;
4570 return 0;
4573 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4575 /*************************************************************************
4576 * Bay subdriver
4579 #ifdef CONFIG_THINKPAD_ACPI_BAY
4581 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4582 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4583 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4584 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4585 ); /* A21e, R30, R31 */
4586 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4587 "_EJ0", /* all others */
4588 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4589 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4590 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4591 ); /* all others */
4592 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4593 "_EJ0", /* 770x */
4594 ); /* all others */
4596 static int __init bay_init(struct ibm_init_struct *iibm)
4598 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4600 TPACPI_ACPIHANDLE_INIT(bay);
4601 if (bay_handle)
4602 TPACPI_ACPIHANDLE_INIT(bay_ej);
4603 TPACPI_ACPIHANDLE_INIT(bay2);
4604 if (bay2_handle)
4605 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4607 tp_features.bay_status = bay_handle &&
4608 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4609 tp_features.bay_status2 = bay2_handle &&
4610 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4612 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4613 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4614 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4615 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4617 vdbg_printk(TPACPI_DBG_INIT,
4618 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4619 str_supported(tp_features.bay_status),
4620 str_supported(tp_features.bay_eject),
4621 str_supported(tp_features.bay_status2),
4622 str_supported(tp_features.bay_eject2));
4624 return (tp_features.bay_status || tp_features.bay_eject ||
4625 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4628 static void bay_notify(struct ibm_struct *ibm, u32 event)
4630 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4631 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4632 dev_name(&ibm->acpi->device->dev),
4633 event, 0);
4636 #define bay_occupied(b) (_sta(b##_handle) & 1)
4638 static int bay_read(char *p)
4640 int len = 0;
4641 int occupied = bay_occupied(bay);
4642 int occupied2 = bay_occupied(bay2);
4643 int eject, eject2;
4645 len += sprintf(p + len, "status:\t\t%s\n",
4646 tp_features.bay_status ?
4647 (occupied ? "occupied" : "unoccupied") :
4648 "not supported");
4649 if (tp_features.bay_status2)
4650 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4651 "occupied" : "unoccupied");
4653 eject = tp_features.bay_eject && occupied;
4654 eject2 = tp_features.bay_eject2 && occupied2;
4656 if (eject && eject2)
4657 len += sprintf(p + len, "commands:\teject, eject2\n");
4658 else if (eject)
4659 len += sprintf(p + len, "commands:\teject\n");
4660 else if (eject2)
4661 len += sprintf(p + len, "commands:\teject2\n");
4663 return len;
4666 static int bay_write(char *buf)
4668 char *cmd;
4670 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4671 return -ENODEV;
4673 while ((cmd = next_cmd(&buf))) {
4674 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4675 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4676 return -EIO;
4677 } else if (tp_features.bay_eject2 &&
4678 strlencmp(cmd, "eject2") == 0) {
4679 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4680 return -EIO;
4681 } else
4682 return -EINVAL;
4685 return 0;
4688 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4689 .notify = bay_notify,
4690 .handle = &bay_handle,
4691 .type = ACPI_SYSTEM_NOTIFY,
4694 static struct ibm_struct bay_driver_data = {
4695 .name = "bay",
4696 .read = bay_read,
4697 .write = bay_write,
4698 .acpi = &ibm_bay_acpidriver,
4701 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4703 /*************************************************************************
4704 * CMOS subdriver
4707 /* sysfs cmos_command -------------------------------------------------- */
4708 static ssize_t cmos_command_store(struct device *dev,
4709 struct device_attribute *attr,
4710 const char *buf, size_t count)
4712 unsigned long cmos_cmd;
4713 int res;
4715 if (parse_strtoul(buf, 21, &cmos_cmd))
4716 return -EINVAL;
4718 res = issue_thinkpad_cmos_command(cmos_cmd);
4719 return (res)? res : count;
4722 static struct device_attribute dev_attr_cmos_command =
4723 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4725 /* --------------------------------------------------------------------- */
4727 static int __init cmos_init(struct ibm_init_struct *iibm)
4729 int res;
4731 vdbg_printk(TPACPI_DBG_INIT,
4732 "initializing cmos commands subdriver\n");
4734 TPACPI_ACPIHANDLE_INIT(cmos);
4736 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4737 str_supported(cmos_handle != NULL));
4739 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4740 if (res)
4741 return res;
4743 return (cmos_handle)? 0 : 1;
4746 static void cmos_exit(void)
4748 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4751 static int cmos_read(char *p)
4753 int len = 0;
4755 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4756 R30, R31, T20-22, X20-21 */
4757 if (!cmos_handle)
4758 len += sprintf(p + len, "status:\t\tnot supported\n");
4759 else {
4760 len += sprintf(p + len, "status:\t\tsupported\n");
4761 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4764 return len;
4767 static int cmos_write(char *buf)
4769 char *cmd;
4770 int cmos_cmd, res;
4772 while ((cmd = next_cmd(&buf))) {
4773 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4774 cmos_cmd >= 0 && cmos_cmd <= 21) {
4775 /* cmos_cmd set */
4776 } else
4777 return -EINVAL;
4779 res = issue_thinkpad_cmos_command(cmos_cmd);
4780 if (res)
4781 return res;
4784 return 0;
4787 static struct ibm_struct cmos_driver_data = {
4788 .name = "cmos",
4789 .read = cmos_read,
4790 .write = cmos_write,
4791 .exit = cmos_exit,
4794 /*************************************************************************
4795 * LED subdriver
4798 enum led_access_mode {
4799 TPACPI_LED_NONE = 0,
4800 TPACPI_LED_570, /* 570 */
4801 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4802 TPACPI_LED_NEW, /* all others */
4805 enum { /* For TPACPI_LED_OLD */
4806 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4807 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4808 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4811 static enum led_access_mode led_supported;
4813 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4814 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4815 /* T20-22, X20-21 */
4816 "LED", /* all others */
4817 ); /* R30, R31 */
4819 #define TPACPI_LED_NUMLEDS 16
4820 static struct tpacpi_led_classdev *tpacpi_leds;
4821 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4822 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4823 /* there's a limit of 19 chars + NULL before 2.6.26 */
4824 "tpacpi::power",
4825 "tpacpi:orange:batt",
4826 "tpacpi:green:batt",
4827 "tpacpi::dock_active",
4828 "tpacpi::bay_active",
4829 "tpacpi::dock_batt",
4830 "tpacpi::unknown_led",
4831 "tpacpi::standby",
4832 "tpacpi::dock_status1",
4833 "tpacpi::dock_status2",
4834 "tpacpi::unknown_led2",
4835 "tpacpi::unknown_led3",
4836 "tpacpi::thinkvantage",
4838 #define TPACPI_SAFE_LEDS 0x1081U
4840 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4842 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4843 return false;
4844 #else
4845 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4846 #endif
4849 static int led_get_status(const unsigned int led)
4851 int status;
4852 enum led_status_t led_s;
4854 switch (led_supported) {
4855 case TPACPI_LED_570:
4856 if (!acpi_evalf(ec_handle,
4857 &status, "GLED", "dd", 1 << led))
4858 return -EIO;
4859 led_s = (status == 0)?
4860 TPACPI_LED_OFF :
4861 ((status == 1)?
4862 TPACPI_LED_ON :
4863 TPACPI_LED_BLINK);
4864 tpacpi_led_state_cache[led] = led_s;
4865 return led_s;
4866 default:
4867 return -ENXIO;
4870 /* not reached */
4873 static int led_set_status(const unsigned int led,
4874 const enum led_status_t ledstatus)
4876 /* off, on, blink. Index is led_status_t */
4877 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4878 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4880 int rc = 0;
4882 switch (led_supported) {
4883 case TPACPI_LED_570:
4884 /* 570 */
4885 if (unlikely(led > 7))
4886 return -EINVAL;
4887 if (unlikely(tpacpi_is_led_restricted(led)))
4888 return -EPERM;
4889 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4890 (1 << led), led_sled_arg1[ledstatus]))
4891 rc = -EIO;
4892 break;
4893 case TPACPI_LED_OLD:
4894 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4895 if (unlikely(led > 7))
4896 return -EINVAL;
4897 if (unlikely(tpacpi_is_led_restricted(led)))
4898 return -EPERM;
4899 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4900 if (rc >= 0)
4901 rc = ec_write(TPACPI_LED_EC_HLBL,
4902 (ledstatus == TPACPI_LED_BLINK) << led);
4903 if (rc >= 0)
4904 rc = ec_write(TPACPI_LED_EC_HLCL,
4905 (ledstatus != TPACPI_LED_OFF) << led);
4906 break;
4907 case TPACPI_LED_NEW:
4908 /* all others */
4909 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4910 return -EINVAL;
4911 if (unlikely(tpacpi_is_led_restricted(led)))
4912 return -EPERM;
4913 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4914 led, led_led_arg1[ledstatus]))
4915 rc = -EIO;
4916 break;
4917 default:
4918 rc = -ENXIO;
4921 if (!rc)
4922 tpacpi_led_state_cache[led] = ledstatus;
4924 return rc;
4927 static void led_set_status_worker(struct work_struct *work)
4929 struct tpacpi_led_classdev *data =
4930 container_of(work, struct tpacpi_led_classdev, work);
4932 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4933 led_set_status(data->led, data->new_state);
4936 static void led_sysfs_set(struct led_classdev *led_cdev,
4937 enum led_brightness brightness)
4939 struct tpacpi_led_classdev *data = container_of(led_cdev,
4940 struct tpacpi_led_classdev, led_classdev);
4942 if (brightness == LED_OFF)
4943 data->new_state = TPACPI_LED_OFF;
4944 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4945 data->new_state = TPACPI_LED_ON;
4946 else
4947 data->new_state = TPACPI_LED_BLINK;
4949 queue_work(tpacpi_wq, &data->work);
4952 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4953 unsigned long *delay_on, unsigned long *delay_off)
4955 struct tpacpi_led_classdev *data = container_of(led_cdev,
4956 struct tpacpi_led_classdev, led_classdev);
4958 /* Can we choose the flash rate? */
4959 if (*delay_on == 0 && *delay_off == 0) {
4960 /* yes. set them to the hardware blink rate (1 Hz) */
4961 *delay_on = 500; /* ms */
4962 *delay_off = 500; /* ms */
4963 } else if ((*delay_on != 500) || (*delay_off != 500))
4964 return -EINVAL;
4966 data->new_state = TPACPI_LED_BLINK;
4967 queue_work(tpacpi_wq, &data->work);
4969 return 0;
4972 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4974 int rc;
4976 struct tpacpi_led_classdev *data = container_of(led_cdev,
4977 struct tpacpi_led_classdev, led_classdev);
4979 rc = led_get_status(data->led);
4981 if (rc == TPACPI_LED_OFF || rc < 0)
4982 rc = LED_OFF; /* no error handling in led class :( */
4983 else
4984 rc = LED_FULL;
4986 return rc;
4989 static void led_exit(void)
4991 unsigned int i;
4993 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4994 if (tpacpi_leds[i].led_classdev.name)
4995 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4998 kfree(tpacpi_leds);
5001 static int __init tpacpi_init_led(unsigned int led)
5003 int rc;
5005 tpacpi_leds[led].led = led;
5007 /* LEDs with no name don't get registered */
5008 if (!tpacpi_led_names[led])
5009 return 0;
5011 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5012 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5013 if (led_supported == TPACPI_LED_570)
5014 tpacpi_leds[led].led_classdev.brightness_get =
5015 &led_sysfs_get;
5017 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5019 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5021 rc = led_classdev_register(&tpacpi_pdev->dev,
5022 &tpacpi_leds[led].led_classdev);
5023 if (rc < 0)
5024 tpacpi_leds[led].led_classdev.name = NULL;
5026 return rc;
5029 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5030 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5031 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5032 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5034 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5035 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5036 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5037 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5038 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5039 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5040 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5041 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5043 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5044 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5045 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5046 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5047 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5049 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5050 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5051 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5052 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5054 /* (1) - may have excess leds enabled on MSB */
5056 /* Defaults (order matters, keep last, don't reorder!) */
5057 { /* Lenovo */
5058 .vendor = PCI_VENDOR_ID_LENOVO,
5059 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5060 .quirks = 0x1fffU,
5062 { /* IBM ThinkPads with no EC version string */
5063 .vendor = PCI_VENDOR_ID_IBM,
5064 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5065 .quirks = 0x00ffU,
5067 { /* IBM ThinkPads with EC version string */
5068 .vendor = PCI_VENDOR_ID_IBM,
5069 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5070 .quirks = 0x00bfU,
5074 #undef TPACPI_LEDQ_IBM
5075 #undef TPACPI_LEDQ_LNV
5077 static int __init led_init(struct ibm_init_struct *iibm)
5079 unsigned int i;
5080 int rc;
5081 unsigned long useful_leds;
5083 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5085 TPACPI_ACPIHANDLE_INIT(led);
5087 if (!led_handle)
5088 /* led not supported on R30, R31 */
5089 led_supported = TPACPI_LED_NONE;
5090 else if (strlencmp(led_path, "SLED") == 0)
5091 /* 570 */
5092 led_supported = TPACPI_LED_570;
5093 else if (strlencmp(led_path, "SYSL") == 0)
5094 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5095 led_supported = TPACPI_LED_OLD;
5096 else
5097 /* all others */
5098 led_supported = TPACPI_LED_NEW;
5100 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5101 str_supported(led_supported), led_supported);
5103 if (led_supported == TPACPI_LED_NONE)
5104 return 1;
5106 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5107 GFP_KERNEL);
5108 if (!tpacpi_leds) {
5109 printk(TPACPI_ERR "Out of memory for LED data\n");
5110 return -ENOMEM;
5113 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5114 ARRAY_SIZE(led_useful_qtable));
5116 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5117 if (!tpacpi_is_led_restricted(i) &&
5118 test_bit(i, &useful_leds)) {
5119 rc = tpacpi_init_led(i);
5120 if (rc < 0) {
5121 led_exit();
5122 return rc;
5127 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5128 printk(TPACPI_NOTICE
5129 "warning: userspace override of important "
5130 "firmware LEDs is enabled\n");
5131 #endif
5132 return 0;
5135 #define str_led_status(s) \
5136 ((s) == TPACPI_LED_OFF ? "off" : \
5137 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5139 static int led_read(char *p)
5141 int len = 0;
5143 if (!led_supported) {
5144 len += sprintf(p + len, "status:\t\tnot supported\n");
5145 return len;
5147 len += sprintf(p + len, "status:\t\tsupported\n");
5149 if (led_supported == TPACPI_LED_570) {
5150 /* 570 */
5151 int i, status;
5152 for (i = 0; i < 8; i++) {
5153 status = led_get_status(i);
5154 if (status < 0)
5155 return -EIO;
5156 len += sprintf(p + len, "%d:\t\t%s\n",
5157 i, str_led_status(status));
5161 len += sprintf(p + len, "commands:\t"
5162 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5164 return len;
5167 static int led_write(char *buf)
5169 char *cmd;
5170 int led, rc;
5171 enum led_status_t s;
5173 if (!led_supported)
5174 return -ENODEV;
5176 while ((cmd = next_cmd(&buf))) {
5177 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5178 return -EINVAL;
5180 if (strstr(cmd, "off")) {
5181 s = TPACPI_LED_OFF;
5182 } else if (strstr(cmd, "on")) {
5183 s = TPACPI_LED_ON;
5184 } else if (strstr(cmd, "blink")) {
5185 s = TPACPI_LED_BLINK;
5186 } else {
5187 return -EINVAL;
5190 rc = led_set_status(led, s);
5191 if (rc < 0)
5192 return rc;
5195 return 0;
5198 static struct ibm_struct led_driver_data = {
5199 .name = "led",
5200 .read = led_read,
5201 .write = led_write,
5202 .exit = led_exit,
5205 /*************************************************************************
5206 * Beep subdriver
5209 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5211 #define TPACPI_BEEP_Q1 0x0001
5213 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5214 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5215 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5218 static int __init beep_init(struct ibm_init_struct *iibm)
5220 unsigned long quirks;
5222 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5224 TPACPI_ACPIHANDLE_INIT(beep);
5226 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5227 str_supported(beep_handle != NULL));
5229 quirks = tpacpi_check_quirks(beep_quirk_table,
5230 ARRAY_SIZE(beep_quirk_table));
5232 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5234 return (beep_handle)? 0 : 1;
5237 static int beep_read(char *p)
5239 int len = 0;
5241 if (!beep_handle)
5242 len += sprintf(p + len, "status:\t\tnot supported\n");
5243 else {
5244 len += sprintf(p + len, "status:\t\tsupported\n");
5245 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5248 return len;
5251 static int beep_write(char *buf)
5253 char *cmd;
5254 int beep_cmd;
5256 if (!beep_handle)
5257 return -ENODEV;
5259 while ((cmd = next_cmd(&buf))) {
5260 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5261 beep_cmd >= 0 && beep_cmd <= 17) {
5262 /* beep_cmd set */
5263 } else
5264 return -EINVAL;
5265 if (tp_features.beep_needs_two_args) {
5266 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5267 beep_cmd, 0))
5268 return -EIO;
5269 } else {
5270 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5271 beep_cmd))
5272 return -EIO;
5276 return 0;
5279 static struct ibm_struct beep_driver_data = {
5280 .name = "beep",
5281 .read = beep_read,
5282 .write = beep_write,
5285 /*************************************************************************
5286 * Thermal subdriver
5289 enum thermal_access_mode {
5290 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5291 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5292 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5293 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5294 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5297 enum { /* TPACPI_THERMAL_TPEC_* */
5298 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5299 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5300 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5303 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5304 struct ibm_thermal_sensors_struct {
5305 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5308 static enum thermal_access_mode thermal_read_mode;
5310 /* idx is zero-based */
5311 static int thermal_get_sensor(int idx, s32 *value)
5313 int t;
5314 s8 tmp;
5315 char tmpi[5];
5317 t = TP_EC_THERMAL_TMP0;
5319 switch (thermal_read_mode) {
5320 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5321 case TPACPI_THERMAL_TPEC_16:
5322 if (idx >= 8 && idx <= 15) {
5323 t = TP_EC_THERMAL_TMP8;
5324 idx -= 8;
5326 /* fallthrough */
5327 #endif
5328 case TPACPI_THERMAL_TPEC_8:
5329 if (idx <= 7) {
5330 if (!acpi_ec_read(t + idx, &tmp))
5331 return -EIO;
5332 *value = tmp * 1000;
5333 return 0;
5335 break;
5337 case TPACPI_THERMAL_ACPI_UPDT:
5338 if (idx <= 7) {
5339 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5340 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5341 return -EIO;
5342 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5343 return -EIO;
5344 *value = (t - 2732) * 100;
5345 return 0;
5347 break;
5349 case TPACPI_THERMAL_ACPI_TMP07:
5350 if (idx <= 7) {
5351 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5352 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5353 return -EIO;
5354 if (t > 127 || t < -127)
5355 t = TP_EC_THERMAL_TMP_NA;
5356 *value = t * 1000;
5357 return 0;
5359 break;
5361 case TPACPI_THERMAL_NONE:
5362 default:
5363 return -ENOSYS;
5366 return -EINVAL;
5369 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5371 int res, i;
5372 int n;
5374 n = 8;
5375 i = 0;
5377 if (!s)
5378 return -EINVAL;
5380 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5381 n = 16;
5383 for (i = 0 ; i < n; i++) {
5384 res = thermal_get_sensor(i, &s->temp[i]);
5385 if (res)
5386 return res;
5389 return n;
5392 /* sysfs temp##_input -------------------------------------------------- */
5394 static ssize_t thermal_temp_input_show(struct device *dev,
5395 struct device_attribute *attr,
5396 char *buf)
5398 struct sensor_device_attribute *sensor_attr =
5399 to_sensor_dev_attr(attr);
5400 int idx = sensor_attr->index;
5401 s32 value;
5402 int res;
5404 res = thermal_get_sensor(idx, &value);
5405 if (res)
5406 return res;
5407 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5408 return -ENXIO;
5410 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5413 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5414 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5415 thermal_temp_input_show, NULL, _idxB)
5417 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5418 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5419 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5420 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5421 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5422 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5423 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5424 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5425 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5426 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5427 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5428 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5429 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5430 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5431 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5432 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5433 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5436 #define THERMAL_ATTRS(X) \
5437 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5439 static struct attribute *thermal_temp_input_attr[] = {
5440 THERMAL_ATTRS(8),
5441 THERMAL_ATTRS(9),
5442 THERMAL_ATTRS(10),
5443 THERMAL_ATTRS(11),
5444 THERMAL_ATTRS(12),
5445 THERMAL_ATTRS(13),
5446 THERMAL_ATTRS(14),
5447 THERMAL_ATTRS(15),
5448 THERMAL_ATTRS(0),
5449 THERMAL_ATTRS(1),
5450 THERMAL_ATTRS(2),
5451 THERMAL_ATTRS(3),
5452 THERMAL_ATTRS(4),
5453 THERMAL_ATTRS(5),
5454 THERMAL_ATTRS(6),
5455 THERMAL_ATTRS(7),
5456 NULL
5459 static const struct attribute_group thermal_temp_input16_group = {
5460 .attrs = thermal_temp_input_attr
5463 static const struct attribute_group thermal_temp_input8_group = {
5464 .attrs = &thermal_temp_input_attr[8]
5467 #undef THERMAL_SENSOR_ATTR_TEMP
5468 #undef THERMAL_ATTRS
5470 /* --------------------------------------------------------------------- */
5472 static int __init thermal_init(struct ibm_init_struct *iibm)
5474 u8 t, ta1, ta2;
5475 int i;
5476 int acpi_tmp7;
5477 int res;
5479 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5481 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5483 if (thinkpad_id.ec_model) {
5485 * Direct EC access mode: sensors at registers
5486 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5487 * non-implemented, thermal sensors return 0x80 when
5488 * not available
5491 ta1 = ta2 = 0;
5492 for (i = 0; i < 8; i++) {
5493 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5494 ta1 |= t;
5495 } else {
5496 ta1 = 0;
5497 break;
5499 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5500 ta2 |= t;
5501 } else {
5502 ta1 = 0;
5503 break;
5506 if (ta1 == 0) {
5507 /* This is sheer paranoia, but we handle it anyway */
5508 if (acpi_tmp7) {
5509 printk(TPACPI_ERR
5510 "ThinkPad ACPI EC access misbehaving, "
5511 "falling back to ACPI TMPx access "
5512 "mode\n");
5513 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5514 } else {
5515 printk(TPACPI_ERR
5516 "ThinkPad ACPI EC access misbehaving, "
5517 "disabling thermal sensors access\n");
5518 thermal_read_mode = TPACPI_THERMAL_NONE;
5520 } else {
5521 thermal_read_mode =
5522 (ta2 != 0) ?
5523 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5525 } else if (acpi_tmp7) {
5526 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5527 /* 600e/x, 770e, 770x */
5528 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5529 } else {
5530 /* Standard ACPI TMPx access, max 8 sensors */
5531 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5533 } else {
5534 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5535 thermal_read_mode = TPACPI_THERMAL_NONE;
5538 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5539 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5540 thermal_read_mode);
5542 switch (thermal_read_mode) {
5543 case TPACPI_THERMAL_TPEC_16:
5544 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5545 &thermal_temp_input16_group);
5546 if (res)
5547 return res;
5548 break;
5549 case TPACPI_THERMAL_TPEC_8:
5550 case TPACPI_THERMAL_ACPI_TMP07:
5551 case TPACPI_THERMAL_ACPI_UPDT:
5552 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5553 &thermal_temp_input8_group);
5554 if (res)
5555 return res;
5556 break;
5557 case TPACPI_THERMAL_NONE:
5558 default:
5559 return 1;
5562 return 0;
5565 static void thermal_exit(void)
5567 switch (thermal_read_mode) {
5568 case TPACPI_THERMAL_TPEC_16:
5569 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5570 &thermal_temp_input16_group);
5571 break;
5572 case TPACPI_THERMAL_TPEC_8:
5573 case TPACPI_THERMAL_ACPI_TMP07:
5574 case TPACPI_THERMAL_ACPI_UPDT:
5575 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5576 &thermal_temp_input16_group);
5577 break;
5578 case TPACPI_THERMAL_NONE:
5579 default:
5580 break;
5584 static int thermal_read(char *p)
5586 int len = 0;
5587 int n, i;
5588 struct ibm_thermal_sensors_struct t;
5590 n = thermal_get_sensors(&t);
5591 if (unlikely(n < 0))
5592 return n;
5594 len += sprintf(p + len, "temperatures:\t");
5596 if (n > 0) {
5597 for (i = 0; i < (n - 1); i++)
5598 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5599 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5600 } else
5601 len += sprintf(p + len, "not supported\n");
5603 return len;
5606 static struct ibm_struct thermal_driver_data = {
5607 .name = "thermal",
5608 .read = thermal_read,
5609 .exit = thermal_exit,
5612 /*************************************************************************
5613 * EC Dump subdriver
5616 static u8 ecdump_regs[256];
5618 static int ecdump_read(char *p)
5620 int len = 0;
5621 int i, j;
5622 u8 v;
5624 len += sprintf(p + len, "EC "
5625 " +00 +01 +02 +03 +04 +05 +06 +07"
5626 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5627 for (i = 0; i < 256; i += 16) {
5628 len += sprintf(p + len, "EC 0x%02x:", i);
5629 for (j = 0; j < 16; j++) {
5630 if (!acpi_ec_read(i + j, &v))
5631 break;
5632 if (v != ecdump_regs[i + j])
5633 len += sprintf(p + len, " *%02x", v);
5634 else
5635 len += sprintf(p + len, " %02x", v);
5636 ecdump_regs[i + j] = v;
5638 len += sprintf(p + len, "\n");
5639 if (j != 16)
5640 break;
5643 /* These are way too dangerous to advertise openly... */
5644 #if 0
5645 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5646 " (<offset> is 00-ff, <value> is 00-ff)\n");
5647 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5648 " (<offset> is 00-ff, <value> is 0-255)\n");
5649 #endif
5650 return len;
5653 static int ecdump_write(char *buf)
5655 char *cmd;
5656 int i, v;
5658 while ((cmd = next_cmd(&buf))) {
5659 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5660 /* i and v set */
5661 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5662 /* i and v set */
5663 } else
5664 return -EINVAL;
5665 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5666 if (!acpi_ec_write(i, v))
5667 return -EIO;
5668 } else
5669 return -EINVAL;
5672 return 0;
5675 static struct ibm_struct ecdump_driver_data = {
5676 .name = "ecdump",
5677 .read = ecdump_read,
5678 .write = ecdump_write,
5679 .flags.experimental = 1,
5682 /*************************************************************************
5683 * Backlight/brightness subdriver
5686 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5689 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5690 * CMOS NVRAM byte 0x5E, bits 0-3.
5692 * EC HBRV (0x31) has the following layout
5693 * Bit 7: unknown function
5694 * Bit 6: unknown function
5695 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5696 * Bit 4: must be set to zero to avoid problems
5697 * Bit 3-0: backlight brightness level
5699 * brightness_get_raw returns status data in the HBRV layout
5701 * WARNING: The X61 has been verified to use HBRV for something else, so
5702 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5703 * testing on the very early *60 Lenovo models...
5706 enum {
5707 TP_EC_BACKLIGHT = 0x31,
5709 /* TP_EC_BACKLIGHT bitmasks */
5710 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5711 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5712 TP_EC_BACKLIGHT_MAPSW = 0x20,
5715 enum tpacpi_brightness_access_mode {
5716 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5717 TPACPI_BRGHT_MODE_EC, /* EC control */
5718 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5719 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5720 TPACPI_BRGHT_MODE_MAX
5723 static struct backlight_device *ibm_backlight_device;
5725 static enum tpacpi_brightness_access_mode brightness_mode =
5726 TPACPI_BRGHT_MODE_MAX;
5728 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5730 static struct mutex brightness_mutex;
5732 /* NVRAM brightness access,
5733 * call with brightness_mutex held! */
5734 static unsigned int tpacpi_brightness_nvram_get(void)
5736 u8 lnvram;
5738 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5739 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5740 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5741 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5743 return lnvram;
5746 static void tpacpi_brightness_checkpoint_nvram(void)
5748 u8 lec = 0;
5749 u8 b_nvram;
5751 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5752 return;
5754 vdbg_printk(TPACPI_DBG_BRGHT,
5755 "trying to checkpoint backlight level to NVRAM...\n");
5757 if (mutex_lock_killable(&brightness_mutex) < 0)
5758 return;
5760 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5761 goto unlock;
5762 lec &= TP_EC_BACKLIGHT_LVLMSK;
5763 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5765 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5766 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5767 /* NVRAM needs update */
5768 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5769 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5770 b_nvram |= lec;
5771 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5772 dbg_printk(TPACPI_DBG_BRGHT,
5773 "updated NVRAM backlight level to %u (0x%02x)\n",
5774 (unsigned int) lec, (unsigned int) b_nvram);
5775 } else
5776 vdbg_printk(TPACPI_DBG_BRGHT,
5777 "NVRAM backlight level already is %u (0x%02x)\n",
5778 (unsigned int) lec, (unsigned int) b_nvram);
5780 unlock:
5781 mutex_unlock(&brightness_mutex);
5785 /* call with brightness_mutex held! */
5786 static int tpacpi_brightness_get_raw(int *status)
5788 u8 lec = 0;
5790 switch (brightness_mode) {
5791 case TPACPI_BRGHT_MODE_UCMS_STEP:
5792 *status = tpacpi_brightness_nvram_get();
5793 return 0;
5794 case TPACPI_BRGHT_MODE_EC:
5795 case TPACPI_BRGHT_MODE_ECNVRAM:
5796 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5797 return -EIO;
5798 *status = lec;
5799 return 0;
5800 default:
5801 return -ENXIO;
5805 /* call with brightness_mutex held! */
5806 /* do NOT call with illegal backlight level value */
5807 static int tpacpi_brightness_set_ec(unsigned int value)
5809 u8 lec = 0;
5811 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5812 return -EIO;
5814 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5815 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5816 (value & TP_EC_BACKLIGHT_LVLMSK))))
5817 return -EIO;
5819 return 0;
5822 /* call with brightness_mutex held! */
5823 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5825 int cmos_cmd, inc;
5826 unsigned int current_value, i;
5828 current_value = tpacpi_brightness_nvram_get();
5830 if (value == current_value)
5831 return 0;
5833 cmos_cmd = (value > current_value) ?
5834 TP_CMOS_BRIGHTNESS_UP :
5835 TP_CMOS_BRIGHTNESS_DOWN;
5836 inc = (value > current_value) ? 1 : -1;
5838 for (i = current_value; i != value; i += inc)
5839 if (issue_thinkpad_cmos_command(cmos_cmd))
5840 return -EIO;
5842 return 0;
5845 /* May return EINTR which can always be mapped to ERESTARTSYS */
5846 static int brightness_set(unsigned int value)
5848 int res;
5850 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5851 value < 0)
5852 return -EINVAL;
5854 vdbg_printk(TPACPI_DBG_BRGHT,
5855 "set backlight level to %d\n", value);
5857 res = mutex_lock_killable(&brightness_mutex);
5858 if (res < 0)
5859 return res;
5861 switch (brightness_mode) {
5862 case TPACPI_BRGHT_MODE_EC:
5863 case TPACPI_BRGHT_MODE_ECNVRAM:
5864 res = tpacpi_brightness_set_ec(value);
5865 break;
5866 case TPACPI_BRGHT_MODE_UCMS_STEP:
5867 res = tpacpi_brightness_set_ucmsstep(value);
5868 break;
5869 default:
5870 res = -ENXIO;
5873 mutex_unlock(&brightness_mutex);
5874 return res;
5877 /* sysfs backlight class ----------------------------------------------- */
5879 static int brightness_update_status(struct backlight_device *bd)
5881 unsigned int level =
5882 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5883 bd->props.power == FB_BLANK_UNBLANK) ?
5884 bd->props.brightness : 0;
5886 dbg_printk(TPACPI_DBG_BRGHT,
5887 "backlight: attempt to set level to %d\n",
5888 level);
5890 /* it is the backlight class's job (caller) to handle
5891 * EINTR and other errors properly */
5892 return brightness_set(level);
5895 static int brightness_get(struct backlight_device *bd)
5897 int status, res;
5899 res = mutex_lock_killable(&brightness_mutex);
5900 if (res < 0)
5901 return 0;
5903 res = tpacpi_brightness_get_raw(&status);
5905 mutex_unlock(&brightness_mutex);
5907 if (res < 0)
5908 return 0;
5910 return status & TP_EC_BACKLIGHT_LVLMSK;
5913 static struct backlight_ops ibm_backlight_data = {
5914 .get_brightness = brightness_get,
5915 .update_status = brightness_update_status,
5918 /* --------------------------------------------------------------------- */
5920 static int __init brightness_init(struct ibm_init_struct *iibm)
5922 int b;
5924 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5926 mutex_init(&brightness_mutex);
5929 * We always attempt to detect acpi support, so as to switch
5930 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5931 * going to publish a backlight interface
5933 b = tpacpi_check_std_acpi_brightness_support();
5934 if (b > 0) {
5936 if (acpi_video_backlight_support()) {
5937 if (brightness_enable > 1) {
5938 printk(TPACPI_NOTICE
5939 "Standard ACPI backlight interface "
5940 "available, not loading native one.\n");
5941 return 1;
5942 } else if (brightness_enable == 1) {
5943 printk(TPACPI_NOTICE
5944 "Backlight control force enabled, even if standard "
5945 "ACPI backlight interface is available\n");
5947 } else {
5948 if (brightness_enable > 1) {
5949 printk(TPACPI_NOTICE
5950 "Standard ACPI backlight interface not "
5951 "available, thinkpad_acpi native "
5952 "brightness control enabled\n");
5957 if (!brightness_enable) {
5958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5959 "brightness support disabled by "
5960 "module parameter\n");
5961 return 1;
5964 if (b > 16) {
5965 printk(TPACPI_ERR
5966 "Unsupported brightness interface, "
5967 "please contact %s\n", TPACPI_MAIL);
5968 return 1;
5970 if (b == 16)
5971 tp_features.bright_16levels = 1;
5974 * Check for module parameter bogosity, note that we
5975 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5976 * able to detect "unspecified"
5978 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5979 return -EINVAL;
5981 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5982 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5983 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5984 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5986 * IBM models that define HBRV probably have
5987 * EC-based backlight level control
5989 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5990 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5991 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5992 else
5993 /* all other IBM ThinkPads */
5994 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5995 } else
5996 /* All Lenovo ThinkPads */
5997 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5999 dbg_printk(TPACPI_DBG_BRGHT,
6000 "selected brightness_mode=%d\n",
6001 brightness_mode);
6004 /* Safety */
6005 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6006 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6007 brightness_mode == TPACPI_BRGHT_MODE_EC))
6008 return -EINVAL;
6010 if (tpacpi_brightness_get_raw(&b) < 0)
6011 return 1;
6013 if (tp_features.bright_16levels)
6014 printk(TPACPI_INFO
6015 "detected a 16-level brightness capable ThinkPad\n");
6017 ibm_backlight_device = backlight_device_register(
6018 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6019 &ibm_backlight_data);
6020 if (IS_ERR(ibm_backlight_device)) {
6021 printk(TPACPI_ERR "Could not register backlight device\n");
6022 return PTR_ERR(ibm_backlight_device);
6024 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6025 "brightness is supported\n");
6027 ibm_backlight_device->props.max_brightness =
6028 (tp_features.bright_16levels)? 15 : 7;
6029 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6030 backlight_update_status(ibm_backlight_device);
6032 return 0;
6035 static void brightness_suspend(pm_message_t state)
6037 tpacpi_brightness_checkpoint_nvram();
6040 static void brightness_shutdown(void)
6042 tpacpi_brightness_checkpoint_nvram();
6045 static void brightness_exit(void)
6047 if (ibm_backlight_device) {
6048 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6049 "calling backlight_device_unregister()\n");
6050 backlight_device_unregister(ibm_backlight_device);
6053 tpacpi_brightness_checkpoint_nvram();
6056 static int brightness_read(char *p)
6058 int len = 0;
6059 int level;
6061 level = brightness_get(NULL);
6062 if (level < 0) {
6063 len += sprintf(p + len, "level:\t\tunreadable\n");
6064 } else {
6065 len += sprintf(p + len, "level:\t\t%d\n", level);
6066 len += sprintf(p + len, "commands:\tup, down\n");
6067 len += sprintf(p + len, "commands:\tlevel <level>"
6068 " (<level> is 0-%d)\n",
6069 (tp_features.bright_16levels) ? 15 : 7);
6072 return len;
6075 static int brightness_write(char *buf)
6077 int level;
6078 int rc;
6079 char *cmd;
6080 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6082 level = brightness_get(NULL);
6083 if (level < 0)
6084 return level;
6086 while ((cmd = next_cmd(&buf))) {
6087 if (strlencmp(cmd, "up") == 0) {
6088 if (level < max_level)
6089 level++;
6090 } else if (strlencmp(cmd, "down") == 0) {
6091 if (level > 0)
6092 level--;
6093 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6094 level >= 0 && level <= max_level) {
6095 /* new level set */
6096 } else
6097 return -EINVAL;
6100 tpacpi_disclose_usertask("procfs brightness",
6101 "set level to %d\n", level);
6104 * Now we know what the final level should be, so we try to set it.
6105 * Doing it this way makes the syscall restartable in case of EINTR
6107 rc = brightness_set(level);
6108 return (rc == -EINTR)? ERESTARTSYS : rc;
6111 static struct ibm_struct brightness_driver_data = {
6112 .name = "brightness",
6113 .read = brightness_read,
6114 .write = brightness_write,
6115 .exit = brightness_exit,
6116 .suspend = brightness_suspend,
6117 .shutdown = brightness_shutdown,
6120 /*************************************************************************
6121 * Volume subdriver
6124 static int volume_offset = 0x30;
6126 static int volume_read(char *p)
6128 int len = 0;
6129 u8 level;
6131 if (!acpi_ec_read(volume_offset, &level)) {
6132 len += sprintf(p + len, "level:\t\tunreadable\n");
6133 } else {
6134 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6135 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6136 len += sprintf(p + len, "commands:\tup, down, mute\n");
6137 len += sprintf(p + len, "commands:\tlevel <level>"
6138 " (<level> is 0-15)\n");
6141 return len;
6144 static int volume_write(char *buf)
6146 int cmos_cmd, inc, i;
6147 u8 level, mute;
6148 int new_level, new_mute;
6149 char *cmd;
6151 while ((cmd = next_cmd(&buf))) {
6152 if (!acpi_ec_read(volume_offset, &level))
6153 return -EIO;
6154 new_mute = mute = level & 0x40;
6155 new_level = level = level & 0xf;
6157 if (strlencmp(cmd, "up") == 0) {
6158 if (mute)
6159 new_mute = 0;
6160 else
6161 new_level = level == 15 ? 15 : level + 1;
6162 } else if (strlencmp(cmd, "down") == 0) {
6163 if (mute)
6164 new_mute = 0;
6165 else
6166 new_level = level == 0 ? 0 : level - 1;
6167 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6168 new_level >= 0 && new_level <= 15) {
6169 /* new_level set */
6170 } else if (strlencmp(cmd, "mute") == 0) {
6171 new_mute = 0x40;
6172 } else
6173 return -EINVAL;
6175 if (new_level != level) {
6176 /* mute doesn't change */
6178 cmos_cmd = (new_level > level) ?
6179 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6180 inc = new_level > level ? 1 : -1;
6182 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6183 !acpi_ec_write(volume_offset, level)))
6184 return -EIO;
6186 for (i = level; i != new_level; i += inc)
6187 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6188 !acpi_ec_write(volume_offset, i + inc))
6189 return -EIO;
6191 if (mute &&
6192 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6193 !acpi_ec_write(volume_offset, new_level + mute))) {
6194 return -EIO;
6198 if (new_mute != mute) {
6199 /* level doesn't change */
6201 cmos_cmd = (new_mute) ?
6202 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6204 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6205 !acpi_ec_write(volume_offset, level + new_mute))
6206 return -EIO;
6210 return 0;
6213 static struct ibm_struct volume_driver_data = {
6214 .name = "volume",
6215 .read = volume_read,
6216 .write = volume_write,
6219 /*************************************************************************
6220 * Fan subdriver
6224 * FAN ACCESS MODES
6226 * TPACPI_FAN_RD_ACPI_GFAN:
6227 * ACPI GFAN method: returns fan level
6229 * see TPACPI_FAN_WR_ACPI_SFAN
6230 * EC 0x2f (HFSP) not available if GFAN exists
6232 * TPACPI_FAN_WR_ACPI_SFAN:
6233 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6235 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6236 * it for writing.
6238 * TPACPI_FAN_WR_TPEC:
6239 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6240 * Supported on almost all ThinkPads
6242 * Fan speed changes of any sort (including those caused by the
6243 * disengaged mode) are usually done slowly by the firmware as the
6244 * maximum ammount of fan duty cycle change per second seems to be
6245 * limited.
6247 * Reading is not available if GFAN exists.
6248 * Writing is not available if SFAN exists.
6250 * Bits
6251 * 7 automatic mode engaged;
6252 * (default operation mode of the ThinkPad)
6253 * fan level is ignored in this mode.
6254 * 6 full speed mode (takes precedence over bit 7);
6255 * not available on all thinkpads. May disable
6256 * the tachometer while the fan controller ramps up
6257 * the speed (which can take up to a few *minutes*).
6258 * Speeds up fan to 100% duty-cycle, which is far above
6259 * the standard RPM levels. It is not impossible that
6260 * it could cause hardware damage.
6261 * 5-3 unused in some models. Extra bits for fan level
6262 * in others, but still useless as all values above
6263 * 7 map to the same speed as level 7 in these models.
6264 * 2-0 fan level (0..7 usually)
6265 * 0x00 = stop
6266 * 0x07 = max (set when temperatures critical)
6267 * Some ThinkPads may have other levels, see
6268 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6270 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6271 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6272 * does so, its initial value is meaningless (0x07).
6274 * For firmware bugs, refer to:
6275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6277 * ----
6279 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6280 * Main fan tachometer reading (in RPM)
6282 * This register is present on all ThinkPads with a new-style EC, and
6283 * it is known not to be present on the A21m/e, and T22, as there is
6284 * something else in offset 0x84 according to the ACPI DSDT. Other
6285 * ThinkPads from this same time period (and earlier) probably lack the
6286 * tachometer as well.
6288 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6289 * was never fixed by IBM to report the EC firmware version string
6290 * probably support the tachometer (like the early X models), so
6291 * detecting it is quite hard. We need more data to know for sure.
6293 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6294 * might result.
6296 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6297 * mode.
6299 * For firmware bugs, refer to:
6300 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6302 * ----
6304 * ThinkPad EC register 0x31 bit 0 (only on select models)
6306 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6307 * show the speed of the main fan. When bit 0 of EC register 0x31
6308 * is one, the tachometer registers show the speed of the auxiliary
6309 * fan.
6311 * Fan control seems to affect both fans, regardless of the state
6312 * of this bit.
6314 * So far, only the firmware for the X60/X61 non-tablet versions
6315 * seem to support this (firmware TP-7M).
6317 * TPACPI_FAN_WR_ACPI_FANS:
6318 * ThinkPad X31, X40, X41. Not available in the X60.
6320 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6321 * high speed. ACPI DSDT seems to map these three speeds to levels
6322 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6323 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6325 * The speeds are stored on handles
6326 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6328 * There are three default speed sets, acessible as handles:
6329 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6331 * ACPI DSDT switches which set is in use depending on various
6332 * factors.
6334 * TPACPI_FAN_WR_TPEC is also available and should be used to
6335 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6336 * but the ACPI tables just mention level 7.
6339 enum { /* Fan control constants */
6340 fan_status_offset = 0x2f, /* EC register 0x2f */
6341 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6342 * 0x84 must be read before 0x85 */
6343 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6344 bit 0 selects which fan is active */
6346 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6347 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6349 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6352 enum fan_status_access_mode {
6353 TPACPI_FAN_NONE = 0, /* No fan status or control */
6354 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6355 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6358 enum fan_control_access_mode {
6359 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6360 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6361 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6362 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6365 enum fan_control_commands {
6366 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6367 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6368 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6369 * and also watchdog cmd */
6372 static int fan_control_allowed;
6374 static enum fan_status_access_mode fan_status_access_mode;
6375 static enum fan_control_access_mode fan_control_access_mode;
6376 static enum fan_control_commands fan_control_commands;
6378 static u8 fan_control_initial_status;
6379 static u8 fan_control_desired_level;
6380 static u8 fan_control_resume_level;
6381 static int fan_watchdog_maxinterval;
6383 static struct mutex fan_mutex;
6385 static void fan_watchdog_fire(struct work_struct *ignored);
6386 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6388 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6389 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6390 "\\FSPD", /* 600e/x, 770e, 770x */
6391 ); /* all others */
6392 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6393 "JFNS", /* 770x-JL */
6394 ); /* all others */
6397 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6398 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6399 * be in auto mode (0x80).
6401 * This is corrected by any write to HFSP either by the driver, or
6402 * by the firmware.
6404 * We assume 0x07 really means auto mode while this quirk is active,
6405 * as this is far more likely than the ThinkPad being in level 7,
6406 * which is only used by the firmware during thermal emergencies.
6408 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6409 * TP-70 (T43, R52), which are known to be buggy.
6412 static void fan_quirk1_setup(void)
6414 if (fan_control_initial_status == 0x07) {
6415 printk(TPACPI_NOTICE
6416 "fan_init: initial fan status is unknown, "
6417 "assuming it is in auto mode\n");
6418 tp_features.fan_ctrl_status_undef = 1;
6422 static void fan_quirk1_handle(u8 *fan_status)
6424 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6425 if (*fan_status != fan_control_initial_status) {
6426 /* something changed the HFSP regisnter since
6427 * driver init time, so it is not undefined
6428 * anymore */
6429 tp_features.fan_ctrl_status_undef = 0;
6430 } else {
6431 /* Return most likely status. In fact, it
6432 * might be the only possible status */
6433 *fan_status = TP_EC_FAN_AUTO;
6438 /* Select main fan on X60/X61, NOOP on others */
6439 static bool fan_select_fan1(void)
6441 if (tp_features.second_fan) {
6442 u8 val;
6444 if (ec_read(fan_select_offset, &val) < 0)
6445 return false;
6446 val &= 0xFEU;
6447 if (ec_write(fan_select_offset, val) < 0)
6448 return false;
6450 return true;
6453 /* Select secondary fan on X60/X61 */
6454 static bool fan_select_fan2(void)
6456 u8 val;
6458 if (!tp_features.second_fan)
6459 return false;
6461 if (ec_read(fan_select_offset, &val) < 0)
6462 return false;
6463 val |= 0x01U;
6464 if (ec_write(fan_select_offset, val) < 0)
6465 return false;
6467 return true;
6471 * Call with fan_mutex held
6473 static void fan_update_desired_level(u8 status)
6475 if ((status &
6476 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6477 if (status > 7)
6478 fan_control_desired_level = 7;
6479 else
6480 fan_control_desired_level = status;
6484 static int fan_get_status(u8 *status)
6486 u8 s;
6488 /* TODO:
6489 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6491 switch (fan_status_access_mode) {
6492 case TPACPI_FAN_RD_ACPI_GFAN:
6493 /* 570, 600e/x, 770e, 770x */
6495 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6496 return -EIO;
6498 if (likely(status))
6499 *status = s & 0x07;
6501 break;
6503 case TPACPI_FAN_RD_TPEC:
6504 /* all except 570, 600e/x, 770e, 770x */
6505 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6506 return -EIO;
6508 if (likely(status)) {
6509 *status = s;
6510 fan_quirk1_handle(status);
6513 break;
6515 default:
6516 return -ENXIO;
6519 return 0;
6522 static int fan_get_status_safe(u8 *status)
6524 int rc;
6525 u8 s;
6527 if (mutex_lock_killable(&fan_mutex))
6528 return -ERESTARTSYS;
6529 rc = fan_get_status(&s);
6530 if (!rc)
6531 fan_update_desired_level(s);
6532 mutex_unlock(&fan_mutex);
6534 if (status)
6535 *status = s;
6537 return rc;
6540 static int fan_get_speed(unsigned int *speed)
6542 u8 hi, lo;
6544 switch (fan_status_access_mode) {
6545 case TPACPI_FAN_RD_TPEC:
6546 /* all except 570, 600e/x, 770e, 770x */
6547 if (unlikely(!fan_select_fan1()))
6548 return -EIO;
6549 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6550 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6551 return -EIO;
6553 if (likely(speed))
6554 *speed = (hi << 8) | lo;
6556 break;
6558 default:
6559 return -ENXIO;
6562 return 0;
6565 static int fan2_get_speed(unsigned int *speed)
6567 u8 hi, lo;
6568 bool rc;
6570 switch (fan_status_access_mode) {
6571 case TPACPI_FAN_RD_TPEC:
6572 /* all except 570, 600e/x, 770e, 770x */
6573 if (unlikely(!fan_select_fan2()))
6574 return -EIO;
6575 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6576 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6577 fan_select_fan1(); /* play it safe */
6578 if (rc)
6579 return -EIO;
6581 if (likely(speed))
6582 *speed = (hi << 8) | lo;
6584 break;
6586 default:
6587 return -ENXIO;
6590 return 0;
6593 static int fan_set_level(int level)
6595 if (!fan_control_allowed)
6596 return -EPERM;
6598 switch (fan_control_access_mode) {
6599 case TPACPI_FAN_WR_ACPI_SFAN:
6600 if (level >= 0 && level <= 7) {
6601 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6602 return -EIO;
6603 } else
6604 return -EINVAL;
6605 break;
6607 case TPACPI_FAN_WR_ACPI_FANS:
6608 case TPACPI_FAN_WR_TPEC:
6609 if (!(level & TP_EC_FAN_AUTO) &&
6610 !(level & TP_EC_FAN_FULLSPEED) &&
6611 ((level < 0) || (level > 7)))
6612 return -EINVAL;
6614 /* safety net should the EC not support AUTO
6615 * or FULLSPEED mode bits and just ignore them */
6616 if (level & TP_EC_FAN_FULLSPEED)
6617 level |= 7; /* safety min speed 7 */
6618 else if (level & TP_EC_FAN_AUTO)
6619 level |= 4; /* safety min speed 4 */
6621 if (!acpi_ec_write(fan_status_offset, level))
6622 return -EIO;
6623 else
6624 tp_features.fan_ctrl_status_undef = 0;
6625 break;
6627 default:
6628 return -ENXIO;
6631 vdbg_printk(TPACPI_DBG_FAN,
6632 "fan control: set fan control register to 0x%02x\n", level);
6633 return 0;
6636 static int fan_set_level_safe(int level)
6638 int rc;
6640 if (!fan_control_allowed)
6641 return -EPERM;
6643 if (mutex_lock_killable(&fan_mutex))
6644 return -ERESTARTSYS;
6646 if (level == TPACPI_FAN_LAST_LEVEL)
6647 level = fan_control_desired_level;
6649 rc = fan_set_level(level);
6650 if (!rc)
6651 fan_update_desired_level(level);
6653 mutex_unlock(&fan_mutex);
6654 return rc;
6657 static int fan_set_enable(void)
6659 u8 s;
6660 int rc;
6662 if (!fan_control_allowed)
6663 return -EPERM;
6665 if (mutex_lock_killable(&fan_mutex))
6666 return -ERESTARTSYS;
6668 switch (fan_control_access_mode) {
6669 case TPACPI_FAN_WR_ACPI_FANS:
6670 case TPACPI_FAN_WR_TPEC:
6671 rc = fan_get_status(&s);
6672 if (rc < 0)
6673 break;
6675 /* Don't go out of emergency fan mode */
6676 if (s != 7) {
6677 s &= 0x07;
6678 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6681 if (!acpi_ec_write(fan_status_offset, s))
6682 rc = -EIO;
6683 else {
6684 tp_features.fan_ctrl_status_undef = 0;
6685 rc = 0;
6687 break;
6689 case TPACPI_FAN_WR_ACPI_SFAN:
6690 rc = fan_get_status(&s);
6691 if (rc < 0)
6692 break;
6694 s &= 0x07;
6696 /* Set fan to at least level 4 */
6697 s |= 4;
6699 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6700 rc = -EIO;
6701 else
6702 rc = 0;
6703 break;
6705 default:
6706 rc = -ENXIO;
6709 mutex_unlock(&fan_mutex);
6711 if (!rc)
6712 vdbg_printk(TPACPI_DBG_FAN,
6713 "fan control: set fan control register to 0x%02x\n",
6715 return rc;
6718 static int fan_set_disable(void)
6720 int rc;
6722 if (!fan_control_allowed)
6723 return -EPERM;
6725 if (mutex_lock_killable(&fan_mutex))
6726 return -ERESTARTSYS;
6728 rc = 0;
6729 switch (fan_control_access_mode) {
6730 case TPACPI_FAN_WR_ACPI_FANS:
6731 case TPACPI_FAN_WR_TPEC:
6732 if (!acpi_ec_write(fan_status_offset, 0x00))
6733 rc = -EIO;
6734 else {
6735 fan_control_desired_level = 0;
6736 tp_features.fan_ctrl_status_undef = 0;
6738 break;
6740 case TPACPI_FAN_WR_ACPI_SFAN:
6741 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6742 rc = -EIO;
6743 else
6744 fan_control_desired_level = 0;
6745 break;
6747 default:
6748 rc = -ENXIO;
6751 if (!rc)
6752 vdbg_printk(TPACPI_DBG_FAN,
6753 "fan control: set fan control register to 0\n");
6755 mutex_unlock(&fan_mutex);
6756 return rc;
6759 static int fan_set_speed(int speed)
6761 int rc;
6763 if (!fan_control_allowed)
6764 return -EPERM;
6766 if (mutex_lock_killable(&fan_mutex))
6767 return -ERESTARTSYS;
6769 rc = 0;
6770 switch (fan_control_access_mode) {
6771 case TPACPI_FAN_WR_ACPI_FANS:
6772 if (speed >= 0 && speed <= 65535) {
6773 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6774 speed, speed, speed))
6775 rc = -EIO;
6776 } else
6777 rc = -EINVAL;
6778 break;
6780 default:
6781 rc = -ENXIO;
6784 mutex_unlock(&fan_mutex);
6785 return rc;
6788 static void fan_watchdog_reset(void)
6790 static int fan_watchdog_active;
6792 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6793 return;
6795 if (fan_watchdog_active)
6796 cancel_delayed_work(&fan_watchdog_task);
6798 if (fan_watchdog_maxinterval > 0 &&
6799 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6800 fan_watchdog_active = 1;
6801 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6802 msecs_to_jiffies(fan_watchdog_maxinterval
6803 * 1000))) {
6804 printk(TPACPI_ERR
6805 "failed to queue the fan watchdog, "
6806 "watchdog will not trigger\n");
6808 } else
6809 fan_watchdog_active = 0;
6812 static void fan_watchdog_fire(struct work_struct *ignored)
6814 int rc;
6816 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6817 return;
6819 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6820 rc = fan_set_enable();
6821 if (rc < 0) {
6822 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6823 "will try again later...\n", -rc);
6824 /* reschedule for later */
6825 fan_watchdog_reset();
6830 * SYSFS fan layout: hwmon compatible (device)
6832 * pwm*_enable:
6833 * 0: "disengaged" mode
6834 * 1: manual mode
6835 * 2: native EC "auto" mode (recommended, hardware default)
6837 * pwm*: set speed in manual mode, ignored otherwise.
6838 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6839 * interpolation.
6841 * fan*_input: tachometer reading, RPM
6844 * SYSFS fan layout: extensions
6846 * fan_watchdog (driver):
6847 * fan watchdog interval in seconds, 0 disables (default), max 120
6850 /* sysfs fan pwm1_enable ----------------------------------------------- */
6851 static ssize_t fan_pwm1_enable_show(struct device *dev,
6852 struct device_attribute *attr,
6853 char *buf)
6855 int res, mode;
6856 u8 status;
6858 res = fan_get_status_safe(&status);
6859 if (res)
6860 return res;
6862 if (status & TP_EC_FAN_FULLSPEED) {
6863 mode = 0;
6864 } else if (status & TP_EC_FAN_AUTO) {
6865 mode = 2;
6866 } else
6867 mode = 1;
6869 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6872 static ssize_t fan_pwm1_enable_store(struct device *dev,
6873 struct device_attribute *attr,
6874 const char *buf, size_t count)
6876 unsigned long t;
6877 int res, level;
6879 if (parse_strtoul(buf, 2, &t))
6880 return -EINVAL;
6882 tpacpi_disclose_usertask("hwmon pwm1_enable",
6883 "set fan mode to %lu\n", t);
6885 switch (t) {
6886 case 0:
6887 level = TP_EC_FAN_FULLSPEED;
6888 break;
6889 case 1:
6890 level = TPACPI_FAN_LAST_LEVEL;
6891 break;
6892 case 2:
6893 level = TP_EC_FAN_AUTO;
6894 break;
6895 case 3:
6896 /* reserved for software-controlled auto mode */
6897 return -ENOSYS;
6898 default:
6899 return -EINVAL;
6902 res = fan_set_level_safe(level);
6903 if (res == -ENXIO)
6904 return -EINVAL;
6905 else if (res < 0)
6906 return res;
6908 fan_watchdog_reset();
6910 return count;
6913 static struct device_attribute dev_attr_fan_pwm1_enable =
6914 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6915 fan_pwm1_enable_show, fan_pwm1_enable_store);
6917 /* sysfs fan pwm1 ------------------------------------------------------ */
6918 static ssize_t fan_pwm1_show(struct device *dev,
6919 struct device_attribute *attr,
6920 char *buf)
6922 int res;
6923 u8 status;
6925 res = fan_get_status_safe(&status);
6926 if (res)
6927 return res;
6929 if ((status &
6930 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6931 status = fan_control_desired_level;
6933 if (status > 7)
6934 status = 7;
6936 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6939 static ssize_t fan_pwm1_store(struct device *dev,
6940 struct device_attribute *attr,
6941 const char *buf, size_t count)
6943 unsigned long s;
6944 int rc;
6945 u8 status, newlevel;
6947 if (parse_strtoul(buf, 255, &s))
6948 return -EINVAL;
6950 tpacpi_disclose_usertask("hwmon pwm1",
6951 "set fan speed to %lu\n", s);
6953 /* scale down from 0-255 to 0-7 */
6954 newlevel = (s >> 5) & 0x07;
6956 if (mutex_lock_killable(&fan_mutex))
6957 return -ERESTARTSYS;
6959 rc = fan_get_status(&status);
6960 if (!rc && (status &
6961 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6962 rc = fan_set_level(newlevel);
6963 if (rc == -ENXIO)
6964 rc = -EINVAL;
6965 else if (!rc) {
6966 fan_update_desired_level(newlevel);
6967 fan_watchdog_reset();
6971 mutex_unlock(&fan_mutex);
6972 return (rc)? rc : count;
6975 static struct device_attribute dev_attr_fan_pwm1 =
6976 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6977 fan_pwm1_show, fan_pwm1_store);
6979 /* sysfs fan fan1_input ------------------------------------------------ */
6980 static ssize_t fan_fan1_input_show(struct device *dev,
6981 struct device_attribute *attr,
6982 char *buf)
6984 int res;
6985 unsigned int speed;
6987 res = fan_get_speed(&speed);
6988 if (res < 0)
6989 return res;
6991 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6994 static struct device_attribute dev_attr_fan_fan1_input =
6995 __ATTR(fan1_input, S_IRUGO,
6996 fan_fan1_input_show, NULL);
6998 /* sysfs fan fan2_input ------------------------------------------------ */
6999 static ssize_t fan_fan2_input_show(struct device *dev,
7000 struct device_attribute *attr,
7001 char *buf)
7003 int res;
7004 unsigned int speed;
7006 res = fan2_get_speed(&speed);
7007 if (res < 0)
7008 return res;
7010 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7013 static struct device_attribute dev_attr_fan_fan2_input =
7014 __ATTR(fan2_input, S_IRUGO,
7015 fan_fan2_input_show, NULL);
7017 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7018 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7019 char *buf)
7021 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7024 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7025 const char *buf, size_t count)
7027 unsigned long t;
7029 if (parse_strtoul(buf, 120, &t))
7030 return -EINVAL;
7032 if (!fan_control_allowed)
7033 return -EPERM;
7035 fan_watchdog_maxinterval = t;
7036 fan_watchdog_reset();
7038 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7040 return count;
7043 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7044 fan_fan_watchdog_show, fan_fan_watchdog_store);
7046 /* --------------------------------------------------------------------- */
7047 static struct attribute *fan_attributes[] = {
7048 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7049 &dev_attr_fan_fan1_input.attr,
7050 NULL, /* for fan2_input */
7051 NULL
7054 static const struct attribute_group fan_attr_group = {
7055 .attrs = fan_attributes,
7058 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7059 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7061 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7062 { .vendor = PCI_VENDOR_ID_IBM, \
7063 .bios = TPACPI_MATCH_ANY, \
7064 .ec = TPID(__id1, __id2), \
7065 .quirks = __quirks }
7067 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7068 { .vendor = PCI_VENDOR_ID_LENOVO, \
7069 .bios = TPACPI_MATCH_ANY, \
7070 .ec = TPID(__id1, __id2), \
7071 .quirks = __quirks }
7073 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7074 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7075 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7076 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7077 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7078 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7081 #undef TPACPI_FAN_QL
7082 #undef TPACPI_FAN_QI
7084 static int __init fan_init(struct ibm_init_struct *iibm)
7086 int rc;
7087 unsigned long quirks;
7089 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7090 "initializing fan subdriver\n");
7092 mutex_init(&fan_mutex);
7093 fan_status_access_mode = TPACPI_FAN_NONE;
7094 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7095 fan_control_commands = 0;
7096 fan_watchdog_maxinterval = 0;
7097 tp_features.fan_ctrl_status_undef = 0;
7098 tp_features.second_fan = 0;
7099 fan_control_desired_level = 7;
7101 TPACPI_ACPIHANDLE_INIT(fans);
7102 TPACPI_ACPIHANDLE_INIT(gfan);
7103 TPACPI_ACPIHANDLE_INIT(sfan);
7105 quirks = tpacpi_check_quirks(fan_quirk_table,
7106 ARRAY_SIZE(fan_quirk_table));
7108 if (gfan_handle) {
7109 /* 570, 600e/x, 770e, 770x */
7110 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7111 } else {
7112 /* all other ThinkPads: note that even old-style
7113 * ThinkPad ECs supports the fan control register */
7114 if (likely(acpi_ec_read(fan_status_offset,
7115 &fan_control_initial_status))) {
7116 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7117 if (quirks & TPACPI_FAN_Q1)
7118 fan_quirk1_setup();
7119 if (quirks & TPACPI_FAN_2FAN) {
7120 tp_features.second_fan = 1;
7121 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7122 "secondary fan support enabled\n");
7124 } else {
7125 printk(TPACPI_ERR
7126 "ThinkPad ACPI EC access misbehaving, "
7127 "fan status and control unavailable\n");
7128 return 1;
7132 if (sfan_handle) {
7133 /* 570, 770x-JL */
7134 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7135 fan_control_commands |=
7136 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7137 } else {
7138 if (!gfan_handle) {
7139 /* gfan without sfan means no fan control */
7140 /* all other models implement TP EC 0x2f control */
7142 if (fans_handle) {
7143 /* X31, X40, X41 */
7144 fan_control_access_mode =
7145 TPACPI_FAN_WR_ACPI_FANS;
7146 fan_control_commands |=
7147 TPACPI_FAN_CMD_SPEED |
7148 TPACPI_FAN_CMD_LEVEL |
7149 TPACPI_FAN_CMD_ENABLE;
7150 } else {
7151 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7152 fan_control_commands |=
7153 TPACPI_FAN_CMD_LEVEL |
7154 TPACPI_FAN_CMD_ENABLE;
7159 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7160 "fan is %s, modes %d, %d\n",
7161 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7162 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7163 fan_status_access_mode, fan_control_access_mode);
7165 /* fan control master switch */
7166 if (!fan_control_allowed) {
7167 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7168 fan_control_commands = 0;
7169 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7170 "fan control features disabled by parameter\n");
7173 /* update fan_control_desired_level */
7174 if (fan_status_access_mode != TPACPI_FAN_NONE)
7175 fan_get_status_safe(NULL);
7177 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7178 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7179 if (tp_features.second_fan) {
7180 /* attach second fan tachometer */
7181 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7182 &dev_attr_fan_fan2_input.attr;
7184 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7185 &fan_attr_group);
7186 if (rc < 0)
7187 return rc;
7189 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7190 &driver_attr_fan_watchdog);
7191 if (rc < 0) {
7192 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7193 &fan_attr_group);
7194 return rc;
7196 return 0;
7197 } else
7198 return 1;
7201 static void fan_exit(void)
7203 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7204 "cancelling any pending fan watchdog tasks\n");
7206 /* FIXME: can we really do this unconditionally? */
7207 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7208 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7209 &driver_attr_fan_watchdog);
7211 cancel_delayed_work(&fan_watchdog_task);
7212 flush_workqueue(tpacpi_wq);
7215 static void fan_suspend(pm_message_t state)
7217 int rc;
7219 if (!fan_control_allowed)
7220 return;
7222 /* Store fan status in cache */
7223 fan_control_resume_level = 0;
7224 rc = fan_get_status_safe(&fan_control_resume_level);
7225 if (rc < 0)
7226 printk(TPACPI_NOTICE
7227 "failed to read fan level for later "
7228 "restore during resume: %d\n", rc);
7230 /* if it is undefined, don't attempt to restore it.
7231 * KEEP THIS LAST */
7232 if (tp_features.fan_ctrl_status_undef)
7233 fan_control_resume_level = 0;
7236 static void fan_resume(void)
7238 u8 current_level = 7;
7239 bool do_set = false;
7240 int rc;
7242 /* DSDT *always* updates status on resume */
7243 tp_features.fan_ctrl_status_undef = 0;
7245 if (!fan_control_allowed ||
7246 !fan_control_resume_level ||
7247 (fan_get_status_safe(&current_level) < 0))
7248 return;
7250 switch (fan_control_access_mode) {
7251 case TPACPI_FAN_WR_ACPI_SFAN:
7252 /* never decrease fan level */
7253 do_set = (fan_control_resume_level > current_level);
7254 break;
7255 case TPACPI_FAN_WR_ACPI_FANS:
7256 case TPACPI_FAN_WR_TPEC:
7257 /* never decrease fan level, scale is:
7258 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7260 * We expect the firmware to set either 7 or AUTO, but we
7261 * handle FULLSPEED out of paranoia.
7263 * So, we can safely only restore FULLSPEED or 7, anything
7264 * else could slow the fan. Restoring AUTO is useless, at
7265 * best that's exactly what the DSDT already set (it is the
7266 * slower it uses).
7268 * Always keep in mind that the DSDT *will* have set the
7269 * fans to what the vendor supposes is the best level. We
7270 * muck with it only to speed the fan up.
7272 if (fan_control_resume_level != 7 &&
7273 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7274 return;
7275 else
7276 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7277 (current_level != fan_control_resume_level);
7278 break;
7279 default:
7280 return;
7282 if (do_set) {
7283 printk(TPACPI_NOTICE
7284 "restoring fan level to 0x%02x\n",
7285 fan_control_resume_level);
7286 rc = fan_set_level_safe(fan_control_resume_level);
7287 if (rc < 0)
7288 printk(TPACPI_NOTICE
7289 "failed to restore fan level: %d\n", rc);
7293 static int fan_read(char *p)
7295 int len = 0;
7296 int rc;
7297 u8 status;
7298 unsigned int speed = 0;
7300 switch (fan_status_access_mode) {
7301 case TPACPI_FAN_RD_ACPI_GFAN:
7302 /* 570, 600e/x, 770e, 770x */
7303 rc = fan_get_status_safe(&status);
7304 if (rc < 0)
7305 return rc;
7307 len += sprintf(p + len, "status:\t\t%s\n"
7308 "level:\t\t%d\n",
7309 (status != 0) ? "enabled" : "disabled", status);
7310 break;
7312 case TPACPI_FAN_RD_TPEC:
7313 /* all except 570, 600e/x, 770e, 770x */
7314 rc = fan_get_status_safe(&status);
7315 if (rc < 0)
7316 return rc;
7318 len += sprintf(p + len, "status:\t\t%s\n",
7319 (status != 0) ? "enabled" : "disabled");
7321 rc = fan_get_speed(&speed);
7322 if (rc < 0)
7323 return rc;
7325 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7327 if (status & TP_EC_FAN_FULLSPEED)
7328 /* Disengaged mode takes precedence */
7329 len += sprintf(p + len, "level:\t\tdisengaged\n");
7330 else if (status & TP_EC_FAN_AUTO)
7331 len += sprintf(p + len, "level:\t\tauto\n");
7332 else
7333 len += sprintf(p + len, "level:\t\t%d\n", status);
7334 break;
7336 case TPACPI_FAN_NONE:
7337 default:
7338 len += sprintf(p + len, "status:\t\tnot supported\n");
7341 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7342 len += sprintf(p + len, "commands:\tlevel <level>");
7344 switch (fan_control_access_mode) {
7345 case TPACPI_FAN_WR_ACPI_SFAN:
7346 len += sprintf(p + len, " (<level> is 0-7)\n");
7347 break;
7349 default:
7350 len += sprintf(p + len, " (<level> is 0-7, "
7351 "auto, disengaged, full-speed)\n");
7352 break;
7356 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7357 len += sprintf(p + len, "commands:\tenable, disable\n"
7358 "commands:\twatchdog <timeout> (<timeout> "
7359 "is 0 (off), 1-120 (seconds))\n");
7361 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7362 len += sprintf(p + len, "commands:\tspeed <speed>"
7363 " (<speed> is 0-65535)\n");
7365 return len;
7368 static int fan_write_cmd_level(const char *cmd, int *rc)
7370 int level;
7372 if (strlencmp(cmd, "level auto") == 0)
7373 level = TP_EC_FAN_AUTO;
7374 else if ((strlencmp(cmd, "level disengaged") == 0) |
7375 (strlencmp(cmd, "level full-speed") == 0))
7376 level = TP_EC_FAN_FULLSPEED;
7377 else if (sscanf(cmd, "level %d", &level) != 1)
7378 return 0;
7380 *rc = fan_set_level_safe(level);
7381 if (*rc == -ENXIO)
7382 printk(TPACPI_ERR "level command accepted for unsupported "
7383 "access mode %d", fan_control_access_mode);
7384 else if (!*rc)
7385 tpacpi_disclose_usertask("procfs fan",
7386 "set level to %d\n", level);
7388 return 1;
7391 static int fan_write_cmd_enable(const char *cmd, int *rc)
7393 if (strlencmp(cmd, "enable") != 0)
7394 return 0;
7396 *rc = fan_set_enable();
7397 if (*rc == -ENXIO)
7398 printk(TPACPI_ERR "enable command accepted for unsupported "
7399 "access mode %d", fan_control_access_mode);
7400 else if (!*rc)
7401 tpacpi_disclose_usertask("procfs fan", "enable\n");
7403 return 1;
7406 static int fan_write_cmd_disable(const char *cmd, int *rc)
7408 if (strlencmp(cmd, "disable") != 0)
7409 return 0;
7411 *rc = fan_set_disable();
7412 if (*rc == -ENXIO)
7413 printk(TPACPI_ERR "disable command accepted for unsupported "
7414 "access mode %d", fan_control_access_mode);
7415 else if (!*rc)
7416 tpacpi_disclose_usertask("procfs fan", "disable\n");
7418 return 1;
7421 static int fan_write_cmd_speed(const char *cmd, int *rc)
7423 int speed;
7425 /* TODO:
7426 * Support speed <low> <medium> <high> ? */
7428 if (sscanf(cmd, "speed %d", &speed) != 1)
7429 return 0;
7431 *rc = fan_set_speed(speed);
7432 if (*rc == -ENXIO)
7433 printk(TPACPI_ERR "speed command accepted for unsupported "
7434 "access mode %d", fan_control_access_mode);
7435 else if (!*rc)
7436 tpacpi_disclose_usertask("procfs fan",
7437 "set speed to %d\n", speed);
7439 return 1;
7442 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7444 int interval;
7446 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7447 return 0;
7449 if (interval < 0 || interval > 120)
7450 *rc = -EINVAL;
7451 else {
7452 fan_watchdog_maxinterval = interval;
7453 tpacpi_disclose_usertask("procfs fan",
7454 "set watchdog timer to %d\n",
7455 interval);
7458 return 1;
7461 static int fan_write(char *buf)
7463 char *cmd;
7464 int rc = 0;
7466 while (!rc && (cmd = next_cmd(&buf))) {
7467 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7468 fan_write_cmd_level(cmd, &rc)) &&
7469 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7470 (fan_write_cmd_enable(cmd, &rc) ||
7471 fan_write_cmd_disable(cmd, &rc) ||
7472 fan_write_cmd_watchdog(cmd, &rc))) &&
7473 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7474 fan_write_cmd_speed(cmd, &rc))
7476 rc = -EINVAL;
7477 else if (!rc)
7478 fan_watchdog_reset();
7481 return rc;
7484 static struct ibm_struct fan_driver_data = {
7485 .name = "fan",
7486 .read = fan_read,
7487 .write = fan_write,
7488 .exit = fan_exit,
7489 .suspend = fan_suspend,
7490 .resume = fan_resume,
7493 /****************************************************************************
7494 ****************************************************************************
7496 * Infrastructure
7498 ****************************************************************************
7499 ****************************************************************************/
7501 /* sysfs name ---------------------------------------------------------- */
7502 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7503 struct device_attribute *attr,
7504 char *buf)
7506 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7509 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7510 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7512 /* --------------------------------------------------------------------- */
7514 /* /proc support */
7515 static struct proc_dir_entry *proc_dir;
7518 * Module and infrastructure proble, init and exit handling
7521 static int force_load;
7523 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7524 static const char * __init str_supported(int is_supported)
7526 static char text_unsupported[] __initdata = "not supported";
7528 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7530 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7532 static void ibm_exit(struct ibm_struct *ibm)
7534 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7536 list_del_init(&ibm->all_drivers);
7538 if (ibm->flags.acpi_notify_installed) {
7539 dbg_printk(TPACPI_DBG_EXIT,
7540 "%s: acpi_remove_notify_handler\n", ibm->name);
7541 BUG_ON(!ibm->acpi);
7542 acpi_remove_notify_handler(*ibm->acpi->handle,
7543 ibm->acpi->type,
7544 dispatch_acpi_notify);
7545 ibm->flags.acpi_notify_installed = 0;
7546 ibm->flags.acpi_notify_installed = 0;
7549 if (ibm->flags.proc_created) {
7550 dbg_printk(TPACPI_DBG_EXIT,
7551 "%s: remove_proc_entry\n", ibm->name);
7552 remove_proc_entry(ibm->name, proc_dir);
7553 ibm->flags.proc_created = 0;
7556 if (ibm->flags.acpi_driver_registered) {
7557 dbg_printk(TPACPI_DBG_EXIT,
7558 "%s: acpi_bus_unregister_driver\n", ibm->name);
7559 BUG_ON(!ibm->acpi);
7560 acpi_bus_unregister_driver(ibm->acpi->driver);
7561 kfree(ibm->acpi->driver);
7562 ibm->acpi->driver = NULL;
7563 ibm->flags.acpi_driver_registered = 0;
7566 if (ibm->flags.init_called && ibm->exit) {
7567 ibm->exit();
7568 ibm->flags.init_called = 0;
7571 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7574 static int __init ibm_init(struct ibm_init_struct *iibm)
7576 int ret;
7577 struct ibm_struct *ibm = iibm->data;
7578 struct proc_dir_entry *entry;
7580 BUG_ON(ibm == NULL);
7582 INIT_LIST_HEAD(&ibm->all_drivers);
7584 if (ibm->flags.experimental && !experimental)
7585 return 0;
7587 dbg_printk(TPACPI_DBG_INIT,
7588 "probing for %s\n", ibm->name);
7590 if (iibm->init) {
7591 ret = iibm->init(iibm);
7592 if (ret > 0)
7593 return 0; /* probe failed */
7594 if (ret)
7595 return ret;
7597 ibm->flags.init_called = 1;
7600 if (ibm->acpi) {
7601 if (ibm->acpi->hid) {
7602 ret = register_tpacpi_subdriver(ibm);
7603 if (ret)
7604 goto err_out;
7607 if (ibm->acpi->notify) {
7608 ret = setup_acpi_notify(ibm);
7609 if (ret == -ENODEV) {
7610 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7611 ibm->name);
7612 ret = 0;
7613 goto err_out;
7615 if (ret < 0)
7616 goto err_out;
7620 dbg_printk(TPACPI_DBG_INIT,
7621 "%s installed\n", ibm->name);
7623 if (ibm->read) {
7624 entry = create_proc_entry(ibm->name,
7625 S_IFREG | S_IRUGO | S_IWUSR,
7626 proc_dir);
7627 if (!entry) {
7628 printk(TPACPI_ERR "unable to create proc entry %s\n",
7629 ibm->name);
7630 ret = -ENODEV;
7631 goto err_out;
7633 entry->data = ibm;
7634 entry->read_proc = &dispatch_procfs_read;
7635 if (ibm->write)
7636 entry->write_proc = &dispatch_procfs_write;
7637 ibm->flags.proc_created = 1;
7640 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7642 return 0;
7644 err_out:
7645 dbg_printk(TPACPI_DBG_INIT,
7646 "%s: at error exit path with result %d\n",
7647 ibm->name, ret);
7649 ibm_exit(ibm);
7650 return (ret < 0)? ret : 0;
7653 /* Probing */
7655 static bool __pure __init tpacpi_is_fw_digit(const char c)
7657 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7660 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7661 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7662 const char t)
7664 return s && strlen(s) >= 8 &&
7665 tpacpi_is_fw_digit(s[0]) &&
7666 tpacpi_is_fw_digit(s[1]) &&
7667 s[2] == t && s[3] == 'T' &&
7668 tpacpi_is_fw_digit(s[4]) &&
7669 tpacpi_is_fw_digit(s[5]) &&
7670 s[6] == 'W' && s[7] == 'W';
7673 /* returns 0 - probe ok, or < 0 - probe error.
7674 * Probe ok doesn't mean thinkpad found.
7675 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7676 static int __must_check __init get_thinkpad_model_data(
7677 struct thinkpad_id_data *tp)
7679 const struct dmi_device *dev = NULL;
7680 char ec_fw_string[18];
7681 char const *s;
7683 if (!tp)
7684 return -EINVAL;
7686 memset(tp, 0, sizeof(*tp));
7688 if (dmi_name_in_vendors("IBM"))
7689 tp->vendor = PCI_VENDOR_ID_IBM;
7690 else if (dmi_name_in_vendors("LENOVO"))
7691 tp->vendor = PCI_VENDOR_ID_LENOVO;
7692 else
7693 return 0;
7695 s = dmi_get_system_info(DMI_BIOS_VERSION);
7696 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7697 if (s && !tp->bios_version_str)
7698 return -ENOMEM;
7700 /* Really ancient ThinkPad 240X will fail this, which is fine */
7701 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7702 return 0;
7704 tp->bios_model = tp->bios_version_str[0]
7705 | (tp->bios_version_str[1] << 8);
7706 tp->bios_release = (tp->bios_version_str[4] << 8)
7707 | tp->bios_version_str[5];
7710 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7711 * X32 or newer, all Z series; Some models must have an
7712 * up-to-date BIOS or they will not be detected.
7714 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7716 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7717 if (sscanf(dev->name,
7718 "IBM ThinkPad Embedded Controller -[%17c",
7719 ec_fw_string) == 1) {
7720 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7721 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7723 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7724 if (!tp->ec_version_str)
7725 return -ENOMEM;
7727 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7728 tp->ec_model = ec_fw_string[0]
7729 | (ec_fw_string[1] << 8);
7730 tp->ec_release = (ec_fw_string[4] << 8)
7731 | ec_fw_string[5];
7732 } else {
7733 printk(TPACPI_NOTICE
7734 "ThinkPad firmware release %s "
7735 "doesn't match the known patterns\n",
7736 ec_fw_string);
7737 printk(TPACPI_NOTICE
7738 "please report this to %s\n",
7739 TPACPI_MAIL);
7741 break;
7745 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7746 if (s && !strnicmp(s, "ThinkPad", 8)) {
7747 tp->model_str = kstrdup(s, GFP_KERNEL);
7748 if (!tp->model_str)
7749 return -ENOMEM;
7752 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7753 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7754 if (s && !tp->nummodel_str)
7755 return -ENOMEM;
7757 return 0;
7760 static int __init probe_for_thinkpad(void)
7762 int is_thinkpad;
7764 if (acpi_disabled)
7765 return -ENODEV;
7768 * Non-ancient models have better DMI tagging, but very old models
7769 * don't.
7771 is_thinkpad = (thinkpad_id.model_str != NULL);
7773 /* ec is required because many other handles are relative to it */
7774 TPACPI_ACPIHANDLE_INIT(ec);
7775 if (!ec_handle) {
7776 if (is_thinkpad)
7777 printk(TPACPI_ERR
7778 "Not yet supported ThinkPad detected!\n");
7779 return -ENODEV;
7783 * Risks a regression on very old machines, but reduces potential
7784 * false positives a damn great deal
7786 if (!is_thinkpad)
7787 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7789 if (!is_thinkpad && !force_load)
7790 return -ENODEV;
7792 return 0;
7796 /* Module init, exit, parameters */
7798 static struct ibm_init_struct ibms_init[] __initdata = {
7800 .init = thinkpad_acpi_driver_init,
7801 .data = &thinkpad_acpi_driver_data,
7804 .init = hotkey_init,
7805 .data = &hotkey_driver_data,
7808 .init = bluetooth_init,
7809 .data = &bluetooth_driver_data,
7812 .init = wan_init,
7813 .data = &wan_driver_data,
7816 .init = uwb_init,
7817 .data = &uwb_driver_data,
7819 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7821 .init = video_init,
7822 .data = &video_driver_data,
7824 #endif
7826 .init = light_init,
7827 .data = &light_driver_data,
7829 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7831 .init = dock_init,
7832 .data = &dock_driver_data[0],
7835 .init = dock_init2,
7836 .data = &dock_driver_data[1],
7838 #endif
7839 #ifdef CONFIG_THINKPAD_ACPI_BAY
7841 .init = bay_init,
7842 .data = &bay_driver_data,
7844 #endif
7846 .init = cmos_init,
7847 .data = &cmos_driver_data,
7850 .init = led_init,
7851 .data = &led_driver_data,
7854 .init = beep_init,
7855 .data = &beep_driver_data,
7858 .init = thermal_init,
7859 .data = &thermal_driver_data,
7862 .data = &ecdump_driver_data,
7865 .init = brightness_init,
7866 .data = &brightness_driver_data,
7869 .data = &volume_driver_data,
7872 .init = fan_init,
7873 .data = &fan_driver_data,
7877 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7879 unsigned int i;
7880 struct ibm_struct *ibm;
7882 if (!kp || !kp->name || !val)
7883 return -EINVAL;
7885 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7886 ibm = ibms_init[i].data;
7887 WARN_ON(ibm == NULL);
7889 if (!ibm || !ibm->name)
7890 continue;
7892 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7893 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7894 return -ENOSPC;
7895 strcpy(ibms_init[i].param, val);
7896 strcat(ibms_init[i].param, ",");
7897 return 0;
7901 return -EINVAL;
7904 module_param(experimental, int, 0);
7905 MODULE_PARM_DESC(experimental,
7906 "Enables experimental features when non-zero");
7908 module_param_named(debug, dbg_level, uint, 0);
7909 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7911 module_param(force_load, bool, 0);
7912 MODULE_PARM_DESC(force_load,
7913 "Attempts to load the driver even on a "
7914 "mis-identified ThinkPad when true");
7916 module_param_named(fan_control, fan_control_allowed, bool, 0);
7917 MODULE_PARM_DESC(fan_control,
7918 "Enables setting fan parameters features when true");
7920 module_param_named(brightness_mode, brightness_mode, uint, 0);
7921 MODULE_PARM_DESC(brightness_mode,
7922 "Selects brightness control strategy: "
7923 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7925 module_param(brightness_enable, uint, 0);
7926 MODULE_PARM_DESC(brightness_enable,
7927 "Enables backlight control when 1, disables when 0");
7929 module_param(hotkey_report_mode, uint, 0);
7930 MODULE_PARM_DESC(hotkey_report_mode,
7931 "used for backwards compatibility with userspace, "
7932 "see documentation");
7934 #define TPACPI_PARAM(feature) \
7935 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7936 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7937 "at module load, see documentation")
7939 TPACPI_PARAM(hotkey);
7940 TPACPI_PARAM(bluetooth);
7941 TPACPI_PARAM(video);
7942 TPACPI_PARAM(light);
7943 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7944 TPACPI_PARAM(dock);
7945 #endif
7946 #ifdef CONFIG_THINKPAD_ACPI_BAY
7947 TPACPI_PARAM(bay);
7948 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7949 TPACPI_PARAM(cmos);
7950 TPACPI_PARAM(led);
7951 TPACPI_PARAM(beep);
7952 TPACPI_PARAM(ecdump);
7953 TPACPI_PARAM(brightness);
7954 TPACPI_PARAM(volume);
7955 TPACPI_PARAM(fan);
7957 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7958 module_param(dbg_wlswemul, uint, 0);
7959 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7960 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7961 MODULE_PARM_DESC(wlsw_state,
7962 "Initial state of the emulated WLSW switch");
7964 module_param(dbg_bluetoothemul, uint, 0);
7965 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7966 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7967 MODULE_PARM_DESC(bluetooth_state,
7968 "Initial state of the emulated bluetooth switch");
7970 module_param(dbg_wwanemul, uint, 0);
7971 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7972 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7973 MODULE_PARM_DESC(wwan_state,
7974 "Initial state of the emulated WWAN switch");
7976 module_param(dbg_uwbemul, uint, 0);
7977 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7978 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7979 MODULE_PARM_DESC(uwb_state,
7980 "Initial state of the emulated UWB switch");
7981 #endif
7983 static void thinkpad_acpi_module_exit(void)
7985 struct ibm_struct *ibm, *itmp;
7987 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7989 list_for_each_entry_safe_reverse(ibm, itmp,
7990 &tpacpi_all_drivers,
7991 all_drivers) {
7992 ibm_exit(ibm);
7995 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7997 if (tpacpi_inputdev) {
7998 if (tp_features.input_device_registered)
7999 input_unregister_device(tpacpi_inputdev);
8000 else
8001 input_free_device(tpacpi_inputdev);
8004 if (tpacpi_hwmon)
8005 hwmon_device_unregister(tpacpi_hwmon);
8007 if (tp_features.sensors_pdev_attrs_registered)
8008 device_remove_file(&tpacpi_sensors_pdev->dev,
8009 &dev_attr_thinkpad_acpi_pdev_name);
8010 if (tpacpi_sensors_pdev)
8011 platform_device_unregister(tpacpi_sensors_pdev);
8012 if (tpacpi_pdev)
8013 platform_device_unregister(tpacpi_pdev);
8015 if (tp_features.sensors_pdrv_attrs_registered)
8016 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8017 if (tp_features.platform_drv_attrs_registered)
8018 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8020 if (tp_features.sensors_pdrv_registered)
8021 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8023 if (tp_features.platform_drv_registered)
8024 platform_driver_unregister(&tpacpi_pdriver);
8026 if (proc_dir)
8027 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8029 if (tpacpi_wq)
8030 destroy_workqueue(tpacpi_wq);
8032 kfree(thinkpad_id.bios_version_str);
8033 kfree(thinkpad_id.ec_version_str);
8034 kfree(thinkpad_id.model_str);
8038 static int __init thinkpad_acpi_module_init(void)
8040 int ret, i;
8042 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8044 /* Parameter checking */
8045 if (hotkey_report_mode > 2)
8046 return -EINVAL;
8048 /* Driver-level probe */
8050 ret = get_thinkpad_model_data(&thinkpad_id);
8051 if (ret) {
8052 printk(TPACPI_ERR
8053 "unable to get DMI data: %d\n", ret);
8054 thinkpad_acpi_module_exit();
8055 return ret;
8057 ret = probe_for_thinkpad();
8058 if (ret) {
8059 thinkpad_acpi_module_exit();
8060 return ret;
8063 /* Driver initialization */
8065 TPACPI_ACPIHANDLE_INIT(ecrd);
8066 TPACPI_ACPIHANDLE_INIT(ecwr);
8068 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8069 if (!tpacpi_wq) {
8070 thinkpad_acpi_module_exit();
8071 return -ENOMEM;
8074 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8075 if (!proc_dir) {
8076 printk(TPACPI_ERR
8077 "unable to create proc dir " TPACPI_PROC_DIR);
8078 thinkpad_acpi_module_exit();
8079 return -ENODEV;
8082 ret = platform_driver_register(&tpacpi_pdriver);
8083 if (ret) {
8084 printk(TPACPI_ERR
8085 "unable to register main platform driver\n");
8086 thinkpad_acpi_module_exit();
8087 return ret;
8089 tp_features.platform_drv_registered = 1;
8091 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8092 if (ret) {
8093 printk(TPACPI_ERR
8094 "unable to register hwmon platform driver\n");
8095 thinkpad_acpi_module_exit();
8096 return ret;
8098 tp_features.sensors_pdrv_registered = 1;
8100 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8101 if (!ret) {
8102 tp_features.platform_drv_attrs_registered = 1;
8103 ret = tpacpi_create_driver_attributes(
8104 &tpacpi_hwmon_pdriver.driver);
8106 if (ret) {
8107 printk(TPACPI_ERR
8108 "unable to create sysfs driver attributes\n");
8109 thinkpad_acpi_module_exit();
8110 return ret;
8112 tp_features.sensors_pdrv_attrs_registered = 1;
8115 /* Device initialization */
8116 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8117 NULL, 0);
8118 if (IS_ERR(tpacpi_pdev)) {
8119 ret = PTR_ERR(tpacpi_pdev);
8120 tpacpi_pdev = NULL;
8121 printk(TPACPI_ERR "unable to register platform device\n");
8122 thinkpad_acpi_module_exit();
8123 return ret;
8125 tpacpi_sensors_pdev = platform_device_register_simple(
8126 TPACPI_HWMON_DRVR_NAME,
8127 -1, NULL, 0);
8128 if (IS_ERR(tpacpi_sensors_pdev)) {
8129 ret = PTR_ERR(tpacpi_sensors_pdev);
8130 tpacpi_sensors_pdev = NULL;
8131 printk(TPACPI_ERR
8132 "unable to register hwmon platform device\n");
8133 thinkpad_acpi_module_exit();
8134 return ret;
8136 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8137 &dev_attr_thinkpad_acpi_pdev_name);
8138 if (ret) {
8139 printk(TPACPI_ERR
8140 "unable to create sysfs hwmon device attributes\n");
8141 thinkpad_acpi_module_exit();
8142 return ret;
8144 tp_features.sensors_pdev_attrs_registered = 1;
8145 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8146 if (IS_ERR(tpacpi_hwmon)) {
8147 ret = PTR_ERR(tpacpi_hwmon);
8148 tpacpi_hwmon = NULL;
8149 printk(TPACPI_ERR "unable to register hwmon device\n");
8150 thinkpad_acpi_module_exit();
8151 return ret;
8153 mutex_init(&tpacpi_inputdev_send_mutex);
8154 tpacpi_inputdev = input_allocate_device();
8155 if (!tpacpi_inputdev) {
8156 printk(TPACPI_ERR "unable to allocate input device\n");
8157 thinkpad_acpi_module_exit();
8158 return -ENOMEM;
8159 } else {
8160 /* Prepare input device, but don't register */
8161 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8162 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8163 tpacpi_inputdev->id.bustype = BUS_HOST;
8164 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8165 thinkpad_id.vendor :
8166 PCI_VENDOR_ID_IBM;
8167 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8168 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8170 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8171 ret = ibm_init(&ibms_init[i]);
8172 if (ret >= 0 && *ibms_init[i].param)
8173 ret = ibms_init[i].data->write(ibms_init[i].param);
8174 if (ret < 0) {
8175 thinkpad_acpi_module_exit();
8176 return ret;
8179 ret = input_register_device(tpacpi_inputdev);
8180 if (ret < 0) {
8181 printk(TPACPI_ERR "unable to register input device\n");
8182 thinkpad_acpi_module_exit();
8183 return ret;
8184 } else {
8185 tp_features.input_device_registered = 1;
8188 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8189 return 0;
8192 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8195 * This will autoload the driver in almost every ThinkPad
8196 * in widespread use.
8198 * Only _VERY_ old models, like the 240, 240x and 570 lack
8199 * the HKEY event interface.
8201 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8204 * DMI matching for module autoloading
8206 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8207 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8209 * Only models listed in thinkwiki will be supported, so add yours
8210 * if it is not there yet.
8212 #define IBM_BIOS_MODULE_ALIAS(__type) \
8213 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8215 /* Ancient thinkpad BIOSes have to be identified by
8216 * BIOS type or model number, and there are far less
8217 * BIOS types than model numbers... */
8218 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8220 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8221 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8222 MODULE_DESCRIPTION(TPACPI_DESC);
8223 MODULE_VERSION(TPACPI_VERSION);
8224 MODULE_LICENSE("GPL");
8226 module_init(thinkpad_acpi_module_init);
8227 module_exit(thinkpad_acpi_module_exit);