ACPI: thinkpad-acpi: resume with radios disabled
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob67cb4b10a303df7c7597be42415658d671d086de
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 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.21"
25 #define TPACPI_SYSFS_VERSION 0x020200
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/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
79 #include <acpi/acnamesp.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,
175 /* Debugging */
176 #define TPACPI_LOG TPACPI_FILE ": "
177 #define TPACPI_ERR KERN_ERR TPACPI_LOG
178 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
179 #define TPACPI_INFO KERN_INFO TPACPI_LOG
180 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
182 #define TPACPI_DBG_ALL 0xffff
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define dbg_printk(a_dbg_level, format, arg...) \
186 do { if (dbg_level & a_dbg_level) \
187 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
188 } while (0)
189 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
190 #define vdbg_printk(a_dbg_level, format, arg...) \
191 dbg_printk(a_dbg_level, format, ## arg)
192 static const char *str_supported(int is_supported);
193 #else
194 #define vdbg_printk(a_dbg_level, format, arg...)
195 #endif
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
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 fan_ctrl_status_undef:1;
265 u32 input_device_registered:1;
266 u32 platform_drv_registered:1;
267 u32 platform_drv_attrs_registered:1;
268 u32 sensors_pdrv_registered:1;
269 u32 sensors_pdrv_attrs_registered:1;
270 u32 sensors_pdev_attrs_registered:1;
271 u32 hotkey_poll_active:1;
272 } tp_features;
274 static struct {
275 u16 hotkey_mask_ff:1;
276 u16 bright_cmos_ec_unsync:1;
277 } tp_warned;
279 struct thinkpad_id_data {
280 unsigned int vendor; /* ThinkPad vendor:
281 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
284 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
286 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
287 u16 ec_model;
289 char *model_str; /* ThinkPad T43 */
290 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
292 static struct thinkpad_id_data thinkpad_id;
294 static enum {
295 TPACPI_LIFE_INIT = 0,
296 TPACPI_LIFE_RUNNING,
297 TPACPI_LIFE_EXITING,
298 } tpacpi_lifecycle;
300 static int experimental;
301 static u32 dbg_level;
303 static struct workqueue_struct *tpacpi_wq;
305 enum led_status_t {
306 TPACPI_LED_OFF = 0,
307 TPACPI_LED_ON,
308 TPACPI_LED_BLINK,
311 /* Special LED class that can defer work */
312 struct tpacpi_led_classdev {
313 struct led_classdev led_classdev;
314 struct work_struct work;
315 enum led_status_t new_state;
316 unsigned int led;
319 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
320 static int dbg_wlswemul;
321 static int tpacpi_wlsw_emulstate;
322 static int dbg_bluetoothemul;
323 static int tpacpi_bluetooth_emulstate;
324 static int dbg_wwanemul;
325 static int tpacpi_wwan_emulstate;
326 #endif
329 /****************************************************************************
330 ****************************************************************************
332 * ACPI Helpers and device model
334 ****************************************************************************
335 ****************************************************************************/
337 /*************************************************************************
338 * ACPI basic handles
341 static acpi_handle root_handle;
343 #define TPACPI_HANDLE(object, parent, paths...) \
344 static acpi_handle object##_handle; \
345 static acpi_handle *object##_parent = &parent##_handle; \
346 static char *object##_path; \
347 static char *object##_paths[] = { paths }
349 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
350 "\\_SB.PCI.ISA.EC", /* 570 */
351 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
352 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
353 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
354 "\\_SB.PCI0.ICH3.EC0", /* R31 */
355 "\\_SB.PCI0.LPC.EC", /* all others */
358 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
359 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
361 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
362 /* T4x, X31, X40 */
363 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
364 "\\CMS", /* R40, R40e */
365 ); /* all others */
367 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
368 "^HKEY", /* R30, R31 */
369 "HKEY", /* all others */
370 ); /* 570 */
372 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
373 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
374 "\\_SB.PCI0.VID0", /* 770e */
375 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
376 "\\_SB.PCI0.AGP.VID", /* all others */
377 ); /* R30, R31 */
380 /*************************************************************************
381 * ACPI helpers
384 static int acpi_evalf(acpi_handle handle,
385 void *res, char *method, char *fmt, ...)
387 char *fmt0 = fmt;
388 struct acpi_object_list params;
389 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
390 struct acpi_buffer result, *resultp;
391 union acpi_object out_obj;
392 acpi_status status;
393 va_list ap;
394 char res_type;
395 int success;
396 int quiet;
398 if (!*fmt) {
399 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
400 return 0;
403 if (*fmt == 'q') {
404 quiet = 1;
405 fmt++;
406 } else
407 quiet = 0;
409 res_type = *(fmt++);
411 params.count = 0;
412 params.pointer = &in_objs[0];
414 va_start(ap, fmt);
415 while (*fmt) {
416 char c = *(fmt++);
417 switch (c) {
418 case 'd': /* int */
419 in_objs[params.count].integer.value = va_arg(ap, int);
420 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
421 break;
422 /* add more types as needed */
423 default:
424 printk(TPACPI_ERR "acpi_evalf() called "
425 "with invalid format character '%c'\n", c);
426 return 0;
429 va_end(ap);
431 if (res_type != 'v') {
432 result.length = sizeof(out_obj);
433 result.pointer = &out_obj;
434 resultp = &result;
435 } else
436 resultp = NULL;
438 status = acpi_evaluate_object(handle, method, &params, resultp);
440 switch (res_type) {
441 case 'd': /* int */
442 if (res)
443 *(int *)res = out_obj.integer.value;
444 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
445 break;
446 case 'v': /* void */
447 success = status == AE_OK;
448 break;
449 /* add more types as needed */
450 default:
451 printk(TPACPI_ERR "acpi_evalf() called "
452 "with invalid format character '%c'\n", res_type);
453 return 0;
456 if (!success && !quiet)
457 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
458 method, fmt0, status);
460 return success;
463 static int acpi_ec_read(int i, u8 *p)
465 int v;
467 if (ecrd_handle) {
468 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
469 return 0;
470 *p = v;
471 } else {
472 if (ec_read(i, p) < 0)
473 return 0;
476 return 1;
479 static int acpi_ec_write(int i, u8 v)
481 if (ecwr_handle) {
482 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
483 return 0;
484 } else {
485 if (ec_write(i, v) < 0)
486 return 0;
489 return 1;
492 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
493 static int _sta(acpi_handle handle)
495 int status;
497 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
498 status = 0;
500 return status;
502 #endif
504 static int issue_thinkpad_cmos_command(int cmos_cmd)
506 if (!cmos_handle)
507 return -ENXIO;
509 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
510 return -EIO;
512 return 0;
515 /*************************************************************************
516 * ACPI device model
519 #define TPACPI_ACPIHANDLE_INIT(object) \
520 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
521 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
523 static void drv_acpi_handle_init(char *name,
524 acpi_handle *handle, acpi_handle parent,
525 char **paths, int num_paths, char **path)
527 int i;
528 acpi_status status;
530 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
531 name);
533 for (i = 0; i < num_paths; i++) {
534 status = acpi_get_handle(parent, paths[i], handle);
535 if (ACPI_SUCCESS(status)) {
536 *path = paths[i];
537 dbg_printk(TPACPI_DBG_INIT,
538 "Found ACPI handle %s for %s\n",
539 *path, name);
540 return;
544 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
545 name);
546 *handle = NULL;
549 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
551 struct ibm_struct *ibm = data;
553 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
554 return;
556 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
557 return;
559 ibm->acpi->notify(ibm, event);
562 static int __init setup_acpi_notify(struct ibm_struct *ibm)
564 acpi_status status;
565 int rc;
567 BUG_ON(!ibm->acpi);
569 if (!*ibm->acpi->handle)
570 return 0;
572 vdbg_printk(TPACPI_DBG_INIT,
573 "setting up ACPI notify for %s\n", ibm->name);
575 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
576 if (rc < 0) {
577 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
578 ibm->name, rc);
579 return -ENODEV;
582 ibm->acpi->device->driver_data = ibm;
583 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
584 TPACPI_ACPI_EVENT_PREFIX,
585 ibm->name);
587 status = acpi_install_notify_handler(*ibm->acpi->handle,
588 ibm->acpi->type, dispatch_acpi_notify, ibm);
589 if (ACPI_FAILURE(status)) {
590 if (status == AE_ALREADY_EXISTS) {
591 printk(TPACPI_NOTICE
592 "another device driver is already "
593 "handling %s events\n", ibm->name);
594 } else {
595 printk(TPACPI_ERR
596 "acpi_install_notify_handler(%s) failed: %d\n",
597 ibm->name, status);
599 return -ENODEV;
601 ibm->flags.acpi_notify_installed = 1;
602 return 0;
605 static int __init tpacpi_device_add(struct acpi_device *device)
607 return 0;
610 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
612 int rc;
614 dbg_printk(TPACPI_DBG_INIT,
615 "registering %s as an ACPI driver\n", ibm->name);
617 BUG_ON(!ibm->acpi);
619 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
620 if (!ibm->acpi->driver) {
621 printk(TPACPI_ERR
622 "failed to allocate memory for ibm->acpi->driver\n");
623 return -ENOMEM;
626 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
627 ibm->acpi->driver->ids = ibm->acpi->hid;
629 ibm->acpi->driver->ops.add = &tpacpi_device_add;
631 rc = acpi_bus_register_driver(ibm->acpi->driver);
632 if (rc < 0) {
633 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
634 ibm->name, rc);
635 kfree(ibm->acpi->driver);
636 ibm->acpi->driver = NULL;
637 } else if (!rc)
638 ibm->flags.acpi_driver_registered = 1;
640 return rc;
644 /****************************************************************************
645 ****************************************************************************
647 * Procfs Helpers
649 ****************************************************************************
650 ****************************************************************************/
652 static int dispatch_procfs_read(char *page, char **start, off_t off,
653 int count, int *eof, void *data)
655 struct ibm_struct *ibm = data;
656 int len;
658 if (!ibm || !ibm->read)
659 return -EINVAL;
661 len = ibm->read(page);
662 if (len < 0)
663 return len;
665 if (len <= off + count)
666 *eof = 1;
667 *start = page + off;
668 len -= off;
669 if (len > count)
670 len = count;
671 if (len < 0)
672 len = 0;
674 return len;
677 static int dispatch_procfs_write(struct file *file,
678 const char __user *userbuf,
679 unsigned long count, void *data)
681 struct ibm_struct *ibm = data;
682 char *kernbuf;
683 int ret;
685 if (!ibm || !ibm->write)
686 return -EINVAL;
688 kernbuf = kmalloc(count + 2, GFP_KERNEL);
689 if (!kernbuf)
690 return -ENOMEM;
692 if (copy_from_user(kernbuf, userbuf, count)) {
693 kfree(kernbuf);
694 return -EFAULT;
697 kernbuf[count] = 0;
698 strcat(kernbuf, ",");
699 ret = ibm->write(kernbuf);
700 if (ret == 0)
701 ret = count;
703 kfree(kernbuf);
705 return ret;
708 static char *next_cmd(char **cmds)
710 char *start = *cmds;
711 char *end;
713 while ((end = strchr(start, ',')) && end == start)
714 start = end + 1;
716 if (!end)
717 return NULL;
719 *end = 0;
720 *cmds = end + 1;
721 return start;
725 /****************************************************************************
726 ****************************************************************************
728 * Device model: input, hwmon and platform
730 ****************************************************************************
731 ****************************************************************************/
733 static struct platform_device *tpacpi_pdev;
734 static struct platform_device *tpacpi_sensors_pdev;
735 static struct device *tpacpi_hwmon;
736 static struct input_dev *tpacpi_inputdev;
737 static struct mutex tpacpi_inputdev_send_mutex;
738 static LIST_HEAD(tpacpi_all_drivers);
740 static int tpacpi_suspend_handler(struct platform_device *pdev,
741 pm_message_t state)
743 struct ibm_struct *ibm, *itmp;
745 list_for_each_entry_safe(ibm, itmp,
746 &tpacpi_all_drivers,
747 all_drivers) {
748 if (ibm->suspend)
749 (ibm->suspend)(state);
752 return 0;
755 static int tpacpi_resume_handler(struct platform_device *pdev)
757 struct ibm_struct *ibm, *itmp;
759 list_for_each_entry_safe(ibm, itmp,
760 &tpacpi_all_drivers,
761 all_drivers) {
762 if (ibm->resume)
763 (ibm->resume)();
766 return 0;
769 static struct platform_driver tpacpi_pdriver = {
770 .driver = {
771 .name = TPACPI_DRVR_NAME,
772 .owner = THIS_MODULE,
774 .suspend = tpacpi_suspend_handler,
775 .resume = tpacpi_resume_handler,
778 static struct platform_driver tpacpi_hwmon_pdriver = {
779 .driver = {
780 .name = TPACPI_HWMON_DRVR_NAME,
781 .owner = THIS_MODULE,
785 /*************************************************************************
786 * sysfs support helpers
789 struct attribute_set {
790 unsigned int members, max_members;
791 struct attribute_group group;
794 struct attribute_set_obj {
795 struct attribute_set s;
796 struct attribute *a;
797 } __attribute__((packed));
799 static struct attribute_set *create_attr_set(unsigned int max_members,
800 const char *name)
802 struct attribute_set_obj *sobj;
804 if (max_members == 0)
805 return NULL;
807 /* Allocates space for implicit NULL at the end too */
808 sobj = kzalloc(sizeof(struct attribute_set_obj) +
809 max_members * sizeof(struct attribute *),
810 GFP_KERNEL);
811 if (!sobj)
812 return NULL;
813 sobj->s.max_members = max_members;
814 sobj->s.group.attrs = &sobj->a;
815 sobj->s.group.name = name;
817 return &sobj->s;
820 #define destroy_attr_set(_set) \
821 kfree(_set);
823 /* not multi-threaded safe, use it in a single thread per set */
824 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
826 if (!s || !attr)
827 return -EINVAL;
829 if (s->members >= s->max_members)
830 return -ENOMEM;
832 s->group.attrs[s->members] = attr;
833 s->members++;
835 return 0;
838 static int add_many_to_attr_set(struct attribute_set *s,
839 struct attribute **attr,
840 unsigned int count)
842 int i, res;
844 for (i = 0; i < count; i++) {
845 res = add_to_attr_set(s, attr[i]);
846 if (res)
847 return res;
850 return 0;
853 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
855 sysfs_remove_group(kobj, &s->group);
856 destroy_attr_set(s);
859 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
860 sysfs_create_group(_kobj, &_attr_set->group)
862 static int parse_strtoul(const char *buf,
863 unsigned long max, unsigned long *value)
865 char *endp;
867 while (*buf && isspace(*buf))
868 buf++;
869 *value = simple_strtoul(buf, &endp, 0);
870 while (*endp && isspace(*endp))
871 endp++;
872 if (*endp || *value > max)
873 return -EINVAL;
875 return 0;
878 static void tpacpi_disable_brightness_delay(void)
880 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
881 printk(TPACPI_NOTICE
882 "ACPI backlight control delay disabled\n");
885 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
887 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
888 union acpi_object *obj;
889 int rc;
891 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
892 obj = (union acpi_object *)buffer.pointer;
893 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
894 printk(TPACPI_ERR "Unknown _BCL data, "
895 "please report this to %s\n", TPACPI_MAIL);
896 rc = 0;
897 } else {
898 rc = obj->package.count;
900 } else {
901 return 0;
904 kfree(buffer.pointer);
905 return rc;
908 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
909 u32 lvl, void *context, void **rv)
911 char name[ACPI_PATH_SEGMENT_LENGTH];
912 struct acpi_buffer buffer = { sizeof(name), &name };
914 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
915 !strncmp("_BCL", name, sizeof(name) - 1)) {
916 BUG_ON(!rv || !*rv);
917 **(int **)rv = tpacpi_query_bcl_levels(handle);
918 return AE_CTRL_TERMINATE;
919 } else {
920 return AE_OK;
925 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
927 static int __init tpacpi_check_std_acpi_brightness_support(void)
929 int status;
930 int bcl_levels = 0;
931 void *bcl_ptr = &bcl_levels;
933 if (!vid_handle) {
934 TPACPI_ACPIHANDLE_INIT(vid);
936 if (!vid_handle)
937 return 0;
940 * Search for a _BCL method, and execute it. This is safe on all
941 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
942 * BIOS in ACPI backlight control mode. We do NOT have to care
943 * about calling the _BCL method in an enabled video device, any
944 * will do for our purposes.
947 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
948 tpacpi_acpi_walk_find_bcl, NULL,
949 &bcl_ptr);
951 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
952 tp_features.bright_acpimode = 1;
953 return (bcl_levels - 2);
956 return 0;
959 static int __init tpacpi_new_rfkill(const unsigned int id,
960 struct rfkill **rfk,
961 const enum rfkill_type rfktype,
962 const char *name,
963 int (*toggle_radio)(void *, enum rfkill_state),
964 int (*get_state)(void *, enum rfkill_state *))
966 int res;
967 enum rfkill_state initial_state;
969 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
970 if (!*rfk) {
971 printk(TPACPI_ERR
972 "failed to allocate memory for rfkill class\n");
973 return -ENOMEM;
976 (*rfk)->name = name;
977 (*rfk)->get_state = get_state;
978 (*rfk)->toggle_radio = toggle_radio;
980 if (!get_state(NULL, &initial_state))
981 (*rfk)->state = initial_state;
983 res = rfkill_register(*rfk);
984 if (res < 0) {
985 printk(TPACPI_ERR
986 "failed to register %s rfkill switch: %d\n",
987 name, res);
988 rfkill_free(*rfk);
989 *rfk = NULL;
990 return res;
993 return 0;
996 /*************************************************************************
997 * thinkpad-acpi driver attributes
1000 /* interface_version --------------------------------------------------- */
1001 static ssize_t tpacpi_driver_interface_version_show(
1002 struct device_driver *drv,
1003 char *buf)
1005 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1008 static DRIVER_ATTR(interface_version, S_IRUGO,
1009 tpacpi_driver_interface_version_show, NULL);
1011 /* debug_level --------------------------------------------------------- */
1012 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1013 char *buf)
1015 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1018 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1019 const char *buf, size_t count)
1021 unsigned long t;
1023 if (parse_strtoul(buf, 0xffff, &t))
1024 return -EINVAL;
1026 dbg_level = t;
1028 return count;
1031 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1032 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1034 /* version ------------------------------------------------------------- */
1035 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1036 char *buf)
1038 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1039 TPACPI_DESC, TPACPI_VERSION);
1042 static DRIVER_ATTR(version, S_IRUGO,
1043 tpacpi_driver_version_show, NULL);
1045 /* --------------------------------------------------------------------- */
1047 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1049 static void tpacpi_send_radiosw_update(void);
1051 /* wlsw_emulstate ------------------------------------------------------ */
1052 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1053 char *buf)
1055 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1058 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1059 const char *buf, size_t count)
1061 unsigned long t;
1063 if (parse_strtoul(buf, 1, &t))
1064 return -EINVAL;
1066 if (tpacpi_wlsw_emulstate != t) {
1067 tpacpi_wlsw_emulstate = !!t;
1068 tpacpi_send_radiosw_update();
1069 } else
1070 tpacpi_wlsw_emulstate = !!t;
1072 return count;
1075 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1076 tpacpi_driver_wlsw_emulstate_show,
1077 tpacpi_driver_wlsw_emulstate_store);
1079 /* bluetooth_emulstate ------------------------------------------------- */
1080 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1081 struct device_driver *drv,
1082 char *buf)
1084 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1087 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1088 struct device_driver *drv,
1089 const char *buf, size_t count)
1091 unsigned long t;
1093 if (parse_strtoul(buf, 1, &t))
1094 return -EINVAL;
1096 tpacpi_bluetooth_emulstate = !!t;
1098 return count;
1101 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1102 tpacpi_driver_bluetooth_emulstate_show,
1103 tpacpi_driver_bluetooth_emulstate_store);
1105 /* wwan_emulstate ------------------------------------------------- */
1106 static ssize_t tpacpi_driver_wwan_emulstate_show(
1107 struct device_driver *drv,
1108 char *buf)
1110 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1113 static ssize_t tpacpi_driver_wwan_emulstate_store(
1114 struct device_driver *drv,
1115 const char *buf, size_t count)
1117 unsigned long t;
1119 if (parse_strtoul(buf, 1, &t))
1120 return -EINVAL;
1122 tpacpi_wwan_emulstate = !!t;
1124 return count;
1127 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1128 tpacpi_driver_wwan_emulstate_show,
1129 tpacpi_driver_wwan_emulstate_store);
1131 #endif
1133 /* --------------------------------------------------------------------- */
1135 static struct driver_attribute *tpacpi_driver_attributes[] = {
1136 &driver_attr_debug_level, &driver_attr_version,
1137 &driver_attr_interface_version,
1140 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1142 int i, res;
1144 i = 0;
1145 res = 0;
1146 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1147 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1148 i++;
1151 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1152 if (!res && dbg_wlswemul)
1153 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1154 if (!res && dbg_bluetoothemul)
1155 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1156 if (!res && dbg_wwanemul)
1157 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1158 #endif
1160 return res;
1163 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1165 int i;
1167 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1168 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1170 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1171 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1172 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1173 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1174 #endif
1177 /****************************************************************************
1178 ****************************************************************************
1180 * Subdrivers
1182 ****************************************************************************
1183 ****************************************************************************/
1185 /*************************************************************************
1186 * thinkpad-acpi init subdriver
1189 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1191 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1192 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1194 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1195 (thinkpad_id.bios_version_str) ?
1196 thinkpad_id.bios_version_str : "unknown",
1197 (thinkpad_id.ec_version_str) ?
1198 thinkpad_id.ec_version_str : "unknown");
1200 if (thinkpad_id.vendor && thinkpad_id.model_str)
1201 printk(TPACPI_INFO "%s %s, model %s\n",
1202 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1203 "IBM" : ((thinkpad_id.vendor ==
1204 PCI_VENDOR_ID_LENOVO) ?
1205 "Lenovo" : "Unknown vendor"),
1206 thinkpad_id.model_str,
1207 (thinkpad_id.nummodel_str) ?
1208 thinkpad_id.nummodel_str : "unknown");
1210 return 0;
1213 static int thinkpad_acpi_driver_read(char *p)
1215 int len = 0;
1217 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1218 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1220 return len;
1223 static struct ibm_struct thinkpad_acpi_driver_data = {
1224 .name = "driver",
1225 .read = thinkpad_acpi_driver_read,
1228 /*************************************************************************
1229 * Hotkey subdriver
1232 enum { /* hot key scan codes (derived from ACPI DSDT) */
1233 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1234 TP_ACPI_HOTKEYSCAN_FNF2,
1235 TP_ACPI_HOTKEYSCAN_FNF3,
1236 TP_ACPI_HOTKEYSCAN_FNF4,
1237 TP_ACPI_HOTKEYSCAN_FNF5,
1238 TP_ACPI_HOTKEYSCAN_FNF6,
1239 TP_ACPI_HOTKEYSCAN_FNF7,
1240 TP_ACPI_HOTKEYSCAN_FNF8,
1241 TP_ACPI_HOTKEYSCAN_FNF9,
1242 TP_ACPI_HOTKEYSCAN_FNF10,
1243 TP_ACPI_HOTKEYSCAN_FNF11,
1244 TP_ACPI_HOTKEYSCAN_FNF12,
1245 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1246 TP_ACPI_HOTKEYSCAN_FNINSERT,
1247 TP_ACPI_HOTKEYSCAN_FNDELETE,
1248 TP_ACPI_HOTKEYSCAN_FNHOME,
1249 TP_ACPI_HOTKEYSCAN_FNEND,
1250 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1251 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1252 TP_ACPI_HOTKEYSCAN_FNSPACE,
1253 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1254 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1255 TP_ACPI_HOTKEYSCAN_MUTE,
1256 TP_ACPI_HOTKEYSCAN_THINKPAD,
1259 enum { /* Keys available through NVRAM polling */
1260 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1261 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1264 enum { /* Positions of some of the keys in hotkey masks */
1265 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1266 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1267 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1268 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1269 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1270 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1271 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1272 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1273 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1274 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1275 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1278 enum { /* NVRAM to ACPI HKEY group map */
1279 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1280 TP_ACPI_HKEY_ZOOM_MASK |
1281 TP_ACPI_HKEY_DISPSWTCH_MASK |
1282 TP_ACPI_HKEY_HIBERNATE_MASK,
1283 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1284 TP_ACPI_HKEY_BRGHTDWN_MASK,
1285 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1286 TP_ACPI_HKEY_VOLDWN_MASK |
1287 TP_ACPI_HKEY_MUTE_MASK,
1290 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1291 struct tp_nvram_state {
1292 u16 thinkpad_toggle:1;
1293 u16 zoom_toggle:1;
1294 u16 display_toggle:1;
1295 u16 thinklight_toggle:1;
1296 u16 hibernate_toggle:1;
1297 u16 displayexp_toggle:1;
1298 u16 display_state:1;
1299 u16 brightness_toggle:1;
1300 u16 volume_toggle:1;
1301 u16 mute:1;
1303 u8 brightness_level;
1304 u8 volume_level;
1307 static struct task_struct *tpacpi_hotkey_task;
1308 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1309 static int hotkey_poll_freq = 10; /* Hz */
1310 static struct mutex hotkey_thread_mutex;
1311 static struct mutex hotkey_thread_data_mutex;
1312 static unsigned int hotkey_config_change;
1314 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1316 #define hotkey_source_mask 0U
1318 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1320 static struct mutex hotkey_mutex;
1322 static enum { /* Reasons for waking up */
1323 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1324 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1325 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1326 } hotkey_wakeup_reason;
1328 static int hotkey_autosleep_ack;
1330 static int hotkey_orig_status;
1331 static u32 hotkey_orig_mask;
1332 static u32 hotkey_all_mask;
1333 static u32 hotkey_reserved_mask;
1334 static u32 hotkey_mask;
1336 static unsigned int hotkey_report_mode;
1338 static u16 *hotkey_keycode_map;
1340 static struct attribute_set *hotkey_dev_attributes;
1342 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1343 #define HOTKEY_CONFIG_CRITICAL_START \
1344 do { \
1345 mutex_lock(&hotkey_thread_data_mutex); \
1346 hotkey_config_change++; \
1347 } while (0);
1348 #define HOTKEY_CONFIG_CRITICAL_END \
1349 mutex_unlock(&hotkey_thread_data_mutex);
1350 #else
1351 #define HOTKEY_CONFIG_CRITICAL_START
1352 #define HOTKEY_CONFIG_CRITICAL_END
1353 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1355 /* HKEY.MHKG() return bits */
1356 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1358 static int hotkey_get_wlsw(int *status)
1360 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1361 if (dbg_wlswemul) {
1362 *status = !!tpacpi_wlsw_emulstate;
1363 return 0;
1365 #endif
1366 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1367 return -EIO;
1368 return 0;
1371 static int hotkey_get_tablet_mode(int *status)
1373 int s;
1375 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1376 return -EIO;
1378 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1379 return 0;
1383 * Call with hotkey_mutex held
1385 static int hotkey_mask_get(void)
1387 u32 m = 0;
1389 if (tp_features.hotkey_mask) {
1390 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1391 return -EIO;
1393 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1395 return 0;
1399 * Call with hotkey_mutex held
1401 static int hotkey_mask_set(u32 mask)
1403 int i;
1404 int rc = 0;
1406 if (tp_features.hotkey_mask) {
1407 if (!tp_warned.hotkey_mask_ff &&
1408 (mask == 0xffff || mask == 0xffffff ||
1409 mask == 0xffffffff)) {
1410 tp_warned.hotkey_mask_ff = 1;
1411 printk(TPACPI_NOTICE
1412 "setting the hotkey mask to 0x%08x is likely "
1413 "not the best way to go about it\n", mask);
1414 printk(TPACPI_NOTICE
1415 "please consider using the driver defaults, "
1416 "and refer to up-to-date thinkpad-acpi "
1417 "documentation\n");
1420 HOTKEY_CONFIG_CRITICAL_START
1421 for (i = 0; i < 32; i++) {
1422 u32 m = 1 << i;
1423 /* enable in firmware mask only keys not in NVRAM
1424 * mode, but enable the key in the cached hotkey_mask
1425 * regardless of mode, or the key will end up
1426 * disabled by hotkey_mask_get() */
1427 if (!acpi_evalf(hkey_handle,
1428 NULL, "MHKM", "vdd", i + 1,
1429 !!((mask & ~hotkey_source_mask) & m))) {
1430 rc = -EIO;
1431 break;
1432 } else {
1433 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1436 HOTKEY_CONFIG_CRITICAL_END
1438 /* hotkey_mask_get must be called unconditionally below */
1439 if (!hotkey_mask_get() && !rc &&
1440 (hotkey_mask & ~hotkey_source_mask) !=
1441 (mask & ~hotkey_source_mask)) {
1442 printk(TPACPI_NOTICE
1443 "requested hot key mask 0x%08x, but "
1444 "firmware forced it to 0x%08x\n",
1445 mask, hotkey_mask);
1447 } else {
1448 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1449 HOTKEY_CONFIG_CRITICAL_START
1450 hotkey_mask = mask & hotkey_source_mask;
1451 HOTKEY_CONFIG_CRITICAL_END
1452 hotkey_mask_get();
1453 if (hotkey_mask != mask) {
1454 printk(TPACPI_NOTICE
1455 "requested hot key mask 0x%08x, "
1456 "forced to 0x%08x (NVRAM poll mask is "
1457 "0x%08x): no firmware mask support\n",
1458 mask, hotkey_mask, hotkey_source_mask);
1460 #else
1461 hotkey_mask_get();
1462 rc = -ENXIO;
1463 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1466 return rc;
1469 static int hotkey_status_get(int *status)
1471 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1472 return -EIO;
1474 return 0;
1477 static int hotkey_status_set(int status)
1479 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1480 return -EIO;
1482 return 0;
1485 static void tpacpi_input_send_tabletsw(void)
1487 int state;
1489 if (tp_features.hotkey_tablet &&
1490 !hotkey_get_tablet_mode(&state)) {
1491 mutex_lock(&tpacpi_inputdev_send_mutex);
1493 input_report_switch(tpacpi_inputdev,
1494 SW_TABLET_MODE, !!state);
1495 input_sync(tpacpi_inputdev);
1497 mutex_unlock(&tpacpi_inputdev_send_mutex);
1501 static void tpacpi_input_send_key(unsigned int scancode)
1503 unsigned int keycode;
1505 keycode = hotkey_keycode_map[scancode];
1507 if (keycode != KEY_RESERVED) {
1508 mutex_lock(&tpacpi_inputdev_send_mutex);
1510 input_report_key(tpacpi_inputdev, keycode, 1);
1511 if (keycode == KEY_UNKNOWN)
1512 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1513 scancode);
1514 input_sync(tpacpi_inputdev);
1516 input_report_key(tpacpi_inputdev, keycode, 0);
1517 if (keycode == KEY_UNKNOWN)
1518 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1519 scancode);
1520 input_sync(tpacpi_inputdev);
1522 mutex_unlock(&tpacpi_inputdev_send_mutex);
1526 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1527 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1529 static void tpacpi_hotkey_send_key(unsigned int scancode)
1531 tpacpi_input_send_key(scancode);
1532 if (hotkey_report_mode < 2) {
1533 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1534 0x80, 0x1001 + scancode);
1538 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1540 u8 d;
1542 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1543 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1544 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1545 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1546 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1547 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1549 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1550 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1551 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1553 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1554 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1555 n->displayexp_toggle =
1556 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1558 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1559 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1560 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1561 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1562 n->brightness_toggle =
1563 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1565 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1566 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1567 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1568 >> TP_NVRAM_POS_LEVEL_VOLUME;
1569 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1570 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1574 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1575 do { \
1576 if ((mask & (1 << __scancode)) && \
1577 oldn->__member != newn->__member) \
1578 tpacpi_hotkey_send_key(__scancode); \
1579 } while (0)
1581 #define TPACPI_MAY_SEND_KEY(__scancode) \
1582 do { if (mask & (1 << __scancode)) \
1583 tpacpi_hotkey_send_key(__scancode); } while (0)
1585 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1586 struct tp_nvram_state *newn,
1587 u32 mask)
1589 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1590 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1591 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1592 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1594 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1596 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1598 /* handle volume */
1599 if (oldn->volume_toggle != newn->volume_toggle) {
1600 if (oldn->mute != newn->mute) {
1601 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1603 if (oldn->volume_level > newn->volume_level) {
1604 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1605 } else if (oldn->volume_level < newn->volume_level) {
1606 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1607 } else if (oldn->mute == newn->mute) {
1608 /* repeated key presses that didn't change state */
1609 if (newn->mute) {
1610 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1611 } else if (newn->volume_level != 0) {
1612 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1613 } else {
1614 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1619 /* handle brightness */
1620 if (oldn->brightness_toggle != newn->brightness_toggle) {
1621 if (oldn->brightness_level < newn->brightness_level) {
1622 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1623 } else if (oldn->brightness_level > newn->brightness_level) {
1624 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1625 } else {
1626 /* repeated key presses that didn't change state */
1627 if (newn->brightness_level != 0) {
1628 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1629 } else {
1630 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1636 #undef TPACPI_COMPARE_KEY
1637 #undef TPACPI_MAY_SEND_KEY
1639 static int hotkey_kthread(void *data)
1641 struct tp_nvram_state s[2];
1642 u32 mask;
1643 unsigned int si, so;
1644 unsigned long t;
1645 unsigned int change_detector, must_reset;
1647 mutex_lock(&hotkey_thread_mutex);
1649 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1650 goto exit;
1652 set_freezable();
1654 so = 0;
1655 si = 1;
1656 t = 0;
1658 /* Initial state for compares */
1659 mutex_lock(&hotkey_thread_data_mutex);
1660 change_detector = hotkey_config_change;
1661 mask = hotkey_source_mask & hotkey_mask;
1662 mutex_unlock(&hotkey_thread_data_mutex);
1663 hotkey_read_nvram(&s[so], mask);
1665 while (!kthread_should_stop() && hotkey_poll_freq) {
1666 if (t == 0)
1667 t = 1000/hotkey_poll_freq;
1668 t = msleep_interruptible(t);
1669 if (unlikely(kthread_should_stop()))
1670 break;
1671 must_reset = try_to_freeze();
1672 if (t > 0 && !must_reset)
1673 continue;
1675 mutex_lock(&hotkey_thread_data_mutex);
1676 if (must_reset || hotkey_config_change != change_detector) {
1677 /* forget old state on thaw or config change */
1678 si = so;
1679 t = 0;
1680 change_detector = hotkey_config_change;
1682 mask = hotkey_source_mask & hotkey_mask;
1683 mutex_unlock(&hotkey_thread_data_mutex);
1685 if (likely(mask)) {
1686 hotkey_read_nvram(&s[si], mask);
1687 if (likely(si != so)) {
1688 hotkey_compare_and_issue_event(&s[so], &s[si],
1689 mask);
1693 so = si;
1694 si ^= 1;
1697 exit:
1698 mutex_unlock(&hotkey_thread_mutex);
1699 return 0;
1702 static void hotkey_poll_stop_sync(void)
1704 if (tpacpi_hotkey_task) {
1705 if (frozen(tpacpi_hotkey_task) ||
1706 freezing(tpacpi_hotkey_task))
1707 thaw_process(tpacpi_hotkey_task);
1709 kthread_stop(tpacpi_hotkey_task);
1710 tpacpi_hotkey_task = NULL;
1711 mutex_lock(&hotkey_thread_mutex);
1712 /* at this point, the thread did exit */
1713 mutex_unlock(&hotkey_thread_mutex);
1717 /* call with hotkey_mutex held */
1718 static void hotkey_poll_setup(int may_warn)
1720 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1721 hotkey_poll_freq > 0 &&
1722 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1723 if (!tpacpi_hotkey_task) {
1724 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1725 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1726 if (IS_ERR(tpacpi_hotkey_task)) {
1727 tpacpi_hotkey_task = NULL;
1728 printk(TPACPI_ERR
1729 "could not create kernel thread "
1730 "for hotkey polling\n");
1733 } else {
1734 hotkey_poll_stop_sync();
1735 if (may_warn &&
1736 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1737 printk(TPACPI_NOTICE
1738 "hot keys 0x%08x require polling, "
1739 "which is currently disabled\n",
1740 hotkey_source_mask);
1745 static void hotkey_poll_setup_safe(int may_warn)
1747 mutex_lock(&hotkey_mutex);
1748 hotkey_poll_setup(may_warn);
1749 mutex_unlock(&hotkey_mutex);
1752 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1754 static void hotkey_poll_setup_safe(int __unused)
1758 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1760 static int hotkey_inputdev_open(struct input_dev *dev)
1762 switch (tpacpi_lifecycle) {
1763 case TPACPI_LIFE_INIT:
1765 * hotkey_init will call hotkey_poll_setup_safe
1766 * at the appropriate moment
1768 return 0;
1769 case TPACPI_LIFE_EXITING:
1770 return -EBUSY;
1771 case TPACPI_LIFE_RUNNING:
1772 hotkey_poll_setup_safe(0);
1773 return 0;
1776 /* Should only happen if tpacpi_lifecycle is corrupt */
1777 BUG();
1778 return -EBUSY;
1781 static void hotkey_inputdev_close(struct input_dev *dev)
1783 /* disable hotkey polling when possible */
1784 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1785 hotkey_poll_setup_safe(0);
1788 /* sysfs hotkey enable ------------------------------------------------- */
1789 static ssize_t hotkey_enable_show(struct device *dev,
1790 struct device_attribute *attr,
1791 char *buf)
1793 int res, status;
1795 res = hotkey_status_get(&status);
1796 if (res)
1797 return res;
1799 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1802 static ssize_t hotkey_enable_store(struct device *dev,
1803 struct device_attribute *attr,
1804 const char *buf, size_t count)
1806 unsigned long t;
1807 int res;
1809 if (parse_strtoul(buf, 1, &t))
1810 return -EINVAL;
1812 res = hotkey_status_set(t);
1814 return (res) ? res : count;
1817 static struct device_attribute dev_attr_hotkey_enable =
1818 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1819 hotkey_enable_show, hotkey_enable_store);
1821 /* sysfs hotkey mask --------------------------------------------------- */
1822 static ssize_t hotkey_mask_show(struct device *dev,
1823 struct device_attribute *attr,
1824 char *buf)
1826 int res;
1828 if (mutex_lock_interruptible(&hotkey_mutex))
1829 return -ERESTARTSYS;
1830 res = hotkey_mask_get();
1831 mutex_unlock(&hotkey_mutex);
1833 return (res)?
1834 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1837 static ssize_t hotkey_mask_store(struct device *dev,
1838 struct device_attribute *attr,
1839 const char *buf, size_t count)
1841 unsigned long t;
1842 int res;
1844 if (parse_strtoul(buf, 0xffffffffUL, &t))
1845 return -EINVAL;
1847 if (mutex_lock_interruptible(&hotkey_mutex))
1848 return -ERESTARTSYS;
1850 res = hotkey_mask_set(t);
1852 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1853 hotkey_poll_setup(1);
1854 #endif
1856 mutex_unlock(&hotkey_mutex);
1858 return (res) ? res : count;
1861 static struct device_attribute dev_attr_hotkey_mask =
1862 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1863 hotkey_mask_show, hotkey_mask_store);
1865 /* sysfs hotkey bios_enabled ------------------------------------------- */
1866 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1867 struct device_attribute *attr,
1868 char *buf)
1870 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1873 static struct device_attribute dev_attr_hotkey_bios_enabled =
1874 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1876 /* sysfs hotkey bios_mask ---------------------------------------------- */
1877 static ssize_t hotkey_bios_mask_show(struct device *dev,
1878 struct device_attribute *attr,
1879 char *buf)
1881 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1884 static struct device_attribute dev_attr_hotkey_bios_mask =
1885 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1887 /* sysfs hotkey all_mask ----------------------------------------------- */
1888 static ssize_t hotkey_all_mask_show(struct device *dev,
1889 struct device_attribute *attr,
1890 char *buf)
1892 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1893 hotkey_all_mask | hotkey_source_mask);
1896 static struct device_attribute dev_attr_hotkey_all_mask =
1897 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1899 /* sysfs hotkey recommended_mask --------------------------------------- */
1900 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1901 struct device_attribute *attr,
1902 char *buf)
1904 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1905 (hotkey_all_mask | hotkey_source_mask)
1906 & ~hotkey_reserved_mask);
1909 static struct device_attribute dev_attr_hotkey_recommended_mask =
1910 __ATTR(hotkey_recommended_mask, S_IRUGO,
1911 hotkey_recommended_mask_show, NULL);
1913 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1916 static ssize_t hotkey_source_mask_show(struct device *dev,
1917 struct device_attribute *attr,
1918 char *buf)
1920 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1923 static ssize_t hotkey_source_mask_store(struct device *dev,
1924 struct device_attribute *attr,
1925 const char *buf, size_t count)
1927 unsigned long t;
1929 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1930 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1931 return -EINVAL;
1933 if (mutex_lock_interruptible(&hotkey_mutex))
1934 return -ERESTARTSYS;
1936 HOTKEY_CONFIG_CRITICAL_START
1937 hotkey_source_mask = t;
1938 HOTKEY_CONFIG_CRITICAL_END
1940 hotkey_poll_setup(1);
1942 mutex_unlock(&hotkey_mutex);
1944 return count;
1947 static struct device_attribute dev_attr_hotkey_source_mask =
1948 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1949 hotkey_source_mask_show, hotkey_source_mask_store);
1951 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1952 static ssize_t hotkey_poll_freq_show(struct device *dev,
1953 struct device_attribute *attr,
1954 char *buf)
1956 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1959 static ssize_t hotkey_poll_freq_store(struct device *dev,
1960 struct device_attribute *attr,
1961 const char *buf, size_t count)
1963 unsigned long t;
1965 if (parse_strtoul(buf, 25, &t))
1966 return -EINVAL;
1968 if (mutex_lock_interruptible(&hotkey_mutex))
1969 return -ERESTARTSYS;
1971 hotkey_poll_freq = t;
1973 hotkey_poll_setup(1);
1974 mutex_unlock(&hotkey_mutex);
1976 return count;
1979 static struct device_attribute dev_attr_hotkey_poll_freq =
1980 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1981 hotkey_poll_freq_show, hotkey_poll_freq_store);
1983 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1985 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1986 static ssize_t hotkey_radio_sw_show(struct device *dev,
1987 struct device_attribute *attr,
1988 char *buf)
1990 int res, s;
1991 res = hotkey_get_wlsw(&s);
1992 if (res < 0)
1993 return res;
1995 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1998 static struct device_attribute dev_attr_hotkey_radio_sw =
1999 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2001 static void hotkey_radio_sw_notify_change(void)
2003 if (tp_features.hotkey_wlsw)
2004 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2005 "hotkey_radio_sw");
2008 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2009 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2010 struct device_attribute *attr,
2011 char *buf)
2013 int res, s;
2014 res = hotkey_get_tablet_mode(&s);
2015 if (res < 0)
2016 return res;
2018 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2021 static struct device_attribute dev_attr_hotkey_tablet_mode =
2022 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2024 static void hotkey_tablet_mode_notify_change(void)
2026 if (tp_features.hotkey_tablet)
2027 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2028 "hotkey_tablet_mode");
2031 /* sysfs hotkey report_mode -------------------------------------------- */
2032 static ssize_t hotkey_report_mode_show(struct device *dev,
2033 struct device_attribute *attr,
2034 char *buf)
2036 return snprintf(buf, PAGE_SIZE, "%d\n",
2037 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2040 static struct device_attribute dev_attr_hotkey_report_mode =
2041 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2043 /* sysfs wakeup reason (pollable) -------------------------------------- */
2044 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2045 struct device_attribute *attr,
2046 char *buf)
2048 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2051 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2052 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2054 static void hotkey_wakeup_reason_notify_change(void)
2056 if (tp_features.hotkey_mask)
2057 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2058 "wakeup_reason");
2061 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2062 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2063 struct device_attribute *attr,
2064 char *buf)
2066 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2069 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2070 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2071 hotkey_wakeup_hotunplug_complete_show, NULL);
2073 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2075 if (tp_features.hotkey_mask)
2076 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2077 "wakeup_hotunplug_complete");
2080 /* --------------------------------------------------------------------- */
2082 static struct attribute *hotkey_attributes[] __initdata = {
2083 &dev_attr_hotkey_enable.attr,
2084 &dev_attr_hotkey_bios_enabled.attr,
2085 &dev_attr_hotkey_report_mode.attr,
2086 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2087 &dev_attr_hotkey_mask.attr,
2088 &dev_attr_hotkey_all_mask.attr,
2089 &dev_attr_hotkey_recommended_mask.attr,
2090 &dev_attr_hotkey_source_mask.attr,
2091 &dev_attr_hotkey_poll_freq.attr,
2092 #endif
2095 static struct attribute *hotkey_mask_attributes[] __initdata = {
2096 &dev_attr_hotkey_bios_mask.attr,
2097 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2098 &dev_attr_hotkey_mask.attr,
2099 &dev_attr_hotkey_all_mask.attr,
2100 &dev_attr_hotkey_recommended_mask.attr,
2101 #endif
2102 &dev_attr_hotkey_wakeup_reason.attr,
2103 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2106 static void bluetooth_update_rfk(void);
2107 static void wan_update_rfk(void);
2108 static void tpacpi_send_radiosw_update(void)
2110 int wlsw;
2112 /* Sync these BEFORE sending any rfkill events */
2113 if (tp_features.bluetooth)
2114 bluetooth_update_rfk();
2115 if (tp_features.wan)
2116 wan_update_rfk();
2118 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2119 mutex_lock(&tpacpi_inputdev_send_mutex);
2121 input_report_switch(tpacpi_inputdev,
2122 SW_RFKILL_ALL, !!wlsw);
2123 input_sync(tpacpi_inputdev);
2125 mutex_unlock(&tpacpi_inputdev_send_mutex);
2127 hotkey_radio_sw_notify_change();
2130 static void hotkey_exit(void)
2132 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2133 hotkey_poll_stop_sync();
2134 #endif
2136 if (hotkey_dev_attributes)
2137 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2139 kfree(hotkey_keycode_map);
2141 if (tp_features.hotkey) {
2142 dbg_printk(TPACPI_DBG_EXIT,
2143 "restoring original hot key mask\n");
2144 /* no short-circuit boolean operator below! */
2145 if ((hotkey_mask_set(hotkey_orig_mask) |
2146 hotkey_status_set(hotkey_orig_status)) != 0)
2147 printk(TPACPI_ERR
2148 "failed to restore hot key mask "
2149 "to BIOS defaults\n");
2153 static int __init hotkey_init(struct ibm_init_struct *iibm)
2155 /* Requirements for changing the default keymaps:
2157 * 1. Many of the keys are mapped to KEY_RESERVED for very
2158 * good reasons. Do not change them unless you have deep
2159 * knowledge on the IBM and Lenovo ThinkPad firmware for
2160 * the various ThinkPad models. The driver behaves
2161 * differently for KEY_RESERVED: such keys have their
2162 * hot key mask *unset* in mask_recommended, and also
2163 * in the initial hot key mask programmed into the
2164 * firmware at driver load time, which means the firm-
2165 * ware may react very differently if you change them to
2166 * something else;
2168 * 2. You must be subscribed to the linux-thinkpad and
2169 * ibm-acpi-devel mailing lists, and you should read the
2170 * list archives since 2007 if you want to change the
2171 * keymaps. This requirement exists so that you will
2172 * know the past history of problems with the thinkpad-
2173 * acpi driver keymaps, and also that you will be
2174 * listening to any bug reports;
2176 * 3. Do not send thinkpad-acpi specific patches directly to
2177 * for merging, *ever*. Send them to the linux-acpi
2178 * mailinglist for comments. Merging is to be done only
2179 * through acpi-test and the ACPI maintainer.
2181 * If the above is too much to ask, don't change the keymap.
2182 * Ask the thinkpad-acpi maintainer to do it, instead.
2184 static u16 ibm_keycode_map[] __initdata = {
2185 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2186 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2187 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2188 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2190 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2191 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2192 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2193 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2195 /* brightness: firmware always reacts to them, unless
2196 * X.org did some tricks in the radeon BIOS scratch
2197 * registers of *some* models */
2198 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2199 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2201 /* Thinklight: firmware always react to it */
2202 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2204 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2205 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2207 /* Volume: firmware always react to it and reprograms
2208 * the built-in *extra* mixer. Never map it to control
2209 * another mixer by default. */
2210 KEY_RESERVED, /* 0x14: VOLUME UP */
2211 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2212 KEY_RESERVED, /* 0x16: MUTE */
2214 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2216 /* (assignments unknown, please report if found) */
2217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2218 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2220 static u16 lenovo_keycode_map[] __initdata = {
2221 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2222 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2223 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2224 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2226 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2227 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2228 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2229 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2231 /* These either have to go through ACPI video, or
2232 * act like in the IBM ThinkPads, so don't ever
2233 * enable them by default */
2234 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2235 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2237 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2239 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2240 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2242 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2243 * react to it and reprograms the built-in *extra* mixer.
2244 * Never map it to control another mixer by default.
2246 * T60?, T61, R60?, R61: firmware and EC tries to send
2247 * these over the regular keyboard, so these are no-ops,
2248 * but there are still weird bugs re. MUTE, so do not
2249 * change unless you get test reports from all Lenovo
2250 * models. May cause the BIOS to interfere with the
2251 * HDA mixer.
2253 KEY_RESERVED, /* 0x14: VOLUME UP */
2254 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2255 KEY_RESERVED, /* 0x16: MUTE */
2257 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2259 /* (assignments unknown, please report if found) */
2260 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2261 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2264 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2265 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2266 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2268 int res, i;
2269 int status;
2270 int hkeyv;
2272 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2274 BUG_ON(!tpacpi_inputdev);
2275 BUG_ON(tpacpi_inputdev->open != NULL ||
2276 tpacpi_inputdev->close != NULL);
2278 TPACPI_ACPIHANDLE_INIT(hkey);
2279 mutex_init(&hotkey_mutex);
2281 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2282 mutex_init(&hotkey_thread_mutex);
2283 mutex_init(&hotkey_thread_data_mutex);
2284 #endif
2286 /* hotkey not supported on 570 */
2287 tp_features.hotkey = hkey_handle != NULL;
2289 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2290 str_supported(tp_features.hotkey));
2292 if (!tp_features.hotkey)
2293 return 1;
2295 tpacpi_disable_brightness_delay();
2297 hotkey_dev_attributes = create_attr_set(13, NULL);
2298 if (!hotkey_dev_attributes)
2299 return -ENOMEM;
2300 res = add_many_to_attr_set(hotkey_dev_attributes,
2301 hotkey_attributes,
2302 ARRAY_SIZE(hotkey_attributes));
2303 if (res)
2304 goto err_exit;
2306 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2307 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2308 for HKEY interface version 0x100 */
2309 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2310 if ((hkeyv >> 8) != 1) {
2311 printk(TPACPI_ERR "unknown version of the "
2312 "HKEY interface: 0x%x\n", hkeyv);
2313 printk(TPACPI_ERR "please report this to %s\n",
2314 TPACPI_MAIL);
2315 } else {
2317 * MHKV 0x100 in A31, R40, R40e,
2318 * T4x, X31, and later
2320 tp_features.hotkey_mask = 1;
2324 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2325 str_supported(tp_features.hotkey_mask));
2327 if (tp_features.hotkey_mask) {
2328 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2329 "MHKA", "qd")) {
2330 printk(TPACPI_ERR
2331 "missing MHKA handler, "
2332 "please report this to %s\n",
2333 TPACPI_MAIL);
2334 /* FN+F12, FN+F4, FN+F3 */
2335 hotkey_all_mask = 0x080cU;
2339 /* hotkey_source_mask *must* be zero for
2340 * the first hotkey_mask_get */
2341 res = hotkey_status_get(&hotkey_orig_status);
2342 if (res)
2343 goto err_exit;
2345 if (tp_features.hotkey_mask) {
2346 res = hotkey_mask_get();
2347 if (res)
2348 goto err_exit;
2350 hotkey_orig_mask = hotkey_mask;
2351 res = add_many_to_attr_set(
2352 hotkey_dev_attributes,
2353 hotkey_mask_attributes,
2354 ARRAY_SIZE(hotkey_mask_attributes));
2355 if (res)
2356 goto err_exit;
2359 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2360 if (tp_features.hotkey_mask) {
2361 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2362 & ~hotkey_all_mask;
2363 } else {
2364 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2367 vdbg_printk(TPACPI_DBG_INIT,
2368 "hotkey source mask 0x%08x, polling freq %d\n",
2369 hotkey_source_mask, hotkey_poll_freq);
2370 #endif
2372 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2373 if (dbg_wlswemul) {
2374 tp_features.hotkey_wlsw = 1;
2375 printk(TPACPI_INFO
2376 "radio switch emulation enabled\n");
2377 } else
2378 #endif
2379 /* Not all thinkpads have a hardware radio switch */
2380 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2381 tp_features.hotkey_wlsw = 1;
2382 printk(TPACPI_INFO
2383 "radio switch found; radios are %s\n",
2384 enabled(status, 0));
2386 if (tp_features.hotkey_wlsw)
2387 res = add_to_attr_set(hotkey_dev_attributes,
2388 &dev_attr_hotkey_radio_sw.attr);
2390 /* For X41t, X60t, X61t Tablets... */
2391 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2392 tp_features.hotkey_tablet = 1;
2393 printk(TPACPI_INFO
2394 "possible tablet mode switch found; "
2395 "ThinkPad in %s mode\n",
2396 (status & TP_HOTKEY_TABLET_MASK)?
2397 "tablet" : "laptop");
2398 res = add_to_attr_set(hotkey_dev_attributes,
2399 &dev_attr_hotkey_tablet_mode.attr);
2402 if (!res)
2403 res = register_attr_set_with_sysfs(
2404 hotkey_dev_attributes,
2405 &tpacpi_pdev->dev.kobj);
2406 if (res)
2407 goto err_exit;
2409 /* Set up key map */
2411 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2412 GFP_KERNEL);
2413 if (!hotkey_keycode_map) {
2414 printk(TPACPI_ERR
2415 "failed to allocate memory for key map\n");
2416 res = -ENOMEM;
2417 goto err_exit;
2420 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2421 dbg_printk(TPACPI_DBG_INIT,
2422 "using Lenovo default hot key map\n");
2423 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2424 TPACPI_HOTKEY_MAP_SIZE);
2425 } else {
2426 dbg_printk(TPACPI_DBG_INIT,
2427 "using IBM default hot key map\n");
2428 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2429 TPACPI_HOTKEY_MAP_SIZE);
2432 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2433 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2434 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2435 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2436 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2437 tpacpi_inputdev->keycode = hotkey_keycode_map;
2438 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2439 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2440 set_bit(hotkey_keycode_map[i],
2441 tpacpi_inputdev->keybit);
2442 } else {
2443 if (i < sizeof(hotkey_reserved_mask)*8)
2444 hotkey_reserved_mask |= 1 << i;
2448 if (tp_features.hotkey_wlsw) {
2449 set_bit(EV_SW, tpacpi_inputdev->evbit);
2450 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2452 if (tp_features.hotkey_tablet) {
2453 set_bit(EV_SW, tpacpi_inputdev->evbit);
2454 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2457 /* Do not issue duplicate brightness change events to
2458 * userspace */
2459 if (!tp_features.bright_acpimode)
2460 /* update bright_acpimode... */
2461 tpacpi_check_std_acpi_brightness_support();
2463 if (tp_features.bright_acpimode) {
2464 printk(TPACPI_INFO
2465 "This ThinkPad has standard ACPI backlight "
2466 "brightness control, supported by the ACPI "
2467 "video driver\n");
2468 printk(TPACPI_NOTICE
2469 "Disabling thinkpad-acpi brightness events "
2470 "by default...\n");
2472 /* The hotkey_reserved_mask change below is not
2473 * necessary while the keys are at KEY_RESERVED in the
2474 * default map, but better safe than sorry, leave it
2475 * here as a marker of what we have to do, especially
2476 * when we finally become able to set this at runtime
2477 * on response to X.org requests */
2478 hotkey_reserved_mask |=
2479 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2480 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2483 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2484 res = hotkey_status_set(1);
2485 if (res) {
2486 hotkey_exit();
2487 return res;
2489 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2490 & ~hotkey_reserved_mask)
2491 | hotkey_orig_mask);
2492 if (res < 0 && res != -ENXIO) {
2493 hotkey_exit();
2494 return res;
2497 dbg_printk(TPACPI_DBG_INIT,
2498 "legacy hot key reporting over procfs %s\n",
2499 (hotkey_report_mode < 2) ?
2500 "enabled" : "disabled");
2502 tpacpi_inputdev->open = &hotkey_inputdev_open;
2503 tpacpi_inputdev->close = &hotkey_inputdev_close;
2505 hotkey_poll_setup_safe(1);
2506 tpacpi_send_radiosw_update();
2507 tpacpi_input_send_tabletsw();
2509 return 0;
2511 err_exit:
2512 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2513 hotkey_dev_attributes = NULL;
2515 return (res < 0)? res : 1;
2518 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2520 u32 hkey;
2521 unsigned int scancode;
2522 int send_acpi_ev;
2523 int ignore_acpi_ev;
2524 int unk_ev;
2526 if (event != 0x80) {
2527 printk(TPACPI_ERR
2528 "unknown HKEY notification event %d\n", event);
2529 /* forward it to userspace, maybe it knows how to handle it */
2530 acpi_bus_generate_netlink_event(
2531 ibm->acpi->device->pnp.device_class,
2532 dev_name(&ibm->acpi->device->dev),
2533 event, 0);
2534 return;
2537 while (1) {
2538 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2539 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2540 return;
2543 if (hkey == 0) {
2544 /* queue empty */
2545 return;
2548 send_acpi_ev = 1;
2549 ignore_acpi_ev = 0;
2550 unk_ev = 0;
2552 switch (hkey >> 12) {
2553 case 1:
2554 /* 0x1000-0x1FFF: key presses */
2555 scancode = hkey & 0xfff;
2556 if (scancode > 0 && scancode < 0x21) {
2557 scancode--;
2558 if (!(hotkey_source_mask & (1 << scancode))) {
2559 tpacpi_input_send_key(scancode);
2560 send_acpi_ev = 0;
2561 } else {
2562 ignore_acpi_ev = 1;
2564 } else {
2565 unk_ev = 1;
2567 break;
2568 case 2:
2569 /* Wakeup reason */
2570 switch (hkey) {
2571 case 0x2304: /* suspend, undock */
2572 case 0x2404: /* hibernation, undock */
2573 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2574 ignore_acpi_ev = 1;
2575 break;
2576 case 0x2305: /* suspend, bay eject */
2577 case 0x2405: /* hibernation, bay eject */
2578 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2579 ignore_acpi_ev = 1;
2580 break;
2581 default:
2582 unk_ev = 1;
2584 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2585 printk(TPACPI_INFO
2586 "woke up due to a hot-unplug "
2587 "request...\n");
2588 hotkey_wakeup_reason_notify_change();
2590 break;
2591 case 3:
2592 /* bay-related wakeups */
2593 if (hkey == 0x3003) {
2594 hotkey_autosleep_ack = 1;
2595 printk(TPACPI_INFO
2596 "bay ejected\n");
2597 hotkey_wakeup_hotunplug_complete_notify_change();
2598 } else {
2599 unk_ev = 1;
2601 break;
2602 case 4:
2603 /* dock-related wakeups */
2604 if (hkey == 0x4003) {
2605 hotkey_autosleep_ack = 1;
2606 printk(TPACPI_INFO
2607 "undocked\n");
2608 hotkey_wakeup_hotunplug_complete_notify_change();
2609 } else {
2610 unk_ev = 1;
2612 break;
2613 case 5:
2614 /* 0x5000-0x5FFF: human interface helpers */
2615 switch (hkey) {
2616 case 0x5010: /* Lenovo new BIOS: brightness changed */
2617 case 0x500b: /* X61t: tablet pen inserted into bay */
2618 case 0x500c: /* X61t: tablet pen removed from bay */
2619 break;
2620 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2621 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2622 tpacpi_input_send_tabletsw();
2623 hotkey_tablet_mode_notify_change();
2624 send_acpi_ev = 0;
2625 break;
2626 case 0x5001:
2627 case 0x5002:
2628 /* LID switch events. Do not propagate */
2629 ignore_acpi_ev = 1;
2630 break;
2631 default:
2632 unk_ev = 1;
2634 break;
2635 case 7:
2636 /* 0x7000-0x7FFF: misc */
2637 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2638 tpacpi_send_radiosw_update();
2639 send_acpi_ev = 0;
2640 break;
2642 /* fallthrough to default */
2643 default:
2644 unk_ev = 1;
2646 if (unk_ev) {
2647 printk(TPACPI_NOTICE
2648 "unhandled HKEY event 0x%04x\n", hkey);
2651 /* Legacy events */
2652 if (!ignore_acpi_ev &&
2653 (send_acpi_ev || hotkey_report_mode < 2)) {
2654 acpi_bus_generate_proc_event(ibm->acpi->device,
2655 event, hkey);
2658 /* netlink events */
2659 if (!ignore_acpi_ev && send_acpi_ev) {
2660 acpi_bus_generate_netlink_event(
2661 ibm->acpi->device->pnp.device_class,
2662 dev_name(&ibm->acpi->device->dev),
2663 event, hkey);
2668 static void hotkey_suspend(pm_message_t state)
2670 /* Do these on suspend, we get the events on early resume! */
2671 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2672 hotkey_autosleep_ack = 0;
2675 static void hotkey_resume(void)
2677 tpacpi_disable_brightness_delay();
2679 if (hotkey_mask_get())
2680 printk(TPACPI_ERR
2681 "error while trying to read hot key mask "
2682 "from firmware\n");
2683 tpacpi_send_radiosw_update();
2684 hotkey_tablet_mode_notify_change();
2685 hotkey_wakeup_reason_notify_change();
2686 hotkey_wakeup_hotunplug_complete_notify_change();
2687 hotkey_poll_setup_safe(0);
2690 /* procfs -------------------------------------------------------------- */
2691 static int hotkey_read(char *p)
2693 int res, status;
2694 int len = 0;
2696 if (!tp_features.hotkey) {
2697 len += sprintf(p + len, "status:\t\tnot supported\n");
2698 return len;
2701 if (mutex_lock_interruptible(&hotkey_mutex))
2702 return -ERESTARTSYS;
2703 res = hotkey_status_get(&status);
2704 if (!res)
2705 res = hotkey_mask_get();
2706 mutex_unlock(&hotkey_mutex);
2707 if (res)
2708 return res;
2710 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2711 if (tp_features.hotkey_mask) {
2712 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2713 len += sprintf(p + len,
2714 "commands:\tenable, disable, reset, <mask>\n");
2715 } else {
2716 len += sprintf(p + len, "mask:\t\tnot supported\n");
2717 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2720 return len;
2723 static int hotkey_write(char *buf)
2725 int res, status;
2726 u32 mask;
2727 char *cmd;
2729 if (!tp_features.hotkey)
2730 return -ENODEV;
2732 if (mutex_lock_interruptible(&hotkey_mutex))
2733 return -ERESTARTSYS;
2735 status = -1;
2736 mask = hotkey_mask;
2738 res = 0;
2739 while ((cmd = next_cmd(&buf))) {
2740 if (strlencmp(cmd, "enable") == 0) {
2741 status = 1;
2742 } else if (strlencmp(cmd, "disable") == 0) {
2743 status = 0;
2744 } else if (strlencmp(cmd, "reset") == 0) {
2745 status = hotkey_orig_status;
2746 mask = hotkey_orig_mask;
2747 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2748 /* mask set */
2749 } else if (sscanf(cmd, "%x", &mask) == 1) {
2750 /* mask set */
2751 } else {
2752 res = -EINVAL;
2753 goto errexit;
2756 if (status != -1)
2757 res = hotkey_status_set(status);
2759 if (!res && mask != hotkey_mask)
2760 res = hotkey_mask_set(mask);
2762 errexit:
2763 mutex_unlock(&hotkey_mutex);
2764 return res;
2767 static const struct acpi_device_id ibm_htk_device_ids[] = {
2768 {TPACPI_ACPI_HKEY_HID, 0},
2769 {"", 0},
2772 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2773 .hid = ibm_htk_device_ids,
2774 .notify = hotkey_notify,
2775 .handle = &hkey_handle,
2776 .type = ACPI_DEVICE_NOTIFY,
2779 static struct ibm_struct hotkey_driver_data = {
2780 .name = "hotkey",
2781 .read = hotkey_read,
2782 .write = hotkey_write,
2783 .exit = hotkey_exit,
2784 .resume = hotkey_resume,
2785 .suspend = hotkey_suspend,
2786 .acpi = &ibm_hotkey_acpidriver,
2789 /*************************************************************************
2790 * Bluetooth subdriver
2793 enum {
2794 /* ACPI GBDC/SBDC bits */
2795 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2796 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2797 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2798 off / last state */
2801 enum {
2802 /* ACPI \BLTH commands */
2803 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2804 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2805 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2806 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2807 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2810 static struct rfkill *tpacpi_bluetooth_rfkill;
2812 static void bluetooth_suspend(pm_message_t state)
2814 /* Try to make sure radio will resume powered off */
2815 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2816 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2819 static int bluetooth_get_radiosw(void)
2821 int status;
2823 if (!tp_features.bluetooth)
2824 return -ENODEV;
2826 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2827 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2828 return RFKILL_STATE_HARD_BLOCKED;
2830 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2831 if (dbg_bluetoothemul)
2832 return (tpacpi_bluetooth_emulstate) ?
2833 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2834 #endif
2836 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2837 return -EIO;
2839 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2840 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2843 static void bluetooth_update_rfk(void)
2845 int status;
2847 if (!tpacpi_bluetooth_rfkill)
2848 return;
2850 status = bluetooth_get_radiosw();
2851 if (status < 0)
2852 return;
2853 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2856 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2858 int status;
2860 if (!tp_features.bluetooth)
2861 return -ENODEV;
2863 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2864 * reason to risk weird behaviour. */
2865 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2866 && radio_on)
2867 return -EPERM;
2869 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2870 if (dbg_bluetoothemul) {
2871 tpacpi_bluetooth_emulstate = !!radio_on;
2872 if (update_rfk)
2873 bluetooth_update_rfk();
2874 return 0;
2876 #endif
2878 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
2879 if (radio_on)
2880 status = TP_ACPI_BLUETOOTH_RADIOSSW;
2881 else
2882 status = 0;
2883 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2884 return -EIO;
2886 if (update_rfk)
2887 bluetooth_update_rfk();
2889 return 0;
2892 /* sysfs bluetooth enable ---------------------------------------------- */
2893 static ssize_t bluetooth_enable_show(struct device *dev,
2894 struct device_attribute *attr,
2895 char *buf)
2897 int status;
2899 status = bluetooth_get_radiosw();
2900 if (status < 0)
2901 return status;
2903 return snprintf(buf, PAGE_SIZE, "%d\n",
2904 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2907 static ssize_t bluetooth_enable_store(struct device *dev,
2908 struct device_attribute *attr,
2909 const char *buf, size_t count)
2911 unsigned long t;
2912 int res;
2914 if (parse_strtoul(buf, 1, &t))
2915 return -EINVAL;
2917 res = bluetooth_set_radiosw(t, 1);
2919 return (res) ? res : count;
2922 static struct device_attribute dev_attr_bluetooth_enable =
2923 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2924 bluetooth_enable_show, bluetooth_enable_store);
2926 /* --------------------------------------------------------------------- */
2928 static struct attribute *bluetooth_attributes[] = {
2929 &dev_attr_bluetooth_enable.attr,
2930 NULL
2933 static const struct attribute_group bluetooth_attr_group = {
2934 .attrs = bluetooth_attributes,
2937 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2939 int bts = bluetooth_get_radiosw();
2941 if (bts < 0)
2942 return bts;
2944 *state = bts;
2945 return 0;
2948 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2950 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2953 static void bluetooth_exit(void)
2955 if (tpacpi_bluetooth_rfkill)
2956 rfkill_unregister(tpacpi_bluetooth_rfkill);
2958 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2959 &bluetooth_attr_group);
2962 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2964 int res;
2965 int status = 0;
2967 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2969 TPACPI_ACPIHANDLE_INIT(hkey);
2971 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2972 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2973 tp_features.bluetooth = hkey_handle &&
2974 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2976 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2977 str_supported(tp_features.bluetooth),
2978 status);
2980 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2981 if (dbg_bluetoothemul) {
2982 tp_features.bluetooth = 1;
2983 printk(TPACPI_INFO
2984 "bluetooth switch emulation enabled\n");
2985 } else
2986 #endif
2987 if (tp_features.bluetooth &&
2988 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2989 /* no bluetooth hardware present in system */
2990 tp_features.bluetooth = 0;
2991 dbg_printk(TPACPI_DBG_INIT,
2992 "bluetooth hardware not installed\n");
2995 if (!tp_features.bluetooth)
2996 return 1;
2998 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2999 &bluetooth_attr_group);
3000 if (res)
3001 return res;
3003 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3004 &tpacpi_bluetooth_rfkill,
3005 RFKILL_TYPE_BLUETOOTH,
3006 "tpacpi_bluetooth_sw",
3007 tpacpi_bluetooth_rfk_set,
3008 tpacpi_bluetooth_rfk_get);
3009 if (res) {
3010 bluetooth_exit();
3011 return res;
3014 return 0;
3017 /* procfs -------------------------------------------------------------- */
3018 static int bluetooth_read(char *p)
3020 int len = 0;
3021 int status = bluetooth_get_radiosw();
3023 if (!tp_features.bluetooth)
3024 len += sprintf(p + len, "status:\t\tnot supported\n");
3025 else {
3026 len += sprintf(p + len, "status:\t\t%s\n",
3027 (status == RFKILL_STATE_UNBLOCKED) ?
3028 "enabled" : "disabled");
3029 len += sprintf(p + len, "commands:\tenable, disable\n");
3032 return len;
3035 static int bluetooth_write(char *buf)
3037 char *cmd;
3039 if (!tp_features.bluetooth)
3040 return -ENODEV;
3042 while ((cmd = next_cmd(&buf))) {
3043 if (strlencmp(cmd, "enable") == 0) {
3044 bluetooth_set_radiosw(1, 1);
3045 } else if (strlencmp(cmd, "disable") == 0) {
3046 bluetooth_set_radiosw(0, 1);
3047 } else
3048 return -EINVAL;
3051 return 0;
3054 static struct ibm_struct bluetooth_driver_data = {
3055 .name = "bluetooth",
3056 .read = bluetooth_read,
3057 .write = bluetooth_write,
3058 .exit = bluetooth_exit,
3059 .suspend = bluetooth_suspend,
3062 /*************************************************************************
3063 * Wan subdriver
3066 enum {
3067 /* ACPI GWAN/SWAN bits */
3068 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3069 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3070 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3071 off / last state */
3074 static struct rfkill *tpacpi_wan_rfkill;
3076 static void wan_suspend(pm_message_t state)
3078 /* Try to make sure radio will resume powered off */
3079 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3080 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3083 static int wan_get_radiosw(void)
3085 int status;
3087 if (!tp_features.wan)
3088 return -ENODEV;
3090 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3091 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3092 return RFKILL_STATE_HARD_BLOCKED;
3094 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3095 if (dbg_wwanemul)
3096 return (tpacpi_wwan_emulstate) ?
3097 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3098 #endif
3100 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3101 return -EIO;
3103 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3104 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3107 static void wan_update_rfk(void)
3109 int status;
3111 if (!tpacpi_wan_rfkill)
3112 return;
3114 status = wan_get_radiosw();
3115 if (status < 0)
3116 return;
3117 rfkill_force_state(tpacpi_wan_rfkill, status);
3120 static int wan_set_radiosw(int radio_on, int update_rfk)
3122 int status;
3124 if (!tp_features.wan)
3125 return -ENODEV;
3127 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3128 * reason to risk weird behaviour. */
3129 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3130 && radio_on)
3131 return -EPERM;
3133 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3134 if (dbg_wwanemul) {
3135 tpacpi_wwan_emulstate = !!radio_on;
3136 if (update_rfk)
3137 wan_update_rfk();
3138 return 0;
3140 #endif
3142 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3143 if (radio_on)
3144 status = TP_ACPI_WANCARD_RADIOSSW;
3145 else
3146 status = 0;
3147 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3148 return -EIO;
3150 if (update_rfk)
3151 wan_update_rfk();
3153 return 0;
3156 /* sysfs wan enable ---------------------------------------------------- */
3157 static ssize_t wan_enable_show(struct device *dev,
3158 struct device_attribute *attr,
3159 char *buf)
3161 int status;
3163 status = wan_get_radiosw();
3164 if (status < 0)
3165 return status;
3167 return snprintf(buf, PAGE_SIZE, "%d\n",
3168 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3171 static ssize_t wan_enable_store(struct device *dev,
3172 struct device_attribute *attr,
3173 const char *buf, size_t count)
3175 unsigned long t;
3176 int res;
3178 if (parse_strtoul(buf, 1, &t))
3179 return -EINVAL;
3181 res = wan_set_radiosw(t, 1);
3183 return (res) ? res : count;
3186 static struct device_attribute dev_attr_wan_enable =
3187 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3188 wan_enable_show, wan_enable_store);
3190 /* --------------------------------------------------------------------- */
3192 static struct attribute *wan_attributes[] = {
3193 &dev_attr_wan_enable.attr,
3194 NULL
3197 static const struct attribute_group wan_attr_group = {
3198 .attrs = wan_attributes,
3201 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3203 int wans = wan_get_radiosw();
3205 if (wans < 0)
3206 return wans;
3208 *state = wans;
3209 return 0;
3212 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3214 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3217 static void wan_exit(void)
3219 if (tpacpi_wan_rfkill)
3220 rfkill_unregister(tpacpi_wan_rfkill);
3222 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3223 &wan_attr_group);
3226 static int __init wan_init(struct ibm_init_struct *iibm)
3228 int res;
3229 int status = 0;
3231 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3233 TPACPI_ACPIHANDLE_INIT(hkey);
3235 tp_features.wan = hkey_handle &&
3236 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3238 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3239 str_supported(tp_features.wan),
3240 status);
3242 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3243 if (dbg_wwanemul) {
3244 tp_features.wan = 1;
3245 printk(TPACPI_INFO
3246 "wwan switch emulation enabled\n");
3247 } else
3248 #endif
3249 if (tp_features.wan &&
3250 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3251 /* no wan hardware present in system */
3252 tp_features.wan = 0;
3253 dbg_printk(TPACPI_DBG_INIT,
3254 "wan hardware not installed\n");
3257 if (!tp_features.wan)
3258 return 1;
3260 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3261 &wan_attr_group);
3262 if (res)
3263 return res;
3265 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3266 &tpacpi_wan_rfkill,
3267 RFKILL_TYPE_WWAN,
3268 "tpacpi_wwan_sw",
3269 tpacpi_wan_rfk_set,
3270 tpacpi_wan_rfk_get);
3271 if (res) {
3272 wan_exit();
3273 return res;
3276 return 0;
3279 /* procfs -------------------------------------------------------------- */
3280 static int wan_read(char *p)
3282 int len = 0;
3283 int status = wan_get_radiosw();
3285 if (!tp_features.wan)
3286 len += sprintf(p + len, "status:\t\tnot supported\n");
3287 else {
3288 len += sprintf(p + len, "status:\t\t%s\n",
3289 (status == RFKILL_STATE_UNBLOCKED) ?
3290 "enabled" : "disabled");
3291 len += sprintf(p + len, "commands:\tenable, disable\n");
3294 return len;
3297 static int wan_write(char *buf)
3299 char *cmd;
3301 if (!tp_features.wan)
3302 return -ENODEV;
3304 while ((cmd = next_cmd(&buf))) {
3305 if (strlencmp(cmd, "enable") == 0) {
3306 wan_set_radiosw(1, 1);
3307 } else if (strlencmp(cmd, "disable") == 0) {
3308 wan_set_radiosw(0, 1);
3309 } else
3310 return -EINVAL;
3313 return 0;
3316 static struct ibm_struct wan_driver_data = {
3317 .name = "wan",
3318 .read = wan_read,
3319 .write = wan_write,
3320 .exit = wan_exit,
3321 .suspend = wan_suspend,
3324 /*************************************************************************
3325 * Video subdriver
3328 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3330 enum video_access_mode {
3331 TPACPI_VIDEO_NONE = 0,
3332 TPACPI_VIDEO_570, /* 570 */
3333 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3334 TPACPI_VIDEO_NEW, /* all others */
3337 enum { /* video status flags, based on VIDEO_570 */
3338 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3339 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3340 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3343 enum { /* TPACPI_VIDEO_570 constants */
3344 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3345 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3346 * video_status_flags */
3347 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3348 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3351 static enum video_access_mode video_supported;
3352 static int video_orig_autosw;
3354 static int video_autosw_get(void);
3355 static int video_autosw_set(int enable);
3357 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3359 static int __init video_init(struct ibm_init_struct *iibm)
3361 int ivga;
3363 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3365 TPACPI_ACPIHANDLE_INIT(vid);
3366 TPACPI_ACPIHANDLE_INIT(vid2);
3368 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3369 /* G41, assume IVGA doesn't change */
3370 vid_handle = vid2_handle;
3372 if (!vid_handle)
3373 /* video switching not supported on R30, R31 */
3374 video_supported = TPACPI_VIDEO_NONE;
3375 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3376 /* 570 */
3377 video_supported = TPACPI_VIDEO_570;
3378 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3379 /* 600e/x, 770e, 770x */
3380 video_supported = TPACPI_VIDEO_770;
3381 else
3382 /* all others */
3383 video_supported = TPACPI_VIDEO_NEW;
3385 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3386 str_supported(video_supported != TPACPI_VIDEO_NONE),
3387 video_supported);
3389 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3392 static void video_exit(void)
3394 dbg_printk(TPACPI_DBG_EXIT,
3395 "restoring original video autoswitch mode\n");
3396 if (video_autosw_set(video_orig_autosw))
3397 printk(TPACPI_ERR "error while trying to restore original "
3398 "video autoswitch mode\n");
3401 static int video_outputsw_get(void)
3403 int status = 0;
3404 int i;
3406 switch (video_supported) {
3407 case TPACPI_VIDEO_570:
3408 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3409 TP_ACPI_VIDEO_570_PHSCMD))
3410 return -EIO;
3411 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3412 break;
3413 case TPACPI_VIDEO_770:
3414 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3415 return -EIO;
3416 if (i)
3417 status |= TP_ACPI_VIDEO_S_LCD;
3418 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3419 return -EIO;
3420 if (i)
3421 status |= TP_ACPI_VIDEO_S_CRT;
3422 break;
3423 case TPACPI_VIDEO_NEW:
3424 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3425 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3426 return -EIO;
3427 if (i)
3428 status |= TP_ACPI_VIDEO_S_CRT;
3430 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3431 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3432 return -EIO;
3433 if (i)
3434 status |= TP_ACPI_VIDEO_S_LCD;
3435 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3436 return -EIO;
3437 if (i)
3438 status |= TP_ACPI_VIDEO_S_DVI;
3439 break;
3440 default:
3441 return -ENOSYS;
3444 return status;
3447 static int video_outputsw_set(int status)
3449 int autosw;
3450 int res = 0;
3452 switch (video_supported) {
3453 case TPACPI_VIDEO_570:
3454 res = acpi_evalf(NULL, NULL,
3455 "\\_SB.PHS2", "vdd",
3456 TP_ACPI_VIDEO_570_PHS2CMD,
3457 status | TP_ACPI_VIDEO_570_PHS2SET);
3458 break;
3459 case TPACPI_VIDEO_770:
3460 autosw = video_autosw_get();
3461 if (autosw < 0)
3462 return autosw;
3464 res = video_autosw_set(1);
3465 if (res)
3466 return res;
3467 res = acpi_evalf(vid_handle, NULL,
3468 "ASWT", "vdd", status * 0x100, 0);
3469 if (!autosw && video_autosw_set(autosw)) {
3470 printk(TPACPI_ERR
3471 "video auto-switch left enabled due to error\n");
3472 return -EIO;
3474 break;
3475 case TPACPI_VIDEO_NEW:
3476 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3477 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3478 break;
3479 default:
3480 return -ENOSYS;
3483 return (res)? 0 : -EIO;
3486 static int video_autosw_get(void)
3488 int autosw = 0;
3490 switch (video_supported) {
3491 case TPACPI_VIDEO_570:
3492 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3493 return -EIO;
3494 break;
3495 case TPACPI_VIDEO_770:
3496 case TPACPI_VIDEO_NEW:
3497 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3498 return -EIO;
3499 break;
3500 default:
3501 return -ENOSYS;
3504 return autosw & 1;
3507 static int video_autosw_set(int enable)
3509 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3510 return -EIO;
3511 return 0;
3514 static int video_outputsw_cycle(void)
3516 int autosw = video_autosw_get();
3517 int res;
3519 if (autosw < 0)
3520 return autosw;
3522 switch (video_supported) {
3523 case TPACPI_VIDEO_570:
3524 res = video_autosw_set(1);
3525 if (res)
3526 return res;
3527 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3528 break;
3529 case TPACPI_VIDEO_770:
3530 case TPACPI_VIDEO_NEW:
3531 res = video_autosw_set(1);
3532 if (res)
3533 return res;
3534 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3535 break;
3536 default:
3537 return -ENOSYS;
3539 if (!autosw && video_autosw_set(autosw)) {
3540 printk(TPACPI_ERR
3541 "video auto-switch left enabled due to error\n");
3542 return -EIO;
3545 return (res)? 0 : -EIO;
3548 static int video_expand_toggle(void)
3550 switch (video_supported) {
3551 case TPACPI_VIDEO_570:
3552 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3553 0 : -EIO;
3554 case TPACPI_VIDEO_770:
3555 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3556 0 : -EIO;
3557 case TPACPI_VIDEO_NEW:
3558 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3559 0 : -EIO;
3560 default:
3561 return -ENOSYS;
3563 /* not reached */
3566 static int video_read(char *p)
3568 int status, autosw;
3569 int len = 0;
3571 if (video_supported == TPACPI_VIDEO_NONE) {
3572 len += sprintf(p + len, "status:\t\tnot supported\n");
3573 return len;
3576 status = video_outputsw_get();
3577 if (status < 0)
3578 return status;
3580 autosw = video_autosw_get();
3581 if (autosw < 0)
3582 return autosw;
3584 len += sprintf(p + len, "status:\t\tsupported\n");
3585 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3586 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3587 if (video_supported == TPACPI_VIDEO_NEW)
3588 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3589 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3590 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3591 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3592 if (video_supported == TPACPI_VIDEO_NEW)
3593 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3594 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3595 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3597 return len;
3600 static int video_write(char *buf)
3602 char *cmd;
3603 int enable, disable, status;
3604 int res;
3606 if (video_supported == TPACPI_VIDEO_NONE)
3607 return -ENODEV;
3609 enable = 0;
3610 disable = 0;
3612 while ((cmd = next_cmd(&buf))) {
3613 if (strlencmp(cmd, "lcd_enable") == 0) {
3614 enable |= TP_ACPI_VIDEO_S_LCD;
3615 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3616 disable |= TP_ACPI_VIDEO_S_LCD;
3617 } else if (strlencmp(cmd, "crt_enable") == 0) {
3618 enable |= TP_ACPI_VIDEO_S_CRT;
3619 } else if (strlencmp(cmd, "crt_disable") == 0) {
3620 disable |= TP_ACPI_VIDEO_S_CRT;
3621 } else if (video_supported == TPACPI_VIDEO_NEW &&
3622 strlencmp(cmd, "dvi_enable") == 0) {
3623 enable |= TP_ACPI_VIDEO_S_DVI;
3624 } else if (video_supported == TPACPI_VIDEO_NEW &&
3625 strlencmp(cmd, "dvi_disable") == 0) {
3626 disable |= TP_ACPI_VIDEO_S_DVI;
3627 } else if (strlencmp(cmd, "auto_enable") == 0) {
3628 res = video_autosw_set(1);
3629 if (res)
3630 return res;
3631 } else if (strlencmp(cmd, "auto_disable") == 0) {
3632 res = video_autosw_set(0);
3633 if (res)
3634 return res;
3635 } else if (strlencmp(cmd, "video_switch") == 0) {
3636 res = video_outputsw_cycle();
3637 if (res)
3638 return res;
3639 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3640 res = video_expand_toggle();
3641 if (res)
3642 return res;
3643 } else
3644 return -EINVAL;
3647 if (enable || disable) {
3648 status = video_outputsw_get();
3649 if (status < 0)
3650 return status;
3651 res = video_outputsw_set((status & ~disable) | enable);
3652 if (res)
3653 return res;
3656 return 0;
3659 static struct ibm_struct video_driver_data = {
3660 .name = "video",
3661 .read = video_read,
3662 .write = video_write,
3663 .exit = video_exit,
3666 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3668 /*************************************************************************
3669 * Light (thinklight) subdriver
3672 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3673 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3675 static int light_get_status(void)
3677 int status = 0;
3679 if (tp_features.light_status) {
3680 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3681 return -EIO;
3682 return (!!status);
3685 return -ENXIO;
3688 static int light_set_status(int status)
3690 int rc;
3692 if (tp_features.light) {
3693 if (cmos_handle) {
3694 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3695 (status)?
3696 TP_CMOS_THINKLIGHT_ON :
3697 TP_CMOS_THINKLIGHT_OFF);
3698 } else {
3699 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3700 (status)? 1 : 0);
3702 return (rc)? 0 : -EIO;
3705 return -ENXIO;
3708 static void light_set_status_worker(struct work_struct *work)
3710 struct tpacpi_led_classdev *data =
3711 container_of(work, struct tpacpi_led_classdev, work);
3713 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3714 light_set_status((data->new_state != TPACPI_LED_OFF));
3717 static void light_sysfs_set(struct led_classdev *led_cdev,
3718 enum led_brightness brightness)
3720 struct tpacpi_led_classdev *data =
3721 container_of(led_cdev,
3722 struct tpacpi_led_classdev,
3723 led_classdev);
3724 data->new_state = (brightness != LED_OFF) ?
3725 TPACPI_LED_ON : TPACPI_LED_OFF;
3726 queue_work(tpacpi_wq, &data->work);
3729 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3731 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3734 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3735 .led_classdev = {
3736 .name = "tpacpi::thinklight",
3737 .brightness_set = &light_sysfs_set,
3738 .brightness_get = &light_sysfs_get,
3742 static int __init light_init(struct ibm_init_struct *iibm)
3744 int rc;
3746 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3748 TPACPI_ACPIHANDLE_INIT(ledb);
3749 TPACPI_ACPIHANDLE_INIT(lght);
3750 TPACPI_ACPIHANDLE_INIT(cmos);
3751 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3753 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3754 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3756 if (tp_features.light)
3757 /* light status not supported on
3758 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3759 tp_features.light_status =
3760 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3762 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3763 str_supported(tp_features.light),
3764 str_supported(tp_features.light_status));
3766 if (!tp_features.light)
3767 return 1;
3769 rc = led_classdev_register(&tpacpi_pdev->dev,
3770 &tpacpi_led_thinklight.led_classdev);
3772 if (rc < 0) {
3773 tp_features.light = 0;
3774 tp_features.light_status = 0;
3775 } else {
3776 rc = 0;
3779 return rc;
3782 static void light_exit(void)
3784 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3785 if (work_pending(&tpacpi_led_thinklight.work))
3786 flush_workqueue(tpacpi_wq);
3789 static int light_read(char *p)
3791 int len = 0;
3792 int status;
3794 if (!tp_features.light) {
3795 len += sprintf(p + len, "status:\t\tnot supported\n");
3796 } else if (!tp_features.light_status) {
3797 len += sprintf(p + len, "status:\t\tunknown\n");
3798 len += sprintf(p + len, "commands:\ton, off\n");
3799 } else {
3800 status = light_get_status();
3801 if (status < 0)
3802 return status;
3803 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3804 len += sprintf(p + len, "commands:\ton, off\n");
3807 return len;
3810 static int light_write(char *buf)
3812 char *cmd;
3813 int newstatus = 0;
3815 if (!tp_features.light)
3816 return -ENODEV;
3818 while ((cmd = next_cmd(&buf))) {
3819 if (strlencmp(cmd, "on") == 0) {
3820 newstatus = 1;
3821 } else if (strlencmp(cmd, "off") == 0) {
3822 newstatus = 0;
3823 } else
3824 return -EINVAL;
3827 return light_set_status(newstatus);
3830 static struct ibm_struct light_driver_data = {
3831 .name = "light",
3832 .read = light_read,
3833 .write = light_write,
3834 .exit = light_exit,
3837 /*************************************************************************
3838 * Dock subdriver
3841 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3843 static void dock_notify(struct ibm_struct *ibm, u32 event);
3844 static int dock_read(char *p);
3845 static int dock_write(char *buf);
3847 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3848 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3849 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3850 "\\_SB.PCI.ISA.SLCE", /* 570 */
3851 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3853 /* don't list other alternatives as we install a notify handler on the 570 */
3854 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3856 static const struct acpi_device_id ibm_pci_device_ids[] = {
3857 {PCI_ROOT_HID_STRING, 0},
3858 {"", 0},
3861 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3863 .notify = dock_notify,
3864 .handle = &dock_handle,
3865 .type = ACPI_SYSTEM_NOTIFY,
3868 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3869 * We just use it to get notifications of dock hotplug
3870 * in very old thinkpads */
3871 .hid = ibm_pci_device_ids,
3872 .notify = dock_notify,
3873 .handle = &pci_handle,
3874 .type = ACPI_SYSTEM_NOTIFY,
3878 static struct ibm_struct dock_driver_data[2] = {
3880 .name = "dock",
3881 .read = dock_read,
3882 .write = dock_write,
3883 .acpi = &ibm_dock_acpidriver[0],
3886 .name = "dock",
3887 .acpi = &ibm_dock_acpidriver[1],
3891 #define dock_docked() (_sta(dock_handle) & 1)
3893 static int __init dock_init(struct ibm_init_struct *iibm)
3895 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3897 TPACPI_ACPIHANDLE_INIT(dock);
3899 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3900 str_supported(dock_handle != NULL));
3902 return (dock_handle)? 0 : 1;
3905 static int __init dock_init2(struct ibm_init_struct *iibm)
3907 int dock2_needed;
3909 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3911 if (dock_driver_data[0].flags.acpi_driver_registered &&
3912 dock_driver_data[0].flags.acpi_notify_installed) {
3913 TPACPI_ACPIHANDLE_INIT(pci);
3914 dock2_needed = (pci_handle != NULL);
3915 vdbg_printk(TPACPI_DBG_INIT,
3916 "dock PCI handler for the TP 570 is %s\n",
3917 str_supported(dock2_needed));
3918 } else {
3919 vdbg_printk(TPACPI_DBG_INIT,
3920 "dock subdriver part 2 not required\n");
3921 dock2_needed = 0;
3924 return (dock2_needed)? 0 : 1;
3927 static void dock_notify(struct ibm_struct *ibm, u32 event)
3929 int docked = dock_docked();
3930 int pci = ibm->acpi->hid && ibm->acpi->device &&
3931 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3932 int data;
3934 if (event == 1 && !pci) /* 570 */
3935 data = 1; /* button */
3936 else if (event == 1 && pci) /* 570 */
3937 data = 3; /* dock */
3938 else if (event == 3 && docked)
3939 data = 1; /* button */
3940 else if (event == 3 && !docked)
3941 data = 2; /* undock */
3942 else if (event == 0 && docked)
3943 data = 3; /* dock */
3944 else {
3945 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3946 event, _sta(dock_handle));
3947 data = 0; /* unknown */
3949 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3950 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3951 dev_name(&ibm->acpi->device->dev),
3952 event, data);
3955 static int dock_read(char *p)
3957 int len = 0;
3958 int docked = dock_docked();
3960 if (!dock_handle)
3961 len += sprintf(p + len, "status:\t\tnot supported\n");
3962 else if (!docked)
3963 len += sprintf(p + len, "status:\t\tundocked\n");
3964 else {
3965 len += sprintf(p + len, "status:\t\tdocked\n");
3966 len += sprintf(p + len, "commands:\tdock, undock\n");
3969 return len;
3972 static int dock_write(char *buf)
3974 char *cmd;
3976 if (!dock_docked())
3977 return -ENODEV;
3979 while ((cmd = next_cmd(&buf))) {
3980 if (strlencmp(cmd, "undock") == 0) {
3981 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3982 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3983 return -EIO;
3984 } else if (strlencmp(cmd, "dock") == 0) {
3985 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3986 return -EIO;
3987 } else
3988 return -EINVAL;
3991 return 0;
3994 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3996 /*************************************************************************
3997 * Bay subdriver
4000 #ifdef CONFIG_THINKPAD_ACPI_BAY
4002 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4003 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4004 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4005 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4006 ); /* A21e, R30, R31 */
4007 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4008 "_EJ0", /* all others */
4009 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4010 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4011 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4012 ); /* all others */
4013 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4014 "_EJ0", /* 770x */
4015 ); /* all others */
4017 static int __init bay_init(struct ibm_init_struct *iibm)
4019 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4021 TPACPI_ACPIHANDLE_INIT(bay);
4022 if (bay_handle)
4023 TPACPI_ACPIHANDLE_INIT(bay_ej);
4024 TPACPI_ACPIHANDLE_INIT(bay2);
4025 if (bay2_handle)
4026 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4028 tp_features.bay_status = bay_handle &&
4029 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4030 tp_features.bay_status2 = bay2_handle &&
4031 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4033 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4034 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4035 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4036 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4038 vdbg_printk(TPACPI_DBG_INIT,
4039 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4040 str_supported(tp_features.bay_status),
4041 str_supported(tp_features.bay_eject),
4042 str_supported(tp_features.bay_status2),
4043 str_supported(tp_features.bay_eject2));
4045 return (tp_features.bay_status || tp_features.bay_eject ||
4046 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4049 static void bay_notify(struct ibm_struct *ibm, u32 event)
4051 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4052 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4053 dev_name(&ibm->acpi->device->dev),
4054 event, 0);
4057 #define bay_occupied(b) (_sta(b##_handle) & 1)
4059 static int bay_read(char *p)
4061 int len = 0;
4062 int occupied = bay_occupied(bay);
4063 int occupied2 = bay_occupied(bay2);
4064 int eject, eject2;
4066 len += sprintf(p + len, "status:\t\t%s\n",
4067 tp_features.bay_status ?
4068 (occupied ? "occupied" : "unoccupied") :
4069 "not supported");
4070 if (tp_features.bay_status2)
4071 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4072 "occupied" : "unoccupied");
4074 eject = tp_features.bay_eject && occupied;
4075 eject2 = tp_features.bay_eject2 && occupied2;
4077 if (eject && eject2)
4078 len += sprintf(p + len, "commands:\teject, eject2\n");
4079 else if (eject)
4080 len += sprintf(p + len, "commands:\teject\n");
4081 else if (eject2)
4082 len += sprintf(p + len, "commands:\teject2\n");
4084 return len;
4087 static int bay_write(char *buf)
4089 char *cmd;
4091 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4092 return -ENODEV;
4094 while ((cmd = next_cmd(&buf))) {
4095 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4096 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4097 return -EIO;
4098 } else if (tp_features.bay_eject2 &&
4099 strlencmp(cmd, "eject2") == 0) {
4100 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4101 return -EIO;
4102 } else
4103 return -EINVAL;
4106 return 0;
4109 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4110 .notify = bay_notify,
4111 .handle = &bay_handle,
4112 .type = ACPI_SYSTEM_NOTIFY,
4115 static struct ibm_struct bay_driver_data = {
4116 .name = "bay",
4117 .read = bay_read,
4118 .write = bay_write,
4119 .acpi = &ibm_bay_acpidriver,
4122 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4124 /*************************************************************************
4125 * CMOS subdriver
4128 /* sysfs cmos_command -------------------------------------------------- */
4129 static ssize_t cmos_command_store(struct device *dev,
4130 struct device_attribute *attr,
4131 const char *buf, size_t count)
4133 unsigned long cmos_cmd;
4134 int res;
4136 if (parse_strtoul(buf, 21, &cmos_cmd))
4137 return -EINVAL;
4139 res = issue_thinkpad_cmos_command(cmos_cmd);
4140 return (res)? res : count;
4143 static struct device_attribute dev_attr_cmos_command =
4144 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4146 /* --------------------------------------------------------------------- */
4148 static int __init cmos_init(struct ibm_init_struct *iibm)
4150 int res;
4152 vdbg_printk(TPACPI_DBG_INIT,
4153 "initializing cmos commands subdriver\n");
4155 TPACPI_ACPIHANDLE_INIT(cmos);
4157 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4158 str_supported(cmos_handle != NULL));
4160 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4161 if (res)
4162 return res;
4164 return (cmos_handle)? 0 : 1;
4167 static void cmos_exit(void)
4169 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4172 static int cmos_read(char *p)
4174 int len = 0;
4176 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4177 R30, R31, T20-22, X20-21 */
4178 if (!cmos_handle)
4179 len += sprintf(p + len, "status:\t\tnot supported\n");
4180 else {
4181 len += sprintf(p + len, "status:\t\tsupported\n");
4182 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4185 return len;
4188 static int cmos_write(char *buf)
4190 char *cmd;
4191 int cmos_cmd, res;
4193 while ((cmd = next_cmd(&buf))) {
4194 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4195 cmos_cmd >= 0 && cmos_cmd <= 21) {
4196 /* cmos_cmd set */
4197 } else
4198 return -EINVAL;
4200 res = issue_thinkpad_cmos_command(cmos_cmd);
4201 if (res)
4202 return res;
4205 return 0;
4208 static struct ibm_struct cmos_driver_data = {
4209 .name = "cmos",
4210 .read = cmos_read,
4211 .write = cmos_write,
4212 .exit = cmos_exit,
4215 /*************************************************************************
4216 * LED subdriver
4219 enum led_access_mode {
4220 TPACPI_LED_NONE = 0,
4221 TPACPI_LED_570, /* 570 */
4222 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4223 TPACPI_LED_NEW, /* all others */
4226 enum { /* For TPACPI_LED_OLD */
4227 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4228 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4229 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4232 static enum led_access_mode led_supported;
4234 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4235 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4236 /* T20-22, X20-21 */
4237 "LED", /* all others */
4238 ); /* R30, R31 */
4240 #define TPACPI_LED_NUMLEDS 8
4241 static struct tpacpi_led_classdev *tpacpi_leds;
4242 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4243 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4244 /* there's a limit of 19 chars + NULL before 2.6.26 */
4245 "tpacpi::power",
4246 "tpacpi:orange:batt",
4247 "tpacpi:green:batt",
4248 "tpacpi::dock_active",
4249 "tpacpi::bay_active",
4250 "tpacpi::dock_batt",
4251 "tpacpi::unknown_led",
4252 "tpacpi::standby",
4255 static int led_get_status(const unsigned int led)
4257 int status;
4258 enum led_status_t led_s;
4260 switch (led_supported) {
4261 case TPACPI_LED_570:
4262 if (!acpi_evalf(ec_handle,
4263 &status, "GLED", "dd", 1 << led))
4264 return -EIO;
4265 led_s = (status == 0)?
4266 TPACPI_LED_OFF :
4267 ((status == 1)?
4268 TPACPI_LED_ON :
4269 TPACPI_LED_BLINK);
4270 tpacpi_led_state_cache[led] = led_s;
4271 return led_s;
4272 default:
4273 return -ENXIO;
4276 /* not reached */
4279 static int led_set_status(const unsigned int led,
4280 const enum led_status_t ledstatus)
4282 /* off, on, blink. Index is led_status_t */
4283 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4284 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4286 int rc = 0;
4288 switch (led_supported) {
4289 case TPACPI_LED_570:
4290 /* 570 */
4291 if (led > 7)
4292 return -EINVAL;
4293 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4294 (1 << led), led_sled_arg1[ledstatus]))
4295 rc = -EIO;
4296 break;
4297 case TPACPI_LED_OLD:
4298 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4299 if (led > 7)
4300 return -EINVAL;
4301 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4302 if (rc >= 0)
4303 rc = ec_write(TPACPI_LED_EC_HLBL,
4304 (ledstatus == TPACPI_LED_BLINK) << led);
4305 if (rc >= 0)
4306 rc = ec_write(TPACPI_LED_EC_HLCL,
4307 (ledstatus != TPACPI_LED_OFF) << led);
4308 break;
4309 case TPACPI_LED_NEW:
4310 /* all others */
4311 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4312 led, led_led_arg1[ledstatus]))
4313 rc = -EIO;
4314 break;
4315 default:
4316 rc = -ENXIO;
4319 if (!rc)
4320 tpacpi_led_state_cache[led] = ledstatus;
4322 return rc;
4325 static void led_set_status_worker(struct work_struct *work)
4327 struct tpacpi_led_classdev *data =
4328 container_of(work, struct tpacpi_led_classdev, work);
4330 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4331 led_set_status(data->led, data->new_state);
4334 static void led_sysfs_set(struct led_classdev *led_cdev,
4335 enum led_brightness brightness)
4337 struct tpacpi_led_classdev *data = container_of(led_cdev,
4338 struct tpacpi_led_classdev, led_classdev);
4340 if (brightness == LED_OFF)
4341 data->new_state = TPACPI_LED_OFF;
4342 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4343 data->new_state = TPACPI_LED_ON;
4344 else
4345 data->new_state = TPACPI_LED_BLINK;
4347 queue_work(tpacpi_wq, &data->work);
4350 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4351 unsigned long *delay_on, unsigned long *delay_off)
4353 struct tpacpi_led_classdev *data = container_of(led_cdev,
4354 struct tpacpi_led_classdev, led_classdev);
4356 /* Can we choose the flash rate? */
4357 if (*delay_on == 0 && *delay_off == 0) {
4358 /* yes. set them to the hardware blink rate (1 Hz) */
4359 *delay_on = 500; /* ms */
4360 *delay_off = 500; /* ms */
4361 } else if ((*delay_on != 500) || (*delay_off != 500))
4362 return -EINVAL;
4364 data->new_state = TPACPI_LED_BLINK;
4365 queue_work(tpacpi_wq, &data->work);
4367 return 0;
4370 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4372 int rc;
4374 struct tpacpi_led_classdev *data = container_of(led_cdev,
4375 struct tpacpi_led_classdev, led_classdev);
4377 rc = led_get_status(data->led);
4379 if (rc == TPACPI_LED_OFF || rc < 0)
4380 rc = LED_OFF; /* no error handling in led class :( */
4381 else
4382 rc = LED_FULL;
4384 return rc;
4387 static void led_exit(void)
4389 unsigned int i;
4391 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4392 if (tpacpi_leds[i].led_classdev.name)
4393 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4396 kfree(tpacpi_leds);
4399 static int __init led_init(struct ibm_init_struct *iibm)
4401 unsigned int i;
4402 int rc;
4404 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4406 TPACPI_ACPIHANDLE_INIT(led);
4408 if (!led_handle)
4409 /* led not supported on R30, R31 */
4410 led_supported = TPACPI_LED_NONE;
4411 else if (strlencmp(led_path, "SLED") == 0)
4412 /* 570 */
4413 led_supported = TPACPI_LED_570;
4414 else if (strlencmp(led_path, "SYSL") == 0)
4415 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4416 led_supported = TPACPI_LED_OLD;
4417 else
4418 /* all others */
4419 led_supported = TPACPI_LED_NEW;
4421 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4422 str_supported(led_supported), led_supported);
4424 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4425 GFP_KERNEL);
4426 if (!tpacpi_leds) {
4427 printk(TPACPI_ERR "Out of memory for LED data\n");
4428 return -ENOMEM;
4431 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4432 tpacpi_leds[i].led = i;
4434 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4435 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4436 if (led_supported == TPACPI_LED_570)
4437 tpacpi_leds[i].led_classdev.brightness_get =
4438 &led_sysfs_get;
4440 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4442 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4444 rc = led_classdev_register(&tpacpi_pdev->dev,
4445 &tpacpi_leds[i].led_classdev);
4446 if (rc < 0) {
4447 tpacpi_leds[i].led_classdev.name = NULL;
4448 led_exit();
4449 return rc;
4453 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4456 #define str_led_status(s) \
4457 ((s) == TPACPI_LED_OFF ? "off" : \
4458 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4460 static int led_read(char *p)
4462 int len = 0;
4464 if (!led_supported) {
4465 len += sprintf(p + len, "status:\t\tnot supported\n");
4466 return len;
4468 len += sprintf(p + len, "status:\t\tsupported\n");
4470 if (led_supported == TPACPI_LED_570) {
4471 /* 570 */
4472 int i, status;
4473 for (i = 0; i < 8; i++) {
4474 status = led_get_status(i);
4475 if (status < 0)
4476 return -EIO;
4477 len += sprintf(p + len, "%d:\t\t%s\n",
4478 i, str_led_status(status));
4482 len += sprintf(p + len, "commands:\t"
4483 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4485 return len;
4488 static int led_write(char *buf)
4490 char *cmd;
4491 int led, rc;
4492 enum led_status_t s;
4494 if (!led_supported)
4495 return -ENODEV;
4497 while ((cmd = next_cmd(&buf))) {
4498 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4499 return -EINVAL;
4501 if (strstr(cmd, "off")) {
4502 s = TPACPI_LED_OFF;
4503 } else if (strstr(cmd, "on")) {
4504 s = TPACPI_LED_ON;
4505 } else if (strstr(cmd, "blink")) {
4506 s = TPACPI_LED_BLINK;
4507 } else {
4508 return -EINVAL;
4511 rc = led_set_status(led, s);
4512 if (rc < 0)
4513 return rc;
4516 return 0;
4519 static struct ibm_struct led_driver_data = {
4520 .name = "led",
4521 .read = led_read,
4522 .write = led_write,
4523 .exit = led_exit,
4526 /*************************************************************************
4527 * Beep subdriver
4530 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4532 static int __init beep_init(struct ibm_init_struct *iibm)
4534 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4536 TPACPI_ACPIHANDLE_INIT(beep);
4538 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4539 str_supported(beep_handle != NULL));
4541 return (beep_handle)? 0 : 1;
4544 static int beep_read(char *p)
4546 int len = 0;
4548 if (!beep_handle)
4549 len += sprintf(p + len, "status:\t\tnot supported\n");
4550 else {
4551 len += sprintf(p + len, "status:\t\tsupported\n");
4552 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4555 return len;
4558 static int beep_write(char *buf)
4560 char *cmd;
4561 int beep_cmd;
4563 if (!beep_handle)
4564 return -ENODEV;
4566 while ((cmd = next_cmd(&buf))) {
4567 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4568 beep_cmd >= 0 && beep_cmd <= 17) {
4569 /* beep_cmd set */
4570 } else
4571 return -EINVAL;
4572 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4573 return -EIO;
4576 return 0;
4579 static struct ibm_struct beep_driver_data = {
4580 .name = "beep",
4581 .read = beep_read,
4582 .write = beep_write,
4585 /*************************************************************************
4586 * Thermal subdriver
4589 enum thermal_access_mode {
4590 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4591 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4592 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4593 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4594 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4597 enum { /* TPACPI_THERMAL_TPEC_* */
4598 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4599 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4600 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4603 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4604 struct ibm_thermal_sensors_struct {
4605 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4608 static enum thermal_access_mode thermal_read_mode;
4610 /* idx is zero-based */
4611 static int thermal_get_sensor(int idx, s32 *value)
4613 int t;
4614 s8 tmp;
4615 char tmpi[5];
4617 t = TP_EC_THERMAL_TMP0;
4619 switch (thermal_read_mode) {
4620 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4621 case TPACPI_THERMAL_TPEC_16:
4622 if (idx >= 8 && idx <= 15) {
4623 t = TP_EC_THERMAL_TMP8;
4624 idx -= 8;
4626 /* fallthrough */
4627 #endif
4628 case TPACPI_THERMAL_TPEC_8:
4629 if (idx <= 7) {
4630 if (!acpi_ec_read(t + idx, &tmp))
4631 return -EIO;
4632 *value = tmp * 1000;
4633 return 0;
4635 break;
4637 case TPACPI_THERMAL_ACPI_UPDT:
4638 if (idx <= 7) {
4639 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4640 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4641 return -EIO;
4642 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4643 return -EIO;
4644 *value = (t - 2732) * 100;
4645 return 0;
4647 break;
4649 case TPACPI_THERMAL_ACPI_TMP07:
4650 if (idx <= 7) {
4651 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4652 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4653 return -EIO;
4654 if (t > 127 || t < -127)
4655 t = TP_EC_THERMAL_TMP_NA;
4656 *value = t * 1000;
4657 return 0;
4659 break;
4661 case TPACPI_THERMAL_NONE:
4662 default:
4663 return -ENOSYS;
4666 return -EINVAL;
4669 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4671 int res, i;
4672 int n;
4674 n = 8;
4675 i = 0;
4677 if (!s)
4678 return -EINVAL;
4680 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4681 n = 16;
4683 for (i = 0 ; i < n; i++) {
4684 res = thermal_get_sensor(i, &s->temp[i]);
4685 if (res)
4686 return res;
4689 return n;
4692 /* sysfs temp##_input -------------------------------------------------- */
4694 static ssize_t thermal_temp_input_show(struct device *dev,
4695 struct device_attribute *attr,
4696 char *buf)
4698 struct sensor_device_attribute *sensor_attr =
4699 to_sensor_dev_attr(attr);
4700 int idx = sensor_attr->index;
4701 s32 value;
4702 int res;
4704 res = thermal_get_sensor(idx, &value);
4705 if (res)
4706 return res;
4707 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4708 return -ENXIO;
4710 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4713 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4714 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4715 thermal_temp_input_show, NULL, _idxB)
4717 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4718 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4719 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4720 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4721 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4722 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4723 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4724 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4725 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4726 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4727 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4728 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4729 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4730 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4731 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4732 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4733 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4736 #define THERMAL_ATTRS(X) \
4737 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4739 static struct attribute *thermal_temp_input_attr[] = {
4740 THERMAL_ATTRS(8),
4741 THERMAL_ATTRS(9),
4742 THERMAL_ATTRS(10),
4743 THERMAL_ATTRS(11),
4744 THERMAL_ATTRS(12),
4745 THERMAL_ATTRS(13),
4746 THERMAL_ATTRS(14),
4747 THERMAL_ATTRS(15),
4748 THERMAL_ATTRS(0),
4749 THERMAL_ATTRS(1),
4750 THERMAL_ATTRS(2),
4751 THERMAL_ATTRS(3),
4752 THERMAL_ATTRS(4),
4753 THERMAL_ATTRS(5),
4754 THERMAL_ATTRS(6),
4755 THERMAL_ATTRS(7),
4756 NULL
4759 static const struct attribute_group thermal_temp_input16_group = {
4760 .attrs = thermal_temp_input_attr
4763 static const struct attribute_group thermal_temp_input8_group = {
4764 .attrs = &thermal_temp_input_attr[8]
4767 #undef THERMAL_SENSOR_ATTR_TEMP
4768 #undef THERMAL_ATTRS
4770 /* --------------------------------------------------------------------- */
4772 static int __init thermal_init(struct ibm_init_struct *iibm)
4774 u8 t, ta1, ta2;
4775 int i;
4776 int acpi_tmp7;
4777 int res;
4779 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4781 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4783 if (thinkpad_id.ec_model) {
4785 * Direct EC access mode: sensors at registers
4786 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4787 * non-implemented, thermal sensors return 0x80 when
4788 * not available
4791 ta1 = ta2 = 0;
4792 for (i = 0; i < 8; i++) {
4793 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4794 ta1 |= t;
4795 } else {
4796 ta1 = 0;
4797 break;
4799 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4800 ta2 |= t;
4801 } else {
4802 ta1 = 0;
4803 break;
4806 if (ta1 == 0) {
4807 /* This is sheer paranoia, but we handle it anyway */
4808 if (acpi_tmp7) {
4809 printk(TPACPI_ERR
4810 "ThinkPad ACPI EC access misbehaving, "
4811 "falling back to ACPI TMPx access "
4812 "mode\n");
4813 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4814 } else {
4815 printk(TPACPI_ERR
4816 "ThinkPad ACPI EC access misbehaving, "
4817 "disabling thermal sensors access\n");
4818 thermal_read_mode = TPACPI_THERMAL_NONE;
4820 } else {
4821 thermal_read_mode =
4822 (ta2 != 0) ?
4823 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4825 } else if (acpi_tmp7) {
4826 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4827 /* 600e/x, 770e, 770x */
4828 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4829 } else {
4830 /* Standard ACPI TMPx access, max 8 sensors */
4831 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4833 } else {
4834 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4835 thermal_read_mode = TPACPI_THERMAL_NONE;
4838 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4839 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4840 thermal_read_mode);
4842 switch (thermal_read_mode) {
4843 case TPACPI_THERMAL_TPEC_16:
4844 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4845 &thermal_temp_input16_group);
4846 if (res)
4847 return res;
4848 break;
4849 case TPACPI_THERMAL_TPEC_8:
4850 case TPACPI_THERMAL_ACPI_TMP07:
4851 case TPACPI_THERMAL_ACPI_UPDT:
4852 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4853 &thermal_temp_input8_group);
4854 if (res)
4855 return res;
4856 break;
4857 case TPACPI_THERMAL_NONE:
4858 default:
4859 return 1;
4862 return 0;
4865 static void thermal_exit(void)
4867 switch (thermal_read_mode) {
4868 case TPACPI_THERMAL_TPEC_16:
4869 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4870 &thermal_temp_input16_group);
4871 break;
4872 case TPACPI_THERMAL_TPEC_8:
4873 case TPACPI_THERMAL_ACPI_TMP07:
4874 case TPACPI_THERMAL_ACPI_UPDT:
4875 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4876 &thermal_temp_input16_group);
4877 break;
4878 case TPACPI_THERMAL_NONE:
4879 default:
4880 break;
4884 static int thermal_read(char *p)
4886 int len = 0;
4887 int n, i;
4888 struct ibm_thermal_sensors_struct t;
4890 n = thermal_get_sensors(&t);
4891 if (unlikely(n < 0))
4892 return n;
4894 len += sprintf(p + len, "temperatures:\t");
4896 if (n > 0) {
4897 for (i = 0; i < (n - 1); i++)
4898 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4899 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4900 } else
4901 len += sprintf(p + len, "not supported\n");
4903 return len;
4906 static struct ibm_struct thermal_driver_data = {
4907 .name = "thermal",
4908 .read = thermal_read,
4909 .exit = thermal_exit,
4912 /*************************************************************************
4913 * EC Dump subdriver
4916 static u8 ecdump_regs[256];
4918 static int ecdump_read(char *p)
4920 int len = 0;
4921 int i, j;
4922 u8 v;
4924 len += sprintf(p + len, "EC "
4925 " +00 +01 +02 +03 +04 +05 +06 +07"
4926 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4927 for (i = 0; i < 256; i += 16) {
4928 len += sprintf(p + len, "EC 0x%02x:", i);
4929 for (j = 0; j < 16; j++) {
4930 if (!acpi_ec_read(i + j, &v))
4931 break;
4932 if (v != ecdump_regs[i + j])
4933 len += sprintf(p + len, " *%02x", v);
4934 else
4935 len += sprintf(p + len, " %02x", v);
4936 ecdump_regs[i + j] = v;
4938 len += sprintf(p + len, "\n");
4939 if (j != 16)
4940 break;
4943 /* These are way too dangerous to advertise openly... */
4944 #if 0
4945 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4946 " (<offset> is 00-ff, <value> is 00-ff)\n");
4947 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4948 " (<offset> is 00-ff, <value> is 0-255)\n");
4949 #endif
4950 return len;
4953 static int ecdump_write(char *buf)
4955 char *cmd;
4956 int i, v;
4958 while ((cmd = next_cmd(&buf))) {
4959 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4960 /* i and v set */
4961 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4962 /* i and v set */
4963 } else
4964 return -EINVAL;
4965 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4966 if (!acpi_ec_write(i, v))
4967 return -EIO;
4968 } else
4969 return -EINVAL;
4972 return 0;
4975 static struct ibm_struct ecdump_driver_data = {
4976 .name = "ecdump",
4977 .read = ecdump_read,
4978 .write = ecdump_write,
4979 .flags.experimental = 1,
4982 /*************************************************************************
4983 * Backlight/brightness subdriver
4986 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4988 enum {
4989 TP_EC_BACKLIGHT = 0x31,
4991 /* TP_EC_BACKLIGHT bitmasks */
4992 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4993 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4994 TP_EC_BACKLIGHT_MAPSW = 0x20,
4997 static struct backlight_device *ibm_backlight_device;
4998 static int brightness_mode;
4999 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5001 static struct mutex brightness_mutex;
5004 * ThinkPads can read brightness from two places: EC 0x31, or
5005 * CMOS NVRAM byte 0x5E, bits 0-3.
5007 * EC 0x31 has the following layout
5008 * Bit 7: unknown function
5009 * Bit 6: unknown function
5010 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5011 * Bit 4: must be set to zero to avoid problems
5012 * Bit 3-0: backlight brightness level
5014 * brightness_get_raw returns status data in the EC 0x31 layout
5016 static int brightness_get_raw(int *status)
5018 u8 lec = 0, lcmos = 0, level = 0;
5020 if (brightness_mode & 1) {
5021 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5022 return -EIO;
5023 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5025 if (brightness_mode & 2) {
5026 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5027 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5028 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5029 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5030 level = lcmos;
5033 if (brightness_mode == 3) {
5034 *status = lec; /* Prefer EC, CMOS is just a backing store */
5035 lec &= TP_EC_BACKLIGHT_LVLMSK;
5036 if (lec == lcmos)
5037 tp_warned.bright_cmos_ec_unsync = 0;
5038 else {
5039 if (!tp_warned.bright_cmos_ec_unsync) {
5040 printk(TPACPI_ERR
5041 "CMOS NVRAM (%u) and EC (%u) do not "
5042 "agree on display brightness level\n",
5043 (unsigned int) lcmos,
5044 (unsigned int) lec);
5045 tp_warned.bright_cmos_ec_unsync = 1;
5047 return -EIO;
5049 } else {
5050 *status = level;
5053 return 0;
5056 /* May return EINTR which can always be mapped to ERESTARTSYS */
5057 static int brightness_set(int value)
5059 int cmos_cmd, inc, i, res;
5060 int current_value;
5061 int command_bits;
5063 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5064 value < 0)
5065 return -EINVAL;
5067 res = mutex_lock_interruptible(&brightness_mutex);
5068 if (res < 0)
5069 return res;
5071 res = brightness_get_raw(&current_value);
5072 if (res < 0)
5073 goto errout;
5075 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5076 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5078 cmos_cmd = value > current_value ?
5079 TP_CMOS_BRIGHTNESS_UP :
5080 TP_CMOS_BRIGHTNESS_DOWN;
5081 inc = (value > current_value)? 1 : -1;
5083 res = 0;
5084 for (i = current_value; i != value; i += inc) {
5085 if ((brightness_mode & 2) &&
5086 issue_thinkpad_cmos_command(cmos_cmd)) {
5087 res = -EIO;
5088 goto errout;
5090 if ((brightness_mode & 1) &&
5091 !acpi_ec_write(TP_EC_BACKLIGHT,
5092 (i + inc) | command_bits)) {
5093 res = -EIO;
5094 goto errout;;
5098 errout:
5099 mutex_unlock(&brightness_mutex);
5100 return res;
5103 /* sysfs backlight class ----------------------------------------------- */
5105 static int brightness_update_status(struct backlight_device *bd)
5107 /* it is the backlight class's job (caller) to handle
5108 * EINTR and other errors properly */
5109 return brightness_set(
5110 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5111 bd->props.power == FB_BLANK_UNBLANK) ?
5112 bd->props.brightness : 0);
5115 static int brightness_get(struct backlight_device *bd)
5117 int status, res;
5119 res = brightness_get_raw(&status);
5120 if (res < 0)
5121 return 0; /* FIXME: teach backlight about error handling */
5123 return status & TP_EC_BACKLIGHT_LVLMSK;
5126 static struct backlight_ops ibm_backlight_data = {
5127 .get_brightness = brightness_get,
5128 .update_status = brightness_update_status,
5131 /* --------------------------------------------------------------------- */
5133 static int __init brightness_init(struct ibm_init_struct *iibm)
5135 int b;
5137 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5139 mutex_init(&brightness_mutex);
5142 * We always attempt to detect acpi support, so as to switch
5143 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5144 * going to publish a backlight interface
5146 b = tpacpi_check_std_acpi_brightness_support();
5147 if (b > 0) {
5149 if (acpi_video_backlight_support()) {
5150 if (brightness_enable > 1) {
5151 printk(TPACPI_NOTICE
5152 "Standard ACPI backlight interface "
5153 "available, not loading native one.\n");
5154 return 1;
5155 } else if (brightness_enable == 1) {
5156 printk(TPACPI_NOTICE
5157 "Backlight control force enabled, even if standard "
5158 "ACPI backlight interface is available\n");
5160 } else {
5161 if (brightness_enable > 1) {
5162 printk(TPACPI_NOTICE
5163 "Standard ACPI backlight interface not "
5164 "available, thinkpad_acpi native "
5165 "brightness control enabled\n");
5170 if (!brightness_enable) {
5171 dbg_printk(TPACPI_DBG_INIT,
5172 "brightness support disabled by "
5173 "module parameter\n");
5174 return 1;
5177 if (b > 16) {
5178 printk(TPACPI_ERR
5179 "Unsupported brightness interface, "
5180 "please contact %s\n", TPACPI_MAIL);
5181 return 1;
5183 if (b == 16)
5184 tp_features.bright_16levels = 1;
5186 if (!brightness_mode) {
5187 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5188 brightness_mode = 2;
5189 else
5190 brightness_mode = 3;
5192 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5193 brightness_mode);
5196 if (brightness_mode > 3)
5197 return -EINVAL;
5199 if (brightness_get_raw(&b) < 0)
5200 return 1;
5202 if (tp_features.bright_16levels)
5203 printk(TPACPI_INFO
5204 "detected a 16-level brightness capable ThinkPad\n");
5206 ibm_backlight_device = backlight_device_register(
5207 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5208 &ibm_backlight_data);
5209 if (IS_ERR(ibm_backlight_device)) {
5210 printk(TPACPI_ERR "Could not register backlight device\n");
5211 return PTR_ERR(ibm_backlight_device);
5213 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5215 ibm_backlight_device->props.max_brightness =
5216 (tp_features.bright_16levels)? 15 : 7;
5217 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5218 backlight_update_status(ibm_backlight_device);
5220 return 0;
5223 static void brightness_exit(void)
5225 if (ibm_backlight_device) {
5226 vdbg_printk(TPACPI_DBG_EXIT,
5227 "calling backlight_device_unregister()\n");
5228 backlight_device_unregister(ibm_backlight_device);
5232 static int brightness_read(char *p)
5234 int len = 0;
5235 int level;
5237 level = brightness_get(NULL);
5238 if (level < 0) {
5239 len += sprintf(p + len, "level:\t\tunreadable\n");
5240 } else {
5241 len += sprintf(p + len, "level:\t\t%d\n", level);
5242 len += sprintf(p + len, "commands:\tup, down\n");
5243 len += sprintf(p + len, "commands:\tlevel <level>"
5244 " (<level> is 0-%d)\n",
5245 (tp_features.bright_16levels) ? 15 : 7);
5248 return len;
5251 static int brightness_write(char *buf)
5253 int level;
5254 int rc;
5255 char *cmd;
5256 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5258 level = brightness_get(NULL);
5259 if (level < 0)
5260 return level;
5262 while ((cmd = next_cmd(&buf))) {
5263 if (strlencmp(cmd, "up") == 0) {
5264 if (level < max_level)
5265 level++;
5266 } else if (strlencmp(cmd, "down") == 0) {
5267 if (level > 0)
5268 level--;
5269 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5270 level >= 0 && level <= max_level) {
5271 /* new level set */
5272 } else
5273 return -EINVAL;
5277 * Now we know what the final level should be, so we try to set it.
5278 * Doing it this way makes the syscall restartable in case of EINTR
5280 rc = brightness_set(level);
5281 return (rc == -EINTR)? ERESTARTSYS : rc;
5284 static struct ibm_struct brightness_driver_data = {
5285 .name = "brightness",
5286 .read = brightness_read,
5287 .write = brightness_write,
5288 .exit = brightness_exit,
5291 /*************************************************************************
5292 * Volume subdriver
5295 static int volume_offset = 0x30;
5297 static int volume_read(char *p)
5299 int len = 0;
5300 u8 level;
5302 if (!acpi_ec_read(volume_offset, &level)) {
5303 len += sprintf(p + len, "level:\t\tunreadable\n");
5304 } else {
5305 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5306 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5307 len += sprintf(p + len, "commands:\tup, down, mute\n");
5308 len += sprintf(p + len, "commands:\tlevel <level>"
5309 " (<level> is 0-15)\n");
5312 return len;
5315 static int volume_write(char *buf)
5317 int cmos_cmd, inc, i;
5318 u8 level, mute;
5319 int new_level, new_mute;
5320 char *cmd;
5322 while ((cmd = next_cmd(&buf))) {
5323 if (!acpi_ec_read(volume_offset, &level))
5324 return -EIO;
5325 new_mute = mute = level & 0x40;
5326 new_level = level = level & 0xf;
5328 if (strlencmp(cmd, "up") == 0) {
5329 if (mute)
5330 new_mute = 0;
5331 else
5332 new_level = level == 15 ? 15 : level + 1;
5333 } else if (strlencmp(cmd, "down") == 0) {
5334 if (mute)
5335 new_mute = 0;
5336 else
5337 new_level = level == 0 ? 0 : level - 1;
5338 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5339 new_level >= 0 && new_level <= 15) {
5340 /* new_level set */
5341 } else if (strlencmp(cmd, "mute") == 0) {
5342 new_mute = 0x40;
5343 } else
5344 return -EINVAL;
5346 if (new_level != level) {
5347 /* mute doesn't change */
5349 cmos_cmd = (new_level > level) ?
5350 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5351 inc = new_level > level ? 1 : -1;
5353 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5354 !acpi_ec_write(volume_offset, level)))
5355 return -EIO;
5357 for (i = level; i != new_level; i += inc)
5358 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5359 !acpi_ec_write(volume_offset, i + inc))
5360 return -EIO;
5362 if (mute &&
5363 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5364 !acpi_ec_write(volume_offset, new_level + mute))) {
5365 return -EIO;
5369 if (new_mute != mute) {
5370 /* level doesn't change */
5372 cmos_cmd = (new_mute) ?
5373 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5375 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5376 !acpi_ec_write(volume_offset, level + new_mute))
5377 return -EIO;
5381 return 0;
5384 static struct ibm_struct volume_driver_data = {
5385 .name = "volume",
5386 .read = volume_read,
5387 .write = volume_write,
5390 /*************************************************************************
5391 * Fan subdriver
5395 * FAN ACCESS MODES
5397 * TPACPI_FAN_RD_ACPI_GFAN:
5398 * ACPI GFAN method: returns fan level
5400 * see TPACPI_FAN_WR_ACPI_SFAN
5401 * EC 0x2f (HFSP) not available if GFAN exists
5403 * TPACPI_FAN_WR_ACPI_SFAN:
5404 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5406 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5407 * it for writing.
5409 * TPACPI_FAN_WR_TPEC:
5410 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5411 * Supported on almost all ThinkPads
5413 * Fan speed changes of any sort (including those caused by the
5414 * disengaged mode) are usually done slowly by the firmware as the
5415 * maximum ammount of fan duty cycle change per second seems to be
5416 * limited.
5418 * Reading is not available if GFAN exists.
5419 * Writing is not available if SFAN exists.
5421 * Bits
5422 * 7 automatic mode engaged;
5423 * (default operation mode of the ThinkPad)
5424 * fan level is ignored in this mode.
5425 * 6 full speed mode (takes precedence over bit 7);
5426 * not available on all thinkpads. May disable
5427 * the tachometer while the fan controller ramps up
5428 * the speed (which can take up to a few *minutes*).
5429 * Speeds up fan to 100% duty-cycle, which is far above
5430 * the standard RPM levels. It is not impossible that
5431 * it could cause hardware damage.
5432 * 5-3 unused in some models. Extra bits for fan level
5433 * in others, but still useless as all values above
5434 * 7 map to the same speed as level 7 in these models.
5435 * 2-0 fan level (0..7 usually)
5436 * 0x00 = stop
5437 * 0x07 = max (set when temperatures critical)
5438 * Some ThinkPads may have other levels, see
5439 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5441 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5442 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5443 * does so, its initial value is meaningless (0x07).
5445 * For firmware bugs, refer to:
5446 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5448 * ----
5450 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5451 * Main fan tachometer reading (in RPM)
5453 * This register is present on all ThinkPads with a new-style EC, and
5454 * it is known not to be present on the A21m/e, and T22, as there is
5455 * something else in offset 0x84 according to the ACPI DSDT. Other
5456 * ThinkPads from this same time period (and earlier) probably lack the
5457 * tachometer as well.
5459 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5460 * was never fixed by IBM to report the EC firmware version string
5461 * probably support the tachometer (like the early X models), so
5462 * detecting it is quite hard. We need more data to know for sure.
5464 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5465 * might result.
5467 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5468 * mode.
5470 * For firmware bugs, refer to:
5471 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5473 * TPACPI_FAN_WR_ACPI_FANS:
5474 * ThinkPad X31, X40, X41. Not available in the X60.
5476 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5477 * high speed. ACPI DSDT seems to map these three speeds to levels
5478 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5479 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5481 * The speeds are stored on handles
5482 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5484 * There are three default speed sets, acessible as handles:
5485 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5487 * ACPI DSDT switches which set is in use depending on various
5488 * factors.
5490 * TPACPI_FAN_WR_TPEC is also available and should be used to
5491 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5492 * but the ACPI tables just mention level 7.
5495 enum { /* Fan control constants */
5496 fan_status_offset = 0x2f, /* EC register 0x2f */
5497 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5498 * 0x84 must be read before 0x85 */
5500 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5501 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5503 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5506 enum fan_status_access_mode {
5507 TPACPI_FAN_NONE = 0, /* No fan status or control */
5508 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5509 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5512 enum fan_control_access_mode {
5513 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5514 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5515 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5516 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5519 enum fan_control_commands {
5520 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5521 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5522 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5523 * and also watchdog cmd */
5526 static int fan_control_allowed;
5528 static enum fan_status_access_mode fan_status_access_mode;
5529 static enum fan_control_access_mode fan_control_access_mode;
5530 static enum fan_control_commands fan_control_commands;
5532 static u8 fan_control_initial_status;
5533 static u8 fan_control_desired_level;
5534 static u8 fan_control_resume_level;
5535 static int fan_watchdog_maxinterval;
5537 static struct mutex fan_mutex;
5539 static void fan_watchdog_fire(struct work_struct *ignored);
5540 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5542 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5543 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5544 "\\FSPD", /* 600e/x, 770e, 770x */
5545 ); /* all others */
5546 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5547 "JFNS", /* 770x-JL */
5548 ); /* all others */
5551 * Call with fan_mutex held
5553 static void fan_update_desired_level(u8 status)
5555 if ((status &
5556 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5557 if (status > 7)
5558 fan_control_desired_level = 7;
5559 else
5560 fan_control_desired_level = status;
5564 static int fan_get_status(u8 *status)
5566 u8 s;
5568 /* TODO:
5569 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5571 switch (fan_status_access_mode) {
5572 case TPACPI_FAN_RD_ACPI_GFAN:
5573 /* 570, 600e/x, 770e, 770x */
5575 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5576 return -EIO;
5578 if (likely(status))
5579 *status = s & 0x07;
5581 break;
5583 case TPACPI_FAN_RD_TPEC:
5584 /* all except 570, 600e/x, 770e, 770x */
5585 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5586 return -EIO;
5588 if (likely(status))
5589 *status = s;
5591 break;
5593 default:
5594 return -ENXIO;
5597 return 0;
5600 static int fan_get_status_safe(u8 *status)
5602 int rc;
5603 u8 s;
5605 if (mutex_lock_interruptible(&fan_mutex))
5606 return -ERESTARTSYS;
5607 rc = fan_get_status(&s);
5608 if (!rc)
5609 fan_update_desired_level(s);
5610 mutex_unlock(&fan_mutex);
5612 if (status)
5613 *status = s;
5615 return rc;
5618 static int fan_get_speed(unsigned int *speed)
5620 u8 hi, lo;
5622 switch (fan_status_access_mode) {
5623 case TPACPI_FAN_RD_TPEC:
5624 /* all except 570, 600e/x, 770e, 770x */
5625 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5626 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5627 return -EIO;
5629 if (likely(speed))
5630 *speed = (hi << 8) | lo;
5632 break;
5634 default:
5635 return -ENXIO;
5638 return 0;
5641 static int fan_set_level(int level)
5643 if (!fan_control_allowed)
5644 return -EPERM;
5646 switch (fan_control_access_mode) {
5647 case TPACPI_FAN_WR_ACPI_SFAN:
5648 if (level >= 0 && level <= 7) {
5649 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5650 return -EIO;
5651 } else
5652 return -EINVAL;
5653 break;
5655 case TPACPI_FAN_WR_ACPI_FANS:
5656 case TPACPI_FAN_WR_TPEC:
5657 if (!(level & TP_EC_FAN_AUTO) &&
5658 !(level & TP_EC_FAN_FULLSPEED) &&
5659 ((level < 0) || (level > 7)))
5660 return -EINVAL;
5662 /* safety net should the EC not support AUTO
5663 * or FULLSPEED mode bits and just ignore them */
5664 if (level & TP_EC_FAN_FULLSPEED)
5665 level |= 7; /* safety min speed 7 */
5666 else if (level & TP_EC_FAN_AUTO)
5667 level |= 4; /* safety min speed 4 */
5669 if (!acpi_ec_write(fan_status_offset, level))
5670 return -EIO;
5671 else
5672 tp_features.fan_ctrl_status_undef = 0;
5673 break;
5675 default:
5676 return -ENXIO;
5678 return 0;
5681 static int fan_set_level_safe(int level)
5683 int rc;
5685 if (!fan_control_allowed)
5686 return -EPERM;
5688 if (mutex_lock_interruptible(&fan_mutex))
5689 return -ERESTARTSYS;
5691 if (level == TPACPI_FAN_LAST_LEVEL)
5692 level = fan_control_desired_level;
5694 rc = fan_set_level(level);
5695 if (!rc)
5696 fan_update_desired_level(level);
5698 mutex_unlock(&fan_mutex);
5699 return rc;
5702 static int fan_set_enable(void)
5704 u8 s;
5705 int rc;
5707 if (!fan_control_allowed)
5708 return -EPERM;
5710 if (mutex_lock_interruptible(&fan_mutex))
5711 return -ERESTARTSYS;
5713 switch (fan_control_access_mode) {
5714 case TPACPI_FAN_WR_ACPI_FANS:
5715 case TPACPI_FAN_WR_TPEC:
5716 rc = fan_get_status(&s);
5717 if (rc < 0)
5718 break;
5720 /* Don't go out of emergency fan mode */
5721 if (s != 7) {
5722 s &= 0x07;
5723 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5726 if (!acpi_ec_write(fan_status_offset, s))
5727 rc = -EIO;
5728 else {
5729 tp_features.fan_ctrl_status_undef = 0;
5730 rc = 0;
5732 break;
5734 case TPACPI_FAN_WR_ACPI_SFAN:
5735 rc = fan_get_status(&s);
5736 if (rc < 0)
5737 break;
5739 s &= 0x07;
5741 /* Set fan to at least level 4 */
5742 s |= 4;
5744 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5745 rc = -EIO;
5746 else
5747 rc = 0;
5748 break;
5750 default:
5751 rc = -ENXIO;
5754 mutex_unlock(&fan_mutex);
5755 return rc;
5758 static int fan_set_disable(void)
5760 int rc;
5762 if (!fan_control_allowed)
5763 return -EPERM;
5765 if (mutex_lock_interruptible(&fan_mutex))
5766 return -ERESTARTSYS;
5768 rc = 0;
5769 switch (fan_control_access_mode) {
5770 case TPACPI_FAN_WR_ACPI_FANS:
5771 case TPACPI_FAN_WR_TPEC:
5772 if (!acpi_ec_write(fan_status_offset, 0x00))
5773 rc = -EIO;
5774 else {
5775 fan_control_desired_level = 0;
5776 tp_features.fan_ctrl_status_undef = 0;
5778 break;
5780 case TPACPI_FAN_WR_ACPI_SFAN:
5781 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5782 rc = -EIO;
5783 else
5784 fan_control_desired_level = 0;
5785 break;
5787 default:
5788 rc = -ENXIO;
5792 mutex_unlock(&fan_mutex);
5793 return rc;
5796 static int fan_set_speed(int speed)
5798 int rc;
5800 if (!fan_control_allowed)
5801 return -EPERM;
5803 if (mutex_lock_interruptible(&fan_mutex))
5804 return -ERESTARTSYS;
5806 rc = 0;
5807 switch (fan_control_access_mode) {
5808 case TPACPI_FAN_WR_ACPI_FANS:
5809 if (speed >= 0 && speed <= 65535) {
5810 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5811 speed, speed, speed))
5812 rc = -EIO;
5813 } else
5814 rc = -EINVAL;
5815 break;
5817 default:
5818 rc = -ENXIO;
5821 mutex_unlock(&fan_mutex);
5822 return rc;
5825 static void fan_watchdog_reset(void)
5827 static int fan_watchdog_active;
5829 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5830 return;
5832 if (fan_watchdog_active)
5833 cancel_delayed_work(&fan_watchdog_task);
5835 if (fan_watchdog_maxinterval > 0 &&
5836 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5837 fan_watchdog_active = 1;
5838 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5839 msecs_to_jiffies(fan_watchdog_maxinterval
5840 * 1000))) {
5841 printk(TPACPI_ERR
5842 "failed to queue the fan watchdog, "
5843 "watchdog will not trigger\n");
5845 } else
5846 fan_watchdog_active = 0;
5849 static void fan_watchdog_fire(struct work_struct *ignored)
5851 int rc;
5853 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5854 return;
5856 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5857 rc = fan_set_enable();
5858 if (rc < 0) {
5859 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5860 "will try again later...\n", -rc);
5861 /* reschedule for later */
5862 fan_watchdog_reset();
5867 * SYSFS fan layout: hwmon compatible (device)
5869 * pwm*_enable:
5870 * 0: "disengaged" mode
5871 * 1: manual mode
5872 * 2: native EC "auto" mode (recommended, hardware default)
5874 * pwm*: set speed in manual mode, ignored otherwise.
5875 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5876 * interpolation.
5878 * fan*_input: tachometer reading, RPM
5881 * SYSFS fan layout: extensions
5883 * fan_watchdog (driver):
5884 * fan watchdog interval in seconds, 0 disables (default), max 120
5887 /* sysfs fan pwm1_enable ----------------------------------------------- */
5888 static ssize_t fan_pwm1_enable_show(struct device *dev,
5889 struct device_attribute *attr,
5890 char *buf)
5892 int res, mode;
5893 u8 status;
5895 res = fan_get_status_safe(&status);
5896 if (res)
5897 return res;
5899 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5900 if (status != fan_control_initial_status) {
5901 tp_features.fan_ctrl_status_undef = 0;
5902 } else {
5903 /* Return most likely status. In fact, it
5904 * might be the only possible status */
5905 status = TP_EC_FAN_AUTO;
5909 if (status & TP_EC_FAN_FULLSPEED) {
5910 mode = 0;
5911 } else if (status & TP_EC_FAN_AUTO) {
5912 mode = 2;
5913 } else
5914 mode = 1;
5916 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5919 static ssize_t fan_pwm1_enable_store(struct device *dev,
5920 struct device_attribute *attr,
5921 const char *buf, size_t count)
5923 unsigned long t;
5924 int res, level;
5926 if (parse_strtoul(buf, 2, &t))
5927 return -EINVAL;
5929 switch (t) {
5930 case 0:
5931 level = TP_EC_FAN_FULLSPEED;
5932 break;
5933 case 1:
5934 level = TPACPI_FAN_LAST_LEVEL;
5935 break;
5936 case 2:
5937 level = TP_EC_FAN_AUTO;
5938 break;
5939 case 3:
5940 /* reserved for software-controlled auto mode */
5941 return -ENOSYS;
5942 default:
5943 return -EINVAL;
5946 res = fan_set_level_safe(level);
5947 if (res == -ENXIO)
5948 return -EINVAL;
5949 else if (res < 0)
5950 return res;
5952 fan_watchdog_reset();
5954 return count;
5957 static struct device_attribute dev_attr_fan_pwm1_enable =
5958 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5959 fan_pwm1_enable_show, fan_pwm1_enable_store);
5961 /* sysfs fan pwm1 ------------------------------------------------------ */
5962 static ssize_t fan_pwm1_show(struct device *dev,
5963 struct device_attribute *attr,
5964 char *buf)
5966 int res;
5967 u8 status;
5969 res = fan_get_status_safe(&status);
5970 if (res)
5971 return res;
5973 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5974 if (status != fan_control_initial_status) {
5975 tp_features.fan_ctrl_status_undef = 0;
5976 } else {
5977 status = TP_EC_FAN_AUTO;
5981 if ((status &
5982 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5983 status = fan_control_desired_level;
5985 if (status > 7)
5986 status = 7;
5988 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5991 static ssize_t fan_pwm1_store(struct device *dev,
5992 struct device_attribute *attr,
5993 const char *buf, size_t count)
5995 unsigned long s;
5996 int rc;
5997 u8 status, newlevel;
5999 if (parse_strtoul(buf, 255, &s))
6000 return -EINVAL;
6002 /* scale down from 0-255 to 0-7 */
6003 newlevel = (s >> 5) & 0x07;
6005 if (mutex_lock_interruptible(&fan_mutex))
6006 return -ERESTARTSYS;
6008 rc = fan_get_status(&status);
6009 if (!rc && (status &
6010 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6011 rc = fan_set_level(newlevel);
6012 if (rc == -ENXIO)
6013 rc = -EINVAL;
6014 else if (!rc) {
6015 fan_update_desired_level(newlevel);
6016 fan_watchdog_reset();
6020 mutex_unlock(&fan_mutex);
6021 return (rc)? rc : count;
6024 static struct device_attribute dev_attr_fan_pwm1 =
6025 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6026 fan_pwm1_show, fan_pwm1_store);
6028 /* sysfs fan fan1_input ------------------------------------------------ */
6029 static ssize_t fan_fan1_input_show(struct device *dev,
6030 struct device_attribute *attr,
6031 char *buf)
6033 int res;
6034 unsigned int speed;
6036 res = fan_get_speed(&speed);
6037 if (res < 0)
6038 return res;
6040 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6043 static struct device_attribute dev_attr_fan_fan1_input =
6044 __ATTR(fan1_input, S_IRUGO,
6045 fan_fan1_input_show, NULL);
6047 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6048 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6049 char *buf)
6051 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6054 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6055 const char *buf, size_t count)
6057 unsigned long t;
6059 if (parse_strtoul(buf, 120, &t))
6060 return -EINVAL;
6062 if (!fan_control_allowed)
6063 return -EPERM;
6065 fan_watchdog_maxinterval = t;
6066 fan_watchdog_reset();
6068 return count;
6071 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6072 fan_fan_watchdog_show, fan_fan_watchdog_store);
6074 /* --------------------------------------------------------------------- */
6075 static struct attribute *fan_attributes[] = {
6076 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6077 &dev_attr_fan_fan1_input.attr,
6078 NULL
6081 static const struct attribute_group fan_attr_group = {
6082 .attrs = fan_attributes,
6085 static int __init fan_init(struct ibm_init_struct *iibm)
6087 int rc;
6089 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6091 mutex_init(&fan_mutex);
6092 fan_status_access_mode = TPACPI_FAN_NONE;
6093 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6094 fan_control_commands = 0;
6095 fan_watchdog_maxinterval = 0;
6096 tp_features.fan_ctrl_status_undef = 0;
6097 fan_control_desired_level = 7;
6099 TPACPI_ACPIHANDLE_INIT(fans);
6100 TPACPI_ACPIHANDLE_INIT(gfan);
6101 TPACPI_ACPIHANDLE_INIT(sfan);
6103 if (gfan_handle) {
6104 /* 570, 600e/x, 770e, 770x */
6105 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6106 } else {
6107 /* all other ThinkPads: note that even old-style
6108 * ThinkPad ECs supports the fan control register */
6109 if (likely(acpi_ec_read(fan_status_offset,
6110 &fan_control_initial_status))) {
6111 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6113 /* In some ThinkPads, neither the EC nor the ACPI
6114 * DSDT initialize the fan status, and it ends up
6115 * being set to 0x07 when it *could* be either
6116 * 0x07 or 0x80.
6118 * Enable for TP-1Y (T43), TP-78 (R51e),
6119 * TP-76 (R52), TP-70 (T43, R52), which are known
6120 * to be buggy. */
6121 if (fan_control_initial_status == 0x07) {
6122 switch (thinkpad_id.ec_model) {
6123 case 0x5931: /* TP-1Y */
6124 case 0x3837: /* TP-78 */
6125 case 0x3637: /* TP-76 */
6126 case 0x3037: /* TP-70 */
6127 printk(TPACPI_NOTICE
6128 "fan_init: initial fan status "
6129 "is unknown, assuming it is "
6130 "in auto mode\n");
6131 tp_features.fan_ctrl_status_undef = 1;
6135 } else {
6136 printk(TPACPI_ERR
6137 "ThinkPad ACPI EC access misbehaving, "
6138 "fan status and control unavailable\n");
6139 return 1;
6143 if (sfan_handle) {
6144 /* 570, 770x-JL */
6145 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6146 fan_control_commands |=
6147 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6148 } else {
6149 if (!gfan_handle) {
6150 /* gfan without sfan means no fan control */
6151 /* all other models implement TP EC 0x2f control */
6153 if (fans_handle) {
6154 /* X31, X40, X41 */
6155 fan_control_access_mode =
6156 TPACPI_FAN_WR_ACPI_FANS;
6157 fan_control_commands |=
6158 TPACPI_FAN_CMD_SPEED |
6159 TPACPI_FAN_CMD_LEVEL |
6160 TPACPI_FAN_CMD_ENABLE;
6161 } else {
6162 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6163 fan_control_commands |=
6164 TPACPI_FAN_CMD_LEVEL |
6165 TPACPI_FAN_CMD_ENABLE;
6170 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6171 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6172 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6173 fan_status_access_mode, fan_control_access_mode);
6175 /* fan control master switch */
6176 if (!fan_control_allowed) {
6177 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6178 fan_control_commands = 0;
6179 dbg_printk(TPACPI_DBG_INIT,
6180 "fan control features disabled by parameter\n");
6183 /* update fan_control_desired_level */
6184 if (fan_status_access_mode != TPACPI_FAN_NONE)
6185 fan_get_status_safe(NULL);
6187 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6188 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6189 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6190 &fan_attr_group);
6191 if (rc < 0)
6192 return rc;
6194 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6195 &driver_attr_fan_watchdog);
6196 if (rc < 0) {
6197 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6198 &fan_attr_group);
6199 return rc;
6201 return 0;
6202 } else
6203 return 1;
6206 static void fan_exit(void)
6208 vdbg_printk(TPACPI_DBG_EXIT,
6209 "cancelling any pending fan watchdog tasks\n");
6211 /* FIXME: can we really do this unconditionally? */
6212 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6213 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6214 &driver_attr_fan_watchdog);
6216 cancel_delayed_work(&fan_watchdog_task);
6217 flush_workqueue(tpacpi_wq);
6220 static void fan_suspend(pm_message_t state)
6222 int rc;
6224 if (!fan_control_allowed)
6225 return;
6227 /* Store fan status in cache */
6228 fan_control_resume_level = 0;
6229 rc = fan_get_status_safe(&fan_control_resume_level);
6230 if (rc < 0)
6231 printk(TPACPI_NOTICE
6232 "failed to read fan level for later "
6233 "restore during resume: %d\n", rc);
6235 /* if it is undefined, don't attempt to restore it.
6236 * KEEP THIS LAST */
6237 if (tp_features.fan_ctrl_status_undef)
6238 fan_control_resume_level = 0;
6241 static void fan_resume(void)
6243 u8 current_level = 7;
6244 bool do_set = false;
6245 int rc;
6247 /* DSDT *always* updates status on resume */
6248 tp_features.fan_ctrl_status_undef = 0;
6250 if (!fan_control_allowed ||
6251 !fan_control_resume_level ||
6252 (fan_get_status_safe(&current_level) < 0))
6253 return;
6255 switch (fan_control_access_mode) {
6256 case TPACPI_FAN_WR_ACPI_SFAN:
6257 /* never decrease fan level */
6258 do_set = (fan_control_resume_level > current_level);
6259 break;
6260 case TPACPI_FAN_WR_ACPI_FANS:
6261 case TPACPI_FAN_WR_TPEC:
6262 /* never decrease fan level, scale is:
6263 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6265 * We expect the firmware to set either 7 or AUTO, but we
6266 * handle FULLSPEED out of paranoia.
6268 * So, we can safely only restore FULLSPEED or 7, anything
6269 * else could slow the fan. Restoring AUTO is useless, at
6270 * best that's exactly what the DSDT already set (it is the
6271 * slower it uses).
6273 * Always keep in mind that the DSDT *will* have set the
6274 * fans to what the vendor supposes is the best level. We
6275 * muck with it only to speed the fan up.
6277 if (fan_control_resume_level != 7 &&
6278 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6279 return;
6280 else
6281 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6282 (current_level != fan_control_resume_level);
6283 break;
6284 default:
6285 return;
6287 if (do_set) {
6288 printk(TPACPI_NOTICE
6289 "restoring fan level to 0x%02x\n",
6290 fan_control_resume_level);
6291 rc = fan_set_level_safe(fan_control_resume_level);
6292 if (rc < 0)
6293 printk(TPACPI_NOTICE
6294 "failed to restore fan level: %d\n", rc);
6298 static int fan_read(char *p)
6300 int len = 0;
6301 int rc;
6302 u8 status;
6303 unsigned int speed = 0;
6305 switch (fan_status_access_mode) {
6306 case TPACPI_FAN_RD_ACPI_GFAN:
6307 /* 570, 600e/x, 770e, 770x */
6308 rc = fan_get_status_safe(&status);
6309 if (rc < 0)
6310 return rc;
6312 len += sprintf(p + len, "status:\t\t%s\n"
6313 "level:\t\t%d\n",
6314 (status != 0) ? "enabled" : "disabled", status);
6315 break;
6317 case TPACPI_FAN_RD_TPEC:
6318 /* all except 570, 600e/x, 770e, 770x */
6319 rc = fan_get_status_safe(&status);
6320 if (rc < 0)
6321 return rc;
6323 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6324 if (status != fan_control_initial_status)
6325 tp_features.fan_ctrl_status_undef = 0;
6326 else
6327 /* Return most likely status. In fact, it
6328 * might be the only possible status */
6329 status = TP_EC_FAN_AUTO;
6332 len += sprintf(p + len, "status:\t\t%s\n",
6333 (status != 0) ? "enabled" : "disabled");
6335 rc = fan_get_speed(&speed);
6336 if (rc < 0)
6337 return rc;
6339 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6341 if (status & TP_EC_FAN_FULLSPEED)
6342 /* Disengaged mode takes precedence */
6343 len += sprintf(p + len, "level:\t\tdisengaged\n");
6344 else if (status & TP_EC_FAN_AUTO)
6345 len += sprintf(p + len, "level:\t\tauto\n");
6346 else
6347 len += sprintf(p + len, "level:\t\t%d\n", status);
6348 break;
6350 case TPACPI_FAN_NONE:
6351 default:
6352 len += sprintf(p + len, "status:\t\tnot supported\n");
6355 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6356 len += sprintf(p + len, "commands:\tlevel <level>");
6358 switch (fan_control_access_mode) {
6359 case TPACPI_FAN_WR_ACPI_SFAN:
6360 len += sprintf(p + len, " (<level> is 0-7)\n");
6361 break;
6363 default:
6364 len += sprintf(p + len, " (<level> is 0-7, "
6365 "auto, disengaged, full-speed)\n");
6366 break;
6370 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6371 len += sprintf(p + len, "commands:\tenable, disable\n"
6372 "commands:\twatchdog <timeout> (<timeout> "
6373 "is 0 (off), 1-120 (seconds))\n");
6375 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6376 len += sprintf(p + len, "commands:\tspeed <speed>"
6377 " (<speed> is 0-65535)\n");
6379 return len;
6382 static int fan_write_cmd_level(const char *cmd, int *rc)
6384 int level;
6386 if (strlencmp(cmd, "level auto") == 0)
6387 level = TP_EC_FAN_AUTO;
6388 else if ((strlencmp(cmd, "level disengaged") == 0) |
6389 (strlencmp(cmd, "level full-speed") == 0))
6390 level = TP_EC_FAN_FULLSPEED;
6391 else if (sscanf(cmd, "level %d", &level) != 1)
6392 return 0;
6394 *rc = fan_set_level_safe(level);
6395 if (*rc == -ENXIO)
6396 printk(TPACPI_ERR "level command accepted for unsupported "
6397 "access mode %d", fan_control_access_mode);
6399 return 1;
6402 static int fan_write_cmd_enable(const char *cmd, int *rc)
6404 if (strlencmp(cmd, "enable") != 0)
6405 return 0;
6407 *rc = fan_set_enable();
6408 if (*rc == -ENXIO)
6409 printk(TPACPI_ERR "enable command accepted for unsupported "
6410 "access mode %d", fan_control_access_mode);
6412 return 1;
6415 static int fan_write_cmd_disable(const char *cmd, int *rc)
6417 if (strlencmp(cmd, "disable") != 0)
6418 return 0;
6420 *rc = fan_set_disable();
6421 if (*rc == -ENXIO)
6422 printk(TPACPI_ERR "disable command accepted for unsupported "
6423 "access mode %d", fan_control_access_mode);
6425 return 1;
6428 static int fan_write_cmd_speed(const char *cmd, int *rc)
6430 int speed;
6432 /* TODO:
6433 * Support speed <low> <medium> <high> ? */
6435 if (sscanf(cmd, "speed %d", &speed) != 1)
6436 return 0;
6438 *rc = fan_set_speed(speed);
6439 if (*rc == -ENXIO)
6440 printk(TPACPI_ERR "speed command accepted for unsupported "
6441 "access mode %d", fan_control_access_mode);
6443 return 1;
6446 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6448 int interval;
6450 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6451 return 0;
6453 if (interval < 0 || interval > 120)
6454 *rc = -EINVAL;
6455 else
6456 fan_watchdog_maxinterval = interval;
6458 return 1;
6461 static int fan_write(char *buf)
6463 char *cmd;
6464 int rc = 0;
6466 while (!rc && (cmd = next_cmd(&buf))) {
6467 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6468 fan_write_cmd_level(cmd, &rc)) &&
6469 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6470 (fan_write_cmd_enable(cmd, &rc) ||
6471 fan_write_cmd_disable(cmd, &rc) ||
6472 fan_write_cmd_watchdog(cmd, &rc))) &&
6473 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6474 fan_write_cmd_speed(cmd, &rc))
6476 rc = -EINVAL;
6477 else if (!rc)
6478 fan_watchdog_reset();
6481 return rc;
6484 static struct ibm_struct fan_driver_data = {
6485 .name = "fan",
6486 .read = fan_read,
6487 .write = fan_write,
6488 .exit = fan_exit,
6489 .suspend = fan_suspend,
6490 .resume = fan_resume,
6493 /****************************************************************************
6494 ****************************************************************************
6496 * Infrastructure
6498 ****************************************************************************
6499 ****************************************************************************/
6501 /* sysfs name ---------------------------------------------------------- */
6502 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6503 struct device_attribute *attr,
6504 char *buf)
6506 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6509 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6510 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6512 /* --------------------------------------------------------------------- */
6514 /* /proc support */
6515 static struct proc_dir_entry *proc_dir;
6518 * Module and infrastructure proble, init and exit handling
6521 static int force_load;
6523 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6524 static const char * __init str_supported(int is_supported)
6526 static char text_unsupported[] __initdata = "not supported";
6528 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6530 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6532 static void ibm_exit(struct ibm_struct *ibm)
6534 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6536 list_del_init(&ibm->all_drivers);
6538 if (ibm->flags.acpi_notify_installed) {
6539 dbg_printk(TPACPI_DBG_EXIT,
6540 "%s: acpi_remove_notify_handler\n", ibm->name);
6541 BUG_ON(!ibm->acpi);
6542 acpi_remove_notify_handler(*ibm->acpi->handle,
6543 ibm->acpi->type,
6544 dispatch_acpi_notify);
6545 ibm->flags.acpi_notify_installed = 0;
6546 ibm->flags.acpi_notify_installed = 0;
6549 if (ibm->flags.proc_created) {
6550 dbg_printk(TPACPI_DBG_EXIT,
6551 "%s: remove_proc_entry\n", ibm->name);
6552 remove_proc_entry(ibm->name, proc_dir);
6553 ibm->flags.proc_created = 0;
6556 if (ibm->flags.acpi_driver_registered) {
6557 dbg_printk(TPACPI_DBG_EXIT,
6558 "%s: acpi_bus_unregister_driver\n", ibm->name);
6559 BUG_ON(!ibm->acpi);
6560 acpi_bus_unregister_driver(ibm->acpi->driver);
6561 kfree(ibm->acpi->driver);
6562 ibm->acpi->driver = NULL;
6563 ibm->flags.acpi_driver_registered = 0;
6566 if (ibm->flags.init_called && ibm->exit) {
6567 ibm->exit();
6568 ibm->flags.init_called = 0;
6571 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6574 static int __init ibm_init(struct ibm_init_struct *iibm)
6576 int ret;
6577 struct ibm_struct *ibm = iibm->data;
6578 struct proc_dir_entry *entry;
6580 BUG_ON(ibm == NULL);
6582 INIT_LIST_HEAD(&ibm->all_drivers);
6584 if (ibm->flags.experimental && !experimental)
6585 return 0;
6587 dbg_printk(TPACPI_DBG_INIT,
6588 "probing for %s\n", ibm->name);
6590 if (iibm->init) {
6591 ret = iibm->init(iibm);
6592 if (ret > 0)
6593 return 0; /* probe failed */
6594 if (ret)
6595 return ret;
6597 ibm->flags.init_called = 1;
6600 if (ibm->acpi) {
6601 if (ibm->acpi->hid) {
6602 ret = register_tpacpi_subdriver(ibm);
6603 if (ret)
6604 goto err_out;
6607 if (ibm->acpi->notify) {
6608 ret = setup_acpi_notify(ibm);
6609 if (ret == -ENODEV) {
6610 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6611 ibm->name);
6612 ret = 0;
6613 goto err_out;
6615 if (ret < 0)
6616 goto err_out;
6620 dbg_printk(TPACPI_DBG_INIT,
6621 "%s installed\n", ibm->name);
6623 if (ibm->read) {
6624 entry = create_proc_entry(ibm->name,
6625 S_IFREG | S_IRUGO | S_IWUSR,
6626 proc_dir);
6627 if (!entry) {
6628 printk(TPACPI_ERR "unable to create proc entry %s\n",
6629 ibm->name);
6630 ret = -ENODEV;
6631 goto err_out;
6633 entry->owner = THIS_MODULE;
6634 entry->data = ibm;
6635 entry->read_proc = &dispatch_procfs_read;
6636 if (ibm->write)
6637 entry->write_proc = &dispatch_procfs_write;
6638 ibm->flags.proc_created = 1;
6641 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6643 return 0;
6645 err_out:
6646 dbg_printk(TPACPI_DBG_INIT,
6647 "%s: at error exit path with result %d\n",
6648 ibm->name, ret);
6650 ibm_exit(ibm);
6651 return (ret < 0)? ret : 0;
6654 /* Probing */
6656 /* returns 0 - probe ok, or < 0 - probe error.
6657 * Probe ok doesn't mean thinkpad found.
6658 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6659 static int __must_check __init get_thinkpad_model_data(
6660 struct thinkpad_id_data *tp)
6662 const struct dmi_device *dev = NULL;
6663 char ec_fw_string[18];
6664 char const *s;
6666 if (!tp)
6667 return -EINVAL;
6669 memset(tp, 0, sizeof(*tp));
6671 if (dmi_name_in_vendors("IBM"))
6672 tp->vendor = PCI_VENDOR_ID_IBM;
6673 else if (dmi_name_in_vendors("LENOVO"))
6674 tp->vendor = PCI_VENDOR_ID_LENOVO;
6675 else
6676 return 0;
6678 s = dmi_get_system_info(DMI_BIOS_VERSION);
6679 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6680 if (s && !tp->bios_version_str)
6681 return -ENOMEM;
6682 if (!tp->bios_version_str)
6683 return 0;
6684 tp->bios_model = tp->bios_version_str[0]
6685 | (tp->bios_version_str[1] << 8);
6688 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6689 * X32 or newer, all Z series; Some models must have an
6690 * up-to-date BIOS or they will not be detected.
6692 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6694 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6695 if (sscanf(dev->name,
6696 "IBM ThinkPad Embedded Controller -[%17c",
6697 ec_fw_string) == 1) {
6698 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6699 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6701 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6702 if (!tp->ec_version_str)
6703 return -ENOMEM;
6704 tp->ec_model = ec_fw_string[0]
6705 | (ec_fw_string[1] << 8);
6706 break;
6710 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6711 if (s && !strnicmp(s, "ThinkPad", 8)) {
6712 tp->model_str = kstrdup(s, GFP_KERNEL);
6713 if (!tp->model_str)
6714 return -ENOMEM;
6717 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6718 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6719 if (s && !tp->nummodel_str)
6720 return -ENOMEM;
6722 return 0;
6725 static int __init probe_for_thinkpad(void)
6727 int is_thinkpad;
6729 if (acpi_disabled)
6730 return -ENODEV;
6733 * Non-ancient models have better DMI tagging, but very old models
6734 * don't.
6736 is_thinkpad = (thinkpad_id.model_str != NULL);
6738 /* ec is required because many other handles are relative to it */
6739 TPACPI_ACPIHANDLE_INIT(ec);
6740 if (!ec_handle) {
6741 if (is_thinkpad)
6742 printk(TPACPI_ERR
6743 "Not yet supported ThinkPad detected!\n");
6744 return -ENODEV;
6748 * Risks a regression on very old machines, but reduces potential
6749 * false positives a damn great deal
6751 if (!is_thinkpad)
6752 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6754 if (!is_thinkpad && !force_load)
6755 return -ENODEV;
6757 return 0;
6761 /* Module init, exit, parameters */
6763 static struct ibm_init_struct ibms_init[] __initdata = {
6765 .init = thinkpad_acpi_driver_init,
6766 .data = &thinkpad_acpi_driver_data,
6769 .init = hotkey_init,
6770 .data = &hotkey_driver_data,
6773 .init = bluetooth_init,
6774 .data = &bluetooth_driver_data,
6777 .init = wan_init,
6778 .data = &wan_driver_data,
6780 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6782 .init = video_init,
6783 .data = &video_driver_data,
6785 #endif
6787 .init = light_init,
6788 .data = &light_driver_data,
6790 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6792 .init = dock_init,
6793 .data = &dock_driver_data[0],
6796 .init = dock_init2,
6797 .data = &dock_driver_data[1],
6799 #endif
6800 #ifdef CONFIG_THINKPAD_ACPI_BAY
6802 .init = bay_init,
6803 .data = &bay_driver_data,
6805 #endif
6807 .init = cmos_init,
6808 .data = &cmos_driver_data,
6811 .init = led_init,
6812 .data = &led_driver_data,
6815 .init = beep_init,
6816 .data = &beep_driver_data,
6819 .init = thermal_init,
6820 .data = &thermal_driver_data,
6823 .data = &ecdump_driver_data,
6826 .init = brightness_init,
6827 .data = &brightness_driver_data,
6830 .data = &volume_driver_data,
6833 .init = fan_init,
6834 .data = &fan_driver_data,
6838 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6840 unsigned int i;
6841 struct ibm_struct *ibm;
6843 if (!kp || !kp->name || !val)
6844 return -EINVAL;
6846 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6847 ibm = ibms_init[i].data;
6848 WARN_ON(ibm == NULL);
6850 if (!ibm || !ibm->name)
6851 continue;
6853 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6854 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6855 return -ENOSPC;
6856 strcpy(ibms_init[i].param, val);
6857 strcat(ibms_init[i].param, ",");
6858 return 0;
6862 return -EINVAL;
6865 module_param(experimental, int, 0);
6866 MODULE_PARM_DESC(experimental,
6867 "Enables experimental features when non-zero");
6869 module_param_named(debug, dbg_level, uint, 0);
6870 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6872 module_param(force_load, bool, 0);
6873 MODULE_PARM_DESC(force_load,
6874 "Attempts to load the driver even on a "
6875 "mis-identified ThinkPad when true");
6877 module_param_named(fan_control, fan_control_allowed, bool, 0);
6878 MODULE_PARM_DESC(fan_control,
6879 "Enables setting fan parameters features when true");
6881 module_param_named(brightness_mode, brightness_mode, int, 0);
6882 MODULE_PARM_DESC(brightness_mode,
6883 "Selects brightness control strategy: "
6884 "0=auto, 1=EC, 2=CMOS, 3=both");
6886 module_param(brightness_enable, uint, 0);
6887 MODULE_PARM_DESC(brightness_enable,
6888 "Enables backlight control when 1, disables when 0");
6890 module_param(hotkey_report_mode, uint, 0);
6891 MODULE_PARM_DESC(hotkey_report_mode,
6892 "used for backwards compatibility with userspace, "
6893 "see documentation");
6895 #define TPACPI_PARAM(feature) \
6896 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6897 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6898 "at module load, see documentation")
6900 TPACPI_PARAM(hotkey);
6901 TPACPI_PARAM(bluetooth);
6902 TPACPI_PARAM(video);
6903 TPACPI_PARAM(light);
6904 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6905 TPACPI_PARAM(dock);
6906 #endif
6907 #ifdef CONFIG_THINKPAD_ACPI_BAY
6908 TPACPI_PARAM(bay);
6909 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6910 TPACPI_PARAM(cmos);
6911 TPACPI_PARAM(led);
6912 TPACPI_PARAM(beep);
6913 TPACPI_PARAM(ecdump);
6914 TPACPI_PARAM(brightness);
6915 TPACPI_PARAM(volume);
6916 TPACPI_PARAM(fan);
6918 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
6919 module_param(dbg_wlswemul, uint, 0);
6920 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
6921 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
6922 MODULE_PARM_DESC(wlsw_state,
6923 "Initial state of the emulated WLSW switch");
6925 module_param(dbg_bluetoothemul, uint, 0);
6926 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
6927 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
6928 MODULE_PARM_DESC(bluetooth_state,
6929 "Initial state of the emulated bluetooth switch");
6931 module_param(dbg_wwanemul, uint, 0);
6932 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
6933 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
6934 MODULE_PARM_DESC(wwan_state,
6935 "Initial state of the emulated WWAN switch");
6936 #endif
6938 static void thinkpad_acpi_module_exit(void)
6940 struct ibm_struct *ibm, *itmp;
6942 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6944 list_for_each_entry_safe_reverse(ibm, itmp,
6945 &tpacpi_all_drivers,
6946 all_drivers) {
6947 ibm_exit(ibm);
6950 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6952 if (tpacpi_inputdev) {
6953 if (tp_features.input_device_registered)
6954 input_unregister_device(tpacpi_inputdev);
6955 else
6956 input_free_device(tpacpi_inputdev);
6959 if (tpacpi_hwmon)
6960 hwmon_device_unregister(tpacpi_hwmon);
6962 if (tp_features.sensors_pdev_attrs_registered)
6963 device_remove_file(&tpacpi_sensors_pdev->dev,
6964 &dev_attr_thinkpad_acpi_pdev_name);
6965 if (tpacpi_sensors_pdev)
6966 platform_device_unregister(tpacpi_sensors_pdev);
6967 if (tpacpi_pdev)
6968 platform_device_unregister(tpacpi_pdev);
6970 if (tp_features.sensors_pdrv_attrs_registered)
6971 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6972 if (tp_features.platform_drv_attrs_registered)
6973 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6975 if (tp_features.sensors_pdrv_registered)
6976 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6978 if (tp_features.platform_drv_registered)
6979 platform_driver_unregister(&tpacpi_pdriver);
6981 if (proc_dir)
6982 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6984 if (tpacpi_wq)
6985 destroy_workqueue(tpacpi_wq);
6987 kfree(thinkpad_id.bios_version_str);
6988 kfree(thinkpad_id.ec_version_str);
6989 kfree(thinkpad_id.model_str);
6993 static int __init thinkpad_acpi_module_init(void)
6995 int ret, i;
6997 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6999 /* Parameter checking */
7000 if (hotkey_report_mode > 2)
7001 return -EINVAL;
7003 /* Driver-level probe */
7005 ret = get_thinkpad_model_data(&thinkpad_id);
7006 if (ret) {
7007 printk(TPACPI_ERR
7008 "unable to get DMI data: %d\n", ret);
7009 thinkpad_acpi_module_exit();
7010 return ret;
7012 ret = probe_for_thinkpad();
7013 if (ret) {
7014 thinkpad_acpi_module_exit();
7015 return ret;
7018 /* Driver initialization */
7020 TPACPI_ACPIHANDLE_INIT(ecrd);
7021 TPACPI_ACPIHANDLE_INIT(ecwr);
7023 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7024 if (!tpacpi_wq) {
7025 thinkpad_acpi_module_exit();
7026 return -ENOMEM;
7029 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7030 if (!proc_dir) {
7031 printk(TPACPI_ERR
7032 "unable to create proc dir " TPACPI_PROC_DIR);
7033 thinkpad_acpi_module_exit();
7034 return -ENODEV;
7036 proc_dir->owner = THIS_MODULE;
7038 ret = platform_driver_register(&tpacpi_pdriver);
7039 if (ret) {
7040 printk(TPACPI_ERR
7041 "unable to register main platform driver\n");
7042 thinkpad_acpi_module_exit();
7043 return ret;
7045 tp_features.platform_drv_registered = 1;
7047 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7048 if (ret) {
7049 printk(TPACPI_ERR
7050 "unable to register hwmon platform driver\n");
7051 thinkpad_acpi_module_exit();
7052 return ret;
7054 tp_features.sensors_pdrv_registered = 1;
7056 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7057 if (!ret) {
7058 tp_features.platform_drv_attrs_registered = 1;
7059 ret = tpacpi_create_driver_attributes(
7060 &tpacpi_hwmon_pdriver.driver);
7062 if (ret) {
7063 printk(TPACPI_ERR
7064 "unable to create sysfs driver attributes\n");
7065 thinkpad_acpi_module_exit();
7066 return ret;
7068 tp_features.sensors_pdrv_attrs_registered = 1;
7071 /* Device initialization */
7072 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7073 NULL, 0);
7074 if (IS_ERR(tpacpi_pdev)) {
7075 ret = PTR_ERR(tpacpi_pdev);
7076 tpacpi_pdev = NULL;
7077 printk(TPACPI_ERR "unable to register platform device\n");
7078 thinkpad_acpi_module_exit();
7079 return ret;
7081 tpacpi_sensors_pdev = platform_device_register_simple(
7082 TPACPI_HWMON_DRVR_NAME,
7083 -1, NULL, 0);
7084 if (IS_ERR(tpacpi_sensors_pdev)) {
7085 ret = PTR_ERR(tpacpi_sensors_pdev);
7086 tpacpi_sensors_pdev = NULL;
7087 printk(TPACPI_ERR
7088 "unable to register hwmon platform device\n");
7089 thinkpad_acpi_module_exit();
7090 return ret;
7092 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7093 &dev_attr_thinkpad_acpi_pdev_name);
7094 if (ret) {
7095 printk(TPACPI_ERR
7096 "unable to create sysfs hwmon device attributes\n");
7097 thinkpad_acpi_module_exit();
7098 return ret;
7100 tp_features.sensors_pdev_attrs_registered = 1;
7101 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7102 if (IS_ERR(tpacpi_hwmon)) {
7103 ret = PTR_ERR(tpacpi_hwmon);
7104 tpacpi_hwmon = NULL;
7105 printk(TPACPI_ERR "unable to register hwmon device\n");
7106 thinkpad_acpi_module_exit();
7107 return ret;
7109 mutex_init(&tpacpi_inputdev_send_mutex);
7110 tpacpi_inputdev = input_allocate_device();
7111 if (!tpacpi_inputdev) {
7112 printk(TPACPI_ERR "unable to allocate input device\n");
7113 thinkpad_acpi_module_exit();
7114 return -ENOMEM;
7115 } else {
7116 /* Prepare input device, but don't register */
7117 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7118 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7119 tpacpi_inputdev->id.bustype = BUS_HOST;
7120 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7121 thinkpad_id.vendor :
7122 PCI_VENDOR_ID_IBM;
7123 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7124 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7126 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7127 ret = ibm_init(&ibms_init[i]);
7128 if (ret >= 0 && *ibms_init[i].param)
7129 ret = ibms_init[i].data->write(ibms_init[i].param);
7130 if (ret < 0) {
7131 thinkpad_acpi_module_exit();
7132 return ret;
7135 ret = input_register_device(tpacpi_inputdev);
7136 if (ret < 0) {
7137 printk(TPACPI_ERR "unable to register input device\n");
7138 thinkpad_acpi_module_exit();
7139 return ret;
7140 } else {
7141 tp_features.input_device_registered = 1;
7144 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7145 return 0;
7148 /* Please remove this in year 2009 */
7149 MODULE_ALIAS("ibm_acpi");
7151 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7154 * DMI matching for module autoloading
7156 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7157 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7159 * Only models listed in thinkwiki will be supported, so add yours
7160 * if it is not there yet.
7162 #define IBM_BIOS_MODULE_ALIAS(__type) \
7163 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7165 /* Non-ancient thinkpads */
7166 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7167 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7169 /* Ancient thinkpad BIOSes have to be identified by
7170 * BIOS type or model number, and there are far less
7171 * BIOS types than model numbers... */
7172 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7173 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7174 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7176 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7177 MODULE_DESCRIPTION(TPACPI_DESC);
7178 MODULE_VERSION(TPACPI_VERSION);
7179 MODULE_LICENSE("GPL");
7181 module_init(thinkpad_acpi_module_init);
7182 module_exit(thinkpad_acpi_module_exit);