ACPI: thinkpad-acpi: resume with radios disabled
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob9f466a8538379eb21df2b5b14d733e7f6ac8cd36
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.21"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
79 #include <acpi/acnamesp.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
175 /* Debugging */
176 #define TPACPI_LOG TPACPI_FILE ": "
177 #define TPACPI_ERR KERN_ERR TPACPI_LOG
178 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
179 #define TPACPI_INFO KERN_INFO TPACPI_LOG
180 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
182 #define TPACPI_DBG_ALL 0xffff
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define dbg_printk(a_dbg_level, format, arg...) \
186 do { if (dbg_level & a_dbg_level) \
187 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
188 } while (0)
189 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
190 #define vdbg_printk(a_dbg_level, format, arg...) \
191 dbg_printk(a_dbg_level, format, ## arg)
192 static const char *str_supported(int is_supported);
193 #else
194 #define vdbg_printk(a_dbg_level, format, arg...)
195 #endif
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
227 struct list_head all_drivers;
229 struct tp_acpi_drv_struct *acpi;
231 struct {
232 u8 acpi_driver_registered:1;
233 u8 acpi_notify_installed:1;
234 u8 proc_created:1;
235 u8 init_called:1;
236 u8 experimental:1;
237 } flags;
240 struct ibm_init_struct {
241 char param[32];
243 int (*init) (struct ibm_init_struct *);
244 struct ibm_struct *data;
247 static struct {
248 #ifdef CONFIG_THINKPAD_ACPI_BAY
249 u32 bay_status:1;
250 u32 bay_eject:1;
251 u32 bay_status2:1;
252 u32 bay_eject2:1;
253 #endif
254 u32 bluetooth:1;
255 u32 hotkey:1;
256 u32 hotkey_mask:1;
257 u32 hotkey_wlsw:1;
258 u32 hotkey_tablet:1;
259 u32 light:1;
260 u32 light_status:1;
261 u32 bright_16levels:1;
262 u32 bright_acpimode:1;
263 u32 wan:1;
264 u32 fan_ctrl_status_undef:1;
265 u32 input_device_registered:1;
266 u32 platform_drv_registered:1;
267 u32 platform_drv_attrs_registered:1;
268 u32 sensors_pdrv_registered:1;
269 u32 sensors_pdrv_attrs_registered:1;
270 u32 sensors_pdev_attrs_registered:1;
271 u32 hotkey_poll_active:1;
272 } tp_features;
274 static struct {
275 u16 hotkey_mask_ff:1;
276 u16 bright_cmos_ec_unsync:1;
277 } tp_warned;
279 struct thinkpad_id_data {
280 unsigned int vendor; /* ThinkPad vendor:
281 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
284 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
286 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
287 u16 ec_model;
289 char *model_str; /* ThinkPad T43 */
290 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
292 static struct thinkpad_id_data thinkpad_id;
294 static enum {
295 TPACPI_LIFE_INIT = 0,
296 TPACPI_LIFE_RUNNING,
297 TPACPI_LIFE_EXITING,
298 } tpacpi_lifecycle;
300 static int experimental;
301 static u32 dbg_level;
303 static struct workqueue_struct *tpacpi_wq;
305 /* Special LED class that can defer work */
306 struct tpacpi_led_classdev {
307 struct led_classdev led_classdev;
308 struct work_struct work;
309 enum led_brightness new_brightness;
310 unsigned int led;
313 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
314 static int dbg_wlswemul;
315 static int tpacpi_wlsw_emulstate;
316 static int dbg_bluetoothemul;
317 static int tpacpi_bluetooth_emulstate;
318 static int dbg_wwanemul;
319 static int tpacpi_wwan_emulstate;
320 #endif
323 /****************************************************************************
324 ****************************************************************************
326 * ACPI Helpers and device model
328 ****************************************************************************
329 ****************************************************************************/
331 /*************************************************************************
332 * ACPI basic handles
335 static acpi_handle root_handle;
337 #define TPACPI_HANDLE(object, parent, paths...) \
338 static acpi_handle object##_handle; \
339 static acpi_handle *object##_parent = &parent##_handle; \
340 static char *object##_path; \
341 static char *object##_paths[] = { paths }
343 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
344 "\\_SB.PCI.ISA.EC", /* 570 */
345 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
346 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
347 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
348 "\\_SB.PCI0.ICH3.EC0", /* R31 */
349 "\\_SB.PCI0.LPC.EC", /* all others */
352 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
353 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
355 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
356 /* T4x, X31, X40 */
357 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
358 "\\CMS", /* R40, R40e */
359 ); /* all others */
361 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
362 "^HKEY", /* R30, R31 */
363 "HKEY", /* all others */
364 ); /* 570 */
366 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
367 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
368 "\\_SB.PCI0.VID0", /* 770e */
369 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
370 "\\_SB.PCI0.AGP.VID", /* all others */
371 ); /* R30, R31 */
374 /*************************************************************************
375 * ACPI helpers
378 static int acpi_evalf(acpi_handle handle,
379 void *res, char *method, char *fmt, ...)
381 char *fmt0 = fmt;
382 struct acpi_object_list params;
383 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
384 struct acpi_buffer result, *resultp;
385 union acpi_object out_obj;
386 acpi_status status;
387 va_list ap;
388 char res_type;
389 int success;
390 int quiet;
392 if (!*fmt) {
393 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
394 return 0;
397 if (*fmt == 'q') {
398 quiet = 1;
399 fmt++;
400 } else
401 quiet = 0;
403 res_type = *(fmt++);
405 params.count = 0;
406 params.pointer = &in_objs[0];
408 va_start(ap, fmt);
409 while (*fmt) {
410 char c = *(fmt++);
411 switch (c) {
412 case 'd': /* int */
413 in_objs[params.count].integer.value = va_arg(ap, int);
414 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
415 break;
416 /* add more types as needed */
417 default:
418 printk(TPACPI_ERR "acpi_evalf() called "
419 "with invalid format character '%c'\n", c);
420 return 0;
423 va_end(ap);
425 if (res_type != 'v') {
426 result.length = sizeof(out_obj);
427 result.pointer = &out_obj;
428 resultp = &result;
429 } else
430 resultp = NULL;
432 status = acpi_evaluate_object(handle, method, &params, resultp);
434 switch (res_type) {
435 case 'd': /* int */
436 if (res)
437 *(int *)res = out_obj.integer.value;
438 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
439 break;
440 case 'v': /* void */
441 success = status == AE_OK;
442 break;
443 /* add more types as needed */
444 default:
445 printk(TPACPI_ERR "acpi_evalf() called "
446 "with invalid format character '%c'\n", res_type);
447 return 0;
450 if (!success && !quiet)
451 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
452 method, fmt0, status);
454 return success;
457 static int acpi_ec_read(int i, u8 *p)
459 int v;
461 if (ecrd_handle) {
462 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
463 return 0;
464 *p = v;
465 } else {
466 if (ec_read(i, p) < 0)
467 return 0;
470 return 1;
473 static int acpi_ec_write(int i, u8 v)
475 if (ecwr_handle) {
476 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
477 return 0;
478 } else {
479 if (ec_write(i, v) < 0)
480 return 0;
483 return 1;
486 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
487 static int _sta(acpi_handle handle)
489 int status;
491 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
492 status = 0;
494 return status;
496 #endif
498 static int issue_thinkpad_cmos_command(int cmos_cmd)
500 if (!cmos_handle)
501 return -ENXIO;
503 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
504 return -EIO;
506 return 0;
509 /*************************************************************************
510 * ACPI device model
513 #define TPACPI_ACPIHANDLE_INIT(object) \
514 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
515 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
517 static void drv_acpi_handle_init(char *name,
518 acpi_handle *handle, acpi_handle parent,
519 char **paths, int num_paths, char **path)
521 int i;
522 acpi_status status;
524 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
525 name);
527 for (i = 0; i < num_paths; i++) {
528 status = acpi_get_handle(parent, paths[i], handle);
529 if (ACPI_SUCCESS(status)) {
530 *path = paths[i];
531 dbg_printk(TPACPI_DBG_INIT,
532 "Found ACPI handle %s for %s\n",
533 *path, name);
534 return;
538 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
539 name);
540 *handle = NULL;
543 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
545 struct ibm_struct *ibm = data;
547 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
548 return;
550 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
551 return;
553 ibm->acpi->notify(ibm, event);
556 static int __init setup_acpi_notify(struct ibm_struct *ibm)
558 acpi_status status;
559 int rc;
561 BUG_ON(!ibm->acpi);
563 if (!*ibm->acpi->handle)
564 return 0;
566 vdbg_printk(TPACPI_DBG_INIT,
567 "setting up ACPI notify for %s\n", ibm->name);
569 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
570 if (rc < 0) {
571 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
572 ibm->name, rc);
573 return -ENODEV;
576 ibm->acpi->device->driver_data = ibm;
577 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
578 TPACPI_ACPI_EVENT_PREFIX,
579 ibm->name);
581 status = acpi_install_notify_handler(*ibm->acpi->handle,
582 ibm->acpi->type, dispatch_acpi_notify, ibm);
583 if (ACPI_FAILURE(status)) {
584 if (status == AE_ALREADY_EXISTS) {
585 printk(TPACPI_NOTICE
586 "another device driver is already "
587 "handling %s events\n", ibm->name);
588 } else {
589 printk(TPACPI_ERR
590 "acpi_install_notify_handler(%s) failed: %d\n",
591 ibm->name, status);
593 return -ENODEV;
595 ibm->flags.acpi_notify_installed = 1;
596 return 0;
599 static int __init tpacpi_device_add(struct acpi_device *device)
601 return 0;
604 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
606 int rc;
608 dbg_printk(TPACPI_DBG_INIT,
609 "registering %s as an ACPI driver\n", ibm->name);
611 BUG_ON(!ibm->acpi);
613 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
614 if (!ibm->acpi->driver) {
615 printk(TPACPI_ERR
616 "failed to allocate memory for ibm->acpi->driver\n");
617 return -ENOMEM;
620 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
621 ibm->acpi->driver->ids = ibm->acpi->hid;
623 ibm->acpi->driver->ops.add = &tpacpi_device_add;
625 rc = acpi_bus_register_driver(ibm->acpi->driver);
626 if (rc < 0) {
627 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
628 ibm->name, rc);
629 kfree(ibm->acpi->driver);
630 ibm->acpi->driver = NULL;
631 } else if (!rc)
632 ibm->flags.acpi_driver_registered = 1;
634 return rc;
638 /****************************************************************************
639 ****************************************************************************
641 * Procfs Helpers
643 ****************************************************************************
644 ****************************************************************************/
646 static int dispatch_procfs_read(char *page, char **start, off_t off,
647 int count, int *eof, void *data)
649 struct ibm_struct *ibm = data;
650 int len;
652 if (!ibm || !ibm->read)
653 return -EINVAL;
655 len = ibm->read(page);
656 if (len < 0)
657 return len;
659 if (len <= off + count)
660 *eof = 1;
661 *start = page + off;
662 len -= off;
663 if (len > count)
664 len = count;
665 if (len < 0)
666 len = 0;
668 return len;
671 static int dispatch_procfs_write(struct file *file,
672 const char __user *userbuf,
673 unsigned long count, void *data)
675 struct ibm_struct *ibm = data;
676 char *kernbuf;
677 int ret;
679 if (!ibm || !ibm->write)
680 return -EINVAL;
682 kernbuf = kmalloc(count + 2, GFP_KERNEL);
683 if (!kernbuf)
684 return -ENOMEM;
686 if (copy_from_user(kernbuf, userbuf, count)) {
687 kfree(kernbuf);
688 return -EFAULT;
691 kernbuf[count] = 0;
692 strcat(kernbuf, ",");
693 ret = ibm->write(kernbuf);
694 if (ret == 0)
695 ret = count;
697 kfree(kernbuf);
699 return ret;
702 static char *next_cmd(char **cmds)
704 char *start = *cmds;
705 char *end;
707 while ((end = strchr(start, ',')) && end == start)
708 start = end + 1;
710 if (!end)
711 return NULL;
713 *end = 0;
714 *cmds = end + 1;
715 return start;
719 /****************************************************************************
720 ****************************************************************************
722 * Device model: input, hwmon and platform
724 ****************************************************************************
725 ****************************************************************************/
727 static struct platform_device *tpacpi_pdev;
728 static struct platform_device *tpacpi_sensors_pdev;
729 static struct device *tpacpi_hwmon;
730 static struct input_dev *tpacpi_inputdev;
731 static struct mutex tpacpi_inputdev_send_mutex;
732 static LIST_HEAD(tpacpi_all_drivers);
734 static int tpacpi_suspend_handler(struct platform_device *pdev,
735 pm_message_t state)
737 struct ibm_struct *ibm, *itmp;
739 list_for_each_entry_safe(ibm, itmp,
740 &tpacpi_all_drivers,
741 all_drivers) {
742 if (ibm->suspend)
743 (ibm->suspend)(state);
746 return 0;
749 static int tpacpi_resume_handler(struct platform_device *pdev)
751 struct ibm_struct *ibm, *itmp;
753 list_for_each_entry_safe(ibm, itmp,
754 &tpacpi_all_drivers,
755 all_drivers) {
756 if (ibm->resume)
757 (ibm->resume)();
760 return 0;
763 static struct platform_driver tpacpi_pdriver = {
764 .driver = {
765 .name = TPACPI_DRVR_NAME,
766 .owner = THIS_MODULE,
768 .suspend = tpacpi_suspend_handler,
769 .resume = tpacpi_resume_handler,
772 static struct platform_driver tpacpi_hwmon_pdriver = {
773 .driver = {
774 .name = TPACPI_HWMON_DRVR_NAME,
775 .owner = THIS_MODULE,
779 /*************************************************************************
780 * sysfs support helpers
783 struct attribute_set {
784 unsigned int members, max_members;
785 struct attribute_group group;
788 struct attribute_set_obj {
789 struct attribute_set s;
790 struct attribute *a;
791 } __attribute__((packed));
793 static struct attribute_set *create_attr_set(unsigned int max_members,
794 const char *name)
796 struct attribute_set_obj *sobj;
798 if (max_members == 0)
799 return NULL;
801 /* Allocates space for implicit NULL at the end too */
802 sobj = kzalloc(sizeof(struct attribute_set_obj) +
803 max_members * sizeof(struct attribute *),
804 GFP_KERNEL);
805 if (!sobj)
806 return NULL;
807 sobj->s.max_members = max_members;
808 sobj->s.group.attrs = &sobj->a;
809 sobj->s.group.name = name;
811 return &sobj->s;
814 #define destroy_attr_set(_set) \
815 kfree(_set);
817 /* not multi-threaded safe, use it in a single thread per set */
818 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
820 if (!s || !attr)
821 return -EINVAL;
823 if (s->members >= s->max_members)
824 return -ENOMEM;
826 s->group.attrs[s->members] = attr;
827 s->members++;
829 return 0;
832 static int add_many_to_attr_set(struct attribute_set *s,
833 struct attribute **attr,
834 unsigned int count)
836 int i, res;
838 for (i = 0; i < count; i++) {
839 res = add_to_attr_set(s, attr[i]);
840 if (res)
841 return res;
844 return 0;
847 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
849 sysfs_remove_group(kobj, &s->group);
850 destroy_attr_set(s);
853 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
854 sysfs_create_group(_kobj, &_attr_set->group)
856 static int parse_strtoul(const char *buf,
857 unsigned long max, unsigned long *value)
859 char *endp;
861 while (*buf && isspace(*buf))
862 buf++;
863 *value = simple_strtoul(buf, &endp, 0);
864 while (*endp && isspace(*endp))
865 endp++;
866 if (*endp || *value > max)
867 return -EINVAL;
869 return 0;
872 static void tpacpi_disable_brightness_delay(void)
874 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
875 printk(TPACPI_NOTICE
876 "ACPI backlight control delay disabled\n");
879 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
881 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
882 union acpi_object *obj;
883 int rc;
885 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
886 obj = (union acpi_object *)buffer.pointer;
887 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
888 printk(TPACPI_ERR "Unknown _BCL data, "
889 "please report this to %s\n", TPACPI_MAIL);
890 rc = 0;
891 } else {
892 rc = obj->package.count;
894 } else {
895 return 0;
898 kfree(buffer.pointer);
899 return rc;
902 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
903 u32 lvl, void *context, void **rv)
905 char name[ACPI_PATH_SEGMENT_LENGTH];
906 struct acpi_buffer buffer = { sizeof(name), &name };
908 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
909 !strncmp("_BCL", name, sizeof(name) - 1)) {
910 BUG_ON(!rv || !*rv);
911 **(int **)rv = tpacpi_query_bcl_levels(handle);
912 return AE_CTRL_TERMINATE;
913 } else {
914 return AE_OK;
919 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
921 static int __init tpacpi_check_std_acpi_brightness_support(void)
923 int status;
924 int bcl_levels = 0;
925 void *bcl_ptr = &bcl_levels;
927 if (!vid_handle) {
928 TPACPI_ACPIHANDLE_INIT(vid);
930 if (!vid_handle)
931 return 0;
934 * Search for a _BCL method, and execute it. This is safe on all
935 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
936 * BIOS in ACPI backlight control mode. We do NOT have to care
937 * about calling the _BCL method in an enabled video device, any
938 * will do for our purposes.
941 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
942 tpacpi_acpi_walk_find_bcl, NULL,
943 &bcl_ptr);
945 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
946 tp_features.bright_acpimode = 1;
947 return (bcl_levels - 2);
950 return 0;
953 static int __init tpacpi_new_rfkill(const unsigned int id,
954 struct rfkill **rfk,
955 const enum rfkill_type rfktype,
956 const char *name,
957 int (*toggle_radio)(void *, enum rfkill_state),
958 int (*get_state)(void *, enum rfkill_state *))
960 int res;
961 enum rfkill_state initial_state;
963 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
964 if (!*rfk) {
965 printk(TPACPI_ERR
966 "failed to allocate memory for rfkill class\n");
967 return -ENOMEM;
970 (*rfk)->name = name;
971 (*rfk)->get_state = get_state;
972 (*rfk)->toggle_radio = toggle_radio;
974 if (!get_state(NULL, &initial_state))
975 (*rfk)->state = initial_state;
977 res = rfkill_register(*rfk);
978 if (res < 0) {
979 printk(TPACPI_ERR
980 "failed to register %s rfkill switch: %d\n",
981 name, res);
982 rfkill_free(*rfk);
983 *rfk = NULL;
984 return res;
987 return 0;
990 /*************************************************************************
991 * thinkpad-acpi driver attributes
994 /* interface_version --------------------------------------------------- */
995 static ssize_t tpacpi_driver_interface_version_show(
996 struct device_driver *drv,
997 char *buf)
999 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1002 static DRIVER_ATTR(interface_version, S_IRUGO,
1003 tpacpi_driver_interface_version_show, NULL);
1005 /* debug_level --------------------------------------------------------- */
1006 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1007 char *buf)
1009 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1012 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1013 const char *buf, size_t count)
1015 unsigned long t;
1017 if (parse_strtoul(buf, 0xffff, &t))
1018 return -EINVAL;
1020 dbg_level = t;
1022 return count;
1025 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1026 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1028 /* version ------------------------------------------------------------- */
1029 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1030 char *buf)
1032 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1033 TPACPI_DESC, TPACPI_VERSION);
1036 static DRIVER_ATTR(version, S_IRUGO,
1037 tpacpi_driver_version_show, NULL);
1039 /* --------------------------------------------------------------------- */
1041 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1043 static void tpacpi_send_radiosw_update(void);
1045 /* wlsw_emulstate ------------------------------------------------------ */
1046 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1047 char *buf)
1049 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1052 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1053 const char *buf, size_t count)
1055 unsigned long t;
1057 if (parse_strtoul(buf, 1, &t))
1058 return -EINVAL;
1060 if (tpacpi_wlsw_emulstate != t) {
1061 tpacpi_wlsw_emulstate = !!t;
1062 tpacpi_send_radiosw_update();
1063 } else
1064 tpacpi_wlsw_emulstate = !!t;
1066 return count;
1069 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1070 tpacpi_driver_wlsw_emulstate_show,
1071 tpacpi_driver_wlsw_emulstate_store);
1073 /* bluetooth_emulstate ------------------------------------------------- */
1074 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1075 struct device_driver *drv,
1076 char *buf)
1078 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1081 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1082 struct device_driver *drv,
1083 const char *buf, size_t count)
1085 unsigned long t;
1087 if (parse_strtoul(buf, 1, &t))
1088 return -EINVAL;
1090 tpacpi_bluetooth_emulstate = !!t;
1092 return count;
1095 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1096 tpacpi_driver_bluetooth_emulstate_show,
1097 tpacpi_driver_bluetooth_emulstate_store);
1099 /* wwan_emulstate ------------------------------------------------- */
1100 static ssize_t tpacpi_driver_wwan_emulstate_show(
1101 struct device_driver *drv,
1102 char *buf)
1104 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1107 static ssize_t tpacpi_driver_wwan_emulstate_store(
1108 struct device_driver *drv,
1109 const char *buf, size_t count)
1111 unsigned long t;
1113 if (parse_strtoul(buf, 1, &t))
1114 return -EINVAL;
1116 tpacpi_wwan_emulstate = !!t;
1118 return count;
1121 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1122 tpacpi_driver_wwan_emulstate_show,
1123 tpacpi_driver_wwan_emulstate_store);
1125 #endif
1127 /* --------------------------------------------------------------------- */
1129 static struct driver_attribute *tpacpi_driver_attributes[] = {
1130 &driver_attr_debug_level, &driver_attr_version,
1131 &driver_attr_interface_version,
1134 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1136 int i, res;
1138 i = 0;
1139 res = 0;
1140 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1141 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1142 i++;
1145 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1146 if (!res && dbg_wlswemul)
1147 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1148 if (!res && dbg_bluetoothemul)
1149 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1150 if (!res && dbg_wwanemul)
1151 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1152 #endif
1154 return res;
1157 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1159 int i;
1161 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1162 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1164 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1165 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1166 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1167 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1168 #endif
1171 /****************************************************************************
1172 ****************************************************************************
1174 * Subdrivers
1176 ****************************************************************************
1177 ****************************************************************************/
1179 /*************************************************************************
1180 * thinkpad-acpi init subdriver
1183 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1185 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1186 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1188 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1189 (thinkpad_id.bios_version_str) ?
1190 thinkpad_id.bios_version_str : "unknown",
1191 (thinkpad_id.ec_version_str) ?
1192 thinkpad_id.ec_version_str : "unknown");
1194 if (thinkpad_id.vendor && thinkpad_id.model_str)
1195 printk(TPACPI_INFO "%s %s, model %s\n",
1196 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1197 "IBM" : ((thinkpad_id.vendor ==
1198 PCI_VENDOR_ID_LENOVO) ?
1199 "Lenovo" : "Unknown vendor"),
1200 thinkpad_id.model_str,
1201 (thinkpad_id.nummodel_str) ?
1202 thinkpad_id.nummodel_str : "unknown");
1204 return 0;
1207 static int thinkpad_acpi_driver_read(char *p)
1209 int len = 0;
1211 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1212 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1214 return len;
1217 static struct ibm_struct thinkpad_acpi_driver_data = {
1218 .name = "driver",
1219 .read = thinkpad_acpi_driver_read,
1222 /*************************************************************************
1223 * Hotkey subdriver
1226 enum { /* hot key scan codes (derived from ACPI DSDT) */
1227 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1228 TP_ACPI_HOTKEYSCAN_FNF2,
1229 TP_ACPI_HOTKEYSCAN_FNF3,
1230 TP_ACPI_HOTKEYSCAN_FNF4,
1231 TP_ACPI_HOTKEYSCAN_FNF5,
1232 TP_ACPI_HOTKEYSCAN_FNF6,
1233 TP_ACPI_HOTKEYSCAN_FNF7,
1234 TP_ACPI_HOTKEYSCAN_FNF8,
1235 TP_ACPI_HOTKEYSCAN_FNF9,
1236 TP_ACPI_HOTKEYSCAN_FNF10,
1237 TP_ACPI_HOTKEYSCAN_FNF11,
1238 TP_ACPI_HOTKEYSCAN_FNF12,
1239 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1240 TP_ACPI_HOTKEYSCAN_FNINSERT,
1241 TP_ACPI_HOTKEYSCAN_FNDELETE,
1242 TP_ACPI_HOTKEYSCAN_FNHOME,
1243 TP_ACPI_HOTKEYSCAN_FNEND,
1244 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1245 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1246 TP_ACPI_HOTKEYSCAN_FNSPACE,
1247 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1248 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1249 TP_ACPI_HOTKEYSCAN_MUTE,
1250 TP_ACPI_HOTKEYSCAN_THINKPAD,
1253 enum { /* Keys available through NVRAM polling */
1254 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1255 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1258 enum { /* Positions of some of the keys in hotkey masks */
1259 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1260 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1261 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1262 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1263 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1264 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1265 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1266 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1267 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1268 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1269 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1272 enum { /* NVRAM to ACPI HKEY group map */
1273 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1274 TP_ACPI_HKEY_ZOOM_MASK |
1275 TP_ACPI_HKEY_DISPSWTCH_MASK |
1276 TP_ACPI_HKEY_HIBERNATE_MASK,
1277 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1278 TP_ACPI_HKEY_BRGHTDWN_MASK,
1279 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1280 TP_ACPI_HKEY_VOLDWN_MASK |
1281 TP_ACPI_HKEY_MUTE_MASK,
1284 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1285 struct tp_nvram_state {
1286 u16 thinkpad_toggle:1;
1287 u16 zoom_toggle:1;
1288 u16 display_toggle:1;
1289 u16 thinklight_toggle:1;
1290 u16 hibernate_toggle:1;
1291 u16 displayexp_toggle:1;
1292 u16 display_state:1;
1293 u16 brightness_toggle:1;
1294 u16 volume_toggle:1;
1295 u16 mute:1;
1297 u8 brightness_level;
1298 u8 volume_level;
1301 static struct task_struct *tpacpi_hotkey_task;
1302 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1303 static int hotkey_poll_freq = 10; /* Hz */
1304 static struct mutex hotkey_thread_mutex;
1305 static struct mutex hotkey_thread_data_mutex;
1306 static unsigned int hotkey_config_change;
1308 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1310 #define hotkey_source_mask 0U
1312 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1314 static struct mutex hotkey_mutex;
1316 static enum { /* Reasons for waking up */
1317 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1318 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1319 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1320 } hotkey_wakeup_reason;
1322 static int hotkey_autosleep_ack;
1324 static int hotkey_orig_status;
1325 static u32 hotkey_orig_mask;
1326 static u32 hotkey_all_mask;
1327 static u32 hotkey_reserved_mask;
1328 static u32 hotkey_mask;
1330 static unsigned int hotkey_report_mode;
1332 static u16 *hotkey_keycode_map;
1334 static struct attribute_set *hotkey_dev_attributes;
1336 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1337 #define HOTKEY_CONFIG_CRITICAL_START \
1338 do { \
1339 mutex_lock(&hotkey_thread_data_mutex); \
1340 hotkey_config_change++; \
1341 } while (0);
1342 #define HOTKEY_CONFIG_CRITICAL_END \
1343 mutex_unlock(&hotkey_thread_data_mutex);
1344 #else
1345 #define HOTKEY_CONFIG_CRITICAL_START
1346 #define HOTKEY_CONFIG_CRITICAL_END
1347 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1349 /* HKEY.MHKG() return bits */
1350 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1352 static int hotkey_get_wlsw(int *status)
1354 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1355 if (dbg_wlswemul) {
1356 *status = !!tpacpi_wlsw_emulstate;
1357 return 0;
1359 #endif
1360 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1361 return -EIO;
1362 return 0;
1365 static int hotkey_get_tablet_mode(int *status)
1367 int s;
1369 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1370 return -EIO;
1372 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1373 return 0;
1377 * Call with hotkey_mutex held
1379 static int hotkey_mask_get(void)
1381 u32 m = 0;
1383 if (tp_features.hotkey_mask) {
1384 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1385 return -EIO;
1387 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1389 return 0;
1393 * Call with hotkey_mutex held
1395 static int hotkey_mask_set(u32 mask)
1397 int i;
1398 int rc = 0;
1400 if (tp_features.hotkey_mask) {
1401 if (!tp_warned.hotkey_mask_ff &&
1402 (mask == 0xffff || mask == 0xffffff ||
1403 mask == 0xffffffff)) {
1404 tp_warned.hotkey_mask_ff = 1;
1405 printk(TPACPI_NOTICE
1406 "setting the hotkey mask to 0x%08x is likely "
1407 "not the best way to go about it\n", mask);
1408 printk(TPACPI_NOTICE
1409 "please consider using the driver defaults, "
1410 "and refer to up-to-date thinkpad-acpi "
1411 "documentation\n");
1414 HOTKEY_CONFIG_CRITICAL_START
1415 for (i = 0; i < 32; i++) {
1416 u32 m = 1 << i;
1417 /* enable in firmware mask only keys not in NVRAM
1418 * mode, but enable the key in the cached hotkey_mask
1419 * regardless of mode, or the key will end up
1420 * disabled by hotkey_mask_get() */
1421 if (!acpi_evalf(hkey_handle,
1422 NULL, "MHKM", "vdd", i + 1,
1423 !!((mask & ~hotkey_source_mask) & m))) {
1424 rc = -EIO;
1425 break;
1426 } else {
1427 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1430 HOTKEY_CONFIG_CRITICAL_END
1432 /* hotkey_mask_get must be called unconditionally below */
1433 if (!hotkey_mask_get() && !rc &&
1434 (hotkey_mask & ~hotkey_source_mask) !=
1435 (mask & ~hotkey_source_mask)) {
1436 printk(TPACPI_NOTICE
1437 "requested hot key mask 0x%08x, but "
1438 "firmware forced it to 0x%08x\n",
1439 mask, hotkey_mask);
1441 } else {
1442 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1443 HOTKEY_CONFIG_CRITICAL_START
1444 hotkey_mask = mask & hotkey_source_mask;
1445 HOTKEY_CONFIG_CRITICAL_END
1446 hotkey_mask_get();
1447 if (hotkey_mask != mask) {
1448 printk(TPACPI_NOTICE
1449 "requested hot key mask 0x%08x, "
1450 "forced to 0x%08x (NVRAM poll mask is "
1451 "0x%08x): no firmware mask support\n",
1452 mask, hotkey_mask, hotkey_source_mask);
1454 #else
1455 hotkey_mask_get();
1456 rc = -ENXIO;
1457 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1460 return rc;
1463 static int hotkey_status_get(int *status)
1465 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1466 return -EIO;
1468 return 0;
1471 static int hotkey_status_set(int status)
1473 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1474 return -EIO;
1476 return 0;
1479 static void tpacpi_input_send_tabletsw(void)
1481 int state;
1483 if (tp_features.hotkey_tablet &&
1484 !hotkey_get_tablet_mode(&state)) {
1485 mutex_lock(&tpacpi_inputdev_send_mutex);
1487 input_report_switch(tpacpi_inputdev,
1488 SW_TABLET_MODE, !!state);
1489 input_sync(tpacpi_inputdev);
1491 mutex_unlock(&tpacpi_inputdev_send_mutex);
1495 static void tpacpi_input_send_key(unsigned int scancode)
1497 unsigned int keycode;
1499 keycode = hotkey_keycode_map[scancode];
1501 if (keycode != KEY_RESERVED) {
1502 mutex_lock(&tpacpi_inputdev_send_mutex);
1504 input_report_key(tpacpi_inputdev, keycode, 1);
1505 if (keycode == KEY_UNKNOWN)
1506 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1507 scancode);
1508 input_sync(tpacpi_inputdev);
1510 input_report_key(tpacpi_inputdev, keycode, 0);
1511 if (keycode == KEY_UNKNOWN)
1512 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1513 scancode);
1514 input_sync(tpacpi_inputdev);
1516 mutex_unlock(&tpacpi_inputdev_send_mutex);
1520 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1521 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1523 static void tpacpi_hotkey_send_key(unsigned int scancode)
1525 tpacpi_input_send_key(scancode);
1526 if (hotkey_report_mode < 2) {
1527 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1528 0x80, 0x1001 + scancode);
1532 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1534 u8 d;
1536 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1537 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1538 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1539 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1540 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1541 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1543 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1544 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1545 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1547 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1548 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1549 n->displayexp_toggle =
1550 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1552 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1553 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1554 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1555 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1556 n->brightness_toggle =
1557 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1559 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1560 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1561 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1562 >> TP_NVRAM_POS_LEVEL_VOLUME;
1563 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1564 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1568 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1569 do { \
1570 if ((mask & (1 << __scancode)) && \
1571 oldn->__member != newn->__member) \
1572 tpacpi_hotkey_send_key(__scancode); \
1573 } while (0)
1575 #define TPACPI_MAY_SEND_KEY(__scancode) \
1576 do { if (mask & (1 << __scancode)) \
1577 tpacpi_hotkey_send_key(__scancode); } while (0)
1579 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1580 struct tp_nvram_state *newn,
1581 u32 mask)
1583 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1584 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1585 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1586 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1588 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1590 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1592 /* handle volume */
1593 if (oldn->volume_toggle != newn->volume_toggle) {
1594 if (oldn->mute != newn->mute) {
1595 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1597 if (oldn->volume_level > newn->volume_level) {
1598 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1599 } else if (oldn->volume_level < newn->volume_level) {
1600 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1601 } else if (oldn->mute == newn->mute) {
1602 /* repeated key presses that didn't change state */
1603 if (newn->mute) {
1604 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1605 } else if (newn->volume_level != 0) {
1606 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1607 } else {
1608 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1613 /* handle brightness */
1614 if (oldn->brightness_toggle != newn->brightness_toggle) {
1615 if (oldn->brightness_level < newn->brightness_level) {
1616 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1617 } else if (oldn->brightness_level > newn->brightness_level) {
1618 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1619 } else {
1620 /* repeated key presses that didn't change state */
1621 if (newn->brightness_level != 0) {
1622 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1623 } else {
1624 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1630 #undef TPACPI_COMPARE_KEY
1631 #undef TPACPI_MAY_SEND_KEY
1633 static int hotkey_kthread(void *data)
1635 struct tp_nvram_state s[2];
1636 u32 mask;
1637 unsigned int si, so;
1638 unsigned long t;
1639 unsigned int change_detector, must_reset;
1641 mutex_lock(&hotkey_thread_mutex);
1643 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1644 goto exit;
1646 set_freezable();
1648 so = 0;
1649 si = 1;
1650 t = 0;
1652 /* Initial state for compares */
1653 mutex_lock(&hotkey_thread_data_mutex);
1654 change_detector = hotkey_config_change;
1655 mask = hotkey_source_mask & hotkey_mask;
1656 mutex_unlock(&hotkey_thread_data_mutex);
1657 hotkey_read_nvram(&s[so], mask);
1659 while (!kthread_should_stop() && hotkey_poll_freq) {
1660 if (t == 0)
1661 t = 1000/hotkey_poll_freq;
1662 t = msleep_interruptible(t);
1663 if (unlikely(kthread_should_stop()))
1664 break;
1665 must_reset = try_to_freeze();
1666 if (t > 0 && !must_reset)
1667 continue;
1669 mutex_lock(&hotkey_thread_data_mutex);
1670 if (must_reset || hotkey_config_change != change_detector) {
1671 /* forget old state on thaw or config change */
1672 si = so;
1673 t = 0;
1674 change_detector = hotkey_config_change;
1676 mask = hotkey_source_mask & hotkey_mask;
1677 mutex_unlock(&hotkey_thread_data_mutex);
1679 if (likely(mask)) {
1680 hotkey_read_nvram(&s[si], mask);
1681 if (likely(si != so)) {
1682 hotkey_compare_and_issue_event(&s[so], &s[si],
1683 mask);
1687 so = si;
1688 si ^= 1;
1691 exit:
1692 mutex_unlock(&hotkey_thread_mutex);
1693 return 0;
1696 static void hotkey_poll_stop_sync(void)
1698 if (tpacpi_hotkey_task) {
1699 if (frozen(tpacpi_hotkey_task) ||
1700 freezing(tpacpi_hotkey_task))
1701 thaw_process(tpacpi_hotkey_task);
1703 kthread_stop(tpacpi_hotkey_task);
1704 tpacpi_hotkey_task = NULL;
1705 mutex_lock(&hotkey_thread_mutex);
1706 /* at this point, the thread did exit */
1707 mutex_unlock(&hotkey_thread_mutex);
1711 /* call with hotkey_mutex held */
1712 static void hotkey_poll_setup(int may_warn)
1714 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1715 hotkey_poll_freq > 0 &&
1716 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1717 if (!tpacpi_hotkey_task) {
1718 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1719 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1720 if (IS_ERR(tpacpi_hotkey_task)) {
1721 tpacpi_hotkey_task = NULL;
1722 printk(TPACPI_ERR
1723 "could not create kernel thread "
1724 "for hotkey polling\n");
1727 } else {
1728 hotkey_poll_stop_sync();
1729 if (may_warn &&
1730 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1731 printk(TPACPI_NOTICE
1732 "hot keys 0x%08x require polling, "
1733 "which is currently disabled\n",
1734 hotkey_source_mask);
1739 static void hotkey_poll_setup_safe(int may_warn)
1741 mutex_lock(&hotkey_mutex);
1742 hotkey_poll_setup(may_warn);
1743 mutex_unlock(&hotkey_mutex);
1746 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1748 static void hotkey_poll_setup_safe(int __unused)
1752 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1754 static int hotkey_inputdev_open(struct input_dev *dev)
1756 switch (tpacpi_lifecycle) {
1757 case TPACPI_LIFE_INIT:
1759 * hotkey_init will call hotkey_poll_setup_safe
1760 * at the appropriate moment
1762 return 0;
1763 case TPACPI_LIFE_EXITING:
1764 return -EBUSY;
1765 case TPACPI_LIFE_RUNNING:
1766 hotkey_poll_setup_safe(0);
1767 return 0;
1770 /* Should only happen if tpacpi_lifecycle is corrupt */
1771 BUG();
1772 return -EBUSY;
1775 static void hotkey_inputdev_close(struct input_dev *dev)
1777 /* disable hotkey polling when possible */
1778 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1779 hotkey_poll_setup_safe(0);
1782 /* sysfs hotkey enable ------------------------------------------------- */
1783 static ssize_t hotkey_enable_show(struct device *dev,
1784 struct device_attribute *attr,
1785 char *buf)
1787 int res, status;
1789 res = hotkey_status_get(&status);
1790 if (res)
1791 return res;
1793 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1796 static ssize_t hotkey_enable_store(struct device *dev,
1797 struct device_attribute *attr,
1798 const char *buf, size_t count)
1800 unsigned long t;
1801 int res;
1803 if (parse_strtoul(buf, 1, &t))
1804 return -EINVAL;
1806 res = hotkey_status_set(t);
1808 return (res) ? res : count;
1811 static struct device_attribute dev_attr_hotkey_enable =
1812 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1813 hotkey_enable_show, hotkey_enable_store);
1815 /* sysfs hotkey mask --------------------------------------------------- */
1816 static ssize_t hotkey_mask_show(struct device *dev,
1817 struct device_attribute *attr,
1818 char *buf)
1820 int res;
1822 if (mutex_lock_interruptible(&hotkey_mutex))
1823 return -ERESTARTSYS;
1824 res = hotkey_mask_get();
1825 mutex_unlock(&hotkey_mutex);
1827 return (res)?
1828 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1831 static ssize_t hotkey_mask_store(struct device *dev,
1832 struct device_attribute *attr,
1833 const char *buf, size_t count)
1835 unsigned long t;
1836 int res;
1838 if (parse_strtoul(buf, 0xffffffffUL, &t))
1839 return -EINVAL;
1841 if (mutex_lock_interruptible(&hotkey_mutex))
1842 return -ERESTARTSYS;
1844 res = hotkey_mask_set(t);
1846 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1847 hotkey_poll_setup(1);
1848 #endif
1850 mutex_unlock(&hotkey_mutex);
1852 return (res) ? res : count;
1855 static struct device_attribute dev_attr_hotkey_mask =
1856 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1857 hotkey_mask_show, hotkey_mask_store);
1859 /* sysfs hotkey bios_enabled ------------------------------------------- */
1860 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1861 struct device_attribute *attr,
1862 char *buf)
1864 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1867 static struct device_attribute dev_attr_hotkey_bios_enabled =
1868 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1870 /* sysfs hotkey bios_mask ---------------------------------------------- */
1871 static ssize_t hotkey_bios_mask_show(struct device *dev,
1872 struct device_attribute *attr,
1873 char *buf)
1875 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1878 static struct device_attribute dev_attr_hotkey_bios_mask =
1879 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1881 /* sysfs hotkey all_mask ----------------------------------------------- */
1882 static ssize_t hotkey_all_mask_show(struct device *dev,
1883 struct device_attribute *attr,
1884 char *buf)
1886 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1887 hotkey_all_mask | hotkey_source_mask);
1890 static struct device_attribute dev_attr_hotkey_all_mask =
1891 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1893 /* sysfs hotkey recommended_mask --------------------------------------- */
1894 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1895 struct device_attribute *attr,
1896 char *buf)
1898 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1899 (hotkey_all_mask | hotkey_source_mask)
1900 & ~hotkey_reserved_mask);
1903 static struct device_attribute dev_attr_hotkey_recommended_mask =
1904 __ATTR(hotkey_recommended_mask, S_IRUGO,
1905 hotkey_recommended_mask_show, NULL);
1907 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1909 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1910 static ssize_t hotkey_source_mask_show(struct device *dev,
1911 struct device_attribute *attr,
1912 char *buf)
1914 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1917 static ssize_t hotkey_source_mask_store(struct device *dev,
1918 struct device_attribute *attr,
1919 const char *buf, size_t count)
1921 unsigned long t;
1923 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1924 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1925 return -EINVAL;
1927 if (mutex_lock_interruptible(&hotkey_mutex))
1928 return -ERESTARTSYS;
1930 HOTKEY_CONFIG_CRITICAL_START
1931 hotkey_source_mask = t;
1932 HOTKEY_CONFIG_CRITICAL_END
1934 hotkey_poll_setup(1);
1936 mutex_unlock(&hotkey_mutex);
1938 return count;
1941 static struct device_attribute dev_attr_hotkey_source_mask =
1942 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1943 hotkey_source_mask_show, hotkey_source_mask_store);
1945 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1946 static ssize_t hotkey_poll_freq_show(struct device *dev,
1947 struct device_attribute *attr,
1948 char *buf)
1950 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1953 static ssize_t hotkey_poll_freq_store(struct device *dev,
1954 struct device_attribute *attr,
1955 const char *buf, size_t count)
1957 unsigned long t;
1959 if (parse_strtoul(buf, 25, &t))
1960 return -EINVAL;
1962 if (mutex_lock_interruptible(&hotkey_mutex))
1963 return -ERESTARTSYS;
1965 hotkey_poll_freq = t;
1967 hotkey_poll_setup(1);
1968 mutex_unlock(&hotkey_mutex);
1970 return count;
1973 static struct device_attribute dev_attr_hotkey_poll_freq =
1974 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1975 hotkey_poll_freq_show, hotkey_poll_freq_store);
1977 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1979 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1980 static ssize_t hotkey_radio_sw_show(struct device *dev,
1981 struct device_attribute *attr,
1982 char *buf)
1984 int res, s;
1985 res = hotkey_get_wlsw(&s);
1986 if (res < 0)
1987 return res;
1989 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1992 static struct device_attribute dev_attr_hotkey_radio_sw =
1993 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1995 static void hotkey_radio_sw_notify_change(void)
1997 if (tp_features.hotkey_wlsw)
1998 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1999 "hotkey_radio_sw");
2002 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2003 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2004 struct device_attribute *attr,
2005 char *buf)
2007 int res, s;
2008 res = hotkey_get_tablet_mode(&s);
2009 if (res < 0)
2010 return res;
2012 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2015 static struct device_attribute dev_attr_hotkey_tablet_mode =
2016 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2018 static void hotkey_tablet_mode_notify_change(void)
2020 if (tp_features.hotkey_tablet)
2021 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2022 "hotkey_tablet_mode");
2025 /* sysfs hotkey report_mode -------------------------------------------- */
2026 static ssize_t hotkey_report_mode_show(struct device *dev,
2027 struct device_attribute *attr,
2028 char *buf)
2030 return snprintf(buf, PAGE_SIZE, "%d\n",
2031 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2034 static struct device_attribute dev_attr_hotkey_report_mode =
2035 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2037 /* sysfs wakeup reason (pollable) -------------------------------------- */
2038 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2039 struct device_attribute *attr,
2040 char *buf)
2042 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2045 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2046 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2048 static void hotkey_wakeup_reason_notify_change(void)
2050 if (tp_features.hotkey_mask)
2051 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2052 "wakeup_reason");
2055 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2056 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2057 struct device_attribute *attr,
2058 char *buf)
2060 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2063 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2064 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2065 hotkey_wakeup_hotunplug_complete_show, NULL);
2067 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2069 if (tp_features.hotkey_mask)
2070 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2071 "wakeup_hotunplug_complete");
2074 /* --------------------------------------------------------------------- */
2076 static struct attribute *hotkey_attributes[] __initdata = {
2077 &dev_attr_hotkey_enable.attr,
2078 &dev_attr_hotkey_bios_enabled.attr,
2079 &dev_attr_hotkey_report_mode.attr,
2080 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2081 &dev_attr_hotkey_mask.attr,
2082 &dev_attr_hotkey_all_mask.attr,
2083 &dev_attr_hotkey_recommended_mask.attr,
2084 &dev_attr_hotkey_source_mask.attr,
2085 &dev_attr_hotkey_poll_freq.attr,
2086 #endif
2089 static struct attribute *hotkey_mask_attributes[] __initdata = {
2090 &dev_attr_hotkey_bios_mask.attr,
2091 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2092 &dev_attr_hotkey_mask.attr,
2093 &dev_attr_hotkey_all_mask.attr,
2094 &dev_attr_hotkey_recommended_mask.attr,
2095 #endif
2096 &dev_attr_hotkey_wakeup_reason.attr,
2097 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2100 static void bluetooth_update_rfk(void);
2101 static void wan_update_rfk(void);
2102 static void tpacpi_send_radiosw_update(void)
2104 int wlsw;
2106 /* Sync these BEFORE sending any rfkill events */
2107 if (tp_features.bluetooth)
2108 bluetooth_update_rfk();
2109 if (tp_features.wan)
2110 wan_update_rfk();
2112 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2113 mutex_lock(&tpacpi_inputdev_send_mutex);
2115 input_report_switch(tpacpi_inputdev,
2116 SW_RFKILL_ALL, !!wlsw);
2117 input_sync(tpacpi_inputdev);
2119 mutex_unlock(&tpacpi_inputdev_send_mutex);
2121 hotkey_radio_sw_notify_change();
2124 static void hotkey_exit(void)
2126 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2127 hotkey_poll_stop_sync();
2128 #endif
2130 if (hotkey_dev_attributes)
2131 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2133 kfree(hotkey_keycode_map);
2135 if (tp_features.hotkey) {
2136 dbg_printk(TPACPI_DBG_EXIT,
2137 "restoring original hot key mask\n");
2138 /* no short-circuit boolean operator below! */
2139 if ((hotkey_mask_set(hotkey_orig_mask) |
2140 hotkey_status_set(hotkey_orig_status)) != 0)
2141 printk(TPACPI_ERR
2142 "failed to restore hot key mask "
2143 "to BIOS defaults\n");
2147 static int __init hotkey_init(struct ibm_init_struct *iibm)
2149 /* Requirements for changing the default keymaps:
2151 * 1. Many of the keys are mapped to KEY_RESERVED for very
2152 * good reasons. Do not change them unless you have deep
2153 * knowledge on the IBM and Lenovo ThinkPad firmware for
2154 * the various ThinkPad models. The driver behaves
2155 * differently for KEY_RESERVED: such keys have their
2156 * hot key mask *unset* in mask_recommended, and also
2157 * in the initial hot key mask programmed into the
2158 * firmware at driver load time, which means the firm-
2159 * ware may react very differently if you change them to
2160 * something else;
2162 * 2. You must be subscribed to the linux-thinkpad and
2163 * ibm-acpi-devel mailing lists, and you should read the
2164 * list archives since 2007 if you want to change the
2165 * keymaps. This requirement exists so that you will
2166 * know the past history of problems with the thinkpad-
2167 * acpi driver keymaps, and also that you will be
2168 * listening to any bug reports;
2170 * 3. Do not send thinkpad-acpi specific patches directly to
2171 * for merging, *ever*. Send them to the linux-acpi
2172 * mailinglist for comments. Merging is to be done only
2173 * through acpi-test and the ACPI maintainer.
2175 * If the above is too much to ask, don't change the keymap.
2176 * Ask the thinkpad-acpi maintainer to do it, instead.
2178 static u16 ibm_keycode_map[] __initdata = {
2179 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2180 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2181 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2182 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2184 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2185 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2186 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2187 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2189 /* brightness: firmware always reacts to them, unless
2190 * X.org did some tricks in the radeon BIOS scratch
2191 * registers of *some* models */
2192 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2193 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2195 /* Thinklight: firmware always react to it */
2196 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2198 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2199 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2201 /* Volume: firmware always react to it and reprograms
2202 * the built-in *extra* mixer. Never map it to control
2203 * another mixer by default. */
2204 KEY_RESERVED, /* 0x14: VOLUME UP */
2205 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2206 KEY_RESERVED, /* 0x16: MUTE */
2208 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2210 /* (assignments unknown, please report if found) */
2211 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2212 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2214 static u16 lenovo_keycode_map[] __initdata = {
2215 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2216 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2217 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2218 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2220 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2221 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2222 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2223 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2225 /* These either have to go through ACPI video, or
2226 * act like in the IBM ThinkPads, so don't ever
2227 * enable them by default */
2228 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2229 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2231 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2233 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2234 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2236 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2237 * react to it and reprograms the built-in *extra* mixer.
2238 * Never map it to control another mixer by default.
2240 * T60?, T61, R60?, R61: firmware and EC tries to send
2241 * these over the regular keyboard, so these are no-ops,
2242 * but there are still weird bugs re. MUTE, so do not
2243 * change unless you get test reports from all Lenovo
2244 * models. May cause the BIOS to interfere with the
2245 * HDA mixer.
2247 KEY_RESERVED, /* 0x14: VOLUME UP */
2248 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2249 KEY_RESERVED, /* 0x16: MUTE */
2251 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2253 /* (assignments unknown, please report if found) */
2254 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2255 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2258 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2259 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2260 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2262 int res, i;
2263 int status;
2264 int hkeyv;
2266 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2268 BUG_ON(!tpacpi_inputdev);
2269 BUG_ON(tpacpi_inputdev->open != NULL ||
2270 tpacpi_inputdev->close != NULL);
2272 TPACPI_ACPIHANDLE_INIT(hkey);
2273 mutex_init(&hotkey_mutex);
2275 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2276 mutex_init(&hotkey_thread_mutex);
2277 mutex_init(&hotkey_thread_data_mutex);
2278 #endif
2280 /* hotkey not supported on 570 */
2281 tp_features.hotkey = hkey_handle != NULL;
2283 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2284 str_supported(tp_features.hotkey));
2286 if (!tp_features.hotkey)
2287 return 1;
2289 tpacpi_disable_brightness_delay();
2291 hotkey_dev_attributes = create_attr_set(13, NULL);
2292 if (!hotkey_dev_attributes)
2293 return -ENOMEM;
2294 res = add_many_to_attr_set(hotkey_dev_attributes,
2295 hotkey_attributes,
2296 ARRAY_SIZE(hotkey_attributes));
2297 if (res)
2298 goto err_exit;
2300 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2301 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2302 for HKEY interface version 0x100 */
2303 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2304 if ((hkeyv >> 8) != 1) {
2305 printk(TPACPI_ERR "unknown version of the "
2306 "HKEY interface: 0x%x\n", hkeyv);
2307 printk(TPACPI_ERR "please report this to %s\n",
2308 TPACPI_MAIL);
2309 } else {
2311 * MHKV 0x100 in A31, R40, R40e,
2312 * T4x, X31, and later
2314 tp_features.hotkey_mask = 1;
2318 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2319 str_supported(tp_features.hotkey_mask));
2321 if (tp_features.hotkey_mask) {
2322 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2323 "MHKA", "qd")) {
2324 printk(TPACPI_ERR
2325 "missing MHKA handler, "
2326 "please report this to %s\n",
2327 TPACPI_MAIL);
2328 /* FN+F12, FN+F4, FN+F3 */
2329 hotkey_all_mask = 0x080cU;
2333 /* hotkey_source_mask *must* be zero for
2334 * the first hotkey_mask_get */
2335 res = hotkey_status_get(&hotkey_orig_status);
2336 if (res)
2337 goto err_exit;
2339 if (tp_features.hotkey_mask) {
2340 res = hotkey_mask_get();
2341 if (res)
2342 goto err_exit;
2344 hotkey_orig_mask = hotkey_mask;
2345 res = add_many_to_attr_set(
2346 hotkey_dev_attributes,
2347 hotkey_mask_attributes,
2348 ARRAY_SIZE(hotkey_mask_attributes));
2349 if (res)
2350 goto err_exit;
2353 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2354 if (tp_features.hotkey_mask) {
2355 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2356 & ~hotkey_all_mask;
2357 } else {
2358 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2361 vdbg_printk(TPACPI_DBG_INIT,
2362 "hotkey source mask 0x%08x, polling freq %d\n",
2363 hotkey_source_mask, hotkey_poll_freq);
2364 #endif
2366 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2367 if (dbg_wlswemul) {
2368 tp_features.hotkey_wlsw = 1;
2369 printk(TPACPI_INFO
2370 "radio switch emulation enabled\n");
2371 } else
2372 #endif
2373 /* Not all thinkpads have a hardware radio switch */
2374 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2375 tp_features.hotkey_wlsw = 1;
2376 printk(TPACPI_INFO
2377 "radio switch found; radios are %s\n",
2378 enabled(status, 0));
2380 if (tp_features.hotkey_wlsw)
2381 res = add_to_attr_set(hotkey_dev_attributes,
2382 &dev_attr_hotkey_radio_sw.attr);
2384 /* For X41t, X60t, X61t Tablets... */
2385 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2386 tp_features.hotkey_tablet = 1;
2387 printk(TPACPI_INFO
2388 "possible tablet mode switch found; "
2389 "ThinkPad in %s mode\n",
2390 (status & TP_HOTKEY_TABLET_MASK)?
2391 "tablet" : "laptop");
2392 res = add_to_attr_set(hotkey_dev_attributes,
2393 &dev_attr_hotkey_tablet_mode.attr);
2396 if (!res)
2397 res = register_attr_set_with_sysfs(
2398 hotkey_dev_attributes,
2399 &tpacpi_pdev->dev.kobj);
2400 if (res)
2401 goto err_exit;
2403 /* Set up key map */
2405 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2406 GFP_KERNEL);
2407 if (!hotkey_keycode_map) {
2408 printk(TPACPI_ERR
2409 "failed to allocate memory for key map\n");
2410 res = -ENOMEM;
2411 goto err_exit;
2414 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2415 dbg_printk(TPACPI_DBG_INIT,
2416 "using Lenovo default hot key map\n");
2417 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2418 TPACPI_HOTKEY_MAP_SIZE);
2419 } else {
2420 dbg_printk(TPACPI_DBG_INIT,
2421 "using IBM default hot key map\n");
2422 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2423 TPACPI_HOTKEY_MAP_SIZE);
2426 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2427 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2428 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2429 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2430 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2431 tpacpi_inputdev->keycode = hotkey_keycode_map;
2432 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2433 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2434 set_bit(hotkey_keycode_map[i],
2435 tpacpi_inputdev->keybit);
2436 } else {
2437 if (i < sizeof(hotkey_reserved_mask)*8)
2438 hotkey_reserved_mask |= 1 << i;
2442 if (tp_features.hotkey_wlsw) {
2443 set_bit(EV_SW, tpacpi_inputdev->evbit);
2444 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2446 if (tp_features.hotkey_tablet) {
2447 set_bit(EV_SW, tpacpi_inputdev->evbit);
2448 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2451 /* Do not issue duplicate brightness change events to
2452 * userspace */
2453 if (!tp_features.bright_acpimode)
2454 /* update bright_acpimode... */
2455 tpacpi_check_std_acpi_brightness_support();
2457 if (tp_features.bright_acpimode) {
2458 printk(TPACPI_INFO
2459 "This ThinkPad has standard ACPI backlight "
2460 "brightness control, supported by the ACPI "
2461 "video driver\n");
2462 printk(TPACPI_NOTICE
2463 "Disabling thinkpad-acpi brightness events "
2464 "by default...\n");
2466 /* The hotkey_reserved_mask change below is not
2467 * necessary while the keys are at KEY_RESERVED in the
2468 * default map, but better safe than sorry, leave it
2469 * here as a marker of what we have to do, especially
2470 * when we finally become able to set this at runtime
2471 * on response to X.org requests */
2472 hotkey_reserved_mask |=
2473 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2474 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2477 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2478 res = hotkey_status_set(1);
2479 if (res) {
2480 hotkey_exit();
2481 return res;
2483 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2484 & ~hotkey_reserved_mask)
2485 | hotkey_orig_mask);
2486 if (res < 0 && res != -ENXIO) {
2487 hotkey_exit();
2488 return res;
2491 dbg_printk(TPACPI_DBG_INIT,
2492 "legacy hot key reporting over procfs %s\n",
2493 (hotkey_report_mode < 2) ?
2494 "enabled" : "disabled");
2496 tpacpi_inputdev->open = &hotkey_inputdev_open;
2497 tpacpi_inputdev->close = &hotkey_inputdev_close;
2499 hotkey_poll_setup_safe(1);
2500 tpacpi_send_radiosw_update();
2501 tpacpi_input_send_tabletsw();
2503 return 0;
2505 err_exit:
2506 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2507 hotkey_dev_attributes = NULL;
2509 return (res < 0)? res : 1;
2512 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2514 u32 hkey;
2515 unsigned int scancode;
2516 int send_acpi_ev;
2517 int ignore_acpi_ev;
2518 int unk_ev;
2520 if (event != 0x80) {
2521 printk(TPACPI_ERR
2522 "unknown HKEY notification event %d\n", event);
2523 /* forward it to userspace, maybe it knows how to handle it */
2524 acpi_bus_generate_netlink_event(
2525 ibm->acpi->device->pnp.device_class,
2526 dev_name(&ibm->acpi->device->dev),
2527 event, 0);
2528 return;
2531 while (1) {
2532 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2533 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2534 return;
2537 if (hkey == 0) {
2538 /* queue empty */
2539 return;
2542 send_acpi_ev = 1;
2543 ignore_acpi_ev = 0;
2544 unk_ev = 0;
2546 switch (hkey >> 12) {
2547 case 1:
2548 /* 0x1000-0x1FFF: key presses */
2549 scancode = hkey & 0xfff;
2550 if (scancode > 0 && scancode < 0x21) {
2551 scancode--;
2552 if (!(hotkey_source_mask & (1 << scancode))) {
2553 tpacpi_input_send_key(scancode);
2554 send_acpi_ev = 0;
2555 } else {
2556 ignore_acpi_ev = 1;
2558 } else {
2559 unk_ev = 1;
2561 break;
2562 case 2:
2563 /* Wakeup reason */
2564 switch (hkey) {
2565 case 0x2304: /* suspend, undock */
2566 case 0x2404: /* hibernation, undock */
2567 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2568 ignore_acpi_ev = 1;
2569 break;
2570 case 0x2305: /* suspend, bay eject */
2571 case 0x2405: /* hibernation, bay eject */
2572 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2573 ignore_acpi_ev = 1;
2574 break;
2575 default:
2576 unk_ev = 1;
2578 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2579 printk(TPACPI_INFO
2580 "woke up due to a hot-unplug "
2581 "request...\n");
2582 hotkey_wakeup_reason_notify_change();
2584 break;
2585 case 3:
2586 /* bay-related wakeups */
2587 if (hkey == 0x3003) {
2588 hotkey_autosleep_ack = 1;
2589 printk(TPACPI_INFO
2590 "bay ejected\n");
2591 hotkey_wakeup_hotunplug_complete_notify_change();
2592 } else {
2593 unk_ev = 1;
2595 break;
2596 case 4:
2597 /* dock-related wakeups */
2598 if (hkey == 0x4003) {
2599 hotkey_autosleep_ack = 1;
2600 printk(TPACPI_INFO
2601 "undocked\n");
2602 hotkey_wakeup_hotunplug_complete_notify_change();
2603 } else {
2604 unk_ev = 1;
2606 break;
2607 case 5:
2608 /* 0x5000-0x5FFF: human interface helpers */
2609 switch (hkey) {
2610 case 0x5010: /* Lenovo new BIOS: brightness changed */
2611 case 0x500b: /* X61t: tablet pen inserted into bay */
2612 case 0x500c: /* X61t: tablet pen removed from bay */
2613 break;
2614 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2615 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2616 tpacpi_input_send_tabletsw();
2617 hotkey_tablet_mode_notify_change();
2618 send_acpi_ev = 0;
2619 break;
2620 case 0x5001:
2621 case 0x5002:
2622 /* LID switch events. Do not propagate */
2623 ignore_acpi_ev = 1;
2624 break;
2625 default:
2626 unk_ev = 1;
2628 break;
2629 case 7:
2630 /* 0x7000-0x7FFF: misc */
2631 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2632 tpacpi_send_radiosw_update();
2633 send_acpi_ev = 0;
2634 break;
2636 /* fallthrough to default */
2637 default:
2638 unk_ev = 1;
2640 if (unk_ev) {
2641 printk(TPACPI_NOTICE
2642 "unhandled HKEY event 0x%04x\n", hkey);
2645 /* Legacy events */
2646 if (!ignore_acpi_ev &&
2647 (send_acpi_ev || hotkey_report_mode < 2)) {
2648 acpi_bus_generate_proc_event(ibm->acpi->device,
2649 event, hkey);
2652 /* netlink events */
2653 if (!ignore_acpi_ev && send_acpi_ev) {
2654 acpi_bus_generate_netlink_event(
2655 ibm->acpi->device->pnp.device_class,
2656 dev_name(&ibm->acpi->device->dev),
2657 event, hkey);
2662 static void hotkey_suspend(pm_message_t state)
2664 /* Do these on suspend, we get the events on early resume! */
2665 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2666 hotkey_autosleep_ack = 0;
2669 static void hotkey_resume(void)
2671 tpacpi_disable_brightness_delay();
2673 if (hotkey_mask_get())
2674 printk(TPACPI_ERR
2675 "error while trying to read hot key mask "
2676 "from firmware\n");
2677 tpacpi_send_radiosw_update();
2678 hotkey_tablet_mode_notify_change();
2679 hotkey_wakeup_reason_notify_change();
2680 hotkey_wakeup_hotunplug_complete_notify_change();
2681 hotkey_poll_setup_safe(0);
2684 /* procfs -------------------------------------------------------------- */
2685 static int hotkey_read(char *p)
2687 int res, status;
2688 int len = 0;
2690 if (!tp_features.hotkey) {
2691 len += sprintf(p + len, "status:\t\tnot supported\n");
2692 return len;
2695 if (mutex_lock_interruptible(&hotkey_mutex))
2696 return -ERESTARTSYS;
2697 res = hotkey_status_get(&status);
2698 if (!res)
2699 res = hotkey_mask_get();
2700 mutex_unlock(&hotkey_mutex);
2701 if (res)
2702 return res;
2704 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2705 if (tp_features.hotkey_mask) {
2706 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2707 len += sprintf(p + len,
2708 "commands:\tenable, disable, reset, <mask>\n");
2709 } else {
2710 len += sprintf(p + len, "mask:\t\tnot supported\n");
2711 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2714 return len;
2717 static int hotkey_write(char *buf)
2719 int res, status;
2720 u32 mask;
2721 char *cmd;
2723 if (!tp_features.hotkey)
2724 return -ENODEV;
2726 if (mutex_lock_interruptible(&hotkey_mutex))
2727 return -ERESTARTSYS;
2729 status = -1;
2730 mask = hotkey_mask;
2732 res = 0;
2733 while ((cmd = next_cmd(&buf))) {
2734 if (strlencmp(cmd, "enable") == 0) {
2735 status = 1;
2736 } else if (strlencmp(cmd, "disable") == 0) {
2737 status = 0;
2738 } else if (strlencmp(cmd, "reset") == 0) {
2739 status = hotkey_orig_status;
2740 mask = hotkey_orig_mask;
2741 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2742 /* mask set */
2743 } else if (sscanf(cmd, "%x", &mask) == 1) {
2744 /* mask set */
2745 } else {
2746 res = -EINVAL;
2747 goto errexit;
2750 if (status != -1)
2751 res = hotkey_status_set(status);
2753 if (!res && mask != hotkey_mask)
2754 res = hotkey_mask_set(mask);
2756 errexit:
2757 mutex_unlock(&hotkey_mutex);
2758 return res;
2761 static const struct acpi_device_id ibm_htk_device_ids[] = {
2762 {TPACPI_ACPI_HKEY_HID, 0},
2763 {"", 0},
2766 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2767 .hid = ibm_htk_device_ids,
2768 .notify = hotkey_notify,
2769 .handle = &hkey_handle,
2770 .type = ACPI_DEVICE_NOTIFY,
2773 static struct ibm_struct hotkey_driver_data = {
2774 .name = "hotkey",
2775 .read = hotkey_read,
2776 .write = hotkey_write,
2777 .exit = hotkey_exit,
2778 .resume = hotkey_resume,
2779 .suspend = hotkey_suspend,
2780 .acpi = &ibm_hotkey_acpidriver,
2783 /*************************************************************************
2784 * Bluetooth subdriver
2787 enum {
2788 /* ACPI GBDC/SBDC bits */
2789 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2790 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2791 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2792 off / last state */
2795 enum {
2796 /* ACPI \BLTH commands */
2797 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2798 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2799 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2800 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2801 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2804 static struct rfkill *tpacpi_bluetooth_rfkill;
2806 static void bluetooth_suspend(pm_message_t state)
2808 /* Try to make sure radio will resume powered off */
2809 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2810 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2813 static int bluetooth_get_radiosw(void)
2815 int status;
2817 if (!tp_features.bluetooth)
2818 return -ENODEV;
2820 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2821 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2822 return RFKILL_STATE_HARD_BLOCKED;
2824 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2825 if (dbg_bluetoothemul)
2826 return (tpacpi_bluetooth_emulstate) ?
2827 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2828 #endif
2830 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2831 return -EIO;
2833 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2834 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2837 static void bluetooth_update_rfk(void)
2839 int status;
2841 if (!tpacpi_bluetooth_rfkill)
2842 return;
2844 status = bluetooth_get_radiosw();
2845 if (status < 0)
2846 return;
2847 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2850 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2852 int status;
2854 if (!tp_features.bluetooth)
2855 return -ENODEV;
2857 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2858 * reason to risk weird behaviour. */
2859 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2860 && radio_on)
2861 return -EPERM;
2863 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2864 if (dbg_bluetoothemul) {
2865 tpacpi_bluetooth_emulstate = !!radio_on;
2866 if (update_rfk)
2867 bluetooth_update_rfk();
2868 return 0;
2870 #endif
2872 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
2873 if (radio_on)
2874 status = TP_ACPI_BLUETOOTH_RADIOSSW;
2875 else
2876 status = 0;
2877 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2878 return -EIO;
2880 if (update_rfk)
2881 bluetooth_update_rfk();
2883 return 0;
2886 /* sysfs bluetooth enable ---------------------------------------------- */
2887 static ssize_t bluetooth_enable_show(struct device *dev,
2888 struct device_attribute *attr,
2889 char *buf)
2891 int status;
2893 status = bluetooth_get_radiosw();
2894 if (status < 0)
2895 return status;
2897 return snprintf(buf, PAGE_SIZE, "%d\n",
2898 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2901 static ssize_t bluetooth_enable_store(struct device *dev,
2902 struct device_attribute *attr,
2903 const char *buf, size_t count)
2905 unsigned long t;
2906 int res;
2908 if (parse_strtoul(buf, 1, &t))
2909 return -EINVAL;
2911 res = bluetooth_set_radiosw(t, 1);
2913 return (res) ? res : count;
2916 static struct device_attribute dev_attr_bluetooth_enable =
2917 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2918 bluetooth_enable_show, bluetooth_enable_store);
2920 /* --------------------------------------------------------------------- */
2922 static struct attribute *bluetooth_attributes[] = {
2923 &dev_attr_bluetooth_enable.attr,
2924 NULL
2927 static const struct attribute_group bluetooth_attr_group = {
2928 .attrs = bluetooth_attributes,
2931 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2933 int bts = bluetooth_get_radiosw();
2935 if (bts < 0)
2936 return bts;
2938 *state = bts;
2939 return 0;
2942 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2944 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2947 static void bluetooth_exit(void)
2949 if (tpacpi_bluetooth_rfkill)
2950 rfkill_unregister(tpacpi_bluetooth_rfkill);
2952 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2953 &bluetooth_attr_group);
2956 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2958 int res;
2959 int status = 0;
2961 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2963 TPACPI_ACPIHANDLE_INIT(hkey);
2965 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2966 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2967 tp_features.bluetooth = hkey_handle &&
2968 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2970 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2971 str_supported(tp_features.bluetooth),
2972 status);
2974 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2975 if (dbg_bluetoothemul) {
2976 tp_features.bluetooth = 1;
2977 printk(TPACPI_INFO
2978 "bluetooth switch emulation enabled\n");
2979 } else
2980 #endif
2981 if (tp_features.bluetooth &&
2982 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2983 /* no bluetooth hardware present in system */
2984 tp_features.bluetooth = 0;
2985 dbg_printk(TPACPI_DBG_INIT,
2986 "bluetooth hardware not installed\n");
2989 if (!tp_features.bluetooth)
2990 return 1;
2992 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2993 &bluetooth_attr_group);
2994 if (res)
2995 return res;
2997 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2998 &tpacpi_bluetooth_rfkill,
2999 RFKILL_TYPE_BLUETOOTH,
3000 "tpacpi_bluetooth_sw",
3001 tpacpi_bluetooth_rfk_set,
3002 tpacpi_bluetooth_rfk_get);
3003 if (res) {
3004 bluetooth_exit();
3005 return res;
3008 return 0;
3011 /* procfs -------------------------------------------------------------- */
3012 static int bluetooth_read(char *p)
3014 int len = 0;
3015 int status = bluetooth_get_radiosw();
3017 if (!tp_features.bluetooth)
3018 len += sprintf(p + len, "status:\t\tnot supported\n");
3019 else {
3020 len += sprintf(p + len, "status:\t\t%s\n",
3021 (status == RFKILL_STATE_UNBLOCKED) ?
3022 "enabled" : "disabled");
3023 len += sprintf(p + len, "commands:\tenable, disable\n");
3026 return len;
3029 static int bluetooth_write(char *buf)
3031 char *cmd;
3033 if (!tp_features.bluetooth)
3034 return -ENODEV;
3036 while ((cmd = next_cmd(&buf))) {
3037 if (strlencmp(cmd, "enable") == 0) {
3038 bluetooth_set_radiosw(1, 1);
3039 } else if (strlencmp(cmd, "disable") == 0) {
3040 bluetooth_set_radiosw(0, 1);
3041 } else
3042 return -EINVAL;
3045 return 0;
3048 static struct ibm_struct bluetooth_driver_data = {
3049 .name = "bluetooth",
3050 .read = bluetooth_read,
3051 .write = bluetooth_write,
3052 .exit = bluetooth_exit,
3053 .suspend = bluetooth_suspend,
3056 /*************************************************************************
3057 * Wan subdriver
3060 enum {
3061 /* ACPI GWAN/SWAN bits */
3062 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3063 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3064 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3065 off / last state */
3068 static struct rfkill *tpacpi_wan_rfkill;
3070 static void wan_suspend(pm_message_t state)
3072 /* Try to make sure radio will resume powered off */
3073 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3074 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3077 static int wan_get_radiosw(void)
3079 int status;
3081 if (!tp_features.wan)
3082 return -ENODEV;
3084 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3085 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3086 return RFKILL_STATE_HARD_BLOCKED;
3088 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3089 if (dbg_wwanemul)
3090 return (tpacpi_wwan_emulstate) ?
3091 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3092 #endif
3094 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3095 return -EIO;
3097 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3098 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3101 static void wan_update_rfk(void)
3103 int status;
3105 if (!tpacpi_wan_rfkill)
3106 return;
3108 status = wan_get_radiosw();
3109 if (status < 0)
3110 return;
3111 rfkill_force_state(tpacpi_wan_rfkill, status);
3114 static int wan_set_radiosw(int radio_on, int update_rfk)
3116 int status;
3118 if (!tp_features.wan)
3119 return -ENODEV;
3121 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3122 * reason to risk weird behaviour. */
3123 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3124 && radio_on)
3125 return -EPERM;
3127 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3128 if (dbg_wwanemul) {
3129 tpacpi_wwan_emulstate = !!radio_on;
3130 if (update_rfk)
3131 wan_update_rfk();
3132 return 0;
3134 #endif
3136 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3137 if (radio_on)
3138 status = TP_ACPI_WANCARD_RADIOSSW;
3139 else
3140 status = 0;
3141 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3142 return -EIO;
3144 if (update_rfk)
3145 wan_update_rfk();
3147 return 0;
3150 /* sysfs wan enable ---------------------------------------------------- */
3151 static ssize_t wan_enable_show(struct device *dev,
3152 struct device_attribute *attr,
3153 char *buf)
3155 int status;
3157 status = wan_get_radiosw();
3158 if (status < 0)
3159 return status;
3161 return snprintf(buf, PAGE_SIZE, "%d\n",
3162 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3165 static ssize_t wan_enable_store(struct device *dev,
3166 struct device_attribute *attr,
3167 const char *buf, size_t count)
3169 unsigned long t;
3170 int res;
3172 if (parse_strtoul(buf, 1, &t))
3173 return -EINVAL;
3175 res = wan_set_radiosw(t, 1);
3177 return (res) ? res : count;
3180 static struct device_attribute dev_attr_wan_enable =
3181 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3182 wan_enable_show, wan_enable_store);
3184 /* --------------------------------------------------------------------- */
3186 static struct attribute *wan_attributes[] = {
3187 &dev_attr_wan_enable.attr,
3188 NULL
3191 static const struct attribute_group wan_attr_group = {
3192 .attrs = wan_attributes,
3195 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3197 int wans = wan_get_radiosw();
3199 if (wans < 0)
3200 return wans;
3202 *state = wans;
3203 return 0;
3206 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3208 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3211 static void wan_exit(void)
3213 if (tpacpi_wan_rfkill)
3214 rfkill_unregister(tpacpi_wan_rfkill);
3216 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3217 &wan_attr_group);
3220 static int __init wan_init(struct ibm_init_struct *iibm)
3222 int res;
3223 int status = 0;
3225 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3227 TPACPI_ACPIHANDLE_INIT(hkey);
3229 tp_features.wan = hkey_handle &&
3230 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3232 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3233 str_supported(tp_features.wan),
3234 status);
3236 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3237 if (dbg_wwanemul) {
3238 tp_features.wan = 1;
3239 printk(TPACPI_INFO
3240 "wwan switch emulation enabled\n");
3241 } else
3242 #endif
3243 if (tp_features.wan &&
3244 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3245 /* no wan hardware present in system */
3246 tp_features.wan = 0;
3247 dbg_printk(TPACPI_DBG_INIT,
3248 "wan hardware not installed\n");
3251 if (!tp_features.wan)
3252 return 1;
3254 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3255 &wan_attr_group);
3256 if (res)
3257 return res;
3259 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3260 &tpacpi_wan_rfkill,
3261 RFKILL_TYPE_WWAN,
3262 "tpacpi_wwan_sw",
3263 tpacpi_wan_rfk_set,
3264 tpacpi_wan_rfk_get);
3265 if (res) {
3266 wan_exit();
3267 return res;
3270 return 0;
3273 /* procfs -------------------------------------------------------------- */
3274 static int wan_read(char *p)
3276 int len = 0;
3277 int status = wan_get_radiosw();
3279 if (!tp_features.wan)
3280 len += sprintf(p + len, "status:\t\tnot supported\n");
3281 else {
3282 len += sprintf(p + len, "status:\t\t%s\n",
3283 (status == RFKILL_STATE_UNBLOCKED) ?
3284 "enabled" : "disabled");
3285 len += sprintf(p + len, "commands:\tenable, disable\n");
3288 return len;
3291 static int wan_write(char *buf)
3293 char *cmd;
3295 if (!tp_features.wan)
3296 return -ENODEV;
3298 while ((cmd = next_cmd(&buf))) {
3299 if (strlencmp(cmd, "enable") == 0) {
3300 wan_set_radiosw(1, 1);
3301 } else if (strlencmp(cmd, "disable") == 0) {
3302 wan_set_radiosw(0, 1);
3303 } else
3304 return -EINVAL;
3307 return 0;
3310 static struct ibm_struct wan_driver_data = {
3311 .name = "wan",
3312 .read = wan_read,
3313 .write = wan_write,
3314 .exit = wan_exit,
3315 .suspend = wan_suspend,
3318 /*************************************************************************
3319 * Video subdriver
3322 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3324 enum video_access_mode {
3325 TPACPI_VIDEO_NONE = 0,
3326 TPACPI_VIDEO_570, /* 570 */
3327 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3328 TPACPI_VIDEO_NEW, /* all others */
3331 enum { /* video status flags, based on VIDEO_570 */
3332 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3333 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3334 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3337 enum { /* TPACPI_VIDEO_570 constants */
3338 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3339 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3340 * video_status_flags */
3341 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3342 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3345 static enum video_access_mode video_supported;
3346 static int video_orig_autosw;
3348 static int video_autosw_get(void);
3349 static int video_autosw_set(int enable);
3351 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3353 static int __init video_init(struct ibm_init_struct *iibm)
3355 int ivga;
3357 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3359 TPACPI_ACPIHANDLE_INIT(vid);
3360 TPACPI_ACPIHANDLE_INIT(vid2);
3362 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3363 /* G41, assume IVGA doesn't change */
3364 vid_handle = vid2_handle;
3366 if (!vid_handle)
3367 /* video switching not supported on R30, R31 */
3368 video_supported = TPACPI_VIDEO_NONE;
3369 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3370 /* 570 */
3371 video_supported = TPACPI_VIDEO_570;
3372 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3373 /* 600e/x, 770e, 770x */
3374 video_supported = TPACPI_VIDEO_770;
3375 else
3376 /* all others */
3377 video_supported = TPACPI_VIDEO_NEW;
3379 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3380 str_supported(video_supported != TPACPI_VIDEO_NONE),
3381 video_supported);
3383 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3386 static void video_exit(void)
3388 dbg_printk(TPACPI_DBG_EXIT,
3389 "restoring original video autoswitch mode\n");
3390 if (video_autosw_set(video_orig_autosw))
3391 printk(TPACPI_ERR "error while trying to restore original "
3392 "video autoswitch mode\n");
3395 static int video_outputsw_get(void)
3397 int status = 0;
3398 int i;
3400 switch (video_supported) {
3401 case TPACPI_VIDEO_570:
3402 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3403 TP_ACPI_VIDEO_570_PHSCMD))
3404 return -EIO;
3405 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3406 break;
3407 case TPACPI_VIDEO_770:
3408 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3409 return -EIO;
3410 if (i)
3411 status |= TP_ACPI_VIDEO_S_LCD;
3412 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3413 return -EIO;
3414 if (i)
3415 status |= TP_ACPI_VIDEO_S_CRT;
3416 break;
3417 case TPACPI_VIDEO_NEW:
3418 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3419 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3420 return -EIO;
3421 if (i)
3422 status |= TP_ACPI_VIDEO_S_CRT;
3424 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3425 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3426 return -EIO;
3427 if (i)
3428 status |= TP_ACPI_VIDEO_S_LCD;
3429 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3430 return -EIO;
3431 if (i)
3432 status |= TP_ACPI_VIDEO_S_DVI;
3433 break;
3434 default:
3435 return -ENOSYS;
3438 return status;
3441 static int video_outputsw_set(int status)
3443 int autosw;
3444 int res = 0;
3446 switch (video_supported) {
3447 case TPACPI_VIDEO_570:
3448 res = acpi_evalf(NULL, NULL,
3449 "\\_SB.PHS2", "vdd",
3450 TP_ACPI_VIDEO_570_PHS2CMD,
3451 status | TP_ACPI_VIDEO_570_PHS2SET);
3452 break;
3453 case TPACPI_VIDEO_770:
3454 autosw = video_autosw_get();
3455 if (autosw < 0)
3456 return autosw;
3458 res = video_autosw_set(1);
3459 if (res)
3460 return res;
3461 res = acpi_evalf(vid_handle, NULL,
3462 "ASWT", "vdd", status * 0x100, 0);
3463 if (!autosw && video_autosw_set(autosw)) {
3464 printk(TPACPI_ERR
3465 "video auto-switch left enabled due to error\n");
3466 return -EIO;
3468 break;
3469 case TPACPI_VIDEO_NEW:
3470 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3471 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3472 break;
3473 default:
3474 return -ENOSYS;
3477 return (res)? 0 : -EIO;
3480 static int video_autosw_get(void)
3482 int autosw = 0;
3484 switch (video_supported) {
3485 case TPACPI_VIDEO_570:
3486 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3487 return -EIO;
3488 break;
3489 case TPACPI_VIDEO_770:
3490 case TPACPI_VIDEO_NEW:
3491 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3492 return -EIO;
3493 break;
3494 default:
3495 return -ENOSYS;
3498 return autosw & 1;
3501 static int video_autosw_set(int enable)
3503 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3504 return -EIO;
3505 return 0;
3508 static int video_outputsw_cycle(void)
3510 int autosw = video_autosw_get();
3511 int res;
3513 if (autosw < 0)
3514 return autosw;
3516 switch (video_supported) {
3517 case TPACPI_VIDEO_570:
3518 res = video_autosw_set(1);
3519 if (res)
3520 return res;
3521 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3522 break;
3523 case TPACPI_VIDEO_770:
3524 case TPACPI_VIDEO_NEW:
3525 res = video_autosw_set(1);
3526 if (res)
3527 return res;
3528 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3529 break;
3530 default:
3531 return -ENOSYS;
3533 if (!autosw && video_autosw_set(autosw)) {
3534 printk(TPACPI_ERR
3535 "video auto-switch left enabled due to error\n");
3536 return -EIO;
3539 return (res)? 0 : -EIO;
3542 static int video_expand_toggle(void)
3544 switch (video_supported) {
3545 case TPACPI_VIDEO_570:
3546 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3547 0 : -EIO;
3548 case TPACPI_VIDEO_770:
3549 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3550 0 : -EIO;
3551 case TPACPI_VIDEO_NEW:
3552 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3553 0 : -EIO;
3554 default:
3555 return -ENOSYS;
3557 /* not reached */
3560 static int video_read(char *p)
3562 int status, autosw;
3563 int len = 0;
3565 if (video_supported == TPACPI_VIDEO_NONE) {
3566 len += sprintf(p + len, "status:\t\tnot supported\n");
3567 return len;
3570 status = video_outputsw_get();
3571 if (status < 0)
3572 return status;
3574 autosw = video_autosw_get();
3575 if (autosw < 0)
3576 return autosw;
3578 len += sprintf(p + len, "status:\t\tsupported\n");
3579 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3580 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3581 if (video_supported == TPACPI_VIDEO_NEW)
3582 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3583 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3584 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3585 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3586 if (video_supported == TPACPI_VIDEO_NEW)
3587 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3588 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3589 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3591 return len;
3594 static int video_write(char *buf)
3596 char *cmd;
3597 int enable, disable, status;
3598 int res;
3600 if (video_supported == TPACPI_VIDEO_NONE)
3601 return -ENODEV;
3603 enable = 0;
3604 disable = 0;
3606 while ((cmd = next_cmd(&buf))) {
3607 if (strlencmp(cmd, "lcd_enable") == 0) {
3608 enable |= TP_ACPI_VIDEO_S_LCD;
3609 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3610 disable |= TP_ACPI_VIDEO_S_LCD;
3611 } else if (strlencmp(cmd, "crt_enable") == 0) {
3612 enable |= TP_ACPI_VIDEO_S_CRT;
3613 } else if (strlencmp(cmd, "crt_disable") == 0) {
3614 disable |= TP_ACPI_VIDEO_S_CRT;
3615 } else if (video_supported == TPACPI_VIDEO_NEW &&
3616 strlencmp(cmd, "dvi_enable") == 0) {
3617 enable |= TP_ACPI_VIDEO_S_DVI;
3618 } else if (video_supported == TPACPI_VIDEO_NEW &&
3619 strlencmp(cmd, "dvi_disable") == 0) {
3620 disable |= TP_ACPI_VIDEO_S_DVI;
3621 } else if (strlencmp(cmd, "auto_enable") == 0) {
3622 res = video_autosw_set(1);
3623 if (res)
3624 return res;
3625 } else if (strlencmp(cmd, "auto_disable") == 0) {
3626 res = video_autosw_set(0);
3627 if (res)
3628 return res;
3629 } else if (strlencmp(cmd, "video_switch") == 0) {
3630 res = video_outputsw_cycle();
3631 if (res)
3632 return res;
3633 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3634 res = video_expand_toggle();
3635 if (res)
3636 return res;
3637 } else
3638 return -EINVAL;
3641 if (enable || disable) {
3642 status = video_outputsw_get();
3643 if (status < 0)
3644 return status;
3645 res = video_outputsw_set((status & ~disable) | enable);
3646 if (res)
3647 return res;
3650 return 0;
3653 static struct ibm_struct video_driver_data = {
3654 .name = "video",
3655 .read = video_read,
3656 .write = video_write,
3657 .exit = video_exit,
3660 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3662 /*************************************************************************
3663 * Light (thinklight) subdriver
3666 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3667 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3669 static int light_get_status(void)
3671 int status = 0;
3673 if (tp_features.light_status) {
3674 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3675 return -EIO;
3676 return (!!status);
3679 return -ENXIO;
3682 static int light_set_status(int status)
3684 int rc;
3686 if (tp_features.light) {
3687 if (cmos_handle) {
3688 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3689 (status)?
3690 TP_CMOS_THINKLIGHT_ON :
3691 TP_CMOS_THINKLIGHT_OFF);
3692 } else {
3693 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3694 (status)? 1 : 0);
3696 return (rc)? 0 : -EIO;
3699 return -ENXIO;
3702 static void light_set_status_worker(struct work_struct *work)
3704 struct tpacpi_led_classdev *data =
3705 container_of(work, struct tpacpi_led_classdev, work);
3707 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3708 light_set_status((data->new_brightness != LED_OFF));
3711 static void light_sysfs_set(struct led_classdev *led_cdev,
3712 enum led_brightness brightness)
3714 struct tpacpi_led_classdev *data =
3715 container_of(led_cdev,
3716 struct tpacpi_led_classdev,
3717 led_classdev);
3718 data->new_brightness = brightness;
3719 queue_work(tpacpi_wq, &data->work);
3722 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3724 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3727 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3728 .led_classdev = {
3729 .name = "tpacpi::thinklight",
3730 .brightness_set = &light_sysfs_set,
3731 .brightness_get = &light_sysfs_get,
3735 static int __init light_init(struct ibm_init_struct *iibm)
3737 int rc;
3739 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3741 TPACPI_ACPIHANDLE_INIT(ledb);
3742 TPACPI_ACPIHANDLE_INIT(lght);
3743 TPACPI_ACPIHANDLE_INIT(cmos);
3744 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3746 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3747 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3749 if (tp_features.light)
3750 /* light status not supported on
3751 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3752 tp_features.light_status =
3753 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3755 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3756 str_supported(tp_features.light),
3757 str_supported(tp_features.light_status));
3759 if (!tp_features.light)
3760 return 1;
3762 rc = led_classdev_register(&tpacpi_pdev->dev,
3763 &tpacpi_led_thinklight.led_classdev);
3765 if (rc < 0) {
3766 tp_features.light = 0;
3767 tp_features.light_status = 0;
3768 } else {
3769 rc = 0;
3772 return rc;
3775 static void light_exit(void)
3777 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3778 if (work_pending(&tpacpi_led_thinklight.work))
3779 flush_workqueue(tpacpi_wq);
3782 static int light_read(char *p)
3784 int len = 0;
3785 int status;
3787 if (!tp_features.light) {
3788 len += sprintf(p + len, "status:\t\tnot supported\n");
3789 } else if (!tp_features.light_status) {
3790 len += sprintf(p + len, "status:\t\tunknown\n");
3791 len += sprintf(p + len, "commands:\ton, off\n");
3792 } else {
3793 status = light_get_status();
3794 if (status < 0)
3795 return status;
3796 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3797 len += sprintf(p + len, "commands:\ton, off\n");
3800 return len;
3803 static int light_write(char *buf)
3805 char *cmd;
3806 int newstatus = 0;
3808 if (!tp_features.light)
3809 return -ENODEV;
3811 while ((cmd = next_cmd(&buf))) {
3812 if (strlencmp(cmd, "on") == 0) {
3813 newstatus = 1;
3814 } else if (strlencmp(cmd, "off") == 0) {
3815 newstatus = 0;
3816 } else
3817 return -EINVAL;
3820 return light_set_status(newstatus);
3823 static struct ibm_struct light_driver_data = {
3824 .name = "light",
3825 .read = light_read,
3826 .write = light_write,
3827 .exit = light_exit,
3830 /*************************************************************************
3831 * Dock subdriver
3834 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3836 static void dock_notify(struct ibm_struct *ibm, u32 event);
3837 static int dock_read(char *p);
3838 static int dock_write(char *buf);
3840 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3841 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3842 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3843 "\\_SB.PCI.ISA.SLCE", /* 570 */
3844 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3846 /* don't list other alternatives as we install a notify handler on the 570 */
3847 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3849 static const struct acpi_device_id ibm_pci_device_ids[] = {
3850 {PCI_ROOT_HID_STRING, 0},
3851 {"", 0},
3854 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3856 .notify = dock_notify,
3857 .handle = &dock_handle,
3858 .type = ACPI_SYSTEM_NOTIFY,
3861 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3862 * We just use it to get notifications of dock hotplug
3863 * in very old thinkpads */
3864 .hid = ibm_pci_device_ids,
3865 .notify = dock_notify,
3866 .handle = &pci_handle,
3867 .type = ACPI_SYSTEM_NOTIFY,
3871 static struct ibm_struct dock_driver_data[2] = {
3873 .name = "dock",
3874 .read = dock_read,
3875 .write = dock_write,
3876 .acpi = &ibm_dock_acpidriver[0],
3879 .name = "dock",
3880 .acpi = &ibm_dock_acpidriver[1],
3884 #define dock_docked() (_sta(dock_handle) & 1)
3886 static int __init dock_init(struct ibm_init_struct *iibm)
3888 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3890 TPACPI_ACPIHANDLE_INIT(dock);
3892 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3893 str_supported(dock_handle != NULL));
3895 return (dock_handle)? 0 : 1;
3898 static int __init dock_init2(struct ibm_init_struct *iibm)
3900 int dock2_needed;
3902 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3904 if (dock_driver_data[0].flags.acpi_driver_registered &&
3905 dock_driver_data[0].flags.acpi_notify_installed) {
3906 TPACPI_ACPIHANDLE_INIT(pci);
3907 dock2_needed = (pci_handle != NULL);
3908 vdbg_printk(TPACPI_DBG_INIT,
3909 "dock PCI handler for the TP 570 is %s\n",
3910 str_supported(dock2_needed));
3911 } else {
3912 vdbg_printk(TPACPI_DBG_INIT,
3913 "dock subdriver part 2 not required\n");
3914 dock2_needed = 0;
3917 return (dock2_needed)? 0 : 1;
3920 static void dock_notify(struct ibm_struct *ibm, u32 event)
3922 int docked = dock_docked();
3923 int pci = ibm->acpi->hid && ibm->acpi->device &&
3924 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3925 int data;
3927 if (event == 1 && !pci) /* 570 */
3928 data = 1; /* button */
3929 else if (event == 1 && pci) /* 570 */
3930 data = 3; /* dock */
3931 else if (event == 3 && docked)
3932 data = 1; /* button */
3933 else if (event == 3 && !docked)
3934 data = 2; /* undock */
3935 else if (event == 0 && docked)
3936 data = 3; /* dock */
3937 else {
3938 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3939 event, _sta(dock_handle));
3940 data = 0; /* unknown */
3942 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3943 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3944 dev_name(&ibm->acpi->device->dev),
3945 event, data);
3948 static int dock_read(char *p)
3950 int len = 0;
3951 int docked = dock_docked();
3953 if (!dock_handle)
3954 len += sprintf(p + len, "status:\t\tnot supported\n");
3955 else if (!docked)
3956 len += sprintf(p + len, "status:\t\tundocked\n");
3957 else {
3958 len += sprintf(p + len, "status:\t\tdocked\n");
3959 len += sprintf(p + len, "commands:\tdock, undock\n");
3962 return len;
3965 static int dock_write(char *buf)
3967 char *cmd;
3969 if (!dock_docked())
3970 return -ENODEV;
3972 while ((cmd = next_cmd(&buf))) {
3973 if (strlencmp(cmd, "undock") == 0) {
3974 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3975 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3976 return -EIO;
3977 } else if (strlencmp(cmd, "dock") == 0) {
3978 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3979 return -EIO;
3980 } else
3981 return -EINVAL;
3984 return 0;
3987 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3989 /*************************************************************************
3990 * Bay subdriver
3993 #ifdef CONFIG_THINKPAD_ACPI_BAY
3995 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3996 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3997 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3998 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3999 ); /* A21e, R30, R31 */
4000 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4001 "_EJ0", /* all others */
4002 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4003 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4004 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4005 ); /* all others */
4006 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4007 "_EJ0", /* 770x */
4008 ); /* all others */
4010 static int __init bay_init(struct ibm_init_struct *iibm)
4012 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4014 TPACPI_ACPIHANDLE_INIT(bay);
4015 if (bay_handle)
4016 TPACPI_ACPIHANDLE_INIT(bay_ej);
4017 TPACPI_ACPIHANDLE_INIT(bay2);
4018 if (bay2_handle)
4019 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4021 tp_features.bay_status = bay_handle &&
4022 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4023 tp_features.bay_status2 = bay2_handle &&
4024 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4026 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4027 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4028 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4029 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4031 vdbg_printk(TPACPI_DBG_INIT,
4032 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4033 str_supported(tp_features.bay_status),
4034 str_supported(tp_features.bay_eject),
4035 str_supported(tp_features.bay_status2),
4036 str_supported(tp_features.bay_eject2));
4038 return (tp_features.bay_status || tp_features.bay_eject ||
4039 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4042 static void bay_notify(struct ibm_struct *ibm, u32 event)
4044 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4045 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4046 dev_name(&ibm->acpi->device->dev),
4047 event, 0);
4050 #define bay_occupied(b) (_sta(b##_handle) & 1)
4052 static int bay_read(char *p)
4054 int len = 0;
4055 int occupied = bay_occupied(bay);
4056 int occupied2 = bay_occupied(bay2);
4057 int eject, eject2;
4059 len += sprintf(p + len, "status:\t\t%s\n",
4060 tp_features.bay_status ?
4061 (occupied ? "occupied" : "unoccupied") :
4062 "not supported");
4063 if (tp_features.bay_status2)
4064 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4065 "occupied" : "unoccupied");
4067 eject = tp_features.bay_eject && occupied;
4068 eject2 = tp_features.bay_eject2 && occupied2;
4070 if (eject && eject2)
4071 len += sprintf(p + len, "commands:\teject, eject2\n");
4072 else if (eject)
4073 len += sprintf(p + len, "commands:\teject\n");
4074 else if (eject2)
4075 len += sprintf(p + len, "commands:\teject2\n");
4077 return len;
4080 static int bay_write(char *buf)
4082 char *cmd;
4084 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4085 return -ENODEV;
4087 while ((cmd = next_cmd(&buf))) {
4088 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4089 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4090 return -EIO;
4091 } else if (tp_features.bay_eject2 &&
4092 strlencmp(cmd, "eject2") == 0) {
4093 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4094 return -EIO;
4095 } else
4096 return -EINVAL;
4099 return 0;
4102 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4103 .notify = bay_notify,
4104 .handle = &bay_handle,
4105 .type = ACPI_SYSTEM_NOTIFY,
4108 static struct ibm_struct bay_driver_data = {
4109 .name = "bay",
4110 .read = bay_read,
4111 .write = bay_write,
4112 .acpi = &ibm_bay_acpidriver,
4115 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4117 /*************************************************************************
4118 * CMOS subdriver
4121 /* sysfs cmos_command -------------------------------------------------- */
4122 static ssize_t cmos_command_store(struct device *dev,
4123 struct device_attribute *attr,
4124 const char *buf, size_t count)
4126 unsigned long cmos_cmd;
4127 int res;
4129 if (parse_strtoul(buf, 21, &cmos_cmd))
4130 return -EINVAL;
4132 res = issue_thinkpad_cmos_command(cmos_cmd);
4133 return (res)? res : count;
4136 static struct device_attribute dev_attr_cmos_command =
4137 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4139 /* --------------------------------------------------------------------- */
4141 static int __init cmos_init(struct ibm_init_struct *iibm)
4143 int res;
4145 vdbg_printk(TPACPI_DBG_INIT,
4146 "initializing cmos commands subdriver\n");
4148 TPACPI_ACPIHANDLE_INIT(cmos);
4150 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4151 str_supported(cmos_handle != NULL));
4153 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4154 if (res)
4155 return res;
4157 return (cmos_handle)? 0 : 1;
4160 static void cmos_exit(void)
4162 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4165 static int cmos_read(char *p)
4167 int len = 0;
4169 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4170 R30, R31, T20-22, X20-21 */
4171 if (!cmos_handle)
4172 len += sprintf(p + len, "status:\t\tnot supported\n");
4173 else {
4174 len += sprintf(p + len, "status:\t\tsupported\n");
4175 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4178 return len;
4181 static int cmos_write(char *buf)
4183 char *cmd;
4184 int cmos_cmd, res;
4186 while ((cmd = next_cmd(&buf))) {
4187 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4188 cmos_cmd >= 0 && cmos_cmd <= 21) {
4189 /* cmos_cmd set */
4190 } else
4191 return -EINVAL;
4193 res = issue_thinkpad_cmos_command(cmos_cmd);
4194 if (res)
4195 return res;
4198 return 0;
4201 static struct ibm_struct cmos_driver_data = {
4202 .name = "cmos",
4203 .read = cmos_read,
4204 .write = cmos_write,
4205 .exit = cmos_exit,
4208 /*************************************************************************
4209 * LED subdriver
4212 enum led_access_mode {
4213 TPACPI_LED_NONE = 0,
4214 TPACPI_LED_570, /* 570 */
4215 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4216 TPACPI_LED_NEW, /* all others */
4219 enum { /* For TPACPI_LED_OLD */
4220 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4221 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4222 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4225 enum led_status_t {
4226 TPACPI_LED_OFF = 0,
4227 TPACPI_LED_ON,
4228 TPACPI_LED_BLINK,
4231 static enum led_access_mode led_supported;
4233 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4234 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4235 /* T20-22, X20-21 */
4236 "LED", /* all others */
4237 ); /* R30, R31 */
4239 #define TPACPI_LED_NUMLEDS 8
4240 static struct tpacpi_led_classdev *tpacpi_leds;
4241 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4242 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4243 /* there's a limit of 19 chars + NULL before 2.6.26 */
4244 "tpacpi::power",
4245 "tpacpi:orange:batt",
4246 "tpacpi:green:batt",
4247 "tpacpi::dock_active",
4248 "tpacpi::bay_active",
4249 "tpacpi::dock_batt",
4250 "tpacpi::unknown_led",
4251 "tpacpi::standby",
4254 static int led_get_status(const unsigned int led)
4256 int status;
4257 enum led_status_t led_s;
4259 switch (led_supported) {
4260 case TPACPI_LED_570:
4261 if (!acpi_evalf(ec_handle,
4262 &status, "GLED", "dd", 1 << led))
4263 return -EIO;
4264 led_s = (status == 0)?
4265 TPACPI_LED_OFF :
4266 ((status == 1)?
4267 TPACPI_LED_ON :
4268 TPACPI_LED_BLINK);
4269 tpacpi_led_state_cache[led] = led_s;
4270 return led_s;
4271 default:
4272 return -ENXIO;
4275 /* not reached */
4278 static int led_set_status(const unsigned int led,
4279 const enum led_status_t ledstatus)
4281 /* off, on, blink. Index is led_status_t */
4282 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4283 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4285 int rc = 0;
4287 switch (led_supported) {
4288 case TPACPI_LED_570:
4289 /* 570 */
4290 if (led > 7)
4291 return -EINVAL;
4292 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4293 (1 << led), led_sled_arg1[ledstatus]))
4294 rc = -EIO;
4295 break;
4296 case TPACPI_LED_OLD:
4297 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4298 if (led > 7)
4299 return -EINVAL;
4300 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4301 if (rc >= 0)
4302 rc = ec_write(TPACPI_LED_EC_HLBL,
4303 (ledstatus == TPACPI_LED_BLINK) << led);
4304 if (rc >= 0)
4305 rc = ec_write(TPACPI_LED_EC_HLCL,
4306 (ledstatus != TPACPI_LED_OFF) << led);
4307 break;
4308 case TPACPI_LED_NEW:
4309 /* all others */
4310 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4311 led, led_led_arg1[ledstatus]))
4312 rc = -EIO;
4313 break;
4314 default:
4315 rc = -ENXIO;
4318 if (!rc)
4319 tpacpi_led_state_cache[led] = ledstatus;
4321 return rc;
4324 static void led_sysfs_set_status(unsigned int led,
4325 enum led_brightness brightness)
4327 led_set_status(led,
4328 (brightness == LED_OFF) ?
4329 TPACPI_LED_OFF :
4330 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4331 TPACPI_LED_BLINK : TPACPI_LED_ON);
4334 static void led_set_status_worker(struct work_struct *work)
4336 struct tpacpi_led_classdev *data =
4337 container_of(work, struct tpacpi_led_classdev, work);
4339 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4340 led_sysfs_set_status(data->led, data->new_brightness);
4343 static void led_sysfs_set(struct led_classdev *led_cdev,
4344 enum led_brightness brightness)
4346 struct tpacpi_led_classdev *data = container_of(led_cdev,
4347 struct tpacpi_led_classdev, led_classdev);
4349 data->new_brightness = brightness;
4350 queue_work(tpacpi_wq, &data->work);
4353 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4354 unsigned long *delay_on, unsigned long *delay_off)
4356 struct tpacpi_led_classdev *data = container_of(led_cdev,
4357 struct tpacpi_led_classdev, led_classdev);
4359 /* Can we choose the flash rate? */
4360 if (*delay_on == 0 && *delay_off == 0) {
4361 /* yes. set them to the hardware blink rate (1 Hz) */
4362 *delay_on = 500; /* ms */
4363 *delay_off = 500; /* ms */
4364 } else if ((*delay_on != 500) || (*delay_off != 500))
4365 return -EINVAL;
4367 data->new_brightness = TPACPI_LED_BLINK;
4368 queue_work(tpacpi_wq, &data->work);
4370 return 0;
4373 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4375 int rc;
4377 struct tpacpi_led_classdev *data = container_of(led_cdev,
4378 struct tpacpi_led_classdev, led_classdev);
4380 rc = led_get_status(data->led);
4382 if (rc == TPACPI_LED_OFF || rc < 0)
4383 rc = LED_OFF; /* no error handling in led class :( */
4384 else
4385 rc = LED_FULL;
4387 return rc;
4390 static void led_exit(void)
4392 unsigned int i;
4394 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4395 if (tpacpi_leds[i].led_classdev.name)
4396 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4399 kfree(tpacpi_leds);
4402 static int __init led_init(struct ibm_init_struct *iibm)
4404 unsigned int i;
4405 int rc;
4407 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4409 TPACPI_ACPIHANDLE_INIT(led);
4411 if (!led_handle)
4412 /* led not supported on R30, R31 */
4413 led_supported = TPACPI_LED_NONE;
4414 else if (strlencmp(led_path, "SLED") == 0)
4415 /* 570 */
4416 led_supported = TPACPI_LED_570;
4417 else if (strlencmp(led_path, "SYSL") == 0)
4418 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4419 led_supported = TPACPI_LED_OLD;
4420 else
4421 /* all others */
4422 led_supported = TPACPI_LED_NEW;
4424 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4425 str_supported(led_supported), led_supported);
4427 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4428 GFP_KERNEL);
4429 if (!tpacpi_leds) {
4430 printk(TPACPI_ERR "Out of memory for LED data\n");
4431 return -ENOMEM;
4434 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4435 tpacpi_leds[i].led = i;
4437 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4438 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4439 if (led_supported == TPACPI_LED_570)
4440 tpacpi_leds[i].led_classdev.brightness_get =
4441 &led_sysfs_get;
4443 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4445 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4447 rc = led_classdev_register(&tpacpi_pdev->dev,
4448 &tpacpi_leds[i].led_classdev);
4449 if (rc < 0) {
4450 tpacpi_leds[i].led_classdev.name = NULL;
4451 led_exit();
4452 return rc;
4456 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4459 #define str_led_status(s) \
4460 ((s) == TPACPI_LED_OFF ? "off" : \
4461 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4463 static int led_read(char *p)
4465 int len = 0;
4467 if (!led_supported) {
4468 len += sprintf(p + len, "status:\t\tnot supported\n");
4469 return len;
4471 len += sprintf(p + len, "status:\t\tsupported\n");
4473 if (led_supported == TPACPI_LED_570) {
4474 /* 570 */
4475 int i, status;
4476 for (i = 0; i < 8; i++) {
4477 status = led_get_status(i);
4478 if (status < 0)
4479 return -EIO;
4480 len += sprintf(p + len, "%d:\t\t%s\n",
4481 i, str_led_status(status));
4485 len += sprintf(p + len, "commands:\t"
4486 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4488 return len;
4491 static int led_write(char *buf)
4493 char *cmd;
4494 int led, rc;
4495 enum led_status_t s;
4497 if (!led_supported)
4498 return -ENODEV;
4500 while ((cmd = next_cmd(&buf))) {
4501 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4502 return -EINVAL;
4504 if (strstr(cmd, "off")) {
4505 s = TPACPI_LED_OFF;
4506 } else if (strstr(cmd, "on")) {
4507 s = TPACPI_LED_ON;
4508 } else if (strstr(cmd, "blink")) {
4509 s = TPACPI_LED_BLINK;
4510 } else {
4511 return -EINVAL;
4514 rc = led_set_status(led, s);
4515 if (rc < 0)
4516 return rc;
4519 return 0;
4522 static struct ibm_struct led_driver_data = {
4523 .name = "led",
4524 .read = led_read,
4525 .write = led_write,
4526 .exit = led_exit,
4529 /*************************************************************************
4530 * Beep subdriver
4533 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4535 static int __init beep_init(struct ibm_init_struct *iibm)
4537 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4539 TPACPI_ACPIHANDLE_INIT(beep);
4541 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4542 str_supported(beep_handle != NULL));
4544 return (beep_handle)? 0 : 1;
4547 static int beep_read(char *p)
4549 int len = 0;
4551 if (!beep_handle)
4552 len += sprintf(p + len, "status:\t\tnot supported\n");
4553 else {
4554 len += sprintf(p + len, "status:\t\tsupported\n");
4555 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4558 return len;
4561 static int beep_write(char *buf)
4563 char *cmd;
4564 int beep_cmd;
4566 if (!beep_handle)
4567 return -ENODEV;
4569 while ((cmd = next_cmd(&buf))) {
4570 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4571 beep_cmd >= 0 && beep_cmd <= 17) {
4572 /* beep_cmd set */
4573 } else
4574 return -EINVAL;
4575 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4576 return -EIO;
4579 return 0;
4582 static struct ibm_struct beep_driver_data = {
4583 .name = "beep",
4584 .read = beep_read,
4585 .write = beep_write,
4588 /*************************************************************************
4589 * Thermal subdriver
4592 enum thermal_access_mode {
4593 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4594 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4595 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4596 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4597 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4600 enum { /* TPACPI_THERMAL_TPEC_* */
4601 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4602 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4603 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4606 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4607 struct ibm_thermal_sensors_struct {
4608 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4611 static enum thermal_access_mode thermal_read_mode;
4613 /* idx is zero-based */
4614 static int thermal_get_sensor(int idx, s32 *value)
4616 int t;
4617 s8 tmp;
4618 char tmpi[5];
4620 t = TP_EC_THERMAL_TMP0;
4622 switch (thermal_read_mode) {
4623 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4624 case TPACPI_THERMAL_TPEC_16:
4625 if (idx >= 8 && idx <= 15) {
4626 t = TP_EC_THERMAL_TMP8;
4627 idx -= 8;
4629 /* fallthrough */
4630 #endif
4631 case TPACPI_THERMAL_TPEC_8:
4632 if (idx <= 7) {
4633 if (!acpi_ec_read(t + idx, &tmp))
4634 return -EIO;
4635 *value = tmp * 1000;
4636 return 0;
4638 break;
4640 case TPACPI_THERMAL_ACPI_UPDT:
4641 if (idx <= 7) {
4642 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4643 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4644 return -EIO;
4645 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4646 return -EIO;
4647 *value = (t - 2732) * 100;
4648 return 0;
4650 break;
4652 case TPACPI_THERMAL_ACPI_TMP07:
4653 if (idx <= 7) {
4654 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4655 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4656 return -EIO;
4657 if (t > 127 || t < -127)
4658 t = TP_EC_THERMAL_TMP_NA;
4659 *value = t * 1000;
4660 return 0;
4662 break;
4664 case TPACPI_THERMAL_NONE:
4665 default:
4666 return -ENOSYS;
4669 return -EINVAL;
4672 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4674 int res, i;
4675 int n;
4677 n = 8;
4678 i = 0;
4680 if (!s)
4681 return -EINVAL;
4683 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4684 n = 16;
4686 for (i = 0 ; i < n; i++) {
4687 res = thermal_get_sensor(i, &s->temp[i]);
4688 if (res)
4689 return res;
4692 return n;
4695 /* sysfs temp##_input -------------------------------------------------- */
4697 static ssize_t thermal_temp_input_show(struct device *dev,
4698 struct device_attribute *attr,
4699 char *buf)
4701 struct sensor_device_attribute *sensor_attr =
4702 to_sensor_dev_attr(attr);
4703 int idx = sensor_attr->index;
4704 s32 value;
4705 int res;
4707 res = thermal_get_sensor(idx, &value);
4708 if (res)
4709 return res;
4710 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4711 return -ENXIO;
4713 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4716 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4717 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4718 thermal_temp_input_show, NULL, _idxB)
4720 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4721 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4722 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4723 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4724 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4725 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4726 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4727 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4728 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4729 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4730 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4731 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4732 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4733 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4734 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4735 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4736 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4739 #define THERMAL_ATTRS(X) \
4740 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4742 static struct attribute *thermal_temp_input_attr[] = {
4743 THERMAL_ATTRS(8),
4744 THERMAL_ATTRS(9),
4745 THERMAL_ATTRS(10),
4746 THERMAL_ATTRS(11),
4747 THERMAL_ATTRS(12),
4748 THERMAL_ATTRS(13),
4749 THERMAL_ATTRS(14),
4750 THERMAL_ATTRS(15),
4751 THERMAL_ATTRS(0),
4752 THERMAL_ATTRS(1),
4753 THERMAL_ATTRS(2),
4754 THERMAL_ATTRS(3),
4755 THERMAL_ATTRS(4),
4756 THERMAL_ATTRS(5),
4757 THERMAL_ATTRS(6),
4758 THERMAL_ATTRS(7),
4759 NULL
4762 static const struct attribute_group thermal_temp_input16_group = {
4763 .attrs = thermal_temp_input_attr
4766 static const struct attribute_group thermal_temp_input8_group = {
4767 .attrs = &thermal_temp_input_attr[8]
4770 #undef THERMAL_SENSOR_ATTR_TEMP
4771 #undef THERMAL_ATTRS
4773 /* --------------------------------------------------------------------- */
4775 static int __init thermal_init(struct ibm_init_struct *iibm)
4777 u8 t, ta1, ta2;
4778 int i;
4779 int acpi_tmp7;
4780 int res;
4782 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4784 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4786 if (thinkpad_id.ec_model) {
4788 * Direct EC access mode: sensors at registers
4789 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4790 * non-implemented, thermal sensors return 0x80 when
4791 * not available
4794 ta1 = ta2 = 0;
4795 for (i = 0; i < 8; i++) {
4796 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4797 ta1 |= t;
4798 } else {
4799 ta1 = 0;
4800 break;
4802 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4803 ta2 |= t;
4804 } else {
4805 ta1 = 0;
4806 break;
4809 if (ta1 == 0) {
4810 /* This is sheer paranoia, but we handle it anyway */
4811 if (acpi_tmp7) {
4812 printk(TPACPI_ERR
4813 "ThinkPad ACPI EC access misbehaving, "
4814 "falling back to ACPI TMPx access "
4815 "mode\n");
4816 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4817 } else {
4818 printk(TPACPI_ERR
4819 "ThinkPad ACPI EC access misbehaving, "
4820 "disabling thermal sensors access\n");
4821 thermal_read_mode = TPACPI_THERMAL_NONE;
4823 } else {
4824 thermal_read_mode =
4825 (ta2 != 0) ?
4826 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4828 } else if (acpi_tmp7) {
4829 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4830 /* 600e/x, 770e, 770x */
4831 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4832 } else {
4833 /* Standard ACPI TMPx access, max 8 sensors */
4834 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4836 } else {
4837 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4838 thermal_read_mode = TPACPI_THERMAL_NONE;
4841 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4842 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4843 thermal_read_mode);
4845 switch (thermal_read_mode) {
4846 case TPACPI_THERMAL_TPEC_16:
4847 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4848 &thermal_temp_input16_group);
4849 if (res)
4850 return res;
4851 break;
4852 case TPACPI_THERMAL_TPEC_8:
4853 case TPACPI_THERMAL_ACPI_TMP07:
4854 case TPACPI_THERMAL_ACPI_UPDT:
4855 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4856 &thermal_temp_input8_group);
4857 if (res)
4858 return res;
4859 break;
4860 case TPACPI_THERMAL_NONE:
4861 default:
4862 return 1;
4865 return 0;
4868 static void thermal_exit(void)
4870 switch (thermal_read_mode) {
4871 case TPACPI_THERMAL_TPEC_16:
4872 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4873 &thermal_temp_input16_group);
4874 break;
4875 case TPACPI_THERMAL_TPEC_8:
4876 case TPACPI_THERMAL_ACPI_TMP07:
4877 case TPACPI_THERMAL_ACPI_UPDT:
4878 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4879 &thermal_temp_input16_group);
4880 break;
4881 case TPACPI_THERMAL_NONE:
4882 default:
4883 break;
4887 static int thermal_read(char *p)
4889 int len = 0;
4890 int n, i;
4891 struct ibm_thermal_sensors_struct t;
4893 n = thermal_get_sensors(&t);
4894 if (unlikely(n < 0))
4895 return n;
4897 len += sprintf(p + len, "temperatures:\t");
4899 if (n > 0) {
4900 for (i = 0; i < (n - 1); i++)
4901 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4902 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4903 } else
4904 len += sprintf(p + len, "not supported\n");
4906 return len;
4909 static struct ibm_struct thermal_driver_data = {
4910 .name = "thermal",
4911 .read = thermal_read,
4912 .exit = thermal_exit,
4915 /*************************************************************************
4916 * EC Dump subdriver
4919 static u8 ecdump_regs[256];
4921 static int ecdump_read(char *p)
4923 int len = 0;
4924 int i, j;
4925 u8 v;
4927 len += sprintf(p + len, "EC "
4928 " +00 +01 +02 +03 +04 +05 +06 +07"
4929 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4930 for (i = 0; i < 256; i += 16) {
4931 len += sprintf(p + len, "EC 0x%02x:", i);
4932 for (j = 0; j < 16; j++) {
4933 if (!acpi_ec_read(i + j, &v))
4934 break;
4935 if (v != ecdump_regs[i + j])
4936 len += sprintf(p + len, " *%02x", v);
4937 else
4938 len += sprintf(p + len, " %02x", v);
4939 ecdump_regs[i + j] = v;
4941 len += sprintf(p + len, "\n");
4942 if (j != 16)
4943 break;
4946 /* These are way too dangerous to advertise openly... */
4947 #if 0
4948 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4949 " (<offset> is 00-ff, <value> is 00-ff)\n");
4950 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4951 " (<offset> is 00-ff, <value> is 0-255)\n");
4952 #endif
4953 return len;
4956 static int ecdump_write(char *buf)
4958 char *cmd;
4959 int i, v;
4961 while ((cmd = next_cmd(&buf))) {
4962 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4963 /* i and v set */
4964 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4965 /* i and v set */
4966 } else
4967 return -EINVAL;
4968 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4969 if (!acpi_ec_write(i, v))
4970 return -EIO;
4971 } else
4972 return -EINVAL;
4975 return 0;
4978 static struct ibm_struct ecdump_driver_data = {
4979 .name = "ecdump",
4980 .read = ecdump_read,
4981 .write = ecdump_write,
4982 .flags.experimental = 1,
4985 /*************************************************************************
4986 * Backlight/brightness subdriver
4989 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4991 enum {
4992 TP_EC_BACKLIGHT = 0x31,
4994 /* TP_EC_BACKLIGHT bitmasks */
4995 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4996 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4997 TP_EC_BACKLIGHT_MAPSW = 0x20,
5000 static struct backlight_device *ibm_backlight_device;
5001 static int brightness_mode;
5002 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5004 static struct mutex brightness_mutex;
5007 * ThinkPads can read brightness from two places: EC 0x31, or
5008 * CMOS NVRAM byte 0x5E, bits 0-3.
5010 * EC 0x31 has the following layout
5011 * Bit 7: unknown function
5012 * Bit 6: unknown function
5013 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5014 * Bit 4: must be set to zero to avoid problems
5015 * Bit 3-0: backlight brightness level
5017 * brightness_get_raw returns status data in the EC 0x31 layout
5019 static int brightness_get_raw(int *status)
5021 u8 lec = 0, lcmos = 0, level = 0;
5023 if (brightness_mode & 1) {
5024 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5025 return -EIO;
5026 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5028 if (brightness_mode & 2) {
5029 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5030 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5031 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5032 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5033 level = lcmos;
5036 if (brightness_mode == 3) {
5037 *status = lec; /* Prefer EC, CMOS is just a backing store */
5038 lec &= TP_EC_BACKLIGHT_LVLMSK;
5039 if (lec == lcmos)
5040 tp_warned.bright_cmos_ec_unsync = 0;
5041 else {
5042 if (!tp_warned.bright_cmos_ec_unsync) {
5043 printk(TPACPI_ERR
5044 "CMOS NVRAM (%u) and EC (%u) do not "
5045 "agree on display brightness level\n",
5046 (unsigned int) lcmos,
5047 (unsigned int) lec);
5048 tp_warned.bright_cmos_ec_unsync = 1;
5050 return -EIO;
5052 } else {
5053 *status = level;
5056 return 0;
5059 /* May return EINTR which can always be mapped to ERESTARTSYS */
5060 static int brightness_set(int value)
5062 int cmos_cmd, inc, i, res;
5063 int current_value;
5064 int command_bits;
5066 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5067 value < 0)
5068 return -EINVAL;
5070 res = mutex_lock_interruptible(&brightness_mutex);
5071 if (res < 0)
5072 return res;
5074 res = brightness_get_raw(&current_value);
5075 if (res < 0)
5076 goto errout;
5078 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5079 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5081 cmos_cmd = value > current_value ?
5082 TP_CMOS_BRIGHTNESS_UP :
5083 TP_CMOS_BRIGHTNESS_DOWN;
5084 inc = (value > current_value)? 1 : -1;
5086 res = 0;
5087 for (i = current_value; i != value; i += inc) {
5088 if ((brightness_mode & 2) &&
5089 issue_thinkpad_cmos_command(cmos_cmd)) {
5090 res = -EIO;
5091 goto errout;
5093 if ((brightness_mode & 1) &&
5094 !acpi_ec_write(TP_EC_BACKLIGHT,
5095 (i + inc) | command_bits)) {
5096 res = -EIO;
5097 goto errout;;
5101 errout:
5102 mutex_unlock(&brightness_mutex);
5103 return res;
5106 /* sysfs backlight class ----------------------------------------------- */
5108 static int brightness_update_status(struct backlight_device *bd)
5110 /* it is the backlight class's job (caller) to handle
5111 * EINTR and other errors properly */
5112 return brightness_set(
5113 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5114 bd->props.power == FB_BLANK_UNBLANK) ?
5115 bd->props.brightness : 0);
5118 static int brightness_get(struct backlight_device *bd)
5120 int status, res;
5122 res = brightness_get_raw(&status);
5123 if (res < 0)
5124 return 0; /* FIXME: teach backlight about error handling */
5126 return status & TP_EC_BACKLIGHT_LVLMSK;
5129 static struct backlight_ops ibm_backlight_data = {
5130 .get_brightness = brightness_get,
5131 .update_status = brightness_update_status,
5134 /* --------------------------------------------------------------------- */
5136 static int __init brightness_init(struct ibm_init_struct *iibm)
5138 int b;
5140 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5142 mutex_init(&brightness_mutex);
5145 * We always attempt to detect acpi support, so as to switch
5146 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5147 * going to publish a backlight interface
5149 b = tpacpi_check_std_acpi_brightness_support();
5150 if (b > 0) {
5151 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5152 printk(TPACPI_NOTICE
5153 "Lenovo BIOS switched to ACPI backlight "
5154 "control mode\n");
5156 if (brightness_enable > 1) {
5157 printk(TPACPI_NOTICE
5158 "standard ACPI backlight interface "
5159 "available, not loading native one...\n");
5160 return 1;
5164 if (!brightness_enable) {
5165 dbg_printk(TPACPI_DBG_INIT,
5166 "brightness support disabled by "
5167 "module parameter\n");
5168 return 1;
5171 if (b > 16) {
5172 printk(TPACPI_ERR
5173 "Unsupported brightness interface, "
5174 "please contact %s\n", TPACPI_MAIL);
5175 return 1;
5177 if (b == 16)
5178 tp_features.bright_16levels = 1;
5180 if (!brightness_mode) {
5181 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5182 brightness_mode = 2;
5183 else
5184 brightness_mode = 3;
5186 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5187 brightness_mode);
5190 if (brightness_mode > 3)
5191 return -EINVAL;
5193 if (brightness_get_raw(&b) < 0)
5194 return 1;
5196 if (tp_features.bright_16levels)
5197 printk(TPACPI_INFO
5198 "detected a 16-level brightness capable ThinkPad\n");
5200 ibm_backlight_device = backlight_device_register(
5201 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5202 &ibm_backlight_data);
5203 if (IS_ERR(ibm_backlight_device)) {
5204 printk(TPACPI_ERR "Could not register backlight device\n");
5205 return PTR_ERR(ibm_backlight_device);
5207 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5209 ibm_backlight_device->props.max_brightness =
5210 (tp_features.bright_16levels)? 15 : 7;
5211 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5212 backlight_update_status(ibm_backlight_device);
5214 return 0;
5217 static void brightness_exit(void)
5219 if (ibm_backlight_device) {
5220 vdbg_printk(TPACPI_DBG_EXIT,
5221 "calling backlight_device_unregister()\n");
5222 backlight_device_unregister(ibm_backlight_device);
5226 static int brightness_read(char *p)
5228 int len = 0;
5229 int level;
5231 level = brightness_get(NULL);
5232 if (level < 0) {
5233 len += sprintf(p + len, "level:\t\tunreadable\n");
5234 } else {
5235 len += sprintf(p + len, "level:\t\t%d\n", level);
5236 len += sprintf(p + len, "commands:\tup, down\n");
5237 len += sprintf(p + len, "commands:\tlevel <level>"
5238 " (<level> is 0-%d)\n",
5239 (tp_features.bright_16levels) ? 15 : 7);
5242 return len;
5245 static int brightness_write(char *buf)
5247 int level;
5248 int rc;
5249 char *cmd;
5250 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5252 level = brightness_get(NULL);
5253 if (level < 0)
5254 return level;
5256 while ((cmd = next_cmd(&buf))) {
5257 if (strlencmp(cmd, "up") == 0) {
5258 if (level < max_level)
5259 level++;
5260 } else if (strlencmp(cmd, "down") == 0) {
5261 if (level > 0)
5262 level--;
5263 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5264 level >= 0 && level <= max_level) {
5265 /* new level set */
5266 } else
5267 return -EINVAL;
5271 * Now we know what the final level should be, so we try to set it.
5272 * Doing it this way makes the syscall restartable in case of EINTR
5274 rc = brightness_set(level);
5275 return (rc == -EINTR)? ERESTARTSYS : rc;
5278 static struct ibm_struct brightness_driver_data = {
5279 .name = "brightness",
5280 .read = brightness_read,
5281 .write = brightness_write,
5282 .exit = brightness_exit,
5285 /*************************************************************************
5286 * Volume subdriver
5289 static int volume_offset = 0x30;
5291 static int volume_read(char *p)
5293 int len = 0;
5294 u8 level;
5296 if (!acpi_ec_read(volume_offset, &level)) {
5297 len += sprintf(p + len, "level:\t\tunreadable\n");
5298 } else {
5299 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5300 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5301 len += sprintf(p + len, "commands:\tup, down, mute\n");
5302 len += sprintf(p + len, "commands:\tlevel <level>"
5303 " (<level> is 0-15)\n");
5306 return len;
5309 static int volume_write(char *buf)
5311 int cmos_cmd, inc, i;
5312 u8 level, mute;
5313 int new_level, new_mute;
5314 char *cmd;
5316 while ((cmd = next_cmd(&buf))) {
5317 if (!acpi_ec_read(volume_offset, &level))
5318 return -EIO;
5319 new_mute = mute = level & 0x40;
5320 new_level = level = level & 0xf;
5322 if (strlencmp(cmd, "up") == 0) {
5323 if (mute)
5324 new_mute = 0;
5325 else
5326 new_level = level == 15 ? 15 : level + 1;
5327 } else if (strlencmp(cmd, "down") == 0) {
5328 if (mute)
5329 new_mute = 0;
5330 else
5331 new_level = level == 0 ? 0 : level - 1;
5332 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5333 new_level >= 0 && new_level <= 15) {
5334 /* new_level set */
5335 } else if (strlencmp(cmd, "mute") == 0) {
5336 new_mute = 0x40;
5337 } else
5338 return -EINVAL;
5340 if (new_level != level) {
5341 /* mute doesn't change */
5343 cmos_cmd = (new_level > level) ?
5344 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5345 inc = new_level > level ? 1 : -1;
5347 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5348 !acpi_ec_write(volume_offset, level)))
5349 return -EIO;
5351 for (i = level; i != new_level; i += inc)
5352 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5353 !acpi_ec_write(volume_offset, i + inc))
5354 return -EIO;
5356 if (mute &&
5357 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5358 !acpi_ec_write(volume_offset, new_level + mute))) {
5359 return -EIO;
5363 if (new_mute != mute) {
5364 /* level doesn't change */
5366 cmos_cmd = (new_mute) ?
5367 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5369 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5370 !acpi_ec_write(volume_offset, level + new_mute))
5371 return -EIO;
5375 return 0;
5378 static struct ibm_struct volume_driver_data = {
5379 .name = "volume",
5380 .read = volume_read,
5381 .write = volume_write,
5384 /*************************************************************************
5385 * Fan subdriver
5389 * FAN ACCESS MODES
5391 * TPACPI_FAN_RD_ACPI_GFAN:
5392 * ACPI GFAN method: returns fan level
5394 * see TPACPI_FAN_WR_ACPI_SFAN
5395 * EC 0x2f (HFSP) not available if GFAN exists
5397 * TPACPI_FAN_WR_ACPI_SFAN:
5398 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5400 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5401 * it for writing.
5403 * TPACPI_FAN_WR_TPEC:
5404 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5405 * Supported on almost all ThinkPads
5407 * Fan speed changes of any sort (including those caused by the
5408 * disengaged mode) are usually done slowly by the firmware as the
5409 * maximum ammount of fan duty cycle change per second seems to be
5410 * limited.
5412 * Reading is not available if GFAN exists.
5413 * Writing is not available if SFAN exists.
5415 * Bits
5416 * 7 automatic mode engaged;
5417 * (default operation mode of the ThinkPad)
5418 * fan level is ignored in this mode.
5419 * 6 full speed mode (takes precedence over bit 7);
5420 * not available on all thinkpads. May disable
5421 * the tachometer while the fan controller ramps up
5422 * the speed (which can take up to a few *minutes*).
5423 * Speeds up fan to 100% duty-cycle, which is far above
5424 * the standard RPM levels. It is not impossible that
5425 * it could cause hardware damage.
5426 * 5-3 unused in some models. Extra bits for fan level
5427 * in others, but still useless as all values above
5428 * 7 map to the same speed as level 7 in these models.
5429 * 2-0 fan level (0..7 usually)
5430 * 0x00 = stop
5431 * 0x07 = max (set when temperatures critical)
5432 * Some ThinkPads may have other levels, see
5433 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5435 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5436 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5437 * does so, its initial value is meaningless (0x07).
5439 * For firmware bugs, refer to:
5440 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5442 * ----
5444 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5445 * Main fan tachometer reading (in RPM)
5447 * This register is present on all ThinkPads with a new-style EC, and
5448 * it is known not to be present on the A21m/e, and T22, as there is
5449 * something else in offset 0x84 according to the ACPI DSDT. Other
5450 * ThinkPads from this same time period (and earlier) probably lack the
5451 * tachometer as well.
5453 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5454 * was never fixed by IBM to report the EC firmware version string
5455 * probably support the tachometer (like the early X models), so
5456 * detecting it is quite hard. We need more data to know for sure.
5458 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5459 * might result.
5461 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5462 * mode.
5464 * For firmware bugs, refer to:
5465 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5467 * TPACPI_FAN_WR_ACPI_FANS:
5468 * ThinkPad X31, X40, X41. Not available in the X60.
5470 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5471 * high speed. ACPI DSDT seems to map these three speeds to levels
5472 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5473 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5475 * The speeds are stored on handles
5476 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5478 * There are three default speed sets, acessible as handles:
5479 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5481 * ACPI DSDT switches which set is in use depending on various
5482 * factors.
5484 * TPACPI_FAN_WR_TPEC is also available and should be used to
5485 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5486 * but the ACPI tables just mention level 7.
5489 enum { /* Fan control constants */
5490 fan_status_offset = 0x2f, /* EC register 0x2f */
5491 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5492 * 0x84 must be read before 0x85 */
5494 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5495 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5497 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5500 enum fan_status_access_mode {
5501 TPACPI_FAN_NONE = 0, /* No fan status or control */
5502 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5503 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5506 enum fan_control_access_mode {
5507 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5508 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5509 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5510 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5513 enum fan_control_commands {
5514 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5515 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5516 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5517 * and also watchdog cmd */
5520 static int fan_control_allowed;
5522 static enum fan_status_access_mode fan_status_access_mode;
5523 static enum fan_control_access_mode fan_control_access_mode;
5524 static enum fan_control_commands fan_control_commands;
5526 static u8 fan_control_initial_status;
5527 static u8 fan_control_desired_level;
5528 static u8 fan_control_resume_level;
5529 static int fan_watchdog_maxinterval;
5531 static struct mutex fan_mutex;
5533 static void fan_watchdog_fire(struct work_struct *ignored);
5534 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5536 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5537 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5538 "\\FSPD", /* 600e/x, 770e, 770x */
5539 ); /* all others */
5540 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5541 "JFNS", /* 770x-JL */
5542 ); /* all others */
5545 * Call with fan_mutex held
5547 static void fan_update_desired_level(u8 status)
5549 if ((status &
5550 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5551 if (status > 7)
5552 fan_control_desired_level = 7;
5553 else
5554 fan_control_desired_level = status;
5558 static int fan_get_status(u8 *status)
5560 u8 s;
5562 /* TODO:
5563 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5565 switch (fan_status_access_mode) {
5566 case TPACPI_FAN_RD_ACPI_GFAN:
5567 /* 570, 600e/x, 770e, 770x */
5569 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5570 return -EIO;
5572 if (likely(status))
5573 *status = s & 0x07;
5575 break;
5577 case TPACPI_FAN_RD_TPEC:
5578 /* all except 570, 600e/x, 770e, 770x */
5579 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5580 return -EIO;
5582 if (likely(status))
5583 *status = s;
5585 break;
5587 default:
5588 return -ENXIO;
5591 return 0;
5594 static int fan_get_status_safe(u8 *status)
5596 int rc;
5597 u8 s;
5599 if (mutex_lock_interruptible(&fan_mutex))
5600 return -ERESTARTSYS;
5601 rc = fan_get_status(&s);
5602 if (!rc)
5603 fan_update_desired_level(s);
5604 mutex_unlock(&fan_mutex);
5606 if (status)
5607 *status = s;
5609 return rc;
5612 static int fan_get_speed(unsigned int *speed)
5614 u8 hi, lo;
5616 switch (fan_status_access_mode) {
5617 case TPACPI_FAN_RD_TPEC:
5618 /* all except 570, 600e/x, 770e, 770x */
5619 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5620 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5621 return -EIO;
5623 if (likely(speed))
5624 *speed = (hi << 8) | lo;
5626 break;
5628 default:
5629 return -ENXIO;
5632 return 0;
5635 static int fan_set_level(int level)
5637 if (!fan_control_allowed)
5638 return -EPERM;
5640 switch (fan_control_access_mode) {
5641 case TPACPI_FAN_WR_ACPI_SFAN:
5642 if (level >= 0 && level <= 7) {
5643 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5644 return -EIO;
5645 } else
5646 return -EINVAL;
5647 break;
5649 case TPACPI_FAN_WR_ACPI_FANS:
5650 case TPACPI_FAN_WR_TPEC:
5651 if (!(level & TP_EC_FAN_AUTO) &&
5652 !(level & TP_EC_FAN_FULLSPEED) &&
5653 ((level < 0) || (level > 7)))
5654 return -EINVAL;
5656 /* safety net should the EC not support AUTO
5657 * or FULLSPEED mode bits and just ignore them */
5658 if (level & TP_EC_FAN_FULLSPEED)
5659 level |= 7; /* safety min speed 7 */
5660 else if (level & TP_EC_FAN_AUTO)
5661 level |= 4; /* safety min speed 4 */
5663 if (!acpi_ec_write(fan_status_offset, level))
5664 return -EIO;
5665 else
5666 tp_features.fan_ctrl_status_undef = 0;
5667 break;
5669 default:
5670 return -ENXIO;
5672 return 0;
5675 static int fan_set_level_safe(int level)
5677 int rc;
5679 if (!fan_control_allowed)
5680 return -EPERM;
5682 if (mutex_lock_interruptible(&fan_mutex))
5683 return -ERESTARTSYS;
5685 if (level == TPACPI_FAN_LAST_LEVEL)
5686 level = fan_control_desired_level;
5688 rc = fan_set_level(level);
5689 if (!rc)
5690 fan_update_desired_level(level);
5692 mutex_unlock(&fan_mutex);
5693 return rc;
5696 static int fan_set_enable(void)
5698 u8 s;
5699 int rc;
5701 if (!fan_control_allowed)
5702 return -EPERM;
5704 if (mutex_lock_interruptible(&fan_mutex))
5705 return -ERESTARTSYS;
5707 switch (fan_control_access_mode) {
5708 case TPACPI_FAN_WR_ACPI_FANS:
5709 case TPACPI_FAN_WR_TPEC:
5710 rc = fan_get_status(&s);
5711 if (rc < 0)
5712 break;
5714 /* Don't go out of emergency fan mode */
5715 if (s != 7) {
5716 s &= 0x07;
5717 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5720 if (!acpi_ec_write(fan_status_offset, s))
5721 rc = -EIO;
5722 else {
5723 tp_features.fan_ctrl_status_undef = 0;
5724 rc = 0;
5726 break;
5728 case TPACPI_FAN_WR_ACPI_SFAN:
5729 rc = fan_get_status(&s);
5730 if (rc < 0)
5731 break;
5733 s &= 0x07;
5735 /* Set fan to at least level 4 */
5736 s |= 4;
5738 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5739 rc = -EIO;
5740 else
5741 rc = 0;
5742 break;
5744 default:
5745 rc = -ENXIO;
5748 mutex_unlock(&fan_mutex);
5749 return rc;
5752 static int fan_set_disable(void)
5754 int rc;
5756 if (!fan_control_allowed)
5757 return -EPERM;
5759 if (mutex_lock_interruptible(&fan_mutex))
5760 return -ERESTARTSYS;
5762 rc = 0;
5763 switch (fan_control_access_mode) {
5764 case TPACPI_FAN_WR_ACPI_FANS:
5765 case TPACPI_FAN_WR_TPEC:
5766 if (!acpi_ec_write(fan_status_offset, 0x00))
5767 rc = -EIO;
5768 else {
5769 fan_control_desired_level = 0;
5770 tp_features.fan_ctrl_status_undef = 0;
5772 break;
5774 case TPACPI_FAN_WR_ACPI_SFAN:
5775 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5776 rc = -EIO;
5777 else
5778 fan_control_desired_level = 0;
5779 break;
5781 default:
5782 rc = -ENXIO;
5786 mutex_unlock(&fan_mutex);
5787 return rc;
5790 static int fan_set_speed(int speed)
5792 int rc;
5794 if (!fan_control_allowed)
5795 return -EPERM;
5797 if (mutex_lock_interruptible(&fan_mutex))
5798 return -ERESTARTSYS;
5800 rc = 0;
5801 switch (fan_control_access_mode) {
5802 case TPACPI_FAN_WR_ACPI_FANS:
5803 if (speed >= 0 && speed <= 65535) {
5804 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5805 speed, speed, speed))
5806 rc = -EIO;
5807 } else
5808 rc = -EINVAL;
5809 break;
5811 default:
5812 rc = -ENXIO;
5815 mutex_unlock(&fan_mutex);
5816 return rc;
5819 static void fan_watchdog_reset(void)
5821 static int fan_watchdog_active;
5823 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5824 return;
5826 if (fan_watchdog_active)
5827 cancel_delayed_work(&fan_watchdog_task);
5829 if (fan_watchdog_maxinterval > 0 &&
5830 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5831 fan_watchdog_active = 1;
5832 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5833 msecs_to_jiffies(fan_watchdog_maxinterval
5834 * 1000))) {
5835 printk(TPACPI_ERR
5836 "failed to queue the fan watchdog, "
5837 "watchdog will not trigger\n");
5839 } else
5840 fan_watchdog_active = 0;
5843 static void fan_watchdog_fire(struct work_struct *ignored)
5845 int rc;
5847 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5848 return;
5850 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5851 rc = fan_set_enable();
5852 if (rc < 0) {
5853 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5854 "will try again later...\n", -rc);
5855 /* reschedule for later */
5856 fan_watchdog_reset();
5861 * SYSFS fan layout: hwmon compatible (device)
5863 * pwm*_enable:
5864 * 0: "disengaged" mode
5865 * 1: manual mode
5866 * 2: native EC "auto" mode (recommended, hardware default)
5868 * pwm*: set speed in manual mode, ignored otherwise.
5869 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5870 * interpolation.
5872 * fan*_input: tachometer reading, RPM
5875 * SYSFS fan layout: extensions
5877 * fan_watchdog (driver):
5878 * fan watchdog interval in seconds, 0 disables (default), max 120
5881 /* sysfs fan pwm1_enable ----------------------------------------------- */
5882 static ssize_t fan_pwm1_enable_show(struct device *dev,
5883 struct device_attribute *attr,
5884 char *buf)
5886 int res, mode;
5887 u8 status;
5889 res = fan_get_status_safe(&status);
5890 if (res)
5891 return res;
5893 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5894 if (status != fan_control_initial_status) {
5895 tp_features.fan_ctrl_status_undef = 0;
5896 } else {
5897 /* Return most likely status. In fact, it
5898 * might be the only possible status */
5899 status = TP_EC_FAN_AUTO;
5903 if (status & TP_EC_FAN_FULLSPEED) {
5904 mode = 0;
5905 } else if (status & TP_EC_FAN_AUTO) {
5906 mode = 2;
5907 } else
5908 mode = 1;
5910 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5913 static ssize_t fan_pwm1_enable_store(struct device *dev,
5914 struct device_attribute *attr,
5915 const char *buf, size_t count)
5917 unsigned long t;
5918 int res, level;
5920 if (parse_strtoul(buf, 2, &t))
5921 return -EINVAL;
5923 switch (t) {
5924 case 0:
5925 level = TP_EC_FAN_FULLSPEED;
5926 break;
5927 case 1:
5928 level = TPACPI_FAN_LAST_LEVEL;
5929 break;
5930 case 2:
5931 level = TP_EC_FAN_AUTO;
5932 break;
5933 case 3:
5934 /* reserved for software-controlled auto mode */
5935 return -ENOSYS;
5936 default:
5937 return -EINVAL;
5940 res = fan_set_level_safe(level);
5941 if (res == -ENXIO)
5942 return -EINVAL;
5943 else if (res < 0)
5944 return res;
5946 fan_watchdog_reset();
5948 return count;
5951 static struct device_attribute dev_attr_fan_pwm1_enable =
5952 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5953 fan_pwm1_enable_show, fan_pwm1_enable_store);
5955 /* sysfs fan pwm1 ------------------------------------------------------ */
5956 static ssize_t fan_pwm1_show(struct device *dev,
5957 struct device_attribute *attr,
5958 char *buf)
5960 int res;
5961 u8 status;
5963 res = fan_get_status_safe(&status);
5964 if (res)
5965 return res;
5967 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5968 if (status != fan_control_initial_status) {
5969 tp_features.fan_ctrl_status_undef = 0;
5970 } else {
5971 status = TP_EC_FAN_AUTO;
5975 if ((status &
5976 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5977 status = fan_control_desired_level;
5979 if (status > 7)
5980 status = 7;
5982 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5985 static ssize_t fan_pwm1_store(struct device *dev,
5986 struct device_attribute *attr,
5987 const char *buf, size_t count)
5989 unsigned long s;
5990 int rc;
5991 u8 status, newlevel;
5993 if (parse_strtoul(buf, 255, &s))
5994 return -EINVAL;
5996 /* scale down from 0-255 to 0-7 */
5997 newlevel = (s >> 5) & 0x07;
5999 if (mutex_lock_interruptible(&fan_mutex))
6000 return -ERESTARTSYS;
6002 rc = fan_get_status(&status);
6003 if (!rc && (status &
6004 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6005 rc = fan_set_level(newlevel);
6006 if (rc == -ENXIO)
6007 rc = -EINVAL;
6008 else if (!rc) {
6009 fan_update_desired_level(newlevel);
6010 fan_watchdog_reset();
6014 mutex_unlock(&fan_mutex);
6015 return (rc)? rc : count;
6018 static struct device_attribute dev_attr_fan_pwm1 =
6019 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6020 fan_pwm1_show, fan_pwm1_store);
6022 /* sysfs fan fan1_input ------------------------------------------------ */
6023 static ssize_t fan_fan1_input_show(struct device *dev,
6024 struct device_attribute *attr,
6025 char *buf)
6027 int res;
6028 unsigned int speed;
6030 res = fan_get_speed(&speed);
6031 if (res < 0)
6032 return res;
6034 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6037 static struct device_attribute dev_attr_fan_fan1_input =
6038 __ATTR(fan1_input, S_IRUGO,
6039 fan_fan1_input_show, NULL);
6041 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6042 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6043 char *buf)
6045 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6048 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6049 const char *buf, size_t count)
6051 unsigned long t;
6053 if (parse_strtoul(buf, 120, &t))
6054 return -EINVAL;
6056 if (!fan_control_allowed)
6057 return -EPERM;
6059 fan_watchdog_maxinterval = t;
6060 fan_watchdog_reset();
6062 return count;
6065 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6066 fan_fan_watchdog_show, fan_fan_watchdog_store);
6068 /* --------------------------------------------------------------------- */
6069 static struct attribute *fan_attributes[] = {
6070 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6071 &dev_attr_fan_fan1_input.attr,
6072 NULL
6075 static const struct attribute_group fan_attr_group = {
6076 .attrs = fan_attributes,
6079 static int __init fan_init(struct ibm_init_struct *iibm)
6081 int rc;
6083 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6085 mutex_init(&fan_mutex);
6086 fan_status_access_mode = TPACPI_FAN_NONE;
6087 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6088 fan_control_commands = 0;
6089 fan_watchdog_maxinterval = 0;
6090 tp_features.fan_ctrl_status_undef = 0;
6091 fan_control_desired_level = 7;
6093 TPACPI_ACPIHANDLE_INIT(fans);
6094 TPACPI_ACPIHANDLE_INIT(gfan);
6095 TPACPI_ACPIHANDLE_INIT(sfan);
6097 if (gfan_handle) {
6098 /* 570, 600e/x, 770e, 770x */
6099 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6100 } else {
6101 /* all other ThinkPads: note that even old-style
6102 * ThinkPad ECs supports the fan control register */
6103 if (likely(acpi_ec_read(fan_status_offset,
6104 &fan_control_initial_status))) {
6105 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6107 /* In some ThinkPads, neither the EC nor the ACPI
6108 * DSDT initialize the fan status, and it ends up
6109 * being set to 0x07 when it *could* be either
6110 * 0x07 or 0x80.
6112 * Enable for TP-1Y (T43), TP-78 (R51e),
6113 * TP-76 (R52), TP-70 (T43, R52), which are known
6114 * to be buggy. */
6115 if (fan_control_initial_status == 0x07) {
6116 switch (thinkpad_id.ec_model) {
6117 case 0x5931: /* TP-1Y */
6118 case 0x3837: /* TP-78 */
6119 case 0x3637: /* TP-76 */
6120 case 0x3037: /* TP-70 */
6121 printk(TPACPI_NOTICE
6122 "fan_init: initial fan status "
6123 "is unknown, assuming it is "
6124 "in auto mode\n");
6125 tp_features.fan_ctrl_status_undef = 1;
6129 } else {
6130 printk(TPACPI_ERR
6131 "ThinkPad ACPI EC access misbehaving, "
6132 "fan status and control unavailable\n");
6133 return 1;
6137 if (sfan_handle) {
6138 /* 570, 770x-JL */
6139 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6140 fan_control_commands |=
6141 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6142 } else {
6143 if (!gfan_handle) {
6144 /* gfan without sfan means no fan control */
6145 /* all other models implement TP EC 0x2f control */
6147 if (fans_handle) {
6148 /* X31, X40, X41 */
6149 fan_control_access_mode =
6150 TPACPI_FAN_WR_ACPI_FANS;
6151 fan_control_commands |=
6152 TPACPI_FAN_CMD_SPEED |
6153 TPACPI_FAN_CMD_LEVEL |
6154 TPACPI_FAN_CMD_ENABLE;
6155 } else {
6156 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6157 fan_control_commands |=
6158 TPACPI_FAN_CMD_LEVEL |
6159 TPACPI_FAN_CMD_ENABLE;
6164 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6165 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6166 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6167 fan_status_access_mode, fan_control_access_mode);
6169 /* fan control master switch */
6170 if (!fan_control_allowed) {
6171 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6172 fan_control_commands = 0;
6173 dbg_printk(TPACPI_DBG_INIT,
6174 "fan control features disabled by parameter\n");
6177 /* update fan_control_desired_level */
6178 if (fan_status_access_mode != TPACPI_FAN_NONE)
6179 fan_get_status_safe(NULL);
6181 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6182 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6183 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6184 &fan_attr_group);
6185 if (rc < 0)
6186 return rc;
6188 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6189 &driver_attr_fan_watchdog);
6190 if (rc < 0) {
6191 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6192 &fan_attr_group);
6193 return rc;
6195 return 0;
6196 } else
6197 return 1;
6200 static void fan_exit(void)
6202 vdbg_printk(TPACPI_DBG_EXIT,
6203 "cancelling any pending fan watchdog tasks\n");
6205 /* FIXME: can we really do this unconditionally? */
6206 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6207 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6208 &driver_attr_fan_watchdog);
6210 cancel_delayed_work(&fan_watchdog_task);
6211 flush_workqueue(tpacpi_wq);
6214 static void fan_suspend(pm_message_t state)
6216 int rc;
6218 if (!fan_control_allowed)
6219 return;
6221 /* Store fan status in cache */
6222 fan_control_resume_level = 0;
6223 rc = fan_get_status_safe(&fan_control_resume_level);
6224 if (rc < 0)
6225 printk(TPACPI_NOTICE
6226 "failed to read fan level for later "
6227 "restore during resume: %d\n", rc);
6229 /* if it is undefined, don't attempt to restore it.
6230 * KEEP THIS LAST */
6231 if (tp_features.fan_ctrl_status_undef)
6232 fan_control_resume_level = 0;
6235 static void fan_resume(void)
6237 u8 current_level = 7;
6238 bool do_set = false;
6239 int rc;
6241 /* DSDT *always* updates status on resume */
6242 tp_features.fan_ctrl_status_undef = 0;
6244 if (!fan_control_allowed ||
6245 !fan_control_resume_level ||
6246 (fan_get_status_safe(&current_level) < 0))
6247 return;
6249 switch (fan_control_access_mode) {
6250 case TPACPI_FAN_WR_ACPI_SFAN:
6251 /* never decrease fan level */
6252 do_set = (fan_control_resume_level > current_level);
6253 break;
6254 case TPACPI_FAN_WR_ACPI_FANS:
6255 case TPACPI_FAN_WR_TPEC:
6256 /* never decrease fan level, scale is:
6257 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6259 * We expect the firmware to set either 7 or AUTO, but we
6260 * handle FULLSPEED out of paranoia.
6262 * So, we can safely only restore FULLSPEED or 7, anything
6263 * else could slow the fan. Restoring AUTO is useless, at
6264 * best that's exactly what the DSDT already set (it is the
6265 * slower it uses).
6267 * Always keep in mind that the DSDT *will* have set the
6268 * fans to what the vendor supposes is the best level. We
6269 * muck with it only to speed the fan up.
6271 if (fan_control_resume_level != 7 &&
6272 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6273 return;
6274 else
6275 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6276 (current_level != fan_control_resume_level);
6277 break;
6278 default:
6279 return;
6281 if (do_set) {
6282 printk(TPACPI_NOTICE
6283 "restoring fan level to 0x%02x\n",
6284 fan_control_resume_level);
6285 rc = fan_set_level_safe(fan_control_resume_level);
6286 if (rc < 0)
6287 printk(TPACPI_NOTICE
6288 "failed to restore fan level: %d\n", rc);
6292 static int fan_read(char *p)
6294 int len = 0;
6295 int rc;
6296 u8 status;
6297 unsigned int speed = 0;
6299 switch (fan_status_access_mode) {
6300 case TPACPI_FAN_RD_ACPI_GFAN:
6301 /* 570, 600e/x, 770e, 770x */
6302 rc = fan_get_status_safe(&status);
6303 if (rc < 0)
6304 return rc;
6306 len += sprintf(p + len, "status:\t\t%s\n"
6307 "level:\t\t%d\n",
6308 (status != 0) ? "enabled" : "disabled", status);
6309 break;
6311 case TPACPI_FAN_RD_TPEC:
6312 /* all except 570, 600e/x, 770e, 770x */
6313 rc = fan_get_status_safe(&status);
6314 if (rc < 0)
6315 return rc;
6317 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6318 if (status != fan_control_initial_status)
6319 tp_features.fan_ctrl_status_undef = 0;
6320 else
6321 /* Return most likely status. In fact, it
6322 * might be the only possible status */
6323 status = TP_EC_FAN_AUTO;
6326 len += sprintf(p + len, "status:\t\t%s\n",
6327 (status != 0) ? "enabled" : "disabled");
6329 rc = fan_get_speed(&speed);
6330 if (rc < 0)
6331 return rc;
6333 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6335 if (status & TP_EC_FAN_FULLSPEED)
6336 /* Disengaged mode takes precedence */
6337 len += sprintf(p + len, "level:\t\tdisengaged\n");
6338 else if (status & TP_EC_FAN_AUTO)
6339 len += sprintf(p + len, "level:\t\tauto\n");
6340 else
6341 len += sprintf(p + len, "level:\t\t%d\n", status);
6342 break;
6344 case TPACPI_FAN_NONE:
6345 default:
6346 len += sprintf(p + len, "status:\t\tnot supported\n");
6349 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6350 len += sprintf(p + len, "commands:\tlevel <level>");
6352 switch (fan_control_access_mode) {
6353 case TPACPI_FAN_WR_ACPI_SFAN:
6354 len += sprintf(p + len, " (<level> is 0-7)\n");
6355 break;
6357 default:
6358 len += sprintf(p + len, " (<level> is 0-7, "
6359 "auto, disengaged, full-speed)\n");
6360 break;
6364 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6365 len += sprintf(p + len, "commands:\tenable, disable\n"
6366 "commands:\twatchdog <timeout> (<timeout> "
6367 "is 0 (off), 1-120 (seconds))\n");
6369 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6370 len += sprintf(p + len, "commands:\tspeed <speed>"
6371 " (<speed> is 0-65535)\n");
6373 return len;
6376 static int fan_write_cmd_level(const char *cmd, int *rc)
6378 int level;
6380 if (strlencmp(cmd, "level auto") == 0)
6381 level = TP_EC_FAN_AUTO;
6382 else if ((strlencmp(cmd, "level disengaged") == 0) |
6383 (strlencmp(cmd, "level full-speed") == 0))
6384 level = TP_EC_FAN_FULLSPEED;
6385 else if (sscanf(cmd, "level %d", &level) != 1)
6386 return 0;
6388 *rc = fan_set_level_safe(level);
6389 if (*rc == -ENXIO)
6390 printk(TPACPI_ERR "level command accepted for unsupported "
6391 "access mode %d", fan_control_access_mode);
6393 return 1;
6396 static int fan_write_cmd_enable(const char *cmd, int *rc)
6398 if (strlencmp(cmd, "enable") != 0)
6399 return 0;
6401 *rc = fan_set_enable();
6402 if (*rc == -ENXIO)
6403 printk(TPACPI_ERR "enable command accepted for unsupported "
6404 "access mode %d", fan_control_access_mode);
6406 return 1;
6409 static int fan_write_cmd_disable(const char *cmd, int *rc)
6411 if (strlencmp(cmd, "disable") != 0)
6412 return 0;
6414 *rc = fan_set_disable();
6415 if (*rc == -ENXIO)
6416 printk(TPACPI_ERR "disable command accepted for unsupported "
6417 "access mode %d", fan_control_access_mode);
6419 return 1;
6422 static int fan_write_cmd_speed(const char *cmd, int *rc)
6424 int speed;
6426 /* TODO:
6427 * Support speed <low> <medium> <high> ? */
6429 if (sscanf(cmd, "speed %d", &speed) != 1)
6430 return 0;
6432 *rc = fan_set_speed(speed);
6433 if (*rc == -ENXIO)
6434 printk(TPACPI_ERR "speed command accepted for unsupported "
6435 "access mode %d", fan_control_access_mode);
6437 return 1;
6440 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6442 int interval;
6444 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6445 return 0;
6447 if (interval < 0 || interval > 120)
6448 *rc = -EINVAL;
6449 else
6450 fan_watchdog_maxinterval = interval;
6452 return 1;
6455 static int fan_write(char *buf)
6457 char *cmd;
6458 int rc = 0;
6460 while (!rc && (cmd = next_cmd(&buf))) {
6461 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6462 fan_write_cmd_level(cmd, &rc)) &&
6463 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6464 (fan_write_cmd_enable(cmd, &rc) ||
6465 fan_write_cmd_disable(cmd, &rc) ||
6466 fan_write_cmd_watchdog(cmd, &rc))) &&
6467 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6468 fan_write_cmd_speed(cmd, &rc))
6470 rc = -EINVAL;
6471 else if (!rc)
6472 fan_watchdog_reset();
6475 return rc;
6478 static struct ibm_struct fan_driver_data = {
6479 .name = "fan",
6480 .read = fan_read,
6481 .write = fan_write,
6482 .exit = fan_exit,
6483 .suspend = fan_suspend,
6484 .resume = fan_resume,
6487 /****************************************************************************
6488 ****************************************************************************
6490 * Infrastructure
6492 ****************************************************************************
6493 ****************************************************************************/
6495 /* sysfs name ---------------------------------------------------------- */
6496 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6497 struct device_attribute *attr,
6498 char *buf)
6500 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6503 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6504 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6506 /* --------------------------------------------------------------------- */
6508 /* /proc support */
6509 static struct proc_dir_entry *proc_dir;
6512 * Module and infrastructure proble, init and exit handling
6515 static int force_load;
6517 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6518 static const char * __init str_supported(int is_supported)
6520 static char text_unsupported[] __initdata = "not supported";
6522 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6524 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6526 static void ibm_exit(struct ibm_struct *ibm)
6528 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6530 list_del_init(&ibm->all_drivers);
6532 if (ibm->flags.acpi_notify_installed) {
6533 dbg_printk(TPACPI_DBG_EXIT,
6534 "%s: acpi_remove_notify_handler\n", ibm->name);
6535 BUG_ON(!ibm->acpi);
6536 acpi_remove_notify_handler(*ibm->acpi->handle,
6537 ibm->acpi->type,
6538 dispatch_acpi_notify);
6539 ibm->flags.acpi_notify_installed = 0;
6540 ibm->flags.acpi_notify_installed = 0;
6543 if (ibm->flags.proc_created) {
6544 dbg_printk(TPACPI_DBG_EXIT,
6545 "%s: remove_proc_entry\n", ibm->name);
6546 remove_proc_entry(ibm->name, proc_dir);
6547 ibm->flags.proc_created = 0;
6550 if (ibm->flags.acpi_driver_registered) {
6551 dbg_printk(TPACPI_DBG_EXIT,
6552 "%s: acpi_bus_unregister_driver\n", ibm->name);
6553 BUG_ON(!ibm->acpi);
6554 acpi_bus_unregister_driver(ibm->acpi->driver);
6555 kfree(ibm->acpi->driver);
6556 ibm->acpi->driver = NULL;
6557 ibm->flags.acpi_driver_registered = 0;
6560 if (ibm->flags.init_called && ibm->exit) {
6561 ibm->exit();
6562 ibm->flags.init_called = 0;
6565 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6568 static int __init ibm_init(struct ibm_init_struct *iibm)
6570 int ret;
6571 struct ibm_struct *ibm = iibm->data;
6572 struct proc_dir_entry *entry;
6574 BUG_ON(ibm == NULL);
6576 INIT_LIST_HEAD(&ibm->all_drivers);
6578 if (ibm->flags.experimental && !experimental)
6579 return 0;
6581 dbg_printk(TPACPI_DBG_INIT,
6582 "probing for %s\n", ibm->name);
6584 if (iibm->init) {
6585 ret = iibm->init(iibm);
6586 if (ret > 0)
6587 return 0; /* probe failed */
6588 if (ret)
6589 return ret;
6591 ibm->flags.init_called = 1;
6594 if (ibm->acpi) {
6595 if (ibm->acpi->hid) {
6596 ret = register_tpacpi_subdriver(ibm);
6597 if (ret)
6598 goto err_out;
6601 if (ibm->acpi->notify) {
6602 ret = setup_acpi_notify(ibm);
6603 if (ret == -ENODEV) {
6604 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6605 ibm->name);
6606 ret = 0;
6607 goto err_out;
6609 if (ret < 0)
6610 goto err_out;
6614 dbg_printk(TPACPI_DBG_INIT,
6615 "%s installed\n", ibm->name);
6617 if (ibm->read) {
6618 entry = create_proc_entry(ibm->name,
6619 S_IFREG | S_IRUGO | S_IWUSR,
6620 proc_dir);
6621 if (!entry) {
6622 printk(TPACPI_ERR "unable to create proc entry %s\n",
6623 ibm->name);
6624 ret = -ENODEV;
6625 goto err_out;
6627 entry->owner = THIS_MODULE;
6628 entry->data = ibm;
6629 entry->read_proc = &dispatch_procfs_read;
6630 if (ibm->write)
6631 entry->write_proc = &dispatch_procfs_write;
6632 ibm->flags.proc_created = 1;
6635 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6637 return 0;
6639 err_out:
6640 dbg_printk(TPACPI_DBG_INIT,
6641 "%s: at error exit path with result %d\n",
6642 ibm->name, ret);
6644 ibm_exit(ibm);
6645 return (ret < 0)? ret : 0;
6648 /* Probing */
6650 /* returns 0 - probe ok, or < 0 - probe error.
6651 * Probe ok doesn't mean thinkpad found.
6652 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6653 static int __must_check __init get_thinkpad_model_data(
6654 struct thinkpad_id_data *tp)
6656 const struct dmi_device *dev = NULL;
6657 char ec_fw_string[18];
6658 char const *s;
6660 if (!tp)
6661 return -EINVAL;
6663 memset(tp, 0, sizeof(*tp));
6665 if (dmi_name_in_vendors("IBM"))
6666 tp->vendor = PCI_VENDOR_ID_IBM;
6667 else if (dmi_name_in_vendors("LENOVO"))
6668 tp->vendor = PCI_VENDOR_ID_LENOVO;
6669 else
6670 return 0;
6672 s = dmi_get_system_info(DMI_BIOS_VERSION);
6673 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6674 if (s && !tp->bios_version_str)
6675 return -ENOMEM;
6676 if (!tp->bios_version_str)
6677 return 0;
6678 tp->bios_model = tp->bios_version_str[0]
6679 | (tp->bios_version_str[1] << 8);
6682 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6683 * X32 or newer, all Z series; Some models must have an
6684 * up-to-date BIOS or they will not be detected.
6686 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6688 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6689 if (sscanf(dev->name,
6690 "IBM ThinkPad Embedded Controller -[%17c",
6691 ec_fw_string) == 1) {
6692 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6693 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6695 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6696 if (!tp->ec_version_str)
6697 return -ENOMEM;
6698 tp->ec_model = ec_fw_string[0]
6699 | (ec_fw_string[1] << 8);
6700 break;
6704 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6705 if (s && !strnicmp(s, "ThinkPad", 8)) {
6706 tp->model_str = kstrdup(s, GFP_KERNEL);
6707 if (!tp->model_str)
6708 return -ENOMEM;
6711 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6712 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6713 if (s && !tp->nummodel_str)
6714 return -ENOMEM;
6716 return 0;
6719 static int __init probe_for_thinkpad(void)
6721 int is_thinkpad;
6723 if (acpi_disabled)
6724 return -ENODEV;
6727 * Non-ancient models have better DMI tagging, but very old models
6728 * don't.
6730 is_thinkpad = (thinkpad_id.model_str != NULL);
6732 /* ec is required because many other handles are relative to it */
6733 TPACPI_ACPIHANDLE_INIT(ec);
6734 if (!ec_handle) {
6735 if (is_thinkpad)
6736 printk(TPACPI_ERR
6737 "Not yet supported ThinkPad detected!\n");
6738 return -ENODEV;
6742 * Risks a regression on very old machines, but reduces potential
6743 * false positives a damn great deal
6745 if (!is_thinkpad)
6746 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6748 if (!is_thinkpad && !force_load)
6749 return -ENODEV;
6751 return 0;
6755 /* Module init, exit, parameters */
6757 static struct ibm_init_struct ibms_init[] __initdata = {
6759 .init = thinkpad_acpi_driver_init,
6760 .data = &thinkpad_acpi_driver_data,
6763 .init = hotkey_init,
6764 .data = &hotkey_driver_data,
6767 .init = bluetooth_init,
6768 .data = &bluetooth_driver_data,
6771 .init = wan_init,
6772 .data = &wan_driver_data,
6774 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6776 .init = video_init,
6777 .data = &video_driver_data,
6779 #endif
6781 .init = light_init,
6782 .data = &light_driver_data,
6784 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6786 .init = dock_init,
6787 .data = &dock_driver_data[0],
6790 .init = dock_init2,
6791 .data = &dock_driver_data[1],
6793 #endif
6794 #ifdef CONFIG_THINKPAD_ACPI_BAY
6796 .init = bay_init,
6797 .data = &bay_driver_data,
6799 #endif
6801 .init = cmos_init,
6802 .data = &cmos_driver_data,
6805 .init = led_init,
6806 .data = &led_driver_data,
6809 .init = beep_init,
6810 .data = &beep_driver_data,
6813 .init = thermal_init,
6814 .data = &thermal_driver_data,
6817 .data = &ecdump_driver_data,
6820 .init = brightness_init,
6821 .data = &brightness_driver_data,
6824 .data = &volume_driver_data,
6827 .init = fan_init,
6828 .data = &fan_driver_data,
6832 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6834 unsigned int i;
6835 struct ibm_struct *ibm;
6837 if (!kp || !kp->name || !val)
6838 return -EINVAL;
6840 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6841 ibm = ibms_init[i].data;
6842 WARN_ON(ibm == NULL);
6844 if (!ibm || !ibm->name)
6845 continue;
6847 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6848 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6849 return -ENOSPC;
6850 strcpy(ibms_init[i].param, val);
6851 strcat(ibms_init[i].param, ",");
6852 return 0;
6856 return -EINVAL;
6859 module_param(experimental, int, 0);
6860 MODULE_PARM_DESC(experimental,
6861 "Enables experimental features when non-zero");
6863 module_param_named(debug, dbg_level, uint, 0);
6864 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6866 module_param(force_load, bool, 0);
6867 MODULE_PARM_DESC(force_load,
6868 "Attempts to load the driver even on a "
6869 "mis-identified ThinkPad when true");
6871 module_param_named(fan_control, fan_control_allowed, bool, 0);
6872 MODULE_PARM_DESC(fan_control,
6873 "Enables setting fan parameters features when true");
6875 module_param_named(brightness_mode, brightness_mode, int, 0);
6876 MODULE_PARM_DESC(brightness_mode,
6877 "Selects brightness control strategy: "
6878 "0=auto, 1=EC, 2=CMOS, 3=both");
6880 module_param(brightness_enable, uint, 0);
6881 MODULE_PARM_DESC(brightness_enable,
6882 "Enables backlight control when 1, disables when 0");
6884 module_param(hotkey_report_mode, uint, 0);
6885 MODULE_PARM_DESC(hotkey_report_mode,
6886 "used for backwards compatibility with userspace, "
6887 "see documentation");
6889 #define TPACPI_PARAM(feature) \
6890 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6891 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6892 "at module load, see documentation")
6894 TPACPI_PARAM(hotkey);
6895 TPACPI_PARAM(bluetooth);
6896 TPACPI_PARAM(video);
6897 TPACPI_PARAM(light);
6898 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6899 TPACPI_PARAM(dock);
6900 #endif
6901 #ifdef CONFIG_THINKPAD_ACPI_BAY
6902 TPACPI_PARAM(bay);
6903 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6904 TPACPI_PARAM(cmos);
6905 TPACPI_PARAM(led);
6906 TPACPI_PARAM(beep);
6907 TPACPI_PARAM(ecdump);
6908 TPACPI_PARAM(brightness);
6909 TPACPI_PARAM(volume);
6910 TPACPI_PARAM(fan);
6912 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
6913 module_param(dbg_wlswemul, uint, 0);
6914 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
6915 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
6916 MODULE_PARM_DESC(wlsw_state,
6917 "Initial state of the emulated WLSW switch");
6919 module_param(dbg_bluetoothemul, uint, 0);
6920 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
6921 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
6922 MODULE_PARM_DESC(bluetooth_state,
6923 "Initial state of the emulated bluetooth switch");
6925 module_param(dbg_wwanemul, uint, 0);
6926 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
6927 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
6928 MODULE_PARM_DESC(wwan_state,
6929 "Initial state of the emulated WWAN switch");
6930 #endif
6932 static void thinkpad_acpi_module_exit(void)
6934 struct ibm_struct *ibm, *itmp;
6936 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6938 list_for_each_entry_safe_reverse(ibm, itmp,
6939 &tpacpi_all_drivers,
6940 all_drivers) {
6941 ibm_exit(ibm);
6944 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6946 if (tpacpi_inputdev) {
6947 if (tp_features.input_device_registered)
6948 input_unregister_device(tpacpi_inputdev);
6949 else
6950 input_free_device(tpacpi_inputdev);
6953 if (tpacpi_hwmon)
6954 hwmon_device_unregister(tpacpi_hwmon);
6956 if (tp_features.sensors_pdev_attrs_registered)
6957 device_remove_file(&tpacpi_sensors_pdev->dev,
6958 &dev_attr_thinkpad_acpi_pdev_name);
6959 if (tpacpi_sensors_pdev)
6960 platform_device_unregister(tpacpi_sensors_pdev);
6961 if (tpacpi_pdev)
6962 platform_device_unregister(tpacpi_pdev);
6964 if (tp_features.sensors_pdrv_attrs_registered)
6965 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6966 if (tp_features.platform_drv_attrs_registered)
6967 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6969 if (tp_features.sensors_pdrv_registered)
6970 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6972 if (tp_features.platform_drv_registered)
6973 platform_driver_unregister(&tpacpi_pdriver);
6975 if (proc_dir)
6976 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6978 if (tpacpi_wq)
6979 destroy_workqueue(tpacpi_wq);
6981 kfree(thinkpad_id.bios_version_str);
6982 kfree(thinkpad_id.ec_version_str);
6983 kfree(thinkpad_id.model_str);
6987 static int __init thinkpad_acpi_module_init(void)
6989 int ret, i;
6991 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6993 /* Parameter checking */
6994 if (hotkey_report_mode > 2)
6995 return -EINVAL;
6997 /* Driver-level probe */
6999 ret = get_thinkpad_model_data(&thinkpad_id);
7000 if (ret) {
7001 printk(TPACPI_ERR
7002 "unable to get DMI data: %d\n", ret);
7003 thinkpad_acpi_module_exit();
7004 return ret;
7006 ret = probe_for_thinkpad();
7007 if (ret) {
7008 thinkpad_acpi_module_exit();
7009 return ret;
7012 /* Driver initialization */
7014 TPACPI_ACPIHANDLE_INIT(ecrd);
7015 TPACPI_ACPIHANDLE_INIT(ecwr);
7017 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7018 if (!tpacpi_wq) {
7019 thinkpad_acpi_module_exit();
7020 return -ENOMEM;
7023 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7024 if (!proc_dir) {
7025 printk(TPACPI_ERR
7026 "unable to create proc dir " TPACPI_PROC_DIR);
7027 thinkpad_acpi_module_exit();
7028 return -ENODEV;
7030 proc_dir->owner = THIS_MODULE;
7032 ret = platform_driver_register(&tpacpi_pdriver);
7033 if (ret) {
7034 printk(TPACPI_ERR
7035 "unable to register main platform driver\n");
7036 thinkpad_acpi_module_exit();
7037 return ret;
7039 tp_features.platform_drv_registered = 1;
7041 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7042 if (ret) {
7043 printk(TPACPI_ERR
7044 "unable to register hwmon platform driver\n");
7045 thinkpad_acpi_module_exit();
7046 return ret;
7048 tp_features.sensors_pdrv_registered = 1;
7050 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7051 if (!ret) {
7052 tp_features.platform_drv_attrs_registered = 1;
7053 ret = tpacpi_create_driver_attributes(
7054 &tpacpi_hwmon_pdriver.driver);
7056 if (ret) {
7057 printk(TPACPI_ERR
7058 "unable to create sysfs driver attributes\n");
7059 thinkpad_acpi_module_exit();
7060 return ret;
7062 tp_features.sensors_pdrv_attrs_registered = 1;
7065 /* Device initialization */
7066 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7067 NULL, 0);
7068 if (IS_ERR(tpacpi_pdev)) {
7069 ret = PTR_ERR(tpacpi_pdev);
7070 tpacpi_pdev = NULL;
7071 printk(TPACPI_ERR "unable to register platform device\n");
7072 thinkpad_acpi_module_exit();
7073 return ret;
7075 tpacpi_sensors_pdev = platform_device_register_simple(
7076 TPACPI_HWMON_DRVR_NAME,
7077 -1, NULL, 0);
7078 if (IS_ERR(tpacpi_sensors_pdev)) {
7079 ret = PTR_ERR(tpacpi_sensors_pdev);
7080 tpacpi_sensors_pdev = NULL;
7081 printk(TPACPI_ERR
7082 "unable to register hwmon platform device\n");
7083 thinkpad_acpi_module_exit();
7084 return ret;
7086 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7087 &dev_attr_thinkpad_acpi_pdev_name);
7088 if (ret) {
7089 printk(TPACPI_ERR
7090 "unable to create sysfs hwmon device attributes\n");
7091 thinkpad_acpi_module_exit();
7092 return ret;
7094 tp_features.sensors_pdev_attrs_registered = 1;
7095 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7096 if (IS_ERR(tpacpi_hwmon)) {
7097 ret = PTR_ERR(tpacpi_hwmon);
7098 tpacpi_hwmon = NULL;
7099 printk(TPACPI_ERR "unable to register hwmon device\n");
7100 thinkpad_acpi_module_exit();
7101 return ret;
7103 mutex_init(&tpacpi_inputdev_send_mutex);
7104 tpacpi_inputdev = input_allocate_device();
7105 if (!tpacpi_inputdev) {
7106 printk(TPACPI_ERR "unable to allocate input device\n");
7107 thinkpad_acpi_module_exit();
7108 return -ENOMEM;
7109 } else {
7110 /* Prepare input device, but don't register */
7111 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7112 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7113 tpacpi_inputdev->id.bustype = BUS_HOST;
7114 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7115 thinkpad_id.vendor :
7116 PCI_VENDOR_ID_IBM;
7117 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7118 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7120 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7121 ret = ibm_init(&ibms_init[i]);
7122 if (ret >= 0 && *ibms_init[i].param)
7123 ret = ibms_init[i].data->write(ibms_init[i].param);
7124 if (ret < 0) {
7125 thinkpad_acpi_module_exit();
7126 return ret;
7129 ret = input_register_device(tpacpi_inputdev);
7130 if (ret < 0) {
7131 printk(TPACPI_ERR "unable to register input device\n");
7132 thinkpad_acpi_module_exit();
7133 return ret;
7134 } else {
7135 tp_features.input_device_registered = 1;
7138 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7139 return 0;
7142 /* Please remove this in year 2009 */
7143 MODULE_ALIAS("ibm_acpi");
7145 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7148 * DMI matching for module autoloading
7150 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7151 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7153 * Only models listed in thinkwiki will be supported, so add yours
7154 * if it is not there yet.
7156 #define IBM_BIOS_MODULE_ALIAS(__type) \
7157 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7159 /* Non-ancient thinkpads */
7160 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7161 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7163 /* Ancient thinkpad BIOSes have to be identified by
7164 * BIOS type or model number, and there are far less
7165 * BIOS types than model numbers... */
7166 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7167 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7168 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7170 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7171 MODULE_DESCRIPTION(TPACPI_DESC);
7172 MODULE_VERSION(TPACPI_VERSION);
7173 MODULE_LICENSE("GPL");
7175 module_init(thinkpad_acpi_module_init);
7176 module_exit(thinkpad_acpi_module_exit);