ACPI: thinkpad-acpi: make the video output feature optional
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob9511b2654f9da2afb57116b9e9911bcc849c0cc9
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 <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
98 /* NVRAM bit masks */
99 enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
124 * Main driver
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
172 struct ibm_struct;
174 struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
184 struct ibm_struct {
185 char *name;
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
206 struct ibm_init_struct {
207 char param[32];
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219 #endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 light:1;
225 u32 light_status:1;
226 u32 bright_16levels:1;
227 u32 bright_acpimode:1;
228 u32 wan:1;
229 u32 fan_ctrl_status_undef:1;
230 u32 input_device_registered:1;
231 u32 platform_drv_registered:1;
232 u32 platform_drv_attrs_registered:1;
233 u32 sensors_pdrv_registered:1;
234 u32 sensors_pdrv_attrs_registered:1;
235 u32 sensors_pdev_attrs_registered:1;
236 u32 hotkey_poll_active:1;
237 } tp_features;
239 struct thinkpad_id_data {
240 unsigned int vendor; /* ThinkPad vendor:
241 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
243 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
244 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
246 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
247 u16 ec_model;
249 char *model_str;
251 static struct thinkpad_id_data thinkpad_id;
253 static enum {
254 TPACPI_LIFE_INIT = 0,
255 TPACPI_LIFE_RUNNING,
256 TPACPI_LIFE_EXITING,
257 } tpacpi_lifecycle;
259 static int experimental;
260 static u32 dbg_level;
262 /****************************************************************************
263 ****************************************************************************
265 * ACPI Helpers and device model
267 ****************************************************************************
268 ****************************************************************************/
270 /*************************************************************************
271 * ACPI basic handles
274 static acpi_handle root_handle;
276 #define TPACPI_HANDLE(object, parent, paths...) \
277 static acpi_handle object##_handle; \
278 static acpi_handle *object##_parent = &parent##_handle; \
279 static char *object##_path; \
280 static char *object##_paths[] = { paths }
282 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
283 "\\_SB.PCI.ISA.EC", /* 570 */
284 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
285 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
286 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
287 "\\_SB.PCI0.ICH3.EC0", /* R31 */
288 "\\_SB.PCI0.LPC.EC", /* all others */
291 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
292 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
294 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
295 /* T4x, X31, X40 */
296 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
297 "\\CMS", /* R40, R40e */
298 ); /* all others */
300 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
301 "^HKEY", /* R30, R31 */
302 "HKEY", /* all others */
303 ); /* 570 */
305 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
306 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
307 "\\_SB.PCI0.VID0", /* 770e */
308 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
309 "\\_SB.PCI0.AGP.VID", /* all others */
310 ); /* R30, R31 */
313 /*************************************************************************
314 * ACPI helpers
317 static int acpi_evalf(acpi_handle handle,
318 void *res, char *method, char *fmt, ...)
320 char *fmt0 = fmt;
321 struct acpi_object_list params;
322 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
323 struct acpi_buffer result, *resultp;
324 union acpi_object out_obj;
325 acpi_status status;
326 va_list ap;
327 char res_type;
328 int success;
329 int quiet;
331 if (!*fmt) {
332 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
333 return 0;
336 if (*fmt == 'q') {
337 quiet = 1;
338 fmt++;
339 } else
340 quiet = 0;
342 res_type = *(fmt++);
344 params.count = 0;
345 params.pointer = &in_objs[0];
347 va_start(ap, fmt);
348 while (*fmt) {
349 char c = *(fmt++);
350 switch (c) {
351 case 'd': /* int */
352 in_objs[params.count].integer.value = va_arg(ap, int);
353 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
354 break;
355 /* add more types as needed */
356 default:
357 printk(TPACPI_ERR "acpi_evalf() called "
358 "with invalid format character '%c'\n", c);
359 return 0;
362 va_end(ap);
364 if (res_type != 'v') {
365 result.length = sizeof(out_obj);
366 result.pointer = &out_obj;
367 resultp = &result;
368 } else
369 resultp = NULL;
371 status = acpi_evaluate_object(handle, method, &params, resultp);
373 switch (res_type) {
374 case 'd': /* int */
375 if (res)
376 *(int *)res = out_obj.integer.value;
377 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
378 break;
379 case 'v': /* void */
380 success = status == AE_OK;
381 break;
382 /* add more types as needed */
383 default:
384 printk(TPACPI_ERR "acpi_evalf() called "
385 "with invalid format character '%c'\n", res_type);
386 return 0;
389 if (!success && !quiet)
390 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
391 method, fmt0, status);
393 return success;
396 static int acpi_ec_read(int i, u8 *p)
398 int v;
400 if (ecrd_handle) {
401 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
402 return 0;
403 *p = v;
404 } else {
405 if (ec_read(i, p) < 0)
406 return 0;
409 return 1;
412 static int acpi_ec_write(int i, u8 v)
414 if (ecwr_handle) {
415 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
416 return 0;
417 } else {
418 if (ec_write(i, v) < 0)
419 return 0;
422 return 1;
425 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
426 static int _sta(acpi_handle handle)
428 int status;
430 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
431 status = 0;
433 return status;
435 #endif
437 static int issue_thinkpad_cmos_command(int cmos_cmd)
439 if (!cmos_handle)
440 return -ENXIO;
442 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
443 return -EIO;
445 return 0;
448 /*************************************************************************
449 * ACPI device model
452 #define TPACPI_ACPIHANDLE_INIT(object) \
453 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
454 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
456 static void drv_acpi_handle_init(char *name,
457 acpi_handle *handle, acpi_handle parent,
458 char **paths, int num_paths, char **path)
460 int i;
461 acpi_status status;
463 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
464 name);
466 for (i = 0; i < num_paths; i++) {
467 status = acpi_get_handle(parent, paths[i], handle);
468 if (ACPI_SUCCESS(status)) {
469 *path = paths[i];
470 dbg_printk(TPACPI_DBG_INIT,
471 "Found ACPI handle %s for %s\n",
472 *path, name);
473 return;
477 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
478 name);
479 *handle = NULL;
482 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
484 struct ibm_struct *ibm = data;
486 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
487 return;
489 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
490 return;
492 ibm->acpi->notify(ibm, event);
495 static int __init setup_acpi_notify(struct ibm_struct *ibm)
497 acpi_status status;
498 int rc;
500 BUG_ON(!ibm->acpi);
502 if (!*ibm->acpi->handle)
503 return 0;
505 vdbg_printk(TPACPI_DBG_INIT,
506 "setting up ACPI notify for %s\n", ibm->name);
508 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
509 if (rc < 0) {
510 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
511 ibm->name, rc);
512 return -ENODEV;
515 acpi_driver_data(ibm->acpi->device) = ibm;
516 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
517 TPACPI_ACPI_EVENT_PREFIX,
518 ibm->name);
520 status = acpi_install_notify_handler(*ibm->acpi->handle,
521 ibm->acpi->type, dispatch_acpi_notify, ibm);
522 if (ACPI_FAILURE(status)) {
523 if (status == AE_ALREADY_EXISTS) {
524 printk(TPACPI_NOTICE
525 "another device driver is already "
526 "handling %s events\n", ibm->name);
527 } else {
528 printk(TPACPI_ERR
529 "acpi_install_notify_handler(%s) failed: %d\n",
530 ibm->name, status);
532 return -ENODEV;
534 ibm->flags.acpi_notify_installed = 1;
535 return 0;
538 static int __init tpacpi_device_add(struct acpi_device *device)
540 return 0;
543 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
545 int rc;
547 dbg_printk(TPACPI_DBG_INIT,
548 "registering %s as an ACPI driver\n", ibm->name);
550 BUG_ON(!ibm->acpi);
552 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
553 if (!ibm->acpi->driver) {
554 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
555 return -ENOMEM;
558 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
559 ibm->acpi->driver->ids = ibm->acpi->hid;
561 ibm->acpi->driver->ops.add = &tpacpi_device_add;
563 rc = acpi_bus_register_driver(ibm->acpi->driver);
564 if (rc < 0) {
565 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
566 ibm->name, rc);
567 kfree(ibm->acpi->driver);
568 ibm->acpi->driver = NULL;
569 } else if (!rc)
570 ibm->flags.acpi_driver_registered = 1;
572 return rc;
576 /****************************************************************************
577 ****************************************************************************
579 * Procfs Helpers
581 ****************************************************************************
582 ****************************************************************************/
584 static int dispatch_procfs_read(char *page, char **start, off_t off,
585 int count, int *eof, void *data)
587 struct ibm_struct *ibm = data;
588 int len;
590 if (!ibm || !ibm->read)
591 return -EINVAL;
593 len = ibm->read(page);
594 if (len < 0)
595 return len;
597 if (len <= off + count)
598 *eof = 1;
599 *start = page + off;
600 len -= off;
601 if (len > count)
602 len = count;
603 if (len < 0)
604 len = 0;
606 return len;
609 static int dispatch_procfs_write(struct file *file,
610 const char __user *userbuf,
611 unsigned long count, void *data)
613 struct ibm_struct *ibm = data;
614 char *kernbuf;
615 int ret;
617 if (!ibm || !ibm->write)
618 return -EINVAL;
620 kernbuf = kmalloc(count + 2, GFP_KERNEL);
621 if (!kernbuf)
622 return -ENOMEM;
624 if (copy_from_user(kernbuf, userbuf, count)) {
625 kfree(kernbuf);
626 return -EFAULT;
629 kernbuf[count] = 0;
630 strcat(kernbuf, ",");
631 ret = ibm->write(kernbuf);
632 if (ret == 0)
633 ret = count;
635 kfree(kernbuf);
637 return ret;
640 static char *next_cmd(char **cmds)
642 char *start = *cmds;
643 char *end;
645 while ((end = strchr(start, ',')) && end == start)
646 start = end + 1;
648 if (!end)
649 return NULL;
651 *end = 0;
652 *cmds = end + 1;
653 return start;
657 /****************************************************************************
658 ****************************************************************************
660 * Device model: input, hwmon and platform
662 ****************************************************************************
663 ****************************************************************************/
665 static struct platform_device *tpacpi_pdev;
666 static struct platform_device *tpacpi_sensors_pdev;
667 static struct device *tpacpi_hwmon;
668 static struct input_dev *tpacpi_inputdev;
669 static struct mutex tpacpi_inputdev_send_mutex;
670 static LIST_HEAD(tpacpi_all_drivers);
672 static int tpacpi_suspend_handler(struct platform_device *pdev,
673 pm_message_t state)
675 struct ibm_struct *ibm, *itmp;
677 list_for_each_entry_safe(ibm, itmp,
678 &tpacpi_all_drivers,
679 all_drivers) {
680 if (ibm->suspend)
681 (ibm->suspend)(state);
684 return 0;
687 static int tpacpi_resume_handler(struct platform_device *pdev)
689 struct ibm_struct *ibm, *itmp;
691 list_for_each_entry_safe(ibm, itmp,
692 &tpacpi_all_drivers,
693 all_drivers) {
694 if (ibm->resume)
695 (ibm->resume)();
698 return 0;
701 static struct platform_driver tpacpi_pdriver = {
702 .driver = {
703 .name = TPACPI_DRVR_NAME,
704 .owner = THIS_MODULE,
706 .suspend = tpacpi_suspend_handler,
707 .resume = tpacpi_resume_handler,
710 static struct platform_driver tpacpi_hwmon_pdriver = {
711 .driver = {
712 .name = TPACPI_HWMON_DRVR_NAME,
713 .owner = THIS_MODULE,
717 /*************************************************************************
718 * sysfs support helpers
721 struct attribute_set {
722 unsigned int members, max_members;
723 struct attribute_group group;
726 struct attribute_set_obj {
727 struct attribute_set s;
728 struct attribute *a;
729 } __attribute__((packed));
731 static struct attribute_set *create_attr_set(unsigned int max_members,
732 const char *name)
734 struct attribute_set_obj *sobj;
736 if (max_members == 0)
737 return NULL;
739 /* Allocates space for implicit NULL at the end too */
740 sobj = kzalloc(sizeof(struct attribute_set_obj) +
741 max_members * sizeof(struct attribute *),
742 GFP_KERNEL);
743 if (!sobj)
744 return NULL;
745 sobj->s.max_members = max_members;
746 sobj->s.group.attrs = &sobj->a;
747 sobj->s.group.name = name;
749 return &sobj->s;
752 #define destroy_attr_set(_set) \
753 kfree(_set);
755 /* not multi-threaded safe, use it in a single thread per set */
756 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
758 if (!s || !attr)
759 return -EINVAL;
761 if (s->members >= s->max_members)
762 return -ENOMEM;
764 s->group.attrs[s->members] = attr;
765 s->members++;
767 return 0;
770 static int add_many_to_attr_set(struct attribute_set *s,
771 struct attribute **attr,
772 unsigned int count)
774 int i, res;
776 for (i = 0; i < count; i++) {
777 res = add_to_attr_set(s, attr[i]);
778 if (res)
779 return res;
782 return 0;
785 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
787 sysfs_remove_group(kobj, &s->group);
788 destroy_attr_set(s);
791 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
792 sysfs_create_group(_kobj, &_attr_set->group)
794 static int parse_strtoul(const char *buf,
795 unsigned long max, unsigned long *value)
797 char *endp;
799 while (*buf && isspace(*buf))
800 buf++;
801 *value = simple_strtoul(buf, &endp, 0);
802 while (*endp && isspace(*endp))
803 endp++;
804 if (*endp || *value > max)
805 return -EINVAL;
807 return 0;
810 /*************************************************************************
811 * thinkpad-acpi driver attributes
814 /* interface_version --------------------------------------------------- */
815 static ssize_t tpacpi_driver_interface_version_show(
816 struct device_driver *drv,
817 char *buf)
819 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
822 static DRIVER_ATTR(interface_version, S_IRUGO,
823 tpacpi_driver_interface_version_show, NULL);
825 /* debug_level --------------------------------------------------------- */
826 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
827 char *buf)
829 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
832 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
833 const char *buf, size_t count)
835 unsigned long t;
837 if (parse_strtoul(buf, 0xffff, &t))
838 return -EINVAL;
840 dbg_level = t;
842 return count;
845 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
846 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
848 /* version ------------------------------------------------------------- */
849 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
850 char *buf)
852 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
853 TPACPI_DESC, TPACPI_VERSION);
856 static DRIVER_ATTR(version, S_IRUGO,
857 tpacpi_driver_version_show, NULL);
859 /* --------------------------------------------------------------------- */
861 static struct driver_attribute *tpacpi_driver_attributes[] = {
862 &driver_attr_debug_level, &driver_attr_version,
863 &driver_attr_interface_version,
866 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
868 int i, res;
870 i = 0;
871 res = 0;
872 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
873 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
874 i++;
877 return res;
880 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
882 int i;
884 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
885 driver_remove_file(drv, tpacpi_driver_attributes[i]);
888 /****************************************************************************
889 ****************************************************************************
891 * Subdrivers
893 ****************************************************************************
894 ****************************************************************************/
896 /*************************************************************************
897 * thinkpad-acpi init subdriver
900 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
902 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
903 printk(TPACPI_INFO "%s\n", TPACPI_URL);
905 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
906 (thinkpad_id.bios_version_str) ?
907 thinkpad_id.bios_version_str : "unknown",
908 (thinkpad_id.ec_version_str) ?
909 thinkpad_id.ec_version_str : "unknown");
911 if (thinkpad_id.vendor && thinkpad_id.model_str)
912 printk(TPACPI_INFO "%s %s\n",
913 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
914 "IBM" : ((thinkpad_id.vendor ==
915 PCI_VENDOR_ID_LENOVO) ?
916 "Lenovo" : "Unknown vendor"),
917 thinkpad_id.model_str);
919 return 0;
922 static int thinkpad_acpi_driver_read(char *p)
924 int len = 0;
926 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
927 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
929 return len;
932 static struct ibm_struct thinkpad_acpi_driver_data = {
933 .name = "driver",
934 .read = thinkpad_acpi_driver_read,
937 /*************************************************************************
938 * Hotkey subdriver
941 enum { /* hot key scan codes (derived from ACPI DSDT) */
942 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
943 TP_ACPI_HOTKEYSCAN_FNF2,
944 TP_ACPI_HOTKEYSCAN_FNF3,
945 TP_ACPI_HOTKEYSCAN_FNF4,
946 TP_ACPI_HOTKEYSCAN_FNF5,
947 TP_ACPI_HOTKEYSCAN_FNF6,
948 TP_ACPI_HOTKEYSCAN_FNF7,
949 TP_ACPI_HOTKEYSCAN_FNF8,
950 TP_ACPI_HOTKEYSCAN_FNF9,
951 TP_ACPI_HOTKEYSCAN_FNF10,
952 TP_ACPI_HOTKEYSCAN_FNF11,
953 TP_ACPI_HOTKEYSCAN_FNF12,
954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
955 TP_ACPI_HOTKEYSCAN_FNINSERT,
956 TP_ACPI_HOTKEYSCAN_FNDELETE,
957 TP_ACPI_HOTKEYSCAN_FNHOME,
958 TP_ACPI_HOTKEYSCAN_FNEND,
959 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
961 TP_ACPI_HOTKEYSCAN_FNSPACE,
962 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
964 TP_ACPI_HOTKEYSCAN_MUTE,
965 TP_ACPI_HOTKEYSCAN_THINKPAD,
968 enum { /* Keys available through NVRAM polling */
969 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
970 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
973 enum { /* Positions of some of the keys in hotkey masks */
974 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
975 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
976 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
977 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
978 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
979 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
980 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
981 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
982 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
983 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
984 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
987 enum { /* NVRAM to ACPI HKEY group map */
988 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
989 TP_ACPI_HKEY_ZOOM_MASK |
990 TP_ACPI_HKEY_DISPSWTCH_MASK |
991 TP_ACPI_HKEY_HIBERNATE_MASK,
992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
993 TP_ACPI_HKEY_BRGHTDWN_MASK,
994 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
995 TP_ACPI_HKEY_VOLDWN_MASK |
996 TP_ACPI_HKEY_MUTE_MASK,
999 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1000 struct tp_nvram_state {
1001 u16 thinkpad_toggle:1;
1002 u16 zoom_toggle:1;
1003 u16 display_toggle:1;
1004 u16 thinklight_toggle:1;
1005 u16 hibernate_toggle:1;
1006 u16 displayexp_toggle:1;
1007 u16 display_state:1;
1008 u16 brightness_toggle:1;
1009 u16 volume_toggle:1;
1010 u16 mute:1;
1012 u8 brightness_level;
1013 u8 volume_level;
1016 static struct task_struct *tpacpi_hotkey_task;
1017 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1018 static int hotkey_poll_freq = 10; /* Hz */
1019 static struct mutex hotkey_thread_mutex;
1020 static struct mutex hotkey_thread_data_mutex;
1021 static unsigned int hotkey_config_change;
1023 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1025 #define hotkey_source_mask 0U
1027 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1029 static struct mutex hotkey_mutex;
1031 static enum { /* Reasons for waking up */
1032 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1033 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1034 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1035 } hotkey_wakeup_reason;
1037 static int hotkey_autosleep_ack;
1039 static int hotkey_orig_status;
1040 static u32 hotkey_orig_mask;
1041 static u32 hotkey_all_mask;
1042 static u32 hotkey_reserved_mask;
1043 static u32 hotkey_mask;
1045 static unsigned int hotkey_report_mode;
1047 static u16 *hotkey_keycode_map;
1049 static struct attribute_set *hotkey_dev_attributes;
1051 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1052 #define HOTKEY_CONFIG_CRITICAL_START \
1053 do { \
1054 mutex_lock(&hotkey_thread_data_mutex); \
1055 hotkey_config_change++; \
1056 } while (0);
1057 #define HOTKEY_CONFIG_CRITICAL_END \
1058 mutex_unlock(&hotkey_thread_data_mutex);
1059 #else
1060 #define HOTKEY_CONFIG_CRITICAL_START
1061 #define HOTKEY_CONFIG_CRITICAL_END
1062 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1064 static int hotkey_get_wlsw(int *status)
1066 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1067 return -EIO;
1068 return 0;
1072 * Call with hotkey_mutex held
1074 static int hotkey_mask_get(void)
1076 u32 m = 0;
1078 if (tp_features.hotkey_mask) {
1079 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1080 return -EIO;
1082 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1084 return 0;
1088 * Call with hotkey_mutex held
1090 static int hotkey_mask_set(u32 mask)
1092 int i;
1093 int rc = 0;
1095 if (tp_features.hotkey_mask) {
1096 HOTKEY_CONFIG_CRITICAL_START
1097 for (i = 0; i < 32; i++) {
1098 u32 m = 1 << i;
1099 /* enable in firmware mask only keys not in NVRAM
1100 * mode, but enable the key in the cached hotkey_mask
1101 * regardless of mode, or the key will end up
1102 * disabled by hotkey_mask_get() */
1103 if (!acpi_evalf(hkey_handle,
1104 NULL, "MHKM", "vdd", i + 1,
1105 !!((mask & ~hotkey_source_mask) & m))) {
1106 rc = -EIO;
1107 break;
1108 } else {
1109 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1112 HOTKEY_CONFIG_CRITICAL_END
1114 /* hotkey_mask_get must be called unconditionally below */
1115 if (!hotkey_mask_get() && !rc &&
1116 (hotkey_mask & ~hotkey_source_mask) !=
1117 (mask & ~hotkey_source_mask)) {
1118 printk(TPACPI_NOTICE
1119 "requested hot key mask 0x%08x, but "
1120 "firmware forced it to 0x%08x\n",
1121 mask, hotkey_mask);
1123 } else {
1124 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1125 HOTKEY_CONFIG_CRITICAL_START
1126 hotkey_mask = mask & hotkey_source_mask;
1127 HOTKEY_CONFIG_CRITICAL_END
1128 hotkey_mask_get();
1129 if (hotkey_mask != mask) {
1130 printk(TPACPI_NOTICE
1131 "requested hot key mask 0x%08x, "
1132 "forced to 0x%08x (NVRAM poll mask is "
1133 "0x%08x): no firmware mask support\n",
1134 mask, hotkey_mask, hotkey_source_mask);
1136 #else
1137 hotkey_mask_get();
1138 rc = -ENXIO;
1139 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1142 return rc;
1145 static int hotkey_status_get(int *status)
1147 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1148 return -EIO;
1150 return 0;
1153 static int hotkey_status_set(int status)
1155 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1156 return -EIO;
1158 return 0;
1161 static void tpacpi_input_send_radiosw(void)
1163 int wlsw;
1165 mutex_lock(&tpacpi_inputdev_send_mutex);
1167 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1168 input_report_switch(tpacpi_inputdev,
1169 SW_RADIO, !!wlsw);
1170 input_sync(tpacpi_inputdev);
1173 mutex_unlock(&tpacpi_inputdev_send_mutex);
1176 static void tpacpi_input_send_key(unsigned int scancode)
1178 unsigned int keycode;
1180 keycode = hotkey_keycode_map[scancode];
1182 if (keycode != KEY_RESERVED) {
1183 mutex_lock(&tpacpi_inputdev_send_mutex);
1185 input_report_key(tpacpi_inputdev, keycode, 1);
1186 if (keycode == KEY_UNKNOWN)
1187 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1188 scancode);
1189 input_sync(tpacpi_inputdev);
1191 input_report_key(tpacpi_inputdev, keycode, 0);
1192 if (keycode == KEY_UNKNOWN)
1193 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1194 scancode);
1195 input_sync(tpacpi_inputdev);
1197 mutex_unlock(&tpacpi_inputdev_send_mutex);
1201 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1202 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1204 static void tpacpi_hotkey_send_key(unsigned int scancode)
1206 tpacpi_input_send_key(scancode);
1207 if (hotkey_report_mode < 2) {
1208 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1209 0x80, 0x1001 + scancode);
1213 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1215 u8 d;
1217 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1218 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1219 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1220 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1221 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1222 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1224 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1225 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1226 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1228 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1229 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1230 n->displayexp_toggle =
1231 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1233 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1234 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1235 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1236 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1237 n->brightness_toggle =
1238 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1240 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1241 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1242 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1243 >> TP_NVRAM_POS_LEVEL_VOLUME;
1244 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1245 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1249 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1250 do { \
1251 if ((mask & (1 << __scancode)) && \
1252 oldn->__member != newn->__member) \
1253 tpacpi_hotkey_send_key(__scancode); \
1254 } while (0)
1256 #define TPACPI_MAY_SEND_KEY(__scancode) \
1257 do { if (mask & (1 << __scancode)) \
1258 tpacpi_hotkey_send_key(__scancode); } while (0)
1260 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1261 struct tp_nvram_state *newn,
1262 u32 mask)
1264 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1265 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1266 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1267 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1269 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1271 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1273 /* handle volume */
1274 if (oldn->volume_toggle != newn->volume_toggle) {
1275 if (oldn->mute != newn->mute) {
1276 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1278 if (oldn->volume_level > newn->volume_level) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1280 } else if (oldn->volume_level < newn->volume_level) {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1282 } else if (oldn->mute == newn->mute) {
1283 /* repeated key presses that didn't change state */
1284 if (newn->mute) {
1285 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1286 } else if (newn->volume_level != 0) {
1287 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1288 } else {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1294 /* handle brightness */
1295 if (oldn->brightness_toggle != newn->brightness_toggle) {
1296 if (oldn->brightness_level < newn->brightness_level) {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1298 } else if (oldn->brightness_level > newn->brightness_level) {
1299 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1300 } else {
1301 /* repeated key presses that didn't change state */
1302 if (newn->brightness_level != 0) {
1303 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1304 } else {
1305 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1311 #undef TPACPI_COMPARE_KEY
1312 #undef TPACPI_MAY_SEND_KEY
1314 static int hotkey_kthread(void *data)
1316 struct tp_nvram_state s[2];
1317 u32 mask;
1318 unsigned int si, so;
1319 unsigned long t;
1320 unsigned int change_detector, must_reset;
1322 mutex_lock(&hotkey_thread_mutex);
1324 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1325 goto exit;
1327 set_freezable();
1329 so = 0;
1330 si = 1;
1331 t = 0;
1333 /* Initial state for compares */
1334 mutex_lock(&hotkey_thread_data_mutex);
1335 change_detector = hotkey_config_change;
1336 mask = hotkey_source_mask & hotkey_mask;
1337 mutex_unlock(&hotkey_thread_data_mutex);
1338 hotkey_read_nvram(&s[so], mask);
1340 while (!kthread_should_stop() && hotkey_poll_freq) {
1341 if (t == 0)
1342 t = 1000/hotkey_poll_freq;
1343 t = msleep_interruptible(t);
1344 if (unlikely(kthread_should_stop()))
1345 break;
1346 must_reset = try_to_freeze();
1347 if (t > 0 && !must_reset)
1348 continue;
1350 mutex_lock(&hotkey_thread_data_mutex);
1351 if (must_reset || hotkey_config_change != change_detector) {
1352 /* forget old state on thaw or config change */
1353 si = so;
1354 t = 0;
1355 change_detector = hotkey_config_change;
1357 mask = hotkey_source_mask & hotkey_mask;
1358 mutex_unlock(&hotkey_thread_data_mutex);
1360 if (likely(mask)) {
1361 hotkey_read_nvram(&s[si], mask);
1362 if (likely(si != so)) {
1363 hotkey_compare_and_issue_event(&s[so], &s[si],
1364 mask);
1368 so = si;
1369 si ^= 1;
1372 exit:
1373 mutex_unlock(&hotkey_thread_mutex);
1374 return 0;
1377 static void hotkey_poll_stop_sync(void)
1379 if (tpacpi_hotkey_task) {
1380 if (frozen(tpacpi_hotkey_task) ||
1381 freezing(tpacpi_hotkey_task))
1382 thaw_process(tpacpi_hotkey_task);
1384 kthread_stop(tpacpi_hotkey_task);
1385 tpacpi_hotkey_task = NULL;
1386 mutex_lock(&hotkey_thread_mutex);
1387 /* at this point, the thread did exit */
1388 mutex_unlock(&hotkey_thread_mutex);
1392 /* call with hotkey_mutex held */
1393 static void hotkey_poll_setup(int may_warn)
1395 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1396 hotkey_poll_freq > 0 &&
1397 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1398 if (!tpacpi_hotkey_task) {
1399 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1400 NULL,
1401 TPACPI_FILE "d");
1402 if (IS_ERR(tpacpi_hotkey_task)) {
1403 tpacpi_hotkey_task = NULL;
1404 printk(TPACPI_ERR
1405 "could not create kernel thread "
1406 "for hotkey polling\n");
1409 } else {
1410 hotkey_poll_stop_sync();
1411 if (may_warn &&
1412 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1413 printk(TPACPI_NOTICE
1414 "hot keys 0x%08x require polling, "
1415 "which is currently disabled\n",
1416 hotkey_source_mask);
1421 static void hotkey_poll_setup_safe(int may_warn)
1423 mutex_lock(&hotkey_mutex);
1424 hotkey_poll_setup(may_warn);
1425 mutex_unlock(&hotkey_mutex);
1428 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1430 static void hotkey_poll_setup_safe(int __unused)
1434 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1436 static int hotkey_inputdev_open(struct input_dev *dev)
1438 switch (tpacpi_lifecycle) {
1439 case TPACPI_LIFE_INIT:
1441 * hotkey_init will call hotkey_poll_setup_safe
1442 * at the appropriate moment
1444 return 0;
1445 case TPACPI_LIFE_EXITING:
1446 return -EBUSY;
1447 case TPACPI_LIFE_RUNNING:
1448 hotkey_poll_setup_safe(0);
1449 return 0;
1452 /* Should only happen if tpacpi_lifecycle is corrupt */
1453 BUG();
1454 return -EBUSY;
1457 static void hotkey_inputdev_close(struct input_dev *dev)
1459 /* disable hotkey polling when possible */
1460 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1461 hotkey_poll_setup_safe(0);
1464 /* sysfs hotkey enable ------------------------------------------------- */
1465 static ssize_t hotkey_enable_show(struct device *dev,
1466 struct device_attribute *attr,
1467 char *buf)
1469 int res, status;
1471 res = hotkey_status_get(&status);
1472 if (res)
1473 return res;
1475 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1478 static ssize_t hotkey_enable_store(struct device *dev,
1479 struct device_attribute *attr,
1480 const char *buf, size_t count)
1482 unsigned long t;
1483 int res;
1485 if (parse_strtoul(buf, 1, &t))
1486 return -EINVAL;
1488 res = hotkey_status_set(t);
1490 return (res) ? res : count;
1493 static struct device_attribute dev_attr_hotkey_enable =
1494 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1495 hotkey_enable_show, hotkey_enable_store);
1497 /* sysfs hotkey mask --------------------------------------------------- */
1498 static ssize_t hotkey_mask_show(struct device *dev,
1499 struct device_attribute *attr,
1500 char *buf)
1502 int res;
1504 if (mutex_lock_interruptible(&hotkey_mutex))
1505 return -ERESTARTSYS;
1506 res = hotkey_mask_get();
1507 mutex_unlock(&hotkey_mutex);
1509 return (res)?
1510 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1513 static ssize_t hotkey_mask_store(struct device *dev,
1514 struct device_attribute *attr,
1515 const char *buf, size_t count)
1517 unsigned long t;
1518 int res;
1520 if (parse_strtoul(buf, 0xffffffffUL, &t))
1521 return -EINVAL;
1523 if (mutex_lock_interruptible(&hotkey_mutex))
1524 return -ERESTARTSYS;
1526 res = hotkey_mask_set(t);
1528 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1529 hotkey_poll_setup(1);
1530 #endif
1532 mutex_unlock(&hotkey_mutex);
1534 return (res) ? res : count;
1537 static struct device_attribute dev_attr_hotkey_mask =
1538 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1539 hotkey_mask_show, hotkey_mask_store);
1541 /* sysfs hotkey bios_enabled ------------------------------------------- */
1542 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1543 struct device_attribute *attr,
1544 char *buf)
1546 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1549 static struct device_attribute dev_attr_hotkey_bios_enabled =
1550 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1552 /* sysfs hotkey bios_mask ---------------------------------------------- */
1553 static ssize_t hotkey_bios_mask_show(struct device *dev,
1554 struct device_attribute *attr,
1555 char *buf)
1557 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1560 static struct device_attribute dev_attr_hotkey_bios_mask =
1561 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1563 /* sysfs hotkey all_mask ----------------------------------------------- */
1564 static ssize_t hotkey_all_mask_show(struct device *dev,
1565 struct device_attribute *attr,
1566 char *buf)
1568 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1569 hotkey_all_mask | hotkey_source_mask);
1572 static struct device_attribute dev_attr_hotkey_all_mask =
1573 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1575 /* sysfs hotkey recommended_mask --------------------------------------- */
1576 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1577 struct device_attribute *attr,
1578 char *buf)
1580 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1581 (hotkey_all_mask | hotkey_source_mask)
1582 & ~hotkey_reserved_mask);
1585 static struct device_attribute dev_attr_hotkey_recommended_mask =
1586 __ATTR(hotkey_recommended_mask, S_IRUGO,
1587 hotkey_recommended_mask_show, NULL);
1589 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1591 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1592 static ssize_t hotkey_source_mask_show(struct device *dev,
1593 struct device_attribute *attr,
1594 char *buf)
1596 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1599 static ssize_t hotkey_source_mask_store(struct device *dev,
1600 struct device_attribute *attr,
1601 const char *buf, size_t count)
1603 unsigned long t;
1605 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1606 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1607 return -EINVAL;
1609 if (mutex_lock_interruptible(&hotkey_mutex))
1610 return -ERESTARTSYS;
1612 HOTKEY_CONFIG_CRITICAL_START
1613 hotkey_source_mask = t;
1614 HOTKEY_CONFIG_CRITICAL_END
1616 hotkey_poll_setup(1);
1618 mutex_unlock(&hotkey_mutex);
1620 return count;
1623 static struct device_attribute dev_attr_hotkey_source_mask =
1624 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1625 hotkey_source_mask_show, hotkey_source_mask_store);
1627 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1628 static ssize_t hotkey_poll_freq_show(struct device *dev,
1629 struct device_attribute *attr,
1630 char *buf)
1632 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1635 static ssize_t hotkey_poll_freq_store(struct device *dev,
1636 struct device_attribute *attr,
1637 const char *buf, size_t count)
1639 unsigned long t;
1641 if (parse_strtoul(buf, 25, &t))
1642 return -EINVAL;
1644 if (mutex_lock_interruptible(&hotkey_mutex))
1645 return -ERESTARTSYS;
1647 hotkey_poll_freq = t;
1649 hotkey_poll_setup(1);
1650 mutex_unlock(&hotkey_mutex);
1652 return count;
1655 static struct device_attribute dev_attr_hotkey_poll_freq =
1656 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1657 hotkey_poll_freq_show, hotkey_poll_freq_store);
1659 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1661 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1662 static ssize_t hotkey_radio_sw_show(struct device *dev,
1663 struct device_attribute *attr,
1664 char *buf)
1666 int res, s;
1667 res = hotkey_get_wlsw(&s);
1668 if (res < 0)
1669 return res;
1671 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1674 static struct device_attribute dev_attr_hotkey_radio_sw =
1675 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1677 static void hotkey_radio_sw_notify_change(void)
1679 if (tp_features.hotkey_wlsw)
1680 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1681 "hotkey_radio_sw");
1684 /* sysfs hotkey report_mode -------------------------------------------- */
1685 static ssize_t hotkey_report_mode_show(struct device *dev,
1686 struct device_attribute *attr,
1687 char *buf)
1689 return snprintf(buf, PAGE_SIZE, "%d\n",
1690 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1693 static struct device_attribute dev_attr_hotkey_report_mode =
1694 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1696 /* sysfs wakeup reason (pollable) -------------------------------------- */
1697 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1698 struct device_attribute *attr,
1699 char *buf)
1701 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1704 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1705 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1707 void hotkey_wakeup_reason_notify_change(void)
1709 if (tp_features.hotkey_mask)
1710 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1711 "wakeup_reason");
1714 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1715 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1716 struct device_attribute *attr,
1717 char *buf)
1719 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1722 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1723 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1724 hotkey_wakeup_hotunplug_complete_show, NULL);
1726 void hotkey_wakeup_hotunplug_complete_notify_change(void)
1728 if (tp_features.hotkey_mask)
1729 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1730 "wakeup_hotunplug_complete");
1733 /* --------------------------------------------------------------------- */
1735 static struct attribute *hotkey_attributes[] __initdata = {
1736 &dev_attr_hotkey_enable.attr,
1737 &dev_attr_hotkey_bios_enabled.attr,
1738 &dev_attr_hotkey_report_mode.attr,
1739 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1740 &dev_attr_hotkey_mask.attr,
1741 &dev_attr_hotkey_all_mask.attr,
1742 &dev_attr_hotkey_recommended_mask.attr,
1743 &dev_attr_hotkey_source_mask.attr,
1744 &dev_attr_hotkey_poll_freq.attr,
1745 #endif
1748 static struct attribute *hotkey_mask_attributes[] __initdata = {
1749 &dev_attr_hotkey_bios_mask.attr,
1750 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1751 &dev_attr_hotkey_mask.attr,
1752 &dev_attr_hotkey_all_mask.attr,
1753 &dev_attr_hotkey_recommended_mask.attr,
1754 #endif
1755 &dev_attr_hotkey_wakeup_reason.attr,
1756 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1759 static int __init hotkey_init(struct ibm_init_struct *iibm)
1761 /* Requirements for changing the default keymaps:
1763 * 1. Many of the keys are mapped to KEY_RESERVED for very
1764 * good reasons. Do not change them unless you have deep
1765 * knowledge on the IBM and Lenovo ThinkPad firmware for
1766 * the various ThinkPad models. The driver behaves
1767 * differently for KEY_RESERVED: such keys have their
1768 * hot key mask *unset* in mask_recommended, and also
1769 * in the initial hot key mask programmed into the
1770 * firmware at driver load time, which means the firm-
1771 * ware may react very differently if you change them to
1772 * something else;
1774 * 2. You must be subscribed to the linux-thinkpad and
1775 * ibm-acpi-devel mailing lists, and you should read the
1776 * list archives since 2007 if you want to change the
1777 * keymaps. This requirement exists so that you will
1778 * know the past history of problems with the thinkpad-
1779 * acpi driver keymaps, and also that you will be
1780 * listening to any bug reports;
1782 * 3. Do not send thinkpad-acpi specific patches directly to
1783 * for merging, *ever*. Send them to the linux-acpi
1784 * mailinglist for comments. Merging is to be done only
1785 * through acpi-test and the ACPI maintainer.
1787 * If the above is too much to ask, don't change the keymap.
1788 * Ask the thinkpad-acpi maintainer to do it, instead.
1790 static u16 ibm_keycode_map[] __initdata = {
1791 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1792 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1793 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1794 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1796 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1797 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1798 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1799 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1801 /* brightness: firmware always reacts to them, unless
1802 * X.org did some tricks in the radeon BIOS scratch
1803 * registers of *some* models */
1804 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1805 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1807 /* Thinklight: firmware always react to it */
1808 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1810 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1811 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1813 /* Volume: firmware always react to it and reprograms
1814 * the built-in *extra* mixer. Never map it to control
1815 * another mixer by default. */
1816 KEY_RESERVED, /* 0x14: VOLUME UP */
1817 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1818 KEY_RESERVED, /* 0x16: MUTE */
1820 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1822 /* (assignments unknown, please report if found) */
1823 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1824 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1826 static u16 lenovo_keycode_map[] __initdata = {
1827 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1828 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1829 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1830 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1832 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1833 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1834 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1835 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1837 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1838 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1840 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1842 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1843 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1845 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1846 * react to it and reprograms the built-in *extra* mixer.
1847 * Never map it to control another mixer by default.
1849 * T60?, T61, R60?, R61: firmware and EC tries to send
1850 * these over the regular keyboard, so these are no-ops,
1851 * but there are still weird bugs re. MUTE, so do not
1852 * change unless you get test reports from all Lenovo
1853 * models. May cause the BIOS to interfere with the
1854 * HDA mixer.
1856 KEY_RESERVED, /* 0x14: VOLUME UP */
1857 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1858 KEY_RESERVED, /* 0x16: MUTE */
1860 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1862 /* (assignments unknown, please report if found) */
1863 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1864 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1867 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1868 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1869 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1871 int res, i;
1872 int status;
1873 int hkeyv;
1875 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1877 BUG_ON(!tpacpi_inputdev);
1878 BUG_ON(tpacpi_inputdev->open != NULL ||
1879 tpacpi_inputdev->close != NULL);
1881 TPACPI_ACPIHANDLE_INIT(hkey);
1882 mutex_init(&hotkey_mutex);
1884 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1885 mutex_init(&hotkey_thread_mutex);
1886 mutex_init(&hotkey_thread_data_mutex);
1887 #endif
1889 /* hotkey not supported on 570 */
1890 tp_features.hotkey = hkey_handle != NULL;
1892 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1893 str_supported(tp_features.hotkey));
1895 if (tp_features.hotkey) {
1896 hotkey_dev_attributes = create_attr_set(12, NULL);
1897 if (!hotkey_dev_attributes)
1898 return -ENOMEM;
1899 res = add_many_to_attr_set(hotkey_dev_attributes,
1900 hotkey_attributes,
1901 ARRAY_SIZE(hotkey_attributes));
1902 if (res)
1903 return res;
1905 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1906 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1907 for HKEY interface version 0x100 */
1908 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1909 if ((hkeyv >> 8) != 1) {
1910 printk(TPACPI_ERR "unknown version of the "
1911 "HKEY interface: 0x%x\n", hkeyv);
1912 printk(TPACPI_ERR "please report this to %s\n",
1913 TPACPI_MAIL);
1914 } else {
1916 * MHKV 0x100 in A31, R40, R40e,
1917 * T4x, X31, and later
1919 tp_features.hotkey_mask = 1;
1923 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1924 str_supported(tp_features.hotkey_mask));
1926 if (tp_features.hotkey_mask) {
1927 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1928 "MHKA", "qd")) {
1929 printk(TPACPI_ERR
1930 "missing MHKA handler, "
1931 "please report this to %s\n",
1932 TPACPI_MAIL);
1933 /* FN+F12, FN+F4, FN+F3 */
1934 hotkey_all_mask = 0x080cU;
1938 /* hotkey_source_mask *must* be zero for
1939 * the first hotkey_mask_get */
1940 res = hotkey_status_get(&hotkey_orig_status);
1941 if (!res && tp_features.hotkey_mask) {
1942 res = hotkey_mask_get();
1943 hotkey_orig_mask = hotkey_mask;
1944 if (!res) {
1945 res = add_many_to_attr_set(
1946 hotkey_dev_attributes,
1947 hotkey_mask_attributes,
1948 ARRAY_SIZE(hotkey_mask_attributes));
1952 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1953 if (tp_features.hotkey_mask) {
1954 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1955 & ~hotkey_all_mask;
1956 } else {
1957 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1960 vdbg_printk(TPACPI_DBG_INIT,
1961 "hotkey source mask 0x%08x, polling freq %d\n",
1962 hotkey_source_mask, hotkey_poll_freq);
1963 #endif
1965 /* Not all thinkpads have a hardware radio switch */
1966 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1967 tp_features.hotkey_wlsw = 1;
1968 printk(TPACPI_INFO
1969 "radio switch found; radios are %s\n",
1970 enabled(status, 0));
1971 res = add_to_attr_set(hotkey_dev_attributes,
1972 &dev_attr_hotkey_radio_sw.attr);
1975 if (!res)
1976 res = register_attr_set_with_sysfs(
1977 hotkey_dev_attributes,
1978 &tpacpi_pdev->dev.kobj);
1979 if (res)
1980 return res;
1982 /* Set up key map */
1984 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1985 GFP_KERNEL);
1986 if (!hotkey_keycode_map) {
1987 printk(TPACPI_ERR
1988 "failed to allocate memory for key map\n");
1989 return -ENOMEM;
1992 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1993 dbg_printk(TPACPI_DBG_INIT,
1994 "using Lenovo default hot key map\n");
1995 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1996 TPACPI_HOTKEY_MAP_SIZE);
1997 } else {
1998 dbg_printk(TPACPI_DBG_INIT,
1999 "using IBM default hot key map\n");
2000 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2001 TPACPI_HOTKEY_MAP_SIZE);
2004 /* Do not issue duplicate brightness change events to
2005 * userspace */
2006 if (tp_features.bright_acpimode) {
2007 printk(TPACPI_NOTICE
2008 "Brightness hot keys firmware issues ACPI "
2009 "video events\n");
2010 printk(TPACPI_NOTICE
2011 "Disabling brightness hot keys on event "
2012 "device keymap\n");
2013 hotkey_keycode_map[TP_ACPI_HOTKEYSCAN_FNHOME] =
2014 KEY_RESERVED;
2015 hotkey_keycode_map[TP_ACPI_HOTKEYSCAN_FNEND] =
2016 KEY_RESERVED;
2019 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2020 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2021 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2022 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2023 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2024 tpacpi_inputdev->keycode = hotkey_keycode_map;
2025 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2026 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2027 set_bit(hotkey_keycode_map[i],
2028 tpacpi_inputdev->keybit);
2029 } else {
2030 if (i < sizeof(hotkey_reserved_mask)*8)
2031 hotkey_reserved_mask |= 1 << i;
2035 if (tp_features.hotkey_wlsw) {
2036 set_bit(EV_SW, tpacpi_inputdev->evbit);
2037 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2040 dbg_printk(TPACPI_DBG_INIT,
2041 "enabling hot key handling\n");
2042 res = hotkey_status_set(1);
2043 if (res)
2044 return res;
2045 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2046 & ~hotkey_reserved_mask)
2047 | hotkey_orig_mask);
2048 if (res < 0 && res != -ENXIO)
2049 return res;
2051 dbg_printk(TPACPI_DBG_INIT,
2052 "legacy hot key reporting over procfs %s\n",
2053 (hotkey_report_mode < 2) ?
2054 "enabled" : "disabled");
2056 tpacpi_inputdev->open = &hotkey_inputdev_open;
2057 tpacpi_inputdev->close = &hotkey_inputdev_close;
2059 hotkey_poll_setup_safe(1);
2060 tpacpi_input_send_radiosw();
2063 return (tp_features.hotkey)? 0 : 1;
2066 static void hotkey_exit(void)
2068 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2069 hotkey_poll_stop_sync();
2070 #endif
2072 if (tp_features.hotkey) {
2073 dbg_printk(TPACPI_DBG_EXIT,
2074 "restoring original hot key mask\n");
2075 /* no short-circuit boolean operator below! */
2076 if ((hotkey_mask_set(hotkey_orig_mask) |
2077 hotkey_status_set(hotkey_orig_status)) != 0)
2078 printk(TPACPI_ERR
2079 "failed to restore hot key mask "
2080 "to BIOS defaults\n");
2083 if (hotkey_dev_attributes) {
2084 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2085 hotkey_dev_attributes = NULL;
2089 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2091 u32 hkey;
2092 unsigned int scancode;
2093 int send_acpi_ev;
2094 int ignore_acpi_ev;
2095 int unk_ev;
2097 if (event != 0x80) {
2098 printk(TPACPI_ERR
2099 "unknown HKEY notification event %d\n", event);
2100 /* forward it to userspace, maybe it knows how to handle it */
2101 acpi_bus_generate_netlink_event(
2102 ibm->acpi->device->pnp.device_class,
2103 ibm->acpi->device->dev.bus_id,
2104 event, 0);
2105 return;
2108 while (1) {
2109 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2110 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2111 return;
2114 if (hkey == 0) {
2115 /* queue empty */
2116 return;
2119 send_acpi_ev = 1;
2120 ignore_acpi_ev = 0;
2121 unk_ev = 0;
2123 switch (hkey >> 12) {
2124 case 1:
2125 /* 0x1000-0x1FFF: key presses */
2126 scancode = hkey & 0xfff;
2127 if (scancode > 0 && scancode < 0x21) {
2128 scancode--;
2129 if (!(hotkey_source_mask & (1 << scancode))) {
2130 tpacpi_input_send_key(scancode);
2131 send_acpi_ev = 0;
2132 } else {
2133 ignore_acpi_ev = 1;
2135 } else {
2136 unk_ev = 1;
2138 break;
2139 case 2:
2140 /* Wakeup reason */
2141 switch (hkey) {
2142 case 0x2304: /* suspend, undock */
2143 case 0x2404: /* hibernation, undock */
2144 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2145 ignore_acpi_ev = 1;
2146 break;
2147 case 0x2305: /* suspend, bay eject */
2148 case 0x2405: /* hibernation, bay eject */
2149 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2150 ignore_acpi_ev = 1;
2151 break;
2152 default:
2153 unk_ev = 1;
2155 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2156 printk(TPACPI_INFO
2157 "woke up due to a hot-unplug "
2158 "request...\n");
2159 hotkey_wakeup_reason_notify_change();
2161 break;
2162 case 3:
2163 /* bay-related wakeups */
2164 if (hkey == 0x3003) {
2165 hotkey_autosleep_ack = 1;
2166 printk(TPACPI_INFO
2167 "bay ejected\n");
2168 hotkey_wakeup_hotunplug_complete_notify_change();
2169 } else {
2170 unk_ev = 1;
2172 break;
2173 case 4:
2174 /* dock-related wakeups */
2175 if (hkey == 0x4003) {
2176 hotkey_autosleep_ack = 1;
2177 printk(TPACPI_INFO
2178 "undocked\n");
2179 hotkey_wakeup_hotunplug_complete_notify_change();
2180 } else {
2181 unk_ev = 1;
2183 break;
2184 case 5:
2185 /* 0x5000-0x5FFF: human interface helpers */
2186 switch (hkey) {
2187 case 0x5010: /* Lenovo new BIOS: brightness changed */
2188 case 0x5009: /* X61t: swivel up (tablet mode) */
2189 case 0x500a: /* X61t: swivel down (normal mode) */
2190 case 0x500b: /* X61t: tablet pen inserted into bay */
2191 case 0x500c: /* X61t: tablet pen removed from bay */
2192 break;
2193 case 0x5001:
2194 case 0x5002:
2195 /* LID switch events. Do not propagate */
2196 ignore_acpi_ev = 1;
2197 break;
2198 default:
2199 unk_ev = 1;
2201 break;
2202 case 7:
2203 /* 0x7000-0x7FFF: misc */
2204 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2205 tpacpi_input_send_radiosw();
2206 hotkey_radio_sw_notify_change();
2207 send_acpi_ev = 0;
2208 break;
2210 /* fallthrough to default */
2211 default:
2212 unk_ev = 1;
2214 if (unk_ev) {
2215 printk(TPACPI_NOTICE
2216 "unhandled HKEY event 0x%04x\n", hkey);
2219 /* Legacy events */
2220 if (!ignore_acpi_ev &&
2221 (send_acpi_ev || hotkey_report_mode < 2)) {
2222 acpi_bus_generate_proc_event(ibm->acpi->device,
2223 event, hkey);
2226 /* netlink events */
2227 if (!ignore_acpi_ev && send_acpi_ev) {
2228 acpi_bus_generate_netlink_event(
2229 ibm->acpi->device->pnp.device_class,
2230 ibm->acpi->device->dev.bus_id,
2231 event, hkey);
2236 static void hotkey_suspend(pm_message_t state)
2238 /* Do these on suspend, we get the events on early resume! */
2239 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2240 hotkey_autosleep_ack = 0;
2243 static void hotkey_resume(void)
2245 if (hotkey_mask_get())
2246 printk(TPACPI_ERR
2247 "error while trying to read hot key mask "
2248 "from firmware\n");
2249 tpacpi_input_send_radiosw();
2250 hotkey_radio_sw_notify_change();
2251 hotkey_wakeup_reason_notify_change();
2252 hotkey_wakeup_hotunplug_complete_notify_change();
2253 hotkey_poll_setup_safe(0);
2256 /* procfs -------------------------------------------------------------- */
2257 static int hotkey_read(char *p)
2259 int res, status;
2260 int len = 0;
2262 if (!tp_features.hotkey) {
2263 len += sprintf(p + len, "status:\t\tnot supported\n");
2264 return len;
2267 if (mutex_lock_interruptible(&hotkey_mutex))
2268 return -ERESTARTSYS;
2269 res = hotkey_status_get(&status);
2270 if (!res)
2271 res = hotkey_mask_get();
2272 mutex_unlock(&hotkey_mutex);
2273 if (res)
2274 return res;
2276 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2277 if (tp_features.hotkey_mask) {
2278 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2279 len += sprintf(p + len,
2280 "commands:\tenable, disable, reset, <mask>\n");
2281 } else {
2282 len += sprintf(p + len, "mask:\t\tnot supported\n");
2283 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2286 return len;
2289 static int hotkey_write(char *buf)
2291 int res, status;
2292 u32 mask;
2293 char *cmd;
2295 if (!tp_features.hotkey)
2296 return -ENODEV;
2298 if (mutex_lock_interruptible(&hotkey_mutex))
2299 return -ERESTARTSYS;
2301 status = -1;
2302 mask = hotkey_mask;
2304 res = 0;
2305 while ((cmd = next_cmd(&buf))) {
2306 if (strlencmp(cmd, "enable") == 0) {
2307 status = 1;
2308 } else if (strlencmp(cmd, "disable") == 0) {
2309 status = 0;
2310 } else if (strlencmp(cmd, "reset") == 0) {
2311 status = hotkey_orig_status;
2312 mask = hotkey_orig_mask;
2313 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2314 /* mask set */
2315 } else if (sscanf(cmd, "%x", &mask) == 1) {
2316 /* mask set */
2317 } else {
2318 res = -EINVAL;
2319 goto errexit;
2322 if (status != -1)
2323 res = hotkey_status_set(status);
2325 if (!res && mask != hotkey_mask)
2326 res = hotkey_mask_set(mask);
2328 errexit:
2329 mutex_unlock(&hotkey_mutex);
2330 return res;
2333 static const struct acpi_device_id ibm_htk_device_ids[] = {
2334 {TPACPI_ACPI_HKEY_HID, 0},
2335 {"", 0},
2338 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2339 .hid = ibm_htk_device_ids,
2340 .notify = hotkey_notify,
2341 .handle = &hkey_handle,
2342 .type = ACPI_DEVICE_NOTIFY,
2345 static struct ibm_struct hotkey_driver_data = {
2346 .name = "hotkey",
2347 .read = hotkey_read,
2348 .write = hotkey_write,
2349 .exit = hotkey_exit,
2350 .resume = hotkey_resume,
2351 .suspend = hotkey_suspend,
2352 .acpi = &ibm_hotkey_acpidriver,
2355 /*************************************************************************
2356 * Bluetooth subdriver
2359 enum {
2360 /* ACPI GBDC/SBDC bits */
2361 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2362 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2363 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2366 static int bluetooth_get_radiosw(void);
2367 static int bluetooth_set_radiosw(int radio_on);
2369 /* sysfs bluetooth enable ---------------------------------------------- */
2370 static ssize_t bluetooth_enable_show(struct device *dev,
2371 struct device_attribute *attr,
2372 char *buf)
2374 int status;
2376 status = bluetooth_get_radiosw();
2377 if (status < 0)
2378 return status;
2380 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2383 static ssize_t bluetooth_enable_store(struct device *dev,
2384 struct device_attribute *attr,
2385 const char *buf, size_t count)
2387 unsigned long t;
2388 int res;
2390 if (parse_strtoul(buf, 1, &t))
2391 return -EINVAL;
2393 res = bluetooth_set_radiosw(t);
2395 return (res) ? res : count;
2398 static struct device_attribute dev_attr_bluetooth_enable =
2399 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2400 bluetooth_enable_show, bluetooth_enable_store);
2402 /* --------------------------------------------------------------------- */
2404 static struct attribute *bluetooth_attributes[] = {
2405 &dev_attr_bluetooth_enable.attr,
2406 NULL
2409 static const struct attribute_group bluetooth_attr_group = {
2410 .attrs = bluetooth_attributes,
2413 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2415 int res;
2416 int status = 0;
2418 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2420 TPACPI_ACPIHANDLE_INIT(hkey);
2422 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2423 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2424 tp_features.bluetooth = hkey_handle &&
2425 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2427 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2428 str_supported(tp_features.bluetooth),
2429 status);
2431 if (tp_features.bluetooth) {
2432 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2433 /* no bluetooth hardware present in system */
2434 tp_features.bluetooth = 0;
2435 dbg_printk(TPACPI_DBG_INIT,
2436 "bluetooth hardware not installed\n");
2437 } else {
2438 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2439 &bluetooth_attr_group);
2440 if (res)
2441 return res;
2445 return (tp_features.bluetooth)? 0 : 1;
2448 static void bluetooth_exit(void)
2450 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2451 &bluetooth_attr_group);
2454 static int bluetooth_get_radiosw(void)
2456 int status;
2458 if (!tp_features.bluetooth)
2459 return -ENODEV;
2461 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2462 return -EIO;
2464 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2467 static int bluetooth_set_radiosw(int radio_on)
2469 int status;
2471 if (!tp_features.bluetooth)
2472 return -ENODEV;
2474 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2475 return -EIO;
2476 if (radio_on)
2477 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2478 else
2479 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2480 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2481 return -EIO;
2483 return 0;
2486 /* procfs -------------------------------------------------------------- */
2487 static int bluetooth_read(char *p)
2489 int len = 0;
2490 int status = bluetooth_get_radiosw();
2492 if (!tp_features.bluetooth)
2493 len += sprintf(p + len, "status:\t\tnot supported\n");
2494 else {
2495 len += sprintf(p + len, "status:\t\t%s\n",
2496 (status)? "enabled" : "disabled");
2497 len += sprintf(p + len, "commands:\tenable, disable\n");
2500 return len;
2503 static int bluetooth_write(char *buf)
2505 char *cmd;
2507 if (!tp_features.bluetooth)
2508 return -ENODEV;
2510 while ((cmd = next_cmd(&buf))) {
2511 if (strlencmp(cmd, "enable") == 0) {
2512 bluetooth_set_radiosw(1);
2513 } else if (strlencmp(cmd, "disable") == 0) {
2514 bluetooth_set_radiosw(0);
2515 } else
2516 return -EINVAL;
2519 return 0;
2522 static struct ibm_struct bluetooth_driver_data = {
2523 .name = "bluetooth",
2524 .read = bluetooth_read,
2525 .write = bluetooth_write,
2526 .exit = bluetooth_exit,
2529 /*************************************************************************
2530 * Wan subdriver
2533 enum {
2534 /* ACPI GWAN/SWAN bits */
2535 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2536 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2537 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2540 static int wan_get_radiosw(void);
2541 static int wan_set_radiosw(int radio_on);
2543 /* sysfs wan enable ---------------------------------------------------- */
2544 static ssize_t wan_enable_show(struct device *dev,
2545 struct device_attribute *attr,
2546 char *buf)
2548 int status;
2550 status = wan_get_radiosw();
2551 if (status < 0)
2552 return status;
2554 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2557 static ssize_t wan_enable_store(struct device *dev,
2558 struct device_attribute *attr,
2559 const char *buf, size_t count)
2561 unsigned long t;
2562 int res;
2564 if (parse_strtoul(buf, 1, &t))
2565 return -EINVAL;
2567 res = wan_set_radiosw(t);
2569 return (res) ? res : count;
2572 static struct device_attribute dev_attr_wan_enable =
2573 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2574 wan_enable_show, wan_enable_store);
2576 /* --------------------------------------------------------------------- */
2578 static struct attribute *wan_attributes[] = {
2579 &dev_attr_wan_enable.attr,
2580 NULL
2583 static const struct attribute_group wan_attr_group = {
2584 .attrs = wan_attributes,
2587 static int __init wan_init(struct ibm_init_struct *iibm)
2589 int res;
2590 int status = 0;
2592 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2594 TPACPI_ACPIHANDLE_INIT(hkey);
2596 tp_features.wan = hkey_handle &&
2597 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2599 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2600 str_supported(tp_features.wan),
2601 status);
2603 if (tp_features.wan) {
2604 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2605 /* no wan hardware present in system */
2606 tp_features.wan = 0;
2607 dbg_printk(TPACPI_DBG_INIT,
2608 "wan hardware not installed\n");
2609 } else {
2610 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2611 &wan_attr_group);
2612 if (res)
2613 return res;
2617 return (tp_features.wan)? 0 : 1;
2620 static void wan_exit(void)
2622 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2623 &wan_attr_group);
2626 static int wan_get_radiosw(void)
2628 int status;
2630 if (!tp_features.wan)
2631 return -ENODEV;
2633 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2634 return -EIO;
2636 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2639 static int wan_set_radiosw(int radio_on)
2641 int status;
2643 if (!tp_features.wan)
2644 return -ENODEV;
2646 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2647 return -EIO;
2648 if (radio_on)
2649 status |= TP_ACPI_WANCARD_RADIOSSW;
2650 else
2651 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2652 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2653 return -EIO;
2655 return 0;
2658 /* procfs -------------------------------------------------------------- */
2659 static int wan_read(char *p)
2661 int len = 0;
2662 int status = wan_get_radiosw();
2664 if (!tp_features.wan)
2665 len += sprintf(p + len, "status:\t\tnot supported\n");
2666 else {
2667 len += sprintf(p + len, "status:\t\t%s\n",
2668 (status)? "enabled" : "disabled");
2669 len += sprintf(p + len, "commands:\tenable, disable\n");
2672 return len;
2675 static int wan_write(char *buf)
2677 char *cmd;
2679 if (!tp_features.wan)
2680 return -ENODEV;
2682 while ((cmd = next_cmd(&buf))) {
2683 if (strlencmp(cmd, "enable") == 0) {
2684 wan_set_radiosw(1);
2685 } else if (strlencmp(cmd, "disable") == 0) {
2686 wan_set_radiosw(0);
2687 } else
2688 return -EINVAL;
2691 return 0;
2694 static struct ibm_struct wan_driver_data = {
2695 .name = "wan",
2696 .read = wan_read,
2697 .write = wan_write,
2698 .exit = wan_exit,
2699 .flags.experimental = 1,
2702 /*************************************************************************
2703 * Video subdriver
2706 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2708 enum video_access_mode {
2709 TPACPI_VIDEO_NONE = 0,
2710 TPACPI_VIDEO_570, /* 570 */
2711 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2712 TPACPI_VIDEO_NEW, /* all others */
2715 enum { /* video status flags, based on VIDEO_570 */
2716 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2717 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2718 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2721 enum { /* TPACPI_VIDEO_570 constants */
2722 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2723 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2724 * video_status_flags */
2725 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2726 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2729 static enum video_access_mode video_supported;
2730 static int video_orig_autosw;
2732 static int video_autosw_get(void);
2733 static int video_autosw_set(int enable);
2735 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2737 static int __init video_init(struct ibm_init_struct *iibm)
2739 int ivga;
2741 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2743 TPACPI_ACPIHANDLE_INIT(vid);
2744 TPACPI_ACPIHANDLE_INIT(vid2);
2746 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2747 /* G41, assume IVGA doesn't change */
2748 vid_handle = vid2_handle;
2750 if (!vid_handle)
2751 /* video switching not supported on R30, R31 */
2752 video_supported = TPACPI_VIDEO_NONE;
2753 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2754 /* 570 */
2755 video_supported = TPACPI_VIDEO_570;
2756 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2757 /* 600e/x, 770e, 770x */
2758 video_supported = TPACPI_VIDEO_770;
2759 else
2760 /* all others */
2761 video_supported = TPACPI_VIDEO_NEW;
2763 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2764 str_supported(video_supported != TPACPI_VIDEO_NONE),
2765 video_supported);
2767 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2770 static void video_exit(void)
2772 dbg_printk(TPACPI_DBG_EXIT,
2773 "restoring original video autoswitch mode\n");
2774 if (video_autosw_set(video_orig_autosw))
2775 printk(TPACPI_ERR "error while trying to restore original "
2776 "video autoswitch mode\n");
2779 static int video_outputsw_get(void)
2781 int status = 0;
2782 int i;
2784 switch (video_supported) {
2785 case TPACPI_VIDEO_570:
2786 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2787 TP_ACPI_VIDEO_570_PHSCMD))
2788 return -EIO;
2789 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2790 break;
2791 case TPACPI_VIDEO_770:
2792 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2793 return -EIO;
2794 if (i)
2795 status |= TP_ACPI_VIDEO_S_LCD;
2796 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2797 return -EIO;
2798 if (i)
2799 status |= TP_ACPI_VIDEO_S_CRT;
2800 break;
2801 case TPACPI_VIDEO_NEW:
2802 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2803 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2804 return -EIO;
2805 if (i)
2806 status |= TP_ACPI_VIDEO_S_CRT;
2808 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2809 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2810 return -EIO;
2811 if (i)
2812 status |= TP_ACPI_VIDEO_S_LCD;
2813 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2814 return -EIO;
2815 if (i)
2816 status |= TP_ACPI_VIDEO_S_DVI;
2817 break;
2818 default:
2819 return -ENOSYS;
2822 return status;
2825 static int video_outputsw_set(int status)
2827 int autosw;
2828 int res = 0;
2830 switch (video_supported) {
2831 case TPACPI_VIDEO_570:
2832 res = acpi_evalf(NULL, NULL,
2833 "\\_SB.PHS2", "vdd",
2834 TP_ACPI_VIDEO_570_PHS2CMD,
2835 status | TP_ACPI_VIDEO_570_PHS2SET);
2836 break;
2837 case TPACPI_VIDEO_770:
2838 autosw = video_autosw_get();
2839 if (autosw < 0)
2840 return autosw;
2842 res = video_autosw_set(1);
2843 if (res)
2844 return res;
2845 res = acpi_evalf(vid_handle, NULL,
2846 "ASWT", "vdd", status * 0x100, 0);
2847 if (!autosw && video_autosw_set(autosw)) {
2848 printk(TPACPI_ERR
2849 "video auto-switch left enabled due to error\n");
2850 return -EIO;
2852 break;
2853 case TPACPI_VIDEO_NEW:
2854 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2855 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2856 break;
2857 default:
2858 return -ENOSYS;
2861 return (res)? 0 : -EIO;
2864 static int video_autosw_get(void)
2866 int autosw = 0;
2868 switch (video_supported) {
2869 case TPACPI_VIDEO_570:
2870 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2871 return -EIO;
2872 break;
2873 case TPACPI_VIDEO_770:
2874 case TPACPI_VIDEO_NEW:
2875 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2876 return -EIO;
2877 break;
2878 default:
2879 return -ENOSYS;
2882 return autosw & 1;
2885 static int video_autosw_set(int enable)
2887 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2888 return -EIO;
2889 return 0;
2892 static int video_outputsw_cycle(void)
2894 int autosw = video_autosw_get();
2895 int res;
2897 if (autosw < 0)
2898 return autosw;
2900 switch (video_supported) {
2901 case TPACPI_VIDEO_570:
2902 res = video_autosw_set(1);
2903 if (res)
2904 return res;
2905 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2906 break;
2907 case TPACPI_VIDEO_770:
2908 case TPACPI_VIDEO_NEW:
2909 res = video_autosw_set(1);
2910 if (res)
2911 return res;
2912 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2913 break;
2914 default:
2915 return -ENOSYS;
2917 if (!autosw && video_autosw_set(autosw)) {
2918 printk(TPACPI_ERR
2919 "video auto-switch left enabled due to error\n");
2920 return -EIO;
2923 return (res)? 0 : -EIO;
2926 static int video_expand_toggle(void)
2928 switch (video_supported) {
2929 case TPACPI_VIDEO_570:
2930 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2931 0 : -EIO;
2932 case TPACPI_VIDEO_770:
2933 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2934 0 : -EIO;
2935 case TPACPI_VIDEO_NEW:
2936 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2937 0 : -EIO;
2938 default:
2939 return -ENOSYS;
2941 /* not reached */
2944 static int video_read(char *p)
2946 int status, autosw;
2947 int len = 0;
2949 if (video_supported == TPACPI_VIDEO_NONE) {
2950 len += sprintf(p + len, "status:\t\tnot supported\n");
2951 return len;
2954 status = video_outputsw_get();
2955 if (status < 0)
2956 return status;
2958 autosw = video_autosw_get();
2959 if (autosw < 0)
2960 return autosw;
2962 len += sprintf(p + len, "status:\t\tsupported\n");
2963 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2964 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2965 if (video_supported == TPACPI_VIDEO_NEW)
2966 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2967 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2968 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2969 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2970 if (video_supported == TPACPI_VIDEO_NEW)
2971 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2972 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2973 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2975 return len;
2978 static int video_write(char *buf)
2980 char *cmd;
2981 int enable, disable, status;
2982 int res;
2984 if (video_supported == TPACPI_VIDEO_NONE)
2985 return -ENODEV;
2987 enable = 0;
2988 disable = 0;
2990 while ((cmd = next_cmd(&buf))) {
2991 if (strlencmp(cmd, "lcd_enable") == 0) {
2992 enable |= TP_ACPI_VIDEO_S_LCD;
2993 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2994 disable |= TP_ACPI_VIDEO_S_LCD;
2995 } else if (strlencmp(cmd, "crt_enable") == 0) {
2996 enable |= TP_ACPI_VIDEO_S_CRT;
2997 } else if (strlencmp(cmd, "crt_disable") == 0) {
2998 disable |= TP_ACPI_VIDEO_S_CRT;
2999 } else if (video_supported == TPACPI_VIDEO_NEW &&
3000 strlencmp(cmd, "dvi_enable") == 0) {
3001 enable |= TP_ACPI_VIDEO_S_DVI;
3002 } else if (video_supported == TPACPI_VIDEO_NEW &&
3003 strlencmp(cmd, "dvi_disable") == 0) {
3004 disable |= TP_ACPI_VIDEO_S_DVI;
3005 } else if (strlencmp(cmd, "auto_enable") == 0) {
3006 res = video_autosw_set(1);
3007 if (res)
3008 return res;
3009 } else if (strlencmp(cmd, "auto_disable") == 0) {
3010 res = video_autosw_set(0);
3011 if (res)
3012 return res;
3013 } else if (strlencmp(cmd, "video_switch") == 0) {
3014 res = video_outputsw_cycle();
3015 if (res)
3016 return res;
3017 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3018 res = video_expand_toggle();
3019 if (res)
3020 return res;
3021 } else
3022 return -EINVAL;
3025 if (enable || disable) {
3026 status = video_outputsw_get();
3027 if (status < 0)
3028 return status;
3029 res = video_outputsw_set((status & ~disable) | enable);
3030 if (res)
3031 return res;
3034 return 0;
3037 static struct ibm_struct video_driver_data = {
3038 .name = "video",
3039 .read = video_read,
3040 .write = video_write,
3041 .exit = video_exit,
3044 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3046 /*************************************************************************
3047 * Light (thinklight) subdriver
3050 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3051 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3053 static int __init light_init(struct ibm_init_struct *iibm)
3055 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3057 TPACPI_ACPIHANDLE_INIT(ledb);
3058 TPACPI_ACPIHANDLE_INIT(lght);
3059 TPACPI_ACPIHANDLE_INIT(cmos);
3061 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3062 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3064 if (tp_features.light)
3065 /* light status not supported on
3066 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3067 tp_features.light_status =
3068 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3070 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3071 str_supported(tp_features.light));
3073 return (tp_features.light)? 0 : 1;
3076 static int light_read(char *p)
3078 int len = 0;
3079 int status = 0;
3081 if (!tp_features.light) {
3082 len += sprintf(p + len, "status:\t\tnot supported\n");
3083 } else if (!tp_features.light_status) {
3084 len += sprintf(p + len, "status:\t\tunknown\n");
3085 len += sprintf(p + len, "commands:\ton, off\n");
3086 } else {
3087 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3088 return -EIO;
3089 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3090 len += sprintf(p + len, "commands:\ton, off\n");
3093 return len;
3096 static int light_write(char *buf)
3098 int cmos_cmd, lght_cmd;
3099 char *cmd;
3100 int success;
3102 if (!tp_features.light)
3103 return -ENODEV;
3105 while ((cmd = next_cmd(&buf))) {
3106 if (strlencmp(cmd, "on") == 0) {
3107 cmos_cmd = 0x0c;
3108 lght_cmd = 1;
3109 } else if (strlencmp(cmd, "off") == 0) {
3110 cmos_cmd = 0x0d;
3111 lght_cmd = 0;
3112 } else
3113 return -EINVAL;
3115 success = cmos_handle ?
3116 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3117 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3118 if (!success)
3119 return -EIO;
3122 return 0;
3125 static struct ibm_struct light_driver_data = {
3126 .name = "light",
3127 .read = light_read,
3128 .write = light_write,
3131 /*************************************************************************
3132 * Dock subdriver
3135 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3137 static void dock_notify(struct ibm_struct *ibm, u32 event);
3138 static int dock_read(char *p);
3139 static int dock_write(char *buf);
3141 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3142 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3143 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3144 "\\_SB.PCI.ISA.SLCE", /* 570 */
3145 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3147 /* don't list other alternatives as we install a notify handler on the 570 */
3148 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3150 static const struct acpi_device_id ibm_pci_device_ids[] = {
3151 {PCI_ROOT_HID_STRING, 0},
3152 {"", 0},
3155 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3157 .notify = dock_notify,
3158 .handle = &dock_handle,
3159 .type = ACPI_SYSTEM_NOTIFY,
3162 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3163 * We just use it to get notifications of dock hotplug
3164 * in very old thinkpads */
3165 .hid = ibm_pci_device_ids,
3166 .notify = dock_notify,
3167 .handle = &pci_handle,
3168 .type = ACPI_SYSTEM_NOTIFY,
3172 static struct ibm_struct dock_driver_data[2] = {
3174 .name = "dock",
3175 .read = dock_read,
3176 .write = dock_write,
3177 .acpi = &ibm_dock_acpidriver[0],
3180 .name = "dock",
3181 .acpi = &ibm_dock_acpidriver[1],
3185 #define dock_docked() (_sta(dock_handle) & 1)
3187 static int __init dock_init(struct ibm_init_struct *iibm)
3189 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3191 TPACPI_ACPIHANDLE_INIT(dock);
3193 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3194 str_supported(dock_handle != NULL));
3196 return (dock_handle)? 0 : 1;
3199 static int __init dock_init2(struct ibm_init_struct *iibm)
3201 int dock2_needed;
3203 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3205 if (dock_driver_data[0].flags.acpi_driver_registered &&
3206 dock_driver_data[0].flags.acpi_notify_installed) {
3207 TPACPI_ACPIHANDLE_INIT(pci);
3208 dock2_needed = (pci_handle != NULL);
3209 vdbg_printk(TPACPI_DBG_INIT,
3210 "dock PCI handler for the TP 570 is %s\n",
3211 str_supported(dock2_needed));
3212 } else {
3213 vdbg_printk(TPACPI_DBG_INIT,
3214 "dock subdriver part 2 not required\n");
3215 dock2_needed = 0;
3218 return (dock2_needed)? 0 : 1;
3221 static void dock_notify(struct ibm_struct *ibm, u32 event)
3223 int docked = dock_docked();
3224 int pci = ibm->acpi->hid && ibm->acpi->device &&
3225 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3226 int data;
3228 if (event == 1 && !pci) /* 570 */
3229 data = 1; /* button */
3230 else if (event == 1 && pci) /* 570 */
3231 data = 3; /* dock */
3232 else if (event == 3 && docked)
3233 data = 1; /* button */
3234 else if (event == 3 && !docked)
3235 data = 2; /* undock */
3236 else if (event == 0 && docked)
3237 data = 3; /* dock */
3238 else {
3239 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3240 event, _sta(dock_handle));
3241 data = 0; /* unknown */
3243 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3244 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3245 ibm->acpi->device->dev.bus_id,
3246 event, data);
3249 static int dock_read(char *p)
3251 int len = 0;
3252 int docked = dock_docked();
3254 if (!dock_handle)
3255 len += sprintf(p + len, "status:\t\tnot supported\n");
3256 else if (!docked)
3257 len += sprintf(p + len, "status:\t\tundocked\n");
3258 else {
3259 len += sprintf(p + len, "status:\t\tdocked\n");
3260 len += sprintf(p + len, "commands:\tdock, undock\n");
3263 return len;
3266 static int dock_write(char *buf)
3268 char *cmd;
3270 if (!dock_docked())
3271 return -ENODEV;
3273 while ((cmd = next_cmd(&buf))) {
3274 if (strlencmp(cmd, "undock") == 0) {
3275 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3276 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3277 return -EIO;
3278 } else if (strlencmp(cmd, "dock") == 0) {
3279 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3280 return -EIO;
3281 } else
3282 return -EINVAL;
3285 return 0;
3288 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3290 /*************************************************************************
3291 * Bay subdriver
3294 #ifdef CONFIG_THINKPAD_ACPI_BAY
3296 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3297 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3298 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3299 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3300 ); /* A21e, R30, R31 */
3301 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3302 "_EJ0", /* all others */
3303 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3304 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3305 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3306 ); /* all others */
3307 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3308 "_EJ0", /* 770x */
3309 ); /* all others */
3311 static int __init bay_init(struct ibm_init_struct *iibm)
3313 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3315 TPACPI_ACPIHANDLE_INIT(bay);
3316 if (bay_handle)
3317 TPACPI_ACPIHANDLE_INIT(bay_ej);
3318 TPACPI_ACPIHANDLE_INIT(bay2);
3319 if (bay2_handle)
3320 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3322 tp_features.bay_status = bay_handle &&
3323 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3324 tp_features.bay_status2 = bay2_handle &&
3325 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3327 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3328 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3329 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3330 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3332 vdbg_printk(TPACPI_DBG_INIT,
3333 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3334 str_supported(tp_features.bay_status),
3335 str_supported(tp_features.bay_eject),
3336 str_supported(tp_features.bay_status2),
3337 str_supported(tp_features.bay_eject2));
3339 return (tp_features.bay_status || tp_features.bay_eject ||
3340 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3343 static void bay_notify(struct ibm_struct *ibm, u32 event)
3345 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3346 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3347 ibm->acpi->device->dev.bus_id,
3348 event, 0);
3351 #define bay_occupied(b) (_sta(b##_handle) & 1)
3353 static int bay_read(char *p)
3355 int len = 0;
3356 int occupied = bay_occupied(bay);
3357 int occupied2 = bay_occupied(bay2);
3358 int eject, eject2;
3360 len += sprintf(p + len, "status:\t\t%s\n",
3361 tp_features.bay_status ?
3362 (occupied ? "occupied" : "unoccupied") :
3363 "not supported");
3364 if (tp_features.bay_status2)
3365 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3366 "occupied" : "unoccupied");
3368 eject = tp_features.bay_eject && occupied;
3369 eject2 = tp_features.bay_eject2 && occupied2;
3371 if (eject && eject2)
3372 len += sprintf(p + len, "commands:\teject, eject2\n");
3373 else if (eject)
3374 len += sprintf(p + len, "commands:\teject\n");
3375 else if (eject2)
3376 len += sprintf(p + len, "commands:\teject2\n");
3378 return len;
3381 static int bay_write(char *buf)
3383 char *cmd;
3385 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3386 return -ENODEV;
3388 while ((cmd = next_cmd(&buf))) {
3389 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3390 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3391 return -EIO;
3392 } else if (tp_features.bay_eject2 &&
3393 strlencmp(cmd, "eject2") == 0) {
3394 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3395 return -EIO;
3396 } else
3397 return -EINVAL;
3400 return 0;
3403 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3404 .notify = bay_notify,
3405 .handle = &bay_handle,
3406 .type = ACPI_SYSTEM_NOTIFY,
3409 static struct ibm_struct bay_driver_data = {
3410 .name = "bay",
3411 .read = bay_read,
3412 .write = bay_write,
3413 .acpi = &ibm_bay_acpidriver,
3416 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3418 /*************************************************************************
3419 * CMOS subdriver
3422 /* sysfs cmos_command -------------------------------------------------- */
3423 static ssize_t cmos_command_store(struct device *dev,
3424 struct device_attribute *attr,
3425 const char *buf, size_t count)
3427 unsigned long cmos_cmd;
3428 int res;
3430 if (parse_strtoul(buf, 21, &cmos_cmd))
3431 return -EINVAL;
3433 res = issue_thinkpad_cmos_command(cmos_cmd);
3434 return (res)? res : count;
3437 static struct device_attribute dev_attr_cmos_command =
3438 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3440 /* --------------------------------------------------------------------- */
3442 static int __init cmos_init(struct ibm_init_struct *iibm)
3444 int res;
3446 vdbg_printk(TPACPI_DBG_INIT,
3447 "initializing cmos commands subdriver\n");
3449 TPACPI_ACPIHANDLE_INIT(cmos);
3451 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3452 str_supported(cmos_handle != NULL));
3454 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3455 if (res)
3456 return res;
3458 return (cmos_handle)? 0 : 1;
3461 static void cmos_exit(void)
3463 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3466 static int cmos_read(char *p)
3468 int len = 0;
3470 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3471 R30, R31, T20-22, X20-21 */
3472 if (!cmos_handle)
3473 len += sprintf(p + len, "status:\t\tnot supported\n");
3474 else {
3475 len += sprintf(p + len, "status:\t\tsupported\n");
3476 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3479 return len;
3482 static int cmos_write(char *buf)
3484 char *cmd;
3485 int cmos_cmd, res;
3487 while ((cmd = next_cmd(&buf))) {
3488 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3489 cmos_cmd >= 0 && cmos_cmd <= 21) {
3490 /* cmos_cmd set */
3491 } else
3492 return -EINVAL;
3494 res = issue_thinkpad_cmos_command(cmos_cmd);
3495 if (res)
3496 return res;
3499 return 0;
3502 static struct ibm_struct cmos_driver_data = {
3503 .name = "cmos",
3504 .read = cmos_read,
3505 .write = cmos_write,
3506 .exit = cmos_exit,
3509 /*************************************************************************
3510 * LED subdriver
3513 enum led_access_mode {
3514 TPACPI_LED_NONE = 0,
3515 TPACPI_LED_570, /* 570 */
3516 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3517 TPACPI_LED_NEW, /* all others */
3520 enum { /* For TPACPI_LED_OLD */
3521 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3522 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3523 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3526 static enum led_access_mode led_supported;
3528 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3529 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3530 /* T20-22, X20-21 */
3531 "LED", /* all others */
3532 ); /* R30, R31 */
3534 static int __init led_init(struct ibm_init_struct *iibm)
3536 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3538 TPACPI_ACPIHANDLE_INIT(led);
3540 if (!led_handle)
3541 /* led not supported on R30, R31 */
3542 led_supported = TPACPI_LED_NONE;
3543 else if (strlencmp(led_path, "SLED") == 0)
3544 /* 570 */
3545 led_supported = TPACPI_LED_570;
3546 else if (strlencmp(led_path, "SYSL") == 0)
3547 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3548 led_supported = TPACPI_LED_OLD;
3549 else
3550 /* all others */
3551 led_supported = TPACPI_LED_NEW;
3553 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3554 str_supported(led_supported), led_supported);
3556 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3559 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3561 static int led_read(char *p)
3563 int len = 0;
3565 if (!led_supported) {
3566 len += sprintf(p + len, "status:\t\tnot supported\n");
3567 return len;
3569 len += sprintf(p + len, "status:\t\tsupported\n");
3571 if (led_supported == TPACPI_LED_570) {
3572 /* 570 */
3573 int i, status;
3574 for (i = 0; i < 8; i++) {
3575 if (!acpi_evalf(ec_handle,
3576 &status, "GLED", "dd", 1 << i))
3577 return -EIO;
3578 len += sprintf(p + len, "%d:\t\t%s\n",
3579 i, led_status(status));
3583 len += sprintf(p + len, "commands:\t"
3584 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3586 return len;
3589 /* off, on, blink */
3590 static const int led_sled_arg1[] = { 0, 1, 3 };
3591 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3592 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3593 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3595 static int led_write(char *buf)
3597 char *cmd;
3598 int led, ind, ret;
3600 if (!led_supported)
3601 return -ENODEV;
3603 while ((cmd = next_cmd(&buf))) {
3604 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3605 return -EINVAL;
3607 if (strstr(cmd, "off")) {
3608 ind = 0;
3609 } else if (strstr(cmd, "on")) {
3610 ind = 1;
3611 } else if (strstr(cmd, "blink")) {
3612 ind = 2;
3613 } else
3614 return -EINVAL;
3616 if (led_supported == TPACPI_LED_570) {
3617 /* 570 */
3618 led = 1 << led;
3619 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3620 led, led_sled_arg1[ind]))
3621 return -EIO;
3622 } else if (led_supported == TPACPI_LED_OLD) {
3623 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3624 led = 1 << led;
3625 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3626 if (ret >= 0)
3627 ret = ec_write(TPACPI_LED_EC_HLBL,
3628 led * led_exp_hlbl[ind]);
3629 if (ret >= 0)
3630 ret = ec_write(TPACPI_LED_EC_HLCL,
3631 led * led_exp_hlcl[ind]);
3632 if (ret < 0)
3633 return ret;
3634 } else {
3635 /* all others */
3636 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3637 led, led_led_arg1[ind]))
3638 return -EIO;
3642 return 0;
3645 static struct ibm_struct led_driver_data = {
3646 .name = "led",
3647 .read = led_read,
3648 .write = led_write,
3651 /*************************************************************************
3652 * Beep subdriver
3655 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3657 static int __init beep_init(struct ibm_init_struct *iibm)
3659 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3661 TPACPI_ACPIHANDLE_INIT(beep);
3663 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3664 str_supported(beep_handle != NULL));
3666 return (beep_handle)? 0 : 1;
3669 static int beep_read(char *p)
3671 int len = 0;
3673 if (!beep_handle)
3674 len += sprintf(p + len, "status:\t\tnot supported\n");
3675 else {
3676 len += sprintf(p + len, "status:\t\tsupported\n");
3677 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3680 return len;
3683 static int beep_write(char *buf)
3685 char *cmd;
3686 int beep_cmd;
3688 if (!beep_handle)
3689 return -ENODEV;
3691 while ((cmd = next_cmd(&buf))) {
3692 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3693 beep_cmd >= 0 && beep_cmd <= 17) {
3694 /* beep_cmd set */
3695 } else
3696 return -EINVAL;
3697 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3698 return -EIO;
3701 return 0;
3704 static struct ibm_struct beep_driver_data = {
3705 .name = "beep",
3706 .read = beep_read,
3707 .write = beep_write,
3710 /*************************************************************************
3711 * Thermal subdriver
3714 enum thermal_access_mode {
3715 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3716 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3717 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3718 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3719 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3722 enum { /* TPACPI_THERMAL_TPEC_* */
3723 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3724 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3725 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3728 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3729 struct ibm_thermal_sensors_struct {
3730 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3733 static enum thermal_access_mode thermal_read_mode;
3735 /* idx is zero-based */
3736 static int thermal_get_sensor(int idx, s32 *value)
3738 int t;
3739 s8 tmp;
3740 char tmpi[5];
3742 t = TP_EC_THERMAL_TMP0;
3744 switch (thermal_read_mode) {
3745 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3746 case TPACPI_THERMAL_TPEC_16:
3747 if (idx >= 8 && idx <= 15) {
3748 t = TP_EC_THERMAL_TMP8;
3749 idx -= 8;
3751 /* fallthrough */
3752 #endif
3753 case TPACPI_THERMAL_TPEC_8:
3754 if (idx <= 7) {
3755 if (!acpi_ec_read(t + idx, &tmp))
3756 return -EIO;
3757 *value = tmp * 1000;
3758 return 0;
3760 break;
3762 case TPACPI_THERMAL_ACPI_UPDT:
3763 if (idx <= 7) {
3764 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3765 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3766 return -EIO;
3767 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3768 return -EIO;
3769 *value = (t - 2732) * 100;
3770 return 0;
3772 break;
3774 case TPACPI_THERMAL_ACPI_TMP07:
3775 if (idx <= 7) {
3776 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3777 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3778 return -EIO;
3779 if (t > 127 || t < -127)
3780 t = TP_EC_THERMAL_TMP_NA;
3781 *value = t * 1000;
3782 return 0;
3784 break;
3786 case TPACPI_THERMAL_NONE:
3787 default:
3788 return -ENOSYS;
3791 return -EINVAL;
3794 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3796 int res, i;
3797 int n;
3799 n = 8;
3800 i = 0;
3802 if (!s)
3803 return -EINVAL;
3805 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3806 n = 16;
3808 for (i = 0 ; i < n; i++) {
3809 res = thermal_get_sensor(i, &s->temp[i]);
3810 if (res)
3811 return res;
3814 return n;
3817 /* sysfs temp##_input -------------------------------------------------- */
3819 static ssize_t thermal_temp_input_show(struct device *dev,
3820 struct device_attribute *attr,
3821 char *buf)
3823 struct sensor_device_attribute *sensor_attr =
3824 to_sensor_dev_attr(attr);
3825 int idx = sensor_attr->index;
3826 s32 value;
3827 int res;
3829 res = thermal_get_sensor(idx, &value);
3830 if (res)
3831 return res;
3832 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3833 return -ENXIO;
3835 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3838 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3839 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3840 thermal_temp_input_show, NULL, _idxB)
3842 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3843 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3844 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3845 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3846 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3847 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3848 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3849 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3850 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3851 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3852 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3853 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3854 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3855 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3856 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3857 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3858 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3861 #define THERMAL_ATTRS(X) \
3862 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3864 static struct attribute *thermal_temp_input_attr[] = {
3865 THERMAL_ATTRS(8),
3866 THERMAL_ATTRS(9),
3867 THERMAL_ATTRS(10),
3868 THERMAL_ATTRS(11),
3869 THERMAL_ATTRS(12),
3870 THERMAL_ATTRS(13),
3871 THERMAL_ATTRS(14),
3872 THERMAL_ATTRS(15),
3873 THERMAL_ATTRS(0),
3874 THERMAL_ATTRS(1),
3875 THERMAL_ATTRS(2),
3876 THERMAL_ATTRS(3),
3877 THERMAL_ATTRS(4),
3878 THERMAL_ATTRS(5),
3879 THERMAL_ATTRS(6),
3880 THERMAL_ATTRS(7),
3881 NULL
3884 static const struct attribute_group thermal_temp_input16_group = {
3885 .attrs = thermal_temp_input_attr
3888 static const struct attribute_group thermal_temp_input8_group = {
3889 .attrs = &thermal_temp_input_attr[8]
3892 #undef THERMAL_SENSOR_ATTR_TEMP
3893 #undef THERMAL_ATTRS
3895 /* --------------------------------------------------------------------- */
3897 static int __init thermal_init(struct ibm_init_struct *iibm)
3899 u8 t, ta1, ta2;
3900 int i;
3901 int acpi_tmp7;
3902 int res;
3904 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3906 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3908 if (thinkpad_id.ec_model) {
3910 * Direct EC access mode: sensors at registers
3911 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3912 * non-implemented, thermal sensors return 0x80 when
3913 * not available
3916 ta1 = ta2 = 0;
3917 for (i = 0; i < 8; i++) {
3918 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3919 ta1 |= t;
3920 } else {
3921 ta1 = 0;
3922 break;
3924 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3925 ta2 |= t;
3926 } else {
3927 ta1 = 0;
3928 break;
3931 if (ta1 == 0) {
3932 /* This is sheer paranoia, but we handle it anyway */
3933 if (acpi_tmp7) {
3934 printk(TPACPI_ERR
3935 "ThinkPad ACPI EC access misbehaving, "
3936 "falling back to ACPI TMPx access "
3937 "mode\n");
3938 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3939 } else {
3940 printk(TPACPI_ERR
3941 "ThinkPad ACPI EC access misbehaving, "
3942 "disabling thermal sensors access\n");
3943 thermal_read_mode = TPACPI_THERMAL_NONE;
3945 } else {
3946 thermal_read_mode =
3947 (ta2 != 0) ?
3948 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3950 } else if (acpi_tmp7) {
3951 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3952 /* 600e/x, 770e, 770x */
3953 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3954 } else {
3955 /* Standard ACPI TMPx access, max 8 sensors */
3956 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3958 } else {
3959 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3960 thermal_read_mode = TPACPI_THERMAL_NONE;
3963 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3964 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3965 thermal_read_mode);
3967 switch (thermal_read_mode) {
3968 case TPACPI_THERMAL_TPEC_16:
3969 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3970 &thermal_temp_input16_group);
3971 if (res)
3972 return res;
3973 break;
3974 case TPACPI_THERMAL_TPEC_8:
3975 case TPACPI_THERMAL_ACPI_TMP07:
3976 case TPACPI_THERMAL_ACPI_UPDT:
3977 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3978 &thermal_temp_input8_group);
3979 if (res)
3980 return res;
3981 break;
3982 case TPACPI_THERMAL_NONE:
3983 default:
3984 return 1;
3987 return 0;
3990 static void thermal_exit(void)
3992 switch (thermal_read_mode) {
3993 case TPACPI_THERMAL_TPEC_16:
3994 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3995 &thermal_temp_input16_group);
3996 break;
3997 case TPACPI_THERMAL_TPEC_8:
3998 case TPACPI_THERMAL_ACPI_TMP07:
3999 case TPACPI_THERMAL_ACPI_UPDT:
4000 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4001 &thermal_temp_input16_group);
4002 break;
4003 case TPACPI_THERMAL_NONE:
4004 default:
4005 break;
4009 static int thermal_read(char *p)
4011 int len = 0;
4012 int n, i;
4013 struct ibm_thermal_sensors_struct t;
4015 n = thermal_get_sensors(&t);
4016 if (unlikely(n < 0))
4017 return n;
4019 len += sprintf(p + len, "temperatures:\t");
4021 if (n > 0) {
4022 for (i = 0; i < (n - 1); i++)
4023 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4024 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4025 } else
4026 len += sprintf(p + len, "not supported\n");
4028 return len;
4031 static struct ibm_struct thermal_driver_data = {
4032 .name = "thermal",
4033 .read = thermal_read,
4034 .exit = thermal_exit,
4037 /*************************************************************************
4038 * EC Dump subdriver
4041 static u8 ecdump_regs[256];
4043 static int ecdump_read(char *p)
4045 int len = 0;
4046 int i, j;
4047 u8 v;
4049 len += sprintf(p + len, "EC "
4050 " +00 +01 +02 +03 +04 +05 +06 +07"
4051 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4052 for (i = 0; i < 256; i += 16) {
4053 len += sprintf(p + len, "EC 0x%02x:", i);
4054 for (j = 0; j < 16; j++) {
4055 if (!acpi_ec_read(i + j, &v))
4056 break;
4057 if (v != ecdump_regs[i + j])
4058 len += sprintf(p + len, " *%02x", v);
4059 else
4060 len += sprintf(p + len, " %02x", v);
4061 ecdump_regs[i + j] = v;
4063 len += sprintf(p + len, "\n");
4064 if (j != 16)
4065 break;
4068 /* These are way too dangerous to advertise openly... */
4069 #if 0
4070 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4071 " (<offset> is 00-ff, <value> is 00-ff)\n");
4072 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4073 " (<offset> is 00-ff, <value> is 0-255)\n");
4074 #endif
4075 return len;
4078 static int ecdump_write(char *buf)
4080 char *cmd;
4081 int i, v;
4083 while ((cmd = next_cmd(&buf))) {
4084 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4085 /* i and v set */
4086 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4087 /* i and v set */
4088 } else
4089 return -EINVAL;
4090 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4091 if (!acpi_ec_write(i, v))
4092 return -EIO;
4093 } else
4094 return -EINVAL;
4097 return 0;
4100 static struct ibm_struct ecdump_driver_data = {
4101 .name = "ecdump",
4102 .read = ecdump_read,
4103 .write = ecdump_write,
4104 .flags.experimental = 1,
4107 /*************************************************************************
4108 * Backlight/brightness subdriver
4111 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4113 static struct backlight_device *ibm_backlight_device;
4114 static int brightness_offset = 0x31;
4115 static int brightness_mode;
4116 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4118 static struct mutex brightness_mutex;
4121 * ThinkPads can read brightness from two places: EC 0x31, or
4122 * CMOS NVRAM byte 0x5E, bits 0-3.
4124 static int brightness_get(struct backlight_device *bd)
4126 u8 lec = 0, lcmos = 0, level = 0;
4128 if (brightness_mode & 1) {
4129 if (!acpi_ec_read(brightness_offset, &lec))
4130 return -EIO;
4131 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4132 level = lec;
4134 if (brightness_mode & 2) {
4135 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4136 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4137 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4138 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4139 level = lcmos;
4142 if (brightness_mode == 3 && lec != lcmos) {
4143 printk(TPACPI_ERR
4144 "CMOS NVRAM (%u) and EC (%u) do not agree "
4145 "on display brightness level\n",
4146 (unsigned int) lcmos,
4147 (unsigned int) lec);
4148 return -EIO;
4151 return level;
4154 /* May return EINTR which can always be mapped to ERESTARTSYS */
4155 static int brightness_set(int value)
4157 int cmos_cmd, inc, i, res;
4158 int current_value;
4160 if (value > ((tp_features.bright_16levels)? 15 : 7))
4161 return -EINVAL;
4163 res = mutex_lock_interruptible(&brightness_mutex);
4164 if (res < 0)
4165 return res;
4167 current_value = brightness_get(NULL);
4168 if (current_value < 0) {
4169 res = current_value;
4170 goto errout;
4173 cmos_cmd = value > current_value ?
4174 TP_CMOS_BRIGHTNESS_UP :
4175 TP_CMOS_BRIGHTNESS_DOWN;
4176 inc = (value > current_value)? 1 : -1;
4178 res = 0;
4179 for (i = current_value; i != value; i += inc) {
4180 if ((brightness_mode & 2) &&
4181 issue_thinkpad_cmos_command(cmos_cmd)) {
4182 res = -EIO;
4183 goto errout;
4185 if ((brightness_mode & 1) &&
4186 !acpi_ec_write(brightness_offset, i + inc)) {
4187 res = -EIO;
4188 goto errout;;
4192 errout:
4193 mutex_unlock(&brightness_mutex);
4194 return res;
4197 /* sysfs backlight class ----------------------------------------------- */
4199 static int brightness_update_status(struct backlight_device *bd)
4201 /* it is the backlight class's job (caller) to handle
4202 * EINTR and other errors properly */
4203 return brightness_set(
4204 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4205 bd->props.power == FB_BLANK_UNBLANK) ?
4206 bd->props.brightness : 0);
4209 static struct backlight_ops ibm_backlight_data = {
4210 .get_brightness = brightness_get,
4211 .update_status = brightness_update_status,
4214 /* --------------------------------------------------------------------- */
4216 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
4218 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4219 union acpi_object *obj;
4220 int rc;
4222 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4223 obj = (union acpi_object *)buffer.pointer;
4224 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4225 printk(TPACPI_ERR "Unknown _BCL data, "
4226 "please report this to %s\n", TPACPI_MAIL);
4227 rc = 0;
4228 } else {
4229 rc = obj->package.count;
4231 } else {
4232 return 0;
4235 kfree(buffer.pointer);
4236 return rc;
4239 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4240 void *context, void **rv)
4242 char name[ACPI_PATH_SEGMENT_LENGTH];
4243 struct acpi_buffer buffer = { sizeof(name), &name };
4245 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4246 !strncmp("_BCL", name, sizeof(name) - 1)) {
4247 BUG_ON(!rv || !*rv);
4248 **(int **)rv = tpacpi_query_bcl_levels(handle);
4249 return AE_CTRL_TERMINATE;
4250 } else {
4251 return AE_OK;
4256 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
4258 static int __init brightness_check_std_acpi_support(void)
4260 int status;
4261 int bcl_levels = 0;
4262 void *bcl_ptr = &bcl_levels;
4264 if (!vid_handle) {
4265 TPACPI_ACPIHANDLE_INIT(vid);
4267 if (!vid_handle)
4268 return 0;
4271 * Search for a _BCL method, and execute it. This is safe on all
4272 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
4273 * BIOS in ACPI backlight control mode. We do NOT have to care
4274 * about calling the _BCL method in an enabled video device, any
4275 * will do for our purposes.
4278 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4279 brightness_find_bcl, NULL, &bcl_ptr);
4281 if (ACPI_SUCCESS(status) && bcl_levels > 2)
4282 return (bcl_levels - 2);
4284 return 0;
4287 static int __init brightness_init(struct ibm_init_struct *iibm)
4289 int b;
4291 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4293 mutex_init(&brightness_mutex);
4296 * We always attempt to detect acpi support, so as to switch
4297 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4298 * going to publish a backlight interface
4300 b = brightness_check_std_acpi_support();
4301 if (b > 0) {
4302 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4303 printk(TPACPI_NOTICE
4304 "Lenovo BIOS switched to ACPI backlight "
4305 "control mode\n");
4307 if (brightness_enable > 1) {
4308 printk(TPACPI_NOTICE
4309 "standard ACPI backlight interface "
4310 "available, not loading native one...\n");
4311 return 1;
4315 if (!brightness_enable) {
4316 dbg_printk(TPACPI_DBG_INIT,
4317 "brightness support disabled by "
4318 "module parameter\n");
4319 return 1;
4322 if (b > 16) {
4323 printk(TPACPI_ERR
4324 "Unsupported brightness interface, "
4325 "please contact %s\n", TPACPI_MAIL);
4326 return 1;
4328 if (b == 16)
4329 tp_features.bright_16levels = 1;
4331 if (!brightness_mode) {
4332 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4333 brightness_mode = 2;
4334 else
4335 brightness_mode = 3;
4337 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4338 brightness_mode);
4341 if (brightness_mode > 3)
4342 return -EINVAL;
4344 b = brightness_get(NULL);
4345 if (b < 0)
4346 return 1;
4348 if (tp_features.bright_16levels)
4349 printk(TPACPI_INFO
4350 "detected a 16-level brightness capable ThinkPad\n");
4352 ibm_backlight_device = backlight_device_register(
4353 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4354 &ibm_backlight_data);
4355 if (IS_ERR(ibm_backlight_device)) {
4356 printk(TPACPI_ERR "Could not register backlight device\n");
4357 return PTR_ERR(ibm_backlight_device);
4359 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4361 ibm_backlight_device->props.max_brightness =
4362 (tp_features.bright_16levels)? 15 : 7;
4363 ibm_backlight_device->props.brightness = b;
4364 backlight_update_status(ibm_backlight_device);
4366 return 0;
4369 static void brightness_exit(void)
4371 if (ibm_backlight_device) {
4372 vdbg_printk(TPACPI_DBG_EXIT,
4373 "calling backlight_device_unregister()\n");
4374 backlight_device_unregister(ibm_backlight_device);
4375 ibm_backlight_device = NULL;
4379 static int brightness_read(char *p)
4381 int len = 0;
4382 int level;
4384 level = brightness_get(NULL);
4385 if (level < 0) {
4386 len += sprintf(p + len, "level:\t\tunreadable\n");
4387 } else {
4388 len += sprintf(p + len, "level:\t\t%d\n", level);
4389 len += sprintf(p + len, "commands:\tup, down\n");
4390 len += sprintf(p + len, "commands:\tlevel <level>"
4391 " (<level> is 0-%d)\n",
4392 (tp_features.bright_16levels) ? 15 : 7);
4395 return len;
4398 static int brightness_write(char *buf)
4400 int level;
4401 int rc;
4402 char *cmd;
4403 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4405 level = brightness_get(NULL);
4406 if (level < 0)
4407 return level;
4409 while ((cmd = next_cmd(&buf))) {
4410 if (strlencmp(cmd, "up") == 0) {
4411 if (level < max_level)
4412 level++;
4413 } else if (strlencmp(cmd, "down") == 0) {
4414 if (level > 0)
4415 level--;
4416 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4417 level >= 0 && level <= max_level) {
4418 /* new level set */
4419 } else
4420 return -EINVAL;
4424 * Now we know what the final level should be, so we try to set it.
4425 * Doing it this way makes the syscall restartable in case of EINTR
4427 rc = brightness_set(level);
4428 return (rc == -EINTR)? ERESTARTSYS : rc;
4431 static struct ibm_struct brightness_driver_data = {
4432 .name = "brightness",
4433 .read = brightness_read,
4434 .write = brightness_write,
4435 .exit = brightness_exit,
4438 /*************************************************************************
4439 * Volume subdriver
4442 static int volume_offset = 0x30;
4444 static int volume_read(char *p)
4446 int len = 0;
4447 u8 level;
4449 if (!acpi_ec_read(volume_offset, &level)) {
4450 len += sprintf(p + len, "level:\t\tunreadable\n");
4451 } else {
4452 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4453 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4454 len += sprintf(p + len, "commands:\tup, down, mute\n");
4455 len += sprintf(p + len, "commands:\tlevel <level>"
4456 " (<level> is 0-15)\n");
4459 return len;
4462 static int volume_write(char *buf)
4464 int cmos_cmd, inc, i;
4465 u8 level, mute;
4466 int new_level, new_mute;
4467 char *cmd;
4469 while ((cmd = next_cmd(&buf))) {
4470 if (!acpi_ec_read(volume_offset, &level))
4471 return -EIO;
4472 new_mute = mute = level & 0x40;
4473 new_level = level = level & 0xf;
4475 if (strlencmp(cmd, "up") == 0) {
4476 if (mute)
4477 new_mute = 0;
4478 else
4479 new_level = level == 15 ? 15 : level + 1;
4480 } else if (strlencmp(cmd, "down") == 0) {
4481 if (mute)
4482 new_mute = 0;
4483 else
4484 new_level = level == 0 ? 0 : level - 1;
4485 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4486 new_level >= 0 && new_level <= 15) {
4487 /* new_level set */
4488 } else if (strlencmp(cmd, "mute") == 0) {
4489 new_mute = 0x40;
4490 } else
4491 return -EINVAL;
4493 if (new_level != level) {
4494 /* mute doesn't change */
4496 cmos_cmd = (new_level > level) ?
4497 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4498 inc = new_level > level ? 1 : -1;
4500 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4501 !acpi_ec_write(volume_offset, level)))
4502 return -EIO;
4504 for (i = level; i != new_level; i += inc)
4505 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4506 !acpi_ec_write(volume_offset, i + inc))
4507 return -EIO;
4509 if (mute &&
4510 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4511 !acpi_ec_write(volume_offset, new_level + mute))) {
4512 return -EIO;
4516 if (new_mute != mute) {
4517 /* level doesn't change */
4519 cmos_cmd = (new_mute) ?
4520 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4522 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4523 !acpi_ec_write(volume_offset, level + new_mute))
4524 return -EIO;
4528 return 0;
4531 static struct ibm_struct volume_driver_data = {
4532 .name = "volume",
4533 .read = volume_read,
4534 .write = volume_write,
4537 /*************************************************************************
4538 * Fan subdriver
4542 * FAN ACCESS MODES
4544 * TPACPI_FAN_RD_ACPI_GFAN:
4545 * ACPI GFAN method: returns fan level
4547 * see TPACPI_FAN_WR_ACPI_SFAN
4548 * EC 0x2f (HFSP) not available if GFAN exists
4550 * TPACPI_FAN_WR_ACPI_SFAN:
4551 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4553 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4554 * it for writing.
4556 * TPACPI_FAN_WR_TPEC:
4557 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4558 * Supported on almost all ThinkPads
4560 * Fan speed changes of any sort (including those caused by the
4561 * disengaged mode) are usually done slowly by the firmware as the
4562 * maximum ammount of fan duty cycle change per second seems to be
4563 * limited.
4565 * Reading is not available if GFAN exists.
4566 * Writing is not available if SFAN exists.
4568 * Bits
4569 * 7 automatic mode engaged;
4570 * (default operation mode of the ThinkPad)
4571 * fan level is ignored in this mode.
4572 * 6 full speed mode (takes precedence over bit 7);
4573 * not available on all thinkpads. May disable
4574 * the tachometer while the fan controller ramps up
4575 * the speed (which can take up to a few *minutes*).
4576 * Speeds up fan to 100% duty-cycle, which is far above
4577 * the standard RPM levels. It is not impossible that
4578 * it could cause hardware damage.
4579 * 5-3 unused in some models. Extra bits for fan level
4580 * in others, but still useless as all values above
4581 * 7 map to the same speed as level 7 in these models.
4582 * 2-0 fan level (0..7 usually)
4583 * 0x00 = stop
4584 * 0x07 = max (set when temperatures critical)
4585 * Some ThinkPads may have other levels, see
4586 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4588 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4589 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4590 * does so, its initial value is meaningless (0x07).
4592 * For firmware bugs, refer to:
4593 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4595 * ----
4597 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4598 * Main fan tachometer reading (in RPM)
4600 * This register is present on all ThinkPads with a new-style EC, and
4601 * it is known not to be present on the A21m/e, and T22, as there is
4602 * something else in offset 0x84 according to the ACPI DSDT. Other
4603 * ThinkPads from this same time period (and earlier) probably lack the
4604 * tachometer as well.
4606 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4607 * was never fixed by IBM to report the EC firmware version string
4608 * probably support the tachometer (like the early X models), so
4609 * detecting it is quite hard. We need more data to know for sure.
4611 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4612 * might result.
4614 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4615 * mode.
4617 * For firmware bugs, refer to:
4618 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4620 * TPACPI_FAN_WR_ACPI_FANS:
4621 * ThinkPad X31, X40, X41. Not available in the X60.
4623 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4624 * high speed. ACPI DSDT seems to map these three speeds to levels
4625 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4626 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4628 * The speeds are stored on handles
4629 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4631 * There are three default speed sets, acessible as handles:
4632 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4634 * ACPI DSDT switches which set is in use depending on various
4635 * factors.
4637 * TPACPI_FAN_WR_TPEC is also available and should be used to
4638 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4639 * but the ACPI tables just mention level 7.
4642 enum { /* Fan control constants */
4643 fan_status_offset = 0x2f, /* EC register 0x2f */
4644 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4645 * 0x84 must be read before 0x85 */
4647 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4648 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4650 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4653 enum fan_status_access_mode {
4654 TPACPI_FAN_NONE = 0, /* No fan status or control */
4655 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4656 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4659 enum fan_control_access_mode {
4660 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4661 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4662 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4663 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4666 enum fan_control_commands {
4667 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4668 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4669 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4670 * and also watchdog cmd */
4673 static int fan_control_allowed;
4675 static enum fan_status_access_mode fan_status_access_mode;
4676 static enum fan_control_access_mode fan_control_access_mode;
4677 static enum fan_control_commands fan_control_commands;
4679 static u8 fan_control_initial_status;
4680 static u8 fan_control_desired_level;
4681 static int fan_watchdog_maxinterval;
4683 static struct mutex fan_mutex;
4685 static void fan_watchdog_fire(struct work_struct *ignored);
4686 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4688 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4689 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4690 "\\FSPD", /* 600e/x, 770e, 770x */
4691 ); /* all others */
4692 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4693 "JFNS", /* 770x-JL */
4694 ); /* all others */
4697 * Call with fan_mutex held
4699 static void fan_update_desired_level(u8 status)
4701 if ((status &
4702 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4703 if (status > 7)
4704 fan_control_desired_level = 7;
4705 else
4706 fan_control_desired_level = status;
4710 static int fan_get_status(u8 *status)
4712 u8 s;
4714 /* TODO:
4715 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4717 switch (fan_status_access_mode) {
4718 case TPACPI_FAN_RD_ACPI_GFAN:
4719 /* 570, 600e/x, 770e, 770x */
4721 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4722 return -EIO;
4724 if (likely(status))
4725 *status = s & 0x07;
4727 break;
4729 case TPACPI_FAN_RD_TPEC:
4730 /* all except 570, 600e/x, 770e, 770x */
4731 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4732 return -EIO;
4734 if (likely(status))
4735 *status = s;
4737 break;
4739 default:
4740 return -ENXIO;
4743 return 0;
4746 static int fan_get_status_safe(u8 *status)
4748 int rc;
4749 u8 s;
4751 if (mutex_lock_interruptible(&fan_mutex))
4752 return -ERESTARTSYS;
4753 rc = fan_get_status(&s);
4754 if (!rc)
4755 fan_update_desired_level(s);
4756 mutex_unlock(&fan_mutex);
4758 if (status)
4759 *status = s;
4761 return rc;
4764 static int fan_get_speed(unsigned int *speed)
4766 u8 hi, lo;
4768 switch (fan_status_access_mode) {
4769 case TPACPI_FAN_RD_TPEC:
4770 /* all except 570, 600e/x, 770e, 770x */
4771 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4772 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4773 return -EIO;
4775 if (likely(speed))
4776 *speed = (hi << 8) | lo;
4778 break;
4780 default:
4781 return -ENXIO;
4784 return 0;
4787 static int fan_set_level(int level)
4789 if (!fan_control_allowed)
4790 return -EPERM;
4792 switch (fan_control_access_mode) {
4793 case TPACPI_FAN_WR_ACPI_SFAN:
4794 if (level >= 0 && level <= 7) {
4795 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4796 return -EIO;
4797 } else
4798 return -EINVAL;
4799 break;
4801 case TPACPI_FAN_WR_ACPI_FANS:
4802 case TPACPI_FAN_WR_TPEC:
4803 if ((level != TP_EC_FAN_AUTO) &&
4804 (level != TP_EC_FAN_FULLSPEED) &&
4805 ((level < 0) || (level > 7)))
4806 return -EINVAL;
4808 /* safety net should the EC not support AUTO
4809 * or FULLSPEED mode bits and just ignore them */
4810 if (level & TP_EC_FAN_FULLSPEED)
4811 level |= 7; /* safety min speed 7 */
4812 else if (level & TP_EC_FAN_AUTO)
4813 level |= 4; /* safety min speed 4 */
4815 if (!acpi_ec_write(fan_status_offset, level))
4816 return -EIO;
4817 else
4818 tp_features.fan_ctrl_status_undef = 0;
4819 break;
4821 default:
4822 return -ENXIO;
4824 return 0;
4827 static int fan_set_level_safe(int level)
4829 int rc;
4831 if (!fan_control_allowed)
4832 return -EPERM;
4834 if (mutex_lock_interruptible(&fan_mutex))
4835 return -ERESTARTSYS;
4837 if (level == TPACPI_FAN_LAST_LEVEL)
4838 level = fan_control_desired_level;
4840 rc = fan_set_level(level);
4841 if (!rc)
4842 fan_update_desired_level(level);
4844 mutex_unlock(&fan_mutex);
4845 return rc;
4848 static int fan_set_enable(void)
4850 u8 s;
4851 int rc;
4853 if (!fan_control_allowed)
4854 return -EPERM;
4856 if (mutex_lock_interruptible(&fan_mutex))
4857 return -ERESTARTSYS;
4859 switch (fan_control_access_mode) {
4860 case TPACPI_FAN_WR_ACPI_FANS:
4861 case TPACPI_FAN_WR_TPEC:
4862 rc = fan_get_status(&s);
4863 if (rc < 0)
4864 break;
4866 /* Don't go out of emergency fan mode */
4867 if (s != 7) {
4868 s &= 0x07;
4869 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4872 if (!acpi_ec_write(fan_status_offset, s))
4873 rc = -EIO;
4874 else {
4875 tp_features.fan_ctrl_status_undef = 0;
4876 rc = 0;
4878 break;
4880 case TPACPI_FAN_WR_ACPI_SFAN:
4881 rc = fan_get_status(&s);
4882 if (rc < 0)
4883 break;
4885 s &= 0x07;
4887 /* Set fan to at least level 4 */
4888 s |= 4;
4890 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4891 rc = -EIO;
4892 else
4893 rc = 0;
4894 break;
4896 default:
4897 rc = -ENXIO;
4900 mutex_unlock(&fan_mutex);
4901 return rc;
4904 static int fan_set_disable(void)
4906 int rc;
4908 if (!fan_control_allowed)
4909 return -EPERM;
4911 if (mutex_lock_interruptible(&fan_mutex))
4912 return -ERESTARTSYS;
4914 rc = 0;
4915 switch (fan_control_access_mode) {
4916 case TPACPI_FAN_WR_ACPI_FANS:
4917 case TPACPI_FAN_WR_TPEC:
4918 if (!acpi_ec_write(fan_status_offset, 0x00))
4919 rc = -EIO;
4920 else {
4921 fan_control_desired_level = 0;
4922 tp_features.fan_ctrl_status_undef = 0;
4924 break;
4926 case TPACPI_FAN_WR_ACPI_SFAN:
4927 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4928 rc = -EIO;
4929 else
4930 fan_control_desired_level = 0;
4931 break;
4933 default:
4934 rc = -ENXIO;
4938 mutex_unlock(&fan_mutex);
4939 return rc;
4942 static int fan_set_speed(int speed)
4944 int rc;
4946 if (!fan_control_allowed)
4947 return -EPERM;
4949 if (mutex_lock_interruptible(&fan_mutex))
4950 return -ERESTARTSYS;
4952 rc = 0;
4953 switch (fan_control_access_mode) {
4954 case TPACPI_FAN_WR_ACPI_FANS:
4955 if (speed >= 0 && speed <= 65535) {
4956 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4957 speed, speed, speed))
4958 rc = -EIO;
4959 } else
4960 rc = -EINVAL;
4961 break;
4963 default:
4964 rc = -ENXIO;
4967 mutex_unlock(&fan_mutex);
4968 return rc;
4971 static void fan_watchdog_reset(void)
4973 static int fan_watchdog_active;
4975 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4976 return;
4978 if (fan_watchdog_active)
4979 cancel_delayed_work(&fan_watchdog_task);
4981 if (fan_watchdog_maxinterval > 0 &&
4982 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4983 fan_watchdog_active = 1;
4984 if (!schedule_delayed_work(&fan_watchdog_task,
4985 msecs_to_jiffies(fan_watchdog_maxinterval
4986 * 1000))) {
4987 printk(TPACPI_ERR
4988 "failed to schedule the fan watchdog, "
4989 "watchdog will not trigger\n");
4991 } else
4992 fan_watchdog_active = 0;
4995 static void fan_watchdog_fire(struct work_struct *ignored)
4997 int rc;
4999 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5000 return;
5002 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5003 rc = fan_set_enable();
5004 if (rc < 0) {
5005 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5006 "will try again later...\n", -rc);
5007 /* reschedule for later */
5008 fan_watchdog_reset();
5013 * SYSFS fan layout: hwmon compatible (device)
5015 * pwm*_enable:
5016 * 0: "disengaged" mode
5017 * 1: manual mode
5018 * 2: native EC "auto" mode (recommended, hardware default)
5020 * pwm*: set speed in manual mode, ignored otherwise.
5021 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5022 * interpolation.
5024 * fan*_input: tachometer reading, RPM
5027 * SYSFS fan layout: extensions
5029 * fan_watchdog (driver):
5030 * fan watchdog interval in seconds, 0 disables (default), max 120
5033 /* sysfs fan pwm1_enable ----------------------------------------------- */
5034 static ssize_t fan_pwm1_enable_show(struct device *dev,
5035 struct device_attribute *attr,
5036 char *buf)
5038 int res, mode;
5039 u8 status;
5041 res = fan_get_status_safe(&status);
5042 if (res)
5043 return res;
5045 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5046 if (status != fan_control_initial_status) {
5047 tp_features.fan_ctrl_status_undef = 0;
5048 } else {
5049 /* Return most likely status. In fact, it
5050 * might be the only possible status */
5051 status = TP_EC_FAN_AUTO;
5055 if (status & TP_EC_FAN_FULLSPEED) {
5056 mode = 0;
5057 } else if (status & TP_EC_FAN_AUTO) {
5058 mode = 2;
5059 } else
5060 mode = 1;
5062 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5065 static ssize_t fan_pwm1_enable_store(struct device *dev,
5066 struct device_attribute *attr,
5067 const char *buf, size_t count)
5069 unsigned long t;
5070 int res, level;
5072 if (parse_strtoul(buf, 2, &t))
5073 return -EINVAL;
5075 switch (t) {
5076 case 0:
5077 level = TP_EC_FAN_FULLSPEED;
5078 break;
5079 case 1:
5080 level = TPACPI_FAN_LAST_LEVEL;
5081 break;
5082 case 2:
5083 level = TP_EC_FAN_AUTO;
5084 break;
5085 case 3:
5086 /* reserved for software-controlled auto mode */
5087 return -ENOSYS;
5088 default:
5089 return -EINVAL;
5092 res = fan_set_level_safe(level);
5093 if (res == -ENXIO)
5094 return -EINVAL;
5095 else if (res < 0)
5096 return res;
5098 fan_watchdog_reset();
5100 return count;
5103 static struct device_attribute dev_attr_fan_pwm1_enable =
5104 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5105 fan_pwm1_enable_show, fan_pwm1_enable_store);
5107 /* sysfs fan pwm1 ------------------------------------------------------ */
5108 static ssize_t fan_pwm1_show(struct device *dev,
5109 struct device_attribute *attr,
5110 char *buf)
5112 int res;
5113 u8 status;
5115 res = fan_get_status_safe(&status);
5116 if (res)
5117 return res;
5119 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5120 if (status != fan_control_initial_status) {
5121 tp_features.fan_ctrl_status_undef = 0;
5122 } else {
5123 status = TP_EC_FAN_AUTO;
5127 if ((status &
5128 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5129 status = fan_control_desired_level;
5131 if (status > 7)
5132 status = 7;
5134 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5137 static ssize_t fan_pwm1_store(struct device *dev,
5138 struct device_attribute *attr,
5139 const char *buf, size_t count)
5141 unsigned long s;
5142 int rc;
5143 u8 status, newlevel;
5145 if (parse_strtoul(buf, 255, &s))
5146 return -EINVAL;
5148 /* scale down from 0-255 to 0-7 */
5149 newlevel = (s >> 5) & 0x07;
5151 if (mutex_lock_interruptible(&fan_mutex))
5152 return -ERESTARTSYS;
5154 rc = fan_get_status(&status);
5155 if (!rc && (status &
5156 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5157 rc = fan_set_level(newlevel);
5158 if (rc == -ENXIO)
5159 rc = -EINVAL;
5160 else if (!rc) {
5161 fan_update_desired_level(newlevel);
5162 fan_watchdog_reset();
5166 mutex_unlock(&fan_mutex);
5167 return (rc)? rc : count;
5170 static struct device_attribute dev_attr_fan_pwm1 =
5171 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5172 fan_pwm1_show, fan_pwm1_store);
5174 /* sysfs fan fan1_input ------------------------------------------------ */
5175 static ssize_t fan_fan1_input_show(struct device *dev,
5176 struct device_attribute *attr,
5177 char *buf)
5179 int res;
5180 unsigned int speed;
5182 res = fan_get_speed(&speed);
5183 if (res < 0)
5184 return res;
5186 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5189 static struct device_attribute dev_attr_fan_fan1_input =
5190 __ATTR(fan1_input, S_IRUGO,
5191 fan_fan1_input_show, NULL);
5193 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5194 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5195 char *buf)
5197 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5200 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5201 const char *buf, size_t count)
5203 unsigned long t;
5205 if (parse_strtoul(buf, 120, &t))
5206 return -EINVAL;
5208 if (!fan_control_allowed)
5209 return -EPERM;
5211 fan_watchdog_maxinterval = t;
5212 fan_watchdog_reset();
5214 return count;
5217 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5218 fan_fan_watchdog_show, fan_fan_watchdog_store);
5220 /* --------------------------------------------------------------------- */
5221 static struct attribute *fan_attributes[] = {
5222 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5223 &dev_attr_fan_fan1_input.attr,
5224 NULL
5227 static const struct attribute_group fan_attr_group = {
5228 .attrs = fan_attributes,
5231 static int __init fan_init(struct ibm_init_struct *iibm)
5233 int rc;
5235 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5237 mutex_init(&fan_mutex);
5238 fan_status_access_mode = TPACPI_FAN_NONE;
5239 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5240 fan_control_commands = 0;
5241 fan_watchdog_maxinterval = 0;
5242 tp_features.fan_ctrl_status_undef = 0;
5243 fan_control_desired_level = 7;
5245 TPACPI_ACPIHANDLE_INIT(fans);
5246 TPACPI_ACPIHANDLE_INIT(gfan);
5247 TPACPI_ACPIHANDLE_INIT(sfan);
5249 if (gfan_handle) {
5250 /* 570, 600e/x, 770e, 770x */
5251 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5252 } else {
5253 /* all other ThinkPads: note that even old-style
5254 * ThinkPad ECs supports the fan control register */
5255 if (likely(acpi_ec_read(fan_status_offset,
5256 &fan_control_initial_status))) {
5257 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5259 /* In some ThinkPads, neither the EC nor the ACPI
5260 * DSDT initialize the fan status, and it ends up
5261 * being set to 0x07 when it *could* be either
5262 * 0x07 or 0x80.
5264 * Enable for TP-1Y (T43), TP-78 (R51e),
5265 * TP-76 (R52), TP-70 (T43, R52), which are known
5266 * to be buggy. */
5267 if (fan_control_initial_status == 0x07) {
5268 switch (thinkpad_id.ec_model) {
5269 case 0x5931: /* TP-1Y */
5270 case 0x3837: /* TP-78 */
5271 case 0x3637: /* TP-76 */
5272 case 0x3037: /* TP-70 */
5273 printk(TPACPI_NOTICE
5274 "fan_init: initial fan status "
5275 "is unknown, assuming it is "
5276 "in auto mode\n");
5277 tp_features.fan_ctrl_status_undef = 1;
5281 } else {
5282 printk(TPACPI_ERR
5283 "ThinkPad ACPI EC access misbehaving, "
5284 "fan status and control unavailable\n");
5285 return 1;
5289 if (sfan_handle) {
5290 /* 570, 770x-JL */
5291 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5292 fan_control_commands |=
5293 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5294 } else {
5295 if (!gfan_handle) {
5296 /* gfan without sfan means no fan control */
5297 /* all other models implement TP EC 0x2f control */
5299 if (fans_handle) {
5300 /* X31, X40, X41 */
5301 fan_control_access_mode =
5302 TPACPI_FAN_WR_ACPI_FANS;
5303 fan_control_commands |=
5304 TPACPI_FAN_CMD_SPEED |
5305 TPACPI_FAN_CMD_LEVEL |
5306 TPACPI_FAN_CMD_ENABLE;
5307 } else {
5308 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5309 fan_control_commands |=
5310 TPACPI_FAN_CMD_LEVEL |
5311 TPACPI_FAN_CMD_ENABLE;
5316 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5317 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5318 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5319 fan_status_access_mode, fan_control_access_mode);
5321 /* fan control master switch */
5322 if (!fan_control_allowed) {
5323 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5324 fan_control_commands = 0;
5325 dbg_printk(TPACPI_DBG_INIT,
5326 "fan control features disabled by parameter\n");
5329 /* update fan_control_desired_level */
5330 if (fan_status_access_mode != TPACPI_FAN_NONE)
5331 fan_get_status_safe(NULL);
5333 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5334 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5335 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5336 &fan_attr_group);
5337 if (!(rc < 0))
5338 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5339 &driver_attr_fan_watchdog);
5340 if (rc < 0)
5341 return rc;
5342 return 0;
5343 } else
5344 return 1;
5347 static void fan_exit(void)
5349 vdbg_printk(TPACPI_DBG_EXIT,
5350 "cancelling any pending fan watchdog tasks\n");
5352 /* FIXME: can we really do this unconditionally? */
5353 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5354 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5355 &driver_attr_fan_watchdog);
5357 cancel_delayed_work(&fan_watchdog_task);
5358 flush_scheduled_work();
5361 static int fan_read(char *p)
5363 int len = 0;
5364 int rc;
5365 u8 status;
5366 unsigned int speed = 0;
5368 switch (fan_status_access_mode) {
5369 case TPACPI_FAN_RD_ACPI_GFAN:
5370 /* 570, 600e/x, 770e, 770x */
5371 rc = fan_get_status_safe(&status);
5372 if (rc < 0)
5373 return rc;
5375 len += sprintf(p + len, "status:\t\t%s\n"
5376 "level:\t\t%d\n",
5377 (status != 0) ? "enabled" : "disabled", status);
5378 break;
5380 case TPACPI_FAN_RD_TPEC:
5381 /* all except 570, 600e/x, 770e, 770x */
5382 rc = fan_get_status_safe(&status);
5383 if (rc < 0)
5384 return rc;
5386 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5387 if (status != fan_control_initial_status)
5388 tp_features.fan_ctrl_status_undef = 0;
5389 else
5390 /* Return most likely status. In fact, it
5391 * might be the only possible status */
5392 status = TP_EC_FAN_AUTO;
5395 len += sprintf(p + len, "status:\t\t%s\n",
5396 (status != 0) ? "enabled" : "disabled");
5398 rc = fan_get_speed(&speed);
5399 if (rc < 0)
5400 return rc;
5402 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5404 if (status & TP_EC_FAN_FULLSPEED)
5405 /* Disengaged mode takes precedence */
5406 len += sprintf(p + len, "level:\t\tdisengaged\n");
5407 else if (status & TP_EC_FAN_AUTO)
5408 len += sprintf(p + len, "level:\t\tauto\n");
5409 else
5410 len += sprintf(p + len, "level:\t\t%d\n", status);
5411 break;
5413 case TPACPI_FAN_NONE:
5414 default:
5415 len += sprintf(p + len, "status:\t\tnot supported\n");
5418 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5419 len += sprintf(p + len, "commands:\tlevel <level>");
5421 switch (fan_control_access_mode) {
5422 case TPACPI_FAN_WR_ACPI_SFAN:
5423 len += sprintf(p + len, " (<level> is 0-7)\n");
5424 break;
5426 default:
5427 len += sprintf(p + len, " (<level> is 0-7, "
5428 "auto, disengaged, full-speed)\n");
5429 break;
5433 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5434 len += sprintf(p + len, "commands:\tenable, disable\n"
5435 "commands:\twatchdog <timeout> (<timeout> "
5436 "is 0 (off), 1-120 (seconds))\n");
5438 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5439 len += sprintf(p + len, "commands:\tspeed <speed>"
5440 " (<speed> is 0-65535)\n");
5442 return len;
5445 static int fan_write_cmd_level(const char *cmd, int *rc)
5447 int level;
5449 if (strlencmp(cmd, "level auto") == 0)
5450 level = TP_EC_FAN_AUTO;
5451 else if ((strlencmp(cmd, "level disengaged") == 0) |
5452 (strlencmp(cmd, "level full-speed") == 0))
5453 level = TP_EC_FAN_FULLSPEED;
5454 else if (sscanf(cmd, "level %d", &level) != 1)
5455 return 0;
5457 *rc = fan_set_level_safe(level);
5458 if (*rc == -ENXIO)
5459 printk(TPACPI_ERR "level command accepted for unsupported "
5460 "access mode %d", fan_control_access_mode);
5462 return 1;
5465 static int fan_write_cmd_enable(const char *cmd, int *rc)
5467 if (strlencmp(cmd, "enable") != 0)
5468 return 0;
5470 *rc = fan_set_enable();
5471 if (*rc == -ENXIO)
5472 printk(TPACPI_ERR "enable command accepted for unsupported "
5473 "access mode %d", fan_control_access_mode);
5475 return 1;
5478 static int fan_write_cmd_disable(const char *cmd, int *rc)
5480 if (strlencmp(cmd, "disable") != 0)
5481 return 0;
5483 *rc = fan_set_disable();
5484 if (*rc == -ENXIO)
5485 printk(TPACPI_ERR "disable command accepted for unsupported "
5486 "access mode %d", fan_control_access_mode);
5488 return 1;
5491 static int fan_write_cmd_speed(const char *cmd, int *rc)
5493 int speed;
5495 /* TODO:
5496 * Support speed <low> <medium> <high> ? */
5498 if (sscanf(cmd, "speed %d", &speed) != 1)
5499 return 0;
5501 *rc = fan_set_speed(speed);
5502 if (*rc == -ENXIO)
5503 printk(TPACPI_ERR "speed command accepted for unsupported "
5504 "access mode %d", fan_control_access_mode);
5506 return 1;
5509 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5511 int interval;
5513 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5514 return 0;
5516 if (interval < 0 || interval > 120)
5517 *rc = -EINVAL;
5518 else
5519 fan_watchdog_maxinterval = interval;
5521 return 1;
5524 static int fan_write(char *buf)
5526 char *cmd;
5527 int rc = 0;
5529 while (!rc && (cmd = next_cmd(&buf))) {
5530 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5531 fan_write_cmd_level(cmd, &rc)) &&
5532 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5533 (fan_write_cmd_enable(cmd, &rc) ||
5534 fan_write_cmd_disable(cmd, &rc) ||
5535 fan_write_cmd_watchdog(cmd, &rc))) &&
5536 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5537 fan_write_cmd_speed(cmd, &rc))
5539 rc = -EINVAL;
5540 else if (!rc)
5541 fan_watchdog_reset();
5544 return rc;
5547 static struct ibm_struct fan_driver_data = {
5548 .name = "fan",
5549 .read = fan_read,
5550 .write = fan_write,
5551 .exit = fan_exit,
5554 /****************************************************************************
5555 ****************************************************************************
5557 * Infrastructure
5559 ****************************************************************************
5560 ****************************************************************************/
5562 /* sysfs name ---------------------------------------------------------- */
5563 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5564 struct device_attribute *attr,
5565 char *buf)
5567 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5570 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5571 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5573 /* --------------------------------------------------------------------- */
5575 /* /proc support */
5576 static struct proc_dir_entry *proc_dir;
5579 * Module and infrastructure proble, init and exit handling
5582 static int force_load;
5584 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5585 static const char * __init str_supported(int is_supported)
5587 static char text_unsupported[] __initdata = "not supported";
5589 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5591 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5593 static void ibm_exit(struct ibm_struct *ibm)
5595 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5597 list_del_init(&ibm->all_drivers);
5599 if (ibm->flags.acpi_notify_installed) {
5600 dbg_printk(TPACPI_DBG_EXIT,
5601 "%s: acpi_remove_notify_handler\n", ibm->name);
5602 BUG_ON(!ibm->acpi);
5603 acpi_remove_notify_handler(*ibm->acpi->handle,
5604 ibm->acpi->type,
5605 dispatch_acpi_notify);
5606 ibm->flags.acpi_notify_installed = 0;
5607 ibm->flags.acpi_notify_installed = 0;
5610 if (ibm->flags.proc_created) {
5611 dbg_printk(TPACPI_DBG_EXIT,
5612 "%s: remove_proc_entry\n", ibm->name);
5613 remove_proc_entry(ibm->name, proc_dir);
5614 ibm->flags.proc_created = 0;
5617 if (ibm->flags.acpi_driver_registered) {
5618 dbg_printk(TPACPI_DBG_EXIT,
5619 "%s: acpi_bus_unregister_driver\n", ibm->name);
5620 BUG_ON(!ibm->acpi);
5621 acpi_bus_unregister_driver(ibm->acpi->driver);
5622 kfree(ibm->acpi->driver);
5623 ibm->acpi->driver = NULL;
5624 ibm->flags.acpi_driver_registered = 0;
5627 if (ibm->flags.init_called && ibm->exit) {
5628 ibm->exit();
5629 ibm->flags.init_called = 0;
5632 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5635 static int __init ibm_init(struct ibm_init_struct *iibm)
5637 int ret;
5638 struct ibm_struct *ibm = iibm->data;
5639 struct proc_dir_entry *entry;
5641 BUG_ON(ibm == NULL);
5643 INIT_LIST_HEAD(&ibm->all_drivers);
5645 if (ibm->flags.experimental && !experimental)
5646 return 0;
5648 dbg_printk(TPACPI_DBG_INIT,
5649 "probing for %s\n", ibm->name);
5651 if (iibm->init) {
5652 ret = iibm->init(iibm);
5653 if (ret > 0)
5654 return 0; /* probe failed */
5655 if (ret)
5656 return ret;
5658 ibm->flags.init_called = 1;
5661 if (ibm->acpi) {
5662 if (ibm->acpi->hid) {
5663 ret = register_tpacpi_subdriver(ibm);
5664 if (ret)
5665 goto err_out;
5668 if (ibm->acpi->notify) {
5669 ret = setup_acpi_notify(ibm);
5670 if (ret == -ENODEV) {
5671 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5672 ibm->name);
5673 ret = 0;
5674 goto err_out;
5676 if (ret < 0)
5677 goto err_out;
5681 dbg_printk(TPACPI_DBG_INIT,
5682 "%s installed\n", ibm->name);
5684 if (ibm->read) {
5685 entry = create_proc_entry(ibm->name,
5686 S_IFREG | S_IRUGO | S_IWUSR,
5687 proc_dir);
5688 if (!entry) {
5689 printk(TPACPI_ERR "unable to create proc entry %s\n",
5690 ibm->name);
5691 ret = -ENODEV;
5692 goto err_out;
5694 entry->owner = THIS_MODULE;
5695 entry->data = ibm;
5696 entry->read_proc = &dispatch_procfs_read;
5697 if (ibm->write)
5698 entry->write_proc = &dispatch_procfs_write;
5699 ibm->flags.proc_created = 1;
5702 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5704 return 0;
5706 err_out:
5707 dbg_printk(TPACPI_DBG_INIT,
5708 "%s: at error exit path with result %d\n",
5709 ibm->name, ret);
5711 ibm_exit(ibm);
5712 return (ret < 0)? ret : 0;
5715 /* Probing */
5717 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5719 const struct dmi_device *dev = NULL;
5720 char ec_fw_string[18];
5722 if (!tp)
5723 return;
5725 memset(tp, 0, sizeof(*tp));
5727 if (dmi_name_in_vendors("IBM"))
5728 tp->vendor = PCI_VENDOR_ID_IBM;
5729 else if (dmi_name_in_vendors("LENOVO"))
5730 tp->vendor = PCI_VENDOR_ID_LENOVO;
5731 else
5732 return;
5734 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5735 GFP_KERNEL);
5736 if (!tp->bios_version_str)
5737 return;
5738 tp->bios_model = tp->bios_version_str[0]
5739 | (tp->bios_version_str[1] << 8);
5742 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5743 * X32 or newer, all Z series; Some models must have an
5744 * up-to-date BIOS or they will not be detected.
5746 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5748 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5749 if (sscanf(dev->name,
5750 "IBM ThinkPad Embedded Controller -[%17c",
5751 ec_fw_string) == 1) {
5752 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5753 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5755 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5756 tp->ec_model = ec_fw_string[0]
5757 | (ec_fw_string[1] << 8);
5758 break;
5762 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5763 GFP_KERNEL);
5764 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5765 kfree(tp->model_str);
5766 tp->model_str = NULL;
5770 static int __init probe_for_thinkpad(void)
5772 int is_thinkpad;
5774 if (acpi_disabled)
5775 return -ENODEV;
5778 * Non-ancient models have better DMI tagging, but very old models
5779 * don't.
5781 is_thinkpad = (thinkpad_id.model_str != NULL);
5783 /* ec is required because many other handles are relative to it */
5784 TPACPI_ACPIHANDLE_INIT(ec);
5785 if (!ec_handle) {
5786 if (is_thinkpad)
5787 printk(TPACPI_ERR
5788 "Not yet supported ThinkPad detected!\n");
5789 return -ENODEV;
5793 * Risks a regression on very old machines, but reduces potential
5794 * false positives a damn great deal
5796 if (!is_thinkpad)
5797 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5799 if (!is_thinkpad && !force_load)
5800 return -ENODEV;
5802 return 0;
5806 /* Module init, exit, parameters */
5808 static struct ibm_init_struct ibms_init[] __initdata = {
5810 .init = thinkpad_acpi_driver_init,
5811 .data = &thinkpad_acpi_driver_data,
5814 .init = hotkey_init,
5815 .data = &hotkey_driver_data,
5818 .init = bluetooth_init,
5819 .data = &bluetooth_driver_data,
5822 .init = wan_init,
5823 .data = &wan_driver_data,
5825 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5827 .init = video_init,
5828 .data = &video_driver_data,
5830 #endif
5832 .init = light_init,
5833 .data = &light_driver_data,
5835 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5837 .init = dock_init,
5838 .data = &dock_driver_data[0],
5841 .init = dock_init2,
5842 .data = &dock_driver_data[1],
5844 #endif
5845 #ifdef CONFIG_THINKPAD_ACPI_BAY
5847 .init = bay_init,
5848 .data = &bay_driver_data,
5850 #endif
5852 .init = cmos_init,
5853 .data = &cmos_driver_data,
5856 .init = led_init,
5857 .data = &led_driver_data,
5860 .init = beep_init,
5861 .data = &beep_driver_data,
5864 .init = thermal_init,
5865 .data = &thermal_driver_data,
5868 .data = &ecdump_driver_data,
5871 .init = brightness_init,
5872 .data = &brightness_driver_data,
5875 .data = &volume_driver_data,
5878 .init = fan_init,
5879 .data = &fan_driver_data,
5883 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5885 unsigned int i;
5886 struct ibm_struct *ibm;
5888 if (!kp || !kp->name || !val)
5889 return -EINVAL;
5891 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5892 ibm = ibms_init[i].data;
5893 WARN_ON(ibm == NULL);
5895 if (!ibm || !ibm->name)
5896 continue;
5898 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5899 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5900 return -ENOSPC;
5901 strcpy(ibms_init[i].param, val);
5902 strcat(ibms_init[i].param, ",");
5903 return 0;
5907 return -EINVAL;
5910 module_param(experimental, int, 0);
5911 MODULE_PARM_DESC(experimental,
5912 "Enables experimental features when non-zero");
5914 module_param_named(debug, dbg_level, uint, 0);
5915 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5917 module_param(force_load, bool, 0);
5918 MODULE_PARM_DESC(force_load,
5919 "Attempts to load the driver even on a "
5920 "mis-identified ThinkPad when true");
5922 module_param_named(fan_control, fan_control_allowed, bool, 0);
5923 MODULE_PARM_DESC(fan_control,
5924 "Enables setting fan parameters features when true");
5926 module_param_named(brightness_mode, brightness_mode, int, 0);
5927 MODULE_PARM_DESC(brightness_mode,
5928 "Selects brightness control strategy: "
5929 "0=auto, 1=EC, 2=CMOS, 3=both");
5931 module_param(brightness_enable, uint, 0);
5932 MODULE_PARM_DESC(brightness_enable,
5933 "Enables backlight control when 1, disables when 0");
5935 module_param(hotkey_report_mode, uint, 0);
5936 MODULE_PARM_DESC(hotkey_report_mode,
5937 "used for backwards compatibility with userspace, "
5938 "see documentation");
5940 #define TPACPI_PARAM(feature) \
5941 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5942 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5943 "at module load, see documentation")
5945 TPACPI_PARAM(hotkey);
5946 TPACPI_PARAM(bluetooth);
5947 TPACPI_PARAM(video);
5948 TPACPI_PARAM(light);
5949 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5950 TPACPI_PARAM(dock);
5951 #endif
5952 #ifdef CONFIG_THINKPAD_ACPI_BAY
5953 TPACPI_PARAM(bay);
5954 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5955 TPACPI_PARAM(cmos);
5956 TPACPI_PARAM(led);
5957 TPACPI_PARAM(beep);
5958 TPACPI_PARAM(ecdump);
5959 TPACPI_PARAM(brightness);
5960 TPACPI_PARAM(volume);
5961 TPACPI_PARAM(fan);
5963 static void thinkpad_acpi_module_exit(void)
5965 struct ibm_struct *ibm, *itmp;
5967 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5969 list_for_each_entry_safe_reverse(ibm, itmp,
5970 &tpacpi_all_drivers,
5971 all_drivers) {
5972 ibm_exit(ibm);
5975 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5977 if (tpacpi_inputdev) {
5978 if (tp_features.input_device_registered)
5979 input_unregister_device(tpacpi_inputdev);
5980 else
5981 input_free_device(tpacpi_inputdev);
5984 if (tpacpi_hwmon)
5985 hwmon_device_unregister(tpacpi_hwmon);
5987 if (tp_features.sensors_pdev_attrs_registered)
5988 device_remove_file(&tpacpi_sensors_pdev->dev,
5989 &dev_attr_thinkpad_acpi_pdev_name);
5990 if (tpacpi_sensors_pdev)
5991 platform_device_unregister(tpacpi_sensors_pdev);
5992 if (tpacpi_pdev)
5993 platform_device_unregister(tpacpi_pdev);
5995 if (tp_features.sensors_pdrv_attrs_registered)
5996 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5997 if (tp_features.platform_drv_attrs_registered)
5998 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6000 if (tp_features.sensors_pdrv_registered)
6001 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6003 if (tp_features.platform_drv_registered)
6004 platform_driver_unregister(&tpacpi_pdriver);
6006 if (proc_dir)
6007 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6009 kfree(thinkpad_id.bios_version_str);
6010 kfree(thinkpad_id.ec_version_str);
6011 kfree(thinkpad_id.model_str);
6015 static int __init thinkpad_acpi_module_init(void)
6017 int ret, i;
6019 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6021 /* Parameter checking */
6022 if (hotkey_report_mode > 2)
6023 return -EINVAL;
6025 /* Driver-level probe */
6027 get_thinkpad_model_data(&thinkpad_id);
6028 ret = probe_for_thinkpad();
6029 if (ret) {
6030 thinkpad_acpi_module_exit();
6031 return ret;
6034 /* Driver initialization */
6036 TPACPI_ACPIHANDLE_INIT(ecrd);
6037 TPACPI_ACPIHANDLE_INIT(ecwr);
6039 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6040 if (!proc_dir) {
6041 printk(TPACPI_ERR
6042 "unable to create proc dir " TPACPI_PROC_DIR);
6043 thinkpad_acpi_module_exit();
6044 return -ENODEV;
6046 proc_dir->owner = THIS_MODULE;
6048 ret = platform_driver_register(&tpacpi_pdriver);
6049 if (ret) {
6050 printk(TPACPI_ERR
6051 "unable to register main platform driver\n");
6052 thinkpad_acpi_module_exit();
6053 return ret;
6055 tp_features.platform_drv_registered = 1;
6057 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6058 if (ret) {
6059 printk(TPACPI_ERR
6060 "unable to register hwmon platform driver\n");
6061 thinkpad_acpi_module_exit();
6062 return ret;
6064 tp_features.sensors_pdrv_registered = 1;
6066 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6067 if (!ret) {
6068 tp_features.platform_drv_attrs_registered = 1;
6069 ret = tpacpi_create_driver_attributes(
6070 &tpacpi_hwmon_pdriver.driver);
6072 if (ret) {
6073 printk(TPACPI_ERR
6074 "unable to create sysfs driver attributes\n");
6075 thinkpad_acpi_module_exit();
6076 return ret;
6078 tp_features.sensors_pdrv_attrs_registered = 1;
6081 /* Device initialization */
6082 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6083 NULL, 0);
6084 if (IS_ERR(tpacpi_pdev)) {
6085 ret = PTR_ERR(tpacpi_pdev);
6086 tpacpi_pdev = NULL;
6087 printk(TPACPI_ERR "unable to register platform device\n");
6088 thinkpad_acpi_module_exit();
6089 return ret;
6091 tpacpi_sensors_pdev = platform_device_register_simple(
6092 TPACPI_HWMON_DRVR_NAME,
6093 -1, NULL, 0);
6094 if (IS_ERR(tpacpi_sensors_pdev)) {
6095 ret = PTR_ERR(tpacpi_sensors_pdev);
6096 tpacpi_sensors_pdev = NULL;
6097 printk(TPACPI_ERR
6098 "unable to register hwmon platform device\n");
6099 thinkpad_acpi_module_exit();
6100 return ret;
6102 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6103 &dev_attr_thinkpad_acpi_pdev_name);
6104 if (ret) {
6105 printk(TPACPI_ERR
6106 "unable to create sysfs hwmon device attributes\n");
6107 thinkpad_acpi_module_exit();
6108 return ret;
6110 tp_features.sensors_pdev_attrs_registered = 1;
6111 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6112 if (IS_ERR(tpacpi_hwmon)) {
6113 ret = PTR_ERR(tpacpi_hwmon);
6114 tpacpi_hwmon = NULL;
6115 printk(TPACPI_ERR "unable to register hwmon device\n");
6116 thinkpad_acpi_module_exit();
6117 return ret;
6119 mutex_init(&tpacpi_inputdev_send_mutex);
6120 tpacpi_inputdev = input_allocate_device();
6121 if (!tpacpi_inputdev) {
6122 printk(TPACPI_ERR "unable to allocate input device\n");
6123 thinkpad_acpi_module_exit();
6124 return -ENOMEM;
6125 } else {
6126 /* Prepare input device, but don't register */
6127 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6128 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6129 tpacpi_inputdev->id.bustype = BUS_HOST;
6130 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6131 thinkpad_id.vendor :
6132 PCI_VENDOR_ID_IBM;
6133 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6134 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6136 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6137 ret = ibm_init(&ibms_init[i]);
6138 if (ret >= 0 && *ibms_init[i].param)
6139 ret = ibms_init[i].data->write(ibms_init[i].param);
6140 if (ret < 0) {
6141 thinkpad_acpi_module_exit();
6142 return ret;
6145 ret = input_register_device(tpacpi_inputdev);
6146 if (ret < 0) {
6147 printk(TPACPI_ERR "unable to register input device\n");
6148 thinkpad_acpi_module_exit();
6149 return ret;
6150 } else {
6151 tp_features.input_device_registered = 1;
6154 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6155 return 0;
6158 /* Please remove this in year 2009 */
6159 MODULE_ALIAS("ibm_acpi");
6162 * DMI matching for module autoloading
6164 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6165 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6167 * Only models listed in thinkwiki will be supported, so add yours
6168 * if it is not there yet.
6170 #define IBM_BIOS_MODULE_ALIAS(__type) \
6171 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6173 /* Non-ancient thinkpads */
6174 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6175 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6177 /* Ancient thinkpad BIOSes have to be identified by
6178 * BIOS type or model number, and there are far less
6179 * BIOS types than model numbers... */
6180 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6181 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6182 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6184 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6185 MODULE_DESCRIPTION(TPACPI_DESC);
6186 MODULE_VERSION(TPACPI_VERSION);
6187 MODULE_LICENSE("GPL");
6189 module_init(thinkpad_acpi_module_init);
6190 module_exit(thinkpad_acpi_module_exit);