thinkpad-acpi: enhanced debugging messages for the fan subdriver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobbcbdc1b95dd97eab0f31b0da213a8c13d8c0a63b
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.22"
25 #define TPACPI_SYSFS_VERSION 0x020300
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
196 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
197 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
198 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
201 /****************************************************************************
202 * Driver-wide structs and misc. variables
205 struct ibm_struct;
207 struct tp_acpi_drv_struct {
208 const struct acpi_device_id *hid;
209 struct acpi_driver *driver;
211 void (*notify) (struct ibm_struct *, u32);
212 acpi_handle *handle;
213 u32 type;
214 struct acpi_device *device;
217 struct ibm_struct {
218 char *name;
220 int (*read) (char *);
221 int (*write) (char *);
222 void (*exit) (void);
223 void (*resume) (void);
224 void (*suspend) (pm_message_t state);
225 void (*shutdown) (void);
227 struct list_head all_drivers;
229 struct tp_acpi_drv_struct *acpi;
231 struct {
232 u8 acpi_driver_registered:1;
233 u8 acpi_notify_installed:1;
234 u8 proc_created:1;
235 u8 init_called:1;
236 u8 experimental:1;
237 } flags;
240 struct ibm_init_struct {
241 char param[32];
243 int (*init) (struct ibm_init_struct *);
244 struct ibm_struct *data;
247 static struct {
248 #ifdef CONFIG_THINKPAD_ACPI_BAY
249 u32 bay_status:1;
250 u32 bay_eject:1;
251 u32 bay_status2:1;
252 u32 bay_eject2:1;
253 #endif
254 u32 bluetooth:1;
255 u32 hotkey:1;
256 u32 hotkey_mask:1;
257 u32 hotkey_wlsw:1;
258 u32 hotkey_tablet:1;
259 u32 light:1;
260 u32 light_status:1;
261 u32 bright_16levels:1;
262 u32 bright_acpimode:1;
263 u32 wan:1;
264 u32 uwb:1;
265 u32 fan_ctrl_status_undef:1;
266 u32 input_device_registered:1;
267 u32 platform_drv_registered:1;
268 u32 platform_drv_attrs_registered:1;
269 u32 sensors_pdrv_registered:1;
270 u32 sensors_pdrv_attrs_registered:1;
271 u32 sensors_pdev_attrs_registered:1;
272 u32 hotkey_poll_active:1;
273 } tp_features;
275 static struct {
276 u16 hotkey_mask_ff:1;
277 u16 bright_cmos_ec_unsync:1;
278 } tp_warned;
280 struct thinkpad_id_data {
281 unsigned int vendor; /* ThinkPad vendor:
282 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
284 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
285 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
287 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
288 u16 ec_model;
290 char *model_str; /* ThinkPad T43 */
291 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
293 static struct thinkpad_id_data thinkpad_id;
295 static enum {
296 TPACPI_LIFE_INIT = 0,
297 TPACPI_LIFE_RUNNING,
298 TPACPI_LIFE_EXITING,
299 } tpacpi_lifecycle;
301 static int experimental;
302 static u32 dbg_level;
304 static struct workqueue_struct *tpacpi_wq;
306 enum led_status_t {
307 TPACPI_LED_OFF = 0,
308 TPACPI_LED_ON,
309 TPACPI_LED_BLINK,
312 /* Special LED class that can defer work */
313 struct tpacpi_led_classdev {
314 struct led_classdev led_classdev;
315 struct work_struct work;
316 enum led_status_t new_state;
317 unsigned int led;
320 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
321 static int dbg_wlswemul;
322 static int tpacpi_wlsw_emulstate;
323 static int dbg_bluetoothemul;
324 static int tpacpi_bluetooth_emulstate;
325 static int dbg_wwanemul;
326 static int tpacpi_wwan_emulstate;
327 static int dbg_uwbemul;
328 static int tpacpi_uwb_emulstate;
329 #endif
332 /*************************************************************************
333 * Debugging helpers
336 #define dbg_printk(a_dbg_level, format, arg...) \
337 do { if (dbg_level & (a_dbg_level)) \
338 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
339 } while (0)
341 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
342 #define vdbg_printk dbg_printk
343 static const char *str_supported(int is_supported);
344 #else
345 #define vdbg_printk(a_dbg_level, format, arg...) \
346 do { } while (0)
347 #endif
349 static void tpacpi_log_usertask(const char * const what)
351 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
352 what, task_tgid_vnr(current));
355 #define tpacpi_disclose_usertask(what, format, arg...) \
356 do { \
357 if (unlikely( \
358 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
359 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
360 printk(TPACPI_DEBUG "%s: PID %d: " format, \
361 what, task_tgid_vnr(current), ## arg); \
363 } while (0)
365 /****************************************************************************
366 ****************************************************************************
368 * ACPI Helpers and device model
370 ****************************************************************************
371 ****************************************************************************/
373 /*************************************************************************
374 * ACPI basic handles
377 static acpi_handle root_handle;
379 #define TPACPI_HANDLE(object, parent, paths...) \
380 static acpi_handle object##_handle; \
381 static acpi_handle *object##_parent = &parent##_handle; \
382 static char *object##_path; \
383 static char *object##_paths[] = { paths }
385 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
386 "\\_SB.PCI.ISA.EC", /* 570 */
387 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
388 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
389 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
390 "\\_SB.PCI0.ICH3.EC0", /* R31 */
391 "\\_SB.PCI0.LPC.EC", /* all others */
394 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
395 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
397 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
398 /* T4x, X31, X40 */
399 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
400 "\\CMS", /* R40, R40e */
401 ); /* all others */
403 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
404 "^HKEY", /* R30, R31 */
405 "HKEY", /* all others */
406 ); /* 570 */
408 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
409 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
410 "\\_SB.PCI0.VID0", /* 770e */
411 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
412 "\\_SB.PCI0.AGP.VID", /* all others */
413 ); /* R30, R31 */
416 /*************************************************************************
417 * ACPI helpers
420 static int acpi_evalf(acpi_handle handle,
421 void *res, char *method, char *fmt, ...)
423 char *fmt0 = fmt;
424 struct acpi_object_list params;
425 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
426 struct acpi_buffer result, *resultp;
427 union acpi_object out_obj;
428 acpi_status status;
429 va_list ap;
430 char res_type;
431 int success;
432 int quiet;
434 if (!*fmt) {
435 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
436 return 0;
439 if (*fmt == 'q') {
440 quiet = 1;
441 fmt++;
442 } else
443 quiet = 0;
445 res_type = *(fmt++);
447 params.count = 0;
448 params.pointer = &in_objs[0];
450 va_start(ap, fmt);
451 while (*fmt) {
452 char c = *(fmt++);
453 switch (c) {
454 case 'd': /* int */
455 in_objs[params.count].integer.value = va_arg(ap, int);
456 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
457 break;
458 /* add more types as needed */
459 default:
460 printk(TPACPI_ERR "acpi_evalf() called "
461 "with invalid format character '%c'\n", c);
462 return 0;
465 va_end(ap);
467 if (res_type != 'v') {
468 result.length = sizeof(out_obj);
469 result.pointer = &out_obj;
470 resultp = &result;
471 } else
472 resultp = NULL;
474 status = acpi_evaluate_object(handle, method, &params, resultp);
476 switch (res_type) {
477 case 'd': /* int */
478 if (res)
479 *(int *)res = out_obj.integer.value;
480 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
481 break;
482 case 'v': /* void */
483 success = status == AE_OK;
484 break;
485 /* add more types as needed */
486 default:
487 printk(TPACPI_ERR "acpi_evalf() called "
488 "with invalid format character '%c'\n", res_type);
489 return 0;
492 if (!success && !quiet)
493 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
494 method, fmt0, status);
496 return success;
499 static int acpi_ec_read(int i, u8 *p)
501 int v;
503 if (ecrd_handle) {
504 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
505 return 0;
506 *p = v;
507 } else {
508 if (ec_read(i, p) < 0)
509 return 0;
512 return 1;
515 static int acpi_ec_write(int i, u8 v)
517 if (ecwr_handle) {
518 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
519 return 0;
520 } else {
521 if (ec_write(i, v) < 0)
522 return 0;
525 return 1;
528 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
529 static int _sta(acpi_handle handle)
531 int status;
533 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
534 status = 0;
536 return status;
538 #endif
540 static int issue_thinkpad_cmos_command(int cmos_cmd)
542 if (!cmos_handle)
543 return -ENXIO;
545 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
546 return -EIO;
548 return 0;
551 /*************************************************************************
552 * ACPI device model
555 #define TPACPI_ACPIHANDLE_INIT(object) \
556 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
557 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
559 static void drv_acpi_handle_init(char *name,
560 acpi_handle *handle, acpi_handle parent,
561 char **paths, int num_paths, char **path)
563 int i;
564 acpi_status status;
566 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
567 name);
569 for (i = 0; i < num_paths; i++) {
570 status = acpi_get_handle(parent, paths[i], handle);
571 if (ACPI_SUCCESS(status)) {
572 *path = paths[i];
573 dbg_printk(TPACPI_DBG_INIT,
574 "Found ACPI handle %s for %s\n",
575 *path, name);
576 return;
580 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
581 name);
582 *handle = NULL;
585 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
587 struct ibm_struct *ibm = data;
589 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
590 return;
592 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
593 return;
595 ibm->acpi->notify(ibm, event);
598 static int __init setup_acpi_notify(struct ibm_struct *ibm)
600 acpi_status status;
601 int rc;
603 BUG_ON(!ibm->acpi);
605 if (!*ibm->acpi->handle)
606 return 0;
608 vdbg_printk(TPACPI_DBG_INIT,
609 "setting up ACPI notify for %s\n", ibm->name);
611 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
612 if (rc < 0) {
613 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
614 ibm->name, rc);
615 return -ENODEV;
618 ibm->acpi->device->driver_data = ibm;
619 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
620 TPACPI_ACPI_EVENT_PREFIX,
621 ibm->name);
623 status = acpi_install_notify_handler(*ibm->acpi->handle,
624 ibm->acpi->type, dispatch_acpi_notify, ibm);
625 if (ACPI_FAILURE(status)) {
626 if (status == AE_ALREADY_EXISTS) {
627 printk(TPACPI_NOTICE
628 "another device driver is already "
629 "handling %s events\n", ibm->name);
630 } else {
631 printk(TPACPI_ERR
632 "acpi_install_notify_handler(%s) failed: %d\n",
633 ibm->name, status);
635 return -ENODEV;
637 ibm->flags.acpi_notify_installed = 1;
638 return 0;
641 static int __init tpacpi_device_add(struct acpi_device *device)
643 return 0;
646 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
648 int rc;
650 dbg_printk(TPACPI_DBG_INIT,
651 "registering %s as an ACPI driver\n", ibm->name);
653 BUG_ON(!ibm->acpi);
655 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
656 if (!ibm->acpi->driver) {
657 printk(TPACPI_ERR
658 "failed to allocate memory for ibm->acpi->driver\n");
659 return -ENOMEM;
662 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
663 ibm->acpi->driver->ids = ibm->acpi->hid;
665 ibm->acpi->driver->ops.add = &tpacpi_device_add;
667 rc = acpi_bus_register_driver(ibm->acpi->driver);
668 if (rc < 0) {
669 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
670 ibm->name, rc);
671 kfree(ibm->acpi->driver);
672 ibm->acpi->driver = NULL;
673 } else if (!rc)
674 ibm->flags.acpi_driver_registered = 1;
676 return rc;
680 /****************************************************************************
681 ****************************************************************************
683 * Procfs Helpers
685 ****************************************************************************
686 ****************************************************************************/
688 static int dispatch_procfs_read(char *page, char **start, off_t off,
689 int count, int *eof, void *data)
691 struct ibm_struct *ibm = data;
692 int len;
694 if (!ibm || !ibm->read)
695 return -EINVAL;
697 len = ibm->read(page);
698 if (len < 0)
699 return len;
701 if (len <= off + count)
702 *eof = 1;
703 *start = page + off;
704 len -= off;
705 if (len > count)
706 len = count;
707 if (len < 0)
708 len = 0;
710 return len;
713 static int dispatch_procfs_write(struct file *file,
714 const char __user *userbuf,
715 unsigned long count, void *data)
717 struct ibm_struct *ibm = data;
718 char *kernbuf;
719 int ret;
721 if (!ibm || !ibm->write)
722 return -EINVAL;
724 kernbuf = kmalloc(count + 2, GFP_KERNEL);
725 if (!kernbuf)
726 return -ENOMEM;
728 if (copy_from_user(kernbuf, userbuf, count)) {
729 kfree(kernbuf);
730 return -EFAULT;
733 kernbuf[count] = 0;
734 strcat(kernbuf, ",");
735 ret = ibm->write(kernbuf);
736 if (ret == 0)
737 ret = count;
739 kfree(kernbuf);
741 return ret;
744 static char *next_cmd(char **cmds)
746 char *start = *cmds;
747 char *end;
749 while ((end = strchr(start, ',')) && end == start)
750 start = end + 1;
752 if (!end)
753 return NULL;
755 *end = 0;
756 *cmds = end + 1;
757 return start;
761 /****************************************************************************
762 ****************************************************************************
764 * Device model: input, hwmon and platform
766 ****************************************************************************
767 ****************************************************************************/
769 static struct platform_device *tpacpi_pdev;
770 static struct platform_device *tpacpi_sensors_pdev;
771 static struct device *tpacpi_hwmon;
772 static struct input_dev *tpacpi_inputdev;
773 static struct mutex tpacpi_inputdev_send_mutex;
774 static LIST_HEAD(tpacpi_all_drivers);
776 static int tpacpi_suspend_handler(struct platform_device *pdev,
777 pm_message_t state)
779 struct ibm_struct *ibm, *itmp;
781 list_for_each_entry_safe(ibm, itmp,
782 &tpacpi_all_drivers,
783 all_drivers) {
784 if (ibm->suspend)
785 (ibm->suspend)(state);
788 return 0;
791 static int tpacpi_resume_handler(struct platform_device *pdev)
793 struct ibm_struct *ibm, *itmp;
795 list_for_each_entry_safe(ibm, itmp,
796 &tpacpi_all_drivers,
797 all_drivers) {
798 if (ibm->resume)
799 (ibm->resume)();
802 return 0;
805 static void tpacpi_shutdown_handler(struct platform_device *pdev)
807 struct ibm_struct *ibm, *itmp;
809 list_for_each_entry_safe(ibm, itmp,
810 &tpacpi_all_drivers,
811 all_drivers) {
812 if (ibm->shutdown)
813 (ibm->shutdown)();
817 static struct platform_driver tpacpi_pdriver = {
818 .driver = {
819 .name = TPACPI_DRVR_NAME,
820 .owner = THIS_MODULE,
822 .suspend = tpacpi_suspend_handler,
823 .resume = tpacpi_resume_handler,
824 .shutdown = tpacpi_shutdown_handler,
827 static struct platform_driver tpacpi_hwmon_pdriver = {
828 .driver = {
829 .name = TPACPI_HWMON_DRVR_NAME,
830 .owner = THIS_MODULE,
834 /*************************************************************************
835 * sysfs support helpers
838 struct attribute_set {
839 unsigned int members, max_members;
840 struct attribute_group group;
843 struct attribute_set_obj {
844 struct attribute_set s;
845 struct attribute *a;
846 } __attribute__((packed));
848 static struct attribute_set *create_attr_set(unsigned int max_members,
849 const char *name)
851 struct attribute_set_obj *sobj;
853 if (max_members == 0)
854 return NULL;
856 /* Allocates space for implicit NULL at the end too */
857 sobj = kzalloc(sizeof(struct attribute_set_obj) +
858 max_members * sizeof(struct attribute *),
859 GFP_KERNEL);
860 if (!sobj)
861 return NULL;
862 sobj->s.max_members = max_members;
863 sobj->s.group.attrs = &sobj->a;
864 sobj->s.group.name = name;
866 return &sobj->s;
869 #define destroy_attr_set(_set) \
870 kfree(_set);
872 /* not multi-threaded safe, use it in a single thread per set */
873 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
875 if (!s || !attr)
876 return -EINVAL;
878 if (s->members >= s->max_members)
879 return -ENOMEM;
881 s->group.attrs[s->members] = attr;
882 s->members++;
884 return 0;
887 static int add_many_to_attr_set(struct attribute_set *s,
888 struct attribute **attr,
889 unsigned int count)
891 int i, res;
893 for (i = 0; i < count; i++) {
894 res = add_to_attr_set(s, attr[i]);
895 if (res)
896 return res;
899 return 0;
902 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
904 sysfs_remove_group(kobj, &s->group);
905 destroy_attr_set(s);
908 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
909 sysfs_create_group(_kobj, &_attr_set->group)
911 static int parse_strtoul(const char *buf,
912 unsigned long max, unsigned long *value)
914 char *endp;
916 while (*buf && isspace(*buf))
917 buf++;
918 *value = simple_strtoul(buf, &endp, 0);
919 while (*endp && isspace(*endp))
920 endp++;
921 if (*endp || *value > max)
922 return -EINVAL;
924 return 0;
927 static void tpacpi_disable_brightness_delay(void)
929 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
930 printk(TPACPI_NOTICE
931 "ACPI backlight control delay disabled\n");
934 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
936 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
937 union acpi_object *obj;
938 int rc;
940 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
941 obj = (union acpi_object *)buffer.pointer;
942 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
943 printk(TPACPI_ERR "Unknown _BCL data, "
944 "please report this to %s\n", TPACPI_MAIL);
945 rc = 0;
946 } else {
947 rc = obj->package.count;
949 } else {
950 return 0;
953 kfree(buffer.pointer);
954 return rc;
957 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
958 u32 lvl, void *context, void **rv)
960 char name[ACPI_PATH_SEGMENT_LENGTH];
961 struct acpi_buffer buffer = { sizeof(name), &name };
963 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
964 !strncmp("_BCL", name, sizeof(name) - 1)) {
965 BUG_ON(!rv || !*rv);
966 **(int **)rv = tpacpi_query_bcl_levels(handle);
967 return AE_CTRL_TERMINATE;
968 } else {
969 return AE_OK;
974 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
976 static int __init tpacpi_check_std_acpi_brightness_support(void)
978 int status;
979 int bcl_levels = 0;
980 void *bcl_ptr = &bcl_levels;
982 if (!vid_handle) {
983 TPACPI_ACPIHANDLE_INIT(vid);
985 if (!vid_handle)
986 return 0;
989 * Search for a _BCL method, and execute it. This is safe on all
990 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
991 * BIOS in ACPI backlight control mode. We do NOT have to care
992 * about calling the _BCL method in an enabled video device, any
993 * will do for our purposes.
996 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
997 tpacpi_acpi_walk_find_bcl, NULL,
998 &bcl_ptr);
1000 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1001 tp_features.bright_acpimode = 1;
1002 return (bcl_levels - 2);
1005 return 0;
1008 static int __init tpacpi_new_rfkill(const unsigned int id,
1009 struct rfkill **rfk,
1010 const enum rfkill_type rfktype,
1011 const char *name,
1012 const bool set_default,
1013 int (*toggle_radio)(void *, enum rfkill_state),
1014 int (*get_state)(void *, enum rfkill_state *))
1016 int res;
1017 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1019 res = get_state(NULL, &initial_state);
1020 if (res < 0) {
1021 printk(TPACPI_ERR
1022 "failed to read initial state for %s, error %d; "
1023 "will turn radio off\n", name, res);
1024 } else if (set_default) {
1025 /* try to set the initial state as the default for the rfkill
1026 * type, since we ask the firmware to preserve it across S5 in
1027 * NVRAM */
1028 if (rfkill_set_default(rfktype,
1029 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1030 RFKILL_STATE_UNBLOCKED :
1031 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1032 vdbg_printk(TPACPI_DBG_RFKILL,
1033 "Default state for %s cannot be changed\n",
1034 name);
1037 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1038 if (!*rfk) {
1039 printk(TPACPI_ERR
1040 "failed to allocate memory for rfkill class\n");
1041 return -ENOMEM;
1044 (*rfk)->name = name;
1045 (*rfk)->get_state = get_state;
1046 (*rfk)->toggle_radio = toggle_radio;
1047 (*rfk)->state = initial_state;
1049 res = rfkill_register(*rfk);
1050 if (res < 0) {
1051 printk(TPACPI_ERR
1052 "failed to register %s rfkill switch: %d\n",
1053 name, res);
1054 rfkill_free(*rfk);
1055 *rfk = NULL;
1056 return res;
1059 return 0;
1062 static void printk_deprecated_attribute(const char * const what,
1063 const char * const details)
1065 tpacpi_log_usertask("deprecated sysfs attribute");
1066 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1067 "will be removed. %s\n",
1068 what, details);
1071 static void printk_deprecated_rfkill_attribute(const char * const what)
1073 printk_deprecated_attribute(what,
1074 "Please switch to generic rfkill before year 2010");
1077 /*************************************************************************
1078 * thinkpad-acpi driver attributes
1081 /* interface_version --------------------------------------------------- */
1082 static ssize_t tpacpi_driver_interface_version_show(
1083 struct device_driver *drv,
1084 char *buf)
1086 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1089 static DRIVER_ATTR(interface_version, S_IRUGO,
1090 tpacpi_driver_interface_version_show, NULL);
1092 /* debug_level --------------------------------------------------------- */
1093 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1094 char *buf)
1096 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1099 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1100 const char *buf, size_t count)
1102 unsigned long t;
1104 if (parse_strtoul(buf, 0xffff, &t))
1105 return -EINVAL;
1107 dbg_level = t;
1109 return count;
1112 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1113 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1115 /* version ------------------------------------------------------------- */
1116 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1117 char *buf)
1119 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1120 TPACPI_DESC, TPACPI_VERSION);
1123 static DRIVER_ATTR(version, S_IRUGO,
1124 tpacpi_driver_version_show, NULL);
1126 /* --------------------------------------------------------------------- */
1128 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1130 static void tpacpi_send_radiosw_update(void);
1132 /* wlsw_emulstate ------------------------------------------------------ */
1133 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1134 char *buf)
1136 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1139 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1140 const char *buf, size_t count)
1142 unsigned long t;
1144 if (parse_strtoul(buf, 1, &t))
1145 return -EINVAL;
1147 if (tpacpi_wlsw_emulstate != t) {
1148 tpacpi_wlsw_emulstate = !!t;
1149 tpacpi_send_radiosw_update();
1150 } else
1151 tpacpi_wlsw_emulstate = !!t;
1153 return count;
1156 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1157 tpacpi_driver_wlsw_emulstate_show,
1158 tpacpi_driver_wlsw_emulstate_store);
1160 /* bluetooth_emulstate ------------------------------------------------- */
1161 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1162 struct device_driver *drv,
1163 char *buf)
1165 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1168 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1169 struct device_driver *drv,
1170 const char *buf, size_t count)
1172 unsigned long t;
1174 if (parse_strtoul(buf, 1, &t))
1175 return -EINVAL;
1177 tpacpi_bluetooth_emulstate = !!t;
1179 return count;
1182 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1183 tpacpi_driver_bluetooth_emulstate_show,
1184 tpacpi_driver_bluetooth_emulstate_store);
1186 /* wwan_emulstate ------------------------------------------------- */
1187 static ssize_t tpacpi_driver_wwan_emulstate_show(
1188 struct device_driver *drv,
1189 char *buf)
1191 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1194 static ssize_t tpacpi_driver_wwan_emulstate_store(
1195 struct device_driver *drv,
1196 const char *buf, size_t count)
1198 unsigned long t;
1200 if (parse_strtoul(buf, 1, &t))
1201 return -EINVAL;
1203 tpacpi_wwan_emulstate = !!t;
1205 return count;
1208 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1209 tpacpi_driver_wwan_emulstate_show,
1210 tpacpi_driver_wwan_emulstate_store);
1212 /* uwb_emulstate ------------------------------------------------- */
1213 static ssize_t tpacpi_driver_uwb_emulstate_show(
1214 struct device_driver *drv,
1215 char *buf)
1217 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1220 static ssize_t tpacpi_driver_uwb_emulstate_store(
1221 struct device_driver *drv,
1222 const char *buf, size_t count)
1224 unsigned long t;
1226 if (parse_strtoul(buf, 1, &t))
1227 return -EINVAL;
1229 tpacpi_uwb_emulstate = !!t;
1231 return count;
1234 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1235 tpacpi_driver_uwb_emulstate_show,
1236 tpacpi_driver_uwb_emulstate_store);
1237 #endif
1239 /* --------------------------------------------------------------------- */
1241 static struct driver_attribute *tpacpi_driver_attributes[] = {
1242 &driver_attr_debug_level, &driver_attr_version,
1243 &driver_attr_interface_version,
1246 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1248 int i, res;
1250 i = 0;
1251 res = 0;
1252 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1253 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1254 i++;
1257 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1258 if (!res && dbg_wlswemul)
1259 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1260 if (!res && dbg_bluetoothemul)
1261 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1262 if (!res && dbg_wwanemul)
1263 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1264 if (!res && dbg_uwbemul)
1265 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1266 #endif
1268 return res;
1271 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1273 int i;
1275 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1276 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1278 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1279 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1280 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1281 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1282 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1283 #endif
1286 /****************************************************************************
1287 ****************************************************************************
1289 * Subdrivers
1291 ****************************************************************************
1292 ****************************************************************************/
1294 /*************************************************************************
1295 * thinkpad-acpi init subdriver
1298 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1300 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1301 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1303 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1304 (thinkpad_id.bios_version_str) ?
1305 thinkpad_id.bios_version_str : "unknown",
1306 (thinkpad_id.ec_version_str) ?
1307 thinkpad_id.ec_version_str : "unknown");
1309 if (thinkpad_id.vendor && thinkpad_id.model_str)
1310 printk(TPACPI_INFO "%s %s, model %s\n",
1311 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1312 "IBM" : ((thinkpad_id.vendor ==
1313 PCI_VENDOR_ID_LENOVO) ?
1314 "Lenovo" : "Unknown vendor"),
1315 thinkpad_id.model_str,
1316 (thinkpad_id.nummodel_str) ?
1317 thinkpad_id.nummodel_str : "unknown");
1319 return 0;
1322 static int thinkpad_acpi_driver_read(char *p)
1324 int len = 0;
1326 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1327 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1329 return len;
1332 static struct ibm_struct thinkpad_acpi_driver_data = {
1333 .name = "driver",
1334 .read = thinkpad_acpi_driver_read,
1337 /*************************************************************************
1338 * Hotkey subdriver
1341 enum { /* hot key scan codes (derived from ACPI DSDT) */
1342 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1343 TP_ACPI_HOTKEYSCAN_FNF2,
1344 TP_ACPI_HOTKEYSCAN_FNF3,
1345 TP_ACPI_HOTKEYSCAN_FNF4,
1346 TP_ACPI_HOTKEYSCAN_FNF5,
1347 TP_ACPI_HOTKEYSCAN_FNF6,
1348 TP_ACPI_HOTKEYSCAN_FNF7,
1349 TP_ACPI_HOTKEYSCAN_FNF8,
1350 TP_ACPI_HOTKEYSCAN_FNF9,
1351 TP_ACPI_HOTKEYSCAN_FNF10,
1352 TP_ACPI_HOTKEYSCAN_FNF11,
1353 TP_ACPI_HOTKEYSCAN_FNF12,
1354 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1355 TP_ACPI_HOTKEYSCAN_FNINSERT,
1356 TP_ACPI_HOTKEYSCAN_FNDELETE,
1357 TP_ACPI_HOTKEYSCAN_FNHOME,
1358 TP_ACPI_HOTKEYSCAN_FNEND,
1359 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1360 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1361 TP_ACPI_HOTKEYSCAN_FNSPACE,
1362 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1363 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1364 TP_ACPI_HOTKEYSCAN_MUTE,
1365 TP_ACPI_HOTKEYSCAN_THINKPAD,
1368 enum { /* Keys available through NVRAM polling */
1369 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1370 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1373 enum { /* Positions of some of the keys in hotkey masks */
1374 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1375 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1376 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1377 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1378 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1379 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1380 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1381 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1382 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1383 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1384 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1387 enum { /* NVRAM to ACPI HKEY group map */
1388 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1389 TP_ACPI_HKEY_ZOOM_MASK |
1390 TP_ACPI_HKEY_DISPSWTCH_MASK |
1391 TP_ACPI_HKEY_HIBERNATE_MASK,
1392 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1393 TP_ACPI_HKEY_BRGHTDWN_MASK,
1394 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1395 TP_ACPI_HKEY_VOLDWN_MASK |
1396 TP_ACPI_HKEY_MUTE_MASK,
1399 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1400 struct tp_nvram_state {
1401 u16 thinkpad_toggle:1;
1402 u16 zoom_toggle:1;
1403 u16 display_toggle:1;
1404 u16 thinklight_toggle:1;
1405 u16 hibernate_toggle:1;
1406 u16 displayexp_toggle:1;
1407 u16 display_state:1;
1408 u16 brightness_toggle:1;
1409 u16 volume_toggle:1;
1410 u16 mute:1;
1412 u8 brightness_level;
1413 u8 volume_level;
1416 static struct task_struct *tpacpi_hotkey_task;
1417 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1418 static int hotkey_poll_freq = 10; /* Hz */
1419 static struct mutex hotkey_thread_mutex;
1420 static struct mutex hotkey_thread_data_mutex;
1421 static unsigned int hotkey_config_change;
1423 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1425 #define hotkey_source_mask 0U
1427 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1429 static struct mutex hotkey_mutex;
1431 static enum { /* Reasons for waking up */
1432 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1433 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1434 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1435 } hotkey_wakeup_reason;
1437 static int hotkey_autosleep_ack;
1439 static u32 hotkey_orig_mask;
1440 static u32 hotkey_all_mask;
1441 static u32 hotkey_reserved_mask;
1442 static u32 hotkey_mask;
1444 static unsigned int hotkey_report_mode;
1446 static u16 *hotkey_keycode_map;
1448 static struct attribute_set *hotkey_dev_attributes;
1450 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1451 #define HOTKEY_CONFIG_CRITICAL_START \
1452 do { \
1453 mutex_lock(&hotkey_thread_data_mutex); \
1454 hotkey_config_change++; \
1455 } while (0);
1456 #define HOTKEY_CONFIG_CRITICAL_END \
1457 mutex_unlock(&hotkey_thread_data_mutex);
1458 #else
1459 #define HOTKEY_CONFIG_CRITICAL_START
1460 #define HOTKEY_CONFIG_CRITICAL_END
1461 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1463 /* HKEY.MHKG() return bits */
1464 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1466 static int hotkey_get_wlsw(int *status)
1468 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1469 if (dbg_wlswemul) {
1470 *status = !!tpacpi_wlsw_emulstate;
1471 return 0;
1473 #endif
1474 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1475 return -EIO;
1476 return 0;
1479 static int hotkey_get_tablet_mode(int *status)
1481 int s;
1483 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1484 return -EIO;
1486 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1487 return 0;
1491 * Call with hotkey_mutex held
1493 static int hotkey_mask_get(void)
1495 u32 m = 0;
1497 if (tp_features.hotkey_mask) {
1498 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1499 return -EIO;
1501 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1503 return 0;
1507 * Call with hotkey_mutex held
1509 static int hotkey_mask_set(u32 mask)
1511 int i;
1512 int rc = 0;
1514 if (tp_features.hotkey_mask) {
1515 if (!tp_warned.hotkey_mask_ff &&
1516 (mask == 0xffff || mask == 0xffffff ||
1517 mask == 0xffffffff)) {
1518 tp_warned.hotkey_mask_ff = 1;
1519 printk(TPACPI_NOTICE
1520 "setting the hotkey mask to 0x%08x is likely "
1521 "not the best way to go about it\n", mask);
1522 printk(TPACPI_NOTICE
1523 "please consider using the driver defaults, "
1524 "and refer to up-to-date thinkpad-acpi "
1525 "documentation\n");
1528 HOTKEY_CONFIG_CRITICAL_START
1529 for (i = 0; i < 32; i++) {
1530 u32 m = 1 << i;
1531 /* enable in firmware mask only keys not in NVRAM
1532 * mode, but enable the key in the cached hotkey_mask
1533 * regardless of mode, or the key will end up
1534 * disabled by hotkey_mask_get() */
1535 if (!acpi_evalf(hkey_handle,
1536 NULL, "MHKM", "vdd", i + 1,
1537 !!((mask & ~hotkey_source_mask) & m))) {
1538 rc = -EIO;
1539 break;
1540 } else {
1541 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1544 HOTKEY_CONFIG_CRITICAL_END
1546 /* hotkey_mask_get must be called unconditionally below */
1547 if (!hotkey_mask_get() && !rc &&
1548 (hotkey_mask & ~hotkey_source_mask) !=
1549 (mask & ~hotkey_source_mask)) {
1550 printk(TPACPI_NOTICE
1551 "requested hot key mask 0x%08x, but "
1552 "firmware forced it to 0x%08x\n",
1553 mask, hotkey_mask);
1555 } else {
1556 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1557 HOTKEY_CONFIG_CRITICAL_START
1558 hotkey_mask = mask & hotkey_source_mask;
1559 HOTKEY_CONFIG_CRITICAL_END
1560 hotkey_mask_get();
1561 if (hotkey_mask != mask) {
1562 printk(TPACPI_NOTICE
1563 "requested hot key mask 0x%08x, "
1564 "forced to 0x%08x (NVRAM poll mask is "
1565 "0x%08x): no firmware mask support\n",
1566 mask, hotkey_mask, hotkey_source_mask);
1568 #else
1569 hotkey_mask_get();
1570 rc = -ENXIO;
1571 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1574 return rc;
1577 static int hotkey_status_get(int *status)
1579 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1580 return -EIO;
1582 return 0;
1585 static int hotkey_status_set(bool enable)
1587 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1588 return -EIO;
1590 return 0;
1593 static void tpacpi_input_send_tabletsw(void)
1595 int state;
1597 if (tp_features.hotkey_tablet &&
1598 !hotkey_get_tablet_mode(&state)) {
1599 mutex_lock(&tpacpi_inputdev_send_mutex);
1601 input_report_switch(tpacpi_inputdev,
1602 SW_TABLET_MODE, !!state);
1603 input_sync(tpacpi_inputdev);
1605 mutex_unlock(&tpacpi_inputdev_send_mutex);
1609 static void tpacpi_input_send_key(unsigned int scancode)
1611 unsigned int keycode;
1613 keycode = hotkey_keycode_map[scancode];
1615 if (keycode != KEY_RESERVED) {
1616 mutex_lock(&tpacpi_inputdev_send_mutex);
1618 input_report_key(tpacpi_inputdev, keycode, 1);
1619 if (keycode == KEY_UNKNOWN)
1620 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1621 scancode);
1622 input_sync(tpacpi_inputdev);
1624 input_report_key(tpacpi_inputdev, keycode, 0);
1625 if (keycode == KEY_UNKNOWN)
1626 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1627 scancode);
1628 input_sync(tpacpi_inputdev);
1630 mutex_unlock(&tpacpi_inputdev_send_mutex);
1634 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1635 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1637 static void tpacpi_hotkey_send_key(unsigned int scancode)
1639 tpacpi_input_send_key(scancode);
1640 if (hotkey_report_mode < 2) {
1641 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1642 0x80, 0x1001 + scancode);
1646 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1648 u8 d;
1650 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1651 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1652 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1653 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1654 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1655 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1657 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1658 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1659 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1661 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1662 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1663 n->displayexp_toggle =
1664 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1666 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1667 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1668 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1669 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1670 n->brightness_toggle =
1671 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1673 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1674 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1675 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1676 >> TP_NVRAM_POS_LEVEL_VOLUME;
1677 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1678 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1682 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1683 do { \
1684 if ((mask & (1 << __scancode)) && \
1685 oldn->__member != newn->__member) \
1686 tpacpi_hotkey_send_key(__scancode); \
1687 } while (0)
1689 #define TPACPI_MAY_SEND_KEY(__scancode) \
1690 do { if (mask & (1 << __scancode)) \
1691 tpacpi_hotkey_send_key(__scancode); } while (0)
1693 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1694 struct tp_nvram_state *newn,
1695 u32 mask)
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1698 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1699 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1700 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1702 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1704 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1706 /* handle volume */
1707 if (oldn->volume_toggle != newn->volume_toggle) {
1708 if (oldn->mute != newn->mute) {
1709 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1711 if (oldn->volume_level > newn->volume_level) {
1712 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1713 } else if (oldn->volume_level < newn->volume_level) {
1714 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1715 } else if (oldn->mute == newn->mute) {
1716 /* repeated key presses that didn't change state */
1717 if (newn->mute) {
1718 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1719 } else if (newn->volume_level != 0) {
1720 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1721 } else {
1722 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1727 /* handle brightness */
1728 if (oldn->brightness_toggle != newn->brightness_toggle) {
1729 if (oldn->brightness_level < newn->brightness_level) {
1730 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1731 } else if (oldn->brightness_level > newn->brightness_level) {
1732 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1733 } else {
1734 /* repeated key presses that didn't change state */
1735 if (newn->brightness_level != 0) {
1736 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1737 } else {
1738 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1744 #undef TPACPI_COMPARE_KEY
1745 #undef TPACPI_MAY_SEND_KEY
1747 static int hotkey_kthread(void *data)
1749 struct tp_nvram_state s[2];
1750 u32 mask;
1751 unsigned int si, so;
1752 unsigned long t;
1753 unsigned int change_detector, must_reset;
1755 mutex_lock(&hotkey_thread_mutex);
1757 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1758 goto exit;
1760 set_freezable();
1762 so = 0;
1763 si = 1;
1764 t = 0;
1766 /* Initial state for compares */
1767 mutex_lock(&hotkey_thread_data_mutex);
1768 change_detector = hotkey_config_change;
1769 mask = hotkey_source_mask & hotkey_mask;
1770 mutex_unlock(&hotkey_thread_data_mutex);
1771 hotkey_read_nvram(&s[so], mask);
1773 while (!kthread_should_stop() && hotkey_poll_freq) {
1774 if (t == 0)
1775 t = 1000/hotkey_poll_freq;
1776 t = msleep_interruptible(t);
1777 if (unlikely(kthread_should_stop()))
1778 break;
1779 must_reset = try_to_freeze();
1780 if (t > 0 && !must_reset)
1781 continue;
1783 mutex_lock(&hotkey_thread_data_mutex);
1784 if (must_reset || hotkey_config_change != change_detector) {
1785 /* forget old state on thaw or config change */
1786 si = so;
1787 t = 0;
1788 change_detector = hotkey_config_change;
1790 mask = hotkey_source_mask & hotkey_mask;
1791 mutex_unlock(&hotkey_thread_data_mutex);
1793 if (likely(mask)) {
1794 hotkey_read_nvram(&s[si], mask);
1795 if (likely(si != so)) {
1796 hotkey_compare_and_issue_event(&s[so], &s[si],
1797 mask);
1801 so = si;
1802 si ^= 1;
1805 exit:
1806 mutex_unlock(&hotkey_thread_mutex);
1807 return 0;
1810 static void hotkey_poll_stop_sync(void)
1812 if (tpacpi_hotkey_task) {
1813 if (frozen(tpacpi_hotkey_task) ||
1814 freezing(tpacpi_hotkey_task))
1815 thaw_process(tpacpi_hotkey_task);
1817 kthread_stop(tpacpi_hotkey_task);
1818 tpacpi_hotkey_task = NULL;
1819 mutex_lock(&hotkey_thread_mutex);
1820 /* at this point, the thread did exit */
1821 mutex_unlock(&hotkey_thread_mutex);
1825 /* call with hotkey_mutex held */
1826 static void hotkey_poll_setup(int may_warn)
1828 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1829 hotkey_poll_freq > 0 &&
1830 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1831 if (!tpacpi_hotkey_task) {
1832 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1833 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1834 if (IS_ERR(tpacpi_hotkey_task)) {
1835 tpacpi_hotkey_task = NULL;
1836 printk(TPACPI_ERR
1837 "could not create kernel thread "
1838 "for hotkey polling\n");
1841 } else {
1842 hotkey_poll_stop_sync();
1843 if (may_warn &&
1844 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1845 printk(TPACPI_NOTICE
1846 "hot keys 0x%08x require polling, "
1847 "which is currently disabled\n",
1848 hotkey_source_mask);
1853 static void hotkey_poll_setup_safe(int may_warn)
1855 mutex_lock(&hotkey_mutex);
1856 hotkey_poll_setup(may_warn);
1857 mutex_unlock(&hotkey_mutex);
1860 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1862 static void hotkey_poll_setup_safe(int __unused)
1866 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1868 static int hotkey_inputdev_open(struct input_dev *dev)
1870 switch (tpacpi_lifecycle) {
1871 case TPACPI_LIFE_INIT:
1873 * hotkey_init will call hotkey_poll_setup_safe
1874 * at the appropriate moment
1876 return 0;
1877 case TPACPI_LIFE_EXITING:
1878 return -EBUSY;
1879 case TPACPI_LIFE_RUNNING:
1880 hotkey_poll_setup_safe(0);
1881 return 0;
1884 /* Should only happen if tpacpi_lifecycle is corrupt */
1885 BUG();
1886 return -EBUSY;
1889 static void hotkey_inputdev_close(struct input_dev *dev)
1891 /* disable hotkey polling when possible */
1892 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1893 hotkey_poll_setup_safe(0);
1896 /* sysfs hotkey enable ------------------------------------------------- */
1897 static ssize_t hotkey_enable_show(struct device *dev,
1898 struct device_attribute *attr,
1899 char *buf)
1901 int res, status;
1903 printk_deprecated_attribute("hotkey_enable",
1904 "Hotkey reporting is always enabled");
1906 res = hotkey_status_get(&status);
1907 if (res)
1908 return res;
1910 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1913 static ssize_t hotkey_enable_store(struct device *dev,
1914 struct device_attribute *attr,
1915 const char *buf, size_t count)
1917 unsigned long t;
1919 printk_deprecated_attribute("hotkey_enable",
1920 "Hotkeys can be disabled through hotkey_mask");
1922 if (parse_strtoul(buf, 1, &t))
1923 return -EINVAL;
1925 if (t == 0)
1926 return -EPERM;
1928 return count;
1931 static struct device_attribute dev_attr_hotkey_enable =
1932 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1933 hotkey_enable_show, hotkey_enable_store);
1935 /* sysfs hotkey mask --------------------------------------------------- */
1936 static ssize_t hotkey_mask_show(struct device *dev,
1937 struct device_attribute *attr,
1938 char *buf)
1940 int res;
1942 if (mutex_lock_killable(&hotkey_mutex))
1943 return -ERESTARTSYS;
1944 res = hotkey_mask_get();
1945 mutex_unlock(&hotkey_mutex);
1947 return (res)?
1948 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1951 static ssize_t hotkey_mask_store(struct device *dev,
1952 struct device_attribute *attr,
1953 const char *buf, size_t count)
1955 unsigned long t;
1956 int res;
1958 if (parse_strtoul(buf, 0xffffffffUL, &t))
1959 return -EINVAL;
1961 if (mutex_lock_killable(&hotkey_mutex))
1962 return -ERESTARTSYS;
1964 res = hotkey_mask_set(t);
1966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1967 hotkey_poll_setup(1);
1968 #endif
1970 mutex_unlock(&hotkey_mutex);
1972 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
1974 return (res) ? res : count;
1977 static struct device_attribute dev_attr_hotkey_mask =
1978 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1979 hotkey_mask_show, hotkey_mask_store);
1981 /* sysfs hotkey bios_enabled ------------------------------------------- */
1982 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1983 struct device_attribute *attr,
1984 char *buf)
1986 return sprintf(buf, "0\n");
1989 static struct device_attribute dev_attr_hotkey_bios_enabled =
1990 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1992 /* sysfs hotkey bios_mask ---------------------------------------------- */
1993 static ssize_t hotkey_bios_mask_show(struct device *dev,
1994 struct device_attribute *attr,
1995 char *buf)
1997 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2000 static struct device_attribute dev_attr_hotkey_bios_mask =
2001 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2003 /* sysfs hotkey all_mask ----------------------------------------------- */
2004 static ssize_t hotkey_all_mask_show(struct device *dev,
2005 struct device_attribute *attr,
2006 char *buf)
2008 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2009 hotkey_all_mask | hotkey_source_mask);
2012 static struct device_attribute dev_attr_hotkey_all_mask =
2013 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2015 /* sysfs hotkey recommended_mask --------------------------------------- */
2016 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2017 struct device_attribute *attr,
2018 char *buf)
2020 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2021 (hotkey_all_mask | hotkey_source_mask)
2022 & ~hotkey_reserved_mask);
2025 static struct device_attribute dev_attr_hotkey_recommended_mask =
2026 __ATTR(hotkey_recommended_mask, S_IRUGO,
2027 hotkey_recommended_mask_show, NULL);
2029 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2031 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2032 static ssize_t hotkey_source_mask_show(struct device *dev,
2033 struct device_attribute *attr,
2034 char *buf)
2036 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2039 static ssize_t hotkey_source_mask_store(struct device *dev,
2040 struct device_attribute *attr,
2041 const char *buf, size_t count)
2043 unsigned long t;
2045 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2046 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2047 return -EINVAL;
2049 if (mutex_lock_killable(&hotkey_mutex))
2050 return -ERESTARTSYS;
2052 HOTKEY_CONFIG_CRITICAL_START
2053 hotkey_source_mask = t;
2054 HOTKEY_CONFIG_CRITICAL_END
2056 hotkey_poll_setup(1);
2058 mutex_unlock(&hotkey_mutex);
2060 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2062 return count;
2065 static struct device_attribute dev_attr_hotkey_source_mask =
2066 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2067 hotkey_source_mask_show, hotkey_source_mask_store);
2069 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2070 static ssize_t hotkey_poll_freq_show(struct device *dev,
2071 struct device_attribute *attr,
2072 char *buf)
2074 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2077 static ssize_t hotkey_poll_freq_store(struct device *dev,
2078 struct device_attribute *attr,
2079 const char *buf, size_t count)
2081 unsigned long t;
2083 if (parse_strtoul(buf, 25, &t))
2084 return -EINVAL;
2086 if (mutex_lock_killable(&hotkey_mutex))
2087 return -ERESTARTSYS;
2089 hotkey_poll_freq = t;
2091 hotkey_poll_setup(1);
2092 mutex_unlock(&hotkey_mutex);
2094 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2096 return count;
2099 static struct device_attribute dev_attr_hotkey_poll_freq =
2100 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2101 hotkey_poll_freq_show, hotkey_poll_freq_store);
2103 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2105 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2106 static ssize_t hotkey_radio_sw_show(struct device *dev,
2107 struct device_attribute *attr,
2108 char *buf)
2110 int res, s;
2111 res = hotkey_get_wlsw(&s);
2112 if (res < 0)
2113 return res;
2115 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2118 static struct device_attribute dev_attr_hotkey_radio_sw =
2119 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2121 static void hotkey_radio_sw_notify_change(void)
2123 if (tp_features.hotkey_wlsw)
2124 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2125 "hotkey_radio_sw");
2128 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2129 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2130 struct device_attribute *attr,
2131 char *buf)
2133 int res, s;
2134 res = hotkey_get_tablet_mode(&s);
2135 if (res < 0)
2136 return res;
2138 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2141 static struct device_attribute dev_attr_hotkey_tablet_mode =
2142 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2144 static void hotkey_tablet_mode_notify_change(void)
2146 if (tp_features.hotkey_tablet)
2147 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2148 "hotkey_tablet_mode");
2151 /* sysfs hotkey report_mode -------------------------------------------- */
2152 static ssize_t hotkey_report_mode_show(struct device *dev,
2153 struct device_attribute *attr,
2154 char *buf)
2156 return snprintf(buf, PAGE_SIZE, "%d\n",
2157 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2160 static struct device_attribute dev_attr_hotkey_report_mode =
2161 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2163 /* sysfs wakeup reason (pollable) -------------------------------------- */
2164 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2165 struct device_attribute *attr,
2166 char *buf)
2168 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2171 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2172 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2174 static void hotkey_wakeup_reason_notify_change(void)
2176 if (tp_features.hotkey_mask)
2177 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2178 "wakeup_reason");
2181 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2182 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2183 struct device_attribute *attr,
2184 char *buf)
2186 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2189 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2190 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2191 hotkey_wakeup_hotunplug_complete_show, NULL);
2193 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2195 if (tp_features.hotkey_mask)
2196 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2197 "wakeup_hotunplug_complete");
2200 /* --------------------------------------------------------------------- */
2202 static struct attribute *hotkey_attributes[] __initdata = {
2203 &dev_attr_hotkey_enable.attr,
2204 &dev_attr_hotkey_bios_enabled.attr,
2205 &dev_attr_hotkey_report_mode.attr,
2206 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2207 &dev_attr_hotkey_mask.attr,
2208 &dev_attr_hotkey_all_mask.attr,
2209 &dev_attr_hotkey_recommended_mask.attr,
2210 &dev_attr_hotkey_source_mask.attr,
2211 &dev_attr_hotkey_poll_freq.attr,
2212 #endif
2215 static struct attribute *hotkey_mask_attributes[] __initdata = {
2216 &dev_attr_hotkey_bios_mask.attr,
2217 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2218 &dev_attr_hotkey_mask.attr,
2219 &dev_attr_hotkey_all_mask.attr,
2220 &dev_attr_hotkey_recommended_mask.attr,
2221 #endif
2222 &dev_attr_hotkey_wakeup_reason.attr,
2223 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2226 static void bluetooth_update_rfk(void);
2227 static void wan_update_rfk(void);
2228 static void uwb_update_rfk(void);
2229 static void tpacpi_send_radiosw_update(void)
2231 int wlsw;
2233 /* Sync these BEFORE sending any rfkill events */
2234 if (tp_features.bluetooth)
2235 bluetooth_update_rfk();
2236 if (tp_features.wan)
2237 wan_update_rfk();
2238 if (tp_features.uwb)
2239 uwb_update_rfk();
2241 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2242 mutex_lock(&tpacpi_inputdev_send_mutex);
2244 input_report_switch(tpacpi_inputdev,
2245 SW_RFKILL_ALL, !!wlsw);
2246 input_sync(tpacpi_inputdev);
2248 mutex_unlock(&tpacpi_inputdev_send_mutex);
2250 hotkey_radio_sw_notify_change();
2253 static void hotkey_exit(void)
2255 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2256 hotkey_poll_stop_sync();
2257 #endif
2259 if (hotkey_dev_attributes)
2260 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2262 kfree(hotkey_keycode_map);
2264 if (tp_features.hotkey) {
2265 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2266 "restoring original hot key mask\n");
2267 /* no short-circuit boolean operator below! */
2268 if ((hotkey_mask_set(hotkey_orig_mask) |
2269 hotkey_status_set(false)) != 0)
2270 printk(TPACPI_ERR
2271 "failed to restore hot key mask "
2272 "to BIOS defaults\n");
2276 static int __init hotkey_init(struct ibm_init_struct *iibm)
2278 /* Requirements for changing the default keymaps:
2280 * 1. Many of the keys are mapped to KEY_RESERVED for very
2281 * good reasons. Do not change them unless you have deep
2282 * knowledge on the IBM and Lenovo ThinkPad firmware for
2283 * the various ThinkPad models. The driver behaves
2284 * differently for KEY_RESERVED: such keys have their
2285 * hot key mask *unset* in mask_recommended, and also
2286 * in the initial hot key mask programmed into the
2287 * firmware at driver load time, which means the firm-
2288 * ware may react very differently if you change them to
2289 * something else;
2291 * 2. You must be subscribed to the linux-thinkpad and
2292 * ibm-acpi-devel mailing lists, and you should read the
2293 * list archives since 2007 if you want to change the
2294 * keymaps. This requirement exists so that you will
2295 * know the past history of problems with the thinkpad-
2296 * acpi driver keymaps, and also that you will be
2297 * listening to any bug reports;
2299 * 3. Do not send thinkpad-acpi specific patches directly to
2300 * for merging, *ever*. Send them to the linux-acpi
2301 * mailinglist for comments. Merging is to be done only
2302 * through acpi-test and the ACPI maintainer.
2304 * If the above is too much to ask, don't change the keymap.
2305 * Ask the thinkpad-acpi maintainer to do it, instead.
2307 static u16 ibm_keycode_map[] __initdata = {
2308 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2309 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2310 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2311 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2313 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2314 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2315 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2316 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2318 /* brightness: firmware always reacts to them, unless
2319 * X.org did some tricks in the radeon BIOS scratch
2320 * registers of *some* models */
2321 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2322 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2324 /* Thinklight: firmware always react to it */
2325 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2327 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2328 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2330 /* Volume: firmware always react to it and reprograms
2331 * the built-in *extra* mixer. Never map it to control
2332 * another mixer by default. */
2333 KEY_RESERVED, /* 0x14: VOLUME UP */
2334 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2335 KEY_RESERVED, /* 0x16: MUTE */
2337 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2339 /* (assignments unknown, please report if found) */
2340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2341 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2343 static u16 lenovo_keycode_map[] __initdata = {
2344 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2345 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2346 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2347 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2349 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2350 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2351 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2352 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2354 /* These either have to go through ACPI video, or
2355 * act like in the IBM ThinkPads, so don't ever
2356 * enable them by default */
2357 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2358 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2360 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2362 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2363 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2365 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2366 * react to it and reprograms the built-in *extra* mixer.
2367 * Never map it to control another mixer by default.
2369 * T60?, T61, R60?, R61: firmware and EC tries to send
2370 * these over the regular keyboard, so these are no-ops,
2371 * but there are still weird bugs re. MUTE, so do not
2372 * change unless you get test reports from all Lenovo
2373 * models. May cause the BIOS to interfere with the
2374 * HDA mixer.
2376 KEY_RESERVED, /* 0x14: VOLUME UP */
2377 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2378 KEY_RESERVED, /* 0x16: MUTE */
2380 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2382 /* (assignments unknown, please report if found) */
2383 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2384 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2387 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2388 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2389 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2391 int res, i;
2392 int status;
2393 int hkeyv;
2395 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2396 "initializing hotkey subdriver\n");
2398 BUG_ON(!tpacpi_inputdev);
2399 BUG_ON(tpacpi_inputdev->open != NULL ||
2400 tpacpi_inputdev->close != NULL);
2402 TPACPI_ACPIHANDLE_INIT(hkey);
2403 mutex_init(&hotkey_mutex);
2405 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2406 mutex_init(&hotkey_thread_mutex);
2407 mutex_init(&hotkey_thread_data_mutex);
2408 #endif
2410 /* hotkey not supported on 570 */
2411 tp_features.hotkey = hkey_handle != NULL;
2413 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2414 "hotkeys are %s\n",
2415 str_supported(tp_features.hotkey));
2417 if (!tp_features.hotkey)
2418 return 1;
2420 tpacpi_disable_brightness_delay();
2422 hotkey_dev_attributes = create_attr_set(13, NULL);
2423 if (!hotkey_dev_attributes)
2424 return -ENOMEM;
2425 res = add_many_to_attr_set(hotkey_dev_attributes,
2426 hotkey_attributes,
2427 ARRAY_SIZE(hotkey_attributes));
2428 if (res)
2429 goto err_exit;
2431 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2432 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2433 for HKEY interface version 0x100 */
2434 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2435 if ((hkeyv >> 8) != 1) {
2436 printk(TPACPI_ERR "unknown version of the "
2437 "HKEY interface: 0x%x\n", hkeyv);
2438 printk(TPACPI_ERR "please report this to %s\n",
2439 TPACPI_MAIL);
2440 } else {
2442 * MHKV 0x100 in A31, R40, R40e,
2443 * T4x, X31, and later
2445 tp_features.hotkey_mask = 1;
2446 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2447 "firmware HKEY interface version: 0x%x\n",
2448 hkeyv);
2452 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2453 "hotkey masks are %s\n",
2454 str_supported(tp_features.hotkey_mask));
2456 if (tp_features.hotkey_mask) {
2457 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2458 "MHKA", "qd")) {
2459 printk(TPACPI_ERR
2460 "missing MHKA handler, "
2461 "please report this to %s\n",
2462 TPACPI_MAIL);
2463 /* FN+F12, FN+F4, FN+F3 */
2464 hotkey_all_mask = 0x080cU;
2468 /* hotkey_source_mask *must* be zero for
2469 * the first hotkey_mask_get */
2470 if (tp_features.hotkey_mask) {
2471 res = hotkey_mask_get();
2472 if (res)
2473 goto err_exit;
2475 hotkey_orig_mask = hotkey_mask;
2476 res = add_many_to_attr_set(
2477 hotkey_dev_attributes,
2478 hotkey_mask_attributes,
2479 ARRAY_SIZE(hotkey_mask_attributes));
2480 if (res)
2481 goto err_exit;
2484 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2485 if (tp_features.hotkey_mask) {
2486 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2487 & ~hotkey_all_mask;
2488 } else {
2489 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2492 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2493 "hotkey source mask 0x%08x, polling freq %d\n",
2494 hotkey_source_mask, hotkey_poll_freq);
2495 #endif
2497 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2498 if (dbg_wlswemul) {
2499 tp_features.hotkey_wlsw = 1;
2500 printk(TPACPI_INFO
2501 "radio switch emulation enabled\n");
2502 } else
2503 #endif
2504 /* Not all thinkpads have a hardware radio switch */
2505 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2506 tp_features.hotkey_wlsw = 1;
2507 printk(TPACPI_INFO
2508 "radio switch found; radios are %s\n",
2509 enabled(status, 0));
2511 if (tp_features.hotkey_wlsw)
2512 res = add_to_attr_set(hotkey_dev_attributes,
2513 &dev_attr_hotkey_radio_sw.attr);
2515 /* For X41t, X60t, X61t Tablets... */
2516 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2517 tp_features.hotkey_tablet = 1;
2518 printk(TPACPI_INFO
2519 "possible tablet mode switch found; "
2520 "ThinkPad in %s mode\n",
2521 (status & TP_HOTKEY_TABLET_MASK)?
2522 "tablet" : "laptop");
2523 res = add_to_attr_set(hotkey_dev_attributes,
2524 &dev_attr_hotkey_tablet_mode.attr);
2527 if (!res)
2528 res = register_attr_set_with_sysfs(
2529 hotkey_dev_attributes,
2530 &tpacpi_pdev->dev.kobj);
2531 if (res)
2532 goto err_exit;
2534 /* Set up key map */
2536 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2537 GFP_KERNEL);
2538 if (!hotkey_keycode_map) {
2539 printk(TPACPI_ERR
2540 "failed to allocate memory for key map\n");
2541 res = -ENOMEM;
2542 goto err_exit;
2545 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2546 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2547 "using Lenovo default hot key map\n");
2548 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2549 TPACPI_HOTKEY_MAP_SIZE);
2550 } else {
2551 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2552 "using IBM default hot key map\n");
2553 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2554 TPACPI_HOTKEY_MAP_SIZE);
2557 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2558 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2559 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2560 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2561 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2562 tpacpi_inputdev->keycode = hotkey_keycode_map;
2563 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2564 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2565 set_bit(hotkey_keycode_map[i],
2566 tpacpi_inputdev->keybit);
2567 } else {
2568 if (i < sizeof(hotkey_reserved_mask)*8)
2569 hotkey_reserved_mask |= 1 << i;
2573 if (tp_features.hotkey_wlsw) {
2574 set_bit(EV_SW, tpacpi_inputdev->evbit);
2575 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2577 if (tp_features.hotkey_tablet) {
2578 set_bit(EV_SW, tpacpi_inputdev->evbit);
2579 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2582 /* Do not issue duplicate brightness change events to
2583 * userspace */
2584 if (!tp_features.bright_acpimode)
2585 /* update bright_acpimode... */
2586 tpacpi_check_std_acpi_brightness_support();
2588 if (tp_features.bright_acpimode) {
2589 printk(TPACPI_INFO
2590 "This ThinkPad has standard ACPI backlight "
2591 "brightness control, supported by the ACPI "
2592 "video driver\n");
2593 printk(TPACPI_NOTICE
2594 "Disabling thinkpad-acpi brightness events "
2595 "by default...\n");
2597 /* The hotkey_reserved_mask change below is not
2598 * necessary while the keys are at KEY_RESERVED in the
2599 * default map, but better safe than sorry, leave it
2600 * here as a marker of what we have to do, especially
2601 * when we finally become able to set this at runtime
2602 * on response to X.org requests */
2603 hotkey_reserved_mask |=
2604 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2605 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2608 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2609 "enabling firmware HKEY event interface...\n");
2610 res = hotkey_status_set(true);
2611 if (res) {
2612 hotkey_exit();
2613 return res;
2615 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2616 & ~hotkey_reserved_mask)
2617 | hotkey_orig_mask);
2618 if (res < 0 && res != -ENXIO) {
2619 hotkey_exit();
2620 return res;
2623 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2624 "legacy ibm/hotkey event reporting over procfs %s\n",
2625 (hotkey_report_mode < 2) ?
2626 "enabled" : "disabled");
2628 tpacpi_inputdev->open = &hotkey_inputdev_open;
2629 tpacpi_inputdev->close = &hotkey_inputdev_close;
2631 hotkey_poll_setup_safe(1);
2632 tpacpi_send_radiosw_update();
2633 tpacpi_input_send_tabletsw();
2635 return 0;
2637 err_exit:
2638 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2639 hotkey_dev_attributes = NULL;
2641 return (res < 0)? res : 1;
2644 static bool hotkey_notify_hotkey(const u32 hkey,
2645 bool *send_acpi_ev,
2646 bool *ignore_acpi_ev)
2648 /* 0x1000-0x1FFF: key presses */
2649 unsigned int scancode = hkey & 0xfff;
2650 *send_acpi_ev = true;
2651 *ignore_acpi_ev = false;
2653 if (scancode > 0 && scancode < 0x21) {
2654 scancode--;
2655 if (!(hotkey_source_mask & (1 << scancode))) {
2656 tpacpi_input_send_key(scancode);
2657 *send_acpi_ev = false;
2658 } else {
2659 *ignore_acpi_ev = true;
2661 return true;
2663 return false;
2666 static bool hotkey_notify_wakeup(const u32 hkey,
2667 bool *send_acpi_ev,
2668 bool *ignore_acpi_ev)
2670 /* 0x2000-0x2FFF: Wakeup reason */
2671 *send_acpi_ev = true;
2672 *ignore_acpi_ev = false;
2674 switch (hkey) {
2675 case 0x2304: /* suspend, undock */
2676 case 0x2404: /* hibernation, undock */
2677 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2678 *ignore_acpi_ev = true;
2679 break;
2681 case 0x2305: /* suspend, bay eject */
2682 case 0x2405: /* hibernation, bay eject */
2683 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2684 *ignore_acpi_ev = true;
2685 break;
2687 case 0x2313: /* Battery on critical low level (S3) */
2688 case 0x2413: /* Battery on critical low level (S4) */
2689 printk(TPACPI_ALERT
2690 "EMERGENCY WAKEUP: battery almost empty\n");
2691 /* how to auto-heal: */
2692 /* 2313: woke up from S3, go to S4/S5 */
2693 /* 2413: woke up from S4, go to S5 */
2694 break;
2696 default:
2697 return false;
2700 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2701 printk(TPACPI_INFO
2702 "woke up due to a hot-unplug "
2703 "request...\n");
2704 hotkey_wakeup_reason_notify_change();
2706 return true;
2709 static bool hotkey_notify_usrevent(const u32 hkey,
2710 bool *send_acpi_ev,
2711 bool *ignore_acpi_ev)
2713 /* 0x5000-0x5FFF: human interface helpers */
2714 *send_acpi_ev = true;
2715 *ignore_acpi_ev = false;
2717 switch (hkey) {
2718 case 0x5010: /* Lenovo new BIOS: brightness changed */
2719 case 0x500b: /* X61t: tablet pen inserted into bay */
2720 case 0x500c: /* X61t: tablet pen removed from bay */
2721 return true;
2723 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2724 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2725 tpacpi_input_send_tabletsw();
2726 hotkey_tablet_mode_notify_change();
2727 *send_acpi_ev = false;
2728 return true;
2730 case 0x5001:
2731 case 0x5002:
2732 /* LID switch events. Do not propagate */
2733 *ignore_acpi_ev = true;
2734 return true;
2736 default:
2737 return false;
2741 static bool hotkey_notify_thermal(const u32 hkey,
2742 bool *send_acpi_ev,
2743 bool *ignore_acpi_ev)
2745 /* 0x6000-0x6FFF: thermal alarms */
2746 *send_acpi_ev = true;
2747 *ignore_acpi_ev = false;
2749 switch (hkey) {
2750 case 0x6011:
2751 printk(TPACPI_CRIT
2752 "THERMAL ALARM: battery is too hot!\n");
2753 /* recommended action: warn user through gui */
2754 return true;
2755 case 0x6012:
2756 printk(TPACPI_ALERT
2757 "THERMAL EMERGENCY: battery is extremely hot!\n");
2758 /* recommended action: immediate sleep/hibernate */
2759 return true;
2760 case 0x6021:
2761 printk(TPACPI_CRIT
2762 "THERMAL ALARM: "
2763 "a sensor reports something is too hot!\n");
2764 /* recommended action: warn user through gui, that */
2765 /* some internal component is too hot */
2766 return true;
2767 case 0x6022:
2768 printk(TPACPI_ALERT
2769 "THERMAL EMERGENCY: "
2770 "a sensor reports something is extremely hot!\n");
2771 /* recommended action: immediate sleep/hibernate */
2772 return true;
2773 case 0x6030:
2774 printk(TPACPI_INFO
2775 "EC reports that Thermal Table has changed\n");
2776 /* recommended action: do nothing, we don't have
2777 * Lenovo ATM information */
2778 return true;
2779 default:
2780 printk(TPACPI_ALERT
2781 "THERMAL ALERT: unknown thermal alarm received\n");
2782 return false;
2786 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2788 u32 hkey;
2789 bool send_acpi_ev;
2790 bool ignore_acpi_ev;
2791 bool known_ev;
2793 if (event != 0x80) {
2794 printk(TPACPI_ERR
2795 "unknown HKEY notification event %d\n", event);
2796 /* forward it to userspace, maybe it knows how to handle it */
2797 acpi_bus_generate_netlink_event(
2798 ibm->acpi->device->pnp.device_class,
2799 dev_name(&ibm->acpi->device->dev),
2800 event, 0);
2801 return;
2804 while (1) {
2805 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2806 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2807 return;
2810 if (hkey == 0) {
2811 /* queue empty */
2812 return;
2815 send_acpi_ev = true;
2816 ignore_acpi_ev = false;
2818 switch (hkey >> 12) {
2819 case 1:
2820 /* 0x1000-0x1FFF: key presses */
2821 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2822 &ignore_acpi_ev);
2823 break;
2824 case 2:
2825 /* 0x2000-0x2FFF: Wakeup reason */
2826 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2827 &ignore_acpi_ev);
2828 break;
2829 case 3:
2830 /* 0x3000-0x3FFF: bay-related wakeups */
2831 if (hkey == 0x3003) {
2832 hotkey_autosleep_ack = 1;
2833 printk(TPACPI_INFO
2834 "bay ejected\n");
2835 hotkey_wakeup_hotunplug_complete_notify_change();
2836 known_ev = true;
2837 } else {
2838 known_ev = false;
2840 break;
2841 case 4:
2842 /* 0x4000-0x4FFF: dock-related wakeups */
2843 if (hkey == 0x4003) {
2844 hotkey_autosleep_ack = 1;
2845 printk(TPACPI_INFO
2846 "undocked\n");
2847 hotkey_wakeup_hotunplug_complete_notify_change();
2848 known_ev = true;
2849 } else {
2850 known_ev = false;
2852 break;
2853 case 5:
2854 /* 0x5000-0x5FFF: human interface helpers */
2855 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2856 &ignore_acpi_ev);
2857 break;
2858 case 6:
2859 /* 0x6000-0x6FFF: thermal alarms */
2860 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2861 &ignore_acpi_ev);
2862 break;
2863 case 7:
2864 /* 0x7000-0x7FFF: misc */
2865 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2866 tpacpi_send_radiosw_update();
2867 send_acpi_ev = 0;
2868 known_ev = true;
2869 break;
2871 /* fallthrough to default */
2872 default:
2873 known_ev = false;
2875 if (!known_ev) {
2876 printk(TPACPI_NOTICE
2877 "unhandled HKEY event 0x%04x\n", hkey);
2878 printk(TPACPI_NOTICE
2879 "please report the conditions when this "
2880 "event happened to %s\n", TPACPI_MAIL);
2883 /* Legacy events */
2884 if (!ignore_acpi_ev &&
2885 (send_acpi_ev || hotkey_report_mode < 2)) {
2886 acpi_bus_generate_proc_event(ibm->acpi->device,
2887 event, hkey);
2890 /* netlink events */
2891 if (!ignore_acpi_ev && send_acpi_ev) {
2892 acpi_bus_generate_netlink_event(
2893 ibm->acpi->device->pnp.device_class,
2894 dev_name(&ibm->acpi->device->dev),
2895 event, hkey);
2900 static void hotkey_suspend(pm_message_t state)
2902 /* Do these on suspend, we get the events on early resume! */
2903 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2904 hotkey_autosleep_ack = 0;
2907 static void hotkey_resume(void)
2909 tpacpi_disable_brightness_delay();
2911 if (hotkey_mask_get())
2912 printk(TPACPI_ERR
2913 "error while trying to read hot key mask "
2914 "from firmware\n");
2915 tpacpi_send_radiosw_update();
2916 hotkey_tablet_mode_notify_change();
2917 hotkey_wakeup_reason_notify_change();
2918 hotkey_wakeup_hotunplug_complete_notify_change();
2919 hotkey_poll_setup_safe(0);
2922 /* procfs -------------------------------------------------------------- */
2923 static int hotkey_read(char *p)
2925 int res, status;
2926 int len = 0;
2928 if (!tp_features.hotkey) {
2929 len += sprintf(p + len, "status:\t\tnot supported\n");
2930 return len;
2933 if (mutex_lock_killable(&hotkey_mutex))
2934 return -ERESTARTSYS;
2935 res = hotkey_status_get(&status);
2936 if (!res)
2937 res = hotkey_mask_get();
2938 mutex_unlock(&hotkey_mutex);
2939 if (res)
2940 return res;
2942 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2943 if (tp_features.hotkey_mask) {
2944 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2945 len += sprintf(p + len,
2946 "commands:\tenable, disable, reset, <mask>\n");
2947 } else {
2948 len += sprintf(p + len, "mask:\t\tnot supported\n");
2949 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2952 return len;
2955 static void hotkey_enabledisable_warn(void)
2957 tpacpi_log_usertask("procfs hotkey enable/disable");
2958 WARN(1, TPACPI_WARN
2959 "hotkey enable/disable functionality has been "
2960 "removed from the driver. Hotkeys are always enabled.\n");
2963 static int hotkey_write(char *buf)
2965 int res;
2966 u32 mask;
2967 char *cmd;
2969 if (!tp_features.hotkey)
2970 return -ENODEV;
2972 if (mutex_lock_killable(&hotkey_mutex))
2973 return -ERESTARTSYS;
2975 mask = hotkey_mask;
2977 res = 0;
2978 while ((cmd = next_cmd(&buf))) {
2979 if (strlencmp(cmd, "enable") == 0) {
2980 hotkey_enabledisable_warn();
2981 } else if (strlencmp(cmd, "disable") == 0) {
2982 hotkey_enabledisable_warn();
2983 res = -EPERM;
2984 } else if (strlencmp(cmd, "reset") == 0) {
2985 mask = hotkey_orig_mask;
2986 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2987 /* mask set */
2988 } else if (sscanf(cmd, "%x", &mask) == 1) {
2989 /* mask set */
2990 } else {
2991 res = -EINVAL;
2992 goto errexit;
2996 if (!res)
2997 tpacpi_disclose_usertask("procfs hotkey",
2998 "set mask to 0x%08x\n", mask);
3000 if (!res && mask != hotkey_mask)
3001 res = hotkey_mask_set(mask);
3003 errexit:
3004 mutex_unlock(&hotkey_mutex);
3005 return res;
3008 static const struct acpi_device_id ibm_htk_device_ids[] = {
3009 {TPACPI_ACPI_HKEY_HID, 0},
3010 {"", 0},
3013 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3014 .hid = ibm_htk_device_ids,
3015 .notify = hotkey_notify,
3016 .handle = &hkey_handle,
3017 .type = ACPI_DEVICE_NOTIFY,
3020 static struct ibm_struct hotkey_driver_data = {
3021 .name = "hotkey",
3022 .read = hotkey_read,
3023 .write = hotkey_write,
3024 .exit = hotkey_exit,
3025 .resume = hotkey_resume,
3026 .suspend = hotkey_suspend,
3027 .acpi = &ibm_hotkey_acpidriver,
3030 /*************************************************************************
3031 * Bluetooth subdriver
3034 enum {
3035 /* ACPI GBDC/SBDC bits */
3036 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3037 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3038 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3039 off / last state */
3042 enum {
3043 /* ACPI \BLTH commands */
3044 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3045 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3046 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3047 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3048 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3051 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3053 static struct rfkill *tpacpi_bluetooth_rfkill;
3055 static void bluetooth_suspend(pm_message_t state)
3057 /* Try to make sure radio will resume powered off */
3058 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3059 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3060 vdbg_printk(TPACPI_DBG_RFKILL,
3061 "bluetooth power down on resume request failed\n");
3064 static int bluetooth_get_radiosw(void)
3066 int status;
3068 if (!tp_features.bluetooth)
3069 return -ENODEV;
3071 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3072 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3073 return RFKILL_STATE_HARD_BLOCKED;
3075 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3076 if (dbg_bluetoothemul)
3077 return (tpacpi_bluetooth_emulstate) ?
3078 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3079 #endif
3081 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3082 return -EIO;
3084 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3085 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3088 static void bluetooth_update_rfk(void)
3090 int status;
3092 if (!tpacpi_bluetooth_rfkill)
3093 return;
3095 status = bluetooth_get_radiosw();
3096 if (status < 0)
3097 return;
3098 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3100 vdbg_printk(TPACPI_DBG_RFKILL,
3101 "forced rfkill state to %d\n",
3102 status);
3105 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3107 int status;
3109 if (!tp_features.bluetooth)
3110 return -ENODEV;
3112 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3113 * reason to risk weird behaviour. */
3114 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3115 && radio_on)
3116 return -EPERM;
3118 vdbg_printk(TPACPI_DBG_RFKILL,
3119 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3121 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3122 if (dbg_bluetoothemul) {
3123 tpacpi_bluetooth_emulstate = !!radio_on;
3124 if (update_rfk)
3125 bluetooth_update_rfk();
3126 return 0;
3128 #endif
3130 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3131 if (radio_on)
3132 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3133 else
3134 status = 0;
3135 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3136 return -EIO;
3138 if (update_rfk)
3139 bluetooth_update_rfk();
3141 return 0;
3144 /* sysfs bluetooth enable ---------------------------------------------- */
3145 static ssize_t bluetooth_enable_show(struct device *dev,
3146 struct device_attribute *attr,
3147 char *buf)
3149 int status;
3151 printk_deprecated_rfkill_attribute("bluetooth_enable");
3153 status = bluetooth_get_radiosw();
3154 if (status < 0)
3155 return status;
3157 return snprintf(buf, PAGE_SIZE, "%d\n",
3158 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3161 static ssize_t bluetooth_enable_store(struct device *dev,
3162 struct device_attribute *attr,
3163 const char *buf, size_t count)
3165 unsigned long t;
3166 int res;
3168 printk_deprecated_rfkill_attribute("bluetooth_enable");
3170 if (parse_strtoul(buf, 1, &t))
3171 return -EINVAL;
3173 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3175 res = bluetooth_set_radiosw(t, 1);
3177 return (res) ? res : count;
3180 static struct device_attribute dev_attr_bluetooth_enable =
3181 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3182 bluetooth_enable_show, bluetooth_enable_store);
3184 /* --------------------------------------------------------------------- */
3186 static struct attribute *bluetooth_attributes[] = {
3187 &dev_attr_bluetooth_enable.attr,
3188 NULL
3191 static const struct attribute_group bluetooth_attr_group = {
3192 .attrs = bluetooth_attributes,
3195 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3197 int bts = bluetooth_get_radiosw();
3199 if (bts < 0)
3200 return bts;
3202 *state = bts;
3203 return 0;
3206 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3208 dbg_printk(TPACPI_DBG_RFKILL,
3209 "request to change radio state to %d\n", state);
3210 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3213 static void bluetooth_shutdown(void)
3215 /* Order firmware to save current state to NVRAM */
3216 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3217 TP_ACPI_BLTH_SAVE_STATE))
3218 printk(TPACPI_NOTICE
3219 "failed to save bluetooth state to NVRAM\n");
3220 else
3221 vdbg_printk(TPACPI_DBG_RFKILL,
3222 "bluestooth state saved to NVRAM\n");
3225 static void bluetooth_exit(void)
3227 bluetooth_shutdown();
3229 if (tpacpi_bluetooth_rfkill)
3230 rfkill_unregister(tpacpi_bluetooth_rfkill);
3232 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3233 &bluetooth_attr_group);
3236 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3238 int res;
3239 int status = 0;
3241 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3242 "initializing bluetooth subdriver\n");
3244 TPACPI_ACPIHANDLE_INIT(hkey);
3246 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3247 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3248 tp_features.bluetooth = hkey_handle &&
3249 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3251 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3252 "bluetooth is %s, status 0x%02x\n",
3253 str_supported(tp_features.bluetooth),
3254 status);
3256 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3257 if (dbg_bluetoothemul) {
3258 tp_features.bluetooth = 1;
3259 printk(TPACPI_INFO
3260 "bluetooth switch emulation enabled\n");
3261 } else
3262 #endif
3263 if (tp_features.bluetooth &&
3264 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3265 /* no bluetooth hardware present in system */
3266 tp_features.bluetooth = 0;
3267 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3268 "bluetooth hardware not installed\n");
3271 if (!tp_features.bluetooth)
3272 return 1;
3274 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3275 &bluetooth_attr_group);
3276 if (res)
3277 return res;
3279 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3280 &tpacpi_bluetooth_rfkill,
3281 RFKILL_TYPE_BLUETOOTH,
3282 TPACPI_RFK_BLUETOOTH_SW_NAME,
3283 true,
3284 tpacpi_bluetooth_rfk_set,
3285 tpacpi_bluetooth_rfk_get);
3286 if (res) {
3287 bluetooth_exit();
3288 return res;
3291 return 0;
3294 /* procfs -------------------------------------------------------------- */
3295 static int bluetooth_read(char *p)
3297 int len = 0;
3298 int status = bluetooth_get_radiosw();
3300 if (!tp_features.bluetooth)
3301 len += sprintf(p + len, "status:\t\tnot supported\n");
3302 else {
3303 len += sprintf(p + len, "status:\t\t%s\n",
3304 (status == RFKILL_STATE_UNBLOCKED) ?
3305 "enabled" : "disabled");
3306 len += sprintf(p + len, "commands:\tenable, disable\n");
3309 return len;
3312 static int bluetooth_write(char *buf)
3314 char *cmd;
3315 int state = -1;
3317 if (!tp_features.bluetooth)
3318 return -ENODEV;
3320 while ((cmd = next_cmd(&buf))) {
3321 if (strlencmp(cmd, "enable") == 0) {
3322 state = 1;
3323 } else if (strlencmp(cmd, "disable") == 0) {
3324 state = 0;
3325 } else
3326 return -EINVAL;
3329 if (state != -1) {
3330 tpacpi_disclose_usertask("procfs bluetooth",
3331 "attempt to %s\n",
3332 state ? "enable" : "disable");
3333 bluetooth_set_radiosw(state, 1);
3336 return 0;
3339 static struct ibm_struct bluetooth_driver_data = {
3340 .name = "bluetooth",
3341 .read = bluetooth_read,
3342 .write = bluetooth_write,
3343 .exit = bluetooth_exit,
3344 .suspend = bluetooth_suspend,
3345 .shutdown = bluetooth_shutdown,
3348 /*************************************************************************
3349 * Wan subdriver
3352 enum {
3353 /* ACPI GWAN/SWAN bits */
3354 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3355 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3356 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3357 off / last state */
3360 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3362 static struct rfkill *tpacpi_wan_rfkill;
3364 static void wan_suspend(pm_message_t state)
3366 /* Try to make sure radio will resume powered off */
3367 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3368 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3369 vdbg_printk(TPACPI_DBG_RFKILL,
3370 "WWAN power down on resume request failed\n");
3373 static int wan_get_radiosw(void)
3375 int status;
3377 if (!tp_features.wan)
3378 return -ENODEV;
3380 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3381 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3382 return RFKILL_STATE_HARD_BLOCKED;
3384 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3385 if (dbg_wwanemul)
3386 return (tpacpi_wwan_emulstate) ?
3387 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3388 #endif
3390 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3391 return -EIO;
3393 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3394 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3397 static void wan_update_rfk(void)
3399 int status;
3401 if (!tpacpi_wan_rfkill)
3402 return;
3404 status = wan_get_radiosw();
3405 if (status < 0)
3406 return;
3407 rfkill_force_state(tpacpi_wan_rfkill, status);
3409 vdbg_printk(TPACPI_DBG_RFKILL,
3410 "forced rfkill state to %d\n",
3411 status);
3414 static int wan_set_radiosw(int radio_on, int update_rfk)
3416 int status;
3418 if (!tp_features.wan)
3419 return -ENODEV;
3421 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3422 * reason to risk weird behaviour. */
3423 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3424 && radio_on)
3425 return -EPERM;
3427 vdbg_printk(TPACPI_DBG_RFKILL,
3428 "will %s WWAN\n", radio_on ? "enable" : "disable");
3430 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3431 if (dbg_wwanemul) {
3432 tpacpi_wwan_emulstate = !!radio_on;
3433 if (update_rfk)
3434 wan_update_rfk();
3435 return 0;
3437 #endif
3439 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3440 if (radio_on)
3441 status = TP_ACPI_WANCARD_RADIOSSW;
3442 else
3443 status = 0;
3444 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3445 return -EIO;
3447 if (update_rfk)
3448 wan_update_rfk();
3450 return 0;
3453 /* sysfs wan enable ---------------------------------------------------- */
3454 static ssize_t wan_enable_show(struct device *dev,
3455 struct device_attribute *attr,
3456 char *buf)
3458 int status;
3460 printk_deprecated_rfkill_attribute("wwan_enable");
3462 status = wan_get_radiosw();
3463 if (status < 0)
3464 return status;
3466 return snprintf(buf, PAGE_SIZE, "%d\n",
3467 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3470 static ssize_t wan_enable_store(struct device *dev,
3471 struct device_attribute *attr,
3472 const char *buf, size_t count)
3474 unsigned long t;
3475 int res;
3477 printk_deprecated_rfkill_attribute("wwan_enable");
3479 if (parse_strtoul(buf, 1, &t))
3480 return -EINVAL;
3482 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3484 res = wan_set_radiosw(t, 1);
3486 return (res) ? res : count;
3489 static struct device_attribute dev_attr_wan_enable =
3490 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3491 wan_enable_show, wan_enable_store);
3493 /* --------------------------------------------------------------------- */
3495 static struct attribute *wan_attributes[] = {
3496 &dev_attr_wan_enable.attr,
3497 NULL
3500 static const struct attribute_group wan_attr_group = {
3501 .attrs = wan_attributes,
3504 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3506 int wans = wan_get_radiosw();
3508 if (wans < 0)
3509 return wans;
3511 *state = wans;
3512 return 0;
3515 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3517 dbg_printk(TPACPI_DBG_RFKILL,
3518 "request to change radio state to %d\n", state);
3519 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3522 static void wan_shutdown(void)
3524 /* Order firmware to save current state to NVRAM */
3525 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3526 TP_ACPI_WGSV_SAVE_STATE))
3527 printk(TPACPI_NOTICE
3528 "failed to save WWAN state to NVRAM\n");
3529 else
3530 vdbg_printk(TPACPI_DBG_RFKILL,
3531 "WWAN state saved to NVRAM\n");
3534 static void wan_exit(void)
3536 wan_shutdown();
3538 if (tpacpi_wan_rfkill)
3539 rfkill_unregister(tpacpi_wan_rfkill);
3541 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3542 &wan_attr_group);
3545 static int __init wan_init(struct ibm_init_struct *iibm)
3547 int res;
3548 int status = 0;
3550 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3551 "initializing wan subdriver\n");
3553 TPACPI_ACPIHANDLE_INIT(hkey);
3555 tp_features.wan = hkey_handle &&
3556 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3558 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3559 "wan is %s, status 0x%02x\n",
3560 str_supported(tp_features.wan),
3561 status);
3563 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3564 if (dbg_wwanemul) {
3565 tp_features.wan = 1;
3566 printk(TPACPI_INFO
3567 "wwan switch emulation enabled\n");
3568 } else
3569 #endif
3570 if (tp_features.wan &&
3571 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3572 /* no wan hardware present in system */
3573 tp_features.wan = 0;
3574 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3575 "wan hardware not installed\n");
3578 if (!tp_features.wan)
3579 return 1;
3581 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3582 &wan_attr_group);
3583 if (res)
3584 return res;
3586 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3587 &tpacpi_wan_rfkill,
3588 RFKILL_TYPE_WWAN,
3589 TPACPI_RFK_WWAN_SW_NAME,
3590 true,
3591 tpacpi_wan_rfk_set,
3592 tpacpi_wan_rfk_get);
3593 if (res) {
3594 wan_exit();
3595 return res;
3598 return 0;
3601 /* procfs -------------------------------------------------------------- */
3602 static int wan_read(char *p)
3604 int len = 0;
3605 int status = wan_get_radiosw();
3607 tpacpi_disclose_usertask("procfs wan", "read");
3609 if (!tp_features.wan)
3610 len += sprintf(p + len, "status:\t\tnot supported\n");
3611 else {
3612 len += sprintf(p + len, "status:\t\t%s\n",
3613 (status == RFKILL_STATE_UNBLOCKED) ?
3614 "enabled" : "disabled");
3615 len += sprintf(p + len, "commands:\tenable, disable\n");
3618 return len;
3621 static int wan_write(char *buf)
3623 char *cmd;
3624 int state = -1;
3626 if (!tp_features.wan)
3627 return -ENODEV;
3629 while ((cmd = next_cmd(&buf))) {
3630 if (strlencmp(cmd, "enable") == 0) {
3631 state = 1;
3632 } else if (strlencmp(cmd, "disable") == 0) {
3633 state = 0;
3634 } else
3635 return -EINVAL;
3638 if (state != -1) {
3639 tpacpi_disclose_usertask("procfs wan",
3640 "attempt to %s\n",
3641 state ? "enable" : "disable");
3642 wan_set_radiosw(state, 1);
3645 return 0;
3648 static struct ibm_struct wan_driver_data = {
3649 .name = "wan",
3650 .read = wan_read,
3651 .write = wan_write,
3652 .exit = wan_exit,
3653 .suspend = wan_suspend,
3654 .shutdown = wan_shutdown,
3657 /*************************************************************************
3658 * UWB subdriver
3661 enum {
3662 /* ACPI GUWB/SUWB bits */
3663 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3664 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3667 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3669 static struct rfkill *tpacpi_uwb_rfkill;
3671 static int uwb_get_radiosw(void)
3673 int status;
3675 if (!tp_features.uwb)
3676 return -ENODEV;
3678 /* WLSW overrides UWB in firmware/hardware, reflect that */
3679 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3680 return RFKILL_STATE_HARD_BLOCKED;
3682 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3683 if (dbg_uwbemul)
3684 return (tpacpi_uwb_emulstate) ?
3685 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3686 #endif
3688 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3689 return -EIO;
3691 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3692 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3695 static void uwb_update_rfk(void)
3697 int status;
3699 if (!tpacpi_uwb_rfkill)
3700 return;
3702 status = uwb_get_radiosw();
3703 if (status < 0)
3704 return;
3705 rfkill_force_state(tpacpi_uwb_rfkill, status);
3707 vdbg_printk(TPACPI_DBG_RFKILL,
3708 "forced rfkill state to %d\n",
3709 status);
3712 static int uwb_set_radiosw(int radio_on, int update_rfk)
3714 int status;
3716 if (!tp_features.uwb)
3717 return -ENODEV;
3719 /* WLSW overrides UWB in firmware/hardware, but there is no
3720 * reason to risk weird behaviour. */
3721 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3722 && radio_on)
3723 return -EPERM;
3725 vdbg_printk(TPACPI_DBG_RFKILL,
3726 "will %s UWB\n", radio_on ? "enable" : "disable");
3728 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3729 if (dbg_uwbemul) {
3730 tpacpi_uwb_emulstate = !!radio_on;
3731 if (update_rfk)
3732 uwb_update_rfk();
3733 return 0;
3735 #endif
3737 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3738 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3739 return -EIO;
3741 if (update_rfk)
3742 uwb_update_rfk();
3744 return 0;
3747 /* --------------------------------------------------------------------- */
3749 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3751 int uwbs = uwb_get_radiosw();
3753 if (uwbs < 0)
3754 return uwbs;
3756 *state = uwbs;
3757 return 0;
3760 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3762 dbg_printk(TPACPI_DBG_RFKILL,
3763 "request to change radio state to %d\n", state);
3764 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3767 static void uwb_exit(void)
3769 if (tpacpi_uwb_rfkill)
3770 rfkill_unregister(tpacpi_uwb_rfkill);
3773 static int __init uwb_init(struct ibm_init_struct *iibm)
3775 int res;
3776 int status = 0;
3778 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3779 "initializing uwb subdriver\n");
3781 TPACPI_ACPIHANDLE_INIT(hkey);
3783 tp_features.uwb = hkey_handle &&
3784 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3786 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3787 "uwb is %s, status 0x%02x\n",
3788 str_supported(tp_features.uwb),
3789 status);
3791 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3792 if (dbg_uwbemul) {
3793 tp_features.uwb = 1;
3794 printk(TPACPI_INFO
3795 "uwb switch emulation enabled\n");
3796 } else
3797 #endif
3798 if (tp_features.uwb &&
3799 !(status & TP_ACPI_UWB_HWPRESENT)) {
3800 /* no uwb hardware present in system */
3801 tp_features.uwb = 0;
3802 dbg_printk(TPACPI_DBG_INIT,
3803 "uwb hardware not installed\n");
3806 if (!tp_features.uwb)
3807 return 1;
3809 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3810 &tpacpi_uwb_rfkill,
3811 RFKILL_TYPE_UWB,
3812 TPACPI_RFK_UWB_SW_NAME,
3813 false,
3814 tpacpi_uwb_rfk_set,
3815 tpacpi_uwb_rfk_get);
3817 return res;
3820 static struct ibm_struct uwb_driver_data = {
3821 .name = "uwb",
3822 .exit = uwb_exit,
3823 .flags.experimental = 1,
3826 /*************************************************************************
3827 * Video subdriver
3830 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3832 enum video_access_mode {
3833 TPACPI_VIDEO_NONE = 0,
3834 TPACPI_VIDEO_570, /* 570 */
3835 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3836 TPACPI_VIDEO_NEW, /* all others */
3839 enum { /* video status flags, based on VIDEO_570 */
3840 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3841 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3842 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3845 enum { /* TPACPI_VIDEO_570 constants */
3846 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3847 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3848 * video_status_flags */
3849 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3850 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3853 static enum video_access_mode video_supported;
3854 static int video_orig_autosw;
3856 static int video_autosw_get(void);
3857 static int video_autosw_set(int enable);
3859 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3861 static int __init video_init(struct ibm_init_struct *iibm)
3863 int ivga;
3865 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3867 TPACPI_ACPIHANDLE_INIT(vid);
3868 TPACPI_ACPIHANDLE_INIT(vid2);
3870 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3871 /* G41, assume IVGA doesn't change */
3872 vid_handle = vid2_handle;
3874 if (!vid_handle)
3875 /* video switching not supported on R30, R31 */
3876 video_supported = TPACPI_VIDEO_NONE;
3877 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3878 /* 570 */
3879 video_supported = TPACPI_VIDEO_570;
3880 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3881 /* 600e/x, 770e, 770x */
3882 video_supported = TPACPI_VIDEO_770;
3883 else
3884 /* all others */
3885 video_supported = TPACPI_VIDEO_NEW;
3887 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3888 str_supported(video_supported != TPACPI_VIDEO_NONE),
3889 video_supported);
3891 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3894 static void video_exit(void)
3896 dbg_printk(TPACPI_DBG_EXIT,
3897 "restoring original video autoswitch mode\n");
3898 if (video_autosw_set(video_orig_autosw))
3899 printk(TPACPI_ERR "error while trying to restore original "
3900 "video autoswitch mode\n");
3903 static int video_outputsw_get(void)
3905 int status = 0;
3906 int i;
3908 switch (video_supported) {
3909 case TPACPI_VIDEO_570:
3910 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3911 TP_ACPI_VIDEO_570_PHSCMD))
3912 return -EIO;
3913 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3914 break;
3915 case TPACPI_VIDEO_770:
3916 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3917 return -EIO;
3918 if (i)
3919 status |= TP_ACPI_VIDEO_S_LCD;
3920 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3921 return -EIO;
3922 if (i)
3923 status |= TP_ACPI_VIDEO_S_CRT;
3924 break;
3925 case TPACPI_VIDEO_NEW:
3926 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3927 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3928 return -EIO;
3929 if (i)
3930 status |= TP_ACPI_VIDEO_S_CRT;
3932 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3933 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3934 return -EIO;
3935 if (i)
3936 status |= TP_ACPI_VIDEO_S_LCD;
3937 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3938 return -EIO;
3939 if (i)
3940 status |= TP_ACPI_VIDEO_S_DVI;
3941 break;
3942 default:
3943 return -ENOSYS;
3946 return status;
3949 static int video_outputsw_set(int status)
3951 int autosw;
3952 int res = 0;
3954 switch (video_supported) {
3955 case TPACPI_VIDEO_570:
3956 res = acpi_evalf(NULL, NULL,
3957 "\\_SB.PHS2", "vdd",
3958 TP_ACPI_VIDEO_570_PHS2CMD,
3959 status | TP_ACPI_VIDEO_570_PHS2SET);
3960 break;
3961 case TPACPI_VIDEO_770:
3962 autosw = video_autosw_get();
3963 if (autosw < 0)
3964 return autosw;
3966 res = video_autosw_set(1);
3967 if (res)
3968 return res;
3969 res = acpi_evalf(vid_handle, NULL,
3970 "ASWT", "vdd", status * 0x100, 0);
3971 if (!autosw && video_autosw_set(autosw)) {
3972 printk(TPACPI_ERR
3973 "video auto-switch left enabled due to error\n");
3974 return -EIO;
3976 break;
3977 case TPACPI_VIDEO_NEW:
3978 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3979 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3980 break;
3981 default:
3982 return -ENOSYS;
3985 return (res)? 0 : -EIO;
3988 static int video_autosw_get(void)
3990 int autosw = 0;
3992 switch (video_supported) {
3993 case TPACPI_VIDEO_570:
3994 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3995 return -EIO;
3996 break;
3997 case TPACPI_VIDEO_770:
3998 case TPACPI_VIDEO_NEW:
3999 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4000 return -EIO;
4001 break;
4002 default:
4003 return -ENOSYS;
4006 return autosw & 1;
4009 static int video_autosw_set(int enable)
4011 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4012 return -EIO;
4013 return 0;
4016 static int video_outputsw_cycle(void)
4018 int autosw = video_autosw_get();
4019 int res;
4021 if (autosw < 0)
4022 return autosw;
4024 switch (video_supported) {
4025 case TPACPI_VIDEO_570:
4026 res = video_autosw_set(1);
4027 if (res)
4028 return res;
4029 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4030 break;
4031 case TPACPI_VIDEO_770:
4032 case TPACPI_VIDEO_NEW:
4033 res = video_autosw_set(1);
4034 if (res)
4035 return res;
4036 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4037 break;
4038 default:
4039 return -ENOSYS;
4041 if (!autosw && video_autosw_set(autosw)) {
4042 printk(TPACPI_ERR
4043 "video auto-switch left enabled due to error\n");
4044 return -EIO;
4047 return (res)? 0 : -EIO;
4050 static int video_expand_toggle(void)
4052 switch (video_supported) {
4053 case TPACPI_VIDEO_570:
4054 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4055 0 : -EIO;
4056 case TPACPI_VIDEO_770:
4057 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4058 0 : -EIO;
4059 case TPACPI_VIDEO_NEW:
4060 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4061 0 : -EIO;
4062 default:
4063 return -ENOSYS;
4065 /* not reached */
4068 static int video_read(char *p)
4070 int status, autosw;
4071 int len = 0;
4073 if (video_supported == TPACPI_VIDEO_NONE) {
4074 len += sprintf(p + len, "status:\t\tnot supported\n");
4075 return len;
4078 status = video_outputsw_get();
4079 if (status < 0)
4080 return status;
4082 autosw = video_autosw_get();
4083 if (autosw < 0)
4084 return autosw;
4086 len += sprintf(p + len, "status:\t\tsupported\n");
4087 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4088 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4089 if (video_supported == TPACPI_VIDEO_NEW)
4090 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4091 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4092 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4093 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4094 if (video_supported == TPACPI_VIDEO_NEW)
4095 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4096 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4097 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4099 return len;
4102 static int video_write(char *buf)
4104 char *cmd;
4105 int enable, disable, status;
4106 int res;
4108 if (video_supported == TPACPI_VIDEO_NONE)
4109 return -ENODEV;
4111 enable = 0;
4112 disable = 0;
4114 while ((cmd = next_cmd(&buf))) {
4115 if (strlencmp(cmd, "lcd_enable") == 0) {
4116 enable |= TP_ACPI_VIDEO_S_LCD;
4117 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4118 disable |= TP_ACPI_VIDEO_S_LCD;
4119 } else if (strlencmp(cmd, "crt_enable") == 0) {
4120 enable |= TP_ACPI_VIDEO_S_CRT;
4121 } else if (strlencmp(cmd, "crt_disable") == 0) {
4122 disable |= TP_ACPI_VIDEO_S_CRT;
4123 } else if (video_supported == TPACPI_VIDEO_NEW &&
4124 strlencmp(cmd, "dvi_enable") == 0) {
4125 enable |= TP_ACPI_VIDEO_S_DVI;
4126 } else if (video_supported == TPACPI_VIDEO_NEW &&
4127 strlencmp(cmd, "dvi_disable") == 0) {
4128 disable |= TP_ACPI_VIDEO_S_DVI;
4129 } else if (strlencmp(cmd, "auto_enable") == 0) {
4130 res = video_autosw_set(1);
4131 if (res)
4132 return res;
4133 } else if (strlencmp(cmd, "auto_disable") == 0) {
4134 res = video_autosw_set(0);
4135 if (res)
4136 return res;
4137 } else if (strlencmp(cmd, "video_switch") == 0) {
4138 res = video_outputsw_cycle();
4139 if (res)
4140 return res;
4141 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4142 res = video_expand_toggle();
4143 if (res)
4144 return res;
4145 } else
4146 return -EINVAL;
4149 if (enable || disable) {
4150 status = video_outputsw_get();
4151 if (status < 0)
4152 return status;
4153 res = video_outputsw_set((status & ~disable) | enable);
4154 if (res)
4155 return res;
4158 return 0;
4161 static struct ibm_struct video_driver_data = {
4162 .name = "video",
4163 .read = video_read,
4164 .write = video_write,
4165 .exit = video_exit,
4168 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4170 /*************************************************************************
4171 * Light (thinklight) subdriver
4174 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4175 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4177 static int light_get_status(void)
4179 int status = 0;
4181 if (tp_features.light_status) {
4182 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4183 return -EIO;
4184 return (!!status);
4187 return -ENXIO;
4190 static int light_set_status(int status)
4192 int rc;
4194 if (tp_features.light) {
4195 if (cmos_handle) {
4196 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4197 (status)?
4198 TP_CMOS_THINKLIGHT_ON :
4199 TP_CMOS_THINKLIGHT_OFF);
4200 } else {
4201 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4202 (status)? 1 : 0);
4204 return (rc)? 0 : -EIO;
4207 return -ENXIO;
4210 static void light_set_status_worker(struct work_struct *work)
4212 struct tpacpi_led_classdev *data =
4213 container_of(work, struct tpacpi_led_classdev, work);
4215 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4216 light_set_status((data->new_state != TPACPI_LED_OFF));
4219 static void light_sysfs_set(struct led_classdev *led_cdev,
4220 enum led_brightness brightness)
4222 struct tpacpi_led_classdev *data =
4223 container_of(led_cdev,
4224 struct tpacpi_led_classdev,
4225 led_classdev);
4226 data->new_state = (brightness != LED_OFF) ?
4227 TPACPI_LED_ON : TPACPI_LED_OFF;
4228 queue_work(tpacpi_wq, &data->work);
4231 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4233 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4236 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4237 .led_classdev = {
4238 .name = "tpacpi::thinklight",
4239 .brightness_set = &light_sysfs_set,
4240 .brightness_get = &light_sysfs_get,
4244 static int __init light_init(struct ibm_init_struct *iibm)
4246 int rc;
4248 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4250 TPACPI_ACPIHANDLE_INIT(ledb);
4251 TPACPI_ACPIHANDLE_INIT(lght);
4252 TPACPI_ACPIHANDLE_INIT(cmos);
4253 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4255 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4256 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4258 if (tp_features.light)
4259 /* light status not supported on
4260 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4261 tp_features.light_status =
4262 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4264 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4265 str_supported(tp_features.light),
4266 str_supported(tp_features.light_status));
4268 if (!tp_features.light)
4269 return 1;
4271 rc = led_classdev_register(&tpacpi_pdev->dev,
4272 &tpacpi_led_thinklight.led_classdev);
4274 if (rc < 0) {
4275 tp_features.light = 0;
4276 tp_features.light_status = 0;
4277 } else {
4278 rc = 0;
4281 return rc;
4284 static void light_exit(void)
4286 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4287 if (work_pending(&tpacpi_led_thinklight.work))
4288 flush_workqueue(tpacpi_wq);
4291 static int light_read(char *p)
4293 int len = 0;
4294 int status;
4296 if (!tp_features.light) {
4297 len += sprintf(p + len, "status:\t\tnot supported\n");
4298 } else if (!tp_features.light_status) {
4299 len += sprintf(p + len, "status:\t\tunknown\n");
4300 len += sprintf(p + len, "commands:\ton, off\n");
4301 } else {
4302 status = light_get_status();
4303 if (status < 0)
4304 return status;
4305 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4306 len += sprintf(p + len, "commands:\ton, off\n");
4309 return len;
4312 static int light_write(char *buf)
4314 char *cmd;
4315 int newstatus = 0;
4317 if (!tp_features.light)
4318 return -ENODEV;
4320 while ((cmd = next_cmd(&buf))) {
4321 if (strlencmp(cmd, "on") == 0) {
4322 newstatus = 1;
4323 } else if (strlencmp(cmd, "off") == 0) {
4324 newstatus = 0;
4325 } else
4326 return -EINVAL;
4329 return light_set_status(newstatus);
4332 static struct ibm_struct light_driver_data = {
4333 .name = "light",
4334 .read = light_read,
4335 .write = light_write,
4336 .exit = light_exit,
4339 /*************************************************************************
4340 * Dock subdriver
4343 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4345 static void dock_notify(struct ibm_struct *ibm, u32 event);
4346 static int dock_read(char *p);
4347 static int dock_write(char *buf);
4349 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4350 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4351 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4352 "\\_SB.PCI.ISA.SLCE", /* 570 */
4353 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4355 /* don't list other alternatives as we install a notify handler on the 570 */
4356 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4358 static const struct acpi_device_id ibm_pci_device_ids[] = {
4359 {PCI_ROOT_HID_STRING, 0},
4360 {"", 0},
4363 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4365 .notify = dock_notify,
4366 .handle = &dock_handle,
4367 .type = ACPI_SYSTEM_NOTIFY,
4370 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4371 * We just use it to get notifications of dock hotplug
4372 * in very old thinkpads */
4373 .hid = ibm_pci_device_ids,
4374 .notify = dock_notify,
4375 .handle = &pci_handle,
4376 .type = ACPI_SYSTEM_NOTIFY,
4380 static struct ibm_struct dock_driver_data[2] = {
4382 .name = "dock",
4383 .read = dock_read,
4384 .write = dock_write,
4385 .acpi = &ibm_dock_acpidriver[0],
4388 .name = "dock",
4389 .acpi = &ibm_dock_acpidriver[1],
4393 #define dock_docked() (_sta(dock_handle) & 1)
4395 static int __init dock_init(struct ibm_init_struct *iibm)
4397 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4399 TPACPI_ACPIHANDLE_INIT(dock);
4401 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4402 str_supported(dock_handle != NULL));
4404 return (dock_handle)? 0 : 1;
4407 static int __init dock_init2(struct ibm_init_struct *iibm)
4409 int dock2_needed;
4411 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4413 if (dock_driver_data[0].flags.acpi_driver_registered &&
4414 dock_driver_data[0].flags.acpi_notify_installed) {
4415 TPACPI_ACPIHANDLE_INIT(pci);
4416 dock2_needed = (pci_handle != NULL);
4417 vdbg_printk(TPACPI_DBG_INIT,
4418 "dock PCI handler for the TP 570 is %s\n",
4419 str_supported(dock2_needed));
4420 } else {
4421 vdbg_printk(TPACPI_DBG_INIT,
4422 "dock subdriver part 2 not required\n");
4423 dock2_needed = 0;
4426 return (dock2_needed)? 0 : 1;
4429 static void dock_notify(struct ibm_struct *ibm, u32 event)
4431 int docked = dock_docked();
4432 int pci = ibm->acpi->hid && ibm->acpi->device &&
4433 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4434 int data;
4436 if (event == 1 && !pci) /* 570 */
4437 data = 1; /* button */
4438 else if (event == 1 && pci) /* 570 */
4439 data = 3; /* dock */
4440 else if (event == 3 && docked)
4441 data = 1; /* button */
4442 else if (event == 3 && !docked)
4443 data = 2; /* undock */
4444 else if (event == 0 && docked)
4445 data = 3; /* dock */
4446 else {
4447 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4448 event, _sta(dock_handle));
4449 data = 0; /* unknown */
4451 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4452 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4453 dev_name(&ibm->acpi->device->dev),
4454 event, data);
4457 static int dock_read(char *p)
4459 int len = 0;
4460 int docked = dock_docked();
4462 if (!dock_handle)
4463 len += sprintf(p + len, "status:\t\tnot supported\n");
4464 else if (!docked)
4465 len += sprintf(p + len, "status:\t\tundocked\n");
4466 else {
4467 len += sprintf(p + len, "status:\t\tdocked\n");
4468 len += sprintf(p + len, "commands:\tdock, undock\n");
4471 return len;
4474 static int dock_write(char *buf)
4476 char *cmd;
4478 if (!dock_docked())
4479 return -ENODEV;
4481 while ((cmd = next_cmd(&buf))) {
4482 if (strlencmp(cmd, "undock") == 0) {
4483 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4484 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4485 return -EIO;
4486 } else if (strlencmp(cmd, "dock") == 0) {
4487 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4488 return -EIO;
4489 } else
4490 return -EINVAL;
4493 return 0;
4496 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4498 /*************************************************************************
4499 * Bay subdriver
4502 #ifdef CONFIG_THINKPAD_ACPI_BAY
4504 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4505 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4506 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4507 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4508 ); /* A21e, R30, R31 */
4509 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4510 "_EJ0", /* all others */
4511 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4512 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4513 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4514 ); /* all others */
4515 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4516 "_EJ0", /* 770x */
4517 ); /* all others */
4519 static int __init bay_init(struct ibm_init_struct *iibm)
4521 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4523 TPACPI_ACPIHANDLE_INIT(bay);
4524 if (bay_handle)
4525 TPACPI_ACPIHANDLE_INIT(bay_ej);
4526 TPACPI_ACPIHANDLE_INIT(bay2);
4527 if (bay2_handle)
4528 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4530 tp_features.bay_status = bay_handle &&
4531 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4532 tp_features.bay_status2 = bay2_handle &&
4533 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4535 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4536 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4537 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4538 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4540 vdbg_printk(TPACPI_DBG_INIT,
4541 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4542 str_supported(tp_features.bay_status),
4543 str_supported(tp_features.bay_eject),
4544 str_supported(tp_features.bay_status2),
4545 str_supported(tp_features.bay_eject2));
4547 return (tp_features.bay_status || tp_features.bay_eject ||
4548 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4551 static void bay_notify(struct ibm_struct *ibm, u32 event)
4553 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4554 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4555 dev_name(&ibm->acpi->device->dev),
4556 event, 0);
4559 #define bay_occupied(b) (_sta(b##_handle) & 1)
4561 static int bay_read(char *p)
4563 int len = 0;
4564 int occupied = bay_occupied(bay);
4565 int occupied2 = bay_occupied(bay2);
4566 int eject, eject2;
4568 len += sprintf(p + len, "status:\t\t%s\n",
4569 tp_features.bay_status ?
4570 (occupied ? "occupied" : "unoccupied") :
4571 "not supported");
4572 if (tp_features.bay_status2)
4573 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4574 "occupied" : "unoccupied");
4576 eject = tp_features.bay_eject && occupied;
4577 eject2 = tp_features.bay_eject2 && occupied2;
4579 if (eject && eject2)
4580 len += sprintf(p + len, "commands:\teject, eject2\n");
4581 else if (eject)
4582 len += sprintf(p + len, "commands:\teject\n");
4583 else if (eject2)
4584 len += sprintf(p + len, "commands:\teject2\n");
4586 return len;
4589 static int bay_write(char *buf)
4591 char *cmd;
4593 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4594 return -ENODEV;
4596 while ((cmd = next_cmd(&buf))) {
4597 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4598 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4599 return -EIO;
4600 } else if (tp_features.bay_eject2 &&
4601 strlencmp(cmd, "eject2") == 0) {
4602 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4603 return -EIO;
4604 } else
4605 return -EINVAL;
4608 return 0;
4611 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4612 .notify = bay_notify,
4613 .handle = &bay_handle,
4614 .type = ACPI_SYSTEM_NOTIFY,
4617 static struct ibm_struct bay_driver_data = {
4618 .name = "bay",
4619 .read = bay_read,
4620 .write = bay_write,
4621 .acpi = &ibm_bay_acpidriver,
4624 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4626 /*************************************************************************
4627 * CMOS subdriver
4630 /* sysfs cmos_command -------------------------------------------------- */
4631 static ssize_t cmos_command_store(struct device *dev,
4632 struct device_attribute *attr,
4633 const char *buf, size_t count)
4635 unsigned long cmos_cmd;
4636 int res;
4638 if (parse_strtoul(buf, 21, &cmos_cmd))
4639 return -EINVAL;
4641 res = issue_thinkpad_cmos_command(cmos_cmd);
4642 return (res)? res : count;
4645 static struct device_attribute dev_attr_cmos_command =
4646 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4648 /* --------------------------------------------------------------------- */
4650 static int __init cmos_init(struct ibm_init_struct *iibm)
4652 int res;
4654 vdbg_printk(TPACPI_DBG_INIT,
4655 "initializing cmos commands subdriver\n");
4657 TPACPI_ACPIHANDLE_INIT(cmos);
4659 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4660 str_supported(cmos_handle != NULL));
4662 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4663 if (res)
4664 return res;
4666 return (cmos_handle)? 0 : 1;
4669 static void cmos_exit(void)
4671 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4674 static int cmos_read(char *p)
4676 int len = 0;
4678 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4679 R30, R31, T20-22, X20-21 */
4680 if (!cmos_handle)
4681 len += sprintf(p + len, "status:\t\tnot supported\n");
4682 else {
4683 len += sprintf(p + len, "status:\t\tsupported\n");
4684 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4687 return len;
4690 static int cmos_write(char *buf)
4692 char *cmd;
4693 int cmos_cmd, res;
4695 while ((cmd = next_cmd(&buf))) {
4696 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4697 cmos_cmd >= 0 && cmos_cmd <= 21) {
4698 /* cmos_cmd set */
4699 } else
4700 return -EINVAL;
4702 res = issue_thinkpad_cmos_command(cmos_cmd);
4703 if (res)
4704 return res;
4707 return 0;
4710 static struct ibm_struct cmos_driver_data = {
4711 .name = "cmos",
4712 .read = cmos_read,
4713 .write = cmos_write,
4714 .exit = cmos_exit,
4717 /*************************************************************************
4718 * LED subdriver
4721 enum led_access_mode {
4722 TPACPI_LED_NONE = 0,
4723 TPACPI_LED_570, /* 570 */
4724 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4725 TPACPI_LED_NEW, /* all others */
4728 enum { /* For TPACPI_LED_OLD */
4729 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4730 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4731 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4734 static enum led_access_mode led_supported;
4736 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4737 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4738 /* T20-22, X20-21 */
4739 "LED", /* all others */
4740 ); /* R30, R31 */
4742 #define TPACPI_LED_NUMLEDS 8
4743 static struct tpacpi_led_classdev *tpacpi_leds;
4744 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4745 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4746 /* there's a limit of 19 chars + NULL before 2.6.26 */
4747 "tpacpi::power",
4748 "tpacpi:orange:batt",
4749 "tpacpi:green:batt",
4750 "tpacpi::dock_active",
4751 "tpacpi::bay_active",
4752 "tpacpi::dock_batt",
4753 "tpacpi::unknown_led",
4754 "tpacpi::standby",
4756 #define TPACPI_SAFE_LEDS 0x0081U
4758 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4760 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4761 return false;
4762 #else
4763 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4764 #endif
4767 static int led_get_status(const unsigned int led)
4769 int status;
4770 enum led_status_t led_s;
4772 switch (led_supported) {
4773 case TPACPI_LED_570:
4774 if (!acpi_evalf(ec_handle,
4775 &status, "GLED", "dd", 1 << led))
4776 return -EIO;
4777 led_s = (status == 0)?
4778 TPACPI_LED_OFF :
4779 ((status == 1)?
4780 TPACPI_LED_ON :
4781 TPACPI_LED_BLINK);
4782 tpacpi_led_state_cache[led] = led_s;
4783 return led_s;
4784 default:
4785 return -ENXIO;
4788 /* not reached */
4791 static int led_set_status(const unsigned int led,
4792 const enum led_status_t ledstatus)
4794 /* off, on, blink. Index is led_status_t */
4795 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4796 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4798 int rc = 0;
4800 switch (led_supported) {
4801 case TPACPI_LED_570:
4802 /* 570 */
4803 if (unlikely(led > 7))
4804 return -EINVAL;
4805 if (unlikely(tpacpi_is_led_restricted(led)))
4806 return -EPERM;
4807 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4808 (1 << led), led_sled_arg1[ledstatus]))
4809 rc = -EIO;
4810 break;
4811 case TPACPI_LED_OLD:
4812 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4813 if (unlikely(led > 7))
4814 return -EINVAL;
4815 if (unlikely(tpacpi_is_led_restricted(led)))
4816 return -EPERM;
4817 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4818 if (rc >= 0)
4819 rc = ec_write(TPACPI_LED_EC_HLBL,
4820 (ledstatus == TPACPI_LED_BLINK) << led);
4821 if (rc >= 0)
4822 rc = ec_write(TPACPI_LED_EC_HLCL,
4823 (ledstatus != TPACPI_LED_OFF) << led);
4824 break;
4825 case TPACPI_LED_NEW:
4826 /* all others */
4827 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4828 return -EINVAL;
4829 if (unlikely(tpacpi_is_led_restricted(led)))
4830 return -EPERM;
4831 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4832 led, led_led_arg1[ledstatus]))
4833 rc = -EIO;
4834 break;
4835 default:
4836 rc = -ENXIO;
4839 if (!rc)
4840 tpacpi_led_state_cache[led] = ledstatus;
4842 return rc;
4845 static void led_set_status_worker(struct work_struct *work)
4847 struct tpacpi_led_classdev *data =
4848 container_of(work, struct tpacpi_led_classdev, work);
4850 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4851 led_set_status(data->led, data->new_state);
4854 static void led_sysfs_set(struct led_classdev *led_cdev,
4855 enum led_brightness brightness)
4857 struct tpacpi_led_classdev *data = container_of(led_cdev,
4858 struct tpacpi_led_classdev, led_classdev);
4860 if (brightness == LED_OFF)
4861 data->new_state = TPACPI_LED_OFF;
4862 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4863 data->new_state = TPACPI_LED_ON;
4864 else
4865 data->new_state = TPACPI_LED_BLINK;
4867 queue_work(tpacpi_wq, &data->work);
4870 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4871 unsigned long *delay_on, unsigned long *delay_off)
4873 struct tpacpi_led_classdev *data = container_of(led_cdev,
4874 struct tpacpi_led_classdev, led_classdev);
4876 /* Can we choose the flash rate? */
4877 if (*delay_on == 0 && *delay_off == 0) {
4878 /* yes. set them to the hardware blink rate (1 Hz) */
4879 *delay_on = 500; /* ms */
4880 *delay_off = 500; /* ms */
4881 } else if ((*delay_on != 500) || (*delay_off != 500))
4882 return -EINVAL;
4884 data->new_state = TPACPI_LED_BLINK;
4885 queue_work(tpacpi_wq, &data->work);
4887 return 0;
4890 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4892 int rc;
4894 struct tpacpi_led_classdev *data = container_of(led_cdev,
4895 struct tpacpi_led_classdev, led_classdev);
4897 rc = led_get_status(data->led);
4899 if (rc == TPACPI_LED_OFF || rc < 0)
4900 rc = LED_OFF; /* no error handling in led class :( */
4901 else
4902 rc = LED_FULL;
4904 return rc;
4907 static void led_exit(void)
4909 unsigned int i;
4911 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4912 if (tpacpi_leds[i].led_classdev.name)
4913 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4916 kfree(tpacpi_leds);
4919 static int __init tpacpi_init_led(unsigned int led)
4921 int rc;
4923 tpacpi_leds[led].led = led;
4925 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4926 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4927 if (led_supported == TPACPI_LED_570)
4928 tpacpi_leds[led].led_classdev.brightness_get =
4929 &led_sysfs_get;
4931 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4933 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4935 rc = led_classdev_register(&tpacpi_pdev->dev,
4936 &tpacpi_leds[led].led_classdev);
4937 if (rc < 0)
4938 tpacpi_leds[led].led_classdev.name = NULL;
4940 return rc;
4943 static int __init led_init(struct ibm_init_struct *iibm)
4945 unsigned int i;
4946 int rc;
4948 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4950 TPACPI_ACPIHANDLE_INIT(led);
4952 if (!led_handle)
4953 /* led not supported on R30, R31 */
4954 led_supported = TPACPI_LED_NONE;
4955 else if (strlencmp(led_path, "SLED") == 0)
4956 /* 570 */
4957 led_supported = TPACPI_LED_570;
4958 else if (strlencmp(led_path, "SYSL") == 0)
4959 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4960 led_supported = TPACPI_LED_OLD;
4961 else
4962 /* all others */
4963 led_supported = TPACPI_LED_NEW;
4965 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4966 str_supported(led_supported), led_supported);
4968 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4969 GFP_KERNEL);
4970 if (!tpacpi_leds) {
4971 printk(TPACPI_ERR "Out of memory for LED data\n");
4972 return -ENOMEM;
4975 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4976 if (!tpacpi_is_led_restricted(i)) {
4977 rc = tpacpi_init_led(i);
4978 if (rc < 0) {
4979 led_exit();
4980 return rc;
4985 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4986 if (led_supported != TPACPI_LED_NONE)
4987 printk(TPACPI_NOTICE
4988 "warning: userspace override of important "
4989 "firmware LEDs is enabled\n");
4990 #endif
4991 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4994 #define str_led_status(s) \
4995 ((s) == TPACPI_LED_OFF ? "off" : \
4996 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4998 static int led_read(char *p)
5000 int len = 0;
5002 if (!led_supported) {
5003 len += sprintf(p + len, "status:\t\tnot supported\n");
5004 return len;
5006 len += sprintf(p + len, "status:\t\tsupported\n");
5008 if (led_supported == TPACPI_LED_570) {
5009 /* 570 */
5010 int i, status;
5011 for (i = 0; i < 8; i++) {
5012 status = led_get_status(i);
5013 if (status < 0)
5014 return -EIO;
5015 len += sprintf(p + len, "%d:\t\t%s\n",
5016 i, str_led_status(status));
5020 len += sprintf(p + len, "commands:\t"
5021 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5023 return len;
5026 static int led_write(char *buf)
5028 char *cmd;
5029 int led, rc;
5030 enum led_status_t s;
5032 if (!led_supported)
5033 return -ENODEV;
5035 while ((cmd = next_cmd(&buf))) {
5036 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5037 return -EINVAL;
5039 if (strstr(cmd, "off")) {
5040 s = TPACPI_LED_OFF;
5041 } else if (strstr(cmd, "on")) {
5042 s = TPACPI_LED_ON;
5043 } else if (strstr(cmd, "blink")) {
5044 s = TPACPI_LED_BLINK;
5045 } else {
5046 return -EINVAL;
5049 rc = led_set_status(led, s);
5050 if (rc < 0)
5051 return rc;
5054 return 0;
5057 static struct ibm_struct led_driver_data = {
5058 .name = "led",
5059 .read = led_read,
5060 .write = led_write,
5061 .exit = led_exit,
5064 /*************************************************************************
5065 * Beep subdriver
5068 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5070 static int __init beep_init(struct ibm_init_struct *iibm)
5072 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5074 TPACPI_ACPIHANDLE_INIT(beep);
5076 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5077 str_supported(beep_handle != NULL));
5079 return (beep_handle)? 0 : 1;
5082 static int beep_read(char *p)
5084 int len = 0;
5086 if (!beep_handle)
5087 len += sprintf(p + len, "status:\t\tnot supported\n");
5088 else {
5089 len += sprintf(p + len, "status:\t\tsupported\n");
5090 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5093 return len;
5096 static int beep_write(char *buf)
5098 char *cmd;
5099 int beep_cmd;
5101 if (!beep_handle)
5102 return -ENODEV;
5104 while ((cmd = next_cmd(&buf))) {
5105 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5106 beep_cmd >= 0 && beep_cmd <= 17) {
5107 /* beep_cmd set */
5108 } else
5109 return -EINVAL;
5110 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5111 return -EIO;
5114 return 0;
5117 static struct ibm_struct beep_driver_data = {
5118 .name = "beep",
5119 .read = beep_read,
5120 .write = beep_write,
5123 /*************************************************************************
5124 * Thermal subdriver
5127 enum thermal_access_mode {
5128 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5129 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5130 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5131 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5132 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5135 enum { /* TPACPI_THERMAL_TPEC_* */
5136 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5137 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5138 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5141 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5142 struct ibm_thermal_sensors_struct {
5143 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5146 static enum thermal_access_mode thermal_read_mode;
5148 /* idx is zero-based */
5149 static int thermal_get_sensor(int idx, s32 *value)
5151 int t;
5152 s8 tmp;
5153 char tmpi[5];
5155 t = TP_EC_THERMAL_TMP0;
5157 switch (thermal_read_mode) {
5158 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5159 case TPACPI_THERMAL_TPEC_16:
5160 if (idx >= 8 && idx <= 15) {
5161 t = TP_EC_THERMAL_TMP8;
5162 idx -= 8;
5164 /* fallthrough */
5165 #endif
5166 case TPACPI_THERMAL_TPEC_8:
5167 if (idx <= 7) {
5168 if (!acpi_ec_read(t + idx, &tmp))
5169 return -EIO;
5170 *value = tmp * 1000;
5171 return 0;
5173 break;
5175 case TPACPI_THERMAL_ACPI_UPDT:
5176 if (idx <= 7) {
5177 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5178 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5179 return -EIO;
5180 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5181 return -EIO;
5182 *value = (t - 2732) * 100;
5183 return 0;
5185 break;
5187 case TPACPI_THERMAL_ACPI_TMP07:
5188 if (idx <= 7) {
5189 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5190 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5191 return -EIO;
5192 if (t > 127 || t < -127)
5193 t = TP_EC_THERMAL_TMP_NA;
5194 *value = t * 1000;
5195 return 0;
5197 break;
5199 case TPACPI_THERMAL_NONE:
5200 default:
5201 return -ENOSYS;
5204 return -EINVAL;
5207 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5209 int res, i;
5210 int n;
5212 n = 8;
5213 i = 0;
5215 if (!s)
5216 return -EINVAL;
5218 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5219 n = 16;
5221 for (i = 0 ; i < n; i++) {
5222 res = thermal_get_sensor(i, &s->temp[i]);
5223 if (res)
5224 return res;
5227 return n;
5230 /* sysfs temp##_input -------------------------------------------------- */
5232 static ssize_t thermal_temp_input_show(struct device *dev,
5233 struct device_attribute *attr,
5234 char *buf)
5236 struct sensor_device_attribute *sensor_attr =
5237 to_sensor_dev_attr(attr);
5238 int idx = sensor_attr->index;
5239 s32 value;
5240 int res;
5242 res = thermal_get_sensor(idx, &value);
5243 if (res)
5244 return res;
5245 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5246 return -ENXIO;
5248 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5251 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5252 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5253 thermal_temp_input_show, NULL, _idxB)
5255 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5256 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5257 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5258 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5259 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5260 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5261 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5262 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5263 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5264 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5265 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5266 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5267 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5268 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5269 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5270 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5271 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5274 #define THERMAL_ATTRS(X) \
5275 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5277 static struct attribute *thermal_temp_input_attr[] = {
5278 THERMAL_ATTRS(8),
5279 THERMAL_ATTRS(9),
5280 THERMAL_ATTRS(10),
5281 THERMAL_ATTRS(11),
5282 THERMAL_ATTRS(12),
5283 THERMAL_ATTRS(13),
5284 THERMAL_ATTRS(14),
5285 THERMAL_ATTRS(15),
5286 THERMAL_ATTRS(0),
5287 THERMAL_ATTRS(1),
5288 THERMAL_ATTRS(2),
5289 THERMAL_ATTRS(3),
5290 THERMAL_ATTRS(4),
5291 THERMAL_ATTRS(5),
5292 THERMAL_ATTRS(6),
5293 THERMAL_ATTRS(7),
5294 NULL
5297 static const struct attribute_group thermal_temp_input16_group = {
5298 .attrs = thermal_temp_input_attr
5301 static const struct attribute_group thermal_temp_input8_group = {
5302 .attrs = &thermal_temp_input_attr[8]
5305 #undef THERMAL_SENSOR_ATTR_TEMP
5306 #undef THERMAL_ATTRS
5308 /* --------------------------------------------------------------------- */
5310 static int __init thermal_init(struct ibm_init_struct *iibm)
5312 u8 t, ta1, ta2;
5313 int i;
5314 int acpi_tmp7;
5315 int res;
5317 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5319 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5321 if (thinkpad_id.ec_model) {
5323 * Direct EC access mode: sensors at registers
5324 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5325 * non-implemented, thermal sensors return 0x80 when
5326 * not available
5329 ta1 = ta2 = 0;
5330 for (i = 0; i < 8; i++) {
5331 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5332 ta1 |= t;
5333 } else {
5334 ta1 = 0;
5335 break;
5337 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5338 ta2 |= t;
5339 } else {
5340 ta1 = 0;
5341 break;
5344 if (ta1 == 0) {
5345 /* This is sheer paranoia, but we handle it anyway */
5346 if (acpi_tmp7) {
5347 printk(TPACPI_ERR
5348 "ThinkPad ACPI EC access misbehaving, "
5349 "falling back to ACPI TMPx access "
5350 "mode\n");
5351 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5352 } else {
5353 printk(TPACPI_ERR
5354 "ThinkPad ACPI EC access misbehaving, "
5355 "disabling thermal sensors access\n");
5356 thermal_read_mode = TPACPI_THERMAL_NONE;
5358 } else {
5359 thermal_read_mode =
5360 (ta2 != 0) ?
5361 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5363 } else if (acpi_tmp7) {
5364 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5365 /* 600e/x, 770e, 770x */
5366 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5367 } else {
5368 /* Standard ACPI TMPx access, max 8 sensors */
5369 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5371 } else {
5372 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5373 thermal_read_mode = TPACPI_THERMAL_NONE;
5376 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5377 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5378 thermal_read_mode);
5380 switch (thermal_read_mode) {
5381 case TPACPI_THERMAL_TPEC_16:
5382 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5383 &thermal_temp_input16_group);
5384 if (res)
5385 return res;
5386 break;
5387 case TPACPI_THERMAL_TPEC_8:
5388 case TPACPI_THERMAL_ACPI_TMP07:
5389 case TPACPI_THERMAL_ACPI_UPDT:
5390 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5391 &thermal_temp_input8_group);
5392 if (res)
5393 return res;
5394 break;
5395 case TPACPI_THERMAL_NONE:
5396 default:
5397 return 1;
5400 return 0;
5403 static void thermal_exit(void)
5405 switch (thermal_read_mode) {
5406 case TPACPI_THERMAL_TPEC_16:
5407 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5408 &thermal_temp_input16_group);
5409 break;
5410 case TPACPI_THERMAL_TPEC_8:
5411 case TPACPI_THERMAL_ACPI_TMP07:
5412 case TPACPI_THERMAL_ACPI_UPDT:
5413 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5414 &thermal_temp_input16_group);
5415 break;
5416 case TPACPI_THERMAL_NONE:
5417 default:
5418 break;
5422 static int thermal_read(char *p)
5424 int len = 0;
5425 int n, i;
5426 struct ibm_thermal_sensors_struct t;
5428 n = thermal_get_sensors(&t);
5429 if (unlikely(n < 0))
5430 return n;
5432 len += sprintf(p + len, "temperatures:\t");
5434 if (n > 0) {
5435 for (i = 0; i < (n - 1); i++)
5436 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5437 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5438 } else
5439 len += sprintf(p + len, "not supported\n");
5441 return len;
5444 static struct ibm_struct thermal_driver_data = {
5445 .name = "thermal",
5446 .read = thermal_read,
5447 .exit = thermal_exit,
5450 /*************************************************************************
5451 * EC Dump subdriver
5454 static u8 ecdump_regs[256];
5456 static int ecdump_read(char *p)
5458 int len = 0;
5459 int i, j;
5460 u8 v;
5462 len += sprintf(p + len, "EC "
5463 " +00 +01 +02 +03 +04 +05 +06 +07"
5464 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5465 for (i = 0; i < 256; i += 16) {
5466 len += sprintf(p + len, "EC 0x%02x:", i);
5467 for (j = 0; j < 16; j++) {
5468 if (!acpi_ec_read(i + j, &v))
5469 break;
5470 if (v != ecdump_regs[i + j])
5471 len += sprintf(p + len, " *%02x", v);
5472 else
5473 len += sprintf(p + len, " %02x", v);
5474 ecdump_regs[i + j] = v;
5476 len += sprintf(p + len, "\n");
5477 if (j != 16)
5478 break;
5481 /* These are way too dangerous to advertise openly... */
5482 #if 0
5483 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5484 " (<offset> is 00-ff, <value> is 00-ff)\n");
5485 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5486 " (<offset> is 00-ff, <value> is 0-255)\n");
5487 #endif
5488 return len;
5491 static int ecdump_write(char *buf)
5493 char *cmd;
5494 int i, v;
5496 while ((cmd = next_cmd(&buf))) {
5497 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5498 /* i and v set */
5499 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5500 /* i and v set */
5501 } else
5502 return -EINVAL;
5503 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5504 if (!acpi_ec_write(i, v))
5505 return -EIO;
5506 } else
5507 return -EINVAL;
5510 return 0;
5513 static struct ibm_struct ecdump_driver_data = {
5514 .name = "ecdump",
5515 .read = ecdump_read,
5516 .write = ecdump_write,
5517 .flags.experimental = 1,
5520 /*************************************************************************
5521 * Backlight/brightness subdriver
5524 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5526 enum {
5527 TP_EC_BACKLIGHT = 0x31,
5529 /* TP_EC_BACKLIGHT bitmasks */
5530 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5531 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5532 TP_EC_BACKLIGHT_MAPSW = 0x20,
5535 static struct backlight_device *ibm_backlight_device;
5536 static int brightness_mode;
5537 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5539 static struct mutex brightness_mutex;
5542 * ThinkPads can read brightness from two places: EC 0x31, or
5543 * CMOS NVRAM byte 0x5E, bits 0-3.
5545 * EC 0x31 has the following layout
5546 * Bit 7: unknown function
5547 * Bit 6: unknown function
5548 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5549 * Bit 4: must be set to zero to avoid problems
5550 * Bit 3-0: backlight brightness level
5552 * brightness_get_raw returns status data in the EC 0x31 layout
5554 static int brightness_get_raw(int *status)
5556 u8 lec = 0, lcmos = 0, level = 0;
5558 if (brightness_mode & 1) {
5559 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5560 return -EIO;
5561 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5563 if (brightness_mode & 2) {
5564 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5565 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5566 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5567 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5568 level = lcmos;
5571 if (brightness_mode == 3) {
5572 *status = lec; /* Prefer EC, CMOS is just a backing store */
5573 lec &= TP_EC_BACKLIGHT_LVLMSK;
5574 if (lec == lcmos)
5575 tp_warned.bright_cmos_ec_unsync = 0;
5576 else {
5577 if (!tp_warned.bright_cmos_ec_unsync) {
5578 printk(TPACPI_ERR
5579 "CMOS NVRAM (%u) and EC (%u) do not "
5580 "agree on display brightness level\n",
5581 (unsigned int) lcmos,
5582 (unsigned int) lec);
5583 tp_warned.bright_cmos_ec_unsync = 1;
5585 return -EIO;
5587 } else {
5588 *status = level;
5591 return 0;
5594 /* May return EINTR which can always be mapped to ERESTARTSYS */
5595 static int brightness_set(int value)
5597 int cmos_cmd, inc, i, res;
5598 int current_value;
5599 int command_bits;
5601 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5602 value < 0)
5603 return -EINVAL;
5605 res = mutex_lock_killable(&brightness_mutex);
5606 if (res < 0)
5607 return res;
5609 res = brightness_get_raw(&current_value);
5610 if (res < 0)
5611 goto errout;
5613 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5614 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5616 cmos_cmd = value > current_value ?
5617 TP_CMOS_BRIGHTNESS_UP :
5618 TP_CMOS_BRIGHTNESS_DOWN;
5619 inc = (value > current_value)? 1 : -1;
5621 res = 0;
5622 for (i = current_value; i != value; i += inc) {
5623 if ((brightness_mode & 2) &&
5624 issue_thinkpad_cmos_command(cmos_cmd)) {
5625 res = -EIO;
5626 goto errout;
5628 if ((brightness_mode & 1) &&
5629 !acpi_ec_write(TP_EC_BACKLIGHT,
5630 (i + inc) | command_bits)) {
5631 res = -EIO;
5632 goto errout;;
5636 errout:
5637 mutex_unlock(&brightness_mutex);
5638 return res;
5641 /* sysfs backlight class ----------------------------------------------- */
5643 static int brightness_update_status(struct backlight_device *bd)
5645 /* it is the backlight class's job (caller) to handle
5646 * EINTR and other errors properly */
5647 return brightness_set(
5648 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5649 bd->props.power == FB_BLANK_UNBLANK) ?
5650 bd->props.brightness : 0);
5653 static int brightness_get(struct backlight_device *bd)
5655 int status, res;
5657 res = brightness_get_raw(&status);
5658 if (res < 0)
5659 return 0; /* FIXME: teach backlight about error handling */
5661 return status & TP_EC_BACKLIGHT_LVLMSK;
5664 static struct backlight_ops ibm_backlight_data = {
5665 .get_brightness = brightness_get,
5666 .update_status = brightness_update_status,
5669 /* --------------------------------------------------------------------- */
5671 static int __init brightness_init(struct ibm_init_struct *iibm)
5673 int b;
5675 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5677 mutex_init(&brightness_mutex);
5680 * We always attempt to detect acpi support, so as to switch
5681 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5682 * going to publish a backlight interface
5684 b = tpacpi_check_std_acpi_brightness_support();
5685 if (b > 0) {
5687 if (acpi_video_backlight_support()) {
5688 if (brightness_enable > 1) {
5689 printk(TPACPI_NOTICE
5690 "Standard ACPI backlight interface "
5691 "available, not loading native one.\n");
5692 return 1;
5693 } else if (brightness_enable == 1) {
5694 printk(TPACPI_NOTICE
5695 "Backlight control force enabled, even if standard "
5696 "ACPI backlight interface is available\n");
5698 } else {
5699 if (brightness_enable > 1) {
5700 printk(TPACPI_NOTICE
5701 "Standard ACPI backlight interface not "
5702 "available, thinkpad_acpi native "
5703 "brightness control enabled\n");
5708 if (!brightness_enable) {
5709 dbg_printk(TPACPI_DBG_INIT,
5710 "brightness support disabled by "
5711 "module parameter\n");
5712 return 1;
5715 if (b > 16) {
5716 printk(TPACPI_ERR
5717 "Unsupported brightness interface, "
5718 "please contact %s\n", TPACPI_MAIL);
5719 return 1;
5721 if (b == 16)
5722 tp_features.bright_16levels = 1;
5724 if (!brightness_mode) {
5725 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5726 brightness_mode = 2;
5727 else
5728 brightness_mode = 3;
5730 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5731 brightness_mode);
5734 if (brightness_mode > 3)
5735 return -EINVAL;
5737 if (brightness_get_raw(&b) < 0)
5738 return 1;
5740 if (tp_features.bright_16levels)
5741 printk(TPACPI_INFO
5742 "detected a 16-level brightness capable ThinkPad\n");
5744 ibm_backlight_device = backlight_device_register(
5745 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5746 &ibm_backlight_data);
5747 if (IS_ERR(ibm_backlight_device)) {
5748 printk(TPACPI_ERR "Could not register backlight device\n");
5749 return PTR_ERR(ibm_backlight_device);
5751 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5753 ibm_backlight_device->props.max_brightness =
5754 (tp_features.bright_16levels)? 15 : 7;
5755 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5756 backlight_update_status(ibm_backlight_device);
5758 return 0;
5761 static void brightness_exit(void)
5763 if (ibm_backlight_device) {
5764 vdbg_printk(TPACPI_DBG_EXIT,
5765 "calling backlight_device_unregister()\n");
5766 backlight_device_unregister(ibm_backlight_device);
5770 static int brightness_read(char *p)
5772 int len = 0;
5773 int level;
5775 level = brightness_get(NULL);
5776 if (level < 0) {
5777 len += sprintf(p + len, "level:\t\tunreadable\n");
5778 } else {
5779 len += sprintf(p + len, "level:\t\t%d\n", level);
5780 len += sprintf(p + len, "commands:\tup, down\n");
5781 len += sprintf(p + len, "commands:\tlevel <level>"
5782 " (<level> is 0-%d)\n",
5783 (tp_features.bright_16levels) ? 15 : 7);
5786 return len;
5789 static int brightness_write(char *buf)
5791 int level;
5792 int rc;
5793 char *cmd;
5794 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5796 level = brightness_get(NULL);
5797 if (level < 0)
5798 return level;
5800 while ((cmd = next_cmd(&buf))) {
5801 if (strlencmp(cmd, "up") == 0) {
5802 if (level < max_level)
5803 level++;
5804 } else if (strlencmp(cmd, "down") == 0) {
5805 if (level > 0)
5806 level--;
5807 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5808 level >= 0 && level <= max_level) {
5809 /* new level set */
5810 } else
5811 return -EINVAL;
5815 * Now we know what the final level should be, so we try to set it.
5816 * Doing it this way makes the syscall restartable in case of EINTR
5818 rc = brightness_set(level);
5819 return (rc == -EINTR)? ERESTARTSYS : rc;
5822 static struct ibm_struct brightness_driver_data = {
5823 .name = "brightness",
5824 .read = brightness_read,
5825 .write = brightness_write,
5826 .exit = brightness_exit,
5829 /*************************************************************************
5830 * Volume subdriver
5833 static int volume_offset = 0x30;
5835 static int volume_read(char *p)
5837 int len = 0;
5838 u8 level;
5840 if (!acpi_ec_read(volume_offset, &level)) {
5841 len += sprintf(p + len, "level:\t\tunreadable\n");
5842 } else {
5843 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5844 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5845 len += sprintf(p + len, "commands:\tup, down, mute\n");
5846 len += sprintf(p + len, "commands:\tlevel <level>"
5847 " (<level> is 0-15)\n");
5850 return len;
5853 static int volume_write(char *buf)
5855 int cmos_cmd, inc, i;
5856 u8 level, mute;
5857 int new_level, new_mute;
5858 char *cmd;
5860 while ((cmd = next_cmd(&buf))) {
5861 if (!acpi_ec_read(volume_offset, &level))
5862 return -EIO;
5863 new_mute = mute = level & 0x40;
5864 new_level = level = level & 0xf;
5866 if (strlencmp(cmd, "up") == 0) {
5867 if (mute)
5868 new_mute = 0;
5869 else
5870 new_level = level == 15 ? 15 : level + 1;
5871 } else if (strlencmp(cmd, "down") == 0) {
5872 if (mute)
5873 new_mute = 0;
5874 else
5875 new_level = level == 0 ? 0 : level - 1;
5876 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5877 new_level >= 0 && new_level <= 15) {
5878 /* new_level set */
5879 } else if (strlencmp(cmd, "mute") == 0) {
5880 new_mute = 0x40;
5881 } else
5882 return -EINVAL;
5884 if (new_level != level) {
5885 /* mute doesn't change */
5887 cmos_cmd = (new_level > level) ?
5888 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5889 inc = new_level > level ? 1 : -1;
5891 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5892 !acpi_ec_write(volume_offset, level)))
5893 return -EIO;
5895 for (i = level; i != new_level; i += inc)
5896 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5897 !acpi_ec_write(volume_offset, i + inc))
5898 return -EIO;
5900 if (mute &&
5901 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5902 !acpi_ec_write(volume_offset, new_level + mute))) {
5903 return -EIO;
5907 if (new_mute != mute) {
5908 /* level doesn't change */
5910 cmos_cmd = (new_mute) ?
5911 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5913 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5914 !acpi_ec_write(volume_offset, level + new_mute))
5915 return -EIO;
5919 return 0;
5922 static struct ibm_struct volume_driver_data = {
5923 .name = "volume",
5924 .read = volume_read,
5925 .write = volume_write,
5928 /*************************************************************************
5929 * Fan subdriver
5933 * FAN ACCESS MODES
5935 * TPACPI_FAN_RD_ACPI_GFAN:
5936 * ACPI GFAN method: returns fan level
5938 * see TPACPI_FAN_WR_ACPI_SFAN
5939 * EC 0x2f (HFSP) not available if GFAN exists
5941 * TPACPI_FAN_WR_ACPI_SFAN:
5942 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5944 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5945 * it for writing.
5947 * TPACPI_FAN_WR_TPEC:
5948 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5949 * Supported on almost all ThinkPads
5951 * Fan speed changes of any sort (including those caused by the
5952 * disengaged mode) are usually done slowly by the firmware as the
5953 * maximum ammount of fan duty cycle change per second seems to be
5954 * limited.
5956 * Reading is not available if GFAN exists.
5957 * Writing is not available if SFAN exists.
5959 * Bits
5960 * 7 automatic mode engaged;
5961 * (default operation mode of the ThinkPad)
5962 * fan level is ignored in this mode.
5963 * 6 full speed mode (takes precedence over bit 7);
5964 * not available on all thinkpads. May disable
5965 * the tachometer while the fan controller ramps up
5966 * the speed (which can take up to a few *minutes*).
5967 * Speeds up fan to 100% duty-cycle, which is far above
5968 * the standard RPM levels. It is not impossible that
5969 * it could cause hardware damage.
5970 * 5-3 unused in some models. Extra bits for fan level
5971 * in others, but still useless as all values above
5972 * 7 map to the same speed as level 7 in these models.
5973 * 2-0 fan level (0..7 usually)
5974 * 0x00 = stop
5975 * 0x07 = max (set when temperatures critical)
5976 * Some ThinkPads may have other levels, see
5977 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5979 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5980 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5981 * does so, its initial value is meaningless (0x07).
5983 * For firmware bugs, refer to:
5984 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5986 * ----
5988 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5989 * Main fan tachometer reading (in RPM)
5991 * This register is present on all ThinkPads with a new-style EC, and
5992 * it is known not to be present on the A21m/e, and T22, as there is
5993 * something else in offset 0x84 according to the ACPI DSDT. Other
5994 * ThinkPads from this same time period (and earlier) probably lack the
5995 * tachometer as well.
5997 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5998 * was never fixed by IBM to report the EC firmware version string
5999 * probably support the tachometer (like the early X models), so
6000 * detecting it is quite hard. We need more data to know for sure.
6002 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6003 * might result.
6005 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6006 * mode.
6008 * For firmware bugs, refer to:
6009 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6011 * TPACPI_FAN_WR_ACPI_FANS:
6012 * ThinkPad X31, X40, X41. Not available in the X60.
6014 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6015 * high speed. ACPI DSDT seems to map these three speeds to levels
6016 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6017 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6019 * The speeds are stored on handles
6020 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6022 * There are three default speed sets, acessible as handles:
6023 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6025 * ACPI DSDT switches which set is in use depending on various
6026 * factors.
6028 * TPACPI_FAN_WR_TPEC is also available and should be used to
6029 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6030 * but the ACPI tables just mention level 7.
6033 enum { /* Fan control constants */
6034 fan_status_offset = 0x2f, /* EC register 0x2f */
6035 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6036 * 0x84 must be read before 0x85 */
6038 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6039 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6041 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6044 enum fan_status_access_mode {
6045 TPACPI_FAN_NONE = 0, /* No fan status or control */
6046 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6047 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6050 enum fan_control_access_mode {
6051 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6052 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6053 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6054 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6057 enum fan_control_commands {
6058 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6059 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6060 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6061 * and also watchdog cmd */
6064 static int fan_control_allowed;
6066 static enum fan_status_access_mode fan_status_access_mode;
6067 static enum fan_control_access_mode fan_control_access_mode;
6068 static enum fan_control_commands fan_control_commands;
6070 static u8 fan_control_initial_status;
6071 static u8 fan_control_desired_level;
6072 static u8 fan_control_resume_level;
6073 static int fan_watchdog_maxinterval;
6075 static struct mutex fan_mutex;
6077 static void fan_watchdog_fire(struct work_struct *ignored);
6078 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6080 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6081 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6082 "\\FSPD", /* 600e/x, 770e, 770x */
6083 ); /* all others */
6084 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6085 "JFNS", /* 770x-JL */
6086 ); /* all others */
6089 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6090 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6091 * be in auto mode (0x80).
6093 * This is corrected by any write to HFSP either by the driver, or
6094 * by the firmware.
6096 * We assume 0x07 really means auto mode while this quirk is active,
6097 * as this is far more likely than the ThinkPad being in level 7,
6098 * which is only used by the firmware during thermal emergencies.
6101 static void fan_quirk1_detect(void)
6103 /* In some ThinkPads, neither the EC nor the ACPI
6104 * DSDT initialize the HFSP register, and it ends up
6105 * being initially set to 0x07 when it *could* be
6106 * either 0x07 or 0x80.
6108 * Enable for TP-1Y (T43), TP-78 (R51e),
6109 * TP-76 (R52), TP-70 (T43, R52), which are known
6110 * to be buggy. */
6111 if (fan_control_initial_status == 0x07) {
6112 switch (thinkpad_id.ec_model) {
6113 case 0x5931: /* TP-1Y */
6114 case 0x3837: /* TP-78 */
6115 case 0x3637: /* TP-76 */
6116 case 0x3037: /* TP-70 */
6117 printk(TPACPI_NOTICE
6118 "fan_init: initial fan status is unknown, "
6119 "assuming it is in auto mode\n");
6120 tp_features.fan_ctrl_status_undef = 1;
6126 static void fan_quirk1_handle(u8 *fan_status)
6128 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6129 if (*fan_status != fan_control_initial_status) {
6130 /* something changed the HFSP regisnter since
6131 * driver init time, so it is not undefined
6132 * anymore */
6133 tp_features.fan_ctrl_status_undef = 0;
6134 } else {
6135 /* Return most likely status. In fact, it
6136 * might be the only possible status */
6137 *fan_status = TP_EC_FAN_AUTO;
6143 * Call with fan_mutex held
6145 static void fan_update_desired_level(u8 status)
6147 if ((status &
6148 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6149 if (status > 7)
6150 fan_control_desired_level = 7;
6151 else
6152 fan_control_desired_level = status;
6156 static int fan_get_status(u8 *status)
6158 u8 s;
6160 /* TODO:
6161 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6163 switch (fan_status_access_mode) {
6164 case TPACPI_FAN_RD_ACPI_GFAN:
6165 /* 570, 600e/x, 770e, 770x */
6167 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6168 return -EIO;
6170 if (likely(status))
6171 *status = s & 0x07;
6173 break;
6175 case TPACPI_FAN_RD_TPEC:
6176 /* all except 570, 600e/x, 770e, 770x */
6177 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6178 return -EIO;
6180 if (likely(status)) {
6181 *status = s;
6182 fan_quirk1_handle(status);
6185 break;
6187 default:
6188 return -ENXIO;
6191 return 0;
6194 static int fan_get_status_safe(u8 *status)
6196 int rc;
6197 u8 s;
6199 if (mutex_lock_killable(&fan_mutex))
6200 return -ERESTARTSYS;
6201 rc = fan_get_status(&s);
6202 if (!rc)
6203 fan_update_desired_level(s);
6204 mutex_unlock(&fan_mutex);
6206 if (status)
6207 *status = s;
6209 return rc;
6212 static int fan_get_speed(unsigned int *speed)
6214 u8 hi, lo;
6216 switch (fan_status_access_mode) {
6217 case TPACPI_FAN_RD_TPEC:
6218 /* all except 570, 600e/x, 770e, 770x */
6219 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6220 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6221 return -EIO;
6223 if (likely(speed))
6224 *speed = (hi << 8) | lo;
6226 break;
6228 default:
6229 return -ENXIO;
6232 return 0;
6235 static int fan_set_level(int level)
6237 if (!fan_control_allowed)
6238 return -EPERM;
6240 switch (fan_control_access_mode) {
6241 case TPACPI_FAN_WR_ACPI_SFAN:
6242 if (level >= 0 && level <= 7) {
6243 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6244 return -EIO;
6245 } else
6246 return -EINVAL;
6247 break;
6249 case TPACPI_FAN_WR_ACPI_FANS:
6250 case TPACPI_FAN_WR_TPEC:
6251 if (!(level & TP_EC_FAN_AUTO) &&
6252 !(level & TP_EC_FAN_FULLSPEED) &&
6253 ((level < 0) || (level > 7)))
6254 return -EINVAL;
6256 /* safety net should the EC not support AUTO
6257 * or FULLSPEED mode bits and just ignore them */
6258 if (level & TP_EC_FAN_FULLSPEED)
6259 level |= 7; /* safety min speed 7 */
6260 else if (level & TP_EC_FAN_AUTO)
6261 level |= 4; /* safety min speed 4 */
6263 if (!acpi_ec_write(fan_status_offset, level))
6264 return -EIO;
6265 else
6266 tp_features.fan_ctrl_status_undef = 0;
6267 break;
6269 default:
6270 return -ENXIO;
6273 vdbg_printk(TPACPI_DBG_FAN,
6274 "fan control: set fan control register to 0x%02x\n", level);
6275 return 0;
6278 static int fan_set_level_safe(int level)
6280 int rc;
6282 if (!fan_control_allowed)
6283 return -EPERM;
6285 if (mutex_lock_killable(&fan_mutex))
6286 return -ERESTARTSYS;
6288 if (level == TPACPI_FAN_LAST_LEVEL)
6289 level = fan_control_desired_level;
6291 rc = fan_set_level(level);
6292 if (!rc)
6293 fan_update_desired_level(level);
6295 mutex_unlock(&fan_mutex);
6296 return rc;
6299 static int fan_set_enable(void)
6301 u8 s;
6302 int rc;
6304 if (!fan_control_allowed)
6305 return -EPERM;
6307 if (mutex_lock_killable(&fan_mutex))
6308 return -ERESTARTSYS;
6310 switch (fan_control_access_mode) {
6311 case TPACPI_FAN_WR_ACPI_FANS:
6312 case TPACPI_FAN_WR_TPEC:
6313 rc = fan_get_status(&s);
6314 if (rc < 0)
6315 break;
6317 /* Don't go out of emergency fan mode */
6318 if (s != 7) {
6319 s &= 0x07;
6320 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6323 if (!acpi_ec_write(fan_status_offset, s))
6324 rc = -EIO;
6325 else {
6326 tp_features.fan_ctrl_status_undef = 0;
6327 rc = 0;
6329 break;
6331 case TPACPI_FAN_WR_ACPI_SFAN:
6332 rc = fan_get_status(&s);
6333 if (rc < 0)
6334 break;
6336 s &= 0x07;
6338 /* Set fan to at least level 4 */
6339 s |= 4;
6341 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6342 rc = -EIO;
6343 else
6344 rc = 0;
6345 break;
6347 default:
6348 rc = -ENXIO;
6351 mutex_unlock(&fan_mutex);
6353 if (!rc)
6354 vdbg_printk(TPACPI_DBG_FAN,
6355 "fan control: set fan control register to 0x%02x\n",
6357 return rc;
6360 static int fan_set_disable(void)
6362 int rc;
6364 if (!fan_control_allowed)
6365 return -EPERM;
6367 if (mutex_lock_killable(&fan_mutex))
6368 return -ERESTARTSYS;
6370 rc = 0;
6371 switch (fan_control_access_mode) {
6372 case TPACPI_FAN_WR_ACPI_FANS:
6373 case TPACPI_FAN_WR_TPEC:
6374 if (!acpi_ec_write(fan_status_offset, 0x00))
6375 rc = -EIO;
6376 else {
6377 fan_control_desired_level = 0;
6378 tp_features.fan_ctrl_status_undef = 0;
6380 break;
6382 case TPACPI_FAN_WR_ACPI_SFAN:
6383 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6384 rc = -EIO;
6385 else
6386 fan_control_desired_level = 0;
6387 break;
6389 default:
6390 rc = -ENXIO;
6393 if (!rc)
6394 vdbg_printk(TPACPI_DBG_FAN,
6395 "fan control: set fan control register to 0\n");
6397 mutex_unlock(&fan_mutex);
6398 return rc;
6401 static int fan_set_speed(int speed)
6403 int rc;
6405 if (!fan_control_allowed)
6406 return -EPERM;
6408 if (mutex_lock_killable(&fan_mutex))
6409 return -ERESTARTSYS;
6411 rc = 0;
6412 switch (fan_control_access_mode) {
6413 case TPACPI_FAN_WR_ACPI_FANS:
6414 if (speed >= 0 && speed <= 65535) {
6415 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6416 speed, speed, speed))
6417 rc = -EIO;
6418 } else
6419 rc = -EINVAL;
6420 break;
6422 default:
6423 rc = -ENXIO;
6426 mutex_unlock(&fan_mutex);
6427 return rc;
6430 static void fan_watchdog_reset(void)
6432 static int fan_watchdog_active;
6434 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6435 return;
6437 if (fan_watchdog_active)
6438 cancel_delayed_work(&fan_watchdog_task);
6440 if (fan_watchdog_maxinterval > 0 &&
6441 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6442 fan_watchdog_active = 1;
6443 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6444 msecs_to_jiffies(fan_watchdog_maxinterval
6445 * 1000))) {
6446 printk(TPACPI_ERR
6447 "failed to queue the fan watchdog, "
6448 "watchdog will not trigger\n");
6450 } else
6451 fan_watchdog_active = 0;
6454 static void fan_watchdog_fire(struct work_struct *ignored)
6456 int rc;
6458 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6459 return;
6461 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6462 rc = fan_set_enable();
6463 if (rc < 0) {
6464 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6465 "will try again later...\n", -rc);
6466 /* reschedule for later */
6467 fan_watchdog_reset();
6472 * SYSFS fan layout: hwmon compatible (device)
6474 * pwm*_enable:
6475 * 0: "disengaged" mode
6476 * 1: manual mode
6477 * 2: native EC "auto" mode (recommended, hardware default)
6479 * pwm*: set speed in manual mode, ignored otherwise.
6480 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6481 * interpolation.
6483 * fan*_input: tachometer reading, RPM
6486 * SYSFS fan layout: extensions
6488 * fan_watchdog (driver):
6489 * fan watchdog interval in seconds, 0 disables (default), max 120
6492 /* sysfs fan pwm1_enable ----------------------------------------------- */
6493 static ssize_t fan_pwm1_enable_show(struct device *dev,
6494 struct device_attribute *attr,
6495 char *buf)
6497 int res, mode;
6498 u8 status;
6500 res = fan_get_status_safe(&status);
6501 if (res)
6502 return res;
6504 if (status & TP_EC_FAN_FULLSPEED) {
6505 mode = 0;
6506 } else if (status & TP_EC_FAN_AUTO) {
6507 mode = 2;
6508 } else
6509 mode = 1;
6511 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6514 static ssize_t fan_pwm1_enable_store(struct device *dev,
6515 struct device_attribute *attr,
6516 const char *buf, size_t count)
6518 unsigned long t;
6519 int res, level;
6521 if (parse_strtoul(buf, 2, &t))
6522 return -EINVAL;
6524 tpacpi_disclose_usertask("hwmon pwm1_enable",
6525 "set fan mode to %lu\n", t);
6527 switch (t) {
6528 case 0:
6529 level = TP_EC_FAN_FULLSPEED;
6530 break;
6531 case 1:
6532 level = TPACPI_FAN_LAST_LEVEL;
6533 break;
6534 case 2:
6535 level = TP_EC_FAN_AUTO;
6536 break;
6537 case 3:
6538 /* reserved for software-controlled auto mode */
6539 return -ENOSYS;
6540 default:
6541 return -EINVAL;
6544 res = fan_set_level_safe(level);
6545 if (res == -ENXIO)
6546 return -EINVAL;
6547 else if (res < 0)
6548 return res;
6550 fan_watchdog_reset();
6552 return count;
6555 static struct device_attribute dev_attr_fan_pwm1_enable =
6556 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6557 fan_pwm1_enable_show, fan_pwm1_enable_store);
6559 /* sysfs fan pwm1 ------------------------------------------------------ */
6560 static ssize_t fan_pwm1_show(struct device *dev,
6561 struct device_attribute *attr,
6562 char *buf)
6564 int res;
6565 u8 status;
6567 res = fan_get_status_safe(&status);
6568 if (res)
6569 return res;
6571 if ((status &
6572 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6573 status = fan_control_desired_level;
6575 if (status > 7)
6576 status = 7;
6578 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6581 static ssize_t fan_pwm1_store(struct device *dev,
6582 struct device_attribute *attr,
6583 const char *buf, size_t count)
6585 unsigned long s;
6586 int rc;
6587 u8 status, newlevel;
6589 if (parse_strtoul(buf, 255, &s))
6590 return -EINVAL;
6592 tpacpi_disclose_usertask("hwmon pwm1",
6593 "set fan speed to %lu\n", s);
6595 /* scale down from 0-255 to 0-7 */
6596 newlevel = (s >> 5) & 0x07;
6598 if (mutex_lock_killable(&fan_mutex))
6599 return -ERESTARTSYS;
6601 rc = fan_get_status(&status);
6602 if (!rc && (status &
6603 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6604 rc = fan_set_level(newlevel);
6605 if (rc == -ENXIO)
6606 rc = -EINVAL;
6607 else if (!rc) {
6608 fan_update_desired_level(newlevel);
6609 fan_watchdog_reset();
6613 mutex_unlock(&fan_mutex);
6614 return (rc)? rc : count;
6617 static struct device_attribute dev_attr_fan_pwm1 =
6618 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6619 fan_pwm1_show, fan_pwm1_store);
6621 /* sysfs fan fan1_input ------------------------------------------------ */
6622 static ssize_t fan_fan1_input_show(struct device *dev,
6623 struct device_attribute *attr,
6624 char *buf)
6626 int res;
6627 unsigned int speed;
6629 res = fan_get_speed(&speed);
6630 if (res < 0)
6631 return res;
6633 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6636 static struct device_attribute dev_attr_fan_fan1_input =
6637 __ATTR(fan1_input, S_IRUGO,
6638 fan_fan1_input_show, NULL);
6640 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6641 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6642 char *buf)
6644 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6647 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6648 const char *buf, size_t count)
6650 unsigned long t;
6652 if (parse_strtoul(buf, 120, &t))
6653 return -EINVAL;
6655 if (!fan_control_allowed)
6656 return -EPERM;
6658 fan_watchdog_maxinterval = t;
6659 fan_watchdog_reset();
6661 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6663 return count;
6666 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6667 fan_fan_watchdog_show, fan_fan_watchdog_store);
6669 /* --------------------------------------------------------------------- */
6670 static struct attribute *fan_attributes[] = {
6671 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6672 &dev_attr_fan_fan1_input.attr,
6673 NULL
6676 static const struct attribute_group fan_attr_group = {
6677 .attrs = fan_attributes,
6680 static int __init fan_init(struct ibm_init_struct *iibm)
6682 int rc;
6684 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6685 "initializing fan subdriver\n");
6687 mutex_init(&fan_mutex);
6688 fan_status_access_mode = TPACPI_FAN_NONE;
6689 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6690 fan_control_commands = 0;
6691 fan_watchdog_maxinterval = 0;
6692 tp_features.fan_ctrl_status_undef = 0;
6693 fan_control_desired_level = 7;
6695 TPACPI_ACPIHANDLE_INIT(fans);
6696 TPACPI_ACPIHANDLE_INIT(gfan);
6697 TPACPI_ACPIHANDLE_INIT(sfan);
6699 if (gfan_handle) {
6700 /* 570, 600e/x, 770e, 770x */
6701 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6702 } else {
6703 /* all other ThinkPads: note that even old-style
6704 * ThinkPad ECs supports the fan control register */
6705 if (likely(acpi_ec_read(fan_status_offset,
6706 &fan_control_initial_status))) {
6707 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6708 fan_quirk1_detect();
6709 } else {
6710 printk(TPACPI_ERR
6711 "ThinkPad ACPI EC access misbehaving, "
6712 "fan status and control unavailable\n");
6713 return 1;
6717 if (sfan_handle) {
6718 /* 570, 770x-JL */
6719 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6720 fan_control_commands |=
6721 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6722 } else {
6723 if (!gfan_handle) {
6724 /* gfan without sfan means no fan control */
6725 /* all other models implement TP EC 0x2f control */
6727 if (fans_handle) {
6728 /* X31, X40, X41 */
6729 fan_control_access_mode =
6730 TPACPI_FAN_WR_ACPI_FANS;
6731 fan_control_commands |=
6732 TPACPI_FAN_CMD_SPEED |
6733 TPACPI_FAN_CMD_LEVEL |
6734 TPACPI_FAN_CMD_ENABLE;
6735 } else {
6736 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6737 fan_control_commands |=
6738 TPACPI_FAN_CMD_LEVEL |
6739 TPACPI_FAN_CMD_ENABLE;
6744 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6745 "fan is %s, modes %d, %d\n",
6746 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6747 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6748 fan_status_access_mode, fan_control_access_mode);
6750 /* fan control master switch */
6751 if (!fan_control_allowed) {
6752 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6753 fan_control_commands = 0;
6754 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6755 "fan control features disabled by parameter\n");
6758 /* update fan_control_desired_level */
6759 if (fan_status_access_mode != TPACPI_FAN_NONE)
6760 fan_get_status_safe(NULL);
6762 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6763 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6764 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6765 &fan_attr_group);
6766 if (rc < 0)
6767 return rc;
6769 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6770 &driver_attr_fan_watchdog);
6771 if (rc < 0) {
6772 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6773 &fan_attr_group);
6774 return rc;
6776 return 0;
6777 } else
6778 return 1;
6781 static void fan_exit(void)
6783 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
6784 "cancelling any pending fan watchdog tasks\n");
6786 /* FIXME: can we really do this unconditionally? */
6787 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6788 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6789 &driver_attr_fan_watchdog);
6791 cancel_delayed_work(&fan_watchdog_task);
6792 flush_workqueue(tpacpi_wq);
6795 static void fan_suspend(pm_message_t state)
6797 int rc;
6799 if (!fan_control_allowed)
6800 return;
6802 /* Store fan status in cache */
6803 fan_control_resume_level = 0;
6804 rc = fan_get_status_safe(&fan_control_resume_level);
6805 if (rc < 0)
6806 printk(TPACPI_NOTICE
6807 "failed to read fan level for later "
6808 "restore during resume: %d\n", rc);
6810 /* if it is undefined, don't attempt to restore it.
6811 * KEEP THIS LAST */
6812 if (tp_features.fan_ctrl_status_undef)
6813 fan_control_resume_level = 0;
6816 static void fan_resume(void)
6818 u8 current_level = 7;
6819 bool do_set = false;
6820 int rc;
6822 /* DSDT *always* updates status on resume */
6823 tp_features.fan_ctrl_status_undef = 0;
6825 if (!fan_control_allowed ||
6826 !fan_control_resume_level ||
6827 (fan_get_status_safe(&current_level) < 0))
6828 return;
6830 switch (fan_control_access_mode) {
6831 case TPACPI_FAN_WR_ACPI_SFAN:
6832 /* never decrease fan level */
6833 do_set = (fan_control_resume_level > current_level);
6834 break;
6835 case TPACPI_FAN_WR_ACPI_FANS:
6836 case TPACPI_FAN_WR_TPEC:
6837 /* never decrease fan level, scale is:
6838 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6840 * We expect the firmware to set either 7 or AUTO, but we
6841 * handle FULLSPEED out of paranoia.
6843 * So, we can safely only restore FULLSPEED or 7, anything
6844 * else could slow the fan. Restoring AUTO is useless, at
6845 * best that's exactly what the DSDT already set (it is the
6846 * slower it uses).
6848 * Always keep in mind that the DSDT *will* have set the
6849 * fans to what the vendor supposes is the best level. We
6850 * muck with it only to speed the fan up.
6852 if (fan_control_resume_level != 7 &&
6853 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6854 return;
6855 else
6856 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6857 (current_level != fan_control_resume_level);
6858 break;
6859 default:
6860 return;
6862 if (do_set) {
6863 printk(TPACPI_NOTICE
6864 "restoring fan level to 0x%02x\n",
6865 fan_control_resume_level);
6866 rc = fan_set_level_safe(fan_control_resume_level);
6867 if (rc < 0)
6868 printk(TPACPI_NOTICE
6869 "failed to restore fan level: %d\n", rc);
6873 static int fan_read(char *p)
6875 int len = 0;
6876 int rc;
6877 u8 status;
6878 unsigned int speed = 0;
6880 switch (fan_status_access_mode) {
6881 case TPACPI_FAN_RD_ACPI_GFAN:
6882 /* 570, 600e/x, 770e, 770x */
6883 rc = fan_get_status_safe(&status);
6884 if (rc < 0)
6885 return rc;
6887 len += sprintf(p + len, "status:\t\t%s\n"
6888 "level:\t\t%d\n",
6889 (status != 0) ? "enabled" : "disabled", status);
6890 break;
6892 case TPACPI_FAN_RD_TPEC:
6893 /* all except 570, 600e/x, 770e, 770x */
6894 rc = fan_get_status_safe(&status);
6895 if (rc < 0)
6896 return rc;
6898 len += sprintf(p + len, "status:\t\t%s\n",
6899 (status != 0) ? "enabled" : "disabled");
6901 rc = fan_get_speed(&speed);
6902 if (rc < 0)
6903 return rc;
6905 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6907 if (status & TP_EC_FAN_FULLSPEED)
6908 /* Disengaged mode takes precedence */
6909 len += sprintf(p + len, "level:\t\tdisengaged\n");
6910 else if (status & TP_EC_FAN_AUTO)
6911 len += sprintf(p + len, "level:\t\tauto\n");
6912 else
6913 len += sprintf(p + len, "level:\t\t%d\n", status);
6914 break;
6916 case TPACPI_FAN_NONE:
6917 default:
6918 len += sprintf(p + len, "status:\t\tnot supported\n");
6921 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6922 len += sprintf(p + len, "commands:\tlevel <level>");
6924 switch (fan_control_access_mode) {
6925 case TPACPI_FAN_WR_ACPI_SFAN:
6926 len += sprintf(p + len, " (<level> is 0-7)\n");
6927 break;
6929 default:
6930 len += sprintf(p + len, " (<level> is 0-7, "
6931 "auto, disengaged, full-speed)\n");
6932 break;
6936 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6937 len += sprintf(p + len, "commands:\tenable, disable\n"
6938 "commands:\twatchdog <timeout> (<timeout> "
6939 "is 0 (off), 1-120 (seconds))\n");
6941 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6942 len += sprintf(p + len, "commands:\tspeed <speed>"
6943 " (<speed> is 0-65535)\n");
6945 return len;
6948 static int fan_write_cmd_level(const char *cmd, int *rc)
6950 int level;
6952 if (strlencmp(cmd, "level auto") == 0)
6953 level = TP_EC_FAN_AUTO;
6954 else if ((strlencmp(cmd, "level disengaged") == 0) |
6955 (strlencmp(cmd, "level full-speed") == 0))
6956 level = TP_EC_FAN_FULLSPEED;
6957 else if (sscanf(cmd, "level %d", &level) != 1)
6958 return 0;
6960 *rc = fan_set_level_safe(level);
6961 if (*rc == -ENXIO)
6962 printk(TPACPI_ERR "level command accepted for unsupported "
6963 "access mode %d", fan_control_access_mode);
6964 else if (!*rc)
6965 tpacpi_disclose_usertask("procfs fan",
6966 "set level to %d\n", level);
6968 return 1;
6971 static int fan_write_cmd_enable(const char *cmd, int *rc)
6973 if (strlencmp(cmd, "enable") != 0)
6974 return 0;
6976 *rc = fan_set_enable();
6977 if (*rc == -ENXIO)
6978 printk(TPACPI_ERR "enable command accepted for unsupported "
6979 "access mode %d", fan_control_access_mode);
6980 else if (!*rc)
6981 tpacpi_disclose_usertask("procfs fan", "enable\n");
6983 return 1;
6986 static int fan_write_cmd_disable(const char *cmd, int *rc)
6988 if (strlencmp(cmd, "disable") != 0)
6989 return 0;
6991 *rc = fan_set_disable();
6992 if (*rc == -ENXIO)
6993 printk(TPACPI_ERR "disable command accepted for unsupported "
6994 "access mode %d", fan_control_access_mode);
6995 else if (!*rc)
6996 tpacpi_disclose_usertask("procfs fan", "disable\n");
6998 return 1;
7001 static int fan_write_cmd_speed(const char *cmd, int *rc)
7003 int speed;
7005 /* TODO:
7006 * Support speed <low> <medium> <high> ? */
7008 if (sscanf(cmd, "speed %d", &speed) != 1)
7009 return 0;
7011 *rc = fan_set_speed(speed);
7012 if (*rc == -ENXIO)
7013 printk(TPACPI_ERR "speed command accepted for unsupported "
7014 "access mode %d", fan_control_access_mode);
7015 else if (!*rc)
7016 tpacpi_disclose_usertask("procfs fan",
7017 "set speed to %d\n", speed);
7019 return 1;
7022 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7024 int interval;
7026 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7027 return 0;
7029 if (interval < 0 || interval > 120)
7030 *rc = -EINVAL;
7031 else {
7032 fan_watchdog_maxinterval = interval;
7033 tpacpi_disclose_usertask("procfs fan",
7034 "set watchdog timer to %d\n",
7035 interval);
7038 return 1;
7041 static int fan_write(char *buf)
7043 char *cmd;
7044 int rc = 0;
7046 while (!rc && (cmd = next_cmd(&buf))) {
7047 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7048 fan_write_cmd_level(cmd, &rc)) &&
7049 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7050 (fan_write_cmd_enable(cmd, &rc) ||
7051 fan_write_cmd_disable(cmd, &rc) ||
7052 fan_write_cmd_watchdog(cmd, &rc))) &&
7053 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7054 fan_write_cmd_speed(cmd, &rc))
7056 rc = -EINVAL;
7057 else if (!rc)
7058 fan_watchdog_reset();
7061 return rc;
7064 static struct ibm_struct fan_driver_data = {
7065 .name = "fan",
7066 .read = fan_read,
7067 .write = fan_write,
7068 .exit = fan_exit,
7069 .suspend = fan_suspend,
7070 .resume = fan_resume,
7073 /****************************************************************************
7074 ****************************************************************************
7076 * Infrastructure
7078 ****************************************************************************
7079 ****************************************************************************/
7081 /* sysfs name ---------------------------------------------------------- */
7082 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7083 struct device_attribute *attr,
7084 char *buf)
7086 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7089 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7090 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7092 /* --------------------------------------------------------------------- */
7094 /* /proc support */
7095 static struct proc_dir_entry *proc_dir;
7098 * Module and infrastructure proble, init and exit handling
7101 static int force_load;
7103 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7104 static const char * __init str_supported(int is_supported)
7106 static char text_unsupported[] __initdata = "not supported";
7108 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7110 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7112 static void ibm_exit(struct ibm_struct *ibm)
7114 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7116 list_del_init(&ibm->all_drivers);
7118 if (ibm->flags.acpi_notify_installed) {
7119 dbg_printk(TPACPI_DBG_EXIT,
7120 "%s: acpi_remove_notify_handler\n", ibm->name);
7121 BUG_ON(!ibm->acpi);
7122 acpi_remove_notify_handler(*ibm->acpi->handle,
7123 ibm->acpi->type,
7124 dispatch_acpi_notify);
7125 ibm->flags.acpi_notify_installed = 0;
7126 ibm->flags.acpi_notify_installed = 0;
7129 if (ibm->flags.proc_created) {
7130 dbg_printk(TPACPI_DBG_EXIT,
7131 "%s: remove_proc_entry\n", ibm->name);
7132 remove_proc_entry(ibm->name, proc_dir);
7133 ibm->flags.proc_created = 0;
7136 if (ibm->flags.acpi_driver_registered) {
7137 dbg_printk(TPACPI_DBG_EXIT,
7138 "%s: acpi_bus_unregister_driver\n", ibm->name);
7139 BUG_ON(!ibm->acpi);
7140 acpi_bus_unregister_driver(ibm->acpi->driver);
7141 kfree(ibm->acpi->driver);
7142 ibm->acpi->driver = NULL;
7143 ibm->flags.acpi_driver_registered = 0;
7146 if (ibm->flags.init_called && ibm->exit) {
7147 ibm->exit();
7148 ibm->flags.init_called = 0;
7151 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7154 static int __init ibm_init(struct ibm_init_struct *iibm)
7156 int ret;
7157 struct ibm_struct *ibm = iibm->data;
7158 struct proc_dir_entry *entry;
7160 BUG_ON(ibm == NULL);
7162 INIT_LIST_HEAD(&ibm->all_drivers);
7164 if (ibm->flags.experimental && !experimental)
7165 return 0;
7167 dbg_printk(TPACPI_DBG_INIT,
7168 "probing for %s\n", ibm->name);
7170 if (iibm->init) {
7171 ret = iibm->init(iibm);
7172 if (ret > 0)
7173 return 0; /* probe failed */
7174 if (ret)
7175 return ret;
7177 ibm->flags.init_called = 1;
7180 if (ibm->acpi) {
7181 if (ibm->acpi->hid) {
7182 ret = register_tpacpi_subdriver(ibm);
7183 if (ret)
7184 goto err_out;
7187 if (ibm->acpi->notify) {
7188 ret = setup_acpi_notify(ibm);
7189 if (ret == -ENODEV) {
7190 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7191 ibm->name);
7192 ret = 0;
7193 goto err_out;
7195 if (ret < 0)
7196 goto err_out;
7200 dbg_printk(TPACPI_DBG_INIT,
7201 "%s installed\n", ibm->name);
7203 if (ibm->read) {
7204 entry = create_proc_entry(ibm->name,
7205 S_IFREG | S_IRUGO | S_IWUSR,
7206 proc_dir);
7207 if (!entry) {
7208 printk(TPACPI_ERR "unable to create proc entry %s\n",
7209 ibm->name);
7210 ret = -ENODEV;
7211 goto err_out;
7213 entry->owner = THIS_MODULE;
7214 entry->data = ibm;
7215 entry->read_proc = &dispatch_procfs_read;
7216 if (ibm->write)
7217 entry->write_proc = &dispatch_procfs_write;
7218 ibm->flags.proc_created = 1;
7221 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7223 return 0;
7225 err_out:
7226 dbg_printk(TPACPI_DBG_INIT,
7227 "%s: at error exit path with result %d\n",
7228 ibm->name, ret);
7230 ibm_exit(ibm);
7231 return (ret < 0)? ret : 0;
7234 /* Probing */
7236 /* returns 0 - probe ok, or < 0 - probe error.
7237 * Probe ok doesn't mean thinkpad found.
7238 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7239 static int __must_check __init get_thinkpad_model_data(
7240 struct thinkpad_id_data *tp)
7242 const struct dmi_device *dev = NULL;
7243 char ec_fw_string[18];
7244 char const *s;
7246 if (!tp)
7247 return -EINVAL;
7249 memset(tp, 0, sizeof(*tp));
7251 if (dmi_name_in_vendors("IBM"))
7252 tp->vendor = PCI_VENDOR_ID_IBM;
7253 else if (dmi_name_in_vendors("LENOVO"))
7254 tp->vendor = PCI_VENDOR_ID_LENOVO;
7255 else
7256 return 0;
7258 s = dmi_get_system_info(DMI_BIOS_VERSION);
7259 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7260 if (s && !tp->bios_version_str)
7261 return -ENOMEM;
7262 if (!tp->bios_version_str)
7263 return 0;
7264 tp->bios_model = tp->bios_version_str[0]
7265 | (tp->bios_version_str[1] << 8);
7268 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7269 * X32 or newer, all Z series; Some models must have an
7270 * up-to-date BIOS or they will not be detected.
7272 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7274 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7275 if (sscanf(dev->name,
7276 "IBM ThinkPad Embedded Controller -[%17c",
7277 ec_fw_string) == 1) {
7278 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7279 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7281 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7282 if (!tp->ec_version_str)
7283 return -ENOMEM;
7284 tp->ec_model = ec_fw_string[0]
7285 | (ec_fw_string[1] << 8);
7286 break;
7290 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7291 if (s && !strnicmp(s, "ThinkPad", 8)) {
7292 tp->model_str = kstrdup(s, GFP_KERNEL);
7293 if (!tp->model_str)
7294 return -ENOMEM;
7297 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7298 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7299 if (s && !tp->nummodel_str)
7300 return -ENOMEM;
7302 return 0;
7305 static int __init probe_for_thinkpad(void)
7307 int is_thinkpad;
7309 if (acpi_disabled)
7310 return -ENODEV;
7313 * Non-ancient models have better DMI tagging, but very old models
7314 * don't.
7316 is_thinkpad = (thinkpad_id.model_str != NULL);
7318 /* ec is required because many other handles are relative to it */
7319 TPACPI_ACPIHANDLE_INIT(ec);
7320 if (!ec_handle) {
7321 if (is_thinkpad)
7322 printk(TPACPI_ERR
7323 "Not yet supported ThinkPad detected!\n");
7324 return -ENODEV;
7328 * Risks a regression on very old machines, but reduces potential
7329 * false positives a damn great deal
7331 if (!is_thinkpad)
7332 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7334 if (!is_thinkpad && !force_load)
7335 return -ENODEV;
7337 return 0;
7341 /* Module init, exit, parameters */
7343 static struct ibm_init_struct ibms_init[] __initdata = {
7345 .init = thinkpad_acpi_driver_init,
7346 .data = &thinkpad_acpi_driver_data,
7349 .init = hotkey_init,
7350 .data = &hotkey_driver_data,
7353 .init = bluetooth_init,
7354 .data = &bluetooth_driver_data,
7357 .init = wan_init,
7358 .data = &wan_driver_data,
7361 .init = uwb_init,
7362 .data = &uwb_driver_data,
7364 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7366 .init = video_init,
7367 .data = &video_driver_data,
7369 #endif
7371 .init = light_init,
7372 .data = &light_driver_data,
7374 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7376 .init = dock_init,
7377 .data = &dock_driver_data[0],
7380 .init = dock_init2,
7381 .data = &dock_driver_data[1],
7383 #endif
7384 #ifdef CONFIG_THINKPAD_ACPI_BAY
7386 .init = bay_init,
7387 .data = &bay_driver_data,
7389 #endif
7391 .init = cmos_init,
7392 .data = &cmos_driver_data,
7395 .init = led_init,
7396 .data = &led_driver_data,
7399 .init = beep_init,
7400 .data = &beep_driver_data,
7403 .init = thermal_init,
7404 .data = &thermal_driver_data,
7407 .data = &ecdump_driver_data,
7410 .init = brightness_init,
7411 .data = &brightness_driver_data,
7414 .data = &volume_driver_data,
7417 .init = fan_init,
7418 .data = &fan_driver_data,
7422 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7424 unsigned int i;
7425 struct ibm_struct *ibm;
7427 if (!kp || !kp->name || !val)
7428 return -EINVAL;
7430 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7431 ibm = ibms_init[i].data;
7432 WARN_ON(ibm == NULL);
7434 if (!ibm || !ibm->name)
7435 continue;
7437 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7438 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7439 return -ENOSPC;
7440 strcpy(ibms_init[i].param, val);
7441 strcat(ibms_init[i].param, ",");
7442 return 0;
7446 return -EINVAL;
7449 module_param(experimental, int, 0);
7450 MODULE_PARM_DESC(experimental,
7451 "Enables experimental features when non-zero");
7453 module_param_named(debug, dbg_level, uint, 0);
7454 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7456 module_param(force_load, bool, 0);
7457 MODULE_PARM_DESC(force_load,
7458 "Attempts to load the driver even on a "
7459 "mis-identified ThinkPad when true");
7461 module_param_named(fan_control, fan_control_allowed, bool, 0);
7462 MODULE_PARM_DESC(fan_control,
7463 "Enables setting fan parameters features when true");
7465 module_param_named(brightness_mode, brightness_mode, int, 0);
7466 MODULE_PARM_DESC(brightness_mode,
7467 "Selects brightness control strategy: "
7468 "0=auto, 1=EC, 2=CMOS, 3=both");
7470 module_param(brightness_enable, uint, 0);
7471 MODULE_PARM_DESC(brightness_enable,
7472 "Enables backlight control when 1, disables when 0");
7474 module_param(hotkey_report_mode, uint, 0);
7475 MODULE_PARM_DESC(hotkey_report_mode,
7476 "used for backwards compatibility with userspace, "
7477 "see documentation");
7479 #define TPACPI_PARAM(feature) \
7480 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7481 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7482 "at module load, see documentation")
7484 TPACPI_PARAM(hotkey);
7485 TPACPI_PARAM(bluetooth);
7486 TPACPI_PARAM(video);
7487 TPACPI_PARAM(light);
7488 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7489 TPACPI_PARAM(dock);
7490 #endif
7491 #ifdef CONFIG_THINKPAD_ACPI_BAY
7492 TPACPI_PARAM(bay);
7493 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7494 TPACPI_PARAM(cmos);
7495 TPACPI_PARAM(led);
7496 TPACPI_PARAM(beep);
7497 TPACPI_PARAM(ecdump);
7498 TPACPI_PARAM(brightness);
7499 TPACPI_PARAM(volume);
7500 TPACPI_PARAM(fan);
7502 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7503 module_param(dbg_wlswemul, uint, 0);
7504 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7505 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7506 MODULE_PARM_DESC(wlsw_state,
7507 "Initial state of the emulated WLSW switch");
7509 module_param(dbg_bluetoothemul, uint, 0);
7510 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7511 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7512 MODULE_PARM_DESC(bluetooth_state,
7513 "Initial state of the emulated bluetooth switch");
7515 module_param(dbg_wwanemul, uint, 0);
7516 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7517 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7518 MODULE_PARM_DESC(wwan_state,
7519 "Initial state of the emulated WWAN switch");
7521 module_param(dbg_uwbemul, uint, 0);
7522 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7523 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7524 MODULE_PARM_DESC(uwb_state,
7525 "Initial state of the emulated UWB switch");
7526 #endif
7528 static void thinkpad_acpi_module_exit(void)
7530 struct ibm_struct *ibm, *itmp;
7532 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7534 list_for_each_entry_safe_reverse(ibm, itmp,
7535 &tpacpi_all_drivers,
7536 all_drivers) {
7537 ibm_exit(ibm);
7540 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7542 if (tpacpi_inputdev) {
7543 if (tp_features.input_device_registered)
7544 input_unregister_device(tpacpi_inputdev);
7545 else
7546 input_free_device(tpacpi_inputdev);
7549 if (tpacpi_hwmon)
7550 hwmon_device_unregister(tpacpi_hwmon);
7552 if (tp_features.sensors_pdev_attrs_registered)
7553 device_remove_file(&tpacpi_sensors_pdev->dev,
7554 &dev_attr_thinkpad_acpi_pdev_name);
7555 if (tpacpi_sensors_pdev)
7556 platform_device_unregister(tpacpi_sensors_pdev);
7557 if (tpacpi_pdev)
7558 platform_device_unregister(tpacpi_pdev);
7560 if (tp_features.sensors_pdrv_attrs_registered)
7561 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7562 if (tp_features.platform_drv_attrs_registered)
7563 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7565 if (tp_features.sensors_pdrv_registered)
7566 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7568 if (tp_features.platform_drv_registered)
7569 platform_driver_unregister(&tpacpi_pdriver);
7571 if (proc_dir)
7572 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7574 if (tpacpi_wq)
7575 destroy_workqueue(tpacpi_wq);
7577 kfree(thinkpad_id.bios_version_str);
7578 kfree(thinkpad_id.ec_version_str);
7579 kfree(thinkpad_id.model_str);
7583 static int __init thinkpad_acpi_module_init(void)
7585 int ret, i;
7587 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7589 /* Parameter checking */
7590 if (hotkey_report_mode > 2)
7591 return -EINVAL;
7593 /* Driver-level probe */
7595 ret = get_thinkpad_model_data(&thinkpad_id);
7596 if (ret) {
7597 printk(TPACPI_ERR
7598 "unable to get DMI data: %d\n", ret);
7599 thinkpad_acpi_module_exit();
7600 return ret;
7602 ret = probe_for_thinkpad();
7603 if (ret) {
7604 thinkpad_acpi_module_exit();
7605 return ret;
7608 /* Driver initialization */
7610 TPACPI_ACPIHANDLE_INIT(ecrd);
7611 TPACPI_ACPIHANDLE_INIT(ecwr);
7613 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7614 if (!tpacpi_wq) {
7615 thinkpad_acpi_module_exit();
7616 return -ENOMEM;
7619 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7620 if (!proc_dir) {
7621 printk(TPACPI_ERR
7622 "unable to create proc dir " TPACPI_PROC_DIR);
7623 thinkpad_acpi_module_exit();
7624 return -ENODEV;
7626 proc_dir->owner = THIS_MODULE;
7628 ret = platform_driver_register(&tpacpi_pdriver);
7629 if (ret) {
7630 printk(TPACPI_ERR
7631 "unable to register main platform driver\n");
7632 thinkpad_acpi_module_exit();
7633 return ret;
7635 tp_features.platform_drv_registered = 1;
7637 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7638 if (ret) {
7639 printk(TPACPI_ERR
7640 "unable to register hwmon platform driver\n");
7641 thinkpad_acpi_module_exit();
7642 return ret;
7644 tp_features.sensors_pdrv_registered = 1;
7646 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7647 if (!ret) {
7648 tp_features.platform_drv_attrs_registered = 1;
7649 ret = tpacpi_create_driver_attributes(
7650 &tpacpi_hwmon_pdriver.driver);
7652 if (ret) {
7653 printk(TPACPI_ERR
7654 "unable to create sysfs driver attributes\n");
7655 thinkpad_acpi_module_exit();
7656 return ret;
7658 tp_features.sensors_pdrv_attrs_registered = 1;
7661 /* Device initialization */
7662 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7663 NULL, 0);
7664 if (IS_ERR(tpacpi_pdev)) {
7665 ret = PTR_ERR(tpacpi_pdev);
7666 tpacpi_pdev = NULL;
7667 printk(TPACPI_ERR "unable to register platform device\n");
7668 thinkpad_acpi_module_exit();
7669 return ret;
7671 tpacpi_sensors_pdev = platform_device_register_simple(
7672 TPACPI_HWMON_DRVR_NAME,
7673 -1, NULL, 0);
7674 if (IS_ERR(tpacpi_sensors_pdev)) {
7675 ret = PTR_ERR(tpacpi_sensors_pdev);
7676 tpacpi_sensors_pdev = NULL;
7677 printk(TPACPI_ERR
7678 "unable to register hwmon platform device\n");
7679 thinkpad_acpi_module_exit();
7680 return ret;
7682 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7683 &dev_attr_thinkpad_acpi_pdev_name);
7684 if (ret) {
7685 printk(TPACPI_ERR
7686 "unable to create sysfs hwmon device attributes\n");
7687 thinkpad_acpi_module_exit();
7688 return ret;
7690 tp_features.sensors_pdev_attrs_registered = 1;
7691 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7692 if (IS_ERR(tpacpi_hwmon)) {
7693 ret = PTR_ERR(tpacpi_hwmon);
7694 tpacpi_hwmon = NULL;
7695 printk(TPACPI_ERR "unable to register hwmon device\n");
7696 thinkpad_acpi_module_exit();
7697 return ret;
7699 mutex_init(&tpacpi_inputdev_send_mutex);
7700 tpacpi_inputdev = input_allocate_device();
7701 if (!tpacpi_inputdev) {
7702 printk(TPACPI_ERR "unable to allocate input device\n");
7703 thinkpad_acpi_module_exit();
7704 return -ENOMEM;
7705 } else {
7706 /* Prepare input device, but don't register */
7707 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7708 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7709 tpacpi_inputdev->id.bustype = BUS_HOST;
7710 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7711 thinkpad_id.vendor :
7712 PCI_VENDOR_ID_IBM;
7713 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7714 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7716 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7717 ret = ibm_init(&ibms_init[i]);
7718 if (ret >= 0 && *ibms_init[i].param)
7719 ret = ibms_init[i].data->write(ibms_init[i].param);
7720 if (ret < 0) {
7721 thinkpad_acpi_module_exit();
7722 return ret;
7725 ret = input_register_device(tpacpi_inputdev);
7726 if (ret < 0) {
7727 printk(TPACPI_ERR "unable to register input device\n");
7728 thinkpad_acpi_module_exit();
7729 return ret;
7730 } else {
7731 tp_features.input_device_registered = 1;
7734 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7735 return 0;
7738 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7741 * DMI matching for module autoloading
7743 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7744 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7746 * Only models listed in thinkwiki will be supported, so add yours
7747 * if it is not there yet.
7749 #define IBM_BIOS_MODULE_ALIAS(__type) \
7750 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7752 /* Non-ancient thinkpads */
7753 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7754 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7756 /* Ancient thinkpad BIOSes have to be identified by
7757 * BIOS type or model number, and there are far less
7758 * BIOS types than model numbers... */
7759 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7760 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7761 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7763 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7764 MODULE_DESCRIPTION(TPACPI_DESC);
7765 MODULE_VERSION(TPACPI_VERSION);
7766 MODULE_LICENSE("GPL");
7768 module_init(thinkpad_acpi_module_init);
7769 module_exit(thinkpad_acpi_module_exit);