ACPI: thinkpad-acpi: prefer standard ACPI backlight level control
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobf71e7222b9ea55061fccf9b4d58e24ebc5975ed5
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 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 IBM_VERSION "0.16"
25 #define TPACPI_SYSFS_VERSION 0x020000
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 static enum {
121 TPACPI_LIFE_INIT = 0,
122 TPACPI_LIFE_RUNNING,
123 TPACPI_LIFE_EXITING,
124 } tpacpi_lifecycle;
126 /****************************************************************************
127 ****************************************************************************
129 * ACPI Helpers and device model
131 ****************************************************************************
132 ****************************************************************************/
134 /*************************************************************************
135 * ACPI basic handles
138 static acpi_handle root_handle;
140 #define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
143 static char *object##_path; \
144 static char *object##_paths[] = { paths }
146 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
155 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
159 /*************************************************************************
160 * Misc ACPI handles
163 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
166 ); /* all others */
168 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
171 ); /* 570 */
174 /*************************************************************************
175 * ACPI helpers
178 static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
181 char *fmt0 = fmt;
182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
203 res_type = *(fmt++);
205 params.count = 0;
206 params.pointer = &in_objs[0];
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
216 /* add more types as needed */
217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
223 va_end(ap);
225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
232 status = acpi_evaluate_object(handle, method, &params, resultp);
234 switch (res_type) {
235 case 'd': /* int */
236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
240 case 'v': /* void */
241 success = status == AE_OK;
242 break;
243 /* add more types as needed */
244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
254 return success;
257 static void __unused acpi_print_int(acpi_handle handle, char *method)
259 int i;
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
267 static int acpi_ec_read(int i, u8 * p)
269 int v;
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
280 return 1;
283 static int acpi_ec_write(int i, u8 v)
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
293 return 1;
296 static int _sta(acpi_handle handle)
298 int status;
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
303 return status;
306 static int issue_thinkpad_cmos_command(int cmos_cmd)
308 if (!cmos_handle)
309 return -ENXIO;
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
314 return 0;
317 /*************************************************************************
318 * ACPI device model
321 static void drv_acpi_handle_init(char *name,
322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
325 int i;
326 acpi_status status;
328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
338 return;
342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
344 *handle = NULL;
347 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
349 struct ibm_struct *ibm = data;
351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
355 return;
357 ibm->acpi->notify(ibm, event);
360 static int __init setup_acpi_notify(struct ibm_struct *ibm)
362 acpi_status status;
363 int rc;
365 BUG_ON(!ibm->acpi);
367 if (!*ibm->acpi->handle)
368 return 0;
370 vdbg_printk(TPACPI_DBG_INIT,
371 "setting up ACPI notify for %s\n", ibm->name);
373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
377 return -ENODEV;
380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
395 return -ENODEV;
397 ibm->flags.acpi_notify_installed = 1;
398 return 0;
401 static int __init tpacpi_device_add(struct acpi_device *device)
403 return 0;
406 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
408 int rc;
410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
413 BUG_ON(!ibm->acpi);
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
423 ibm->acpi->driver->ops.add = &tpacpi_device_add;
425 rc = acpi_bus_register_driver(ibm->acpi->driver);
426 if (rc < 0) {
427 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
428 ibm->acpi->hid, rc);
429 kfree(ibm->acpi->driver);
430 ibm->acpi->driver = NULL;
431 } else if (!rc)
432 ibm->flags.acpi_driver_registered = 1;
434 return rc;
438 /****************************************************************************
439 ****************************************************************************
441 * Procfs Helpers
443 ****************************************************************************
444 ****************************************************************************/
446 static int dispatch_procfs_read(char *page, char **start, off_t off,
447 int count, int *eof, void *data)
449 struct ibm_struct *ibm = data;
450 int len;
452 if (!ibm || !ibm->read)
453 return -EINVAL;
455 len = ibm->read(page);
456 if (len < 0)
457 return len;
459 if (len <= off + count)
460 *eof = 1;
461 *start = page + off;
462 len -= off;
463 if (len > count)
464 len = count;
465 if (len < 0)
466 len = 0;
468 return len;
471 static int dispatch_procfs_write(struct file *file,
472 const char __user * userbuf,
473 unsigned long count, void *data)
475 struct ibm_struct *ibm = data;
476 char *kernbuf;
477 int ret;
479 if (!ibm || !ibm->write)
480 return -EINVAL;
482 kernbuf = kmalloc(count + 2, GFP_KERNEL);
483 if (!kernbuf)
484 return -ENOMEM;
486 if (copy_from_user(kernbuf, userbuf, count)) {
487 kfree(kernbuf);
488 return -EFAULT;
491 kernbuf[count] = 0;
492 strcat(kernbuf, ",");
493 ret = ibm->write(kernbuf);
494 if (ret == 0)
495 ret = count;
497 kfree(kernbuf);
499 return ret;
502 static char *next_cmd(char **cmds)
504 char *start = *cmds;
505 char *end;
507 while ((end = strchr(start, ',')) && end == start)
508 start = end + 1;
510 if (!end)
511 return NULL;
513 *end = 0;
514 *cmds = end + 1;
515 return start;
519 /****************************************************************************
520 ****************************************************************************
522 * Device model: input, hwmon and platform
524 ****************************************************************************
525 ****************************************************************************/
527 static struct platform_device *tpacpi_pdev;
528 static struct platform_device *tpacpi_sensors_pdev;
529 static struct class_device *tpacpi_hwmon;
530 static struct input_dev *tpacpi_inputdev;
531 static struct mutex tpacpi_inputdev_send_mutex;
534 static int tpacpi_resume_handler(struct platform_device *pdev)
536 struct ibm_struct *ibm, *itmp;
538 list_for_each_entry_safe(ibm, itmp,
539 &tpacpi_all_drivers,
540 all_drivers) {
541 if (ibm->resume)
542 (ibm->resume)();
545 return 0;
548 static struct platform_driver tpacpi_pdriver = {
549 .driver = {
550 .name = IBM_DRVR_NAME,
551 .owner = THIS_MODULE,
553 .resume = tpacpi_resume_handler,
556 static struct platform_driver tpacpi_hwmon_pdriver = {
557 .driver = {
558 .name = IBM_HWMON_DRVR_NAME,
559 .owner = THIS_MODULE,
563 /*************************************************************************
564 * thinkpad-acpi driver attributes
567 /* interface_version --------------------------------------------------- */
568 static ssize_t tpacpi_driver_interface_version_show(
569 struct device_driver *drv,
570 char *buf)
572 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
575 static DRIVER_ATTR(interface_version, S_IRUGO,
576 tpacpi_driver_interface_version_show, NULL);
578 /* debug_level --------------------------------------------------------- */
579 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
580 char *buf)
582 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
585 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
586 const char *buf, size_t count)
588 unsigned long t;
590 if (parse_strtoul(buf, 0xffff, &t))
591 return -EINVAL;
593 dbg_level = t;
595 return count;
598 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
599 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
601 /* version ------------------------------------------------------------- */
602 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
603 char *buf)
605 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
608 static DRIVER_ATTR(version, S_IRUGO,
609 tpacpi_driver_version_show, NULL);
611 /* --------------------------------------------------------------------- */
613 static struct driver_attribute* tpacpi_driver_attributes[] = {
614 &driver_attr_debug_level, &driver_attr_version,
615 &driver_attr_interface_version,
618 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
620 int i, res;
622 i = 0;
623 res = 0;
624 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
625 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
626 i++;
629 return res;
632 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
634 int i;
636 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
637 driver_remove_file(drv, tpacpi_driver_attributes[i]);
640 /*************************************************************************
641 * sysfs support helpers
644 struct attribute_set_obj {
645 struct attribute_set s;
646 struct attribute *a;
647 } __attribute__((packed));
649 static struct attribute_set *create_attr_set(unsigned int max_members,
650 const char* name)
652 struct attribute_set_obj *sobj;
654 if (max_members == 0)
655 return NULL;
657 /* Allocates space for implicit NULL at the end too */
658 sobj = kzalloc(sizeof(struct attribute_set_obj) +
659 max_members * sizeof(struct attribute *),
660 GFP_KERNEL);
661 if (!sobj)
662 return NULL;
663 sobj->s.max_members = max_members;
664 sobj->s.group.attrs = &sobj->a;
665 sobj->s.group.name = name;
667 return &sobj->s;
670 /* not multi-threaded safe, use it in a single thread per set */
671 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
673 if (!s || !attr)
674 return -EINVAL;
676 if (s->members >= s->max_members)
677 return -ENOMEM;
679 s->group.attrs[s->members] = attr;
680 s->members++;
682 return 0;
685 static int add_many_to_attr_set(struct attribute_set* s,
686 struct attribute **attr,
687 unsigned int count)
689 int i, res;
691 for (i = 0; i < count; i++) {
692 res = add_to_attr_set(s, attr[i]);
693 if (res)
694 return res;
697 return 0;
700 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
702 sysfs_remove_group(kobj, &s->group);
703 destroy_attr_set(s);
706 static int parse_strtoul(const char *buf,
707 unsigned long max, unsigned long *value)
709 char *endp;
711 while (*buf && isspace(*buf))
712 buf++;
713 *value = simple_strtoul(buf, &endp, 0);
714 while (*endp && isspace(*endp))
715 endp++;
716 if (*endp || *value > max)
717 return -EINVAL;
719 return 0;
722 /****************************************************************************
723 ****************************************************************************
725 * Subdrivers
727 ****************************************************************************
728 ****************************************************************************/
730 /*************************************************************************
731 * thinkpad-acpi init subdriver
734 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
736 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
737 printk(IBM_INFO "%s\n", IBM_URL);
739 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
740 (thinkpad_id.bios_version_str) ?
741 thinkpad_id.bios_version_str : "unknown",
742 (thinkpad_id.ec_version_str) ?
743 thinkpad_id.ec_version_str : "unknown");
745 if (thinkpad_id.vendor && thinkpad_id.model_str)
746 printk(IBM_INFO "%s %s\n",
747 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
748 "IBM" : ((thinkpad_id.vendor ==
749 PCI_VENDOR_ID_LENOVO) ?
750 "Lenovo" : "Unknown vendor"),
751 thinkpad_id.model_str);
753 return 0;
756 static int thinkpad_acpi_driver_read(char *p)
758 int len = 0;
760 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
761 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
763 return len;
766 static struct ibm_struct thinkpad_acpi_driver_data = {
767 .name = "driver",
768 .read = thinkpad_acpi_driver_read,
771 /*************************************************************************
772 * Hotkey subdriver
775 static int hotkey_orig_status;
776 static u32 hotkey_orig_mask;
777 static u32 hotkey_all_mask;
778 static u32 hotkey_reserved_mask;
780 static u16 *hotkey_keycode_map;
782 static struct attribute_set *hotkey_dev_attributes;
784 static int hotkey_get_wlsw(int *status)
786 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
787 return -EIO;
788 return 0;
791 /* sysfs hotkey enable ------------------------------------------------- */
792 static ssize_t hotkey_enable_show(struct device *dev,
793 struct device_attribute *attr,
794 char *buf)
796 int res, status;
797 u32 mask;
799 res = hotkey_get(&status, &mask);
800 if (res)
801 return res;
803 return snprintf(buf, PAGE_SIZE, "%d\n", status);
806 static ssize_t hotkey_enable_store(struct device *dev,
807 struct device_attribute *attr,
808 const char *buf, size_t count)
810 unsigned long t;
811 int res, status;
812 u32 mask;
814 if (parse_strtoul(buf, 1, &t))
815 return -EINVAL;
817 res = hotkey_get(&status, &mask);
818 if (!res)
819 res = hotkey_set(t, mask);
821 return (res) ? res : count;
824 static struct device_attribute dev_attr_hotkey_enable =
825 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
826 hotkey_enable_show, hotkey_enable_store);
828 /* sysfs hotkey mask --------------------------------------------------- */
829 static ssize_t hotkey_mask_show(struct device *dev,
830 struct device_attribute *attr,
831 char *buf)
833 int res, status;
834 u32 mask;
836 res = hotkey_get(&status, &mask);
837 if (res)
838 return res;
840 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
843 static ssize_t hotkey_mask_store(struct device *dev,
844 struct device_attribute *attr,
845 const char *buf, size_t count)
847 unsigned long t;
848 int res, status;
849 u32 mask;
851 if (parse_strtoul(buf, 0xffffffffUL, &t))
852 return -EINVAL;
854 res = hotkey_get(&status, &mask);
855 if (!res)
856 hotkey_set(status, t);
858 return (res) ? res : count;
861 static struct device_attribute dev_attr_hotkey_mask =
862 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
863 hotkey_mask_show, hotkey_mask_store);
865 /* sysfs hotkey bios_enabled ------------------------------------------- */
866 static ssize_t hotkey_bios_enabled_show(struct device *dev,
867 struct device_attribute *attr,
868 char *buf)
870 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
873 static struct device_attribute dev_attr_hotkey_bios_enabled =
874 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
876 /* sysfs hotkey bios_mask ---------------------------------------------- */
877 static ssize_t hotkey_bios_mask_show(struct device *dev,
878 struct device_attribute *attr,
879 char *buf)
881 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
884 static struct device_attribute dev_attr_hotkey_bios_mask =
885 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
887 /* sysfs hotkey all_mask ----------------------------------------------- */
888 static ssize_t hotkey_all_mask_show(struct device *dev,
889 struct device_attribute *attr,
890 char *buf)
892 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
895 static struct device_attribute dev_attr_hotkey_all_mask =
896 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
898 /* sysfs hotkey recommended_mask --------------------------------------- */
899 static ssize_t hotkey_recommended_mask_show(struct device *dev,
900 struct device_attribute *attr,
901 char *buf)
903 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
904 hotkey_all_mask & ~hotkey_reserved_mask);
907 static struct device_attribute dev_attr_hotkey_recommended_mask =
908 __ATTR(hotkey_recommended_mask, S_IRUGO,
909 hotkey_recommended_mask_show, NULL);
911 /* sysfs hotkey radio_sw ----------------------------------------------- */
912 static ssize_t hotkey_radio_sw_show(struct device *dev,
913 struct device_attribute *attr,
914 char *buf)
916 int res, s;
917 res = hotkey_get_wlsw(&s);
918 if (res < 0)
919 return res;
921 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
924 static struct device_attribute dev_attr_hotkey_radio_sw =
925 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
927 /* sysfs hotkey report_mode -------------------------------------------- */
928 static ssize_t hotkey_report_mode_show(struct device *dev,
929 struct device_attribute *attr,
930 char *buf)
932 return snprintf(buf, PAGE_SIZE, "%d\n",
933 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
936 static ssize_t hotkey_report_mode_store(struct device *dev,
937 struct device_attribute *attr,
938 const char *buf, size_t count)
940 unsigned long t;
941 int res = 0;
943 if (parse_strtoul(buf, 2, &t) || (t < 1))
944 return -EINVAL;
946 if (t != hotkey_report_mode) {
947 if (tp_features.hotkey_report_mode_locked)
948 return -EPERM;
949 hotkey_report_mode = t;
950 printk(IBM_NOTICE "hot key report mode set to %d\n",
951 hotkey_report_mode);
954 return (res) ? res : count;
957 static struct device_attribute dev_attr_hotkey_report_mode =
958 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
959 hotkey_report_mode_show, hotkey_report_mode_store);
961 /* --------------------------------------------------------------------- */
963 static struct attribute *hotkey_attributes[] __initdata = {
964 &dev_attr_hotkey_enable.attr,
965 &dev_attr_hotkey_report_mode.attr,
968 static struct attribute *hotkey_mask_attributes[] __initdata = {
969 &dev_attr_hotkey_mask.attr,
970 &dev_attr_hotkey_bios_enabled.attr,
971 &dev_attr_hotkey_bios_mask.attr,
972 &dev_attr_hotkey_all_mask.attr,
973 &dev_attr_hotkey_recommended_mask.attr,
976 static int __init hotkey_init(struct ibm_init_struct *iibm)
979 static u16 ibm_keycode_map[] __initdata = {
980 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
981 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
982 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
983 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
984 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
985 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
986 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
987 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
988 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
989 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
990 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
991 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
992 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
993 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
994 KEY_RESERVED, /* 0x14: VOLUME UP */
995 KEY_RESERVED, /* 0x15: VOLUME DOWN */
996 KEY_RESERVED, /* 0x16: MUTE */
997 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
998 /* (assignments unknown, please report if found) */
999 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1000 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1002 static u16 lenovo_keycode_map[] __initdata = {
1003 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1004 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1005 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1006 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1007 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1008 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1009 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1010 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1011 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1012 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1013 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1014 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1015 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1016 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1017 KEY_RESERVED, /* 0x14: VOLUME UP */
1018 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1019 KEY_RESERVED, /* 0x16: MUTE */
1020 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1021 /* (assignments unknown, please report if found) */
1022 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1023 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1026 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1027 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1028 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1030 int res, i;
1031 int status;
1032 int hkeyv;
1034 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1036 BUG_ON(!tpacpi_inputdev);
1038 IBM_ACPIHANDLE_INIT(hkey);
1039 mutex_init(&hotkey_mutex);
1041 /* hotkey not supported on 570 */
1042 tp_features.hotkey = hkey_handle != NULL;
1044 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1045 str_supported(tp_features.hotkey));
1047 if (tp_features.hotkey) {
1048 hotkey_dev_attributes = create_attr_set(8, NULL);
1049 if (!hotkey_dev_attributes)
1050 return -ENOMEM;
1051 res = add_many_to_attr_set(hotkey_dev_attributes,
1052 hotkey_attributes,
1053 ARRAY_SIZE(hotkey_attributes));
1054 if (res)
1055 return res;
1057 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1058 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1059 for HKEY interface version 0x100 */
1060 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1061 if ((hkeyv >> 8) != 1) {
1062 printk(IBM_ERR "unknown version of the "
1063 "HKEY interface: 0x%x\n", hkeyv);
1064 printk(IBM_ERR "please report this to %s\n",
1065 IBM_MAIL);
1066 } else {
1068 * MHKV 0x100 in A31, R40, R40e,
1069 * T4x, X31, and later
1070 * */
1071 tp_features.hotkey_mask = 1;
1075 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1076 str_supported(tp_features.hotkey_mask));
1078 if (tp_features.hotkey_mask) {
1079 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1080 "MHKA", "qd")) {
1081 printk(IBM_ERR
1082 "missing MHKA handler, "
1083 "please report this to %s\n",
1084 IBM_MAIL);
1085 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1089 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1090 if (!res && tp_features.hotkey_mask) {
1091 res = add_many_to_attr_set(hotkey_dev_attributes,
1092 hotkey_mask_attributes,
1093 ARRAY_SIZE(hotkey_mask_attributes));
1096 /* Not all thinkpads have a hardware radio switch */
1097 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1098 tp_features.hotkey_wlsw = 1;
1099 printk(IBM_INFO
1100 "radio switch found; radios are %s\n",
1101 enabled(status, 0));
1102 res = add_to_attr_set(hotkey_dev_attributes,
1103 &dev_attr_hotkey_radio_sw.attr);
1106 if (!res)
1107 res = register_attr_set_with_sysfs(
1108 hotkey_dev_attributes,
1109 &tpacpi_pdev->dev.kobj);
1110 if (res)
1111 return res;
1113 /* Set up key map */
1115 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1116 GFP_KERNEL);
1117 if (!hotkey_keycode_map) {
1118 printk(IBM_ERR "failed to allocate memory for key map\n");
1119 return -ENOMEM;
1122 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1123 dbg_printk(TPACPI_DBG_INIT,
1124 "using Lenovo default hot key map\n");
1125 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1126 TPACPI_HOTKEY_MAP_SIZE);
1127 } else {
1128 dbg_printk(TPACPI_DBG_INIT,
1129 "using IBM default hot key map\n");
1130 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1131 TPACPI_HOTKEY_MAP_SIZE);
1134 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1135 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1136 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1137 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1138 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1139 tpacpi_inputdev->keycode = hotkey_keycode_map;
1140 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1141 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1142 set_bit(hotkey_keycode_map[i],
1143 tpacpi_inputdev->keybit);
1144 } else {
1145 if (i < sizeof(hotkey_reserved_mask)*8)
1146 hotkey_reserved_mask |= 1 << i;
1150 if (tp_features.hotkey_wlsw) {
1151 set_bit(EV_SW, tpacpi_inputdev->evbit);
1152 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1155 dbg_printk(TPACPI_DBG_INIT,
1156 "enabling hot key handling\n");
1157 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1158 | hotkey_orig_mask);
1159 if (res)
1160 return res;
1162 if (hotkey_report_mode > 0) {
1163 tp_features.hotkey_report_mode_locked = 1;
1164 } else {
1165 hotkey_report_mode = 1;
1166 vdbg_printk(TPACPI_DBG_INIT,
1167 "hot key reporting mode can be "
1168 "changed at runtime\n");
1171 dbg_printk(TPACPI_DBG_INIT,
1172 "legacy hot key reporting over procfs %s\n",
1173 (hotkey_report_mode < 2) ?
1174 "enabled" : "disabled");
1177 return (tp_features.hotkey)? 0 : 1;
1180 static void hotkey_exit(void)
1182 int res;
1184 if (tp_features.hotkey) {
1185 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1186 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1187 if (res)
1188 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1191 if (hotkey_dev_attributes) {
1192 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1193 hotkey_dev_attributes = NULL;
1197 static void tpacpi_input_send_key(unsigned int scancode,
1198 unsigned int keycode)
1200 if (keycode != KEY_RESERVED) {
1201 mutex_lock(&tpacpi_inputdev_send_mutex);
1203 input_report_key(tpacpi_inputdev, keycode, 1);
1204 if (keycode == KEY_UNKNOWN)
1205 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1206 scancode);
1207 input_sync(tpacpi_inputdev);
1209 input_report_key(tpacpi_inputdev, keycode, 0);
1210 if (keycode == KEY_UNKNOWN)
1211 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1212 scancode);
1213 input_sync(tpacpi_inputdev);
1215 mutex_unlock(&tpacpi_inputdev_send_mutex);
1219 static void tpacpi_input_send_radiosw(void)
1221 int wlsw;
1223 mutex_lock(&tpacpi_inputdev_send_mutex);
1225 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1226 input_report_switch(tpacpi_inputdev,
1227 SW_RADIO, !!wlsw);
1228 input_sync(tpacpi_inputdev);
1231 mutex_unlock(&tpacpi_inputdev_send_mutex);
1234 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1236 u32 hkey;
1237 unsigned int keycode, scancode;
1238 int send_acpi_ev;
1239 int ignore_acpi_ev;
1241 if (event != 0x80) {
1242 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1243 /* forward it to userspace, maybe it knows how to handle it */
1244 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1246 return;
1249 while (1) {
1250 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1251 printk(IBM_ERR "failed to retrieve HKEY event\n");
1252 return;
1255 if (hkey == 0) {
1256 /* queue empty */
1257 return;
1260 send_acpi_ev = 0;
1261 ignore_acpi_ev = 0;
1263 switch (hkey >> 12) {
1264 case 1:
1265 /* 0x1000-0x1FFF: key presses */
1266 scancode = hkey & 0xfff;
1267 if (scancode > 0 && scancode < 0x21) {
1268 scancode--;
1269 keycode = hotkey_keycode_map[scancode];
1270 tpacpi_input_send_key(scancode, keycode);
1271 } else {
1272 printk(IBM_ERR
1273 "hotkey 0x%04x out of range for keyboard map\n",
1274 hkey);
1275 send_acpi_ev = 1;
1277 break;
1278 case 5:
1279 /* 0x5000-0x5FFF: LID */
1280 /* we don't handle it through this path, just
1281 * eat up known LID events */
1282 if (hkey != 0x5001 && hkey != 0x5002) {
1283 printk(IBM_ERR
1284 "unknown LID-related HKEY event: 0x%04x\n",
1285 hkey);
1286 send_acpi_ev = 1;
1287 } else {
1288 ignore_acpi_ev = 1;
1290 break;
1291 case 7:
1292 /* 0x7000-0x7FFF: misc */
1293 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1294 tpacpi_input_send_radiosw();
1295 break;
1297 /* fallthrough to default */
1298 default:
1299 /* case 2: dock-related */
1300 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1301 /* case 3: ultra-bay related. maybe bay in dock? */
1302 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1303 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1304 send_acpi_ev = 1;
1307 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
1308 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1313 static void hotkey_resume(void)
1315 tpacpi_input_send_radiosw();
1319 * Call with hotkey_mutex held
1321 static int hotkey_get(int *status, u32 *mask)
1323 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1324 return -EIO;
1326 if (tp_features.hotkey_mask)
1327 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1328 return -EIO;
1330 return 0;
1334 * Call with hotkey_mutex held
1336 static int hotkey_set(int status, u32 mask)
1338 int i;
1340 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1341 return -EIO;
1343 if (tp_features.hotkey_mask)
1344 for (i = 0; i < 32; i++) {
1345 int bit = ((1 << i) & mask) != 0;
1346 if (!acpi_evalf(hkey_handle,
1347 NULL, "MHKM", "vdd", i + 1, bit))
1348 return -EIO;
1351 return 0;
1354 /* procfs -------------------------------------------------------------- */
1355 static int hotkey_read(char *p)
1357 int res, status;
1358 u32 mask;
1359 int len = 0;
1361 if (!tp_features.hotkey) {
1362 len += sprintf(p + len, "status:\t\tnot supported\n");
1363 return len;
1366 res = mutex_lock_interruptible(&hotkey_mutex);
1367 if (res < 0)
1368 return res;
1369 res = hotkey_get(&status, &mask);
1370 mutex_unlock(&hotkey_mutex);
1371 if (res)
1372 return res;
1374 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1375 if (tp_features.hotkey_mask) {
1376 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1377 len += sprintf(p + len,
1378 "commands:\tenable, disable, reset, <mask>\n");
1379 } else {
1380 len += sprintf(p + len, "mask:\t\tnot supported\n");
1381 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1384 return len;
1387 static int hotkey_write(char *buf)
1389 int res, status;
1390 u32 mask;
1391 char *cmd;
1392 int do_cmd = 0;
1394 if (!tp_features.hotkey)
1395 return -ENODEV;
1397 res = mutex_lock_interruptible(&hotkey_mutex);
1398 if (res < 0)
1399 return res;
1401 res = hotkey_get(&status, &mask);
1402 if (res)
1403 goto errexit;
1405 res = 0;
1406 while ((cmd = next_cmd(&buf))) {
1407 if (strlencmp(cmd, "enable") == 0) {
1408 status = 1;
1409 } else if (strlencmp(cmd, "disable") == 0) {
1410 status = 0;
1411 } else if (strlencmp(cmd, "reset") == 0) {
1412 status = hotkey_orig_status;
1413 mask = hotkey_orig_mask;
1414 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1415 /* mask set */
1416 } else if (sscanf(cmd, "%x", &mask) == 1) {
1417 /* mask set */
1418 } else {
1419 res = -EINVAL;
1420 goto errexit;
1422 do_cmd = 1;
1425 if (do_cmd)
1426 res = hotkey_set(status, mask);
1428 errexit:
1429 mutex_unlock(&hotkey_mutex);
1430 return res;
1433 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1434 .hid = IBM_HKEY_HID,
1435 .notify = hotkey_notify,
1436 .handle = &hkey_handle,
1437 .type = ACPI_DEVICE_NOTIFY,
1440 static struct ibm_struct hotkey_driver_data = {
1441 .name = "hotkey",
1442 .read = hotkey_read,
1443 .write = hotkey_write,
1444 .exit = hotkey_exit,
1445 .resume = hotkey_resume,
1446 .acpi = &ibm_hotkey_acpidriver,
1449 /*************************************************************************
1450 * Bluetooth subdriver
1453 /* sysfs bluetooth enable ---------------------------------------------- */
1454 static ssize_t bluetooth_enable_show(struct device *dev,
1455 struct device_attribute *attr,
1456 char *buf)
1458 int status;
1460 status = bluetooth_get_radiosw();
1461 if (status < 0)
1462 return status;
1464 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1467 static ssize_t bluetooth_enable_store(struct device *dev,
1468 struct device_attribute *attr,
1469 const char *buf, size_t count)
1471 unsigned long t;
1472 int res;
1474 if (parse_strtoul(buf, 1, &t))
1475 return -EINVAL;
1477 res = bluetooth_set_radiosw(t);
1479 return (res) ? res : count;
1482 static struct device_attribute dev_attr_bluetooth_enable =
1483 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1484 bluetooth_enable_show, bluetooth_enable_store);
1486 /* --------------------------------------------------------------------- */
1488 static struct attribute *bluetooth_attributes[] = {
1489 &dev_attr_bluetooth_enable.attr,
1490 NULL
1493 static const struct attribute_group bluetooth_attr_group = {
1494 .attrs = bluetooth_attributes,
1497 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1499 int res;
1500 int status = 0;
1502 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1504 IBM_ACPIHANDLE_INIT(hkey);
1506 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1507 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1508 tp_features.bluetooth = hkey_handle &&
1509 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1511 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1512 str_supported(tp_features.bluetooth),
1513 status);
1515 if (tp_features.bluetooth) {
1516 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1517 /* no bluetooth hardware present in system */
1518 tp_features.bluetooth = 0;
1519 dbg_printk(TPACPI_DBG_INIT,
1520 "bluetooth hardware not installed\n");
1521 } else {
1522 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1523 &bluetooth_attr_group);
1524 if (res)
1525 return res;
1529 return (tp_features.bluetooth)? 0 : 1;
1532 static void bluetooth_exit(void)
1534 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1535 &bluetooth_attr_group);
1538 static int bluetooth_get_radiosw(void)
1540 int status;
1542 if (!tp_features.bluetooth)
1543 return -ENODEV;
1545 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1546 return -EIO;
1548 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1551 static int bluetooth_set_radiosw(int radio_on)
1553 int status;
1555 if (!tp_features.bluetooth)
1556 return -ENODEV;
1558 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1559 return -EIO;
1560 if (radio_on)
1561 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1562 else
1563 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1564 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1565 return -EIO;
1567 return 0;
1570 /* procfs -------------------------------------------------------------- */
1571 static int bluetooth_read(char *p)
1573 int len = 0;
1574 int status = bluetooth_get_radiosw();
1576 if (!tp_features.bluetooth)
1577 len += sprintf(p + len, "status:\t\tnot supported\n");
1578 else {
1579 len += sprintf(p + len, "status:\t\t%s\n",
1580 (status)? "enabled" : "disabled");
1581 len += sprintf(p + len, "commands:\tenable, disable\n");
1584 return len;
1587 static int bluetooth_write(char *buf)
1589 char *cmd;
1591 if (!tp_features.bluetooth)
1592 return -ENODEV;
1594 while ((cmd = next_cmd(&buf))) {
1595 if (strlencmp(cmd, "enable") == 0) {
1596 bluetooth_set_radiosw(1);
1597 } else if (strlencmp(cmd, "disable") == 0) {
1598 bluetooth_set_radiosw(0);
1599 } else
1600 return -EINVAL;
1603 return 0;
1606 static struct ibm_struct bluetooth_driver_data = {
1607 .name = "bluetooth",
1608 .read = bluetooth_read,
1609 .write = bluetooth_write,
1610 .exit = bluetooth_exit,
1613 /*************************************************************************
1614 * Wan subdriver
1617 /* sysfs wan enable ---------------------------------------------------- */
1618 static ssize_t wan_enable_show(struct device *dev,
1619 struct device_attribute *attr,
1620 char *buf)
1622 int status;
1624 status = wan_get_radiosw();
1625 if (status < 0)
1626 return status;
1628 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1631 static ssize_t wan_enable_store(struct device *dev,
1632 struct device_attribute *attr,
1633 const char *buf, size_t count)
1635 unsigned long t;
1636 int res;
1638 if (parse_strtoul(buf, 1, &t))
1639 return -EINVAL;
1641 res = wan_set_radiosw(t);
1643 return (res) ? res : count;
1646 static struct device_attribute dev_attr_wan_enable =
1647 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1648 wan_enable_show, wan_enable_store);
1650 /* --------------------------------------------------------------------- */
1652 static struct attribute *wan_attributes[] = {
1653 &dev_attr_wan_enable.attr,
1654 NULL
1657 static const struct attribute_group wan_attr_group = {
1658 .attrs = wan_attributes,
1661 static int __init wan_init(struct ibm_init_struct *iibm)
1663 int res;
1664 int status = 0;
1666 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1668 IBM_ACPIHANDLE_INIT(hkey);
1670 tp_features.wan = hkey_handle &&
1671 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1673 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1674 str_supported(tp_features.wan),
1675 status);
1677 if (tp_features.wan) {
1678 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1679 /* no wan hardware present in system */
1680 tp_features.wan = 0;
1681 dbg_printk(TPACPI_DBG_INIT,
1682 "wan hardware not installed\n");
1683 } else {
1684 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1685 &wan_attr_group);
1686 if (res)
1687 return res;
1691 return (tp_features.wan)? 0 : 1;
1694 static void wan_exit(void)
1696 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1697 &wan_attr_group);
1700 static int wan_get_radiosw(void)
1702 int status;
1704 if (!tp_features.wan)
1705 return -ENODEV;
1707 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1708 return -EIO;
1710 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1713 static int wan_set_radiosw(int radio_on)
1715 int status;
1717 if (!tp_features.wan)
1718 return -ENODEV;
1720 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1721 return -EIO;
1722 if (radio_on)
1723 status |= TP_ACPI_WANCARD_RADIOSSW;
1724 else
1725 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1726 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1727 return -EIO;
1729 return 0;
1732 /* procfs -------------------------------------------------------------- */
1733 static int wan_read(char *p)
1735 int len = 0;
1736 int status = wan_get_radiosw();
1738 if (!tp_features.wan)
1739 len += sprintf(p + len, "status:\t\tnot supported\n");
1740 else {
1741 len += sprintf(p + len, "status:\t\t%s\n",
1742 (status)? "enabled" : "disabled");
1743 len += sprintf(p + len, "commands:\tenable, disable\n");
1746 return len;
1749 static int wan_write(char *buf)
1751 char *cmd;
1753 if (!tp_features.wan)
1754 return -ENODEV;
1756 while ((cmd = next_cmd(&buf))) {
1757 if (strlencmp(cmd, "enable") == 0) {
1758 wan_set_radiosw(1);
1759 } else if (strlencmp(cmd, "disable") == 0) {
1760 wan_set_radiosw(0);
1761 } else
1762 return -EINVAL;
1765 return 0;
1768 static struct ibm_struct wan_driver_data = {
1769 .name = "wan",
1770 .read = wan_read,
1771 .write = wan_write,
1772 .exit = wan_exit,
1773 .flags.experimental = 1,
1776 /*************************************************************************
1777 * Video subdriver
1780 static enum video_access_mode video_supported;
1781 static int video_orig_autosw;
1783 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1784 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1785 "\\_SB.PCI0.VID0", /* 770e */
1786 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1787 "\\_SB.PCI0.AGP.VID", /* all others */
1788 ); /* R30, R31 */
1790 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1792 static int __init video_init(struct ibm_init_struct *iibm)
1794 int ivga;
1796 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1798 IBM_ACPIHANDLE_INIT(vid);
1799 IBM_ACPIHANDLE_INIT(vid2);
1801 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1802 /* G41, assume IVGA doesn't change */
1803 vid_handle = vid2_handle;
1805 if (!vid_handle)
1806 /* video switching not supported on R30, R31 */
1807 video_supported = TPACPI_VIDEO_NONE;
1808 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1809 /* 570 */
1810 video_supported = TPACPI_VIDEO_570;
1811 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1812 /* 600e/x, 770e, 770x */
1813 video_supported = TPACPI_VIDEO_770;
1814 else
1815 /* all others */
1816 video_supported = TPACPI_VIDEO_NEW;
1818 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1819 str_supported(video_supported != TPACPI_VIDEO_NONE),
1820 video_supported);
1822 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1825 static void video_exit(void)
1827 dbg_printk(TPACPI_DBG_EXIT,
1828 "restoring original video autoswitch mode\n");
1829 if (video_autosw_set(video_orig_autosw))
1830 printk(IBM_ERR "error while trying to restore original "
1831 "video autoswitch mode\n");
1834 static int video_outputsw_get(void)
1836 int status = 0;
1837 int i;
1839 switch (video_supported) {
1840 case TPACPI_VIDEO_570:
1841 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1842 TP_ACPI_VIDEO_570_PHSCMD))
1843 return -EIO;
1844 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1845 break;
1846 case TPACPI_VIDEO_770:
1847 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1848 return -EIO;
1849 if (i)
1850 status |= TP_ACPI_VIDEO_S_LCD;
1851 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1852 return -EIO;
1853 if (i)
1854 status |= TP_ACPI_VIDEO_S_CRT;
1855 break;
1856 case TPACPI_VIDEO_NEW:
1857 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1858 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1859 return -EIO;
1860 if (i)
1861 status |= TP_ACPI_VIDEO_S_CRT;
1863 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1864 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1865 return -EIO;
1866 if (i)
1867 status |= TP_ACPI_VIDEO_S_LCD;
1868 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1869 return -EIO;
1870 if (i)
1871 status |= TP_ACPI_VIDEO_S_DVI;
1872 break;
1873 default:
1874 return -ENOSYS;
1877 return status;
1880 static int video_outputsw_set(int status)
1882 int autosw;
1883 int res = 0;
1885 switch (video_supported) {
1886 case TPACPI_VIDEO_570:
1887 res = acpi_evalf(NULL, NULL,
1888 "\\_SB.PHS2", "vdd",
1889 TP_ACPI_VIDEO_570_PHS2CMD,
1890 status | TP_ACPI_VIDEO_570_PHS2SET);
1891 break;
1892 case TPACPI_VIDEO_770:
1893 autosw = video_autosw_get();
1894 if (autosw < 0)
1895 return autosw;
1897 res = video_autosw_set(1);
1898 if (res)
1899 return res;
1900 res = acpi_evalf(vid_handle, NULL,
1901 "ASWT", "vdd", status * 0x100, 0);
1902 if (!autosw && video_autosw_set(autosw)) {
1903 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1904 return -EIO;
1906 break;
1907 case TPACPI_VIDEO_NEW:
1908 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1909 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1910 break;
1911 default:
1912 return -ENOSYS;
1915 return (res)? 0 : -EIO;
1918 static int video_autosw_get(void)
1920 int autosw = 0;
1922 switch (video_supported) {
1923 case TPACPI_VIDEO_570:
1924 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1925 return -EIO;
1926 break;
1927 case TPACPI_VIDEO_770:
1928 case TPACPI_VIDEO_NEW:
1929 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1930 return -EIO;
1931 break;
1932 default:
1933 return -ENOSYS;
1936 return autosw & 1;
1939 static int video_autosw_set(int enable)
1941 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1942 return -EIO;
1943 return 0;
1946 static int video_outputsw_cycle(void)
1948 int autosw = video_autosw_get();
1949 int res;
1951 if (autosw < 0)
1952 return autosw;
1954 switch (video_supported) {
1955 case TPACPI_VIDEO_570:
1956 res = video_autosw_set(1);
1957 if (res)
1958 return res;
1959 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1960 break;
1961 case TPACPI_VIDEO_770:
1962 case TPACPI_VIDEO_NEW:
1963 res = video_autosw_set(1);
1964 if (res)
1965 return res;
1966 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1967 break;
1968 default:
1969 return -ENOSYS;
1971 if (!autosw && video_autosw_set(autosw)) {
1972 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1973 return -EIO;
1976 return (res)? 0 : -EIO;
1979 static int video_expand_toggle(void)
1981 switch (video_supported) {
1982 case TPACPI_VIDEO_570:
1983 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1984 0 : -EIO;
1985 case TPACPI_VIDEO_770:
1986 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1987 0 : -EIO;
1988 case TPACPI_VIDEO_NEW:
1989 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1990 0 : -EIO;
1991 default:
1992 return -ENOSYS;
1994 /* not reached */
1997 static int video_read(char *p)
1999 int status, autosw;
2000 int len = 0;
2002 if (video_supported == TPACPI_VIDEO_NONE) {
2003 len += sprintf(p + len, "status:\t\tnot supported\n");
2004 return len;
2007 status = video_outputsw_get();
2008 if (status < 0)
2009 return status;
2011 autosw = video_autosw_get();
2012 if (autosw < 0)
2013 return autosw;
2015 len += sprintf(p + len, "status:\t\tsupported\n");
2016 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2017 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2018 if (video_supported == TPACPI_VIDEO_NEW)
2019 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2020 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2021 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2022 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2023 if (video_supported == TPACPI_VIDEO_NEW)
2024 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2025 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2026 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2028 return len;
2031 static int video_write(char *buf)
2033 char *cmd;
2034 int enable, disable, status;
2035 int res;
2037 if (video_supported == TPACPI_VIDEO_NONE)
2038 return -ENODEV;
2040 enable = 0;
2041 disable = 0;
2043 while ((cmd = next_cmd(&buf))) {
2044 if (strlencmp(cmd, "lcd_enable") == 0) {
2045 enable |= TP_ACPI_VIDEO_S_LCD;
2046 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2047 disable |= TP_ACPI_VIDEO_S_LCD;
2048 } else if (strlencmp(cmd, "crt_enable") == 0) {
2049 enable |= TP_ACPI_VIDEO_S_CRT;
2050 } else if (strlencmp(cmd, "crt_disable") == 0) {
2051 disable |= TP_ACPI_VIDEO_S_CRT;
2052 } else if (video_supported == TPACPI_VIDEO_NEW &&
2053 strlencmp(cmd, "dvi_enable") == 0) {
2054 enable |= TP_ACPI_VIDEO_S_DVI;
2055 } else if (video_supported == TPACPI_VIDEO_NEW &&
2056 strlencmp(cmd, "dvi_disable") == 0) {
2057 disable |= TP_ACPI_VIDEO_S_DVI;
2058 } else if (strlencmp(cmd, "auto_enable") == 0) {
2059 res = video_autosw_set(1);
2060 if (res)
2061 return res;
2062 } else if (strlencmp(cmd, "auto_disable") == 0) {
2063 res = video_autosw_set(0);
2064 if (res)
2065 return res;
2066 } else if (strlencmp(cmd, "video_switch") == 0) {
2067 res = video_outputsw_cycle();
2068 if (res)
2069 return res;
2070 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2071 res = video_expand_toggle();
2072 if (res)
2073 return res;
2074 } else
2075 return -EINVAL;
2078 if (enable || disable) {
2079 status = video_outputsw_get();
2080 if (status < 0)
2081 return status;
2082 res = video_outputsw_set((status & ~disable) | enable);
2083 if (res)
2084 return res;
2087 return 0;
2090 static struct ibm_struct video_driver_data = {
2091 .name = "video",
2092 .read = video_read,
2093 .write = video_write,
2094 .exit = video_exit,
2097 /*************************************************************************
2098 * Light (thinklight) subdriver
2101 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2102 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2104 static int __init light_init(struct ibm_init_struct *iibm)
2106 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2108 IBM_ACPIHANDLE_INIT(ledb);
2109 IBM_ACPIHANDLE_INIT(lght);
2110 IBM_ACPIHANDLE_INIT(cmos);
2112 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2113 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2115 if (tp_features.light)
2116 /* light status not supported on
2117 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2118 tp_features.light_status =
2119 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2121 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2122 str_supported(tp_features.light));
2124 return (tp_features.light)? 0 : 1;
2127 static int light_read(char *p)
2129 int len = 0;
2130 int status = 0;
2132 if (!tp_features.light) {
2133 len += sprintf(p + len, "status:\t\tnot supported\n");
2134 } else if (!tp_features.light_status) {
2135 len += sprintf(p + len, "status:\t\tunknown\n");
2136 len += sprintf(p + len, "commands:\ton, off\n");
2137 } else {
2138 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2139 return -EIO;
2140 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2141 len += sprintf(p + len, "commands:\ton, off\n");
2144 return len;
2147 static int light_write(char *buf)
2149 int cmos_cmd, lght_cmd;
2150 char *cmd;
2151 int success;
2153 if (!tp_features.light)
2154 return -ENODEV;
2156 while ((cmd = next_cmd(&buf))) {
2157 if (strlencmp(cmd, "on") == 0) {
2158 cmos_cmd = 0x0c;
2159 lght_cmd = 1;
2160 } else if (strlencmp(cmd, "off") == 0) {
2161 cmos_cmd = 0x0d;
2162 lght_cmd = 0;
2163 } else
2164 return -EINVAL;
2166 success = cmos_handle ?
2167 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2168 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2169 if (!success)
2170 return -EIO;
2173 return 0;
2176 static struct ibm_struct light_driver_data = {
2177 .name = "light",
2178 .read = light_read,
2179 .write = light_write,
2182 /*************************************************************************
2183 * Dock subdriver
2186 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2188 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2189 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2190 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2191 "\\_SB.PCI.ISA.SLCE", /* 570 */
2192 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2194 /* don't list other alternatives as we install a notify handler on the 570 */
2195 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2197 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2199 .notify = dock_notify,
2200 .handle = &dock_handle,
2201 .type = ACPI_SYSTEM_NOTIFY,
2204 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2205 * We just use it to get notifications of dock hotplug
2206 * in very old thinkpads */
2207 .hid = PCI_ROOT_HID_STRING,
2208 .notify = dock_notify,
2209 .handle = &pci_handle,
2210 .type = ACPI_SYSTEM_NOTIFY,
2214 static struct ibm_struct dock_driver_data[2] = {
2216 .name = "dock",
2217 .read = dock_read,
2218 .write = dock_write,
2219 .acpi = &ibm_dock_acpidriver[0],
2222 .name = "dock",
2223 .acpi = &ibm_dock_acpidriver[1],
2227 #define dock_docked() (_sta(dock_handle) & 1)
2229 static int __init dock_init(struct ibm_init_struct *iibm)
2231 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2233 IBM_ACPIHANDLE_INIT(dock);
2235 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2236 str_supported(dock_handle != NULL));
2238 return (dock_handle)? 0 : 1;
2241 static int __init dock_init2(struct ibm_init_struct *iibm)
2243 int dock2_needed;
2245 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2247 if (dock_driver_data[0].flags.acpi_driver_registered &&
2248 dock_driver_data[0].flags.acpi_notify_installed) {
2249 IBM_ACPIHANDLE_INIT(pci);
2250 dock2_needed = (pci_handle != NULL);
2251 vdbg_printk(TPACPI_DBG_INIT,
2252 "dock PCI handler for the TP 570 is %s\n",
2253 str_supported(dock2_needed));
2254 } else {
2255 vdbg_printk(TPACPI_DBG_INIT,
2256 "dock subdriver part 2 not required\n");
2257 dock2_needed = 0;
2260 return (dock2_needed)? 0 : 1;
2263 static void dock_notify(struct ibm_struct *ibm, u32 event)
2265 int docked = dock_docked();
2266 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2268 if (event == 1 && !pci) /* 570 */
2269 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2270 else if (event == 1 && pci) /* 570 */
2271 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2272 else if (event == 3 && docked)
2273 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2274 else if (event == 3 && !docked)
2275 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2276 else if (event == 0 && docked)
2277 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2278 else {
2279 printk(IBM_ERR "unknown dock event %d, status %d\n",
2280 event, _sta(dock_handle));
2281 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2285 static int dock_read(char *p)
2287 int len = 0;
2288 int docked = dock_docked();
2290 if (!dock_handle)
2291 len += sprintf(p + len, "status:\t\tnot supported\n");
2292 else if (!docked)
2293 len += sprintf(p + len, "status:\t\tundocked\n");
2294 else {
2295 len += sprintf(p + len, "status:\t\tdocked\n");
2296 len += sprintf(p + len, "commands:\tdock, undock\n");
2299 return len;
2302 static int dock_write(char *buf)
2304 char *cmd;
2306 if (!dock_docked())
2307 return -ENODEV;
2309 while ((cmd = next_cmd(&buf))) {
2310 if (strlencmp(cmd, "undock") == 0) {
2311 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2312 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2313 return -EIO;
2314 } else if (strlencmp(cmd, "dock") == 0) {
2315 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2316 return -EIO;
2317 } else
2318 return -EINVAL;
2321 return 0;
2324 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2326 /*************************************************************************
2327 * Bay subdriver
2330 #ifdef CONFIG_THINKPAD_ACPI_BAY
2331 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2332 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2333 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2334 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2335 ); /* A21e, R30, R31 */
2336 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2337 "_EJ0", /* all others */
2338 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2339 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2340 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2341 ); /* all others */
2342 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2343 "_EJ0", /* 770x */
2344 ); /* all others */
2346 static int __init bay_init(struct ibm_init_struct *iibm)
2348 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2350 IBM_ACPIHANDLE_INIT(bay);
2351 if (bay_handle)
2352 IBM_ACPIHANDLE_INIT(bay_ej);
2353 IBM_ACPIHANDLE_INIT(bay2);
2354 if (bay2_handle)
2355 IBM_ACPIHANDLE_INIT(bay2_ej);
2357 tp_features.bay_status = bay_handle &&
2358 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2359 tp_features.bay_status2 = bay2_handle &&
2360 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2362 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2363 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2364 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2365 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2367 vdbg_printk(TPACPI_DBG_INIT,
2368 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2369 str_supported(tp_features.bay_status),
2370 str_supported(tp_features.bay_eject),
2371 str_supported(tp_features.bay_status2),
2372 str_supported(tp_features.bay_eject2));
2374 return (tp_features.bay_status || tp_features.bay_eject ||
2375 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2378 static void bay_notify(struct ibm_struct *ibm, u32 event)
2380 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2383 #define bay_occupied(b) (_sta(b##_handle) & 1)
2385 static int bay_read(char *p)
2387 int len = 0;
2388 int occupied = bay_occupied(bay);
2389 int occupied2 = bay_occupied(bay2);
2390 int eject, eject2;
2392 len += sprintf(p + len, "status:\t\t%s\n",
2393 tp_features.bay_status ?
2394 (occupied ? "occupied" : "unoccupied") :
2395 "not supported");
2396 if (tp_features.bay_status2)
2397 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2398 "occupied" : "unoccupied");
2400 eject = tp_features.bay_eject && occupied;
2401 eject2 = tp_features.bay_eject2 && occupied2;
2403 if (eject && eject2)
2404 len += sprintf(p + len, "commands:\teject, eject2\n");
2405 else if (eject)
2406 len += sprintf(p + len, "commands:\teject\n");
2407 else if (eject2)
2408 len += sprintf(p + len, "commands:\teject2\n");
2410 return len;
2413 static int bay_write(char *buf)
2415 char *cmd;
2417 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2418 return -ENODEV;
2420 while ((cmd = next_cmd(&buf))) {
2421 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2422 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2423 return -EIO;
2424 } else if (tp_features.bay_eject2 &&
2425 strlencmp(cmd, "eject2") == 0) {
2426 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2427 return -EIO;
2428 } else
2429 return -EINVAL;
2432 return 0;
2435 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2436 .notify = bay_notify,
2437 .handle = &bay_handle,
2438 .type = ACPI_SYSTEM_NOTIFY,
2441 static struct ibm_struct bay_driver_data = {
2442 .name = "bay",
2443 .read = bay_read,
2444 .write = bay_write,
2445 .acpi = &ibm_bay_acpidriver,
2448 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2450 /*************************************************************************
2451 * CMOS subdriver
2454 /* sysfs cmos_command -------------------------------------------------- */
2455 static ssize_t cmos_command_store(struct device *dev,
2456 struct device_attribute *attr,
2457 const char *buf, size_t count)
2459 unsigned long cmos_cmd;
2460 int res;
2462 if (parse_strtoul(buf, 21, &cmos_cmd))
2463 return -EINVAL;
2465 res = issue_thinkpad_cmos_command(cmos_cmd);
2466 return (res)? res : count;
2469 static struct device_attribute dev_attr_cmos_command =
2470 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2472 /* --------------------------------------------------------------------- */
2474 static int __init cmos_init(struct ibm_init_struct *iibm)
2476 int res;
2478 vdbg_printk(TPACPI_DBG_INIT,
2479 "initializing cmos commands subdriver\n");
2481 IBM_ACPIHANDLE_INIT(cmos);
2483 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2484 str_supported(cmos_handle != NULL));
2486 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2487 if (res)
2488 return res;
2490 return (cmos_handle)? 0 : 1;
2493 static void cmos_exit(void)
2495 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2498 static int cmos_read(char *p)
2500 int len = 0;
2502 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2503 R30, R31, T20-22, X20-21 */
2504 if (!cmos_handle)
2505 len += sprintf(p + len, "status:\t\tnot supported\n");
2506 else {
2507 len += sprintf(p + len, "status:\t\tsupported\n");
2508 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2511 return len;
2514 static int cmos_write(char *buf)
2516 char *cmd;
2517 int cmos_cmd, res;
2519 while ((cmd = next_cmd(&buf))) {
2520 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2521 cmos_cmd >= 0 && cmos_cmd <= 21) {
2522 /* cmos_cmd set */
2523 } else
2524 return -EINVAL;
2526 res = issue_thinkpad_cmos_command(cmos_cmd);
2527 if (res)
2528 return res;
2531 return 0;
2534 static struct ibm_struct cmos_driver_data = {
2535 .name = "cmos",
2536 .read = cmos_read,
2537 .write = cmos_write,
2538 .exit = cmos_exit,
2541 /*************************************************************************
2542 * LED subdriver
2545 static enum led_access_mode led_supported;
2547 IBM_HANDLE(led, ec, "SLED", /* 570 */
2548 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2549 "LED", /* all others */
2550 ); /* R30, R31 */
2552 static int __init led_init(struct ibm_init_struct *iibm)
2554 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2556 IBM_ACPIHANDLE_INIT(led);
2558 if (!led_handle)
2559 /* led not supported on R30, R31 */
2560 led_supported = TPACPI_LED_NONE;
2561 else if (strlencmp(led_path, "SLED") == 0)
2562 /* 570 */
2563 led_supported = TPACPI_LED_570;
2564 else if (strlencmp(led_path, "SYSL") == 0)
2565 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2566 led_supported = TPACPI_LED_OLD;
2567 else
2568 /* all others */
2569 led_supported = TPACPI_LED_NEW;
2571 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2572 str_supported(led_supported), led_supported);
2574 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2577 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2579 static int led_read(char *p)
2581 int len = 0;
2583 if (!led_supported) {
2584 len += sprintf(p + len, "status:\t\tnot supported\n");
2585 return len;
2587 len += sprintf(p + len, "status:\t\tsupported\n");
2589 if (led_supported == TPACPI_LED_570) {
2590 /* 570 */
2591 int i, status;
2592 for (i = 0; i < 8; i++) {
2593 if (!acpi_evalf(ec_handle,
2594 &status, "GLED", "dd", 1 << i))
2595 return -EIO;
2596 len += sprintf(p + len, "%d:\t\t%s\n",
2597 i, led_status(status));
2601 len += sprintf(p + len, "commands:\t"
2602 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2604 return len;
2607 /* off, on, blink */
2608 static const int led_sled_arg1[] = { 0, 1, 3 };
2609 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2610 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2611 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2613 static int led_write(char *buf)
2615 char *cmd;
2616 int led, ind, ret;
2618 if (!led_supported)
2619 return -ENODEV;
2621 while ((cmd = next_cmd(&buf))) {
2622 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2623 return -EINVAL;
2625 if (strstr(cmd, "off")) {
2626 ind = 0;
2627 } else if (strstr(cmd, "on")) {
2628 ind = 1;
2629 } else if (strstr(cmd, "blink")) {
2630 ind = 2;
2631 } else
2632 return -EINVAL;
2634 if (led_supported == TPACPI_LED_570) {
2635 /* 570 */
2636 led = 1 << led;
2637 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2638 led, led_sled_arg1[ind]))
2639 return -EIO;
2640 } else if (led_supported == TPACPI_LED_OLD) {
2641 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2642 led = 1 << led;
2643 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2644 if (ret >= 0)
2645 ret =
2646 ec_write(TPACPI_LED_EC_HLBL,
2647 led * led_exp_hlbl[ind]);
2648 if (ret >= 0)
2649 ret =
2650 ec_write(TPACPI_LED_EC_HLCL,
2651 led * led_exp_hlcl[ind]);
2652 if (ret < 0)
2653 return ret;
2654 } else {
2655 /* all others */
2656 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2657 led, led_led_arg1[ind]))
2658 return -EIO;
2662 return 0;
2665 static struct ibm_struct led_driver_data = {
2666 .name = "led",
2667 .read = led_read,
2668 .write = led_write,
2671 /*************************************************************************
2672 * Beep subdriver
2675 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2677 static int __init beep_init(struct ibm_init_struct *iibm)
2679 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2681 IBM_ACPIHANDLE_INIT(beep);
2683 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2684 str_supported(beep_handle != NULL));
2686 return (beep_handle)? 0 : 1;
2689 static int beep_read(char *p)
2691 int len = 0;
2693 if (!beep_handle)
2694 len += sprintf(p + len, "status:\t\tnot supported\n");
2695 else {
2696 len += sprintf(p + len, "status:\t\tsupported\n");
2697 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2700 return len;
2703 static int beep_write(char *buf)
2705 char *cmd;
2706 int beep_cmd;
2708 if (!beep_handle)
2709 return -ENODEV;
2711 while ((cmd = next_cmd(&buf))) {
2712 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2713 beep_cmd >= 0 && beep_cmd <= 17) {
2714 /* beep_cmd set */
2715 } else
2716 return -EINVAL;
2717 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2718 return -EIO;
2721 return 0;
2724 static struct ibm_struct beep_driver_data = {
2725 .name = "beep",
2726 .read = beep_read,
2727 .write = beep_write,
2730 /*************************************************************************
2731 * Thermal subdriver
2734 static enum thermal_access_mode thermal_read_mode;
2736 /* sysfs temp##_input -------------------------------------------------- */
2738 static ssize_t thermal_temp_input_show(struct device *dev,
2739 struct device_attribute *attr,
2740 char *buf)
2742 struct sensor_device_attribute *sensor_attr =
2743 to_sensor_dev_attr(attr);
2744 int idx = sensor_attr->index;
2745 s32 value;
2746 int res;
2748 res = thermal_get_sensor(idx, &value);
2749 if (res)
2750 return res;
2751 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2752 return -ENXIO;
2754 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2757 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2758 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2760 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2761 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2762 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2763 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2764 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2765 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2766 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2767 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2768 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2769 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2770 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2771 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2772 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2773 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2774 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2775 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2776 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2779 #define THERMAL_ATTRS(X) \
2780 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2782 static struct attribute *thermal_temp_input_attr[] = {
2783 THERMAL_ATTRS(8),
2784 THERMAL_ATTRS(9),
2785 THERMAL_ATTRS(10),
2786 THERMAL_ATTRS(11),
2787 THERMAL_ATTRS(12),
2788 THERMAL_ATTRS(13),
2789 THERMAL_ATTRS(14),
2790 THERMAL_ATTRS(15),
2791 THERMAL_ATTRS(0),
2792 THERMAL_ATTRS(1),
2793 THERMAL_ATTRS(2),
2794 THERMAL_ATTRS(3),
2795 THERMAL_ATTRS(4),
2796 THERMAL_ATTRS(5),
2797 THERMAL_ATTRS(6),
2798 THERMAL_ATTRS(7),
2799 NULL
2802 static const struct attribute_group thermal_temp_input16_group = {
2803 .attrs = thermal_temp_input_attr
2806 static const struct attribute_group thermal_temp_input8_group = {
2807 .attrs = &thermal_temp_input_attr[8]
2810 #undef THERMAL_SENSOR_ATTR_TEMP
2811 #undef THERMAL_ATTRS
2813 /* --------------------------------------------------------------------- */
2815 static int __init thermal_init(struct ibm_init_struct *iibm)
2817 u8 t, ta1, ta2;
2818 int i;
2819 int acpi_tmp7;
2820 int res;
2822 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2824 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2826 if (thinkpad_id.ec_model) {
2828 * Direct EC access mode: sensors at registers
2829 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2830 * non-implemented, thermal sensors return 0x80 when
2831 * not available
2834 ta1 = ta2 = 0;
2835 for (i = 0; i < 8; i++) {
2836 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2837 ta1 |= t;
2838 } else {
2839 ta1 = 0;
2840 break;
2842 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2843 ta2 |= t;
2844 } else {
2845 ta1 = 0;
2846 break;
2849 if (ta1 == 0) {
2850 /* This is sheer paranoia, but we handle it anyway */
2851 if (acpi_tmp7) {
2852 printk(IBM_ERR
2853 "ThinkPad ACPI EC access misbehaving, "
2854 "falling back to ACPI TMPx access mode\n");
2855 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2856 } else {
2857 printk(IBM_ERR
2858 "ThinkPad ACPI EC access misbehaving, "
2859 "disabling thermal sensors access\n");
2860 thermal_read_mode = TPACPI_THERMAL_NONE;
2862 } else {
2863 thermal_read_mode =
2864 (ta2 != 0) ?
2865 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2867 } else if (acpi_tmp7) {
2868 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2869 /* 600e/x, 770e, 770x */
2870 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2871 } else {
2872 /* Standard ACPI TMPx access, max 8 sensors */
2873 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2875 } else {
2876 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2877 thermal_read_mode = TPACPI_THERMAL_NONE;
2880 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2881 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2882 thermal_read_mode);
2884 switch(thermal_read_mode) {
2885 case TPACPI_THERMAL_TPEC_16:
2886 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2887 &thermal_temp_input16_group);
2888 if (res)
2889 return res;
2890 break;
2891 case TPACPI_THERMAL_TPEC_8:
2892 case TPACPI_THERMAL_ACPI_TMP07:
2893 case TPACPI_THERMAL_ACPI_UPDT:
2894 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2895 &thermal_temp_input8_group);
2896 if (res)
2897 return res;
2898 break;
2899 case TPACPI_THERMAL_NONE:
2900 default:
2901 return 1;
2904 return 0;
2907 static void thermal_exit(void)
2909 switch(thermal_read_mode) {
2910 case TPACPI_THERMAL_TPEC_16:
2911 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2912 &thermal_temp_input16_group);
2913 break;
2914 case TPACPI_THERMAL_TPEC_8:
2915 case TPACPI_THERMAL_ACPI_TMP07:
2916 case TPACPI_THERMAL_ACPI_UPDT:
2917 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2918 &thermal_temp_input16_group);
2919 break;
2920 case TPACPI_THERMAL_NONE:
2921 default:
2922 break;
2926 /* idx is zero-based */
2927 static int thermal_get_sensor(int idx, s32 *value)
2929 int t;
2930 s8 tmp;
2931 char tmpi[5];
2933 t = TP_EC_THERMAL_TMP0;
2935 switch (thermal_read_mode) {
2936 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2937 case TPACPI_THERMAL_TPEC_16:
2938 if (idx >= 8 && idx <= 15) {
2939 t = TP_EC_THERMAL_TMP8;
2940 idx -= 8;
2942 /* fallthrough */
2943 #endif
2944 case TPACPI_THERMAL_TPEC_8:
2945 if (idx <= 7) {
2946 if (!acpi_ec_read(t + idx, &tmp))
2947 return -EIO;
2948 *value = tmp * 1000;
2949 return 0;
2951 break;
2953 case TPACPI_THERMAL_ACPI_UPDT:
2954 if (idx <= 7) {
2955 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2956 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2957 return -EIO;
2958 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2959 return -EIO;
2960 *value = (t - 2732) * 100;
2961 return 0;
2963 break;
2965 case TPACPI_THERMAL_ACPI_TMP07:
2966 if (idx <= 7) {
2967 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2968 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2969 return -EIO;
2970 if (t > 127 || t < -127)
2971 t = TP_EC_THERMAL_TMP_NA;
2972 *value = t * 1000;
2973 return 0;
2975 break;
2977 case TPACPI_THERMAL_NONE:
2978 default:
2979 return -ENOSYS;
2982 return -EINVAL;
2985 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2987 int res, i;
2988 int n;
2990 n = 8;
2991 i = 0;
2993 if (!s)
2994 return -EINVAL;
2996 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2997 n = 16;
2999 for(i = 0 ; i < n; i++) {
3000 res = thermal_get_sensor(i, &s->temp[i]);
3001 if (res)
3002 return res;
3005 return n;
3008 static int thermal_read(char *p)
3010 int len = 0;
3011 int n, i;
3012 struct ibm_thermal_sensors_struct t;
3014 n = thermal_get_sensors(&t);
3015 if (unlikely(n < 0))
3016 return n;
3018 len += sprintf(p + len, "temperatures:\t");
3020 if (n > 0) {
3021 for (i = 0; i < (n - 1); i++)
3022 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3023 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3024 } else
3025 len += sprintf(p + len, "not supported\n");
3027 return len;
3030 static struct ibm_struct thermal_driver_data = {
3031 .name = "thermal",
3032 .read = thermal_read,
3033 .exit = thermal_exit,
3036 /*************************************************************************
3037 * EC Dump subdriver
3040 static u8 ecdump_regs[256];
3042 static int ecdump_read(char *p)
3044 int len = 0;
3045 int i, j;
3046 u8 v;
3048 len += sprintf(p + len, "EC "
3049 " +00 +01 +02 +03 +04 +05 +06 +07"
3050 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3051 for (i = 0; i < 256; i += 16) {
3052 len += sprintf(p + len, "EC 0x%02x:", i);
3053 for (j = 0; j < 16; j++) {
3054 if (!acpi_ec_read(i + j, &v))
3055 break;
3056 if (v != ecdump_regs[i + j])
3057 len += sprintf(p + len, " *%02x", v);
3058 else
3059 len += sprintf(p + len, " %02x", v);
3060 ecdump_regs[i + j] = v;
3062 len += sprintf(p + len, "\n");
3063 if (j != 16)
3064 break;
3067 /* These are way too dangerous to advertise openly... */
3068 #if 0
3069 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3070 " (<offset> is 00-ff, <value> is 00-ff)\n");
3071 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3072 " (<offset> is 00-ff, <value> is 0-255)\n");
3073 #endif
3074 return len;
3077 static int ecdump_write(char *buf)
3079 char *cmd;
3080 int i, v;
3082 while ((cmd = next_cmd(&buf))) {
3083 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3084 /* i and v set */
3085 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3086 /* i and v set */
3087 } else
3088 return -EINVAL;
3089 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3090 if (!acpi_ec_write(i, v))
3091 return -EIO;
3092 } else
3093 return -EINVAL;
3096 return 0;
3099 static struct ibm_struct ecdump_driver_data = {
3100 .name = "ecdump",
3101 .read = ecdump_read,
3102 .write = ecdump_write,
3103 .flags.experimental = 1,
3106 /*************************************************************************
3107 * Backlight/brightness subdriver
3110 static struct backlight_device *ibm_backlight_device;
3112 static struct backlight_ops ibm_backlight_data = {
3113 .get_brightness = brightness_get,
3114 .update_status = brightness_update_status,
3117 static struct mutex brightness_mutex;
3119 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3121 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3122 union acpi_object *obj;
3123 int rc;
3125 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
3126 obj = (union acpi_object *)buffer.pointer;
3127 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3128 printk(IBM_ERR "Unknown BCLL data, "
3129 "please report this to %s\n", IBM_MAIL);
3130 rc = 0;
3131 } else {
3132 rc = obj->package.count;
3134 } else {
3135 return 0;
3138 kfree(buffer.pointer);
3139 return rc;
3142 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
3143 void *context, void **rv)
3145 char name[ACPI_PATH_SEGMENT_LENGTH];
3146 struct acpi_buffer buffer = { sizeof(name), &name };
3148 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3149 !strncmp("BCLL", name, sizeof(name) - 1)) {
3150 if (tpacpi_query_bcll_levels(handle) == 16) {
3151 *rv = handle;
3152 return AE_CTRL_TERMINATE;
3153 } else {
3154 return AE_OK;
3156 } else {
3157 return AE_OK;
3161 static int __init brightness_check_levels(void)
3163 int status;
3164 void *found_node = NULL;
3166 if (!vid_handle) {
3167 IBM_ACPIHANDLE_INIT(vid);
3169 if (!vid_handle)
3170 return 0;
3172 /* Search for a BCLL package with 16 levels */
3173 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
3174 brightness_find_bcll, NULL, &found_node);
3176 return (ACPI_SUCCESS(status) && found_node != NULL);
3179 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
3180 void *context, void **rv)
3182 char name[ACPI_PATH_SEGMENT_LENGTH];
3183 struct acpi_buffer buffer = { sizeof(name), &name };
3185 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3186 !strncmp("_BCL", name, sizeof(name) - 1)) {
3187 *rv = handle;
3188 return AE_CTRL_TERMINATE;
3189 } else {
3190 return AE_OK;
3194 static int __init brightness_check_std_acpi_support(void)
3196 int status;
3197 void *found_node = NULL;
3199 if (!vid_handle) {
3200 IBM_ACPIHANDLE_INIT(vid);
3202 if (!vid_handle)
3203 return 0;
3205 /* Search for a _BCL method, but don't execute it */
3206 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
3207 brightness_find_bcl, NULL, &found_node);
3209 return (ACPI_SUCCESS(status) && found_node != NULL);
3212 static int __init brightness_init(struct ibm_init_struct *iibm)
3214 int b;
3216 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3218 mutex_init(&brightness_mutex);
3220 if (!brightness_enable) {
3221 dbg_printk(TPACPI_DBG_INIT,
3222 "brightness support disabled by module parameter\n");
3223 return 1;
3224 } else if (brightness_enable > 1) {
3225 if (brightness_check_std_acpi_support()) {
3226 printk(IBM_NOTICE
3227 "standard ACPI backlight interface available, not loading native one...\n");
3228 return 1;
3232 if (!brightness_mode) {
3233 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3234 brightness_mode = 2;
3235 else
3236 brightness_mode = 3;
3238 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3239 brightness_mode);
3242 if (brightness_mode > 3)
3243 return -EINVAL;
3245 tp_features.bright_16levels =
3246 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
3247 brightness_check_levels();
3249 b = brightness_get(NULL);
3250 if (b < 0)
3251 return 1;
3253 if (tp_features.bright_16levels)
3254 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
3256 ibm_backlight_device = backlight_device_register(
3257 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3258 &ibm_backlight_data);
3259 if (IS_ERR(ibm_backlight_device)) {
3260 printk(IBM_ERR "Could not register backlight device\n");
3261 return PTR_ERR(ibm_backlight_device);
3263 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3265 ibm_backlight_device->props.max_brightness =
3266 (tp_features.bright_16levels)? 15 : 7;
3267 ibm_backlight_device->props.brightness = b;
3268 backlight_update_status(ibm_backlight_device);
3270 return 0;
3273 static void brightness_exit(void)
3275 if (ibm_backlight_device) {
3276 vdbg_printk(TPACPI_DBG_EXIT,
3277 "calling backlight_device_unregister()\n");
3278 backlight_device_unregister(ibm_backlight_device);
3279 ibm_backlight_device = NULL;
3283 static int brightness_update_status(struct backlight_device *bd)
3285 return brightness_set(
3286 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3287 bd->props.power == FB_BLANK_UNBLANK) ?
3288 bd->props.brightness : 0);
3292 * ThinkPads can read brightness from two places: EC 0x31, or
3293 * CMOS NVRAM byte 0x5E, bits 0-3.
3295 static int brightness_get(struct backlight_device *bd)
3297 u8 lec = 0, lcmos = 0, level = 0;
3299 if (brightness_mode & 1) {
3300 if (!acpi_ec_read(brightness_offset, &lec))
3301 return -EIO;
3302 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3303 level = lec;
3305 if (brightness_mode & 2) {
3306 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3307 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3308 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3309 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3310 level = lcmos;
3313 if (brightness_mode == 3 && lec != lcmos) {
3314 printk(IBM_ERR
3315 "CMOS NVRAM (%u) and EC (%u) do not agree "
3316 "on display brightness level\n",
3317 (unsigned int) lcmos,
3318 (unsigned int) lec);
3319 return -EIO;
3322 return level;
3325 static int brightness_set(int value)
3327 int cmos_cmd, inc, i, res;
3328 int current_value;
3330 if (value > ((tp_features.bright_16levels)? 15 : 7))
3331 return -EINVAL;
3333 res = mutex_lock_interruptible(&brightness_mutex);
3334 if (res < 0)
3335 return res;
3337 current_value = brightness_get(NULL);
3338 if (current_value < 0) {
3339 res = current_value;
3340 goto errout;
3343 cmos_cmd = value > current_value ?
3344 TP_CMOS_BRIGHTNESS_UP :
3345 TP_CMOS_BRIGHTNESS_DOWN;
3346 inc = (value > current_value)? 1 : -1;
3348 res = 0;
3349 for (i = current_value; i != value; i += inc) {
3350 if ((brightness_mode & 2) &&
3351 issue_thinkpad_cmos_command(cmos_cmd)) {
3352 res = -EIO;
3353 goto errout;
3355 if ((brightness_mode & 1) &&
3356 !acpi_ec_write(brightness_offset, i + inc)) {
3357 res = -EIO;
3358 goto errout;;
3362 errout:
3363 mutex_unlock(&brightness_mutex);
3364 return res;
3367 static int brightness_read(char *p)
3369 int len = 0;
3370 int level;
3372 if ((level = brightness_get(NULL)) < 0) {
3373 len += sprintf(p + len, "level:\t\tunreadable\n");
3374 } else {
3375 len += sprintf(p + len, "level:\t\t%d\n", level);
3376 len += sprintf(p + len, "commands:\tup, down\n");
3377 len += sprintf(p + len, "commands:\tlevel <level>"
3378 " (<level> is 0-%d)\n",
3379 (tp_features.bright_16levels) ? 15 : 7);
3382 return len;
3385 static int brightness_write(char *buf)
3387 int level;
3388 int new_level;
3389 char *cmd;
3390 int max_level = (tp_features.bright_16levels) ? 15 : 7;
3392 while ((cmd = next_cmd(&buf))) {
3393 if ((level = brightness_get(NULL)) < 0)
3394 return level;
3396 if (strlencmp(cmd, "up") == 0) {
3397 new_level = level == (max_level)?
3398 max_level : level + 1;
3399 } else if (strlencmp(cmd, "down") == 0) {
3400 new_level = level == 0? 0 : level - 1;
3401 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3402 new_level >= 0 && new_level <= max_level) {
3403 /* new_level set */
3404 } else
3405 return -EINVAL;
3407 brightness_set(new_level);
3410 return 0;
3413 static struct ibm_struct brightness_driver_data = {
3414 .name = "brightness",
3415 .read = brightness_read,
3416 .write = brightness_write,
3417 .exit = brightness_exit,
3420 /*************************************************************************
3421 * Volume subdriver
3424 static int volume_read(char *p)
3426 int len = 0;
3427 u8 level;
3429 if (!acpi_ec_read(volume_offset, &level)) {
3430 len += sprintf(p + len, "level:\t\tunreadable\n");
3431 } else {
3432 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3433 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3434 len += sprintf(p + len, "commands:\tup, down, mute\n");
3435 len += sprintf(p + len, "commands:\tlevel <level>"
3436 " (<level> is 0-15)\n");
3439 return len;
3442 static int volume_write(char *buf)
3444 int cmos_cmd, inc, i;
3445 u8 level, mute;
3446 int new_level, new_mute;
3447 char *cmd;
3449 while ((cmd = next_cmd(&buf))) {
3450 if (!acpi_ec_read(volume_offset, &level))
3451 return -EIO;
3452 new_mute = mute = level & 0x40;
3453 new_level = level = level & 0xf;
3455 if (strlencmp(cmd, "up") == 0) {
3456 if (mute)
3457 new_mute = 0;
3458 else
3459 new_level = level == 15 ? 15 : level + 1;
3460 } else if (strlencmp(cmd, "down") == 0) {
3461 if (mute)
3462 new_mute = 0;
3463 else
3464 new_level = level == 0 ? 0 : level - 1;
3465 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3466 new_level >= 0 && new_level <= 15) {
3467 /* new_level set */
3468 } else if (strlencmp(cmd, "mute") == 0) {
3469 new_mute = 0x40;
3470 } else
3471 return -EINVAL;
3473 if (new_level != level) { /* mute doesn't change */
3474 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3475 inc = new_level > level ? 1 : -1;
3477 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3478 !acpi_ec_write(volume_offset, level)))
3479 return -EIO;
3481 for (i = level; i != new_level; i += inc)
3482 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3483 !acpi_ec_write(volume_offset, i + inc))
3484 return -EIO;
3486 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3487 !acpi_ec_write(volume_offset,
3488 new_level + mute)))
3489 return -EIO;
3492 if (new_mute != mute) { /* level doesn't change */
3493 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3495 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3496 !acpi_ec_write(volume_offset, level + new_mute))
3497 return -EIO;
3501 return 0;
3504 static struct ibm_struct volume_driver_data = {
3505 .name = "volume",
3506 .read = volume_read,
3507 .write = volume_write,
3510 /*************************************************************************
3511 * Fan subdriver
3515 * FAN ACCESS MODES
3517 * TPACPI_FAN_RD_ACPI_GFAN:
3518 * ACPI GFAN method: returns fan level
3520 * see TPACPI_FAN_WR_ACPI_SFAN
3521 * EC 0x2f (HFSP) not available if GFAN exists
3523 * TPACPI_FAN_WR_ACPI_SFAN:
3524 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3526 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3527 * it for writing.
3529 * TPACPI_FAN_WR_TPEC:
3530 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3531 * Supported on almost all ThinkPads
3533 * Fan speed changes of any sort (including those caused by the
3534 * disengaged mode) are usually done slowly by the firmware as the
3535 * maximum ammount of fan duty cycle change per second seems to be
3536 * limited.
3538 * Reading is not available if GFAN exists.
3539 * Writing is not available if SFAN exists.
3541 * Bits
3542 * 7 automatic mode engaged;
3543 * (default operation mode of the ThinkPad)
3544 * fan level is ignored in this mode.
3545 * 6 full speed mode (takes precedence over bit 7);
3546 * not available on all thinkpads. May disable
3547 * the tachometer while the fan controller ramps up
3548 * the speed (which can take up to a few *minutes*).
3549 * Speeds up fan to 100% duty-cycle, which is far above
3550 * the standard RPM levels. It is not impossible that
3551 * it could cause hardware damage.
3552 * 5-3 unused in some models. Extra bits for fan level
3553 * in others, but still useless as all values above
3554 * 7 map to the same speed as level 7 in these models.
3555 * 2-0 fan level (0..7 usually)
3556 * 0x00 = stop
3557 * 0x07 = max (set when temperatures critical)
3558 * Some ThinkPads may have other levels, see
3559 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3561 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3562 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3563 * does so, its initial value is meaningless (0x07).
3565 * For firmware bugs, refer to:
3566 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3568 * ----
3570 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3571 * Main fan tachometer reading (in RPM)
3573 * This register is present on all ThinkPads with a new-style EC, and
3574 * it is known not to be present on the A21m/e, and T22, as there is
3575 * something else in offset 0x84 according to the ACPI DSDT. Other
3576 * ThinkPads from this same time period (and earlier) probably lack the
3577 * tachometer as well.
3579 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3580 * was never fixed by IBM to report the EC firmware version string
3581 * probably support the tachometer (like the early X models), so
3582 * detecting it is quite hard. We need more data to know for sure.
3584 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3585 * might result.
3587 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3588 * mode.
3590 * For firmware bugs, refer to:
3591 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3593 * TPACPI_FAN_WR_ACPI_FANS:
3594 * ThinkPad X31, X40, X41. Not available in the X60.
3596 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3597 * high speed. ACPI DSDT seems to map these three speeds to levels
3598 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3599 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3601 * The speeds are stored on handles
3602 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3604 * There are three default speed sets, acessible as handles:
3605 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3607 * ACPI DSDT switches which set is in use depending on various
3608 * factors.
3610 * TPACPI_FAN_WR_TPEC is also available and should be used to
3611 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3612 * but the ACPI tables just mention level 7.
3615 static enum fan_status_access_mode fan_status_access_mode;
3616 static enum fan_control_access_mode fan_control_access_mode;
3617 static enum fan_control_commands fan_control_commands;
3619 static u8 fan_control_initial_status;
3620 static u8 fan_control_desired_level;
3622 static void fan_watchdog_fire(struct work_struct *ignored);
3623 static int fan_watchdog_maxinterval;
3624 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3626 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3627 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3628 "\\FSPD", /* 600e/x, 770e, 770x */
3629 ); /* all others */
3630 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3631 "JFNS", /* 770x-JL */
3632 ); /* all others */
3635 * SYSFS fan layout: hwmon compatible (device)
3637 * pwm*_enable:
3638 * 0: "disengaged" mode
3639 * 1: manual mode
3640 * 2: native EC "auto" mode (recommended, hardware default)
3642 * pwm*: set speed in manual mode, ignored otherwise.
3643 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3644 * interpolation.
3646 * fan*_input: tachometer reading, RPM
3649 * SYSFS fan layout: extensions
3651 * fan_watchdog (driver):
3652 * fan watchdog interval in seconds, 0 disables (default), max 120
3655 /* sysfs fan pwm1_enable ----------------------------------------------- */
3656 static ssize_t fan_pwm1_enable_show(struct device *dev,
3657 struct device_attribute *attr,
3658 char *buf)
3660 int res, mode;
3661 u8 status;
3663 res = fan_get_status_safe(&status);
3664 if (res)
3665 return res;
3667 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3668 if (status != fan_control_initial_status) {
3669 tp_features.fan_ctrl_status_undef = 0;
3670 } else {
3671 /* Return most likely status. In fact, it
3672 * might be the only possible status */
3673 status = TP_EC_FAN_AUTO;
3677 if (status & TP_EC_FAN_FULLSPEED) {
3678 mode = 0;
3679 } else if (status & TP_EC_FAN_AUTO) {
3680 mode = 2;
3681 } else
3682 mode = 1;
3684 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3687 static ssize_t fan_pwm1_enable_store(struct device *dev,
3688 struct device_attribute *attr,
3689 const char *buf, size_t count)
3691 unsigned long t;
3692 int res, level;
3694 if (parse_strtoul(buf, 2, &t))
3695 return -EINVAL;
3697 switch (t) {
3698 case 0:
3699 level = TP_EC_FAN_FULLSPEED;
3700 break;
3701 case 1:
3702 level = TPACPI_FAN_LAST_LEVEL;
3703 break;
3704 case 2:
3705 level = TP_EC_FAN_AUTO;
3706 break;
3707 case 3:
3708 /* reserved for software-controlled auto mode */
3709 return -ENOSYS;
3710 default:
3711 return -EINVAL;
3714 res = fan_set_level_safe(level);
3715 if (res == -ENXIO)
3716 return -EINVAL;
3717 else if (res < 0)
3718 return res;
3720 fan_watchdog_reset();
3722 return count;
3725 static struct device_attribute dev_attr_fan_pwm1_enable =
3726 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3727 fan_pwm1_enable_show, fan_pwm1_enable_store);
3729 /* sysfs fan pwm1 ------------------------------------------------------ */
3730 static ssize_t fan_pwm1_show(struct device *dev,
3731 struct device_attribute *attr,
3732 char *buf)
3734 int res;
3735 u8 status;
3737 res = fan_get_status_safe(&status);
3738 if (res)
3739 return res;
3741 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3742 if (status != fan_control_initial_status) {
3743 tp_features.fan_ctrl_status_undef = 0;
3744 } else {
3745 status = TP_EC_FAN_AUTO;
3749 if ((status &
3750 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3751 status = fan_control_desired_level;
3753 if (status > 7)
3754 status = 7;
3756 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3759 static ssize_t fan_pwm1_store(struct device *dev,
3760 struct device_attribute *attr,
3761 const char *buf, size_t count)
3763 unsigned long s;
3764 int rc;
3765 u8 status, newlevel;
3767 if (parse_strtoul(buf, 255, &s))
3768 return -EINVAL;
3770 /* scale down from 0-255 to 0-7 */
3771 newlevel = (s >> 5) & 0x07;
3773 rc = mutex_lock_interruptible(&fan_mutex);
3774 if (rc < 0)
3775 return rc;
3777 rc = fan_get_status(&status);
3778 if (!rc && (status &
3779 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3780 rc = fan_set_level(newlevel);
3781 if (rc == -ENXIO)
3782 rc = -EINVAL;
3783 else if (!rc) {
3784 fan_update_desired_level(newlevel);
3785 fan_watchdog_reset();
3789 mutex_unlock(&fan_mutex);
3790 return (rc)? rc : count;
3793 static struct device_attribute dev_attr_fan_pwm1 =
3794 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3795 fan_pwm1_show, fan_pwm1_store);
3797 /* sysfs fan fan1_input ------------------------------------------------ */
3798 static ssize_t fan_fan1_input_show(struct device *dev,
3799 struct device_attribute *attr,
3800 char *buf)
3802 int res;
3803 unsigned int speed;
3805 res = fan_get_speed(&speed);
3806 if (res < 0)
3807 return res;
3809 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3812 static struct device_attribute dev_attr_fan_fan1_input =
3813 __ATTR(fan1_input, S_IRUGO,
3814 fan_fan1_input_show, NULL);
3816 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
3817 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3818 char *buf)
3820 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3823 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3824 const char *buf, size_t count)
3826 unsigned long t;
3828 if (parse_strtoul(buf, 120, &t))
3829 return -EINVAL;
3831 if (!fan_control_allowed)
3832 return -EPERM;
3834 fan_watchdog_maxinterval = t;
3835 fan_watchdog_reset();
3837 return count;
3840 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3841 fan_fan_watchdog_show, fan_fan_watchdog_store);
3843 /* --------------------------------------------------------------------- */
3844 static struct attribute *fan_attributes[] = {
3845 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3846 &dev_attr_fan_fan1_input.attr,
3847 NULL
3850 static const struct attribute_group fan_attr_group = {
3851 .attrs = fan_attributes,
3854 static int __init fan_init(struct ibm_init_struct *iibm)
3856 int rc;
3858 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3860 mutex_init(&fan_mutex);
3861 fan_status_access_mode = TPACPI_FAN_NONE;
3862 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3863 fan_control_commands = 0;
3864 fan_watchdog_maxinterval = 0;
3865 tp_features.fan_ctrl_status_undef = 0;
3866 fan_control_desired_level = 7;
3868 IBM_ACPIHANDLE_INIT(fans);
3869 IBM_ACPIHANDLE_INIT(gfan);
3870 IBM_ACPIHANDLE_INIT(sfan);
3872 if (gfan_handle) {
3873 /* 570, 600e/x, 770e, 770x */
3874 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3875 } else {
3876 /* all other ThinkPads: note that even old-style
3877 * ThinkPad ECs supports the fan control register */
3878 if (likely(acpi_ec_read(fan_status_offset,
3879 &fan_control_initial_status))) {
3880 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3882 /* In some ThinkPads, neither the EC nor the ACPI
3883 * DSDT initialize the fan status, and it ends up
3884 * being set to 0x07 when it *could* be either
3885 * 0x07 or 0x80.
3887 * Enable for TP-1Y (T43), TP-78 (R51e),
3888 * TP-76 (R52), TP-70 (T43, R52), which are known
3889 * to be buggy. */
3890 if (fan_control_initial_status == 0x07) {
3891 switch (thinkpad_id.ec_model) {
3892 case 0x5931: /* TP-1Y */
3893 case 0x3837: /* TP-78 */
3894 case 0x3637: /* TP-76 */
3895 case 0x3037: /* TP-70 */
3896 printk(IBM_NOTICE
3897 "fan_init: initial fan status is "
3898 "unknown, assuming it is in auto "
3899 "mode\n");
3900 tp_features.fan_ctrl_status_undef = 1;
3904 } else {
3905 printk(IBM_ERR
3906 "ThinkPad ACPI EC access misbehaving, "
3907 "fan status and control unavailable\n");
3908 return 1;
3912 if (sfan_handle) {
3913 /* 570, 770x-JL */
3914 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3915 fan_control_commands |=
3916 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3917 } else {
3918 if (!gfan_handle) {
3919 /* gfan without sfan means no fan control */
3920 /* all other models implement TP EC 0x2f control */
3922 if (fans_handle) {
3923 /* X31, X40, X41 */
3924 fan_control_access_mode =
3925 TPACPI_FAN_WR_ACPI_FANS;
3926 fan_control_commands |=
3927 TPACPI_FAN_CMD_SPEED |
3928 TPACPI_FAN_CMD_LEVEL |
3929 TPACPI_FAN_CMD_ENABLE;
3930 } else {
3931 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3932 fan_control_commands |=
3933 TPACPI_FAN_CMD_LEVEL |
3934 TPACPI_FAN_CMD_ENABLE;
3939 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3940 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3941 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3942 fan_status_access_mode, fan_control_access_mode);
3944 /* fan control master switch */
3945 if (!fan_control_allowed) {
3946 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3947 fan_control_commands = 0;
3948 dbg_printk(TPACPI_DBG_INIT,
3949 "fan control features disabled by parameter\n");
3952 /* update fan_control_desired_level */
3953 if (fan_status_access_mode != TPACPI_FAN_NONE)
3954 fan_get_status_safe(NULL);
3956 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3957 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3958 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3959 &fan_attr_group);
3960 if (!(rc < 0))
3961 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
3962 &driver_attr_fan_watchdog);
3963 if (rc < 0)
3964 return rc;
3965 return 0;
3966 } else
3967 return 1;
3971 * Call with fan_mutex held
3973 static void fan_update_desired_level(u8 status)
3975 if ((status &
3976 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3977 if (status > 7)
3978 fan_control_desired_level = 7;
3979 else
3980 fan_control_desired_level = status;
3984 static int fan_get_status(u8 *status)
3986 u8 s;
3988 /* TODO:
3989 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3991 switch (fan_status_access_mode) {
3992 case TPACPI_FAN_RD_ACPI_GFAN:
3993 /* 570, 600e/x, 770e, 770x */
3995 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3996 return -EIO;
3998 if (likely(status))
3999 *status = s & 0x07;
4001 break;
4003 case TPACPI_FAN_RD_TPEC:
4004 /* all except 570, 600e/x, 770e, 770x */
4005 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4006 return -EIO;
4008 if (likely(status))
4009 *status = s;
4011 break;
4013 default:
4014 return -ENXIO;
4017 return 0;
4020 static int fan_get_status_safe(u8 *status)
4022 int rc;
4023 u8 s;
4025 rc = mutex_lock_interruptible(&fan_mutex);
4026 if (rc < 0)
4027 return rc;
4028 rc = fan_get_status(&s);
4029 if (!rc)
4030 fan_update_desired_level(s);
4031 mutex_unlock(&fan_mutex);
4033 if (status)
4034 *status = s;
4036 return rc;
4039 static void fan_exit(void)
4041 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
4043 /* FIXME: can we really do this unconditionally? */
4044 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
4045 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
4047 cancel_delayed_work(&fan_watchdog_task);
4048 flush_scheduled_work();
4051 static int fan_get_speed(unsigned int *speed)
4053 u8 hi, lo;
4055 switch (fan_status_access_mode) {
4056 case TPACPI_FAN_RD_TPEC:
4057 /* all except 570, 600e/x, 770e, 770x */
4058 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4059 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4060 return -EIO;
4062 if (likely(speed))
4063 *speed = (hi << 8) | lo;
4065 break;
4067 default:
4068 return -ENXIO;
4071 return 0;
4074 static void fan_watchdog_fire(struct work_struct *ignored)
4076 int rc;
4078 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4079 return;
4081 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4082 rc = fan_set_enable();
4083 if (rc < 0) {
4084 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4085 "will try again later...\n", -rc);
4086 /* reschedule for later */
4087 fan_watchdog_reset();
4091 static void fan_watchdog_reset(void)
4093 static int fan_watchdog_active;
4095 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4096 return;
4098 if (fan_watchdog_active)
4099 cancel_delayed_work(&fan_watchdog_task);
4101 if (fan_watchdog_maxinterval > 0 &&
4102 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4103 fan_watchdog_active = 1;
4104 if (!schedule_delayed_work(&fan_watchdog_task,
4105 msecs_to_jiffies(fan_watchdog_maxinterval
4106 * 1000))) {
4107 printk(IBM_ERR "failed to schedule the fan watchdog, "
4108 "watchdog will not trigger\n");
4110 } else
4111 fan_watchdog_active = 0;
4114 static int fan_set_level(int level)
4116 if (!fan_control_allowed)
4117 return -EPERM;
4119 switch (fan_control_access_mode) {
4120 case TPACPI_FAN_WR_ACPI_SFAN:
4121 if (level >= 0 && level <= 7) {
4122 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4123 return -EIO;
4124 } else
4125 return -EINVAL;
4126 break;
4128 case TPACPI_FAN_WR_ACPI_FANS:
4129 case TPACPI_FAN_WR_TPEC:
4130 if ((level != TP_EC_FAN_AUTO) &&
4131 (level != TP_EC_FAN_FULLSPEED) &&
4132 ((level < 0) || (level > 7)))
4133 return -EINVAL;
4135 /* safety net should the EC not support AUTO
4136 * or FULLSPEED mode bits and just ignore them */
4137 if (level & TP_EC_FAN_FULLSPEED)
4138 level |= 7; /* safety min speed 7 */
4139 else if (level & TP_EC_FAN_FULLSPEED)
4140 level |= 4; /* safety min speed 4 */
4142 if (!acpi_ec_write(fan_status_offset, level))
4143 return -EIO;
4144 else
4145 tp_features.fan_ctrl_status_undef = 0;
4146 break;
4148 default:
4149 return -ENXIO;
4151 return 0;
4154 static int fan_set_level_safe(int level)
4156 int rc;
4158 if (!fan_control_allowed)
4159 return -EPERM;
4161 rc = mutex_lock_interruptible(&fan_mutex);
4162 if (rc < 0)
4163 return rc;
4165 if (level == TPACPI_FAN_LAST_LEVEL)
4166 level = fan_control_desired_level;
4168 rc = fan_set_level(level);
4169 if (!rc)
4170 fan_update_desired_level(level);
4172 mutex_unlock(&fan_mutex);
4173 return rc;
4176 static int fan_set_enable(void)
4178 u8 s;
4179 int rc;
4181 if (!fan_control_allowed)
4182 return -EPERM;
4184 rc = mutex_lock_interruptible(&fan_mutex);
4185 if (rc < 0)
4186 return rc;
4188 switch (fan_control_access_mode) {
4189 case TPACPI_FAN_WR_ACPI_FANS:
4190 case TPACPI_FAN_WR_TPEC:
4191 rc = fan_get_status(&s);
4192 if (rc < 0)
4193 break;
4195 /* Don't go out of emergency fan mode */
4196 if (s != 7) {
4197 s &= 0x07;
4198 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4201 if (!acpi_ec_write(fan_status_offset, s))
4202 rc = -EIO;
4203 else {
4204 tp_features.fan_ctrl_status_undef = 0;
4205 rc = 0;
4207 break;
4209 case TPACPI_FAN_WR_ACPI_SFAN:
4210 rc = fan_get_status(&s);
4211 if (rc < 0)
4212 break;
4214 s &= 0x07;
4216 /* Set fan to at least level 4 */
4217 s |= 4;
4219 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4220 rc= -EIO;
4221 else
4222 rc = 0;
4223 break;
4225 default:
4226 rc = -ENXIO;
4229 mutex_unlock(&fan_mutex);
4230 return rc;
4233 static int fan_set_disable(void)
4235 int rc;
4237 if (!fan_control_allowed)
4238 return -EPERM;
4240 rc = mutex_lock_interruptible(&fan_mutex);
4241 if (rc < 0)
4242 return rc;
4244 rc = 0;
4245 switch (fan_control_access_mode) {
4246 case TPACPI_FAN_WR_ACPI_FANS:
4247 case TPACPI_FAN_WR_TPEC:
4248 if (!acpi_ec_write(fan_status_offset, 0x00))
4249 rc = -EIO;
4250 else {
4251 fan_control_desired_level = 0;
4252 tp_features.fan_ctrl_status_undef = 0;
4254 break;
4256 case TPACPI_FAN_WR_ACPI_SFAN:
4257 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4258 rc = -EIO;
4259 else
4260 fan_control_desired_level = 0;
4261 break;
4263 default:
4264 rc = -ENXIO;
4268 mutex_unlock(&fan_mutex);
4269 return rc;
4272 static int fan_set_speed(int speed)
4274 int rc;
4276 if (!fan_control_allowed)
4277 return -EPERM;
4279 rc = mutex_lock_interruptible(&fan_mutex);
4280 if (rc < 0)
4281 return rc;
4283 rc = 0;
4284 switch (fan_control_access_mode) {
4285 case TPACPI_FAN_WR_ACPI_FANS:
4286 if (speed >= 0 && speed <= 65535) {
4287 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4288 speed, speed, speed))
4289 rc = -EIO;
4290 } else
4291 rc = -EINVAL;
4292 break;
4294 default:
4295 rc = -ENXIO;
4298 mutex_unlock(&fan_mutex);
4299 return rc;
4302 static int fan_read(char *p)
4304 int len = 0;
4305 int rc;
4306 u8 status;
4307 unsigned int speed = 0;
4309 switch (fan_status_access_mode) {
4310 case TPACPI_FAN_RD_ACPI_GFAN:
4311 /* 570, 600e/x, 770e, 770x */
4312 if ((rc = fan_get_status_safe(&status)) < 0)
4313 return rc;
4315 len += sprintf(p + len, "status:\t\t%s\n"
4316 "level:\t\t%d\n",
4317 (status != 0) ? "enabled" : "disabled", status);
4318 break;
4320 case TPACPI_FAN_RD_TPEC:
4321 /* all except 570, 600e/x, 770e, 770x */
4322 if ((rc = fan_get_status_safe(&status)) < 0)
4323 return rc;
4325 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4326 if (status != fan_control_initial_status)
4327 tp_features.fan_ctrl_status_undef = 0;
4328 else
4329 /* Return most likely status. In fact, it
4330 * might be the only possible status */
4331 status = TP_EC_FAN_AUTO;
4334 len += sprintf(p + len, "status:\t\t%s\n",
4335 (status != 0) ? "enabled" : "disabled");
4337 if ((rc = fan_get_speed(&speed)) < 0)
4338 return rc;
4340 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4342 if (status & TP_EC_FAN_FULLSPEED)
4343 /* Disengaged mode takes precedence */
4344 len += sprintf(p + len, "level:\t\tdisengaged\n");
4345 else if (status & TP_EC_FAN_AUTO)
4346 len += sprintf(p + len, "level:\t\tauto\n");
4347 else
4348 len += sprintf(p + len, "level:\t\t%d\n", status);
4349 break;
4351 case TPACPI_FAN_NONE:
4352 default:
4353 len += sprintf(p + len, "status:\t\tnot supported\n");
4356 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4357 len += sprintf(p + len, "commands:\tlevel <level>");
4359 switch (fan_control_access_mode) {
4360 case TPACPI_FAN_WR_ACPI_SFAN:
4361 len += sprintf(p + len, " (<level> is 0-7)\n");
4362 break;
4364 default:
4365 len += sprintf(p + len, " (<level> is 0-7, "
4366 "auto, disengaged, full-speed)\n");
4367 break;
4371 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4372 len += sprintf(p + len, "commands:\tenable, disable\n"
4373 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4374 "1-120 (seconds))\n");
4376 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4377 len += sprintf(p + len, "commands:\tspeed <speed>"
4378 " (<speed> is 0-65535)\n");
4380 return len;
4383 static int fan_write_cmd_level(const char *cmd, int *rc)
4385 int level;
4387 if (strlencmp(cmd, "level auto") == 0)
4388 level = TP_EC_FAN_AUTO;
4389 else if ((strlencmp(cmd, "level disengaged") == 0) |
4390 (strlencmp(cmd, "level full-speed") == 0))
4391 level = TP_EC_FAN_FULLSPEED;
4392 else if (sscanf(cmd, "level %d", &level) != 1)
4393 return 0;
4395 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4396 printk(IBM_ERR "level command accepted for unsupported "
4397 "access mode %d", fan_control_access_mode);
4399 return 1;
4402 static int fan_write_cmd_enable(const char *cmd, int *rc)
4404 if (strlencmp(cmd, "enable") != 0)
4405 return 0;
4407 if ((*rc = fan_set_enable()) == -ENXIO)
4408 printk(IBM_ERR "enable command accepted for unsupported "
4409 "access mode %d", fan_control_access_mode);
4411 return 1;
4414 static int fan_write_cmd_disable(const char *cmd, int *rc)
4416 if (strlencmp(cmd, "disable") != 0)
4417 return 0;
4419 if ((*rc = fan_set_disable()) == -ENXIO)
4420 printk(IBM_ERR "disable command accepted for unsupported "
4421 "access mode %d", fan_control_access_mode);
4423 return 1;
4426 static int fan_write_cmd_speed(const char *cmd, int *rc)
4428 int speed;
4430 /* TODO:
4431 * Support speed <low> <medium> <high> ? */
4433 if (sscanf(cmd, "speed %d", &speed) != 1)
4434 return 0;
4436 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4437 printk(IBM_ERR "speed command accepted for unsupported "
4438 "access mode %d", fan_control_access_mode);
4440 return 1;
4443 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4445 int interval;
4447 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4448 return 0;
4450 if (interval < 0 || interval > 120)
4451 *rc = -EINVAL;
4452 else
4453 fan_watchdog_maxinterval = interval;
4455 return 1;
4458 static int fan_write(char *buf)
4460 char *cmd;
4461 int rc = 0;
4463 while (!rc && (cmd = next_cmd(&buf))) {
4464 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4465 fan_write_cmd_level(cmd, &rc)) &&
4466 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4467 (fan_write_cmd_enable(cmd, &rc) ||
4468 fan_write_cmd_disable(cmd, &rc) ||
4469 fan_write_cmd_watchdog(cmd, &rc))) &&
4470 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4471 fan_write_cmd_speed(cmd, &rc))
4473 rc = -EINVAL;
4474 else if (!rc)
4475 fan_watchdog_reset();
4478 return rc;
4481 static struct ibm_struct fan_driver_data = {
4482 .name = "fan",
4483 .read = fan_read,
4484 .write = fan_write,
4485 .exit = fan_exit,
4488 /****************************************************************************
4489 ****************************************************************************
4491 * Infrastructure
4493 ****************************************************************************
4494 ****************************************************************************/
4496 /* sysfs name ---------------------------------------------------------- */
4497 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
4498 struct device_attribute *attr,
4499 char *buf)
4501 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
4504 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
4505 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
4507 /* --------------------------------------------------------------------- */
4509 /* /proc support */
4510 static struct proc_dir_entry *proc_dir;
4512 /* Subdriver registry */
4513 static LIST_HEAD(tpacpi_all_drivers);
4517 * Module and infrastructure proble, init and exit handling
4520 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4521 static const char * __init str_supported(int is_supported)
4523 static char text_unsupported[] __initdata = "not supported";
4525 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4527 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4529 static int __init ibm_init(struct ibm_init_struct *iibm)
4531 int ret;
4532 struct ibm_struct *ibm = iibm->data;
4533 struct proc_dir_entry *entry;
4535 BUG_ON(ibm == NULL);
4537 INIT_LIST_HEAD(&ibm->all_drivers);
4539 if (ibm->flags.experimental && !experimental)
4540 return 0;
4542 dbg_printk(TPACPI_DBG_INIT,
4543 "probing for %s\n", ibm->name);
4545 if (iibm->init) {
4546 ret = iibm->init(iibm);
4547 if (ret > 0)
4548 return 0; /* probe failed */
4549 if (ret)
4550 return ret;
4552 ibm->flags.init_called = 1;
4555 if (ibm->acpi) {
4556 if (ibm->acpi->hid) {
4557 ret = register_tpacpi_subdriver(ibm);
4558 if (ret)
4559 goto err_out;
4562 if (ibm->acpi->notify) {
4563 ret = setup_acpi_notify(ibm);
4564 if (ret == -ENODEV) {
4565 printk(IBM_NOTICE "disabling subdriver %s\n",
4566 ibm->name);
4567 ret = 0;
4568 goto err_out;
4570 if (ret < 0)
4571 goto err_out;
4575 dbg_printk(TPACPI_DBG_INIT,
4576 "%s installed\n", ibm->name);
4578 if (ibm->read) {
4579 entry = create_proc_entry(ibm->name,
4580 S_IFREG | S_IRUGO | S_IWUSR,
4581 proc_dir);
4582 if (!entry) {
4583 printk(IBM_ERR "unable to create proc entry %s\n",
4584 ibm->name);
4585 ret = -ENODEV;
4586 goto err_out;
4588 entry->owner = THIS_MODULE;
4589 entry->data = ibm;
4590 entry->read_proc = &dispatch_procfs_read;
4591 if (ibm->write)
4592 entry->write_proc = &dispatch_procfs_write;
4593 ibm->flags.proc_created = 1;
4596 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4598 return 0;
4600 err_out:
4601 dbg_printk(TPACPI_DBG_INIT,
4602 "%s: at error exit path with result %d\n",
4603 ibm->name, ret);
4605 ibm_exit(ibm);
4606 return (ret < 0)? ret : 0;
4609 static void ibm_exit(struct ibm_struct *ibm)
4611 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4613 list_del_init(&ibm->all_drivers);
4615 if (ibm->flags.acpi_notify_installed) {
4616 dbg_printk(TPACPI_DBG_EXIT,
4617 "%s: acpi_remove_notify_handler\n", ibm->name);
4618 BUG_ON(!ibm->acpi);
4619 acpi_remove_notify_handler(*ibm->acpi->handle,
4620 ibm->acpi->type,
4621 dispatch_acpi_notify);
4622 ibm->flags.acpi_notify_installed = 0;
4623 ibm->flags.acpi_notify_installed = 0;
4626 if (ibm->flags.proc_created) {
4627 dbg_printk(TPACPI_DBG_EXIT,
4628 "%s: remove_proc_entry\n", ibm->name);
4629 remove_proc_entry(ibm->name, proc_dir);
4630 ibm->flags.proc_created = 0;
4633 if (ibm->flags.acpi_driver_registered) {
4634 dbg_printk(TPACPI_DBG_EXIT,
4635 "%s: acpi_bus_unregister_driver\n", ibm->name);
4636 BUG_ON(!ibm->acpi);
4637 acpi_bus_unregister_driver(ibm->acpi->driver);
4638 kfree(ibm->acpi->driver);
4639 ibm->acpi->driver = NULL;
4640 ibm->flags.acpi_driver_registered = 0;
4643 if (ibm->flags.init_called && ibm->exit) {
4644 ibm->exit();
4645 ibm->flags.init_called = 0;
4648 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4651 /* Probing */
4653 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4655 struct dmi_device *dev = NULL;
4656 char ec_fw_string[18];
4658 if (!tp)
4659 return;
4661 memset(tp, 0, sizeof(*tp));
4663 if (dmi_name_in_vendors("IBM"))
4664 tp->vendor = PCI_VENDOR_ID_IBM;
4665 else if (dmi_name_in_vendors("LENOVO"))
4666 tp->vendor = PCI_VENDOR_ID_LENOVO;
4667 else
4668 return;
4670 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4671 GFP_KERNEL);
4672 if (!tp->bios_version_str)
4673 return;
4674 tp->bios_model = tp->bios_version_str[0]
4675 | (tp->bios_version_str[1] << 8);
4678 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4679 * X32 or newer, all Z series; Some models must have an
4680 * up-to-date BIOS or they will not be detected.
4682 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4684 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4685 if (sscanf(dev->name,
4686 "IBM ThinkPad Embedded Controller -[%17c",
4687 ec_fw_string) == 1) {
4688 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4689 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4691 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4692 tp->ec_model = ec_fw_string[0]
4693 | (ec_fw_string[1] << 8);
4694 break;
4698 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4699 GFP_KERNEL);
4700 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4701 kfree(tp->model_str);
4702 tp->model_str = NULL;
4706 static int __init probe_for_thinkpad(void)
4708 int is_thinkpad;
4710 if (acpi_disabled)
4711 return -ENODEV;
4714 * Non-ancient models have better DMI tagging, but very old models
4715 * don't.
4717 is_thinkpad = (thinkpad_id.model_str != NULL);
4719 /* ec is required because many other handles are relative to it */
4720 IBM_ACPIHANDLE_INIT(ec);
4721 if (!ec_handle) {
4722 if (is_thinkpad)
4723 printk(IBM_ERR
4724 "Not yet supported ThinkPad detected!\n");
4725 return -ENODEV;
4729 * Risks a regression on very old machines, but reduces potential
4730 * false positives a damn great deal
4732 if (!is_thinkpad)
4733 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4735 if (!is_thinkpad && !force_load)
4736 return -ENODEV;
4738 return 0;
4742 /* Module init, exit, parameters */
4744 static struct ibm_init_struct ibms_init[] __initdata = {
4746 .init = thinkpad_acpi_driver_init,
4747 .data = &thinkpad_acpi_driver_data,
4750 .init = hotkey_init,
4751 .data = &hotkey_driver_data,
4754 .init = bluetooth_init,
4755 .data = &bluetooth_driver_data,
4758 .init = wan_init,
4759 .data = &wan_driver_data,
4762 .init = video_init,
4763 .data = &video_driver_data,
4766 .init = light_init,
4767 .data = &light_driver_data,
4769 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4771 .init = dock_init,
4772 .data = &dock_driver_data[0],
4775 .init = dock_init2,
4776 .data = &dock_driver_data[1],
4778 #endif
4779 #ifdef CONFIG_THINKPAD_ACPI_BAY
4781 .init = bay_init,
4782 .data = &bay_driver_data,
4784 #endif
4786 .init = cmos_init,
4787 .data = &cmos_driver_data,
4790 .init = led_init,
4791 .data = &led_driver_data,
4794 .init = beep_init,
4795 .data = &beep_driver_data,
4798 .init = thermal_init,
4799 .data = &thermal_driver_data,
4802 .data = &ecdump_driver_data,
4805 .init = brightness_init,
4806 .data = &brightness_driver_data,
4809 .data = &volume_driver_data,
4812 .init = fan_init,
4813 .data = &fan_driver_data,
4817 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4819 unsigned int i;
4820 struct ibm_struct *ibm;
4822 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4823 ibm = ibms_init[i].data;
4824 BUG_ON(ibm == NULL);
4826 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4827 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4828 return -ENOSPC;
4829 strcpy(ibms_init[i].param, val);
4830 strcat(ibms_init[i].param, ",");
4831 return 0;
4835 return -EINVAL;
4838 static int experimental;
4839 module_param(experimental, int, 0);
4841 static u32 dbg_level;
4842 module_param_named(debug, dbg_level, uint, 0);
4844 static int force_load;
4845 module_param(force_load, bool, 0);
4847 static int fan_control_allowed;
4848 module_param_named(fan_control, fan_control_allowed, bool, 0);
4850 static int brightness_mode;
4851 module_param_named(brightness_mode, brightness_mode, int, 0);
4853 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4854 module_param(brightness_enable, uint, 0);
4856 static unsigned int hotkey_report_mode;
4857 module_param(hotkey_report_mode, uint, 0);
4859 #define IBM_PARAM(feature) \
4860 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4862 IBM_PARAM(hotkey);
4863 IBM_PARAM(bluetooth);
4864 IBM_PARAM(video);
4865 IBM_PARAM(light);
4866 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4867 IBM_PARAM(dock);
4868 #endif
4869 #ifdef CONFIG_THINKPAD_ACPI_BAY
4870 IBM_PARAM(bay);
4871 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4872 IBM_PARAM(cmos);
4873 IBM_PARAM(led);
4874 IBM_PARAM(beep);
4875 IBM_PARAM(ecdump);
4876 IBM_PARAM(brightness);
4877 IBM_PARAM(volume);
4878 IBM_PARAM(fan);
4880 static int __init thinkpad_acpi_module_init(void)
4882 int ret, i;
4884 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4886 /* Parameter checking */
4887 if (hotkey_report_mode > 2)
4888 return -EINVAL;
4890 /* Driver-level probe */
4892 get_thinkpad_model_data(&thinkpad_id);
4893 ret = probe_for_thinkpad();
4894 if (ret) {
4895 thinkpad_acpi_module_exit();
4896 return ret;
4899 /* Driver initialization */
4901 IBM_ACPIHANDLE_INIT(ecrd);
4902 IBM_ACPIHANDLE_INIT(ecwr);
4904 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4905 if (!proc_dir) {
4906 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4907 thinkpad_acpi_module_exit();
4908 return -ENODEV;
4910 proc_dir->owner = THIS_MODULE;
4912 ret = platform_driver_register(&tpacpi_pdriver);
4913 if (ret) {
4914 printk(IBM_ERR "unable to register main platform driver\n");
4915 thinkpad_acpi_module_exit();
4916 return ret;
4918 tp_features.platform_drv_registered = 1;
4920 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
4921 if (ret) {
4922 printk(IBM_ERR "unable to register hwmon platform driver\n");
4923 thinkpad_acpi_module_exit();
4924 return ret;
4926 tp_features.sensors_pdrv_registered = 1;
4928 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4929 if (!ret) {
4930 tp_features.platform_drv_attrs_registered = 1;
4931 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
4933 if (ret) {
4934 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4935 thinkpad_acpi_module_exit();
4936 return ret;
4938 tp_features.sensors_pdrv_attrs_registered = 1;
4941 /* Device initialization */
4942 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4943 NULL, 0);
4944 if (IS_ERR(tpacpi_pdev)) {
4945 ret = PTR_ERR(tpacpi_pdev);
4946 tpacpi_pdev = NULL;
4947 printk(IBM_ERR "unable to register platform device\n");
4948 thinkpad_acpi_module_exit();
4949 return ret;
4951 tpacpi_sensors_pdev = platform_device_register_simple(
4952 IBM_HWMON_DRVR_NAME,
4953 -1, NULL, 0);
4954 if (IS_ERR(tpacpi_sensors_pdev)) {
4955 ret = PTR_ERR(tpacpi_sensors_pdev);
4956 tpacpi_sensors_pdev = NULL;
4957 printk(IBM_ERR "unable to register hwmon platform device\n");
4958 thinkpad_acpi_module_exit();
4959 return ret;
4961 ret = device_create_file(&tpacpi_sensors_pdev->dev,
4962 &dev_attr_thinkpad_acpi_pdev_name);
4963 if (ret) {
4964 printk(IBM_ERR
4965 "unable to create sysfs hwmon device attributes\n");
4966 thinkpad_acpi_module_exit();
4967 return ret;
4969 tp_features.sensors_pdev_attrs_registered = 1;
4970 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
4971 if (IS_ERR(tpacpi_hwmon)) {
4972 ret = PTR_ERR(tpacpi_hwmon);
4973 tpacpi_hwmon = NULL;
4974 printk(IBM_ERR "unable to register hwmon device\n");
4975 thinkpad_acpi_module_exit();
4976 return ret;
4978 mutex_init(&tpacpi_inputdev_send_mutex);
4979 tpacpi_inputdev = input_allocate_device();
4980 if (!tpacpi_inputdev) {
4981 printk(IBM_ERR "unable to allocate input device\n");
4982 thinkpad_acpi_module_exit();
4983 return -ENOMEM;
4984 } else {
4985 /* Prepare input device, but don't register */
4986 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4987 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4988 tpacpi_inputdev->id.bustype = BUS_HOST;
4989 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4990 thinkpad_id.vendor :
4991 PCI_VENDOR_ID_IBM;
4992 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4993 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4995 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4996 ret = ibm_init(&ibms_init[i]);
4997 if (ret >= 0 && *ibms_init[i].param)
4998 ret = ibms_init[i].data->write(ibms_init[i].param);
4999 if (ret < 0) {
5000 thinkpad_acpi_module_exit();
5001 return ret;
5004 ret = input_register_device(tpacpi_inputdev);
5005 if (ret < 0) {
5006 printk(IBM_ERR "unable to register input device\n");
5007 thinkpad_acpi_module_exit();
5008 return ret;
5009 } else {
5010 tp_features.input_device_registered = 1;
5013 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5014 return 0;
5017 static void thinkpad_acpi_module_exit(void)
5019 struct ibm_struct *ibm, *itmp;
5021 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5023 list_for_each_entry_safe_reverse(ibm, itmp,
5024 &tpacpi_all_drivers,
5025 all_drivers) {
5026 ibm_exit(ibm);
5029 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5031 if (tpacpi_inputdev) {
5032 if (tp_features.input_device_registered)
5033 input_unregister_device(tpacpi_inputdev);
5034 else
5035 input_free_device(tpacpi_inputdev);
5038 if (tpacpi_hwmon)
5039 hwmon_device_unregister(tpacpi_hwmon);
5041 if (tp_features.sensors_pdev_attrs_registered)
5042 device_remove_file(&tpacpi_sensors_pdev->dev,
5043 &dev_attr_thinkpad_acpi_pdev_name);
5044 if (tpacpi_sensors_pdev)
5045 platform_device_unregister(tpacpi_sensors_pdev);
5046 if (tpacpi_pdev)
5047 platform_device_unregister(tpacpi_pdev);
5049 if (tp_features.sensors_pdrv_attrs_registered)
5050 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5051 if (tp_features.platform_drv_attrs_registered)
5052 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5054 if (tp_features.sensors_pdrv_registered)
5055 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5057 if (tp_features.platform_drv_registered)
5058 platform_driver_unregister(&tpacpi_pdriver);
5060 if (proc_dir)
5061 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5063 kfree(thinkpad_id.bios_version_str);
5064 kfree(thinkpad_id.ec_version_str);
5065 kfree(thinkpad_id.model_str);
5068 module_init(thinkpad_acpi_module_init);
5069 module_exit(thinkpad_acpi_module_exit);