ACPI: thinkpad-acpi: add sysfs led class support to thinkpad leds (v3.2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob31ea9f7b139611dbb12a24a5023779155725c5c3
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.19"
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 <asm/uaccess.h>
73 #include <linux/dmi.h>
74 #include <linux/jiffies.h>
75 #include <linux/workqueue.h>
77 #include <acpi/acpi_drivers.h>
78 #include <acpi/acnamesp.h>
80 #include <linux/pci_ids.h>
83 /* ThinkPad CMOS commands */
84 #define TP_CMOS_VOLUME_DOWN 0
85 #define TP_CMOS_VOLUME_UP 1
86 #define TP_CMOS_VOLUME_MUTE 2
87 #define TP_CMOS_BRIGHTNESS_UP 4
88 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 #define TP_CMOS_THINKLIGHT_ON 12
90 #define TP_CMOS_THINKLIGHT_OFF 13
92 /* NVRAM Addresses */
93 enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
101 /* NVRAM bit masks */
102 enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
118 /* ACPI HIDs */
119 #define TPACPI_ACPI_HKEY_HID "IBM0068"
121 /* Input IDs */
122 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /****************************************************************************
127 * Main driver
130 #define TPACPI_NAME "thinkpad"
131 #define TPACPI_DESC "ThinkPad ACPI Extras"
132 #define TPACPI_FILE TPACPI_NAME "_acpi"
133 #define TPACPI_URL "http://ibm-acpi.sf.net/"
134 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
136 #define TPACPI_PROC_DIR "ibm"
137 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
138 #define TPACPI_DRVR_NAME TPACPI_FILE
139 #define TPACPI_DRVR_SHORTNAME "tpacpi"
140 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
142 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_MAX_ACPI_ARGS 3
146 /* Debugging */
147 #define TPACPI_LOG TPACPI_FILE ": "
148 #define TPACPI_ERR KERN_ERR TPACPI_LOG
149 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
150 #define TPACPI_INFO KERN_INFO TPACPI_LOG
151 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
153 #define TPACPI_DBG_ALL 0xffff
154 #define TPACPI_DBG_ALL 0xffff
155 #define TPACPI_DBG_INIT 0x0001
156 #define TPACPI_DBG_EXIT 0x0002
157 #define dbg_printk(a_dbg_level, format, arg...) \
158 do { if (dbg_level & a_dbg_level) \
159 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
160 } while (0)
161 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
162 #define vdbg_printk(a_dbg_level, format, arg...) \
163 dbg_printk(a_dbg_level, format, ## arg)
164 static const char *str_supported(int is_supported);
165 #else
166 #define vdbg_printk(a_dbg_level, format, arg...)
167 #endif
169 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
170 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
171 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
174 /****************************************************************************
175 * Driver-wide structs and misc. variables
178 struct ibm_struct;
180 struct tp_acpi_drv_struct {
181 const struct acpi_device_id *hid;
182 struct acpi_driver *driver;
184 void (*notify) (struct ibm_struct *, u32);
185 acpi_handle *handle;
186 u32 type;
187 struct acpi_device *device;
190 struct ibm_struct {
191 char *name;
193 int (*read) (char *);
194 int (*write) (char *);
195 void (*exit) (void);
196 void (*resume) (void);
197 void (*suspend) (pm_message_t state);
199 struct list_head all_drivers;
201 struct tp_acpi_drv_struct *acpi;
203 struct {
204 u8 acpi_driver_registered:1;
205 u8 acpi_notify_installed:1;
206 u8 proc_created:1;
207 u8 init_called:1;
208 u8 experimental:1;
209 } flags;
212 struct ibm_init_struct {
213 char param[32];
215 int (*init) (struct ibm_init_struct *);
216 struct ibm_struct *data;
219 static struct {
220 #ifdef CONFIG_THINKPAD_ACPI_BAY
221 u32 bay_status:1;
222 u32 bay_eject:1;
223 u32 bay_status2:1;
224 u32 bay_eject2:1;
225 #endif
226 u32 bluetooth:1;
227 u32 hotkey:1;
228 u32 hotkey_mask:1;
229 u32 hotkey_wlsw:1;
230 u32 hotkey_tablet:1;
231 u32 light:1;
232 u32 light_status:1;
233 u32 bright_16levels:1;
234 u32 bright_acpimode:1;
235 u32 wan:1;
236 u32 fan_ctrl_status_undef:1;
237 u32 input_device_registered:1;
238 u32 platform_drv_registered:1;
239 u32 platform_drv_attrs_registered:1;
240 u32 sensors_pdrv_registered:1;
241 u32 sensors_pdrv_attrs_registered:1;
242 u32 sensors_pdev_attrs_registered:1;
243 u32 hotkey_poll_active:1;
244 } tp_features;
246 static struct {
247 u16 hotkey_mask_ff:1;
248 u16 bright_cmos_ec_unsync:1;
249 } tp_warned;
251 struct thinkpad_id_data {
252 unsigned int vendor; /* ThinkPad vendor:
253 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
255 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
256 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
258 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
259 u16 ec_model;
261 char *model_str; /* ThinkPad T43 */
262 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
264 static struct thinkpad_id_data thinkpad_id;
266 static enum {
267 TPACPI_LIFE_INIT = 0,
268 TPACPI_LIFE_RUNNING,
269 TPACPI_LIFE_EXITING,
270 } tpacpi_lifecycle;
272 static int experimental;
273 static u32 dbg_level;
275 /* Special LED class that can defer work */
276 struct tpacpi_led_classdev {
277 struct led_classdev led_classdev;
278 struct work_struct work;
279 enum led_brightness new_brightness;
280 unsigned int led;
283 /****************************************************************************
284 ****************************************************************************
286 * ACPI Helpers and device model
288 ****************************************************************************
289 ****************************************************************************/
291 /*************************************************************************
292 * ACPI basic handles
295 static acpi_handle root_handle;
297 #define TPACPI_HANDLE(object, parent, paths...) \
298 static acpi_handle object##_handle; \
299 static acpi_handle *object##_parent = &parent##_handle; \
300 static char *object##_path; \
301 static char *object##_paths[] = { paths }
303 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
304 "\\_SB.PCI.ISA.EC", /* 570 */
305 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
306 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
307 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
308 "\\_SB.PCI0.ICH3.EC0", /* R31 */
309 "\\_SB.PCI0.LPC.EC", /* all others */
312 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
313 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
315 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
316 /* T4x, X31, X40 */
317 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
318 "\\CMS", /* R40, R40e */
319 ); /* all others */
321 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
322 "^HKEY", /* R30, R31 */
323 "HKEY", /* all others */
324 ); /* 570 */
326 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
327 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
328 "\\_SB.PCI0.VID0", /* 770e */
329 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
330 "\\_SB.PCI0.AGP.VID", /* all others */
331 ); /* R30, R31 */
334 /*************************************************************************
335 * ACPI helpers
338 static int acpi_evalf(acpi_handle handle,
339 void *res, char *method, char *fmt, ...)
341 char *fmt0 = fmt;
342 struct acpi_object_list params;
343 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
344 struct acpi_buffer result, *resultp;
345 union acpi_object out_obj;
346 acpi_status status;
347 va_list ap;
348 char res_type;
349 int success;
350 int quiet;
352 if (!*fmt) {
353 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
354 return 0;
357 if (*fmt == 'q') {
358 quiet = 1;
359 fmt++;
360 } else
361 quiet = 0;
363 res_type = *(fmt++);
365 params.count = 0;
366 params.pointer = &in_objs[0];
368 va_start(ap, fmt);
369 while (*fmt) {
370 char c = *(fmt++);
371 switch (c) {
372 case 'd': /* int */
373 in_objs[params.count].integer.value = va_arg(ap, int);
374 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
375 break;
376 /* add more types as needed */
377 default:
378 printk(TPACPI_ERR "acpi_evalf() called "
379 "with invalid format character '%c'\n", c);
380 return 0;
383 va_end(ap);
385 if (res_type != 'v') {
386 result.length = sizeof(out_obj);
387 result.pointer = &out_obj;
388 resultp = &result;
389 } else
390 resultp = NULL;
392 status = acpi_evaluate_object(handle, method, &params, resultp);
394 switch (res_type) {
395 case 'd': /* int */
396 if (res)
397 *(int *)res = out_obj.integer.value;
398 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
399 break;
400 case 'v': /* void */
401 success = status == AE_OK;
402 break;
403 /* add more types as needed */
404 default:
405 printk(TPACPI_ERR "acpi_evalf() called "
406 "with invalid format character '%c'\n", res_type);
407 return 0;
410 if (!success && !quiet)
411 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
412 method, fmt0, status);
414 return success;
417 static int acpi_ec_read(int i, u8 *p)
419 int v;
421 if (ecrd_handle) {
422 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
423 return 0;
424 *p = v;
425 } else {
426 if (ec_read(i, p) < 0)
427 return 0;
430 return 1;
433 static int acpi_ec_write(int i, u8 v)
435 if (ecwr_handle) {
436 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
437 return 0;
438 } else {
439 if (ec_write(i, v) < 0)
440 return 0;
443 return 1;
446 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
447 static int _sta(acpi_handle handle)
449 int status;
451 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
452 status = 0;
454 return status;
456 #endif
458 static int issue_thinkpad_cmos_command(int cmos_cmd)
460 if (!cmos_handle)
461 return -ENXIO;
463 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
464 return -EIO;
466 return 0;
469 /*************************************************************************
470 * ACPI device model
473 #define TPACPI_ACPIHANDLE_INIT(object) \
474 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
475 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
477 static void drv_acpi_handle_init(char *name,
478 acpi_handle *handle, acpi_handle parent,
479 char **paths, int num_paths, char **path)
481 int i;
482 acpi_status status;
484 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
485 name);
487 for (i = 0; i < num_paths; i++) {
488 status = acpi_get_handle(parent, paths[i], handle);
489 if (ACPI_SUCCESS(status)) {
490 *path = paths[i];
491 dbg_printk(TPACPI_DBG_INIT,
492 "Found ACPI handle %s for %s\n",
493 *path, name);
494 return;
498 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
499 name);
500 *handle = NULL;
503 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
505 struct ibm_struct *ibm = data;
507 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
508 return;
510 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
511 return;
513 ibm->acpi->notify(ibm, event);
516 static int __init setup_acpi_notify(struct ibm_struct *ibm)
518 acpi_status status;
519 int rc;
521 BUG_ON(!ibm->acpi);
523 if (!*ibm->acpi->handle)
524 return 0;
526 vdbg_printk(TPACPI_DBG_INIT,
527 "setting up ACPI notify for %s\n", ibm->name);
529 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
530 if (rc < 0) {
531 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
532 ibm->name, rc);
533 return -ENODEV;
536 acpi_driver_data(ibm->acpi->device) = ibm;
537 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
538 TPACPI_ACPI_EVENT_PREFIX,
539 ibm->name);
541 status = acpi_install_notify_handler(*ibm->acpi->handle,
542 ibm->acpi->type, dispatch_acpi_notify, ibm);
543 if (ACPI_FAILURE(status)) {
544 if (status == AE_ALREADY_EXISTS) {
545 printk(TPACPI_NOTICE
546 "another device driver is already "
547 "handling %s events\n", ibm->name);
548 } else {
549 printk(TPACPI_ERR
550 "acpi_install_notify_handler(%s) failed: %d\n",
551 ibm->name, status);
553 return -ENODEV;
555 ibm->flags.acpi_notify_installed = 1;
556 return 0;
559 static int __init tpacpi_device_add(struct acpi_device *device)
561 return 0;
564 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
566 int rc;
568 dbg_printk(TPACPI_DBG_INIT,
569 "registering %s as an ACPI driver\n", ibm->name);
571 BUG_ON(!ibm->acpi);
573 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
574 if (!ibm->acpi->driver) {
575 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
576 return -ENOMEM;
579 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
580 ibm->acpi->driver->ids = ibm->acpi->hid;
582 ibm->acpi->driver->ops.add = &tpacpi_device_add;
584 rc = acpi_bus_register_driver(ibm->acpi->driver);
585 if (rc < 0) {
586 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
587 ibm->name, rc);
588 kfree(ibm->acpi->driver);
589 ibm->acpi->driver = NULL;
590 } else if (!rc)
591 ibm->flags.acpi_driver_registered = 1;
593 return rc;
597 /****************************************************************************
598 ****************************************************************************
600 * Procfs Helpers
602 ****************************************************************************
603 ****************************************************************************/
605 static int dispatch_procfs_read(char *page, char **start, off_t off,
606 int count, int *eof, void *data)
608 struct ibm_struct *ibm = data;
609 int len;
611 if (!ibm || !ibm->read)
612 return -EINVAL;
614 len = ibm->read(page);
615 if (len < 0)
616 return len;
618 if (len <= off + count)
619 *eof = 1;
620 *start = page + off;
621 len -= off;
622 if (len > count)
623 len = count;
624 if (len < 0)
625 len = 0;
627 return len;
630 static int dispatch_procfs_write(struct file *file,
631 const char __user *userbuf,
632 unsigned long count, void *data)
634 struct ibm_struct *ibm = data;
635 char *kernbuf;
636 int ret;
638 if (!ibm || !ibm->write)
639 return -EINVAL;
641 kernbuf = kmalloc(count + 2, GFP_KERNEL);
642 if (!kernbuf)
643 return -ENOMEM;
645 if (copy_from_user(kernbuf, userbuf, count)) {
646 kfree(kernbuf);
647 return -EFAULT;
650 kernbuf[count] = 0;
651 strcat(kernbuf, ",");
652 ret = ibm->write(kernbuf);
653 if (ret == 0)
654 ret = count;
656 kfree(kernbuf);
658 return ret;
661 static char *next_cmd(char **cmds)
663 char *start = *cmds;
664 char *end;
666 while ((end = strchr(start, ',')) && end == start)
667 start = end + 1;
669 if (!end)
670 return NULL;
672 *end = 0;
673 *cmds = end + 1;
674 return start;
678 /****************************************************************************
679 ****************************************************************************
681 * Device model: input, hwmon and platform
683 ****************************************************************************
684 ****************************************************************************/
686 static struct platform_device *tpacpi_pdev;
687 static struct platform_device *tpacpi_sensors_pdev;
688 static struct device *tpacpi_hwmon;
689 static struct input_dev *tpacpi_inputdev;
690 static struct mutex tpacpi_inputdev_send_mutex;
691 static LIST_HEAD(tpacpi_all_drivers);
693 static int tpacpi_suspend_handler(struct platform_device *pdev,
694 pm_message_t state)
696 struct ibm_struct *ibm, *itmp;
698 list_for_each_entry_safe(ibm, itmp,
699 &tpacpi_all_drivers,
700 all_drivers) {
701 if (ibm->suspend)
702 (ibm->suspend)(state);
705 return 0;
708 static int tpacpi_resume_handler(struct platform_device *pdev)
710 struct ibm_struct *ibm, *itmp;
712 list_for_each_entry_safe(ibm, itmp,
713 &tpacpi_all_drivers,
714 all_drivers) {
715 if (ibm->resume)
716 (ibm->resume)();
719 return 0;
722 static struct platform_driver tpacpi_pdriver = {
723 .driver = {
724 .name = TPACPI_DRVR_NAME,
725 .owner = THIS_MODULE,
727 .suspend = tpacpi_suspend_handler,
728 .resume = tpacpi_resume_handler,
731 static struct platform_driver tpacpi_hwmon_pdriver = {
732 .driver = {
733 .name = TPACPI_HWMON_DRVR_NAME,
734 .owner = THIS_MODULE,
738 /*************************************************************************
739 * sysfs support helpers
742 struct attribute_set {
743 unsigned int members, max_members;
744 struct attribute_group group;
747 struct attribute_set_obj {
748 struct attribute_set s;
749 struct attribute *a;
750 } __attribute__((packed));
752 static struct attribute_set *create_attr_set(unsigned int max_members,
753 const char *name)
755 struct attribute_set_obj *sobj;
757 if (max_members == 0)
758 return NULL;
760 /* Allocates space for implicit NULL at the end too */
761 sobj = kzalloc(sizeof(struct attribute_set_obj) +
762 max_members * sizeof(struct attribute *),
763 GFP_KERNEL);
764 if (!sobj)
765 return NULL;
766 sobj->s.max_members = max_members;
767 sobj->s.group.attrs = &sobj->a;
768 sobj->s.group.name = name;
770 return &sobj->s;
773 #define destroy_attr_set(_set) \
774 kfree(_set);
776 /* not multi-threaded safe, use it in a single thread per set */
777 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
779 if (!s || !attr)
780 return -EINVAL;
782 if (s->members >= s->max_members)
783 return -ENOMEM;
785 s->group.attrs[s->members] = attr;
786 s->members++;
788 return 0;
791 static int add_many_to_attr_set(struct attribute_set *s,
792 struct attribute **attr,
793 unsigned int count)
795 int i, res;
797 for (i = 0; i < count; i++) {
798 res = add_to_attr_set(s, attr[i]);
799 if (res)
800 return res;
803 return 0;
806 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
808 sysfs_remove_group(kobj, &s->group);
809 destroy_attr_set(s);
812 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
813 sysfs_create_group(_kobj, &_attr_set->group)
815 static int parse_strtoul(const char *buf,
816 unsigned long max, unsigned long *value)
818 char *endp;
820 while (*buf && isspace(*buf))
821 buf++;
822 *value = simple_strtoul(buf, &endp, 0);
823 while (*endp && isspace(*endp))
824 endp++;
825 if (*endp || *value > max)
826 return -EINVAL;
828 return 0;
831 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
833 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
834 union acpi_object *obj;
835 int rc;
837 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
838 obj = (union acpi_object *)buffer.pointer;
839 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
840 printk(TPACPI_ERR "Unknown _BCL data, "
841 "please report this to %s\n", TPACPI_MAIL);
842 rc = 0;
843 } else {
844 rc = obj->package.count;
846 } else {
847 return 0;
850 kfree(buffer.pointer);
851 return rc;
854 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
855 u32 lvl, void *context, void **rv)
857 char name[ACPI_PATH_SEGMENT_LENGTH];
858 struct acpi_buffer buffer = { sizeof(name), &name };
860 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
861 !strncmp("_BCL", name, sizeof(name) - 1)) {
862 BUG_ON(!rv || !*rv);
863 **(int **)rv = tpacpi_query_bcl_levels(handle);
864 return AE_CTRL_TERMINATE;
865 } else {
866 return AE_OK;
871 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
873 static int __init tpacpi_check_std_acpi_brightness_support(void)
875 int status;
876 int bcl_levels = 0;
877 void *bcl_ptr = &bcl_levels;
879 if (!vid_handle) {
880 TPACPI_ACPIHANDLE_INIT(vid);
882 if (!vid_handle)
883 return 0;
886 * Search for a _BCL method, and execute it. This is safe on all
887 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
888 * BIOS in ACPI backlight control mode. We do NOT have to care
889 * about calling the _BCL method in an enabled video device, any
890 * will do for our purposes.
893 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
894 tpacpi_acpi_walk_find_bcl, NULL,
895 &bcl_ptr);
897 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
898 tp_features.bright_acpimode = 1;
899 return (bcl_levels - 2);
902 return 0;
905 /*************************************************************************
906 * thinkpad-acpi driver attributes
909 /* interface_version --------------------------------------------------- */
910 static ssize_t tpacpi_driver_interface_version_show(
911 struct device_driver *drv,
912 char *buf)
914 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
917 static DRIVER_ATTR(interface_version, S_IRUGO,
918 tpacpi_driver_interface_version_show, NULL);
920 /* debug_level --------------------------------------------------------- */
921 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
922 char *buf)
924 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
927 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
928 const char *buf, size_t count)
930 unsigned long t;
932 if (parse_strtoul(buf, 0xffff, &t))
933 return -EINVAL;
935 dbg_level = t;
937 return count;
940 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
941 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
943 /* version ------------------------------------------------------------- */
944 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
945 char *buf)
947 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
948 TPACPI_DESC, TPACPI_VERSION);
951 static DRIVER_ATTR(version, S_IRUGO,
952 tpacpi_driver_version_show, NULL);
954 /* --------------------------------------------------------------------- */
956 static struct driver_attribute *tpacpi_driver_attributes[] = {
957 &driver_attr_debug_level, &driver_attr_version,
958 &driver_attr_interface_version,
961 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
963 int i, res;
965 i = 0;
966 res = 0;
967 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
968 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
969 i++;
972 return res;
975 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
977 int i;
979 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
980 driver_remove_file(drv, tpacpi_driver_attributes[i]);
983 /****************************************************************************
984 ****************************************************************************
986 * Subdrivers
988 ****************************************************************************
989 ****************************************************************************/
991 /*************************************************************************
992 * thinkpad-acpi init subdriver
995 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
997 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
998 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1000 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1001 (thinkpad_id.bios_version_str) ?
1002 thinkpad_id.bios_version_str : "unknown",
1003 (thinkpad_id.ec_version_str) ?
1004 thinkpad_id.ec_version_str : "unknown");
1006 if (thinkpad_id.vendor && thinkpad_id.model_str)
1007 printk(TPACPI_INFO "%s %s, model %s\n",
1008 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1009 "IBM" : ((thinkpad_id.vendor ==
1010 PCI_VENDOR_ID_LENOVO) ?
1011 "Lenovo" : "Unknown vendor"),
1012 thinkpad_id.model_str,
1013 (thinkpad_id.nummodel_str) ?
1014 thinkpad_id.nummodel_str : "unknown");
1016 return 0;
1019 static int thinkpad_acpi_driver_read(char *p)
1021 int len = 0;
1023 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1024 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1026 return len;
1029 static struct ibm_struct thinkpad_acpi_driver_data = {
1030 .name = "driver",
1031 .read = thinkpad_acpi_driver_read,
1034 /*************************************************************************
1035 * Hotkey subdriver
1038 enum { /* hot key scan codes (derived from ACPI DSDT) */
1039 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1040 TP_ACPI_HOTKEYSCAN_FNF2,
1041 TP_ACPI_HOTKEYSCAN_FNF3,
1042 TP_ACPI_HOTKEYSCAN_FNF4,
1043 TP_ACPI_HOTKEYSCAN_FNF5,
1044 TP_ACPI_HOTKEYSCAN_FNF6,
1045 TP_ACPI_HOTKEYSCAN_FNF7,
1046 TP_ACPI_HOTKEYSCAN_FNF8,
1047 TP_ACPI_HOTKEYSCAN_FNF9,
1048 TP_ACPI_HOTKEYSCAN_FNF10,
1049 TP_ACPI_HOTKEYSCAN_FNF11,
1050 TP_ACPI_HOTKEYSCAN_FNF12,
1051 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1052 TP_ACPI_HOTKEYSCAN_FNINSERT,
1053 TP_ACPI_HOTKEYSCAN_FNDELETE,
1054 TP_ACPI_HOTKEYSCAN_FNHOME,
1055 TP_ACPI_HOTKEYSCAN_FNEND,
1056 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1057 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1058 TP_ACPI_HOTKEYSCAN_FNSPACE,
1059 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1060 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1061 TP_ACPI_HOTKEYSCAN_MUTE,
1062 TP_ACPI_HOTKEYSCAN_THINKPAD,
1065 enum { /* Keys available through NVRAM polling */
1066 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1067 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1070 enum { /* Positions of some of the keys in hotkey masks */
1071 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1072 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1073 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1074 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1075 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1076 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1077 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1078 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1079 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1080 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1081 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1084 enum { /* NVRAM to ACPI HKEY group map */
1085 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1086 TP_ACPI_HKEY_ZOOM_MASK |
1087 TP_ACPI_HKEY_DISPSWTCH_MASK |
1088 TP_ACPI_HKEY_HIBERNATE_MASK,
1089 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1090 TP_ACPI_HKEY_BRGHTDWN_MASK,
1091 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1092 TP_ACPI_HKEY_VOLDWN_MASK |
1093 TP_ACPI_HKEY_MUTE_MASK,
1096 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1097 struct tp_nvram_state {
1098 u16 thinkpad_toggle:1;
1099 u16 zoom_toggle:1;
1100 u16 display_toggle:1;
1101 u16 thinklight_toggle:1;
1102 u16 hibernate_toggle:1;
1103 u16 displayexp_toggle:1;
1104 u16 display_state:1;
1105 u16 brightness_toggle:1;
1106 u16 volume_toggle:1;
1107 u16 mute:1;
1109 u8 brightness_level;
1110 u8 volume_level;
1113 static struct task_struct *tpacpi_hotkey_task;
1114 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1115 static int hotkey_poll_freq = 10; /* Hz */
1116 static struct mutex hotkey_thread_mutex;
1117 static struct mutex hotkey_thread_data_mutex;
1118 static unsigned int hotkey_config_change;
1120 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1122 #define hotkey_source_mask 0U
1124 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1126 static struct mutex hotkey_mutex;
1128 static enum { /* Reasons for waking up */
1129 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1130 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1131 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1132 } hotkey_wakeup_reason;
1134 static int hotkey_autosleep_ack;
1136 static int hotkey_orig_status;
1137 static u32 hotkey_orig_mask;
1138 static u32 hotkey_all_mask;
1139 static u32 hotkey_reserved_mask;
1140 static u32 hotkey_mask;
1142 static unsigned int hotkey_report_mode;
1144 static u16 *hotkey_keycode_map;
1146 static struct attribute_set *hotkey_dev_attributes;
1148 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1149 #define HOTKEY_CONFIG_CRITICAL_START \
1150 do { \
1151 mutex_lock(&hotkey_thread_data_mutex); \
1152 hotkey_config_change++; \
1153 } while (0);
1154 #define HOTKEY_CONFIG_CRITICAL_END \
1155 mutex_unlock(&hotkey_thread_data_mutex);
1156 #else
1157 #define HOTKEY_CONFIG_CRITICAL_START
1158 #define HOTKEY_CONFIG_CRITICAL_END
1159 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1161 /* HKEY.MHKG() return bits */
1162 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1164 static int hotkey_get_wlsw(int *status)
1166 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1167 return -EIO;
1168 return 0;
1171 static int hotkey_get_tablet_mode(int *status)
1173 int s;
1175 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1176 return -EIO;
1178 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1179 return 0;
1183 * Call with hotkey_mutex held
1185 static int hotkey_mask_get(void)
1187 u32 m = 0;
1189 if (tp_features.hotkey_mask) {
1190 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1191 return -EIO;
1193 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1195 return 0;
1199 * Call with hotkey_mutex held
1201 static int hotkey_mask_set(u32 mask)
1203 int i;
1204 int rc = 0;
1206 if (tp_features.hotkey_mask) {
1207 if (!tp_warned.hotkey_mask_ff &&
1208 (mask == 0xffff || mask == 0xffffff ||
1209 mask == 0xffffffff)) {
1210 tp_warned.hotkey_mask_ff = 1;
1211 printk(TPACPI_NOTICE
1212 "setting the hotkey mask to 0x%08x is likely "
1213 "not the best way to go about it\n", mask);
1214 printk(TPACPI_NOTICE
1215 "please consider using the driver defaults, "
1216 "and refer to up-to-date thinkpad-acpi "
1217 "documentation\n");
1220 HOTKEY_CONFIG_CRITICAL_START
1221 for (i = 0; i < 32; i++) {
1222 u32 m = 1 << i;
1223 /* enable in firmware mask only keys not in NVRAM
1224 * mode, but enable the key in the cached hotkey_mask
1225 * regardless of mode, or the key will end up
1226 * disabled by hotkey_mask_get() */
1227 if (!acpi_evalf(hkey_handle,
1228 NULL, "MHKM", "vdd", i + 1,
1229 !!((mask & ~hotkey_source_mask) & m))) {
1230 rc = -EIO;
1231 break;
1232 } else {
1233 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1236 HOTKEY_CONFIG_CRITICAL_END
1238 /* hotkey_mask_get must be called unconditionally below */
1239 if (!hotkey_mask_get() && !rc &&
1240 (hotkey_mask & ~hotkey_source_mask) !=
1241 (mask & ~hotkey_source_mask)) {
1242 printk(TPACPI_NOTICE
1243 "requested hot key mask 0x%08x, but "
1244 "firmware forced it to 0x%08x\n",
1245 mask, hotkey_mask);
1247 } else {
1248 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1249 HOTKEY_CONFIG_CRITICAL_START
1250 hotkey_mask = mask & hotkey_source_mask;
1251 HOTKEY_CONFIG_CRITICAL_END
1252 hotkey_mask_get();
1253 if (hotkey_mask != mask) {
1254 printk(TPACPI_NOTICE
1255 "requested hot key mask 0x%08x, "
1256 "forced to 0x%08x (NVRAM poll mask is "
1257 "0x%08x): no firmware mask support\n",
1258 mask, hotkey_mask, hotkey_source_mask);
1260 #else
1261 hotkey_mask_get();
1262 rc = -ENXIO;
1263 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1266 return rc;
1269 static int hotkey_status_get(int *status)
1271 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1272 return -EIO;
1274 return 0;
1277 static int hotkey_status_set(int status)
1279 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1280 return -EIO;
1282 return 0;
1285 static void tpacpi_input_send_radiosw(void)
1287 int wlsw;
1289 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1290 mutex_lock(&tpacpi_inputdev_send_mutex);
1292 input_report_switch(tpacpi_inputdev,
1293 SW_RADIO, !!wlsw);
1294 input_sync(tpacpi_inputdev);
1296 mutex_unlock(&tpacpi_inputdev_send_mutex);
1300 static void tpacpi_input_send_tabletsw(void)
1302 int state;
1304 if (tp_features.hotkey_tablet &&
1305 !hotkey_get_tablet_mode(&state)) {
1306 mutex_lock(&tpacpi_inputdev_send_mutex);
1308 input_report_switch(tpacpi_inputdev,
1309 SW_TABLET_MODE, !!state);
1310 input_sync(tpacpi_inputdev);
1312 mutex_unlock(&tpacpi_inputdev_send_mutex);
1316 static void tpacpi_input_send_key(unsigned int scancode)
1318 unsigned int keycode;
1320 keycode = hotkey_keycode_map[scancode];
1322 if (keycode != KEY_RESERVED) {
1323 mutex_lock(&tpacpi_inputdev_send_mutex);
1325 input_report_key(tpacpi_inputdev, keycode, 1);
1326 if (keycode == KEY_UNKNOWN)
1327 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1328 scancode);
1329 input_sync(tpacpi_inputdev);
1331 input_report_key(tpacpi_inputdev, keycode, 0);
1332 if (keycode == KEY_UNKNOWN)
1333 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1334 scancode);
1335 input_sync(tpacpi_inputdev);
1337 mutex_unlock(&tpacpi_inputdev_send_mutex);
1341 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1342 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1344 static void tpacpi_hotkey_send_key(unsigned int scancode)
1346 tpacpi_input_send_key(scancode);
1347 if (hotkey_report_mode < 2) {
1348 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1349 0x80, 0x1001 + scancode);
1353 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1355 u8 d;
1357 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1358 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1359 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1360 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1361 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1362 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1364 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1365 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1366 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1368 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1369 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1370 n->displayexp_toggle =
1371 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1373 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1374 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1375 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1376 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1377 n->brightness_toggle =
1378 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1380 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1381 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1382 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1383 >> TP_NVRAM_POS_LEVEL_VOLUME;
1384 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1385 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1389 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1390 do { \
1391 if ((mask & (1 << __scancode)) && \
1392 oldn->__member != newn->__member) \
1393 tpacpi_hotkey_send_key(__scancode); \
1394 } while (0)
1396 #define TPACPI_MAY_SEND_KEY(__scancode) \
1397 do { if (mask & (1 << __scancode)) \
1398 tpacpi_hotkey_send_key(__scancode); } while (0)
1400 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1401 struct tp_nvram_state *newn,
1402 u32 mask)
1404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1413 /* handle volume */
1414 if (oldn->volume_toggle != newn->volume_toggle) {
1415 if (oldn->mute != newn->mute) {
1416 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1418 if (oldn->volume_level > newn->volume_level) {
1419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1420 } else if (oldn->volume_level < newn->volume_level) {
1421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1422 } else if (oldn->mute == newn->mute) {
1423 /* repeated key presses that didn't change state */
1424 if (newn->mute) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1426 } else if (newn->volume_level != 0) {
1427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1428 } else {
1429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1434 /* handle brightness */
1435 if (oldn->brightness_toggle != newn->brightness_toggle) {
1436 if (oldn->brightness_level < newn->brightness_level) {
1437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1438 } else if (oldn->brightness_level > newn->brightness_level) {
1439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1440 } else {
1441 /* repeated key presses that didn't change state */
1442 if (newn->brightness_level != 0) {
1443 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1444 } else {
1445 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1451 #undef TPACPI_COMPARE_KEY
1452 #undef TPACPI_MAY_SEND_KEY
1454 static int hotkey_kthread(void *data)
1456 struct tp_nvram_state s[2];
1457 u32 mask;
1458 unsigned int si, so;
1459 unsigned long t;
1460 unsigned int change_detector, must_reset;
1462 mutex_lock(&hotkey_thread_mutex);
1464 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1465 goto exit;
1467 set_freezable();
1469 so = 0;
1470 si = 1;
1471 t = 0;
1473 /* Initial state for compares */
1474 mutex_lock(&hotkey_thread_data_mutex);
1475 change_detector = hotkey_config_change;
1476 mask = hotkey_source_mask & hotkey_mask;
1477 mutex_unlock(&hotkey_thread_data_mutex);
1478 hotkey_read_nvram(&s[so], mask);
1480 while (!kthread_should_stop() && hotkey_poll_freq) {
1481 if (t == 0)
1482 t = 1000/hotkey_poll_freq;
1483 t = msleep_interruptible(t);
1484 if (unlikely(kthread_should_stop()))
1485 break;
1486 must_reset = try_to_freeze();
1487 if (t > 0 && !must_reset)
1488 continue;
1490 mutex_lock(&hotkey_thread_data_mutex);
1491 if (must_reset || hotkey_config_change != change_detector) {
1492 /* forget old state on thaw or config change */
1493 si = so;
1494 t = 0;
1495 change_detector = hotkey_config_change;
1497 mask = hotkey_source_mask & hotkey_mask;
1498 mutex_unlock(&hotkey_thread_data_mutex);
1500 if (likely(mask)) {
1501 hotkey_read_nvram(&s[si], mask);
1502 if (likely(si != so)) {
1503 hotkey_compare_and_issue_event(&s[so], &s[si],
1504 mask);
1508 so = si;
1509 si ^= 1;
1512 exit:
1513 mutex_unlock(&hotkey_thread_mutex);
1514 return 0;
1517 static void hotkey_poll_stop_sync(void)
1519 if (tpacpi_hotkey_task) {
1520 if (frozen(tpacpi_hotkey_task) ||
1521 freezing(tpacpi_hotkey_task))
1522 thaw_process(tpacpi_hotkey_task);
1524 kthread_stop(tpacpi_hotkey_task);
1525 tpacpi_hotkey_task = NULL;
1526 mutex_lock(&hotkey_thread_mutex);
1527 /* at this point, the thread did exit */
1528 mutex_unlock(&hotkey_thread_mutex);
1532 /* call with hotkey_mutex held */
1533 static void hotkey_poll_setup(int may_warn)
1535 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1536 hotkey_poll_freq > 0 &&
1537 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1538 if (!tpacpi_hotkey_task) {
1539 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1540 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1541 if (IS_ERR(tpacpi_hotkey_task)) {
1542 tpacpi_hotkey_task = NULL;
1543 printk(TPACPI_ERR
1544 "could not create kernel thread "
1545 "for hotkey polling\n");
1548 } else {
1549 hotkey_poll_stop_sync();
1550 if (may_warn &&
1551 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1552 printk(TPACPI_NOTICE
1553 "hot keys 0x%08x require polling, "
1554 "which is currently disabled\n",
1555 hotkey_source_mask);
1560 static void hotkey_poll_setup_safe(int may_warn)
1562 mutex_lock(&hotkey_mutex);
1563 hotkey_poll_setup(may_warn);
1564 mutex_unlock(&hotkey_mutex);
1567 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1569 static void hotkey_poll_setup_safe(int __unused)
1573 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1575 static int hotkey_inputdev_open(struct input_dev *dev)
1577 switch (tpacpi_lifecycle) {
1578 case TPACPI_LIFE_INIT:
1580 * hotkey_init will call hotkey_poll_setup_safe
1581 * at the appropriate moment
1583 return 0;
1584 case TPACPI_LIFE_EXITING:
1585 return -EBUSY;
1586 case TPACPI_LIFE_RUNNING:
1587 hotkey_poll_setup_safe(0);
1588 return 0;
1591 /* Should only happen if tpacpi_lifecycle is corrupt */
1592 BUG();
1593 return -EBUSY;
1596 static void hotkey_inputdev_close(struct input_dev *dev)
1598 /* disable hotkey polling when possible */
1599 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1600 hotkey_poll_setup_safe(0);
1603 /* sysfs hotkey enable ------------------------------------------------- */
1604 static ssize_t hotkey_enable_show(struct device *dev,
1605 struct device_attribute *attr,
1606 char *buf)
1608 int res, status;
1610 res = hotkey_status_get(&status);
1611 if (res)
1612 return res;
1614 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1617 static ssize_t hotkey_enable_store(struct device *dev,
1618 struct device_attribute *attr,
1619 const char *buf, size_t count)
1621 unsigned long t;
1622 int res;
1624 if (parse_strtoul(buf, 1, &t))
1625 return -EINVAL;
1627 res = hotkey_status_set(t);
1629 return (res) ? res : count;
1632 static struct device_attribute dev_attr_hotkey_enable =
1633 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1634 hotkey_enable_show, hotkey_enable_store);
1636 /* sysfs hotkey mask --------------------------------------------------- */
1637 static ssize_t hotkey_mask_show(struct device *dev,
1638 struct device_attribute *attr,
1639 char *buf)
1641 int res;
1643 if (mutex_lock_interruptible(&hotkey_mutex))
1644 return -ERESTARTSYS;
1645 res = hotkey_mask_get();
1646 mutex_unlock(&hotkey_mutex);
1648 return (res)?
1649 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1652 static ssize_t hotkey_mask_store(struct device *dev,
1653 struct device_attribute *attr,
1654 const char *buf, size_t count)
1656 unsigned long t;
1657 int res;
1659 if (parse_strtoul(buf, 0xffffffffUL, &t))
1660 return -EINVAL;
1662 if (mutex_lock_interruptible(&hotkey_mutex))
1663 return -ERESTARTSYS;
1665 res = hotkey_mask_set(t);
1667 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1668 hotkey_poll_setup(1);
1669 #endif
1671 mutex_unlock(&hotkey_mutex);
1673 return (res) ? res : count;
1676 static struct device_attribute dev_attr_hotkey_mask =
1677 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1678 hotkey_mask_show, hotkey_mask_store);
1680 /* sysfs hotkey bios_enabled ------------------------------------------- */
1681 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1682 struct device_attribute *attr,
1683 char *buf)
1685 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1688 static struct device_attribute dev_attr_hotkey_bios_enabled =
1689 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1691 /* sysfs hotkey bios_mask ---------------------------------------------- */
1692 static ssize_t hotkey_bios_mask_show(struct device *dev,
1693 struct device_attribute *attr,
1694 char *buf)
1696 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1699 static struct device_attribute dev_attr_hotkey_bios_mask =
1700 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1702 /* sysfs hotkey all_mask ----------------------------------------------- */
1703 static ssize_t hotkey_all_mask_show(struct device *dev,
1704 struct device_attribute *attr,
1705 char *buf)
1707 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1708 hotkey_all_mask | hotkey_source_mask);
1711 static struct device_attribute dev_attr_hotkey_all_mask =
1712 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1714 /* sysfs hotkey recommended_mask --------------------------------------- */
1715 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1716 struct device_attribute *attr,
1717 char *buf)
1719 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1720 (hotkey_all_mask | hotkey_source_mask)
1721 & ~hotkey_reserved_mask);
1724 static struct device_attribute dev_attr_hotkey_recommended_mask =
1725 __ATTR(hotkey_recommended_mask, S_IRUGO,
1726 hotkey_recommended_mask_show, NULL);
1728 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1730 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1731 static ssize_t hotkey_source_mask_show(struct device *dev,
1732 struct device_attribute *attr,
1733 char *buf)
1735 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1738 static ssize_t hotkey_source_mask_store(struct device *dev,
1739 struct device_attribute *attr,
1740 const char *buf, size_t count)
1742 unsigned long t;
1744 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1745 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1746 return -EINVAL;
1748 if (mutex_lock_interruptible(&hotkey_mutex))
1749 return -ERESTARTSYS;
1751 HOTKEY_CONFIG_CRITICAL_START
1752 hotkey_source_mask = t;
1753 HOTKEY_CONFIG_CRITICAL_END
1755 hotkey_poll_setup(1);
1757 mutex_unlock(&hotkey_mutex);
1759 return count;
1762 static struct device_attribute dev_attr_hotkey_source_mask =
1763 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1764 hotkey_source_mask_show, hotkey_source_mask_store);
1766 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1767 static ssize_t hotkey_poll_freq_show(struct device *dev,
1768 struct device_attribute *attr,
1769 char *buf)
1771 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1774 static ssize_t hotkey_poll_freq_store(struct device *dev,
1775 struct device_attribute *attr,
1776 const char *buf, size_t count)
1778 unsigned long t;
1780 if (parse_strtoul(buf, 25, &t))
1781 return -EINVAL;
1783 if (mutex_lock_interruptible(&hotkey_mutex))
1784 return -ERESTARTSYS;
1786 hotkey_poll_freq = t;
1788 hotkey_poll_setup(1);
1789 mutex_unlock(&hotkey_mutex);
1791 return count;
1794 static struct device_attribute dev_attr_hotkey_poll_freq =
1795 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1796 hotkey_poll_freq_show, hotkey_poll_freq_store);
1798 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1800 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1801 static ssize_t hotkey_radio_sw_show(struct device *dev,
1802 struct device_attribute *attr,
1803 char *buf)
1805 int res, s;
1806 res = hotkey_get_wlsw(&s);
1807 if (res < 0)
1808 return res;
1810 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1813 static struct device_attribute dev_attr_hotkey_radio_sw =
1814 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1816 static void hotkey_radio_sw_notify_change(void)
1818 if (tp_features.hotkey_wlsw)
1819 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1820 "hotkey_radio_sw");
1823 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1824 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1825 struct device_attribute *attr,
1826 char *buf)
1828 int res, s;
1829 res = hotkey_get_tablet_mode(&s);
1830 if (res < 0)
1831 return res;
1833 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1836 static struct device_attribute dev_attr_hotkey_tablet_mode =
1837 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1839 static void hotkey_tablet_mode_notify_change(void)
1841 if (tp_features.hotkey_tablet)
1842 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1843 "hotkey_tablet_mode");
1846 /* sysfs hotkey report_mode -------------------------------------------- */
1847 static ssize_t hotkey_report_mode_show(struct device *dev,
1848 struct device_attribute *attr,
1849 char *buf)
1851 return snprintf(buf, PAGE_SIZE, "%d\n",
1852 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1855 static struct device_attribute dev_attr_hotkey_report_mode =
1856 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1858 /* sysfs wakeup reason (pollable) -------------------------------------- */
1859 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1860 struct device_attribute *attr,
1861 char *buf)
1863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1866 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1867 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1869 static void hotkey_wakeup_reason_notify_change(void)
1871 if (tp_features.hotkey_mask)
1872 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1873 "wakeup_reason");
1876 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1877 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1878 struct device_attribute *attr,
1879 char *buf)
1881 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1884 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1885 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1886 hotkey_wakeup_hotunplug_complete_show, NULL);
1888 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1890 if (tp_features.hotkey_mask)
1891 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1892 "wakeup_hotunplug_complete");
1895 /* --------------------------------------------------------------------- */
1897 static struct attribute *hotkey_attributes[] __initdata = {
1898 &dev_attr_hotkey_enable.attr,
1899 &dev_attr_hotkey_bios_enabled.attr,
1900 &dev_attr_hotkey_report_mode.attr,
1901 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1902 &dev_attr_hotkey_mask.attr,
1903 &dev_attr_hotkey_all_mask.attr,
1904 &dev_attr_hotkey_recommended_mask.attr,
1905 &dev_attr_hotkey_source_mask.attr,
1906 &dev_attr_hotkey_poll_freq.attr,
1907 #endif
1910 static struct attribute *hotkey_mask_attributes[] __initdata = {
1911 &dev_attr_hotkey_bios_mask.attr,
1912 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1913 &dev_attr_hotkey_mask.attr,
1914 &dev_attr_hotkey_all_mask.attr,
1915 &dev_attr_hotkey_recommended_mask.attr,
1916 #endif
1917 &dev_attr_hotkey_wakeup_reason.attr,
1918 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1921 static int __init hotkey_init(struct ibm_init_struct *iibm)
1923 /* Requirements for changing the default keymaps:
1925 * 1. Many of the keys are mapped to KEY_RESERVED for very
1926 * good reasons. Do not change them unless you have deep
1927 * knowledge on the IBM and Lenovo ThinkPad firmware for
1928 * the various ThinkPad models. The driver behaves
1929 * differently for KEY_RESERVED: such keys have their
1930 * hot key mask *unset* in mask_recommended, and also
1931 * in the initial hot key mask programmed into the
1932 * firmware at driver load time, which means the firm-
1933 * ware may react very differently if you change them to
1934 * something else;
1936 * 2. You must be subscribed to the linux-thinkpad and
1937 * ibm-acpi-devel mailing lists, and you should read the
1938 * list archives since 2007 if you want to change the
1939 * keymaps. This requirement exists so that you will
1940 * know the past history of problems with the thinkpad-
1941 * acpi driver keymaps, and also that you will be
1942 * listening to any bug reports;
1944 * 3. Do not send thinkpad-acpi specific patches directly to
1945 * for merging, *ever*. Send them to the linux-acpi
1946 * mailinglist for comments. Merging is to be done only
1947 * through acpi-test and the ACPI maintainer.
1949 * If the above is too much to ask, don't change the keymap.
1950 * Ask the thinkpad-acpi maintainer to do it, instead.
1952 static u16 ibm_keycode_map[] __initdata = {
1953 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1954 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1955 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1956 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1958 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1959 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1960 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1961 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1963 /* brightness: firmware always reacts to them, unless
1964 * X.org did some tricks in the radeon BIOS scratch
1965 * registers of *some* models */
1966 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1967 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1969 /* Thinklight: firmware always react to it */
1970 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1972 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1973 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1975 /* Volume: firmware always react to it and reprograms
1976 * the built-in *extra* mixer. Never map it to control
1977 * another mixer by default. */
1978 KEY_RESERVED, /* 0x14: VOLUME UP */
1979 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1980 KEY_RESERVED, /* 0x16: MUTE */
1982 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1984 /* (assignments unknown, please report if found) */
1985 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1986 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1988 static u16 lenovo_keycode_map[] __initdata = {
1989 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1990 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1991 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1992 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1994 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1995 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1996 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1997 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1999 /* These either have to go through ACPI video, or
2000 * act like in the IBM ThinkPads, so don't ever
2001 * enable them by default */
2002 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2003 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2005 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2007 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2008 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2010 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2011 * react to it and reprograms the built-in *extra* mixer.
2012 * Never map it to control another mixer by default.
2014 * T60?, T61, R60?, R61: firmware and EC tries to send
2015 * these over the regular keyboard, so these are no-ops,
2016 * but there are still weird bugs re. MUTE, so do not
2017 * change unless you get test reports from all Lenovo
2018 * models. May cause the BIOS to interfere with the
2019 * HDA mixer.
2021 KEY_RESERVED, /* 0x14: VOLUME UP */
2022 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2023 KEY_RESERVED, /* 0x16: MUTE */
2025 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2027 /* (assignments unknown, please report if found) */
2028 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2029 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2032 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2033 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2034 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2036 int res, i;
2037 int status;
2038 int hkeyv;
2040 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2042 BUG_ON(!tpacpi_inputdev);
2043 BUG_ON(tpacpi_inputdev->open != NULL ||
2044 tpacpi_inputdev->close != NULL);
2046 TPACPI_ACPIHANDLE_INIT(hkey);
2047 mutex_init(&hotkey_mutex);
2049 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2050 mutex_init(&hotkey_thread_mutex);
2051 mutex_init(&hotkey_thread_data_mutex);
2052 #endif
2054 /* hotkey not supported on 570 */
2055 tp_features.hotkey = hkey_handle != NULL;
2057 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2058 str_supported(tp_features.hotkey));
2060 if (tp_features.hotkey) {
2061 hotkey_dev_attributes = create_attr_set(13, NULL);
2062 if (!hotkey_dev_attributes)
2063 return -ENOMEM;
2064 res = add_many_to_attr_set(hotkey_dev_attributes,
2065 hotkey_attributes,
2066 ARRAY_SIZE(hotkey_attributes));
2067 if (res)
2068 return res;
2070 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2071 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2072 for HKEY interface version 0x100 */
2073 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2074 if ((hkeyv >> 8) != 1) {
2075 printk(TPACPI_ERR "unknown version of the "
2076 "HKEY interface: 0x%x\n", hkeyv);
2077 printk(TPACPI_ERR "please report this to %s\n",
2078 TPACPI_MAIL);
2079 } else {
2081 * MHKV 0x100 in A31, R40, R40e,
2082 * T4x, X31, and later
2084 tp_features.hotkey_mask = 1;
2088 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2089 str_supported(tp_features.hotkey_mask));
2091 if (tp_features.hotkey_mask) {
2092 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2093 "MHKA", "qd")) {
2094 printk(TPACPI_ERR
2095 "missing MHKA handler, "
2096 "please report this to %s\n",
2097 TPACPI_MAIL);
2098 /* FN+F12, FN+F4, FN+F3 */
2099 hotkey_all_mask = 0x080cU;
2103 /* hotkey_source_mask *must* be zero for
2104 * the first hotkey_mask_get */
2105 res = hotkey_status_get(&hotkey_orig_status);
2106 if (!res && tp_features.hotkey_mask) {
2107 res = hotkey_mask_get();
2108 hotkey_orig_mask = hotkey_mask;
2109 if (!res) {
2110 res = add_many_to_attr_set(
2111 hotkey_dev_attributes,
2112 hotkey_mask_attributes,
2113 ARRAY_SIZE(hotkey_mask_attributes));
2117 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2118 if (tp_features.hotkey_mask) {
2119 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2120 & ~hotkey_all_mask;
2121 } else {
2122 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2125 vdbg_printk(TPACPI_DBG_INIT,
2126 "hotkey source mask 0x%08x, polling freq %d\n",
2127 hotkey_source_mask, hotkey_poll_freq);
2128 #endif
2130 /* Not all thinkpads have a hardware radio switch */
2131 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2132 tp_features.hotkey_wlsw = 1;
2133 printk(TPACPI_INFO
2134 "radio switch found; radios are %s\n",
2135 enabled(status, 0));
2136 res = add_to_attr_set(hotkey_dev_attributes,
2137 &dev_attr_hotkey_radio_sw.attr);
2140 /* For X41t, X60t, X61t Tablets... */
2141 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2142 tp_features.hotkey_tablet = 1;
2143 printk(TPACPI_INFO
2144 "possible tablet mode switch found; "
2145 "ThinkPad in %s mode\n",
2146 (status & TP_HOTKEY_TABLET_MASK)?
2147 "tablet" : "laptop");
2148 res = add_to_attr_set(hotkey_dev_attributes,
2149 &dev_attr_hotkey_tablet_mode.attr);
2152 if (!res)
2153 res = register_attr_set_with_sysfs(
2154 hotkey_dev_attributes,
2155 &tpacpi_pdev->dev.kobj);
2156 if (res)
2157 return res;
2159 /* Set up key map */
2161 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2162 GFP_KERNEL);
2163 if (!hotkey_keycode_map) {
2164 printk(TPACPI_ERR
2165 "failed to allocate memory for key map\n");
2166 return -ENOMEM;
2169 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2170 dbg_printk(TPACPI_DBG_INIT,
2171 "using Lenovo default hot key map\n");
2172 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2173 TPACPI_HOTKEY_MAP_SIZE);
2174 } else {
2175 dbg_printk(TPACPI_DBG_INIT,
2176 "using IBM default hot key map\n");
2177 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2178 TPACPI_HOTKEY_MAP_SIZE);
2181 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2182 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2183 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2184 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2185 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2186 tpacpi_inputdev->keycode = hotkey_keycode_map;
2187 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2188 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2189 set_bit(hotkey_keycode_map[i],
2190 tpacpi_inputdev->keybit);
2191 } else {
2192 if (i < sizeof(hotkey_reserved_mask)*8)
2193 hotkey_reserved_mask |= 1 << i;
2197 if (tp_features.hotkey_wlsw) {
2198 set_bit(EV_SW, tpacpi_inputdev->evbit);
2199 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2201 if (tp_features.hotkey_tablet) {
2202 set_bit(EV_SW, tpacpi_inputdev->evbit);
2203 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2206 /* Do not issue duplicate brightness change events to
2207 * userspace */
2208 if (!tp_features.bright_acpimode)
2209 /* update bright_acpimode... */
2210 tpacpi_check_std_acpi_brightness_support();
2212 if (tp_features.bright_acpimode) {
2213 printk(TPACPI_INFO
2214 "This ThinkPad has standard ACPI backlight "
2215 "brightness control, supported by the ACPI "
2216 "video driver\n");
2217 printk(TPACPI_NOTICE
2218 "Disabling thinkpad-acpi brightness events "
2219 "by default...\n");
2221 /* The hotkey_reserved_mask change below is not
2222 * necessary while the keys are at KEY_RESERVED in the
2223 * default map, but better safe than sorry, leave it
2224 * here as a marker of what we have to do, especially
2225 * when we finally become able to set this at runtime
2226 * on response to X.org requests */
2227 hotkey_reserved_mask |=
2228 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2229 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2232 dbg_printk(TPACPI_DBG_INIT,
2233 "enabling hot key handling\n");
2234 res = hotkey_status_set(1);
2235 if (res)
2236 return res;
2237 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2238 & ~hotkey_reserved_mask)
2239 | hotkey_orig_mask);
2240 if (res < 0 && res != -ENXIO)
2241 return res;
2243 dbg_printk(TPACPI_DBG_INIT,
2244 "legacy hot key reporting over procfs %s\n",
2245 (hotkey_report_mode < 2) ?
2246 "enabled" : "disabled");
2248 tpacpi_inputdev->open = &hotkey_inputdev_open;
2249 tpacpi_inputdev->close = &hotkey_inputdev_close;
2251 hotkey_poll_setup_safe(1);
2252 tpacpi_input_send_radiosw();
2253 tpacpi_input_send_tabletsw();
2256 return (tp_features.hotkey)? 0 : 1;
2259 static void hotkey_exit(void)
2261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 hotkey_poll_stop_sync();
2263 #endif
2265 if (tp_features.hotkey) {
2266 dbg_printk(TPACPI_DBG_EXIT,
2267 "restoring original hot key mask\n");
2268 /* no short-circuit boolean operator below! */
2269 if ((hotkey_mask_set(hotkey_orig_mask) |
2270 hotkey_status_set(hotkey_orig_status)) != 0)
2271 printk(TPACPI_ERR
2272 "failed to restore hot key mask "
2273 "to BIOS defaults\n");
2276 if (hotkey_dev_attributes) {
2277 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2278 hotkey_dev_attributes = NULL;
2282 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2284 u32 hkey;
2285 unsigned int scancode;
2286 int send_acpi_ev;
2287 int ignore_acpi_ev;
2288 int unk_ev;
2290 if (event != 0x80) {
2291 printk(TPACPI_ERR
2292 "unknown HKEY notification event %d\n", event);
2293 /* forward it to userspace, maybe it knows how to handle it */
2294 acpi_bus_generate_netlink_event(
2295 ibm->acpi->device->pnp.device_class,
2296 ibm->acpi->device->dev.bus_id,
2297 event, 0);
2298 return;
2301 while (1) {
2302 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2303 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2304 return;
2307 if (hkey == 0) {
2308 /* queue empty */
2309 return;
2312 send_acpi_ev = 1;
2313 ignore_acpi_ev = 0;
2314 unk_ev = 0;
2316 switch (hkey >> 12) {
2317 case 1:
2318 /* 0x1000-0x1FFF: key presses */
2319 scancode = hkey & 0xfff;
2320 if (scancode > 0 && scancode < 0x21) {
2321 scancode--;
2322 if (!(hotkey_source_mask & (1 << scancode))) {
2323 tpacpi_input_send_key(scancode);
2324 send_acpi_ev = 0;
2325 } else {
2326 ignore_acpi_ev = 1;
2328 } else {
2329 unk_ev = 1;
2331 break;
2332 case 2:
2333 /* Wakeup reason */
2334 switch (hkey) {
2335 case 0x2304: /* suspend, undock */
2336 case 0x2404: /* hibernation, undock */
2337 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2338 ignore_acpi_ev = 1;
2339 break;
2340 case 0x2305: /* suspend, bay eject */
2341 case 0x2405: /* hibernation, bay eject */
2342 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2343 ignore_acpi_ev = 1;
2344 break;
2345 default:
2346 unk_ev = 1;
2348 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2349 printk(TPACPI_INFO
2350 "woke up due to a hot-unplug "
2351 "request...\n");
2352 hotkey_wakeup_reason_notify_change();
2354 break;
2355 case 3:
2356 /* bay-related wakeups */
2357 if (hkey == 0x3003) {
2358 hotkey_autosleep_ack = 1;
2359 printk(TPACPI_INFO
2360 "bay ejected\n");
2361 hotkey_wakeup_hotunplug_complete_notify_change();
2362 } else {
2363 unk_ev = 1;
2365 break;
2366 case 4:
2367 /* dock-related wakeups */
2368 if (hkey == 0x4003) {
2369 hotkey_autosleep_ack = 1;
2370 printk(TPACPI_INFO
2371 "undocked\n");
2372 hotkey_wakeup_hotunplug_complete_notify_change();
2373 } else {
2374 unk_ev = 1;
2376 break;
2377 case 5:
2378 /* 0x5000-0x5FFF: human interface helpers */
2379 switch (hkey) {
2380 case 0x5010: /* Lenovo new BIOS: brightness changed */
2381 case 0x500b: /* X61t: tablet pen inserted into bay */
2382 case 0x500c: /* X61t: tablet pen removed from bay */
2383 break;
2384 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2385 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2386 tpacpi_input_send_tabletsw();
2387 hotkey_tablet_mode_notify_change();
2388 send_acpi_ev = 0;
2389 break;
2390 case 0x5001:
2391 case 0x5002:
2392 /* LID switch events. Do not propagate */
2393 ignore_acpi_ev = 1;
2394 break;
2395 default:
2396 unk_ev = 1;
2398 break;
2399 case 7:
2400 /* 0x7000-0x7FFF: misc */
2401 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2402 tpacpi_input_send_radiosw();
2403 hotkey_radio_sw_notify_change();
2404 send_acpi_ev = 0;
2405 break;
2407 /* fallthrough to default */
2408 default:
2409 unk_ev = 1;
2411 if (unk_ev) {
2412 printk(TPACPI_NOTICE
2413 "unhandled HKEY event 0x%04x\n", hkey);
2416 /* Legacy events */
2417 if (!ignore_acpi_ev &&
2418 (send_acpi_ev || hotkey_report_mode < 2)) {
2419 acpi_bus_generate_proc_event(ibm->acpi->device,
2420 event, hkey);
2423 /* netlink events */
2424 if (!ignore_acpi_ev && send_acpi_ev) {
2425 acpi_bus_generate_netlink_event(
2426 ibm->acpi->device->pnp.device_class,
2427 ibm->acpi->device->dev.bus_id,
2428 event, hkey);
2433 static void hotkey_suspend(pm_message_t state)
2435 /* Do these on suspend, we get the events on early resume! */
2436 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2437 hotkey_autosleep_ack = 0;
2440 static void hotkey_resume(void)
2442 if (hotkey_mask_get())
2443 printk(TPACPI_ERR
2444 "error while trying to read hot key mask "
2445 "from firmware\n");
2446 tpacpi_input_send_radiosw();
2447 hotkey_radio_sw_notify_change();
2448 hotkey_tablet_mode_notify_change();
2449 hotkey_wakeup_reason_notify_change();
2450 hotkey_wakeup_hotunplug_complete_notify_change();
2451 hotkey_poll_setup_safe(0);
2454 /* procfs -------------------------------------------------------------- */
2455 static int hotkey_read(char *p)
2457 int res, status;
2458 int len = 0;
2460 if (!tp_features.hotkey) {
2461 len += sprintf(p + len, "status:\t\tnot supported\n");
2462 return len;
2465 if (mutex_lock_interruptible(&hotkey_mutex))
2466 return -ERESTARTSYS;
2467 res = hotkey_status_get(&status);
2468 if (!res)
2469 res = hotkey_mask_get();
2470 mutex_unlock(&hotkey_mutex);
2471 if (res)
2472 return res;
2474 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2475 if (tp_features.hotkey_mask) {
2476 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2477 len += sprintf(p + len,
2478 "commands:\tenable, disable, reset, <mask>\n");
2479 } else {
2480 len += sprintf(p + len, "mask:\t\tnot supported\n");
2481 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2484 return len;
2487 static int hotkey_write(char *buf)
2489 int res, status;
2490 u32 mask;
2491 char *cmd;
2493 if (!tp_features.hotkey)
2494 return -ENODEV;
2496 if (mutex_lock_interruptible(&hotkey_mutex))
2497 return -ERESTARTSYS;
2499 status = -1;
2500 mask = hotkey_mask;
2502 res = 0;
2503 while ((cmd = next_cmd(&buf))) {
2504 if (strlencmp(cmd, "enable") == 0) {
2505 status = 1;
2506 } else if (strlencmp(cmd, "disable") == 0) {
2507 status = 0;
2508 } else if (strlencmp(cmd, "reset") == 0) {
2509 status = hotkey_orig_status;
2510 mask = hotkey_orig_mask;
2511 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2512 /* mask set */
2513 } else if (sscanf(cmd, "%x", &mask) == 1) {
2514 /* mask set */
2515 } else {
2516 res = -EINVAL;
2517 goto errexit;
2520 if (status != -1)
2521 res = hotkey_status_set(status);
2523 if (!res && mask != hotkey_mask)
2524 res = hotkey_mask_set(mask);
2526 errexit:
2527 mutex_unlock(&hotkey_mutex);
2528 return res;
2531 static const struct acpi_device_id ibm_htk_device_ids[] = {
2532 {TPACPI_ACPI_HKEY_HID, 0},
2533 {"", 0},
2536 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2537 .hid = ibm_htk_device_ids,
2538 .notify = hotkey_notify,
2539 .handle = &hkey_handle,
2540 .type = ACPI_DEVICE_NOTIFY,
2543 static struct ibm_struct hotkey_driver_data = {
2544 .name = "hotkey",
2545 .read = hotkey_read,
2546 .write = hotkey_write,
2547 .exit = hotkey_exit,
2548 .resume = hotkey_resume,
2549 .suspend = hotkey_suspend,
2550 .acpi = &ibm_hotkey_acpidriver,
2553 /*************************************************************************
2554 * Bluetooth subdriver
2557 enum {
2558 /* ACPI GBDC/SBDC bits */
2559 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2560 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2561 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2564 static int bluetooth_get_radiosw(void);
2565 static int bluetooth_set_radiosw(int radio_on);
2567 /* sysfs bluetooth enable ---------------------------------------------- */
2568 static ssize_t bluetooth_enable_show(struct device *dev,
2569 struct device_attribute *attr,
2570 char *buf)
2572 int status;
2574 status = bluetooth_get_radiosw();
2575 if (status < 0)
2576 return status;
2578 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2581 static ssize_t bluetooth_enable_store(struct device *dev,
2582 struct device_attribute *attr,
2583 const char *buf, size_t count)
2585 unsigned long t;
2586 int res;
2588 if (parse_strtoul(buf, 1, &t))
2589 return -EINVAL;
2591 res = bluetooth_set_radiosw(t);
2593 return (res) ? res : count;
2596 static struct device_attribute dev_attr_bluetooth_enable =
2597 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2598 bluetooth_enable_show, bluetooth_enable_store);
2600 /* --------------------------------------------------------------------- */
2602 static struct attribute *bluetooth_attributes[] = {
2603 &dev_attr_bluetooth_enable.attr,
2604 NULL
2607 static const struct attribute_group bluetooth_attr_group = {
2608 .attrs = bluetooth_attributes,
2611 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2613 int res;
2614 int status = 0;
2616 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2618 TPACPI_ACPIHANDLE_INIT(hkey);
2620 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2621 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2622 tp_features.bluetooth = hkey_handle &&
2623 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2625 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2626 str_supported(tp_features.bluetooth),
2627 status);
2629 if (tp_features.bluetooth) {
2630 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2631 /* no bluetooth hardware present in system */
2632 tp_features.bluetooth = 0;
2633 dbg_printk(TPACPI_DBG_INIT,
2634 "bluetooth hardware not installed\n");
2635 } else {
2636 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2637 &bluetooth_attr_group);
2638 if (res)
2639 return res;
2643 return (tp_features.bluetooth)? 0 : 1;
2646 static void bluetooth_exit(void)
2648 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2649 &bluetooth_attr_group);
2652 static int bluetooth_get_radiosw(void)
2654 int status;
2656 if (!tp_features.bluetooth)
2657 return -ENODEV;
2659 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2660 return -EIO;
2662 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2665 static int bluetooth_set_radiosw(int radio_on)
2667 int status;
2669 if (!tp_features.bluetooth)
2670 return -ENODEV;
2672 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2673 return -EIO;
2674 if (radio_on)
2675 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2676 else
2677 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2678 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2679 return -EIO;
2681 return 0;
2684 /* procfs -------------------------------------------------------------- */
2685 static int bluetooth_read(char *p)
2687 int len = 0;
2688 int status = bluetooth_get_radiosw();
2690 if (!tp_features.bluetooth)
2691 len += sprintf(p + len, "status:\t\tnot supported\n");
2692 else {
2693 len += sprintf(p + len, "status:\t\t%s\n",
2694 (status)? "enabled" : "disabled");
2695 len += sprintf(p + len, "commands:\tenable, disable\n");
2698 return len;
2701 static int bluetooth_write(char *buf)
2703 char *cmd;
2705 if (!tp_features.bluetooth)
2706 return -ENODEV;
2708 while ((cmd = next_cmd(&buf))) {
2709 if (strlencmp(cmd, "enable") == 0) {
2710 bluetooth_set_radiosw(1);
2711 } else if (strlencmp(cmd, "disable") == 0) {
2712 bluetooth_set_radiosw(0);
2713 } else
2714 return -EINVAL;
2717 return 0;
2720 static struct ibm_struct bluetooth_driver_data = {
2721 .name = "bluetooth",
2722 .read = bluetooth_read,
2723 .write = bluetooth_write,
2724 .exit = bluetooth_exit,
2727 /*************************************************************************
2728 * Wan subdriver
2731 enum {
2732 /* ACPI GWAN/SWAN bits */
2733 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2734 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2735 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2738 static int wan_get_radiosw(void);
2739 static int wan_set_radiosw(int radio_on);
2741 /* sysfs wan enable ---------------------------------------------------- */
2742 static ssize_t wan_enable_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2746 int status;
2748 status = wan_get_radiosw();
2749 if (status < 0)
2750 return status;
2752 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2755 static ssize_t wan_enable_store(struct device *dev,
2756 struct device_attribute *attr,
2757 const char *buf, size_t count)
2759 unsigned long t;
2760 int res;
2762 if (parse_strtoul(buf, 1, &t))
2763 return -EINVAL;
2765 res = wan_set_radiosw(t);
2767 return (res) ? res : count;
2770 static struct device_attribute dev_attr_wan_enable =
2771 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2772 wan_enable_show, wan_enable_store);
2774 /* --------------------------------------------------------------------- */
2776 static struct attribute *wan_attributes[] = {
2777 &dev_attr_wan_enable.attr,
2778 NULL
2781 static const struct attribute_group wan_attr_group = {
2782 .attrs = wan_attributes,
2785 static int __init wan_init(struct ibm_init_struct *iibm)
2787 int res;
2788 int status = 0;
2790 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2792 TPACPI_ACPIHANDLE_INIT(hkey);
2794 tp_features.wan = hkey_handle &&
2795 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2797 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2798 str_supported(tp_features.wan),
2799 status);
2801 if (tp_features.wan) {
2802 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2803 /* no wan hardware present in system */
2804 tp_features.wan = 0;
2805 dbg_printk(TPACPI_DBG_INIT,
2806 "wan hardware not installed\n");
2807 } else {
2808 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2809 &wan_attr_group);
2810 if (res)
2811 return res;
2815 return (tp_features.wan)? 0 : 1;
2818 static void wan_exit(void)
2820 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2821 &wan_attr_group);
2824 static int wan_get_radiosw(void)
2826 int status;
2828 if (!tp_features.wan)
2829 return -ENODEV;
2831 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2832 return -EIO;
2834 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2837 static int wan_set_radiosw(int radio_on)
2839 int status;
2841 if (!tp_features.wan)
2842 return -ENODEV;
2844 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2845 return -EIO;
2846 if (radio_on)
2847 status |= TP_ACPI_WANCARD_RADIOSSW;
2848 else
2849 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2850 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2851 return -EIO;
2853 return 0;
2856 /* procfs -------------------------------------------------------------- */
2857 static int wan_read(char *p)
2859 int len = 0;
2860 int status = wan_get_radiosw();
2862 if (!tp_features.wan)
2863 len += sprintf(p + len, "status:\t\tnot supported\n");
2864 else {
2865 len += sprintf(p + len, "status:\t\t%s\n",
2866 (status)? "enabled" : "disabled");
2867 len += sprintf(p + len, "commands:\tenable, disable\n");
2870 return len;
2873 static int wan_write(char *buf)
2875 char *cmd;
2877 if (!tp_features.wan)
2878 return -ENODEV;
2880 while ((cmd = next_cmd(&buf))) {
2881 if (strlencmp(cmd, "enable") == 0) {
2882 wan_set_radiosw(1);
2883 } else if (strlencmp(cmd, "disable") == 0) {
2884 wan_set_radiosw(0);
2885 } else
2886 return -EINVAL;
2889 return 0;
2892 static struct ibm_struct wan_driver_data = {
2893 .name = "wan",
2894 .read = wan_read,
2895 .write = wan_write,
2896 .exit = wan_exit,
2897 .flags.experimental = 1,
2900 /*************************************************************************
2901 * Video subdriver
2904 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2906 enum video_access_mode {
2907 TPACPI_VIDEO_NONE = 0,
2908 TPACPI_VIDEO_570, /* 570 */
2909 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2910 TPACPI_VIDEO_NEW, /* all others */
2913 enum { /* video status flags, based on VIDEO_570 */
2914 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2915 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2916 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2919 enum { /* TPACPI_VIDEO_570 constants */
2920 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2921 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2922 * video_status_flags */
2923 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2924 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2927 static enum video_access_mode video_supported;
2928 static int video_orig_autosw;
2930 static int video_autosw_get(void);
2931 static int video_autosw_set(int enable);
2933 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2935 static int __init video_init(struct ibm_init_struct *iibm)
2937 int ivga;
2939 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2941 TPACPI_ACPIHANDLE_INIT(vid);
2942 TPACPI_ACPIHANDLE_INIT(vid2);
2944 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2945 /* G41, assume IVGA doesn't change */
2946 vid_handle = vid2_handle;
2948 if (!vid_handle)
2949 /* video switching not supported on R30, R31 */
2950 video_supported = TPACPI_VIDEO_NONE;
2951 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2952 /* 570 */
2953 video_supported = TPACPI_VIDEO_570;
2954 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2955 /* 600e/x, 770e, 770x */
2956 video_supported = TPACPI_VIDEO_770;
2957 else
2958 /* all others */
2959 video_supported = TPACPI_VIDEO_NEW;
2961 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2962 str_supported(video_supported != TPACPI_VIDEO_NONE),
2963 video_supported);
2965 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2968 static void video_exit(void)
2970 dbg_printk(TPACPI_DBG_EXIT,
2971 "restoring original video autoswitch mode\n");
2972 if (video_autosw_set(video_orig_autosw))
2973 printk(TPACPI_ERR "error while trying to restore original "
2974 "video autoswitch mode\n");
2977 static int video_outputsw_get(void)
2979 int status = 0;
2980 int i;
2982 switch (video_supported) {
2983 case TPACPI_VIDEO_570:
2984 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2985 TP_ACPI_VIDEO_570_PHSCMD))
2986 return -EIO;
2987 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2988 break;
2989 case TPACPI_VIDEO_770:
2990 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2991 return -EIO;
2992 if (i)
2993 status |= TP_ACPI_VIDEO_S_LCD;
2994 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2995 return -EIO;
2996 if (i)
2997 status |= TP_ACPI_VIDEO_S_CRT;
2998 break;
2999 case TPACPI_VIDEO_NEW:
3000 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3001 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3002 return -EIO;
3003 if (i)
3004 status |= TP_ACPI_VIDEO_S_CRT;
3006 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3007 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3008 return -EIO;
3009 if (i)
3010 status |= TP_ACPI_VIDEO_S_LCD;
3011 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3012 return -EIO;
3013 if (i)
3014 status |= TP_ACPI_VIDEO_S_DVI;
3015 break;
3016 default:
3017 return -ENOSYS;
3020 return status;
3023 static int video_outputsw_set(int status)
3025 int autosw;
3026 int res = 0;
3028 switch (video_supported) {
3029 case TPACPI_VIDEO_570:
3030 res = acpi_evalf(NULL, NULL,
3031 "\\_SB.PHS2", "vdd",
3032 TP_ACPI_VIDEO_570_PHS2CMD,
3033 status | TP_ACPI_VIDEO_570_PHS2SET);
3034 break;
3035 case TPACPI_VIDEO_770:
3036 autosw = video_autosw_get();
3037 if (autosw < 0)
3038 return autosw;
3040 res = video_autosw_set(1);
3041 if (res)
3042 return res;
3043 res = acpi_evalf(vid_handle, NULL,
3044 "ASWT", "vdd", status * 0x100, 0);
3045 if (!autosw && video_autosw_set(autosw)) {
3046 printk(TPACPI_ERR
3047 "video auto-switch left enabled due to error\n");
3048 return -EIO;
3050 break;
3051 case TPACPI_VIDEO_NEW:
3052 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3053 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3054 break;
3055 default:
3056 return -ENOSYS;
3059 return (res)? 0 : -EIO;
3062 static int video_autosw_get(void)
3064 int autosw = 0;
3066 switch (video_supported) {
3067 case TPACPI_VIDEO_570:
3068 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3069 return -EIO;
3070 break;
3071 case TPACPI_VIDEO_770:
3072 case TPACPI_VIDEO_NEW:
3073 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3074 return -EIO;
3075 break;
3076 default:
3077 return -ENOSYS;
3080 return autosw & 1;
3083 static int video_autosw_set(int enable)
3085 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3086 return -EIO;
3087 return 0;
3090 static int video_outputsw_cycle(void)
3092 int autosw = video_autosw_get();
3093 int res;
3095 if (autosw < 0)
3096 return autosw;
3098 switch (video_supported) {
3099 case TPACPI_VIDEO_570:
3100 res = video_autosw_set(1);
3101 if (res)
3102 return res;
3103 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3104 break;
3105 case TPACPI_VIDEO_770:
3106 case TPACPI_VIDEO_NEW:
3107 res = video_autosw_set(1);
3108 if (res)
3109 return res;
3110 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3111 break;
3112 default:
3113 return -ENOSYS;
3115 if (!autosw && video_autosw_set(autosw)) {
3116 printk(TPACPI_ERR
3117 "video auto-switch left enabled due to error\n");
3118 return -EIO;
3121 return (res)? 0 : -EIO;
3124 static int video_expand_toggle(void)
3126 switch (video_supported) {
3127 case TPACPI_VIDEO_570:
3128 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3129 0 : -EIO;
3130 case TPACPI_VIDEO_770:
3131 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3132 0 : -EIO;
3133 case TPACPI_VIDEO_NEW:
3134 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3135 0 : -EIO;
3136 default:
3137 return -ENOSYS;
3139 /* not reached */
3142 static int video_read(char *p)
3144 int status, autosw;
3145 int len = 0;
3147 if (video_supported == TPACPI_VIDEO_NONE) {
3148 len += sprintf(p + len, "status:\t\tnot supported\n");
3149 return len;
3152 status = video_outputsw_get();
3153 if (status < 0)
3154 return status;
3156 autosw = video_autosw_get();
3157 if (autosw < 0)
3158 return autosw;
3160 len += sprintf(p + len, "status:\t\tsupported\n");
3161 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3162 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3163 if (video_supported == TPACPI_VIDEO_NEW)
3164 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3165 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3166 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3167 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3168 if (video_supported == TPACPI_VIDEO_NEW)
3169 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3170 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3171 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3173 return len;
3176 static int video_write(char *buf)
3178 char *cmd;
3179 int enable, disable, status;
3180 int res;
3182 if (video_supported == TPACPI_VIDEO_NONE)
3183 return -ENODEV;
3185 enable = 0;
3186 disable = 0;
3188 while ((cmd = next_cmd(&buf))) {
3189 if (strlencmp(cmd, "lcd_enable") == 0) {
3190 enable |= TP_ACPI_VIDEO_S_LCD;
3191 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3192 disable |= TP_ACPI_VIDEO_S_LCD;
3193 } else if (strlencmp(cmd, "crt_enable") == 0) {
3194 enable |= TP_ACPI_VIDEO_S_CRT;
3195 } else if (strlencmp(cmd, "crt_disable") == 0) {
3196 disable |= TP_ACPI_VIDEO_S_CRT;
3197 } else if (video_supported == TPACPI_VIDEO_NEW &&
3198 strlencmp(cmd, "dvi_enable") == 0) {
3199 enable |= TP_ACPI_VIDEO_S_DVI;
3200 } else if (video_supported == TPACPI_VIDEO_NEW &&
3201 strlencmp(cmd, "dvi_disable") == 0) {
3202 disable |= TP_ACPI_VIDEO_S_DVI;
3203 } else if (strlencmp(cmd, "auto_enable") == 0) {
3204 res = video_autosw_set(1);
3205 if (res)
3206 return res;
3207 } else if (strlencmp(cmd, "auto_disable") == 0) {
3208 res = video_autosw_set(0);
3209 if (res)
3210 return res;
3211 } else if (strlencmp(cmd, "video_switch") == 0) {
3212 res = video_outputsw_cycle();
3213 if (res)
3214 return res;
3215 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3216 res = video_expand_toggle();
3217 if (res)
3218 return res;
3219 } else
3220 return -EINVAL;
3223 if (enable || disable) {
3224 status = video_outputsw_get();
3225 if (status < 0)
3226 return status;
3227 res = video_outputsw_set((status & ~disable) | enable);
3228 if (res)
3229 return res;
3232 return 0;
3235 static struct ibm_struct video_driver_data = {
3236 .name = "video",
3237 .read = video_read,
3238 .write = video_write,
3239 .exit = video_exit,
3242 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3244 /*************************************************************************
3245 * Light (thinklight) subdriver
3248 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3249 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3251 static int light_get_status(void)
3253 int status = 0;
3255 if (tp_features.light_status) {
3256 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3257 return -EIO;
3258 return (!!status);
3261 return -ENXIO;
3264 static int light_set_status(int status)
3266 int rc;
3268 if (tp_features.light) {
3269 if (cmos_handle) {
3270 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3271 (status)?
3272 TP_CMOS_THINKLIGHT_ON :
3273 TP_CMOS_THINKLIGHT_OFF);
3274 } else {
3275 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3276 (status)? 1 : 0);
3278 return (rc)? 0 : -EIO;
3281 return -ENXIO;
3284 static void light_set_status_worker(struct work_struct *work)
3286 struct tpacpi_led_classdev *data =
3287 container_of(work, struct tpacpi_led_classdev, work);
3289 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3290 light_set_status((data->new_brightness != LED_OFF));
3293 static void light_sysfs_set(struct led_classdev *led_cdev,
3294 enum led_brightness brightness)
3296 struct tpacpi_led_classdev *data =
3297 container_of(led_cdev,
3298 struct tpacpi_led_classdev,
3299 led_classdev);
3300 data->new_brightness = brightness;
3301 schedule_work(&data->work);
3304 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3306 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3309 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3310 .led_classdev = {
3311 .name = "tpacpi::thinklight",
3312 .brightness_set = &light_sysfs_set,
3313 .brightness_get = &light_sysfs_get,
3317 static int __init light_init(struct ibm_init_struct *iibm)
3319 int rc = 0;
3321 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3323 TPACPI_ACPIHANDLE_INIT(ledb);
3324 TPACPI_ACPIHANDLE_INIT(lght);
3325 TPACPI_ACPIHANDLE_INIT(cmos);
3326 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3328 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3329 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3331 if (tp_features.light)
3332 /* light status not supported on
3333 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3334 tp_features.light_status =
3335 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3337 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3338 str_supported(tp_features.light));
3340 if (tp_features.light) {
3341 rc = led_classdev_register(&tpacpi_pdev->dev,
3342 &tpacpi_led_thinklight.led_classdev);
3345 if (rc < 0) {
3346 tp_features.light = 0;
3347 tp_features.light_status = 0;
3348 } else {
3349 rc = (tp_features.light)? 0 : 1;
3351 return rc;
3354 static void light_exit(void)
3356 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3357 if (work_pending(&tpacpi_led_thinklight.work))
3358 flush_scheduled_work();
3361 static int light_read(char *p)
3363 int len = 0;
3364 int status;
3366 if (!tp_features.light) {
3367 len += sprintf(p + len, "status:\t\tnot supported\n");
3368 } else if (!tp_features.light_status) {
3369 len += sprintf(p + len, "status:\t\tunknown\n");
3370 len += sprintf(p + len, "commands:\ton, off\n");
3371 } else {
3372 status = light_get_status();
3373 if (status < 0)
3374 return status;
3375 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3376 len += sprintf(p + len, "commands:\ton, off\n");
3379 return len;
3382 static int light_write(char *buf)
3384 char *cmd;
3385 int newstatus = 0;
3387 if (!tp_features.light)
3388 return -ENODEV;
3390 while ((cmd = next_cmd(&buf))) {
3391 if (strlencmp(cmd, "on") == 0) {
3392 newstatus = 1;
3393 } else if (strlencmp(cmd, "off") == 0) {
3394 newstatus = 0;
3395 } else
3396 return -EINVAL;
3399 return light_set_status(newstatus);
3402 static struct ibm_struct light_driver_data = {
3403 .name = "light",
3404 .read = light_read,
3405 .write = light_write,
3406 .exit = light_exit,
3409 /*************************************************************************
3410 * Dock subdriver
3413 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3415 static void dock_notify(struct ibm_struct *ibm, u32 event);
3416 static int dock_read(char *p);
3417 static int dock_write(char *buf);
3419 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3420 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3421 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3422 "\\_SB.PCI.ISA.SLCE", /* 570 */
3423 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3425 /* don't list other alternatives as we install a notify handler on the 570 */
3426 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3428 static const struct acpi_device_id ibm_pci_device_ids[] = {
3429 {PCI_ROOT_HID_STRING, 0},
3430 {"", 0},
3433 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3435 .notify = dock_notify,
3436 .handle = &dock_handle,
3437 .type = ACPI_SYSTEM_NOTIFY,
3440 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3441 * We just use it to get notifications of dock hotplug
3442 * in very old thinkpads */
3443 .hid = ibm_pci_device_ids,
3444 .notify = dock_notify,
3445 .handle = &pci_handle,
3446 .type = ACPI_SYSTEM_NOTIFY,
3450 static struct ibm_struct dock_driver_data[2] = {
3452 .name = "dock",
3453 .read = dock_read,
3454 .write = dock_write,
3455 .acpi = &ibm_dock_acpidriver[0],
3458 .name = "dock",
3459 .acpi = &ibm_dock_acpidriver[1],
3463 #define dock_docked() (_sta(dock_handle) & 1)
3465 static int __init dock_init(struct ibm_init_struct *iibm)
3467 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3469 TPACPI_ACPIHANDLE_INIT(dock);
3471 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3472 str_supported(dock_handle != NULL));
3474 return (dock_handle)? 0 : 1;
3477 static int __init dock_init2(struct ibm_init_struct *iibm)
3479 int dock2_needed;
3481 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3483 if (dock_driver_data[0].flags.acpi_driver_registered &&
3484 dock_driver_data[0].flags.acpi_notify_installed) {
3485 TPACPI_ACPIHANDLE_INIT(pci);
3486 dock2_needed = (pci_handle != NULL);
3487 vdbg_printk(TPACPI_DBG_INIT,
3488 "dock PCI handler for the TP 570 is %s\n",
3489 str_supported(dock2_needed));
3490 } else {
3491 vdbg_printk(TPACPI_DBG_INIT,
3492 "dock subdriver part 2 not required\n");
3493 dock2_needed = 0;
3496 return (dock2_needed)? 0 : 1;
3499 static void dock_notify(struct ibm_struct *ibm, u32 event)
3501 int docked = dock_docked();
3502 int pci = ibm->acpi->hid && ibm->acpi->device &&
3503 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3504 int data;
3506 if (event == 1 && !pci) /* 570 */
3507 data = 1; /* button */
3508 else if (event == 1 && pci) /* 570 */
3509 data = 3; /* dock */
3510 else if (event == 3 && docked)
3511 data = 1; /* button */
3512 else if (event == 3 && !docked)
3513 data = 2; /* undock */
3514 else if (event == 0 && docked)
3515 data = 3; /* dock */
3516 else {
3517 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3518 event, _sta(dock_handle));
3519 data = 0; /* unknown */
3521 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3522 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3523 ibm->acpi->device->dev.bus_id,
3524 event, data);
3527 static int dock_read(char *p)
3529 int len = 0;
3530 int docked = dock_docked();
3532 if (!dock_handle)
3533 len += sprintf(p + len, "status:\t\tnot supported\n");
3534 else if (!docked)
3535 len += sprintf(p + len, "status:\t\tundocked\n");
3536 else {
3537 len += sprintf(p + len, "status:\t\tdocked\n");
3538 len += sprintf(p + len, "commands:\tdock, undock\n");
3541 return len;
3544 static int dock_write(char *buf)
3546 char *cmd;
3548 if (!dock_docked())
3549 return -ENODEV;
3551 while ((cmd = next_cmd(&buf))) {
3552 if (strlencmp(cmd, "undock") == 0) {
3553 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3554 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3555 return -EIO;
3556 } else if (strlencmp(cmd, "dock") == 0) {
3557 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3558 return -EIO;
3559 } else
3560 return -EINVAL;
3563 return 0;
3566 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3568 /*************************************************************************
3569 * Bay subdriver
3572 #ifdef CONFIG_THINKPAD_ACPI_BAY
3574 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3575 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3576 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3577 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3578 ); /* A21e, R30, R31 */
3579 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3580 "_EJ0", /* all others */
3581 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3582 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3583 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3584 ); /* all others */
3585 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3586 "_EJ0", /* 770x */
3587 ); /* all others */
3589 static int __init bay_init(struct ibm_init_struct *iibm)
3591 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3593 TPACPI_ACPIHANDLE_INIT(bay);
3594 if (bay_handle)
3595 TPACPI_ACPIHANDLE_INIT(bay_ej);
3596 TPACPI_ACPIHANDLE_INIT(bay2);
3597 if (bay2_handle)
3598 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3600 tp_features.bay_status = bay_handle &&
3601 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3602 tp_features.bay_status2 = bay2_handle &&
3603 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3605 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3606 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3607 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3608 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3610 vdbg_printk(TPACPI_DBG_INIT,
3611 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3612 str_supported(tp_features.bay_status),
3613 str_supported(tp_features.bay_eject),
3614 str_supported(tp_features.bay_status2),
3615 str_supported(tp_features.bay_eject2));
3617 return (tp_features.bay_status || tp_features.bay_eject ||
3618 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3621 static void bay_notify(struct ibm_struct *ibm, u32 event)
3623 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3624 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3625 ibm->acpi->device->dev.bus_id,
3626 event, 0);
3629 #define bay_occupied(b) (_sta(b##_handle) & 1)
3631 static int bay_read(char *p)
3633 int len = 0;
3634 int occupied = bay_occupied(bay);
3635 int occupied2 = bay_occupied(bay2);
3636 int eject, eject2;
3638 len += sprintf(p + len, "status:\t\t%s\n",
3639 tp_features.bay_status ?
3640 (occupied ? "occupied" : "unoccupied") :
3641 "not supported");
3642 if (tp_features.bay_status2)
3643 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3644 "occupied" : "unoccupied");
3646 eject = tp_features.bay_eject && occupied;
3647 eject2 = tp_features.bay_eject2 && occupied2;
3649 if (eject && eject2)
3650 len += sprintf(p + len, "commands:\teject, eject2\n");
3651 else if (eject)
3652 len += sprintf(p + len, "commands:\teject\n");
3653 else if (eject2)
3654 len += sprintf(p + len, "commands:\teject2\n");
3656 return len;
3659 static int bay_write(char *buf)
3661 char *cmd;
3663 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3664 return -ENODEV;
3666 while ((cmd = next_cmd(&buf))) {
3667 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3668 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3669 return -EIO;
3670 } else if (tp_features.bay_eject2 &&
3671 strlencmp(cmd, "eject2") == 0) {
3672 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3673 return -EIO;
3674 } else
3675 return -EINVAL;
3678 return 0;
3681 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3682 .notify = bay_notify,
3683 .handle = &bay_handle,
3684 .type = ACPI_SYSTEM_NOTIFY,
3687 static struct ibm_struct bay_driver_data = {
3688 .name = "bay",
3689 .read = bay_read,
3690 .write = bay_write,
3691 .acpi = &ibm_bay_acpidriver,
3694 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3696 /*************************************************************************
3697 * CMOS subdriver
3700 /* sysfs cmos_command -------------------------------------------------- */
3701 static ssize_t cmos_command_store(struct device *dev,
3702 struct device_attribute *attr,
3703 const char *buf, size_t count)
3705 unsigned long cmos_cmd;
3706 int res;
3708 if (parse_strtoul(buf, 21, &cmos_cmd))
3709 return -EINVAL;
3711 res = issue_thinkpad_cmos_command(cmos_cmd);
3712 return (res)? res : count;
3715 static struct device_attribute dev_attr_cmos_command =
3716 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3718 /* --------------------------------------------------------------------- */
3720 static int __init cmos_init(struct ibm_init_struct *iibm)
3722 int res;
3724 vdbg_printk(TPACPI_DBG_INIT,
3725 "initializing cmos commands subdriver\n");
3727 TPACPI_ACPIHANDLE_INIT(cmos);
3729 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3730 str_supported(cmos_handle != NULL));
3732 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3733 if (res)
3734 return res;
3736 return (cmos_handle)? 0 : 1;
3739 static void cmos_exit(void)
3741 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3744 static int cmos_read(char *p)
3746 int len = 0;
3748 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3749 R30, R31, T20-22, X20-21 */
3750 if (!cmos_handle)
3751 len += sprintf(p + len, "status:\t\tnot supported\n");
3752 else {
3753 len += sprintf(p + len, "status:\t\tsupported\n");
3754 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3757 return len;
3760 static int cmos_write(char *buf)
3762 char *cmd;
3763 int cmos_cmd, res;
3765 while ((cmd = next_cmd(&buf))) {
3766 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3767 cmos_cmd >= 0 && cmos_cmd <= 21) {
3768 /* cmos_cmd set */
3769 } else
3770 return -EINVAL;
3772 res = issue_thinkpad_cmos_command(cmos_cmd);
3773 if (res)
3774 return res;
3777 return 0;
3780 static struct ibm_struct cmos_driver_data = {
3781 .name = "cmos",
3782 .read = cmos_read,
3783 .write = cmos_write,
3784 .exit = cmos_exit,
3787 /*************************************************************************
3788 * LED subdriver
3791 enum led_access_mode {
3792 TPACPI_LED_NONE = 0,
3793 TPACPI_LED_570, /* 570 */
3794 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3795 TPACPI_LED_NEW, /* all others */
3798 enum { /* For TPACPI_LED_OLD */
3799 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3800 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3801 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3804 enum led_status_t {
3805 TPACPI_LED_OFF = 0,
3806 TPACPI_LED_ON,
3807 TPACPI_LED_BLINK,
3810 static enum led_access_mode led_supported;
3812 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3813 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3814 /* T20-22, X20-21 */
3815 "LED", /* all others */
3816 ); /* R30, R31 */
3818 #define TPACPI_LED_NUMLEDS 8
3819 static struct tpacpi_led_classdev *tpacpi_leds;
3820 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
3821 static const char const *tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
3822 /* there's a limit of 19 chars + NULL before 2.6.26 */
3823 "tpacpi::power",
3824 "tpacpi:orange:batt",
3825 "tpacpi:green:batt",
3826 "tpacpi::dock_active",
3827 "tpacpi::bay_active",
3828 "tpacpi::dock_batt",
3829 "tpacpi::unknown_led",
3830 "tpacpi::standby",
3833 static int led_get_status(unsigned int led)
3835 int status;
3836 enum led_status_t led_s;
3838 switch (led_supported) {
3839 case TPACPI_LED_570:
3840 if (!acpi_evalf(ec_handle,
3841 &status, "GLED", "dd", 1 << led))
3842 return -EIO;
3843 led_s = (status == 0)?
3844 TPACPI_LED_OFF :
3845 ((status == 1)?
3846 TPACPI_LED_ON :
3847 TPACPI_LED_BLINK);
3848 tpacpi_led_state_cache[led] = led_s;
3849 return led_s;
3850 default:
3851 return -ENXIO;
3854 /* not reached */
3857 static int led_set_status(unsigned int led, enum led_status_t ledstatus)
3859 /* off, on, blink. Index is led_status_t */
3860 static const int const led_sled_arg1[] = { 0, 1, 3 };
3861 static const int const led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3862 static const int const led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3863 static const int const led_led_arg1[] = { 0, 0x80, 0xc0 };
3865 int rc = 0;
3867 switch (led_supported) {
3868 case TPACPI_LED_570:
3869 /* 570 */
3870 led = 1 << led;
3871 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3872 led, led_sled_arg1[ledstatus]))
3873 rc = -EIO;
3874 break;
3875 case TPACPI_LED_OLD:
3876 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3877 led = 1 << led;
3878 rc = ec_write(TPACPI_LED_EC_HLMS, led);
3879 if (rc >= 0)
3880 rc = ec_write(TPACPI_LED_EC_HLBL,
3881 led * led_exp_hlbl[ledstatus]);
3882 if (rc >= 0)
3883 rc = ec_write(TPACPI_LED_EC_HLCL,
3884 led * led_exp_hlcl[ledstatus]);
3885 break;
3886 case TPACPI_LED_NEW:
3887 /* all others */
3888 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3889 led, led_led_arg1[ledstatus]))
3890 rc = -EIO;
3891 break;
3892 default:
3893 rc = -ENXIO;
3896 if (!rc)
3897 tpacpi_led_state_cache[led] = ledstatus;
3899 return rc;
3902 static void led_sysfs_set_status(unsigned int led,
3903 enum led_brightness brightness)
3905 led_set_status(led,
3906 (brightness == LED_OFF) ?
3907 TPACPI_LED_OFF :
3908 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3909 TPACPI_LED_BLINK : TPACPI_LED_ON);
3912 static void led_set_status_worker(struct work_struct *work)
3914 struct tpacpi_led_classdev *data =
3915 container_of(work, struct tpacpi_led_classdev, work);
3917 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3918 led_sysfs_set_status(data->led, data->new_brightness);
3921 static void led_sysfs_set(struct led_classdev *led_cdev,
3922 enum led_brightness brightness)
3924 struct tpacpi_led_classdev *data = container_of(led_cdev,
3925 struct tpacpi_led_classdev, led_classdev);
3927 data->new_brightness = brightness;
3928 schedule_work(&data->work);
3931 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3932 unsigned long *delay_on, unsigned long *delay_off)
3934 struct tpacpi_led_classdev *data = container_of(led_cdev,
3935 struct tpacpi_led_classdev, led_classdev);
3937 /* Can we choose the flash rate? */
3938 if (*delay_on == 0 && *delay_off == 0) {
3939 /* yes. set them to the hardware blink rate (1 Hz) */
3940 *delay_on = 500; /* ms */
3941 *delay_off = 500; /* ms */
3942 } else if ((*delay_on != 500) || (*delay_off != 500))
3943 return -EINVAL;
3945 data->new_brightness = TPACPI_LED_BLINK;
3946 schedule_work(&data->work);
3948 return 0;
3951 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3953 int rc;
3955 struct tpacpi_led_classdev *data = container_of(led_cdev,
3956 struct tpacpi_led_classdev, led_classdev);
3958 rc = led_get_status(data->led);
3960 if (rc == TPACPI_LED_OFF || rc < 0)
3961 rc = LED_OFF; /* no error handling in led class :( */
3962 else
3963 rc = LED_FULL;
3965 return rc;
3968 static void led_exit(void)
3970 unsigned int i;
3972 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
3973 if (tpacpi_leds[i].led_classdev.name)
3974 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
3977 kfree(tpacpi_leds);
3978 tpacpi_leds = NULL;
3981 static int __init led_init(struct ibm_init_struct *iibm)
3983 unsigned int i;
3984 int rc;
3986 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3988 TPACPI_ACPIHANDLE_INIT(led);
3990 if (!led_handle)
3991 /* led not supported on R30, R31 */
3992 led_supported = TPACPI_LED_NONE;
3993 else if (strlencmp(led_path, "SLED") == 0)
3994 /* 570 */
3995 led_supported = TPACPI_LED_570;
3996 else if (strlencmp(led_path, "SYSL") == 0)
3997 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3998 led_supported = TPACPI_LED_OLD;
3999 else
4000 /* all others */
4001 led_supported = TPACPI_LED_NEW;
4003 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4004 str_supported(led_supported), led_supported);
4006 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4007 GFP_KERNEL);
4008 if (!tpacpi_leds) {
4009 printk(TPACPI_ERR "Out of memory for LED data\n");
4010 return -ENOMEM;
4013 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4014 tpacpi_leds[i].led = i;
4016 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4017 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4018 if (led_supported == TPACPI_LED_570)
4019 tpacpi_leds[i].led_classdev.brightness_get =
4020 &led_sysfs_get;
4022 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4024 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4026 rc = led_classdev_register(&tpacpi_pdev->dev,
4027 &tpacpi_leds[i].led_classdev);
4028 if (rc < 0) {
4029 tpacpi_leds[i].led_classdev.name = NULL;
4030 led_exit();
4031 return rc;
4035 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4038 #define str_led_status(s) \
4039 ((s) == TPACPI_LED_OFF ? "off" : \
4040 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4042 static int led_read(char *p)
4044 int len = 0;
4046 if (!led_supported) {
4047 len += sprintf(p + len, "status:\t\tnot supported\n");
4048 return len;
4050 len += sprintf(p + len, "status:\t\tsupported\n");
4052 if (led_supported == TPACPI_LED_570) {
4053 /* 570 */
4054 int i, status;
4055 for (i = 0; i < 8; i++) {
4056 status = led_get_status(i);
4057 if (status < 0)
4058 return -EIO;
4059 len += sprintf(p + len, "%d:\t\t%s\n",
4060 i, str_led_status(status));
4064 len += sprintf(p + len, "commands:\t"
4065 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4067 return len;
4070 static int led_write(char *buf)
4072 char *cmd;
4073 int led, rc;
4074 enum led_status_t s;
4076 if (!led_supported)
4077 return -ENODEV;
4079 while ((cmd = next_cmd(&buf))) {
4080 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4081 return -EINVAL;
4083 if (strstr(cmd, "off")) {
4084 s = TPACPI_LED_OFF;
4085 } else if (strstr(cmd, "on")) {
4086 s = TPACPI_LED_ON;
4087 } else if (strstr(cmd, "blink")) {
4088 s = TPACPI_LED_BLINK;
4089 } else {
4090 return -EINVAL;
4093 rc = led_set_status(led, s);
4094 if (rc < 0)
4095 return rc;
4098 return 0;
4101 static struct ibm_struct led_driver_data = {
4102 .name = "led",
4103 .read = led_read,
4104 .write = led_write,
4105 .exit = led_exit,
4108 /*************************************************************************
4109 * Beep subdriver
4112 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4114 static int __init beep_init(struct ibm_init_struct *iibm)
4116 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4118 TPACPI_ACPIHANDLE_INIT(beep);
4120 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4121 str_supported(beep_handle != NULL));
4123 return (beep_handle)? 0 : 1;
4126 static int beep_read(char *p)
4128 int len = 0;
4130 if (!beep_handle)
4131 len += sprintf(p + len, "status:\t\tnot supported\n");
4132 else {
4133 len += sprintf(p + len, "status:\t\tsupported\n");
4134 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4137 return len;
4140 static int beep_write(char *buf)
4142 char *cmd;
4143 int beep_cmd;
4145 if (!beep_handle)
4146 return -ENODEV;
4148 while ((cmd = next_cmd(&buf))) {
4149 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4150 beep_cmd >= 0 && beep_cmd <= 17) {
4151 /* beep_cmd set */
4152 } else
4153 return -EINVAL;
4154 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4155 return -EIO;
4158 return 0;
4161 static struct ibm_struct beep_driver_data = {
4162 .name = "beep",
4163 .read = beep_read,
4164 .write = beep_write,
4167 /*************************************************************************
4168 * Thermal subdriver
4171 enum thermal_access_mode {
4172 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4173 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4174 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4175 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4176 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4179 enum { /* TPACPI_THERMAL_TPEC_* */
4180 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4181 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4182 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4185 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4186 struct ibm_thermal_sensors_struct {
4187 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4190 static enum thermal_access_mode thermal_read_mode;
4192 /* idx is zero-based */
4193 static int thermal_get_sensor(int idx, s32 *value)
4195 int t;
4196 s8 tmp;
4197 char tmpi[5];
4199 t = TP_EC_THERMAL_TMP0;
4201 switch (thermal_read_mode) {
4202 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4203 case TPACPI_THERMAL_TPEC_16:
4204 if (idx >= 8 && idx <= 15) {
4205 t = TP_EC_THERMAL_TMP8;
4206 idx -= 8;
4208 /* fallthrough */
4209 #endif
4210 case TPACPI_THERMAL_TPEC_8:
4211 if (idx <= 7) {
4212 if (!acpi_ec_read(t + idx, &tmp))
4213 return -EIO;
4214 *value = tmp * 1000;
4215 return 0;
4217 break;
4219 case TPACPI_THERMAL_ACPI_UPDT:
4220 if (idx <= 7) {
4221 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4222 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4223 return -EIO;
4224 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4225 return -EIO;
4226 *value = (t - 2732) * 100;
4227 return 0;
4229 break;
4231 case TPACPI_THERMAL_ACPI_TMP07:
4232 if (idx <= 7) {
4233 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4234 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4235 return -EIO;
4236 if (t > 127 || t < -127)
4237 t = TP_EC_THERMAL_TMP_NA;
4238 *value = t * 1000;
4239 return 0;
4241 break;
4243 case TPACPI_THERMAL_NONE:
4244 default:
4245 return -ENOSYS;
4248 return -EINVAL;
4251 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4253 int res, i;
4254 int n;
4256 n = 8;
4257 i = 0;
4259 if (!s)
4260 return -EINVAL;
4262 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4263 n = 16;
4265 for (i = 0 ; i < n; i++) {
4266 res = thermal_get_sensor(i, &s->temp[i]);
4267 if (res)
4268 return res;
4271 return n;
4274 /* sysfs temp##_input -------------------------------------------------- */
4276 static ssize_t thermal_temp_input_show(struct device *dev,
4277 struct device_attribute *attr,
4278 char *buf)
4280 struct sensor_device_attribute *sensor_attr =
4281 to_sensor_dev_attr(attr);
4282 int idx = sensor_attr->index;
4283 s32 value;
4284 int res;
4286 res = thermal_get_sensor(idx, &value);
4287 if (res)
4288 return res;
4289 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4290 return -ENXIO;
4292 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4295 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4296 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4297 thermal_temp_input_show, NULL, _idxB)
4299 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4300 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4301 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4302 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4303 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4304 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4305 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4306 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4307 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4308 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4309 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4310 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4311 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4312 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4313 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4314 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4315 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4318 #define THERMAL_ATTRS(X) \
4319 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4321 static struct attribute *thermal_temp_input_attr[] = {
4322 THERMAL_ATTRS(8),
4323 THERMAL_ATTRS(9),
4324 THERMAL_ATTRS(10),
4325 THERMAL_ATTRS(11),
4326 THERMAL_ATTRS(12),
4327 THERMAL_ATTRS(13),
4328 THERMAL_ATTRS(14),
4329 THERMAL_ATTRS(15),
4330 THERMAL_ATTRS(0),
4331 THERMAL_ATTRS(1),
4332 THERMAL_ATTRS(2),
4333 THERMAL_ATTRS(3),
4334 THERMAL_ATTRS(4),
4335 THERMAL_ATTRS(5),
4336 THERMAL_ATTRS(6),
4337 THERMAL_ATTRS(7),
4338 NULL
4341 static const struct attribute_group thermal_temp_input16_group = {
4342 .attrs = thermal_temp_input_attr
4345 static const struct attribute_group thermal_temp_input8_group = {
4346 .attrs = &thermal_temp_input_attr[8]
4349 #undef THERMAL_SENSOR_ATTR_TEMP
4350 #undef THERMAL_ATTRS
4352 /* --------------------------------------------------------------------- */
4354 static int __init thermal_init(struct ibm_init_struct *iibm)
4356 u8 t, ta1, ta2;
4357 int i;
4358 int acpi_tmp7;
4359 int res;
4361 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4363 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4365 if (thinkpad_id.ec_model) {
4367 * Direct EC access mode: sensors at registers
4368 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4369 * non-implemented, thermal sensors return 0x80 when
4370 * not available
4373 ta1 = ta2 = 0;
4374 for (i = 0; i < 8; i++) {
4375 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4376 ta1 |= t;
4377 } else {
4378 ta1 = 0;
4379 break;
4381 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4382 ta2 |= t;
4383 } else {
4384 ta1 = 0;
4385 break;
4388 if (ta1 == 0) {
4389 /* This is sheer paranoia, but we handle it anyway */
4390 if (acpi_tmp7) {
4391 printk(TPACPI_ERR
4392 "ThinkPad ACPI EC access misbehaving, "
4393 "falling back to ACPI TMPx access "
4394 "mode\n");
4395 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4396 } else {
4397 printk(TPACPI_ERR
4398 "ThinkPad ACPI EC access misbehaving, "
4399 "disabling thermal sensors access\n");
4400 thermal_read_mode = TPACPI_THERMAL_NONE;
4402 } else {
4403 thermal_read_mode =
4404 (ta2 != 0) ?
4405 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4407 } else if (acpi_tmp7) {
4408 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4409 /* 600e/x, 770e, 770x */
4410 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4411 } else {
4412 /* Standard ACPI TMPx access, max 8 sensors */
4413 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4415 } else {
4416 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4417 thermal_read_mode = TPACPI_THERMAL_NONE;
4420 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4421 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4422 thermal_read_mode);
4424 switch (thermal_read_mode) {
4425 case TPACPI_THERMAL_TPEC_16:
4426 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4427 &thermal_temp_input16_group);
4428 if (res)
4429 return res;
4430 break;
4431 case TPACPI_THERMAL_TPEC_8:
4432 case TPACPI_THERMAL_ACPI_TMP07:
4433 case TPACPI_THERMAL_ACPI_UPDT:
4434 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4435 &thermal_temp_input8_group);
4436 if (res)
4437 return res;
4438 break;
4439 case TPACPI_THERMAL_NONE:
4440 default:
4441 return 1;
4444 return 0;
4447 static void thermal_exit(void)
4449 switch (thermal_read_mode) {
4450 case TPACPI_THERMAL_TPEC_16:
4451 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4452 &thermal_temp_input16_group);
4453 break;
4454 case TPACPI_THERMAL_TPEC_8:
4455 case TPACPI_THERMAL_ACPI_TMP07:
4456 case TPACPI_THERMAL_ACPI_UPDT:
4457 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4458 &thermal_temp_input16_group);
4459 break;
4460 case TPACPI_THERMAL_NONE:
4461 default:
4462 break;
4466 static int thermal_read(char *p)
4468 int len = 0;
4469 int n, i;
4470 struct ibm_thermal_sensors_struct t;
4472 n = thermal_get_sensors(&t);
4473 if (unlikely(n < 0))
4474 return n;
4476 len += sprintf(p + len, "temperatures:\t");
4478 if (n > 0) {
4479 for (i = 0; i < (n - 1); i++)
4480 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4481 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4482 } else
4483 len += sprintf(p + len, "not supported\n");
4485 return len;
4488 static struct ibm_struct thermal_driver_data = {
4489 .name = "thermal",
4490 .read = thermal_read,
4491 .exit = thermal_exit,
4494 /*************************************************************************
4495 * EC Dump subdriver
4498 static u8 ecdump_regs[256];
4500 static int ecdump_read(char *p)
4502 int len = 0;
4503 int i, j;
4504 u8 v;
4506 len += sprintf(p + len, "EC "
4507 " +00 +01 +02 +03 +04 +05 +06 +07"
4508 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4509 for (i = 0; i < 256; i += 16) {
4510 len += sprintf(p + len, "EC 0x%02x:", i);
4511 for (j = 0; j < 16; j++) {
4512 if (!acpi_ec_read(i + j, &v))
4513 break;
4514 if (v != ecdump_regs[i + j])
4515 len += sprintf(p + len, " *%02x", v);
4516 else
4517 len += sprintf(p + len, " %02x", v);
4518 ecdump_regs[i + j] = v;
4520 len += sprintf(p + len, "\n");
4521 if (j != 16)
4522 break;
4525 /* These are way too dangerous to advertise openly... */
4526 #if 0
4527 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4528 " (<offset> is 00-ff, <value> is 00-ff)\n");
4529 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4530 " (<offset> is 00-ff, <value> is 0-255)\n");
4531 #endif
4532 return len;
4535 static int ecdump_write(char *buf)
4537 char *cmd;
4538 int i, v;
4540 while ((cmd = next_cmd(&buf))) {
4541 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4542 /* i and v set */
4543 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4544 /* i and v set */
4545 } else
4546 return -EINVAL;
4547 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4548 if (!acpi_ec_write(i, v))
4549 return -EIO;
4550 } else
4551 return -EINVAL;
4554 return 0;
4557 static struct ibm_struct ecdump_driver_data = {
4558 .name = "ecdump",
4559 .read = ecdump_read,
4560 .write = ecdump_write,
4561 .flags.experimental = 1,
4564 /*************************************************************************
4565 * Backlight/brightness subdriver
4568 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4570 enum {
4571 TP_EC_BACKLIGHT = 0x31,
4573 /* TP_EC_BACKLIGHT bitmasks */
4574 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4575 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4576 TP_EC_BACKLIGHT_MAPSW = 0x20,
4579 static struct backlight_device *ibm_backlight_device;
4580 static int brightness_mode;
4581 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4583 static struct mutex brightness_mutex;
4586 * ThinkPads can read brightness from two places: EC 0x31, or
4587 * CMOS NVRAM byte 0x5E, bits 0-3.
4589 * EC 0x31 has the following layout
4590 * Bit 7: unknown function
4591 * Bit 6: unknown function
4592 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4593 * Bit 4: must be set to zero to avoid problems
4594 * Bit 3-0: backlight brightness level
4596 * brightness_get_raw returns status data in the EC 0x31 layout
4598 static int brightness_get_raw(int *status)
4600 u8 lec = 0, lcmos = 0, level = 0;
4602 if (brightness_mode & 1) {
4603 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4604 return -EIO;
4605 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4607 if (brightness_mode & 2) {
4608 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4609 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4610 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4611 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4612 level = lcmos;
4615 if (brightness_mode == 3) {
4616 *status = lec; /* Prefer EC, CMOS is just a backing store */
4617 lec &= TP_EC_BACKLIGHT_LVLMSK;
4618 if (lec == lcmos)
4619 tp_warned.bright_cmos_ec_unsync = 0;
4620 else {
4621 if (!tp_warned.bright_cmos_ec_unsync) {
4622 printk(TPACPI_ERR
4623 "CMOS NVRAM (%u) and EC (%u) do not "
4624 "agree on display brightness level\n",
4625 (unsigned int) lcmos,
4626 (unsigned int) lec);
4627 tp_warned.bright_cmos_ec_unsync = 1;
4629 return -EIO;
4631 } else {
4632 *status = level;
4635 return 0;
4638 /* May return EINTR which can always be mapped to ERESTARTSYS */
4639 static int brightness_set(int value)
4641 int cmos_cmd, inc, i, res;
4642 int current_value;
4643 int command_bits;
4645 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4646 value < 0)
4647 return -EINVAL;
4649 res = mutex_lock_interruptible(&brightness_mutex);
4650 if (res < 0)
4651 return res;
4653 res = brightness_get_raw(&current_value);
4654 if (res < 0)
4655 goto errout;
4657 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4658 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4660 cmos_cmd = value > current_value ?
4661 TP_CMOS_BRIGHTNESS_UP :
4662 TP_CMOS_BRIGHTNESS_DOWN;
4663 inc = (value > current_value)? 1 : -1;
4665 res = 0;
4666 for (i = current_value; i != value; i += inc) {
4667 if ((brightness_mode & 2) &&
4668 issue_thinkpad_cmos_command(cmos_cmd)) {
4669 res = -EIO;
4670 goto errout;
4672 if ((brightness_mode & 1) &&
4673 !acpi_ec_write(TP_EC_BACKLIGHT,
4674 (i + inc) | command_bits)) {
4675 res = -EIO;
4676 goto errout;;
4680 errout:
4681 mutex_unlock(&brightness_mutex);
4682 return res;
4685 /* sysfs backlight class ----------------------------------------------- */
4687 static int brightness_update_status(struct backlight_device *bd)
4689 /* it is the backlight class's job (caller) to handle
4690 * EINTR and other errors properly */
4691 return brightness_set(
4692 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4693 bd->props.power == FB_BLANK_UNBLANK) ?
4694 bd->props.brightness : 0);
4697 static int brightness_get(struct backlight_device *bd)
4699 int status, res;
4701 res = brightness_get_raw(&status);
4702 if (res < 0)
4703 return 0; /* FIXME: teach backlight about error handling */
4705 return status & TP_EC_BACKLIGHT_LVLMSK;
4708 static struct backlight_ops ibm_backlight_data = {
4709 .get_brightness = brightness_get,
4710 .update_status = brightness_update_status,
4713 /* --------------------------------------------------------------------- */
4715 static int __init brightness_init(struct ibm_init_struct *iibm)
4717 int b;
4719 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4721 mutex_init(&brightness_mutex);
4724 * We always attempt to detect acpi support, so as to switch
4725 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4726 * going to publish a backlight interface
4728 b = tpacpi_check_std_acpi_brightness_support();
4729 if (b > 0) {
4730 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4731 printk(TPACPI_NOTICE
4732 "Lenovo BIOS switched to ACPI backlight "
4733 "control mode\n");
4735 if (brightness_enable > 1) {
4736 printk(TPACPI_NOTICE
4737 "standard ACPI backlight interface "
4738 "available, not loading native one...\n");
4739 return 1;
4743 if (!brightness_enable) {
4744 dbg_printk(TPACPI_DBG_INIT,
4745 "brightness support disabled by "
4746 "module parameter\n");
4747 return 1;
4750 if (b > 16) {
4751 printk(TPACPI_ERR
4752 "Unsupported brightness interface, "
4753 "please contact %s\n", TPACPI_MAIL);
4754 return 1;
4756 if (b == 16)
4757 tp_features.bright_16levels = 1;
4759 if (!brightness_mode) {
4760 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4761 brightness_mode = 2;
4762 else
4763 brightness_mode = 3;
4765 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4766 brightness_mode);
4769 if (brightness_mode > 3)
4770 return -EINVAL;
4772 if (brightness_get_raw(&b) < 0)
4773 return 1;
4775 if (tp_features.bright_16levels)
4776 printk(TPACPI_INFO
4777 "detected a 16-level brightness capable ThinkPad\n");
4779 ibm_backlight_device = backlight_device_register(
4780 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4781 &ibm_backlight_data);
4782 if (IS_ERR(ibm_backlight_device)) {
4783 printk(TPACPI_ERR "Could not register backlight device\n");
4784 return PTR_ERR(ibm_backlight_device);
4786 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4788 ibm_backlight_device->props.max_brightness =
4789 (tp_features.bright_16levels)? 15 : 7;
4790 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4791 backlight_update_status(ibm_backlight_device);
4793 return 0;
4796 static void brightness_exit(void)
4798 if (ibm_backlight_device) {
4799 vdbg_printk(TPACPI_DBG_EXIT,
4800 "calling backlight_device_unregister()\n");
4801 backlight_device_unregister(ibm_backlight_device);
4802 ibm_backlight_device = NULL;
4806 static int brightness_read(char *p)
4808 int len = 0;
4809 int level;
4811 level = brightness_get(NULL);
4812 if (level < 0) {
4813 len += sprintf(p + len, "level:\t\tunreadable\n");
4814 } else {
4815 len += sprintf(p + len, "level:\t\t%d\n", level);
4816 len += sprintf(p + len, "commands:\tup, down\n");
4817 len += sprintf(p + len, "commands:\tlevel <level>"
4818 " (<level> is 0-%d)\n",
4819 (tp_features.bright_16levels) ? 15 : 7);
4822 return len;
4825 static int brightness_write(char *buf)
4827 int level;
4828 int rc;
4829 char *cmd;
4830 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4832 level = brightness_get(NULL);
4833 if (level < 0)
4834 return level;
4836 while ((cmd = next_cmd(&buf))) {
4837 if (strlencmp(cmd, "up") == 0) {
4838 if (level < max_level)
4839 level++;
4840 } else if (strlencmp(cmd, "down") == 0) {
4841 if (level > 0)
4842 level--;
4843 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4844 level >= 0 && level <= max_level) {
4845 /* new level set */
4846 } else
4847 return -EINVAL;
4851 * Now we know what the final level should be, so we try to set it.
4852 * Doing it this way makes the syscall restartable in case of EINTR
4854 rc = brightness_set(level);
4855 return (rc == -EINTR)? ERESTARTSYS : rc;
4858 static struct ibm_struct brightness_driver_data = {
4859 .name = "brightness",
4860 .read = brightness_read,
4861 .write = brightness_write,
4862 .exit = brightness_exit,
4865 /*************************************************************************
4866 * Volume subdriver
4869 static int volume_offset = 0x30;
4871 static int volume_read(char *p)
4873 int len = 0;
4874 u8 level;
4876 if (!acpi_ec_read(volume_offset, &level)) {
4877 len += sprintf(p + len, "level:\t\tunreadable\n");
4878 } else {
4879 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4880 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4881 len += sprintf(p + len, "commands:\tup, down, mute\n");
4882 len += sprintf(p + len, "commands:\tlevel <level>"
4883 " (<level> is 0-15)\n");
4886 return len;
4889 static int volume_write(char *buf)
4891 int cmos_cmd, inc, i;
4892 u8 level, mute;
4893 int new_level, new_mute;
4894 char *cmd;
4896 while ((cmd = next_cmd(&buf))) {
4897 if (!acpi_ec_read(volume_offset, &level))
4898 return -EIO;
4899 new_mute = mute = level & 0x40;
4900 new_level = level = level & 0xf;
4902 if (strlencmp(cmd, "up") == 0) {
4903 if (mute)
4904 new_mute = 0;
4905 else
4906 new_level = level == 15 ? 15 : level + 1;
4907 } else if (strlencmp(cmd, "down") == 0) {
4908 if (mute)
4909 new_mute = 0;
4910 else
4911 new_level = level == 0 ? 0 : level - 1;
4912 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4913 new_level >= 0 && new_level <= 15) {
4914 /* new_level set */
4915 } else if (strlencmp(cmd, "mute") == 0) {
4916 new_mute = 0x40;
4917 } else
4918 return -EINVAL;
4920 if (new_level != level) {
4921 /* mute doesn't change */
4923 cmos_cmd = (new_level > level) ?
4924 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4925 inc = new_level > level ? 1 : -1;
4927 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4928 !acpi_ec_write(volume_offset, level)))
4929 return -EIO;
4931 for (i = level; i != new_level; i += inc)
4932 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4933 !acpi_ec_write(volume_offset, i + inc))
4934 return -EIO;
4936 if (mute &&
4937 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4938 !acpi_ec_write(volume_offset, new_level + mute))) {
4939 return -EIO;
4943 if (new_mute != mute) {
4944 /* level doesn't change */
4946 cmos_cmd = (new_mute) ?
4947 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4949 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4950 !acpi_ec_write(volume_offset, level + new_mute))
4951 return -EIO;
4955 return 0;
4958 static struct ibm_struct volume_driver_data = {
4959 .name = "volume",
4960 .read = volume_read,
4961 .write = volume_write,
4964 /*************************************************************************
4965 * Fan subdriver
4969 * FAN ACCESS MODES
4971 * TPACPI_FAN_RD_ACPI_GFAN:
4972 * ACPI GFAN method: returns fan level
4974 * see TPACPI_FAN_WR_ACPI_SFAN
4975 * EC 0x2f (HFSP) not available if GFAN exists
4977 * TPACPI_FAN_WR_ACPI_SFAN:
4978 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4980 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4981 * it for writing.
4983 * TPACPI_FAN_WR_TPEC:
4984 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4985 * Supported on almost all ThinkPads
4987 * Fan speed changes of any sort (including those caused by the
4988 * disengaged mode) are usually done slowly by the firmware as the
4989 * maximum ammount of fan duty cycle change per second seems to be
4990 * limited.
4992 * Reading is not available if GFAN exists.
4993 * Writing is not available if SFAN exists.
4995 * Bits
4996 * 7 automatic mode engaged;
4997 * (default operation mode of the ThinkPad)
4998 * fan level is ignored in this mode.
4999 * 6 full speed mode (takes precedence over bit 7);
5000 * not available on all thinkpads. May disable
5001 * the tachometer while the fan controller ramps up
5002 * the speed (which can take up to a few *minutes*).
5003 * Speeds up fan to 100% duty-cycle, which is far above
5004 * the standard RPM levels. It is not impossible that
5005 * it could cause hardware damage.
5006 * 5-3 unused in some models. Extra bits for fan level
5007 * in others, but still useless as all values above
5008 * 7 map to the same speed as level 7 in these models.
5009 * 2-0 fan level (0..7 usually)
5010 * 0x00 = stop
5011 * 0x07 = max (set when temperatures critical)
5012 * Some ThinkPads may have other levels, see
5013 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5015 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5016 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5017 * does so, its initial value is meaningless (0x07).
5019 * For firmware bugs, refer to:
5020 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5022 * ----
5024 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5025 * Main fan tachometer reading (in RPM)
5027 * This register is present on all ThinkPads with a new-style EC, and
5028 * it is known not to be present on the A21m/e, and T22, as there is
5029 * something else in offset 0x84 according to the ACPI DSDT. Other
5030 * ThinkPads from this same time period (and earlier) probably lack the
5031 * tachometer as well.
5033 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5034 * was never fixed by IBM to report the EC firmware version string
5035 * probably support the tachometer (like the early X models), so
5036 * detecting it is quite hard. We need more data to know for sure.
5038 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5039 * might result.
5041 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5042 * mode.
5044 * For firmware bugs, refer to:
5045 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5047 * TPACPI_FAN_WR_ACPI_FANS:
5048 * ThinkPad X31, X40, X41. Not available in the X60.
5050 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5051 * high speed. ACPI DSDT seems to map these three speeds to levels
5052 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5053 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5055 * The speeds are stored on handles
5056 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5058 * There are three default speed sets, acessible as handles:
5059 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5061 * ACPI DSDT switches which set is in use depending on various
5062 * factors.
5064 * TPACPI_FAN_WR_TPEC is also available and should be used to
5065 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5066 * but the ACPI tables just mention level 7.
5069 enum { /* Fan control constants */
5070 fan_status_offset = 0x2f, /* EC register 0x2f */
5071 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5072 * 0x84 must be read before 0x85 */
5074 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5075 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5077 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5080 enum fan_status_access_mode {
5081 TPACPI_FAN_NONE = 0, /* No fan status or control */
5082 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5083 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5086 enum fan_control_access_mode {
5087 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5088 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5089 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5090 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5093 enum fan_control_commands {
5094 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5095 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5096 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5097 * and also watchdog cmd */
5100 static int fan_control_allowed;
5102 static enum fan_status_access_mode fan_status_access_mode;
5103 static enum fan_control_access_mode fan_control_access_mode;
5104 static enum fan_control_commands fan_control_commands;
5106 static u8 fan_control_initial_status;
5107 static u8 fan_control_desired_level;
5108 static int fan_watchdog_maxinterval;
5110 static struct mutex fan_mutex;
5112 static void fan_watchdog_fire(struct work_struct *ignored);
5113 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5115 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5116 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5117 "\\FSPD", /* 600e/x, 770e, 770x */
5118 ); /* all others */
5119 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5120 "JFNS", /* 770x-JL */
5121 ); /* all others */
5124 * Call with fan_mutex held
5126 static void fan_update_desired_level(u8 status)
5128 if ((status &
5129 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5130 if (status > 7)
5131 fan_control_desired_level = 7;
5132 else
5133 fan_control_desired_level = status;
5137 static int fan_get_status(u8 *status)
5139 u8 s;
5141 /* TODO:
5142 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5144 switch (fan_status_access_mode) {
5145 case TPACPI_FAN_RD_ACPI_GFAN:
5146 /* 570, 600e/x, 770e, 770x */
5148 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5149 return -EIO;
5151 if (likely(status))
5152 *status = s & 0x07;
5154 break;
5156 case TPACPI_FAN_RD_TPEC:
5157 /* all except 570, 600e/x, 770e, 770x */
5158 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5159 return -EIO;
5161 if (likely(status))
5162 *status = s;
5164 break;
5166 default:
5167 return -ENXIO;
5170 return 0;
5173 static int fan_get_status_safe(u8 *status)
5175 int rc;
5176 u8 s;
5178 if (mutex_lock_interruptible(&fan_mutex))
5179 return -ERESTARTSYS;
5180 rc = fan_get_status(&s);
5181 if (!rc)
5182 fan_update_desired_level(s);
5183 mutex_unlock(&fan_mutex);
5185 if (status)
5186 *status = s;
5188 return rc;
5191 static int fan_get_speed(unsigned int *speed)
5193 u8 hi, lo;
5195 switch (fan_status_access_mode) {
5196 case TPACPI_FAN_RD_TPEC:
5197 /* all except 570, 600e/x, 770e, 770x */
5198 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5199 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5200 return -EIO;
5202 if (likely(speed))
5203 *speed = (hi << 8) | lo;
5205 break;
5207 default:
5208 return -ENXIO;
5211 return 0;
5214 static int fan_set_level(int level)
5216 if (!fan_control_allowed)
5217 return -EPERM;
5219 switch (fan_control_access_mode) {
5220 case TPACPI_FAN_WR_ACPI_SFAN:
5221 if (level >= 0 && level <= 7) {
5222 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5223 return -EIO;
5224 } else
5225 return -EINVAL;
5226 break;
5228 case TPACPI_FAN_WR_ACPI_FANS:
5229 case TPACPI_FAN_WR_TPEC:
5230 if ((level != TP_EC_FAN_AUTO) &&
5231 (level != TP_EC_FAN_FULLSPEED) &&
5232 ((level < 0) || (level > 7)))
5233 return -EINVAL;
5235 /* safety net should the EC not support AUTO
5236 * or FULLSPEED mode bits and just ignore them */
5237 if (level & TP_EC_FAN_FULLSPEED)
5238 level |= 7; /* safety min speed 7 */
5239 else if (level & TP_EC_FAN_AUTO)
5240 level |= 4; /* safety min speed 4 */
5242 if (!acpi_ec_write(fan_status_offset, level))
5243 return -EIO;
5244 else
5245 tp_features.fan_ctrl_status_undef = 0;
5246 break;
5248 default:
5249 return -ENXIO;
5251 return 0;
5254 static int fan_set_level_safe(int level)
5256 int rc;
5258 if (!fan_control_allowed)
5259 return -EPERM;
5261 if (mutex_lock_interruptible(&fan_mutex))
5262 return -ERESTARTSYS;
5264 if (level == TPACPI_FAN_LAST_LEVEL)
5265 level = fan_control_desired_level;
5267 rc = fan_set_level(level);
5268 if (!rc)
5269 fan_update_desired_level(level);
5271 mutex_unlock(&fan_mutex);
5272 return rc;
5275 static int fan_set_enable(void)
5277 u8 s;
5278 int rc;
5280 if (!fan_control_allowed)
5281 return -EPERM;
5283 if (mutex_lock_interruptible(&fan_mutex))
5284 return -ERESTARTSYS;
5286 switch (fan_control_access_mode) {
5287 case TPACPI_FAN_WR_ACPI_FANS:
5288 case TPACPI_FAN_WR_TPEC:
5289 rc = fan_get_status(&s);
5290 if (rc < 0)
5291 break;
5293 /* Don't go out of emergency fan mode */
5294 if (s != 7) {
5295 s &= 0x07;
5296 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5299 if (!acpi_ec_write(fan_status_offset, s))
5300 rc = -EIO;
5301 else {
5302 tp_features.fan_ctrl_status_undef = 0;
5303 rc = 0;
5305 break;
5307 case TPACPI_FAN_WR_ACPI_SFAN:
5308 rc = fan_get_status(&s);
5309 if (rc < 0)
5310 break;
5312 s &= 0x07;
5314 /* Set fan to at least level 4 */
5315 s |= 4;
5317 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5318 rc = -EIO;
5319 else
5320 rc = 0;
5321 break;
5323 default:
5324 rc = -ENXIO;
5327 mutex_unlock(&fan_mutex);
5328 return rc;
5331 static int fan_set_disable(void)
5333 int rc;
5335 if (!fan_control_allowed)
5336 return -EPERM;
5338 if (mutex_lock_interruptible(&fan_mutex))
5339 return -ERESTARTSYS;
5341 rc = 0;
5342 switch (fan_control_access_mode) {
5343 case TPACPI_FAN_WR_ACPI_FANS:
5344 case TPACPI_FAN_WR_TPEC:
5345 if (!acpi_ec_write(fan_status_offset, 0x00))
5346 rc = -EIO;
5347 else {
5348 fan_control_desired_level = 0;
5349 tp_features.fan_ctrl_status_undef = 0;
5351 break;
5353 case TPACPI_FAN_WR_ACPI_SFAN:
5354 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5355 rc = -EIO;
5356 else
5357 fan_control_desired_level = 0;
5358 break;
5360 default:
5361 rc = -ENXIO;
5365 mutex_unlock(&fan_mutex);
5366 return rc;
5369 static int fan_set_speed(int speed)
5371 int rc;
5373 if (!fan_control_allowed)
5374 return -EPERM;
5376 if (mutex_lock_interruptible(&fan_mutex))
5377 return -ERESTARTSYS;
5379 rc = 0;
5380 switch (fan_control_access_mode) {
5381 case TPACPI_FAN_WR_ACPI_FANS:
5382 if (speed >= 0 && speed <= 65535) {
5383 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5384 speed, speed, speed))
5385 rc = -EIO;
5386 } else
5387 rc = -EINVAL;
5388 break;
5390 default:
5391 rc = -ENXIO;
5394 mutex_unlock(&fan_mutex);
5395 return rc;
5398 static void fan_watchdog_reset(void)
5400 static int fan_watchdog_active;
5402 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5403 return;
5405 if (fan_watchdog_active)
5406 cancel_delayed_work(&fan_watchdog_task);
5408 if (fan_watchdog_maxinterval > 0 &&
5409 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5410 fan_watchdog_active = 1;
5411 if (!schedule_delayed_work(&fan_watchdog_task,
5412 msecs_to_jiffies(fan_watchdog_maxinterval
5413 * 1000))) {
5414 printk(TPACPI_ERR
5415 "failed to schedule the fan watchdog, "
5416 "watchdog will not trigger\n");
5418 } else
5419 fan_watchdog_active = 0;
5422 static void fan_watchdog_fire(struct work_struct *ignored)
5424 int rc;
5426 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5427 return;
5429 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5430 rc = fan_set_enable();
5431 if (rc < 0) {
5432 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5433 "will try again later...\n", -rc);
5434 /* reschedule for later */
5435 fan_watchdog_reset();
5440 * SYSFS fan layout: hwmon compatible (device)
5442 * pwm*_enable:
5443 * 0: "disengaged" mode
5444 * 1: manual mode
5445 * 2: native EC "auto" mode (recommended, hardware default)
5447 * pwm*: set speed in manual mode, ignored otherwise.
5448 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5449 * interpolation.
5451 * fan*_input: tachometer reading, RPM
5454 * SYSFS fan layout: extensions
5456 * fan_watchdog (driver):
5457 * fan watchdog interval in seconds, 0 disables (default), max 120
5460 /* sysfs fan pwm1_enable ----------------------------------------------- */
5461 static ssize_t fan_pwm1_enable_show(struct device *dev,
5462 struct device_attribute *attr,
5463 char *buf)
5465 int res, mode;
5466 u8 status;
5468 res = fan_get_status_safe(&status);
5469 if (res)
5470 return res;
5472 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5473 if (status != fan_control_initial_status) {
5474 tp_features.fan_ctrl_status_undef = 0;
5475 } else {
5476 /* Return most likely status. In fact, it
5477 * might be the only possible status */
5478 status = TP_EC_FAN_AUTO;
5482 if (status & TP_EC_FAN_FULLSPEED) {
5483 mode = 0;
5484 } else if (status & TP_EC_FAN_AUTO) {
5485 mode = 2;
5486 } else
5487 mode = 1;
5489 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5492 static ssize_t fan_pwm1_enable_store(struct device *dev,
5493 struct device_attribute *attr,
5494 const char *buf, size_t count)
5496 unsigned long t;
5497 int res, level;
5499 if (parse_strtoul(buf, 2, &t))
5500 return -EINVAL;
5502 switch (t) {
5503 case 0:
5504 level = TP_EC_FAN_FULLSPEED;
5505 break;
5506 case 1:
5507 level = TPACPI_FAN_LAST_LEVEL;
5508 break;
5509 case 2:
5510 level = TP_EC_FAN_AUTO;
5511 break;
5512 case 3:
5513 /* reserved for software-controlled auto mode */
5514 return -ENOSYS;
5515 default:
5516 return -EINVAL;
5519 res = fan_set_level_safe(level);
5520 if (res == -ENXIO)
5521 return -EINVAL;
5522 else if (res < 0)
5523 return res;
5525 fan_watchdog_reset();
5527 return count;
5530 static struct device_attribute dev_attr_fan_pwm1_enable =
5531 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5532 fan_pwm1_enable_show, fan_pwm1_enable_store);
5534 /* sysfs fan pwm1 ------------------------------------------------------ */
5535 static ssize_t fan_pwm1_show(struct device *dev,
5536 struct device_attribute *attr,
5537 char *buf)
5539 int res;
5540 u8 status;
5542 res = fan_get_status_safe(&status);
5543 if (res)
5544 return res;
5546 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5547 if (status != fan_control_initial_status) {
5548 tp_features.fan_ctrl_status_undef = 0;
5549 } else {
5550 status = TP_EC_FAN_AUTO;
5554 if ((status &
5555 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5556 status = fan_control_desired_level;
5558 if (status > 7)
5559 status = 7;
5561 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5564 static ssize_t fan_pwm1_store(struct device *dev,
5565 struct device_attribute *attr,
5566 const char *buf, size_t count)
5568 unsigned long s;
5569 int rc;
5570 u8 status, newlevel;
5572 if (parse_strtoul(buf, 255, &s))
5573 return -EINVAL;
5575 /* scale down from 0-255 to 0-7 */
5576 newlevel = (s >> 5) & 0x07;
5578 if (mutex_lock_interruptible(&fan_mutex))
5579 return -ERESTARTSYS;
5581 rc = fan_get_status(&status);
5582 if (!rc && (status &
5583 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5584 rc = fan_set_level(newlevel);
5585 if (rc == -ENXIO)
5586 rc = -EINVAL;
5587 else if (!rc) {
5588 fan_update_desired_level(newlevel);
5589 fan_watchdog_reset();
5593 mutex_unlock(&fan_mutex);
5594 return (rc)? rc : count;
5597 static struct device_attribute dev_attr_fan_pwm1 =
5598 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5599 fan_pwm1_show, fan_pwm1_store);
5601 /* sysfs fan fan1_input ------------------------------------------------ */
5602 static ssize_t fan_fan1_input_show(struct device *dev,
5603 struct device_attribute *attr,
5604 char *buf)
5606 int res;
5607 unsigned int speed;
5609 res = fan_get_speed(&speed);
5610 if (res < 0)
5611 return res;
5613 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5616 static struct device_attribute dev_attr_fan_fan1_input =
5617 __ATTR(fan1_input, S_IRUGO,
5618 fan_fan1_input_show, NULL);
5620 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5621 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5622 char *buf)
5624 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5627 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5628 const char *buf, size_t count)
5630 unsigned long t;
5632 if (parse_strtoul(buf, 120, &t))
5633 return -EINVAL;
5635 if (!fan_control_allowed)
5636 return -EPERM;
5638 fan_watchdog_maxinterval = t;
5639 fan_watchdog_reset();
5641 return count;
5644 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5645 fan_fan_watchdog_show, fan_fan_watchdog_store);
5647 /* --------------------------------------------------------------------- */
5648 static struct attribute *fan_attributes[] = {
5649 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5650 &dev_attr_fan_fan1_input.attr,
5651 NULL
5654 static const struct attribute_group fan_attr_group = {
5655 .attrs = fan_attributes,
5658 static int __init fan_init(struct ibm_init_struct *iibm)
5660 int rc;
5662 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5664 mutex_init(&fan_mutex);
5665 fan_status_access_mode = TPACPI_FAN_NONE;
5666 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5667 fan_control_commands = 0;
5668 fan_watchdog_maxinterval = 0;
5669 tp_features.fan_ctrl_status_undef = 0;
5670 fan_control_desired_level = 7;
5672 TPACPI_ACPIHANDLE_INIT(fans);
5673 TPACPI_ACPIHANDLE_INIT(gfan);
5674 TPACPI_ACPIHANDLE_INIT(sfan);
5676 if (gfan_handle) {
5677 /* 570, 600e/x, 770e, 770x */
5678 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5679 } else {
5680 /* all other ThinkPads: note that even old-style
5681 * ThinkPad ECs supports the fan control register */
5682 if (likely(acpi_ec_read(fan_status_offset,
5683 &fan_control_initial_status))) {
5684 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5686 /* In some ThinkPads, neither the EC nor the ACPI
5687 * DSDT initialize the fan status, and it ends up
5688 * being set to 0x07 when it *could* be either
5689 * 0x07 or 0x80.
5691 * Enable for TP-1Y (T43), TP-78 (R51e),
5692 * TP-76 (R52), TP-70 (T43, R52), which are known
5693 * to be buggy. */
5694 if (fan_control_initial_status == 0x07) {
5695 switch (thinkpad_id.ec_model) {
5696 case 0x5931: /* TP-1Y */
5697 case 0x3837: /* TP-78 */
5698 case 0x3637: /* TP-76 */
5699 case 0x3037: /* TP-70 */
5700 printk(TPACPI_NOTICE
5701 "fan_init: initial fan status "
5702 "is unknown, assuming it is "
5703 "in auto mode\n");
5704 tp_features.fan_ctrl_status_undef = 1;
5708 } else {
5709 printk(TPACPI_ERR
5710 "ThinkPad ACPI EC access misbehaving, "
5711 "fan status and control unavailable\n");
5712 return 1;
5716 if (sfan_handle) {
5717 /* 570, 770x-JL */
5718 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5719 fan_control_commands |=
5720 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5721 } else {
5722 if (!gfan_handle) {
5723 /* gfan without sfan means no fan control */
5724 /* all other models implement TP EC 0x2f control */
5726 if (fans_handle) {
5727 /* X31, X40, X41 */
5728 fan_control_access_mode =
5729 TPACPI_FAN_WR_ACPI_FANS;
5730 fan_control_commands |=
5731 TPACPI_FAN_CMD_SPEED |
5732 TPACPI_FAN_CMD_LEVEL |
5733 TPACPI_FAN_CMD_ENABLE;
5734 } else {
5735 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5736 fan_control_commands |=
5737 TPACPI_FAN_CMD_LEVEL |
5738 TPACPI_FAN_CMD_ENABLE;
5743 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5744 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5745 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5746 fan_status_access_mode, fan_control_access_mode);
5748 /* fan control master switch */
5749 if (!fan_control_allowed) {
5750 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5751 fan_control_commands = 0;
5752 dbg_printk(TPACPI_DBG_INIT,
5753 "fan control features disabled by parameter\n");
5756 /* update fan_control_desired_level */
5757 if (fan_status_access_mode != TPACPI_FAN_NONE)
5758 fan_get_status_safe(NULL);
5760 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5761 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5762 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5763 &fan_attr_group);
5764 if (!(rc < 0))
5765 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5766 &driver_attr_fan_watchdog);
5767 if (rc < 0)
5768 return rc;
5769 return 0;
5770 } else
5771 return 1;
5774 static void fan_exit(void)
5776 vdbg_printk(TPACPI_DBG_EXIT,
5777 "cancelling any pending fan watchdog tasks\n");
5779 /* FIXME: can we really do this unconditionally? */
5780 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5781 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5782 &driver_attr_fan_watchdog);
5784 cancel_delayed_work(&fan_watchdog_task);
5785 flush_scheduled_work();
5788 static int fan_read(char *p)
5790 int len = 0;
5791 int rc;
5792 u8 status;
5793 unsigned int speed = 0;
5795 switch (fan_status_access_mode) {
5796 case TPACPI_FAN_RD_ACPI_GFAN:
5797 /* 570, 600e/x, 770e, 770x */
5798 rc = fan_get_status_safe(&status);
5799 if (rc < 0)
5800 return rc;
5802 len += sprintf(p + len, "status:\t\t%s\n"
5803 "level:\t\t%d\n",
5804 (status != 0) ? "enabled" : "disabled", status);
5805 break;
5807 case TPACPI_FAN_RD_TPEC:
5808 /* all except 570, 600e/x, 770e, 770x */
5809 rc = fan_get_status_safe(&status);
5810 if (rc < 0)
5811 return rc;
5813 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5814 if (status != fan_control_initial_status)
5815 tp_features.fan_ctrl_status_undef = 0;
5816 else
5817 /* Return most likely status. In fact, it
5818 * might be the only possible status */
5819 status = TP_EC_FAN_AUTO;
5822 len += sprintf(p + len, "status:\t\t%s\n",
5823 (status != 0) ? "enabled" : "disabled");
5825 rc = fan_get_speed(&speed);
5826 if (rc < 0)
5827 return rc;
5829 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5831 if (status & TP_EC_FAN_FULLSPEED)
5832 /* Disengaged mode takes precedence */
5833 len += sprintf(p + len, "level:\t\tdisengaged\n");
5834 else if (status & TP_EC_FAN_AUTO)
5835 len += sprintf(p + len, "level:\t\tauto\n");
5836 else
5837 len += sprintf(p + len, "level:\t\t%d\n", status);
5838 break;
5840 case TPACPI_FAN_NONE:
5841 default:
5842 len += sprintf(p + len, "status:\t\tnot supported\n");
5845 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5846 len += sprintf(p + len, "commands:\tlevel <level>");
5848 switch (fan_control_access_mode) {
5849 case TPACPI_FAN_WR_ACPI_SFAN:
5850 len += sprintf(p + len, " (<level> is 0-7)\n");
5851 break;
5853 default:
5854 len += sprintf(p + len, " (<level> is 0-7, "
5855 "auto, disengaged, full-speed)\n");
5856 break;
5860 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5861 len += sprintf(p + len, "commands:\tenable, disable\n"
5862 "commands:\twatchdog <timeout> (<timeout> "
5863 "is 0 (off), 1-120 (seconds))\n");
5865 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5866 len += sprintf(p + len, "commands:\tspeed <speed>"
5867 " (<speed> is 0-65535)\n");
5869 return len;
5872 static int fan_write_cmd_level(const char *cmd, int *rc)
5874 int level;
5876 if (strlencmp(cmd, "level auto") == 0)
5877 level = TP_EC_FAN_AUTO;
5878 else if ((strlencmp(cmd, "level disengaged") == 0) |
5879 (strlencmp(cmd, "level full-speed") == 0))
5880 level = TP_EC_FAN_FULLSPEED;
5881 else if (sscanf(cmd, "level %d", &level) != 1)
5882 return 0;
5884 *rc = fan_set_level_safe(level);
5885 if (*rc == -ENXIO)
5886 printk(TPACPI_ERR "level command accepted for unsupported "
5887 "access mode %d", fan_control_access_mode);
5889 return 1;
5892 static int fan_write_cmd_enable(const char *cmd, int *rc)
5894 if (strlencmp(cmd, "enable") != 0)
5895 return 0;
5897 *rc = fan_set_enable();
5898 if (*rc == -ENXIO)
5899 printk(TPACPI_ERR "enable command accepted for unsupported "
5900 "access mode %d", fan_control_access_mode);
5902 return 1;
5905 static int fan_write_cmd_disable(const char *cmd, int *rc)
5907 if (strlencmp(cmd, "disable") != 0)
5908 return 0;
5910 *rc = fan_set_disable();
5911 if (*rc == -ENXIO)
5912 printk(TPACPI_ERR "disable command accepted for unsupported "
5913 "access mode %d", fan_control_access_mode);
5915 return 1;
5918 static int fan_write_cmd_speed(const char *cmd, int *rc)
5920 int speed;
5922 /* TODO:
5923 * Support speed <low> <medium> <high> ? */
5925 if (sscanf(cmd, "speed %d", &speed) != 1)
5926 return 0;
5928 *rc = fan_set_speed(speed);
5929 if (*rc == -ENXIO)
5930 printk(TPACPI_ERR "speed command accepted for unsupported "
5931 "access mode %d", fan_control_access_mode);
5933 return 1;
5936 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5938 int interval;
5940 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5941 return 0;
5943 if (interval < 0 || interval > 120)
5944 *rc = -EINVAL;
5945 else
5946 fan_watchdog_maxinterval = interval;
5948 return 1;
5951 static int fan_write(char *buf)
5953 char *cmd;
5954 int rc = 0;
5956 while (!rc && (cmd = next_cmd(&buf))) {
5957 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5958 fan_write_cmd_level(cmd, &rc)) &&
5959 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5960 (fan_write_cmd_enable(cmd, &rc) ||
5961 fan_write_cmd_disable(cmd, &rc) ||
5962 fan_write_cmd_watchdog(cmd, &rc))) &&
5963 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5964 fan_write_cmd_speed(cmd, &rc))
5966 rc = -EINVAL;
5967 else if (!rc)
5968 fan_watchdog_reset();
5971 return rc;
5974 static struct ibm_struct fan_driver_data = {
5975 .name = "fan",
5976 .read = fan_read,
5977 .write = fan_write,
5978 .exit = fan_exit,
5981 /****************************************************************************
5982 ****************************************************************************
5984 * Infrastructure
5986 ****************************************************************************
5987 ****************************************************************************/
5989 /* sysfs name ---------------------------------------------------------- */
5990 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5991 struct device_attribute *attr,
5992 char *buf)
5994 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5997 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5998 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6000 /* --------------------------------------------------------------------- */
6002 /* /proc support */
6003 static struct proc_dir_entry *proc_dir;
6006 * Module and infrastructure proble, init and exit handling
6009 static int force_load;
6011 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6012 static const char * __init str_supported(int is_supported)
6014 static char text_unsupported[] __initdata = "not supported";
6016 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6018 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6020 static void ibm_exit(struct ibm_struct *ibm)
6022 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6024 list_del_init(&ibm->all_drivers);
6026 if (ibm->flags.acpi_notify_installed) {
6027 dbg_printk(TPACPI_DBG_EXIT,
6028 "%s: acpi_remove_notify_handler\n", ibm->name);
6029 BUG_ON(!ibm->acpi);
6030 acpi_remove_notify_handler(*ibm->acpi->handle,
6031 ibm->acpi->type,
6032 dispatch_acpi_notify);
6033 ibm->flags.acpi_notify_installed = 0;
6034 ibm->flags.acpi_notify_installed = 0;
6037 if (ibm->flags.proc_created) {
6038 dbg_printk(TPACPI_DBG_EXIT,
6039 "%s: remove_proc_entry\n", ibm->name);
6040 remove_proc_entry(ibm->name, proc_dir);
6041 ibm->flags.proc_created = 0;
6044 if (ibm->flags.acpi_driver_registered) {
6045 dbg_printk(TPACPI_DBG_EXIT,
6046 "%s: acpi_bus_unregister_driver\n", ibm->name);
6047 BUG_ON(!ibm->acpi);
6048 acpi_bus_unregister_driver(ibm->acpi->driver);
6049 kfree(ibm->acpi->driver);
6050 ibm->acpi->driver = NULL;
6051 ibm->flags.acpi_driver_registered = 0;
6054 if (ibm->flags.init_called && ibm->exit) {
6055 ibm->exit();
6056 ibm->flags.init_called = 0;
6059 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6062 static int __init ibm_init(struct ibm_init_struct *iibm)
6064 int ret;
6065 struct ibm_struct *ibm = iibm->data;
6066 struct proc_dir_entry *entry;
6068 BUG_ON(ibm == NULL);
6070 INIT_LIST_HEAD(&ibm->all_drivers);
6072 if (ibm->flags.experimental && !experimental)
6073 return 0;
6075 dbg_printk(TPACPI_DBG_INIT,
6076 "probing for %s\n", ibm->name);
6078 if (iibm->init) {
6079 ret = iibm->init(iibm);
6080 if (ret > 0)
6081 return 0; /* probe failed */
6082 if (ret)
6083 return ret;
6085 ibm->flags.init_called = 1;
6088 if (ibm->acpi) {
6089 if (ibm->acpi->hid) {
6090 ret = register_tpacpi_subdriver(ibm);
6091 if (ret)
6092 goto err_out;
6095 if (ibm->acpi->notify) {
6096 ret = setup_acpi_notify(ibm);
6097 if (ret == -ENODEV) {
6098 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6099 ibm->name);
6100 ret = 0;
6101 goto err_out;
6103 if (ret < 0)
6104 goto err_out;
6108 dbg_printk(TPACPI_DBG_INIT,
6109 "%s installed\n", ibm->name);
6111 if (ibm->read) {
6112 entry = create_proc_entry(ibm->name,
6113 S_IFREG | S_IRUGO | S_IWUSR,
6114 proc_dir);
6115 if (!entry) {
6116 printk(TPACPI_ERR "unable to create proc entry %s\n",
6117 ibm->name);
6118 ret = -ENODEV;
6119 goto err_out;
6121 entry->owner = THIS_MODULE;
6122 entry->data = ibm;
6123 entry->read_proc = &dispatch_procfs_read;
6124 if (ibm->write)
6125 entry->write_proc = &dispatch_procfs_write;
6126 ibm->flags.proc_created = 1;
6129 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6131 return 0;
6133 err_out:
6134 dbg_printk(TPACPI_DBG_INIT,
6135 "%s: at error exit path with result %d\n",
6136 ibm->name, ret);
6138 ibm_exit(ibm);
6139 return (ret < 0)? ret : 0;
6142 /* Probing */
6144 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
6146 const struct dmi_device *dev = NULL;
6147 char ec_fw_string[18];
6149 if (!tp)
6150 return;
6152 memset(tp, 0, sizeof(*tp));
6154 if (dmi_name_in_vendors("IBM"))
6155 tp->vendor = PCI_VENDOR_ID_IBM;
6156 else if (dmi_name_in_vendors("LENOVO"))
6157 tp->vendor = PCI_VENDOR_ID_LENOVO;
6158 else
6159 return;
6161 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6162 GFP_KERNEL);
6163 if (!tp->bios_version_str)
6164 return;
6165 tp->bios_model = tp->bios_version_str[0]
6166 | (tp->bios_version_str[1] << 8);
6169 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6170 * X32 or newer, all Z series; Some models must have an
6171 * up-to-date BIOS or they will not be detected.
6173 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6175 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6176 if (sscanf(dev->name,
6177 "IBM ThinkPad Embedded Controller -[%17c",
6178 ec_fw_string) == 1) {
6179 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6180 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6182 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6183 tp->ec_model = ec_fw_string[0]
6184 | (ec_fw_string[1] << 8);
6185 break;
6189 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6190 GFP_KERNEL);
6191 if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6192 kfree(tp->model_str);
6193 tp->model_str = NULL;
6196 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6197 GFP_KERNEL);
6200 static int __init probe_for_thinkpad(void)
6202 int is_thinkpad;
6204 if (acpi_disabled)
6205 return -ENODEV;
6208 * Non-ancient models have better DMI tagging, but very old models
6209 * don't.
6211 is_thinkpad = (thinkpad_id.model_str != NULL);
6213 /* ec is required because many other handles are relative to it */
6214 TPACPI_ACPIHANDLE_INIT(ec);
6215 if (!ec_handle) {
6216 if (is_thinkpad)
6217 printk(TPACPI_ERR
6218 "Not yet supported ThinkPad detected!\n");
6219 return -ENODEV;
6223 * Risks a regression on very old machines, but reduces potential
6224 * false positives a damn great deal
6226 if (!is_thinkpad)
6227 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6229 if (!is_thinkpad && !force_load)
6230 return -ENODEV;
6232 return 0;
6236 /* Module init, exit, parameters */
6238 static struct ibm_init_struct ibms_init[] __initdata = {
6240 .init = thinkpad_acpi_driver_init,
6241 .data = &thinkpad_acpi_driver_data,
6244 .init = hotkey_init,
6245 .data = &hotkey_driver_data,
6248 .init = bluetooth_init,
6249 .data = &bluetooth_driver_data,
6252 .init = wan_init,
6253 .data = &wan_driver_data,
6255 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6257 .init = video_init,
6258 .data = &video_driver_data,
6260 #endif
6262 .init = light_init,
6263 .data = &light_driver_data,
6265 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6267 .init = dock_init,
6268 .data = &dock_driver_data[0],
6271 .init = dock_init2,
6272 .data = &dock_driver_data[1],
6274 #endif
6275 #ifdef CONFIG_THINKPAD_ACPI_BAY
6277 .init = bay_init,
6278 .data = &bay_driver_data,
6280 #endif
6282 .init = cmos_init,
6283 .data = &cmos_driver_data,
6286 .init = led_init,
6287 .data = &led_driver_data,
6290 .init = beep_init,
6291 .data = &beep_driver_data,
6294 .init = thermal_init,
6295 .data = &thermal_driver_data,
6298 .data = &ecdump_driver_data,
6301 .init = brightness_init,
6302 .data = &brightness_driver_data,
6305 .data = &volume_driver_data,
6308 .init = fan_init,
6309 .data = &fan_driver_data,
6313 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6315 unsigned int i;
6316 struct ibm_struct *ibm;
6318 if (!kp || !kp->name || !val)
6319 return -EINVAL;
6321 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6322 ibm = ibms_init[i].data;
6323 WARN_ON(ibm == NULL);
6325 if (!ibm || !ibm->name)
6326 continue;
6328 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6329 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6330 return -ENOSPC;
6331 strcpy(ibms_init[i].param, val);
6332 strcat(ibms_init[i].param, ",");
6333 return 0;
6337 return -EINVAL;
6340 module_param(experimental, int, 0);
6341 MODULE_PARM_DESC(experimental,
6342 "Enables experimental features when non-zero");
6344 module_param_named(debug, dbg_level, uint, 0);
6345 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6347 module_param(force_load, bool, 0);
6348 MODULE_PARM_DESC(force_load,
6349 "Attempts to load the driver even on a "
6350 "mis-identified ThinkPad when true");
6352 module_param_named(fan_control, fan_control_allowed, bool, 0);
6353 MODULE_PARM_DESC(fan_control,
6354 "Enables setting fan parameters features when true");
6356 module_param_named(brightness_mode, brightness_mode, int, 0);
6357 MODULE_PARM_DESC(brightness_mode,
6358 "Selects brightness control strategy: "
6359 "0=auto, 1=EC, 2=CMOS, 3=both");
6361 module_param(brightness_enable, uint, 0);
6362 MODULE_PARM_DESC(brightness_enable,
6363 "Enables backlight control when 1, disables when 0");
6365 module_param(hotkey_report_mode, uint, 0);
6366 MODULE_PARM_DESC(hotkey_report_mode,
6367 "used for backwards compatibility with userspace, "
6368 "see documentation");
6370 #define TPACPI_PARAM(feature) \
6371 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6372 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6373 "at module load, see documentation")
6375 TPACPI_PARAM(hotkey);
6376 TPACPI_PARAM(bluetooth);
6377 TPACPI_PARAM(video);
6378 TPACPI_PARAM(light);
6379 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6380 TPACPI_PARAM(dock);
6381 #endif
6382 #ifdef CONFIG_THINKPAD_ACPI_BAY
6383 TPACPI_PARAM(bay);
6384 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6385 TPACPI_PARAM(cmos);
6386 TPACPI_PARAM(led);
6387 TPACPI_PARAM(beep);
6388 TPACPI_PARAM(ecdump);
6389 TPACPI_PARAM(brightness);
6390 TPACPI_PARAM(volume);
6391 TPACPI_PARAM(fan);
6393 static void thinkpad_acpi_module_exit(void)
6395 struct ibm_struct *ibm, *itmp;
6397 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6399 list_for_each_entry_safe_reverse(ibm, itmp,
6400 &tpacpi_all_drivers,
6401 all_drivers) {
6402 ibm_exit(ibm);
6405 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6407 if (tpacpi_inputdev) {
6408 if (tp_features.input_device_registered)
6409 input_unregister_device(tpacpi_inputdev);
6410 else
6411 input_free_device(tpacpi_inputdev);
6414 if (tpacpi_hwmon)
6415 hwmon_device_unregister(tpacpi_hwmon);
6417 if (tp_features.sensors_pdev_attrs_registered)
6418 device_remove_file(&tpacpi_sensors_pdev->dev,
6419 &dev_attr_thinkpad_acpi_pdev_name);
6420 if (tpacpi_sensors_pdev)
6421 platform_device_unregister(tpacpi_sensors_pdev);
6422 if (tpacpi_pdev)
6423 platform_device_unregister(tpacpi_pdev);
6425 if (tp_features.sensors_pdrv_attrs_registered)
6426 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6427 if (tp_features.platform_drv_attrs_registered)
6428 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6430 if (tp_features.sensors_pdrv_registered)
6431 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6433 if (tp_features.platform_drv_registered)
6434 platform_driver_unregister(&tpacpi_pdriver);
6436 if (proc_dir)
6437 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6439 kfree(thinkpad_id.bios_version_str);
6440 kfree(thinkpad_id.ec_version_str);
6441 kfree(thinkpad_id.model_str);
6445 static int __init thinkpad_acpi_module_init(void)
6447 int ret, i;
6449 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6451 /* Parameter checking */
6452 if (hotkey_report_mode > 2)
6453 return -EINVAL;
6455 /* Driver-level probe */
6457 get_thinkpad_model_data(&thinkpad_id);
6458 ret = probe_for_thinkpad();
6459 if (ret) {
6460 thinkpad_acpi_module_exit();
6461 return ret;
6464 /* Driver initialization */
6466 TPACPI_ACPIHANDLE_INIT(ecrd);
6467 TPACPI_ACPIHANDLE_INIT(ecwr);
6469 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6470 if (!proc_dir) {
6471 printk(TPACPI_ERR
6472 "unable to create proc dir " TPACPI_PROC_DIR);
6473 thinkpad_acpi_module_exit();
6474 return -ENODEV;
6476 proc_dir->owner = THIS_MODULE;
6478 ret = platform_driver_register(&tpacpi_pdriver);
6479 if (ret) {
6480 printk(TPACPI_ERR
6481 "unable to register main platform driver\n");
6482 thinkpad_acpi_module_exit();
6483 return ret;
6485 tp_features.platform_drv_registered = 1;
6487 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6488 if (ret) {
6489 printk(TPACPI_ERR
6490 "unable to register hwmon platform driver\n");
6491 thinkpad_acpi_module_exit();
6492 return ret;
6494 tp_features.sensors_pdrv_registered = 1;
6496 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6497 if (!ret) {
6498 tp_features.platform_drv_attrs_registered = 1;
6499 ret = tpacpi_create_driver_attributes(
6500 &tpacpi_hwmon_pdriver.driver);
6502 if (ret) {
6503 printk(TPACPI_ERR
6504 "unable to create sysfs driver attributes\n");
6505 thinkpad_acpi_module_exit();
6506 return ret;
6508 tp_features.sensors_pdrv_attrs_registered = 1;
6511 /* Device initialization */
6512 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6513 NULL, 0);
6514 if (IS_ERR(tpacpi_pdev)) {
6515 ret = PTR_ERR(tpacpi_pdev);
6516 tpacpi_pdev = NULL;
6517 printk(TPACPI_ERR "unable to register platform device\n");
6518 thinkpad_acpi_module_exit();
6519 return ret;
6521 tpacpi_sensors_pdev = platform_device_register_simple(
6522 TPACPI_HWMON_DRVR_NAME,
6523 -1, NULL, 0);
6524 if (IS_ERR(tpacpi_sensors_pdev)) {
6525 ret = PTR_ERR(tpacpi_sensors_pdev);
6526 tpacpi_sensors_pdev = NULL;
6527 printk(TPACPI_ERR
6528 "unable to register hwmon platform device\n");
6529 thinkpad_acpi_module_exit();
6530 return ret;
6532 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6533 &dev_attr_thinkpad_acpi_pdev_name);
6534 if (ret) {
6535 printk(TPACPI_ERR
6536 "unable to create sysfs hwmon device attributes\n");
6537 thinkpad_acpi_module_exit();
6538 return ret;
6540 tp_features.sensors_pdev_attrs_registered = 1;
6541 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6542 if (IS_ERR(tpacpi_hwmon)) {
6543 ret = PTR_ERR(tpacpi_hwmon);
6544 tpacpi_hwmon = NULL;
6545 printk(TPACPI_ERR "unable to register hwmon device\n");
6546 thinkpad_acpi_module_exit();
6547 return ret;
6549 mutex_init(&tpacpi_inputdev_send_mutex);
6550 tpacpi_inputdev = input_allocate_device();
6551 if (!tpacpi_inputdev) {
6552 printk(TPACPI_ERR "unable to allocate input device\n");
6553 thinkpad_acpi_module_exit();
6554 return -ENOMEM;
6555 } else {
6556 /* Prepare input device, but don't register */
6557 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6558 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6559 tpacpi_inputdev->id.bustype = BUS_HOST;
6560 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6561 thinkpad_id.vendor :
6562 PCI_VENDOR_ID_IBM;
6563 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6564 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6566 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6567 ret = ibm_init(&ibms_init[i]);
6568 if (ret >= 0 && *ibms_init[i].param)
6569 ret = ibms_init[i].data->write(ibms_init[i].param);
6570 if (ret < 0) {
6571 thinkpad_acpi_module_exit();
6572 return ret;
6575 ret = input_register_device(tpacpi_inputdev);
6576 if (ret < 0) {
6577 printk(TPACPI_ERR "unable to register input device\n");
6578 thinkpad_acpi_module_exit();
6579 return ret;
6580 } else {
6581 tp_features.input_device_registered = 1;
6584 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6585 return 0;
6588 /* Please remove this in year 2009 */
6589 MODULE_ALIAS("ibm_acpi");
6591 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6594 * DMI matching for module autoloading
6596 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6597 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6599 * Only models listed in thinkwiki will be supported, so add yours
6600 * if it is not there yet.
6602 #define IBM_BIOS_MODULE_ALIAS(__type) \
6603 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6605 /* Non-ancient thinkpads */
6606 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6607 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6609 /* Ancient thinkpad BIOSes have to be identified by
6610 * BIOS type or model number, and there are far less
6611 * BIOS types than model numbers... */
6612 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6613 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6614 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6616 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6617 MODULE_DESCRIPTION(TPACPI_DESC);
6618 MODULE_VERSION(TPACPI_VERSION);
6619 MODULE_LICENSE("GPL");
6621 module_init(thinkpad_acpi_module_init);
6622 module_exit(thinkpad_acpi_module_exit);