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
blobb2c5913ff72e478fa2b9374eab9fe70571f53329
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>
80 #include <linux/pci_ids.h>
83 /* ThinkPad CMOS commands */
84 #define TP_CMOS_VOLUME_DOWN 0
85 #define TP_CMOS_VOLUME_UP 1
86 #define TP_CMOS_VOLUME_MUTE 2
87 #define TP_CMOS_BRIGHTNESS_UP 4
88 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 #define TP_CMOS_THINKLIGHT_ON 12
90 #define TP_CMOS_THINKLIGHT_OFF 13
92 /* NVRAM Addresses */
93 enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
101 /* NVRAM bit masks */
102 enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
118 /* ACPI HIDs */
119 #define TPACPI_ACPI_HKEY_HID "IBM0068"
121 /* Input IDs */
122 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123 #define TPACPI_HKEY_INPUT_VERSION 0x4101
125 /* ACPI \WGSV commands */
126 enum {
127 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
128 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
129 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
130 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
133 /* TP_ACPI_WGSV_GET_STATE bits */
134 enum {
135 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
136 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
137 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
138 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
139 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
140 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
141 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
142 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
143 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
144 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
147 /****************************************************************************
148 * Main driver
151 #define TPACPI_NAME "thinkpad"
152 #define TPACPI_DESC "ThinkPad ACPI Extras"
153 #define TPACPI_FILE TPACPI_NAME "_acpi"
154 #define TPACPI_URL "http://ibm-acpi.sf.net/"
155 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
157 #define TPACPI_PROC_DIR "ibm"
158 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
159 #define TPACPI_DRVR_NAME TPACPI_FILE
160 #define TPACPI_DRVR_SHORTNAME "tpacpi"
161 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
163 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
164 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
166 #define TPACPI_MAX_ACPI_ARGS 3
168 /* rfkill switches */
169 enum {
170 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
171 TPACPI_RFK_WWAN_SW_ID,
174 /* Debugging */
175 #define TPACPI_LOG TPACPI_FILE ": "
176 #define TPACPI_ERR KERN_ERR TPACPI_LOG
177 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
178 #define TPACPI_INFO KERN_INFO TPACPI_LOG
179 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
181 #define TPACPI_DBG_ALL 0xffff
182 #define TPACPI_DBG_INIT 0x0001
183 #define TPACPI_DBG_EXIT 0x0002
184 #define dbg_printk(a_dbg_level, format, arg...) \
185 do { if (dbg_level & a_dbg_level) \
186 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
187 } while (0)
188 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
189 #define vdbg_printk(a_dbg_level, format, arg...) \
190 dbg_printk(a_dbg_level, format, ## arg)
191 static const char *str_supported(int is_supported);
192 #else
193 #define vdbg_printk(a_dbg_level, format, arg...)
194 #endif
196 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
197 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
198 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
201 /****************************************************************************
202 * Driver-wide structs and misc. variables
205 struct ibm_struct;
207 struct tp_acpi_drv_struct {
208 const struct acpi_device_id *hid;
209 struct acpi_driver *driver;
211 void (*notify) (struct ibm_struct *, u32);
212 acpi_handle *handle;
213 u32 type;
214 struct acpi_device *device;
217 struct ibm_struct {
218 char *name;
220 int (*read) (char *);
221 int (*write) (char *);
222 void (*exit) (void);
223 void (*resume) (void);
224 void (*suspend) (pm_message_t state);
226 struct list_head all_drivers;
228 struct tp_acpi_drv_struct *acpi;
230 struct {
231 u8 acpi_driver_registered:1;
232 u8 acpi_notify_installed:1;
233 u8 proc_created:1;
234 u8 init_called:1;
235 u8 experimental:1;
236 } flags;
239 struct ibm_init_struct {
240 char param[32];
242 int (*init) (struct ibm_init_struct *);
243 struct ibm_struct *data;
246 static struct {
247 #ifdef CONFIG_THINKPAD_ACPI_BAY
248 u32 bay_status:1;
249 u32 bay_eject:1;
250 u32 bay_status2:1;
251 u32 bay_eject2:1;
252 #endif
253 u32 bluetooth:1;
254 u32 hotkey:1;
255 u32 hotkey_mask:1;
256 u32 hotkey_wlsw:1;
257 u32 hotkey_tablet:1;
258 u32 light:1;
259 u32 light_status:1;
260 u32 bright_16levels:1;
261 u32 bright_acpimode:1;
262 u32 wan:1;
263 u32 fan_ctrl_status_undef:1;
264 u32 input_device_registered:1;
265 u32 platform_drv_registered:1;
266 u32 platform_drv_attrs_registered:1;
267 u32 sensors_pdrv_registered:1;
268 u32 sensors_pdrv_attrs_registered:1;
269 u32 sensors_pdev_attrs_registered:1;
270 u32 hotkey_poll_active:1;
271 } tp_features;
273 static struct {
274 u16 hotkey_mask_ff:1;
275 u16 bright_cmos_ec_unsync:1;
276 } tp_warned;
278 struct thinkpad_id_data {
279 unsigned int vendor; /* ThinkPad vendor:
280 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
282 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
283 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
285 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
286 u16 ec_model;
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
293 static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 /* Special LED class that can defer work */
305 struct tpacpi_led_classdev {
306 struct led_classdev led_classdev;
307 struct work_struct work;
308 enum led_brightness new_brightness;
309 unsigned int led;
312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
313 static int dbg_wlswemul;
314 static int tpacpi_wlsw_emulstate;
315 static int dbg_bluetoothemul;
316 static int tpacpi_bluetooth_emulstate;
317 static int dbg_wwanemul;
318 static int tpacpi_wwan_emulstate;
319 #endif
322 /****************************************************************************
323 ****************************************************************************
325 * ACPI Helpers and device model
327 ****************************************************************************
328 ****************************************************************************/
330 /*************************************************************************
331 * ACPI basic handles
334 static acpi_handle root_handle;
336 #define TPACPI_HANDLE(object, parent, paths...) \
337 static acpi_handle object##_handle; \
338 static acpi_handle *object##_parent = &parent##_handle; \
339 static char *object##_path; \
340 static char *object##_paths[] = { paths }
342 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
343 "\\_SB.PCI.ISA.EC", /* 570 */
344 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
345 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
346 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
347 "\\_SB.PCI0.ICH3.EC0", /* R31 */
348 "\\_SB.PCI0.LPC.EC", /* all others */
351 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
352 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
354 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
355 /* T4x, X31, X40 */
356 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
357 "\\CMS", /* R40, R40e */
358 ); /* all others */
360 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
361 "^HKEY", /* R30, R31 */
362 "HKEY", /* all others */
363 ); /* 570 */
365 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
366 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
367 "\\_SB.PCI0.VID0", /* 770e */
368 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
369 "\\_SB.PCI0.AGP.VID", /* all others */
370 ); /* R30, R31 */
373 /*************************************************************************
374 * ACPI helpers
377 static int acpi_evalf(acpi_handle handle,
378 void *res, char *method, char *fmt, ...)
380 char *fmt0 = fmt;
381 struct acpi_object_list params;
382 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
383 struct acpi_buffer result, *resultp;
384 union acpi_object out_obj;
385 acpi_status status;
386 va_list ap;
387 char res_type;
388 int success;
389 int quiet;
391 if (!*fmt) {
392 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
393 return 0;
396 if (*fmt == 'q') {
397 quiet = 1;
398 fmt++;
399 } else
400 quiet = 0;
402 res_type = *(fmt++);
404 params.count = 0;
405 params.pointer = &in_objs[0];
407 va_start(ap, fmt);
408 while (*fmt) {
409 char c = *(fmt++);
410 switch (c) {
411 case 'd': /* int */
412 in_objs[params.count].integer.value = va_arg(ap, int);
413 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
414 break;
415 /* add more types as needed */
416 default:
417 printk(TPACPI_ERR "acpi_evalf() called "
418 "with invalid format character '%c'\n", c);
419 return 0;
422 va_end(ap);
424 if (res_type != 'v') {
425 result.length = sizeof(out_obj);
426 result.pointer = &out_obj;
427 resultp = &result;
428 } else
429 resultp = NULL;
431 status = acpi_evaluate_object(handle, method, &params, resultp);
433 switch (res_type) {
434 case 'd': /* int */
435 if (res)
436 *(int *)res = out_obj.integer.value;
437 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
438 break;
439 case 'v': /* void */
440 success = status == AE_OK;
441 break;
442 /* add more types as needed */
443 default:
444 printk(TPACPI_ERR "acpi_evalf() called "
445 "with invalid format character '%c'\n", res_type);
446 return 0;
449 if (!success && !quiet)
450 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
451 method, fmt0, status);
453 return success;
456 static int acpi_ec_read(int i, u8 *p)
458 int v;
460 if (ecrd_handle) {
461 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
462 return 0;
463 *p = v;
464 } else {
465 if (ec_read(i, p) < 0)
466 return 0;
469 return 1;
472 static int acpi_ec_write(int i, u8 v)
474 if (ecwr_handle) {
475 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
476 return 0;
477 } else {
478 if (ec_write(i, v) < 0)
479 return 0;
482 return 1;
485 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
486 static int _sta(acpi_handle handle)
488 int status;
490 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
491 status = 0;
493 return status;
495 #endif
497 static int issue_thinkpad_cmos_command(int cmos_cmd)
499 if (!cmos_handle)
500 return -ENXIO;
502 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
503 return -EIO;
505 return 0;
508 /*************************************************************************
509 * ACPI device model
512 #define TPACPI_ACPIHANDLE_INIT(object) \
513 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
514 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
516 static void drv_acpi_handle_init(char *name,
517 acpi_handle *handle, acpi_handle parent,
518 char **paths, int num_paths, char **path)
520 int i;
521 acpi_status status;
523 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
524 name);
526 for (i = 0; i < num_paths; i++) {
527 status = acpi_get_handle(parent, paths[i], handle);
528 if (ACPI_SUCCESS(status)) {
529 *path = paths[i];
530 dbg_printk(TPACPI_DBG_INIT,
531 "Found ACPI handle %s for %s\n",
532 *path, name);
533 return;
537 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
538 name);
539 *handle = NULL;
542 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
544 struct ibm_struct *ibm = data;
546 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
547 return;
549 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
550 return;
552 ibm->acpi->notify(ibm, event);
555 static int __init setup_acpi_notify(struct ibm_struct *ibm)
557 acpi_status status;
558 int rc;
560 BUG_ON(!ibm->acpi);
562 if (!*ibm->acpi->handle)
563 return 0;
565 vdbg_printk(TPACPI_DBG_INIT,
566 "setting up ACPI notify for %s\n", ibm->name);
568 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
569 if (rc < 0) {
570 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
571 ibm->name, rc);
572 return -ENODEV;
575 ibm->acpi->device->driver_data = ibm;
576 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
577 TPACPI_ACPI_EVENT_PREFIX,
578 ibm->name);
580 status = acpi_install_notify_handler(*ibm->acpi->handle,
581 ibm->acpi->type, dispatch_acpi_notify, ibm);
582 if (ACPI_FAILURE(status)) {
583 if (status == AE_ALREADY_EXISTS) {
584 printk(TPACPI_NOTICE
585 "another device driver is already "
586 "handling %s events\n", ibm->name);
587 } else {
588 printk(TPACPI_ERR
589 "acpi_install_notify_handler(%s) failed: %d\n",
590 ibm->name, status);
592 return -ENODEV;
594 ibm->flags.acpi_notify_installed = 1;
595 return 0;
598 static int __init tpacpi_device_add(struct acpi_device *device)
600 return 0;
603 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
605 int rc;
607 dbg_printk(TPACPI_DBG_INIT,
608 "registering %s as an ACPI driver\n", ibm->name);
610 BUG_ON(!ibm->acpi);
612 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
613 if (!ibm->acpi->driver) {
614 printk(TPACPI_ERR
615 "failed to allocate memory for ibm->acpi->driver\n");
616 return -ENOMEM;
619 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
620 ibm->acpi->driver->ids = ibm->acpi->hid;
622 ibm->acpi->driver->ops.add = &tpacpi_device_add;
624 rc = acpi_bus_register_driver(ibm->acpi->driver);
625 if (rc < 0) {
626 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
627 ibm->name, rc);
628 kfree(ibm->acpi->driver);
629 ibm->acpi->driver = NULL;
630 } else if (!rc)
631 ibm->flags.acpi_driver_registered = 1;
633 return rc;
637 /****************************************************************************
638 ****************************************************************************
640 * Procfs Helpers
642 ****************************************************************************
643 ****************************************************************************/
645 static int dispatch_procfs_read(char *page, char **start, off_t off,
646 int count, int *eof, void *data)
648 struct ibm_struct *ibm = data;
649 int len;
651 if (!ibm || !ibm->read)
652 return -EINVAL;
654 len = ibm->read(page);
655 if (len < 0)
656 return len;
658 if (len <= off + count)
659 *eof = 1;
660 *start = page + off;
661 len -= off;
662 if (len > count)
663 len = count;
664 if (len < 0)
665 len = 0;
667 return len;
670 static int dispatch_procfs_write(struct file *file,
671 const char __user *userbuf,
672 unsigned long count, void *data)
674 struct ibm_struct *ibm = data;
675 char *kernbuf;
676 int ret;
678 if (!ibm || !ibm->write)
679 return -EINVAL;
681 kernbuf = kmalloc(count + 2, GFP_KERNEL);
682 if (!kernbuf)
683 return -ENOMEM;
685 if (copy_from_user(kernbuf, userbuf, count)) {
686 kfree(kernbuf);
687 return -EFAULT;
690 kernbuf[count] = 0;
691 strcat(kernbuf, ",");
692 ret = ibm->write(kernbuf);
693 if (ret == 0)
694 ret = count;
696 kfree(kernbuf);
698 return ret;
701 static char *next_cmd(char **cmds)
703 char *start = *cmds;
704 char *end;
706 while ((end = strchr(start, ',')) && end == start)
707 start = end + 1;
709 if (!end)
710 return NULL;
712 *end = 0;
713 *cmds = end + 1;
714 return start;
718 /****************************************************************************
719 ****************************************************************************
721 * Device model: input, hwmon and platform
723 ****************************************************************************
724 ****************************************************************************/
726 static struct platform_device *tpacpi_pdev;
727 static struct platform_device *tpacpi_sensors_pdev;
728 static struct device *tpacpi_hwmon;
729 static struct input_dev *tpacpi_inputdev;
730 static struct mutex tpacpi_inputdev_send_mutex;
731 static LIST_HEAD(tpacpi_all_drivers);
733 static int tpacpi_suspend_handler(struct platform_device *pdev,
734 pm_message_t state)
736 struct ibm_struct *ibm, *itmp;
738 list_for_each_entry_safe(ibm, itmp,
739 &tpacpi_all_drivers,
740 all_drivers) {
741 if (ibm->suspend)
742 (ibm->suspend)(state);
745 return 0;
748 static int tpacpi_resume_handler(struct platform_device *pdev)
750 struct ibm_struct *ibm, *itmp;
752 list_for_each_entry_safe(ibm, itmp,
753 &tpacpi_all_drivers,
754 all_drivers) {
755 if (ibm->resume)
756 (ibm->resume)();
759 return 0;
762 static struct platform_driver tpacpi_pdriver = {
763 .driver = {
764 .name = TPACPI_DRVR_NAME,
765 .owner = THIS_MODULE,
767 .suspend = tpacpi_suspend_handler,
768 .resume = tpacpi_resume_handler,
771 static struct platform_driver tpacpi_hwmon_pdriver = {
772 .driver = {
773 .name = TPACPI_HWMON_DRVR_NAME,
774 .owner = THIS_MODULE,
778 /*************************************************************************
779 * sysfs support helpers
782 struct attribute_set {
783 unsigned int members, max_members;
784 struct attribute_group group;
787 struct attribute_set_obj {
788 struct attribute_set s;
789 struct attribute *a;
790 } __attribute__((packed));
792 static struct attribute_set *create_attr_set(unsigned int max_members,
793 const char *name)
795 struct attribute_set_obj *sobj;
797 if (max_members == 0)
798 return NULL;
800 /* Allocates space for implicit NULL at the end too */
801 sobj = kzalloc(sizeof(struct attribute_set_obj) +
802 max_members * sizeof(struct attribute *),
803 GFP_KERNEL);
804 if (!sobj)
805 return NULL;
806 sobj->s.max_members = max_members;
807 sobj->s.group.attrs = &sobj->a;
808 sobj->s.group.name = name;
810 return &sobj->s;
813 #define destroy_attr_set(_set) \
814 kfree(_set);
816 /* not multi-threaded safe, use it in a single thread per set */
817 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
819 if (!s || !attr)
820 return -EINVAL;
822 if (s->members >= s->max_members)
823 return -ENOMEM;
825 s->group.attrs[s->members] = attr;
826 s->members++;
828 return 0;
831 static int add_many_to_attr_set(struct attribute_set *s,
832 struct attribute **attr,
833 unsigned int count)
835 int i, res;
837 for (i = 0; i < count; i++) {
838 res = add_to_attr_set(s, attr[i]);
839 if (res)
840 return res;
843 return 0;
846 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
848 sysfs_remove_group(kobj, &s->group);
849 destroy_attr_set(s);
852 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
853 sysfs_create_group(_kobj, &_attr_set->group)
855 static int parse_strtoul(const char *buf,
856 unsigned long max, unsigned long *value)
858 char *endp;
860 while (*buf && isspace(*buf))
861 buf++;
862 *value = simple_strtoul(buf, &endp, 0);
863 while (*endp && isspace(*endp))
864 endp++;
865 if (*endp || *value > max)
866 return -EINVAL;
868 return 0;
871 static void tpacpi_disable_brightness_delay(void)
873 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
874 printk(TPACPI_NOTICE
875 "ACPI backlight control delay disabled\n");
878 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
880 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
881 union acpi_object *obj;
882 int rc;
884 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
885 obj = (union acpi_object *)buffer.pointer;
886 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
887 printk(TPACPI_ERR "Unknown _BCL data, "
888 "please report this to %s\n", TPACPI_MAIL);
889 rc = 0;
890 } else {
891 rc = obj->package.count;
893 } else {
894 return 0;
897 kfree(buffer.pointer);
898 return rc;
901 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
902 u32 lvl, void *context, void **rv)
904 char name[ACPI_PATH_SEGMENT_LENGTH];
905 struct acpi_buffer buffer = { sizeof(name), &name };
907 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
908 !strncmp("_BCL", name, sizeof(name) - 1)) {
909 BUG_ON(!rv || !*rv);
910 **(int **)rv = tpacpi_query_bcl_levels(handle);
911 return AE_CTRL_TERMINATE;
912 } else {
913 return AE_OK;
918 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
920 static int __init tpacpi_check_std_acpi_brightness_support(void)
922 int status;
923 int bcl_levels = 0;
924 void *bcl_ptr = &bcl_levels;
926 if (!vid_handle) {
927 TPACPI_ACPIHANDLE_INIT(vid);
929 if (!vid_handle)
930 return 0;
933 * Search for a _BCL method, and execute it. This is safe on all
934 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
935 * BIOS in ACPI backlight control mode. We do NOT have to care
936 * about calling the _BCL method in an enabled video device, any
937 * will do for our purposes.
940 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
941 tpacpi_acpi_walk_find_bcl, NULL,
942 &bcl_ptr);
944 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
945 tp_features.bright_acpimode = 1;
946 return (bcl_levels - 2);
949 return 0;
952 static int __init tpacpi_new_rfkill(const unsigned int id,
953 struct rfkill **rfk,
954 const enum rfkill_type rfktype,
955 const char *name,
956 int (*toggle_radio)(void *, enum rfkill_state),
957 int (*get_state)(void *, enum rfkill_state *))
959 int res;
960 enum rfkill_state initial_state;
962 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
963 if (!*rfk) {
964 printk(TPACPI_ERR
965 "failed to allocate memory for rfkill class\n");
966 return -ENOMEM;
969 (*rfk)->name = name;
970 (*rfk)->get_state = get_state;
971 (*rfk)->toggle_radio = toggle_radio;
973 if (!get_state(NULL, &initial_state))
974 (*rfk)->state = initial_state;
976 res = rfkill_register(*rfk);
977 if (res < 0) {
978 printk(TPACPI_ERR
979 "failed to register %s rfkill switch: %d\n",
980 name, res);
981 rfkill_free(*rfk);
982 *rfk = NULL;
983 return res;
986 return 0;
989 /*************************************************************************
990 * thinkpad-acpi driver attributes
993 /* interface_version --------------------------------------------------- */
994 static ssize_t tpacpi_driver_interface_version_show(
995 struct device_driver *drv,
996 char *buf)
998 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1001 static DRIVER_ATTR(interface_version, S_IRUGO,
1002 tpacpi_driver_interface_version_show, NULL);
1004 /* debug_level --------------------------------------------------------- */
1005 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1006 char *buf)
1008 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1011 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1012 const char *buf, size_t count)
1014 unsigned long t;
1016 if (parse_strtoul(buf, 0xffff, &t))
1017 return -EINVAL;
1019 dbg_level = t;
1021 return count;
1024 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1025 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1027 /* version ------------------------------------------------------------- */
1028 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1029 char *buf)
1031 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1032 TPACPI_DESC, TPACPI_VERSION);
1035 static DRIVER_ATTR(version, S_IRUGO,
1036 tpacpi_driver_version_show, NULL);
1038 /* --------------------------------------------------------------------- */
1040 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1042 static void tpacpi_send_radiosw_update(void);
1044 /* wlsw_emulstate ------------------------------------------------------ */
1045 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1046 char *buf)
1048 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1051 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1052 const char *buf, size_t count)
1054 unsigned long t;
1056 if (parse_strtoul(buf, 1, &t))
1057 return -EINVAL;
1059 if (tpacpi_wlsw_emulstate != t) {
1060 tpacpi_wlsw_emulstate = !!t;
1061 tpacpi_send_radiosw_update();
1062 } else
1063 tpacpi_wlsw_emulstate = !!t;
1065 return count;
1068 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1069 tpacpi_driver_wlsw_emulstate_show,
1070 tpacpi_driver_wlsw_emulstate_store);
1072 /* bluetooth_emulstate ------------------------------------------------- */
1073 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1074 struct device_driver *drv,
1075 char *buf)
1077 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1080 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1081 struct device_driver *drv,
1082 const char *buf, size_t count)
1084 unsigned long t;
1086 if (parse_strtoul(buf, 1, &t))
1087 return -EINVAL;
1089 tpacpi_bluetooth_emulstate = !!t;
1091 return count;
1094 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1095 tpacpi_driver_bluetooth_emulstate_show,
1096 tpacpi_driver_bluetooth_emulstate_store);
1098 /* wwan_emulstate ------------------------------------------------- */
1099 static ssize_t tpacpi_driver_wwan_emulstate_show(
1100 struct device_driver *drv,
1101 char *buf)
1103 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1106 static ssize_t tpacpi_driver_wwan_emulstate_store(
1107 struct device_driver *drv,
1108 const char *buf, size_t count)
1110 unsigned long t;
1112 if (parse_strtoul(buf, 1, &t))
1113 return -EINVAL;
1115 tpacpi_wwan_emulstate = !!t;
1117 return count;
1120 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1121 tpacpi_driver_wwan_emulstate_show,
1122 tpacpi_driver_wwan_emulstate_store);
1124 #endif
1126 /* --------------------------------------------------------------------- */
1128 static struct driver_attribute *tpacpi_driver_attributes[] = {
1129 &driver_attr_debug_level, &driver_attr_version,
1130 &driver_attr_interface_version,
1133 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1135 int i, res;
1137 i = 0;
1138 res = 0;
1139 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1140 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1141 i++;
1144 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1145 if (!res && dbg_wlswemul)
1146 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1147 if (!res && dbg_bluetoothemul)
1148 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1149 if (!res && dbg_wwanemul)
1150 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1151 #endif
1153 return res;
1156 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1158 int i;
1160 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1161 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1163 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1164 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1165 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1166 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1167 #endif
1170 /****************************************************************************
1171 ****************************************************************************
1173 * Subdrivers
1175 ****************************************************************************
1176 ****************************************************************************/
1178 /*************************************************************************
1179 * thinkpad-acpi init subdriver
1182 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1184 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1185 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1187 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1188 (thinkpad_id.bios_version_str) ?
1189 thinkpad_id.bios_version_str : "unknown",
1190 (thinkpad_id.ec_version_str) ?
1191 thinkpad_id.ec_version_str : "unknown");
1193 if (thinkpad_id.vendor && thinkpad_id.model_str)
1194 printk(TPACPI_INFO "%s %s, model %s\n",
1195 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1196 "IBM" : ((thinkpad_id.vendor ==
1197 PCI_VENDOR_ID_LENOVO) ?
1198 "Lenovo" : "Unknown vendor"),
1199 thinkpad_id.model_str,
1200 (thinkpad_id.nummodel_str) ?
1201 thinkpad_id.nummodel_str : "unknown");
1203 return 0;
1206 static int thinkpad_acpi_driver_read(char *p)
1208 int len = 0;
1210 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1211 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1213 return len;
1216 static struct ibm_struct thinkpad_acpi_driver_data = {
1217 .name = "driver",
1218 .read = thinkpad_acpi_driver_read,
1221 /*************************************************************************
1222 * Hotkey subdriver
1225 enum { /* hot key scan codes (derived from ACPI DSDT) */
1226 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1227 TP_ACPI_HOTKEYSCAN_FNF2,
1228 TP_ACPI_HOTKEYSCAN_FNF3,
1229 TP_ACPI_HOTKEYSCAN_FNF4,
1230 TP_ACPI_HOTKEYSCAN_FNF5,
1231 TP_ACPI_HOTKEYSCAN_FNF6,
1232 TP_ACPI_HOTKEYSCAN_FNF7,
1233 TP_ACPI_HOTKEYSCAN_FNF8,
1234 TP_ACPI_HOTKEYSCAN_FNF9,
1235 TP_ACPI_HOTKEYSCAN_FNF10,
1236 TP_ACPI_HOTKEYSCAN_FNF11,
1237 TP_ACPI_HOTKEYSCAN_FNF12,
1238 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1239 TP_ACPI_HOTKEYSCAN_FNINSERT,
1240 TP_ACPI_HOTKEYSCAN_FNDELETE,
1241 TP_ACPI_HOTKEYSCAN_FNHOME,
1242 TP_ACPI_HOTKEYSCAN_FNEND,
1243 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1244 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1245 TP_ACPI_HOTKEYSCAN_FNSPACE,
1246 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1247 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1248 TP_ACPI_HOTKEYSCAN_MUTE,
1249 TP_ACPI_HOTKEYSCAN_THINKPAD,
1252 enum { /* Keys available through NVRAM polling */
1253 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1254 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1257 enum { /* Positions of some of the keys in hotkey masks */
1258 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1259 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1260 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1261 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1262 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1263 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1264 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1265 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1266 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1267 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1268 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1271 enum { /* NVRAM to ACPI HKEY group map */
1272 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1273 TP_ACPI_HKEY_ZOOM_MASK |
1274 TP_ACPI_HKEY_DISPSWTCH_MASK |
1275 TP_ACPI_HKEY_HIBERNATE_MASK,
1276 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1277 TP_ACPI_HKEY_BRGHTDWN_MASK,
1278 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1279 TP_ACPI_HKEY_VOLDWN_MASK |
1280 TP_ACPI_HKEY_MUTE_MASK,
1283 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1284 struct tp_nvram_state {
1285 u16 thinkpad_toggle:1;
1286 u16 zoom_toggle:1;
1287 u16 display_toggle:1;
1288 u16 thinklight_toggle:1;
1289 u16 hibernate_toggle:1;
1290 u16 displayexp_toggle:1;
1291 u16 display_state:1;
1292 u16 brightness_toggle:1;
1293 u16 volume_toggle:1;
1294 u16 mute:1;
1296 u8 brightness_level;
1297 u8 volume_level;
1300 static struct task_struct *tpacpi_hotkey_task;
1301 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1302 static int hotkey_poll_freq = 10; /* Hz */
1303 static struct mutex hotkey_thread_mutex;
1304 static struct mutex hotkey_thread_data_mutex;
1305 static unsigned int hotkey_config_change;
1307 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1309 #define hotkey_source_mask 0U
1311 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1313 static struct mutex hotkey_mutex;
1315 static enum { /* Reasons for waking up */
1316 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1317 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1318 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1319 } hotkey_wakeup_reason;
1321 static int hotkey_autosleep_ack;
1323 static int hotkey_orig_status;
1324 static u32 hotkey_orig_mask;
1325 static u32 hotkey_all_mask;
1326 static u32 hotkey_reserved_mask;
1327 static u32 hotkey_mask;
1329 static unsigned int hotkey_report_mode;
1331 static u16 *hotkey_keycode_map;
1333 static struct attribute_set *hotkey_dev_attributes;
1335 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1336 #define HOTKEY_CONFIG_CRITICAL_START \
1337 do { \
1338 mutex_lock(&hotkey_thread_data_mutex); \
1339 hotkey_config_change++; \
1340 } while (0);
1341 #define HOTKEY_CONFIG_CRITICAL_END \
1342 mutex_unlock(&hotkey_thread_data_mutex);
1343 #else
1344 #define HOTKEY_CONFIG_CRITICAL_START
1345 #define HOTKEY_CONFIG_CRITICAL_END
1346 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1348 /* HKEY.MHKG() return bits */
1349 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1351 static int hotkey_get_wlsw(int *status)
1353 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1354 if (dbg_wlswemul) {
1355 *status = !!tpacpi_wlsw_emulstate;
1356 return 0;
1358 #endif
1359 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1360 return -EIO;
1361 return 0;
1364 static int hotkey_get_tablet_mode(int *status)
1366 int s;
1368 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1369 return -EIO;
1371 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1372 return 0;
1376 * Call with hotkey_mutex held
1378 static int hotkey_mask_get(void)
1380 u32 m = 0;
1382 if (tp_features.hotkey_mask) {
1383 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1384 return -EIO;
1386 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1388 return 0;
1392 * Call with hotkey_mutex held
1394 static int hotkey_mask_set(u32 mask)
1396 int i;
1397 int rc = 0;
1399 if (tp_features.hotkey_mask) {
1400 if (!tp_warned.hotkey_mask_ff &&
1401 (mask == 0xffff || mask == 0xffffff ||
1402 mask == 0xffffffff)) {
1403 tp_warned.hotkey_mask_ff = 1;
1404 printk(TPACPI_NOTICE
1405 "setting the hotkey mask to 0x%08x is likely "
1406 "not the best way to go about it\n", mask);
1407 printk(TPACPI_NOTICE
1408 "please consider using the driver defaults, "
1409 "and refer to up-to-date thinkpad-acpi "
1410 "documentation\n");
1413 HOTKEY_CONFIG_CRITICAL_START
1414 for (i = 0; i < 32; i++) {
1415 u32 m = 1 << i;
1416 /* enable in firmware mask only keys not in NVRAM
1417 * mode, but enable the key in the cached hotkey_mask
1418 * regardless of mode, or the key will end up
1419 * disabled by hotkey_mask_get() */
1420 if (!acpi_evalf(hkey_handle,
1421 NULL, "MHKM", "vdd", i + 1,
1422 !!((mask & ~hotkey_source_mask) & m))) {
1423 rc = -EIO;
1424 break;
1425 } else {
1426 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1429 HOTKEY_CONFIG_CRITICAL_END
1431 /* hotkey_mask_get must be called unconditionally below */
1432 if (!hotkey_mask_get() && !rc &&
1433 (hotkey_mask & ~hotkey_source_mask) !=
1434 (mask & ~hotkey_source_mask)) {
1435 printk(TPACPI_NOTICE
1436 "requested hot key mask 0x%08x, but "
1437 "firmware forced it to 0x%08x\n",
1438 mask, hotkey_mask);
1440 } else {
1441 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1442 HOTKEY_CONFIG_CRITICAL_START
1443 hotkey_mask = mask & hotkey_source_mask;
1444 HOTKEY_CONFIG_CRITICAL_END
1445 hotkey_mask_get();
1446 if (hotkey_mask != mask) {
1447 printk(TPACPI_NOTICE
1448 "requested hot key mask 0x%08x, "
1449 "forced to 0x%08x (NVRAM poll mask is "
1450 "0x%08x): no firmware mask support\n",
1451 mask, hotkey_mask, hotkey_source_mask);
1453 #else
1454 hotkey_mask_get();
1455 rc = -ENXIO;
1456 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1459 return rc;
1462 static int hotkey_status_get(int *status)
1464 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1465 return -EIO;
1467 return 0;
1470 static int hotkey_status_set(int status)
1472 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1473 return -EIO;
1475 return 0;
1478 static void tpacpi_input_send_tabletsw(void)
1480 int state;
1482 if (tp_features.hotkey_tablet &&
1483 !hotkey_get_tablet_mode(&state)) {
1484 mutex_lock(&tpacpi_inputdev_send_mutex);
1486 input_report_switch(tpacpi_inputdev,
1487 SW_TABLET_MODE, !!state);
1488 input_sync(tpacpi_inputdev);
1490 mutex_unlock(&tpacpi_inputdev_send_mutex);
1494 static void tpacpi_input_send_key(unsigned int scancode)
1496 unsigned int keycode;
1498 keycode = hotkey_keycode_map[scancode];
1500 if (keycode != KEY_RESERVED) {
1501 mutex_lock(&tpacpi_inputdev_send_mutex);
1503 input_report_key(tpacpi_inputdev, keycode, 1);
1504 if (keycode == KEY_UNKNOWN)
1505 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1506 scancode);
1507 input_sync(tpacpi_inputdev);
1509 input_report_key(tpacpi_inputdev, keycode, 0);
1510 if (keycode == KEY_UNKNOWN)
1511 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1512 scancode);
1513 input_sync(tpacpi_inputdev);
1515 mutex_unlock(&tpacpi_inputdev_send_mutex);
1519 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1520 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1522 static void tpacpi_hotkey_send_key(unsigned int scancode)
1524 tpacpi_input_send_key(scancode);
1525 if (hotkey_report_mode < 2) {
1526 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1527 0x80, 0x1001 + scancode);
1531 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1533 u8 d;
1535 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1536 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1537 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1538 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1539 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1540 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1542 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1543 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1544 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1546 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1547 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1548 n->displayexp_toggle =
1549 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1551 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1552 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1553 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1554 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1555 n->brightness_toggle =
1556 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1558 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1559 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1560 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1561 >> TP_NVRAM_POS_LEVEL_VOLUME;
1562 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1563 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1567 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1568 do { \
1569 if ((mask & (1 << __scancode)) && \
1570 oldn->__member != newn->__member) \
1571 tpacpi_hotkey_send_key(__scancode); \
1572 } while (0)
1574 #define TPACPI_MAY_SEND_KEY(__scancode) \
1575 do { if (mask & (1 << __scancode)) \
1576 tpacpi_hotkey_send_key(__scancode); } while (0)
1578 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1579 struct tp_nvram_state *newn,
1580 u32 mask)
1582 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1583 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1584 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1585 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1587 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1589 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1591 /* handle volume */
1592 if (oldn->volume_toggle != newn->volume_toggle) {
1593 if (oldn->mute != newn->mute) {
1594 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1596 if (oldn->volume_level > newn->volume_level) {
1597 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1598 } else if (oldn->volume_level < newn->volume_level) {
1599 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1600 } else if (oldn->mute == newn->mute) {
1601 /* repeated key presses that didn't change state */
1602 if (newn->mute) {
1603 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1604 } else if (newn->volume_level != 0) {
1605 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1606 } else {
1607 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1612 /* handle brightness */
1613 if (oldn->brightness_toggle != newn->brightness_toggle) {
1614 if (oldn->brightness_level < newn->brightness_level) {
1615 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1616 } else if (oldn->brightness_level > newn->brightness_level) {
1617 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1618 } else {
1619 /* repeated key presses that didn't change state */
1620 if (newn->brightness_level != 0) {
1621 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1622 } else {
1623 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1629 #undef TPACPI_COMPARE_KEY
1630 #undef TPACPI_MAY_SEND_KEY
1632 static int hotkey_kthread(void *data)
1634 struct tp_nvram_state s[2];
1635 u32 mask;
1636 unsigned int si, so;
1637 unsigned long t;
1638 unsigned int change_detector, must_reset;
1640 mutex_lock(&hotkey_thread_mutex);
1642 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1643 goto exit;
1645 set_freezable();
1647 so = 0;
1648 si = 1;
1649 t = 0;
1651 /* Initial state for compares */
1652 mutex_lock(&hotkey_thread_data_mutex);
1653 change_detector = hotkey_config_change;
1654 mask = hotkey_source_mask & hotkey_mask;
1655 mutex_unlock(&hotkey_thread_data_mutex);
1656 hotkey_read_nvram(&s[so], mask);
1658 while (!kthread_should_stop() && hotkey_poll_freq) {
1659 if (t == 0)
1660 t = 1000/hotkey_poll_freq;
1661 t = msleep_interruptible(t);
1662 if (unlikely(kthread_should_stop()))
1663 break;
1664 must_reset = try_to_freeze();
1665 if (t > 0 && !must_reset)
1666 continue;
1668 mutex_lock(&hotkey_thread_data_mutex);
1669 if (must_reset || hotkey_config_change != change_detector) {
1670 /* forget old state on thaw or config change */
1671 si = so;
1672 t = 0;
1673 change_detector = hotkey_config_change;
1675 mask = hotkey_source_mask & hotkey_mask;
1676 mutex_unlock(&hotkey_thread_data_mutex);
1678 if (likely(mask)) {
1679 hotkey_read_nvram(&s[si], mask);
1680 if (likely(si != so)) {
1681 hotkey_compare_and_issue_event(&s[so], &s[si],
1682 mask);
1686 so = si;
1687 si ^= 1;
1690 exit:
1691 mutex_unlock(&hotkey_thread_mutex);
1692 return 0;
1695 static void hotkey_poll_stop_sync(void)
1697 if (tpacpi_hotkey_task) {
1698 if (frozen(tpacpi_hotkey_task) ||
1699 freezing(tpacpi_hotkey_task))
1700 thaw_process(tpacpi_hotkey_task);
1702 kthread_stop(tpacpi_hotkey_task);
1703 tpacpi_hotkey_task = NULL;
1704 mutex_lock(&hotkey_thread_mutex);
1705 /* at this point, the thread did exit */
1706 mutex_unlock(&hotkey_thread_mutex);
1710 /* call with hotkey_mutex held */
1711 static void hotkey_poll_setup(int may_warn)
1713 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1714 hotkey_poll_freq > 0 &&
1715 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1716 if (!tpacpi_hotkey_task) {
1717 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1718 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1719 if (IS_ERR(tpacpi_hotkey_task)) {
1720 tpacpi_hotkey_task = NULL;
1721 printk(TPACPI_ERR
1722 "could not create kernel thread "
1723 "for hotkey polling\n");
1726 } else {
1727 hotkey_poll_stop_sync();
1728 if (may_warn &&
1729 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1730 printk(TPACPI_NOTICE
1731 "hot keys 0x%08x require polling, "
1732 "which is currently disabled\n",
1733 hotkey_source_mask);
1738 static void hotkey_poll_setup_safe(int may_warn)
1740 mutex_lock(&hotkey_mutex);
1741 hotkey_poll_setup(may_warn);
1742 mutex_unlock(&hotkey_mutex);
1745 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1747 static void hotkey_poll_setup_safe(int __unused)
1751 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1753 static int hotkey_inputdev_open(struct input_dev *dev)
1755 switch (tpacpi_lifecycle) {
1756 case TPACPI_LIFE_INIT:
1758 * hotkey_init will call hotkey_poll_setup_safe
1759 * at the appropriate moment
1761 return 0;
1762 case TPACPI_LIFE_EXITING:
1763 return -EBUSY;
1764 case TPACPI_LIFE_RUNNING:
1765 hotkey_poll_setup_safe(0);
1766 return 0;
1769 /* Should only happen if tpacpi_lifecycle is corrupt */
1770 BUG();
1771 return -EBUSY;
1774 static void hotkey_inputdev_close(struct input_dev *dev)
1776 /* disable hotkey polling when possible */
1777 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1778 hotkey_poll_setup_safe(0);
1781 /* sysfs hotkey enable ------------------------------------------------- */
1782 static ssize_t hotkey_enable_show(struct device *dev,
1783 struct device_attribute *attr,
1784 char *buf)
1786 int res, status;
1788 res = hotkey_status_get(&status);
1789 if (res)
1790 return res;
1792 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1795 static ssize_t hotkey_enable_store(struct device *dev,
1796 struct device_attribute *attr,
1797 const char *buf, size_t count)
1799 unsigned long t;
1800 int res;
1802 if (parse_strtoul(buf, 1, &t))
1803 return -EINVAL;
1805 res = hotkey_status_set(t);
1807 return (res) ? res : count;
1810 static struct device_attribute dev_attr_hotkey_enable =
1811 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1812 hotkey_enable_show, hotkey_enable_store);
1814 /* sysfs hotkey mask --------------------------------------------------- */
1815 static ssize_t hotkey_mask_show(struct device *dev,
1816 struct device_attribute *attr,
1817 char *buf)
1819 int res;
1821 if (mutex_lock_interruptible(&hotkey_mutex))
1822 return -ERESTARTSYS;
1823 res = hotkey_mask_get();
1824 mutex_unlock(&hotkey_mutex);
1826 return (res)?
1827 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1830 static ssize_t hotkey_mask_store(struct device *dev,
1831 struct device_attribute *attr,
1832 const char *buf, size_t count)
1834 unsigned long t;
1835 int res;
1837 if (parse_strtoul(buf, 0xffffffffUL, &t))
1838 return -EINVAL;
1840 if (mutex_lock_interruptible(&hotkey_mutex))
1841 return -ERESTARTSYS;
1843 res = hotkey_mask_set(t);
1845 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1846 hotkey_poll_setup(1);
1847 #endif
1849 mutex_unlock(&hotkey_mutex);
1851 return (res) ? res : count;
1854 static struct device_attribute dev_attr_hotkey_mask =
1855 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1856 hotkey_mask_show, hotkey_mask_store);
1858 /* sysfs hotkey bios_enabled ------------------------------------------- */
1859 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1860 struct device_attribute *attr,
1861 char *buf)
1863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1866 static struct device_attribute dev_attr_hotkey_bios_enabled =
1867 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1869 /* sysfs hotkey bios_mask ---------------------------------------------- */
1870 static ssize_t hotkey_bios_mask_show(struct device *dev,
1871 struct device_attribute *attr,
1872 char *buf)
1874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1877 static struct device_attribute dev_attr_hotkey_bios_mask =
1878 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1880 /* sysfs hotkey all_mask ----------------------------------------------- */
1881 static ssize_t hotkey_all_mask_show(struct device *dev,
1882 struct device_attribute *attr,
1883 char *buf)
1885 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1886 hotkey_all_mask | hotkey_source_mask);
1889 static struct device_attribute dev_attr_hotkey_all_mask =
1890 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1892 /* sysfs hotkey recommended_mask --------------------------------------- */
1893 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1894 struct device_attribute *attr,
1895 char *buf)
1897 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1898 (hotkey_all_mask | hotkey_source_mask)
1899 & ~hotkey_reserved_mask);
1902 static struct device_attribute dev_attr_hotkey_recommended_mask =
1903 __ATTR(hotkey_recommended_mask, S_IRUGO,
1904 hotkey_recommended_mask_show, NULL);
1906 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1908 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1909 static ssize_t hotkey_source_mask_show(struct device *dev,
1910 struct device_attribute *attr,
1911 char *buf)
1913 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1916 static ssize_t hotkey_source_mask_store(struct device *dev,
1917 struct device_attribute *attr,
1918 const char *buf, size_t count)
1920 unsigned long t;
1922 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1923 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1924 return -EINVAL;
1926 if (mutex_lock_interruptible(&hotkey_mutex))
1927 return -ERESTARTSYS;
1929 HOTKEY_CONFIG_CRITICAL_START
1930 hotkey_source_mask = t;
1931 HOTKEY_CONFIG_CRITICAL_END
1933 hotkey_poll_setup(1);
1935 mutex_unlock(&hotkey_mutex);
1937 return count;
1940 static struct device_attribute dev_attr_hotkey_source_mask =
1941 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1942 hotkey_source_mask_show, hotkey_source_mask_store);
1944 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1945 static ssize_t hotkey_poll_freq_show(struct device *dev,
1946 struct device_attribute *attr,
1947 char *buf)
1949 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1952 static ssize_t hotkey_poll_freq_store(struct device *dev,
1953 struct device_attribute *attr,
1954 const char *buf, size_t count)
1956 unsigned long t;
1958 if (parse_strtoul(buf, 25, &t))
1959 return -EINVAL;
1961 if (mutex_lock_interruptible(&hotkey_mutex))
1962 return -ERESTARTSYS;
1964 hotkey_poll_freq = t;
1966 hotkey_poll_setup(1);
1967 mutex_unlock(&hotkey_mutex);
1969 return count;
1972 static struct device_attribute dev_attr_hotkey_poll_freq =
1973 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1974 hotkey_poll_freq_show, hotkey_poll_freq_store);
1976 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1978 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1979 static ssize_t hotkey_radio_sw_show(struct device *dev,
1980 struct device_attribute *attr,
1981 char *buf)
1983 int res, s;
1984 res = hotkey_get_wlsw(&s);
1985 if (res < 0)
1986 return res;
1988 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1991 static struct device_attribute dev_attr_hotkey_radio_sw =
1992 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1994 static void hotkey_radio_sw_notify_change(void)
1996 if (tp_features.hotkey_wlsw)
1997 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1998 "hotkey_radio_sw");
2001 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2002 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2003 struct device_attribute *attr,
2004 char *buf)
2006 int res, s;
2007 res = hotkey_get_tablet_mode(&s);
2008 if (res < 0)
2009 return res;
2011 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2014 static struct device_attribute dev_attr_hotkey_tablet_mode =
2015 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2017 static void hotkey_tablet_mode_notify_change(void)
2019 if (tp_features.hotkey_tablet)
2020 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2021 "hotkey_tablet_mode");
2024 /* sysfs hotkey report_mode -------------------------------------------- */
2025 static ssize_t hotkey_report_mode_show(struct device *dev,
2026 struct device_attribute *attr,
2027 char *buf)
2029 return snprintf(buf, PAGE_SIZE, "%d\n",
2030 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2033 static struct device_attribute dev_attr_hotkey_report_mode =
2034 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2036 /* sysfs wakeup reason (pollable) -------------------------------------- */
2037 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2038 struct device_attribute *attr,
2039 char *buf)
2041 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2044 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2045 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2047 static void hotkey_wakeup_reason_notify_change(void)
2049 if (tp_features.hotkey_mask)
2050 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2051 "wakeup_reason");
2054 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2055 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2056 struct device_attribute *attr,
2057 char *buf)
2059 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2062 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2063 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2064 hotkey_wakeup_hotunplug_complete_show, NULL);
2066 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2068 if (tp_features.hotkey_mask)
2069 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2070 "wakeup_hotunplug_complete");
2073 /* --------------------------------------------------------------------- */
2075 static struct attribute *hotkey_attributes[] __initdata = {
2076 &dev_attr_hotkey_enable.attr,
2077 &dev_attr_hotkey_bios_enabled.attr,
2078 &dev_attr_hotkey_report_mode.attr,
2079 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2080 &dev_attr_hotkey_mask.attr,
2081 &dev_attr_hotkey_all_mask.attr,
2082 &dev_attr_hotkey_recommended_mask.attr,
2083 &dev_attr_hotkey_source_mask.attr,
2084 &dev_attr_hotkey_poll_freq.attr,
2085 #endif
2088 static struct attribute *hotkey_mask_attributes[] __initdata = {
2089 &dev_attr_hotkey_bios_mask.attr,
2090 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2091 &dev_attr_hotkey_mask.attr,
2092 &dev_attr_hotkey_all_mask.attr,
2093 &dev_attr_hotkey_recommended_mask.attr,
2094 #endif
2095 &dev_attr_hotkey_wakeup_reason.attr,
2096 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2099 static void bluetooth_update_rfk(void);
2100 static void wan_update_rfk(void);
2101 static void tpacpi_send_radiosw_update(void)
2103 int wlsw;
2105 /* Sync these BEFORE sending any rfkill events */
2106 if (tp_features.bluetooth)
2107 bluetooth_update_rfk();
2108 if (tp_features.wan)
2109 wan_update_rfk();
2111 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2112 mutex_lock(&tpacpi_inputdev_send_mutex);
2114 input_report_switch(tpacpi_inputdev,
2115 SW_RFKILL_ALL, !!wlsw);
2116 input_sync(tpacpi_inputdev);
2118 mutex_unlock(&tpacpi_inputdev_send_mutex);
2120 hotkey_radio_sw_notify_change();
2123 static void hotkey_exit(void)
2125 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2126 hotkey_poll_stop_sync();
2127 #endif
2129 if (hotkey_dev_attributes)
2130 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2132 kfree(hotkey_keycode_map);
2134 if (tp_features.hotkey) {
2135 dbg_printk(TPACPI_DBG_EXIT,
2136 "restoring original hot key mask\n");
2137 /* no short-circuit boolean operator below! */
2138 if ((hotkey_mask_set(hotkey_orig_mask) |
2139 hotkey_status_set(hotkey_orig_status)) != 0)
2140 printk(TPACPI_ERR
2141 "failed to restore hot key mask "
2142 "to BIOS defaults\n");
2146 static int __init hotkey_init(struct ibm_init_struct *iibm)
2148 /* Requirements for changing the default keymaps:
2150 * 1. Many of the keys are mapped to KEY_RESERVED for very
2151 * good reasons. Do not change them unless you have deep
2152 * knowledge on the IBM and Lenovo ThinkPad firmware for
2153 * the various ThinkPad models. The driver behaves
2154 * differently for KEY_RESERVED: such keys have their
2155 * hot key mask *unset* in mask_recommended, and also
2156 * in the initial hot key mask programmed into the
2157 * firmware at driver load time, which means the firm-
2158 * ware may react very differently if you change them to
2159 * something else;
2161 * 2. You must be subscribed to the linux-thinkpad and
2162 * ibm-acpi-devel mailing lists, and you should read the
2163 * list archives since 2007 if you want to change the
2164 * keymaps. This requirement exists so that you will
2165 * know the past history of problems with the thinkpad-
2166 * acpi driver keymaps, and also that you will be
2167 * listening to any bug reports;
2169 * 3. Do not send thinkpad-acpi specific patches directly to
2170 * for merging, *ever*. Send them to the linux-acpi
2171 * mailinglist for comments. Merging is to be done only
2172 * through acpi-test and the ACPI maintainer.
2174 * If the above is too much to ask, don't change the keymap.
2175 * Ask the thinkpad-acpi maintainer to do it, instead.
2177 static u16 ibm_keycode_map[] __initdata = {
2178 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2179 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2180 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2181 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2183 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2184 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2185 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2186 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2188 /* brightness: firmware always reacts to them, unless
2189 * X.org did some tricks in the radeon BIOS scratch
2190 * registers of *some* models */
2191 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2192 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2194 /* Thinklight: firmware always react to it */
2195 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2197 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2198 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2200 /* Volume: firmware always react to it and reprograms
2201 * the built-in *extra* mixer. Never map it to control
2202 * another mixer by default. */
2203 KEY_RESERVED, /* 0x14: VOLUME UP */
2204 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2205 KEY_RESERVED, /* 0x16: MUTE */
2207 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2209 /* (assignments unknown, please report if found) */
2210 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2211 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2213 static u16 lenovo_keycode_map[] __initdata = {
2214 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2215 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2216 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2217 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2219 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2220 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2221 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2222 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2224 /* These either have to go through ACPI video, or
2225 * act like in the IBM ThinkPads, so don't ever
2226 * enable them by default */
2227 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2228 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2230 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2232 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2233 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2235 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2236 * react to it and reprograms the built-in *extra* mixer.
2237 * Never map it to control another mixer by default.
2239 * T60?, T61, R60?, R61: firmware and EC tries to send
2240 * these over the regular keyboard, so these are no-ops,
2241 * but there are still weird bugs re. MUTE, so do not
2242 * change unless you get test reports from all Lenovo
2243 * models. May cause the BIOS to interfere with the
2244 * HDA mixer.
2246 KEY_RESERVED, /* 0x14: VOLUME UP */
2247 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2248 KEY_RESERVED, /* 0x16: MUTE */
2250 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2252 /* (assignments unknown, please report if found) */
2253 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2254 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2257 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2258 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2259 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2261 int res, i;
2262 int status;
2263 int hkeyv;
2265 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2267 BUG_ON(!tpacpi_inputdev);
2268 BUG_ON(tpacpi_inputdev->open != NULL ||
2269 tpacpi_inputdev->close != NULL);
2271 TPACPI_ACPIHANDLE_INIT(hkey);
2272 mutex_init(&hotkey_mutex);
2274 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2275 mutex_init(&hotkey_thread_mutex);
2276 mutex_init(&hotkey_thread_data_mutex);
2277 #endif
2279 /* hotkey not supported on 570 */
2280 tp_features.hotkey = hkey_handle != NULL;
2282 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2283 str_supported(tp_features.hotkey));
2285 if (!tp_features.hotkey)
2286 return 1;
2288 tpacpi_disable_brightness_delay();
2290 hotkey_dev_attributes = create_attr_set(13, NULL);
2291 if (!hotkey_dev_attributes)
2292 return -ENOMEM;
2293 res = add_many_to_attr_set(hotkey_dev_attributes,
2294 hotkey_attributes,
2295 ARRAY_SIZE(hotkey_attributes));
2296 if (res)
2297 goto err_exit;
2299 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2300 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2301 for HKEY interface version 0x100 */
2302 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2303 if ((hkeyv >> 8) != 1) {
2304 printk(TPACPI_ERR "unknown version of the "
2305 "HKEY interface: 0x%x\n", hkeyv);
2306 printk(TPACPI_ERR "please report this to %s\n",
2307 TPACPI_MAIL);
2308 } else {
2310 * MHKV 0x100 in A31, R40, R40e,
2311 * T4x, X31, and later
2313 tp_features.hotkey_mask = 1;
2317 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2318 str_supported(tp_features.hotkey_mask));
2320 if (tp_features.hotkey_mask) {
2321 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2322 "MHKA", "qd")) {
2323 printk(TPACPI_ERR
2324 "missing MHKA handler, "
2325 "please report this to %s\n",
2326 TPACPI_MAIL);
2327 /* FN+F12, FN+F4, FN+F3 */
2328 hotkey_all_mask = 0x080cU;
2332 /* hotkey_source_mask *must* be zero for
2333 * the first hotkey_mask_get */
2334 res = hotkey_status_get(&hotkey_orig_status);
2335 if (res)
2336 goto err_exit;
2338 if (tp_features.hotkey_mask) {
2339 res = hotkey_mask_get();
2340 if (res)
2341 goto err_exit;
2343 hotkey_orig_mask = hotkey_mask;
2344 res = add_many_to_attr_set(
2345 hotkey_dev_attributes,
2346 hotkey_mask_attributes,
2347 ARRAY_SIZE(hotkey_mask_attributes));
2348 if (res)
2349 goto err_exit;
2352 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2353 if (tp_features.hotkey_mask) {
2354 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2355 & ~hotkey_all_mask;
2356 } else {
2357 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2360 vdbg_printk(TPACPI_DBG_INIT,
2361 "hotkey source mask 0x%08x, polling freq %d\n",
2362 hotkey_source_mask, hotkey_poll_freq);
2363 #endif
2365 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2366 if (dbg_wlswemul) {
2367 tp_features.hotkey_wlsw = 1;
2368 printk(TPACPI_INFO
2369 "radio switch emulation enabled\n");
2370 } else
2371 #endif
2372 /* Not all thinkpads have a hardware radio switch */
2373 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2374 tp_features.hotkey_wlsw = 1;
2375 printk(TPACPI_INFO
2376 "radio switch found; radios are %s\n",
2377 enabled(status, 0));
2379 if (tp_features.hotkey_wlsw)
2380 res = add_to_attr_set(hotkey_dev_attributes,
2381 &dev_attr_hotkey_radio_sw.attr);
2383 /* For X41t, X60t, X61t Tablets... */
2384 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2385 tp_features.hotkey_tablet = 1;
2386 printk(TPACPI_INFO
2387 "possible tablet mode switch found; "
2388 "ThinkPad in %s mode\n",
2389 (status & TP_HOTKEY_TABLET_MASK)?
2390 "tablet" : "laptop");
2391 res = add_to_attr_set(hotkey_dev_attributes,
2392 &dev_attr_hotkey_tablet_mode.attr);
2395 if (!res)
2396 res = register_attr_set_with_sysfs(
2397 hotkey_dev_attributes,
2398 &tpacpi_pdev->dev.kobj);
2399 if (res)
2400 goto err_exit;
2402 /* Set up key map */
2404 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2405 GFP_KERNEL);
2406 if (!hotkey_keycode_map) {
2407 printk(TPACPI_ERR
2408 "failed to allocate memory for key map\n");
2409 res = -ENOMEM;
2410 goto err_exit;
2413 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2414 dbg_printk(TPACPI_DBG_INIT,
2415 "using Lenovo default hot key map\n");
2416 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2417 TPACPI_HOTKEY_MAP_SIZE);
2418 } else {
2419 dbg_printk(TPACPI_DBG_INIT,
2420 "using IBM default hot key map\n");
2421 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2422 TPACPI_HOTKEY_MAP_SIZE);
2425 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2426 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2427 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2428 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2429 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2430 tpacpi_inputdev->keycode = hotkey_keycode_map;
2431 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2432 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2433 set_bit(hotkey_keycode_map[i],
2434 tpacpi_inputdev->keybit);
2435 } else {
2436 if (i < sizeof(hotkey_reserved_mask)*8)
2437 hotkey_reserved_mask |= 1 << i;
2441 if (tp_features.hotkey_wlsw) {
2442 set_bit(EV_SW, tpacpi_inputdev->evbit);
2443 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2445 if (tp_features.hotkey_tablet) {
2446 set_bit(EV_SW, tpacpi_inputdev->evbit);
2447 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2450 /* Do not issue duplicate brightness change events to
2451 * userspace */
2452 if (!tp_features.bright_acpimode)
2453 /* update bright_acpimode... */
2454 tpacpi_check_std_acpi_brightness_support();
2456 if (tp_features.bright_acpimode) {
2457 printk(TPACPI_INFO
2458 "This ThinkPad has standard ACPI backlight "
2459 "brightness control, supported by the ACPI "
2460 "video driver\n");
2461 printk(TPACPI_NOTICE
2462 "Disabling thinkpad-acpi brightness events "
2463 "by default...\n");
2465 /* The hotkey_reserved_mask change below is not
2466 * necessary while the keys are at KEY_RESERVED in the
2467 * default map, but better safe than sorry, leave it
2468 * here as a marker of what we have to do, especially
2469 * when we finally become able to set this at runtime
2470 * on response to X.org requests */
2471 hotkey_reserved_mask |=
2472 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2473 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2476 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2477 res = hotkey_status_set(1);
2478 if (res) {
2479 hotkey_exit();
2480 return res;
2482 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2483 & ~hotkey_reserved_mask)
2484 | hotkey_orig_mask);
2485 if (res < 0 && res != -ENXIO) {
2486 hotkey_exit();
2487 return res;
2490 dbg_printk(TPACPI_DBG_INIT,
2491 "legacy hot key reporting over procfs %s\n",
2492 (hotkey_report_mode < 2) ?
2493 "enabled" : "disabled");
2495 tpacpi_inputdev->open = &hotkey_inputdev_open;
2496 tpacpi_inputdev->close = &hotkey_inputdev_close;
2498 hotkey_poll_setup_safe(1);
2499 tpacpi_send_radiosw_update();
2500 tpacpi_input_send_tabletsw();
2502 return 0;
2504 err_exit:
2505 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2506 hotkey_dev_attributes = NULL;
2508 return (res < 0)? res : 1;
2511 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2513 u32 hkey;
2514 unsigned int scancode;
2515 int send_acpi_ev;
2516 int ignore_acpi_ev;
2517 int unk_ev;
2519 if (event != 0x80) {
2520 printk(TPACPI_ERR
2521 "unknown HKEY notification event %d\n", event);
2522 /* forward it to userspace, maybe it knows how to handle it */
2523 acpi_bus_generate_netlink_event(
2524 ibm->acpi->device->pnp.device_class,
2525 dev_name(&ibm->acpi->device->dev),
2526 event, 0);
2527 return;
2530 while (1) {
2531 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2532 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2533 return;
2536 if (hkey == 0) {
2537 /* queue empty */
2538 return;
2541 send_acpi_ev = 1;
2542 ignore_acpi_ev = 0;
2543 unk_ev = 0;
2545 switch (hkey >> 12) {
2546 case 1:
2547 /* 0x1000-0x1FFF: key presses */
2548 scancode = hkey & 0xfff;
2549 if (scancode > 0 && scancode < 0x21) {
2550 scancode--;
2551 if (!(hotkey_source_mask & (1 << scancode))) {
2552 tpacpi_input_send_key(scancode);
2553 send_acpi_ev = 0;
2554 } else {
2555 ignore_acpi_ev = 1;
2557 } else {
2558 unk_ev = 1;
2560 break;
2561 case 2:
2562 /* Wakeup reason */
2563 switch (hkey) {
2564 case 0x2304: /* suspend, undock */
2565 case 0x2404: /* hibernation, undock */
2566 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2567 ignore_acpi_ev = 1;
2568 break;
2569 case 0x2305: /* suspend, bay eject */
2570 case 0x2405: /* hibernation, bay eject */
2571 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2572 ignore_acpi_ev = 1;
2573 break;
2574 default:
2575 unk_ev = 1;
2577 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2578 printk(TPACPI_INFO
2579 "woke up due to a hot-unplug "
2580 "request...\n");
2581 hotkey_wakeup_reason_notify_change();
2583 break;
2584 case 3:
2585 /* bay-related wakeups */
2586 if (hkey == 0x3003) {
2587 hotkey_autosleep_ack = 1;
2588 printk(TPACPI_INFO
2589 "bay ejected\n");
2590 hotkey_wakeup_hotunplug_complete_notify_change();
2591 } else {
2592 unk_ev = 1;
2594 break;
2595 case 4:
2596 /* dock-related wakeups */
2597 if (hkey == 0x4003) {
2598 hotkey_autosleep_ack = 1;
2599 printk(TPACPI_INFO
2600 "undocked\n");
2601 hotkey_wakeup_hotunplug_complete_notify_change();
2602 } else {
2603 unk_ev = 1;
2605 break;
2606 case 5:
2607 /* 0x5000-0x5FFF: human interface helpers */
2608 switch (hkey) {
2609 case 0x5010: /* Lenovo new BIOS: brightness changed */
2610 case 0x500b: /* X61t: tablet pen inserted into bay */
2611 case 0x500c: /* X61t: tablet pen removed from bay */
2612 break;
2613 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2614 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2615 tpacpi_input_send_tabletsw();
2616 hotkey_tablet_mode_notify_change();
2617 send_acpi_ev = 0;
2618 break;
2619 case 0x5001:
2620 case 0x5002:
2621 /* LID switch events. Do not propagate */
2622 ignore_acpi_ev = 1;
2623 break;
2624 default:
2625 unk_ev = 1;
2627 break;
2628 case 7:
2629 /* 0x7000-0x7FFF: misc */
2630 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2631 tpacpi_send_radiosw_update();
2632 send_acpi_ev = 0;
2633 break;
2635 /* fallthrough to default */
2636 default:
2637 unk_ev = 1;
2639 if (unk_ev) {
2640 printk(TPACPI_NOTICE
2641 "unhandled HKEY event 0x%04x\n", hkey);
2644 /* Legacy events */
2645 if (!ignore_acpi_ev &&
2646 (send_acpi_ev || hotkey_report_mode < 2)) {
2647 acpi_bus_generate_proc_event(ibm->acpi->device,
2648 event, hkey);
2651 /* netlink events */
2652 if (!ignore_acpi_ev && send_acpi_ev) {
2653 acpi_bus_generate_netlink_event(
2654 ibm->acpi->device->pnp.device_class,
2655 dev_name(&ibm->acpi->device->dev),
2656 event, hkey);
2661 static void hotkey_suspend(pm_message_t state)
2663 /* Do these on suspend, we get the events on early resume! */
2664 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2665 hotkey_autosleep_ack = 0;
2668 static void hotkey_resume(void)
2670 tpacpi_disable_brightness_delay();
2672 if (hotkey_mask_get())
2673 printk(TPACPI_ERR
2674 "error while trying to read hot key mask "
2675 "from firmware\n");
2676 tpacpi_send_radiosw_update();
2677 hotkey_tablet_mode_notify_change();
2678 hotkey_wakeup_reason_notify_change();
2679 hotkey_wakeup_hotunplug_complete_notify_change();
2680 hotkey_poll_setup_safe(0);
2683 /* procfs -------------------------------------------------------------- */
2684 static int hotkey_read(char *p)
2686 int res, status;
2687 int len = 0;
2689 if (!tp_features.hotkey) {
2690 len += sprintf(p + len, "status:\t\tnot supported\n");
2691 return len;
2694 if (mutex_lock_interruptible(&hotkey_mutex))
2695 return -ERESTARTSYS;
2696 res = hotkey_status_get(&status);
2697 if (!res)
2698 res = hotkey_mask_get();
2699 mutex_unlock(&hotkey_mutex);
2700 if (res)
2701 return res;
2703 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2704 if (tp_features.hotkey_mask) {
2705 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2706 len += sprintf(p + len,
2707 "commands:\tenable, disable, reset, <mask>\n");
2708 } else {
2709 len += sprintf(p + len, "mask:\t\tnot supported\n");
2710 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2713 return len;
2716 static int hotkey_write(char *buf)
2718 int res, status;
2719 u32 mask;
2720 char *cmd;
2722 if (!tp_features.hotkey)
2723 return -ENODEV;
2725 if (mutex_lock_interruptible(&hotkey_mutex))
2726 return -ERESTARTSYS;
2728 status = -1;
2729 mask = hotkey_mask;
2731 res = 0;
2732 while ((cmd = next_cmd(&buf))) {
2733 if (strlencmp(cmd, "enable") == 0) {
2734 status = 1;
2735 } else if (strlencmp(cmd, "disable") == 0) {
2736 status = 0;
2737 } else if (strlencmp(cmd, "reset") == 0) {
2738 status = hotkey_orig_status;
2739 mask = hotkey_orig_mask;
2740 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2741 /* mask set */
2742 } else if (sscanf(cmd, "%x", &mask) == 1) {
2743 /* mask set */
2744 } else {
2745 res = -EINVAL;
2746 goto errexit;
2749 if (status != -1)
2750 res = hotkey_status_set(status);
2752 if (!res && mask != hotkey_mask)
2753 res = hotkey_mask_set(mask);
2755 errexit:
2756 mutex_unlock(&hotkey_mutex);
2757 return res;
2760 static const struct acpi_device_id ibm_htk_device_ids[] = {
2761 {TPACPI_ACPI_HKEY_HID, 0},
2762 {"", 0},
2765 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2766 .hid = ibm_htk_device_ids,
2767 .notify = hotkey_notify,
2768 .handle = &hkey_handle,
2769 .type = ACPI_DEVICE_NOTIFY,
2772 static struct ibm_struct hotkey_driver_data = {
2773 .name = "hotkey",
2774 .read = hotkey_read,
2775 .write = hotkey_write,
2776 .exit = hotkey_exit,
2777 .resume = hotkey_resume,
2778 .suspend = hotkey_suspend,
2779 .acpi = &ibm_hotkey_acpidriver,
2782 /*************************************************************************
2783 * Bluetooth subdriver
2786 enum {
2787 /* ACPI GBDC/SBDC bits */
2788 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2789 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2790 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2791 off / last state */
2794 enum {
2795 /* ACPI \BLTH commands */
2796 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2797 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2798 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2799 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2800 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2803 static struct rfkill *tpacpi_bluetooth_rfkill;
2805 static void bluetooth_suspend(pm_message_t state)
2807 /* Try to make sure radio will resume powered off */
2808 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2809 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2812 static int bluetooth_get_radiosw(void)
2814 int status;
2816 if (!tp_features.bluetooth)
2817 return -ENODEV;
2819 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2820 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2821 return RFKILL_STATE_HARD_BLOCKED;
2823 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2824 if (dbg_bluetoothemul)
2825 return (tpacpi_bluetooth_emulstate) ?
2826 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2827 #endif
2829 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2830 return -EIO;
2832 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2833 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2836 static void bluetooth_update_rfk(void)
2838 int status;
2840 if (!tpacpi_bluetooth_rfkill)
2841 return;
2843 status = bluetooth_get_radiosw();
2844 if (status < 0)
2845 return;
2846 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2849 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2851 int status;
2853 if (!tp_features.bluetooth)
2854 return -ENODEV;
2856 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2857 * reason to risk weird behaviour. */
2858 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2859 && radio_on)
2860 return -EPERM;
2862 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2863 if (dbg_bluetoothemul) {
2864 tpacpi_bluetooth_emulstate = !!radio_on;
2865 if (update_rfk)
2866 bluetooth_update_rfk();
2867 return 0;
2869 #endif
2871 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
2872 if (radio_on)
2873 status = TP_ACPI_BLUETOOTH_RADIOSSW;
2874 else
2875 status = 0;
2876 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2877 return -EIO;
2879 if (update_rfk)
2880 bluetooth_update_rfk();
2882 return 0;
2885 /* sysfs bluetooth enable ---------------------------------------------- */
2886 static ssize_t bluetooth_enable_show(struct device *dev,
2887 struct device_attribute *attr,
2888 char *buf)
2890 int status;
2892 status = bluetooth_get_radiosw();
2893 if (status < 0)
2894 return status;
2896 return snprintf(buf, PAGE_SIZE, "%d\n",
2897 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2900 static ssize_t bluetooth_enable_store(struct device *dev,
2901 struct device_attribute *attr,
2902 const char *buf, size_t count)
2904 unsigned long t;
2905 int res;
2907 if (parse_strtoul(buf, 1, &t))
2908 return -EINVAL;
2910 res = bluetooth_set_radiosw(t, 1);
2912 return (res) ? res : count;
2915 static struct device_attribute dev_attr_bluetooth_enable =
2916 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2917 bluetooth_enable_show, bluetooth_enable_store);
2919 /* --------------------------------------------------------------------- */
2921 static struct attribute *bluetooth_attributes[] = {
2922 &dev_attr_bluetooth_enable.attr,
2923 NULL
2926 static const struct attribute_group bluetooth_attr_group = {
2927 .attrs = bluetooth_attributes,
2930 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2932 int bts = bluetooth_get_radiosw();
2934 if (bts < 0)
2935 return bts;
2937 *state = bts;
2938 return 0;
2941 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2943 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2946 static void bluetooth_exit(void)
2948 if (tpacpi_bluetooth_rfkill)
2949 rfkill_unregister(tpacpi_bluetooth_rfkill);
2951 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2952 &bluetooth_attr_group);
2955 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2957 int res;
2958 int status = 0;
2960 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2962 TPACPI_ACPIHANDLE_INIT(hkey);
2964 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2965 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2966 tp_features.bluetooth = hkey_handle &&
2967 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2969 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2970 str_supported(tp_features.bluetooth),
2971 status);
2973 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2974 if (dbg_bluetoothemul) {
2975 tp_features.bluetooth = 1;
2976 printk(TPACPI_INFO
2977 "bluetooth switch emulation enabled\n");
2978 } else
2979 #endif
2980 if (tp_features.bluetooth &&
2981 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2982 /* no bluetooth hardware present in system */
2983 tp_features.bluetooth = 0;
2984 dbg_printk(TPACPI_DBG_INIT,
2985 "bluetooth hardware not installed\n");
2988 if (!tp_features.bluetooth)
2989 return 1;
2991 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2992 &bluetooth_attr_group);
2993 if (res)
2994 return res;
2996 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2997 &tpacpi_bluetooth_rfkill,
2998 RFKILL_TYPE_BLUETOOTH,
2999 "tpacpi_bluetooth_sw",
3000 tpacpi_bluetooth_rfk_set,
3001 tpacpi_bluetooth_rfk_get);
3002 if (res) {
3003 bluetooth_exit();
3004 return res;
3007 return 0;
3010 /* procfs -------------------------------------------------------------- */
3011 static int bluetooth_read(char *p)
3013 int len = 0;
3014 int status = bluetooth_get_radiosw();
3016 if (!tp_features.bluetooth)
3017 len += sprintf(p + len, "status:\t\tnot supported\n");
3018 else {
3019 len += sprintf(p + len, "status:\t\t%s\n",
3020 (status == RFKILL_STATE_UNBLOCKED) ?
3021 "enabled" : "disabled");
3022 len += sprintf(p + len, "commands:\tenable, disable\n");
3025 return len;
3028 static int bluetooth_write(char *buf)
3030 char *cmd;
3032 if (!tp_features.bluetooth)
3033 return -ENODEV;
3035 while ((cmd = next_cmd(&buf))) {
3036 if (strlencmp(cmd, "enable") == 0) {
3037 bluetooth_set_radiosw(1, 1);
3038 } else if (strlencmp(cmd, "disable") == 0) {
3039 bluetooth_set_radiosw(0, 1);
3040 } else
3041 return -EINVAL;
3044 return 0;
3047 static struct ibm_struct bluetooth_driver_data = {
3048 .name = "bluetooth",
3049 .read = bluetooth_read,
3050 .write = bluetooth_write,
3051 .exit = bluetooth_exit,
3052 .suspend = bluetooth_suspend,
3055 /*************************************************************************
3056 * Wan subdriver
3059 enum {
3060 /* ACPI GWAN/SWAN bits */
3061 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3062 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3063 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3064 off / last state */
3067 static struct rfkill *tpacpi_wan_rfkill;
3069 static void wan_suspend(pm_message_t state)
3071 /* Try to make sure radio will resume powered off */
3072 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3073 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3076 static int wan_get_radiosw(void)
3078 int status;
3080 if (!tp_features.wan)
3081 return -ENODEV;
3083 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3084 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3085 return RFKILL_STATE_HARD_BLOCKED;
3087 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3088 if (dbg_wwanemul)
3089 return (tpacpi_wwan_emulstate) ?
3090 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3091 #endif
3093 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3094 return -EIO;
3096 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3097 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3100 static void wan_update_rfk(void)
3102 int status;
3104 if (!tpacpi_wan_rfkill)
3105 return;
3107 status = wan_get_radiosw();
3108 if (status < 0)
3109 return;
3110 rfkill_force_state(tpacpi_wan_rfkill, status);
3113 static int wan_set_radiosw(int radio_on, int update_rfk)
3115 int status;
3117 if (!tp_features.wan)
3118 return -ENODEV;
3120 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3121 * reason to risk weird behaviour. */
3122 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3123 && radio_on)
3124 return -EPERM;
3126 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3127 if (dbg_wwanemul) {
3128 tpacpi_wwan_emulstate = !!radio_on;
3129 if (update_rfk)
3130 wan_update_rfk();
3131 return 0;
3133 #endif
3135 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3136 if (radio_on)
3137 status = TP_ACPI_WANCARD_RADIOSSW;
3138 else
3139 status = 0;
3140 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3141 return -EIO;
3143 if (update_rfk)
3144 wan_update_rfk();
3146 return 0;
3149 /* sysfs wan enable ---------------------------------------------------- */
3150 static ssize_t wan_enable_show(struct device *dev,
3151 struct device_attribute *attr,
3152 char *buf)
3154 int status;
3156 status = wan_get_radiosw();
3157 if (status < 0)
3158 return status;
3160 return snprintf(buf, PAGE_SIZE, "%d\n",
3161 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3164 static ssize_t wan_enable_store(struct device *dev,
3165 struct device_attribute *attr,
3166 const char *buf, size_t count)
3168 unsigned long t;
3169 int res;
3171 if (parse_strtoul(buf, 1, &t))
3172 return -EINVAL;
3174 res = wan_set_radiosw(t, 1);
3176 return (res) ? res : count;
3179 static struct device_attribute dev_attr_wan_enable =
3180 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3181 wan_enable_show, wan_enable_store);
3183 /* --------------------------------------------------------------------- */
3185 static struct attribute *wan_attributes[] = {
3186 &dev_attr_wan_enable.attr,
3187 NULL
3190 static const struct attribute_group wan_attr_group = {
3191 .attrs = wan_attributes,
3194 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3196 int wans = wan_get_radiosw();
3198 if (wans < 0)
3199 return wans;
3201 *state = wans;
3202 return 0;
3205 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3207 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3210 static void wan_exit(void)
3212 if (tpacpi_wan_rfkill)
3213 rfkill_unregister(tpacpi_wan_rfkill);
3215 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3216 &wan_attr_group);
3219 static int __init wan_init(struct ibm_init_struct *iibm)
3221 int res;
3222 int status = 0;
3224 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3226 TPACPI_ACPIHANDLE_INIT(hkey);
3228 tp_features.wan = hkey_handle &&
3229 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3231 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3232 str_supported(tp_features.wan),
3233 status);
3235 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3236 if (dbg_wwanemul) {
3237 tp_features.wan = 1;
3238 printk(TPACPI_INFO
3239 "wwan switch emulation enabled\n");
3240 } else
3241 #endif
3242 if (tp_features.wan &&
3243 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3244 /* no wan hardware present in system */
3245 tp_features.wan = 0;
3246 dbg_printk(TPACPI_DBG_INIT,
3247 "wan hardware not installed\n");
3250 if (!tp_features.wan)
3251 return 1;
3253 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3254 &wan_attr_group);
3255 if (res)
3256 return res;
3258 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3259 &tpacpi_wan_rfkill,
3260 RFKILL_TYPE_WWAN,
3261 "tpacpi_wwan_sw",
3262 tpacpi_wan_rfk_set,
3263 tpacpi_wan_rfk_get);
3264 if (res) {
3265 wan_exit();
3266 return res;
3269 return 0;
3272 /* procfs -------------------------------------------------------------- */
3273 static int wan_read(char *p)
3275 int len = 0;
3276 int status = wan_get_radiosw();
3278 if (!tp_features.wan)
3279 len += sprintf(p + len, "status:\t\tnot supported\n");
3280 else {
3281 len += sprintf(p + len, "status:\t\t%s\n",
3282 (status == RFKILL_STATE_UNBLOCKED) ?
3283 "enabled" : "disabled");
3284 len += sprintf(p + len, "commands:\tenable, disable\n");
3287 return len;
3290 static int wan_write(char *buf)
3292 char *cmd;
3294 if (!tp_features.wan)
3295 return -ENODEV;
3297 while ((cmd = next_cmd(&buf))) {
3298 if (strlencmp(cmd, "enable") == 0) {
3299 wan_set_radiosw(1, 1);
3300 } else if (strlencmp(cmd, "disable") == 0) {
3301 wan_set_radiosw(0, 1);
3302 } else
3303 return -EINVAL;
3306 return 0;
3309 static struct ibm_struct wan_driver_data = {
3310 .name = "wan",
3311 .read = wan_read,
3312 .write = wan_write,
3313 .exit = wan_exit,
3314 .suspend = wan_suspend,
3317 /*************************************************************************
3318 * Video subdriver
3321 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3323 enum video_access_mode {
3324 TPACPI_VIDEO_NONE = 0,
3325 TPACPI_VIDEO_570, /* 570 */
3326 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3327 TPACPI_VIDEO_NEW, /* all others */
3330 enum { /* video status flags, based on VIDEO_570 */
3331 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3332 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3333 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3336 enum { /* TPACPI_VIDEO_570 constants */
3337 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3338 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3339 * video_status_flags */
3340 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3341 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3344 static enum video_access_mode video_supported;
3345 static int video_orig_autosw;
3347 static int video_autosw_get(void);
3348 static int video_autosw_set(int enable);
3350 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3352 static int __init video_init(struct ibm_init_struct *iibm)
3354 int ivga;
3356 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3358 TPACPI_ACPIHANDLE_INIT(vid);
3359 TPACPI_ACPIHANDLE_INIT(vid2);
3361 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3362 /* G41, assume IVGA doesn't change */
3363 vid_handle = vid2_handle;
3365 if (!vid_handle)
3366 /* video switching not supported on R30, R31 */
3367 video_supported = TPACPI_VIDEO_NONE;
3368 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3369 /* 570 */
3370 video_supported = TPACPI_VIDEO_570;
3371 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3372 /* 600e/x, 770e, 770x */
3373 video_supported = TPACPI_VIDEO_770;
3374 else
3375 /* all others */
3376 video_supported = TPACPI_VIDEO_NEW;
3378 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3379 str_supported(video_supported != TPACPI_VIDEO_NONE),
3380 video_supported);
3382 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3385 static void video_exit(void)
3387 dbg_printk(TPACPI_DBG_EXIT,
3388 "restoring original video autoswitch mode\n");
3389 if (video_autosw_set(video_orig_autosw))
3390 printk(TPACPI_ERR "error while trying to restore original "
3391 "video autoswitch mode\n");
3394 static int video_outputsw_get(void)
3396 int status = 0;
3397 int i;
3399 switch (video_supported) {
3400 case TPACPI_VIDEO_570:
3401 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3402 TP_ACPI_VIDEO_570_PHSCMD))
3403 return -EIO;
3404 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3405 break;
3406 case TPACPI_VIDEO_770:
3407 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3408 return -EIO;
3409 if (i)
3410 status |= TP_ACPI_VIDEO_S_LCD;
3411 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3412 return -EIO;
3413 if (i)
3414 status |= TP_ACPI_VIDEO_S_CRT;
3415 break;
3416 case TPACPI_VIDEO_NEW:
3417 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3418 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3419 return -EIO;
3420 if (i)
3421 status |= TP_ACPI_VIDEO_S_CRT;
3423 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3424 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3425 return -EIO;
3426 if (i)
3427 status |= TP_ACPI_VIDEO_S_LCD;
3428 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3429 return -EIO;
3430 if (i)
3431 status |= TP_ACPI_VIDEO_S_DVI;
3432 break;
3433 default:
3434 return -ENOSYS;
3437 return status;
3440 static int video_outputsw_set(int status)
3442 int autosw;
3443 int res = 0;
3445 switch (video_supported) {
3446 case TPACPI_VIDEO_570:
3447 res = acpi_evalf(NULL, NULL,
3448 "\\_SB.PHS2", "vdd",
3449 TP_ACPI_VIDEO_570_PHS2CMD,
3450 status | TP_ACPI_VIDEO_570_PHS2SET);
3451 break;
3452 case TPACPI_VIDEO_770:
3453 autosw = video_autosw_get();
3454 if (autosw < 0)
3455 return autosw;
3457 res = video_autosw_set(1);
3458 if (res)
3459 return res;
3460 res = acpi_evalf(vid_handle, NULL,
3461 "ASWT", "vdd", status * 0x100, 0);
3462 if (!autosw && video_autosw_set(autosw)) {
3463 printk(TPACPI_ERR
3464 "video auto-switch left enabled due to error\n");
3465 return -EIO;
3467 break;
3468 case TPACPI_VIDEO_NEW:
3469 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3470 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3471 break;
3472 default:
3473 return -ENOSYS;
3476 return (res)? 0 : -EIO;
3479 static int video_autosw_get(void)
3481 int autosw = 0;
3483 switch (video_supported) {
3484 case TPACPI_VIDEO_570:
3485 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3486 return -EIO;
3487 break;
3488 case TPACPI_VIDEO_770:
3489 case TPACPI_VIDEO_NEW:
3490 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3491 return -EIO;
3492 break;
3493 default:
3494 return -ENOSYS;
3497 return autosw & 1;
3500 static int video_autosw_set(int enable)
3502 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3503 return -EIO;
3504 return 0;
3507 static int video_outputsw_cycle(void)
3509 int autosw = video_autosw_get();
3510 int res;
3512 if (autosw < 0)
3513 return autosw;
3515 switch (video_supported) {
3516 case TPACPI_VIDEO_570:
3517 res = video_autosw_set(1);
3518 if (res)
3519 return res;
3520 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3521 break;
3522 case TPACPI_VIDEO_770:
3523 case TPACPI_VIDEO_NEW:
3524 res = video_autosw_set(1);
3525 if (res)
3526 return res;
3527 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3528 break;
3529 default:
3530 return -ENOSYS;
3532 if (!autosw && video_autosw_set(autosw)) {
3533 printk(TPACPI_ERR
3534 "video auto-switch left enabled due to error\n");
3535 return -EIO;
3538 return (res)? 0 : -EIO;
3541 static int video_expand_toggle(void)
3543 switch (video_supported) {
3544 case TPACPI_VIDEO_570:
3545 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3546 0 : -EIO;
3547 case TPACPI_VIDEO_770:
3548 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3549 0 : -EIO;
3550 case TPACPI_VIDEO_NEW:
3551 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3552 0 : -EIO;
3553 default:
3554 return -ENOSYS;
3556 /* not reached */
3559 static int video_read(char *p)
3561 int status, autosw;
3562 int len = 0;
3564 if (video_supported == TPACPI_VIDEO_NONE) {
3565 len += sprintf(p + len, "status:\t\tnot supported\n");
3566 return len;
3569 status = video_outputsw_get();
3570 if (status < 0)
3571 return status;
3573 autosw = video_autosw_get();
3574 if (autosw < 0)
3575 return autosw;
3577 len += sprintf(p + len, "status:\t\tsupported\n");
3578 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3579 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3580 if (video_supported == TPACPI_VIDEO_NEW)
3581 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3582 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3583 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3584 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3585 if (video_supported == TPACPI_VIDEO_NEW)
3586 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3587 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3588 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3590 return len;
3593 static int video_write(char *buf)
3595 char *cmd;
3596 int enable, disable, status;
3597 int res;
3599 if (video_supported == TPACPI_VIDEO_NONE)
3600 return -ENODEV;
3602 enable = 0;
3603 disable = 0;
3605 while ((cmd = next_cmd(&buf))) {
3606 if (strlencmp(cmd, "lcd_enable") == 0) {
3607 enable |= TP_ACPI_VIDEO_S_LCD;
3608 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3609 disable |= TP_ACPI_VIDEO_S_LCD;
3610 } else if (strlencmp(cmd, "crt_enable") == 0) {
3611 enable |= TP_ACPI_VIDEO_S_CRT;
3612 } else if (strlencmp(cmd, "crt_disable") == 0) {
3613 disable |= TP_ACPI_VIDEO_S_CRT;
3614 } else if (video_supported == TPACPI_VIDEO_NEW &&
3615 strlencmp(cmd, "dvi_enable") == 0) {
3616 enable |= TP_ACPI_VIDEO_S_DVI;
3617 } else if (video_supported == TPACPI_VIDEO_NEW &&
3618 strlencmp(cmd, "dvi_disable") == 0) {
3619 disable |= TP_ACPI_VIDEO_S_DVI;
3620 } else if (strlencmp(cmd, "auto_enable") == 0) {
3621 res = video_autosw_set(1);
3622 if (res)
3623 return res;
3624 } else if (strlencmp(cmd, "auto_disable") == 0) {
3625 res = video_autosw_set(0);
3626 if (res)
3627 return res;
3628 } else if (strlencmp(cmd, "video_switch") == 0) {
3629 res = video_outputsw_cycle();
3630 if (res)
3631 return res;
3632 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3633 res = video_expand_toggle();
3634 if (res)
3635 return res;
3636 } else
3637 return -EINVAL;
3640 if (enable || disable) {
3641 status = video_outputsw_get();
3642 if (status < 0)
3643 return status;
3644 res = video_outputsw_set((status & ~disable) | enable);
3645 if (res)
3646 return res;
3649 return 0;
3652 static struct ibm_struct video_driver_data = {
3653 .name = "video",
3654 .read = video_read,
3655 .write = video_write,
3656 .exit = video_exit,
3659 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3661 /*************************************************************************
3662 * Light (thinklight) subdriver
3665 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3666 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3668 static int light_get_status(void)
3670 int status = 0;
3672 if (tp_features.light_status) {
3673 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3674 return -EIO;
3675 return (!!status);
3678 return -ENXIO;
3681 static int light_set_status(int status)
3683 int rc;
3685 if (tp_features.light) {
3686 if (cmos_handle) {
3687 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3688 (status)?
3689 TP_CMOS_THINKLIGHT_ON :
3690 TP_CMOS_THINKLIGHT_OFF);
3691 } else {
3692 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3693 (status)? 1 : 0);
3695 return (rc)? 0 : -EIO;
3698 return -ENXIO;
3701 static void light_set_status_worker(struct work_struct *work)
3703 struct tpacpi_led_classdev *data =
3704 container_of(work, struct tpacpi_led_classdev, work);
3706 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3707 light_set_status((data->new_brightness != LED_OFF));
3710 static void light_sysfs_set(struct led_classdev *led_cdev,
3711 enum led_brightness brightness)
3713 struct tpacpi_led_classdev *data =
3714 container_of(led_cdev,
3715 struct tpacpi_led_classdev,
3716 led_classdev);
3717 data->new_brightness = brightness;
3718 queue_work(tpacpi_wq, &data->work);
3721 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3723 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3726 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3727 .led_classdev = {
3728 .name = "tpacpi::thinklight",
3729 .brightness_set = &light_sysfs_set,
3730 .brightness_get = &light_sysfs_get,
3734 static int __init light_init(struct ibm_init_struct *iibm)
3736 int rc;
3738 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3740 TPACPI_ACPIHANDLE_INIT(ledb);
3741 TPACPI_ACPIHANDLE_INIT(lght);
3742 TPACPI_ACPIHANDLE_INIT(cmos);
3743 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3745 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3746 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3748 if (tp_features.light)
3749 /* light status not supported on
3750 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3751 tp_features.light_status =
3752 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3754 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3755 str_supported(tp_features.light),
3756 str_supported(tp_features.light_status));
3758 if (!tp_features.light)
3759 return 1;
3761 rc = led_classdev_register(&tpacpi_pdev->dev,
3762 &tpacpi_led_thinklight.led_classdev);
3764 if (rc < 0) {
3765 tp_features.light = 0;
3766 tp_features.light_status = 0;
3767 } else {
3768 rc = 0;
3771 return rc;
3774 static void light_exit(void)
3776 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3777 if (work_pending(&tpacpi_led_thinklight.work))
3778 flush_workqueue(tpacpi_wq);
3781 static int light_read(char *p)
3783 int len = 0;
3784 int status;
3786 if (!tp_features.light) {
3787 len += sprintf(p + len, "status:\t\tnot supported\n");
3788 } else if (!tp_features.light_status) {
3789 len += sprintf(p + len, "status:\t\tunknown\n");
3790 len += sprintf(p + len, "commands:\ton, off\n");
3791 } else {
3792 status = light_get_status();
3793 if (status < 0)
3794 return status;
3795 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3796 len += sprintf(p + len, "commands:\ton, off\n");
3799 return len;
3802 static int light_write(char *buf)
3804 char *cmd;
3805 int newstatus = 0;
3807 if (!tp_features.light)
3808 return -ENODEV;
3810 while ((cmd = next_cmd(&buf))) {
3811 if (strlencmp(cmd, "on") == 0) {
3812 newstatus = 1;
3813 } else if (strlencmp(cmd, "off") == 0) {
3814 newstatus = 0;
3815 } else
3816 return -EINVAL;
3819 return light_set_status(newstatus);
3822 static struct ibm_struct light_driver_data = {
3823 .name = "light",
3824 .read = light_read,
3825 .write = light_write,
3826 .exit = light_exit,
3829 /*************************************************************************
3830 * Dock subdriver
3833 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3835 static void dock_notify(struct ibm_struct *ibm, u32 event);
3836 static int dock_read(char *p);
3837 static int dock_write(char *buf);
3839 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3840 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3841 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3842 "\\_SB.PCI.ISA.SLCE", /* 570 */
3843 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3845 /* don't list other alternatives as we install a notify handler on the 570 */
3846 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3848 static const struct acpi_device_id ibm_pci_device_ids[] = {
3849 {PCI_ROOT_HID_STRING, 0},
3850 {"", 0},
3853 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3855 .notify = dock_notify,
3856 .handle = &dock_handle,
3857 .type = ACPI_SYSTEM_NOTIFY,
3860 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3861 * We just use it to get notifications of dock hotplug
3862 * in very old thinkpads */
3863 .hid = ibm_pci_device_ids,
3864 .notify = dock_notify,
3865 .handle = &pci_handle,
3866 .type = ACPI_SYSTEM_NOTIFY,
3870 static struct ibm_struct dock_driver_data[2] = {
3872 .name = "dock",
3873 .read = dock_read,
3874 .write = dock_write,
3875 .acpi = &ibm_dock_acpidriver[0],
3878 .name = "dock",
3879 .acpi = &ibm_dock_acpidriver[1],
3883 #define dock_docked() (_sta(dock_handle) & 1)
3885 static int __init dock_init(struct ibm_init_struct *iibm)
3887 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3889 TPACPI_ACPIHANDLE_INIT(dock);
3891 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3892 str_supported(dock_handle != NULL));
3894 return (dock_handle)? 0 : 1;
3897 static int __init dock_init2(struct ibm_init_struct *iibm)
3899 int dock2_needed;
3901 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3903 if (dock_driver_data[0].flags.acpi_driver_registered &&
3904 dock_driver_data[0].flags.acpi_notify_installed) {
3905 TPACPI_ACPIHANDLE_INIT(pci);
3906 dock2_needed = (pci_handle != NULL);
3907 vdbg_printk(TPACPI_DBG_INIT,
3908 "dock PCI handler for the TP 570 is %s\n",
3909 str_supported(dock2_needed));
3910 } else {
3911 vdbg_printk(TPACPI_DBG_INIT,
3912 "dock subdriver part 2 not required\n");
3913 dock2_needed = 0;
3916 return (dock2_needed)? 0 : 1;
3919 static void dock_notify(struct ibm_struct *ibm, u32 event)
3921 int docked = dock_docked();
3922 int pci = ibm->acpi->hid && ibm->acpi->device &&
3923 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3924 int data;
3926 if (event == 1 && !pci) /* 570 */
3927 data = 1; /* button */
3928 else if (event == 1 && pci) /* 570 */
3929 data = 3; /* dock */
3930 else if (event == 3 && docked)
3931 data = 1; /* button */
3932 else if (event == 3 && !docked)
3933 data = 2; /* undock */
3934 else if (event == 0 && docked)
3935 data = 3; /* dock */
3936 else {
3937 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3938 event, _sta(dock_handle));
3939 data = 0; /* unknown */
3941 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3942 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3943 dev_name(&ibm->acpi->device->dev),
3944 event, data);
3947 static int dock_read(char *p)
3949 int len = 0;
3950 int docked = dock_docked();
3952 if (!dock_handle)
3953 len += sprintf(p + len, "status:\t\tnot supported\n");
3954 else if (!docked)
3955 len += sprintf(p + len, "status:\t\tundocked\n");
3956 else {
3957 len += sprintf(p + len, "status:\t\tdocked\n");
3958 len += sprintf(p + len, "commands:\tdock, undock\n");
3961 return len;
3964 static int dock_write(char *buf)
3966 char *cmd;
3968 if (!dock_docked())
3969 return -ENODEV;
3971 while ((cmd = next_cmd(&buf))) {
3972 if (strlencmp(cmd, "undock") == 0) {
3973 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3974 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3975 return -EIO;
3976 } else if (strlencmp(cmd, "dock") == 0) {
3977 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3978 return -EIO;
3979 } else
3980 return -EINVAL;
3983 return 0;
3986 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3988 /*************************************************************************
3989 * Bay subdriver
3992 #ifdef CONFIG_THINKPAD_ACPI_BAY
3994 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3995 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3996 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3997 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3998 ); /* A21e, R30, R31 */
3999 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4000 "_EJ0", /* all others */
4001 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4002 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4003 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4004 ); /* all others */
4005 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4006 "_EJ0", /* 770x */
4007 ); /* all others */
4009 static int __init bay_init(struct ibm_init_struct *iibm)
4011 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4013 TPACPI_ACPIHANDLE_INIT(bay);
4014 if (bay_handle)
4015 TPACPI_ACPIHANDLE_INIT(bay_ej);
4016 TPACPI_ACPIHANDLE_INIT(bay2);
4017 if (bay2_handle)
4018 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4020 tp_features.bay_status = bay_handle &&
4021 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4022 tp_features.bay_status2 = bay2_handle &&
4023 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4025 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4026 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4027 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4028 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4030 vdbg_printk(TPACPI_DBG_INIT,
4031 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4032 str_supported(tp_features.bay_status),
4033 str_supported(tp_features.bay_eject),
4034 str_supported(tp_features.bay_status2),
4035 str_supported(tp_features.bay_eject2));
4037 return (tp_features.bay_status || tp_features.bay_eject ||
4038 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4041 static void bay_notify(struct ibm_struct *ibm, u32 event)
4043 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4044 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4045 dev_name(&ibm->acpi->device->dev),
4046 event, 0);
4049 #define bay_occupied(b) (_sta(b##_handle) & 1)
4051 static int bay_read(char *p)
4053 int len = 0;
4054 int occupied = bay_occupied(bay);
4055 int occupied2 = bay_occupied(bay2);
4056 int eject, eject2;
4058 len += sprintf(p + len, "status:\t\t%s\n",
4059 tp_features.bay_status ?
4060 (occupied ? "occupied" : "unoccupied") :
4061 "not supported");
4062 if (tp_features.bay_status2)
4063 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4064 "occupied" : "unoccupied");
4066 eject = tp_features.bay_eject && occupied;
4067 eject2 = tp_features.bay_eject2 && occupied2;
4069 if (eject && eject2)
4070 len += sprintf(p + len, "commands:\teject, eject2\n");
4071 else if (eject)
4072 len += sprintf(p + len, "commands:\teject\n");
4073 else if (eject2)
4074 len += sprintf(p + len, "commands:\teject2\n");
4076 return len;
4079 static int bay_write(char *buf)
4081 char *cmd;
4083 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4084 return -ENODEV;
4086 while ((cmd = next_cmd(&buf))) {
4087 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4088 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4089 return -EIO;
4090 } else if (tp_features.bay_eject2 &&
4091 strlencmp(cmd, "eject2") == 0) {
4092 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4093 return -EIO;
4094 } else
4095 return -EINVAL;
4098 return 0;
4101 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4102 .notify = bay_notify,
4103 .handle = &bay_handle,
4104 .type = ACPI_SYSTEM_NOTIFY,
4107 static struct ibm_struct bay_driver_data = {
4108 .name = "bay",
4109 .read = bay_read,
4110 .write = bay_write,
4111 .acpi = &ibm_bay_acpidriver,
4114 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4116 /*************************************************************************
4117 * CMOS subdriver
4120 /* sysfs cmos_command -------------------------------------------------- */
4121 static ssize_t cmos_command_store(struct device *dev,
4122 struct device_attribute *attr,
4123 const char *buf, size_t count)
4125 unsigned long cmos_cmd;
4126 int res;
4128 if (parse_strtoul(buf, 21, &cmos_cmd))
4129 return -EINVAL;
4131 res = issue_thinkpad_cmos_command(cmos_cmd);
4132 return (res)? res : count;
4135 static struct device_attribute dev_attr_cmos_command =
4136 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4138 /* --------------------------------------------------------------------- */
4140 static int __init cmos_init(struct ibm_init_struct *iibm)
4142 int res;
4144 vdbg_printk(TPACPI_DBG_INIT,
4145 "initializing cmos commands subdriver\n");
4147 TPACPI_ACPIHANDLE_INIT(cmos);
4149 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4150 str_supported(cmos_handle != NULL));
4152 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4153 if (res)
4154 return res;
4156 return (cmos_handle)? 0 : 1;
4159 static void cmos_exit(void)
4161 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4164 static int cmos_read(char *p)
4166 int len = 0;
4168 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4169 R30, R31, T20-22, X20-21 */
4170 if (!cmos_handle)
4171 len += sprintf(p + len, "status:\t\tnot supported\n");
4172 else {
4173 len += sprintf(p + len, "status:\t\tsupported\n");
4174 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4177 return len;
4180 static int cmos_write(char *buf)
4182 char *cmd;
4183 int cmos_cmd, res;
4185 while ((cmd = next_cmd(&buf))) {
4186 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4187 cmos_cmd >= 0 && cmos_cmd <= 21) {
4188 /* cmos_cmd set */
4189 } else
4190 return -EINVAL;
4192 res = issue_thinkpad_cmos_command(cmos_cmd);
4193 if (res)
4194 return res;
4197 return 0;
4200 static struct ibm_struct cmos_driver_data = {
4201 .name = "cmos",
4202 .read = cmos_read,
4203 .write = cmos_write,
4204 .exit = cmos_exit,
4207 /*************************************************************************
4208 * LED subdriver
4211 enum led_access_mode {
4212 TPACPI_LED_NONE = 0,
4213 TPACPI_LED_570, /* 570 */
4214 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4215 TPACPI_LED_NEW, /* all others */
4218 enum { /* For TPACPI_LED_OLD */
4219 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4220 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4221 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4224 enum led_status_t {
4225 TPACPI_LED_OFF = 0,
4226 TPACPI_LED_ON,
4227 TPACPI_LED_BLINK,
4230 static enum led_access_mode led_supported;
4232 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4233 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4234 /* T20-22, X20-21 */
4235 "LED", /* all others */
4236 ); /* R30, R31 */
4238 #define TPACPI_LED_NUMLEDS 8
4239 static struct tpacpi_led_classdev *tpacpi_leds;
4240 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4241 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4242 /* there's a limit of 19 chars + NULL before 2.6.26 */
4243 "tpacpi::power",
4244 "tpacpi:orange:batt",
4245 "tpacpi:green:batt",
4246 "tpacpi::dock_active",
4247 "tpacpi::bay_active",
4248 "tpacpi::dock_batt",
4249 "tpacpi::unknown_led",
4250 "tpacpi::standby",
4253 static int led_get_status(const unsigned int led)
4255 int status;
4256 enum led_status_t led_s;
4258 switch (led_supported) {
4259 case TPACPI_LED_570:
4260 if (!acpi_evalf(ec_handle,
4261 &status, "GLED", "dd", 1 << led))
4262 return -EIO;
4263 led_s = (status == 0)?
4264 TPACPI_LED_OFF :
4265 ((status == 1)?
4266 TPACPI_LED_ON :
4267 TPACPI_LED_BLINK);
4268 tpacpi_led_state_cache[led] = led_s;
4269 return led_s;
4270 default:
4271 return -ENXIO;
4274 /* not reached */
4277 static int led_set_status(const unsigned int led,
4278 const enum led_status_t ledstatus)
4280 /* off, on, blink. Index is led_status_t */
4281 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4282 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4284 int rc = 0;
4286 switch (led_supported) {
4287 case TPACPI_LED_570:
4288 /* 570 */
4289 if (led > 7)
4290 return -EINVAL;
4291 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4292 (1 << led), led_sled_arg1[ledstatus]))
4293 rc = -EIO;
4294 break;
4295 case TPACPI_LED_OLD:
4296 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4297 if (led > 7)
4298 return -EINVAL;
4299 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4300 if (rc >= 0)
4301 rc = ec_write(TPACPI_LED_EC_HLBL,
4302 (ledstatus == TPACPI_LED_BLINK) << led);
4303 if (rc >= 0)
4304 rc = ec_write(TPACPI_LED_EC_HLCL,
4305 (ledstatus != TPACPI_LED_OFF) << led);
4306 break;
4307 case TPACPI_LED_NEW:
4308 /* all others */
4309 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4310 led, led_led_arg1[ledstatus]))
4311 rc = -EIO;
4312 break;
4313 default:
4314 rc = -ENXIO;
4317 if (!rc)
4318 tpacpi_led_state_cache[led] = ledstatus;
4320 return rc;
4323 static void led_sysfs_set_status(unsigned int led,
4324 enum led_brightness brightness)
4326 led_set_status(led,
4327 (brightness == LED_OFF) ?
4328 TPACPI_LED_OFF :
4329 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4330 TPACPI_LED_BLINK : TPACPI_LED_ON);
4333 static void led_set_status_worker(struct work_struct *work)
4335 struct tpacpi_led_classdev *data =
4336 container_of(work, struct tpacpi_led_classdev, work);
4338 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4339 led_sysfs_set_status(data->led, data->new_brightness);
4342 static void led_sysfs_set(struct led_classdev *led_cdev,
4343 enum led_brightness brightness)
4345 struct tpacpi_led_classdev *data = container_of(led_cdev,
4346 struct tpacpi_led_classdev, led_classdev);
4348 data->new_brightness = brightness;
4349 queue_work(tpacpi_wq, &data->work);
4352 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4353 unsigned long *delay_on, unsigned long *delay_off)
4355 struct tpacpi_led_classdev *data = container_of(led_cdev,
4356 struct tpacpi_led_classdev, led_classdev);
4358 /* Can we choose the flash rate? */
4359 if (*delay_on == 0 && *delay_off == 0) {
4360 /* yes. set them to the hardware blink rate (1 Hz) */
4361 *delay_on = 500; /* ms */
4362 *delay_off = 500; /* ms */
4363 } else if ((*delay_on != 500) || (*delay_off != 500))
4364 return -EINVAL;
4366 data->new_brightness = TPACPI_LED_BLINK;
4367 queue_work(tpacpi_wq, &data->work);
4369 return 0;
4372 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4374 int rc;
4376 struct tpacpi_led_classdev *data = container_of(led_cdev,
4377 struct tpacpi_led_classdev, led_classdev);
4379 rc = led_get_status(data->led);
4381 if (rc == TPACPI_LED_OFF || rc < 0)
4382 rc = LED_OFF; /* no error handling in led class :( */
4383 else
4384 rc = LED_FULL;
4386 return rc;
4389 static void led_exit(void)
4391 unsigned int i;
4393 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4394 if (tpacpi_leds[i].led_classdev.name)
4395 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4398 kfree(tpacpi_leds);
4401 static int __init led_init(struct ibm_init_struct *iibm)
4403 unsigned int i;
4404 int rc;
4406 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4408 TPACPI_ACPIHANDLE_INIT(led);
4410 if (!led_handle)
4411 /* led not supported on R30, R31 */
4412 led_supported = TPACPI_LED_NONE;
4413 else if (strlencmp(led_path, "SLED") == 0)
4414 /* 570 */
4415 led_supported = TPACPI_LED_570;
4416 else if (strlencmp(led_path, "SYSL") == 0)
4417 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4418 led_supported = TPACPI_LED_OLD;
4419 else
4420 /* all others */
4421 led_supported = TPACPI_LED_NEW;
4423 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4424 str_supported(led_supported), led_supported);
4426 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4427 GFP_KERNEL);
4428 if (!tpacpi_leds) {
4429 printk(TPACPI_ERR "Out of memory for LED data\n");
4430 return -ENOMEM;
4433 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4434 tpacpi_leds[i].led = i;
4436 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4437 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4438 if (led_supported == TPACPI_LED_570)
4439 tpacpi_leds[i].led_classdev.brightness_get =
4440 &led_sysfs_get;
4442 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4444 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4446 rc = led_classdev_register(&tpacpi_pdev->dev,
4447 &tpacpi_leds[i].led_classdev);
4448 if (rc < 0) {
4449 tpacpi_leds[i].led_classdev.name = NULL;
4450 led_exit();
4451 return rc;
4455 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4458 #define str_led_status(s) \
4459 ((s) == TPACPI_LED_OFF ? "off" : \
4460 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4462 static int led_read(char *p)
4464 int len = 0;
4466 if (!led_supported) {
4467 len += sprintf(p + len, "status:\t\tnot supported\n");
4468 return len;
4470 len += sprintf(p + len, "status:\t\tsupported\n");
4472 if (led_supported == TPACPI_LED_570) {
4473 /* 570 */
4474 int i, status;
4475 for (i = 0; i < 8; i++) {
4476 status = led_get_status(i);
4477 if (status < 0)
4478 return -EIO;
4479 len += sprintf(p + len, "%d:\t\t%s\n",
4480 i, str_led_status(status));
4484 len += sprintf(p + len, "commands:\t"
4485 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4487 return len;
4490 static int led_write(char *buf)
4492 char *cmd;
4493 int led, rc;
4494 enum led_status_t s;
4496 if (!led_supported)
4497 return -ENODEV;
4499 while ((cmd = next_cmd(&buf))) {
4500 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4501 return -EINVAL;
4503 if (strstr(cmd, "off")) {
4504 s = TPACPI_LED_OFF;
4505 } else if (strstr(cmd, "on")) {
4506 s = TPACPI_LED_ON;
4507 } else if (strstr(cmd, "blink")) {
4508 s = TPACPI_LED_BLINK;
4509 } else {
4510 return -EINVAL;
4513 rc = led_set_status(led, s);
4514 if (rc < 0)
4515 return rc;
4518 return 0;
4521 static struct ibm_struct led_driver_data = {
4522 .name = "led",
4523 .read = led_read,
4524 .write = led_write,
4525 .exit = led_exit,
4528 /*************************************************************************
4529 * Beep subdriver
4532 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4534 static int __init beep_init(struct ibm_init_struct *iibm)
4536 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4538 TPACPI_ACPIHANDLE_INIT(beep);
4540 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4541 str_supported(beep_handle != NULL));
4543 return (beep_handle)? 0 : 1;
4546 static int beep_read(char *p)
4548 int len = 0;
4550 if (!beep_handle)
4551 len += sprintf(p + len, "status:\t\tnot supported\n");
4552 else {
4553 len += sprintf(p + len, "status:\t\tsupported\n");
4554 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4557 return len;
4560 static int beep_write(char *buf)
4562 char *cmd;
4563 int beep_cmd;
4565 if (!beep_handle)
4566 return -ENODEV;
4568 while ((cmd = next_cmd(&buf))) {
4569 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4570 beep_cmd >= 0 && beep_cmd <= 17) {
4571 /* beep_cmd set */
4572 } else
4573 return -EINVAL;
4574 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4575 return -EIO;
4578 return 0;
4581 static struct ibm_struct beep_driver_data = {
4582 .name = "beep",
4583 .read = beep_read,
4584 .write = beep_write,
4587 /*************************************************************************
4588 * Thermal subdriver
4591 enum thermal_access_mode {
4592 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4593 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4594 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4595 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4596 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4599 enum { /* TPACPI_THERMAL_TPEC_* */
4600 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4601 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4602 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4605 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4606 struct ibm_thermal_sensors_struct {
4607 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4610 static enum thermal_access_mode thermal_read_mode;
4612 /* idx is zero-based */
4613 static int thermal_get_sensor(int idx, s32 *value)
4615 int t;
4616 s8 tmp;
4617 char tmpi[5];
4619 t = TP_EC_THERMAL_TMP0;
4621 switch (thermal_read_mode) {
4622 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4623 case TPACPI_THERMAL_TPEC_16:
4624 if (idx >= 8 && idx <= 15) {
4625 t = TP_EC_THERMAL_TMP8;
4626 idx -= 8;
4628 /* fallthrough */
4629 #endif
4630 case TPACPI_THERMAL_TPEC_8:
4631 if (idx <= 7) {
4632 if (!acpi_ec_read(t + idx, &tmp))
4633 return -EIO;
4634 *value = tmp * 1000;
4635 return 0;
4637 break;
4639 case TPACPI_THERMAL_ACPI_UPDT:
4640 if (idx <= 7) {
4641 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4642 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4643 return -EIO;
4644 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4645 return -EIO;
4646 *value = (t - 2732) * 100;
4647 return 0;
4649 break;
4651 case TPACPI_THERMAL_ACPI_TMP07:
4652 if (idx <= 7) {
4653 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4654 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4655 return -EIO;
4656 if (t > 127 || t < -127)
4657 t = TP_EC_THERMAL_TMP_NA;
4658 *value = t * 1000;
4659 return 0;
4661 break;
4663 case TPACPI_THERMAL_NONE:
4664 default:
4665 return -ENOSYS;
4668 return -EINVAL;
4671 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4673 int res, i;
4674 int n;
4676 n = 8;
4677 i = 0;
4679 if (!s)
4680 return -EINVAL;
4682 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4683 n = 16;
4685 for (i = 0 ; i < n; i++) {
4686 res = thermal_get_sensor(i, &s->temp[i]);
4687 if (res)
4688 return res;
4691 return n;
4694 /* sysfs temp##_input -------------------------------------------------- */
4696 static ssize_t thermal_temp_input_show(struct device *dev,
4697 struct device_attribute *attr,
4698 char *buf)
4700 struct sensor_device_attribute *sensor_attr =
4701 to_sensor_dev_attr(attr);
4702 int idx = sensor_attr->index;
4703 s32 value;
4704 int res;
4706 res = thermal_get_sensor(idx, &value);
4707 if (res)
4708 return res;
4709 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4710 return -ENXIO;
4712 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4715 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4716 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4717 thermal_temp_input_show, NULL, _idxB)
4719 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4720 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4721 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4722 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4723 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4724 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4725 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4726 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4727 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4728 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4729 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4730 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4731 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4732 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4733 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4734 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4735 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4738 #define THERMAL_ATTRS(X) \
4739 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4741 static struct attribute *thermal_temp_input_attr[] = {
4742 THERMAL_ATTRS(8),
4743 THERMAL_ATTRS(9),
4744 THERMAL_ATTRS(10),
4745 THERMAL_ATTRS(11),
4746 THERMAL_ATTRS(12),
4747 THERMAL_ATTRS(13),
4748 THERMAL_ATTRS(14),
4749 THERMAL_ATTRS(15),
4750 THERMAL_ATTRS(0),
4751 THERMAL_ATTRS(1),
4752 THERMAL_ATTRS(2),
4753 THERMAL_ATTRS(3),
4754 THERMAL_ATTRS(4),
4755 THERMAL_ATTRS(5),
4756 THERMAL_ATTRS(6),
4757 THERMAL_ATTRS(7),
4758 NULL
4761 static const struct attribute_group thermal_temp_input16_group = {
4762 .attrs = thermal_temp_input_attr
4765 static const struct attribute_group thermal_temp_input8_group = {
4766 .attrs = &thermal_temp_input_attr[8]
4769 #undef THERMAL_SENSOR_ATTR_TEMP
4770 #undef THERMAL_ATTRS
4772 /* --------------------------------------------------------------------- */
4774 static int __init thermal_init(struct ibm_init_struct *iibm)
4776 u8 t, ta1, ta2;
4777 int i;
4778 int acpi_tmp7;
4779 int res;
4781 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4783 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4785 if (thinkpad_id.ec_model) {
4787 * Direct EC access mode: sensors at registers
4788 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4789 * non-implemented, thermal sensors return 0x80 when
4790 * not available
4793 ta1 = ta2 = 0;
4794 for (i = 0; i < 8; i++) {
4795 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4796 ta1 |= t;
4797 } else {
4798 ta1 = 0;
4799 break;
4801 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4802 ta2 |= t;
4803 } else {
4804 ta1 = 0;
4805 break;
4808 if (ta1 == 0) {
4809 /* This is sheer paranoia, but we handle it anyway */
4810 if (acpi_tmp7) {
4811 printk(TPACPI_ERR
4812 "ThinkPad ACPI EC access misbehaving, "
4813 "falling back to ACPI TMPx access "
4814 "mode\n");
4815 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4816 } else {
4817 printk(TPACPI_ERR
4818 "ThinkPad ACPI EC access misbehaving, "
4819 "disabling thermal sensors access\n");
4820 thermal_read_mode = TPACPI_THERMAL_NONE;
4822 } else {
4823 thermal_read_mode =
4824 (ta2 != 0) ?
4825 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4827 } else if (acpi_tmp7) {
4828 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4829 /* 600e/x, 770e, 770x */
4830 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4831 } else {
4832 /* Standard ACPI TMPx access, max 8 sensors */
4833 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4835 } else {
4836 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4837 thermal_read_mode = TPACPI_THERMAL_NONE;
4840 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4841 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4842 thermal_read_mode);
4844 switch (thermal_read_mode) {
4845 case TPACPI_THERMAL_TPEC_16:
4846 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4847 &thermal_temp_input16_group);
4848 if (res)
4849 return res;
4850 break;
4851 case TPACPI_THERMAL_TPEC_8:
4852 case TPACPI_THERMAL_ACPI_TMP07:
4853 case TPACPI_THERMAL_ACPI_UPDT:
4854 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4855 &thermal_temp_input8_group);
4856 if (res)
4857 return res;
4858 break;
4859 case TPACPI_THERMAL_NONE:
4860 default:
4861 return 1;
4864 return 0;
4867 static void thermal_exit(void)
4869 switch (thermal_read_mode) {
4870 case TPACPI_THERMAL_TPEC_16:
4871 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4872 &thermal_temp_input16_group);
4873 break;
4874 case TPACPI_THERMAL_TPEC_8:
4875 case TPACPI_THERMAL_ACPI_TMP07:
4876 case TPACPI_THERMAL_ACPI_UPDT:
4877 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4878 &thermal_temp_input16_group);
4879 break;
4880 case TPACPI_THERMAL_NONE:
4881 default:
4882 break;
4886 static int thermal_read(char *p)
4888 int len = 0;
4889 int n, i;
4890 struct ibm_thermal_sensors_struct t;
4892 n = thermal_get_sensors(&t);
4893 if (unlikely(n < 0))
4894 return n;
4896 len += sprintf(p + len, "temperatures:\t");
4898 if (n > 0) {
4899 for (i = 0; i < (n - 1); i++)
4900 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4901 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4902 } else
4903 len += sprintf(p + len, "not supported\n");
4905 return len;
4908 static struct ibm_struct thermal_driver_data = {
4909 .name = "thermal",
4910 .read = thermal_read,
4911 .exit = thermal_exit,
4914 /*************************************************************************
4915 * EC Dump subdriver
4918 static u8 ecdump_regs[256];
4920 static int ecdump_read(char *p)
4922 int len = 0;
4923 int i, j;
4924 u8 v;
4926 len += sprintf(p + len, "EC "
4927 " +00 +01 +02 +03 +04 +05 +06 +07"
4928 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4929 for (i = 0; i < 256; i += 16) {
4930 len += sprintf(p + len, "EC 0x%02x:", i);
4931 for (j = 0; j < 16; j++) {
4932 if (!acpi_ec_read(i + j, &v))
4933 break;
4934 if (v != ecdump_regs[i + j])
4935 len += sprintf(p + len, " *%02x", v);
4936 else
4937 len += sprintf(p + len, " %02x", v);
4938 ecdump_regs[i + j] = v;
4940 len += sprintf(p + len, "\n");
4941 if (j != 16)
4942 break;
4945 /* These are way too dangerous to advertise openly... */
4946 #if 0
4947 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4948 " (<offset> is 00-ff, <value> is 00-ff)\n");
4949 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4950 " (<offset> is 00-ff, <value> is 0-255)\n");
4951 #endif
4952 return len;
4955 static int ecdump_write(char *buf)
4957 char *cmd;
4958 int i, v;
4960 while ((cmd = next_cmd(&buf))) {
4961 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4962 /* i and v set */
4963 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4964 /* i and v set */
4965 } else
4966 return -EINVAL;
4967 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4968 if (!acpi_ec_write(i, v))
4969 return -EIO;
4970 } else
4971 return -EINVAL;
4974 return 0;
4977 static struct ibm_struct ecdump_driver_data = {
4978 .name = "ecdump",
4979 .read = ecdump_read,
4980 .write = ecdump_write,
4981 .flags.experimental = 1,
4984 /*************************************************************************
4985 * Backlight/brightness subdriver
4988 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4990 enum {
4991 TP_EC_BACKLIGHT = 0x31,
4993 /* TP_EC_BACKLIGHT bitmasks */
4994 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4995 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4996 TP_EC_BACKLIGHT_MAPSW = 0x20,
4999 static struct backlight_device *ibm_backlight_device;
5000 static int brightness_mode;
5001 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5003 static struct mutex brightness_mutex;
5006 * ThinkPads can read brightness from two places: EC 0x31, or
5007 * CMOS NVRAM byte 0x5E, bits 0-3.
5009 * EC 0x31 has the following layout
5010 * Bit 7: unknown function
5011 * Bit 6: unknown function
5012 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5013 * Bit 4: must be set to zero to avoid problems
5014 * Bit 3-0: backlight brightness level
5016 * brightness_get_raw returns status data in the EC 0x31 layout
5018 static int brightness_get_raw(int *status)
5020 u8 lec = 0, lcmos = 0, level = 0;
5022 if (brightness_mode & 1) {
5023 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5024 return -EIO;
5025 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5027 if (brightness_mode & 2) {
5028 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5029 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5030 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5031 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5032 level = lcmos;
5035 if (brightness_mode == 3) {
5036 *status = lec; /* Prefer EC, CMOS is just a backing store */
5037 lec &= TP_EC_BACKLIGHT_LVLMSK;
5038 if (lec == lcmos)
5039 tp_warned.bright_cmos_ec_unsync = 0;
5040 else {
5041 if (!tp_warned.bright_cmos_ec_unsync) {
5042 printk(TPACPI_ERR
5043 "CMOS NVRAM (%u) and EC (%u) do not "
5044 "agree on display brightness level\n",
5045 (unsigned int) lcmos,
5046 (unsigned int) lec);
5047 tp_warned.bright_cmos_ec_unsync = 1;
5049 return -EIO;
5051 } else {
5052 *status = level;
5055 return 0;
5058 /* May return EINTR which can always be mapped to ERESTARTSYS */
5059 static int brightness_set(int value)
5061 int cmos_cmd, inc, i, res;
5062 int current_value;
5063 int command_bits;
5065 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5066 value < 0)
5067 return -EINVAL;
5069 res = mutex_lock_interruptible(&brightness_mutex);
5070 if (res < 0)
5071 return res;
5073 res = brightness_get_raw(&current_value);
5074 if (res < 0)
5075 goto errout;
5077 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5078 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5080 cmos_cmd = value > current_value ?
5081 TP_CMOS_BRIGHTNESS_UP :
5082 TP_CMOS_BRIGHTNESS_DOWN;
5083 inc = (value > current_value)? 1 : -1;
5085 res = 0;
5086 for (i = current_value; i != value; i += inc) {
5087 if ((brightness_mode & 2) &&
5088 issue_thinkpad_cmos_command(cmos_cmd)) {
5089 res = -EIO;
5090 goto errout;
5092 if ((brightness_mode & 1) &&
5093 !acpi_ec_write(TP_EC_BACKLIGHT,
5094 (i + inc) | command_bits)) {
5095 res = -EIO;
5096 goto errout;;
5100 errout:
5101 mutex_unlock(&brightness_mutex);
5102 return res;
5105 /* sysfs backlight class ----------------------------------------------- */
5107 static int brightness_update_status(struct backlight_device *bd)
5109 /* it is the backlight class's job (caller) to handle
5110 * EINTR and other errors properly */
5111 return brightness_set(
5112 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5113 bd->props.power == FB_BLANK_UNBLANK) ?
5114 bd->props.brightness : 0);
5117 static int brightness_get(struct backlight_device *bd)
5119 int status, res;
5121 res = brightness_get_raw(&status);
5122 if (res < 0)
5123 return 0; /* FIXME: teach backlight about error handling */
5125 return status & TP_EC_BACKLIGHT_LVLMSK;
5128 static struct backlight_ops ibm_backlight_data = {
5129 .get_brightness = brightness_get,
5130 .update_status = brightness_update_status,
5133 /* --------------------------------------------------------------------- */
5135 static int __init brightness_init(struct ibm_init_struct *iibm)
5137 int b;
5139 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5141 mutex_init(&brightness_mutex);
5144 * We always attempt to detect acpi support, so as to switch
5145 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5146 * going to publish a backlight interface
5148 b = tpacpi_check_std_acpi_brightness_support();
5149 if (b > 0) {
5151 if (acpi_video_backlight_support()) {
5152 if (brightness_enable > 1) {
5153 printk(TPACPI_NOTICE
5154 "Standard ACPI backlight interface "
5155 "available, not loading native one.\n");
5156 return 1;
5157 } else if (brightness_enable == 1) {
5158 printk(TPACPI_NOTICE
5159 "Backlight control force enabled, even if standard "
5160 "ACPI backlight interface is available\n");
5162 } else {
5163 if (brightness_enable > 1) {
5164 printk(TPACPI_NOTICE
5165 "Standard ACPI backlight interface not "
5166 "available, thinkpad_acpi native "
5167 "brightness control enabled\n");
5172 if (!brightness_enable) {
5173 dbg_printk(TPACPI_DBG_INIT,
5174 "brightness support disabled by "
5175 "module parameter\n");
5176 return 1;
5179 if (b > 16) {
5180 printk(TPACPI_ERR
5181 "Unsupported brightness interface, "
5182 "please contact %s\n", TPACPI_MAIL);
5183 return 1;
5185 if (b == 16)
5186 tp_features.bright_16levels = 1;
5188 if (!brightness_mode) {
5189 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5190 brightness_mode = 2;
5191 else
5192 brightness_mode = 3;
5194 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5195 brightness_mode);
5198 if (brightness_mode > 3)
5199 return -EINVAL;
5201 if (brightness_get_raw(&b) < 0)
5202 return 1;
5204 if (tp_features.bright_16levels)
5205 printk(TPACPI_INFO
5206 "detected a 16-level brightness capable ThinkPad\n");
5208 ibm_backlight_device = backlight_device_register(
5209 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5210 &ibm_backlight_data);
5211 if (IS_ERR(ibm_backlight_device)) {
5212 printk(TPACPI_ERR "Could not register backlight device\n");
5213 return PTR_ERR(ibm_backlight_device);
5215 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5217 ibm_backlight_device->props.max_brightness =
5218 (tp_features.bright_16levels)? 15 : 7;
5219 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5220 backlight_update_status(ibm_backlight_device);
5222 return 0;
5225 static void brightness_exit(void)
5227 if (ibm_backlight_device) {
5228 vdbg_printk(TPACPI_DBG_EXIT,
5229 "calling backlight_device_unregister()\n");
5230 backlight_device_unregister(ibm_backlight_device);
5234 static int brightness_read(char *p)
5236 int len = 0;
5237 int level;
5239 level = brightness_get(NULL);
5240 if (level < 0) {
5241 len += sprintf(p + len, "level:\t\tunreadable\n");
5242 } else {
5243 len += sprintf(p + len, "level:\t\t%d\n", level);
5244 len += sprintf(p + len, "commands:\tup, down\n");
5245 len += sprintf(p + len, "commands:\tlevel <level>"
5246 " (<level> is 0-%d)\n",
5247 (tp_features.bright_16levels) ? 15 : 7);
5250 return len;
5253 static int brightness_write(char *buf)
5255 int level;
5256 int rc;
5257 char *cmd;
5258 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5260 level = brightness_get(NULL);
5261 if (level < 0)
5262 return level;
5264 while ((cmd = next_cmd(&buf))) {
5265 if (strlencmp(cmd, "up") == 0) {
5266 if (level < max_level)
5267 level++;
5268 } else if (strlencmp(cmd, "down") == 0) {
5269 if (level > 0)
5270 level--;
5271 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5272 level >= 0 && level <= max_level) {
5273 /* new level set */
5274 } else
5275 return -EINVAL;
5279 * Now we know what the final level should be, so we try to set it.
5280 * Doing it this way makes the syscall restartable in case of EINTR
5282 rc = brightness_set(level);
5283 return (rc == -EINTR)? ERESTARTSYS : rc;
5286 static struct ibm_struct brightness_driver_data = {
5287 .name = "brightness",
5288 .read = brightness_read,
5289 .write = brightness_write,
5290 .exit = brightness_exit,
5293 /*************************************************************************
5294 * Volume subdriver
5297 static int volume_offset = 0x30;
5299 static int volume_read(char *p)
5301 int len = 0;
5302 u8 level;
5304 if (!acpi_ec_read(volume_offset, &level)) {
5305 len += sprintf(p + len, "level:\t\tunreadable\n");
5306 } else {
5307 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5308 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5309 len += sprintf(p + len, "commands:\tup, down, mute\n");
5310 len += sprintf(p + len, "commands:\tlevel <level>"
5311 " (<level> is 0-15)\n");
5314 return len;
5317 static int volume_write(char *buf)
5319 int cmos_cmd, inc, i;
5320 u8 level, mute;
5321 int new_level, new_mute;
5322 char *cmd;
5324 while ((cmd = next_cmd(&buf))) {
5325 if (!acpi_ec_read(volume_offset, &level))
5326 return -EIO;
5327 new_mute = mute = level & 0x40;
5328 new_level = level = level & 0xf;
5330 if (strlencmp(cmd, "up") == 0) {
5331 if (mute)
5332 new_mute = 0;
5333 else
5334 new_level = level == 15 ? 15 : level + 1;
5335 } else if (strlencmp(cmd, "down") == 0) {
5336 if (mute)
5337 new_mute = 0;
5338 else
5339 new_level = level == 0 ? 0 : level - 1;
5340 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5341 new_level >= 0 && new_level <= 15) {
5342 /* new_level set */
5343 } else if (strlencmp(cmd, "mute") == 0) {
5344 new_mute = 0x40;
5345 } else
5346 return -EINVAL;
5348 if (new_level != level) {
5349 /* mute doesn't change */
5351 cmos_cmd = (new_level > level) ?
5352 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5353 inc = new_level > level ? 1 : -1;
5355 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5356 !acpi_ec_write(volume_offset, level)))
5357 return -EIO;
5359 for (i = level; i != new_level; i += inc)
5360 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5361 !acpi_ec_write(volume_offset, i + inc))
5362 return -EIO;
5364 if (mute &&
5365 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5366 !acpi_ec_write(volume_offset, new_level + mute))) {
5367 return -EIO;
5371 if (new_mute != mute) {
5372 /* level doesn't change */
5374 cmos_cmd = (new_mute) ?
5375 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5377 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5378 !acpi_ec_write(volume_offset, level + new_mute))
5379 return -EIO;
5383 return 0;
5386 static struct ibm_struct volume_driver_data = {
5387 .name = "volume",
5388 .read = volume_read,
5389 .write = volume_write,
5392 /*************************************************************************
5393 * Fan subdriver
5397 * FAN ACCESS MODES
5399 * TPACPI_FAN_RD_ACPI_GFAN:
5400 * ACPI GFAN method: returns fan level
5402 * see TPACPI_FAN_WR_ACPI_SFAN
5403 * EC 0x2f (HFSP) not available if GFAN exists
5405 * TPACPI_FAN_WR_ACPI_SFAN:
5406 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5408 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5409 * it for writing.
5411 * TPACPI_FAN_WR_TPEC:
5412 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5413 * Supported on almost all ThinkPads
5415 * Fan speed changes of any sort (including those caused by the
5416 * disengaged mode) are usually done slowly by the firmware as the
5417 * maximum ammount of fan duty cycle change per second seems to be
5418 * limited.
5420 * Reading is not available if GFAN exists.
5421 * Writing is not available if SFAN exists.
5423 * Bits
5424 * 7 automatic mode engaged;
5425 * (default operation mode of the ThinkPad)
5426 * fan level is ignored in this mode.
5427 * 6 full speed mode (takes precedence over bit 7);
5428 * not available on all thinkpads. May disable
5429 * the tachometer while the fan controller ramps up
5430 * the speed (which can take up to a few *minutes*).
5431 * Speeds up fan to 100% duty-cycle, which is far above
5432 * the standard RPM levels. It is not impossible that
5433 * it could cause hardware damage.
5434 * 5-3 unused in some models. Extra bits for fan level
5435 * in others, but still useless as all values above
5436 * 7 map to the same speed as level 7 in these models.
5437 * 2-0 fan level (0..7 usually)
5438 * 0x00 = stop
5439 * 0x07 = max (set when temperatures critical)
5440 * Some ThinkPads may have other levels, see
5441 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5443 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5444 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5445 * does so, its initial value is meaningless (0x07).
5447 * For firmware bugs, refer to:
5448 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5450 * ----
5452 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5453 * Main fan tachometer reading (in RPM)
5455 * This register is present on all ThinkPads with a new-style EC, and
5456 * it is known not to be present on the A21m/e, and T22, as there is
5457 * something else in offset 0x84 according to the ACPI DSDT. Other
5458 * ThinkPads from this same time period (and earlier) probably lack the
5459 * tachometer as well.
5461 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5462 * was never fixed by IBM to report the EC firmware version string
5463 * probably support the tachometer (like the early X models), so
5464 * detecting it is quite hard. We need more data to know for sure.
5466 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5467 * might result.
5469 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5470 * mode.
5472 * For firmware bugs, refer to:
5473 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5475 * TPACPI_FAN_WR_ACPI_FANS:
5476 * ThinkPad X31, X40, X41. Not available in the X60.
5478 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5479 * high speed. ACPI DSDT seems to map these three speeds to levels
5480 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5481 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5483 * The speeds are stored on handles
5484 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5486 * There are three default speed sets, acessible as handles:
5487 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5489 * ACPI DSDT switches which set is in use depending on various
5490 * factors.
5492 * TPACPI_FAN_WR_TPEC is also available and should be used to
5493 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5494 * but the ACPI tables just mention level 7.
5497 enum { /* Fan control constants */
5498 fan_status_offset = 0x2f, /* EC register 0x2f */
5499 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5500 * 0x84 must be read before 0x85 */
5502 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5503 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5505 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5508 enum fan_status_access_mode {
5509 TPACPI_FAN_NONE = 0, /* No fan status or control */
5510 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5511 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5514 enum fan_control_access_mode {
5515 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5516 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5517 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5518 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5521 enum fan_control_commands {
5522 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5523 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5524 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5525 * and also watchdog cmd */
5528 static int fan_control_allowed;
5530 static enum fan_status_access_mode fan_status_access_mode;
5531 static enum fan_control_access_mode fan_control_access_mode;
5532 static enum fan_control_commands fan_control_commands;
5534 static u8 fan_control_initial_status;
5535 static u8 fan_control_desired_level;
5536 static u8 fan_control_resume_level;
5537 static int fan_watchdog_maxinterval;
5539 static struct mutex fan_mutex;
5541 static void fan_watchdog_fire(struct work_struct *ignored);
5542 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5544 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5545 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5546 "\\FSPD", /* 600e/x, 770e, 770x */
5547 ); /* all others */
5548 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5549 "JFNS", /* 770x-JL */
5550 ); /* all others */
5553 * Call with fan_mutex held
5555 static void fan_update_desired_level(u8 status)
5557 if ((status &
5558 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5559 if (status > 7)
5560 fan_control_desired_level = 7;
5561 else
5562 fan_control_desired_level = status;
5566 static int fan_get_status(u8 *status)
5568 u8 s;
5570 /* TODO:
5571 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5573 switch (fan_status_access_mode) {
5574 case TPACPI_FAN_RD_ACPI_GFAN:
5575 /* 570, 600e/x, 770e, 770x */
5577 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5578 return -EIO;
5580 if (likely(status))
5581 *status = s & 0x07;
5583 break;
5585 case TPACPI_FAN_RD_TPEC:
5586 /* all except 570, 600e/x, 770e, 770x */
5587 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5588 return -EIO;
5590 if (likely(status))
5591 *status = s;
5593 break;
5595 default:
5596 return -ENXIO;
5599 return 0;
5602 static int fan_get_status_safe(u8 *status)
5604 int rc;
5605 u8 s;
5607 if (mutex_lock_interruptible(&fan_mutex))
5608 return -ERESTARTSYS;
5609 rc = fan_get_status(&s);
5610 if (!rc)
5611 fan_update_desired_level(s);
5612 mutex_unlock(&fan_mutex);
5614 if (status)
5615 *status = s;
5617 return rc;
5620 static int fan_get_speed(unsigned int *speed)
5622 u8 hi, lo;
5624 switch (fan_status_access_mode) {
5625 case TPACPI_FAN_RD_TPEC:
5626 /* all except 570, 600e/x, 770e, 770x */
5627 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5628 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5629 return -EIO;
5631 if (likely(speed))
5632 *speed = (hi << 8) | lo;
5634 break;
5636 default:
5637 return -ENXIO;
5640 return 0;
5643 static int fan_set_level(int level)
5645 if (!fan_control_allowed)
5646 return -EPERM;
5648 switch (fan_control_access_mode) {
5649 case TPACPI_FAN_WR_ACPI_SFAN:
5650 if (level >= 0 && level <= 7) {
5651 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5652 return -EIO;
5653 } else
5654 return -EINVAL;
5655 break;
5657 case TPACPI_FAN_WR_ACPI_FANS:
5658 case TPACPI_FAN_WR_TPEC:
5659 if (!(level & TP_EC_FAN_AUTO) &&
5660 !(level & TP_EC_FAN_FULLSPEED) &&
5661 ((level < 0) || (level > 7)))
5662 return -EINVAL;
5664 /* safety net should the EC not support AUTO
5665 * or FULLSPEED mode bits and just ignore them */
5666 if (level & TP_EC_FAN_FULLSPEED)
5667 level |= 7; /* safety min speed 7 */
5668 else if (level & TP_EC_FAN_AUTO)
5669 level |= 4; /* safety min speed 4 */
5671 if (!acpi_ec_write(fan_status_offset, level))
5672 return -EIO;
5673 else
5674 tp_features.fan_ctrl_status_undef = 0;
5675 break;
5677 default:
5678 return -ENXIO;
5680 return 0;
5683 static int fan_set_level_safe(int level)
5685 int rc;
5687 if (!fan_control_allowed)
5688 return -EPERM;
5690 if (mutex_lock_interruptible(&fan_mutex))
5691 return -ERESTARTSYS;
5693 if (level == TPACPI_FAN_LAST_LEVEL)
5694 level = fan_control_desired_level;
5696 rc = fan_set_level(level);
5697 if (!rc)
5698 fan_update_desired_level(level);
5700 mutex_unlock(&fan_mutex);
5701 return rc;
5704 static int fan_set_enable(void)
5706 u8 s;
5707 int rc;
5709 if (!fan_control_allowed)
5710 return -EPERM;
5712 if (mutex_lock_interruptible(&fan_mutex))
5713 return -ERESTARTSYS;
5715 switch (fan_control_access_mode) {
5716 case TPACPI_FAN_WR_ACPI_FANS:
5717 case TPACPI_FAN_WR_TPEC:
5718 rc = fan_get_status(&s);
5719 if (rc < 0)
5720 break;
5722 /* Don't go out of emergency fan mode */
5723 if (s != 7) {
5724 s &= 0x07;
5725 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5728 if (!acpi_ec_write(fan_status_offset, s))
5729 rc = -EIO;
5730 else {
5731 tp_features.fan_ctrl_status_undef = 0;
5732 rc = 0;
5734 break;
5736 case TPACPI_FAN_WR_ACPI_SFAN:
5737 rc = fan_get_status(&s);
5738 if (rc < 0)
5739 break;
5741 s &= 0x07;
5743 /* Set fan to at least level 4 */
5744 s |= 4;
5746 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5747 rc = -EIO;
5748 else
5749 rc = 0;
5750 break;
5752 default:
5753 rc = -ENXIO;
5756 mutex_unlock(&fan_mutex);
5757 return rc;
5760 static int fan_set_disable(void)
5762 int rc;
5764 if (!fan_control_allowed)
5765 return -EPERM;
5767 if (mutex_lock_interruptible(&fan_mutex))
5768 return -ERESTARTSYS;
5770 rc = 0;
5771 switch (fan_control_access_mode) {
5772 case TPACPI_FAN_WR_ACPI_FANS:
5773 case TPACPI_FAN_WR_TPEC:
5774 if (!acpi_ec_write(fan_status_offset, 0x00))
5775 rc = -EIO;
5776 else {
5777 fan_control_desired_level = 0;
5778 tp_features.fan_ctrl_status_undef = 0;
5780 break;
5782 case TPACPI_FAN_WR_ACPI_SFAN:
5783 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5784 rc = -EIO;
5785 else
5786 fan_control_desired_level = 0;
5787 break;
5789 default:
5790 rc = -ENXIO;
5794 mutex_unlock(&fan_mutex);
5795 return rc;
5798 static int fan_set_speed(int speed)
5800 int rc;
5802 if (!fan_control_allowed)
5803 return -EPERM;
5805 if (mutex_lock_interruptible(&fan_mutex))
5806 return -ERESTARTSYS;
5808 rc = 0;
5809 switch (fan_control_access_mode) {
5810 case TPACPI_FAN_WR_ACPI_FANS:
5811 if (speed >= 0 && speed <= 65535) {
5812 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5813 speed, speed, speed))
5814 rc = -EIO;
5815 } else
5816 rc = -EINVAL;
5817 break;
5819 default:
5820 rc = -ENXIO;
5823 mutex_unlock(&fan_mutex);
5824 return rc;
5827 static void fan_watchdog_reset(void)
5829 static int fan_watchdog_active;
5831 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5832 return;
5834 if (fan_watchdog_active)
5835 cancel_delayed_work(&fan_watchdog_task);
5837 if (fan_watchdog_maxinterval > 0 &&
5838 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5839 fan_watchdog_active = 1;
5840 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5841 msecs_to_jiffies(fan_watchdog_maxinterval
5842 * 1000))) {
5843 printk(TPACPI_ERR
5844 "failed to queue the fan watchdog, "
5845 "watchdog will not trigger\n");
5847 } else
5848 fan_watchdog_active = 0;
5851 static void fan_watchdog_fire(struct work_struct *ignored)
5853 int rc;
5855 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5856 return;
5858 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5859 rc = fan_set_enable();
5860 if (rc < 0) {
5861 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5862 "will try again later...\n", -rc);
5863 /* reschedule for later */
5864 fan_watchdog_reset();
5869 * SYSFS fan layout: hwmon compatible (device)
5871 * pwm*_enable:
5872 * 0: "disengaged" mode
5873 * 1: manual mode
5874 * 2: native EC "auto" mode (recommended, hardware default)
5876 * pwm*: set speed in manual mode, ignored otherwise.
5877 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5878 * interpolation.
5880 * fan*_input: tachometer reading, RPM
5883 * SYSFS fan layout: extensions
5885 * fan_watchdog (driver):
5886 * fan watchdog interval in seconds, 0 disables (default), max 120
5889 /* sysfs fan pwm1_enable ----------------------------------------------- */
5890 static ssize_t fan_pwm1_enable_show(struct device *dev,
5891 struct device_attribute *attr,
5892 char *buf)
5894 int res, mode;
5895 u8 status;
5897 res = fan_get_status_safe(&status);
5898 if (res)
5899 return res;
5901 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5902 if (status != fan_control_initial_status) {
5903 tp_features.fan_ctrl_status_undef = 0;
5904 } else {
5905 /* Return most likely status. In fact, it
5906 * might be the only possible status */
5907 status = TP_EC_FAN_AUTO;
5911 if (status & TP_EC_FAN_FULLSPEED) {
5912 mode = 0;
5913 } else if (status & TP_EC_FAN_AUTO) {
5914 mode = 2;
5915 } else
5916 mode = 1;
5918 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5921 static ssize_t fan_pwm1_enable_store(struct device *dev,
5922 struct device_attribute *attr,
5923 const char *buf, size_t count)
5925 unsigned long t;
5926 int res, level;
5928 if (parse_strtoul(buf, 2, &t))
5929 return -EINVAL;
5931 switch (t) {
5932 case 0:
5933 level = TP_EC_FAN_FULLSPEED;
5934 break;
5935 case 1:
5936 level = TPACPI_FAN_LAST_LEVEL;
5937 break;
5938 case 2:
5939 level = TP_EC_FAN_AUTO;
5940 break;
5941 case 3:
5942 /* reserved for software-controlled auto mode */
5943 return -ENOSYS;
5944 default:
5945 return -EINVAL;
5948 res = fan_set_level_safe(level);
5949 if (res == -ENXIO)
5950 return -EINVAL;
5951 else if (res < 0)
5952 return res;
5954 fan_watchdog_reset();
5956 return count;
5959 static struct device_attribute dev_attr_fan_pwm1_enable =
5960 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5961 fan_pwm1_enable_show, fan_pwm1_enable_store);
5963 /* sysfs fan pwm1 ------------------------------------------------------ */
5964 static ssize_t fan_pwm1_show(struct device *dev,
5965 struct device_attribute *attr,
5966 char *buf)
5968 int res;
5969 u8 status;
5971 res = fan_get_status_safe(&status);
5972 if (res)
5973 return res;
5975 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5976 if (status != fan_control_initial_status) {
5977 tp_features.fan_ctrl_status_undef = 0;
5978 } else {
5979 status = TP_EC_FAN_AUTO;
5983 if ((status &
5984 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5985 status = fan_control_desired_level;
5987 if (status > 7)
5988 status = 7;
5990 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5993 static ssize_t fan_pwm1_store(struct device *dev,
5994 struct device_attribute *attr,
5995 const char *buf, size_t count)
5997 unsigned long s;
5998 int rc;
5999 u8 status, newlevel;
6001 if (parse_strtoul(buf, 255, &s))
6002 return -EINVAL;
6004 /* scale down from 0-255 to 0-7 */
6005 newlevel = (s >> 5) & 0x07;
6007 if (mutex_lock_interruptible(&fan_mutex))
6008 return -ERESTARTSYS;
6010 rc = fan_get_status(&status);
6011 if (!rc && (status &
6012 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6013 rc = fan_set_level(newlevel);
6014 if (rc == -ENXIO)
6015 rc = -EINVAL;
6016 else if (!rc) {
6017 fan_update_desired_level(newlevel);
6018 fan_watchdog_reset();
6022 mutex_unlock(&fan_mutex);
6023 return (rc)? rc : count;
6026 static struct device_attribute dev_attr_fan_pwm1 =
6027 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6028 fan_pwm1_show, fan_pwm1_store);
6030 /* sysfs fan fan1_input ------------------------------------------------ */
6031 static ssize_t fan_fan1_input_show(struct device *dev,
6032 struct device_attribute *attr,
6033 char *buf)
6035 int res;
6036 unsigned int speed;
6038 res = fan_get_speed(&speed);
6039 if (res < 0)
6040 return res;
6042 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6045 static struct device_attribute dev_attr_fan_fan1_input =
6046 __ATTR(fan1_input, S_IRUGO,
6047 fan_fan1_input_show, NULL);
6049 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6050 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6051 char *buf)
6053 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6056 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6057 const char *buf, size_t count)
6059 unsigned long t;
6061 if (parse_strtoul(buf, 120, &t))
6062 return -EINVAL;
6064 if (!fan_control_allowed)
6065 return -EPERM;
6067 fan_watchdog_maxinterval = t;
6068 fan_watchdog_reset();
6070 return count;
6073 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6074 fan_fan_watchdog_show, fan_fan_watchdog_store);
6076 /* --------------------------------------------------------------------- */
6077 static struct attribute *fan_attributes[] = {
6078 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6079 &dev_attr_fan_fan1_input.attr,
6080 NULL
6083 static const struct attribute_group fan_attr_group = {
6084 .attrs = fan_attributes,
6087 static int __init fan_init(struct ibm_init_struct *iibm)
6089 int rc;
6091 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6093 mutex_init(&fan_mutex);
6094 fan_status_access_mode = TPACPI_FAN_NONE;
6095 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6096 fan_control_commands = 0;
6097 fan_watchdog_maxinterval = 0;
6098 tp_features.fan_ctrl_status_undef = 0;
6099 fan_control_desired_level = 7;
6101 TPACPI_ACPIHANDLE_INIT(fans);
6102 TPACPI_ACPIHANDLE_INIT(gfan);
6103 TPACPI_ACPIHANDLE_INIT(sfan);
6105 if (gfan_handle) {
6106 /* 570, 600e/x, 770e, 770x */
6107 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6108 } else {
6109 /* all other ThinkPads: note that even old-style
6110 * ThinkPad ECs supports the fan control register */
6111 if (likely(acpi_ec_read(fan_status_offset,
6112 &fan_control_initial_status))) {
6113 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6115 /* In some ThinkPads, neither the EC nor the ACPI
6116 * DSDT initialize the fan status, and it ends up
6117 * being set to 0x07 when it *could* be either
6118 * 0x07 or 0x80.
6120 * Enable for TP-1Y (T43), TP-78 (R51e),
6121 * TP-76 (R52), TP-70 (T43, R52), which are known
6122 * to be buggy. */
6123 if (fan_control_initial_status == 0x07) {
6124 switch (thinkpad_id.ec_model) {
6125 case 0x5931: /* TP-1Y */
6126 case 0x3837: /* TP-78 */
6127 case 0x3637: /* TP-76 */
6128 case 0x3037: /* TP-70 */
6129 printk(TPACPI_NOTICE
6130 "fan_init: initial fan status "
6131 "is unknown, assuming it is "
6132 "in auto mode\n");
6133 tp_features.fan_ctrl_status_undef = 1;
6137 } else {
6138 printk(TPACPI_ERR
6139 "ThinkPad ACPI EC access misbehaving, "
6140 "fan status and control unavailable\n");
6141 return 1;
6145 if (sfan_handle) {
6146 /* 570, 770x-JL */
6147 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6148 fan_control_commands |=
6149 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6150 } else {
6151 if (!gfan_handle) {
6152 /* gfan without sfan means no fan control */
6153 /* all other models implement TP EC 0x2f control */
6155 if (fans_handle) {
6156 /* X31, X40, X41 */
6157 fan_control_access_mode =
6158 TPACPI_FAN_WR_ACPI_FANS;
6159 fan_control_commands |=
6160 TPACPI_FAN_CMD_SPEED |
6161 TPACPI_FAN_CMD_LEVEL |
6162 TPACPI_FAN_CMD_ENABLE;
6163 } else {
6164 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6165 fan_control_commands |=
6166 TPACPI_FAN_CMD_LEVEL |
6167 TPACPI_FAN_CMD_ENABLE;
6172 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6173 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6174 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6175 fan_status_access_mode, fan_control_access_mode);
6177 /* fan control master switch */
6178 if (!fan_control_allowed) {
6179 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6180 fan_control_commands = 0;
6181 dbg_printk(TPACPI_DBG_INIT,
6182 "fan control features disabled by parameter\n");
6185 /* update fan_control_desired_level */
6186 if (fan_status_access_mode != TPACPI_FAN_NONE)
6187 fan_get_status_safe(NULL);
6189 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6190 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6191 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6192 &fan_attr_group);
6193 if (rc < 0)
6194 return rc;
6196 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6197 &driver_attr_fan_watchdog);
6198 if (rc < 0) {
6199 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6200 &fan_attr_group);
6201 return rc;
6203 return 0;
6204 } else
6205 return 1;
6208 static void fan_exit(void)
6210 vdbg_printk(TPACPI_DBG_EXIT,
6211 "cancelling any pending fan watchdog tasks\n");
6213 /* FIXME: can we really do this unconditionally? */
6214 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6215 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6216 &driver_attr_fan_watchdog);
6218 cancel_delayed_work(&fan_watchdog_task);
6219 flush_workqueue(tpacpi_wq);
6222 static void fan_suspend(pm_message_t state)
6224 int rc;
6226 if (!fan_control_allowed)
6227 return;
6229 /* Store fan status in cache */
6230 fan_control_resume_level = 0;
6231 rc = fan_get_status_safe(&fan_control_resume_level);
6232 if (rc < 0)
6233 printk(TPACPI_NOTICE
6234 "failed to read fan level for later "
6235 "restore during resume: %d\n", rc);
6237 /* if it is undefined, don't attempt to restore it.
6238 * KEEP THIS LAST */
6239 if (tp_features.fan_ctrl_status_undef)
6240 fan_control_resume_level = 0;
6243 static void fan_resume(void)
6245 u8 current_level = 7;
6246 bool do_set = false;
6247 int rc;
6249 /* DSDT *always* updates status on resume */
6250 tp_features.fan_ctrl_status_undef = 0;
6252 if (!fan_control_allowed ||
6253 !fan_control_resume_level ||
6254 (fan_get_status_safe(&current_level) < 0))
6255 return;
6257 switch (fan_control_access_mode) {
6258 case TPACPI_FAN_WR_ACPI_SFAN:
6259 /* never decrease fan level */
6260 do_set = (fan_control_resume_level > current_level);
6261 break;
6262 case TPACPI_FAN_WR_ACPI_FANS:
6263 case TPACPI_FAN_WR_TPEC:
6264 /* never decrease fan level, scale is:
6265 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6267 * We expect the firmware to set either 7 or AUTO, but we
6268 * handle FULLSPEED out of paranoia.
6270 * So, we can safely only restore FULLSPEED or 7, anything
6271 * else could slow the fan. Restoring AUTO is useless, at
6272 * best that's exactly what the DSDT already set (it is the
6273 * slower it uses).
6275 * Always keep in mind that the DSDT *will* have set the
6276 * fans to what the vendor supposes is the best level. We
6277 * muck with it only to speed the fan up.
6279 if (fan_control_resume_level != 7 &&
6280 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6281 return;
6282 else
6283 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6284 (current_level != fan_control_resume_level);
6285 break;
6286 default:
6287 return;
6289 if (do_set) {
6290 printk(TPACPI_NOTICE
6291 "restoring fan level to 0x%02x\n",
6292 fan_control_resume_level);
6293 rc = fan_set_level_safe(fan_control_resume_level);
6294 if (rc < 0)
6295 printk(TPACPI_NOTICE
6296 "failed to restore fan level: %d\n", rc);
6300 static int fan_read(char *p)
6302 int len = 0;
6303 int rc;
6304 u8 status;
6305 unsigned int speed = 0;
6307 switch (fan_status_access_mode) {
6308 case TPACPI_FAN_RD_ACPI_GFAN:
6309 /* 570, 600e/x, 770e, 770x */
6310 rc = fan_get_status_safe(&status);
6311 if (rc < 0)
6312 return rc;
6314 len += sprintf(p + len, "status:\t\t%s\n"
6315 "level:\t\t%d\n",
6316 (status != 0) ? "enabled" : "disabled", status);
6317 break;
6319 case TPACPI_FAN_RD_TPEC:
6320 /* all except 570, 600e/x, 770e, 770x */
6321 rc = fan_get_status_safe(&status);
6322 if (rc < 0)
6323 return rc;
6325 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6326 if (status != fan_control_initial_status)
6327 tp_features.fan_ctrl_status_undef = 0;
6328 else
6329 /* Return most likely status. In fact, it
6330 * might be the only possible status */
6331 status = TP_EC_FAN_AUTO;
6334 len += sprintf(p + len, "status:\t\t%s\n",
6335 (status != 0) ? "enabled" : "disabled");
6337 rc = fan_get_speed(&speed);
6338 if (rc < 0)
6339 return rc;
6341 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6343 if (status & TP_EC_FAN_FULLSPEED)
6344 /* Disengaged mode takes precedence */
6345 len += sprintf(p + len, "level:\t\tdisengaged\n");
6346 else if (status & TP_EC_FAN_AUTO)
6347 len += sprintf(p + len, "level:\t\tauto\n");
6348 else
6349 len += sprintf(p + len, "level:\t\t%d\n", status);
6350 break;
6352 case TPACPI_FAN_NONE:
6353 default:
6354 len += sprintf(p + len, "status:\t\tnot supported\n");
6357 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6358 len += sprintf(p + len, "commands:\tlevel <level>");
6360 switch (fan_control_access_mode) {
6361 case TPACPI_FAN_WR_ACPI_SFAN:
6362 len += sprintf(p + len, " (<level> is 0-7)\n");
6363 break;
6365 default:
6366 len += sprintf(p + len, " (<level> is 0-7, "
6367 "auto, disengaged, full-speed)\n");
6368 break;
6372 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6373 len += sprintf(p + len, "commands:\tenable, disable\n"
6374 "commands:\twatchdog <timeout> (<timeout> "
6375 "is 0 (off), 1-120 (seconds))\n");
6377 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6378 len += sprintf(p + len, "commands:\tspeed <speed>"
6379 " (<speed> is 0-65535)\n");
6381 return len;
6384 static int fan_write_cmd_level(const char *cmd, int *rc)
6386 int level;
6388 if (strlencmp(cmd, "level auto") == 0)
6389 level = TP_EC_FAN_AUTO;
6390 else if ((strlencmp(cmd, "level disengaged") == 0) |
6391 (strlencmp(cmd, "level full-speed") == 0))
6392 level = TP_EC_FAN_FULLSPEED;
6393 else if (sscanf(cmd, "level %d", &level) != 1)
6394 return 0;
6396 *rc = fan_set_level_safe(level);
6397 if (*rc == -ENXIO)
6398 printk(TPACPI_ERR "level command accepted for unsupported "
6399 "access mode %d", fan_control_access_mode);
6401 return 1;
6404 static int fan_write_cmd_enable(const char *cmd, int *rc)
6406 if (strlencmp(cmd, "enable") != 0)
6407 return 0;
6409 *rc = fan_set_enable();
6410 if (*rc == -ENXIO)
6411 printk(TPACPI_ERR "enable command accepted for unsupported "
6412 "access mode %d", fan_control_access_mode);
6414 return 1;
6417 static int fan_write_cmd_disable(const char *cmd, int *rc)
6419 if (strlencmp(cmd, "disable") != 0)
6420 return 0;
6422 *rc = fan_set_disable();
6423 if (*rc == -ENXIO)
6424 printk(TPACPI_ERR "disable command accepted for unsupported "
6425 "access mode %d", fan_control_access_mode);
6427 return 1;
6430 static int fan_write_cmd_speed(const char *cmd, int *rc)
6432 int speed;
6434 /* TODO:
6435 * Support speed <low> <medium> <high> ? */
6437 if (sscanf(cmd, "speed %d", &speed) != 1)
6438 return 0;
6440 *rc = fan_set_speed(speed);
6441 if (*rc == -ENXIO)
6442 printk(TPACPI_ERR "speed command accepted for unsupported "
6443 "access mode %d", fan_control_access_mode);
6445 return 1;
6448 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6450 int interval;
6452 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6453 return 0;
6455 if (interval < 0 || interval > 120)
6456 *rc = -EINVAL;
6457 else
6458 fan_watchdog_maxinterval = interval;
6460 return 1;
6463 static int fan_write(char *buf)
6465 char *cmd;
6466 int rc = 0;
6468 while (!rc && (cmd = next_cmd(&buf))) {
6469 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6470 fan_write_cmd_level(cmd, &rc)) &&
6471 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6472 (fan_write_cmd_enable(cmd, &rc) ||
6473 fan_write_cmd_disable(cmd, &rc) ||
6474 fan_write_cmd_watchdog(cmd, &rc))) &&
6475 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6476 fan_write_cmd_speed(cmd, &rc))
6478 rc = -EINVAL;
6479 else if (!rc)
6480 fan_watchdog_reset();
6483 return rc;
6486 static struct ibm_struct fan_driver_data = {
6487 .name = "fan",
6488 .read = fan_read,
6489 .write = fan_write,
6490 .exit = fan_exit,
6491 .suspend = fan_suspend,
6492 .resume = fan_resume,
6495 /****************************************************************************
6496 ****************************************************************************
6498 * Infrastructure
6500 ****************************************************************************
6501 ****************************************************************************/
6503 /* sysfs name ---------------------------------------------------------- */
6504 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6505 struct device_attribute *attr,
6506 char *buf)
6508 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6511 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6512 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6514 /* --------------------------------------------------------------------- */
6516 /* /proc support */
6517 static struct proc_dir_entry *proc_dir;
6520 * Module and infrastructure proble, init and exit handling
6523 static int force_load;
6525 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6526 static const char * __init str_supported(int is_supported)
6528 static char text_unsupported[] __initdata = "not supported";
6530 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6532 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6534 static void ibm_exit(struct ibm_struct *ibm)
6536 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6538 list_del_init(&ibm->all_drivers);
6540 if (ibm->flags.acpi_notify_installed) {
6541 dbg_printk(TPACPI_DBG_EXIT,
6542 "%s: acpi_remove_notify_handler\n", ibm->name);
6543 BUG_ON(!ibm->acpi);
6544 acpi_remove_notify_handler(*ibm->acpi->handle,
6545 ibm->acpi->type,
6546 dispatch_acpi_notify);
6547 ibm->flags.acpi_notify_installed = 0;
6548 ibm->flags.acpi_notify_installed = 0;
6551 if (ibm->flags.proc_created) {
6552 dbg_printk(TPACPI_DBG_EXIT,
6553 "%s: remove_proc_entry\n", ibm->name);
6554 remove_proc_entry(ibm->name, proc_dir);
6555 ibm->flags.proc_created = 0;
6558 if (ibm->flags.acpi_driver_registered) {
6559 dbg_printk(TPACPI_DBG_EXIT,
6560 "%s: acpi_bus_unregister_driver\n", ibm->name);
6561 BUG_ON(!ibm->acpi);
6562 acpi_bus_unregister_driver(ibm->acpi->driver);
6563 kfree(ibm->acpi->driver);
6564 ibm->acpi->driver = NULL;
6565 ibm->flags.acpi_driver_registered = 0;
6568 if (ibm->flags.init_called && ibm->exit) {
6569 ibm->exit();
6570 ibm->flags.init_called = 0;
6573 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6576 static int __init ibm_init(struct ibm_init_struct *iibm)
6578 int ret;
6579 struct ibm_struct *ibm = iibm->data;
6580 struct proc_dir_entry *entry;
6582 BUG_ON(ibm == NULL);
6584 INIT_LIST_HEAD(&ibm->all_drivers);
6586 if (ibm->flags.experimental && !experimental)
6587 return 0;
6589 dbg_printk(TPACPI_DBG_INIT,
6590 "probing for %s\n", ibm->name);
6592 if (iibm->init) {
6593 ret = iibm->init(iibm);
6594 if (ret > 0)
6595 return 0; /* probe failed */
6596 if (ret)
6597 return ret;
6599 ibm->flags.init_called = 1;
6602 if (ibm->acpi) {
6603 if (ibm->acpi->hid) {
6604 ret = register_tpacpi_subdriver(ibm);
6605 if (ret)
6606 goto err_out;
6609 if (ibm->acpi->notify) {
6610 ret = setup_acpi_notify(ibm);
6611 if (ret == -ENODEV) {
6612 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6613 ibm->name);
6614 ret = 0;
6615 goto err_out;
6617 if (ret < 0)
6618 goto err_out;
6622 dbg_printk(TPACPI_DBG_INIT,
6623 "%s installed\n", ibm->name);
6625 if (ibm->read) {
6626 entry = create_proc_entry(ibm->name,
6627 S_IFREG | S_IRUGO | S_IWUSR,
6628 proc_dir);
6629 if (!entry) {
6630 printk(TPACPI_ERR "unable to create proc entry %s\n",
6631 ibm->name);
6632 ret = -ENODEV;
6633 goto err_out;
6635 entry->owner = THIS_MODULE;
6636 entry->data = ibm;
6637 entry->read_proc = &dispatch_procfs_read;
6638 if (ibm->write)
6639 entry->write_proc = &dispatch_procfs_write;
6640 ibm->flags.proc_created = 1;
6643 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6645 return 0;
6647 err_out:
6648 dbg_printk(TPACPI_DBG_INIT,
6649 "%s: at error exit path with result %d\n",
6650 ibm->name, ret);
6652 ibm_exit(ibm);
6653 return (ret < 0)? ret : 0;
6656 /* Probing */
6658 /* returns 0 - probe ok, or < 0 - probe error.
6659 * Probe ok doesn't mean thinkpad found.
6660 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6661 static int __must_check __init get_thinkpad_model_data(
6662 struct thinkpad_id_data *tp)
6664 const struct dmi_device *dev = NULL;
6665 char ec_fw_string[18];
6666 char const *s;
6668 if (!tp)
6669 return -EINVAL;
6671 memset(tp, 0, sizeof(*tp));
6673 if (dmi_name_in_vendors("IBM"))
6674 tp->vendor = PCI_VENDOR_ID_IBM;
6675 else if (dmi_name_in_vendors("LENOVO"))
6676 tp->vendor = PCI_VENDOR_ID_LENOVO;
6677 else
6678 return 0;
6680 s = dmi_get_system_info(DMI_BIOS_VERSION);
6681 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6682 if (s && !tp->bios_version_str)
6683 return -ENOMEM;
6684 if (!tp->bios_version_str)
6685 return 0;
6686 tp->bios_model = tp->bios_version_str[0]
6687 | (tp->bios_version_str[1] << 8);
6690 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6691 * X32 or newer, all Z series; Some models must have an
6692 * up-to-date BIOS or they will not be detected.
6694 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6696 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6697 if (sscanf(dev->name,
6698 "IBM ThinkPad Embedded Controller -[%17c",
6699 ec_fw_string) == 1) {
6700 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6701 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6703 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6704 if (!tp->ec_version_str)
6705 return -ENOMEM;
6706 tp->ec_model = ec_fw_string[0]
6707 | (ec_fw_string[1] << 8);
6708 break;
6712 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6713 if (s && !strnicmp(s, "ThinkPad", 8)) {
6714 tp->model_str = kstrdup(s, GFP_KERNEL);
6715 if (!tp->model_str)
6716 return -ENOMEM;
6719 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6720 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6721 if (s && !tp->nummodel_str)
6722 return -ENOMEM;
6724 return 0;
6727 static int __init probe_for_thinkpad(void)
6729 int is_thinkpad;
6731 if (acpi_disabled)
6732 return -ENODEV;
6735 * Non-ancient models have better DMI tagging, but very old models
6736 * don't.
6738 is_thinkpad = (thinkpad_id.model_str != NULL);
6740 /* ec is required because many other handles are relative to it */
6741 TPACPI_ACPIHANDLE_INIT(ec);
6742 if (!ec_handle) {
6743 if (is_thinkpad)
6744 printk(TPACPI_ERR
6745 "Not yet supported ThinkPad detected!\n");
6746 return -ENODEV;
6750 * Risks a regression on very old machines, but reduces potential
6751 * false positives a damn great deal
6753 if (!is_thinkpad)
6754 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6756 if (!is_thinkpad && !force_load)
6757 return -ENODEV;
6759 return 0;
6763 /* Module init, exit, parameters */
6765 static struct ibm_init_struct ibms_init[] __initdata = {
6767 .init = thinkpad_acpi_driver_init,
6768 .data = &thinkpad_acpi_driver_data,
6771 .init = hotkey_init,
6772 .data = &hotkey_driver_data,
6775 .init = bluetooth_init,
6776 .data = &bluetooth_driver_data,
6779 .init = wan_init,
6780 .data = &wan_driver_data,
6782 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6784 .init = video_init,
6785 .data = &video_driver_data,
6787 #endif
6789 .init = light_init,
6790 .data = &light_driver_data,
6792 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6794 .init = dock_init,
6795 .data = &dock_driver_data[0],
6798 .init = dock_init2,
6799 .data = &dock_driver_data[1],
6801 #endif
6802 #ifdef CONFIG_THINKPAD_ACPI_BAY
6804 .init = bay_init,
6805 .data = &bay_driver_data,
6807 #endif
6809 .init = cmos_init,
6810 .data = &cmos_driver_data,
6813 .init = led_init,
6814 .data = &led_driver_data,
6817 .init = beep_init,
6818 .data = &beep_driver_data,
6821 .init = thermal_init,
6822 .data = &thermal_driver_data,
6825 .data = &ecdump_driver_data,
6828 .init = brightness_init,
6829 .data = &brightness_driver_data,
6832 .data = &volume_driver_data,
6835 .init = fan_init,
6836 .data = &fan_driver_data,
6840 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6842 unsigned int i;
6843 struct ibm_struct *ibm;
6845 if (!kp || !kp->name || !val)
6846 return -EINVAL;
6848 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6849 ibm = ibms_init[i].data;
6850 WARN_ON(ibm == NULL);
6852 if (!ibm || !ibm->name)
6853 continue;
6855 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6856 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6857 return -ENOSPC;
6858 strcpy(ibms_init[i].param, val);
6859 strcat(ibms_init[i].param, ",");
6860 return 0;
6864 return -EINVAL;
6867 module_param(experimental, int, 0);
6868 MODULE_PARM_DESC(experimental,
6869 "Enables experimental features when non-zero");
6871 module_param_named(debug, dbg_level, uint, 0);
6872 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6874 module_param(force_load, bool, 0);
6875 MODULE_PARM_DESC(force_load,
6876 "Attempts to load the driver even on a "
6877 "mis-identified ThinkPad when true");
6879 module_param_named(fan_control, fan_control_allowed, bool, 0);
6880 MODULE_PARM_DESC(fan_control,
6881 "Enables setting fan parameters features when true");
6883 module_param_named(brightness_mode, brightness_mode, int, 0);
6884 MODULE_PARM_DESC(brightness_mode,
6885 "Selects brightness control strategy: "
6886 "0=auto, 1=EC, 2=CMOS, 3=both");
6888 module_param(brightness_enable, uint, 0);
6889 MODULE_PARM_DESC(brightness_enable,
6890 "Enables backlight control when 1, disables when 0");
6892 module_param(hotkey_report_mode, uint, 0);
6893 MODULE_PARM_DESC(hotkey_report_mode,
6894 "used for backwards compatibility with userspace, "
6895 "see documentation");
6897 #define TPACPI_PARAM(feature) \
6898 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6899 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6900 "at module load, see documentation")
6902 TPACPI_PARAM(hotkey);
6903 TPACPI_PARAM(bluetooth);
6904 TPACPI_PARAM(video);
6905 TPACPI_PARAM(light);
6906 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6907 TPACPI_PARAM(dock);
6908 #endif
6909 #ifdef CONFIG_THINKPAD_ACPI_BAY
6910 TPACPI_PARAM(bay);
6911 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6912 TPACPI_PARAM(cmos);
6913 TPACPI_PARAM(led);
6914 TPACPI_PARAM(beep);
6915 TPACPI_PARAM(ecdump);
6916 TPACPI_PARAM(brightness);
6917 TPACPI_PARAM(volume);
6918 TPACPI_PARAM(fan);
6920 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
6921 module_param(dbg_wlswemul, uint, 0);
6922 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
6923 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
6924 MODULE_PARM_DESC(wlsw_state,
6925 "Initial state of the emulated WLSW switch");
6927 module_param(dbg_bluetoothemul, uint, 0);
6928 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
6929 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
6930 MODULE_PARM_DESC(bluetooth_state,
6931 "Initial state of the emulated bluetooth switch");
6933 module_param(dbg_wwanemul, uint, 0);
6934 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
6935 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
6936 MODULE_PARM_DESC(wwan_state,
6937 "Initial state of the emulated WWAN switch");
6938 #endif
6940 static void thinkpad_acpi_module_exit(void)
6942 struct ibm_struct *ibm, *itmp;
6944 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6946 list_for_each_entry_safe_reverse(ibm, itmp,
6947 &tpacpi_all_drivers,
6948 all_drivers) {
6949 ibm_exit(ibm);
6952 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6954 if (tpacpi_inputdev) {
6955 if (tp_features.input_device_registered)
6956 input_unregister_device(tpacpi_inputdev);
6957 else
6958 input_free_device(tpacpi_inputdev);
6961 if (tpacpi_hwmon)
6962 hwmon_device_unregister(tpacpi_hwmon);
6964 if (tp_features.sensors_pdev_attrs_registered)
6965 device_remove_file(&tpacpi_sensors_pdev->dev,
6966 &dev_attr_thinkpad_acpi_pdev_name);
6967 if (tpacpi_sensors_pdev)
6968 platform_device_unregister(tpacpi_sensors_pdev);
6969 if (tpacpi_pdev)
6970 platform_device_unregister(tpacpi_pdev);
6972 if (tp_features.sensors_pdrv_attrs_registered)
6973 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6974 if (tp_features.platform_drv_attrs_registered)
6975 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6977 if (tp_features.sensors_pdrv_registered)
6978 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6980 if (tp_features.platform_drv_registered)
6981 platform_driver_unregister(&tpacpi_pdriver);
6983 if (proc_dir)
6984 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6986 if (tpacpi_wq)
6987 destroy_workqueue(tpacpi_wq);
6989 kfree(thinkpad_id.bios_version_str);
6990 kfree(thinkpad_id.ec_version_str);
6991 kfree(thinkpad_id.model_str);
6995 static int __init thinkpad_acpi_module_init(void)
6997 int ret, i;
6999 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7001 /* Parameter checking */
7002 if (hotkey_report_mode > 2)
7003 return -EINVAL;
7005 /* Driver-level probe */
7007 ret = get_thinkpad_model_data(&thinkpad_id);
7008 if (ret) {
7009 printk(TPACPI_ERR
7010 "unable to get DMI data: %d\n", ret);
7011 thinkpad_acpi_module_exit();
7012 return ret;
7014 ret = probe_for_thinkpad();
7015 if (ret) {
7016 thinkpad_acpi_module_exit();
7017 return ret;
7020 /* Driver initialization */
7022 TPACPI_ACPIHANDLE_INIT(ecrd);
7023 TPACPI_ACPIHANDLE_INIT(ecwr);
7025 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7026 if (!tpacpi_wq) {
7027 thinkpad_acpi_module_exit();
7028 return -ENOMEM;
7031 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7032 if (!proc_dir) {
7033 printk(TPACPI_ERR
7034 "unable to create proc dir " TPACPI_PROC_DIR);
7035 thinkpad_acpi_module_exit();
7036 return -ENODEV;
7038 proc_dir->owner = THIS_MODULE;
7040 ret = platform_driver_register(&tpacpi_pdriver);
7041 if (ret) {
7042 printk(TPACPI_ERR
7043 "unable to register main platform driver\n");
7044 thinkpad_acpi_module_exit();
7045 return ret;
7047 tp_features.platform_drv_registered = 1;
7049 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7050 if (ret) {
7051 printk(TPACPI_ERR
7052 "unable to register hwmon platform driver\n");
7053 thinkpad_acpi_module_exit();
7054 return ret;
7056 tp_features.sensors_pdrv_registered = 1;
7058 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7059 if (!ret) {
7060 tp_features.platform_drv_attrs_registered = 1;
7061 ret = tpacpi_create_driver_attributes(
7062 &tpacpi_hwmon_pdriver.driver);
7064 if (ret) {
7065 printk(TPACPI_ERR
7066 "unable to create sysfs driver attributes\n");
7067 thinkpad_acpi_module_exit();
7068 return ret;
7070 tp_features.sensors_pdrv_attrs_registered = 1;
7073 /* Device initialization */
7074 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7075 NULL, 0);
7076 if (IS_ERR(tpacpi_pdev)) {
7077 ret = PTR_ERR(tpacpi_pdev);
7078 tpacpi_pdev = NULL;
7079 printk(TPACPI_ERR "unable to register platform device\n");
7080 thinkpad_acpi_module_exit();
7081 return ret;
7083 tpacpi_sensors_pdev = platform_device_register_simple(
7084 TPACPI_HWMON_DRVR_NAME,
7085 -1, NULL, 0);
7086 if (IS_ERR(tpacpi_sensors_pdev)) {
7087 ret = PTR_ERR(tpacpi_sensors_pdev);
7088 tpacpi_sensors_pdev = NULL;
7089 printk(TPACPI_ERR
7090 "unable to register hwmon platform device\n");
7091 thinkpad_acpi_module_exit();
7092 return ret;
7094 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7095 &dev_attr_thinkpad_acpi_pdev_name);
7096 if (ret) {
7097 printk(TPACPI_ERR
7098 "unable to create sysfs hwmon device attributes\n");
7099 thinkpad_acpi_module_exit();
7100 return ret;
7102 tp_features.sensors_pdev_attrs_registered = 1;
7103 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7104 if (IS_ERR(tpacpi_hwmon)) {
7105 ret = PTR_ERR(tpacpi_hwmon);
7106 tpacpi_hwmon = NULL;
7107 printk(TPACPI_ERR "unable to register hwmon device\n");
7108 thinkpad_acpi_module_exit();
7109 return ret;
7111 mutex_init(&tpacpi_inputdev_send_mutex);
7112 tpacpi_inputdev = input_allocate_device();
7113 if (!tpacpi_inputdev) {
7114 printk(TPACPI_ERR "unable to allocate input device\n");
7115 thinkpad_acpi_module_exit();
7116 return -ENOMEM;
7117 } else {
7118 /* Prepare input device, but don't register */
7119 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7120 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7121 tpacpi_inputdev->id.bustype = BUS_HOST;
7122 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7123 thinkpad_id.vendor :
7124 PCI_VENDOR_ID_IBM;
7125 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7126 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7128 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7129 ret = ibm_init(&ibms_init[i]);
7130 if (ret >= 0 && *ibms_init[i].param)
7131 ret = ibms_init[i].data->write(ibms_init[i].param);
7132 if (ret < 0) {
7133 thinkpad_acpi_module_exit();
7134 return ret;
7137 ret = input_register_device(tpacpi_inputdev);
7138 if (ret < 0) {
7139 printk(TPACPI_ERR "unable to register input device\n");
7140 thinkpad_acpi_module_exit();
7141 return ret;
7142 } else {
7143 tp_features.input_device_registered = 1;
7146 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7147 return 0;
7150 /* Please remove this in year 2009 */
7151 MODULE_ALIAS("ibm_acpi");
7153 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7156 * DMI matching for module autoloading
7158 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7159 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7161 * Only models listed in thinkwiki will be supported, so add yours
7162 * if it is not there yet.
7164 #define IBM_BIOS_MODULE_ALIAS(__type) \
7165 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7167 /* Non-ancient thinkpads */
7168 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7169 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7171 /* Ancient thinkpad BIOSes have to be identified by
7172 * BIOS type or model number, and there are far less
7173 * BIOS types than model numbers... */
7174 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7175 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7176 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7178 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7179 MODULE_DESCRIPTION(TPACPI_DESC);
7180 MODULE_VERSION(TPACPI_VERSION);
7181 MODULE_LICENSE("GPL");
7183 module_init(thinkpad_acpi_module_init);
7184 module_exit(thinkpad_acpi_module_exit);