thinkpad-acpi: fix sign of ERESTARTSYS return
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobd3ade96bca638eaf819cf1aa9e7a68b5838ed78d
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
127 /****************************************************************************
128 * Main driver
131 #define TPACPI_NAME "thinkpad"
132 #define TPACPI_DESC "ThinkPad ACPI Extras"
133 #define TPACPI_FILE TPACPI_NAME "_acpi"
134 #define TPACPI_URL "http://ibm-acpi.sf.net/"
135 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
137 #define TPACPI_PROC_DIR "ibm"
138 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
139 #define TPACPI_DRVR_NAME TPACPI_FILE
140 #define TPACPI_DRVR_SHORTNAME "tpacpi"
141 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
143 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
146 #define TPACPI_MAX_ACPI_ARGS 3
148 /* rfkill switches */
149 enum {
150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
151 TPACPI_RFK_WWAN_SW_ID,
154 /* Debugging */
155 #define TPACPI_LOG TPACPI_FILE ": "
156 #define TPACPI_ERR KERN_ERR TPACPI_LOG
157 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
158 #define TPACPI_INFO KERN_INFO TPACPI_LOG
159 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
161 #define TPACPI_DBG_ALL 0xffff
162 #define TPACPI_DBG_ALL 0xffff
163 #define TPACPI_DBG_INIT 0x0001
164 #define TPACPI_DBG_EXIT 0x0002
165 #define dbg_printk(a_dbg_level, format, arg...) \
166 do { if (dbg_level & a_dbg_level) \
167 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
168 } while (0)
169 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
170 #define vdbg_printk(a_dbg_level, format, arg...) \
171 dbg_printk(a_dbg_level, format, ## arg)
172 static const char *str_supported(int is_supported);
173 #else
174 #define vdbg_printk(a_dbg_level, format, arg...)
175 #endif
177 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
178 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
179 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
182 /****************************************************************************
183 * Driver-wide structs and misc. variables
186 struct ibm_struct;
188 struct tp_acpi_drv_struct {
189 const struct acpi_device_id *hid;
190 struct acpi_driver *driver;
192 void (*notify) (struct ibm_struct *, u32);
193 acpi_handle *handle;
194 u32 type;
195 struct acpi_device *device;
198 struct ibm_struct {
199 char *name;
201 int (*read) (char *);
202 int (*write) (char *);
203 void (*exit) (void);
204 void (*resume) (void);
205 void (*suspend) (pm_message_t state);
207 struct list_head all_drivers;
209 struct tp_acpi_drv_struct *acpi;
211 struct {
212 u8 acpi_driver_registered:1;
213 u8 acpi_notify_installed:1;
214 u8 proc_created:1;
215 u8 init_called:1;
216 u8 experimental:1;
217 } flags;
220 struct ibm_init_struct {
221 char param[32];
223 int (*init) (struct ibm_init_struct *);
224 struct ibm_struct *data;
227 static struct {
228 #ifdef CONFIG_THINKPAD_ACPI_BAY
229 u32 bay_status:1;
230 u32 bay_eject:1;
231 u32 bay_status2:1;
232 u32 bay_eject2:1;
233 #endif
234 u32 bluetooth:1;
235 u32 hotkey:1;
236 u32 hotkey_mask:1;
237 u32 hotkey_wlsw:1;
238 u32 hotkey_tablet:1;
239 u32 light:1;
240 u32 light_status:1;
241 u32 bright_16levels:1;
242 u32 bright_acpimode:1;
243 u32 wan:1;
244 u32 fan_ctrl_status_undef:1;
245 u32 input_device_registered:1;
246 u32 platform_drv_registered:1;
247 u32 platform_drv_attrs_registered:1;
248 u32 sensors_pdrv_registered:1;
249 u32 sensors_pdrv_attrs_registered:1;
250 u32 sensors_pdev_attrs_registered:1;
251 u32 hotkey_poll_active:1;
252 } tp_features;
254 static struct {
255 u16 hotkey_mask_ff:1;
256 u16 bright_cmos_ec_unsync:1;
257 } tp_warned;
259 struct thinkpad_id_data {
260 unsigned int vendor; /* ThinkPad vendor:
261 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
263 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
264 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
266 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
267 u16 ec_model;
269 char *model_str; /* ThinkPad T43 */
270 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
272 static struct thinkpad_id_data thinkpad_id;
274 static enum {
275 TPACPI_LIFE_INIT = 0,
276 TPACPI_LIFE_RUNNING,
277 TPACPI_LIFE_EXITING,
278 } tpacpi_lifecycle;
280 static int experimental;
281 static u32 dbg_level;
283 static struct workqueue_struct *tpacpi_wq;
285 enum led_status_t {
286 TPACPI_LED_OFF = 0,
287 TPACPI_LED_ON,
288 TPACPI_LED_BLINK,
291 /* Special LED class that can defer work */
292 struct tpacpi_led_classdev {
293 struct led_classdev led_classdev;
294 struct work_struct work;
295 enum led_status_t new_state;
296 unsigned int led;
299 /****************************************************************************
300 ****************************************************************************
302 * ACPI Helpers and device model
304 ****************************************************************************
305 ****************************************************************************/
307 /*************************************************************************
308 * ACPI basic handles
311 static acpi_handle root_handle;
313 #define TPACPI_HANDLE(object, parent, paths...) \
314 static acpi_handle object##_handle; \
315 static acpi_handle *object##_parent = &parent##_handle; \
316 static char *object##_path; \
317 static char *object##_paths[] = { paths }
319 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
320 "\\_SB.PCI.ISA.EC", /* 570 */
321 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
322 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
323 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
324 "\\_SB.PCI0.ICH3.EC0", /* R31 */
325 "\\_SB.PCI0.LPC.EC", /* all others */
328 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
329 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
331 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
332 /* T4x, X31, X40 */
333 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
334 "\\CMS", /* R40, R40e */
335 ); /* all others */
337 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
338 "^HKEY", /* R30, R31 */
339 "HKEY", /* all others */
340 ); /* 570 */
342 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
343 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
344 "\\_SB.PCI0.VID0", /* 770e */
345 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
346 "\\_SB.PCI0.AGP.VID", /* all others */
347 ); /* R30, R31 */
350 /*************************************************************************
351 * ACPI helpers
354 static int acpi_evalf(acpi_handle handle,
355 void *res, char *method, char *fmt, ...)
357 char *fmt0 = fmt;
358 struct acpi_object_list params;
359 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
360 struct acpi_buffer result, *resultp;
361 union acpi_object out_obj;
362 acpi_status status;
363 va_list ap;
364 char res_type;
365 int success;
366 int quiet;
368 if (!*fmt) {
369 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
370 return 0;
373 if (*fmt == 'q') {
374 quiet = 1;
375 fmt++;
376 } else
377 quiet = 0;
379 res_type = *(fmt++);
381 params.count = 0;
382 params.pointer = &in_objs[0];
384 va_start(ap, fmt);
385 while (*fmt) {
386 char c = *(fmt++);
387 switch (c) {
388 case 'd': /* int */
389 in_objs[params.count].integer.value = va_arg(ap, int);
390 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
391 break;
392 /* add more types as needed */
393 default:
394 printk(TPACPI_ERR "acpi_evalf() called "
395 "with invalid format character '%c'\n", c);
396 return 0;
399 va_end(ap);
401 if (res_type != 'v') {
402 result.length = sizeof(out_obj);
403 result.pointer = &out_obj;
404 resultp = &result;
405 } else
406 resultp = NULL;
408 status = acpi_evaluate_object(handle, method, &params, resultp);
410 switch (res_type) {
411 case 'd': /* int */
412 if (res)
413 *(int *)res = out_obj.integer.value;
414 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
415 break;
416 case 'v': /* void */
417 success = status == AE_OK;
418 break;
419 /* add more types as needed */
420 default:
421 printk(TPACPI_ERR "acpi_evalf() called "
422 "with invalid format character '%c'\n", res_type);
423 return 0;
426 if (!success && !quiet)
427 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
428 method, fmt0, status);
430 return success;
433 static int acpi_ec_read(int i, u8 *p)
435 int v;
437 if (ecrd_handle) {
438 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
439 return 0;
440 *p = v;
441 } else {
442 if (ec_read(i, p) < 0)
443 return 0;
446 return 1;
449 static int acpi_ec_write(int i, u8 v)
451 if (ecwr_handle) {
452 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
453 return 0;
454 } else {
455 if (ec_write(i, v) < 0)
456 return 0;
459 return 1;
462 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
463 static int _sta(acpi_handle handle)
465 int status;
467 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
468 status = 0;
470 return status;
472 #endif
474 static int issue_thinkpad_cmos_command(int cmos_cmd)
476 if (!cmos_handle)
477 return -ENXIO;
479 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
480 return -EIO;
482 return 0;
485 /*************************************************************************
486 * ACPI device model
489 #define TPACPI_ACPIHANDLE_INIT(object) \
490 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
491 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
493 static void drv_acpi_handle_init(char *name,
494 acpi_handle *handle, acpi_handle parent,
495 char **paths, int num_paths, char **path)
497 int i;
498 acpi_status status;
500 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
501 name);
503 for (i = 0; i < num_paths; i++) {
504 status = acpi_get_handle(parent, paths[i], handle);
505 if (ACPI_SUCCESS(status)) {
506 *path = paths[i];
507 dbg_printk(TPACPI_DBG_INIT,
508 "Found ACPI handle %s for %s\n",
509 *path, name);
510 return;
514 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
515 name);
516 *handle = NULL;
519 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
521 struct ibm_struct *ibm = data;
523 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
524 return;
526 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
527 return;
529 ibm->acpi->notify(ibm, event);
532 static int __init setup_acpi_notify(struct ibm_struct *ibm)
534 acpi_status status;
535 int rc;
537 BUG_ON(!ibm->acpi);
539 if (!*ibm->acpi->handle)
540 return 0;
542 vdbg_printk(TPACPI_DBG_INIT,
543 "setting up ACPI notify for %s\n", ibm->name);
545 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
546 if (rc < 0) {
547 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
548 ibm->name, rc);
549 return -ENODEV;
552 acpi_driver_data(ibm->acpi->device) = ibm;
553 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
554 TPACPI_ACPI_EVENT_PREFIX,
555 ibm->name);
557 status = acpi_install_notify_handler(*ibm->acpi->handle,
558 ibm->acpi->type, dispatch_acpi_notify, ibm);
559 if (ACPI_FAILURE(status)) {
560 if (status == AE_ALREADY_EXISTS) {
561 printk(TPACPI_NOTICE
562 "another device driver is already "
563 "handling %s events\n", ibm->name);
564 } else {
565 printk(TPACPI_ERR
566 "acpi_install_notify_handler(%s) failed: %d\n",
567 ibm->name, status);
569 return -ENODEV;
571 ibm->flags.acpi_notify_installed = 1;
572 return 0;
575 static int __init tpacpi_device_add(struct acpi_device *device)
577 return 0;
580 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
582 int rc;
584 dbg_printk(TPACPI_DBG_INIT,
585 "registering %s as an ACPI driver\n", ibm->name);
587 BUG_ON(!ibm->acpi);
589 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
590 if (!ibm->acpi->driver) {
591 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
592 return -ENOMEM;
595 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
596 ibm->acpi->driver->ids = ibm->acpi->hid;
598 ibm->acpi->driver->ops.add = &tpacpi_device_add;
600 rc = acpi_bus_register_driver(ibm->acpi->driver);
601 if (rc < 0) {
602 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
603 ibm->name, rc);
604 kfree(ibm->acpi->driver);
605 ibm->acpi->driver = NULL;
606 } else if (!rc)
607 ibm->flags.acpi_driver_registered = 1;
609 return rc;
613 /****************************************************************************
614 ****************************************************************************
616 * Procfs Helpers
618 ****************************************************************************
619 ****************************************************************************/
621 static int dispatch_procfs_read(char *page, char **start, off_t off,
622 int count, int *eof, void *data)
624 struct ibm_struct *ibm = data;
625 int len;
627 if (!ibm || !ibm->read)
628 return -EINVAL;
630 len = ibm->read(page);
631 if (len < 0)
632 return len;
634 if (len <= off + count)
635 *eof = 1;
636 *start = page + off;
637 len -= off;
638 if (len > count)
639 len = count;
640 if (len < 0)
641 len = 0;
643 return len;
646 static int dispatch_procfs_write(struct file *file,
647 const char __user *userbuf,
648 unsigned long count, void *data)
650 struct ibm_struct *ibm = data;
651 char *kernbuf;
652 int ret;
654 if (!ibm || !ibm->write)
655 return -EINVAL;
657 kernbuf = kmalloc(count + 2, GFP_KERNEL);
658 if (!kernbuf)
659 return -ENOMEM;
661 if (copy_from_user(kernbuf, userbuf, count)) {
662 kfree(kernbuf);
663 return -EFAULT;
666 kernbuf[count] = 0;
667 strcat(kernbuf, ",");
668 ret = ibm->write(kernbuf);
669 if (ret == 0)
670 ret = count;
672 kfree(kernbuf);
674 return ret;
677 static char *next_cmd(char **cmds)
679 char *start = *cmds;
680 char *end;
682 while ((end = strchr(start, ',')) && end == start)
683 start = end + 1;
685 if (!end)
686 return NULL;
688 *end = 0;
689 *cmds = end + 1;
690 return start;
694 /****************************************************************************
695 ****************************************************************************
697 * Device model: input, hwmon and platform
699 ****************************************************************************
700 ****************************************************************************/
702 static struct platform_device *tpacpi_pdev;
703 static struct platform_device *tpacpi_sensors_pdev;
704 static struct device *tpacpi_hwmon;
705 static struct input_dev *tpacpi_inputdev;
706 static struct mutex tpacpi_inputdev_send_mutex;
707 static LIST_HEAD(tpacpi_all_drivers);
709 static int tpacpi_suspend_handler(struct platform_device *pdev,
710 pm_message_t state)
712 struct ibm_struct *ibm, *itmp;
714 list_for_each_entry_safe(ibm, itmp,
715 &tpacpi_all_drivers,
716 all_drivers) {
717 if (ibm->suspend)
718 (ibm->suspend)(state);
721 return 0;
724 static int tpacpi_resume_handler(struct platform_device *pdev)
726 struct ibm_struct *ibm, *itmp;
728 list_for_each_entry_safe(ibm, itmp,
729 &tpacpi_all_drivers,
730 all_drivers) {
731 if (ibm->resume)
732 (ibm->resume)();
735 return 0;
738 static struct platform_driver tpacpi_pdriver = {
739 .driver = {
740 .name = TPACPI_DRVR_NAME,
741 .owner = THIS_MODULE,
743 .suspend = tpacpi_suspend_handler,
744 .resume = tpacpi_resume_handler,
747 static struct platform_driver tpacpi_hwmon_pdriver = {
748 .driver = {
749 .name = TPACPI_HWMON_DRVR_NAME,
750 .owner = THIS_MODULE,
754 /*************************************************************************
755 * sysfs support helpers
758 struct attribute_set {
759 unsigned int members, max_members;
760 struct attribute_group group;
763 struct attribute_set_obj {
764 struct attribute_set s;
765 struct attribute *a;
766 } __attribute__((packed));
768 static struct attribute_set *create_attr_set(unsigned int max_members,
769 const char *name)
771 struct attribute_set_obj *sobj;
773 if (max_members == 0)
774 return NULL;
776 /* Allocates space for implicit NULL at the end too */
777 sobj = kzalloc(sizeof(struct attribute_set_obj) +
778 max_members * sizeof(struct attribute *),
779 GFP_KERNEL);
780 if (!sobj)
781 return NULL;
782 sobj->s.max_members = max_members;
783 sobj->s.group.attrs = &sobj->a;
784 sobj->s.group.name = name;
786 return &sobj->s;
789 #define destroy_attr_set(_set) \
790 kfree(_set);
792 /* not multi-threaded safe, use it in a single thread per set */
793 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
795 if (!s || !attr)
796 return -EINVAL;
798 if (s->members >= s->max_members)
799 return -ENOMEM;
801 s->group.attrs[s->members] = attr;
802 s->members++;
804 return 0;
807 static int add_many_to_attr_set(struct attribute_set *s,
808 struct attribute **attr,
809 unsigned int count)
811 int i, res;
813 for (i = 0; i < count; i++) {
814 res = add_to_attr_set(s, attr[i]);
815 if (res)
816 return res;
819 return 0;
822 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
824 sysfs_remove_group(kobj, &s->group);
825 destroy_attr_set(s);
828 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
829 sysfs_create_group(_kobj, &_attr_set->group)
831 static int parse_strtoul(const char *buf,
832 unsigned long max, unsigned long *value)
834 char *endp;
836 while (*buf && isspace(*buf))
837 buf++;
838 *value = simple_strtoul(buf, &endp, 0);
839 while (*endp && isspace(*endp))
840 endp++;
841 if (*endp || *value > max)
842 return -EINVAL;
844 return 0;
847 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
849 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
850 union acpi_object *obj;
851 int rc;
853 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
854 obj = (union acpi_object *)buffer.pointer;
855 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
856 printk(TPACPI_ERR "Unknown _BCL data, "
857 "please report this to %s\n", TPACPI_MAIL);
858 rc = 0;
859 } else {
860 rc = obj->package.count;
862 } else {
863 return 0;
866 kfree(buffer.pointer);
867 return rc;
870 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
871 u32 lvl, void *context, void **rv)
873 char name[ACPI_PATH_SEGMENT_LENGTH];
874 struct acpi_buffer buffer = { sizeof(name), &name };
876 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
877 !strncmp("_BCL", name, sizeof(name) - 1)) {
878 BUG_ON(!rv || !*rv);
879 **(int **)rv = tpacpi_query_bcl_levels(handle);
880 return AE_CTRL_TERMINATE;
881 } else {
882 return AE_OK;
887 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
889 static int __init tpacpi_check_std_acpi_brightness_support(void)
891 int status;
892 int bcl_levels = 0;
893 void *bcl_ptr = &bcl_levels;
895 if (!vid_handle) {
896 TPACPI_ACPIHANDLE_INIT(vid);
898 if (!vid_handle)
899 return 0;
902 * Search for a _BCL method, and execute it. This is safe on all
903 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
904 * BIOS in ACPI backlight control mode. We do NOT have to care
905 * about calling the _BCL method in an enabled video device, any
906 * will do for our purposes.
909 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
910 tpacpi_acpi_walk_find_bcl, NULL,
911 &bcl_ptr);
913 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
914 tp_features.bright_acpimode = 1;
915 return (bcl_levels - 2);
918 return 0;
921 static int __init tpacpi_new_rfkill(const unsigned int id,
922 struct rfkill **rfk,
923 const enum rfkill_type rfktype,
924 const char *name,
925 int (*toggle_radio)(void *, enum rfkill_state),
926 int (*get_state)(void *, enum rfkill_state *))
928 int res;
929 enum rfkill_state initial_state;
931 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
932 if (!*rfk) {
933 printk(TPACPI_ERR
934 "failed to allocate memory for rfkill class\n");
935 return -ENOMEM;
938 (*rfk)->name = name;
939 (*rfk)->get_state = get_state;
940 (*rfk)->toggle_radio = toggle_radio;
942 if (!get_state(NULL, &initial_state))
943 (*rfk)->state = initial_state;
945 res = rfkill_register(*rfk);
946 if (res < 0) {
947 printk(TPACPI_ERR
948 "failed to register %s rfkill switch: %d\n",
949 name, res);
950 rfkill_free(*rfk);
951 *rfk = NULL;
952 return res;
955 return 0;
958 /*************************************************************************
959 * thinkpad-acpi driver attributes
962 /* interface_version --------------------------------------------------- */
963 static ssize_t tpacpi_driver_interface_version_show(
964 struct device_driver *drv,
965 char *buf)
967 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
970 static DRIVER_ATTR(interface_version, S_IRUGO,
971 tpacpi_driver_interface_version_show, NULL);
973 /* debug_level --------------------------------------------------------- */
974 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
975 char *buf)
977 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
980 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
981 const char *buf, size_t count)
983 unsigned long t;
985 if (parse_strtoul(buf, 0xffff, &t))
986 return -EINVAL;
988 dbg_level = t;
990 return count;
993 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
994 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
996 /* version ------------------------------------------------------------- */
997 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
998 char *buf)
1000 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1001 TPACPI_DESC, TPACPI_VERSION);
1004 static DRIVER_ATTR(version, S_IRUGO,
1005 tpacpi_driver_version_show, NULL);
1007 /* --------------------------------------------------------------------- */
1009 static struct driver_attribute *tpacpi_driver_attributes[] = {
1010 &driver_attr_debug_level, &driver_attr_version,
1011 &driver_attr_interface_version,
1014 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1016 int i, res;
1018 i = 0;
1019 res = 0;
1020 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1021 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1022 i++;
1025 return res;
1028 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1030 int i;
1032 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1033 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1036 /****************************************************************************
1037 ****************************************************************************
1039 * Subdrivers
1041 ****************************************************************************
1042 ****************************************************************************/
1044 /*************************************************************************
1045 * thinkpad-acpi init subdriver
1048 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1050 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1051 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1053 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1054 (thinkpad_id.bios_version_str) ?
1055 thinkpad_id.bios_version_str : "unknown",
1056 (thinkpad_id.ec_version_str) ?
1057 thinkpad_id.ec_version_str : "unknown");
1059 if (thinkpad_id.vendor && thinkpad_id.model_str)
1060 printk(TPACPI_INFO "%s %s, model %s\n",
1061 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1062 "IBM" : ((thinkpad_id.vendor ==
1063 PCI_VENDOR_ID_LENOVO) ?
1064 "Lenovo" : "Unknown vendor"),
1065 thinkpad_id.model_str,
1066 (thinkpad_id.nummodel_str) ?
1067 thinkpad_id.nummodel_str : "unknown");
1069 return 0;
1072 static int thinkpad_acpi_driver_read(char *p)
1074 int len = 0;
1076 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1077 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1079 return len;
1082 static struct ibm_struct thinkpad_acpi_driver_data = {
1083 .name = "driver",
1084 .read = thinkpad_acpi_driver_read,
1087 /*************************************************************************
1088 * Hotkey subdriver
1091 enum { /* hot key scan codes (derived from ACPI DSDT) */
1092 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1093 TP_ACPI_HOTKEYSCAN_FNF2,
1094 TP_ACPI_HOTKEYSCAN_FNF3,
1095 TP_ACPI_HOTKEYSCAN_FNF4,
1096 TP_ACPI_HOTKEYSCAN_FNF5,
1097 TP_ACPI_HOTKEYSCAN_FNF6,
1098 TP_ACPI_HOTKEYSCAN_FNF7,
1099 TP_ACPI_HOTKEYSCAN_FNF8,
1100 TP_ACPI_HOTKEYSCAN_FNF9,
1101 TP_ACPI_HOTKEYSCAN_FNF10,
1102 TP_ACPI_HOTKEYSCAN_FNF11,
1103 TP_ACPI_HOTKEYSCAN_FNF12,
1104 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1105 TP_ACPI_HOTKEYSCAN_FNINSERT,
1106 TP_ACPI_HOTKEYSCAN_FNDELETE,
1107 TP_ACPI_HOTKEYSCAN_FNHOME,
1108 TP_ACPI_HOTKEYSCAN_FNEND,
1109 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1110 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1111 TP_ACPI_HOTKEYSCAN_FNSPACE,
1112 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1113 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1114 TP_ACPI_HOTKEYSCAN_MUTE,
1115 TP_ACPI_HOTKEYSCAN_THINKPAD,
1118 enum { /* Keys available through NVRAM polling */
1119 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1120 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1123 enum { /* Positions of some of the keys in hotkey masks */
1124 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1125 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1126 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1127 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1128 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1129 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1130 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1131 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1132 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1133 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1134 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1137 enum { /* NVRAM to ACPI HKEY group map */
1138 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1139 TP_ACPI_HKEY_ZOOM_MASK |
1140 TP_ACPI_HKEY_DISPSWTCH_MASK |
1141 TP_ACPI_HKEY_HIBERNATE_MASK,
1142 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1143 TP_ACPI_HKEY_BRGHTDWN_MASK,
1144 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1145 TP_ACPI_HKEY_VOLDWN_MASK |
1146 TP_ACPI_HKEY_MUTE_MASK,
1149 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1150 struct tp_nvram_state {
1151 u16 thinkpad_toggle:1;
1152 u16 zoom_toggle:1;
1153 u16 display_toggle:1;
1154 u16 thinklight_toggle:1;
1155 u16 hibernate_toggle:1;
1156 u16 displayexp_toggle:1;
1157 u16 display_state:1;
1158 u16 brightness_toggle:1;
1159 u16 volume_toggle:1;
1160 u16 mute:1;
1162 u8 brightness_level;
1163 u8 volume_level;
1166 static struct task_struct *tpacpi_hotkey_task;
1167 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1168 static int hotkey_poll_freq = 10; /* Hz */
1169 static struct mutex hotkey_thread_mutex;
1170 static struct mutex hotkey_thread_data_mutex;
1171 static unsigned int hotkey_config_change;
1173 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1175 #define hotkey_source_mask 0U
1177 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1179 static struct mutex hotkey_mutex;
1181 static enum { /* Reasons for waking up */
1182 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1183 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1184 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1185 } hotkey_wakeup_reason;
1187 static int hotkey_autosleep_ack;
1189 static int hotkey_orig_status;
1190 static u32 hotkey_orig_mask;
1191 static u32 hotkey_all_mask;
1192 static u32 hotkey_reserved_mask;
1193 static u32 hotkey_mask;
1195 static unsigned int hotkey_report_mode;
1197 static u16 *hotkey_keycode_map;
1199 static struct attribute_set *hotkey_dev_attributes;
1201 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1202 #define HOTKEY_CONFIG_CRITICAL_START \
1203 do { \
1204 mutex_lock(&hotkey_thread_data_mutex); \
1205 hotkey_config_change++; \
1206 } while (0);
1207 #define HOTKEY_CONFIG_CRITICAL_END \
1208 mutex_unlock(&hotkey_thread_data_mutex);
1209 #else
1210 #define HOTKEY_CONFIG_CRITICAL_START
1211 #define HOTKEY_CONFIG_CRITICAL_END
1212 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1214 /* HKEY.MHKG() return bits */
1215 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1217 static int hotkey_get_wlsw(int *status)
1219 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1220 return -EIO;
1221 return 0;
1224 static int hotkey_get_tablet_mode(int *status)
1226 int s;
1228 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1229 return -EIO;
1231 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1232 return 0;
1236 * Call with hotkey_mutex held
1238 static int hotkey_mask_get(void)
1240 u32 m = 0;
1242 if (tp_features.hotkey_mask) {
1243 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1244 return -EIO;
1246 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1248 return 0;
1252 * Call with hotkey_mutex held
1254 static int hotkey_mask_set(u32 mask)
1256 int i;
1257 int rc = 0;
1259 if (tp_features.hotkey_mask) {
1260 if (!tp_warned.hotkey_mask_ff &&
1261 (mask == 0xffff || mask == 0xffffff ||
1262 mask == 0xffffffff)) {
1263 tp_warned.hotkey_mask_ff = 1;
1264 printk(TPACPI_NOTICE
1265 "setting the hotkey mask to 0x%08x is likely "
1266 "not the best way to go about it\n", mask);
1267 printk(TPACPI_NOTICE
1268 "please consider using the driver defaults, "
1269 "and refer to up-to-date thinkpad-acpi "
1270 "documentation\n");
1273 HOTKEY_CONFIG_CRITICAL_START
1274 for (i = 0; i < 32; i++) {
1275 u32 m = 1 << i;
1276 /* enable in firmware mask only keys not in NVRAM
1277 * mode, but enable the key in the cached hotkey_mask
1278 * regardless of mode, or the key will end up
1279 * disabled by hotkey_mask_get() */
1280 if (!acpi_evalf(hkey_handle,
1281 NULL, "MHKM", "vdd", i + 1,
1282 !!((mask & ~hotkey_source_mask) & m))) {
1283 rc = -EIO;
1284 break;
1285 } else {
1286 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1289 HOTKEY_CONFIG_CRITICAL_END
1291 /* hotkey_mask_get must be called unconditionally below */
1292 if (!hotkey_mask_get() && !rc &&
1293 (hotkey_mask & ~hotkey_source_mask) !=
1294 (mask & ~hotkey_source_mask)) {
1295 printk(TPACPI_NOTICE
1296 "requested hot key mask 0x%08x, but "
1297 "firmware forced it to 0x%08x\n",
1298 mask, hotkey_mask);
1300 } else {
1301 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1302 HOTKEY_CONFIG_CRITICAL_START
1303 hotkey_mask = mask & hotkey_source_mask;
1304 HOTKEY_CONFIG_CRITICAL_END
1305 hotkey_mask_get();
1306 if (hotkey_mask != mask) {
1307 printk(TPACPI_NOTICE
1308 "requested hot key mask 0x%08x, "
1309 "forced to 0x%08x (NVRAM poll mask is "
1310 "0x%08x): no firmware mask support\n",
1311 mask, hotkey_mask, hotkey_source_mask);
1313 #else
1314 hotkey_mask_get();
1315 rc = -ENXIO;
1316 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1319 return rc;
1322 static int hotkey_status_get(int *status)
1324 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1325 return -EIO;
1327 return 0;
1330 static int hotkey_status_set(int status)
1332 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1333 return -EIO;
1335 return 0;
1338 static void tpacpi_input_send_tabletsw(void)
1340 int state;
1342 if (tp_features.hotkey_tablet &&
1343 !hotkey_get_tablet_mode(&state)) {
1344 mutex_lock(&tpacpi_inputdev_send_mutex);
1346 input_report_switch(tpacpi_inputdev,
1347 SW_TABLET_MODE, !!state);
1348 input_sync(tpacpi_inputdev);
1350 mutex_unlock(&tpacpi_inputdev_send_mutex);
1354 static void tpacpi_input_send_key(unsigned int scancode)
1356 unsigned int keycode;
1358 keycode = hotkey_keycode_map[scancode];
1360 if (keycode != KEY_RESERVED) {
1361 mutex_lock(&tpacpi_inputdev_send_mutex);
1363 input_report_key(tpacpi_inputdev, keycode, 1);
1364 if (keycode == KEY_UNKNOWN)
1365 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1366 scancode);
1367 input_sync(tpacpi_inputdev);
1369 input_report_key(tpacpi_inputdev, keycode, 0);
1370 if (keycode == KEY_UNKNOWN)
1371 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1372 scancode);
1373 input_sync(tpacpi_inputdev);
1375 mutex_unlock(&tpacpi_inputdev_send_mutex);
1379 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1380 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1382 static void tpacpi_hotkey_send_key(unsigned int scancode)
1384 tpacpi_input_send_key(scancode);
1385 if (hotkey_report_mode < 2) {
1386 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1387 0x80, 0x1001 + scancode);
1391 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1393 u8 d;
1395 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1396 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1397 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1398 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1399 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1400 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1402 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1403 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1404 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1406 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1407 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1408 n->displayexp_toggle =
1409 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1411 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1412 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1413 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1414 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1415 n->brightness_toggle =
1416 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1418 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1419 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1420 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1421 >> TP_NVRAM_POS_LEVEL_VOLUME;
1422 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1423 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1427 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1428 do { \
1429 if ((mask & (1 << __scancode)) && \
1430 oldn->__member != newn->__member) \
1431 tpacpi_hotkey_send_key(__scancode); \
1432 } while (0)
1434 #define TPACPI_MAY_SEND_KEY(__scancode) \
1435 do { if (mask & (1 << __scancode)) \
1436 tpacpi_hotkey_send_key(__scancode); } while (0)
1438 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1439 struct tp_nvram_state *newn,
1440 u32 mask)
1442 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1445 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1447 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1449 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1451 /* handle volume */
1452 if (oldn->volume_toggle != newn->volume_toggle) {
1453 if (oldn->mute != newn->mute) {
1454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1456 if (oldn->volume_level > newn->volume_level) {
1457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1458 } else if (oldn->volume_level < newn->volume_level) {
1459 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1460 } else if (oldn->mute == newn->mute) {
1461 /* repeated key presses that didn't change state */
1462 if (newn->mute) {
1463 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1464 } else if (newn->volume_level != 0) {
1465 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1466 } else {
1467 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1472 /* handle brightness */
1473 if (oldn->brightness_toggle != newn->brightness_toggle) {
1474 if (oldn->brightness_level < newn->brightness_level) {
1475 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1476 } else if (oldn->brightness_level > newn->brightness_level) {
1477 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1478 } else {
1479 /* repeated key presses that didn't change state */
1480 if (newn->brightness_level != 0) {
1481 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1482 } else {
1483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1489 #undef TPACPI_COMPARE_KEY
1490 #undef TPACPI_MAY_SEND_KEY
1492 static int hotkey_kthread(void *data)
1494 struct tp_nvram_state s[2];
1495 u32 mask;
1496 unsigned int si, so;
1497 unsigned long t;
1498 unsigned int change_detector, must_reset;
1500 mutex_lock(&hotkey_thread_mutex);
1502 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1503 goto exit;
1505 set_freezable();
1507 so = 0;
1508 si = 1;
1509 t = 0;
1511 /* Initial state for compares */
1512 mutex_lock(&hotkey_thread_data_mutex);
1513 change_detector = hotkey_config_change;
1514 mask = hotkey_source_mask & hotkey_mask;
1515 mutex_unlock(&hotkey_thread_data_mutex);
1516 hotkey_read_nvram(&s[so], mask);
1518 while (!kthread_should_stop() && hotkey_poll_freq) {
1519 if (t == 0)
1520 t = 1000/hotkey_poll_freq;
1521 t = msleep_interruptible(t);
1522 if (unlikely(kthread_should_stop()))
1523 break;
1524 must_reset = try_to_freeze();
1525 if (t > 0 && !must_reset)
1526 continue;
1528 mutex_lock(&hotkey_thread_data_mutex);
1529 if (must_reset || hotkey_config_change != change_detector) {
1530 /* forget old state on thaw or config change */
1531 si = so;
1532 t = 0;
1533 change_detector = hotkey_config_change;
1535 mask = hotkey_source_mask & hotkey_mask;
1536 mutex_unlock(&hotkey_thread_data_mutex);
1538 if (likely(mask)) {
1539 hotkey_read_nvram(&s[si], mask);
1540 if (likely(si != so)) {
1541 hotkey_compare_and_issue_event(&s[so], &s[si],
1542 mask);
1546 so = si;
1547 si ^= 1;
1550 exit:
1551 mutex_unlock(&hotkey_thread_mutex);
1552 return 0;
1555 static void hotkey_poll_stop_sync(void)
1557 if (tpacpi_hotkey_task) {
1558 if (frozen(tpacpi_hotkey_task) ||
1559 freezing(tpacpi_hotkey_task))
1560 thaw_process(tpacpi_hotkey_task);
1562 kthread_stop(tpacpi_hotkey_task);
1563 tpacpi_hotkey_task = NULL;
1564 mutex_lock(&hotkey_thread_mutex);
1565 /* at this point, the thread did exit */
1566 mutex_unlock(&hotkey_thread_mutex);
1570 /* call with hotkey_mutex held */
1571 static void hotkey_poll_setup(int may_warn)
1573 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1574 hotkey_poll_freq > 0 &&
1575 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1576 if (!tpacpi_hotkey_task) {
1577 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1578 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1579 if (IS_ERR(tpacpi_hotkey_task)) {
1580 tpacpi_hotkey_task = NULL;
1581 printk(TPACPI_ERR
1582 "could not create kernel thread "
1583 "for hotkey polling\n");
1586 } else {
1587 hotkey_poll_stop_sync();
1588 if (may_warn &&
1589 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1590 printk(TPACPI_NOTICE
1591 "hot keys 0x%08x require polling, "
1592 "which is currently disabled\n",
1593 hotkey_source_mask);
1598 static void hotkey_poll_setup_safe(int may_warn)
1600 mutex_lock(&hotkey_mutex);
1601 hotkey_poll_setup(may_warn);
1602 mutex_unlock(&hotkey_mutex);
1605 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1607 static void hotkey_poll_setup_safe(int __unused)
1611 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1613 static int hotkey_inputdev_open(struct input_dev *dev)
1615 switch (tpacpi_lifecycle) {
1616 case TPACPI_LIFE_INIT:
1618 * hotkey_init will call hotkey_poll_setup_safe
1619 * at the appropriate moment
1621 return 0;
1622 case TPACPI_LIFE_EXITING:
1623 return -EBUSY;
1624 case TPACPI_LIFE_RUNNING:
1625 hotkey_poll_setup_safe(0);
1626 return 0;
1629 /* Should only happen if tpacpi_lifecycle is corrupt */
1630 BUG();
1631 return -EBUSY;
1634 static void hotkey_inputdev_close(struct input_dev *dev)
1636 /* disable hotkey polling when possible */
1637 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1638 hotkey_poll_setup_safe(0);
1641 /* sysfs hotkey enable ------------------------------------------------- */
1642 static ssize_t hotkey_enable_show(struct device *dev,
1643 struct device_attribute *attr,
1644 char *buf)
1646 int res, status;
1648 res = hotkey_status_get(&status);
1649 if (res)
1650 return res;
1652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1655 static ssize_t hotkey_enable_store(struct device *dev,
1656 struct device_attribute *attr,
1657 const char *buf, size_t count)
1659 unsigned long t;
1660 int res;
1662 if (parse_strtoul(buf, 1, &t))
1663 return -EINVAL;
1665 res = hotkey_status_set(t);
1667 return (res) ? res : count;
1670 static struct device_attribute dev_attr_hotkey_enable =
1671 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1672 hotkey_enable_show, hotkey_enable_store);
1674 /* sysfs hotkey mask --------------------------------------------------- */
1675 static ssize_t hotkey_mask_show(struct device *dev,
1676 struct device_attribute *attr,
1677 char *buf)
1679 int res;
1681 if (mutex_lock_interruptible(&hotkey_mutex))
1682 return -ERESTARTSYS;
1683 res = hotkey_mask_get();
1684 mutex_unlock(&hotkey_mutex);
1686 return (res)?
1687 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1690 static ssize_t hotkey_mask_store(struct device *dev,
1691 struct device_attribute *attr,
1692 const char *buf, size_t count)
1694 unsigned long t;
1695 int res;
1697 if (parse_strtoul(buf, 0xffffffffUL, &t))
1698 return -EINVAL;
1700 if (mutex_lock_interruptible(&hotkey_mutex))
1701 return -ERESTARTSYS;
1703 res = hotkey_mask_set(t);
1705 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1706 hotkey_poll_setup(1);
1707 #endif
1709 mutex_unlock(&hotkey_mutex);
1711 return (res) ? res : count;
1714 static struct device_attribute dev_attr_hotkey_mask =
1715 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1716 hotkey_mask_show, hotkey_mask_store);
1718 /* sysfs hotkey bios_enabled ------------------------------------------- */
1719 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1720 struct device_attribute *attr,
1721 char *buf)
1723 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1726 static struct device_attribute dev_attr_hotkey_bios_enabled =
1727 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1729 /* sysfs hotkey bios_mask ---------------------------------------------- */
1730 static ssize_t hotkey_bios_mask_show(struct device *dev,
1731 struct device_attribute *attr,
1732 char *buf)
1734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1737 static struct device_attribute dev_attr_hotkey_bios_mask =
1738 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1740 /* sysfs hotkey all_mask ----------------------------------------------- */
1741 static ssize_t hotkey_all_mask_show(struct device *dev,
1742 struct device_attribute *attr,
1743 char *buf)
1745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1746 hotkey_all_mask | hotkey_source_mask);
1749 static struct device_attribute dev_attr_hotkey_all_mask =
1750 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1752 /* sysfs hotkey recommended_mask --------------------------------------- */
1753 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1754 struct device_attribute *attr,
1755 char *buf)
1757 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1758 (hotkey_all_mask | hotkey_source_mask)
1759 & ~hotkey_reserved_mask);
1762 static struct device_attribute dev_attr_hotkey_recommended_mask =
1763 __ATTR(hotkey_recommended_mask, S_IRUGO,
1764 hotkey_recommended_mask_show, NULL);
1766 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1768 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1769 static ssize_t hotkey_source_mask_show(struct device *dev,
1770 struct device_attribute *attr,
1771 char *buf)
1773 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1776 static ssize_t hotkey_source_mask_store(struct device *dev,
1777 struct device_attribute *attr,
1778 const char *buf, size_t count)
1780 unsigned long t;
1782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1784 return -EINVAL;
1786 if (mutex_lock_interruptible(&hotkey_mutex))
1787 return -ERESTARTSYS;
1789 HOTKEY_CONFIG_CRITICAL_START
1790 hotkey_source_mask = t;
1791 HOTKEY_CONFIG_CRITICAL_END
1793 hotkey_poll_setup(1);
1795 mutex_unlock(&hotkey_mutex);
1797 return count;
1800 static struct device_attribute dev_attr_hotkey_source_mask =
1801 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1802 hotkey_source_mask_show, hotkey_source_mask_store);
1804 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1805 static ssize_t hotkey_poll_freq_show(struct device *dev,
1806 struct device_attribute *attr,
1807 char *buf)
1809 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1812 static ssize_t hotkey_poll_freq_store(struct device *dev,
1813 struct device_attribute *attr,
1814 const char *buf, size_t count)
1816 unsigned long t;
1818 if (parse_strtoul(buf, 25, &t))
1819 return -EINVAL;
1821 if (mutex_lock_interruptible(&hotkey_mutex))
1822 return -ERESTARTSYS;
1824 hotkey_poll_freq = t;
1826 hotkey_poll_setup(1);
1827 mutex_unlock(&hotkey_mutex);
1829 return count;
1832 static struct device_attribute dev_attr_hotkey_poll_freq =
1833 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1834 hotkey_poll_freq_show, hotkey_poll_freq_store);
1836 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1838 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1839 static ssize_t hotkey_radio_sw_show(struct device *dev,
1840 struct device_attribute *attr,
1841 char *buf)
1843 int res, s;
1844 res = hotkey_get_wlsw(&s);
1845 if (res < 0)
1846 return res;
1848 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1851 static struct device_attribute dev_attr_hotkey_radio_sw =
1852 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1854 static void hotkey_radio_sw_notify_change(void)
1856 if (tp_features.hotkey_wlsw)
1857 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1858 "hotkey_radio_sw");
1861 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1862 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1863 struct device_attribute *attr,
1864 char *buf)
1866 int res, s;
1867 res = hotkey_get_tablet_mode(&s);
1868 if (res < 0)
1869 return res;
1871 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1874 static struct device_attribute dev_attr_hotkey_tablet_mode =
1875 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1877 static void hotkey_tablet_mode_notify_change(void)
1879 if (tp_features.hotkey_tablet)
1880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1881 "hotkey_tablet_mode");
1884 /* sysfs hotkey report_mode -------------------------------------------- */
1885 static ssize_t hotkey_report_mode_show(struct device *dev,
1886 struct device_attribute *attr,
1887 char *buf)
1889 return snprintf(buf, PAGE_SIZE, "%d\n",
1890 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1893 static struct device_attribute dev_attr_hotkey_report_mode =
1894 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1896 /* sysfs wakeup reason (pollable) -------------------------------------- */
1897 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1898 struct device_attribute *attr,
1899 char *buf)
1901 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1904 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1905 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1907 static void hotkey_wakeup_reason_notify_change(void)
1909 if (tp_features.hotkey_mask)
1910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1911 "wakeup_reason");
1914 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1915 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1916 struct device_attribute *attr,
1917 char *buf)
1919 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1922 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1923 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1924 hotkey_wakeup_hotunplug_complete_show, NULL);
1926 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1928 if (tp_features.hotkey_mask)
1929 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1930 "wakeup_hotunplug_complete");
1933 /* --------------------------------------------------------------------- */
1935 static struct attribute *hotkey_attributes[] __initdata = {
1936 &dev_attr_hotkey_enable.attr,
1937 &dev_attr_hotkey_bios_enabled.attr,
1938 &dev_attr_hotkey_report_mode.attr,
1939 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1940 &dev_attr_hotkey_mask.attr,
1941 &dev_attr_hotkey_all_mask.attr,
1942 &dev_attr_hotkey_recommended_mask.attr,
1943 &dev_attr_hotkey_source_mask.attr,
1944 &dev_attr_hotkey_poll_freq.attr,
1945 #endif
1948 static struct attribute *hotkey_mask_attributes[] __initdata = {
1949 &dev_attr_hotkey_bios_mask.attr,
1950 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1951 &dev_attr_hotkey_mask.attr,
1952 &dev_attr_hotkey_all_mask.attr,
1953 &dev_attr_hotkey_recommended_mask.attr,
1954 #endif
1955 &dev_attr_hotkey_wakeup_reason.attr,
1956 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1959 static void bluetooth_update_rfk(void);
1960 static void wan_update_rfk(void);
1961 static void tpacpi_send_radiosw_update(void)
1963 int wlsw;
1965 /* Sync these BEFORE sending any rfkill events */
1966 if (tp_features.bluetooth)
1967 bluetooth_update_rfk();
1968 if (tp_features.wan)
1969 wan_update_rfk();
1971 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1972 mutex_lock(&tpacpi_inputdev_send_mutex);
1974 input_report_switch(tpacpi_inputdev,
1975 SW_RFKILL_ALL, !!wlsw);
1976 input_sync(tpacpi_inputdev);
1978 mutex_unlock(&tpacpi_inputdev_send_mutex);
1980 hotkey_radio_sw_notify_change();
1983 static void hotkey_exit(void)
1985 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1986 hotkey_poll_stop_sync();
1987 #endif
1989 if (hotkey_dev_attributes)
1990 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1992 kfree(hotkey_keycode_map);
1994 if (tp_features.hotkey) {
1995 dbg_printk(TPACPI_DBG_EXIT,
1996 "restoring original hot key mask\n");
1997 /* no short-circuit boolean operator below! */
1998 if ((hotkey_mask_set(hotkey_orig_mask) |
1999 hotkey_status_set(hotkey_orig_status)) != 0)
2000 printk(TPACPI_ERR
2001 "failed to restore hot key mask "
2002 "to BIOS defaults\n");
2006 static int __init hotkey_init(struct ibm_init_struct *iibm)
2008 /* Requirements for changing the default keymaps:
2010 * 1. Many of the keys are mapped to KEY_RESERVED for very
2011 * good reasons. Do not change them unless you have deep
2012 * knowledge on the IBM and Lenovo ThinkPad firmware for
2013 * the various ThinkPad models. The driver behaves
2014 * differently for KEY_RESERVED: such keys have their
2015 * hot key mask *unset* in mask_recommended, and also
2016 * in the initial hot key mask programmed into the
2017 * firmware at driver load time, which means the firm-
2018 * ware may react very differently if you change them to
2019 * something else;
2021 * 2. You must be subscribed to the linux-thinkpad and
2022 * ibm-acpi-devel mailing lists, and you should read the
2023 * list archives since 2007 if you want to change the
2024 * keymaps. This requirement exists so that you will
2025 * know the past history of problems with the thinkpad-
2026 * acpi driver keymaps, and also that you will be
2027 * listening to any bug reports;
2029 * 3. Do not send thinkpad-acpi specific patches directly to
2030 * for merging, *ever*. Send them to the linux-acpi
2031 * mailinglist for comments. Merging is to be done only
2032 * through acpi-test and the ACPI maintainer.
2034 * If the above is too much to ask, don't change the keymap.
2035 * Ask the thinkpad-acpi maintainer to do it, instead.
2037 static u16 ibm_keycode_map[] __initdata = {
2038 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2039 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2040 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2041 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2043 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2044 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2045 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2046 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2048 /* brightness: firmware always reacts to them, unless
2049 * X.org did some tricks in the radeon BIOS scratch
2050 * registers of *some* models */
2051 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2052 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2054 /* Thinklight: firmware always react to it */
2055 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2057 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2058 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2060 /* Volume: firmware always react to it and reprograms
2061 * the built-in *extra* mixer. Never map it to control
2062 * another mixer by default. */
2063 KEY_RESERVED, /* 0x14: VOLUME UP */
2064 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2065 KEY_RESERVED, /* 0x16: MUTE */
2067 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2069 /* (assignments unknown, please report if found) */
2070 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2071 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2073 static u16 lenovo_keycode_map[] __initdata = {
2074 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2075 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2076 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2077 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2079 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2080 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2081 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2082 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2084 /* These either have to go through ACPI video, or
2085 * act like in the IBM ThinkPads, so don't ever
2086 * enable them by default */
2087 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2088 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2090 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2092 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2093 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2095 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2096 * react to it and reprograms the built-in *extra* mixer.
2097 * Never map it to control another mixer by default.
2099 * T60?, T61, R60?, R61: firmware and EC tries to send
2100 * these over the regular keyboard, so these are no-ops,
2101 * but there are still weird bugs re. MUTE, so do not
2102 * change unless you get test reports from all Lenovo
2103 * models. May cause the BIOS to interfere with the
2104 * HDA mixer.
2106 KEY_RESERVED, /* 0x14: VOLUME UP */
2107 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2108 KEY_RESERVED, /* 0x16: MUTE */
2110 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2112 /* (assignments unknown, please report if found) */
2113 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2114 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2117 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2118 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2119 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2121 int res, i;
2122 int status;
2123 int hkeyv;
2125 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2127 BUG_ON(!tpacpi_inputdev);
2128 BUG_ON(tpacpi_inputdev->open != NULL ||
2129 tpacpi_inputdev->close != NULL);
2131 TPACPI_ACPIHANDLE_INIT(hkey);
2132 mutex_init(&hotkey_mutex);
2134 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2135 mutex_init(&hotkey_thread_mutex);
2136 mutex_init(&hotkey_thread_data_mutex);
2137 #endif
2139 /* hotkey not supported on 570 */
2140 tp_features.hotkey = hkey_handle != NULL;
2142 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2143 str_supported(tp_features.hotkey));
2145 if (!tp_features.hotkey)
2146 return 1;
2148 hotkey_dev_attributes = create_attr_set(13, NULL);
2149 if (!hotkey_dev_attributes)
2150 return -ENOMEM;
2151 res = add_many_to_attr_set(hotkey_dev_attributes,
2152 hotkey_attributes,
2153 ARRAY_SIZE(hotkey_attributes));
2154 if (res)
2155 goto err_exit;
2157 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2158 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2159 for HKEY interface version 0x100 */
2160 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2161 if ((hkeyv >> 8) != 1) {
2162 printk(TPACPI_ERR "unknown version of the "
2163 "HKEY interface: 0x%x\n", hkeyv);
2164 printk(TPACPI_ERR "please report this to %s\n",
2165 TPACPI_MAIL);
2166 } else {
2168 * MHKV 0x100 in A31, R40, R40e,
2169 * T4x, X31, and later
2171 tp_features.hotkey_mask = 1;
2175 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2176 str_supported(tp_features.hotkey_mask));
2178 if (tp_features.hotkey_mask) {
2179 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2180 "MHKA", "qd")) {
2181 printk(TPACPI_ERR
2182 "missing MHKA handler, "
2183 "please report this to %s\n",
2184 TPACPI_MAIL);
2185 /* FN+F12, FN+F4, FN+F3 */
2186 hotkey_all_mask = 0x080cU;
2190 /* hotkey_source_mask *must* be zero for
2191 * the first hotkey_mask_get */
2192 res = hotkey_status_get(&hotkey_orig_status);
2193 if (res)
2194 goto err_exit;
2196 if (tp_features.hotkey_mask) {
2197 res = hotkey_mask_get();
2198 if (res)
2199 goto err_exit;
2201 hotkey_orig_mask = hotkey_mask;
2202 res = add_many_to_attr_set(
2203 hotkey_dev_attributes,
2204 hotkey_mask_attributes,
2205 ARRAY_SIZE(hotkey_mask_attributes));
2206 if (res)
2207 goto err_exit;
2210 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2211 if (tp_features.hotkey_mask) {
2212 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2213 & ~hotkey_all_mask;
2214 } else {
2215 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2218 vdbg_printk(TPACPI_DBG_INIT,
2219 "hotkey source mask 0x%08x, polling freq %d\n",
2220 hotkey_source_mask, hotkey_poll_freq);
2221 #endif
2223 /* Not all thinkpads have a hardware radio switch */
2224 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2225 tp_features.hotkey_wlsw = 1;
2226 printk(TPACPI_INFO
2227 "radio switch found; radios are %s\n",
2228 enabled(status, 0));
2230 if (tp_features.hotkey_wlsw)
2231 res = add_to_attr_set(hotkey_dev_attributes,
2232 &dev_attr_hotkey_radio_sw.attr);
2234 /* For X41t, X60t, X61t Tablets... */
2235 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2236 tp_features.hotkey_tablet = 1;
2237 printk(TPACPI_INFO
2238 "possible tablet mode switch found; "
2239 "ThinkPad in %s mode\n",
2240 (status & TP_HOTKEY_TABLET_MASK)?
2241 "tablet" : "laptop");
2242 res = add_to_attr_set(hotkey_dev_attributes,
2243 &dev_attr_hotkey_tablet_mode.attr);
2246 if (!res)
2247 res = register_attr_set_with_sysfs(
2248 hotkey_dev_attributes,
2249 &tpacpi_pdev->dev.kobj);
2250 if (res)
2251 goto err_exit;
2253 /* Set up key map */
2255 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2256 GFP_KERNEL);
2257 if (!hotkey_keycode_map) {
2258 printk(TPACPI_ERR
2259 "failed to allocate memory for key map\n");
2260 res = -ENOMEM;
2261 goto err_exit;
2264 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2265 dbg_printk(TPACPI_DBG_INIT,
2266 "using Lenovo default hot key map\n");
2267 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2268 TPACPI_HOTKEY_MAP_SIZE);
2269 } else {
2270 dbg_printk(TPACPI_DBG_INIT,
2271 "using IBM default hot key map\n");
2272 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2273 TPACPI_HOTKEY_MAP_SIZE);
2276 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2277 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2278 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2279 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2280 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2281 tpacpi_inputdev->keycode = hotkey_keycode_map;
2282 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2283 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2284 set_bit(hotkey_keycode_map[i],
2285 tpacpi_inputdev->keybit);
2286 } else {
2287 if (i < sizeof(hotkey_reserved_mask)*8)
2288 hotkey_reserved_mask |= 1 << i;
2292 if (tp_features.hotkey_wlsw) {
2293 set_bit(EV_SW, tpacpi_inputdev->evbit);
2294 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2296 if (tp_features.hotkey_tablet) {
2297 set_bit(EV_SW, tpacpi_inputdev->evbit);
2298 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2301 /* Do not issue duplicate brightness change events to
2302 * userspace */
2303 if (!tp_features.bright_acpimode)
2304 /* update bright_acpimode... */
2305 tpacpi_check_std_acpi_brightness_support();
2307 if (tp_features.bright_acpimode) {
2308 printk(TPACPI_INFO
2309 "This ThinkPad has standard ACPI backlight "
2310 "brightness control, supported by the ACPI "
2311 "video driver\n");
2312 printk(TPACPI_NOTICE
2313 "Disabling thinkpad-acpi brightness events "
2314 "by default...\n");
2316 /* The hotkey_reserved_mask change below is not
2317 * necessary while the keys are at KEY_RESERVED in the
2318 * default map, but better safe than sorry, leave it
2319 * here as a marker of what we have to do, especially
2320 * when we finally become able to set this at runtime
2321 * on response to X.org requests */
2322 hotkey_reserved_mask |=
2323 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2324 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2327 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2328 res = hotkey_status_set(1);
2329 if (res) {
2330 hotkey_exit();
2331 return res;
2333 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2334 & ~hotkey_reserved_mask)
2335 | hotkey_orig_mask);
2336 if (res < 0 && res != -ENXIO) {
2337 hotkey_exit();
2338 return res;
2341 dbg_printk(TPACPI_DBG_INIT,
2342 "legacy hot key reporting over procfs %s\n",
2343 (hotkey_report_mode < 2) ?
2344 "enabled" : "disabled");
2346 tpacpi_inputdev->open = &hotkey_inputdev_open;
2347 tpacpi_inputdev->close = &hotkey_inputdev_close;
2349 hotkey_poll_setup_safe(1);
2350 tpacpi_send_radiosw_update();
2351 tpacpi_input_send_tabletsw();
2353 return 0;
2355 err_exit:
2356 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2357 hotkey_dev_attributes = NULL;
2359 return (res < 0)? res : 1;
2362 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2364 u32 hkey;
2365 unsigned int scancode;
2366 int send_acpi_ev;
2367 int ignore_acpi_ev;
2368 int unk_ev;
2370 if (event != 0x80) {
2371 printk(TPACPI_ERR
2372 "unknown HKEY notification event %d\n", event);
2373 /* forward it to userspace, maybe it knows how to handle it */
2374 acpi_bus_generate_netlink_event(
2375 ibm->acpi->device->pnp.device_class,
2376 ibm->acpi->device->dev.bus_id,
2377 event, 0);
2378 return;
2381 while (1) {
2382 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2383 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2384 return;
2387 if (hkey == 0) {
2388 /* queue empty */
2389 return;
2392 send_acpi_ev = 1;
2393 ignore_acpi_ev = 0;
2394 unk_ev = 0;
2396 switch (hkey >> 12) {
2397 case 1:
2398 /* 0x1000-0x1FFF: key presses */
2399 scancode = hkey & 0xfff;
2400 if (scancode > 0 && scancode < 0x21) {
2401 scancode--;
2402 if (!(hotkey_source_mask & (1 << scancode))) {
2403 tpacpi_input_send_key(scancode);
2404 send_acpi_ev = 0;
2405 } else {
2406 ignore_acpi_ev = 1;
2408 } else {
2409 unk_ev = 1;
2411 break;
2412 case 2:
2413 /* Wakeup reason */
2414 switch (hkey) {
2415 case 0x2304: /* suspend, undock */
2416 case 0x2404: /* hibernation, undock */
2417 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2418 ignore_acpi_ev = 1;
2419 break;
2420 case 0x2305: /* suspend, bay eject */
2421 case 0x2405: /* hibernation, bay eject */
2422 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2423 ignore_acpi_ev = 1;
2424 break;
2425 default:
2426 unk_ev = 1;
2428 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2429 printk(TPACPI_INFO
2430 "woke up due to a hot-unplug "
2431 "request...\n");
2432 hotkey_wakeup_reason_notify_change();
2434 break;
2435 case 3:
2436 /* bay-related wakeups */
2437 if (hkey == 0x3003) {
2438 hotkey_autosleep_ack = 1;
2439 printk(TPACPI_INFO
2440 "bay ejected\n");
2441 hotkey_wakeup_hotunplug_complete_notify_change();
2442 } else {
2443 unk_ev = 1;
2445 break;
2446 case 4:
2447 /* dock-related wakeups */
2448 if (hkey == 0x4003) {
2449 hotkey_autosleep_ack = 1;
2450 printk(TPACPI_INFO
2451 "undocked\n");
2452 hotkey_wakeup_hotunplug_complete_notify_change();
2453 } else {
2454 unk_ev = 1;
2456 break;
2457 case 5:
2458 /* 0x5000-0x5FFF: human interface helpers */
2459 switch (hkey) {
2460 case 0x5010: /* Lenovo new BIOS: brightness changed */
2461 case 0x500b: /* X61t: tablet pen inserted into bay */
2462 case 0x500c: /* X61t: tablet pen removed from bay */
2463 break;
2464 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2465 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2466 tpacpi_input_send_tabletsw();
2467 hotkey_tablet_mode_notify_change();
2468 send_acpi_ev = 0;
2469 break;
2470 case 0x5001:
2471 case 0x5002:
2472 /* LID switch events. Do not propagate */
2473 ignore_acpi_ev = 1;
2474 break;
2475 default:
2476 unk_ev = 1;
2478 break;
2479 case 7:
2480 /* 0x7000-0x7FFF: misc */
2481 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2482 tpacpi_send_radiosw_update();
2483 send_acpi_ev = 0;
2484 break;
2486 /* fallthrough to default */
2487 default:
2488 unk_ev = 1;
2490 if (unk_ev) {
2491 printk(TPACPI_NOTICE
2492 "unhandled HKEY event 0x%04x\n", hkey);
2495 /* Legacy events */
2496 if (!ignore_acpi_ev &&
2497 (send_acpi_ev || hotkey_report_mode < 2)) {
2498 acpi_bus_generate_proc_event(ibm->acpi->device,
2499 event, hkey);
2502 /* netlink events */
2503 if (!ignore_acpi_ev && send_acpi_ev) {
2504 acpi_bus_generate_netlink_event(
2505 ibm->acpi->device->pnp.device_class,
2506 ibm->acpi->device->dev.bus_id,
2507 event, hkey);
2512 static void hotkey_suspend(pm_message_t state)
2514 /* Do these on suspend, we get the events on early resume! */
2515 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2516 hotkey_autosleep_ack = 0;
2519 static void hotkey_resume(void)
2521 if (hotkey_mask_get())
2522 printk(TPACPI_ERR
2523 "error while trying to read hot key mask "
2524 "from firmware\n");
2525 tpacpi_send_radiosw_update();
2526 hotkey_tablet_mode_notify_change();
2527 hotkey_wakeup_reason_notify_change();
2528 hotkey_wakeup_hotunplug_complete_notify_change();
2529 hotkey_poll_setup_safe(0);
2532 /* procfs -------------------------------------------------------------- */
2533 static int hotkey_read(char *p)
2535 int res, status;
2536 int len = 0;
2538 if (!tp_features.hotkey) {
2539 len += sprintf(p + len, "status:\t\tnot supported\n");
2540 return len;
2543 if (mutex_lock_interruptible(&hotkey_mutex))
2544 return -ERESTARTSYS;
2545 res = hotkey_status_get(&status);
2546 if (!res)
2547 res = hotkey_mask_get();
2548 mutex_unlock(&hotkey_mutex);
2549 if (res)
2550 return res;
2552 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2553 if (tp_features.hotkey_mask) {
2554 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2555 len += sprintf(p + len,
2556 "commands:\tenable, disable, reset, <mask>\n");
2557 } else {
2558 len += sprintf(p + len, "mask:\t\tnot supported\n");
2559 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2562 return len;
2565 static int hotkey_write(char *buf)
2567 int res, status;
2568 u32 mask;
2569 char *cmd;
2571 if (!tp_features.hotkey)
2572 return -ENODEV;
2574 if (mutex_lock_interruptible(&hotkey_mutex))
2575 return -ERESTARTSYS;
2577 status = -1;
2578 mask = hotkey_mask;
2580 res = 0;
2581 while ((cmd = next_cmd(&buf))) {
2582 if (strlencmp(cmd, "enable") == 0) {
2583 status = 1;
2584 } else if (strlencmp(cmd, "disable") == 0) {
2585 status = 0;
2586 } else if (strlencmp(cmd, "reset") == 0) {
2587 status = hotkey_orig_status;
2588 mask = hotkey_orig_mask;
2589 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2590 /* mask set */
2591 } else if (sscanf(cmd, "%x", &mask) == 1) {
2592 /* mask set */
2593 } else {
2594 res = -EINVAL;
2595 goto errexit;
2598 if (status != -1)
2599 res = hotkey_status_set(status);
2601 if (!res && mask != hotkey_mask)
2602 res = hotkey_mask_set(mask);
2604 errexit:
2605 mutex_unlock(&hotkey_mutex);
2606 return res;
2609 static const struct acpi_device_id ibm_htk_device_ids[] = {
2610 {TPACPI_ACPI_HKEY_HID, 0},
2611 {"", 0},
2614 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2615 .hid = ibm_htk_device_ids,
2616 .notify = hotkey_notify,
2617 .handle = &hkey_handle,
2618 .type = ACPI_DEVICE_NOTIFY,
2621 static struct ibm_struct hotkey_driver_data = {
2622 .name = "hotkey",
2623 .read = hotkey_read,
2624 .write = hotkey_write,
2625 .exit = hotkey_exit,
2626 .resume = hotkey_resume,
2627 .suspend = hotkey_suspend,
2628 .acpi = &ibm_hotkey_acpidriver,
2631 /*************************************************************************
2632 * Bluetooth subdriver
2635 enum {
2636 /* ACPI GBDC/SBDC bits */
2637 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2638 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2639 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2642 static struct rfkill *tpacpi_bluetooth_rfkill;
2644 static int bluetooth_get_radiosw(void)
2646 int status;
2648 if (!tp_features.bluetooth)
2649 return -ENODEV;
2651 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2652 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2653 return RFKILL_STATE_HARD_BLOCKED;
2655 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2656 return -EIO;
2658 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2659 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2662 static void bluetooth_update_rfk(void)
2664 int status;
2666 if (!tpacpi_bluetooth_rfkill)
2667 return;
2669 status = bluetooth_get_radiosw();
2670 if (status < 0)
2671 return;
2672 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2675 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2677 int status;
2679 if (!tp_features.bluetooth)
2680 return -ENODEV;
2682 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2683 * reason to risk weird behaviour. */
2684 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2685 && radio_on)
2686 return -EPERM;
2688 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2689 return -EIO;
2690 if (radio_on)
2691 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2692 else
2693 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2694 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2695 return -EIO;
2697 if (update_rfk)
2698 bluetooth_update_rfk();
2700 return 0;
2703 /* sysfs bluetooth enable ---------------------------------------------- */
2704 static ssize_t bluetooth_enable_show(struct device *dev,
2705 struct device_attribute *attr,
2706 char *buf)
2708 int status;
2710 status = bluetooth_get_radiosw();
2711 if (status < 0)
2712 return status;
2714 return snprintf(buf, PAGE_SIZE, "%d\n",
2715 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2718 static ssize_t bluetooth_enable_store(struct device *dev,
2719 struct device_attribute *attr,
2720 const char *buf, size_t count)
2722 unsigned long t;
2723 int res;
2725 if (parse_strtoul(buf, 1, &t))
2726 return -EINVAL;
2728 res = bluetooth_set_radiosw(t, 1);
2730 return (res) ? res : count;
2733 static struct device_attribute dev_attr_bluetooth_enable =
2734 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2735 bluetooth_enable_show, bluetooth_enable_store);
2737 /* --------------------------------------------------------------------- */
2739 static struct attribute *bluetooth_attributes[] = {
2740 &dev_attr_bluetooth_enable.attr,
2741 NULL
2744 static const struct attribute_group bluetooth_attr_group = {
2745 .attrs = bluetooth_attributes,
2748 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2750 int bts = bluetooth_get_radiosw();
2752 if (bts < 0)
2753 return bts;
2755 *state = bts;
2756 return 0;
2759 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2761 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2764 static void bluetooth_exit(void)
2766 if (tpacpi_bluetooth_rfkill)
2767 rfkill_unregister(tpacpi_bluetooth_rfkill);
2769 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2770 &bluetooth_attr_group);
2773 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2775 int res;
2776 int status = 0;
2778 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2780 TPACPI_ACPIHANDLE_INIT(hkey);
2782 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2783 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2784 tp_features.bluetooth = hkey_handle &&
2785 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2787 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2788 str_supported(tp_features.bluetooth),
2789 status);
2791 if (tp_features.bluetooth &&
2792 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2793 /* no bluetooth hardware present in system */
2794 tp_features.bluetooth = 0;
2795 dbg_printk(TPACPI_DBG_INIT,
2796 "bluetooth hardware not installed\n");
2799 if (!tp_features.bluetooth)
2800 return 1;
2802 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2803 &bluetooth_attr_group);
2804 if (res)
2805 return res;
2807 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2808 &tpacpi_bluetooth_rfkill,
2809 RFKILL_TYPE_BLUETOOTH,
2810 "tpacpi_bluetooth_sw",
2811 tpacpi_bluetooth_rfk_set,
2812 tpacpi_bluetooth_rfk_get);
2813 if (res) {
2814 bluetooth_exit();
2815 return res;
2818 return 0;
2821 /* procfs -------------------------------------------------------------- */
2822 static int bluetooth_read(char *p)
2824 int len = 0;
2825 int status = bluetooth_get_radiosw();
2827 if (!tp_features.bluetooth)
2828 len += sprintf(p + len, "status:\t\tnot supported\n");
2829 else {
2830 len += sprintf(p + len, "status:\t\t%s\n",
2831 (status == RFKILL_STATE_UNBLOCKED) ?
2832 "enabled" : "disabled");
2833 len += sprintf(p + len, "commands:\tenable, disable\n");
2836 return len;
2839 static int bluetooth_write(char *buf)
2841 char *cmd;
2843 if (!tp_features.bluetooth)
2844 return -ENODEV;
2846 while ((cmd = next_cmd(&buf))) {
2847 if (strlencmp(cmd, "enable") == 0) {
2848 bluetooth_set_radiosw(1, 1);
2849 } else if (strlencmp(cmd, "disable") == 0) {
2850 bluetooth_set_radiosw(0, 1);
2851 } else
2852 return -EINVAL;
2855 return 0;
2858 static struct ibm_struct bluetooth_driver_data = {
2859 .name = "bluetooth",
2860 .read = bluetooth_read,
2861 .write = bluetooth_write,
2862 .exit = bluetooth_exit,
2865 /*************************************************************************
2866 * Wan subdriver
2869 enum {
2870 /* ACPI GWAN/SWAN bits */
2871 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2872 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2873 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2876 static struct rfkill *tpacpi_wan_rfkill;
2878 static int wan_get_radiosw(void)
2880 int status;
2882 if (!tp_features.wan)
2883 return -ENODEV;
2885 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2886 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2887 return RFKILL_STATE_HARD_BLOCKED;
2889 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2890 return -EIO;
2892 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2893 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2896 static void wan_update_rfk(void)
2898 int status;
2900 if (!tpacpi_wan_rfkill)
2901 return;
2903 status = wan_get_radiosw();
2904 if (status < 0)
2905 return;
2906 rfkill_force_state(tpacpi_wan_rfkill, status);
2909 static int wan_set_radiosw(int radio_on, int update_rfk)
2911 int status;
2913 if (!tp_features.wan)
2914 return -ENODEV;
2916 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2917 * reason to risk weird behaviour. */
2918 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2919 && radio_on)
2920 return -EPERM;
2922 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2923 return -EIO;
2924 if (radio_on)
2925 status |= TP_ACPI_WANCARD_RADIOSSW;
2926 else
2927 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2928 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2929 return -EIO;
2931 if (update_rfk)
2932 wan_update_rfk();
2934 return 0;
2937 /* sysfs wan enable ---------------------------------------------------- */
2938 static ssize_t wan_enable_show(struct device *dev,
2939 struct device_attribute *attr,
2940 char *buf)
2942 int status;
2944 status = wan_get_radiosw();
2945 if (status < 0)
2946 return status;
2948 return snprintf(buf, PAGE_SIZE, "%d\n",
2949 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2952 static ssize_t wan_enable_store(struct device *dev,
2953 struct device_attribute *attr,
2954 const char *buf, size_t count)
2956 unsigned long t;
2957 int res;
2959 if (parse_strtoul(buf, 1, &t))
2960 return -EINVAL;
2962 res = wan_set_radiosw(t, 1);
2964 return (res) ? res : count;
2967 static struct device_attribute dev_attr_wan_enable =
2968 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2969 wan_enable_show, wan_enable_store);
2971 /* --------------------------------------------------------------------- */
2973 static struct attribute *wan_attributes[] = {
2974 &dev_attr_wan_enable.attr,
2975 NULL
2978 static const struct attribute_group wan_attr_group = {
2979 .attrs = wan_attributes,
2982 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2984 int wans = wan_get_radiosw();
2986 if (wans < 0)
2987 return wans;
2989 *state = wans;
2990 return 0;
2993 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2995 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2998 static void wan_exit(void)
3000 if (tpacpi_wan_rfkill)
3001 rfkill_unregister(tpacpi_wan_rfkill);
3003 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3004 &wan_attr_group);
3007 static int __init wan_init(struct ibm_init_struct *iibm)
3009 int res;
3010 int status = 0;
3012 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3014 TPACPI_ACPIHANDLE_INIT(hkey);
3016 tp_features.wan = hkey_handle &&
3017 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3019 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3020 str_supported(tp_features.wan),
3021 status);
3023 if (tp_features.wan &&
3024 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3025 /* no wan hardware present in system */
3026 tp_features.wan = 0;
3027 dbg_printk(TPACPI_DBG_INIT,
3028 "wan hardware not installed\n");
3031 if (!tp_features.wan)
3032 return 1;
3034 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3035 &wan_attr_group);
3036 if (res)
3037 return res;
3039 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3040 &tpacpi_wan_rfkill,
3041 RFKILL_TYPE_WWAN,
3042 "tpacpi_wwan_sw",
3043 tpacpi_wan_rfk_set,
3044 tpacpi_wan_rfk_get);
3045 if (res) {
3046 wan_exit();
3047 return res;
3050 return 0;
3053 /* procfs -------------------------------------------------------------- */
3054 static int wan_read(char *p)
3056 int len = 0;
3057 int status = wan_get_radiosw();
3059 if (!tp_features.wan)
3060 len += sprintf(p + len, "status:\t\tnot supported\n");
3061 else {
3062 len += sprintf(p + len, "status:\t\t%s\n",
3063 (status == RFKILL_STATE_UNBLOCKED) ?
3064 "enabled" : "disabled");
3065 len += sprintf(p + len, "commands:\tenable, disable\n");
3068 return len;
3071 static int wan_write(char *buf)
3073 char *cmd;
3075 if (!tp_features.wan)
3076 return -ENODEV;
3078 while ((cmd = next_cmd(&buf))) {
3079 if (strlencmp(cmd, "enable") == 0) {
3080 wan_set_radiosw(1, 1);
3081 } else if (strlencmp(cmd, "disable") == 0) {
3082 wan_set_radiosw(0, 1);
3083 } else
3084 return -EINVAL;
3087 return 0;
3090 static struct ibm_struct wan_driver_data = {
3091 .name = "wan",
3092 .read = wan_read,
3093 .write = wan_write,
3094 .exit = wan_exit,
3097 /*************************************************************************
3098 * Video subdriver
3101 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3103 enum video_access_mode {
3104 TPACPI_VIDEO_NONE = 0,
3105 TPACPI_VIDEO_570, /* 570 */
3106 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3107 TPACPI_VIDEO_NEW, /* all others */
3110 enum { /* video status flags, based on VIDEO_570 */
3111 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3112 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3113 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3116 enum { /* TPACPI_VIDEO_570 constants */
3117 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3118 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3119 * video_status_flags */
3120 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3121 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3124 static enum video_access_mode video_supported;
3125 static int video_orig_autosw;
3127 static int video_autosw_get(void);
3128 static int video_autosw_set(int enable);
3130 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3132 static int __init video_init(struct ibm_init_struct *iibm)
3134 int ivga;
3136 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3138 TPACPI_ACPIHANDLE_INIT(vid);
3139 TPACPI_ACPIHANDLE_INIT(vid2);
3141 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3142 /* G41, assume IVGA doesn't change */
3143 vid_handle = vid2_handle;
3145 if (!vid_handle)
3146 /* video switching not supported on R30, R31 */
3147 video_supported = TPACPI_VIDEO_NONE;
3148 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3149 /* 570 */
3150 video_supported = TPACPI_VIDEO_570;
3151 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3152 /* 600e/x, 770e, 770x */
3153 video_supported = TPACPI_VIDEO_770;
3154 else
3155 /* all others */
3156 video_supported = TPACPI_VIDEO_NEW;
3158 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3159 str_supported(video_supported != TPACPI_VIDEO_NONE),
3160 video_supported);
3162 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3165 static void video_exit(void)
3167 dbg_printk(TPACPI_DBG_EXIT,
3168 "restoring original video autoswitch mode\n");
3169 if (video_autosw_set(video_orig_autosw))
3170 printk(TPACPI_ERR "error while trying to restore original "
3171 "video autoswitch mode\n");
3174 static int video_outputsw_get(void)
3176 int status = 0;
3177 int i;
3179 switch (video_supported) {
3180 case TPACPI_VIDEO_570:
3181 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3182 TP_ACPI_VIDEO_570_PHSCMD))
3183 return -EIO;
3184 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3185 break;
3186 case TPACPI_VIDEO_770:
3187 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3188 return -EIO;
3189 if (i)
3190 status |= TP_ACPI_VIDEO_S_LCD;
3191 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3192 return -EIO;
3193 if (i)
3194 status |= TP_ACPI_VIDEO_S_CRT;
3195 break;
3196 case TPACPI_VIDEO_NEW:
3197 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3198 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3199 return -EIO;
3200 if (i)
3201 status |= TP_ACPI_VIDEO_S_CRT;
3203 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3204 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3205 return -EIO;
3206 if (i)
3207 status |= TP_ACPI_VIDEO_S_LCD;
3208 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3209 return -EIO;
3210 if (i)
3211 status |= TP_ACPI_VIDEO_S_DVI;
3212 break;
3213 default:
3214 return -ENOSYS;
3217 return status;
3220 static int video_outputsw_set(int status)
3222 int autosw;
3223 int res = 0;
3225 switch (video_supported) {
3226 case TPACPI_VIDEO_570:
3227 res = acpi_evalf(NULL, NULL,
3228 "\\_SB.PHS2", "vdd",
3229 TP_ACPI_VIDEO_570_PHS2CMD,
3230 status | TP_ACPI_VIDEO_570_PHS2SET);
3231 break;
3232 case TPACPI_VIDEO_770:
3233 autosw = video_autosw_get();
3234 if (autosw < 0)
3235 return autosw;
3237 res = video_autosw_set(1);
3238 if (res)
3239 return res;
3240 res = acpi_evalf(vid_handle, NULL,
3241 "ASWT", "vdd", status * 0x100, 0);
3242 if (!autosw && video_autosw_set(autosw)) {
3243 printk(TPACPI_ERR
3244 "video auto-switch left enabled due to error\n");
3245 return -EIO;
3247 break;
3248 case TPACPI_VIDEO_NEW:
3249 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3250 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3251 break;
3252 default:
3253 return -ENOSYS;
3256 return (res)? 0 : -EIO;
3259 static int video_autosw_get(void)
3261 int autosw = 0;
3263 switch (video_supported) {
3264 case TPACPI_VIDEO_570:
3265 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3266 return -EIO;
3267 break;
3268 case TPACPI_VIDEO_770:
3269 case TPACPI_VIDEO_NEW:
3270 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3271 return -EIO;
3272 break;
3273 default:
3274 return -ENOSYS;
3277 return autosw & 1;
3280 static int video_autosw_set(int enable)
3282 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3283 return -EIO;
3284 return 0;
3287 static int video_outputsw_cycle(void)
3289 int autosw = video_autosw_get();
3290 int res;
3292 if (autosw < 0)
3293 return autosw;
3295 switch (video_supported) {
3296 case TPACPI_VIDEO_570:
3297 res = video_autosw_set(1);
3298 if (res)
3299 return res;
3300 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3301 break;
3302 case TPACPI_VIDEO_770:
3303 case TPACPI_VIDEO_NEW:
3304 res = video_autosw_set(1);
3305 if (res)
3306 return res;
3307 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3308 break;
3309 default:
3310 return -ENOSYS;
3312 if (!autosw && video_autosw_set(autosw)) {
3313 printk(TPACPI_ERR
3314 "video auto-switch left enabled due to error\n");
3315 return -EIO;
3318 return (res)? 0 : -EIO;
3321 static int video_expand_toggle(void)
3323 switch (video_supported) {
3324 case TPACPI_VIDEO_570:
3325 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3326 0 : -EIO;
3327 case TPACPI_VIDEO_770:
3328 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3329 0 : -EIO;
3330 case TPACPI_VIDEO_NEW:
3331 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3332 0 : -EIO;
3333 default:
3334 return -ENOSYS;
3336 /* not reached */
3339 static int video_read(char *p)
3341 int status, autosw;
3342 int len = 0;
3344 if (video_supported == TPACPI_VIDEO_NONE) {
3345 len += sprintf(p + len, "status:\t\tnot supported\n");
3346 return len;
3349 status = video_outputsw_get();
3350 if (status < 0)
3351 return status;
3353 autosw = video_autosw_get();
3354 if (autosw < 0)
3355 return autosw;
3357 len += sprintf(p + len, "status:\t\tsupported\n");
3358 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3359 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3360 if (video_supported == TPACPI_VIDEO_NEW)
3361 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3362 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3363 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3364 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3365 if (video_supported == TPACPI_VIDEO_NEW)
3366 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3367 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3368 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3370 return len;
3373 static int video_write(char *buf)
3375 char *cmd;
3376 int enable, disable, status;
3377 int res;
3379 if (video_supported == TPACPI_VIDEO_NONE)
3380 return -ENODEV;
3382 enable = 0;
3383 disable = 0;
3385 while ((cmd = next_cmd(&buf))) {
3386 if (strlencmp(cmd, "lcd_enable") == 0) {
3387 enable |= TP_ACPI_VIDEO_S_LCD;
3388 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3389 disable |= TP_ACPI_VIDEO_S_LCD;
3390 } else if (strlencmp(cmd, "crt_enable") == 0) {
3391 enable |= TP_ACPI_VIDEO_S_CRT;
3392 } else if (strlencmp(cmd, "crt_disable") == 0) {
3393 disable |= TP_ACPI_VIDEO_S_CRT;
3394 } else if (video_supported == TPACPI_VIDEO_NEW &&
3395 strlencmp(cmd, "dvi_enable") == 0) {
3396 enable |= TP_ACPI_VIDEO_S_DVI;
3397 } else if (video_supported == TPACPI_VIDEO_NEW &&
3398 strlencmp(cmd, "dvi_disable") == 0) {
3399 disable |= TP_ACPI_VIDEO_S_DVI;
3400 } else if (strlencmp(cmd, "auto_enable") == 0) {
3401 res = video_autosw_set(1);
3402 if (res)
3403 return res;
3404 } else if (strlencmp(cmd, "auto_disable") == 0) {
3405 res = video_autosw_set(0);
3406 if (res)
3407 return res;
3408 } else if (strlencmp(cmd, "video_switch") == 0) {
3409 res = video_outputsw_cycle();
3410 if (res)
3411 return res;
3412 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3413 res = video_expand_toggle();
3414 if (res)
3415 return res;
3416 } else
3417 return -EINVAL;
3420 if (enable || disable) {
3421 status = video_outputsw_get();
3422 if (status < 0)
3423 return status;
3424 res = video_outputsw_set((status & ~disable) | enable);
3425 if (res)
3426 return res;
3429 return 0;
3432 static struct ibm_struct video_driver_data = {
3433 .name = "video",
3434 .read = video_read,
3435 .write = video_write,
3436 .exit = video_exit,
3439 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3441 /*************************************************************************
3442 * Light (thinklight) subdriver
3445 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3446 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3448 static int light_get_status(void)
3450 int status = 0;
3452 if (tp_features.light_status) {
3453 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3454 return -EIO;
3455 return (!!status);
3458 return -ENXIO;
3461 static int light_set_status(int status)
3463 int rc;
3465 if (tp_features.light) {
3466 if (cmos_handle) {
3467 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3468 (status)?
3469 TP_CMOS_THINKLIGHT_ON :
3470 TP_CMOS_THINKLIGHT_OFF);
3471 } else {
3472 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3473 (status)? 1 : 0);
3475 return (rc)? 0 : -EIO;
3478 return -ENXIO;
3481 static void light_set_status_worker(struct work_struct *work)
3483 struct tpacpi_led_classdev *data =
3484 container_of(work, struct tpacpi_led_classdev, work);
3486 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3487 light_set_status((data->new_state != TPACPI_LED_OFF));
3490 static void light_sysfs_set(struct led_classdev *led_cdev,
3491 enum led_brightness brightness)
3493 struct tpacpi_led_classdev *data =
3494 container_of(led_cdev,
3495 struct tpacpi_led_classdev,
3496 led_classdev);
3497 data->new_state = (brightness != LED_OFF) ?
3498 TPACPI_LED_ON : TPACPI_LED_OFF;
3499 queue_work(tpacpi_wq, &data->work);
3502 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3504 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3507 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3508 .led_classdev = {
3509 .name = "tpacpi::thinklight",
3510 .brightness_set = &light_sysfs_set,
3511 .brightness_get = &light_sysfs_get,
3515 static int __init light_init(struct ibm_init_struct *iibm)
3517 int rc;
3519 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3521 TPACPI_ACPIHANDLE_INIT(ledb);
3522 TPACPI_ACPIHANDLE_INIT(lght);
3523 TPACPI_ACPIHANDLE_INIT(cmos);
3524 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3526 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3527 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3529 if (tp_features.light)
3530 /* light status not supported on
3531 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3532 tp_features.light_status =
3533 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3535 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3536 str_supported(tp_features.light),
3537 str_supported(tp_features.light_status));
3539 if (!tp_features.light)
3540 return 1;
3542 rc = led_classdev_register(&tpacpi_pdev->dev,
3543 &tpacpi_led_thinklight.led_classdev);
3545 if (rc < 0) {
3546 tp_features.light = 0;
3547 tp_features.light_status = 0;
3548 } else {
3549 rc = 0;
3552 return rc;
3555 static void light_exit(void)
3557 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3558 if (work_pending(&tpacpi_led_thinklight.work))
3559 flush_workqueue(tpacpi_wq);
3562 static int light_read(char *p)
3564 int len = 0;
3565 int status;
3567 if (!tp_features.light) {
3568 len += sprintf(p + len, "status:\t\tnot supported\n");
3569 } else if (!tp_features.light_status) {
3570 len += sprintf(p + len, "status:\t\tunknown\n");
3571 len += sprintf(p + len, "commands:\ton, off\n");
3572 } else {
3573 status = light_get_status();
3574 if (status < 0)
3575 return status;
3576 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3577 len += sprintf(p + len, "commands:\ton, off\n");
3580 return len;
3583 static int light_write(char *buf)
3585 char *cmd;
3586 int newstatus = 0;
3588 if (!tp_features.light)
3589 return -ENODEV;
3591 while ((cmd = next_cmd(&buf))) {
3592 if (strlencmp(cmd, "on") == 0) {
3593 newstatus = 1;
3594 } else if (strlencmp(cmd, "off") == 0) {
3595 newstatus = 0;
3596 } else
3597 return -EINVAL;
3600 return light_set_status(newstatus);
3603 static struct ibm_struct light_driver_data = {
3604 .name = "light",
3605 .read = light_read,
3606 .write = light_write,
3607 .exit = light_exit,
3610 /*************************************************************************
3611 * Dock subdriver
3614 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3616 static void dock_notify(struct ibm_struct *ibm, u32 event);
3617 static int dock_read(char *p);
3618 static int dock_write(char *buf);
3620 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3621 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3622 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3623 "\\_SB.PCI.ISA.SLCE", /* 570 */
3624 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3626 /* don't list other alternatives as we install a notify handler on the 570 */
3627 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3629 static const struct acpi_device_id ibm_pci_device_ids[] = {
3630 {PCI_ROOT_HID_STRING, 0},
3631 {"", 0},
3634 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3636 .notify = dock_notify,
3637 .handle = &dock_handle,
3638 .type = ACPI_SYSTEM_NOTIFY,
3641 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3642 * We just use it to get notifications of dock hotplug
3643 * in very old thinkpads */
3644 .hid = ibm_pci_device_ids,
3645 .notify = dock_notify,
3646 .handle = &pci_handle,
3647 .type = ACPI_SYSTEM_NOTIFY,
3651 static struct ibm_struct dock_driver_data[2] = {
3653 .name = "dock",
3654 .read = dock_read,
3655 .write = dock_write,
3656 .acpi = &ibm_dock_acpidriver[0],
3659 .name = "dock",
3660 .acpi = &ibm_dock_acpidriver[1],
3664 #define dock_docked() (_sta(dock_handle) & 1)
3666 static int __init dock_init(struct ibm_init_struct *iibm)
3668 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3670 TPACPI_ACPIHANDLE_INIT(dock);
3672 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3673 str_supported(dock_handle != NULL));
3675 return (dock_handle)? 0 : 1;
3678 static int __init dock_init2(struct ibm_init_struct *iibm)
3680 int dock2_needed;
3682 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3684 if (dock_driver_data[0].flags.acpi_driver_registered &&
3685 dock_driver_data[0].flags.acpi_notify_installed) {
3686 TPACPI_ACPIHANDLE_INIT(pci);
3687 dock2_needed = (pci_handle != NULL);
3688 vdbg_printk(TPACPI_DBG_INIT,
3689 "dock PCI handler for the TP 570 is %s\n",
3690 str_supported(dock2_needed));
3691 } else {
3692 vdbg_printk(TPACPI_DBG_INIT,
3693 "dock subdriver part 2 not required\n");
3694 dock2_needed = 0;
3697 return (dock2_needed)? 0 : 1;
3700 static void dock_notify(struct ibm_struct *ibm, u32 event)
3702 int docked = dock_docked();
3703 int pci = ibm->acpi->hid && ibm->acpi->device &&
3704 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3705 int data;
3707 if (event == 1 && !pci) /* 570 */
3708 data = 1; /* button */
3709 else if (event == 1 && pci) /* 570 */
3710 data = 3; /* dock */
3711 else if (event == 3 && docked)
3712 data = 1; /* button */
3713 else if (event == 3 && !docked)
3714 data = 2; /* undock */
3715 else if (event == 0 && docked)
3716 data = 3; /* dock */
3717 else {
3718 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3719 event, _sta(dock_handle));
3720 data = 0; /* unknown */
3722 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3723 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3724 ibm->acpi->device->dev.bus_id,
3725 event, data);
3728 static int dock_read(char *p)
3730 int len = 0;
3731 int docked = dock_docked();
3733 if (!dock_handle)
3734 len += sprintf(p + len, "status:\t\tnot supported\n");
3735 else if (!docked)
3736 len += sprintf(p + len, "status:\t\tundocked\n");
3737 else {
3738 len += sprintf(p + len, "status:\t\tdocked\n");
3739 len += sprintf(p + len, "commands:\tdock, undock\n");
3742 return len;
3745 static int dock_write(char *buf)
3747 char *cmd;
3749 if (!dock_docked())
3750 return -ENODEV;
3752 while ((cmd = next_cmd(&buf))) {
3753 if (strlencmp(cmd, "undock") == 0) {
3754 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3755 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3756 return -EIO;
3757 } else if (strlencmp(cmd, "dock") == 0) {
3758 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3759 return -EIO;
3760 } else
3761 return -EINVAL;
3764 return 0;
3767 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3769 /*************************************************************************
3770 * Bay subdriver
3773 #ifdef CONFIG_THINKPAD_ACPI_BAY
3775 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3776 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3777 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3778 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3779 ); /* A21e, R30, R31 */
3780 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3781 "_EJ0", /* all others */
3782 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3783 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3784 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3785 ); /* all others */
3786 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3787 "_EJ0", /* 770x */
3788 ); /* all others */
3790 static int __init bay_init(struct ibm_init_struct *iibm)
3792 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3794 TPACPI_ACPIHANDLE_INIT(bay);
3795 if (bay_handle)
3796 TPACPI_ACPIHANDLE_INIT(bay_ej);
3797 TPACPI_ACPIHANDLE_INIT(bay2);
3798 if (bay2_handle)
3799 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3801 tp_features.bay_status = bay_handle &&
3802 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3803 tp_features.bay_status2 = bay2_handle &&
3804 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3806 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3807 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3808 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3809 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3811 vdbg_printk(TPACPI_DBG_INIT,
3812 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3813 str_supported(tp_features.bay_status),
3814 str_supported(tp_features.bay_eject),
3815 str_supported(tp_features.bay_status2),
3816 str_supported(tp_features.bay_eject2));
3818 return (tp_features.bay_status || tp_features.bay_eject ||
3819 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3822 static void bay_notify(struct ibm_struct *ibm, u32 event)
3824 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3825 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3826 ibm->acpi->device->dev.bus_id,
3827 event, 0);
3830 #define bay_occupied(b) (_sta(b##_handle) & 1)
3832 static int bay_read(char *p)
3834 int len = 0;
3835 int occupied = bay_occupied(bay);
3836 int occupied2 = bay_occupied(bay2);
3837 int eject, eject2;
3839 len += sprintf(p + len, "status:\t\t%s\n",
3840 tp_features.bay_status ?
3841 (occupied ? "occupied" : "unoccupied") :
3842 "not supported");
3843 if (tp_features.bay_status2)
3844 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3845 "occupied" : "unoccupied");
3847 eject = tp_features.bay_eject && occupied;
3848 eject2 = tp_features.bay_eject2 && occupied2;
3850 if (eject && eject2)
3851 len += sprintf(p + len, "commands:\teject, eject2\n");
3852 else if (eject)
3853 len += sprintf(p + len, "commands:\teject\n");
3854 else if (eject2)
3855 len += sprintf(p + len, "commands:\teject2\n");
3857 return len;
3860 static int bay_write(char *buf)
3862 char *cmd;
3864 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3865 return -ENODEV;
3867 while ((cmd = next_cmd(&buf))) {
3868 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3869 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3870 return -EIO;
3871 } else if (tp_features.bay_eject2 &&
3872 strlencmp(cmd, "eject2") == 0) {
3873 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3874 return -EIO;
3875 } else
3876 return -EINVAL;
3879 return 0;
3882 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3883 .notify = bay_notify,
3884 .handle = &bay_handle,
3885 .type = ACPI_SYSTEM_NOTIFY,
3888 static struct ibm_struct bay_driver_data = {
3889 .name = "bay",
3890 .read = bay_read,
3891 .write = bay_write,
3892 .acpi = &ibm_bay_acpidriver,
3895 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3897 /*************************************************************************
3898 * CMOS subdriver
3901 /* sysfs cmos_command -------------------------------------------------- */
3902 static ssize_t cmos_command_store(struct device *dev,
3903 struct device_attribute *attr,
3904 const char *buf, size_t count)
3906 unsigned long cmos_cmd;
3907 int res;
3909 if (parse_strtoul(buf, 21, &cmos_cmd))
3910 return -EINVAL;
3912 res = issue_thinkpad_cmos_command(cmos_cmd);
3913 return (res)? res : count;
3916 static struct device_attribute dev_attr_cmos_command =
3917 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3919 /* --------------------------------------------------------------------- */
3921 static int __init cmos_init(struct ibm_init_struct *iibm)
3923 int res;
3925 vdbg_printk(TPACPI_DBG_INIT,
3926 "initializing cmos commands subdriver\n");
3928 TPACPI_ACPIHANDLE_INIT(cmos);
3930 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3931 str_supported(cmos_handle != NULL));
3933 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3934 if (res)
3935 return res;
3937 return (cmos_handle)? 0 : 1;
3940 static void cmos_exit(void)
3942 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3945 static int cmos_read(char *p)
3947 int len = 0;
3949 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3950 R30, R31, T20-22, X20-21 */
3951 if (!cmos_handle)
3952 len += sprintf(p + len, "status:\t\tnot supported\n");
3953 else {
3954 len += sprintf(p + len, "status:\t\tsupported\n");
3955 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3958 return len;
3961 static int cmos_write(char *buf)
3963 char *cmd;
3964 int cmos_cmd, res;
3966 while ((cmd = next_cmd(&buf))) {
3967 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3968 cmos_cmd >= 0 && cmos_cmd <= 21) {
3969 /* cmos_cmd set */
3970 } else
3971 return -EINVAL;
3973 res = issue_thinkpad_cmos_command(cmos_cmd);
3974 if (res)
3975 return res;
3978 return 0;
3981 static struct ibm_struct cmos_driver_data = {
3982 .name = "cmos",
3983 .read = cmos_read,
3984 .write = cmos_write,
3985 .exit = cmos_exit,
3988 /*************************************************************************
3989 * LED subdriver
3992 enum led_access_mode {
3993 TPACPI_LED_NONE = 0,
3994 TPACPI_LED_570, /* 570 */
3995 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3996 TPACPI_LED_NEW, /* all others */
3999 enum { /* For TPACPI_LED_OLD */
4000 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4001 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4002 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4005 static enum led_access_mode led_supported;
4007 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4008 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4009 /* T20-22, X20-21 */
4010 "LED", /* all others */
4011 ); /* R30, R31 */
4013 #define TPACPI_LED_NUMLEDS 8
4014 static struct tpacpi_led_classdev *tpacpi_leds;
4015 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4016 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4017 /* there's a limit of 19 chars + NULL before 2.6.26 */
4018 "tpacpi::power",
4019 "tpacpi:orange:batt",
4020 "tpacpi:green:batt",
4021 "tpacpi::dock_active",
4022 "tpacpi::bay_active",
4023 "tpacpi::dock_batt",
4024 "tpacpi::unknown_led",
4025 "tpacpi::standby",
4028 static int led_get_status(const unsigned int led)
4030 int status;
4031 enum led_status_t led_s;
4033 switch (led_supported) {
4034 case TPACPI_LED_570:
4035 if (!acpi_evalf(ec_handle,
4036 &status, "GLED", "dd", 1 << led))
4037 return -EIO;
4038 led_s = (status == 0)?
4039 TPACPI_LED_OFF :
4040 ((status == 1)?
4041 TPACPI_LED_ON :
4042 TPACPI_LED_BLINK);
4043 tpacpi_led_state_cache[led] = led_s;
4044 return led_s;
4045 default:
4046 return -ENXIO;
4049 /* not reached */
4052 static int led_set_status(const unsigned int led,
4053 const enum led_status_t ledstatus)
4055 /* off, on, blink. Index is led_status_t */
4056 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4057 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4059 int rc = 0;
4061 switch (led_supported) {
4062 case TPACPI_LED_570:
4063 /* 570 */
4064 if (led > 7)
4065 return -EINVAL;
4066 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4067 (1 << led), led_sled_arg1[ledstatus]))
4068 rc = -EIO;
4069 break;
4070 case TPACPI_LED_OLD:
4071 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4072 if (led > 7)
4073 return -EINVAL;
4074 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4075 if (rc >= 0)
4076 rc = ec_write(TPACPI_LED_EC_HLBL,
4077 (ledstatus == TPACPI_LED_BLINK) << led);
4078 if (rc >= 0)
4079 rc = ec_write(TPACPI_LED_EC_HLCL,
4080 (ledstatus != TPACPI_LED_OFF) << led);
4081 break;
4082 case TPACPI_LED_NEW:
4083 /* all others */
4084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4085 led, led_led_arg1[ledstatus]))
4086 rc = -EIO;
4087 break;
4088 default:
4089 rc = -ENXIO;
4092 if (!rc)
4093 tpacpi_led_state_cache[led] = ledstatus;
4095 return rc;
4098 static void led_set_status_worker(struct work_struct *work)
4100 struct tpacpi_led_classdev *data =
4101 container_of(work, struct tpacpi_led_classdev, work);
4103 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4104 led_set_status(data->led, data->new_state);
4107 static void led_sysfs_set(struct led_classdev *led_cdev,
4108 enum led_brightness brightness)
4110 struct tpacpi_led_classdev *data = container_of(led_cdev,
4111 struct tpacpi_led_classdev, led_classdev);
4113 if (brightness == LED_OFF)
4114 data->new_state = TPACPI_LED_OFF;
4115 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4116 data->new_state = TPACPI_LED_ON;
4117 else
4118 data->new_state = TPACPI_LED_BLINK;
4120 queue_work(tpacpi_wq, &data->work);
4123 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4124 unsigned long *delay_on, unsigned long *delay_off)
4126 struct tpacpi_led_classdev *data = container_of(led_cdev,
4127 struct tpacpi_led_classdev, led_classdev);
4129 /* Can we choose the flash rate? */
4130 if (*delay_on == 0 && *delay_off == 0) {
4131 /* yes. set them to the hardware blink rate (1 Hz) */
4132 *delay_on = 500; /* ms */
4133 *delay_off = 500; /* ms */
4134 } else if ((*delay_on != 500) || (*delay_off != 500))
4135 return -EINVAL;
4137 data->new_state = TPACPI_LED_BLINK;
4138 queue_work(tpacpi_wq, &data->work);
4140 return 0;
4143 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4145 int rc;
4147 struct tpacpi_led_classdev *data = container_of(led_cdev,
4148 struct tpacpi_led_classdev, led_classdev);
4150 rc = led_get_status(data->led);
4152 if (rc == TPACPI_LED_OFF || rc < 0)
4153 rc = LED_OFF; /* no error handling in led class :( */
4154 else
4155 rc = LED_FULL;
4157 return rc;
4160 static void led_exit(void)
4162 unsigned int i;
4164 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4165 if (tpacpi_leds[i].led_classdev.name)
4166 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4169 kfree(tpacpi_leds);
4172 static int __init led_init(struct ibm_init_struct *iibm)
4174 unsigned int i;
4175 int rc;
4177 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4179 TPACPI_ACPIHANDLE_INIT(led);
4181 if (!led_handle)
4182 /* led not supported on R30, R31 */
4183 led_supported = TPACPI_LED_NONE;
4184 else if (strlencmp(led_path, "SLED") == 0)
4185 /* 570 */
4186 led_supported = TPACPI_LED_570;
4187 else if (strlencmp(led_path, "SYSL") == 0)
4188 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4189 led_supported = TPACPI_LED_OLD;
4190 else
4191 /* all others */
4192 led_supported = TPACPI_LED_NEW;
4194 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4195 str_supported(led_supported), led_supported);
4197 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4198 GFP_KERNEL);
4199 if (!tpacpi_leds) {
4200 printk(TPACPI_ERR "Out of memory for LED data\n");
4201 return -ENOMEM;
4204 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4205 tpacpi_leds[i].led = i;
4207 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4208 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4209 if (led_supported == TPACPI_LED_570)
4210 tpacpi_leds[i].led_classdev.brightness_get =
4211 &led_sysfs_get;
4213 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4215 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4217 rc = led_classdev_register(&tpacpi_pdev->dev,
4218 &tpacpi_leds[i].led_classdev);
4219 if (rc < 0) {
4220 tpacpi_leds[i].led_classdev.name = NULL;
4221 led_exit();
4222 return rc;
4226 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4229 #define str_led_status(s) \
4230 ((s) == TPACPI_LED_OFF ? "off" : \
4231 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4233 static int led_read(char *p)
4235 int len = 0;
4237 if (!led_supported) {
4238 len += sprintf(p + len, "status:\t\tnot supported\n");
4239 return len;
4241 len += sprintf(p + len, "status:\t\tsupported\n");
4243 if (led_supported == TPACPI_LED_570) {
4244 /* 570 */
4245 int i, status;
4246 for (i = 0; i < 8; i++) {
4247 status = led_get_status(i);
4248 if (status < 0)
4249 return -EIO;
4250 len += sprintf(p + len, "%d:\t\t%s\n",
4251 i, str_led_status(status));
4255 len += sprintf(p + len, "commands:\t"
4256 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4258 return len;
4261 static int led_write(char *buf)
4263 char *cmd;
4264 int led, rc;
4265 enum led_status_t s;
4267 if (!led_supported)
4268 return -ENODEV;
4270 while ((cmd = next_cmd(&buf))) {
4271 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4272 return -EINVAL;
4274 if (strstr(cmd, "off")) {
4275 s = TPACPI_LED_OFF;
4276 } else if (strstr(cmd, "on")) {
4277 s = TPACPI_LED_ON;
4278 } else if (strstr(cmd, "blink")) {
4279 s = TPACPI_LED_BLINK;
4280 } else {
4281 return -EINVAL;
4284 rc = led_set_status(led, s);
4285 if (rc < 0)
4286 return rc;
4289 return 0;
4292 static struct ibm_struct led_driver_data = {
4293 .name = "led",
4294 .read = led_read,
4295 .write = led_write,
4296 .exit = led_exit,
4299 /*************************************************************************
4300 * Beep subdriver
4303 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4305 static int __init beep_init(struct ibm_init_struct *iibm)
4307 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4309 TPACPI_ACPIHANDLE_INIT(beep);
4311 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4312 str_supported(beep_handle != NULL));
4314 return (beep_handle)? 0 : 1;
4317 static int beep_read(char *p)
4319 int len = 0;
4321 if (!beep_handle)
4322 len += sprintf(p + len, "status:\t\tnot supported\n");
4323 else {
4324 len += sprintf(p + len, "status:\t\tsupported\n");
4325 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4328 return len;
4331 static int beep_write(char *buf)
4333 char *cmd;
4334 int beep_cmd;
4336 if (!beep_handle)
4337 return -ENODEV;
4339 while ((cmd = next_cmd(&buf))) {
4340 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4341 beep_cmd >= 0 && beep_cmd <= 17) {
4342 /* beep_cmd set */
4343 } else
4344 return -EINVAL;
4345 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4346 return -EIO;
4349 return 0;
4352 static struct ibm_struct beep_driver_data = {
4353 .name = "beep",
4354 .read = beep_read,
4355 .write = beep_write,
4358 /*************************************************************************
4359 * Thermal subdriver
4362 enum thermal_access_mode {
4363 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4364 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4365 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4366 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4367 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4370 enum { /* TPACPI_THERMAL_TPEC_* */
4371 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4372 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4373 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4376 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4377 struct ibm_thermal_sensors_struct {
4378 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4381 static enum thermal_access_mode thermal_read_mode;
4383 /* idx is zero-based */
4384 static int thermal_get_sensor(int idx, s32 *value)
4386 int t;
4387 s8 tmp;
4388 char tmpi[5];
4390 t = TP_EC_THERMAL_TMP0;
4392 switch (thermal_read_mode) {
4393 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4394 case TPACPI_THERMAL_TPEC_16:
4395 if (idx >= 8 && idx <= 15) {
4396 t = TP_EC_THERMAL_TMP8;
4397 idx -= 8;
4399 /* fallthrough */
4400 #endif
4401 case TPACPI_THERMAL_TPEC_8:
4402 if (idx <= 7) {
4403 if (!acpi_ec_read(t + idx, &tmp))
4404 return -EIO;
4405 *value = tmp * 1000;
4406 return 0;
4408 break;
4410 case TPACPI_THERMAL_ACPI_UPDT:
4411 if (idx <= 7) {
4412 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4413 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4414 return -EIO;
4415 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4416 return -EIO;
4417 *value = (t - 2732) * 100;
4418 return 0;
4420 break;
4422 case TPACPI_THERMAL_ACPI_TMP07:
4423 if (idx <= 7) {
4424 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4425 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4426 return -EIO;
4427 if (t > 127 || t < -127)
4428 t = TP_EC_THERMAL_TMP_NA;
4429 *value = t * 1000;
4430 return 0;
4432 break;
4434 case TPACPI_THERMAL_NONE:
4435 default:
4436 return -ENOSYS;
4439 return -EINVAL;
4442 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4444 int res, i;
4445 int n;
4447 n = 8;
4448 i = 0;
4450 if (!s)
4451 return -EINVAL;
4453 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4454 n = 16;
4456 for (i = 0 ; i < n; i++) {
4457 res = thermal_get_sensor(i, &s->temp[i]);
4458 if (res)
4459 return res;
4462 return n;
4465 /* sysfs temp##_input -------------------------------------------------- */
4467 static ssize_t thermal_temp_input_show(struct device *dev,
4468 struct device_attribute *attr,
4469 char *buf)
4471 struct sensor_device_attribute *sensor_attr =
4472 to_sensor_dev_attr(attr);
4473 int idx = sensor_attr->index;
4474 s32 value;
4475 int res;
4477 res = thermal_get_sensor(idx, &value);
4478 if (res)
4479 return res;
4480 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4481 return -ENXIO;
4483 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4486 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4487 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4488 thermal_temp_input_show, NULL, _idxB)
4490 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4491 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4492 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4493 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4494 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4495 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4496 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4497 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4498 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4499 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4500 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4501 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4502 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4503 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4504 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4505 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4506 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4509 #define THERMAL_ATTRS(X) \
4510 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4512 static struct attribute *thermal_temp_input_attr[] = {
4513 THERMAL_ATTRS(8),
4514 THERMAL_ATTRS(9),
4515 THERMAL_ATTRS(10),
4516 THERMAL_ATTRS(11),
4517 THERMAL_ATTRS(12),
4518 THERMAL_ATTRS(13),
4519 THERMAL_ATTRS(14),
4520 THERMAL_ATTRS(15),
4521 THERMAL_ATTRS(0),
4522 THERMAL_ATTRS(1),
4523 THERMAL_ATTRS(2),
4524 THERMAL_ATTRS(3),
4525 THERMAL_ATTRS(4),
4526 THERMAL_ATTRS(5),
4527 THERMAL_ATTRS(6),
4528 THERMAL_ATTRS(7),
4529 NULL
4532 static const struct attribute_group thermal_temp_input16_group = {
4533 .attrs = thermal_temp_input_attr
4536 static const struct attribute_group thermal_temp_input8_group = {
4537 .attrs = &thermal_temp_input_attr[8]
4540 #undef THERMAL_SENSOR_ATTR_TEMP
4541 #undef THERMAL_ATTRS
4543 /* --------------------------------------------------------------------- */
4545 static int __init thermal_init(struct ibm_init_struct *iibm)
4547 u8 t, ta1, ta2;
4548 int i;
4549 int acpi_tmp7;
4550 int res;
4552 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4554 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4556 if (thinkpad_id.ec_model) {
4558 * Direct EC access mode: sensors at registers
4559 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4560 * non-implemented, thermal sensors return 0x80 when
4561 * not available
4564 ta1 = ta2 = 0;
4565 for (i = 0; i < 8; i++) {
4566 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4567 ta1 |= t;
4568 } else {
4569 ta1 = 0;
4570 break;
4572 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4573 ta2 |= t;
4574 } else {
4575 ta1 = 0;
4576 break;
4579 if (ta1 == 0) {
4580 /* This is sheer paranoia, but we handle it anyway */
4581 if (acpi_tmp7) {
4582 printk(TPACPI_ERR
4583 "ThinkPad ACPI EC access misbehaving, "
4584 "falling back to ACPI TMPx access "
4585 "mode\n");
4586 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4587 } else {
4588 printk(TPACPI_ERR
4589 "ThinkPad ACPI EC access misbehaving, "
4590 "disabling thermal sensors access\n");
4591 thermal_read_mode = TPACPI_THERMAL_NONE;
4593 } else {
4594 thermal_read_mode =
4595 (ta2 != 0) ?
4596 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4598 } else if (acpi_tmp7) {
4599 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4600 /* 600e/x, 770e, 770x */
4601 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4602 } else {
4603 /* Standard ACPI TMPx access, max 8 sensors */
4604 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4606 } else {
4607 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4608 thermal_read_mode = TPACPI_THERMAL_NONE;
4611 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4612 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4613 thermal_read_mode);
4615 switch (thermal_read_mode) {
4616 case TPACPI_THERMAL_TPEC_16:
4617 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4618 &thermal_temp_input16_group);
4619 if (res)
4620 return res;
4621 break;
4622 case TPACPI_THERMAL_TPEC_8:
4623 case TPACPI_THERMAL_ACPI_TMP07:
4624 case TPACPI_THERMAL_ACPI_UPDT:
4625 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4626 &thermal_temp_input8_group);
4627 if (res)
4628 return res;
4629 break;
4630 case TPACPI_THERMAL_NONE:
4631 default:
4632 return 1;
4635 return 0;
4638 static void thermal_exit(void)
4640 switch (thermal_read_mode) {
4641 case TPACPI_THERMAL_TPEC_16:
4642 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4643 &thermal_temp_input16_group);
4644 break;
4645 case TPACPI_THERMAL_TPEC_8:
4646 case TPACPI_THERMAL_ACPI_TMP07:
4647 case TPACPI_THERMAL_ACPI_UPDT:
4648 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4649 &thermal_temp_input16_group);
4650 break;
4651 case TPACPI_THERMAL_NONE:
4652 default:
4653 break;
4657 static int thermal_read(char *p)
4659 int len = 0;
4660 int n, i;
4661 struct ibm_thermal_sensors_struct t;
4663 n = thermal_get_sensors(&t);
4664 if (unlikely(n < 0))
4665 return n;
4667 len += sprintf(p + len, "temperatures:\t");
4669 if (n > 0) {
4670 for (i = 0; i < (n - 1); i++)
4671 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4672 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4673 } else
4674 len += sprintf(p + len, "not supported\n");
4676 return len;
4679 static struct ibm_struct thermal_driver_data = {
4680 .name = "thermal",
4681 .read = thermal_read,
4682 .exit = thermal_exit,
4685 /*************************************************************************
4686 * EC Dump subdriver
4689 static u8 ecdump_regs[256];
4691 static int ecdump_read(char *p)
4693 int len = 0;
4694 int i, j;
4695 u8 v;
4697 len += sprintf(p + len, "EC "
4698 " +00 +01 +02 +03 +04 +05 +06 +07"
4699 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4700 for (i = 0; i < 256; i += 16) {
4701 len += sprintf(p + len, "EC 0x%02x:", i);
4702 for (j = 0; j < 16; j++) {
4703 if (!acpi_ec_read(i + j, &v))
4704 break;
4705 if (v != ecdump_regs[i + j])
4706 len += sprintf(p + len, " *%02x", v);
4707 else
4708 len += sprintf(p + len, " %02x", v);
4709 ecdump_regs[i + j] = v;
4711 len += sprintf(p + len, "\n");
4712 if (j != 16)
4713 break;
4716 /* These are way too dangerous to advertise openly... */
4717 #if 0
4718 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4719 " (<offset> is 00-ff, <value> is 00-ff)\n");
4720 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4721 " (<offset> is 00-ff, <value> is 0-255)\n");
4722 #endif
4723 return len;
4726 static int ecdump_write(char *buf)
4728 char *cmd;
4729 int i, v;
4731 while ((cmd = next_cmd(&buf))) {
4732 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4733 /* i and v set */
4734 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4735 /* i and v set */
4736 } else
4737 return -EINVAL;
4738 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4739 if (!acpi_ec_write(i, v))
4740 return -EIO;
4741 } else
4742 return -EINVAL;
4745 return 0;
4748 static struct ibm_struct ecdump_driver_data = {
4749 .name = "ecdump",
4750 .read = ecdump_read,
4751 .write = ecdump_write,
4752 .flags.experimental = 1,
4755 /*************************************************************************
4756 * Backlight/brightness subdriver
4759 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4761 enum {
4762 TP_EC_BACKLIGHT = 0x31,
4764 /* TP_EC_BACKLIGHT bitmasks */
4765 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4766 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4767 TP_EC_BACKLIGHT_MAPSW = 0x20,
4770 static struct backlight_device *ibm_backlight_device;
4771 static int brightness_mode;
4772 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4774 static struct mutex brightness_mutex;
4777 * ThinkPads can read brightness from two places: EC 0x31, or
4778 * CMOS NVRAM byte 0x5E, bits 0-3.
4780 * EC 0x31 has the following layout
4781 * Bit 7: unknown function
4782 * Bit 6: unknown function
4783 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4784 * Bit 4: must be set to zero to avoid problems
4785 * Bit 3-0: backlight brightness level
4787 * brightness_get_raw returns status data in the EC 0x31 layout
4789 static int brightness_get_raw(int *status)
4791 u8 lec = 0, lcmos = 0, level = 0;
4793 if (brightness_mode & 1) {
4794 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4795 return -EIO;
4796 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4798 if (brightness_mode & 2) {
4799 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4800 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4801 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4802 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4803 level = lcmos;
4806 if (brightness_mode == 3) {
4807 *status = lec; /* Prefer EC, CMOS is just a backing store */
4808 lec &= TP_EC_BACKLIGHT_LVLMSK;
4809 if (lec == lcmos)
4810 tp_warned.bright_cmos_ec_unsync = 0;
4811 else {
4812 if (!tp_warned.bright_cmos_ec_unsync) {
4813 printk(TPACPI_ERR
4814 "CMOS NVRAM (%u) and EC (%u) do not "
4815 "agree on display brightness level\n",
4816 (unsigned int) lcmos,
4817 (unsigned int) lec);
4818 tp_warned.bright_cmos_ec_unsync = 1;
4820 return -EIO;
4822 } else {
4823 *status = level;
4826 return 0;
4829 /* May return EINTR which can always be mapped to ERESTARTSYS */
4830 static int brightness_set(int value)
4832 int cmos_cmd, inc, i, res;
4833 int current_value;
4834 int command_bits;
4836 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4837 value < 0)
4838 return -EINVAL;
4840 res = mutex_lock_interruptible(&brightness_mutex);
4841 if (res < 0)
4842 return res;
4844 res = brightness_get_raw(&current_value);
4845 if (res < 0)
4846 goto errout;
4848 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4849 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4851 cmos_cmd = value > current_value ?
4852 TP_CMOS_BRIGHTNESS_UP :
4853 TP_CMOS_BRIGHTNESS_DOWN;
4854 inc = (value > current_value)? 1 : -1;
4856 res = 0;
4857 for (i = current_value; i != value; i += inc) {
4858 if ((brightness_mode & 2) &&
4859 issue_thinkpad_cmos_command(cmos_cmd)) {
4860 res = -EIO;
4861 goto errout;
4863 if ((brightness_mode & 1) &&
4864 !acpi_ec_write(TP_EC_BACKLIGHT,
4865 (i + inc) | command_bits)) {
4866 res = -EIO;
4867 goto errout;;
4871 errout:
4872 mutex_unlock(&brightness_mutex);
4873 return res;
4876 /* sysfs backlight class ----------------------------------------------- */
4878 static int brightness_update_status(struct backlight_device *bd)
4880 /* it is the backlight class's job (caller) to handle
4881 * EINTR and other errors properly */
4882 return brightness_set(
4883 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4884 bd->props.power == FB_BLANK_UNBLANK) ?
4885 bd->props.brightness : 0);
4888 static int brightness_get(struct backlight_device *bd)
4890 int status, res;
4892 res = brightness_get_raw(&status);
4893 if (res < 0)
4894 return 0; /* FIXME: teach backlight about error handling */
4896 return status & TP_EC_BACKLIGHT_LVLMSK;
4899 static struct backlight_ops ibm_backlight_data = {
4900 .get_brightness = brightness_get,
4901 .update_status = brightness_update_status,
4904 /* --------------------------------------------------------------------- */
4906 static int __init brightness_init(struct ibm_init_struct *iibm)
4908 int b;
4910 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4912 mutex_init(&brightness_mutex);
4915 * We always attempt to detect acpi support, so as to switch
4916 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4917 * going to publish a backlight interface
4919 b = tpacpi_check_std_acpi_brightness_support();
4920 if (b > 0) {
4921 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4922 printk(TPACPI_NOTICE
4923 "Lenovo BIOS switched to ACPI backlight "
4924 "control mode\n");
4926 if (brightness_enable > 1) {
4927 printk(TPACPI_NOTICE
4928 "standard ACPI backlight interface "
4929 "available, not loading native one...\n");
4930 return 1;
4934 if (!brightness_enable) {
4935 dbg_printk(TPACPI_DBG_INIT,
4936 "brightness support disabled by "
4937 "module parameter\n");
4938 return 1;
4941 if (b > 16) {
4942 printk(TPACPI_ERR
4943 "Unsupported brightness interface, "
4944 "please contact %s\n", TPACPI_MAIL);
4945 return 1;
4947 if (b == 16)
4948 tp_features.bright_16levels = 1;
4950 if (!brightness_mode) {
4951 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4952 brightness_mode = 2;
4953 else
4954 brightness_mode = 3;
4956 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4957 brightness_mode);
4960 if (brightness_mode > 3)
4961 return -EINVAL;
4963 if (brightness_get_raw(&b) < 0)
4964 return 1;
4966 if (tp_features.bright_16levels)
4967 printk(TPACPI_INFO
4968 "detected a 16-level brightness capable ThinkPad\n");
4970 ibm_backlight_device = backlight_device_register(
4971 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4972 &ibm_backlight_data);
4973 if (IS_ERR(ibm_backlight_device)) {
4974 printk(TPACPI_ERR "Could not register backlight device\n");
4975 return PTR_ERR(ibm_backlight_device);
4977 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4979 ibm_backlight_device->props.max_brightness =
4980 (tp_features.bright_16levels)? 15 : 7;
4981 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4982 backlight_update_status(ibm_backlight_device);
4984 return 0;
4987 static void brightness_exit(void)
4989 if (ibm_backlight_device) {
4990 vdbg_printk(TPACPI_DBG_EXIT,
4991 "calling backlight_device_unregister()\n");
4992 backlight_device_unregister(ibm_backlight_device);
4996 static int brightness_read(char *p)
4998 int len = 0;
4999 int level;
5001 level = brightness_get(NULL);
5002 if (level < 0) {
5003 len += sprintf(p + len, "level:\t\tunreadable\n");
5004 } else {
5005 len += sprintf(p + len, "level:\t\t%d\n", level);
5006 len += sprintf(p + len, "commands:\tup, down\n");
5007 len += sprintf(p + len, "commands:\tlevel <level>"
5008 " (<level> is 0-%d)\n",
5009 (tp_features.bright_16levels) ? 15 : 7);
5012 return len;
5015 static int brightness_write(char *buf)
5017 int level;
5018 int rc;
5019 char *cmd;
5020 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5022 level = brightness_get(NULL);
5023 if (level < 0)
5024 return level;
5026 while ((cmd = next_cmd(&buf))) {
5027 if (strlencmp(cmd, "up") == 0) {
5028 if (level < max_level)
5029 level++;
5030 } else if (strlencmp(cmd, "down") == 0) {
5031 if (level > 0)
5032 level--;
5033 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5034 level >= 0 && level <= max_level) {
5035 /* new level set */
5036 } else
5037 return -EINVAL;
5041 * Now we know what the final level should be, so we try to set it.
5042 * Doing it this way makes the syscall restartable in case of EINTR
5044 rc = brightness_set(level);
5045 return (rc == -EINTR)? -ERESTARTSYS : rc;
5048 static struct ibm_struct brightness_driver_data = {
5049 .name = "brightness",
5050 .read = brightness_read,
5051 .write = brightness_write,
5052 .exit = brightness_exit,
5055 /*************************************************************************
5056 * Volume subdriver
5059 static int volume_offset = 0x30;
5061 static int volume_read(char *p)
5063 int len = 0;
5064 u8 level;
5066 if (!acpi_ec_read(volume_offset, &level)) {
5067 len += sprintf(p + len, "level:\t\tunreadable\n");
5068 } else {
5069 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5070 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5071 len += sprintf(p + len, "commands:\tup, down, mute\n");
5072 len += sprintf(p + len, "commands:\tlevel <level>"
5073 " (<level> is 0-15)\n");
5076 return len;
5079 static int volume_write(char *buf)
5081 int cmos_cmd, inc, i;
5082 u8 level, mute;
5083 int new_level, new_mute;
5084 char *cmd;
5086 while ((cmd = next_cmd(&buf))) {
5087 if (!acpi_ec_read(volume_offset, &level))
5088 return -EIO;
5089 new_mute = mute = level & 0x40;
5090 new_level = level = level & 0xf;
5092 if (strlencmp(cmd, "up") == 0) {
5093 if (mute)
5094 new_mute = 0;
5095 else
5096 new_level = level == 15 ? 15 : level + 1;
5097 } else if (strlencmp(cmd, "down") == 0) {
5098 if (mute)
5099 new_mute = 0;
5100 else
5101 new_level = level == 0 ? 0 : level - 1;
5102 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5103 new_level >= 0 && new_level <= 15) {
5104 /* new_level set */
5105 } else if (strlencmp(cmd, "mute") == 0) {
5106 new_mute = 0x40;
5107 } else
5108 return -EINVAL;
5110 if (new_level != level) {
5111 /* mute doesn't change */
5113 cmos_cmd = (new_level > level) ?
5114 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5115 inc = new_level > level ? 1 : -1;
5117 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5118 !acpi_ec_write(volume_offset, level)))
5119 return -EIO;
5121 for (i = level; i != new_level; i += inc)
5122 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5123 !acpi_ec_write(volume_offset, i + inc))
5124 return -EIO;
5126 if (mute &&
5127 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5128 !acpi_ec_write(volume_offset, new_level + mute))) {
5129 return -EIO;
5133 if (new_mute != mute) {
5134 /* level doesn't change */
5136 cmos_cmd = (new_mute) ?
5137 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5139 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5140 !acpi_ec_write(volume_offset, level + new_mute))
5141 return -EIO;
5145 return 0;
5148 static struct ibm_struct volume_driver_data = {
5149 .name = "volume",
5150 .read = volume_read,
5151 .write = volume_write,
5154 /*************************************************************************
5155 * Fan subdriver
5159 * FAN ACCESS MODES
5161 * TPACPI_FAN_RD_ACPI_GFAN:
5162 * ACPI GFAN method: returns fan level
5164 * see TPACPI_FAN_WR_ACPI_SFAN
5165 * EC 0x2f (HFSP) not available if GFAN exists
5167 * TPACPI_FAN_WR_ACPI_SFAN:
5168 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5170 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5171 * it for writing.
5173 * TPACPI_FAN_WR_TPEC:
5174 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5175 * Supported on almost all ThinkPads
5177 * Fan speed changes of any sort (including those caused by the
5178 * disengaged mode) are usually done slowly by the firmware as the
5179 * maximum ammount of fan duty cycle change per second seems to be
5180 * limited.
5182 * Reading is not available if GFAN exists.
5183 * Writing is not available if SFAN exists.
5185 * Bits
5186 * 7 automatic mode engaged;
5187 * (default operation mode of the ThinkPad)
5188 * fan level is ignored in this mode.
5189 * 6 full speed mode (takes precedence over bit 7);
5190 * not available on all thinkpads. May disable
5191 * the tachometer while the fan controller ramps up
5192 * the speed (which can take up to a few *minutes*).
5193 * Speeds up fan to 100% duty-cycle, which is far above
5194 * the standard RPM levels. It is not impossible that
5195 * it could cause hardware damage.
5196 * 5-3 unused in some models. Extra bits for fan level
5197 * in others, but still useless as all values above
5198 * 7 map to the same speed as level 7 in these models.
5199 * 2-0 fan level (0..7 usually)
5200 * 0x00 = stop
5201 * 0x07 = max (set when temperatures critical)
5202 * Some ThinkPads may have other levels, see
5203 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5205 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5206 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5207 * does so, its initial value is meaningless (0x07).
5209 * For firmware bugs, refer to:
5210 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5212 * ----
5214 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5215 * Main fan tachometer reading (in RPM)
5217 * This register is present on all ThinkPads with a new-style EC, and
5218 * it is known not to be present on the A21m/e, and T22, as there is
5219 * something else in offset 0x84 according to the ACPI DSDT. Other
5220 * ThinkPads from this same time period (and earlier) probably lack the
5221 * tachometer as well.
5223 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5224 * was never fixed by IBM to report the EC firmware version string
5225 * probably support the tachometer (like the early X models), so
5226 * detecting it is quite hard. We need more data to know for sure.
5228 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5229 * might result.
5231 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5232 * mode.
5234 * For firmware bugs, refer to:
5235 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5237 * TPACPI_FAN_WR_ACPI_FANS:
5238 * ThinkPad X31, X40, X41. Not available in the X60.
5240 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5241 * high speed. ACPI DSDT seems to map these three speeds to levels
5242 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5243 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5245 * The speeds are stored on handles
5246 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5248 * There are three default speed sets, acessible as handles:
5249 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5251 * ACPI DSDT switches which set is in use depending on various
5252 * factors.
5254 * TPACPI_FAN_WR_TPEC is also available and should be used to
5255 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5256 * but the ACPI tables just mention level 7.
5259 enum { /* Fan control constants */
5260 fan_status_offset = 0x2f, /* EC register 0x2f */
5261 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5262 * 0x84 must be read before 0x85 */
5264 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5265 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5267 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5270 enum fan_status_access_mode {
5271 TPACPI_FAN_NONE = 0, /* No fan status or control */
5272 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5273 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5276 enum fan_control_access_mode {
5277 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5278 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5279 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5280 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5283 enum fan_control_commands {
5284 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5285 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5286 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5287 * and also watchdog cmd */
5290 static int fan_control_allowed;
5292 static enum fan_status_access_mode fan_status_access_mode;
5293 static enum fan_control_access_mode fan_control_access_mode;
5294 static enum fan_control_commands fan_control_commands;
5296 static u8 fan_control_initial_status;
5297 static u8 fan_control_desired_level;
5298 static int fan_watchdog_maxinterval;
5300 static struct mutex fan_mutex;
5302 static void fan_watchdog_fire(struct work_struct *ignored);
5303 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5305 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5306 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5307 "\\FSPD", /* 600e/x, 770e, 770x */
5308 ); /* all others */
5309 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5310 "JFNS", /* 770x-JL */
5311 ); /* all others */
5314 * Call with fan_mutex held
5316 static void fan_update_desired_level(u8 status)
5318 if ((status &
5319 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5320 if (status > 7)
5321 fan_control_desired_level = 7;
5322 else
5323 fan_control_desired_level = status;
5327 static int fan_get_status(u8 *status)
5329 u8 s;
5331 /* TODO:
5332 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5334 switch (fan_status_access_mode) {
5335 case TPACPI_FAN_RD_ACPI_GFAN:
5336 /* 570, 600e/x, 770e, 770x */
5338 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5339 return -EIO;
5341 if (likely(status))
5342 *status = s & 0x07;
5344 break;
5346 case TPACPI_FAN_RD_TPEC:
5347 /* all except 570, 600e/x, 770e, 770x */
5348 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5349 return -EIO;
5351 if (likely(status))
5352 *status = s;
5354 break;
5356 default:
5357 return -ENXIO;
5360 return 0;
5363 static int fan_get_status_safe(u8 *status)
5365 int rc;
5366 u8 s;
5368 if (mutex_lock_interruptible(&fan_mutex))
5369 return -ERESTARTSYS;
5370 rc = fan_get_status(&s);
5371 if (!rc)
5372 fan_update_desired_level(s);
5373 mutex_unlock(&fan_mutex);
5375 if (status)
5376 *status = s;
5378 return rc;
5381 static int fan_get_speed(unsigned int *speed)
5383 u8 hi, lo;
5385 switch (fan_status_access_mode) {
5386 case TPACPI_FAN_RD_TPEC:
5387 /* all except 570, 600e/x, 770e, 770x */
5388 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5389 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5390 return -EIO;
5392 if (likely(speed))
5393 *speed = (hi << 8) | lo;
5395 break;
5397 default:
5398 return -ENXIO;
5401 return 0;
5404 static int fan_set_level(int level)
5406 if (!fan_control_allowed)
5407 return -EPERM;
5409 switch (fan_control_access_mode) {
5410 case TPACPI_FAN_WR_ACPI_SFAN:
5411 if (level >= 0 && level <= 7) {
5412 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5413 return -EIO;
5414 } else
5415 return -EINVAL;
5416 break;
5418 case TPACPI_FAN_WR_ACPI_FANS:
5419 case TPACPI_FAN_WR_TPEC:
5420 if ((level != TP_EC_FAN_AUTO) &&
5421 (level != TP_EC_FAN_FULLSPEED) &&
5422 ((level < 0) || (level > 7)))
5423 return -EINVAL;
5425 /* safety net should the EC not support AUTO
5426 * or FULLSPEED mode bits and just ignore them */
5427 if (level & TP_EC_FAN_FULLSPEED)
5428 level |= 7; /* safety min speed 7 */
5429 else if (level & TP_EC_FAN_AUTO)
5430 level |= 4; /* safety min speed 4 */
5432 if (!acpi_ec_write(fan_status_offset, level))
5433 return -EIO;
5434 else
5435 tp_features.fan_ctrl_status_undef = 0;
5436 break;
5438 default:
5439 return -ENXIO;
5441 return 0;
5444 static int fan_set_level_safe(int level)
5446 int rc;
5448 if (!fan_control_allowed)
5449 return -EPERM;
5451 if (mutex_lock_interruptible(&fan_mutex))
5452 return -ERESTARTSYS;
5454 if (level == TPACPI_FAN_LAST_LEVEL)
5455 level = fan_control_desired_level;
5457 rc = fan_set_level(level);
5458 if (!rc)
5459 fan_update_desired_level(level);
5461 mutex_unlock(&fan_mutex);
5462 return rc;
5465 static int fan_set_enable(void)
5467 u8 s;
5468 int rc;
5470 if (!fan_control_allowed)
5471 return -EPERM;
5473 if (mutex_lock_interruptible(&fan_mutex))
5474 return -ERESTARTSYS;
5476 switch (fan_control_access_mode) {
5477 case TPACPI_FAN_WR_ACPI_FANS:
5478 case TPACPI_FAN_WR_TPEC:
5479 rc = fan_get_status(&s);
5480 if (rc < 0)
5481 break;
5483 /* Don't go out of emergency fan mode */
5484 if (s != 7) {
5485 s &= 0x07;
5486 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5489 if (!acpi_ec_write(fan_status_offset, s))
5490 rc = -EIO;
5491 else {
5492 tp_features.fan_ctrl_status_undef = 0;
5493 rc = 0;
5495 break;
5497 case TPACPI_FAN_WR_ACPI_SFAN:
5498 rc = fan_get_status(&s);
5499 if (rc < 0)
5500 break;
5502 s &= 0x07;
5504 /* Set fan to at least level 4 */
5505 s |= 4;
5507 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5508 rc = -EIO;
5509 else
5510 rc = 0;
5511 break;
5513 default:
5514 rc = -ENXIO;
5517 mutex_unlock(&fan_mutex);
5518 return rc;
5521 static int fan_set_disable(void)
5523 int rc;
5525 if (!fan_control_allowed)
5526 return -EPERM;
5528 if (mutex_lock_interruptible(&fan_mutex))
5529 return -ERESTARTSYS;
5531 rc = 0;
5532 switch (fan_control_access_mode) {
5533 case TPACPI_FAN_WR_ACPI_FANS:
5534 case TPACPI_FAN_WR_TPEC:
5535 if (!acpi_ec_write(fan_status_offset, 0x00))
5536 rc = -EIO;
5537 else {
5538 fan_control_desired_level = 0;
5539 tp_features.fan_ctrl_status_undef = 0;
5541 break;
5543 case TPACPI_FAN_WR_ACPI_SFAN:
5544 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5545 rc = -EIO;
5546 else
5547 fan_control_desired_level = 0;
5548 break;
5550 default:
5551 rc = -ENXIO;
5555 mutex_unlock(&fan_mutex);
5556 return rc;
5559 static int fan_set_speed(int speed)
5561 int rc;
5563 if (!fan_control_allowed)
5564 return -EPERM;
5566 if (mutex_lock_interruptible(&fan_mutex))
5567 return -ERESTARTSYS;
5569 rc = 0;
5570 switch (fan_control_access_mode) {
5571 case TPACPI_FAN_WR_ACPI_FANS:
5572 if (speed >= 0 && speed <= 65535) {
5573 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5574 speed, speed, speed))
5575 rc = -EIO;
5576 } else
5577 rc = -EINVAL;
5578 break;
5580 default:
5581 rc = -ENXIO;
5584 mutex_unlock(&fan_mutex);
5585 return rc;
5588 static void fan_watchdog_reset(void)
5590 static int fan_watchdog_active;
5592 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5593 return;
5595 if (fan_watchdog_active)
5596 cancel_delayed_work(&fan_watchdog_task);
5598 if (fan_watchdog_maxinterval > 0 &&
5599 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5600 fan_watchdog_active = 1;
5601 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5602 msecs_to_jiffies(fan_watchdog_maxinterval
5603 * 1000))) {
5604 printk(TPACPI_ERR
5605 "failed to queue the fan watchdog, "
5606 "watchdog will not trigger\n");
5608 } else
5609 fan_watchdog_active = 0;
5612 static void fan_watchdog_fire(struct work_struct *ignored)
5614 int rc;
5616 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5617 return;
5619 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5620 rc = fan_set_enable();
5621 if (rc < 0) {
5622 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5623 "will try again later...\n", -rc);
5624 /* reschedule for later */
5625 fan_watchdog_reset();
5630 * SYSFS fan layout: hwmon compatible (device)
5632 * pwm*_enable:
5633 * 0: "disengaged" mode
5634 * 1: manual mode
5635 * 2: native EC "auto" mode (recommended, hardware default)
5637 * pwm*: set speed in manual mode, ignored otherwise.
5638 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5639 * interpolation.
5641 * fan*_input: tachometer reading, RPM
5644 * SYSFS fan layout: extensions
5646 * fan_watchdog (driver):
5647 * fan watchdog interval in seconds, 0 disables (default), max 120
5650 /* sysfs fan pwm1_enable ----------------------------------------------- */
5651 static ssize_t fan_pwm1_enable_show(struct device *dev,
5652 struct device_attribute *attr,
5653 char *buf)
5655 int res, mode;
5656 u8 status;
5658 res = fan_get_status_safe(&status);
5659 if (res)
5660 return res;
5662 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5663 if (status != fan_control_initial_status) {
5664 tp_features.fan_ctrl_status_undef = 0;
5665 } else {
5666 /* Return most likely status. In fact, it
5667 * might be the only possible status */
5668 status = TP_EC_FAN_AUTO;
5672 if (status & TP_EC_FAN_FULLSPEED) {
5673 mode = 0;
5674 } else if (status & TP_EC_FAN_AUTO) {
5675 mode = 2;
5676 } else
5677 mode = 1;
5679 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5682 static ssize_t fan_pwm1_enable_store(struct device *dev,
5683 struct device_attribute *attr,
5684 const char *buf, size_t count)
5686 unsigned long t;
5687 int res, level;
5689 if (parse_strtoul(buf, 2, &t))
5690 return -EINVAL;
5692 switch (t) {
5693 case 0:
5694 level = TP_EC_FAN_FULLSPEED;
5695 break;
5696 case 1:
5697 level = TPACPI_FAN_LAST_LEVEL;
5698 break;
5699 case 2:
5700 level = TP_EC_FAN_AUTO;
5701 break;
5702 case 3:
5703 /* reserved for software-controlled auto mode */
5704 return -ENOSYS;
5705 default:
5706 return -EINVAL;
5709 res = fan_set_level_safe(level);
5710 if (res == -ENXIO)
5711 return -EINVAL;
5712 else if (res < 0)
5713 return res;
5715 fan_watchdog_reset();
5717 return count;
5720 static struct device_attribute dev_attr_fan_pwm1_enable =
5721 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5722 fan_pwm1_enable_show, fan_pwm1_enable_store);
5724 /* sysfs fan pwm1 ------------------------------------------------------ */
5725 static ssize_t fan_pwm1_show(struct device *dev,
5726 struct device_attribute *attr,
5727 char *buf)
5729 int res;
5730 u8 status;
5732 res = fan_get_status_safe(&status);
5733 if (res)
5734 return res;
5736 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5737 if (status != fan_control_initial_status) {
5738 tp_features.fan_ctrl_status_undef = 0;
5739 } else {
5740 status = TP_EC_FAN_AUTO;
5744 if ((status &
5745 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5746 status = fan_control_desired_level;
5748 if (status > 7)
5749 status = 7;
5751 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5754 static ssize_t fan_pwm1_store(struct device *dev,
5755 struct device_attribute *attr,
5756 const char *buf, size_t count)
5758 unsigned long s;
5759 int rc;
5760 u8 status, newlevel;
5762 if (parse_strtoul(buf, 255, &s))
5763 return -EINVAL;
5765 /* scale down from 0-255 to 0-7 */
5766 newlevel = (s >> 5) & 0x07;
5768 if (mutex_lock_interruptible(&fan_mutex))
5769 return -ERESTARTSYS;
5771 rc = fan_get_status(&status);
5772 if (!rc && (status &
5773 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5774 rc = fan_set_level(newlevel);
5775 if (rc == -ENXIO)
5776 rc = -EINVAL;
5777 else if (!rc) {
5778 fan_update_desired_level(newlevel);
5779 fan_watchdog_reset();
5783 mutex_unlock(&fan_mutex);
5784 return (rc)? rc : count;
5787 static struct device_attribute dev_attr_fan_pwm1 =
5788 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5789 fan_pwm1_show, fan_pwm1_store);
5791 /* sysfs fan fan1_input ------------------------------------------------ */
5792 static ssize_t fan_fan1_input_show(struct device *dev,
5793 struct device_attribute *attr,
5794 char *buf)
5796 int res;
5797 unsigned int speed;
5799 res = fan_get_speed(&speed);
5800 if (res < 0)
5801 return res;
5803 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5806 static struct device_attribute dev_attr_fan_fan1_input =
5807 __ATTR(fan1_input, S_IRUGO,
5808 fan_fan1_input_show, NULL);
5810 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5811 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5812 char *buf)
5814 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5817 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5818 const char *buf, size_t count)
5820 unsigned long t;
5822 if (parse_strtoul(buf, 120, &t))
5823 return -EINVAL;
5825 if (!fan_control_allowed)
5826 return -EPERM;
5828 fan_watchdog_maxinterval = t;
5829 fan_watchdog_reset();
5831 return count;
5834 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5835 fan_fan_watchdog_show, fan_fan_watchdog_store);
5837 /* --------------------------------------------------------------------- */
5838 static struct attribute *fan_attributes[] = {
5839 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5840 &dev_attr_fan_fan1_input.attr,
5841 NULL
5844 static const struct attribute_group fan_attr_group = {
5845 .attrs = fan_attributes,
5848 static int __init fan_init(struct ibm_init_struct *iibm)
5850 int rc;
5852 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5854 mutex_init(&fan_mutex);
5855 fan_status_access_mode = TPACPI_FAN_NONE;
5856 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5857 fan_control_commands = 0;
5858 fan_watchdog_maxinterval = 0;
5859 tp_features.fan_ctrl_status_undef = 0;
5860 fan_control_desired_level = 7;
5862 TPACPI_ACPIHANDLE_INIT(fans);
5863 TPACPI_ACPIHANDLE_INIT(gfan);
5864 TPACPI_ACPIHANDLE_INIT(sfan);
5866 if (gfan_handle) {
5867 /* 570, 600e/x, 770e, 770x */
5868 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5869 } else {
5870 /* all other ThinkPads: note that even old-style
5871 * ThinkPad ECs supports the fan control register */
5872 if (likely(acpi_ec_read(fan_status_offset,
5873 &fan_control_initial_status))) {
5874 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5876 /* In some ThinkPads, neither the EC nor the ACPI
5877 * DSDT initialize the fan status, and it ends up
5878 * being set to 0x07 when it *could* be either
5879 * 0x07 or 0x80.
5881 * Enable for TP-1Y (T43), TP-78 (R51e),
5882 * TP-76 (R52), TP-70 (T43, R52), which are known
5883 * to be buggy. */
5884 if (fan_control_initial_status == 0x07) {
5885 switch (thinkpad_id.ec_model) {
5886 case 0x5931: /* TP-1Y */
5887 case 0x3837: /* TP-78 */
5888 case 0x3637: /* TP-76 */
5889 case 0x3037: /* TP-70 */
5890 printk(TPACPI_NOTICE
5891 "fan_init: initial fan status "
5892 "is unknown, assuming it is "
5893 "in auto mode\n");
5894 tp_features.fan_ctrl_status_undef = 1;
5898 } else {
5899 printk(TPACPI_ERR
5900 "ThinkPad ACPI EC access misbehaving, "
5901 "fan status and control unavailable\n");
5902 return 1;
5906 if (sfan_handle) {
5907 /* 570, 770x-JL */
5908 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5909 fan_control_commands |=
5910 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5911 } else {
5912 if (!gfan_handle) {
5913 /* gfan without sfan means no fan control */
5914 /* all other models implement TP EC 0x2f control */
5916 if (fans_handle) {
5917 /* X31, X40, X41 */
5918 fan_control_access_mode =
5919 TPACPI_FAN_WR_ACPI_FANS;
5920 fan_control_commands |=
5921 TPACPI_FAN_CMD_SPEED |
5922 TPACPI_FAN_CMD_LEVEL |
5923 TPACPI_FAN_CMD_ENABLE;
5924 } else {
5925 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5926 fan_control_commands |=
5927 TPACPI_FAN_CMD_LEVEL |
5928 TPACPI_FAN_CMD_ENABLE;
5933 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5934 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5935 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5936 fan_status_access_mode, fan_control_access_mode);
5938 /* fan control master switch */
5939 if (!fan_control_allowed) {
5940 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5941 fan_control_commands = 0;
5942 dbg_printk(TPACPI_DBG_INIT,
5943 "fan control features disabled by parameter\n");
5946 /* update fan_control_desired_level */
5947 if (fan_status_access_mode != TPACPI_FAN_NONE)
5948 fan_get_status_safe(NULL);
5950 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5951 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5952 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5953 &fan_attr_group);
5954 if (rc < 0)
5955 return rc;
5957 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5958 &driver_attr_fan_watchdog);
5959 if (rc < 0) {
5960 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5961 &fan_attr_group);
5962 return rc;
5964 return 0;
5965 } else
5966 return 1;
5969 static void fan_exit(void)
5971 vdbg_printk(TPACPI_DBG_EXIT,
5972 "cancelling any pending fan watchdog tasks\n");
5974 /* FIXME: can we really do this unconditionally? */
5975 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5976 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5977 &driver_attr_fan_watchdog);
5979 cancel_delayed_work(&fan_watchdog_task);
5980 flush_workqueue(tpacpi_wq);
5983 static int fan_read(char *p)
5985 int len = 0;
5986 int rc;
5987 u8 status;
5988 unsigned int speed = 0;
5990 switch (fan_status_access_mode) {
5991 case TPACPI_FAN_RD_ACPI_GFAN:
5992 /* 570, 600e/x, 770e, 770x */
5993 rc = fan_get_status_safe(&status);
5994 if (rc < 0)
5995 return rc;
5997 len += sprintf(p + len, "status:\t\t%s\n"
5998 "level:\t\t%d\n",
5999 (status != 0) ? "enabled" : "disabled", status);
6000 break;
6002 case TPACPI_FAN_RD_TPEC:
6003 /* all except 570, 600e/x, 770e, 770x */
6004 rc = fan_get_status_safe(&status);
6005 if (rc < 0)
6006 return rc;
6008 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6009 if (status != fan_control_initial_status)
6010 tp_features.fan_ctrl_status_undef = 0;
6011 else
6012 /* Return most likely status. In fact, it
6013 * might be the only possible status */
6014 status = TP_EC_FAN_AUTO;
6017 len += sprintf(p + len, "status:\t\t%s\n",
6018 (status != 0) ? "enabled" : "disabled");
6020 rc = fan_get_speed(&speed);
6021 if (rc < 0)
6022 return rc;
6024 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6026 if (status & TP_EC_FAN_FULLSPEED)
6027 /* Disengaged mode takes precedence */
6028 len += sprintf(p + len, "level:\t\tdisengaged\n");
6029 else if (status & TP_EC_FAN_AUTO)
6030 len += sprintf(p + len, "level:\t\tauto\n");
6031 else
6032 len += sprintf(p + len, "level:\t\t%d\n", status);
6033 break;
6035 case TPACPI_FAN_NONE:
6036 default:
6037 len += sprintf(p + len, "status:\t\tnot supported\n");
6040 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6041 len += sprintf(p + len, "commands:\tlevel <level>");
6043 switch (fan_control_access_mode) {
6044 case TPACPI_FAN_WR_ACPI_SFAN:
6045 len += sprintf(p + len, " (<level> is 0-7)\n");
6046 break;
6048 default:
6049 len += sprintf(p + len, " (<level> is 0-7, "
6050 "auto, disengaged, full-speed)\n");
6051 break;
6055 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6056 len += sprintf(p + len, "commands:\tenable, disable\n"
6057 "commands:\twatchdog <timeout> (<timeout> "
6058 "is 0 (off), 1-120 (seconds))\n");
6060 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6061 len += sprintf(p + len, "commands:\tspeed <speed>"
6062 " (<speed> is 0-65535)\n");
6064 return len;
6067 static int fan_write_cmd_level(const char *cmd, int *rc)
6069 int level;
6071 if (strlencmp(cmd, "level auto") == 0)
6072 level = TP_EC_FAN_AUTO;
6073 else if ((strlencmp(cmd, "level disengaged") == 0) |
6074 (strlencmp(cmd, "level full-speed") == 0))
6075 level = TP_EC_FAN_FULLSPEED;
6076 else if (sscanf(cmd, "level %d", &level) != 1)
6077 return 0;
6079 *rc = fan_set_level_safe(level);
6080 if (*rc == -ENXIO)
6081 printk(TPACPI_ERR "level command accepted for unsupported "
6082 "access mode %d", fan_control_access_mode);
6084 return 1;
6087 static int fan_write_cmd_enable(const char *cmd, int *rc)
6089 if (strlencmp(cmd, "enable") != 0)
6090 return 0;
6092 *rc = fan_set_enable();
6093 if (*rc == -ENXIO)
6094 printk(TPACPI_ERR "enable command accepted for unsupported "
6095 "access mode %d", fan_control_access_mode);
6097 return 1;
6100 static int fan_write_cmd_disable(const char *cmd, int *rc)
6102 if (strlencmp(cmd, "disable") != 0)
6103 return 0;
6105 *rc = fan_set_disable();
6106 if (*rc == -ENXIO)
6107 printk(TPACPI_ERR "disable command accepted for unsupported "
6108 "access mode %d", fan_control_access_mode);
6110 return 1;
6113 static int fan_write_cmd_speed(const char *cmd, int *rc)
6115 int speed;
6117 /* TODO:
6118 * Support speed <low> <medium> <high> ? */
6120 if (sscanf(cmd, "speed %d", &speed) != 1)
6121 return 0;
6123 *rc = fan_set_speed(speed);
6124 if (*rc == -ENXIO)
6125 printk(TPACPI_ERR "speed command accepted for unsupported "
6126 "access mode %d", fan_control_access_mode);
6128 return 1;
6131 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6133 int interval;
6135 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6136 return 0;
6138 if (interval < 0 || interval > 120)
6139 *rc = -EINVAL;
6140 else
6141 fan_watchdog_maxinterval = interval;
6143 return 1;
6146 static int fan_write(char *buf)
6148 char *cmd;
6149 int rc = 0;
6151 while (!rc && (cmd = next_cmd(&buf))) {
6152 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6153 fan_write_cmd_level(cmd, &rc)) &&
6154 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6155 (fan_write_cmd_enable(cmd, &rc) ||
6156 fan_write_cmd_disable(cmd, &rc) ||
6157 fan_write_cmd_watchdog(cmd, &rc))) &&
6158 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6159 fan_write_cmd_speed(cmd, &rc))
6161 rc = -EINVAL;
6162 else if (!rc)
6163 fan_watchdog_reset();
6166 return rc;
6169 static struct ibm_struct fan_driver_data = {
6170 .name = "fan",
6171 .read = fan_read,
6172 .write = fan_write,
6173 .exit = fan_exit,
6176 /****************************************************************************
6177 ****************************************************************************
6179 * Infrastructure
6181 ****************************************************************************
6182 ****************************************************************************/
6184 /* sysfs name ---------------------------------------------------------- */
6185 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6186 struct device_attribute *attr,
6187 char *buf)
6189 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6192 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6193 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6195 /* --------------------------------------------------------------------- */
6197 /* /proc support */
6198 static struct proc_dir_entry *proc_dir;
6201 * Module and infrastructure proble, init and exit handling
6204 static int force_load;
6206 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6207 static const char * __init str_supported(int is_supported)
6209 static char text_unsupported[] __initdata = "not supported";
6211 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6213 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6215 static void ibm_exit(struct ibm_struct *ibm)
6217 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6219 list_del_init(&ibm->all_drivers);
6221 if (ibm->flags.acpi_notify_installed) {
6222 dbg_printk(TPACPI_DBG_EXIT,
6223 "%s: acpi_remove_notify_handler\n", ibm->name);
6224 BUG_ON(!ibm->acpi);
6225 acpi_remove_notify_handler(*ibm->acpi->handle,
6226 ibm->acpi->type,
6227 dispatch_acpi_notify);
6228 ibm->flags.acpi_notify_installed = 0;
6229 ibm->flags.acpi_notify_installed = 0;
6232 if (ibm->flags.proc_created) {
6233 dbg_printk(TPACPI_DBG_EXIT,
6234 "%s: remove_proc_entry\n", ibm->name);
6235 remove_proc_entry(ibm->name, proc_dir);
6236 ibm->flags.proc_created = 0;
6239 if (ibm->flags.acpi_driver_registered) {
6240 dbg_printk(TPACPI_DBG_EXIT,
6241 "%s: acpi_bus_unregister_driver\n", ibm->name);
6242 BUG_ON(!ibm->acpi);
6243 acpi_bus_unregister_driver(ibm->acpi->driver);
6244 kfree(ibm->acpi->driver);
6245 ibm->acpi->driver = NULL;
6246 ibm->flags.acpi_driver_registered = 0;
6249 if (ibm->flags.init_called && ibm->exit) {
6250 ibm->exit();
6251 ibm->flags.init_called = 0;
6254 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6257 static int __init ibm_init(struct ibm_init_struct *iibm)
6259 int ret;
6260 struct ibm_struct *ibm = iibm->data;
6261 struct proc_dir_entry *entry;
6263 BUG_ON(ibm == NULL);
6265 INIT_LIST_HEAD(&ibm->all_drivers);
6267 if (ibm->flags.experimental && !experimental)
6268 return 0;
6270 dbg_printk(TPACPI_DBG_INIT,
6271 "probing for %s\n", ibm->name);
6273 if (iibm->init) {
6274 ret = iibm->init(iibm);
6275 if (ret > 0)
6276 return 0; /* probe failed */
6277 if (ret)
6278 return ret;
6280 ibm->flags.init_called = 1;
6283 if (ibm->acpi) {
6284 if (ibm->acpi->hid) {
6285 ret = register_tpacpi_subdriver(ibm);
6286 if (ret)
6287 goto err_out;
6290 if (ibm->acpi->notify) {
6291 ret = setup_acpi_notify(ibm);
6292 if (ret == -ENODEV) {
6293 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6294 ibm->name);
6295 ret = 0;
6296 goto err_out;
6298 if (ret < 0)
6299 goto err_out;
6303 dbg_printk(TPACPI_DBG_INIT,
6304 "%s installed\n", ibm->name);
6306 if (ibm->read) {
6307 entry = create_proc_entry(ibm->name,
6308 S_IFREG | S_IRUGO | S_IWUSR,
6309 proc_dir);
6310 if (!entry) {
6311 printk(TPACPI_ERR "unable to create proc entry %s\n",
6312 ibm->name);
6313 ret = -ENODEV;
6314 goto err_out;
6316 entry->owner = THIS_MODULE;
6317 entry->data = ibm;
6318 entry->read_proc = &dispatch_procfs_read;
6319 if (ibm->write)
6320 entry->write_proc = &dispatch_procfs_write;
6321 ibm->flags.proc_created = 1;
6324 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6326 return 0;
6328 err_out:
6329 dbg_printk(TPACPI_DBG_INIT,
6330 "%s: at error exit path with result %d\n",
6331 ibm->name, ret);
6333 ibm_exit(ibm);
6334 return (ret < 0)? ret : 0;
6337 /* Probing */
6339 /* returns 0 - probe ok, or < 0 - probe error.
6340 * Probe ok doesn't mean thinkpad found.
6341 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6342 static int __must_check __init get_thinkpad_model_data(
6343 struct thinkpad_id_data *tp)
6345 const struct dmi_device *dev = NULL;
6346 char ec_fw_string[18];
6347 char const *s;
6349 if (!tp)
6350 return -EINVAL;
6352 memset(tp, 0, sizeof(*tp));
6354 if (dmi_name_in_vendors("IBM"))
6355 tp->vendor = PCI_VENDOR_ID_IBM;
6356 else if (dmi_name_in_vendors("LENOVO"))
6357 tp->vendor = PCI_VENDOR_ID_LENOVO;
6358 else
6359 return 0;
6361 s = dmi_get_system_info(DMI_BIOS_VERSION);
6362 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6363 if (s && !tp->bios_version_str)
6364 return -ENOMEM;
6365 if (!tp->bios_version_str)
6366 return 0;
6367 tp->bios_model = tp->bios_version_str[0]
6368 | (tp->bios_version_str[1] << 8);
6371 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6372 * X32 or newer, all Z series; Some models must have an
6373 * up-to-date BIOS or they will not be detected.
6375 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6377 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6378 if (sscanf(dev->name,
6379 "IBM ThinkPad Embedded Controller -[%17c",
6380 ec_fw_string) == 1) {
6381 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6382 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6384 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6385 if (!tp->ec_version_str)
6386 return -ENOMEM;
6387 tp->ec_model = ec_fw_string[0]
6388 | (ec_fw_string[1] << 8);
6389 break;
6393 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6394 if (s && !strnicmp(s, "ThinkPad", 8)) {
6395 tp->model_str = kstrdup(s, GFP_KERNEL);
6396 if (!tp->model_str)
6397 return -ENOMEM;
6400 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6401 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6402 if (s && !tp->nummodel_str)
6403 return -ENOMEM;
6405 return 0;
6408 static int __init probe_for_thinkpad(void)
6410 int is_thinkpad;
6412 if (acpi_disabled)
6413 return -ENODEV;
6416 * Non-ancient models have better DMI tagging, but very old models
6417 * don't.
6419 is_thinkpad = (thinkpad_id.model_str != NULL);
6421 /* ec is required because many other handles are relative to it */
6422 TPACPI_ACPIHANDLE_INIT(ec);
6423 if (!ec_handle) {
6424 if (is_thinkpad)
6425 printk(TPACPI_ERR
6426 "Not yet supported ThinkPad detected!\n");
6427 return -ENODEV;
6431 * Risks a regression on very old machines, but reduces potential
6432 * false positives a damn great deal
6434 if (!is_thinkpad)
6435 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6437 if (!is_thinkpad && !force_load)
6438 return -ENODEV;
6440 return 0;
6444 /* Module init, exit, parameters */
6446 static struct ibm_init_struct ibms_init[] __initdata = {
6448 .init = thinkpad_acpi_driver_init,
6449 .data = &thinkpad_acpi_driver_data,
6452 .init = hotkey_init,
6453 .data = &hotkey_driver_data,
6456 .init = bluetooth_init,
6457 .data = &bluetooth_driver_data,
6460 .init = wan_init,
6461 .data = &wan_driver_data,
6463 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6465 .init = video_init,
6466 .data = &video_driver_data,
6468 #endif
6470 .init = light_init,
6471 .data = &light_driver_data,
6473 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6475 .init = dock_init,
6476 .data = &dock_driver_data[0],
6479 .init = dock_init2,
6480 .data = &dock_driver_data[1],
6482 #endif
6483 #ifdef CONFIG_THINKPAD_ACPI_BAY
6485 .init = bay_init,
6486 .data = &bay_driver_data,
6488 #endif
6490 .init = cmos_init,
6491 .data = &cmos_driver_data,
6494 .init = led_init,
6495 .data = &led_driver_data,
6498 .init = beep_init,
6499 .data = &beep_driver_data,
6502 .init = thermal_init,
6503 .data = &thermal_driver_data,
6506 .data = &ecdump_driver_data,
6509 .init = brightness_init,
6510 .data = &brightness_driver_data,
6513 .data = &volume_driver_data,
6516 .init = fan_init,
6517 .data = &fan_driver_data,
6521 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6523 unsigned int i;
6524 struct ibm_struct *ibm;
6526 if (!kp || !kp->name || !val)
6527 return -EINVAL;
6529 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6530 ibm = ibms_init[i].data;
6531 WARN_ON(ibm == NULL);
6533 if (!ibm || !ibm->name)
6534 continue;
6536 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6537 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6538 return -ENOSPC;
6539 strcpy(ibms_init[i].param, val);
6540 strcat(ibms_init[i].param, ",");
6541 return 0;
6545 return -EINVAL;
6548 module_param(experimental, int, 0);
6549 MODULE_PARM_DESC(experimental,
6550 "Enables experimental features when non-zero");
6552 module_param_named(debug, dbg_level, uint, 0);
6553 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6555 module_param(force_load, bool, 0);
6556 MODULE_PARM_DESC(force_load,
6557 "Attempts to load the driver even on a "
6558 "mis-identified ThinkPad when true");
6560 module_param_named(fan_control, fan_control_allowed, bool, 0);
6561 MODULE_PARM_DESC(fan_control,
6562 "Enables setting fan parameters features when true");
6564 module_param_named(brightness_mode, brightness_mode, int, 0);
6565 MODULE_PARM_DESC(brightness_mode,
6566 "Selects brightness control strategy: "
6567 "0=auto, 1=EC, 2=CMOS, 3=both");
6569 module_param(brightness_enable, uint, 0);
6570 MODULE_PARM_DESC(brightness_enable,
6571 "Enables backlight control when 1, disables when 0");
6573 module_param(hotkey_report_mode, uint, 0);
6574 MODULE_PARM_DESC(hotkey_report_mode,
6575 "used for backwards compatibility with userspace, "
6576 "see documentation");
6578 #define TPACPI_PARAM(feature) \
6579 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6580 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6581 "at module load, see documentation")
6583 TPACPI_PARAM(hotkey);
6584 TPACPI_PARAM(bluetooth);
6585 TPACPI_PARAM(video);
6586 TPACPI_PARAM(light);
6587 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6588 TPACPI_PARAM(dock);
6589 #endif
6590 #ifdef CONFIG_THINKPAD_ACPI_BAY
6591 TPACPI_PARAM(bay);
6592 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6593 TPACPI_PARAM(cmos);
6594 TPACPI_PARAM(led);
6595 TPACPI_PARAM(beep);
6596 TPACPI_PARAM(ecdump);
6597 TPACPI_PARAM(brightness);
6598 TPACPI_PARAM(volume);
6599 TPACPI_PARAM(fan);
6601 static void thinkpad_acpi_module_exit(void)
6603 struct ibm_struct *ibm, *itmp;
6605 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6607 list_for_each_entry_safe_reverse(ibm, itmp,
6608 &tpacpi_all_drivers,
6609 all_drivers) {
6610 ibm_exit(ibm);
6613 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6615 if (tpacpi_inputdev) {
6616 if (tp_features.input_device_registered)
6617 input_unregister_device(tpacpi_inputdev);
6618 else
6619 input_free_device(tpacpi_inputdev);
6622 if (tpacpi_hwmon)
6623 hwmon_device_unregister(tpacpi_hwmon);
6625 if (tp_features.sensors_pdev_attrs_registered)
6626 device_remove_file(&tpacpi_sensors_pdev->dev,
6627 &dev_attr_thinkpad_acpi_pdev_name);
6628 if (tpacpi_sensors_pdev)
6629 platform_device_unregister(tpacpi_sensors_pdev);
6630 if (tpacpi_pdev)
6631 platform_device_unregister(tpacpi_pdev);
6633 if (tp_features.sensors_pdrv_attrs_registered)
6634 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6635 if (tp_features.platform_drv_attrs_registered)
6636 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6638 if (tp_features.sensors_pdrv_registered)
6639 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6641 if (tp_features.platform_drv_registered)
6642 platform_driver_unregister(&tpacpi_pdriver);
6644 if (proc_dir)
6645 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6647 if (tpacpi_wq)
6648 destroy_workqueue(tpacpi_wq);
6650 kfree(thinkpad_id.bios_version_str);
6651 kfree(thinkpad_id.ec_version_str);
6652 kfree(thinkpad_id.model_str);
6656 static int __init thinkpad_acpi_module_init(void)
6658 int ret, i;
6660 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6662 /* Parameter checking */
6663 if (hotkey_report_mode > 2)
6664 return -EINVAL;
6666 /* Driver-level probe */
6668 ret = get_thinkpad_model_data(&thinkpad_id);
6669 if (ret) {
6670 printk(TPACPI_ERR
6671 "unable to get DMI data: %d\n", ret);
6672 thinkpad_acpi_module_exit();
6673 return ret;
6675 ret = probe_for_thinkpad();
6676 if (ret) {
6677 thinkpad_acpi_module_exit();
6678 return ret;
6681 /* Driver initialization */
6683 TPACPI_ACPIHANDLE_INIT(ecrd);
6684 TPACPI_ACPIHANDLE_INIT(ecwr);
6686 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6687 if (!tpacpi_wq) {
6688 thinkpad_acpi_module_exit();
6689 return -ENOMEM;
6692 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6693 if (!proc_dir) {
6694 printk(TPACPI_ERR
6695 "unable to create proc dir " TPACPI_PROC_DIR);
6696 thinkpad_acpi_module_exit();
6697 return -ENODEV;
6699 proc_dir->owner = THIS_MODULE;
6701 ret = platform_driver_register(&tpacpi_pdriver);
6702 if (ret) {
6703 printk(TPACPI_ERR
6704 "unable to register main platform driver\n");
6705 thinkpad_acpi_module_exit();
6706 return ret;
6708 tp_features.platform_drv_registered = 1;
6710 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6711 if (ret) {
6712 printk(TPACPI_ERR
6713 "unable to register hwmon platform driver\n");
6714 thinkpad_acpi_module_exit();
6715 return ret;
6717 tp_features.sensors_pdrv_registered = 1;
6719 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6720 if (!ret) {
6721 tp_features.platform_drv_attrs_registered = 1;
6722 ret = tpacpi_create_driver_attributes(
6723 &tpacpi_hwmon_pdriver.driver);
6725 if (ret) {
6726 printk(TPACPI_ERR
6727 "unable to create sysfs driver attributes\n");
6728 thinkpad_acpi_module_exit();
6729 return ret;
6731 tp_features.sensors_pdrv_attrs_registered = 1;
6734 /* Device initialization */
6735 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6736 NULL, 0);
6737 if (IS_ERR(tpacpi_pdev)) {
6738 ret = PTR_ERR(tpacpi_pdev);
6739 tpacpi_pdev = NULL;
6740 printk(TPACPI_ERR "unable to register platform device\n");
6741 thinkpad_acpi_module_exit();
6742 return ret;
6744 tpacpi_sensors_pdev = platform_device_register_simple(
6745 TPACPI_HWMON_DRVR_NAME,
6746 -1, NULL, 0);
6747 if (IS_ERR(tpacpi_sensors_pdev)) {
6748 ret = PTR_ERR(tpacpi_sensors_pdev);
6749 tpacpi_sensors_pdev = NULL;
6750 printk(TPACPI_ERR
6751 "unable to register hwmon platform device\n");
6752 thinkpad_acpi_module_exit();
6753 return ret;
6755 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6756 &dev_attr_thinkpad_acpi_pdev_name);
6757 if (ret) {
6758 printk(TPACPI_ERR
6759 "unable to create sysfs hwmon device attributes\n");
6760 thinkpad_acpi_module_exit();
6761 return ret;
6763 tp_features.sensors_pdev_attrs_registered = 1;
6764 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6765 if (IS_ERR(tpacpi_hwmon)) {
6766 ret = PTR_ERR(tpacpi_hwmon);
6767 tpacpi_hwmon = NULL;
6768 printk(TPACPI_ERR "unable to register hwmon device\n");
6769 thinkpad_acpi_module_exit();
6770 return ret;
6772 mutex_init(&tpacpi_inputdev_send_mutex);
6773 tpacpi_inputdev = input_allocate_device();
6774 if (!tpacpi_inputdev) {
6775 printk(TPACPI_ERR "unable to allocate input device\n");
6776 thinkpad_acpi_module_exit();
6777 return -ENOMEM;
6778 } else {
6779 /* Prepare input device, but don't register */
6780 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6781 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6782 tpacpi_inputdev->id.bustype = BUS_HOST;
6783 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6784 thinkpad_id.vendor :
6785 PCI_VENDOR_ID_IBM;
6786 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6787 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6789 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6790 ret = ibm_init(&ibms_init[i]);
6791 if (ret >= 0 && *ibms_init[i].param)
6792 ret = ibms_init[i].data->write(ibms_init[i].param);
6793 if (ret < 0) {
6794 thinkpad_acpi_module_exit();
6795 return ret;
6798 ret = input_register_device(tpacpi_inputdev);
6799 if (ret < 0) {
6800 printk(TPACPI_ERR "unable to register input device\n");
6801 thinkpad_acpi_module_exit();
6802 return ret;
6803 } else {
6804 tp_features.input_device_registered = 1;
6807 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6808 return 0;
6811 /* Please remove this in year 2009 */
6812 MODULE_ALIAS("ibm_acpi");
6814 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6817 * DMI matching for module autoloading
6819 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6820 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6822 * Only models listed in thinkwiki will be supported, so add yours
6823 * if it is not there yet.
6825 #define IBM_BIOS_MODULE_ALIAS(__type) \
6826 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
6828 /* Non-ancient thinkpads */
6829 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6830 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6832 /* Ancient thinkpad BIOSes have to be identified by
6833 * BIOS type or model number, and there are far less
6834 * BIOS types than model numbers... */
6835 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
6836 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
6837 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
6839 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6840 MODULE_DESCRIPTION(TPACPI_DESC);
6841 MODULE_VERSION(TPACPI_VERSION);
6842 MODULE_LICENSE("GPL");
6844 module_init(thinkpad_acpi_module_init);
6845 module_exit(thinkpad_acpi_module_exit);