ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob7705e10f729207166e48a8a95ec12bca9b55f8ac
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-20080621"
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 /* Special LED class that can defer work */
286 struct tpacpi_led_classdev {
287 struct led_classdev led_classdev;
288 struct work_struct work;
289 enum led_brightness new_brightness;
290 unsigned int led;
293 /****************************************************************************
294 ****************************************************************************
296 * ACPI Helpers and device model
298 ****************************************************************************
299 ****************************************************************************/
301 /*************************************************************************
302 * ACPI basic handles
305 static acpi_handle root_handle;
307 #define TPACPI_HANDLE(object, parent, paths...) \
308 static acpi_handle object##_handle; \
309 static acpi_handle *object##_parent = &parent##_handle; \
310 static char *object##_path; \
311 static char *object##_paths[] = { paths }
313 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
314 "\\_SB.PCI.ISA.EC", /* 570 */
315 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
316 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
317 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
318 "\\_SB.PCI0.ICH3.EC0", /* R31 */
319 "\\_SB.PCI0.LPC.EC", /* all others */
322 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
323 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
325 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
326 /* T4x, X31, X40 */
327 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
328 "\\CMS", /* R40, R40e */
329 ); /* all others */
331 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
332 "^HKEY", /* R30, R31 */
333 "HKEY", /* all others */
334 ); /* 570 */
336 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
337 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
338 "\\_SB.PCI0.VID0", /* 770e */
339 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
340 "\\_SB.PCI0.AGP.VID", /* all others */
341 ); /* R30, R31 */
344 /*************************************************************************
345 * ACPI helpers
348 static int acpi_evalf(acpi_handle handle,
349 void *res, char *method, char *fmt, ...)
351 char *fmt0 = fmt;
352 struct acpi_object_list params;
353 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
354 struct acpi_buffer result, *resultp;
355 union acpi_object out_obj;
356 acpi_status status;
357 va_list ap;
358 char res_type;
359 int success;
360 int quiet;
362 if (!*fmt) {
363 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
364 return 0;
367 if (*fmt == 'q') {
368 quiet = 1;
369 fmt++;
370 } else
371 quiet = 0;
373 res_type = *(fmt++);
375 params.count = 0;
376 params.pointer = &in_objs[0];
378 va_start(ap, fmt);
379 while (*fmt) {
380 char c = *(fmt++);
381 switch (c) {
382 case 'd': /* int */
383 in_objs[params.count].integer.value = va_arg(ap, int);
384 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
385 break;
386 /* add more types as needed */
387 default:
388 printk(TPACPI_ERR "acpi_evalf() called "
389 "with invalid format character '%c'\n", c);
390 return 0;
393 va_end(ap);
395 if (res_type != 'v') {
396 result.length = sizeof(out_obj);
397 result.pointer = &out_obj;
398 resultp = &result;
399 } else
400 resultp = NULL;
402 status = acpi_evaluate_object(handle, method, &params, resultp);
404 switch (res_type) {
405 case 'd': /* int */
406 if (res)
407 *(int *)res = out_obj.integer.value;
408 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
409 break;
410 case 'v': /* void */
411 success = status == AE_OK;
412 break;
413 /* add more types as needed */
414 default:
415 printk(TPACPI_ERR "acpi_evalf() called "
416 "with invalid format character '%c'\n", res_type);
417 return 0;
420 if (!success && !quiet)
421 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
422 method, fmt0, status);
424 return success;
427 static int acpi_ec_read(int i, u8 *p)
429 int v;
431 if (ecrd_handle) {
432 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
433 return 0;
434 *p = v;
435 } else {
436 if (ec_read(i, p) < 0)
437 return 0;
440 return 1;
443 static int acpi_ec_write(int i, u8 v)
445 if (ecwr_handle) {
446 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
447 return 0;
448 } else {
449 if (ec_write(i, v) < 0)
450 return 0;
453 return 1;
456 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
457 static int _sta(acpi_handle handle)
459 int status;
461 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
462 status = 0;
464 return status;
466 #endif
468 static int issue_thinkpad_cmos_command(int cmos_cmd)
470 if (!cmos_handle)
471 return -ENXIO;
473 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
474 return -EIO;
476 return 0;
479 /*************************************************************************
480 * ACPI device model
483 #define TPACPI_ACPIHANDLE_INIT(object) \
484 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
485 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
487 static void drv_acpi_handle_init(char *name,
488 acpi_handle *handle, acpi_handle parent,
489 char **paths, int num_paths, char **path)
491 int i;
492 acpi_status status;
494 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
495 name);
497 for (i = 0; i < num_paths; i++) {
498 status = acpi_get_handle(parent, paths[i], handle);
499 if (ACPI_SUCCESS(status)) {
500 *path = paths[i];
501 dbg_printk(TPACPI_DBG_INIT,
502 "Found ACPI handle %s for %s\n",
503 *path, name);
504 return;
508 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
509 name);
510 *handle = NULL;
513 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
515 struct ibm_struct *ibm = data;
517 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
518 return;
520 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
521 return;
523 ibm->acpi->notify(ibm, event);
526 static int __init setup_acpi_notify(struct ibm_struct *ibm)
528 acpi_status status;
529 int rc;
531 BUG_ON(!ibm->acpi);
533 if (!*ibm->acpi->handle)
534 return 0;
536 vdbg_printk(TPACPI_DBG_INIT,
537 "setting up ACPI notify for %s\n", ibm->name);
539 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
540 if (rc < 0) {
541 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
542 ibm->name, rc);
543 return -ENODEV;
546 acpi_driver_data(ibm->acpi->device) = ibm;
547 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
548 TPACPI_ACPI_EVENT_PREFIX,
549 ibm->name);
551 status = acpi_install_notify_handler(*ibm->acpi->handle,
552 ibm->acpi->type, dispatch_acpi_notify, ibm);
553 if (ACPI_FAILURE(status)) {
554 if (status == AE_ALREADY_EXISTS) {
555 printk(TPACPI_NOTICE
556 "another device driver is already "
557 "handling %s events\n", ibm->name);
558 } else {
559 printk(TPACPI_ERR
560 "acpi_install_notify_handler(%s) failed: %d\n",
561 ibm->name, status);
563 return -ENODEV;
565 ibm->flags.acpi_notify_installed = 1;
566 return 0;
569 static int __init tpacpi_device_add(struct acpi_device *device)
571 return 0;
574 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
576 int rc;
578 dbg_printk(TPACPI_DBG_INIT,
579 "registering %s as an ACPI driver\n", ibm->name);
581 BUG_ON(!ibm->acpi);
583 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
584 if (!ibm->acpi->driver) {
585 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
586 return -ENOMEM;
589 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
590 ibm->acpi->driver->ids = ibm->acpi->hid;
592 ibm->acpi->driver->ops.add = &tpacpi_device_add;
594 rc = acpi_bus_register_driver(ibm->acpi->driver);
595 if (rc < 0) {
596 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
597 ibm->name, rc);
598 kfree(ibm->acpi->driver);
599 ibm->acpi->driver = NULL;
600 } else if (!rc)
601 ibm->flags.acpi_driver_registered = 1;
603 return rc;
607 /****************************************************************************
608 ****************************************************************************
610 * Procfs Helpers
612 ****************************************************************************
613 ****************************************************************************/
615 static int dispatch_procfs_read(char *page, char **start, off_t off,
616 int count, int *eof, void *data)
618 struct ibm_struct *ibm = data;
619 int len;
621 if (!ibm || !ibm->read)
622 return -EINVAL;
624 len = ibm->read(page);
625 if (len < 0)
626 return len;
628 if (len <= off + count)
629 *eof = 1;
630 *start = page + off;
631 len -= off;
632 if (len > count)
633 len = count;
634 if (len < 0)
635 len = 0;
637 return len;
640 static int dispatch_procfs_write(struct file *file,
641 const char __user *userbuf,
642 unsigned long count, void *data)
644 struct ibm_struct *ibm = data;
645 char *kernbuf;
646 int ret;
648 if (!ibm || !ibm->write)
649 return -EINVAL;
651 kernbuf = kmalloc(count + 2, GFP_KERNEL);
652 if (!kernbuf)
653 return -ENOMEM;
655 if (copy_from_user(kernbuf, userbuf, count)) {
656 kfree(kernbuf);
657 return -EFAULT;
660 kernbuf[count] = 0;
661 strcat(kernbuf, ",");
662 ret = ibm->write(kernbuf);
663 if (ret == 0)
664 ret = count;
666 kfree(kernbuf);
668 return ret;
671 static char *next_cmd(char **cmds)
673 char *start = *cmds;
674 char *end;
676 while ((end = strchr(start, ',')) && end == start)
677 start = end + 1;
679 if (!end)
680 return NULL;
682 *end = 0;
683 *cmds = end + 1;
684 return start;
688 /****************************************************************************
689 ****************************************************************************
691 * Device model: input, hwmon and platform
693 ****************************************************************************
694 ****************************************************************************/
696 static struct platform_device *tpacpi_pdev;
697 static struct platform_device *tpacpi_sensors_pdev;
698 static struct device *tpacpi_hwmon;
699 static struct input_dev *tpacpi_inputdev;
700 static struct mutex tpacpi_inputdev_send_mutex;
701 static LIST_HEAD(tpacpi_all_drivers);
703 static int tpacpi_suspend_handler(struct platform_device *pdev,
704 pm_message_t state)
706 struct ibm_struct *ibm, *itmp;
708 list_for_each_entry_safe(ibm, itmp,
709 &tpacpi_all_drivers,
710 all_drivers) {
711 if (ibm->suspend)
712 (ibm->suspend)(state);
715 return 0;
718 static int tpacpi_resume_handler(struct platform_device *pdev)
720 struct ibm_struct *ibm, *itmp;
722 list_for_each_entry_safe(ibm, itmp,
723 &tpacpi_all_drivers,
724 all_drivers) {
725 if (ibm->resume)
726 (ibm->resume)();
729 return 0;
732 static struct platform_driver tpacpi_pdriver = {
733 .driver = {
734 .name = TPACPI_DRVR_NAME,
735 .owner = THIS_MODULE,
737 .suspend = tpacpi_suspend_handler,
738 .resume = tpacpi_resume_handler,
741 static struct platform_driver tpacpi_hwmon_pdriver = {
742 .driver = {
743 .name = TPACPI_HWMON_DRVR_NAME,
744 .owner = THIS_MODULE,
748 /*************************************************************************
749 * sysfs support helpers
752 struct attribute_set {
753 unsigned int members, max_members;
754 struct attribute_group group;
757 struct attribute_set_obj {
758 struct attribute_set s;
759 struct attribute *a;
760 } __attribute__((packed));
762 static struct attribute_set *create_attr_set(unsigned int max_members,
763 const char *name)
765 struct attribute_set_obj *sobj;
767 if (max_members == 0)
768 return NULL;
770 /* Allocates space for implicit NULL at the end too */
771 sobj = kzalloc(sizeof(struct attribute_set_obj) +
772 max_members * sizeof(struct attribute *),
773 GFP_KERNEL);
774 if (!sobj)
775 return NULL;
776 sobj->s.max_members = max_members;
777 sobj->s.group.attrs = &sobj->a;
778 sobj->s.group.name = name;
780 return &sobj->s;
783 #define destroy_attr_set(_set) \
784 kfree(_set);
786 /* not multi-threaded safe, use it in a single thread per set */
787 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
789 if (!s || !attr)
790 return -EINVAL;
792 if (s->members >= s->max_members)
793 return -ENOMEM;
795 s->group.attrs[s->members] = attr;
796 s->members++;
798 return 0;
801 static int add_many_to_attr_set(struct attribute_set *s,
802 struct attribute **attr,
803 unsigned int count)
805 int i, res;
807 for (i = 0; i < count; i++) {
808 res = add_to_attr_set(s, attr[i]);
809 if (res)
810 return res;
813 return 0;
816 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
818 sysfs_remove_group(kobj, &s->group);
819 destroy_attr_set(s);
822 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
823 sysfs_create_group(_kobj, &_attr_set->group)
825 static int parse_strtoul(const char *buf,
826 unsigned long max, unsigned long *value)
828 char *endp;
830 while (*buf && isspace(*buf))
831 buf++;
832 *value = simple_strtoul(buf, &endp, 0);
833 while (*endp && isspace(*endp))
834 endp++;
835 if (*endp || *value > max)
836 return -EINVAL;
838 return 0;
841 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
843 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
844 union acpi_object *obj;
845 int rc;
847 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
848 obj = (union acpi_object *)buffer.pointer;
849 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
850 printk(TPACPI_ERR "Unknown _BCL data, "
851 "please report this to %s\n", TPACPI_MAIL);
852 rc = 0;
853 } else {
854 rc = obj->package.count;
856 } else {
857 return 0;
860 kfree(buffer.pointer);
861 return rc;
864 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
865 u32 lvl, void *context, void **rv)
867 char name[ACPI_PATH_SEGMENT_LENGTH];
868 struct acpi_buffer buffer = { sizeof(name), &name };
870 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
871 !strncmp("_BCL", name, sizeof(name) - 1)) {
872 BUG_ON(!rv || !*rv);
873 **(int **)rv = tpacpi_query_bcl_levels(handle);
874 return AE_CTRL_TERMINATE;
875 } else {
876 return AE_OK;
881 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
883 static int __init tpacpi_check_std_acpi_brightness_support(void)
885 int status;
886 int bcl_levels = 0;
887 void *bcl_ptr = &bcl_levels;
889 if (!vid_handle) {
890 TPACPI_ACPIHANDLE_INIT(vid);
892 if (!vid_handle)
893 return 0;
896 * Search for a _BCL method, and execute it. This is safe on all
897 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
898 * BIOS in ACPI backlight control mode. We do NOT have to care
899 * about calling the _BCL method in an enabled video device, any
900 * will do for our purposes.
903 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
904 tpacpi_acpi_walk_find_bcl, NULL,
905 &bcl_ptr);
907 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
908 tp_features.bright_acpimode = 1;
909 return (bcl_levels - 2);
912 return 0;
915 static int __init tpacpi_new_rfkill(const unsigned int id,
916 struct rfkill **rfk,
917 const enum rfkill_type rfktype,
918 const char* name,
919 int (*toggle_radio)(void *, enum rfkill_state),
920 int (*get_state)(void *, enum rfkill_state *))
922 int res;
924 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
925 if (!*rfk) {
926 printk(TPACPI_ERR
927 "failed to allocate memory for rfkill class\n");
928 return -ENOMEM;
931 (*rfk)->name = name;
932 (*rfk)->get_state = get_state;
933 (*rfk)->toggle_radio = toggle_radio;
935 res = rfkill_register(*rfk);
936 if (res < 0) {
937 printk(TPACPI_ERR
938 "failed to register %s rfkill switch: %d\n",
939 name, res);
940 rfkill_free(*rfk);
941 *rfk = NULL;
942 return res;
945 return 0;
948 /*************************************************************************
949 * thinkpad-acpi driver attributes
952 /* interface_version --------------------------------------------------- */
953 static ssize_t tpacpi_driver_interface_version_show(
954 struct device_driver *drv,
955 char *buf)
957 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
960 static DRIVER_ATTR(interface_version, S_IRUGO,
961 tpacpi_driver_interface_version_show, NULL);
963 /* debug_level --------------------------------------------------------- */
964 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
965 char *buf)
967 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
970 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
971 const char *buf, size_t count)
973 unsigned long t;
975 if (parse_strtoul(buf, 0xffff, &t))
976 return -EINVAL;
978 dbg_level = t;
980 return count;
983 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
984 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
986 /* version ------------------------------------------------------------- */
987 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
988 char *buf)
990 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
991 TPACPI_DESC, TPACPI_VERSION);
994 static DRIVER_ATTR(version, S_IRUGO,
995 tpacpi_driver_version_show, NULL);
997 /* --------------------------------------------------------------------- */
999 static struct driver_attribute *tpacpi_driver_attributes[] = {
1000 &driver_attr_debug_level, &driver_attr_version,
1001 &driver_attr_interface_version,
1004 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1006 int i, res;
1008 i = 0;
1009 res = 0;
1010 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1011 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1012 i++;
1015 return res;
1018 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1020 int i;
1022 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1023 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1026 /****************************************************************************
1027 ****************************************************************************
1029 * Subdrivers
1031 ****************************************************************************
1032 ****************************************************************************/
1034 /*************************************************************************
1035 * thinkpad-acpi init subdriver
1038 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1040 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1041 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1043 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1044 (thinkpad_id.bios_version_str) ?
1045 thinkpad_id.bios_version_str : "unknown",
1046 (thinkpad_id.ec_version_str) ?
1047 thinkpad_id.ec_version_str : "unknown");
1049 if (thinkpad_id.vendor && thinkpad_id.model_str)
1050 printk(TPACPI_INFO "%s %s, model %s\n",
1051 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1052 "IBM" : ((thinkpad_id.vendor ==
1053 PCI_VENDOR_ID_LENOVO) ?
1054 "Lenovo" : "Unknown vendor"),
1055 thinkpad_id.model_str,
1056 (thinkpad_id.nummodel_str) ?
1057 thinkpad_id.nummodel_str : "unknown");
1059 return 0;
1062 static int thinkpad_acpi_driver_read(char *p)
1064 int len = 0;
1066 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1067 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1069 return len;
1072 static struct ibm_struct thinkpad_acpi_driver_data = {
1073 .name = "driver",
1074 .read = thinkpad_acpi_driver_read,
1077 /*************************************************************************
1078 * Hotkey subdriver
1081 enum { /* hot key scan codes (derived from ACPI DSDT) */
1082 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1083 TP_ACPI_HOTKEYSCAN_FNF2,
1084 TP_ACPI_HOTKEYSCAN_FNF3,
1085 TP_ACPI_HOTKEYSCAN_FNF4,
1086 TP_ACPI_HOTKEYSCAN_FNF5,
1087 TP_ACPI_HOTKEYSCAN_FNF6,
1088 TP_ACPI_HOTKEYSCAN_FNF7,
1089 TP_ACPI_HOTKEYSCAN_FNF8,
1090 TP_ACPI_HOTKEYSCAN_FNF9,
1091 TP_ACPI_HOTKEYSCAN_FNF10,
1092 TP_ACPI_HOTKEYSCAN_FNF11,
1093 TP_ACPI_HOTKEYSCAN_FNF12,
1094 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1095 TP_ACPI_HOTKEYSCAN_FNINSERT,
1096 TP_ACPI_HOTKEYSCAN_FNDELETE,
1097 TP_ACPI_HOTKEYSCAN_FNHOME,
1098 TP_ACPI_HOTKEYSCAN_FNEND,
1099 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1100 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1101 TP_ACPI_HOTKEYSCAN_FNSPACE,
1102 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1103 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1104 TP_ACPI_HOTKEYSCAN_MUTE,
1105 TP_ACPI_HOTKEYSCAN_THINKPAD,
1108 enum { /* Keys available through NVRAM polling */
1109 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1110 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1113 enum { /* Positions of some of the keys in hotkey masks */
1114 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1115 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1116 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1117 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1118 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1119 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1120 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1121 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1122 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1123 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1124 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1127 enum { /* NVRAM to ACPI HKEY group map */
1128 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1129 TP_ACPI_HKEY_ZOOM_MASK |
1130 TP_ACPI_HKEY_DISPSWTCH_MASK |
1131 TP_ACPI_HKEY_HIBERNATE_MASK,
1132 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1133 TP_ACPI_HKEY_BRGHTDWN_MASK,
1134 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1135 TP_ACPI_HKEY_VOLDWN_MASK |
1136 TP_ACPI_HKEY_MUTE_MASK,
1139 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1140 struct tp_nvram_state {
1141 u16 thinkpad_toggle:1;
1142 u16 zoom_toggle:1;
1143 u16 display_toggle:1;
1144 u16 thinklight_toggle:1;
1145 u16 hibernate_toggle:1;
1146 u16 displayexp_toggle:1;
1147 u16 display_state:1;
1148 u16 brightness_toggle:1;
1149 u16 volume_toggle:1;
1150 u16 mute:1;
1152 u8 brightness_level;
1153 u8 volume_level;
1156 static struct task_struct *tpacpi_hotkey_task;
1157 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1158 static int hotkey_poll_freq = 10; /* Hz */
1159 static struct mutex hotkey_thread_mutex;
1160 static struct mutex hotkey_thread_data_mutex;
1161 static unsigned int hotkey_config_change;
1163 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1165 #define hotkey_source_mask 0U
1167 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1169 static struct mutex hotkey_mutex;
1171 static enum { /* Reasons for waking up */
1172 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1173 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1174 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1175 } hotkey_wakeup_reason;
1177 static int hotkey_autosleep_ack;
1179 static int hotkey_orig_status;
1180 static u32 hotkey_orig_mask;
1181 static u32 hotkey_all_mask;
1182 static u32 hotkey_reserved_mask;
1183 static u32 hotkey_mask;
1185 static unsigned int hotkey_report_mode;
1187 static u16 *hotkey_keycode_map;
1189 static struct attribute_set *hotkey_dev_attributes;
1191 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1192 #define HOTKEY_CONFIG_CRITICAL_START \
1193 do { \
1194 mutex_lock(&hotkey_thread_data_mutex); \
1195 hotkey_config_change++; \
1196 } while (0);
1197 #define HOTKEY_CONFIG_CRITICAL_END \
1198 mutex_unlock(&hotkey_thread_data_mutex);
1199 #else
1200 #define HOTKEY_CONFIG_CRITICAL_START
1201 #define HOTKEY_CONFIG_CRITICAL_END
1202 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1204 /* HKEY.MHKG() return bits */
1205 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1207 static int hotkey_get_wlsw(int *status)
1209 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1210 return -EIO;
1211 return 0;
1214 static int hotkey_get_tablet_mode(int *status)
1216 int s;
1218 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1219 return -EIO;
1221 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1222 return 0;
1226 * Call with hotkey_mutex held
1228 static int hotkey_mask_get(void)
1230 u32 m = 0;
1232 if (tp_features.hotkey_mask) {
1233 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1234 return -EIO;
1236 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1238 return 0;
1242 * Call with hotkey_mutex held
1244 static int hotkey_mask_set(u32 mask)
1246 int i;
1247 int rc = 0;
1249 if (tp_features.hotkey_mask) {
1250 if (!tp_warned.hotkey_mask_ff &&
1251 (mask == 0xffff || mask == 0xffffff ||
1252 mask == 0xffffffff)) {
1253 tp_warned.hotkey_mask_ff = 1;
1254 printk(TPACPI_NOTICE
1255 "setting the hotkey mask to 0x%08x is likely "
1256 "not the best way to go about it\n", mask);
1257 printk(TPACPI_NOTICE
1258 "please consider using the driver defaults, "
1259 "and refer to up-to-date thinkpad-acpi "
1260 "documentation\n");
1263 HOTKEY_CONFIG_CRITICAL_START
1264 for (i = 0; i < 32; i++) {
1265 u32 m = 1 << i;
1266 /* enable in firmware mask only keys not in NVRAM
1267 * mode, but enable the key in the cached hotkey_mask
1268 * regardless of mode, or the key will end up
1269 * disabled by hotkey_mask_get() */
1270 if (!acpi_evalf(hkey_handle,
1271 NULL, "MHKM", "vdd", i + 1,
1272 !!((mask & ~hotkey_source_mask) & m))) {
1273 rc = -EIO;
1274 break;
1275 } else {
1276 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1279 HOTKEY_CONFIG_CRITICAL_END
1281 /* hotkey_mask_get must be called unconditionally below */
1282 if (!hotkey_mask_get() && !rc &&
1283 (hotkey_mask & ~hotkey_source_mask) !=
1284 (mask & ~hotkey_source_mask)) {
1285 printk(TPACPI_NOTICE
1286 "requested hot key mask 0x%08x, but "
1287 "firmware forced it to 0x%08x\n",
1288 mask, hotkey_mask);
1290 } else {
1291 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1292 HOTKEY_CONFIG_CRITICAL_START
1293 hotkey_mask = mask & hotkey_source_mask;
1294 HOTKEY_CONFIG_CRITICAL_END
1295 hotkey_mask_get();
1296 if (hotkey_mask != mask) {
1297 printk(TPACPI_NOTICE
1298 "requested hot key mask 0x%08x, "
1299 "forced to 0x%08x (NVRAM poll mask is "
1300 "0x%08x): no firmware mask support\n",
1301 mask, hotkey_mask, hotkey_source_mask);
1303 #else
1304 hotkey_mask_get();
1305 rc = -ENXIO;
1306 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1309 return rc;
1312 static int hotkey_status_get(int *status)
1314 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1315 return -EIO;
1317 return 0;
1320 static int hotkey_status_set(int status)
1322 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1323 return -EIO;
1325 return 0;
1328 static void tpacpi_input_send_tabletsw(void)
1330 int state;
1332 if (tp_features.hotkey_tablet &&
1333 !hotkey_get_tablet_mode(&state)) {
1334 mutex_lock(&tpacpi_inputdev_send_mutex);
1336 input_report_switch(tpacpi_inputdev,
1337 SW_TABLET_MODE, !!state);
1338 input_sync(tpacpi_inputdev);
1340 mutex_unlock(&tpacpi_inputdev_send_mutex);
1344 static void tpacpi_input_send_key(unsigned int scancode)
1346 unsigned int keycode;
1348 keycode = hotkey_keycode_map[scancode];
1350 if (keycode != KEY_RESERVED) {
1351 mutex_lock(&tpacpi_inputdev_send_mutex);
1353 input_report_key(tpacpi_inputdev, keycode, 1);
1354 if (keycode == KEY_UNKNOWN)
1355 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1356 scancode);
1357 input_sync(tpacpi_inputdev);
1359 input_report_key(tpacpi_inputdev, keycode, 0);
1360 if (keycode == KEY_UNKNOWN)
1361 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1362 scancode);
1363 input_sync(tpacpi_inputdev);
1365 mutex_unlock(&tpacpi_inputdev_send_mutex);
1369 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1370 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1372 static void tpacpi_hotkey_send_key(unsigned int scancode)
1374 tpacpi_input_send_key(scancode);
1375 if (hotkey_report_mode < 2) {
1376 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1377 0x80, 0x1001 + scancode);
1381 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1383 u8 d;
1385 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1386 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1387 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1388 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1389 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1390 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1392 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1393 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1394 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1396 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1397 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1398 n->displayexp_toggle =
1399 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1401 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1402 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1403 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1404 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1405 n->brightness_toggle =
1406 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1408 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1409 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1410 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1411 >> TP_NVRAM_POS_LEVEL_VOLUME;
1412 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1413 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1417 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1418 do { \
1419 if ((mask & (1 << __scancode)) && \
1420 oldn->__member != newn->__member) \
1421 tpacpi_hotkey_send_key(__scancode); \
1422 } while (0)
1424 #define TPACPI_MAY_SEND_KEY(__scancode) \
1425 do { if (mask & (1 << __scancode)) \
1426 tpacpi_hotkey_send_key(__scancode); } while (0)
1428 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1429 struct tp_nvram_state *newn,
1430 u32 mask)
1432 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1433 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1434 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1435 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1437 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1439 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1441 /* handle volume */
1442 if (oldn->volume_toggle != newn->volume_toggle) {
1443 if (oldn->mute != newn->mute) {
1444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1446 if (oldn->volume_level > newn->volume_level) {
1447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1448 } else if (oldn->volume_level < newn->volume_level) {
1449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1450 } else if (oldn->mute == newn->mute) {
1451 /* repeated key presses that didn't change state */
1452 if (newn->mute) {
1453 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1454 } else if (newn->volume_level != 0) {
1455 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1456 } else {
1457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1462 /* handle brightness */
1463 if (oldn->brightness_toggle != newn->brightness_toggle) {
1464 if (oldn->brightness_level < newn->brightness_level) {
1465 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1466 } else if (oldn->brightness_level > newn->brightness_level) {
1467 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1468 } else {
1469 /* repeated key presses that didn't change state */
1470 if (newn->brightness_level != 0) {
1471 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1472 } else {
1473 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1479 #undef TPACPI_COMPARE_KEY
1480 #undef TPACPI_MAY_SEND_KEY
1482 static int hotkey_kthread(void *data)
1484 struct tp_nvram_state s[2];
1485 u32 mask;
1486 unsigned int si, so;
1487 unsigned long t;
1488 unsigned int change_detector, must_reset;
1490 mutex_lock(&hotkey_thread_mutex);
1492 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1493 goto exit;
1495 set_freezable();
1497 so = 0;
1498 si = 1;
1499 t = 0;
1501 /* Initial state for compares */
1502 mutex_lock(&hotkey_thread_data_mutex);
1503 change_detector = hotkey_config_change;
1504 mask = hotkey_source_mask & hotkey_mask;
1505 mutex_unlock(&hotkey_thread_data_mutex);
1506 hotkey_read_nvram(&s[so], mask);
1508 while (!kthread_should_stop() && hotkey_poll_freq) {
1509 if (t == 0)
1510 t = 1000/hotkey_poll_freq;
1511 t = msleep_interruptible(t);
1512 if (unlikely(kthread_should_stop()))
1513 break;
1514 must_reset = try_to_freeze();
1515 if (t > 0 && !must_reset)
1516 continue;
1518 mutex_lock(&hotkey_thread_data_mutex);
1519 if (must_reset || hotkey_config_change != change_detector) {
1520 /* forget old state on thaw or config change */
1521 si = so;
1522 t = 0;
1523 change_detector = hotkey_config_change;
1525 mask = hotkey_source_mask & hotkey_mask;
1526 mutex_unlock(&hotkey_thread_data_mutex);
1528 if (likely(mask)) {
1529 hotkey_read_nvram(&s[si], mask);
1530 if (likely(si != so)) {
1531 hotkey_compare_and_issue_event(&s[so], &s[si],
1532 mask);
1536 so = si;
1537 si ^= 1;
1540 exit:
1541 mutex_unlock(&hotkey_thread_mutex);
1542 return 0;
1545 static void hotkey_poll_stop_sync(void)
1547 if (tpacpi_hotkey_task) {
1548 if (frozen(tpacpi_hotkey_task) ||
1549 freezing(tpacpi_hotkey_task))
1550 thaw_process(tpacpi_hotkey_task);
1552 kthread_stop(tpacpi_hotkey_task);
1553 tpacpi_hotkey_task = NULL;
1554 mutex_lock(&hotkey_thread_mutex);
1555 /* at this point, the thread did exit */
1556 mutex_unlock(&hotkey_thread_mutex);
1560 /* call with hotkey_mutex held */
1561 static void hotkey_poll_setup(int may_warn)
1563 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1564 hotkey_poll_freq > 0 &&
1565 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1566 if (!tpacpi_hotkey_task) {
1567 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1568 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1569 if (IS_ERR(tpacpi_hotkey_task)) {
1570 tpacpi_hotkey_task = NULL;
1571 printk(TPACPI_ERR
1572 "could not create kernel thread "
1573 "for hotkey polling\n");
1576 } else {
1577 hotkey_poll_stop_sync();
1578 if (may_warn &&
1579 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1580 printk(TPACPI_NOTICE
1581 "hot keys 0x%08x require polling, "
1582 "which is currently disabled\n",
1583 hotkey_source_mask);
1588 static void hotkey_poll_setup_safe(int may_warn)
1590 mutex_lock(&hotkey_mutex);
1591 hotkey_poll_setup(may_warn);
1592 mutex_unlock(&hotkey_mutex);
1595 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1597 static void hotkey_poll_setup_safe(int __unused)
1601 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1603 static int hotkey_inputdev_open(struct input_dev *dev)
1605 switch (tpacpi_lifecycle) {
1606 case TPACPI_LIFE_INIT:
1608 * hotkey_init will call hotkey_poll_setup_safe
1609 * at the appropriate moment
1611 return 0;
1612 case TPACPI_LIFE_EXITING:
1613 return -EBUSY;
1614 case TPACPI_LIFE_RUNNING:
1615 hotkey_poll_setup_safe(0);
1616 return 0;
1619 /* Should only happen if tpacpi_lifecycle is corrupt */
1620 BUG();
1621 return -EBUSY;
1624 static void hotkey_inputdev_close(struct input_dev *dev)
1626 /* disable hotkey polling when possible */
1627 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1628 hotkey_poll_setup_safe(0);
1631 /* sysfs hotkey enable ------------------------------------------------- */
1632 static ssize_t hotkey_enable_show(struct device *dev,
1633 struct device_attribute *attr,
1634 char *buf)
1636 int res, status;
1638 res = hotkey_status_get(&status);
1639 if (res)
1640 return res;
1642 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1645 static ssize_t hotkey_enable_store(struct device *dev,
1646 struct device_attribute *attr,
1647 const char *buf, size_t count)
1649 unsigned long t;
1650 int res;
1652 if (parse_strtoul(buf, 1, &t))
1653 return -EINVAL;
1655 res = hotkey_status_set(t);
1657 return (res) ? res : count;
1660 static struct device_attribute dev_attr_hotkey_enable =
1661 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1662 hotkey_enable_show, hotkey_enable_store);
1664 /* sysfs hotkey mask --------------------------------------------------- */
1665 static ssize_t hotkey_mask_show(struct device *dev,
1666 struct device_attribute *attr,
1667 char *buf)
1669 int res;
1671 if (mutex_lock_interruptible(&hotkey_mutex))
1672 return -ERESTARTSYS;
1673 res = hotkey_mask_get();
1674 mutex_unlock(&hotkey_mutex);
1676 return (res)?
1677 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1680 static ssize_t hotkey_mask_store(struct device *dev,
1681 struct device_attribute *attr,
1682 const char *buf, size_t count)
1684 unsigned long t;
1685 int res;
1687 if (parse_strtoul(buf, 0xffffffffUL, &t))
1688 return -EINVAL;
1690 if (mutex_lock_interruptible(&hotkey_mutex))
1691 return -ERESTARTSYS;
1693 res = hotkey_mask_set(t);
1695 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1696 hotkey_poll_setup(1);
1697 #endif
1699 mutex_unlock(&hotkey_mutex);
1701 return (res) ? res : count;
1704 static struct device_attribute dev_attr_hotkey_mask =
1705 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1706 hotkey_mask_show, hotkey_mask_store);
1708 /* sysfs hotkey bios_enabled ------------------------------------------- */
1709 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1710 struct device_attribute *attr,
1711 char *buf)
1713 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1716 static struct device_attribute dev_attr_hotkey_bios_enabled =
1717 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1719 /* sysfs hotkey bios_mask ---------------------------------------------- */
1720 static ssize_t hotkey_bios_mask_show(struct device *dev,
1721 struct device_attribute *attr,
1722 char *buf)
1724 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1727 static struct device_attribute dev_attr_hotkey_bios_mask =
1728 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1730 /* sysfs hotkey all_mask ----------------------------------------------- */
1731 static ssize_t hotkey_all_mask_show(struct device *dev,
1732 struct device_attribute *attr,
1733 char *buf)
1735 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1736 hotkey_all_mask | hotkey_source_mask);
1739 static struct device_attribute dev_attr_hotkey_all_mask =
1740 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1742 /* sysfs hotkey recommended_mask --------------------------------------- */
1743 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1744 struct device_attribute *attr,
1745 char *buf)
1747 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1748 (hotkey_all_mask | hotkey_source_mask)
1749 & ~hotkey_reserved_mask);
1752 static struct device_attribute dev_attr_hotkey_recommended_mask =
1753 __ATTR(hotkey_recommended_mask, S_IRUGO,
1754 hotkey_recommended_mask_show, NULL);
1756 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1758 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1759 static ssize_t hotkey_source_mask_show(struct device *dev,
1760 struct device_attribute *attr,
1761 char *buf)
1763 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1766 static ssize_t hotkey_source_mask_store(struct device *dev,
1767 struct device_attribute *attr,
1768 const char *buf, size_t count)
1770 unsigned long t;
1772 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1773 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1774 return -EINVAL;
1776 if (mutex_lock_interruptible(&hotkey_mutex))
1777 return -ERESTARTSYS;
1779 HOTKEY_CONFIG_CRITICAL_START
1780 hotkey_source_mask = t;
1781 HOTKEY_CONFIG_CRITICAL_END
1783 hotkey_poll_setup(1);
1785 mutex_unlock(&hotkey_mutex);
1787 return count;
1790 static struct device_attribute dev_attr_hotkey_source_mask =
1791 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1792 hotkey_source_mask_show, hotkey_source_mask_store);
1794 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1795 static ssize_t hotkey_poll_freq_show(struct device *dev,
1796 struct device_attribute *attr,
1797 char *buf)
1799 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1802 static ssize_t hotkey_poll_freq_store(struct device *dev,
1803 struct device_attribute *attr,
1804 const char *buf, size_t count)
1806 unsigned long t;
1808 if (parse_strtoul(buf, 25, &t))
1809 return -EINVAL;
1811 if (mutex_lock_interruptible(&hotkey_mutex))
1812 return -ERESTARTSYS;
1814 hotkey_poll_freq = t;
1816 hotkey_poll_setup(1);
1817 mutex_unlock(&hotkey_mutex);
1819 return count;
1822 static struct device_attribute dev_attr_hotkey_poll_freq =
1823 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1824 hotkey_poll_freq_show, hotkey_poll_freq_store);
1826 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1828 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1829 static ssize_t hotkey_radio_sw_show(struct device *dev,
1830 struct device_attribute *attr,
1831 char *buf)
1833 int res, s;
1834 res = hotkey_get_wlsw(&s);
1835 if (res < 0)
1836 return res;
1838 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1841 static struct device_attribute dev_attr_hotkey_radio_sw =
1842 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1844 static void hotkey_radio_sw_notify_change(void)
1846 if (tp_features.hotkey_wlsw)
1847 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1848 "hotkey_radio_sw");
1851 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1852 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1853 struct device_attribute *attr,
1854 char *buf)
1856 int res, s;
1857 res = hotkey_get_tablet_mode(&s);
1858 if (res < 0)
1859 return res;
1861 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1864 static struct device_attribute dev_attr_hotkey_tablet_mode =
1865 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1867 static void hotkey_tablet_mode_notify_change(void)
1869 if (tp_features.hotkey_tablet)
1870 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1871 "hotkey_tablet_mode");
1874 /* sysfs hotkey report_mode -------------------------------------------- */
1875 static ssize_t hotkey_report_mode_show(struct device *dev,
1876 struct device_attribute *attr,
1877 char *buf)
1879 return snprintf(buf, PAGE_SIZE, "%d\n",
1880 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1883 static struct device_attribute dev_attr_hotkey_report_mode =
1884 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1886 /* sysfs wakeup reason (pollable) -------------------------------------- */
1887 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1888 struct device_attribute *attr,
1889 char *buf)
1891 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1894 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1895 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1897 static void hotkey_wakeup_reason_notify_change(void)
1899 if (tp_features.hotkey_mask)
1900 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1901 "wakeup_reason");
1904 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1905 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1906 struct device_attribute *attr,
1907 char *buf)
1909 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1912 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1913 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1914 hotkey_wakeup_hotunplug_complete_show, NULL);
1916 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1918 if (tp_features.hotkey_mask)
1919 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1920 "wakeup_hotunplug_complete");
1923 /* --------------------------------------------------------------------- */
1925 static struct attribute *hotkey_attributes[] __initdata = {
1926 &dev_attr_hotkey_enable.attr,
1927 &dev_attr_hotkey_bios_enabled.attr,
1928 &dev_attr_hotkey_report_mode.attr,
1929 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1930 &dev_attr_hotkey_mask.attr,
1931 &dev_attr_hotkey_all_mask.attr,
1932 &dev_attr_hotkey_recommended_mask.attr,
1933 &dev_attr_hotkey_source_mask.attr,
1934 &dev_attr_hotkey_poll_freq.attr,
1935 #endif
1938 static struct attribute *hotkey_mask_attributes[] __initdata = {
1939 &dev_attr_hotkey_bios_mask.attr,
1940 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1941 &dev_attr_hotkey_mask.attr,
1942 &dev_attr_hotkey_all_mask.attr,
1943 &dev_attr_hotkey_recommended_mask.attr,
1944 #endif
1945 &dev_attr_hotkey_wakeup_reason.attr,
1946 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1949 static void bluetooth_update_rfk(void);
1950 static void wan_update_rfk(void);
1951 static void tpacpi_send_radiosw_update(void)
1953 int wlsw;
1955 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1956 mutex_lock(&tpacpi_inputdev_send_mutex);
1958 input_report_switch(tpacpi_inputdev,
1959 SW_RFKILL_ALL, !!wlsw);
1960 input_sync(tpacpi_inputdev);
1962 mutex_unlock(&tpacpi_inputdev_send_mutex);
1964 hotkey_radio_sw_notify_change();
1966 if (tp_features.bluetooth)
1967 bluetooth_update_rfk();
1968 if (tp_features.wan)
1969 wan_update_rfk();
1972 static void hotkey_exit(void)
1974 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1975 hotkey_poll_stop_sync();
1976 #endif
1978 if (hotkey_dev_attributes)
1979 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1981 kfree(hotkey_keycode_map);
1983 if (tp_features.hotkey) {
1984 dbg_printk(TPACPI_DBG_EXIT,
1985 "restoring original hot key mask\n");
1986 /* no short-circuit boolean operator below! */
1987 if ((hotkey_mask_set(hotkey_orig_mask) |
1988 hotkey_status_set(hotkey_orig_status)) != 0)
1989 printk(TPACPI_ERR
1990 "failed to restore hot key mask "
1991 "to BIOS defaults\n");
1995 static int __init hotkey_init(struct ibm_init_struct *iibm)
1997 /* Requirements for changing the default keymaps:
1999 * 1. Many of the keys are mapped to KEY_RESERVED for very
2000 * good reasons. Do not change them unless you have deep
2001 * knowledge on the IBM and Lenovo ThinkPad firmware for
2002 * the various ThinkPad models. The driver behaves
2003 * differently for KEY_RESERVED: such keys have their
2004 * hot key mask *unset* in mask_recommended, and also
2005 * in the initial hot key mask programmed into the
2006 * firmware at driver load time, which means the firm-
2007 * ware may react very differently if you change them to
2008 * something else;
2010 * 2. You must be subscribed to the linux-thinkpad and
2011 * ibm-acpi-devel mailing lists, and you should read the
2012 * list archives since 2007 if you want to change the
2013 * keymaps. This requirement exists so that you will
2014 * know the past history of problems with the thinkpad-
2015 * acpi driver keymaps, and also that you will be
2016 * listening to any bug reports;
2018 * 3. Do not send thinkpad-acpi specific patches directly to
2019 * for merging, *ever*. Send them to the linux-acpi
2020 * mailinglist for comments. Merging is to be done only
2021 * through acpi-test and the ACPI maintainer.
2023 * If the above is too much to ask, don't change the keymap.
2024 * Ask the thinkpad-acpi maintainer to do it, instead.
2026 static u16 ibm_keycode_map[] __initdata = {
2027 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2028 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2029 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2030 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2032 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2033 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2034 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2035 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2037 /* brightness: firmware always reacts to them, unless
2038 * X.org did some tricks in the radeon BIOS scratch
2039 * registers of *some* models */
2040 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2041 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2043 /* Thinklight: firmware always react to it */
2044 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2046 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2047 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2049 /* Volume: firmware always react to it and reprograms
2050 * the built-in *extra* mixer. Never map it to control
2051 * another mixer by default. */
2052 KEY_RESERVED, /* 0x14: VOLUME UP */
2053 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2054 KEY_RESERVED, /* 0x16: MUTE */
2056 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2058 /* (assignments unknown, please report if found) */
2059 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2060 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2062 static u16 lenovo_keycode_map[] __initdata = {
2063 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2064 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2065 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2066 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2068 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2069 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2070 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2071 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2073 /* These either have to go through ACPI video, or
2074 * act like in the IBM ThinkPads, so don't ever
2075 * enable them by default */
2076 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2077 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2079 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2081 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2082 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2084 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2085 * react to it and reprograms the built-in *extra* mixer.
2086 * Never map it to control another mixer by default.
2088 * T60?, T61, R60?, R61: firmware and EC tries to send
2089 * these over the regular keyboard, so these are no-ops,
2090 * but there are still weird bugs re. MUTE, so do not
2091 * change unless you get test reports from all Lenovo
2092 * models. May cause the BIOS to interfere with the
2093 * HDA mixer.
2095 KEY_RESERVED, /* 0x14: VOLUME UP */
2096 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2097 KEY_RESERVED, /* 0x16: MUTE */
2099 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2101 /* (assignments unknown, please report if found) */
2102 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2103 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2106 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2107 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2108 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2110 int res, i;
2111 int status;
2112 int hkeyv;
2114 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2116 BUG_ON(!tpacpi_inputdev);
2117 BUG_ON(tpacpi_inputdev->open != NULL ||
2118 tpacpi_inputdev->close != NULL);
2120 TPACPI_ACPIHANDLE_INIT(hkey);
2121 mutex_init(&hotkey_mutex);
2123 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2124 mutex_init(&hotkey_thread_mutex);
2125 mutex_init(&hotkey_thread_data_mutex);
2126 #endif
2128 /* hotkey not supported on 570 */
2129 tp_features.hotkey = hkey_handle != NULL;
2131 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2132 str_supported(tp_features.hotkey));
2134 if (!tp_features.hotkey)
2135 return 1;
2137 hotkey_dev_attributes = create_attr_set(13, NULL);
2138 if (!hotkey_dev_attributes)
2139 return -ENOMEM;
2140 res = add_many_to_attr_set(hotkey_dev_attributes,
2141 hotkey_attributes,
2142 ARRAY_SIZE(hotkey_attributes));
2143 if (res)
2144 goto err_exit;
2146 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2147 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2148 for HKEY interface version 0x100 */
2149 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2150 if ((hkeyv >> 8) != 1) {
2151 printk(TPACPI_ERR "unknown version of the "
2152 "HKEY interface: 0x%x\n", hkeyv);
2153 printk(TPACPI_ERR "please report this to %s\n",
2154 TPACPI_MAIL);
2155 } else {
2157 * MHKV 0x100 in A31, R40, R40e,
2158 * T4x, X31, and later
2160 tp_features.hotkey_mask = 1;
2164 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2165 str_supported(tp_features.hotkey_mask));
2167 if (tp_features.hotkey_mask) {
2168 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2169 "MHKA", "qd")) {
2170 printk(TPACPI_ERR
2171 "missing MHKA handler, "
2172 "please report this to %s\n",
2173 TPACPI_MAIL);
2174 /* FN+F12, FN+F4, FN+F3 */
2175 hotkey_all_mask = 0x080cU;
2179 /* hotkey_source_mask *must* be zero for
2180 * the first hotkey_mask_get */
2181 res = hotkey_status_get(&hotkey_orig_status);
2182 if (res)
2183 goto err_exit;
2185 if (tp_features.hotkey_mask) {
2186 res = hotkey_mask_get();
2187 if (res)
2188 goto err_exit;
2190 hotkey_orig_mask = hotkey_mask;
2191 res = add_many_to_attr_set(
2192 hotkey_dev_attributes,
2193 hotkey_mask_attributes,
2194 ARRAY_SIZE(hotkey_mask_attributes));
2195 if (res)
2196 goto err_exit;
2199 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2200 if (tp_features.hotkey_mask) {
2201 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2202 & ~hotkey_all_mask;
2203 } else {
2204 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2207 vdbg_printk(TPACPI_DBG_INIT,
2208 "hotkey source mask 0x%08x, polling freq %d\n",
2209 hotkey_source_mask, hotkey_poll_freq);
2210 #endif
2212 /* Not all thinkpads have a hardware radio switch */
2213 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2214 tp_features.hotkey_wlsw = 1;
2215 printk(TPACPI_INFO
2216 "radio switch found; radios are %s\n",
2217 enabled(status, 0));
2219 if (tp_features.hotkey_wlsw)
2220 res = add_to_attr_set(hotkey_dev_attributes,
2221 &dev_attr_hotkey_radio_sw.attr);
2223 /* For X41t, X60t, X61t Tablets... */
2224 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2225 tp_features.hotkey_tablet = 1;
2226 printk(TPACPI_INFO
2227 "possible tablet mode switch found; "
2228 "ThinkPad in %s mode\n",
2229 (status & TP_HOTKEY_TABLET_MASK)?
2230 "tablet" : "laptop");
2231 res = add_to_attr_set(hotkey_dev_attributes,
2232 &dev_attr_hotkey_tablet_mode.attr);
2235 if (!res)
2236 res = register_attr_set_with_sysfs(
2237 hotkey_dev_attributes,
2238 &tpacpi_pdev->dev.kobj);
2239 if (res)
2240 goto err_exit;
2242 /* Set up key map */
2244 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2245 GFP_KERNEL);
2246 if (!hotkey_keycode_map) {
2247 printk(TPACPI_ERR
2248 "failed to allocate memory for key map\n");
2249 res = -ENOMEM;
2250 goto err_exit;
2253 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2254 dbg_printk(TPACPI_DBG_INIT,
2255 "using Lenovo default hot key map\n");
2256 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2257 TPACPI_HOTKEY_MAP_SIZE);
2258 } else {
2259 dbg_printk(TPACPI_DBG_INIT,
2260 "using IBM default hot key map\n");
2261 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2262 TPACPI_HOTKEY_MAP_SIZE);
2265 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2266 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2267 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2268 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2269 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2270 tpacpi_inputdev->keycode = hotkey_keycode_map;
2271 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2272 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2273 set_bit(hotkey_keycode_map[i],
2274 tpacpi_inputdev->keybit);
2275 } else {
2276 if (i < sizeof(hotkey_reserved_mask)*8)
2277 hotkey_reserved_mask |= 1 << i;
2281 if (tp_features.hotkey_wlsw) {
2282 set_bit(EV_SW, tpacpi_inputdev->evbit);
2283 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2285 if (tp_features.hotkey_tablet) {
2286 set_bit(EV_SW, tpacpi_inputdev->evbit);
2287 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2290 /* Do not issue duplicate brightness change events to
2291 * userspace */
2292 if (!tp_features.bright_acpimode)
2293 /* update bright_acpimode... */
2294 tpacpi_check_std_acpi_brightness_support();
2296 if (tp_features.bright_acpimode) {
2297 printk(TPACPI_INFO
2298 "This ThinkPad has standard ACPI backlight "
2299 "brightness control, supported by the ACPI "
2300 "video driver\n");
2301 printk(TPACPI_NOTICE
2302 "Disabling thinkpad-acpi brightness events "
2303 "by default...\n");
2305 /* The hotkey_reserved_mask change below is not
2306 * necessary while the keys are at KEY_RESERVED in the
2307 * default map, but better safe than sorry, leave it
2308 * here as a marker of what we have to do, especially
2309 * when we finally become able to set this at runtime
2310 * on response to X.org requests */
2311 hotkey_reserved_mask |=
2312 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2313 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2316 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2317 res = hotkey_status_set(1);
2318 if (res) {
2319 hotkey_exit();
2320 return res;
2322 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2323 & ~hotkey_reserved_mask)
2324 | hotkey_orig_mask);
2325 if (res < 0 && res != -ENXIO) {
2326 hotkey_exit();
2327 return res;
2330 dbg_printk(TPACPI_DBG_INIT,
2331 "legacy hot key reporting over procfs %s\n",
2332 (hotkey_report_mode < 2) ?
2333 "enabled" : "disabled");
2335 tpacpi_inputdev->open = &hotkey_inputdev_open;
2336 tpacpi_inputdev->close = &hotkey_inputdev_close;
2338 hotkey_poll_setup_safe(1);
2339 tpacpi_send_radiosw_update();
2340 tpacpi_input_send_tabletsw();
2342 return 0;
2344 err_exit:
2345 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2346 hotkey_dev_attributes = NULL;
2348 return (res < 0)? res : 1;
2351 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2353 u32 hkey;
2354 unsigned int scancode;
2355 int send_acpi_ev;
2356 int ignore_acpi_ev;
2357 int unk_ev;
2359 if (event != 0x80) {
2360 printk(TPACPI_ERR
2361 "unknown HKEY notification event %d\n", event);
2362 /* forward it to userspace, maybe it knows how to handle it */
2363 acpi_bus_generate_netlink_event(
2364 ibm->acpi->device->pnp.device_class,
2365 ibm->acpi->device->dev.bus_id,
2366 event, 0);
2367 return;
2370 while (1) {
2371 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2372 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2373 return;
2376 if (hkey == 0) {
2377 /* queue empty */
2378 return;
2381 send_acpi_ev = 1;
2382 ignore_acpi_ev = 0;
2383 unk_ev = 0;
2385 switch (hkey >> 12) {
2386 case 1:
2387 /* 0x1000-0x1FFF: key presses */
2388 scancode = hkey & 0xfff;
2389 if (scancode > 0 && scancode < 0x21) {
2390 scancode--;
2391 if (!(hotkey_source_mask & (1 << scancode))) {
2392 tpacpi_input_send_key(scancode);
2393 send_acpi_ev = 0;
2394 } else {
2395 ignore_acpi_ev = 1;
2397 } else {
2398 unk_ev = 1;
2400 break;
2401 case 2:
2402 /* Wakeup reason */
2403 switch (hkey) {
2404 case 0x2304: /* suspend, undock */
2405 case 0x2404: /* hibernation, undock */
2406 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2407 ignore_acpi_ev = 1;
2408 break;
2409 case 0x2305: /* suspend, bay eject */
2410 case 0x2405: /* hibernation, bay eject */
2411 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2412 ignore_acpi_ev = 1;
2413 break;
2414 default:
2415 unk_ev = 1;
2417 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2418 printk(TPACPI_INFO
2419 "woke up due to a hot-unplug "
2420 "request...\n");
2421 hotkey_wakeup_reason_notify_change();
2423 break;
2424 case 3:
2425 /* bay-related wakeups */
2426 if (hkey == 0x3003) {
2427 hotkey_autosleep_ack = 1;
2428 printk(TPACPI_INFO
2429 "bay ejected\n");
2430 hotkey_wakeup_hotunplug_complete_notify_change();
2431 } else {
2432 unk_ev = 1;
2434 break;
2435 case 4:
2436 /* dock-related wakeups */
2437 if (hkey == 0x4003) {
2438 hotkey_autosleep_ack = 1;
2439 printk(TPACPI_INFO
2440 "undocked\n");
2441 hotkey_wakeup_hotunplug_complete_notify_change();
2442 } else {
2443 unk_ev = 1;
2445 break;
2446 case 5:
2447 /* 0x5000-0x5FFF: human interface helpers */
2448 switch (hkey) {
2449 case 0x5010: /* Lenovo new BIOS: brightness changed */
2450 case 0x500b: /* X61t: tablet pen inserted into bay */
2451 case 0x500c: /* X61t: tablet pen removed from bay */
2452 break;
2453 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2454 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2455 tpacpi_input_send_tabletsw();
2456 hotkey_tablet_mode_notify_change();
2457 send_acpi_ev = 0;
2458 break;
2459 case 0x5001:
2460 case 0x5002:
2461 /* LID switch events. Do not propagate */
2462 ignore_acpi_ev = 1;
2463 break;
2464 default:
2465 unk_ev = 1;
2467 break;
2468 case 7:
2469 /* 0x7000-0x7FFF: misc */
2470 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2471 tpacpi_send_radiosw_update();
2472 send_acpi_ev = 0;
2473 break;
2475 /* fallthrough to default */
2476 default:
2477 unk_ev = 1;
2479 if (unk_ev) {
2480 printk(TPACPI_NOTICE
2481 "unhandled HKEY event 0x%04x\n", hkey);
2484 /* Legacy events */
2485 if (!ignore_acpi_ev &&
2486 (send_acpi_ev || hotkey_report_mode < 2)) {
2487 acpi_bus_generate_proc_event(ibm->acpi->device,
2488 event, hkey);
2491 /* netlink events */
2492 if (!ignore_acpi_ev && send_acpi_ev) {
2493 acpi_bus_generate_netlink_event(
2494 ibm->acpi->device->pnp.device_class,
2495 ibm->acpi->device->dev.bus_id,
2496 event, hkey);
2501 static void hotkey_suspend(pm_message_t state)
2503 /* Do these on suspend, we get the events on early resume! */
2504 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2505 hotkey_autosleep_ack = 0;
2508 static void hotkey_resume(void)
2510 if (hotkey_mask_get())
2511 printk(TPACPI_ERR
2512 "error while trying to read hot key mask "
2513 "from firmware\n");
2514 tpacpi_send_radiosw_update();
2515 hotkey_tablet_mode_notify_change();
2516 hotkey_wakeup_reason_notify_change();
2517 hotkey_wakeup_hotunplug_complete_notify_change();
2518 hotkey_poll_setup_safe(0);
2521 /* procfs -------------------------------------------------------------- */
2522 static int hotkey_read(char *p)
2524 int res, status;
2525 int len = 0;
2527 if (!tp_features.hotkey) {
2528 len += sprintf(p + len, "status:\t\tnot supported\n");
2529 return len;
2532 if (mutex_lock_interruptible(&hotkey_mutex))
2533 return -ERESTARTSYS;
2534 res = hotkey_status_get(&status);
2535 if (!res)
2536 res = hotkey_mask_get();
2537 mutex_unlock(&hotkey_mutex);
2538 if (res)
2539 return res;
2541 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2542 if (tp_features.hotkey_mask) {
2543 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2544 len += sprintf(p + len,
2545 "commands:\tenable, disable, reset, <mask>\n");
2546 } else {
2547 len += sprintf(p + len, "mask:\t\tnot supported\n");
2548 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2551 return len;
2554 static int hotkey_write(char *buf)
2556 int res, status;
2557 u32 mask;
2558 char *cmd;
2560 if (!tp_features.hotkey)
2561 return -ENODEV;
2563 if (mutex_lock_interruptible(&hotkey_mutex))
2564 return -ERESTARTSYS;
2566 status = -1;
2567 mask = hotkey_mask;
2569 res = 0;
2570 while ((cmd = next_cmd(&buf))) {
2571 if (strlencmp(cmd, "enable") == 0) {
2572 status = 1;
2573 } else if (strlencmp(cmd, "disable") == 0) {
2574 status = 0;
2575 } else if (strlencmp(cmd, "reset") == 0) {
2576 status = hotkey_orig_status;
2577 mask = hotkey_orig_mask;
2578 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2579 /* mask set */
2580 } else if (sscanf(cmd, "%x", &mask) == 1) {
2581 /* mask set */
2582 } else {
2583 res = -EINVAL;
2584 goto errexit;
2587 if (status != -1)
2588 res = hotkey_status_set(status);
2590 if (!res && mask != hotkey_mask)
2591 res = hotkey_mask_set(mask);
2593 errexit:
2594 mutex_unlock(&hotkey_mutex);
2595 return res;
2598 static const struct acpi_device_id ibm_htk_device_ids[] = {
2599 {TPACPI_ACPI_HKEY_HID, 0},
2600 {"", 0},
2603 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2604 .hid = ibm_htk_device_ids,
2605 .notify = hotkey_notify,
2606 .handle = &hkey_handle,
2607 .type = ACPI_DEVICE_NOTIFY,
2610 static struct ibm_struct hotkey_driver_data = {
2611 .name = "hotkey",
2612 .read = hotkey_read,
2613 .write = hotkey_write,
2614 .exit = hotkey_exit,
2615 .resume = hotkey_resume,
2616 .suspend = hotkey_suspend,
2617 .acpi = &ibm_hotkey_acpidriver,
2620 /*************************************************************************
2621 * Bluetooth subdriver
2624 enum {
2625 /* ACPI GBDC/SBDC bits */
2626 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2627 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2628 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2631 static struct platform_device *tpacpi_rfk_btsw_pdev;
2632 static struct rfkill *tpacpi_bluetooth_rfkill;
2634 static int bluetooth_get_radiosw(void)
2636 int status;
2638 if (!tp_features.bluetooth)
2639 return -ENODEV;
2641 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2642 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2643 return 0;
2645 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2646 return -EIO;
2648 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2651 static void bluetooth_update_rfk(void)
2653 int status;
2655 if (!tpacpi_bluetooth_rfkill)
2656 return;
2658 status = bluetooth_get_radiosw();
2659 if (status < 0)
2660 return;
2661 rfkill_force_state(tpacpi_bluetooth_rfkill,
2662 (status)? RFKILL_STATE_ON :
2663 RFKILL_STATE_OFF);
2666 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2668 int status;
2670 if (!tp_features.bluetooth)
2671 return -ENODEV;
2673 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2674 return -EIO;
2675 if (radio_on)
2676 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2677 else
2678 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2679 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2680 return -EIO;
2682 if (update_rfk)
2683 bluetooth_update_rfk();
2685 return 0;
2688 /* sysfs bluetooth enable ---------------------------------------------- */
2689 static ssize_t bluetooth_enable_show(struct device *dev,
2690 struct device_attribute *attr,
2691 char *buf)
2693 int status;
2695 status = bluetooth_get_radiosw();
2696 if (status < 0)
2697 return status;
2699 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2702 static ssize_t bluetooth_enable_store(struct device *dev,
2703 struct device_attribute *attr,
2704 const char *buf, size_t count)
2706 unsigned long t;
2707 int res;
2709 if (parse_strtoul(buf, 1, &t))
2710 return -EINVAL;
2712 res = bluetooth_set_radiosw(t, 1);
2714 return (res) ? res : count;
2717 static struct device_attribute dev_attr_bluetooth_enable =
2718 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2719 bluetooth_enable_show, bluetooth_enable_store);
2721 /* --------------------------------------------------------------------- */
2723 static struct attribute *bluetooth_attributes[] = {
2724 &dev_attr_bluetooth_enable.attr,
2725 NULL
2728 static const struct attribute_group bluetooth_attr_group = {
2729 .attrs = bluetooth_attributes,
2732 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2734 int bts = bluetooth_get_radiosw();
2736 if (bts >= 0) {
2737 *state = (bts)? RFKILL_STATE_ON : RFKILL_STATE_OFF;
2738 return 0;
2739 } else
2740 return bts;
2743 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2745 return bluetooth_set_radiosw((state == RFKILL_STATE_ON), 0);
2748 static void bluetooth_exit(void)
2750 if (tpacpi_bluetooth_rfkill)
2751 rfkill_unregister(tpacpi_bluetooth_rfkill);
2753 if (tpacpi_rfk_btsw_pdev)
2754 platform_device_unregister(tpacpi_rfk_btsw_pdev);
2756 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2757 &bluetooth_attr_group);
2760 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2762 int res;
2763 int status = 0;
2765 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2767 TPACPI_ACPIHANDLE_INIT(hkey);
2769 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2770 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2771 tp_features.bluetooth = hkey_handle &&
2772 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2774 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2775 str_supported(tp_features.bluetooth),
2776 status);
2778 if (tp_features.bluetooth &&
2779 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2780 /* no bluetooth hardware present in system */
2781 tp_features.bluetooth = 0;
2782 dbg_printk(TPACPI_DBG_INIT,
2783 "bluetooth hardware not installed\n");
2786 if (!tp_features.bluetooth)
2787 return 1;
2789 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2790 &bluetooth_attr_group);
2791 if (res)
2792 return res;
2794 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2795 &tpacpi_bluetooth_rfkill,
2796 RFKILL_TYPE_BLUETOOTH,
2797 "tpacpi_bluetooth_sw",
2798 tpacpi_bluetooth_rfk_set,
2799 tpacpi_bluetooth_rfk_get);
2800 if (res) {
2801 bluetooth_exit();
2802 return res;
2805 return 0;
2808 /* procfs -------------------------------------------------------------- */
2809 static int bluetooth_read(char *p)
2811 int len = 0;
2812 int status = bluetooth_get_radiosw();
2814 if (!tp_features.bluetooth)
2815 len += sprintf(p + len, "status:\t\tnot supported\n");
2816 else {
2817 len += sprintf(p + len, "status:\t\t%s\n",
2818 (status)? "enabled" : "disabled");
2819 len += sprintf(p + len, "commands:\tenable, disable\n");
2822 return len;
2825 static int bluetooth_write(char *buf)
2827 char *cmd;
2829 if (!tp_features.bluetooth)
2830 return -ENODEV;
2832 while ((cmd = next_cmd(&buf))) {
2833 if (strlencmp(cmd, "enable") == 0) {
2834 bluetooth_set_radiosw(1, 1);
2835 } else if (strlencmp(cmd, "disable") == 0) {
2836 bluetooth_set_radiosw(0, 1);
2837 } else
2838 return -EINVAL;
2841 return 0;
2844 static struct ibm_struct bluetooth_driver_data = {
2845 .name = "bluetooth",
2846 .read = bluetooth_read,
2847 .write = bluetooth_write,
2848 .exit = bluetooth_exit,
2851 /*************************************************************************
2852 * Wan subdriver
2855 enum {
2856 /* ACPI GWAN/SWAN bits */
2857 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2858 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2859 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2862 static struct platform_device *tpacpi_rfk_wansw_pdev;
2863 static struct rfkill *tpacpi_wan_rfkill;
2865 static int wan_get_radiosw(void)
2867 int status;
2869 if (!tp_features.wan)
2870 return -ENODEV;
2872 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2873 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2874 return 0;
2876 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2877 return -EIO;
2879 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2882 static void wan_update_rfk(void)
2884 int status;
2886 if (!tpacpi_wan_rfkill)
2887 return;
2889 status = wan_get_radiosw();
2890 if (status < 0)
2891 return;
2892 rfkill_force_state(tpacpi_wan_rfkill,
2893 (status)? RFKILL_STATE_ON :
2894 RFKILL_STATE_OFF);
2897 static int wan_set_radiosw(int radio_on, int update_rfk)
2899 int status;
2901 if (!tp_features.wan)
2902 return -ENODEV;
2904 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2905 return -EIO;
2906 if (radio_on)
2907 status |= TP_ACPI_WANCARD_RADIOSSW;
2908 else
2909 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2910 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2911 return -EIO;
2913 if (update_rfk)
2914 wan_update_rfk();
2916 return 0;
2919 /* sysfs wan enable ---------------------------------------------------- */
2920 static ssize_t wan_enable_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2924 int status;
2926 status = wan_get_radiosw();
2927 if (status < 0)
2928 return status;
2930 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2933 static ssize_t wan_enable_store(struct device *dev,
2934 struct device_attribute *attr,
2935 const char *buf, size_t count)
2937 unsigned long t;
2938 int res;
2940 if (parse_strtoul(buf, 1, &t))
2941 return -EINVAL;
2943 res = wan_set_radiosw(t, 1);
2945 return (res) ? res : count;
2948 static struct device_attribute dev_attr_wan_enable =
2949 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2950 wan_enable_show, wan_enable_store);
2952 /* --------------------------------------------------------------------- */
2954 static struct attribute *wan_attributes[] = {
2955 &dev_attr_wan_enable.attr,
2956 NULL
2959 static const struct attribute_group wan_attr_group = {
2960 .attrs = wan_attributes,
2963 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2965 int wans = wan_get_radiosw();
2967 if (wans >= 0) {
2968 *state = (wans)? RFKILL_STATE_ON : RFKILL_STATE_OFF;
2969 return 0;
2970 } else
2971 return wans;
2974 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2976 return wan_set_radiosw((state == RFKILL_STATE_ON), 0);
2979 static void wan_exit(void)
2981 if (tpacpi_wan_rfkill)
2982 rfkill_unregister(tpacpi_wan_rfkill);
2984 if (tpacpi_rfk_wansw_pdev)
2985 platform_device_unregister(tpacpi_rfk_wansw_pdev);
2987 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2988 &wan_attr_group);
2991 static int __init wan_init(struct ibm_init_struct *iibm)
2993 int res;
2994 int status = 0;
2996 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2998 TPACPI_ACPIHANDLE_INIT(hkey);
3000 tp_features.wan = hkey_handle &&
3001 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3003 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3004 str_supported(tp_features.wan),
3005 status);
3007 if (tp_features.wan &&
3008 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3009 /* no wan hardware present in system */
3010 tp_features.wan = 0;
3011 dbg_printk(TPACPI_DBG_INIT,
3012 "wan hardware not installed\n");
3015 if (!tp_features.wan)
3016 return 1;
3018 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3019 &wan_attr_group);
3020 if (res)
3021 return res;
3023 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3024 &tpacpi_wan_rfkill,
3025 RFKILL_TYPE_WWAN,
3026 "tpacpi_wwan_sw",
3027 tpacpi_wan_rfk_set,
3028 tpacpi_wan_rfk_get);
3029 if (res) {
3030 wan_exit();
3031 return res;
3034 return 0;
3037 /* procfs -------------------------------------------------------------- */
3038 static int wan_read(char *p)
3040 int len = 0;
3041 int status = wan_get_radiosw();
3043 if (!tp_features.wan)
3044 len += sprintf(p + len, "status:\t\tnot supported\n");
3045 else {
3046 len += sprintf(p + len, "status:\t\t%s\n",
3047 (status)? "enabled" : "disabled");
3048 len += sprintf(p + len, "commands:\tenable, disable\n");
3051 return len;
3054 static int wan_write(char *buf)
3056 char *cmd;
3058 if (!tp_features.wan)
3059 return -ENODEV;
3061 while ((cmd = next_cmd(&buf))) {
3062 if (strlencmp(cmd, "enable") == 0) {
3063 wan_set_radiosw(1, 1);
3064 } else if (strlencmp(cmd, "disable") == 0) {
3065 wan_set_radiosw(0, 1);
3066 } else
3067 return -EINVAL;
3070 return 0;
3073 static struct ibm_struct wan_driver_data = {
3074 .name = "wan",
3075 .read = wan_read,
3076 .write = wan_write,
3077 .exit = wan_exit,
3078 .flags.experimental = 1,
3081 /*************************************************************************
3082 * Video subdriver
3085 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3087 enum video_access_mode {
3088 TPACPI_VIDEO_NONE = 0,
3089 TPACPI_VIDEO_570, /* 570 */
3090 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3091 TPACPI_VIDEO_NEW, /* all others */
3094 enum { /* video status flags, based on VIDEO_570 */
3095 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3096 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3097 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3100 enum { /* TPACPI_VIDEO_570 constants */
3101 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3102 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3103 * video_status_flags */
3104 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3105 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3108 static enum video_access_mode video_supported;
3109 static int video_orig_autosw;
3111 static int video_autosw_get(void);
3112 static int video_autosw_set(int enable);
3114 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3116 static int __init video_init(struct ibm_init_struct *iibm)
3118 int ivga;
3120 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3122 TPACPI_ACPIHANDLE_INIT(vid);
3123 TPACPI_ACPIHANDLE_INIT(vid2);
3125 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3126 /* G41, assume IVGA doesn't change */
3127 vid_handle = vid2_handle;
3129 if (!vid_handle)
3130 /* video switching not supported on R30, R31 */
3131 video_supported = TPACPI_VIDEO_NONE;
3132 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3133 /* 570 */
3134 video_supported = TPACPI_VIDEO_570;
3135 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3136 /* 600e/x, 770e, 770x */
3137 video_supported = TPACPI_VIDEO_770;
3138 else
3139 /* all others */
3140 video_supported = TPACPI_VIDEO_NEW;
3142 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3143 str_supported(video_supported != TPACPI_VIDEO_NONE),
3144 video_supported);
3146 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3149 static void video_exit(void)
3151 dbg_printk(TPACPI_DBG_EXIT,
3152 "restoring original video autoswitch mode\n");
3153 if (video_autosw_set(video_orig_autosw))
3154 printk(TPACPI_ERR "error while trying to restore original "
3155 "video autoswitch mode\n");
3158 static int video_outputsw_get(void)
3160 int status = 0;
3161 int i;
3163 switch (video_supported) {
3164 case TPACPI_VIDEO_570:
3165 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3166 TP_ACPI_VIDEO_570_PHSCMD))
3167 return -EIO;
3168 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3169 break;
3170 case TPACPI_VIDEO_770:
3171 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3172 return -EIO;
3173 if (i)
3174 status |= TP_ACPI_VIDEO_S_LCD;
3175 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3176 return -EIO;
3177 if (i)
3178 status |= TP_ACPI_VIDEO_S_CRT;
3179 break;
3180 case TPACPI_VIDEO_NEW:
3181 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3182 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3183 return -EIO;
3184 if (i)
3185 status |= TP_ACPI_VIDEO_S_CRT;
3187 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3188 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3189 return -EIO;
3190 if (i)
3191 status |= TP_ACPI_VIDEO_S_LCD;
3192 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3193 return -EIO;
3194 if (i)
3195 status |= TP_ACPI_VIDEO_S_DVI;
3196 break;
3197 default:
3198 return -ENOSYS;
3201 return status;
3204 static int video_outputsw_set(int status)
3206 int autosw;
3207 int res = 0;
3209 switch (video_supported) {
3210 case TPACPI_VIDEO_570:
3211 res = acpi_evalf(NULL, NULL,
3212 "\\_SB.PHS2", "vdd",
3213 TP_ACPI_VIDEO_570_PHS2CMD,
3214 status | TP_ACPI_VIDEO_570_PHS2SET);
3215 break;
3216 case TPACPI_VIDEO_770:
3217 autosw = video_autosw_get();
3218 if (autosw < 0)
3219 return autosw;
3221 res = video_autosw_set(1);
3222 if (res)
3223 return res;
3224 res = acpi_evalf(vid_handle, NULL,
3225 "ASWT", "vdd", status * 0x100, 0);
3226 if (!autosw && video_autosw_set(autosw)) {
3227 printk(TPACPI_ERR
3228 "video auto-switch left enabled due to error\n");
3229 return -EIO;
3231 break;
3232 case TPACPI_VIDEO_NEW:
3233 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3234 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3235 break;
3236 default:
3237 return -ENOSYS;
3240 return (res)? 0 : -EIO;
3243 static int video_autosw_get(void)
3245 int autosw = 0;
3247 switch (video_supported) {
3248 case TPACPI_VIDEO_570:
3249 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3250 return -EIO;
3251 break;
3252 case TPACPI_VIDEO_770:
3253 case TPACPI_VIDEO_NEW:
3254 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3255 return -EIO;
3256 break;
3257 default:
3258 return -ENOSYS;
3261 return autosw & 1;
3264 static int video_autosw_set(int enable)
3266 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3267 return -EIO;
3268 return 0;
3271 static int video_outputsw_cycle(void)
3273 int autosw = video_autosw_get();
3274 int res;
3276 if (autosw < 0)
3277 return autosw;
3279 switch (video_supported) {
3280 case TPACPI_VIDEO_570:
3281 res = video_autosw_set(1);
3282 if (res)
3283 return res;
3284 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3285 break;
3286 case TPACPI_VIDEO_770:
3287 case TPACPI_VIDEO_NEW:
3288 res = video_autosw_set(1);
3289 if (res)
3290 return res;
3291 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3292 break;
3293 default:
3294 return -ENOSYS;
3296 if (!autosw && video_autosw_set(autosw)) {
3297 printk(TPACPI_ERR
3298 "video auto-switch left enabled due to error\n");
3299 return -EIO;
3302 return (res)? 0 : -EIO;
3305 static int video_expand_toggle(void)
3307 switch (video_supported) {
3308 case TPACPI_VIDEO_570:
3309 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3310 0 : -EIO;
3311 case TPACPI_VIDEO_770:
3312 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3313 0 : -EIO;
3314 case TPACPI_VIDEO_NEW:
3315 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3316 0 : -EIO;
3317 default:
3318 return -ENOSYS;
3320 /* not reached */
3323 static int video_read(char *p)
3325 int status, autosw;
3326 int len = 0;
3328 if (video_supported == TPACPI_VIDEO_NONE) {
3329 len += sprintf(p + len, "status:\t\tnot supported\n");
3330 return len;
3333 status = video_outputsw_get();
3334 if (status < 0)
3335 return status;
3337 autosw = video_autosw_get();
3338 if (autosw < 0)
3339 return autosw;
3341 len += sprintf(p + len, "status:\t\tsupported\n");
3342 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3343 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3344 if (video_supported == TPACPI_VIDEO_NEW)
3345 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3346 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3347 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3348 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3349 if (video_supported == TPACPI_VIDEO_NEW)
3350 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3351 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3352 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3354 return len;
3357 static int video_write(char *buf)
3359 char *cmd;
3360 int enable, disable, status;
3361 int res;
3363 if (video_supported == TPACPI_VIDEO_NONE)
3364 return -ENODEV;
3366 enable = 0;
3367 disable = 0;
3369 while ((cmd = next_cmd(&buf))) {
3370 if (strlencmp(cmd, "lcd_enable") == 0) {
3371 enable |= TP_ACPI_VIDEO_S_LCD;
3372 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3373 disable |= TP_ACPI_VIDEO_S_LCD;
3374 } else if (strlencmp(cmd, "crt_enable") == 0) {
3375 enable |= TP_ACPI_VIDEO_S_CRT;
3376 } else if (strlencmp(cmd, "crt_disable") == 0) {
3377 disable |= TP_ACPI_VIDEO_S_CRT;
3378 } else if (video_supported == TPACPI_VIDEO_NEW &&
3379 strlencmp(cmd, "dvi_enable") == 0) {
3380 enable |= TP_ACPI_VIDEO_S_DVI;
3381 } else if (video_supported == TPACPI_VIDEO_NEW &&
3382 strlencmp(cmd, "dvi_disable") == 0) {
3383 disable |= TP_ACPI_VIDEO_S_DVI;
3384 } else if (strlencmp(cmd, "auto_enable") == 0) {
3385 res = video_autosw_set(1);
3386 if (res)
3387 return res;
3388 } else if (strlencmp(cmd, "auto_disable") == 0) {
3389 res = video_autosw_set(0);
3390 if (res)
3391 return res;
3392 } else if (strlencmp(cmd, "video_switch") == 0) {
3393 res = video_outputsw_cycle();
3394 if (res)
3395 return res;
3396 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3397 res = video_expand_toggle();
3398 if (res)
3399 return res;
3400 } else
3401 return -EINVAL;
3404 if (enable || disable) {
3405 status = video_outputsw_get();
3406 if (status < 0)
3407 return status;
3408 res = video_outputsw_set((status & ~disable) | enable);
3409 if (res)
3410 return res;
3413 return 0;
3416 static struct ibm_struct video_driver_data = {
3417 .name = "video",
3418 .read = video_read,
3419 .write = video_write,
3420 .exit = video_exit,
3423 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3425 /*************************************************************************
3426 * Light (thinklight) subdriver
3429 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3430 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3432 static int light_get_status(void)
3434 int status = 0;
3436 if (tp_features.light_status) {
3437 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3438 return -EIO;
3439 return (!!status);
3442 return -ENXIO;
3445 static int light_set_status(int status)
3447 int rc;
3449 if (tp_features.light) {
3450 if (cmos_handle) {
3451 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3452 (status)?
3453 TP_CMOS_THINKLIGHT_ON :
3454 TP_CMOS_THINKLIGHT_OFF);
3455 } else {
3456 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3457 (status)? 1 : 0);
3459 return (rc)? 0 : -EIO;
3462 return -ENXIO;
3465 static void light_set_status_worker(struct work_struct *work)
3467 struct tpacpi_led_classdev *data =
3468 container_of(work, struct tpacpi_led_classdev, work);
3470 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3471 light_set_status((data->new_brightness != LED_OFF));
3474 static void light_sysfs_set(struct led_classdev *led_cdev,
3475 enum led_brightness brightness)
3477 struct tpacpi_led_classdev *data =
3478 container_of(led_cdev,
3479 struct tpacpi_led_classdev,
3480 led_classdev);
3481 data->new_brightness = brightness;
3482 queue_work(tpacpi_wq, &data->work);
3485 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3487 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3490 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3491 .led_classdev = {
3492 .name = "tpacpi::thinklight",
3493 .brightness_set = &light_sysfs_set,
3494 .brightness_get = &light_sysfs_get,
3498 static int __init light_init(struct ibm_init_struct *iibm)
3500 int rc;
3502 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3504 TPACPI_ACPIHANDLE_INIT(ledb);
3505 TPACPI_ACPIHANDLE_INIT(lght);
3506 TPACPI_ACPIHANDLE_INIT(cmos);
3507 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3509 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3510 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3512 if (tp_features.light)
3513 /* light status not supported on
3514 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3515 tp_features.light_status =
3516 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3518 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3519 str_supported(tp_features.light),
3520 str_supported(tp_features.light_status));
3522 if (!tp_features.light)
3523 return 1;
3525 rc = led_classdev_register(&tpacpi_pdev->dev,
3526 &tpacpi_led_thinklight.led_classdev);
3528 if (rc < 0) {
3529 tp_features.light = 0;
3530 tp_features.light_status = 0;
3531 } else {
3532 rc = 0;
3535 return rc;
3538 static void light_exit(void)
3540 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3541 if (work_pending(&tpacpi_led_thinklight.work))
3542 flush_workqueue(tpacpi_wq);
3545 static int light_read(char *p)
3547 int len = 0;
3548 int status;
3550 if (!tp_features.light) {
3551 len += sprintf(p + len, "status:\t\tnot supported\n");
3552 } else if (!tp_features.light_status) {
3553 len += sprintf(p + len, "status:\t\tunknown\n");
3554 len += sprintf(p + len, "commands:\ton, off\n");
3555 } else {
3556 status = light_get_status();
3557 if (status < 0)
3558 return status;
3559 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3560 len += sprintf(p + len, "commands:\ton, off\n");
3563 return len;
3566 static int light_write(char *buf)
3568 char *cmd;
3569 int newstatus = 0;
3571 if (!tp_features.light)
3572 return -ENODEV;
3574 while ((cmd = next_cmd(&buf))) {
3575 if (strlencmp(cmd, "on") == 0) {
3576 newstatus = 1;
3577 } else if (strlencmp(cmd, "off") == 0) {
3578 newstatus = 0;
3579 } else
3580 return -EINVAL;
3583 return light_set_status(newstatus);
3586 static struct ibm_struct light_driver_data = {
3587 .name = "light",
3588 .read = light_read,
3589 .write = light_write,
3590 .exit = light_exit,
3593 /*************************************************************************
3594 * Dock subdriver
3597 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3599 static void dock_notify(struct ibm_struct *ibm, u32 event);
3600 static int dock_read(char *p);
3601 static int dock_write(char *buf);
3603 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3604 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3605 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3606 "\\_SB.PCI.ISA.SLCE", /* 570 */
3607 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3609 /* don't list other alternatives as we install a notify handler on the 570 */
3610 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3612 static const struct acpi_device_id ibm_pci_device_ids[] = {
3613 {PCI_ROOT_HID_STRING, 0},
3614 {"", 0},
3617 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3619 .notify = dock_notify,
3620 .handle = &dock_handle,
3621 .type = ACPI_SYSTEM_NOTIFY,
3624 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3625 * We just use it to get notifications of dock hotplug
3626 * in very old thinkpads */
3627 .hid = ibm_pci_device_ids,
3628 .notify = dock_notify,
3629 .handle = &pci_handle,
3630 .type = ACPI_SYSTEM_NOTIFY,
3634 static struct ibm_struct dock_driver_data[2] = {
3636 .name = "dock",
3637 .read = dock_read,
3638 .write = dock_write,
3639 .acpi = &ibm_dock_acpidriver[0],
3642 .name = "dock",
3643 .acpi = &ibm_dock_acpidriver[1],
3647 #define dock_docked() (_sta(dock_handle) & 1)
3649 static int __init dock_init(struct ibm_init_struct *iibm)
3651 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3653 TPACPI_ACPIHANDLE_INIT(dock);
3655 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3656 str_supported(dock_handle != NULL));
3658 return (dock_handle)? 0 : 1;
3661 static int __init dock_init2(struct ibm_init_struct *iibm)
3663 int dock2_needed;
3665 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3667 if (dock_driver_data[0].flags.acpi_driver_registered &&
3668 dock_driver_data[0].flags.acpi_notify_installed) {
3669 TPACPI_ACPIHANDLE_INIT(pci);
3670 dock2_needed = (pci_handle != NULL);
3671 vdbg_printk(TPACPI_DBG_INIT,
3672 "dock PCI handler for the TP 570 is %s\n",
3673 str_supported(dock2_needed));
3674 } else {
3675 vdbg_printk(TPACPI_DBG_INIT,
3676 "dock subdriver part 2 not required\n");
3677 dock2_needed = 0;
3680 return (dock2_needed)? 0 : 1;
3683 static void dock_notify(struct ibm_struct *ibm, u32 event)
3685 int docked = dock_docked();
3686 int pci = ibm->acpi->hid && ibm->acpi->device &&
3687 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3688 int data;
3690 if (event == 1 && !pci) /* 570 */
3691 data = 1; /* button */
3692 else if (event == 1 && pci) /* 570 */
3693 data = 3; /* dock */
3694 else if (event == 3 && docked)
3695 data = 1; /* button */
3696 else if (event == 3 && !docked)
3697 data = 2; /* undock */
3698 else if (event == 0 && docked)
3699 data = 3; /* dock */
3700 else {
3701 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3702 event, _sta(dock_handle));
3703 data = 0; /* unknown */
3705 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3706 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3707 ibm->acpi->device->dev.bus_id,
3708 event, data);
3711 static int dock_read(char *p)
3713 int len = 0;
3714 int docked = dock_docked();
3716 if (!dock_handle)
3717 len += sprintf(p + len, "status:\t\tnot supported\n");
3718 else if (!docked)
3719 len += sprintf(p + len, "status:\t\tundocked\n");
3720 else {
3721 len += sprintf(p + len, "status:\t\tdocked\n");
3722 len += sprintf(p + len, "commands:\tdock, undock\n");
3725 return len;
3728 static int dock_write(char *buf)
3730 char *cmd;
3732 if (!dock_docked())
3733 return -ENODEV;
3735 while ((cmd = next_cmd(&buf))) {
3736 if (strlencmp(cmd, "undock") == 0) {
3737 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3738 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3739 return -EIO;
3740 } else if (strlencmp(cmd, "dock") == 0) {
3741 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3742 return -EIO;
3743 } else
3744 return -EINVAL;
3747 return 0;
3750 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3752 /*************************************************************************
3753 * Bay subdriver
3756 #ifdef CONFIG_THINKPAD_ACPI_BAY
3758 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3759 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3760 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3761 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3762 ); /* A21e, R30, R31 */
3763 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3764 "_EJ0", /* all others */
3765 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3766 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3767 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3768 ); /* all others */
3769 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3770 "_EJ0", /* 770x */
3771 ); /* all others */
3773 static int __init bay_init(struct ibm_init_struct *iibm)
3775 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3777 TPACPI_ACPIHANDLE_INIT(bay);
3778 if (bay_handle)
3779 TPACPI_ACPIHANDLE_INIT(bay_ej);
3780 TPACPI_ACPIHANDLE_INIT(bay2);
3781 if (bay2_handle)
3782 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3784 tp_features.bay_status = bay_handle &&
3785 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3786 tp_features.bay_status2 = bay2_handle &&
3787 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3789 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3790 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3791 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3792 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3794 vdbg_printk(TPACPI_DBG_INIT,
3795 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3796 str_supported(tp_features.bay_status),
3797 str_supported(tp_features.bay_eject),
3798 str_supported(tp_features.bay_status2),
3799 str_supported(tp_features.bay_eject2));
3801 return (tp_features.bay_status || tp_features.bay_eject ||
3802 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3805 static void bay_notify(struct ibm_struct *ibm, u32 event)
3807 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3808 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3809 ibm->acpi->device->dev.bus_id,
3810 event, 0);
3813 #define bay_occupied(b) (_sta(b##_handle) & 1)
3815 static int bay_read(char *p)
3817 int len = 0;
3818 int occupied = bay_occupied(bay);
3819 int occupied2 = bay_occupied(bay2);
3820 int eject, eject2;
3822 len += sprintf(p + len, "status:\t\t%s\n",
3823 tp_features.bay_status ?
3824 (occupied ? "occupied" : "unoccupied") :
3825 "not supported");
3826 if (tp_features.bay_status2)
3827 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3828 "occupied" : "unoccupied");
3830 eject = tp_features.bay_eject && occupied;
3831 eject2 = tp_features.bay_eject2 && occupied2;
3833 if (eject && eject2)
3834 len += sprintf(p + len, "commands:\teject, eject2\n");
3835 else if (eject)
3836 len += sprintf(p + len, "commands:\teject\n");
3837 else if (eject2)
3838 len += sprintf(p + len, "commands:\teject2\n");
3840 return len;
3843 static int bay_write(char *buf)
3845 char *cmd;
3847 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3848 return -ENODEV;
3850 while ((cmd = next_cmd(&buf))) {
3851 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3852 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3853 return -EIO;
3854 } else if (tp_features.bay_eject2 &&
3855 strlencmp(cmd, "eject2") == 0) {
3856 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3857 return -EIO;
3858 } else
3859 return -EINVAL;
3862 return 0;
3865 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3866 .notify = bay_notify,
3867 .handle = &bay_handle,
3868 .type = ACPI_SYSTEM_NOTIFY,
3871 static struct ibm_struct bay_driver_data = {
3872 .name = "bay",
3873 .read = bay_read,
3874 .write = bay_write,
3875 .acpi = &ibm_bay_acpidriver,
3878 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3880 /*************************************************************************
3881 * CMOS subdriver
3884 /* sysfs cmos_command -------------------------------------------------- */
3885 static ssize_t cmos_command_store(struct device *dev,
3886 struct device_attribute *attr,
3887 const char *buf, size_t count)
3889 unsigned long cmos_cmd;
3890 int res;
3892 if (parse_strtoul(buf, 21, &cmos_cmd))
3893 return -EINVAL;
3895 res = issue_thinkpad_cmos_command(cmos_cmd);
3896 return (res)? res : count;
3899 static struct device_attribute dev_attr_cmos_command =
3900 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3902 /* --------------------------------------------------------------------- */
3904 static int __init cmos_init(struct ibm_init_struct *iibm)
3906 int res;
3908 vdbg_printk(TPACPI_DBG_INIT,
3909 "initializing cmos commands subdriver\n");
3911 TPACPI_ACPIHANDLE_INIT(cmos);
3913 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3914 str_supported(cmos_handle != NULL));
3916 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3917 if (res)
3918 return res;
3920 return (cmos_handle)? 0 : 1;
3923 static void cmos_exit(void)
3925 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3928 static int cmos_read(char *p)
3930 int len = 0;
3932 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3933 R30, R31, T20-22, X20-21 */
3934 if (!cmos_handle)
3935 len += sprintf(p + len, "status:\t\tnot supported\n");
3936 else {
3937 len += sprintf(p + len, "status:\t\tsupported\n");
3938 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3941 return len;
3944 static int cmos_write(char *buf)
3946 char *cmd;
3947 int cmos_cmd, res;
3949 while ((cmd = next_cmd(&buf))) {
3950 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3951 cmos_cmd >= 0 && cmos_cmd <= 21) {
3952 /* cmos_cmd set */
3953 } else
3954 return -EINVAL;
3956 res = issue_thinkpad_cmos_command(cmos_cmd);
3957 if (res)
3958 return res;
3961 return 0;
3964 static struct ibm_struct cmos_driver_data = {
3965 .name = "cmos",
3966 .read = cmos_read,
3967 .write = cmos_write,
3968 .exit = cmos_exit,
3971 /*************************************************************************
3972 * LED subdriver
3975 enum led_access_mode {
3976 TPACPI_LED_NONE = 0,
3977 TPACPI_LED_570, /* 570 */
3978 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3979 TPACPI_LED_NEW, /* all others */
3982 enum { /* For TPACPI_LED_OLD */
3983 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3984 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3985 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3988 enum led_status_t {
3989 TPACPI_LED_OFF = 0,
3990 TPACPI_LED_ON,
3991 TPACPI_LED_BLINK,
3994 static enum led_access_mode led_supported;
3996 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3997 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3998 /* T20-22, X20-21 */
3999 "LED", /* all others */
4000 ); /* R30, R31 */
4002 #define TPACPI_LED_NUMLEDS 8
4003 static struct tpacpi_led_classdev *tpacpi_leds;
4004 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4005 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4006 /* there's a limit of 19 chars + NULL before 2.6.26 */
4007 "tpacpi::power",
4008 "tpacpi:orange:batt",
4009 "tpacpi:green:batt",
4010 "tpacpi::dock_active",
4011 "tpacpi::bay_active",
4012 "tpacpi::dock_batt",
4013 "tpacpi::unknown_led",
4014 "tpacpi::standby",
4017 static int led_get_status(const unsigned int led)
4019 int status;
4020 enum led_status_t led_s;
4022 switch (led_supported) {
4023 case TPACPI_LED_570:
4024 if (!acpi_evalf(ec_handle,
4025 &status, "GLED", "dd", 1 << led))
4026 return -EIO;
4027 led_s = (status == 0)?
4028 TPACPI_LED_OFF :
4029 ((status == 1)?
4030 TPACPI_LED_ON :
4031 TPACPI_LED_BLINK);
4032 tpacpi_led_state_cache[led] = led_s;
4033 return led_s;
4034 default:
4035 return -ENXIO;
4038 /* not reached */
4041 static int led_set_status(const unsigned int led,
4042 const enum led_status_t ledstatus)
4044 /* off, on, blink. Index is led_status_t */
4045 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4046 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4048 int rc = 0;
4050 switch (led_supported) {
4051 case TPACPI_LED_570:
4052 /* 570 */
4053 if (led > 7)
4054 return -EINVAL;
4055 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4056 (1 << led), led_sled_arg1[ledstatus]))
4057 rc = -EIO;
4058 break;
4059 case TPACPI_LED_OLD:
4060 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4061 if (led > 7)
4062 return -EINVAL;
4063 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4064 if (rc >= 0)
4065 rc = ec_write(TPACPI_LED_EC_HLBL,
4066 (ledstatus == TPACPI_LED_BLINK) << led);
4067 if (rc >= 0)
4068 rc = ec_write(TPACPI_LED_EC_HLCL,
4069 (ledstatus != TPACPI_LED_OFF) << led);
4070 break;
4071 case TPACPI_LED_NEW:
4072 /* all others */
4073 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4074 led, led_led_arg1[ledstatus]))
4075 rc = -EIO;
4076 break;
4077 default:
4078 rc = -ENXIO;
4081 if (!rc)
4082 tpacpi_led_state_cache[led] = ledstatus;
4084 return rc;
4087 static void led_sysfs_set_status(unsigned int led,
4088 enum led_brightness brightness)
4090 led_set_status(led,
4091 (brightness == LED_OFF) ?
4092 TPACPI_LED_OFF :
4093 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4094 TPACPI_LED_BLINK : TPACPI_LED_ON);
4097 static void led_set_status_worker(struct work_struct *work)
4099 struct tpacpi_led_classdev *data =
4100 container_of(work, struct tpacpi_led_classdev, work);
4102 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4103 led_sysfs_set_status(data->led, data->new_brightness);
4106 static void led_sysfs_set(struct led_classdev *led_cdev,
4107 enum led_brightness brightness)
4109 struct tpacpi_led_classdev *data = container_of(led_cdev,
4110 struct tpacpi_led_classdev, led_classdev);
4112 data->new_brightness = brightness;
4113 queue_work(tpacpi_wq, &data->work);
4116 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4117 unsigned long *delay_on, unsigned long *delay_off)
4119 struct tpacpi_led_classdev *data = container_of(led_cdev,
4120 struct tpacpi_led_classdev, led_classdev);
4122 /* Can we choose the flash rate? */
4123 if (*delay_on == 0 && *delay_off == 0) {
4124 /* yes. set them to the hardware blink rate (1 Hz) */
4125 *delay_on = 500; /* ms */
4126 *delay_off = 500; /* ms */
4127 } else if ((*delay_on != 500) || (*delay_off != 500))
4128 return -EINVAL;
4130 data->new_brightness = TPACPI_LED_BLINK;
4131 queue_work(tpacpi_wq, &data->work);
4133 return 0;
4136 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4138 int rc;
4140 struct tpacpi_led_classdev *data = container_of(led_cdev,
4141 struct tpacpi_led_classdev, led_classdev);
4143 rc = led_get_status(data->led);
4145 if (rc == TPACPI_LED_OFF || rc < 0)
4146 rc = LED_OFF; /* no error handling in led class :( */
4147 else
4148 rc = LED_FULL;
4150 return rc;
4153 static void led_exit(void)
4155 unsigned int i;
4157 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4158 if (tpacpi_leds[i].led_classdev.name)
4159 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4162 kfree(tpacpi_leds);
4165 static int __init led_init(struct ibm_init_struct *iibm)
4167 unsigned int i;
4168 int rc;
4170 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4172 TPACPI_ACPIHANDLE_INIT(led);
4174 if (!led_handle)
4175 /* led not supported on R30, R31 */
4176 led_supported = TPACPI_LED_NONE;
4177 else if (strlencmp(led_path, "SLED") == 0)
4178 /* 570 */
4179 led_supported = TPACPI_LED_570;
4180 else if (strlencmp(led_path, "SYSL") == 0)
4181 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4182 led_supported = TPACPI_LED_OLD;
4183 else
4184 /* all others */
4185 led_supported = TPACPI_LED_NEW;
4187 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4188 str_supported(led_supported), led_supported);
4190 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4191 GFP_KERNEL);
4192 if (!tpacpi_leds) {
4193 printk(TPACPI_ERR "Out of memory for LED data\n");
4194 return -ENOMEM;
4197 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4198 tpacpi_leds[i].led = i;
4200 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4201 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4202 if (led_supported == TPACPI_LED_570)
4203 tpacpi_leds[i].led_classdev.brightness_get =
4204 &led_sysfs_get;
4206 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4208 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4210 rc = led_classdev_register(&tpacpi_pdev->dev,
4211 &tpacpi_leds[i].led_classdev);
4212 if (rc < 0) {
4213 tpacpi_leds[i].led_classdev.name = NULL;
4214 led_exit();
4215 return rc;
4219 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4222 #define str_led_status(s) \
4223 ((s) == TPACPI_LED_OFF ? "off" : \
4224 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4226 static int led_read(char *p)
4228 int len = 0;
4230 if (!led_supported) {
4231 len += sprintf(p + len, "status:\t\tnot supported\n");
4232 return len;
4234 len += sprintf(p + len, "status:\t\tsupported\n");
4236 if (led_supported == TPACPI_LED_570) {
4237 /* 570 */
4238 int i, status;
4239 for (i = 0; i < 8; i++) {
4240 status = led_get_status(i);
4241 if (status < 0)
4242 return -EIO;
4243 len += sprintf(p + len, "%d:\t\t%s\n",
4244 i, str_led_status(status));
4248 len += sprintf(p + len, "commands:\t"
4249 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4251 return len;
4254 static int led_write(char *buf)
4256 char *cmd;
4257 int led, rc;
4258 enum led_status_t s;
4260 if (!led_supported)
4261 return -ENODEV;
4263 while ((cmd = next_cmd(&buf))) {
4264 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4265 return -EINVAL;
4267 if (strstr(cmd, "off")) {
4268 s = TPACPI_LED_OFF;
4269 } else if (strstr(cmd, "on")) {
4270 s = TPACPI_LED_ON;
4271 } else if (strstr(cmd, "blink")) {
4272 s = TPACPI_LED_BLINK;
4273 } else {
4274 return -EINVAL;
4277 rc = led_set_status(led, s);
4278 if (rc < 0)
4279 return rc;
4282 return 0;
4285 static struct ibm_struct led_driver_data = {
4286 .name = "led",
4287 .read = led_read,
4288 .write = led_write,
4289 .exit = led_exit,
4292 /*************************************************************************
4293 * Beep subdriver
4296 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4298 static int __init beep_init(struct ibm_init_struct *iibm)
4300 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4302 TPACPI_ACPIHANDLE_INIT(beep);
4304 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4305 str_supported(beep_handle != NULL));
4307 return (beep_handle)? 0 : 1;
4310 static int beep_read(char *p)
4312 int len = 0;
4314 if (!beep_handle)
4315 len += sprintf(p + len, "status:\t\tnot supported\n");
4316 else {
4317 len += sprintf(p + len, "status:\t\tsupported\n");
4318 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4321 return len;
4324 static int beep_write(char *buf)
4326 char *cmd;
4327 int beep_cmd;
4329 if (!beep_handle)
4330 return -ENODEV;
4332 while ((cmd = next_cmd(&buf))) {
4333 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4334 beep_cmd >= 0 && beep_cmd <= 17) {
4335 /* beep_cmd set */
4336 } else
4337 return -EINVAL;
4338 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4339 return -EIO;
4342 return 0;
4345 static struct ibm_struct beep_driver_data = {
4346 .name = "beep",
4347 .read = beep_read,
4348 .write = beep_write,
4351 /*************************************************************************
4352 * Thermal subdriver
4355 enum thermal_access_mode {
4356 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4357 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4358 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4359 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4360 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4363 enum { /* TPACPI_THERMAL_TPEC_* */
4364 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4365 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4366 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4369 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4370 struct ibm_thermal_sensors_struct {
4371 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4374 static enum thermal_access_mode thermal_read_mode;
4376 /* idx is zero-based */
4377 static int thermal_get_sensor(int idx, s32 *value)
4379 int t;
4380 s8 tmp;
4381 char tmpi[5];
4383 t = TP_EC_THERMAL_TMP0;
4385 switch (thermal_read_mode) {
4386 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4387 case TPACPI_THERMAL_TPEC_16:
4388 if (idx >= 8 && idx <= 15) {
4389 t = TP_EC_THERMAL_TMP8;
4390 idx -= 8;
4392 /* fallthrough */
4393 #endif
4394 case TPACPI_THERMAL_TPEC_8:
4395 if (idx <= 7) {
4396 if (!acpi_ec_read(t + idx, &tmp))
4397 return -EIO;
4398 *value = tmp * 1000;
4399 return 0;
4401 break;
4403 case TPACPI_THERMAL_ACPI_UPDT:
4404 if (idx <= 7) {
4405 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4406 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4407 return -EIO;
4408 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4409 return -EIO;
4410 *value = (t - 2732) * 100;
4411 return 0;
4413 break;
4415 case TPACPI_THERMAL_ACPI_TMP07:
4416 if (idx <= 7) {
4417 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4418 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4419 return -EIO;
4420 if (t > 127 || t < -127)
4421 t = TP_EC_THERMAL_TMP_NA;
4422 *value = t * 1000;
4423 return 0;
4425 break;
4427 case TPACPI_THERMAL_NONE:
4428 default:
4429 return -ENOSYS;
4432 return -EINVAL;
4435 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4437 int res, i;
4438 int n;
4440 n = 8;
4441 i = 0;
4443 if (!s)
4444 return -EINVAL;
4446 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4447 n = 16;
4449 for (i = 0 ; i < n; i++) {
4450 res = thermal_get_sensor(i, &s->temp[i]);
4451 if (res)
4452 return res;
4455 return n;
4458 /* sysfs temp##_input -------------------------------------------------- */
4460 static ssize_t thermal_temp_input_show(struct device *dev,
4461 struct device_attribute *attr,
4462 char *buf)
4464 struct sensor_device_attribute *sensor_attr =
4465 to_sensor_dev_attr(attr);
4466 int idx = sensor_attr->index;
4467 s32 value;
4468 int res;
4470 res = thermal_get_sensor(idx, &value);
4471 if (res)
4472 return res;
4473 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4474 return -ENXIO;
4476 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4479 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4480 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4481 thermal_temp_input_show, NULL, _idxB)
4483 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4484 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4485 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4486 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4487 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4488 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4489 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4490 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4491 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4492 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4493 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4494 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4495 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4496 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4497 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4498 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4499 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4502 #define THERMAL_ATTRS(X) \
4503 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4505 static struct attribute *thermal_temp_input_attr[] = {
4506 THERMAL_ATTRS(8),
4507 THERMAL_ATTRS(9),
4508 THERMAL_ATTRS(10),
4509 THERMAL_ATTRS(11),
4510 THERMAL_ATTRS(12),
4511 THERMAL_ATTRS(13),
4512 THERMAL_ATTRS(14),
4513 THERMAL_ATTRS(15),
4514 THERMAL_ATTRS(0),
4515 THERMAL_ATTRS(1),
4516 THERMAL_ATTRS(2),
4517 THERMAL_ATTRS(3),
4518 THERMAL_ATTRS(4),
4519 THERMAL_ATTRS(5),
4520 THERMAL_ATTRS(6),
4521 THERMAL_ATTRS(7),
4522 NULL
4525 static const struct attribute_group thermal_temp_input16_group = {
4526 .attrs = thermal_temp_input_attr
4529 static const struct attribute_group thermal_temp_input8_group = {
4530 .attrs = &thermal_temp_input_attr[8]
4533 #undef THERMAL_SENSOR_ATTR_TEMP
4534 #undef THERMAL_ATTRS
4536 /* --------------------------------------------------------------------- */
4538 static int __init thermal_init(struct ibm_init_struct *iibm)
4540 u8 t, ta1, ta2;
4541 int i;
4542 int acpi_tmp7;
4543 int res;
4545 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4547 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4549 if (thinkpad_id.ec_model) {
4551 * Direct EC access mode: sensors at registers
4552 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4553 * non-implemented, thermal sensors return 0x80 when
4554 * not available
4557 ta1 = ta2 = 0;
4558 for (i = 0; i < 8; i++) {
4559 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4560 ta1 |= t;
4561 } else {
4562 ta1 = 0;
4563 break;
4565 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4566 ta2 |= t;
4567 } else {
4568 ta1 = 0;
4569 break;
4572 if (ta1 == 0) {
4573 /* This is sheer paranoia, but we handle it anyway */
4574 if (acpi_tmp7) {
4575 printk(TPACPI_ERR
4576 "ThinkPad ACPI EC access misbehaving, "
4577 "falling back to ACPI TMPx access "
4578 "mode\n");
4579 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4580 } else {
4581 printk(TPACPI_ERR
4582 "ThinkPad ACPI EC access misbehaving, "
4583 "disabling thermal sensors access\n");
4584 thermal_read_mode = TPACPI_THERMAL_NONE;
4586 } else {
4587 thermal_read_mode =
4588 (ta2 != 0) ?
4589 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4591 } else if (acpi_tmp7) {
4592 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4593 /* 600e/x, 770e, 770x */
4594 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4595 } else {
4596 /* Standard ACPI TMPx access, max 8 sensors */
4597 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4599 } else {
4600 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4601 thermal_read_mode = TPACPI_THERMAL_NONE;
4604 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4605 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4606 thermal_read_mode);
4608 switch (thermal_read_mode) {
4609 case TPACPI_THERMAL_TPEC_16:
4610 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4611 &thermal_temp_input16_group);
4612 if (res)
4613 return res;
4614 break;
4615 case TPACPI_THERMAL_TPEC_8:
4616 case TPACPI_THERMAL_ACPI_TMP07:
4617 case TPACPI_THERMAL_ACPI_UPDT:
4618 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4619 &thermal_temp_input8_group);
4620 if (res)
4621 return res;
4622 break;
4623 case TPACPI_THERMAL_NONE:
4624 default:
4625 return 1;
4628 return 0;
4631 static void thermal_exit(void)
4633 switch (thermal_read_mode) {
4634 case TPACPI_THERMAL_TPEC_16:
4635 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4636 &thermal_temp_input16_group);
4637 break;
4638 case TPACPI_THERMAL_TPEC_8:
4639 case TPACPI_THERMAL_ACPI_TMP07:
4640 case TPACPI_THERMAL_ACPI_UPDT:
4641 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4642 &thermal_temp_input16_group);
4643 break;
4644 case TPACPI_THERMAL_NONE:
4645 default:
4646 break;
4650 static int thermal_read(char *p)
4652 int len = 0;
4653 int n, i;
4654 struct ibm_thermal_sensors_struct t;
4656 n = thermal_get_sensors(&t);
4657 if (unlikely(n < 0))
4658 return n;
4660 len += sprintf(p + len, "temperatures:\t");
4662 if (n > 0) {
4663 for (i = 0; i < (n - 1); i++)
4664 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4665 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4666 } else
4667 len += sprintf(p + len, "not supported\n");
4669 return len;
4672 static struct ibm_struct thermal_driver_data = {
4673 .name = "thermal",
4674 .read = thermal_read,
4675 .exit = thermal_exit,
4678 /*************************************************************************
4679 * EC Dump subdriver
4682 static u8 ecdump_regs[256];
4684 static int ecdump_read(char *p)
4686 int len = 0;
4687 int i, j;
4688 u8 v;
4690 len += sprintf(p + len, "EC "
4691 " +00 +01 +02 +03 +04 +05 +06 +07"
4692 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4693 for (i = 0; i < 256; i += 16) {
4694 len += sprintf(p + len, "EC 0x%02x:", i);
4695 for (j = 0; j < 16; j++) {
4696 if (!acpi_ec_read(i + j, &v))
4697 break;
4698 if (v != ecdump_regs[i + j])
4699 len += sprintf(p + len, " *%02x", v);
4700 else
4701 len += sprintf(p + len, " %02x", v);
4702 ecdump_regs[i + j] = v;
4704 len += sprintf(p + len, "\n");
4705 if (j != 16)
4706 break;
4709 /* These are way too dangerous to advertise openly... */
4710 #if 0
4711 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4712 " (<offset> is 00-ff, <value> is 00-ff)\n");
4713 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4714 " (<offset> is 00-ff, <value> is 0-255)\n");
4715 #endif
4716 return len;
4719 static int ecdump_write(char *buf)
4721 char *cmd;
4722 int i, v;
4724 while ((cmd = next_cmd(&buf))) {
4725 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4726 /* i and v set */
4727 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4728 /* i and v set */
4729 } else
4730 return -EINVAL;
4731 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4732 if (!acpi_ec_write(i, v))
4733 return -EIO;
4734 } else
4735 return -EINVAL;
4738 return 0;
4741 static struct ibm_struct ecdump_driver_data = {
4742 .name = "ecdump",
4743 .read = ecdump_read,
4744 .write = ecdump_write,
4745 .flags.experimental = 1,
4748 /*************************************************************************
4749 * Backlight/brightness subdriver
4752 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4754 enum {
4755 TP_EC_BACKLIGHT = 0x31,
4757 /* TP_EC_BACKLIGHT bitmasks */
4758 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4759 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4760 TP_EC_BACKLIGHT_MAPSW = 0x20,
4763 static struct backlight_device *ibm_backlight_device;
4764 static int brightness_mode;
4765 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4767 static struct mutex brightness_mutex;
4770 * ThinkPads can read brightness from two places: EC 0x31, or
4771 * CMOS NVRAM byte 0x5E, bits 0-3.
4773 * EC 0x31 has the following layout
4774 * Bit 7: unknown function
4775 * Bit 6: unknown function
4776 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4777 * Bit 4: must be set to zero to avoid problems
4778 * Bit 3-0: backlight brightness level
4780 * brightness_get_raw returns status data in the EC 0x31 layout
4782 static int brightness_get_raw(int *status)
4784 u8 lec = 0, lcmos = 0, level = 0;
4786 if (brightness_mode & 1) {
4787 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4788 return -EIO;
4789 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4791 if (brightness_mode & 2) {
4792 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4793 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4794 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4795 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4796 level = lcmos;
4799 if (brightness_mode == 3) {
4800 *status = lec; /* Prefer EC, CMOS is just a backing store */
4801 lec &= TP_EC_BACKLIGHT_LVLMSK;
4802 if (lec == lcmos)
4803 tp_warned.bright_cmos_ec_unsync = 0;
4804 else {
4805 if (!tp_warned.bright_cmos_ec_unsync) {
4806 printk(TPACPI_ERR
4807 "CMOS NVRAM (%u) and EC (%u) do not "
4808 "agree on display brightness level\n",
4809 (unsigned int) lcmos,
4810 (unsigned int) lec);
4811 tp_warned.bright_cmos_ec_unsync = 1;
4813 return -EIO;
4815 } else {
4816 *status = level;
4819 return 0;
4822 /* May return EINTR which can always be mapped to ERESTARTSYS */
4823 static int brightness_set(int value)
4825 int cmos_cmd, inc, i, res;
4826 int current_value;
4827 int command_bits;
4829 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4830 value < 0)
4831 return -EINVAL;
4833 res = mutex_lock_interruptible(&brightness_mutex);
4834 if (res < 0)
4835 return res;
4837 res = brightness_get_raw(&current_value);
4838 if (res < 0)
4839 goto errout;
4841 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4842 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4844 cmos_cmd = value > current_value ?
4845 TP_CMOS_BRIGHTNESS_UP :
4846 TP_CMOS_BRIGHTNESS_DOWN;
4847 inc = (value > current_value)? 1 : -1;
4849 res = 0;
4850 for (i = current_value; i != value; i += inc) {
4851 if ((brightness_mode & 2) &&
4852 issue_thinkpad_cmos_command(cmos_cmd)) {
4853 res = -EIO;
4854 goto errout;
4856 if ((brightness_mode & 1) &&
4857 !acpi_ec_write(TP_EC_BACKLIGHT,
4858 (i + inc) | command_bits)) {
4859 res = -EIO;
4860 goto errout;;
4864 errout:
4865 mutex_unlock(&brightness_mutex);
4866 return res;
4869 /* sysfs backlight class ----------------------------------------------- */
4871 static int brightness_update_status(struct backlight_device *bd)
4873 /* it is the backlight class's job (caller) to handle
4874 * EINTR and other errors properly */
4875 return brightness_set(
4876 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4877 bd->props.power == FB_BLANK_UNBLANK) ?
4878 bd->props.brightness : 0);
4881 static int brightness_get(struct backlight_device *bd)
4883 int status, res;
4885 res = brightness_get_raw(&status);
4886 if (res < 0)
4887 return 0; /* FIXME: teach backlight about error handling */
4889 return status & TP_EC_BACKLIGHT_LVLMSK;
4892 static struct backlight_ops ibm_backlight_data = {
4893 .get_brightness = brightness_get,
4894 .update_status = brightness_update_status,
4897 /* --------------------------------------------------------------------- */
4899 static int __init brightness_init(struct ibm_init_struct *iibm)
4901 int b;
4903 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4905 mutex_init(&brightness_mutex);
4908 * We always attempt to detect acpi support, so as to switch
4909 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4910 * going to publish a backlight interface
4912 b = tpacpi_check_std_acpi_brightness_support();
4913 if (b > 0) {
4914 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4915 printk(TPACPI_NOTICE
4916 "Lenovo BIOS switched to ACPI backlight "
4917 "control mode\n");
4919 if (brightness_enable > 1) {
4920 printk(TPACPI_NOTICE
4921 "standard ACPI backlight interface "
4922 "available, not loading native one...\n");
4923 return 1;
4927 if (!brightness_enable) {
4928 dbg_printk(TPACPI_DBG_INIT,
4929 "brightness support disabled by "
4930 "module parameter\n");
4931 return 1;
4934 if (b > 16) {
4935 printk(TPACPI_ERR
4936 "Unsupported brightness interface, "
4937 "please contact %s\n", TPACPI_MAIL);
4938 return 1;
4940 if (b == 16)
4941 tp_features.bright_16levels = 1;
4943 if (!brightness_mode) {
4944 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4945 brightness_mode = 2;
4946 else
4947 brightness_mode = 3;
4949 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4950 brightness_mode);
4953 if (brightness_mode > 3)
4954 return -EINVAL;
4956 if (brightness_get_raw(&b) < 0)
4957 return 1;
4959 if (tp_features.bright_16levels)
4960 printk(TPACPI_INFO
4961 "detected a 16-level brightness capable ThinkPad\n");
4963 ibm_backlight_device = backlight_device_register(
4964 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4965 &ibm_backlight_data);
4966 if (IS_ERR(ibm_backlight_device)) {
4967 printk(TPACPI_ERR "Could not register backlight device\n");
4968 return PTR_ERR(ibm_backlight_device);
4970 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4972 ibm_backlight_device->props.max_brightness =
4973 (tp_features.bright_16levels)? 15 : 7;
4974 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4975 backlight_update_status(ibm_backlight_device);
4977 return 0;
4980 static void brightness_exit(void)
4982 if (ibm_backlight_device) {
4983 vdbg_printk(TPACPI_DBG_EXIT,
4984 "calling backlight_device_unregister()\n");
4985 backlight_device_unregister(ibm_backlight_device);
4989 static int brightness_read(char *p)
4991 int len = 0;
4992 int level;
4994 level = brightness_get(NULL);
4995 if (level < 0) {
4996 len += sprintf(p + len, "level:\t\tunreadable\n");
4997 } else {
4998 len += sprintf(p + len, "level:\t\t%d\n", level);
4999 len += sprintf(p + len, "commands:\tup, down\n");
5000 len += sprintf(p + len, "commands:\tlevel <level>"
5001 " (<level> is 0-%d)\n",
5002 (tp_features.bright_16levels) ? 15 : 7);
5005 return len;
5008 static int brightness_write(char *buf)
5010 int level;
5011 int rc;
5012 char *cmd;
5013 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5015 level = brightness_get(NULL);
5016 if (level < 0)
5017 return level;
5019 while ((cmd = next_cmd(&buf))) {
5020 if (strlencmp(cmd, "up") == 0) {
5021 if (level < max_level)
5022 level++;
5023 } else if (strlencmp(cmd, "down") == 0) {
5024 if (level > 0)
5025 level--;
5026 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5027 level >= 0 && level <= max_level) {
5028 /* new level set */
5029 } else
5030 return -EINVAL;
5034 * Now we know what the final level should be, so we try to set it.
5035 * Doing it this way makes the syscall restartable in case of EINTR
5037 rc = brightness_set(level);
5038 return (rc == -EINTR)? ERESTARTSYS : rc;
5041 static struct ibm_struct brightness_driver_data = {
5042 .name = "brightness",
5043 .read = brightness_read,
5044 .write = brightness_write,
5045 .exit = brightness_exit,
5048 /*************************************************************************
5049 * Volume subdriver
5052 static int volume_offset = 0x30;
5054 static int volume_read(char *p)
5056 int len = 0;
5057 u8 level;
5059 if (!acpi_ec_read(volume_offset, &level)) {
5060 len += sprintf(p + len, "level:\t\tunreadable\n");
5061 } else {
5062 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5063 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5064 len += sprintf(p + len, "commands:\tup, down, mute\n");
5065 len += sprintf(p + len, "commands:\tlevel <level>"
5066 " (<level> is 0-15)\n");
5069 return len;
5072 static int volume_write(char *buf)
5074 int cmos_cmd, inc, i;
5075 u8 level, mute;
5076 int new_level, new_mute;
5077 char *cmd;
5079 while ((cmd = next_cmd(&buf))) {
5080 if (!acpi_ec_read(volume_offset, &level))
5081 return -EIO;
5082 new_mute = mute = level & 0x40;
5083 new_level = level = level & 0xf;
5085 if (strlencmp(cmd, "up") == 0) {
5086 if (mute)
5087 new_mute = 0;
5088 else
5089 new_level = level == 15 ? 15 : level + 1;
5090 } else if (strlencmp(cmd, "down") == 0) {
5091 if (mute)
5092 new_mute = 0;
5093 else
5094 new_level = level == 0 ? 0 : level - 1;
5095 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5096 new_level >= 0 && new_level <= 15) {
5097 /* new_level set */
5098 } else if (strlencmp(cmd, "mute") == 0) {
5099 new_mute = 0x40;
5100 } else
5101 return -EINVAL;
5103 if (new_level != level) {
5104 /* mute doesn't change */
5106 cmos_cmd = (new_level > level) ?
5107 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5108 inc = new_level > level ? 1 : -1;
5110 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5111 !acpi_ec_write(volume_offset, level)))
5112 return -EIO;
5114 for (i = level; i != new_level; i += inc)
5115 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5116 !acpi_ec_write(volume_offset, i + inc))
5117 return -EIO;
5119 if (mute &&
5120 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5121 !acpi_ec_write(volume_offset, new_level + mute))) {
5122 return -EIO;
5126 if (new_mute != mute) {
5127 /* level doesn't change */
5129 cmos_cmd = (new_mute) ?
5130 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5132 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5133 !acpi_ec_write(volume_offset, level + new_mute))
5134 return -EIO;
5138 return 0;
5141 static struct ibm_struct volume_driver_data = {
5142 .name = "volume",
5143 .read = volume_read,
5144 .write = volume_write,
5147 /*************************************************************************
5148 * Fan subdriver
5152 * FAN ACCESS MODES
5154 * TPACPI_FAN_RD_ACPI_GFAN:
5155 * ACPI GFAN method: returns fan level
5157 * see TPACPI_FAN_WR_ACPI_SFAN
5158 * EC 0x2f (HFSP) not available if GFAN exists
5160 * TPACPI_FAN_WR_ACPI_SFAN:
5161 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5163 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5164 * it for writing.
5166 * TPACPI_FAN_WR_TPEC:
5167 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5168 * Supported on almost all ThinkPads
5170 * Fan speed changes of any sort (including those caused by the
5171 * disengaged mode) are usually done slowly by the firmware as the
5172 * maximum ammount of fan duty cycle change per second seems to be
5173 * limited.
5175 * Reading is not available if GFAN exists.
5176 * Writing is not available if SFAN exists.
5178 * Bits
5179 * 7 automatic mode engaged;
5180 * (default operation mode of the ThinkPad)
5181 * fan level is ignored in this mode.
5182 * 6 full speed mode (takes precedence over bit 7);
5183 * not available on all thinkpads. May disable
5184 * the tachometer while the fan controller ramps up
5185 * the speed (which can take up to a few *minutes*).
5186 * Speeds up fan to 100% duty-cycle, which is far above
5187 * the standard RPM levels. It is not impossible that
5188 * it could cause hardware damage.
5189 * 5-3 unused in some models. Extra bits for fan level
5190 * in others, but still useless as all values above
5191 * 7 map to the same speed as level 7 in these models.
5192 * 2-0 fan level (0..7 usually)
5193 * 0x00 = stop
5194 * 0x07 = max (set when temperatures critical)
5195 * Some ThinkPads may have other levels, see
5196 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5198 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5199 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5200 * does so, its initial value is meaningless (0x07).
5202 * For firmware bugs, refer to:
5203 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5205 * ----
5207 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5208 * Main fan tachometer reading (in RPM)
5210 * This register is present on all ThinkPads with a new-style EC, and
5211 * it is known not to be present on the A21m/e, and T22, as there is
5212 * something else in offset 0x84 according to the ACPI DSDT. Other
5213 * ThinkPads from this same time period (and earlier) probably lack the
5214 * tachometer as well.
5216 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5217 * was never fixed by IBM to report the EC firmware version string
5218 * probably support the tachometer (like the early X models), so
5219 * detecting it is quite hard. We need more data to know for sure.
5221 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5222 * might result.
5224 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5225 * mode.
5227 * For firmware bugs, refer to:
5228 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5230 * TPACPI_FAN_WR_ACPI_FANS:
5231 * ThinkPad X31, X40, X41. Not available in the X60.
5233 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5234 * high speed. ACPI DSDT seems to map these three speeds to levels
5235 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5236 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5238 * The speeds are stored on handles
5239 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5241 * There are three default speed sets, acessible as handles:
5242 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5244 * ACPI DSDT switches which set is in use depending on various
5245 * factors.
5247 * TPACPI_FAN_WR_TPEC is also available and should be used to
5248 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5249 * but the ACPI tables just mention level 7.
5252 enum { /* Fan control constants */
5253 fan_status_offset = 0x2f, /* EC register 0x2f */
5254 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5255 * 0x84 must be read before 0x85 */
5257 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5258 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5260 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5263 enum fan_status_access_mode {
5264 TPACPI_FAN_NONE = 0, /* No fan status or control */
5265 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5266 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5269 enum fan_control_access_mode {
5270 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5271 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5272 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5273 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5276 enum fan_control_commands {
5277 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5278 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5279 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5280 * and also watchdog cmd */
5283 static int fan_control_allowed;
5285 static enum fan_status_access_mode fan_status_access_mode;
5286 static enum fan_control_access_mode fan_control_access_mode;
5287 static enum fan_control_commands fan_control_commands;
5289 static u8 fan_control_initial_status;
5290 static u8 fan_control_desired_level;
5291 static int fan_watchdog_maxinterval;
5293 static struct mutex fan_mutex;
5295 static void fan_watchdog_fire(struct work_struct *ignored);
5296 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5298 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5299 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5300 "\\FSPD", /* 600e/x, 770e, 770x */
5301 ); /* all others */
5302 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5303 "JFNS", /* 770x-JL */
5304 ); /* all others */
5307 * Call with fan_mutex held
5309 static void fan_update_desired_level(u8 status)
5311 if ((status &
5312 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5313 if (status > 7)
5314 fan_control_desired_level = 7;
5315 else
5316 fan_control_desired_level = status;
5320 static int fan_get_status(u8 *status)
5322 u8 s;
5324 /* TODO:
5325 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5327 switch (fan_status_access_mode) {
5328 case TPACPI_FAN_RD_ACPI_GFAN:
5329 /* 570, 600e/x, 770e, 770x */
5331 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5332 return -EIO;
5334 if (likely(status))
5335 *status = s & 0x07;
5337 break;
5339 case TPACPI_FAN_RD_TPEC:
5340 /* all except 570, 600e/x, 770e, 770x */
5341 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5342 return -EIO;
5344 if (likely(status))
5345 *status = s;
5347 break;
5349 default:
5350 return -ENXIO;
5353 return 0;
5356 static int fan_get_status_safe(u8 *status)
5358 int rc;
5359 u8 s;
5361 if (mutex_lock_interruptible(&fan_mutex))
5362 return -ERESTARTSYS;
5363 rc = fan_get_status(&s);
5364 if (!rc)
5365 fan_update_desired_level(s);
5366 mutex_unlock(&fan_mutex);
5368 if (status)
5369 *status = s;
5371 return rc;
5374 static int fan_get_speed(unsigned int *speed)
5376 u8 hi, lo;
5378 switch (fan_status_access_mode) {
5379 case TPACPI_FAN_RD_TPEC:
5380 /* all except 570, 600e/x, 770e, 770x */
5381 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5382 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5383 return -EIO;
5385 if (likely(speed))
5386 *speed = (hi << 8) | lo;
5388 break;
5390 default:
5391 return -ENXIO;
5394 return 0;
5397 static int fan_set_level(int level)
5399 if (!fan_control_allowed)
5400 return -EPERM;
5402 switch (fan_control_access_mode) {
5403 case TPACPI_FAN_WR_ACPI_SFAN:
5404 if (level >= 0 && level <= 7) {
5405 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5406 return -EIO;
5407 } else
5408 return -EINVAL;
5409 break;
5411 case TPACPI_FAN_WR_ACPI_FANS:
5412 case TPACPI_FAN_WR_TPEC:
5413 if ((level != TP_EC_FAN_AUTO) &&
5414 (level != TP_EC_FAN_FULLSPEED) &&
5415 ((level < 0) || (level > 7)))
5416 return -EINVAL;
5418 /* safety net should the EC not support AUTO
5419 * or FULLSPEED mode bits and just ignore them */
5420 if (level & TP_EC_FAN_FULLSPEED)
5421 level |= 7; /* safety min speed 7 */
5422 else if (level & TP_EC_FAN_AUTO)
5423 level |= 4; /* safety min speed 4 */
5425 if (!acpi_ec_write(fan_status_offset, level))
5426 return -EIO;
5427 else
5428 tp_features.fan_ctrl_status_undef = 0;
5429 break;
5431 default:
5432 return -ENXIO;
5434 return 0;
5437 static int fan_set_level_safe(int level)
5439 int rc;
5441 if (!fan_control_allowed)
5442 return -EPERM;
5444 if (mutex_lock_interruptible(&fan_mutex))
5445 return -ERESTARTSYS;
5447 if (level == TPACPI_FAN_LAST_LEVEL)
5448 level = fan_control_desired_level;
5450 rc = fan_set_level(level);
5451 if (!rc)
5452 fan_update_desired_level(level);
5454 mutex_unlock(&fan_mutex);
5455 return rc;
5458 static int fan_set_enable(void)
5460 u8 s;
5461 int rc;
5463 if (!fan_control_allowed)
5464 return -EPERM;
5466 if (mutex_lock_interruptible(&fan_mutex))
5467 return -ERESTARTSYS;
5469 switch (fan_control_access_mode) {
5470 case TPACPI_FAN_WR_ACPI_FANS:
5471 case TPACPI_FAN_WR_TPEC:
5472 rc = fan_get_status(&s);
5473 if (rc < 0)
5474 break;
5476 /* Don't go out of emergency fan mode */
5477 if (s != 7) {
5478 s &= 0x07;
5479 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5482 if (!acpi_ec_write(fan_status_offset, s))
5483 rc = -EIO;
5484 else {
5485 tp_features.fan_ctrl_status_undef = 0;
5486 rc = 0;
5488 break;
5490 case TPACPI_FAN_WR_ACPI_SFAN:
5491 rc = fan_get_status(&s);
5492 if (rc < 0)
5493 break;
5495 s &= 0x07;
5497 /* Set fan to at least level 4 */
5498 s |= 4;
5500 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5501 rc = -EIO;
5502 else
5503 rc = 0;
5504 break;
5506 default:
5507 rc = -ENXIO;
5510 mutex_unlock(&fan_mutex);
5511 return rc;
5514 static int fan_set_disable(void)
5516 int rc;
5518 if (!fan_control_allowed)
5519 return -EPERM;
5521 if (mutex_lock_interruptible(&fan_mutex))
5522 return -ERESTARTSYS;
5524 rc = 0;
5525 switch (fan_control_access_mode) {
5526 case TPACPI_FAN_WR_ACPI_FANS:
5527 case TPACPI_FAN_WR_TPEC:
5528 if (!acpi_ec_write(fan_status_offset, 0x00))
5529 rc = -EIO;
5530 else {
5531 fan_control_desired_level = 0;
5532 tp_features.fan_ctrl_status_undef = 0;
5534 break;
5536 case TPACPI_FAN_WR_ACPI_SFAN:
5537 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5538 rc = -EIO;
5539 else
5540 fan_control_desired_level = 0;
5541 break;
5543 default:
5544 rc = -ENXIO;
5548 mutex_unlock(&fan_mutex);
5549 return rc;
5552 static int fan_set_speed(int speed)
5554 int rc;
5556 if (!fan_control_allowed)
5557 return -EPERM;
5559 if (mutex_lock_interruptible(&fan_mutex))
5560 return -ERESTARTSYS;
5562 rc = 0;
5563 switch (fan_control_access_mode) {
5564 case TPACPI_FAN_WR_ACPI_FANS:
5565 if (speed >= 0 && speed <= 65535) {
5566 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5567 speed, speed, speed))
5568 rc = -EIO;
5569 } else
5570 rc = -EINVAL;
5571 break;
5573 default:
5574 rc = -ENXIO;
5577 mutex_unlock(&fan_mutex);
5578 return rc;
5581 static void fan_watchdog_reset(void)
5583 static int fan_watchdog_active;
5585 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5586 return;
5588 if (fan_watchdog_active)
5589 cancel_delayed_work(&fan_watchdog_task);
5591 if (fan_watchdog_maxinterval > 0 &&
5592 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5593 fan_watchdog_active = 1;
5594 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5595 msecs_to_jiffies(fan_watchdog_maxinterval
5596 * 1000))) {
5597 printk(TPACPI_ERR
5598 "failed to queue the fan watchdog, "
5599 "watchdog will not trigger\n");
5601 } else
5602 fan_watchdog_active = 0;
5605 static void fan_watchdog_fire(struct work_struct *ignored)
5607 int rc;
5609 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5610 return;
5612 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5613 rc = fan_set_enable();
5614 if (rc < 0) {
5615 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5616 "will try again later...\n", -rc);
5617 /* reschedule for later */
5618 fan_watchdog_reset();
5623 * SYSFS fan layout: hwmon compatible (device)
5625 * pwm*_enable:
5626 * 0: "disengaged" mode
5627 * 1: manual mode
5628 * 2: native EC "auto" mode (recommended, hardware default)
5630 * pwm*: set speed in manual mode, ignored otherwise.
5631 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5632 * interpolation.
5634 * fan*_input: tachometer reading, RPM
5637 * SYSFS fan layout: extensions
5639 * fan_watchdog (driver):
5640 * fan watchdog interval in seconds, 0 disables (default), max 120
5643 /* sysfs fan pwm1_enable ----------------------------------------------- */
5644 static ssize_t fan_pwm1_enable_show(struct device *dev,
5645 struct device_attribute *attr,
5646 char *buf)
5648 int res, mode;
5649 u8 status;
5651 res = fan_get_status_safe(&status);
5652 if (res)
5653 return res;
5655 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5656 if (status != fan_control_initial_status) {
5657 tp_features.fan_ctrl_status_undef = 0;
5658 } else {
5659 /* Return most likely status. In fact, it
5660 * might be the only possible status */
5661 status = TP_EC_FAN_AUTO;
5665 if (status & TP_EC_FAN_FULLSPEED) {
5666 mode = 0;
5667 } else if (status & TP_EC_FAN_AUTO) {
5668 mode = 2;
5669 } else
5670 mode = 1;
5672 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5675 static ssize_t fan_pwm1_enable_store(struct device *dev,
5676 struct device_attribute *attr,
5677 const char *buf, size_t count)
5679 unsigned long t;
5680 int res, level;
5682 if (parse_strtoul(buf, 2, &t))
5683 return -EINVAL;
5685 switch (t) {
5686 case 0:
5687 level = TP_EC_FAN_FULLSPEED;
5688 break;
5689 case 1:
5690 level = TPACPI_FAN_LAST_LEVEL;
5691 break;
5692 case 2:
5693 level = TP_EC_FAN_AUTO;
5694 break;
5695 case 3:
5696 /* reserved for software-controlled auto mode */
5697 return -ENOSYS;
5698 default:
5699 return -EINVAL;
5702 res = fan_set_level_safe(level);
5703 if (res == -ENXIO)
5704 return -EINVAL;
5705 else if (res < 0)
5706 return res;
5708 fan_watchdog_reset();
5710 return count;
5713 static struct device_attribute dev_attr_fan_pwm1_enable =
5714 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5715 fan_pwm1_enable_show, fan_pwm1_enable_store);
5717 /* sysfs fan pwm1 ------------------------------------------------------ */
5718 static ssize_t fan_pwm1_show(struct device *dev,
5719 struct device_attribute *attr,
5720 char *buf)
5722 int res;
5723 u8 status;
5725 res = fan_get_status_safe(&status);
5726 if (res)
5727 return res;
5729 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5730 if (status != fan_control_initial_status) {
5731 tp_features.fan_ctrl_status_undef = 0;
5732 } else {
5733 status = TP_EC_FAN_AUTO;
5737 if ((status &
5738 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5739 status = fan_control_desired_level;
5741 if (status > 7)
5742 status = 7;
5744 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5747 static ssize_t fan_pwm1_store(struct device *dev,
5748 struct device_attribute *attr,
5749 const char *buf, size_t count)
5751 unsigned long s;
5752 int rc;
5753 u8 status, newlevel;
5755 if (parse_strtoul(buf, 255, &s))
5756 return -EINVAL;
5758 /* scale down from 0-255 to 0-7 */
5759 newlevel = (s >> 5) & 0x07;
5761 if (mutex_lock_interruptible(&fan_mutex))
5762 return -ERESTARTSYS;
5764 rc = fan_get_status(&status);
5765 if (!rc && (status &
5766 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5767 rc = fan_set_level(newlevel);
5768 if (rc == -ENXIO)
5769 rc = -EINVAL;
5770 else if (!rc) {
5771 fan_update_desired_level(newlevel);
5772 fan_watchdog_reset();
5776 mutex_unlock(&fan_mutex);
5777 return (rc)? rc : count;
5780 static struct device_attribute dev_attr_fan_pwm1 =
5781 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5782 fan_pwm1_show, fan_pwm1_store);
5784 /* sysfs fan fan1_input ------------------------------------------------ */
5785 static ssize_t fan_fan1_input_show(struct device *dev,
5786 struct device_attribute *attr,
5787 char *buf)
5789 int res;
5790 unsigned int speed;
5792 res = fan_get_speed(&speed);
5793 if (res < 0)
5794 return res;
5796 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5799 static struct device_attribute dev_attr_fan_fan1_input =
5800 __ATTR(fan1_input, S_IRUGO,
5801 fan_fan1_input_show, NULL);
5803 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5804 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5805 char *buf)
5807 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5810 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5811 const char *buf, size_t count)
5813 unsigned long t;
5815 if (parse_strtoul(buf, 120, &t))
5816 return -EINVAL;
5818 if (!fan_control_allowed)
5819 return -EPERM;
5821 fan_watchdog_maxinterval = t;
5822 fan_watchdog_reset();
5824 return count;
5827 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5828 fan_fan_watchdog_show, fan_fan_watchdog_store);
5830 /* --------------------------------------------------------------------- */
5831 static struct attribute *fan_attributes[] = {
5832 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5833 &dev_attr_fan_fan1_input.attr,
5834 NULL
5837 static const struct attribute_group fan_attr_group = {
5838 .attrs = fan_attributes,
5841 static int __init fan_init(struct ibm_init_struct *iibm)
5843 int rc;
5845 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5847 mutex_init(&fan_mutex);
5848 fan_status_access_mode = TPACPI_FAN_NONE;
5849 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5850 fan_control_commands = 0;
5851 fan_watchdog_maxinterval = 0;
5852 tp_features.fan_ctrl_status_undef = 0;
5853 fan_control_desired_level = 7;
5855 TPACPI_ACPIHANDLE_INIT(fans);
5856 TPACPI_ACPIHANDLE_INIT(gfan);
5857 TPACPI_ACPIHANDLE_INIT(sfan);
5859 if (gfan_handle) {
5860 /* 570, 600e/x, 770e, 770x */
5861 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5862 } else {
5863 /* all other ThinkPads: note that even old-style
5864 * ThinkPad ECs supports the fan control register */
5865 if (likely(acpi_ec_read(fan_status_offset,
5866 &fan_control_initial_status))) {
5867 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5869 /* In some ThinkPads, neither the EC nor the ACPI
5870 * DSDT initialize the fan status, and it ends up
5871 * being set to 0x07 when it *could* be either
5872 * 0x07 or 0x80.
5874 * Enable for TP-1Y (T43), TP-78 (R51e),
5875 * TP-76 (R52), TP-70 (T43, R52), which are known
5876 * to be buggy. */
5877 if (fan_control_initial_status == 0x07) {
5878 switch (thinkpad_id.ec_model) {
5879 case 0x5931: /* TP-1Y */
5880 case 0x3837: /* TP-78 */
5881 case 0x3637: /* TP-76 */
5882 case 0x3037: /* TP-70 */
5883 printk(TPACPI_NOTICE
5884 "fan_init: initial fan status "
5885 "is unknown, assuming it is "
5886 "in auto mode\n");
5887 tp_features.fan_ctrl_status_undef = 1;
5891 } else {
5892 printk(TPACPI_ERR
5893 "ThinkPad ACPI EC access misbehaving, "
5894 "fan status and control unavailable\n");
5895 return 1;
5899 if (sfan_handle) {
5900 /* 570, 770x-JL */
5901 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5902 fan_control_commands |=
5903 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5904 } else {
5905 if (!gfan_handle) {
5906 /* gfan without sfan means no fan control */
5907 /* all other models implement TP EC 0x2f control */
5909 if (fans_handle) {
5910 /* X31, X40, X41 */
5911 fan_control_access_mode =
5912 TPACPI_FAN_WR_ACPI_FANS;
5913 fan_control_commands |=
5914 TPACPI_FAN_CMD_SPEED |
5915 TPACPI_FAN_CMD_LEVEL |
5916 TPACPI_FAN_CMD_ENABLE;
5917 } else {
5918 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5919 fan_control_commands |=
5920 TPACPI_FAN_CMD_LEVEL |
5921 TPACPI_FAN_CMD_ENABLE;
5926 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5927 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5928 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5929 fan_status_access_mode, fan_control_access_mode);
5931 /* fan control master switch */
5932 if (!fan_control_allowed) {
5933 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5934 fan_control_commands = 0;
5935 dbg_printk(TPACPI_DBG_INIT,
5936 "fan control features disabled by parameter\n");
5939 /* update fan_control_desired_level */
5940 if (fan_status_access_mode != TPACPI_FAN_NONE)
5941 fan_get_status_safe(NULL);
5943 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5944 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5945 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5946 &fan_attr_group);
5947 if (rc < 0)
5948 return rc;
5950 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5951 &driver_attr_fan_watchdog);
5952 if (rc < 0) {
5953 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5954 &fan_attr_group);
5955 return rc;
5957 return 0;
5958 } else
5959 return 1;
5962 static void fan_exit(void)
5964 vdbg_printk(TPACPI_DBG_EXIT,
5965 "cancelling any pending fan watchdog tasks\n");
5967 /* FIXME: can we really do this unconditionally? */
5968 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5969 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5970 &driver_attr_fan_watchdog);
5972 cancel_delayed_work(&fan_watchdog_task);
5973 flush_workqueue(tpacpi_wq);
5976 static int fan_read(char *p)
5978 int len = 0;
5979 int rc;
5980 u8 status;
5981 unsigned int speed = 0;
5983 switch (fan_status_access_mode) {
5984 case TPACPI_FAN_RD_ACPI_GFAN:
5985 /* 570, 600e/x, 770e, 770x */
5986 rc = fan_get_status_safe(&status);
5987 if (rc < 0)
5988 return rc;
5990 len += sprintf(p + len, "status:\t\t%s\n"
5991 "level:\t\t%d\n",
5992 (status != 0) ? "enabled" : "disabled", status);
5993 break;
5995 case TPACPI_FAN_RD_TPEC:
5996 /* all except 570, 600e/x, 770e, 770x */
5997 rc = fan_get_status_safe(&status);
5998 if (rc < 0)
5999 return rc;
6001 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6002 if (status != fan_control_initial_status)
6003 tp_features.fan_ctrl_status_undef = 0;
6004 else
6005 /* Return most likely status. In fact, it
6006 * might be the only possible status */
6007 status = TP_EC_FAN_AUTO;
6010 len += sprintf(p + len, "status:\t\t%s\n",
6011 (status != 0) ? "enabled" : "disabled");
6013 rc = fan_get_speed(&speed);
6014 if (rc < 0)
6015 return rc;
6017 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6019 if (status & TP_EC_FAN_FULLSPEED)
6020 /* Disengaged mode takes precedence */
6021 len += sprintf(p + len, "level:\t\tdisengaged\n");
6022 else if (status & TP_EC_FAN_AUTO)
6023 len += sprintf(p + len, "level:\t\tauto\n");
6024 else
6025 len += sprintf(p + len, "level:\t\t%d\n", status);
6026 break;
6028 case TPACPI_FAN_NONE:
6029 default:
6030 len += sprintf(p + len, "status:\t\tnot supported\n");
6033 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6034 len += sprintf(p + len, "commands:\tlevel <level>");
6036 switch (fan_control_access_mode) {
6037 case TPACPI_FAN_WR_ACPI_SFAN:
6038 len += sprintf(p + len, " (<level> is 0-7)\n");
6039 break;
6041 default:
6042 len += sprintf(p + len, " (<level> is 0-7, "
6043 "auto, disengaged, full-speed)\n");
6044 break;
6048 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6049 len += sprintf(p + len, "commands:\tenable, disable\n"
6050 "commands:\twatchdog <timeout> (<timeout> "
6051 "is 0 (off), 1-120 (seconds))\n");
6053 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6054 len += sprintf(p + len, "commands:\tspeed <speed>"
6055 " (<speed> is 0-65535)\n");
6057 return len;
6060 static int fan_write_cmd_level(const char *cmd, int *rc)
6062 int level;
6064 if (strlencmp(cmd, "level auto") == 0)
6065 level = TP_EC_FAN_AUTO;
6066 else if ((strlencmp(cmd, "level disengaged") == 0) |
6067 (strlencmp(cmd, "level full-speed") == 0))
6068 level = TP_EC_FAN_FULLSPEED;
6069 else if (sscanf(cmd, "level %d", &level) != 1)
6070 return 0;
6072 *rc = fan_set_level_safe(level);
6073 if (*rc == -ENXIO)
6074 printk(TPACPI_ERR "level command accepted for unsupported "
6075 "access mode %d", fan_control_access_mode);
6077 return 1;
6080 static int fan_write_cmd_enable(const char *cmd, int *rc)
6082 if (strlencmp(cmd, "enable") != 0)
6083 return 0;
6085 *rc = fan_set_enable();
6086 if (*rc == -ENXIO)
6087 printk(TPACPI_ERR "enable command accepted for unsupported "
6088 "access mode %d", fan_control_access_mode);
6090 return 1;
6093 static int fan_write_cmd_disable(const char *cmd, int *rc)
6095 if (strlencmp(cmd, "disable") != 0)
6096 return 0;
6098 *rc = fan_set_disable();
6099 if (*rc == -ENXIO)
6100 printk(TPACPI_ERR "disable command accepted for unsupported "
6101 "access mode %d", fan_control_access_mode);
6103 return 1;
6106 static int fan_write_cmd_speed(const char *cmd, int *rc)
6108 int speed;
6110 /* TODO:
6111 * Support speed <low> <medium> <high> ? */
6113 if (sscanf(cmd, "speed %d", &speed) != 1)
6114 return 0;
6116 *rc = fan_set_speed(speed);
6117 if (*rc == -ENXIO)
6118 printk(TPACPI_ERR "speed command accepted for unsupported "
6119 "access mode %d", fan_control_access_mode);
6121 return 1;
6124 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6126 int interval;
6128 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6129 return 0;
6131 if (interval < 0 || interval > 120)
6132 *rc = -EINVAL;
6133 else
6134 fan_watchdog_maxinterval = interval;
6136 return 1;
6139 static int fan_write(char *buf)
6141 char *cmd;
6142 int rc = 0;
6144 while (!rc && (cmd = next_cmd(&buf))) {
6145 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6146 fan_write_cmd_level(cmd, &rc)) &&
6147 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6148 (fan_write_cmd_enable(cmd, &rc) ||
6149 fan_write_cmd_disable(cmd, &rc) ||
6150 fan_write_cmd_watchdog(cmd, &rc))) &&
6151 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6152 fan_write_cmd_speed(cmd, &rc))
6154 rc = -EINVAL;
6155 else if (!rc)
6156 fan_watchdog_reset();
6159 return rc;
6162 static struct ibm_struct fan_driver_data = {
6163 .name = "fan",
6164 .read = fan_read,
6165 .write = fan_write,
6166 .exit = fan_exit,
6169 /****************************************************************************
6170 ****************************************************************************
6172 * Infrastructure
6174 ****************************************************************************
6175 ****************************************************************************/
6177 /* sysfs name ---------------------------------------------------------- */
6178 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6179 struct device_attribute *attr,
6180 char *buf)
6182 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6185 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6186 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6188 /* --------------------------------------------------------------------- */
6190 /* /proc support */
6191 static struct proc_dir_entry *proc_dir;
6194 * Module and infrastructure proble, init and exit handling
6197 static int force_load;
6199 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6200 static const char * __init str_supported(int is_supported)
6202 static char text_unsupported[] __initdata = "not supported";
6204 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6206 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6208 static void ibm_exit(struct ibm_struct *ibm)
6210 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6212 list_del_init(&ibm->all_drivers);
6214 if (ibm->flags.acpi_notify_installed) {
6215 dbg_printk(TPACPI_DBG_EXIT,
6216 "%s: acpi_remove_notify_handler\n", ibm->name);
6217 BUG_ON(!ibm->acpi);
6218 acpi_remove_notify_handler(*ibm->acpi->handle,
6219 ibm->acpi->type,
6220 dispatch_acpi_notify);
6221 ibm->flags.acpi_notify_installed = 0;
6222 ibm->flags.acpi_notify_installed = 0;
6225 if (ibm->flags.proc_created) {
6226 dbg_printk(TPACPI_DBG_EXIT,
6227 "%s: remove_proc_entry\n", ibm->name);
6228 remove_proc_entry(ibm->name, proc_dir);
6229 ibm->flags.proc_created = 0;
6232 if (ibm->flags.acpi_driver_registered) {
6233 dbg_printk(TPACPI_DBG_EXIT,
6234 "%s: acpi_bus_unregister_driver\n", ibm->name);
6235 BUG_ON(!ibm->acpi);
6236 acpi_bus_unregister_driver(ibm->acpi->driver);
6237 kfree(ibm->acpi->driver);
6238 ibm->acpi->driver = NULL;
6239 ibm->flags.acpi_driver_registered = 0;
6242 if (ibm->flags.init_called && ibm->exit) {
6243 ibm->exit();
6244 ibm->flags.init_called = 0;
6247 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6250 static int __init ibm_init(struct ibm_init_struct *iibm)
6252 int ret;
6253 struct ibm_struct *ibm = iibm->data;
6254 struct proc_dir_entry *entry;
6256 BUG_ON(ibm == NULL);
6258 INIT_LIST_HEAD(&ibm->all_drivers);
6260 if (ibm->flags.experimental && !experimental)
6261 return 0;
6263 dbg_printk(TPACPI_DBG_INIT,
6264 "probing for %s\n", ibm->name);
6266 if (iibm->init) {
6267 ret = iibm->init(iibm);
6268 if (ret > 0)
6269 return 0; /* probe failed */
6270 if (ret)
6271 return ret;
6273 ibm->flags.init_called = 1;
6276 if (ibm->acpi) {
6277 if (ibm->acpi->hid) {
6278 ret = register_tpacpi_subdriver(ibm);
6279 if (ret)
6280 goto err_out;
6283 if (ibm->acpi->notify) {
6284 ret = setup_acpi_notify(ibm);
6285 if (ret == -ENODEV) {
6286 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6287 ibm->name);
6288 ret = 0;
6289 goto err_out;
6291 if (ret < 0)
6292 goto err_out;
6296 dbg_printk(TPACPI_DBG_INIT,
6297 "%s installed\n", ibm->name);
6299 if (ibm->read) {
6300 entry = create_proc_entry(ibm->name,
6301 S_IFREG | S_IRUGO | S_IWUSR,
6302 proc_dir);
6303 if (!entry) {
6304 printk(TPACPI_ERR "unable to create proc entry %s\n",
6305 ibm->name);
6306 ret = -ENODEV;
6307 goto err_out;
6309 entry->owner = THIS_MODULE;
6310 entry->data = ibm;
6311 entry->read_proc = &dispatch_procfs_read;
6312 if (ibm->write)
6313 entry->write_proc = &dispatch_procfs_write;
6314 ibm->flags.proc_created = 1;
6317 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6319 return 0;
6321 err_out:
6322 dbg_printk(TPACPI_DBG_INIT,
6323 "%s: at error exit path with result %d\n",
6324 ibm->name, ret);
6326 ibm_exit(ibm);
6327 return (ret < 0)? ret : 0;
6330 /* Probing */
6332 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
6334 const struct dmi_device *dev = NULL;
6335 char ec_fw_string[18];
6337 if (!tp)
6338 return;
6340 memset(tp, 0, sizeof(*tp));
6342 if (dmi_name_in_vendors("IBM"))
6343 tp->vendor = PCI_VENDOR_ID_IBM;
6344 else if (dmi_name_in_vendors("LENOVO"))
6345 tp->vendor = PCI_VENDOR_ID_LENOVO;
6346 else
6347 return;
6349 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6350 GFP_KERNEL);
6351 if (!tp->bios_version_str)
6352 return;
6353 tp->bios_model = tp->bios_version_str[0]
6354 | (tp->bios_version_str[1] << 8);
6357 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6358 * X32 or newer, all Z series; Some models must have an
6359 * up-to-date BIOS or they will not be detected.
6361 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6363 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6364 if (sscanf(dev->name,
6365 "IBM ThinkPad Embedded Controller -[%17c",
6366 ec_fw_string) == 1) {
6367 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6368 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6370 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6371 tp->ec_model = ec_fw_string[0]
6372 | (ec_fw_string[1] << 8);
6373 break;
6377 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6378 GFP_KERNEL);
6379 if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6380 kfree(tp->model_str);
6381 tp->model_str = NULL;
6384 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6385 GFP_KERNEL);
6388 static int __init probe_for_thinkpad(void)
6390 int is_thinkpad;
6392 if (acpi_disabled)
6393 return -ENODEV;
6396 * Non-ancient models have better DMI tagging, but very old models
6397 * don't.
6399 is_thinkpad = (thinkpad_id.model_str != NULL);
6401 /* ec is required because many other handles are relative to it */
6402 TPACPI_ACPIHANDLE_INIT(ec);
6403 if (!ec_handle) {
6404 if (is_thinkpad)
6405 printk(TPACPI_ERR
6406 "Not yet supported ThinkPad detected!\n");
6407 return -ENODEV;
6411 * Risks a regression on very old machines, but reduces potential
6412 * false positives a damn great deal
6414 if (!is_thinkpad)
6415 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6417 if (!is_thinkpad && !force_load)
6418 return -ENODEV;
6420 return 0;
6424 /* Module init, exit, parameters */
6426 static struct ibm_init_struct ibms_init[] __initdata = {
6428 .init = thinkpad_acpi_driver_init,
6429 .data = &thinkpad_acpi_driver_data,
6432 .init = hotkey_init,
6433 .data = &hotkey_driver_data,
6436 .init = bluetooth_init,
6437 .data = &bluetooth_driver_data,
6440 .init = wan_init,
6441 .data = &wan_driver_data,
6443 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6445 .init = video_init,
6446 .data = &video_driver_data,
6448 #endif
6450 .init = light_init,
6451 .data = &light_driver_data,
6453 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6455 .init = dock_init,
6456 .data = &dock_driver_data[0],
6459 .init = dock_init2,
6460 .data = &dock_driver_data[1],
6462 #endif
6463 #ifdef CONFIG_THINKPAD_ACPI_BAY
6465 .init = bay_init,
6466 .data = &bay_driver_data,
6468 #endif
6470 .init = cmos_init,
6471 .data = &cmos_driver_data,
6474 .init = led_init,
6475 .data = &led_driver_data,
6478 .init = beep_init,
6479 .data = &beep_driver_data,
6482 .init = thermal_init,
6483 .data = &thermal_driver_data,
6486 .data = &ecdump_driver_data,
6489 .init = brightness_init,
6490 .data = &brightness_driver_data,
6493 .data = &volume_driver_data,
6496 .init = fan_init,
6497 .data = &fan_driver_data,
6501 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6503 unsigned int i;
6504 struct ibm_struct *ibm;
6506 if (!kp || !kp->name || !val)
6507 return -EINVAL;
6509 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6510 ibm = ibms_init[i].data;
6511 WARN_ON(ibm == NULL);
6513 if (!ibm || !ibm->name)
6514 continue;
6516 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6517 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6518 return -ENOSPC;
6519 strcpy(ibms_init[i].param, val);
6520 strcat(ibms_init[i].param, ",");
6521 return 0;
6525 return -EINVAL;
6528 module_param(experimental, int, 0);
6529 MODULE_PARM_DESC(experimental,
6530 "Enables experimental features when non-zero");
6532 module_param_named(debug, dbg_level, uint, 0);
6533 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6535 module_param(force_load, bool, 0);
6536 MODULE_PARM_DESC(force_load,
6537 "Attempts to load the driver even on a "
6538 "mis-identified ThinkPad when true");
6540 module_param_named(fan_control, fan_control_allowed, bool, 0);
6541 MODULE_PARM_DESC(fan_control,
6542 "Enables setting fan parameters features when true");
6544 module_param_named(brightness_mode, brightness_mode, int, 0);
6545 MODULE_PARM_DESC(brightness_mode,
6546 "Selects brightness control strategy: "
6547 "0=auto, 1=EC, 2=CMOS, 3=both");
6549 module_param(brightness_enable, uint, 0);
6550 MODULE_PARM_DESC(brightness_enable,
6551 "Enables backlight control when 1, disables when 0");
6553 module_param(hotkey_report_mode, uint, 0);
6554 MODULE_PARM_DESC(hotkey_report_mode,
6555 "used for backwards compatibility with userspace, "
6556 "see documentation");
6558 #define TPACPI_PARAM(feature) \
6559 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6560 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6561 "at module load, see documentation")
6563 TPACPI_PARAM(hotkey);
6564 TPACPI_PARAM(bluetooth);
6565 TPACPI_PARAM(video);
6566 TPACPI_PARAM(light);
6567 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6568 TPACPI_PARAM(dock);
6569 #endif
6570 #ifdef CONFIG_THINKPAD_ACPI_BAY
6571 TPACPI_PARAM(bay);
6572 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6573 TPACPI_PARAM(cmos);
6574 TPACPI_PARAM(led);
6575 TPACPI_PARAM(beep);
6576 TPACPI_PARAM(ecdump);
6577 TPACPI_PARAM(brightness);
6578 TPACPI_PARAM(volume);
6579 TPACPI_PARAM(fan);
6581 static void thinkpad_acpi_module_exit(void)
6583 struct ibm_struct *ibm, *itmp;
6585 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6587 list_for_each_entry_safe_reverse(ibm, itmp,
6588 &tpacpi_all_drivers,
6589 all_drivers) {
6590 ibm_exit(ibm);
6593 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6595 if (tpacpi_inputdev) {
6596 if (tp_features.input_device_registered)
6597 input_unregister_device(tpacpi_inputdev);
6598 else
6599 input_free_device(tpacpi_inputdev);
6602 if (tpacpi_hwmon)
6603 hwmon_device_unregister(tpacpi_hwmon);
6605 if (tp_features.sensors_pdev_attrs_registered)
6606 device_remove_file(&tpacpi_sensors_pdev->dev,
6607 &dev_attr_thinkpad_acpi_pdev_name);
6608 if (tpacpi_sensors_pdev)
6609 platform_device_unregister(tpacpi_sensors_pdev);
6610 if (tpacpi_pdev)
6611 platform_device_unregister(tpacpi_pdev);
6613 if (tp_features.sensors_pdrv_attrs_registered)
6614 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6615 if (tp_features.platform_drv_attrs_registered)
6616 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6618 if (tp_features.sensors_pdrv_registered)
6619 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6621 if (tp_features.platform_drv_registered)
6622 platform_driver_unregister(&tpacpi_pdriver);
6624 if (proc_dir)
6625 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6627 if (tpacpi_wq)
6628 destroy_workqueue(tpacpi_wq);
6630 kfree(thinkpad_id.bios_version_str);
6631 kfree(thinkpad_id.ec_version_str);
6632 kfree(thinkpad_id.model_str);
6636 static int __init thinkpad_acpi_module_init(void)
6638 int ret, i;
6640 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6642 /* Parameter checking */
6643 if (hotkey_report_mode > 2)
6644 return -EINVAL;
6646 /* Driver-level probe */
6648 get_thinkpad_model_data(&thinkpad_id);
6649 ret = probe_for_thinkpad();
6650 if (ret) {
6651 thinkpad_acpi_module_exit();
6652 return ret;
6655 /* Driver initialization */
6657 TPACPI_ACPIHANDLE_INIT(ecrd);
6658 TPACPI_ACPIHANDLE_INIT(ecwr);
6660 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6661 if (!tpacpi_wq) {
6662 thinkpad_acpi_module_exit();
6663 return -ENOMEM;
6666 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6667 if (!proc_dir) {
6668 printk(TPACPI_ERR
6669 "unable to create proc dir " TPACPI_PROC_DIR);
6670 thinkpad_acpi_module_exit();
6671 return -ENODEV;
6673 proc_dir->owner = THIS_MODULE;
6675 ret = platform_driver_register(&tpacpi_pdriver);
6676 if (ret) {
6677 printk(TPACPI_ERR
6678 "unable to register main platform driver\n");
6679 thinkpad_acpi_module_exit();
6680 return ret;
6682 tp_features.platform_drv_registered = 1;
6684 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6685 if (ret) {
6686 printk(TPACPI_ERR
6687 "unable to register hwmon platform driver\n");
6688 thinkpad_acpi_module_exit();
6689 return ret;
6691 tp_features.sensors_pdrv_registered = 1;
6693 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6694 if (!ret) {
6695 tp_features.platform_drv_attrs_registered = 1;
6696 ret = tpacpi_create_driver_attributes(
6697 &tpacpi_hwmon_pdriver.driver);
6699 if (ret) {
6700 printk(TPACPI_ERR
6701 "unable to create sysfs driver attributes\n");
6702 thinkpad_acpi_module_exit();
6703 return ret;
6705 tp_features.sensors_pdrv_attrs_registered = 1;
6708 /* Device initialization */
6709 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6710 NULL, 0);
6711 if (IS_ERR(tpacpi_pdev)) {
6712 ret = PTR_ERR(tpacpi_pdev);
6713 tpacpi_pdev = NULL;
6714 printk(TPACPI_ERR "unable to register platform device\n");
6715 thinkpad_acpi_module_exit();
6716 return ret;
6718 tpacpi_sensors_pdev = platform_device_register_simple(
6719 TPACPI_HWMON_DRVR_NAME,
6720 -1, NULL, 0);
6721 if (IS_ERR(tpacpi_sensors_pdev)) {
6722 ret = PTR_ERR(tpacpi_sensors_pdev);
6723 tpacpi_sensors_pdev = NULL;
6724 printk(TPACPI_ERR
6725 "unable to register hwmon platform device\n");
6726 thinkpad_acpi_module_exit();
6727 return ret;
6729 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6730 &dev_attr_thinkpad_acpi_pdev_name);
6731 if (ret) {
6732 printk(TPACPI_ERR
6733 "unable to create sysfs hwmon device attributes\n");
6734 thinkpad_acpi_module_exit();
6735 return ret;
6737 tp_features.sensors_pdev_attrs_registered = 1;
6738 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6739 if (IS_ERR(tpacpi_hwmon)) {
6740 ret = PTR_ERR(tpacpi_hwmon);
6741 tpacpi_hwmon = NULL;
6742 printk(TPACPI_ERR "unable to register hwmon device\n");
6743 thinkpad_acpi_module_exit();
6744 return ret;
6746 mutex_init(&tpacpi_inputdev_send_mutex);
6747 tpacpi_inputdev = input_allocate_device();
6748 if (!tpacpi_inputdev) {
6749 printk(TPACPI_ERR "unable to allocate input device\n");
6750 thinkpad_acpi_module_exit();
6751 return -ENOMEM;
6752 } else {
6753 /* Prepare input device, but don't register */
6754 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6755 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6756 tpacpi_inputdev->id.bustype = BUS_HOST;
6757 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6758 thinkpad_id.vendor :
6759 PCI_VENDOR_ID_IBM;
6760 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6761 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6763 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6764 ret = ibm_init(&ibms_init[i]);
6765 if (ret >= 0 && *ibms_init[i].param)
6766 ret = ibms_init[i].data->write(ibms_init[i].param);
6767 if (ret < 0) {
6768 thinkpad_acpi_module_exit();
6769 return ret;
6772 ret = input_register_device(tpacpi_inputdev);
6773 if (ret < 0) {
6774 printk(TPACPI_ERR "unable to register input device\n");
6775 thinkpad_acpi_module_exit();
6776 return ret;
6777 } else {
6778 tp_features.input_device_registered = 1;
6781 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6782 return 0;
6785 /* Please remove this in year 2009 */
6786 MODULE_ALIAS("ibm_acpi");
6788 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6791 * DMI matching for module autoloading
6793 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6794 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6796 * Only models listed in thinkwiki will be supported, so add yours
6797 * if it is not there yet.
6799 #define IBM_BIOS_MODULE_ALIAS(__type) \
6800 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6802 /* Non-ancient thinkpads */
6803 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6804 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6806 /* Ancient thinkpad BIOSes have to be identified by
6807 * BIOS type or model number, and there are far less
6808 * BIOS types than model numbers... */
6809 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6810 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6811 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6813 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6814 MODULE_DESCRIPTION(TPACPI_DESC);
6815 MODULE_VERSION(TPACPI_VERSION);
6816 MODULE_LICENSE("GPL");
6818 module_init(thinkpad_acpi_module_init);
6819 module_exit(thinkpad_acpi_module_exit);