ACPI: thinkpad-acpi: dequeue all pending hot key events at once (v2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob0e0d5c5566f037ef883106d334b029d52bba0b7f
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 0x010000
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 class_device *tpacpi_hwmon;
529 static struct input_dev *tpacpi_inputdev;
530 static struct mutex tpacpi_inputdev_send_mutex;
533 static int tpacpi_resume_handler(struct platform_device *pdev)
535 struct ibm_struct *ibm, *itmp;
537 list_for_each_entry_safe(ibm, itmp,
538 &tpacpi_all_drivers,
539 all_drivers) {
540 if (ibm->resume)
541 (ibm->resume)();
544 return 0;
547 static struct platform_driver tpacpi_pdriver = {
548 .driver = {
549 .name = IBM_DRVR_NAME,
550 .owner = THIS_MODULE,
552 .resume = tpacpi_resume_handler,
556 /*************************************************************************
557 * thinkpad-acpi driver attributes
560 /* interface_version --------------------------------------------------- */
561 static ssize_t tpacpi_driver_interface_version_show(
562 struct device_driver *drv,
563 char *buf)
565 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
568 static DRIVER_ATTR(interface_version, S_IRUGO,
569 tpacpi_driver_interface_version_show, NULL);
571 /* debug_level --------------------------------------------------------- */
572 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
573 char *buf)
575 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
578 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
579 const char *buf, size_t count)
581 unsigned long t;
583 if (parse_strtoul(buf, 0xffff, &t))
584 return -EINVAL;
586 dbg_level = t;
588 return count;
591 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
592 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
594 /* version ------------------------------------------------------------- */
595 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
596 char *buf)
598 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
601 static DRIVER_ATTR(version, S_IRUGO,
602 tpacpi_driver_version_show, NULL);
604 /* --------------------------------------------------------------------- */
606 static struct driver_attribute* tpacpi_driver_attributes[] = {
607 &driver_attr_debug_level, &driver_attr_version,
608 &driver_attr_interface_version,
611 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
613 int i, res;
615 i = 0;
616 res = 0;
617 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
618 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
619 i++;
622 return res;
625 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
627 int i;
629 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
630 driver_remove_file(drv, tpacpi_driver_attributes[i]);
633 /*************************************************************************
634 * sysfs support helpers
637 struct attribute_set_obj {
638 struct attribute_set s;
639 struct attribute *a;
640 } __attribute__((packed));
642 static struct attribute_set *create_attr_set(unsigned int max_members,
643 const char* name)
645 struct attribute_set_obj *sobj;
647 if (max_members == 0)
648 return NULL;
650 /* Allocates space for implicit NULL at the end too */
651 sobj = kzalloc(sizeof(struct attribute_set_obj) +
652 max_members * sizeof(struct attribute *),
653 GFP_KERNEL);
654 if (!sobj)
655 return NULL;
656 sobj->s.max_members = max_members;
657 sobj->s.group.attrs = &sobj->a;
658 sobj->s.group.name = name;
660 return &sobj->s;
663 /* not multi-threaded safe, use it in a single thread per set */
664 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
666 if (!s || !attr)
667 return -EINVAL;
669 if (s->members >= s->max_members)
670 return -ENOMEM;
672 s->group.attrs[s->members] = attr;
673 s->members++;
675 return 0;
678 static int add_many_to_attr_set(struct attribute_set* s,
679 struct attribute **attr,
680 unsigned int count)
682 int i, res;
684 for (i = 0; i < count; i++) {
685 res = add_to_attr_set(s, attr[i]);
686 if (res)
687 return res;
690 return 0;
693 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
695 sysfs_remove_group(kobj, &s->group);
696 destroy_attr_set(s);
699 static int parse_strtoul(const char *buf,
700 unsigned long max, unsigned long *value)
702 char *endp;
704 *value = simple_strtoul(buf, &endp, 0);
705 while (*endp && isspace(*endp))
706 endp++;
707 if (*endp || *value > max)
708 return -EINVAL;
710 return 0;
713 /****************************************************************************
714 ****************************************************************************
716 * Subdrivers
718 ****************************************************************************
719 ****************************************************************************/
721 /*************************************************************************
722 * thinkpad-acpi init subdriver
725 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
727 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
728 printk(IBM_INFO "%s\n", IBM_URL);
730 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
731 (thinkpad_id.bios_version_str) ?
732 thinkpad_id.bios_version_str : "unknown",
733 (thinkpad_id.ec_version_str) ?
734 thinkpad_id.ec_version_str : "unknown");
736 if (thinkpad_id.vendor && thinkpad_id.model_str)
737 printk(IBM_INFO "%s %s\n",
738 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
739 "IBM" : ((thinkpad_id.vendor ==
740 PCI_VENDOR_ID_LENOVO) ?
741 "Lenovo" : "Unknown vendor"),
742 thinkpad_id.model_str);
744 return 0;
747 static int thinkpad_acpi_driver_read(char *p)
749 int len = 0;
751 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
752 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
754 return len;
757 static struct ibm_struct thinkpad_acpi_driver_data = {
758 .name = "driver",
759 .read = thinkpad_acpi_driver_read,
762 /*************************************************************************
763 * Hotkey subdriver
766 static int hotkey_orig_status;
767 static u32 hotkey_orig_mask;
768 static u32 hotkey_all_mask;
769 static u32 hotkey_reserved_mask;
771 static u16 *hotkey_keycode_map;
773 static struct attribute_set *hotkey_dev_attributes;
775 static int hotkey_get_wlsw(int *status)
777 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
778 return -EIO;
779 return 0;
782 /* sysfs hotkey enable ------------------------------------------------- */
783 static ssize_t hotkey_enable_show(struct device *dev,
784 struct device_attribute *attr,
785 char *buf)
787 int res, status;
788 u32 mask;
790 res = hotkey_get(&status, &mask);
791 if (res)
792 return res;
794 return snprintf(buf, PAGE_SIZE, "%d\n", status);
797 static ssize_t hotkey_enable_store(struct device *dev,
798 struct device_attribute *attr,
799 const char *buf, size_t count)
801 unsigned long t;
802 int res, status;
803 u32 mask;
805 if (parse_strtoul(buf, 1, &t))
806 return -EINVAL;
808 res = hotkey_get(&status, &mask);
809 if (!res)
810 res = hotkey_set(t, mask);
812 return (res) ? res : count;
815 static struct device_attribute dev_attr_hotkey_enable =
816 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
817 hotkey_enable_show, hotkey_enable_store);
819 /* sysfs hotkey mask --------------------------------------------------- */
820 static ssize_t hotkey_mask_show(struct device *dev,
821 struct device_attribute *attr,
822 char *buf)
824 int res, status;
825 u32 mask;
827 res = hotkey_get(&status, &mask);
828 if (res)
829 return res;
831 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
834 static ssize_t hotkey_mask_store(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf, size_t count)
838 unsigned long t;
839 int res, status;
840 u32 mask;
842 if (parse_strtoul(buf, 0xffffffffUL, &t))
843 return -EINVAL;
845 res = hotkey_get(&status, &mask);
846 if (!res)
847 hotkey_set(status, t);
849 return (res) ? res : count;
852 static struct device_attribute dev_attr_hotkey_mask =
853 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
854 hotkey_mask_show, hotkey_mask_store);
856 /* sysfs hotkey bios_enabled ------------------------------------------- */
857 static ssize_t hotkey_bios_enabled_show(struct device *dev,
858 struct device_attribute *attr,
859 char *buf)
861 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
864 static struct device_attribute dev_attr_hotkey_bios_enabled =
865 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
867 /* sysfs hotkey bios_mask ---------------------------------------------- */
868 static ssize_t hotkey_bios_mask_show(struct device *dev,
869 struct device_attribute *attr,
870 char *buf)
872 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
875 static struct device_attribute dev_attr_hotkey_bios_mask =
876 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
878 /* sysfs hotkey all_mask ----------------------------------------------- */
879 static ssize_t hotkey_all_mask_show(struct device *dev,
880 struct device_attribute *attr,
881 char *buf)
883 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
886 static struct device_attribute dev_attr_hotkey_all_mask =
887 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
889 /* sysfs hotkey recommended_mask --------------------------------------- */
890 static ssize_t hotkey_recommended_mask_show(struct device *dev,
891 struct device_attribute *attr,
892 char *buf)
894 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
895 hotkey_all_mask & ~hotkey_reserved_mask);
898 static struct device_attribute dev_attr_hotkey_recommended_mask =
899 __ATTR(hotkey_recommended_mask, S_IRUGO,
900 hotkey_recommended_mask_show, NULL);
902 /* sysfs hotkey radio_sw ----------------------------------------------- */
903 static ssize_t hotkey_radio_sw_show(struct device *dev,
904 struct device_attribute *attr,
905 char *buf)
907 int res, s;
908 res = hotkey_get_wlsw(&s);
909 if (res < 0)
910 return res;
912 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
915 static struct device_attribute dev_attr_hotkey_radio_sw =
916 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
918 /* sysfs hotkey report_mode -------------------------------------------- */
919 static ssize_t hotkey_report_mode_show(struct device *dev,
920 struct device_attribute *attr,
921 char *buf)
923 return snprintf(buf, PAGE_SIZE, "%d\n",
924 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
927 static ssize_t hotkey_report_mode_store(struct device *dev,
928 struct device_attribute *attr,
929 const char *buf, size_t count)
931 unsigned long t;
932 int res = 0;
934 if (parse_strtoul(buf, 2, &t) || (t < 1))
935 return -EINVAL;
937 if (t != hotkey_report_mode) {
938 if (tp_features.hotkey_report_mode_locked)
939 return -EPERM;
940 hotkey_report_mode = t;
941 printk(IBM_NOTICE "hot key report mode set to %d\n",
942 hotkey_report_mode);
945 return (res) ? res : count;
948 static struct device_attribute dev_attr_hotkey_report_mode =
949 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
950 hotkey_report_mode_show, hotkey_report_mode_store);
952 /* --------------------------------------------------------------------- */
954 static struct attribute *hotkey_attributes[] __initdata = {
955 &dev_attr_hotkey_enable.attr,
956 &dev_attr_hotkey_report_mode.attr,
959 static struct attribute *hotkey_mask_attributes[] __initdata = {
960 &dev_attr_hotkey_mask.attr,
961 &dev_attr_hotkey_bios_enabled.attr,
962 &dev_attr_hotkey_bios_mask.attr,
963 &dev_attr_hotkey_all_mask.attr,
964 &dev_attr_hotkey_recommended_mask.attr,
967 static int __init hotkey_init(struct ibm_init_struct *iibm)
970 static u16 ibm_keycode_map[] __initdata = {
971 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
972 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
973 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
974 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
975 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
976 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
977 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
978 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
979 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
980 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
981 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
982 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
983 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
984 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
985 KEY_RESERVED, /* 0x14: VOLUME UP */
986 KEY_RESERVED, /* 0x15: VOLUME DOWN */
987 KEY_RESERVED, /* 0x16: MUTE */
988 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
989 /* (assignments unknown, please report if found) */
990 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
991 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
993 static u16 lenovo_keycode_map[] __initdata = {
994 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
995 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
996 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
997 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
998 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
999 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1000 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1001 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1002 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1003 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1004 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1005 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1006 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1007 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1008 KEY_RESERVED, /* 0x14: VOLUME UP */
1009 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1010 KEY_RESERVED, /* 0x16: MUTE */
1011 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1012 /* (assignments unknown, please report if found) */
1013 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1014 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1017 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1018 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1019 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1021 int res, i;
1022 int status;
1023 int hkeyv;
1025 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1027 BUG_ON(!tpacpi_inputdev);
1029 IBM_ACPIHANDLE_INIT(hkey);
1030 mutex_init(&hotkey_mutex);
1032 /* hotkey not supported on 570 */
1033 tp_features.hotkey = hkey_handle != NULL;
1035 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1036 str_supported(tp_features.hotkey));
1038 if (tp_features.hotkey) {
1039 hotkey_dev_attributes = create_attr_set(8, NULL);
1040 if (!hotkey_dev_attributes)
1041 return -ENOMEM;
1042 res = add_many_to_attr_set(hotkey_dev_attributes,
1043 hotkey_attributes,
1044 ARRAY_SIZE(hotkey_attributes));
1045 if (res)
1046 return res;
1048 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1049 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1050 for HKEY interface version 0x100 */
1051 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1052 if ((hkeyv >> 8) != 1) {
1053 printk(IBM_ERR "unknown version of the "
1054 "HKEY interface: 0x%x\n", hkeyv);
1055 printk(IBM_ERR "please report this to %s\n",
1056 IBM_MAIL);
1057 } else {
1059 * MHKV 0x100 in A31, R40, R40e,
1060 * T4x, X31, and later
1061 * */
1062 tp_features.hotkey_mask = 1;
1066 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1067 str_supported(tp_features.hotkey_mask));
1069 if (tp_features.hotkey_mask) {
1070 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1071 "MHKA", "qd")) {
1072 printk(IBM_ERR
1073 "missing MHKA handler, "
1074 "please report this to %s\n",
1075 IBM_MAIL);
1076 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1080 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1081 if (!res && tp_features.hotkey_mask) {
1082 res = add_many_to_attr_set(hotkey_dev_attributes,
1083 hotkey_mask_attributes,
1084 ARRAY_SIZE(hotkey_mask_attributes));
1087 /* Not all thinkpads have a hardware radio switch */
1088 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1089 tp_features.hotkey_wlsw = 1;
1090 printk(IBM_INFO
1091 "radio switch found; radios are %s\n",
1092 enabled(status, 0));
1093 res = add_to_attr_set(hotkey_dev_attributes,
1094 &dev_attr_hotkey_radio_sw.attr);
1097 if (!res)
1098 res = register_attr_set_with_sysfs(
1099 hotkey_dev_attributes,
1100 &tpacpi_pdev->dev.kobj);
1101 if (res)
1102 return res;
1104 /* Set up key map */
1106 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1107 GFP_KERNEL);
1108 if (!hotkey_keycode_map) {
1109 printk(IBM_ERR "failed to allocate memory for key map\n");
1110 return -ENOMEM;
1113 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1114 dbg_printk(TPACPI_DBG_INIT,
1115 "using Lenovo default hot key map\n");
1116 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1117 TPACPI_HOTKEY_MAP_SIZE);
1118 } else {
1119 dbg_printk(TPACPI_DBG_INIT,
1120 "using IBM default hot key map\n");
1121 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1122 TPACPI_HOTKEY_MAP_SIZE);
1125 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1126 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1127 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1128 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1129 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1130 tpacpi_inputdev->keycode = hotkey_keycode_map;
1131 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1132 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1133 set_bit(hotkey_keycode_map[i],
1134 tpacpi_inputdev->keybit);
1135 } else {
1136 if (i < sizeof(hotkey_reserved_mask)*8)
1137 hotkey_reserved_mask |= 1 << i;
1141 if (tp_features.hotkey_wlsw) {
1142 set_bit(EV_SW, tpacpi_inputdev->evbit);
1143 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1146 dbg_printk(TPACPI_DBG_INIT,
1147 "enabling hot key handling\n");
1148 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1149 | hotkey_orig_mask);
1150 if (res)
1151 return res;
1153 if (hotkey_report_mode > 0) {
1154 tp_features.hotkey_report_mode_locked = 1;
1155 } else {
1156 hotkey_report_mode = 1;
1157 vdbg_printk(TPACPI_DBG_INIT,
1158 "hot key reporting mode can be "
1159 "changed at runtime\n");
1162 dbg_printk(TPACPI_DBG_INIT,
1163 "legacy hot key reporting over procfs %s\n",
1164 (hotkey_report_mode < 2) ?
1165 "enabled" : "disabled");
1168 return (tp_features.hotkey)? 0 : 1;
1171 static void hotkey_exit(void)
1173 int res;
1175 if (tp_features.hotkey) {
1176 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1177 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1178 if (res)
1179 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1182 if (hotkey_dev_attributes) {
1183 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1184 hotkey_dev_attributes = NULL;
1188 static void tpacpi_input_send_key(unsigned int scancode,
1189 unsigned int keycode)
1191 if (keycode != KEY_RESERVED) {
1192 mutex_lock(&tpacpi_inputdev_send_mutex);
1194 input_report_key(tpacpi_inputdev, keycode, 1);
1195 if (keycode == KEY_UNKNOWN)
1196 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1197 scancode);
1198 input_sync(tpacpi_inputdev);
1200 input_report_key(tpacpi_inputdev, keycode, 0);
1201 if (keycode == KEY_UNKNOWN)
1202 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1203 scancode);
1204 input_sync(tpacpi_inputdev);
1206 mutex_unlock(&tpacpi_inputdev_send_mutex);
1210 static void tpacpi_input_send_radiosw(void)
1212 int wlsw;
1214 mutex_lock(&tpacpi_inputdev_send_mutex);
1216 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1217 input_report_switch(tpacpi_inputdev,
1218 SW_RADIO, !!wlsw);
1219 input_sync(tpacpi_inputdev);
1222 mutex_unlock(&tpacpi_inputdev_send_mutex);
1225 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1227 u32 hkey;
1228 unsigned int keycode, scancode;
1229 int send_acpi_ev;
1231 if (event != 0x80) {
1232 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1233 /* forward it to userspace, maybe it knows how to handle it */
1234 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1236 return;
1239 while (1) {
1240 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1241 printk(IBM_ERR "failed to retrieve HKEY event\n");
1242 return;
1245 if (hkey == 0) {
1246 /* queue empty */
1247 return;
1250 send_acpi_ev = 0;
1252 switch (hkey >> 12) {
1253 case 1:
1254 /* 0x1000-0x1FFF: key presses */
1255 scancode = hkey & 0xfff;
1256 if (scancode > 0 && scancode < 0x21) {
1257 scancode--;
1258 keycode = hotkey_keycode_map[scancode];
1259 tpacpi_input_send_key(scancode, keycode);
1260 } else {
1261 printk(IBM_ERR
1262 "hotkey 0x%04x out of range for keyboard map\n",
1263 hkey);
1264 send_acpi_ev = 1;
1266 break;
1267 case 5:
1268 /* 0x5000-0x5FFF: LID */
1269 /* we don't handle it through this path, just
1270 * eat up known LID events */
1271 if (hkey != 0x5001 && hkey != 0x5002) {
1272 printk(IBM_ERR
1273 "unknown LID-related HKEY event: 0x%04x\n",
1274 hkey);
1275 send_acpi_ev = 1;
1277 break;
1278 case 7:
1279 /* 0x7000-0x7FFF: misc */
1280 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1281 tpacpi_input_send_radiosw();
1282 break;
1284 /* fallthrough to default */
1285 default:
1286 /* case 2: dock-related */
1287 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1288 /* case 3: ultra-bay related. maybe bay in dock? */
1289 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1290 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1291 send_acpi_ev = 1;
1294 if (send_acpi_ev || hotkey_report_mode < 2) {
1295 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1300 static void hotkey_resume(void)
1302 tpacpi_input_send_radiosw();
1306 * Call with hotkey_mutex held
1308 static int hotkey_get(int *status, u32 *mask)
1310 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1311 return -EIO;
1313 if (tp_features.hotkey_mask)
1314 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1315 return -EIO;
1317 return 0;
1321 * Call with hotkey_mutex held
1323 static int hotkey_set(int status, u32 mask)
1325 int i;
1327 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1328 return -EIO;
1330 if (tp_features.hotkey_mask)
1331 for (i = 0; i < 32; i++) {
1332 int bit = ((1 << i) & mask) != 0;
1333 if (!acpi_evalf(hkey_handle,
1334 NULL, "MHKM", "vdd", i + 1, bit))
1335 return -EIO;
1338 return 0;
1341 /* procfs -------------------------------------------------------------- */
1342 static int hotkey_read(char *p)
1344 int res, status;
1345 u32 mask;
1346 int len = 0;
1348 if (!tp_features.hotkey) {
1349 len += sprintf(p + len, "status:\t\tnot supported\n");
1350 return len;
1353 res = mutex_lock_interruptible(&hotkey_mutex);
1354 if (res < 0)
1355 return res;
1356 res = hotkey_get(&status, &mask);
1357 mutex_unlock(&hotkey_mutex);
1358 if (res)
1359 return res;
1361 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1362 if (tp_features.hotkey_mask) {
1363 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1364 len += sprintf(p + len,
1365 "commands:\tenable, disable, reset, <mask>\n");
1366 } else {
1367 len += sprintf(p + len, "mask:\t\tnot supported\n");
1368 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1371 return len;
1374 static int hotkey_write(char *buf)
1376 int res, status;
1377 u32 mask;
1378 char *cmd;
1379 int do_cmd = 0;
1381 if (!tp_features.hotkey)
1382 return -ENODEV;
1384 res = mutex_lock_interruptible(&hotkey_mutex);
1385 if (res < 0)
1386 return res;
1388 res = hotkey_get(&status, &mask);
1389 if (res)
1390 goto errexit;
1392 res = 0;
1393 while ((cmd = next_cmd(&buf))) {
1394 if (strlencmp(cmd, "enable") == 0) {
1395 status = 1;
1396 } else if (strlencmp(cmd, "disable") == 0) {
1397 status = 0;
1398 } else if (strlencmp(cmd, "reset") == 0) {
1399 status = hotkey_orig_status;
1400 mask = hotkey_orig_mask;
1401 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1402 /* mask set */
1403 } else if (sscanf(cmd, "%x", &mask) == 1) {
1404 /* mask set */
1405 } else {
1406 res = -EINVAL;
1407 goto errexit;
1409 do_cmd = 1;
1412 if (do_cmd)
1413 res = hotkey_set(status, mask);
1415 errexit:
1416 mutex_unlock(&hotkey_mutex);
1417 return res;
1420 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1421 .hid = IBM_HKEY_HID,
1422 .notify = hotkey_notify,
1423 .handle = &hkey_handle,
1424 .type = ACPI_DEVICE_NOTIFY,
1427 static struct ibm_struct hotkey_driver_data = {
1428 .name = "hotkey",
1429 .read = hotkey_read,
1430 .write = hotkey_write,
1431 .exit = hotkey_exit,
1432 .resume = hotkey_resume,
1433 .acpi = &ibm_hotkey_acpidriver,
1436 /*************************************************************************
1437 * Bluetooth subdriver
1440 /* sysfs bluetooth enable ---------------------------------------------- */
1441 static ssize_t bluetooth_enable_show(struct device *dev,
1442 struct device_attribute *attr,
1443 char *buf)
1445 int status;
1447 status = bluetooth_get_radiosw();
1448 if (status < 0)
1449 return status;
1451 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1454 static ssize_t bluetooth_enable_store(struct device *dev,
1455 struct device_attribute *attr,
1456 const char *buf, size_t count)
1458 unsigned long t;
1459 int res;
1461 if (parse_strtoul(buf, 1, &t))
1462 return -EINVAL;
1464 res = bluetooth_set_radiosw(t);
1466 return (res) ? res : count;
1469 static struct device_attribute dev_attr_bluetooth_enable =
1470 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1471 bluetooth_enable_show, bluetooth_enable_store);
1473 /* --------------------------------------------------------------------- */
1475 static struct attribute *bluetooth_attributes[] = {
1476 &dev_attr_bluetooth_enable.attr,
1477 NULL
1480 static const struct attribute_group bluetooth_attr_group = {
1481 .attrs = bluetooth_attributes,
1484 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1486 int res;
1487 int status = 0;
1489 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1491 IBM_ACPIHANDLE_INIT(hkey);
1493 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1494 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1495 tp_features.bluetooth = hkey_handle &&
1496 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1498 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1499 str_supported(tp_features.bluetooth),
1500 status);
1502 if (tp_features.bluetooth) {
1503 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1504 /* no bluetooth hardware present in system */
1505 tp_features.bluetooth = 0;
1506 dbg_printk(TPACPI_DBG_INIT,
1507 "bluetooth hardware not installed\n");
1508 } else {
1509 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1510 &bluetooth_attr_group);
1511 if (res)
1512 return res;
1516 return (tp_features.bluetooth)? 0 : 1;
1519 static void bluetooth_exit(void)
1521 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1522 &bluetooth_attr_group);
1525 static int bluetooth_get_radiosw(void)
1527 int status;
1529 if (!tp_features.bluetooth)
1530 return -ENODEV;
1532 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1533 return -EIO;
1535 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1538 static int bluetooth_set_radiosw(int radio_on)
1540 int status;
1542 if (!tp_features.bluetooth)
1543 return -ENODEV;
1545 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1546 return -EIO;
1547 if (radio_on)
1548 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1549 else
1550 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1551 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1552 return -EIO;
1554 return 0;
1557 /* procfs -------------------------------------------------------------- */
1558 static int bluetooth_read(char *p)
1560 int len = 0;
1561 int status = bluetooth_get_radiosw();
1563 if (!tp_features.bluetooth)
1564 len += sprintf(p + len, "status:\t\tnot supported\n");
1565 else {
1566 len += sprintf(p + len, "status:\t\t%s\n",
1567 (status)? "enabled" : "disabled");
1568 len += sprintf(p + len, "commands:\tenable, disable\n");
1571 return len;
1574 static int bluetooth_write(char *buf)
1576 char *cmd;
1578 if (!tp_features.bluetooth)
1579 return -ENODEV;
1581 while ((cmd = next_cmd(&buf))) {
1582 if (strlencmp(cmd, "enable") == 0) {
1583 bluetooth_set_radiosw(1);
1584 } else if (strlencmp(cmd, "disable") == 0) {
1585 bluetooth_set_radiosw(0);
1586 } else
1587 return -EINVAL;
1590 return 0;
1593 static struct ibm_struct bluetooth_driver_data = {
1594 .name = "bluetooth",
1595 .read = bluetooth_read,
1596 .write = bluetooth_write,
1597 .exit = bluetooth_exit,
1600 /*************************************************************************
1601 * Wan subdriver
1604 /* sysfs wan enable ---------------------------------------------------- */
1605 static ssize_t wan_enable_show(struct device *dev,
1606 struct device_attribute *attr,
1607 char *buf)
1609 int status;
1611 status = wan_get_radiosw();
1612 if (status < 0)
1613 return status;
1615 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1618 static ssize_t wan_enable_store(struct device *dev,
1619 struct device_attribute *attr,
1620 const char *buf, size_t count)
1622 unsigned long t;
1623 int res;
1625 if (parse_strtoul(buf, 1, &t))
1626 return -EINVAL;
1628 res = wan_set_radiosw(t);
1630 return (res) ? res : count;
1633 static struct device_attribute dev_attr_wan_enable =
1634 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1635 wan_enable_show, wan_enable_store);
1637 /* --------------------------------------------------------------------- */
1639 static struct attribute *wan_attributes[] = {
1640 &dev_attr_wan_enable.attr,
1641 NULL
1644 static const struct attribute_group wan_attr_group = {
1645 .attrs = wan_attributes,
1648 static int __init wan_init(struct ibm_init_struct *iibm)
1650 int res;
1651 int status = 0;
1653 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1655 IBM_ACPIHANDLE_INIT(hkey);
1657 tp_features.wan = hkey_handle &&
1658 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1660 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1661 str_supported(tp_features.wan),
1662 status);
1664 if (tp_features.wan) {
1665 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1666 /* no wan hardware present in system */
1667 tp_features.wan = 0;
1668 dbg_printk(TPACPI_DBG_INIT,
1669 "wan hardware not installed\n");
1670 } else {
1671 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1672 &wan_attr_group);
1673 if (res)
1674 return res;
1678 return (tp_features.wan)? 0 : 1;
1681 static void wan_exit(void)
1683 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1684 &wan_attr_group);
1687 static int wan_get_radiosw(void)
1689 int status;
1691 if (!tp_features.wan)
1692 return -ENODEV;
1694 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1695 return -EIO;
1697 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1700 static int wan_set_radiosw(int radio_on)
1702 int status;
1704 if (!tp_features.wan)
1705 return -ENODEV;
1707 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1708 return -EIO;
1709 if (radio_on)
1710 status |= TP_ACPI_WANCARD_RADIOSSW;
1711 else
1712 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1713 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1714 return -EIO;
1716 return 0;
1719 /* procfs -------------------------------------------------------------- */
1720 static int wan_read(char *p)
1722 int len = 0;
1723 int status = wan_get_radiosw();
1725 if (!tp_features.wan)
1726 len += sprintf(p + len, "status:\t\tnot supported\n");
1727 else {
1728 len += sprintf(p + len, "status:\t\t%s\n",
1729 (status)? "enabled" : "disabled");
1730 len += sprintf(p + len, "commands:\tenable, disable\n");
1733 return len;
1736 static int wan_write(char *buf)
1738 char *cmd;
1740 if (!tp_features.wan)
1741 return -ENODEV;
1743 while ((cmd = next_cmd(&buf))) {
1744 if (strlencmp(cmd, "enable") == 0) {
1745 wan_set_radiosw(1);
1746 } else if (strlencmp(cmd, "disable") == 0) {
1747 wan_set_radiosw(0);
1748 } else
1749 return -EINVAL;
1752 return 0;
1755 static struct ibm_struct wan_driver_data = {
1756 .name = "wan",
1757 .read = wan_read,
1758 .write = wan_write,
1759 .exit = wan_exit,
1760 .flags.experimental = 1,
1763 /*************************************************************************
1764 * Video subdriver
1767 static enum video_access_mode video_supported;
1768 static int video_orig_autosw;
1770 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1771 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1772 "\\_SB.PCI0.VID0", /* 770e */
1773 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1774 "\\_SB.PCI0.AGP.VID", /* all others */
1775 ); /* R30, R31 */
1777 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1779 static int __init video_init(struct ibm_init_struct *iibm)
1781 int ivga;
1783 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1785 IBM_ACPIHANDLE_INIT(vid);
1786 IBM_ACPIHANDLE_INIT(vid2);
1788 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1789 /* G41, assume IVGA doesn't change */
1790 vid_handle = vid2_handle;
1792 if (!vid_handle)
1793 /* video switching not supported on R30, R31 */
1794 video_supported = TPACPI_VIDEO_NONE;
1795 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1796 /* 570 */
1797 video_supported = TPACPI_VIDEO_570;
1798 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1799 /* 600e/x, 770e, 770x */
1800 video_supported = TPACPI_VIDEO_770;
1801 else
1802 /* all others */
1803 video_supported = TPACPI_VIDEO_NEW;
1805 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1806 str_supported(video_supported != TPACPI_VIDEO_NONE),
1807 video_supported);
1809 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1812 static void video_exit(void)
1814 dbg_printk(TPACPI_DBG_EXIT,
1815 "restoring original video autoswitch mode\n");
1816 if (video_autosw_set(video_orig_autosw))
1817 printk(IBM_ERR "error while trying to restore original "
1818 "video autoswitch mode\n");
1821 static int video_outputsw_get(void)
1823 int status = 0;
1824 int i;
1826 switch (video_supported) {
1827 case TPACPI_VIDEO_570:
1828 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1829 TP_ACPI_VIDEO_570_PHSCMD))
1830 return -EIO;
1831 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1832 break;
1833 case TPACPI_VIDEO_770:
1834 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1835 return -EIO;
1836 if (i)
1837 status |= TP_ACPI_VIDEO_S_LCD;
1838 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1839 return -EIO;
1840 if (i)
1841 status |= TP_ACPI_VIDEO_S_CRT;
1842 break;
1843 case TPACPI_VIDEO_NEW:
1844 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1845 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1846 return -EIO;
1847 if (i)
1848 status |= TP_ACPI_VIDEO_S_CRT;
1850 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1851 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1852 return -EIO;
1853 if (i)
1854 status |= TP_ACPI_VIDEO_S_LCD;
1855 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1856 return -EIO;
1857 if (i)
1858 status |= TP_ACPI_VIDEO_S_DVI;
1859 break;
1860 default:
1861 return -ENOSYS;
1864 return status;
1867 static int video_outputsw_set(int status)
1869 int autosw;
1870 int res = 0;
1872 switch (video_supported) {
1873 case TPACPI_VIDEO_570:
1874 res = acpi_evalf(NULL, NULL,
1875 "\\_SB.PHS2", "vdd",
1876 TP_ACPI_VIDEO_570_PHS2CMD,
1877 status | TP_ACPI_VIDEO_570_PHS2SET);
1878 break;
1879 case TPACPI_VIDEO_770:
1880 autosw = video_autosw_get();
1881 if (autosw < 0)
1882 return autosw;
1884 res = video_autosw_set(1);
1885 if (res)
1886 return res;
1887 res = acpi_evalf(vid_handle, NULL,
1888 "ASWT", "vdd", status * 0x100, 0);
1889 if (!autosw && video_autosw_set(autosw)) {
1890 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1891 return -EIO;
1893 break;
1894 case TPACPI_VIDEO_NEW:
1895 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1896 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1897 break;
1898 default:
1899 return -ENOSYS;
1902 return (res)? 0 : -EIO;
1905 static int video_autosw_get(void)
1907 int autosw = 0;
1909 switch (video_supported) {
1910 case TPACPI_VIDEO_570:
1911 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1912 return -EIO;
1913 break;
1914 case TPACPI_VIDEO_770:
1915 case TPACPI_VIDEO_NEW:
1916 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1917 return -EIO;
1918 break;
1919 default:
1920 return -ENOSYS;
1923 return autosw & 1;
1926 static int video_autosw_set(int enable)
1928 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1929 return -EIO;
1930 return 0;
1933 static int video_outputsw_cycle(void)
1935 int autosw = video_autosw_get();
1936 int res;
1938 if (autosw < 0)
1939 return autosw;
1941 switch (video_supported) {
1942 case TPACPI_VIDEO_570:
1943 res = video_autosw_set(1);
1944 if (res)
1945 return res;
1946 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1947 break;
1948 case TPACPI_VIDEO_770:
1949 case TPACPI_VIDEO_NEW:
1950 res = video_autosw_set(1);
1951 if (res)
1952 return res;
1953 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1954 break;
1955 default:
1956 return -ENOSYS;
1958 if (!autosw && video_autosw_set(autosw)) {
1959 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1960 return -EIO;
1963 return (res)? 0 : -EIO;
1966 static int video_expand_toggle(void)
1968 switch (video_supported) {
1969 case TPACPI_VIDEO_570:
1970 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1971 0 : -EIO;
1972 case TPACPI_VIDEO_770:
1973 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1974 0 : -EIO;
1975 case TPACPI_VIDEO_NEW:
1976 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1977 0 : -EIO;
1978 default:
1979 return -ENOSYS;
1981 /* not reached */
1984 static int video_read(char *p)
1986 int status, autosw;
1987 int len = 0;
1989 if (video_supported == TPACPI_VIDEO_NONE) {
1990 len += sprintf(p + len, "status:\t\tnot supported\n");
1991 return len;
1994 status = video_outputsw_get();
1995 if (status < 0)
1996 return status;
1998 autosw = video_autosw_get();
1999 if (autosw < 0)
2000 return autosw;
2002 len += sprintf(p + len, "status:\t\tsupported\n");
2003 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2004 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2005 if (video_supported == TPACPI_VIDEO_NEW)
2006 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2007 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2008 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2009 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2010 if (video_supported == TPACPI_VIDEO_NEW)
2011 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2012 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2013 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2015 return len;
2018 static int video_write(char *buf)
2020 char *cmd;
2021 int enable, disable, status;
2022 int res;
2024 if (video_supported == TPACPI_VIDEO_NONE)
2025 return -ENODEV;
2027 enable = 0;
2028 disable = 0;
2030 while ((cmd = next_cmd(&buf))) {
2031 if (strlencmp(cmd, "lcd_enable") == 0) {
2032 enable |= TP_ACPI_VIDEO_S_LCD;
2033 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2034 disable |= TP_ACPI_VIDEO_S_LCD;
2035 } else if (strlencmp(cmd, "crt_enable") == 0) {
2036 enable |= TP_ACPI_VIDEO_S_CRT;
2037 } else if (strlencmp(cmd, "crt_disable") == 0) {
2038 disable |= TP_ACPI_VIDEO_S_CRT;
2039 } else if (video_supported == TPACPI_VIDEO_NEW &&
2040 strlencmp(cmd, "dvi_enable") == 0) {
2041 enable |= TP_ACPI_VIDEO_S_DVI;
2042 } else if (video_supported == TPACPI_VIDEO_NEW &&
2043 strlencmp(cmd, "dvi_disable") == 0) {
2044 disable |= TP_ACPI_VIDEO_S_DVI;
2045 } else if (strlencmp(cmd, "auto_enable") == 0) {
2046 res = video_autosw_set(1);
2047 if (res)
2048 return res;
2049 } else if (strlencmp(cmd, "auto_disable") == 0) {
2050 res = video_autosw_set(0);
2051 if (res)
2052 return res;
2053 } else if (strlencmp(cmd, "video_switch") == 0) {
2054 res = video_outputsw_cycle();
2055 if (res)
2056 return res;
2057 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2058 res = video_expand_toggle();
2059 if (res)
2060 return res;
2061 } else
2062 return -EINVAL;
2065 if (enable || disable) {
2066 status = video_outputsw_get();
2067 if (status < 0)
2068 return status;
2069 res = video_outputsw_set((status & ~disable) | enable);
2070 if (res)
2071 return res;
2074 return 0;
2077 static struct ibm_struct video_driver_data = {
2078 .name = "video",
2079 .read = video_read,
2080 .write = video_write,
2081 .exit = video_exit,
2084 /*************************************************************************
2085 * Light (thinklight) subdriver
2088 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2089 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2091 static int __init light_init(struct ibm_init_struct *iibm)
2093 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2095 IBM_ACPIHANDLE_INIT(ledb);
2096 IBM_ACPIHANDLE_INIT(lght);
2097 IBM_ACPIHANDLE_INIT(cmos);
2099 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2100 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2102 if (tp_features.light)
2103 /* light status not supported on
2104 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2105 tp_features.light_status =
2106 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2108 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2109 str_supported(tp_features.light));
2111 return (tp_features.light)? 0 : 1;
2114 static int light_read(char *p)
2116 int len = 0;
2117 int status = 0;
2119 if (!tp_features.light) {
2120 len += sprintf(p + len, "status:\t\tnot supported\n");
2121 } else if (!tp_features.light_status) {
2122 len += sprintf(p + len, "status:\t\tunknown\n");
2123 len += sprintf(p + len, "commands:\ton, off\n");
2124 } else {
2125 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2126 return -EIO;
2127 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2128 len += sprintf(p + len, "commands:\ton, off\n");
2131 return len;
2134 static int light_write(char *buf)
2136 int cmos_cmd, lght_cmd;
2137 char *cmd;
2138 int success;
2140 if (!tp_features.light)
2141 return -ENODEV;
2143 while ((cmd = next_cmd(&buf))) {
2144 if (strlencmp(cmd, "on") == 0) {
2145 cmos_cmd = 0x0c;
2146 lght_cmd = 1;
2147 } else if (strlencmp(cmd, "off") == 0) {
2148 cmos_cmd = 0x0d;
2149 lght_cmd = 0;
2150 } else
2151 return -EINVAL;
2153 success = cmos_handle ?
2154 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2155 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2156 if (!success)
2157 return -EIO;
2160 return 0;
2163 static struct ibm_struct light_driver_data = {
2164 .name = "light",
2165 .read = light_read,
2166 .write = light_write,
2169 /*************************************************************************
2170 * Dock subdriver
2173 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2175 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2176 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2177 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2178 "\\_SB.PCI.ISA.SLCE", /* 570 */
2179 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2181 /* don't list other alternatives as we install a notify handler on the 570 */
2182 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2184 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2186 .notify = dock_notify,
2187 .handle = &dock_handle,
2188 .type = ACPI_SYSTEM_NOTIFY,
2191 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2192 * We just use it to get notifications of dock hotplug
2193 * in very old thinkpads */
2194 .hid = PCI_ROOT_HID_STRING,
2195 .notify = dock_notify,
2196 .handle = &pci_handle,
2197 .type = ACPI_SYSTEM_NOTIFY,
2201 static struct ibm_struct dock_driver_data[2] = {
2203 .name = "dock",
2204 .read = dock_read,
2205 .write = dock_write,
2206 .acpi = &ibm_dock_acpidriver[0],
2209 .name = "dock",
2210 .acpi = &ibm_dock_acpidriver[1],
2214 #define dock_docked() (_sta(dock_handle) & 1)
2216 static int __init dock_init(struct ibm_init_struct *iibm)
2218 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2220 IBM_ACPIHANDLE_INIT(dock);
2222 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2223 str_supported(dock_handle != NULL));
2225 return (dock_handle)? 0 : 1;
2228 static int __init dock_init2(struct ibm_init_struct *iibm)
2230 int dock2_needed;
2232 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2234 if (dock_driver_data[0].flags.acpi_driver_registered &&
2235 dock_driver_data[0].flags.acpi_notify_installed) {
2236 IBM_ACPIHANDLE_INIT(pci);
2237 dock2_needed = (pci_handle != NULL);
2238 vdbg_printk(TPACPI_DBG_INIT,
2239 "dock PCI handler for the TP 570 is %s\n",
2240 str_supported(dock2_needed));
2241 } else {
2242 vdbg_printk(TPACPI_DBG_INIT,
2243 "dock subdriver part 2 not required\n");
2244 dock2_needed = 0;
2247 return (dock2_needed)? 0 : 1;
2250 static void dock_notify(struct ibm_struct *ibm, u32 event)
2252 int docked = dock_docked();
2253 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2255 if (event == 1 && !pci) /* 570 */
2256 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2257 else if (event == 1 && pci) /* 570 */
2258 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2259 else if (event == 3 && docked)
2260 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2261 else if (event == 3 && !docked)
2262 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2263 else if (event == 0 && docked)
2264 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2265 else {
2266 printk(IBM_ERR "unknown dock event %d, status %d\n",
2267 event, _sta(dock_handle));
2268 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2272 static int dock_read(char *p)
2274 int len = 0;
2275 int docked = dock_docked();
2277 if (!dock_handle)
2278 len += sprintf(p + len, "status:\t\tnot supported\n");
2279 else if (!docked)
2280 len += sprintf(p + len, "status:\t\tundocked\n");
2281 else {
2282 len += sprintf(p + len, "status:\t\tdocked\n");
2283 len += sprintf(p + len, "commands:\tdock, undock\n");
2286 return len;
2289 static int dock_write(char *buf)
2291 char *cmd;
2293 if (!dock_docked())
2294 return -ENODEV;
2296 while ((cmd = next_cmd(&buf))) {
2297 if (strlencmp(cmd, "undock") == 0) {
2298 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2299 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2300 return -EIO;
2301 } else if (strlencmp(cmd, "dock") == 0) {
2302 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2303 return -EIO;
2304 } else
2305 return -EINVAL;
2308 return 0;
2311 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2313 /*************************************************************************
2314 * Bay subdriver
2317 #ifdef CONFIG_THINKPAD_ACPI_BAY
2318 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2319 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2320 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2321 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2322 ); /* A21e, R30, R31 */
2323 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2324 "_EJ0", /* all others */
2325 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2326 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2327 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2328 ); /* all others */
2329 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2330 "_EJ0", /* 770x */
2331 ); /* all others */
2333 static int __init bay_init(struct ibm_init_struct *iibm)
2335 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2337 IBM_ACPIHANDLE_INIT(bay);
2338 if (bay_handle)
2339 IBM_ACPIHANDLE_INIT(bay_ej);
2340 IBM_ACPIHANDLE_INIT(bay2);
2341 if (bay2_handle)
2342 IBM_ACPIHANDLE_INIT(bay2_ej);
2344 tp_features.bay_status = bay_handle &&
2345 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2346 tp_features.bay_status2 = bay2_handle &&
2347 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2349 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2350 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2351 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2352 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2354 vdbg_printk(TPACPI_DBG_INIT,
2355 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2356 str_supported(tp_features.bay_status),
2357 str_supported(tp_features.bay_eject),
2358 str_supported(tp_features.bay_status2),
2359 str_supported(tp_features.bay_eject2));
2361 return (tp_features.bay_status || tp_features.bay_eject ||
2362 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2365 static void bay_notify(struct ibm_struct *ibm, u32 event)
2367 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2370 #define bay_occupied(b) (_sta(b##_handle) & 1)
2372 static int bay_read(char *p)
2374 int len = 0;
2375 int occupied = bay_occupied(bay);
2376 int occupied2 = bay_occupied(bay2);
2377 int eject, eject2;
2379 len += sprintf(p + len, "status:\t\t%s\n",
2380 tp_features.bay_status ?
2381 (occupied ? "occupied" : "unoccupied") :
2382 "not supported");
2383 if (tp_features.bay_status2)
2384 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2385 "occupied" : "unoccupied");
2387 eject = tp_features.bay_eject && occupied;
2388 eject2 = tp_features.bay_eject2 && occupied2;
2390 if (eject && eject2)
2391 len += sprintf(p + len, "commands:\teject, eject2\n");
2392 else if (eject)
2393 len += sprintf(p + len, "commands:\teject\n");
2394 else if (eject2)
2395 len += sprintf(p + len, "commands:\teject2\n");
2397 return len;
2400 static int bay_write(char *buf)
2402 char *cmd;
2404 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2405 return -ENODEV;
2407 while ((cmd = next_cmd(&buf))) {
2408 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2409 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2410 return -EIO;
2411 } else if (tp_features.bay_eject2 &&
2412 strlencmp(cmd, "eject2") == 0) {
2413 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2414 return -EIO;
2415 } else
2416 return -EINVAL;
2419 return 0;
2422 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2423 .notify = bay_notify,
2424 .handle = &bay_handle,
2425 .type = ACPI_SYSTEM_NOTIFY,
2428 static struct ibm_struct bay_driver_data = {
2429 .name = "bay",
2430 .read = bay_read,
2431 .write = bay_write,
2432 .acpi = &ibm_bay_acpidriver,
2435 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2437 /*************************************************************************
2438 * CMOS subdriver
2441 /* sysfs cmos_command -------------------------------------------------- */
2442 static ssize_t cmos_command_store(struct device *dev,
2443 struct device_attribute *attr,
2444 const char *buf, size_t count)
2446 unsigned long cmos_cmd;
2447 int res;
2449 if (parse_strtoul(buf, 21, &cmos_cmd))
2450 return -EINVAL;
2452 res = issue_thinkpad_cmos_command(cmos_cmd);
2453 return (res)? res : count;
2456 static struct device_attribute dev_attr_cmos_command =
2457 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2459 /* --------------------------------------------------------------------- */
2461 static int __init cmos_init(struct ibm_init_struct *iibm)
2463 int res;
2465 vdbg_printk(TPACPI_DBG_INIT,
2466 "initializing cmos commands subdriver\n");
2468 IBM_ACPIHANDLE_INIT(cmos);
2470 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2471 str_supported(cmos_handle != NULL));
2473 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2474 if (res)
2475 return res;
2477 return (cmos_handle)? 0 : 1;
2480 static void cmos_exit(void)
2482 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2485 static int cmos_read(char *p)
2487 int len = 0;
2489 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2490 R30, R31, T20-22, X20-21 */
2491 if (!cmos_handle)
2492 len += sprintf(p + len, "status:\t\tnot supported\n");
2493 else {
2494 len += sprintf(p + len, "status:\t\tsupported\n");
2495 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2498 return len;
2501 static int cmos_write(char *buf)
2503 char *cmd;
2504 int cmos_cmd, res;
2506 while ((cmd = next_cmd(&buf))) {
2507 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2508 cmos_cmd >= 0 && cmos_cmd <= 21) {
2509 /* cmos_cmd set */
2510 } else
2511 return -EINVAL;
2513 res = issue_thinkpad_cmos_command(cmos_cmd);
2514 if (res)
2515 return res;
2518 return 0;
2521 static struct ibm_struct cmos_driver_data = {
2522 .name = "cmos",
2523 .read = cmos_read,
2524 .write = cmos_write,
2525 .exit = cmos_exit,
2528 /*************************************************************************
2529 * LED subdriver
2532 static enum led_access_mode led_supported;
2534 IBM_HANDLE(led, ec, "SLED", /* 570 */
2535 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2536 "LED", /* all others */
2537 ); /* R30, R31 */
2539 static int __init led_init(struct ibm_init_struct *iibm)
2541 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2543 IBM_ACPIHANDLE_INIT(led);
2545 if (!led_handle)
2546 /* led not supported on R30, R31 */
2547 led_supported = TPACPI_LED_NONE;
2548 else if (strlencmp(led_path, "SLED") == 0)
2549 /* 570 */
2550 led_supported = TPACPI_LED_570;
2551 else if (strlencmp(led_path, "SYSL") == 0)
2552 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2553 led_supported = TPACPI_LED_OLD;
2554 else
2555 /* all others */
2556 led_supported = TPACPI_LED_NEW;
2558 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2559 str_supported(led_supported), led_supported);
2561 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2564 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2566 static int led_read(char *p)
2568 int len = 0;
2570 if (!led_supported) {
2571 len += sprintf(p + len, "status:\t\tnot supported\n");
2572 return len;
2574 len += sprintf(p + len, "status:\t\tsupported\n");
2576 if (led_supported == TPACPI_LED_570) {
2577 /* 570 */
2578 int i, status;
2579 for (i = 0; i < 8; i++) {
2580 if (!acpi_evalf(ec_handle,
2581 &status, "GLED", "dd", 1 << i))
2582 return -EIO;
2583 len += sprintf(p + len, "%d:\t\t%s\n",
2584 i, led_status(status));
2588 len += sprintf(p + len, "commands:\t"
2589 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2591 return len;
2594 /* off, on, blink */
2595 static const int led_sled_arg1[] = { 0, 1, 3 };
2596 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2597 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2598 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2600 static int led_write(char *buf)
2602 char *cmd;
2603 int led, ind, ret;
2605 if (!led_supported)
2606 return -ENODEV;
2608 while ((cmd = next_cmd(&buf))) {
2609 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2610 return -EINVAL;
2612 if (strstr(cmd, "off")) {
2613 ind = 0;
2614 } else if (strstr(cmd, "on")) {
2615 ind = 1;
2616 } else if (strstr(cmd, "blink")) {
2617 ind = 2;
2618 } else
2619 return -EINVAL;
2621 if (led_supported == TPACPI_LED_570) {
2622 /* 570 */
2623 led = 1 << led;
2624 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2625 led, led_sled_arg1[ind]))
2626 return -EIO;
2627 } else if (led_supported == TPACPI_LED_OLD) {
2628 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2629 led = 1 << led;
2630 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2631 if (ret >= 0)
2632 ret =
2633 ec_write(TPACPI_LED_EC_HLBL,
2634 led * led_exp_hlbl[ind]);
2635 if (ret >= 0)
2636 ret =
2637 ec_write(TPACPI_LED_EC_HLCL,
2638 led * led_exp_hlcl[ind]);
2639 if (ret < 0)
2640 return ret;
2641 } else {
2642 /* all others */
2643 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2644 led, led_led_arg1[ind]))
2645 return -EIO;
2649 return 0;
2652 static struct ibm_struct led_driver_data = {
2653 .name = "led",
2654 .read = led_read,
2655 .write = led_write,
2658 /*************************************************************************
2659 * Beep subdriver
2662 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2664 static int __init beep_init(struct ibm_init_struct *iibm)
2666 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2668 IBM_ACPIHANDLE_INIT(beep);
2670 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2671 str_supported(beep_handle != NULL));
2673 return (beep_handle)? 0 : 1;
2676 static int beep_read(char *p)
2678 int len = 0;
2680 if (!beep_handle)
2681 len += sprintf(p + len, "status:\t\tnot supported\n");
2682 else {
2683 len += sprintf(p + len, "status:\t\tsupported\n");
2684 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2687 return len;
2690 static int beep_write(char *buf)
2692 char *cmd;
2693 int beep_cmd;
2695 if (!beep_handle)
2696 return -ENODEV;
2698 while ((cmd = next_cmd(&buf))) {
2699 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2700 beep_cmd >= 0 && beep_cmd <= 17) {
2701 /* beep_cmd set */
2702 } else
2703 return -EINVAL;
2704 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2705 return -EIO;
2708 return 0;
2711 static struct ibm_struct beep_driver_data = {
2712 .name = "beep",
2713 .read = beep_read,
2714 .write = beep_write,
2717 /*************************************************************************
2718 * Thermal subdriver
2721 static enum thermal_access_mode thermal_read_mode;
2723 /* sysfs temp##_input -------------------------------------------------- */
2725 static ssize_t thermal_temp_input_show(struct device *dev,
2726 struct device_attribute *attr,
2727 char *buf)
2729 struct sensor_device_attribute *sensor_attr =
2730 to_sensor_dev_attr(attr);
2731 int idx = sensor_attr->index;
2732 s32 value;
2733 int res;
2735 res = thermal_get_sensor(idx, &value);
2736 if (res)
2737 return res;
2738 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2739 return -ENXIO;
2741 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2744 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2745 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2747 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2748 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2749 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2750 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2751 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2752 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2753 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2754 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2755 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2756 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2757 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2758 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2759 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2760 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2761 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2762 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2763 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2766 #define THERMAL_ATTRS(X) \
2767 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2769 static struct attribute *thermal_temp_input_attr[] = {
2770 THERMAL_ATTRS(8),
2771 THERMAL_ATTRS(9),
2772 THERMAL_ATTRS(10),
2773 THERMAL_ATTRS(11),
2774 THERMAL_ATTRS(12),
2775 THERMAL_ATTRS(13),
2776 THERMAL_ATTRS(14),
2777 THERMAL_ATTRS(15),
2778 THERMAL_ATTRS(0),
2779 THERMAL_ATTRS(1),
2780 THERMAL_ATTRS(2),
2781 THERMAL_ATTRS(3),
2782 THERMAL_ATTRS(4),
2783 THERMAL_ATTRS(5),
2784 THERMAL_ATTRS(6),
2785 THERMAL_ATTRS(7),
2786 NULL
2789 static const struct attribute_group thermal_temp_input16_group = {
2790 .attrs = thermal_temp_input_attr
2793 static const struct attribute_group thermal_temp_input8_group = {
2794 .attrs = &thermal_temp_input_attr[8]
2797 #undef THERMAL_SENSOR_ATTR_TEMP
2798 #undef THERMAL_ATTRS
2800 /* --------------------------------------------------------------------- */
2802 static int __init thermal_init(struct ibm_init_struct *iibm)
2804 u8 t, ta1, ta2;
2805 int i;
2806 int acpi_tmp7;
2807 int res;
2809 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2811 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2813 if (thinkpad_id.ec_model) {
2815 * Direct EC access mode: sensors at registers
2816 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2817 * non-implemented, thermal sensors return 0x80 when
2818 * not available
2821 ta1 = ta2 = 0;
2822 for (i = 0; i < 8; i++) {
2823 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2824 ta1 |= t;
2825 } else {
2826 ta1 = 0;
2827 break;
2829 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2830 ta2 |= t;
2831 } else {
2832 ta1 = 0;
2833 break;
2836 if (ta1 == 0) {
2837 /* This is sheer paranoia, but we handle it anyway */
2838 if (acpi_tmp7) {
2839 printk(IBM_ERR
2840 "ThinkPad ACPI EC access misbehaving, "
2841 "falling back to ACPI TMPx access mode\n");
2842 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2843 } else {
2844 printk(IBM_ERR
2845 "ThinkPad ACPI EC access misbehaving, "
2846 "disabling thermal sensors access\n");
2847 thermal_read_mode = TPACPI_THERMAL_NONE;
2849 } else {
2850 thermal_read_mode =
2851 (ta2 != 0) ?
2852 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2854 } else if (acpi_tmp7) {
2855 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2856 /* 600e/x, 770e, 770x */
2857 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2858 } else {
2859 /* Standard ACPI TMPx access, max 8 sensors */
2860 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2862 } else {
2863 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2864 thermal_read_mode = TPACPI_THERMAL_NONE;
2867 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2868 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2869 thermal_read_mode);
2871 switch(thermal_read_mode) {
2872 case TPACPI_THERMAL_TPEC_16:
2873 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2874 &thermal_temp_input16_group);
2875 if (res)
2876 return res;
2877 break;
2878 case TPACPI_THERMAL_TPEC_8:
2879 case TPACPI_THERMAL_ACPI_TMP07:
2880 case TPACPI_THERMAL_ACPI_UPDT:
2881 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2882 &thermal_temp_input8_group);
2883 if (res)
2884 return res;
2885 break;
2886 case TPACPI_THERMAL_NONE:
2887 default:
2888 return 1;
2891 return 0;
2894 static void thermal_exit(void)
2896 switch(thermal_read_mode) {
2897 case TPACPI_THERMAL_TPEC_16:
2898 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2899 &thermal_temp_input16_group);
2900 break;
2901 case TPACPI_THERMAL_TPEC_8:
2902 case TPACPI_THERMAL_ACPI_TMP07:
2903 case TPACPI_THERMAL_ACPI_UPDT:
2904 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2905 &thermal_temp_input16_group);
2906 break;
2907 case TPACPI_THERMAL_NONE:
2908 default:
2909 break;
2913 /* idx is zero-based */
2914 static int thermal_get_sensor(int idx, s32 *value)
2916 int t;
2917 s8 tmp;
2918 char tmpi[5];
2920 t = TP_EC_THERMAL_TMP0;
2922 switch (thermal_read_mode) {
2923 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2924 case TPACPI_THERMAL_TPEC_16:
2925 if (idx >= 8 && idx <= 15) {
2926 t = TP_EC_THERMAL_TMP8;
2927 idx -= 8;
2929 /* fallthrough */
2930 #endif
2931 case TPACPI_THERMAL_TPEC_8:
2932 if (idx <= 7) {
2933 if (!acpi_ec_read(t + idx, &tmp))
2934 return -EIO;
2935 *value = tmp * 1000;
2936 return 0;
2938 break;
2940 case TPACPI_THERMAL_ACPI_UPDT:
2941 if (idx <= 7) {
2942 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2943 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2944 return -EIO;
2945 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2946 return -EIO;
2947 *value = (t - 2732) * 100;
2948 return 0;
2950 break;
2952 case TPACPI_THERMAL_ACPI_TMP07:
2953 if (idx <= 7) {
2954 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2955 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2956 return -EIO;
2957 if (t > 127 || t < -127)
2958 t = TP_EC_THERMAL_TMP_NA;
2959 *value = t * 1000;
2960 return 0;
2962 break;
2964 case TPACPI_THERMAL_NONE:
2965 default:
2966 return -ENOSYS;
2969 return -EINVAL;
2972 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2974 int res, i;
2975 int n;
2977 n = 8;
2978 i = 0;
2980 if (!s)
2981 return -EINVAL;
2983 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2984 n = 16;
2986 for(i = 0 ; i < n; i++) {
2987 res = thermal_get_sensor(i, &s->temp[i]);
2988 if (res)
2989 return res;
2992 return n;
2995 static int thermal_read(char *p)
2997 int len = 0;
2998 int n, i;
2999 struct ibm_thermal_sensors_struct t;
3001 n = thermal_get_sensors(&t);
3002 if (unlikely(n < 0))
3003 return n;
3005 len += sprintf(p + len, "temperatures:\t");
3007 if (n > 0) {
3008 for (i = 0; i < (n - 1); i++)
3009 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3010 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3011 } else
3012 len += sprintf(p + len, "not supported\n");
3014 return len;
3017 static struct ibm_struct thermal_driver_data = {
3018 .name = "thermal",
3019 .read = thermal_read,
3020 .exit = thermal_exit,
3023 /*************************************************************************
3024 * EC Dump subdriver
3027 static u8 ecdump_regs[256];
3029 static int ecdump_read(char *p)
3031 int len = 0;
3032 int i, j;
3033 u8 v;
3035 len += sprintf(p + len, "EC "
3036 " +00 +01 +02 +03 +04 +05 +06 +07"
3037 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3038 for (i = 0; i < 256; i += 16) {
3039 len += sprintf(p + len, "EC 0x%02x:", i);
3040 for (j = 0; j < 16; j++) {
3041 if (!acpi_ec_read(i + j, &v))
3042 break;
3043 if (v != ecdump_regs[i + j])
3044 len += sprintf(p + len, " *%02x", v);
3045 else
3046 len += sprintf(p + len, " %02x", v);
3047 ecdump_regs[i + j] = v;
3049 len += sprintf(p + len, "\n");
3050 if (j != 16)
3051 break;
3054 /* These are way too dangerous to advertise openly... */
3055 #if 0
3056 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3057 " (<offset> is 00-ff, <value> is 00-ff)\n");
3058 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3059 " (<offset> is 00-ff, <value> is 0-255)\n");
3060 #endif
3061 return len;
3064 static int ecdump_write(char *buf)
3066 char *cmd;
3067 int i, v;
3069 while ((cmd = next_cmd(&buf))) {
3070 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3071 /* i and v set */
3072 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3073 /* i and v set */
3074 } else
3075 return -EINVAL;
3076 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3077 if (!acpi_ec_write(i, v))
3078 return -EIO;
3079 } else
3080 return -EINVAL;
3083 return 0;
3086 static struct ibm_struct ecdump_driver_data = {
3087 .name = "ecdump",
3088 .read = ecdump_read,
3089 .write = ecdump_write,
3090 .flags.experimental = 1,
3093 /*************************************************************************
3094 * Backlight/brightness subdriver
3097 static struct backlight_device *ibm_backlight_device;
3099 static struct backlight_ops ibm_backlight_data = {
3100 .get_brightness = brightness_get,
3101 .update_status = brightness_update_status,
3104 static struct mutex brightness_mutex;
3106 static int __init brightness_init(struct ibm_init_struct *iibm)
3108 int b;
3110 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3112 mutex_init(&brightness_mutex);
3114 if (!brightness_mode) {
3115 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3116 brightness_mode = 2;
3117 else
3118 brightness_mode = 3;
3120 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3121 brightness_mode);
3124 if (brightness_mode > 3)
3125 return -EINVAL;
3127 b = brightness_get(NULL);
3128 if (b < 0)
3129 return 1;
3131 ibm_backlight_device = backlight_device_register(
3132 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3133 &ibm_backlight_data);
3134 if (IS_ERR(ibm_backlight_device)) {
3135 printk(IBM_ERR "Could not register backlight device\n");
3136 return PTR_ERR(ibm_backlight_device);
3138 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3140 ibm_backlight_device->props.max_brightness = 7;
3141 ibm_backlight_device->props.brightness = b;
3142 backlight_update_status(ibm_backlight_device);
3144 return 0;
3147 static void brightness_exit(void)
3149 if (ibm_backlight_device) {
3150 vdbg_printk(TPACPI_DBG_EXIT,
3151 "calling backlight_device_unregister()\n");
3152 backlight_device_unregister(ibm_backlight_device);
3153 ibm_backlight_device = NULL;
3157 static int brightness_update_status(struct backlight_device *bd)
3159 return brightness_set(
3160 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3161 bd->props.power == FB_BLANK_UNBLANK) ?
3162 bd->props.brightness : 0);
3166 * ThinkPads can read brightness from two places: EC 0x31, or
3167 * CMOS NVRAM byte 0x5E, bits 0-3.
3169 static int brightness_get(struct backlight_device *bd)
3171 u8 lec = 0, lcmos = 0, level = 0;
3173 if (brightness_mode & 1) {
3174 if (!acpi_ec_read(brightness_offset, &lec))
3175 return -EIO;
3176 lec &= 7;
3177 level = lec;
3179 if (brightness_mode & 2) {
3180 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3181 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3182 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3183 level = lcmos;
3186 if (brightness_mode == 3 && lec != lcmos) {
3187 printk(IBM_ERR
3188 "CMOS NVRAM (%u) and EC (%u) do not agree "
3189 "on display brightness level\n",
3190 (unsigned int) lcmos,
3191 (unsigned int) lec);
3192 return -EIO;
3195 return level;
3198 static int brightness_set(int value)
3200 int cmos_cmd, inc, i, res;
3201 int current_value;
3203 if (value > 7)
3204 return -EINVAL;
3206 res = mutex_lock_interruptible(&brightness_mutex);
3207 if (res < 0)
3208 return res;
3210 current_value = brightness_get(NULL);
3211 if (current_value < 0) {
3212 res = current_value;
3213 goto errout;
3216 cmos_cmd = value > current_value ?
3217 TP_CMOS_BRIGHTNESS_UP :
3218 TP_CMOS_BRIGHTNESS_DOWN;
3219 inc = value > current_value ? 1 : -1;
3221 res = 0;
3222 for (i = current_value; i != value; i += inc) {
3223 if ((brightness_mode & 2) &&
3224 issue_thinkpad_cmos_command(cmos_cmd)) {
3225 res = -EIO;
3226 goto errout;
3228 if ((brightness_mode & 1) &&
3229 !acpi_ec_write(brightness_offset, i + inc)) {
3230 res = -EIO;
3231 goto errout;;
3235 errout:
3236 mutex_unlock(&brightness_mutex);
3237 return res;
3240 static int brightness_read(char *p)
3242 int len = 0;
3243 int level;
3245 if ((level = brightness_get(NULL)) < 0) {
3246 len += sprintf(p + len, "level:\t\tunreadable\n");
3247 } else {
3248 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3249 len += sprintf(p + len, "commands:\tup, down\n");
3250 len += sprintf(p + len, "commands:\tlevel <level>"
3251 " (<level> is 0-7)\n");
3254 return len;
3257 static int brightness_write(char *buf)
3259 int level;
3260 int new_level;
3261 char *cmd;
3263 while ((cmd = next_cmd(&buf))) {
3264 if ((level = brightness_get(NULL)) < 0)
3265 return level;
3266 level &= 7;
3268 if (strlencmp(cmd, "up") == 0) {
3269 new_level = level == 7 ? 7 : level + 1;
3270 } else if (strlencmp(cmd, "down") == 0) {
3271 new_level = level == 0 ? 0 : level - 1;
3272 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3273 new_level >= 0 && new_level <= 7) {
3274 /* new_level set */
3275 } else
3276 return -EINVAL;
3278 brightness_set(new_level);
3281 return 0;
3284 static struct ibm_struct brightness_driver_data = {
3285 .name = "brightness",
3286 .read = brightness_read,
3287 .write = brightness_write,
3288 .exit = brightness_exit,
3291 /*************************************************************************
3292 * Volume subdriver
3295 static int volume_read(char *p)
3297 int len = 0;
3298 u8 level;
3300 if (!acpi_ec_read(volume_offset, &level)) {
3301 len += sprintf(p + len, "level:\t\tunreadable\n");
3302 } else {
3303 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3304 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3305 len += sprintf(p + len, "commands:\tup, down, mute\n");
3306 len += sprintf(p + len, "commands:\tlevel <level>"
3307 " (<level> is 0-15)\n");
3310 return len;
3313 static int volume_write(char *buf)
3315 int cmos_cmd, inc, i;
3316 u8 level, mute;
3317 int new_level, new_mute;
3318 char *cmd;
3320 while ((cmd = next_cmd(&buf))) {
3321 if (!acpi_ec_read(volume_offset, &level))
3322 return -EIO;
3323 new_mute = mute = level & 0x40;
3324 new_level = level = level & 0xf;
3326 if (strlencmp(cmd, "up") == 0) {
3327 if (mute)
3328 new_mute = 0;
3329 else
3330 new_level = level == 15 ? 15 : level + 1;
3331 } else if (strlencmp(cmd, "down") == 0) {
3332 if (mute)
3333 new_mute = 0;
3334 else
3335 new_level = level == 0 ? 0 : level - 1;
3336 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3337 new_level >= 0 && new_level <= 15) {
3338 /* new_level set */
3339 } else if (strlencmp(cmd, "mute") == 0) {
3340 new_mute = 0x40;
3341 } else
3342 return -EINVAL;
3344 if (new_level != level) { /* mute doesn't change */
3345 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3346 inc = new_level > level ? 1 : -1;
3348 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3349 !acpi_ec_write(volume_offset, level)))
3350 return -EIO;
3352 for (i = level; i != new_level; i += inc)
3353 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3354 !acpi_ec_write(volume_offset, i + inc))
3355 return -EIO;
3357 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3358 !acpi_ec_write(volume_offset,
3359 new_level + mute)))
3360 return -EIO;
3363 if (new_mute != mute) { /* level doesn't change */
3364 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3366 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3367 !acpi_ec_write(volume_offset, level + new_mute))
3368 return -EIO;
3372 return 0;
3375 static struct ibm_struct volume_driver_data = {
3376 .name = "volume",
3377 .read = volume_read,
3378 .write = volume_write,
3381 /*************************************************************************
3382 * Fan subdriver
3386 * FAN ACCESS MODES
3388 * TPACPI_FAN_RD_ACPI_GFAN:
3389 * ACPI GFAN method: returns fan level
3391 * see TPACPI_FAN_WR_ACPI_SFAN
3392 * EC 0x2f (HFSP) not available if GFAN exists
3394 * TPACPI_FAN_WR_ACPI_SFAN:
3395 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3397 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3398 * it for writing.
3400 * TPACPI_FAN_WR_TPEC:
3401 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3402 * Supported on almost all ThinkPads
3404 * Fan speed changes of any sort (including those caused by the
3405 * disengaged mode) are usually done slowly by the firmware as the
3406 * maximum ammount of fan duty cycle change per second seems to be
3407 * limited.
3409 * Reading is not available if GFAN exists.
3410 * Writing is not available if SFAN exists.
3412 * Bits
3413 * 7 automatic mode engaged;
3414 * (default operation mode of the ThinkPad)
3415 * fan level is ignored in this mode.
3416 * 6 full speed mode (takes precedence over bit 7);
3417 * not available on all thinkpads. May disable
3418 * the tachometer while the fan controller ramps up
3419 * the speed (which can take up to a few *minutes*).
3420 * Speeds up fan to 100% duty-cycle, which is far above
3421 * the standard RPM levels. It is not impossible that
3422 * it could cause hardware damage.
3423 * 5-3 unused in some models. Extra bits for fan level
3424 * in others, but still useless as all values above
3425 * 7 map to the same speed as level 7 in these models.
3426 * 2-0 fan level (0..7 usually)
3427 * 0x00 = stop
3428 * 0x07 = max (set when temperatures critical)
3429 * Some ThinkPads may have other levels, see
3430 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3432 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3433 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3434 * does so, its initial value is meaningless (0x07).
3436 * For firmware bugs, refer to:
3437 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3439 * ----
3441 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3442 * Main fan tachometer reading (in RPM)
3444 * This register is present on all ThinkPads with a new-style EC, and
3445 * it is known not to be present on the A21m/e, and T22, as there is
3446 * something else in offset 0x84 according to the ACPI DSDT. Other
3447 * ThinkPads from this same time period (and earlier) probably lack the
3448 * tachometer as well.
3450 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3451 * was never fixed by IBM to report the EC firmware version string
3452 * probably support the tachometer (like the early X models), so
3453 * detecting it is quite hard. We need more data to know for sure.
3455 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3456 * might result.
3458 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3459 * mode.
3461 * For firmware bugs, refer to:
3462 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3464 * TPACPI_FAN_WR_ACPI_FANS:
3465 * ThinkPad X31, X40, X41. Not available in the X60.
3467 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3468 * high speed. ACPI DSDT seems to map these three speeds to levels
3469 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3470 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3472 * The speeds are stored on handles
3473 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3475 * There are three default speed sets, acessible as handles:
3476 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3478 * ACPI DSDT switches which set is in use depending on various
3479 * factors.
3481 * TPACPI_FAN_WR_TPEC is also available and should be used to
3482 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3483 * but the ACPI tables just mention level 7.
3486 static enum fan_status_access_mode fan_status_access_mode;
3487 static enum fan_control_access_mode fan_control_access_mode;
3488 static enum fan_control_commands fan_control_commands;
3490 static u8 fan_control_initial_status;
3491 static u8 fan_control_desired_level;
3493 static void fan_watchdog_fire(struct work_struct *ignored);
3494 static int fan_watchdog_maxinterval;
3495 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3497 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3498 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3499 "\\FSPD", /* 600e/x, 770e, 770x */
3500 ); /* all others */
3501 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3502 "JFNS", /* 770x-JL */
3503 ); /* all others */
3506 * SYSFS fan layout: hwmon compatible (device)
3508 * pwm*_enable:
3509 * 0: "disengaged" mode
3510 * 1: manual mode
3511 * 2: native EC "auto" mode (recommended, hardware default)
3513 * pwm*: set speed in manual mode, ignored otherwise.
3514 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3515 * interpolation.
3517 * fan*_input: tachometer reading, RPM
3520 * SYSFS fan layout: extensions
3522 * fan_watchdog (driver):
3523 * fan watchdog interval in seconds, 0 disables (default), max 120
3526 /* sysfs fan pwm1_enable ----------------------------------------------- */
3527 static ssize_t fan_pwm1_enable_show(struct device *dev,
3528 struct device_attribute *attr,
3529 char *buf)
3531 int res, mode;
3532 u8 status;
3534 res = fan_get_status_safe(&status);
3535 if (res)
3536 return res;
3538 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3539 if (status != fan_control_initial_status) {
3540 tp_features.fan_ctrl_status_undef = 0;
3541 } else {
3542 /* Return most likely status. In fact, it
3543 * might be the only possible status */
3544 status = TP_EC_FAN_AUTO;
3548 if (status & TP_EC_FAN_FULLSPEED) {
3549 mode = 0;
3550 } else if (status & TP_EC_FAN_AUTO) {
3551 mode = 2;
3552 } else
3553 mode = 1;
3555 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3558 static ssize_t fan_pwm1_enable_store(struct device *dev,
3559 struct device_attribute *attr,
3560 const char *buf, size_t count)
3562 unsigned long t;
3563 int res, level;
3565 if (parse_strtoul(buf, 2, &t))
3566 return -EINVAL;
3568 switch (t) {
3569 case 0:
3570 level = TP_EC_FAN_FULLSPEED;
3571 break;
3572 case 1:
3573 level = TPACPI_FAN_LAST_LEVEL;
3574 break;
3575 case 2:
3576 level = TP_EC_FAN_AUTO;
3577 break;
3578 case 3:
3579 /* reserved for software-controlled auto mode */
3580 return -ENOSYS;
3581 default:
3582 return -EINVAL;
3585 res = fan_set_level_safe(level);
3586 if (res == -ENXIO)
3587 return -EINVAL;
3588 else if (res < 0)
3589 return res;
3591 fan_watchdog_reset();
3593 return count;
3596 static struct device_attribute dev_attr_fan_pwm1_enable =
3597 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3598 fan_pwm1_enable_show, fan_pwm1_enable_store);
3600 /* sysfs fan pwm1 ------------------------------------------------------ */
3601 static ssize_t fan_pwm1_show(struct device *dev,
3602 struct device_attribute *attr,
3603 char *buf)
3605 int res;
3606 u8 status;
3608 res = fan_get_status_safe(&status);
3609 if (res)
3610 return res;
3612 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3613 if (status != fan_control_initial_status) {
3614 tp_features.fan_ctrl_status_undef = 0;
3615 } else {
3616 status = TP_EC_FAN_AUTO;
3620 if ((status &
3621 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3622 status = fan_control_desired_level;
3624 if (status > 7)
3625 status = 7;
3627 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3630 static ssize_t fan_pwm1_store(struct device *dev,
3631 struct device_attribute *attr,
3632 const char *buf, size_t count)
3634 unsigned long s;
3635 int rc;
3636 u8 status, newlevel;
3638 if (parse_strtoul(buf, 255, &s))
3639 return -EINVAL;
3641 /* scale down from 0-255 to 0-7 */
3642 newlevel = (s >> 5) & 0x07;
3644 rc = mutex_lock_interruptible(&fan_mutex);
3645 if (rc < 0)
3646 return rc;
3648 rc = fan_get_status(&status);
3649 if (!rc && (status &
3650 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3651 rc = fan_set_level(newlevel);
3652 if (rc == -ENXIO)
3653 rc = -EINVAL;
3654 else if (!rc) {
3655 fan_update_desired_level(newlevel);
3656 fan_watchdog_reset();
3660 mutex_unlock(&fan_mutex);
3661 return (rc)? rc : count;
3664 static struct device_attribute dev_attr_fan_pwm1 =
3665 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3666 fan_pwm1_show, fan_pwm1_store);
3668 /* sysfs fan fan1_input ------------------------------------------------ */
3669 static ssize_t fan_fan1_input_show(struct device *dev,
3670 struct device_attribute *attr,
3671 char *buf)
3673 int res;
3674 unsigned int speed;
3676 res = fan_get_speed(&speed);
3677 if (res < 0)
3678 return res;
3680 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3683 static struct device_attribute dev_attr_fan_fan1_input =
3684 __ATTR(fan1_input, S_IRUGO,
3685 fan_fan1_input_show, NULL);
3687 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3688 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3689 char *buf)
3691 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3694 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3695 const char *buf, size_t count)
3697 unsigned long t;
3699 if (parse_strtoul(buf, 120, &t))
3700 return -EINVAL;
3702 if (!fan_control_allowed)
3703 return -EPERM;
3705 fan_watchdog_maxinterval = t;
3706 fan_watchdog_reset();
3708 return count;
3711 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3712 fan_fan_watchdog_show, fan_fan_watchdog_store);
3714 /* --------------------------------------------------------------------- */
3715 static struct attribute *fan_attributes[] = {
3716 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3717 &dev_attr_fan_fan1_input.attr,
3718 NULL
3721 static const struct attribute_group fan_attr_group = {
3722 .attrs = fan_attributes,
3725 static int __init fan_init(struct ibm_init_struct *iibm)
3727 int rc;
3729 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3731 mutex_init(&fan_mutex);
3732 fan_status_access_mode = TPACPI_FAN_NONE;
3733 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3734 fan_control_commands = 0;
3735 fan_watchdog_maxinterval = 0;
3736 tp_features.fan_ctrl_status_undef = 0;
3737 fan_control_desired_level = 7;
3739 IBM_ACPIHANDLE_INIT(fans);
3740 IBM_ACPIHANDLE_INIT(gfan);
3741 IBM_ACPIHANDLE_INIT(sfan);
3743 if (gfan_handle) {
3744 /* 570, 600e/x, 770e, 770x */
3745 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3746 } else {
3747 /* all other ThinkPads: note that even old-style
3748 * ThinkPad ECs supports the fan control register */
3749 if (likely(acpi_ec_read(fan_status_offset,
3750 &fan_control_initial_status))) {
3751 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3753 /* In some ThinkPads, neither the EC nor the ACPI
3754 * DSDT initialize the fan status, and it ends up
3755 * being set to 0x07 when it *could* be either
3756 * 0x07 or 0x80.
3758 * Enable for TP-1Y (T43), TP-78 (R51e),
3759 * TP-76 (R52), TP-70 (T43, R52), which are known
3760 * to be buggy. */
3761 if (fan_control_initial_status == 0x07) {
3762 switch (thinkpad_id.ec_model) {
3763 case 0x5931: /* TP-1Y */
3764 case 0x3837: /* TP-78 */
3765 case 0x3637: /* TP-76 */
3766 case 0x3037: /* TP-70 */
3767 printk(IBM_NOTICE
3768 "fan_init: initial fan status is "
3769 "unknown, assuming it is in auto "
3770 "mode\n");
3771 tp_features.fan_ctrl_status_undef = 1;
3775 } else {
3776 printk(IBM_ERR
3777 "ThinkPad ACPI EC access misbehaving, "
3778 "fan status and control unavailable\n");
3779 return 1;
3783 if (sfan_handle) {
3784 /* 570, 770x-JL */
3785 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3786 fan_control_commands |=
3787 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3788 } else {
3789 if (!gfan_handle) {
3790 /* gfan without sfan means no fan control */
3791 /* all other models implement TP EC 0x2f control */
3793 if (fans_handle) {
3794 /* X31, X40, X41 */
3795 fan_control_access_mode =
3796 TPACPI_FAN_WR_ACPI_FANS;
3797 fan_control_commands |=
3798 TPACPI_FAN_CMD_SPEED |
3799 TPACPI_FAN_CMD_LEVEL |
3800 TPACPI_FAN_CMD_ENABLE;
3801 } else {
3802 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3803 fan_control_commands |=
3804 TPACPI_FAN_CMD_LEVEL |
3805 TPACPI_FAN_CMD_ENABLE;
3810 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3811 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3812 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3813 fan_status_access_mode, fan_control_access_mode);
3815 /* fan control master switch */
3816 if (!fan_control_allowed) {
3817 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3818 fan_control_commands = 0;
3819 dbg_printk(TPACPI_DBG_INIT,
3820 "fan control features disabled by parameter\n");
3823 /* update fan_control_desired_level */
3824 if (fan_status_access_mode != TPACPI_FAN_NONE)
3825 fan_get_status_safe(NULL);
3827 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3828 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3829 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3830 &fan_attr_group);
3831 if (!(rc < 0))
3832 rc = driver_create_file(&tpacpi_pdriver.driver,
3833 &driver_attr_fan_watchdog);
3834 if (rc < 0)
3835 return rc;
3836 return 0;
3837 } else
3838 return 1;
3842 * Call with fan_mutex held
3844 static void fan_update_desired_level(u8 status)
3846 if ((status &
3847 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3848 if (status > 7)
3849 fan_control_desired_level = 7;
3850 else
3851 fan_control_desired_level = status;
3855 static int fan_get_status(u8 *status)
3857 u8 s;
3859 /* TODO:
3860 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3862 switch (fan_status_access_mode) {
3863 case TPACPI_FAN_RD_ACPI_GFAN:
3864 /* 570, 600e/x, 770e, 770x */
3866 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3867 return -EIO;
3869 if (likely(status))
3870 *status = s & 0x07;
3872 break;
3874 case TPACPI_FAN_RD_TPEC:
3875 /* all except 570, 600e/x, 770e, 770x */
3876 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3877 return -EIO;
3879 if (likely(status))
3880 *status = s;
3882 break;
3884 default:
3885 return -ENXIO;
3888 return 0;
3891 static int fan_get_status_safe(u8 *status)
3893 int rc;
3894 u8 s;
3896 rc = mutex_lock_interruptible(&fan_mutex);
3897 if (rc < 0)
3898 return rc;
3899 rc = fan_get_status(&s);
3900 if (!rc)
3901 fan_update_desired_level(s);
3902 mutex_unlock(&fan_mutex);
3904 if (status)
3905 *status = s;
3907 return rc;
3910 static void fan_exit(void)
3912 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3914 /* FIXME: can we really do this unconditionally? */
3915 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3916 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3918 cancel_delayed_work(&fan_watchdog_task);
3919 flush_scheduled_work();
3922 static int fan_get_speed(unsigned int *speed)
3924 u8 hi, lo;
3926 switch (fan_status_access_mode) {
3927 case TPACPI_FAN_RD_TPEC:
3928 /* all except 570, 600e/x, 770e, 770x */
3929 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3930 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3931 return -EIO;
3933 if (likely(speed))
3934 *speed = (hi << 8) | lo;
3936 break;
3938 default:
3939 return -ENXIO;
3942 return 0;
3945 static void fan_watchdog_fire(struct work_struct *ignored)
3947 int rc;
3949 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
3950 return;
3952 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3953 rc = fan_set_enable();
3954 if (rc < 0) {
3955 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3956 "will try again later...\n", -rc);
3957 /* reschedule for later */
3958 fan_watchdog_reset();
3962 static void fan_watchdog_reset(void)
3964 static int fan_watchdog_active;
3966 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3967 return;
3969 if (fan_watchdog_active)
3970 cancel_delayed_work(&fan_watchdog_task);
3972 if (fan_watchdog_maxinterval > 0 &&
3973 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
3974 fan_watchdog_active = 1;
3975 if (!schedule_delayed_work(&fan_watchdog_task,
3976 msecs_to_jiffies(fan_watchdog_maxinterval
3977 * 1000))) {
3978 printk(IBM_ERR "failed to schedule the fan watchdog, "
3979 "watchdog will not trigger\n");
3981 } else
3982 fan_watchdog_active = 0;
3985 static int fan_set_level(int level)
3987 if (!fan_control_allowed)
3988 return -EPERM;
3990 switch (fan_control_access_mode) {
3991 case TPACPI_FAN_WR_ACPI_SFAN:
3992 if (level >= 0 && level <= 7) {
3993 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3994 return -EIO;
3995 } else
3996 return -EINVAL;
3997 break;
3999 case TPACPI_FAN_WR_ACPI_FANS:
4000 case TPACPI_FAN_WR_TPEC:
4001 if ((level != TP_EC_FAN_AUTO) &&
4002 (level != TP_EC_FAN_FULLSPEED) &&
4003 ((level < 0) || (level > 7)))
4004 return -EINVAL;
4006 /* safety net should the EC not support AUTO
4007 * or FULLSPEED mode bits and just ignore them */
4008 if (level & TP_EC_FAN_FULLSPEED)
4009 level |= 7; /* safety min speed 7 */
4010 else if (level & TP_EC_FAN_FULLSPEED)
4011 level |= 4; /* safety min speed 4 */
4013 if (!acpi_ec_write(fan_status_offset, level))
4014 return -EIO;
4015 else
4016 tp_features.fan_ctrl_status_undef = 0;
4017 break;
4019 default:
4020 return -ENXIO;
4022 return 0;
4025 static int fan_set_level_safe(int level)
4027 int rc;
4029 if (!fan_control_allowed)
4030 return -EPERM;
4032 rc = mutex_lock_interruptible(&fan_mutex);
4033 if (rc < 0)
4034 return rc;
4036 if (level == TPACPI_FAN_LAST_LEVEL)
4037 level = fan_control_desired_level;
4039 rc = fan_set_level(level);
4040 if (!rc)
4041 fan_update_desired_level(level);
4043 mutex_unlock(&fan_mutex);
4044 return rc;
4047 static int fan_set_enable(void)
4049 u8 s;
4050 int rc;
4052 if (!fan_control_allowed)
4053 return -EPERM;
4055 rc = mutex_lock_interruptible(&fan_mutex);
4056 if (rc < 0)
4057 return rc;
4059 switch (fan_control_access_mode) {
4060 case TPACPI_FAN_WR_ACPI_FANS:
4061 case TPACPI_FAN_WR_TPEC:
4062 rc = fan_get_status(&s);
4063 if (rc < 0)
4064 break;
4066 /* Don't go out of emergency fan mode */
4067 if (s != 7) {
4068 s &= 0x07;
4069 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4072 if (!acpi_ec_write(fan_status_offset, s))
4073 rc = -EIO;
4074 else {
4075 tp_features.fan_ctrl_status_undef = 0;
4076 rc = 0;
4078 break;
4080 case TPACPI_FAN_WR_ACPI_SFAN:
4081 rc = fan_get_status(&s);
4082 if (rc < 0)
4083 break;
4085 s &= 0x07;
4087 /* Set fan to at least level 4 */
4088 s |= 4;
4090 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4091 rc= -EIO;
4092 else
4093 rc = 0;
4094 break;
4096 default:
4097 rc = -ENXIO;
4100 mutex_unlock(&fan_mutex);
4101 return rc;
4104 static int fan_set_disable(void)
4106 int rc;
4108 if (!fan_control_allowed)
4109 return -EPERM;
4111 rc = mutex_lock_interruptible(&fan_mutex);
4112 if (rc < 0)
4113 return rc;
4115 rc = 0;
4116 switch (fan_control_access_mode) {
4117 case TPACPI_FAN_WR_ACPI_FANS:
4118 case TPACPI_FAN_WR_TPEC:
4119 if (!acpi_ec_write(fan_status_offset, 0x00))
4120 rc = -EIO;
4121 else {
4122 fan_control_desired_level = 0;
4123 tp_features.fan_ctrl_status_undef = 0;
4125 break;
4127 case TPACPI_FAN_WR_ACPI_SFAN:
4128 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4129 rc = -EIO;
4130 else
4131 fan_control_desired_level = 0;
4132 break;
4134 default:
4135 rc = -ENXIO;
4139 mutex_unlock(&fan_mutex);
4140 return rc;
4143 static int fan_set_speed(int speed)
4145 int rc;
4147 if (!fan_control_allowed)
4148 return -EPERM;
4150 rc = mutex_lock_interruptible(&fan_mutex);
4151 if (rc < 0)
4152 return rc;
4154 rc = 0;
4155 switch (fan_control_access_mode) {
4156 case TPACPI_FAN_WR_ACPI_FANS:
4157 if (speed >= 0 && speed <= 65535) {
4158 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4159 speed, speed, speed))
4160 rc = -EIO;
4161 } else
4162 rc = -EINVAL;
4163 break;
4165 default:
4166 rc = -ENXIO;
4169 mutex_unlock(&fan_mutex);
4170 return rc;
4173 static int fan_read(char *p)
4175 int len = 0;
4176 int rc;
4177 u8 status;
4178 unsigned int speed = 0;
4180 switch (fan_status_access_mode) {
4181 case TPACPI_FAN_RD_ACPI_GFAN:
4182 /* 570, 600e/x, 770e, 770x */
4183 if ((rc = fan_get_status_safe(&status)) < 0)
4184 return rc;
4186 len += sprintf(p + len, "status:\t\t%s\n"
4187 "level:\t\t%d\n",
4188 (status != 0) ? "enabled" : "disabled", status);
4189 break;
4191 case TPACPI_FAN_RD_TPEC:
4192 /* all except 570, 600e/x, 770e, 770x */
4193 if ((rc = fan_get_status_safe(&status)) < 0)
4194 return rc;
4196 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4197 if (status != fan_control_initial_status)
4198 tp_features.fan_ctrl_status_undef = 0;
4199 else
4200 /* Return most likely status. In fact, it
4201 * might be the only possible status */
4202 status = TP_EC_FAN_AUTO;
4205 len += sprintf(p + len, "status:\t\t%s\n",
4206 (status != 0) ? "enabled" : "disabled");
4208 if ((rc = fan_get_speed(&speed)) < 0)
4209 return rc;
4211 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4213 if (status & TP_EC_FAN_FULLSPEED)
4214 /* Disengaged mode takes precedence */
4215 len += sprintf(p + len, "level:\t\tdisengaged\n");
4216 else if (status & TP_EC_FAN_AUTO)
4217 len += sprintf(p + len, "level:\t\tauto\n");
4218 else
4219 len += sprintf(p + len, "level:\t\t%d\n", status);
4220 break;
4222 case TPACPI_FAN_NONE:
4223 default:
4224 len += sprintf(p + len, "status:\t\tnot supported\n");
4227 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4228 len += sprintf(p + len, "commands:\tlevel <level>");
4230 switch (fan_control_access_mode) {
4231 case TPACPI_FAN_WR_ACPI_SFAN:
4232 len += sprintf(p + len, " (<level> is 0-7)\n");
4233 break;
4235 default:
4236 len += sprintf(p + len, " (<level> is 0-7, "
4237 "auto, disengaged, full-speed)\n");
4238 break;
4242 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4243 len += sprintf(p + len, "commands:\tenable, disable\n"
4244 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4245 "1-120 (seconds))\n");
4247 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4248 len += sprintf(p + len, "commands:\tspeed <speed>"
4249 " (<speed> is 0-65535)\n");
4251 return len;
4254 static int fan_write_cmd_level(const char *cmd, int *rc)
4256 int level;
4258 if (strlencmp(cmd, "level auto") == 0)
4259 level = TP_EC_FAN_AUTO;
4260 else if ((strlencmp(cmd, "level disengaged") == 0) |
4261 (strlencmp(cmd, "level full-speed") == 0))
4262 level = TP_EC_FAN_FULLSPEED;
4263 else if (sscanf(cmd, "level %d", &level) != 1)
4264 return 0;
4266 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4267 printk(IBM_ERR "level command accepted for unsupported "
4268 "access mode %d", fan_control_access_mode);
4270 return 1;
4273 static int fan_write_cmd_enable(const char *cmd, int *rc)
4275 if (strlencmp(cmd, "enable") != 0)
4276 return 0;
4278 if ((*rc = fan_set_enable()) == -ENXIO)
4279 printk(IBM_ERR "enable command accepted for unsupported "
4280 "access mode %d", fan_control_access_mode);
4282 return 1;
4285 static int fan_write_cmd_disable(const char *cmd, int *rc)
4287 if (strlencmp(cmd, "disable") != 0)
4288 return 0;
4290 if ((*rc = fan_set_disable()) == -ENXIO)
4291 printk(IBM_ERR "disable command accepted for unsupported "
4292 "access mode %d", fan_control_access_mode);
4294 return 1;
4297 static int fan_write_cmd_speed(const char *cmd, int *rc)
4299 int speed;
4301 /* TODO:
4302 * Support speed <low> <medium> <high> ? */
4304 if (sscanf(cmd, "speed %d", &speed) != 1)
4305 return 0;
4307 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4308 printk(IBM_ERR "speed command accepted for unsupported "
4309 "access mode %d", fan_control_access_mode);
4311 return 1;
4314 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4316 int interval;
4318 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4319 return 0;
4321 if (interval < 0 || interval > 120)
4322 *rc = -EINVAL;
4323 else
4324 fan_watchdog_maxinterval = interval;
4326 return 1;
4329 static int fan_write(char *buf)
4331 char *cmd;
4332 int rc = 0;
4334 while (!rc && (cmd = next_cmd(&buf))) {
4335 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4336 fan_write_cmd_level(cmd, &rc)) &&
4337 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4338 (fan_write_cmd_enable(cmd, &rc) ||
4339 fan_write_cmd_disable(cmd, &rc) ||
4340 fan_write_cmd_watchdog(cmd, &rc))) &&
4341 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4342 fan_write_cmd_speed(cmd, &rc))
4344 rc = -EINVAL;
4345 else if (!rc)
4346 fan_watchdog_reset();
4349 return rc;
4352 static struct ibm_struct fan_driver_data = {
4353 .name = "fan",
4354 .read = fan_read,
4355 .write = fan_write,
4356 .exit = fan_exit,
4359 /****************************************************************************
4360 ****************************************************************************
4362 * Infrastructure
4364 ****************************************************************************
4365 ****************************************************************************/
4367 /* /proc support */
4368 static struct proc_dir_entry *proc_dir;
4370 /* Subdriver registry */
4371 static LIST_HEAD(tpacpi_all_drivers);
4375 * Module and infrastructure proble, init and exit handling
4378 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4379 static const char * __init str_supported(int is_supported)
4381 static char text_unsupported[] __initdata = "not supported";
4383 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4385 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4387 static int __init ibm_init(struct ibm_init_struct *iibm)
4389 int ret;
4390 struct ibm_struct *ibm = iibm->data;
4391 struct proc_dir_entry *entry;
4393 BUG_ON(ibm == NULL);
4395 INIT_LIST_HEAD(&ibm->all_drivers);
4397 if (ibm->flags.experimental && !experimental)
4398 return 0;
4400 dbg_printk(TPACPI_DBG_INIT,
4401 "probing for %s\n", ibm->name);
4403 if (iibm->init) {
4404 ret = iibm->init(iibm);
4405 if (ret > 0)
4406 return 0; /* probe failed */
4407 if (ret)
4408 return ret;
4410 ibm->flags.init_called = 1;
4413 if (ibm->acpi) {
4414 if (ibm->acpi->hid) {
4415 ret = register_tpacpi_subdriver(ibm);
4416 if (ret)
4417 goto err_out;
4420 if (ibm->acpi->notify) {
4421 ret = setup_acpi_notify(ibm);
4422 if (ret == -ENODEV) {
4423 printk(IBM_NOTICE "disabling subdriver %s\n",
4424 ibm->name);
4425 ret = 0;
4426 goto err_out;
4428 if (ret < 0)
4429 goto err_out;
4433 dbg_printk(TPACPI_DBG_INIT,
4434 "%s installed\n", ibm->name);
4436 if (ibm->read) {
4437 entry = create_proc_entry(ibm->name,
4438 S_IFREG | S_IRUGO | S_IWUSR,
4439 proc_dir);
4440 if (!entry) {
4441 printk(IBM_ERR "unable to create proc entry %s\n",
4442 ibm->name);
4443 ret = -ENODEV;
4444 goto err_out;
4446 entry->owner = THIS_MODULE;
4447 entry->data = ibm;
4448 entry->read_proc = &dispatch_procfs_read;
4449 if (ibm->write)
4450 entry->write_proc = &dispatch_procfs_write;
4451 ibm->flags.proc_created = 1;
4454 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4456 return 0;
4458 err_out:
4459 dbg_printk(TPACPI_DBG_INIT,
4460 "%s: at error exit path with result %d\n",
4461 ibm->name, ret);
4463 ibm_exit(ibm);
4464 return (ret < 0)? ret : 0;
4467 static void ibm_exit(struct ibm_struct *ibm)
4469 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4471 list_del_init(&ibm->all_drivers);
4473 if (ibm->flags.acpi_notify_installed) {
4474 dbg_printk(TPACPI_DBG_EXIT,
4475 "%s: acpi_remove_notify_handler\n", ibm->name);
4476 BUG_ON(!ibm->acpi);
4477 acpi_remove_notify_handler(*ibm->acpi->handle,
4478 ibm->acpi->type,
4479 dispatch_acpi_notify);
4480 ibm->flags.acpi_notify_installed = 0;
4481 ibm->flags.acpi_notify_installed = 0;
4484 if (ibm->flags.proc_created) {
4485 dbg_printk(TPACPI_DBG_EXIT,
4486 "%s: remove_proc_entry\n", ibm->name);
4487 remove_proc_entry(ibm->name, proc_dir);
4488 ibm->flags.proc_created = 0;
4491 if (ibm->flags.acpi_driver_registered) {
4492 dbg_printk(TPACPI_DBG_EXIT,
4493 "%s: acpi_bus_unregister_driver\n", ibm->name);
4494 BUG_ON(!ibm->acpi);
4495 acpi_bus_unregister_driver(ibm->acpi->driver);
4496 kfree(ibm->acpi->driver);
4497 ibm->acpi->driver = NULL;
4498 ibm->flags.acpi_driver_registered = 0;
4501 if (ibm->flags.init_called && ibm->exit) {
4502 ibm->exit();
4503 ibm->flags.init_called = 0;
4506 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4509 /* Probing */
4511 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4513 struct dmi_device *dev = NULL;
4514 char ec_fw_string[18];
4516 if (!tp)
4517 return;
4519 memset(tp, 0, sizeof(*tp));
4521 if (dmi_name_in_vendors("IBM"))
4522 tp->vendor = PCI_VENDOR_ID_IBM;
4523 else if (dmi_name_in_vendors("LENOVO"))
4524 tp->vendor = PCI_VENDOR_ID_LENOVO;
4525 else
4526 return;
4528 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4529 GFP_KERNEL);
4530 if (!tp->bios_version_str)
4531 return;
4532 tp->bios_model = tp->bios_version_str[0]
4533 | (tp->bios_version_str[1] << 8);
4536 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4537 * X32 or newer, all Z series; Some models must have an
4538 * up-to-date BIOS or they will not be detected.
4540 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4542 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4543 if (sscanf(dev->name,
4544 "IBM ThinkPad Embedded Controller -[%17c",
4545 ec_fw_string) == 1) {
4546 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4547 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4549 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4550 tp->ec_model = ec_fw_string[0]
4551 | (ec_fw_string[1] << 8);
4552 break;
4556 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4557 GFP_KERNEL);
4558 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4559 kfree(tp->model_str);
4560 tp->model_str = NULL;
4564 static int __init probe_for_thinkpad(void)
4566 int is_thinkpad;
4568 if (acpi_disabled)
4569 return -ENODEV;
4572 * Non-ancient models have better DMI tagging, but very old models
4573 * don't.
4575 is_thinkpad = (thinkpad_id.model_str != NULL);
4577 /* ec is required because many other handles are relative to it */
4578 IBM_ACPIHANDLE_INIT(ec);
4579 if (!ec_handle) {
4580 if (is_thinkpad)
4581 printk(IBM_ERR
4582 "Not yet supported ThinkPad detected!\n");
4583 return -ENODEV;
4587 * Risks a regression on very old machines, but reduces potential
4588 * false positives a damn great deal
4590 if (!is_thinkpad)
4591 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4593 if (!is_thinkpad && !force_load)
4594 return -ENODEV;
4596 return 0;
4600 /* Module init, exit, parameters */
4602 static struct ibm_init_struct ibms_init[] __initdata = {
4604 .init = thinkpad_acpi_driver_init,
4605 .data = &thinkpad_acpi_driver_data,
4608 .init = hotkey_init,
4609 .data = &hotkey_driver_data,
4612 .init = bluetooth_init,
4613 .data = &bluetooth_driver_data,
4616 .init = wan_init,
4617 .data = &wan_driver_data,
4620 .init = video_init,
4621 .data = &video_driver_data,
4624 .init = light_init,
4625 .data = &light_driver_data,
4627 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4629 .init = dock_init,
4630 .data = &dock_driver_data[0],
4633 .init = dock_init2,
4634 .data = &dock_driver_data[1],
4636 #endif
4637 #ifdef CONFIG_THINKPAD_ACPI_BAY
4639 .init = bay_init,
4640 .data = &bay_driver_data,
4642 #endif
4644 .init = cmos_init,
4645 .data = &cmos_driver_data,
4648 .init = led_init,
4649 .data = &led_driver_data,
4652 .init = beep_init,
4653 .data = &beep_driver_data,
4656 .init = thermal_init,
4657 .data = &thermal_driver_data,
4660 .data = &ecdump_driver_data,
4663 .init = brightness_init,
4664 .data = &brightness_driver_data,
4667 .data = &volume_driver_data,
4670 .init = fan_init,
4671 .data = &fan_driver_data,
4675 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4677 unsigned int i;
4678 struct ibm_struct *ibm;
4680 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4681 ibm = ibms_init[i].data;
4682 BUG_ON(ibm == NULL);
4684 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4685 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4686 return -ENOSPC;
4687 strcpy(ibms_init[i].param, val);
4688 strcat(ibms_init[i].param, ",");
4689 return 0;
4693 return -EINVAL;
4696 static int experimental;
4697 module_param(experimental, int, 0);
4699 static u32 dbg_level;
4700 module_param_named(debug, dbg_level, uint, 0);
4702 static int force_load;
4703 module_param(force_load, bool, 0);
4705 static int fan_control_allowed;
4706 module_param_named(fan_control, fan_control_allowed, bool, 0);
4708 static int brightness_mode;
4709 module_param_named(brightness_mode, brightness_mode, int, 0);
4711 static unsigned int hotkey_report_mode;
4712 module_param(hotkey_report_mode, uint, 0);
4714 #define IBM_PARAM(feature) \
4715 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4717 IBM_PARAM(hotkey);
4718 IBM_PARAM(bluetooth);
4719 IBM_PARAM(video);
4720 IBM_PARAM(light);
4721 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4722 IBM_PARAM(dock);
4723 #endif
4724 #ifdef CONFIG_THINKPAD_ACPI_BAY
4725 IBM_PARAM(bay);
4726 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4727 IBM_PARAM(cmos);
4728 IBM_PARAM(led);
4729 IBM_PARAM(beep);
4730 IBM_PARAM(ecdump);
4731 IBM_PARAM(brightness);
4732 IBM_PARAM(volume);
4733 IBM_PARAM(fan);
4735 static int __init thinkpad_acpi_module_init(void)
4737 int ret, i;
4739 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4741 /* Parameter checking */
4742 if (hotkey_report_mode > 2)
4743 return -EINVAL;
4745 /* Driver-level probe */
4747 get_thinkpad_model_data(&thinkpad_id);
4748 ret = probe_for_thinkpad();
4749 if (ret) {
4750 thinkpad_acpi_module_exit();
4751 return ret;
4754 /* Driver initialization */
4756 IBM_ACPIHANDLE_INIT(ecrd);
4757 IBM_ACPIHANDLE_INIT(ecwr);
4759 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4760 if (!proc_dir) {
4761 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4762 thinkpad_acpi_module_exit();
4763 return -ENODEV;
4765 proc_dir->owner = THIS_MODULE;
4767 ret = platform_driver_register(&tpacpi_pdriver);
4768 if (ret) {
4769 printk(IBM_ERR "unable to register platform driver\n");
4770 thinkpad_acpi_module_exit();
4771 return ret;
4773 tp_features.platform_drv_registered = 1;
4775 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4776 if (ret) {
4777 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4778 thinkpad_acpi_module_exit();
4779 return ret;
4781 tp_features.platform_drv_attrs_registered = 1;
4784 /* Device initialization */
4785 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4786 NULL, 0);
4787 if (IS_ERR(tpacpi_pdev)) {
4788 ret = PTR_ERR(tpacpi_pdev);
4789 tpacpi_pdev = NULL;
4790 printk(IBM_ERR "unable to register platform device\n");
4791 thinkpad_acpi_module_exit();
4792 return ret;
4794 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4795 if (IS_ERR(tpacpi_hwmon)) {
4796 ret = PTR_ERR(tpacpi_hwmon);
4797 tpacpi_hwmon = NULL;
4798 printk(IBM_ERR "unable to register hwmon device\n");
4799 thinkpad_acpi_module_exit();
4800 return ret;
4802 mutex_init(&tpacpi_inputdev_send_mutex);
4803 tpacpi_inputdev = input_allocate_device();
4804 if (!tpacpi_inputdev) {
4805 printk(IBM_ERR "unable to allocate input device\n");
4806 thinkpad_acpi_module_exit();
4807 return -ENOMEM;
4808 } else {
4809 /* Prepare input device, but don't register */
4810 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4811 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4812 tpacpi_inputdev->id.bustype = BUS_HOST;
4813 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4814 thinkpad_id.vendor :
4815 PCI_VENDOR_ID_IBM;
4816 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4817 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4819 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4820 ret = ibm_init(&ibms_init[i]);
4821 if (ret >= 0 && *ibms_init[i].param)
4822 ret = ibms_init[i].data->write(ibms_init[i].param);
4823 if (ret < 0) {
4824 thinkpad_acpi_module_exit();
4825 return ret;
4828 ret = input_register_device(tpacpi_inputdev);
4829 if (ret < 0) {
4830 printk(IBM_ERR "unable to register input device\n");
4831 thinkpad_acpi_module_exit();
4832 return ret;
4833 } else {
4834 tp_features.input_device_registered = 1;
4837 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
4838 return 0;
4841 static void thinkpad_acpi_module_exit(void)
4843 struct ibm_struct *ibm, *itmp;
4845 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
4847 list_for_each_entry_safe_reverse(ibm, itmp,
4848 &tpacpi_all_drivers,
4849 all_drivers) {
4850 ibm_exit(ibm);
4853 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4855 if (tpacpi_inputdev) {
4856 if (tp_features.input_device_registered)
4857 input_unregister_device(tpacpi_inputdev);
4858 else
4859 input_free_device(tpacpi_inputdev);
4862 if (tpacpi_hwmon)
4863 hwmon_device_unregister(tpacpi_hwmon);
4865 if (tpacpi_pdev)
4866 platform_device_unregister(tpacpi_pdev);
4868 if (tp_features.platform_drv_attrs_registered)
4869 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4871 if (tp_features.platform_drv_registered)
4872 platform_driver_unregister(&tpacpi_pdriver);
4874 if (proc_dir)
4875 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4877 kfree(thinkpad_id.bios_version_str);
4878 kfree(thinkpad_id.ec_version_str);
4879 kfree(thinkpad_id.model_str);
4882 module_init(thinkpad_acpi_module_init);
4883 module_exit(thinkpad_acpi_module_exit);