ACPI: thinkpad-acpi: make EC-based thermal readings non-experimental
[safe/jmp/linux-2.6] / drivers / misc / thinkpad_acpi.c
1 /*
2  *  thinkpad_acpi.c - ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6  *  Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #define IBM_VERSION "0.14"
25 #define TPACPI_SYSFS_VERSION 0x010000
26
27 /*
28  *  Changelog:
29  *  2007-03-27  0.14    renamed to thinkpad_acpi and moved to
30  *                      drivers/misc.
31  *
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.
35  *
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
83  */
84
85 #include "thinkpad_acpi.h"
86
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
91
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
94
95 /*
96  * DMI matching for module autoloading
97  *
98  * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99  * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
100  *
101  * Only models listed in thinkwiki will be supported, so add yours
102  * if it is not there yet.
103  */
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105         MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
106
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
110
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]");
117
118 #define __unused __attribute__ ((unused))
119
120 /****************************************************************************
121  ****************************************************************************
122  *
123  * ACPI Helpers and device model
124  *
125  ****************************************************************************
126  ****************************************************************************/
127
128 /*************************************************************************
129  * ACPI basic handles
130  */
131
132 static acpi_handle root_handle;
133
134 #define IBM_HANDLE(object, parent, paths...)                    \
135         static acpi_handle  object##_handle;                    \
136         static acpi_handle *object##_parent = &parent##_handle; \
137         static char        *object##_path;                      \
138         static char        *object##_paths[] = { paths }
139
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
141            "\\_SB.PCI.ISA.EC",  /* 570 */
142            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
143            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
145            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
146            "\\_SB.PCI0.LPC.EC", /* all others */
147            );
148
149 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
151
152
153 /*************************************************************************
154  * Misc ACPI handles
155  */
156
157 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
158            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159            "\\CMS",             /* R40, R40e */
160            );                   /* all others */
161
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
163            "^HKEY",             /* R30, R31 */
164            "HKEY",              /* all others */
165            );                   /* 570 */
166
167
168 /*************************************************************************
169  * ACPI helpers
170  */
171
172 static int acpi_evalf(acpi_handle handle,
173                       void *res, char *method, char *fmt, ...)
174 {
175         char *fmt0 = fmt;
176         struct acpi_object_list params;
177         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178         struct acpi_buffer result, *resultp;
179         union acpi_object out_obj;
180         acpi_status status;
181         va_list ap;
182         char res_type;
183         int success;
184         int quiet;
185
186         if (!*fmt) {
187                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188                 return 0;
189         }
190
191         if (*fmt == 'q') {
192                 quiet = 1;
193                 fmt++;
194         } else
195                 quiet = 0;
196
197         res_type = *(fmt++);
198
199         params.count = 0;
200         params.pointer = &in_objs[0];
201
202         va_start(ap, fmt);
203         while (*fmt) {
204                 char c = *(fmt++);
205                 switch (c) {
206                 case 'd':       /* int */
207                         in_objs[params.count].integer.value = va_arg(ap, int);
208                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209                         break;
210                         /* add more types as needed */
211                 default:
212                         printk(IBM_ERR "acpi_evalf() called "
213                                "with invalid format character '%c'\n", c);
214                         return 0;
215                 }
216         }
217         va_end(ap);
218
219         if (res_type != 'v') {
220                 result.length = sizeof(out_obj);
221                 result.pointer = &out_obj;
222                 resultp = &result;
223         } else
224                 resultp = NULL;
225
226         status = acpi_evaluate_object(handle, method, &params, resultp);
227
228         switch (res_type) {
229         case 'd':               /* int */
230                 if (res)
231                         *(int *)res = out_obj.integer.value;
232                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233                 break;
234         case 'v':               /* void */
235                 success = status == AE_OK;
236                 break;
237                 /* add more types as needed */
238         default:
239                 printk(IBM_ERR "acpi_evalf() called "
240                        "with invalid format character '%c'\n", res_type);
241                 return 0;
242         }
243
244         if (!success && !quiet)
245                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246                        method, fmt0, status);
247
248         return success;
249 }
250
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
252 {
253         int i;
254
255         if (acpi_evalf(handle, &i, method, "d"))
256                 printk(IBM_INFO "%s = 0x%x\n", method, i);
257         else
258                 printk(IBM_ERR "error calling %s\n", method);
259 }
260
261 static int acpi_ec_read(int i, u8 * p)
262 {
263         int v;
264
265         if (ecrd_handle) {
266                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267                         return 0;
268                 *p = v;
269         } else {
270                 if (ec_read(i, p) < 0)
271                         return 0;
272         }
273
274         return 1;
275 }
276
277 static int acpi_ec_write(int i, u8 v)
278 {
279         if (ecwr_handle) {
280                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281                         return 0;
282         } else {
283                 if (ec_write(i, v) < 0)
284                         return 0;
285         }
286
287         return 1;
288 }
289
290 static int _sta(acpi_handle handle)
291 {
292         int status;
293
294         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295                 status = 0;
296
297         return status;
298 }
299
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
301 {
302         if (!cmos_handle)
303                 return -ENXIO;
304
305         if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306                 return -EIO;
307
308         return 0;
309 }
310
311 /*************************************************************************
312  * ACPI device model
313  */
314
315 static void drv_acpi_handle_init(char *name,
316                            acpi_handle *handle, acpi_handle parent,
317                            char **paths, int num_paths, char **path)
318 {
319         int i;
320         acpi_status status;
321
322         vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323                 name);
324
325         for (i = 0; i < num_paths; i++) {
326                 status = acpi_get_handle(parent, paths[i], handle);
327                 if (ACPI_SUCCESS(status)) {
328                         *path = paths[i];
329                         dbg_printk(TPACPI_DBG_INIT,
330                                    "Found ACPI handle %s for %s\n",
331                                    *path, name);
332                         return;
333                 }
334         }
335
336         vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337                     name);
338         *handle = NULL;
339 }
340
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
342 {
343         struct ibm_struct *ibm = data;
344
345         if (!ibm || !ibm->acpi || !ibm->acpi->notify)
346                 return;
347
348         ibm->acpi->notify(ibm, event);
349 }
350
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
352 {
353         acpi_status status;
354         int rc;
355
356         BUG_ON(!ibm->acpi);
357
358         if (!*ibm->acpi->handle)
359                 return 0;
360
361         vdbg_printk(TPACPI_DBG_INIT,
362                 "setting up ACPI notify for %s\n", ibm->name);
363
364         rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365         if (rc < 0) {
366                 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367                         ibm->name, rc);
368                 return -ENODEV;
369         }
370
371         acpi_driver_data(ibm->acpi->device) = ibm;
372         sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373                 IBM_ACPI_EVENT_PREFIX,
374                 ibm->name);
375
376         status = acpi_install_notify_handler(*ibm->acpi->handle,
377                         ibm->acpi->type, dispatch_acpi_notify, ibm);
378         if (ACPI_FAILURE(status)) {
379                 if (status == AE_ALREADY_EXISTS) {
380                         printk(IBM_NOTICE "another device driver is already handling %s events\n",
381                                 ibm->name);
382                 } else {
383                         printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384                                 ibm->name, status);
385                 }
386                 return -ENODEV;
387         }
388         ibm->flags.acpi_notify_installed = 1;
389         return 0;
390 }
391
392 static int __init tpacpi_device_add(struct acpi_device *device)
393 {
394         return 0;
395 }
396
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
398 {
399         int rc;
400
401         dbg_printk(TPACPI_DBG_INIT,
402                 "registering %s as an ACPI driver\n", ibm->name);
403
404         BUG_ON(!ibm->acpi);
405
406         ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407         if (!ibm->acpi->driver) {
408                 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409                 return -ENOMEM;
410         }
411
412         sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413         ibm->acpi->driver->ids = ibm->acpi->hid;
414         ibm->acpi->driver->ops.add = &tpacpi_device_add;
415
416         rc = acpi_bus_register_driver(ibm->acpi->driver);
417         if (rc < 0) {
418                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
419                        ibm->acpi->hid, rc);
420                 kfree(ibm->acpi->driver);
421                 ibm->acpi->driver = NULL;
422         } else if (!rc)
423                 ibm->flags.acpi_driver_registered = 1;
424
425         return rc;
426 }
427
428
429 /****************************************************************************
430  ****************************************************************************
431  *
432  * Procfs Helpers
433  *
434  ****************************************************************************
435  ****************************************************************************/
436
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438                         int count, int *eof, void *data)
439 {
440         struct ibm_struct *ibm = data;
441         int len;
442
443         if (!ibm || !ibm->read)
444                 return -EINVAL;
445
446         len = ibm->read(page);
447         if (len < 0)
448                 return len;
449
450         if (len <= off + count)
451                 *eof = 1;
452         *start = page + off;
453         len -= off;
454         if (len > count)
455                 len = count;
456         if (len < 0)
457                 len = 0;
458
459         return len;
460 }
461
462 static int dispatch_procfs_write(struct file *file,
463                         const char __user * userbuf,
464                         unsigned long count, void *data)
465 {
466         struct ibm_struct *ibm = data;
467         char *kernbuf;
468         int ret;
469
470         if (!ibm || !ibm->write)
471                 return -EINVAL;
472
473         kernbuf = kmalloc(count + 2, GFP_KERNEL);
474         if (!kernbuf)
475                 return -ENOMEM;
476
477         if (copy_from_user(kernbuf, userbuf, count)) {
478                 kfree(kernbuf);
479                 return -EFAULT;
480         }
481
482         kernbuf[count] = 0;
483         strcat(kernbuf, ",");
484         ret = ibm->write(kernbuf);
485         if (ret == 0)
486                 ret = count;
487
488         kfree(kernbuf);
489
490         return ret;
491 }
492
493 static char *next_cmd(char **cmds)
494 {
495         char *start = *cmds;
496         char *end;
497
498         while ((end = strchr(start, ',')) && end == start)
499                 start = end + 1;
500
501         if (!end)
502                 return NULL;
503
504         *end = 0;
505         *cmds = end + 1;
506         return start;
507 }
508
509
510 /****************************************************************************
511  ****************************************************************************
512  *
513  * Device model: input, hwmon and platform
514  *
515  ****************************************************************************
516  ****************************************************************************/
517
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520 static struct input_dev *tpacpi_inputdev;
521
522
523 static int tpacpi_resume_handler(struct platform_device *pdev)
524 {
525         struct ibm_struct *ibm, *itmp;
526
527         list_for_each_entry_safe(ibm, itmp,
528                                  &tpacpi_all_drivers,
529                                  all_drivers) {
530                 if (ibm->resume)
531                         (ibm->resume)();
532         }
533
534         return 0;
535 }
536
537 static struct platform_driver tpacpi_pdriver = {
538         .driver = {
539                 .name = IBM_DRVR_NAME,
540                 .owner = THIS_MODULE,
541         },
542         .resume = tpacpi_resume_handler,
543 };
544
545
546 /*************************************************************************
547  * thinkpad-acpi driver attributes
548  */
549
550 /* interface_version --------------------------------------------------- */
551 static ssize_t tpacpi_driver_interface_version_show(
552                                 struct device_driver *drv,
553                                 char *buf)
554 {
555         return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
556 }
557
558 static DRIVER_ATTR(interface_version, S_IRUGO,
559                 tpacpi_driver_interface_version_show, NULL);
560
561 /* debug_level --------------------------------------------------------- */
562 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
563                                                 char *buf)
564 {
565         return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
566 }
567
568 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
569                                                 const char *buf, size_t count)
570 {
571         unsigned long t;
572
573         if (parse_strtoul(buf, 0xffff, &t))
574                 return -EINVAL;
575
576         dbg_level = t;
577
578         return count;
579 }
580
581 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
582                 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
583
584 /* version ------------------------------------------------------------- */
585 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
586                                                 char *buf)
587 {
588         return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
589 }
590
591 static DRIVER_ATTR(version, S_IRUGO,
592                 tpacpi_driver_version_show, NULL);
593
594 /* --------------------------------------------------------------------- */
595
596 static struct driver_attribute* tpacpi_driver_attributes[] = {
597         &driver_attr_debug_level, &driver_attr_version,
598         &driver_attr_interface_version,
599 };
600
601 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
602 {
603         int i, res;
604
605         i = 0;
606         res = 0;
607         while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
608                 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
609                 i++;
610         }
611
612         return res;
613 }
614
615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
616 {
617         int i;
618
619         for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
620                 driver_remove_file(drv, tpacpi_driver_attributes[i]);
621 }
622
623 /*************************************************************************
624  * sysfs support helpers
625  */
626
627 struct attribute_set_obj {
628         struct attribute_set s;
629         struct attribute *a;
630 } __attribute__((packed));
631
632 static struct attribute_set *create_attr_set(unsigned int max_members,
633                                                 const char* name)
634 {
635         struct attribute_set_obj *sobj;
636
637         if (max_members == 0)
638                 return NULL;
639
640         /* Allocates space for implicit NULL at the end too */
641         sobj = kzalloc(sizeof(struct attribute_set_obj) +
642                     max_members * sizeof(struct attribute *),
643                     GFP_KERNEL);
644         if (!sobj)
645                 return NULL;
646         sobj->s.max_members = max_members;
647         sobj->s.group.attrs = &sobj->a;
648         sobj->s.group.name = name;
649
650         return &sobj->s;
651 }
652
653 /* not multi-threaded safe, use it in a single thread per set */
654 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
655 {
656         if (!s || !attr)
657                 return -EINVAL;
658
659         if (s->members >= s->max_members)
660                 return -ENOMEM;
661
662         s->group.attrs[s->members] = attr;
663         s->members++;
664
665         return 0;
666 }
667
668 static int add_many_to_attr_set(struct attribute_set* s,
669                         struct attribute **attr,
670                         unsigned int count)
671 {
672         int i, res;
673
674         for (i = 0; i < count; i++) {
675                 res = add_to_attr_set(s, attr[i]);
676                 if (res)
677                         return res;
678         }
679
680         return 0;
681 }
682
683 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
684 {
685         sysfs_remove_group(kobj, &s->group);
686         destroy_attr_set(s);
687 }
688
689 static int parse_strtoul(const char *buf,
690                 unsigned long max, unsigned long *value)
691 {
692         char *endp;
693
694         *value = simple_strtoul(buf, &endp, 0);
695         while (*endp && isspace(*endp))
696                 endp++;
697         if (*endp || *value > max)
698                 return -EINVAL;
699
700         return 0;
701 }
702
703 /****************************************************************************
704  ****************************************************************************
705  *
706  * Subdrivers
707  *
708  ****************************************************************************
709  ****************************************************************************/
710
711 /*************************************************************************
712  * thinkpad-acpi init subdriver
713  */
714
715 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
716 {
717         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
718         printk(IBM_INFO "%s\n", IBM_URL);
719
720         printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
721                 (thinkpad_id.bios_version_str) ?
722                         thinkpad_id.bios_version_str : "unknown",
723                 (thinkpad_id.ec_version_str) ?
724                         thinkpad_id.ec_version_str : "unknown");
725
726         if (thinkpad_id.vendor && thinkpad_id.model_str)
727                 printk(IBM_INFO "%s %s\n",
728                         (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
729                                 "IBM" : ((thinkpad_id.vendor ==
730                                                 PCI_VENDOR_ID_LENOVO) ?
731                                         "Lenovo" : "Unknown vendor"),
732                         thinkpad_id.model_str);
733
734         return 0;
735 }
736
737 static int thinkpad_acpi_driver_read(char *p)
738 {
739         int len = 0;
740
741         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
742         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
743
744         return len;
745 }
746
747 static struct ibm_struct thinkpad_acpi_driver_data = {
748         .name = "driver",
749         .read = thinkpad_acpi_driver_read,
750 };
751
752 /*************************************************************************
753  * Hotkey subdriver
754  */
755
756 static int hotkey_orig_status;
757 static u32 hotkey_orig_mask;
758 static u32 hotkey_all_mask;
759 static u32 hotkey_reserved_mask;
760
761 static u16 *hotkey_keycode_map;
762
763 static struct attribute_set *hotkey_dev_attributes;
764
765 static int hotkey_get_wlsw(int *status)
766 {
767         if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
768                 return -EIO;
769         return 0;
770 }
771
772 /* sysfs hotkey enable ------------------------------------------------- */
773 static ssize_t hotkey_enable_show(struct device *dev,
774                            struct device_attribute *attr,
775                            char *buf)
776 {
777         int res, status;
778         u32 mask;
779
780         res = hotkey_get(&status, &mask);
781         if (res)
782                 return res;
783
784         return snprintf(buf, PAGE_SIZE, "%d\n", status);
785 }
786
787 static ssize_t hotkey_enable_store(struct device *dev,
788                             struct device_attribute *attr,
789                             const char *buf, size_t count)
790 {
791         unsigned long t;
792         int res, status;
793         u32 mask;
794
795         if (parse_strtoul(buf, 1, &t))
796                 return -EINVAL;
797
798         res = hotkey_get(&status, &mask);
799         if (!res)
800                 res = hotkey_set(t, mask);
801
802         return (res) ? res : count;
803 }
804
805 static struct device_attribute dev_attr_hotkey_enable =
806         __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
807                 hotkey_enable_show, hotkey_enable_store);
808
809 /* sysfs hotkey mask --------------------------------------------------- */
810 static ssize_t hotkey_mask_show(struct device *dev,
811                            struct device_attribute *attr,
812                            char *buf)
813 {
814         int res, status;
815         u32 mask;
816
817         res = hotkey_get(&status, &mask);
818         if (res)
819                 return res;
820
821         return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
822 }
823
824 static ssize_t hotkey_mask_store(struct device *dev,
825                             struct device_attribute *attr,
826                             const char *buf, size_t count)
827 {
828         unsigned long t;
829         int res, status;
830         u32 mask;
831
832         if (parse_strtoul(buf, 0xffffffffUL, &t))
833                 return -EINVAL;
834
835         res = hotkey_get(&status, &mask);
836         if (!res)
837                 hotkey_set(status, t);
838
839         return (res) ? res : count;
840 }
841
842 static struct device_attribute dev_attr_hotkey_mask =
843         __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
844                 hotkey_mask_show, hotkey_mask_store);
845
846 /* sysfs hotkey bios_enabled ------------------------------------------- */
847 static ssize_t hotkey_bios_enabled_show(struct device *dev,
848                            struct device_attribute *attr,
849                            char *buf)
850 {
851         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
852 }
853
854 static struct device_attribute dev_attr_hotkey_bios_enabled =
855         __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
856
857 /* sysfs hotkey bios_mask ---------------------------------------------- */
858 static ssize_t hotkey_bios_mask_show(struct device *dev,
859                            struct device_attribute *attr,
860                            char *buf)
861 {
862         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
863 }
864
865 static struct device_attribute dev_attr_hotkey_bios_mask =
866         __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
867
868 /* sysfs hotkey all_mask ----------------------------------------------- */
869 static ssize_t hotkey_all_mask_show(struct device *dev,
870                            struct device_attribute *attr,
871                            char *buf)
872 {
873         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
874 }
875
876 static struct device_attribute dev_attr_hotkey_all_mask =
877         __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
878
879 /* sysfs hotkey recommended_mask --------------------------------------- */
880 static ssize_t hotkey_recommended_mask_show(struct device *dev,
881                                             struct device_attribute *attr,
882                                             char *buf)
883 {
884         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
885                         hotkey_all_mask & ~hotkey_reserved_mask);
886 }
887
888 static struct device_attribute dev_attr_hotkey_recommended_mask =
889         __ATTR(hotkey_recommended_mask, S_IRUGO,
890                 hotkey_recommended_mask_show, NULL);
891
892 /* sysfs hotkey radio_sw ----------------------------------------------- */
893 static ssize_t hotkey_radio_sw_show(struct device *dev,
894                            struct device_attribute *attr,
895                            char *buf)
896 {
897         int res, s;
898         res = hotkey_get_wlsw(&s);
899         if (res < 0)
900                 return res;
901
902         return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
903 }
904
905 static struct device_attribute dev_attr_hotkey_radio_sw =
906         __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
907
908 /* --------------------------------------------------------------------- */
909
910 static struct attribute *hotkey_mask_attributes[] = {
911         &dev_attr_hotkey_mask.attr,
912         &dev_attr_hotkey_bios_enabled.attr,
913         &dev_attr_hotkey_bios_mask.attr,
914         &dev_attr_hotkey_all_mask.attr,
915         &dev_attr_hotkey_recommended_mask.attr,
916 };
917
918 static int __init hotkey_init(struct ibm_init_struct *iibm)
919 {
920
921         static u16 ibm_keycode_map[] __initdata = {
922                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
923                 KEY_FN_F1,      KEY_FN_F2,      KEY_COFFEE,     KEY_SLEEP,
924                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
925                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
926                 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
927                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
928                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
929                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
930                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
931                 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
932                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
933                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
934                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
935                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
936                 KEY_RESERVED,   /* 0x14: VOLUME UP */
937                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
938                 KEY_RESERVED,   /* 0x16: MUTE */
939                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
940                 /* (assignments unknown, please report if found) */
941                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
942                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
943         };
944         static u16 lenovo_keycode_map[] __initdata = {
945                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
946                 KEY_FN_F1,      KEY_COFFEE,     KEY_BATTERY,    KEY_SLEEP,
947                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
948                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
949                 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
950                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
951                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
952                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
953                 KEY_BRIGHTNESSUP,       /* 0x0F: FN+HOME (brightness up) */
954                 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
955                 KEY_BRIGHTNESSDOWN,     /* 0x10: FN+END (brightness down) */
956                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
957                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
958                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
959                 KEY_RESERVED,   /* 0x14: VOLUME UP */
960                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
961                 KEY_RESERVED,   /* 0x16: MUTE */
962                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
963                 /* (assignments unknown, please report if found) */
964                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
965                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
966         };
967
968 #define TPACPI_HOTKEY_MAP_LEN           ARRAY_SIZE(ibm_keycode_map)
969 #define TPACPI_HOTKEY_MAP_SIZE          sizeof(ibm_keycode_map)
970 #define TPACPI_HOTKEY_MAP_TYPESIZE      sizeof(ibm_keycode_map[0])
971
972         int res, i;
973         int status;
974
975         vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
976
977         BUG_ON(!tpacpi_inputdev);
978
979         IBM_ACPIHANDLE_INIT(hkey);
980         mutex_init(&hotkey_mutex);
981
982         /* hotkey not supported on 570 */
983         tp_features.hotkey = hkey_handle != NULL;
984
985         vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
986                 str_supported(tp_features.hotkey));
987
988         if (tp_features.hotkey) {
989                 hotkey_dev_attributes = create_attr_set(7, NULL);
990                 if (!hotkey_dev_attributes)
991                         return -ENOMEM;
992                 res = add_to_attr_set(hotkey_dev_attributes,
993                                 &dev_attr_hotkey_enable.attr);
994                 if (res)
995                         return res;
996
997                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
998                    A30, R30, R31, T20-22, X20-21, X22-24 */
999                 tp_features.hotkey_mask =
1000                         acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
1001
1002                 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1003                         str_supported(tp_features.hotkey_mask));
1004
1005                 if (tp_features.hotkey_mask) {
1006                         /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1007                         if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1008                                         "MHKA", "qd"))
1009                                 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1010                 }
1011
1012                 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1013                 if (!res && tp_features.hotkey_mask) {
1014                         res = add_many_to_attr_set(hotkey_dev_attributes,
1015                                 hotkey_mask_attributes,
1016                                 ARRAY_SIZE(hotkey_mask_attributes));
1017                 }
1018
1019                 /* Not all thinkpads have a hardware radio switch */
1020                 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1021                         tp_features.hotkey_wlsw = 1;
1022                         printk(IBM_INFO
1023                                 "radio switch found; radios are %s\n",
1024                                 enabled(status, 0));
1025                         res = add_to_attr_set(hotkey_dev_attributes,
1026                                         &dev_attr_hotkey_radio_sw.attr);
1027                 }
1028
1029                 if (!res)
1030                         res = register_attr_set_with_sysfs(
1031                                         hotkey_dev_attributes,
1032                                         &tpacpi_pdev->dev.kobj);
1033                 if (res)
1034                         return res;
1035
1036                 /* Set up key map */
1037
1038                 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1039                                                 GFP_KERNEL);
1040                 if (!hotkey_keycode_map) {
1041                         printk(IBM_ERR "failed to allocate memory for key map\n");
1042                         return -ENOMEM;
1043                 }
1044
1045                 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1046                         dbg_printk(TPACPI_DBG_INIT,
1047                                    "using Lenovo default hot key map\n");
1048                         memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1049                                 TPACPI_HOTKEY_MAP_SIZE);
1050                 } else {
1051                         dbg_printk(TPACPI_DBG_INIT,
1052                                    "using IBM default hot key map\n");
1053                         memcpy(hotkey_keycode_map, &ibm_keycode_map,
1054                                 TPACPI_HOTKEY_MAP_SIZE);
1055                 }
1056
1057 #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1058                 for (i = 0; i < 12; i++)
1059                         hotkey_keycode_map[i] = KEY_UNKNOWN;
1060 #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1061
1062                 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1063                 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1064                 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1065                 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1066                 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1067                 tpacpi_inputdev->keycode = hotkey_keycode_map;
1068                 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1069                         if (hotkey_keycode_map[i] != KEY_RESERVED) {
1070                                 set_bit(hotkey_keycode_map[i],
1071                                         tpacpi_inputdev->keybit);
1072                         } else {
1073                                 if (i < sizeof(hotkey_reserved_mask)*8)
1074                                         hotkey_reserved_mask |= 1 << i;
1075                         }
1076                 }
1077
1078                 if (tp_features.hotkey_wlsw) {
1079                         set_bit(EV_SW, tpacpi_inputdev->evbit);
1080                         set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1081                 }
1082
1083 #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1084                 dbg_printk(TPACPI_DBG_INIT,
1085                                 "enabling hot key handling\n");
1086                 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1087                                         | hotkey_orig_mask);
1088                 if (res)
1089                         return res;
1090 #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1091         }
1092
1093         return (tp_features.hotkey)? 0 : 1;
1094 }
1095
1096 static void hotkey_exit(void)
1097 {
1098         int res;
1099
1100         if (tp_features.hotkey) {
1101                 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1102                 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1103                 if (res)
1104                         printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1105         }
1106
1107         if (hotkey_dev_attributes) {
1108                 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1109                 hotkey_dev_attributes = NULL;
1110         }
1111 }
1112
1113 static void tpacpi_input_send_key(unsigned int scancode,
1114                                   unsigned int keycode)
1115 {
1116         if (keycode != KEY_RESERVED) {
1117                 input_report_key(tpacpi_inputdev, keycode, 1);
1118                 if (keycode == KEY_UNKNOWN)
1119                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1120                                     scancode);
1121                 input_sync(tpacpi_inputdev);
1122
1123                 input_report_key(tpacpi_inputdev, keycode, 0);
1124                 if (keycode == KEY_UNKNOWN)
1125                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1126                                     scancode);
1127                 input_sync(tpacpi_inputdev);
1128         }
1129 }
1130
1131 static void tpacpi_input_send_radiosw(void)
1132 {
1133         int wlsw;
1134
1135         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
1136                 input_report_switch(tpacpi_inputdev,
1137                                     SW_RADIO, !!wlsw);
1138 }
1139
1140 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1141 {
1142         u32 hkey;
1143         unsigned int keycode, scancode;
1144         int sendacpi = 1;
1145
1146         if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1147                 if (tpacpi_inputdev->users > 0) {
1148                         switch (hkey >> 12) {
1149                         case 1:
1150                                 /* 0x1000-0x1FFF: key presses */
1151                                 scancode = hkey & 0xfff;
1152                                 if (scancode > 0 && scancode < 0x21) {
1153                                         scancode--;
1154                                         keycode = hotkey_keycode_map[scancode];
1155                                         tpacpi_input_send_key(scancode, keycode);
1156                                         sendacpi = (keycode == KEY_RESERVED
1157                                                 || keycode == KEY_UNKNOWN);
1158                                 } else {
1159                                         printk(IBM_ERR
1160                                                "hotkey 0x%04x out of range for keyboard map\n",
1161                                                hkey);
1162                                 }
1163                                 break;
1164                         case 5:
1165                                 /* 0x5000-0x5FFF: LID */
1166                                 /* we don't handle it through this path, just
1167                                  * eat up known LID events */
1168                                 if (hkey != 0x5001 && hkey != 0x5002) {
1169                                         printk(IBM_ERR
1170                                                 "unknown LID-related hotkey event: 0x%04x\n",
1171                                                 hkey);
1172                                 }
1173                                 break;
1174                         case 7:
1175                                 /* 0x7000-0x7FFF: misc */
1176                                 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1177                                                 tpacpi_input_send_radiosw();
1178                                                 sendacpi = 0;
1179                                         break;
1180                                 }
1181                                 /* fallthrough to default */
1182                         default:
1183                                 /* case 2: dock-related */
1184                                 /*      0x2305 - T43 waking up due to bay lever eject while aslept */
1185                                 /* case 3: ultra-bay related. maybe bay in dock? */
1186                                 /*      0x3003 - T43 after wake up by bay lever eject (0x2305) */
1187                                 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1188                         }
1189                 }
1190
1191                 if (sendacpi)
1192                         acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1193         } else {
1194                 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1195                 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1196         }
1197 }
1198
1199 static void hotkey_resume(void)
1200 {
1201         tpacpi_input_send_radiosw();
1202 }
1203
1204 /*
1205  * Call with hotkey_mutex held
1206  */
1207 static int hotkey_get(int *status, u32 *mask)
1208 {
1209         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1210                 return -EIO;
1211
1212         if (tp_features.hotkey_mask)
1213                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1214                         return -EIO;
1215
1216         return 0;
1217 }
1218
1219 /*
1220  * Call with hotkey_mutex held
1221  */
1222 static int hotkey_set(int status, u32 mask)
1223 {
1224         int i;
1225
1226         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1227                 return -EIO;
1228
1229         if (tp_features.hotkey_mask)
1230                 for (i = 0; i < 32; i++) {
1231                         int bit = ((1 << i) & mask) != 0;
1232                         if (!acpi_evalf(hkey_handle,
1233                                         NULL, "MHKM", "vdd", i + 1, bit))
1234                                 return -EIO;
1235                 }
1236
1237         return 0;
1238 }
1239
1240 /* procfs -------------------------------------------------------------- */
1241 static int hotkey_read(char *p)
1242 {
1243         int res, status;
1244         u32 mask;
1245         int len = 0;
1246
1247         if (!tp_features.hotkey) {
1248                 len += sprintf(p + len, "status:\t\tnot supported\n");
1249                 return len;
1250         }
1251
1252         res = mutex_lock_interruptible(&hotkey_mutex);
1253         if (res < 0)
1254                 return res;
1255         res = hotkey_get(&status, &mask);
1256         mutex_unlock(&hotkey_mutex);
1257         if (res)
1258                 return res;
1259
1260         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1261         if (tp_features.hotkey_mask) {
1262                 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1263                 len += sprintf(p + len,
1264                                "commands:\tenable, disable, reset, <mask>\n");
1265         } else {
1266                 len += sprintf(p + len, "mask:\t\tnot supported\n");
1267                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1268         }
1269
1270         return len;
1271 }
1272
1273 static int hotkey_write(char *buf)
1274 {
1275         int res, status;
1276         u32 mask;
1277         char *cmd;
1278         int do_cmd = 0;
1279
1280         if (!tp_features.hotkey)
1281                 return -ENODEV;
1282
1283         res = mutex_lock_interruptible(&hotkey_mutex);
1284         if (res < 0)
1285                 return res;
1286
1287         res = hotkey_get(&status, &mask);
1288         if (res)
1289                 goto errexit;
1290
1291         res = 0;
1292         while ((cmd = next_cmd(&buf))) {
1293                 if (strlencmp(cmd, "enable") == 0) {
1294                         status = 1;
1295                 } else if (strlencmp(cmd, "disable") == 0) {
1296                         status = 0;
1297                 } else if (strlencmp(cmd, "reset") == 0) {
1298                         status = hotkey_orig_status;
1299                         mask = hotkey_orig_mask;
1300                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1301                         /* mask set */
1302                 } else if (sscanf(cmd, "%x", &mask) == 1) {
1303                         /* mask set */
1304                 } else {
1305                         res = -EINVAL;
1306                         goto errexit;
1307                 }
1308                 do_cmd = 1;
1309         }
1310
1311         if (do_cmd)
1312                 res = hotkey_set(status, mask);
1313
1314 errexit:
1315         mutex_unlock(&hotkey_mutex);
1316         return res;
1317 }
1318
1319 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1320         .hid = IBM_HKEY_HID,
1321         .notify = hotkey_notify,
1322         .handle = &hkey_handle,
1323         .type = ACPI_DEVICE_NOTIFY,
1324 };
1325
1326 static struct ibm_struct hotkey_driver_data = {
1327         .name = "hotkey",
1328         .read = hotkey_read,
1329         .write = hotkey_write,
1330         .exit = hotkey_exit,
1331         .resume = hotkey_resume,
1332         .acpi = &ibm_hotkey_acpidriver,
1333 };
1334
1335 /*************************************************************************
1336  * Bluetooth subdriver
1337  */
1338
1339 /* sysfs bluetooth enable ---------------------------------------------- */
1340 static ssize_t bluetooth_enable_show(struct device *dev,
1341                            struct device_attribute *attr,
1342                            char *buf)
1343 {
1344         int status;
1345
1346         status = bluetooth_get_radiosw();
1347         if (status < 0)
1348                 return status;
1349
1350         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1351 }
1352
1353 static ssize_t bluetooth_enable_store(struct device *dev,
1354                             struct device_attribute *attr,
1355                             const char *buf, size_t count)
1356 {
1357         unsigned long t;
1358         int res;
1359
1360         if (parse_strtoul(buf, 1, &t))
1361                 return -EINVAL;
1362
1363         res = bluetooth_set_radiosw(t);
1364
1365         return (res) ? res : count;
1366 }
1367
1368 static struct device_attribute dev_attr_bluetooth_enable =
1369         __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1370                 bluetooth_enable_show, bluetooth_enable_store);
1371
1372 /* --------------------------------------------------------------------- */
1373
1374 static struct attribute *bluetooth_attributes[] = {
1375         &dev_attr_bluetooth_enable.attr,
1376         NULL
1377 };
1378
1379 static const struct attribute_group bluetooth_attr_group = {
1380         .attrs = bluetooth_attributes,
1381 };
1382
1383 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1384 {
1385         int res;
1386         int status = 0;
1387
1388         vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1389
1390         IBM_ACPIHANDLE_INIT(hkey);
1391
1392         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1393            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1394         tp_features.bluetooth = hkey_handle &&
1395             acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1396
1397         vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1398                 str_supported(tp_features.bluetooth),
1399                 status);
1400
1401         if (tp_features.bluetooth) {
1402                 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1403                         /* no bluetooth hardware present in system */
1404                         tp_features.bluetooth = 0;
1405                         dbg_printk(TPACPI_DBG_INIT,
1406                                    "bluetooth hardware not installed\n");
1407                 } else {
1408                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1409                                         &bluetooth_attr_group);
1410                         if (res)
1411                                 return res;
1412                 }
1413         }
1414
1415         return (tp_features.bluetooth)? 0 : 1;
1416 }
1417
1418 static void bluetooth_exit(void)
1419 {
1420         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1421                         &bluetooth_attr_group);
1422 }
1423
1424 static int bluetooth_get_radiosw(void)
1425 {
1426         int status;
1427
1428         if (!tp_features.bluetooth)
1429                 return -ENODEV;
1430
1431         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1432                 return -EIO;
1433
1434         return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1435 }
1436
1437 static int bluetooth_set_radiosw(int radio_on)
1438 {
1439         int status;
1440
1441         if (!tp_features.bluetooth)
1442                 return -ENODEV;
1443
1444         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1445                 return -EIO;
1446         if (radio_on)
1447                 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1448         else
1449                 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1450         if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1451                 return -EIO;
1452
1453         return 0;
1454 }
1455
1456 /* procfs -------------------------------------------------------------- */
1457 static int bluetooth_read(char *p)
1458 {
1459         int len = 0;
1460         int status = bluetooth_get_radiosw();
1461
1462         if (!tp_features.bluetooth)
1463                 len += sprintf(p + len, "status:\t\tnot supported\n");
1464         else {
1465                 len += sprintf(p + len, "status:\t\t%s\n",
1466                                 (status)? "enabled" : "disabled");
1467                 len += sprintf(p + len, "commands:\tenable, disable\n");
1468         }
1469
1470         return len;
1471 }
1472
1473 static int bluetooth_write(char *buf)
1474 {
1475         char *cmd;
1476
1477         if (!tp_features.bluetooth)
1478                 return -ENODEV;
1479
1480         while ((cmd = next_cmd(&buf))) {
1481                 if (strlencmp(cmd, "enable") == 0) {
1482                         bluetooth_set_radiosw(1);
1483                 } else if (strlencmp(cmd, "disable") == 0) {
1484                         bluetooth_set_radiosw(0);
1485                 } else
1486                         return -EINVAL;
1487         }
1488
1489         return 0;
1490 }
1491
1492 static struct ibm_struct bluetooth_driver_data = {
1493         .name = "bluetooth",
1494         .read = bluetooth_read,
1495         .write = bluetooth_write,
1496         .exit = bluetooth_exit,
1497 };
1498
1499 /*************************************************************************
1500  * Wan subdriver
1501  */
1502
1503 /* sysfs wan enable ---------------------------------------------------- */
1504 static ssize_t wan_enable_show(struct device *dev,
1505                            struct device_attribute *attr,
1506                            char *buf)
1507 {
1508         int status;
1509
1510         status = wan_get_radiosw();
1511         if (status < 0)
1512                 return status;
1513
1514         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1515 }
1516
1517 static ssize_t wan_enable_store(struct device *dev,
1518                             struct device_attribute *attr,
1519                             const char *buf, size_t count)
1520 {
1521         unsigned long t;
1522         int res;
1523
1524         if (parse_strtoul(buf, 1, &t))
1525                 return -EINVAL;
1526
1527         res = wan_set_radiosw(t);
1528
1529         return (res) ? res : count;
1530 }
1531
1532 static struct device_attribute dev_attr_wan_enable =
1533         __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1534                 wan_enable_show, wan_enable_store);
1535
1536 /* --------------------------------------------------------------------- */
1537
1538 static struct attribute *wan_attributes[] = {
1539         &dev_attr_wan_enable.attr,
1540         NULL
1541 };
1542
1543 static const struct attribute_group wan_attr_group = {
1544         .attrs = wan_attributes,
1545 };
1546
1547 static int __init wan_init(struct ibm_init_struct *iibm)
1548 {
1549         int res;
1550         int status = 0;
1551
1552         vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1553
1554         IBM_ACPIHANDLE_INIT(hkey);
1555
1556         tp_features.wan = hkey_handle &&
1557             acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1558
1559         vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1560                 str_supported(tp_features.wan),
1561                 status);
1562
1563         if (tp_features.wan) {
1564                 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1565                         /* no wan hardware present in system */
1566                         tp_features.wan = 0;
1567                         dbg_printk(TPACPI_DBG_INIT,
1568                                    "wan hardware not installed\n");
1569                 } else {
1570                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1571                                         &wan_attr_group);
1572                         if (res)
1573                                 return res;
1574                 }
1575         }
1576
1577         return (tp_features.wan)? 0 : 1;
1578 }
1579
1580 static void wan_exit(void)
1581 {
1582         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1583                 &wan_attr_group);
1584 }
1585
1586 static int wan_get_radiosw(void)
1587 {
1588         int status;
1589
1590         if (!tp_features.wan)
1591                 return -ENODEV;
1592
1593         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1594                 return -EIO;
1595
1596         return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1597 }
1598
1599 static int wan_set_radiosw(int radio_on)
1600 {
1601         int status;
1602
1603         if (!tp_features.wan)
1604                 return -ENODEV;
1605
1606         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1607                 return -EIO;
1608         if (radio_on)
1609                 status |= TP_ACPI_WANCARD_RADIOSSW;
1610         else
1611                 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1612         if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1613                 return -EIO;
1614
1615         return 0;
1616 }
1617
1618 /* procfs -------------------------------------------------------------- */
1619 static int wan_read(char *p)
1620 {
1621         int len = 0;
1622         int status = wan_get_radiosw();
1623
1624         if (!tp_features.wan)
1625                 len += sprintf(p + len, "status:\t\tnot supported\n");
1626         else {
1627                 len += sprintf(p + len, "status:\t\t%s\n",
1628                                 (status)? "enabled" : "disabled");
1629                 len += sprintf(p + len, "commands:\tenable, disable\n");
1630         }
1631
1632         return len;
1633 }
1634
1635 static int wan_write(char *buf)
1636 {
1637         char *cmd;
1638
1639         if (!tp_features.wan)
1640                 return -ENODEV;
1641
1642         while ((cmd = next_cmd(&buf))) {
1643                 if (strlencmp(cmd, "enable") == 0) {
1644                         wan_set_radiosw(1);
1645                 } else if (strlencmp(cmd, "disable") == 0) {
1646                         wan_set_radiosw(0);
1647                 } else
1648                         return -EINVAL;
1649         }
1650
1651         return 0;
1652 }
1653
1654 static struct ibm_struct wan_driver_data = {
1655         .name = "wan",
1656         .read = wan_read,
1657         .write = wan_write,
1658         .exit = wan_exit,
1659         .flags.experimental = 1,
1660 };
1661
1662 /*************************************************************************
1663  * Video subdriver
1664  */
1665
1666 static enum video_access_mode video_supported;
1667 static int video_orig_autosw;
1668
1669 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
1670            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
1671            "\\_SB.PCI0.VID0",   /* 770e */
1672            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
1673            "\\_SB.PCI0.AGP.VID",        /* all others */
1674            );                           /* R30, R31 */
1675
1676 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
1677
1678 static int __init video_init(struct ibm_init_struct *iibm)
1679 {
1680         int ivga;
1681
1682         vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1683
1684         IBM_ACPIHANDLE_INIT(vid);
1685         IBM_ACPIHANDLE_INIT(vid2);
1686
1687         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1688                 /* G41, assume IVGA doesn't change */
1689                 vid_handle = vid2_handle;
1690
1691         if (!vid_handle)
1692                 /* video switching not supported on R30, R31 */
1693                 video_supported = TPACPI_VIDEO_NONE;
1694         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1695                 /* 570 */
1696                 video_supported = TPACPI_VIDEO_570;
1697         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1698                 /* 600e/x, 770e, 770x */
1699                 video_supported = TPACPI_VIDEO_770;
1700         else
1701                 /* all others */
1702                 video_supported = TPACPI_VIDEO_NEW;
1703
1704         vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1705                 str_supported(video_supported != TPACPI_VIDEO_NONE),
1706                 video_supported);
1707
1708         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1709 }
1710
1711 static void video_exit(void)
1712 {
1713         dbg_printk(TPACPI_DBG_EXIT,
1714                    "restoring original video autoswitch mode\n");
1715         if (video_autosw_set(video_orig_autosw))
1716                 printk(IBM_ERR "error while trying to restore original "
1717                         "video autoswitch mode\n");
1718 }
1719
1720 static int video_outputsw_get(void)
1721 {
1722         int status = 0;
1723         int i;
1724
1725         switch (video_supported) {
1726         case TPACPI_VIDEO_570:
1727                 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1728                                  TP_ACPI_VIDEO_570_PHSCMD))
1729                         return -EIO;
1730                 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1731                 break;
1732         case TPACPI_VIDEO_770:
1733                 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1734                         return -EIO;
1735                 if (i)
1736                         status |= TP_ACPI_VIDEO_S_LCD;
1737                 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1738                         return -EIO;
1739                 if (i)
1740                         status |= TP_ACPI_VIDEO_S_CRT;
1741                 break;
1742         case TPACPI_VIDEO_NEW:
1743                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1744                     !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1745                         return -EIO;
1746                 if (i)
1747                         status |= TP_ACPI_VIDEO_S_CRT;
1748
1749                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1750                     !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1751                         return -EIO;
1752                 if (i)
1753                         status |= TP_ACPI_VIDEO_S_LCD;
1754                 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1755                         return -EIO;
1756                 if (i)
1757                         status |= TP_ACPI_VIDEO_S_DVI;
1758                 break;
1759         default:
1760                 return -ENOSYS;
1761         }
1762
1763         return status;
1764 }
1765
1766 static int video_outputsw_set(int status)
1767 {
1768         int autosw;
1769         int res = 0;
1770
1771         switch (video_supported) {
1772         case TPACPI_VIDEO_570:
1773                 res = acpi_evalf(NULL, NULL,
1774                                  "\\_SB.PHS2", "vdd",
1775                                  TP_ACPI_VIDEO_570_PHS2CMD,
1776                                  status | TP_ACPI_VIDEO_570_PHS2SET);
1777                 break;
1778         case TPACPI_VIDEO_770:
1779                 autosw = video_autosw_get();
1780                 if (autosw < 0)
1781                         return autosw;
1782
1783                 res = video_autosw_set(1);
1784                 if (res)
1785                         return res;
1786                 res = acpi_evalf(vid_handle, NULL,
1787                                  "ASWT", "vdd", status * 0x100, 0);
1788                 if (!autosw && video_autosw_set(autosw)) {
1789                         printk(IBM_ERR "video auto-switch left enabled due to error\n");
1790                         return -EIO;
1791                 }
1792                 break;
1793         case TPACPI_VIDEO_NEW:
1794                 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1795                         acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1796                 break;
1797         default:
1798                 return -ENOSYS;
1799         }
1800
1801         return (res)? 0 : -EIO;
1802 }
1803
1804 static int video_autosw_get(void)
1805 {
1806         int autosw = 0;
1807
1808         switch (video_supported) {
1809         case TPACPI_VIDEO_570:
1810                 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1811                         return -EIO;
1812                 break;
1813         case TPACPI_VIDEO_770:
1814         case TPACPI_VIDEO_NEW:
1815                 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1816                         return -EIO;
1817                 break;
1818         default:
1819                 return -ENOSYS;
1820         }
1821
1822         return autosw & 1;
1823 }
1824
1825 static int video_autosw_set(int enable)
1826 {
1827         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1828                 return -EIO;
1829         return 0;
1830 }
1831
1832 static int video_outputsw_cycle(void)
1833 {
1834         int autosw = video_autosw_get();
1835         int res;
1836
1837         if (autosw < 0)
1838                 return autosw;
1839
1840         switch (video_supported) {
1841         case TPACPI_VIDEO_570:
1842                 res = video_autosw_set(1);
1843                 if (res)
1844                         return res;
1845                 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1846                 break;
1847         case TPACPI_VIDEO_770:
1848         case TPACPI_VIDEO_NEW:
1849                 res = video_autosw_set(1);
1850                 if (res)
1851                         return res;
1852                 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1853                 break;
1854         default:
1855                 return -ENOSYS;
1856         }
1857         if (!autosw && video_autosw_set(autosw)) {
1858                 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1859                 return -EIO;
1860         }
1861
1862         return (res)? 0 : -EIO;
1863 }
1864
1865 static int video_expand_toggle(void)
1866 {
1867         switch (video_supported) {
1868         case TPACPI_VIDEO_570:
1869                 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1870                         0 : -EIO;
1871         case TPACPI_VIDEO_770:
1872                 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1873                         0 : -EIO;
1874         case TPACPI_VIDEO_NEW:
1875                 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1876                         0 : -EIO;
1877         default:
1878                 return -ENOSYS;
1879         }
1880         /* not reached */
1881 }
1882
1883 static int video_read(char *p)
1884 {
1885         int status, autosw;
1886         int len = 0;
1887
1888         if (video_supported == TPACPI_VIDEO_NONE) {
1889                 len += sprintf(p + len, "status:\t\tnot supported\n");
1890                 return len;
1891         }
1892
1893         status = video_outputsw_get();
1894         if (status < 0)
1895                 return status;
1896
1897         autosw = video_autosw_get();
1898         if (autosw < 0)
1899                 return autosw;
1900
1901         len += sprintf(p + len, "status:\t\tsupported\n");
1902         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1903         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1904         if (video_supported == TPACPI_VIDEO_NEW)
1905                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1906         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1907         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1908         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1909         if (video_supported == TPACPI_VIDEO_NEW)
1910                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1911         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1912         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1913
1914         return len;
1915 }
1916
1917 static int video_write(char *buf)
1918 {
1919         char *cmd;
1920         int enable, disable, status;
1921         int res;
1922
1923         if (video_supported == TPACPI_VIDEO_NONE)
1924                 return -ENODEV;
1925
1926         enable = 0;
1927         disable = 0;
1928
1929         while ((cmd = next_cmd(&buf))) {
1930                 if (strlencmp(cmd, "lcd_enable") == 0) {
1931                         enable |= TP_ACPI_VIDEO_S_LCD;
1932                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1933                         disable |= TP_ACPI_VIDEO_S_LCD;
1934                 } else if (strlencmp(cmd, "crt_enable") == 0) {
1935                         enable |= TP_ACPI_VIDEO_S_CRT;
1936                 } else if (strlencmp(cmd, "crt_disable") == 0) {
1937                         disable |= TP_ACPI_VIDEO_S_CRT;
1938                 } else if (video_supported == TPACPI_VIDEO_NEW &&
1939                            strlencmp(cmd, "dvi_enable") == 0) {
1940                         enable |= TP_ACPI_VIDEO_S_DVI;
1941                 } else if (video_supported == TPACPI_VIDEO_NEW &&
1942                            strlencmp(cmd, "dvi_disable") == 0) {
1943                         disable |= TP_ACPI_VIDEO_S_DVI;
1944                 } else if (strlencmp(cmd, "auto_enable") == 0) {
1945                         res = video_autosw_set(1);
1946                         if (res)
1947                                 return res;
1948                 } else if (strlencmp(cmd, "auto_disable") == 0) {
1949                         res = video_autosw_set(0);
1950                         if (res)
1951                                 return res;
1952                 } else if (strlencmp(cmd, "video_switch") == 0) {
1953                         res = video_outputsw_cycle();
1954                         if (res)
1955                                 return res;
1956                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1957                         res = video_expand_toggle();
1958                         if (res)
1959                                 return res;
1960                 } else
1961                         return -EINVAL;
1962         }
1963
1964         if (enable || disable) {
1965                 status = video_outputsw_get();
1966                 if (status < 0)
1967                         return status;
1968                 res = video_outputsw_set((status & ~disable) | enable);
1969                 if (res)
1970                         return res;
1971         }
1972
1973         return 0;
1974 }
1975
1976 static struct ibm_struct video_driver_data = {
1977         .name = "video",
1978         .read = video_read,
1979         .write = video_write,
1980         .exit = video_exit,
1981 };
1982
1983 /*************************************************************************
1984  * Light (thinklight) subdriver
1985  */
1986
1987 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A2xm/p, T20-22, X20-21 */
1988 IBM_HANDLE(ledb, ec, "LEDB");           /* G4x */
1989
1990 static int __init light_init(struct ibm_init_struct *iibm)
1991 {
1992         vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1993
1994         IBM_ACPIHANDLE_INIT(ledb);
1995         IBM_ACPIHANDLE_INIT(lght);
1996         IBM_ACPIHANDLE_INIT(cmos);
1997
1998         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1999         tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2000
2001         if (tp_features.light)
2002                 /* light status not supported on
2003                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2004                 tp_features.light_status =
2005                         acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2006
2007         vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2008                 str_supported(tp_features.light));
2009
2010         return (tp_features.light)? 0 : 1;
2011 }
2012
2013 static int light_read(char *p)
2014 {
2015         int len = 0;
2016         int status = 0;
2017
2018         if (!tp_features.light) {
2019                 len += sprintf(p + len, "status:\t\tnot supported\n");
2020         } else if (!tp_features.light_status) {
2021                 len += sprintf(p + len, "status:\t\tunknown\n");
2022                 len += sprintf(p + len, "commands:\ton, off\n");
2023         } else {
2024                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2025                         return -EIO;
2026                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2027                 len += sprintf(p + len, "commands:\ton, off\n");
2028         }
2029
2030         return len;
2031 }
2032
2033 static int light_write(char *buf)
2034 {
2035         int cmos_cmd, lght_cmd;
2036         char *cmd;
2037         int success;
2038
2039         if (!tp_features.light)
2040                 return -ENODEV;
2041
2042         while ((cmd = next_cmd(&buf))) {
2043                 if (strlencmp(cmd, "on") == 0) {
2044                         cmos_cmd = 0x0c;
2045                         lght_cmd = 1;
2046                 } else if (strlencmp(cmd, "off") == 0) {
2047                         cmos_cmd = 0x0d;
2048                         lght_cmd = 0;
2049                 } else
2050                         return -EINVAL;
2051
2052                 success = cmos_handle ?
2053                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2054                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2055                 if (!success)
2056                         return -EIO;
2057         }
2058
2059         return 0;
2060 }
2061
2062 static struct ibm_struct light_driver_data = {
2063         .name = "light",
2064         .read = light_read,
2065         .write = light_write,
2066 };
2067
2068 /*************************************************************************
2069  * Dock subdriver
2070  */
2071
2072 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2073
2074 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
2075            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2076            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
2077            "\\_SB.PCI.ISA.SLCE",        /* 570 */
2078     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2079
2080 /* don't list other alternatives as we install a notify handler on the 570 */
2081 IBM_HANDLE(pci, root, "\\_SB.PCI");     /* 570 */
2082
2083 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2084         {
2085          .notify = dock_notify,
2086          .handle = &dock_handle,
2087          .type = ACPI_SYSTEM_NOTIFY,
2088         },
2089         {
2090         /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2091          * We just use it to get notifications of dock hotplug
2092          * in very old thinkpads */
2093          .hid = PCI_ROOT_HID_STRING,
2094          .notify = dock_notify,
2095          .handle = &pci_handle,
2096          .type = ACPI_SYSTEM_NOTIFY,
2097         },
2098 };
2099
2100 static struct ibm_struct dock_driver_data[2] = {
2101         {
2102          .name = "dock",
2103          .read = dock_read,
2104          .write = dock_write,
2105          .acpi = &ibm_dock_acpidriver[0],
2106         },
2107         {
2108          .name = "dock",
2109          .acpi = &ibm_dock_acpidriver[1],
2110         },
2111 };
2112
2113 #define dock_docked() (_sta(dock_handle) & 1)
2114
2115 static int __init dock_init(struct ibm_init_struct *iibm)
2116 {
2117         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2118
2119         IBM_ACPIHANDLE_INIT(dock);
2120
2121         vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2122                 str_supported(dock_handle != NULL));
2123
2124         return (dock_handle)? 0 : 1;
2125 }
2126
2127 static int __init dock_init2(struct ibm_init_struct *iibm)
2128 {
2129         int dock2_needed;
2130
2131         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2132
2133         if (dock_driver_data[0].flags.acpi_driver_registered &&
2134             dock_driver_data[0].flags.acpi_notify_installed) {
2135                 IBM_ACPIHANDLE_INIT(pci);
2136                 dock2_needed = (pci_handle != NULL);
2137                 vdbg_printk(TPACPI_DBG_INIT,
2138                             "dock PCI handler for the TP 570 is %s\n",
2139                             str_supported(dock2_needed));
2140         } else {
2141                 vdbg_printk(TPACPI_DBG_INIT,
2142                 "dock subdriver part 2 not required\n");
2143                 dock2_needed = 0;
2144         }
2145
2146         return (dock2_needed)? 0 : 1;
2147 }
2148
2149 static void dock_notify(struct ibm_struct *ibm, u32 event)
2150 {
2151         int docked = dock_docked();
2152         int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2153
2154         if (event == 1 && !pci) /* 570 */
2155                 acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
2156         else if (event == 1 && pci)     /* 570 */
2157                 acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
2158         else if (event == 3 && docked)
2159                 acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
2160         else if (event == 3 && !docked)
2161                 acpi_bus_generate_event(ibm->acpi->device, event, 2);   /* undock */
2162         else if (event == 0 && docked)
2163                 acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
2164         else {
2165                 printk(IBM_ERR "unknown dock event %d, status %d\n",
2166                        event, _sta(dock_handle));
2167                 acpi_bus_generate_event(ibm->acpi->device, event, 0);   /* unknown */
2168         }
2169 }
2170
2171 static int dock_read(char *p)
2172 {
2173         int len = 0;
2174         int docked = dock_docked();
2175
2176         if (!dock_handle)
2177                 len += sprintf(p + len, "status:\t\tnot supported\n");
2178         else if (!docked)
2179                 len += sprintf(p + len, "status:\t\tundocked\n");
2180         else {
2181                 len += sprintf(p + len, "status:\t\tdocked\n");
2182                 len += sprintf(p + len, "commands:\tdock, undock\n");
2183         }
2184
2185         return len;
2186 }
2187
2188 static int dock_write(char *buf)
2189 {
2190         char *cmd;
2191
2192         if (!dock_docked())
2193                 return -ENODEV;
2194
2195         while ((cmd = next_cmd(&buf))) {
2196                 if (strlencmp(cmd, "undock") == 0) {
2197                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2198                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2199                                 return -EIO;
2200                 } else if (strlencmp(cmd, "dock") == 0) {
2201                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2202                                 return -EIO;
2203                 } else
2204                         return -EINVAL;
2205         }
2206
2207         return 0;
2208 }
2209
2210 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2211
2212 /*************************************************************************
2213  * Bay subdriver
2214  */
2215
2216 #ifdef CONFIG_THINKPAD_ACPI_BAY
2217 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",        /* 570 */
2218            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2219            "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2220            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2221            );                           /* A21e, R30, R31 */
2222 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2223            "_EJ0",              /* all others */
2224            );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2225 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",     /* A3x, R32 */
2226            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2227            );                           /* all others */
2228 IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
2229            "_EJ0",                      /* 770x */
2230            );                           /* all others */
2231
2232 static int __init bay_init(struct ibm_init_struct *iibm)
2233 {
2234         vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2235
2236         IBM_ACPIHANDLE_INIT(bay);
2237         if (bay_handle)
2238                 IBM_ACPIHANDLE_INIT(bay_ej);
2239         IBM_ACPIHANDLE_INIT(bay2);
2240         if (bay2_handle)
2241                 IBM_ACPIHANDLE_INIT(bay2_ej);
2242
2243         tp_features.bay_status = bay_handle &&
2244                 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2245         tp_features.bay_status2 = bay2_handle &&
2246                 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2247
2248         tp_features.bay_eject = bay_handle && bay_ej_handle &&
2249                 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2250         tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2251                 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2252
2253         vdbg_printk(TPACPI_DBG_INIT,
2254                 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2255                 str_supported(tp_features.bay_status),
2256                 str_supported(tp_features.bay_eject),
2257                 str_supported(tp_features.bay_status2),
2258                 str_supported(tp_features.bay_eject2));
2259
2260         return (tp_features.bay_status || tp_features.bay_eject ||
2261                 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2262 }
2263
2264 static void bay_notify(struct ibm_struct *ibm, u32 event)
2265 {
2266         acpi_bus_generate_event(ibm->acpi->device, event, 0);
2267 }
2268
2269 #define bay_occupied(b) (_sta(b##_handle) & 1)
2270
2271 static int bay_read(char *p)
2272 {
2273         int len = 0;
2274         int occupied = bay_occupied(bay);
2275         int occupied2 = bay_occupied(bay2);
2276         int eject, eject2;
2277
2278         len += sprintf(p + len, "status:\t\t%s\n",
2279                 tp_features.bay_status ?
2280                         (occupied ? "occupied" : "unoccupied") :
2281                                 "not supported");
2282         if (tp_features.bay_status2)
2283                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2284                                "occupied" : "unoccupied");
2285
2286         eject = tp_features.bay_eject && occupied;
2287         eject2 = tp_features.bay_eject2 && occupied2;
2288
2289         if (eject && eject2)
2290                 len += sprintf(p + len, "commands:\teject, eject2\n");
2291         else if (eject)
2292                 len += sprintf(p + len, "commands:\teject\n");
2293         else if (eject2)
2294                 len += sprintf(p + len, "commands:\teject2\n");
2295
2296         return len;
2297 }
2298
2299 static int bay_write(char *buf)
2300 {
2301         char *cmd;
2302
2303         if (!tp_features.bay_eject && !tp_features.bay_eject2)
2304                 return -ENODEV;
2305
2306         while ((cmd = next_cmd(&buf))) {
2307                 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2308                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2309                                 return -EIO;
2310                 } else if (tp_features.bay_eject2 &&
2311                            strlencmp(cmd, "eject2") == 0) {
2312                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2313                                 return -EIO;
2314                 } else
2315                         return -EINVAL;
2316         }
2317
2318         return 0;
2319 }
2320
2321 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2322         .notify = bay_notify,
2323         .handle = &bay_handle,
2324         .type = ACPI_SYSTEM_NOTIFY,
2325 };
2326
2327 static struct ibm_struct bay_driver_data = {
2328         .name = "bay",
2329         .read = bay_read,
2330         .write = bay_write,
2331         .acpi = &ibm_bay_acpidriver,
2332 };
2333
2334 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2335
2336 /*************************************************************************
2337  * CMOS subdriver
2338  */
2339
2340 /* sysfs cmos_command -------------------------------------------------- */
2341 static ssize_t cmos_command_store(struct device *dev,
2342                             struct device_attribute *attr,
2343                             const char *buf, size_t count)
2344 {
2345         unsigned long cmos_cmd;
2346         int res;
2347
2348         if (parse_strtoul(buf, 21, &cmos_cmd))
2349                 return -EINVAL;
2350
2351         res = issue_thinkpad_cmos_command(cmos_cmd);
2352         return (res)? res : count;
2353 }
2354
2355 static struct device_attribute dev_attr_cmos_command =
2356         __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2357
2358 /* --------------------------------------------------------------------- */
2359
2360 static int __init cmos_init(struct ibm_init_struct *iibm)
2361 {
2362         int res;
2363
2364         vdbg_printk(TPACPI_DBG_INIT,
2365                 "initializing cmos commands subdriver\n");
2366
2367         IBM_ACPIHANDLE_INIT(cmos);
2368
2369         vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2370                 str_supported(cmos_handle != NULL));
2371
2372         res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2373         if (res)
2374                 return res;
2375
2376         return (cmos_handle)? 0 : 1;
2377 }
2378
2379 static void cmos_exit(void)
2380 {
2381         device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2382 }
2383
2384 static int cmos_read(char *p)
2385 {
2386         int len = 0;
2387
2388         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2389            R30, R31, T20-22, X20-21 */
2390         if (!cmos_handle)
2391                 len += sprintf(p + len, "status:\t\tnot supported\n");
2392         else {
2393                 len += sprintf(p + len, "status:\t\tsupported\n");
2394                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2395         }
2396
2397         return len;
2398 }
2399
2400 static int cmos_write(char *buf)
2401 {
2402         char *cmd;
2403         int cmos_cmd, res;
2404
2405         while ((cmd = next_cmd(&buf))) {
2406                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2407                     cmos_cmd >= 0 && cmos_cmd <= 21) {
2408                         /* cmos_cmd set */
2409                 } else
2410                         return -EINVAL;
2411
2412                 res = issue_thinkpad_cmos_command(cmos_cmd);
2413                 if (res)
2414                         return res;
2415         }
2416
2417         return 0;
2418 }
2419
2420 static struct ibm_struct cmos_driver_data = {
2421         .name = "cmos",
2422         .read = cmos_read,
2423         .write = cmos_write,
2424         .exit = cmos_exit,
2425 };
2426
2427 /*************************************************************************
2428  * LED subdriver
2429  */
2430
2431 static enum led_access_mode led_supported;
2432
2433 IBM_HANDLE(led, ec, "SLED",     /* 570 */
2434            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2435            "LED",               /* all others */
2436            );                   /* R30, R31 */
2437
2438 static int __init led_init(struct ibm_init_struct *iibm)
2439 {
2440         vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2441
2442         IBM_ACPIHANDLE_INIT(led);
2443
2444         if (!led_handle)
2445                 /* led not supported on R30, R31 */
2446                 led_supported = TPACPI_LED_NONE;
2447         else if (strlencmp(led_path, "SLED") == 0)
2448                 /* 570 */
2449                 led_supported = TPACPI_LED_570;
2450         else if (strlencmp(led_path, "SYSL") == 0)
2451                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2452                 led_supported = TPACPI_LED_OLD;
2453         else
2454                 /* all others */
2455                 led_supported = TPACPI_LED_NEW;
2456
2457         vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2458                 str_supported(led_supported), led_supported);
2459
2460         return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2461 }
2462
2463 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2464
2465 static int led_read(char *p)
2466 {
2467         int len = 0;
2468
2469         if (!led_supported) {
2470                 len += sprintf(p + len, "status:\t\tnot supported\n");
2471                 return len;
2472         }
2473         len += sprintf(p + len, "status:\t\tsupported\n");
2474
2475         if (led_supported == TPACPI_LED_570) {
2476                 /* 570 */
2477                 int i, status;
2478                 for (i = 0; i < 8; i++) {
2479                         if (!acpi_evalf(ec_handle,
2480                                         &status, "GLED", "dd", 1 << i))
2481                                 return -EIO;
2482                         len += sprintf(p + len, "%d:\t\t%s\n",
2483                                        i, led_status(status));
2484                 }
2485         }
2486
2487         len += sprintf(p + len, "commands:\t"
2488                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2489
2490         return len;
2491 }
2492
2493 /* off, on, blink */
2494 static const int led_sled_arg1[] = { 0, 1, 3 };
2495 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
2496 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
2497 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2498
2499 static int led_write(char *buf)
2500 {
2501         char *cmd;
2502         int led, ind, ret;
2503
2504         if (!led_supported)
2505                 return -ENODEV;
2506
2507         while ((cmd = next_cmd(&buf))) {
2508                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2509                         return -EINVAL;
2510
2511                 if (strstr(cmd, "off")) {
2512                         ind = 0;
2513                 } else if (strstr(cmd, "on")) {
2514                         ind = 1;
2515                 } else if (strstr(cmd, "blink")) {
2516                         ind = 2;
2517                 } else
2518                         return -EINVAL;
2519
2520                 if (led_supported == TPACPI_LED_570) {
2521                         /* 570 */
2522                         led = 1 << led;
2523                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2524                                         led, led_sled_arg1[ind]))
2525                                 return -EIO;
2526                 } else if (led_supported == TPACPI_LED_OLD) {
2527                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2528                         led = 1 << led;
2529                         ret = ec_write(TPACPI_LED_EC_HLMS, led);
2530                         if (ret >= 0)
2531                                 ret =
2532                                     ec_write(TPACPI_LED_EC_HLBL,
2533                                              led * led_exp_hlbl[ind]);
2534                         if (ret >= 0)
2535                                 ret =
2536                                     ec_write(TPACPI_LED_EC_HLCL,
2537                                              led * led_exp_hlcl[ind]);
2538                         if (ret < 0)
2539                                 return ret;
2540                 } else {
2541                         /* all others */
2542                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2543                                         led, led_led_arg1[ind]))
2544                                 return -EIO;
2545                 }
2546         }
2547
2548         return 0;
2549 }
2550
2551 static struct ibm_struct led_driver_data = {
2552         .name = "led",
2553         .read = led_read,
2554         .write = led_write,
2555 };
2556
2557 /*************************************************************************
2558  * Beep subdriver
2559  */
2560
2561 IBM_HANDLE(beep, ec, "BEEP");   /* all except R30, R31 */
2562
2563 static int __init beep_init(struct ibm_init_struct *iibm)
2564 {
2565         vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2566
2567         IBM_ACPIHANDLE_INIT(beep);
2568
2569         vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2570                 str_supported(beep_handle != NULL));
2571
2572         return (beep_handle)? 0 : 1;
2573 }
2574
2575 static int beep_read(char *p)
2576 {
2577         int len = 0;
2578
2579         if (!beep_handle)
2580                 len += sprintf(p + len, "status:\t\tnot supported\n");
2581         else {
2582                 len += sprintf(p + len, "status:\t\tsupported\n");
2583                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2584         }
2585
2586         return len;
2587 }
2588
2589 static int beep_write(char *buf)
2590 {
2591         char *cmd;
2592         int beep_cmd;
2593
2594         if (!beep_handle)
2595                 return -ENODEV;
2596
2597         while ((cmd = next_cmd(&buf))) {
2598                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2599                     beep_cmd >= 0 && beep_cmd <= 17) {
2600                         /* beep_cmd set */
2601                 } else
2602                         return -EINVAL;
2603                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2604                         return -EIO;
2605         }
2606
2607         return 0;
2608 }
2609
2610 static struct ibm_struct beep_driver_data = {
2611         .name = "beep",
2612         .read = beep_read,
2613         .write = beep_write,
2614 };
2615
2616 /*************************************************************************
2617  * Thermal subdriver
2618  */
2619
2620 static enum thermal_access_mode thermal_read_mode;
2621
2622 /* sysfs temp##_input -------------------------------------------------- */
2623
2624 static ssize_t thermal_temp_input_show(struct device *dev,
2625                            struct device_attribute *attr,
2626                            char *buf)
2627 {
2628         struct sensor_device_attribute *sensor_attr =
2629                                         to_sensor_dev_attr(attr);
2630         int idx = sensor_attr->index;
2631         s32 value;
2632         int res;
2633
2634         res = thermal_get_sensor(idx, &value);
2635         if (res)
2636                 return res;
2637         if (value == TP_EC_THERMAL_TMP_NA * 1000)
2638                 return -ENXIO;
2639
2640         return snprintf(buf, PAGE_SIZE, "%d\n", value);
2641 }
2642
2643 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2644          SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2645
2646 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2647         THERMAL_SENSOR_ATTR_TEMP(1, 0),
2648         THERMAL_SENSOR_ATTR_TEMP(2, 1),
2649         THERMAL_SENSOR_ATTR_TEMP(3, 2),
2650         THERMAL_SENSOR_ATTR_TEMP(4, 3),
2651         THERMAL_SENSOR_ATTR_TEMP(5, 4),
2652         THERMAL_SENSOR_ATTR_TEMP(6, 5),
2653         THERMAL_SENSOR_ATTR_TEMP(7, 6),
2654         THERMAL_SENSOR_ATTR_TEMP(8, 7),
2655         THERMAL_SENSOR_ATTR_TEMP(9, 8),
2656         THERMAL_SENSOR_ATTR_TEMP(10, 9),
2657         THERMAL_SENSOR_ATTR_TEMP(11, 10),
2658         THERMAL_SENSOR_ATTR_TEMP(12, 11),
2659         THERMAL_SENSOR_ATTR_TEMP(13, 12),
2660         THERMAL_SENSOR_ATTR_TEMP(14, 13),
2661         THERMAL_SENSOR_ATTR_TEMP(15, 14),
2662         THERMAL_SENSOR_ATTR_TEMP(16, 15),
2663 };
2664
2665 #define THERMAL_ATTRS(X) \
2666         &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2667
2668 static struct attribute *thermal_temp_input_attr[] = {
2669         THERMAL_ATTRS(8),
2670         THERMAL_ATTRS(9),
2671         THERMAL_ATTRS(10),
2672         THERMAL_ATTRS(11),
2673         THERMAL_ATTRS(12),
2674         THERMAL_ATTRS(13),
2675         THERMAL_ATTRS(14),
2676         THERMAL_ATTRS(15),
2677         THERMAL_ATTRS(0),
2678         THERMAL_ATTRS(1),
2679         THERMAL_ATTRS(2),
2680         THERMAL_ATTRS(3),
2681         THERMAL_ATTRS(4),
2682         THERMAL_ATTRS(5),
2683         THERMAL_ATTRS(6),
2684         THERMAL_ATTRS(7),
2685         NULL
2686 };
2687
2688 static const struct attribute_group thermal_temp_input16_group = {
2689         .attrs = thermal_temp_input_attr
2690 };
2691
2692 static const struct attribute_group thermal_temp_input8_group = {
2693         .attrs = &thermal_temp_input_attr[8]
2694 };
2695
2696 #undef THERMAL_SENSOR_ATTR_TEMP
2697 #undef THERMAL_ATTRS
2698
2699 /* --------------------------------------------------------------------- */
2700
2701 static int __init thermal_init(struct ibm_init_struct *iibm)
2702 {
2703         u8 t, ta1, ta2;
2704         int i;
2705         int acpi_tmp7;
2706         int res;
2707
2708         vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2709
2710         acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2711
2712         if (thinkpad_id.ec_model) {
2713                 /*
2714                  * Direct EC access mode: sensors at registers
2715                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
2716                  * non-implemented, thermal sensors return 0x80 when
2717                  * not available
2718                  */
2719
2720                 ta1 = ta2 = 0;
2721                 for (i = 0; i < 8; i++) {
2722                         if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2723                                 ta1 |= t;
2724                         } else {
2725                                 ta1 = 0;
2726                                 break;
2727                         }
2728                         if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2729                                 ta2 |= t;
2730                         } else {
2731                                 ta1 = 0;
2732                                 break;
2733                         }
2734                 }
2735                 if (ta1 == 0) {
2736                         /* This is sheer paranoia, but we handle it anyway */
2737                         if (acpi_tmp7) {
2738                                 printk(IBM_ERR
2739                                        "ThinkPad ACPI EC access misbehaving, "
2740                                        "falling back to ACPI TMPx access mode\n");
2741                                 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2742                         } else {
2743                                 printk(IBM_ERR
2744                                        "ThinkPad ACPI EC access misbehaving, "
2745                                        "disabling thermal sensors access\n");
2746                                 thermal_read_mode = TPACPI_THERMAL_NONE;
2747                         }
2748                 } else {
2749                         thermal_read_mode =
2750                             (ta2 != 0) ?
2751                             TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2752                 }
2753         } else if (acpi_tmp7) {
2754                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2755                         /* 600e/x, 770e, 770x */
2756                         thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2757                 } else {
2758                         /* Standard ACPI TMPx access, max 8 sensors */
2759                         thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2760                 }
2761         } else {
2762                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2763                 thermal_read_mode = TPACPI_THERMAL_NONE;
2764         }
2765
2766         vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2767                 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2768                 thermal_read_mode);
2769
2770         switch(thermal_read_mode) {
2771         case TPACPI_THERMAL_TPEC_16:
2772                 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2773                                 &thermal_temp_input16_group);
2774                 if (res)
2775                         return res;
2776                 break;
2777         case TPACPI_THERMAL_TPEC_8:
2778         case TPACPI_THERMAL_ACPI_TMP07:
2779         case TPACPI_THERMAL_ACPI_UPDT:
2780                 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2781                                 &thermal_temp_input8_group);
2782                 if (res)
2783                         return res;
2784                 break;
2785         case TPACPI_THERMAL_NONE:
2786         default:
2787                 return 1;
2788         }
2789
2790         return 0;
2791 }
2792
2793 static void thermal_exit(void)
2794 {
2795         switch(thermal_read_mode) {
2796         case TPACPI_THERMAL_TPEC_16:
2797                 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2798                                    &thermal_temp_input16_group);
2799                 break;
2800         case TPACPI_THERMAL_TPEC_8:
2801         case TPACPI_THERMAL_ACPI_TMP07:
2802         case TPACPI_THERMAL_ACPI_UPDT:
2803                 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2804                                    &thermal_temp_input16_group);
2805                 break;
2806         case TPACPI_THERMAL_NONE:
2807         default:
2808                 break;
2809         }
2810 }
2811
2812 /* idx is zero-based */
2813 static int thermal_get_sensor(int idx, s32 *value)
2814 {
2815         int t;
2816         s8 tmp;
2817         char tmpi[5];
2818
2819         t = TP_EC_THERMAL_TMP0;
2820
2821         switch (thermal_read_mode) {
2822 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2823         case TPACPI_THERMAL_TPEC_16:
2824                 if (idx >= 8 && idx <= 15) {
2825                         t = TP_EC_THERMAL_TMP8;
2826                         idx -= 8;
2827                 }
2828                 /* fallthrough */
2829 #endif
2830         case TPACPI_THERMAL_TPEC_8:
2831                 if (idx <= 7) {
2832                         if (!acpi_ec_read(t + idx, &tmp))
2833                                 return -EIO;
2834                         *value = tmp * 1000;
2835                         return 0;
2836                 }
2837                 break;
2838
2839         case TPACPI_THERMAL_ACPI_UPDT:
2840                 if (idx <= 7) {
2841                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2842                         if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2843                                 return -EIO;
2844                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2845                                 return -EIO;
2846                         *value = (t - 2732) * 100;
2847                         return 0;
2848                 }
2849                 break;
2850
2851         case TPACPI_THERMAL_ACPI_TMP07:
2852                 if (idx <= 7) {
2853                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2854                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2855                                 return -EIO;
2856                         if (t > 127 || t < -127)
2857                                 t = TP_EC_THERMAL_TMP_NA;
2858                         *value = t * 1000;
2859                         return 0;
2860                 }
2861                 break;
2862
2863         case TPACPI_THERMAL_NONE:
2864         default:
2865                 return -ENOSYS;
2866         }
2867
2868         return -EINVAL;
2869 }
2870
2871 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2872 {
2873         int res, i;
2874         int n;
2875
2876         n = 8;
2877         i = 0;
2878
2879         if (!s)
2880                 return -EINVAL;
2881
2882         if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2883                 n = 16;
2884
2885         for(i = 0 ; i < n; i++) {
2886                 res = thermal_get_sensor(i, &s->temp[i]);
2887                 if (res)
2888                         return res;
2889         }
2890
2891         return n;
2892 }
2893
2894 static int thermal_read(char *p)
2895 {
2896         int len = 0;
2897         int n, i;
2898         struct ibm_thermal_sensors_struct t;
2899
2900         n = thermal_get_sensors(&t);
2901         if (unlikely(n < 0))
2902                 return n;
2903
2904         len += sprintf(p + len, "temperatures:\t");
2905
2906         if (n > 0) {
2907                 for (i = 0; i < (n - 1); i++)
2908                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2909                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2910         } else
2911                 len += sprintf(p + len, "not supported\n");
2912
2913         return len;
2914 }
2915
2916 static struct ibm_struct thermal_driver_data = {
2917         .name = "thermal",
2918         .read = thermal_read,
2919         .exit = thermal_exit,
2920 };
2921
2922 /*************************************************************************
2923  * EC Dump subdriver
2924  */
2925
2926 static u8 ecdump_regs[256];
2927
2928 static int ecdump_read(char *p)
2929 {
2930         int len = 0;
2931         int i, j;
2932         u8 v;
2933
2934         len += sprintf(p + len, "EC      "
2935                        " +00 +01 +02 +03 +04 +05 +06 +07"
2936                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2937         for (i = 0; i < 256; i += 16) {
2938                 len += sprintf(p + len, "EC 0x%02x:", i);
2939                 for (j = 0; j < 16; j++) {
2940                         if (!acpi_ec_read(i + j, &v))
2941                                 break;
2942                         if (v != ecdump_regs[i + j])
2943                                 len += sprintf(p + len, " *%02x", v);
2944                         else
2945                                 len += sprintf(p + len, "  %02x", v);
2946                         ecdump_regs[i + j] = v;
2947                 }
2948                 len += sprintf(p + len, "\n");
2949                 if (j != 16)
2950                         break;
2951         }
2952
2953         /* These are way too dangerous to advertise openly... */
2954 #if 0
2955         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2956                        " (<offset> is 00-ff, <value> is 00-ff)\n");
2957         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
2958                        " (<offset> is 00-ff, <value> is 0-255)\n");
2959 #endif
2960         return len;
2961 }
2962
2963 static int ecdump_write(char *buf)
2964 {
2965         char *cmd;
2966         int i, v;
2967
2968         while ((cmd = next_cmd(&buf))) {
2969                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2970                         /* i and v set */
2971                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2972                         /* i and v set */
2973                 } else
2974                         return -EINVAL;
2975                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2976                         if (!acpi_ec_write(i, v))
2977                                 return -EIO;
2978                 } else
2979                         return -EINVAL;
2980         }
2981
2982         return 0;
2983 }
2984
2985 static struct ibm_struct ecdump_driver_data = {
2986         .name = "ecdump",
2987         .read = ecdump_read,
2988         .write = ecdump_write,
2989         .flags.experimental = 1,
2990 };
2991
2992 /*************************************************************************
2993  * Backlight/brightness subdriver
2994  */
2995
2996 static struct backlight_device *ibm_backlight_device;
2997
2998 static struct backlight_ops ibm_backlight_data = {
2999         .get_brightness = brightness_get,
3000         .update_status  = brightness_update_status,
3001 };
3002
3003 static int __init brightness_init(struct ibm_init_struct *iibm)
3004 {
3005         int b;
3006
3007         vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3008
3009         if (!brightness_mode) {
3010                 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3011                         brightness_mode = 2;
3012                 else
3013                         brightness_mode = 3;
3014
3015                 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3016                         brightness_mode);
3017         }
3018
3019         if (brightness_mode > 3)
3020                 return -EINVAL;
3021
3022         b = brightness_get(NULL);
3023         if (b < 0)
3024                 return 1;
3025
3026         ibm_backlight_device = backlight_device_register(
3027                                         TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3028                                         &ibm_backlight_data);
3029         if (IS_ERR(ibm_backlight_device)) {
3030                 printk(IBM_ERR "Could not register backlight device\n");
3031                 return PTR_ERR(ibm_backlight_device);
3032         }
3033         vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3034
3035         ibm_backlight_device->props.max_brightness = 7;
3036         ibm_backlight_device->props.brightness = b;
3037         backlight_update_status(ibm_backlight_device);
3038
3039         return 0;
3040 }
3041
3042 static void brightness_exit(void)
3043 {
3044         if (ibm_backlight_device) {
3045                 vdbg_printk(TPACPI_DBG_EXIT,
3046                             "calling backlight_device_unregister()\n");
3047                 backlight_device_unregister(ibm_backlight_device);
3048                 ibm_backlight_device = NULL;
3049         }
3050 }
3051
3052 static int brightness_update_status(struct backlight_device *bd)
3053 {
3054         return brightness_set(
3055                 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3056                  bd->props.power == FB_BLANK_UNBLANK) ?
3057                                 bd->props.brightness : 0);
3058 }
3059
3060 /*
3061  * ThinkPads can read brightness from two places: EC 0x31, or
3062  * CMOS NVRAM byte 0x5E, bits 0-3.
3063  */
3064 static int brightness_get(struct backlight_device *bd)
3065 {
3066         u8 lec = 0, lcmos = 0, level = 0;
3067
3068         if (brightness_mode & 1) {
3069                 if (!acpi_ec_read(brightness_offset, &lec))
3070                         return -EIO;
3071                 lec &= 7;
3072                 level = lec;
3073         };
3074         if (brightness_mode & 2) {
3075                 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3076                          & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3077                         >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3078                 level = lcmos;
3079         }
3080
3081         if (brightness_mode == 3 && lec != lcmos) {
3082                 printk(IBM_ERR
3083                         "CMOS NVRAM (%u) and EC (%u) do not agree "
3084                         "on display brightness level\n",
3085                         (unsigned int) lcmos,
3086                         (unsigned int) lec);
3087                 return -EIO;
3088         }
3089
3090         return level;
3091 }
3092
3093 static int brightness_set(int value)
3094 {
3095         int cmos_cmd, inc, i;
3096         int current_value = brightness_get(NULL);
3097
3098         if (value > 7)
3099                 return -EINVAL;
3100
3101         cmos_cmd = value > current_value ?
3102                         TP_CMOS_BRIGHTNESS_UP :
3103                         TP_CMOS_BRIGHTNESS_DOWN;
3104         inc = value > current_value ? 1 : -1;
3105
3106         for (i = current_value; i != value; i += inc) {
3107                 if ((brightness_mode & 2) &&
3108                     issue_thinkpad_cmos_command(cmos_cmd))
3109                         return -EIO;
3110                 if ((brightness_mode & 1) &&
3111                     !acpi_ec_write(brightness_offset, i + inc))
3112                         return -EIO;
3113         }
3114
3115         return 0;
3116 }
3117
3118 static int brightness_read(char *p)
3119 {
3120         int len = 0;
3121         int level;
3122
3123         if ((level = brightness_get(NULL)) < 0) {
3124                 len += sprintf(p + len, "level:\t\tunreadable\n");
3125         } else {
3126                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3127                 len += sprintf(p + len, "commands:\tup, down\n");
3128                 len += sprintf(p + len, "commands:\tlevel <level>"
3129                                " (<level> is 0-7)\n");
3130         }
3131
3132         return len;
3133 }
3134
3135 static int brightness_write(char *buf)
3136 {
3137         int level;
3138         int new_level;
3139         char *cmd;
3140
3141         while ((cmd = next_cmd(&buf))) {
3142                 if ((level = brightness_get(NULL)) < 0)
3143                         return level;
3144                 level &= 7;
3145
3146                 if (strlencmp(cmd, "up") == 0) {
3147                         new_level = level == 7 ? 7 : level + 1;
3148                 } else if (strlencmp(cmd, "down") == 0) {
3149                         new_level = level == 0 ? 0 : level - 1;
3150                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3151                            new_level >= 0 && new_level <= 7) {
3152                         /* new_level set */
3153                 } else
3154                         return -EINVAL;
3155
3156                 brightness_set(new_level);
3157         }
3158
3159         return 0;
3160 }
3161
3162 static struct ibm_struct brightness_driver_data = {
3163         .name = "brightness",
3164         .read = brightness_read,
3165         .write = brightness_write,
3166         .exit = brightness_exit,
3167 };
3168
3169 /*************************************************************************
3170  * Volume subdriver
3171  */
3172
3173 static int volume_read(char *p)
3174 {
3175         int len = 0;
3176         u8 level;
3177
3178         if (!acpi_ec_read(volume_offset, &level)) {
3179                 len += sprintf(p + len, "level:\t\tunreadable\n");
3180         } else {
3181                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3182                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3183                 len += sprintf(p + len, "commands:\tup, down, mute\n");
3184                 len += sprintf(p + len, "commands:\tlevel <level>"
3185                                " (<level> is 0-15)\n");
3186         }
3187
3188         return len;
3189 }
3190
3191 static int volume_write(char *buf)
3192 {
3193         int cmos_cmd, inc, i;
3194         u8 level, mute;
3195         int new_level, new_mute;
3196         char *cmd;
3197
3198         while ((cmd = next_cmd(&buf))) {
3199                 if (!acpi_ec_read(volume_offset, &level))
3200                         return -EIO;
3201                 new_mute = mute = level & 0x40;
3202                 new_level = level = level & 0xf;
3203
3204                 if (strlencmp(cmd, "up") == 0) {
3205                         if (mute)
3206                                 new_mute = 0;
3207                         else
3208                                 new_level = level == 15 ? 15 : level + 1;
3209                 } else if (strlencmp(cmd, "down") == 0) {
3210                         if (mute)
3211                                 new_mute = 0;
3212                         else
3213                                 new_level = level == 0 ? 0 : level - 1;
3214                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3215                            new_level >= 0 && new_level <= 15) {
3216                         /* new_level set */
3217                 } else if (strlencmp(cmd, "mute") == 0) {
3218                         new_mute = 0x40;
3219                 } else
3220                         return -EINVAL;
3221
3222                 if (new_level != level) {       /* mute doesn't change */
3223                         cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3224                         inc = new_level > level ? 1 : -1;
3225
3226                         if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3227                                      !acpi_ec_write(volume_offset, level)))
3228                                 return -EIO;
3229
3230                         for (i = level; i != new_level; i += inc)
3231                                 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3232                                     !acpi_ec_write(volume_offset, i + inc))
3233                                         return -EIO;
3234
3235                         if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3236                                      !acpi_ec_write(volume_offset,
3237                                                     new_level + mute)))
3238                                 return -EIO;
3239                 }
3240
3241                 if (new_mute != mute) { /* level doesn't change */
3242                         cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3243
3244                         if (issue_thinkpad_cmos_command(cmos_cmd) ||
3245                             !acpi_ec_write(volume_offset, level + new_mute))
3246                                 return -EIO;
3247                 }
3248         }
3249
3250         return 0;
3251 }
3252
3253 static struct ibm_struct volume_driver_data = {
3254         .name = "volume",
3255         .read = volume_read,
3256         .write = volume_write,
3257 };
3258
3259 /*************************************************************************
3260  * Fan subdriver
3261  */
3262
3263 /*
3264  * FAN ACCESS MODES
3265  *
3266  * TPACPI_FAN_RD_ACPI_GFAN:
3267  *      ACPI GFAN method: returns fan level
3268  *
3269  *      see TPACPI_FAN_WR_ACPI_SFAN
3270  *      EC 0x2f (HFSP) not available if GFAN exists
3271  *
3272  * TPACPI_FAN_WR_ACPI_SFAN:
3273  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3274  *
3275  *      EC 0x2f (HFSP) might be available *for reading*, but do not use
3276  *      it for writing.
3277  *
3278  * TPACPI_FAN_WR_TPEC:
3279  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode
3280  *      Supported on almost all ThinkPads
3281  *
3282  *      Fan speed changes of any sort (including those caused by the
3283  *      disengaged mode) are usually done slowly by the firmware as the
3284  *      maximum ammount of fan duty cycle change per second seems to be
3285  *      limited.
3286  *
3287  *      Reading is not available if GFAN exists.
3288  *      Writing is not available if SFAN exists.
3289  *
3290  *      Bits
3291  *       7      automatic mode engaged;
3292  *              (default operation mode of the ThinkPad)
3293  *              fan level is ignored in this mode.
3294  *       6      full speed mode (takes precedence over bit 7);
3295  *              not available on all thinkpads.  May disable
3296  *              the tachometer while the fan controller ramps up
3297  *              the speed (which can take up to a few *minutes*).
3298  *              Speeds up fan to 100% duty-cycle, which is far above
3299  *              the standard RPM levels.  It is not impossible that
3300  *              it could cause hardware damage.
3301  *      5-3     unused in some models.  Extra bits for fan level
3302  *              in others, but still useless as all values above
3303  *              7 map to the same speed as level 7 in these models.
3304  *      2-0     fan level (0..7 usually)
3305  *                      0x00 = stop
3306  *                      0x07 = max (set when temperatures critical)
3307  *              Some ThinkPads may have other levels, see
3308  *              TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3309  *
3310  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3311  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3312  *      does so, its initial value is meaningless (0x07).
3313  *
3314  *      For firmware bugs, refer to:
3315  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3316  *
3317  *      ----
3318  *
3319  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3320  *      Main fan tachometer reading (in RPM)
3321  *
3322  *      This register is present on all ThinkPads with a new-style EC, and
3323  *      it is known not to be present on the A21m/e, and T22, as there is
3324  *      something else in offset 0x84 according to the ACPI DSDT.  Other
3325  *      ThinkPads from this same time period (and earlier) probably lack the
3326  *      tachometer as well.
3327  *
3328  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3329  *      was never fixed by IBM to report the EC firmware version string
3330  *      probably support the tachometer (like the early X models), so
3331  *      detecting it is quite hard.  We need more data to know for sure.
3332  *
3333  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3334  *      might result.
3335  *
3336  *      FIRMWARE BUG: may go stale while the EC is switching to full speed
3337  *      mode.
3338  *
3339  *      For firmware bugs, refer to:
3340  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3341  *
3342  * TPACPI_FAN_WR_ACPI_FANS:
3343  *      ThinkPad X31, X40, X41.  Not available in the X60.
3344  *
3345  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
3346  *      high speed.  ACPI DSDT seems to map these three speeds to levels
3347  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3348  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3349  *
3350  *      The speeds are stored on handles
3351  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3352  *
3353  *      There are three default speed sets, acessible as handles:
3354  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3355  *
3356  *      ACPI DSDT switches which set is in use depending on various
3357  *      factors.
3358  *
3359  *      TPACPI_FAN_WR_TPEC is also available and should be used to
3360  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
3361  *      but the ACPI tables just mention level 7.
3362  */
3363
3364 static enum fan_status_access_mode fan_status_access_mode;
3365 static enum fan_control_access_mode fan_control_access_mode;
3366 static enum fan_control_commands fan_control_commands;
3367
3368 static u8 fan_control_initial_status;
3369 static u8 fan_control_desired_level;
3370
3371 static void fan_watchdog_fire(struct work_struct *ignored);
3372 static int fan_watchdog_maxinterval;
3373 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3374
3375 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40, X41 */
3376 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
3377            "\\FSPD",            /* 600e/x, 770e, 770x */
3378            );                   /* all others */
3379 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
3380            "JFNS",              /* 770x-JL */
3381            );                   /* all others */
3382
3383 /*
3384  * SYSFS fan layout: hwmon compatible (device)
3385  *
3386  * pwm*_enable:
3387  *      0: "disengaged" mode
3388  *      1: manual mode
3389  *      2: native EC "auto" mode (recommended, hardware default)
3390  *
3391  * pwm*: set speed in manual mode, ignored otherwise.
3392  *      0 is level 0; 255 is level 7. Intermediate points done with linear
3393  *      interpolation.
3394  *
3395  * fan*_input: tachometer reading, RPM
3396  *
3397  *
3398  * SYSFS fan layout: extensions
3399  *
3400  * fan_watchdog (driver):
3401  *      fan watchdog interval in seconds, 0 disables (default), max 120
3402  */
3403
3404 /* sysfs fan pwm1_enable ----------------------------------------------- */
3405 static ssize_t fan_pwm1_enable_show(struct device *dev,
3406                                     struct device_attribute *attr,
3407                                     char *buf)
3408 {
3409         int res, mode;
3410         u8 status;
3411
3412         res = fan_get_status_safe(&status);
3413         if (res)
3414                 return res;
3415
3416         if (unlikely(tp_features.fan_ctrl_status_undef)) {
3417                 if (status != fan_control_initial_status) {
3418                         tp_features.fan_ctrl_status_undef = 0;
3419                 } else {
3420                         /* Return most likely status. In fact, it
3421                          * might be the only possible status */
3422                         status = TP_EC_FAN_AUTO;
3423                 }
3424         }
3425
3426         if (status & TP_EC_FAN_FULLSPEED) {
3427                 mode = 0;
3428         } else if (status & TP_EC_FAN_AUTO) {
3429                 mode = 2;
3430         } else
3431                 mode = 1;
3432
3433         return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3434 }
3435
3436 static ssize_t fan_pwm1_enable_store(struct device *dev,
3437                                      struct device_attribute *attr,
3438                                      const char *buf, size_t count)
3439 {
3440         unsigned long t;
3441         int res, level;
3442
3443         if (parse_strtoul(buf, 2, &t))
3444                 return -EINVAL;
3445
3446         switch (t) {
3447         case 0:
3448                 level = TP_EC_FAN_FULLSPEED;
3449                 break;
3450         case 1:
3451                 level = TPACPI_FAN_LAST_LEVEL;
3452                 break;
3453         case 2:
3454                 level = TP_EC_FAN_AUTO;
3455                 break;
3456         case 3:
3457                 /* reserved for software-controlled auto mode */
3458                 return -ENOSYS;
3459         default:
3460                 return -EINVAL;
3461         }
3462
3463         res = fan_set_level_safe(level);
3464         if (res == -ENXIO)
3465                 return -EINVAL;
3466         else if (res < 0)
3467                 return res;
3468
3469         fan_watchdog_reset();
3470
3471         return count;
3472 }
3473
3474 static struct device_attribute dev_attr_fan_pwm1_enable =
3475         __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3476                 fan_pwm1_enable_show, fan_pwm1_enable_store);
3477
3478 /* sysfs fan pwm1 ------------------------------------------------------ */
3479 static ssize_t fan_pwm1_show(struct device *dev,
3480                              struct device_attribute *attr,
3481                              char *buf)
3482 {
3483         int res;
3484         u8 status;
3485
3486         res = fan_get_status_safe(&status);
3487         if (res)
3488                 return res;
3489
3490         if (unlikely(tp_features.fan_ctrl_status_undef)) {
3491                 if (status != fan_control_initial_status) {
3492                         tp_features.fan_ctrl_status_undef = 0;
3493                 } else {
3494                         status = TP_EC_FAN_AUTO;
3495                 }
3496         }
3497
3498         if ((status &
3499              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3500                 status = fan_control_desired_level;
3501
3502         if (status > 7)
3503                 status = 7;
3504
3505         return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3506 }
3507
3508 static ssize_t fan_pwm1_store(struct device *dev,
3509                               struct device_attribute *attr,
3510                               const char *buf, size_t count)
3511 {
3512         unsigned long s;
3513         int rc;
3514         u8 status, newlevel;
3515
3516         if (parse_strtoul(buf, 255, &s))
3517                 return -EINVAL;
3518
3519         /* scale down from 0-255 to 0-7 */
3520         newlevel = (s >> 5) & 0x07;
3521
3522         rc = mutex_lock_interruptible(&fan_mutex);
3523         if (rc < 0)
3524                 return rc;
3525
3526         rc = fan_get_status(&status);
3527         if (!rc && (status &
3528                     (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3529                 rc = fan_set_level(newlevel);
3530                 if (rc == -ENXIO)
3531                         rc = -EINVAL;
3532                 else if (!rc) {
3533                         fan_update_desired_level(newlevel);
3534                         fan_watchdog_reset();
3535                 }
3536         }
3537
3538         mutex_unlock(&fan_mutex);
3539         return (rc)? rc : count;
3540 }
3541
3542 static struct device_attribute dev_attr_fan_pwm1 =
3543         __ATTR(pwm1, S_IWUSR | S_IRUGO,
3544                 fan_pwm1_show, fan_pwm1_store);
3545
3546 /* sysfs fan fan1_input ------------------------------------------------ */
3547 static ssize_t fan_fan1_input_show(struct device *dev,
3548                            struct device_attribute *attr,
3549                            char *buf)
3550 {
3551         int res;
3552         unsigned int speed;
3553
3554         res = fan_get_speed(&speed);
3555         if (res < 0)
3556                 return res;
3557
3558         return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3559 }
3560
3561 static struct device_attribute dev_attr_fan_fan1_input =
3562         __ATTR(fan1_input, S_IRUGO,
3563                 fan_fan1_input_show, NULL);
3564
3565 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3566 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3567                                      char *buf)
3568 {
3569         return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3570 }
3571
3572 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3573                                       const char *buf, size_t count)
3574 {
3575         unsigned long t;
3576
3577         if (parse_strtoul(buf, 120, &t))
3578                 return -EINVAL;
3579
3580         if (!fan_control_allowed)
3581                 return -EPERM;
3582
3583         fan_watchdog_maxinterval = t;
3584         fan_watchdog_reset();
3585
3586         return count;
3587 }
3588
3589 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3590                 fan_fan_watchdog_show, fan_fan_watchdog_store);
3591
3592 /* --------------------------------------------------------------------- */
3593 static struct attribute *fan_attributes[] = {
3594         &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3595         &dev_attr_fan_fan1_input.attr,
3596         NULL
3597 };
3598
3599 static const struct attribute_group fan_attr_group = {
3600         .attrs = fan_attributes,
3601 };
3602
3603 static int __init fan_init(struct ibm_init_struct *iibm)
3604 {
3605         int rc;
3606
3607         vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3608
3609         mutex_init(&fan_mutex);
3610         fan_status_access_mode = TPACPI_FAN_NONE;
3611         fan_control_access_mode = TPACPI_FAN_WR_NONE;
3612         fan_control_commands = 0;
3613         fan_watchdog_maxinterval = 0;
3614         tp_features.fan_ctrl_status_undef = 0;
3615         fan_control_desired_level = 7;
3616
3617         IBM_ACPIHANDLE_INIT(fans);
3618         IBM_ACPIHANDLE_INIT(gfan);
3619         IBM_ACPIHANDLE_INIT(sfan);
3620
3621         if (gfan_handle) {
3622                 /* 570, 600e/x, 770e, 770x */
3623                 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3624         } else {
3625                 /* all other ThinkPads: note that even old-style
3626                  * ThinkPad ECs supports the fan control register */
3627                 if (likely(acpi_ec_read(fan_status_offset,
3628                                         &fan_control_initial_status))) {
3629                         fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3630
3631                         /* In some ThinkPads, neither the EC nor the ACPI
3632                          * DSDT initialize the fan status, and it ends up
3633                          * being set to 0x07 when it *could* be either
3634                          * 0x07 or 0x80.
3635                          *
3636                          * Enable for TP-1Y (T43), TP-78 (R51e),
3637                          * TP-76 (R52), TP-70 (T43, R52), which are known
3638                          * to be buggy. */
3639                         if (fan_control_initial_status == 0x07) {
3640                                 switch (thinkpad_id.ec_model) {
3641                                 case 0x5931: /* TP-1Y */
3642                                 case 0x3837: /* TP-78 */
3643                                 case 0x3637: /* TP-76 */
3644                                 case 0x3037: /* TP-70 */
3645                                         printk(IBM_NOTICE
3646                                                "fan_init: initial fan status is "
3647                                                "unknown, assuming it is in auto "
3648                                                "mode\n");
3649                                         tp_features.fan_ctrl_status_undef = 1;
3650                                         ;;
3651                                 }
3652                         }
3653                 } else {
3654                         printk(IBM_ERR
3655                                "ThinkPad ACPI EC access misbehaving, "
3656                                "fan status and control unavailable\n");
3657                         return 1;
3658                 }
3659         }
3660
3661         if (sfan_handle) {
3662                 /* 570, 770x-JL */
3663                 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3664                 fan_control_commands |=
3665                     TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3666         } else {
3667                 if (!gfan_handle) {
3668                         /* gfan without sfan means no fan control */
3669                         /* all other models implement TP EC 0x2f control */
3670
3671                         if (fans_handle) {
3672                                 /* X31, X40, X41 */
3673                                 fan_control_access_mode =
3674                                     TPACPI_FAN_WR_ACPI_FANS;
3675                                 fan_control_commands |=
3676                                     TPACPI_FAN_CMD_SPEED |
3677                                     TPACPI_FAN_CMD_LEVEL |
3678                                     TPACPI_FAN_CMD_ENABLE;
3679                         } else {
3680                                 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3681                                 fan_control_commands |=
3682                                     TPACPI_FAN_CMD_LEVEL |
3683                                     TPACPI_FAN_CMD_ENABLE;
3684                         }
3685                 }
3686         }
3687
3688         vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3689                 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3690                   fan_control_access_mode != TPACPI_FAN_WR_NONE),
3691                 fan_status_access_mode, fan_control_access_mode);
3692
3693         /* fan control master switch */
3694         if (!fan_control_allowed) {
3695                 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3696                 fan_control_commands = 0;
3697                 dbg_printk(TPACPI_DBG_INIT,
3698                            "fan control features disabled by parameter\n");
3699         }
3700
3701         /* update fan_control_desired_level */
3702         if (fan_status_access_mode != TPACPI_FAN_NONE)
3703                 fan_get_status_safe(NULL);
3704
3705         if (fan_status_access_mode != TPACPI_FAN_NONE ||
3706             fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3707                 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3708                                          &fan_attr_group);
3709                 if (!(rc < 0))
3710                         rc = driver_create_file(&tpacpi_pdriver.driver,
3711                                         &driver_attr_fan_watchdog);
3712                 if (rc < 0)
3713                         return rc;
3714                 return 0;
3715         } else
3716                 return 1;
3717 }
3718
3719 /*
3720  * Call with fan_mutex held
3721  */
3722 static void fan_update_desired_level(u8 status)
3723 {
3724         if ((status &
3725              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3726                 if (status > 7)
3727                         fan_control_desired_level = 7;
3728                 else
3729                         fan_control_desired_level = status;
3730         }
3731 }
3732
3733 static int fan_get_status(u8 *status)
3734 {
3735         u8 s;
3736
3737         /* TODO:
3738          * Add TPACPI_FAN_RD_ACPI_FANS ? */
3739
3740         switch (fan_status_access_mode) {
3741         case TPACPI_FAN_RD_ACPI_GFAN:
3742                 /* 570, 600e/x, 770e, 770x */
3743
3744                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3745                         return -EIO;
3746
3747                 if (likely(status))
3748                         *status = s & 0x07;
3749
3750                 break;
3751
3752         case TPACPI_FAN_RD_TPEC:
3753                 /* all except 570, 600e/x, 770e, 770x */
3754                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3755                         return -EIO;
3756
3757                 if (likely(status))
3758                         *status = s;
3759
3760                 break;
3761
3762         default:
3763                 return -ENXIO;
3764         }
3765
3766         return 0;
3767 }
3768
3769 static int fan_get_status_safe(u8 *status)
3770 {
3771         int rc;
3772         u8 s;
3773
3774         rc = mutex_lock_interruptible(&fan_mutex);
3775         if (rc < 0)
3776                 return rc;
3777         rc = fan_get_status(&s);
3778         if (!rc)
3779                 fan_update_desired_level(s);
3780         mutex_unlock(&fan_mutex);
3781
3782         if (status)
3783                 *status = s;
3784
3785         return rc;
3786 }
3787
3788 static void fan_exit(void)
3789 {
3790         vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3791
3792         /* FIXME: can we really do this unconditionally? */
3793         sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3794         driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3795
3796         cancel_delayed_work(&fan_watchdog_task);
3797         flush_scheduled_work();
3798 }
3799
3800 static int fan_get_speed(unsigned int *speed)
3801 {
3802         u8 hi, lo;
3803
3804         switch (fan_status_access_mode) {
3805         case TPACPI_FAN_RD_TPEC:
3806                 /* all except 570, 600e/x, 770e, 770x */
3807                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3808                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3809                         return -EIO;
3810
3811                 if (likely(speed))
3812                         *speed = (hi << 8) | lo;
3813
3814                 break;
3815
3816         default:
3817                 return -ENXIO;
3818         }
3819
3820         return 0;
3821 }
3822
3823 static void fan_watchdog_fire(struct work_struct *ignored)
3824 {
3825         int rc;
3826
3827         printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3828         rc = fan_set_enable();
3829         if (rc < 0) {
3830                 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3831                         "will try again later...\n", -rc);
3832                 /* reschedule for later */
3833                 fan_watchdog_reset();
3834         }
3835 }
3836
3837 static void fan_watchdog_reset(void)
3838 {
3839         static int fan_watchdog_active;
3840
3841         if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3842                 return;
3843
3844         if (fan_watchdog_active)
3845                 cancel_delayed_work(&fan_watchdog_task);
3846
3847         if (fan_watchdog_maxinterval > 0) {
3848                 fan_watchdog_active = 1;
3849                 if (!schedule_delayed_work(&fan_watchdog_task,
3850                                 msecs_to_jiffies(fan_watchdog_maxinterval
3851                                                  * 1000))) {
3852                         printk(IBM_ERR "failed to schedule the fan watchdog, "
3853                                "watchdog will not trigger\n");
3854                 }
3855         } else
3856                 fan_watchdog_active = 0;
3857 }
3858
3859 static int fan_set_level(int level)
3860 {
3861         if (!fan_control_allowed)
3862                 return -EPERM;
3863
3864         switch (fan_control_access_mode) {
3865         case TPACPI_FAN_WR_ACPI_SFAN:
3866                 if (level >= 0 && level <= 7) {
3867                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3868                                 return -EIO;
3869                 } else
3870                         return -EINVAL;
3871                 break;
3872
3873         case TPACPI_FAN_WR_ACPI_FANS:
3874         case TPACPI_FAN_WR_TPEC:
3875                 if ((level != TP_EC_FAN_AUTO) &&
3876                     (level != TP_EC_FAN_FULLSPEED) &&
3877                     ((level < 0) || (level > 7)))
3878                         return -EINVAL;
3879
3880                 /* safety net should the EC not support AUTO
3881                  * or FULLSPEED mode bits and just ignore them */
3882                 if (level & TP_EC_FAN_FULLSPEED)
3883                         level |= 7;     /* safety min speed 7 */
3884                 else if (level & TP_EC_FAN_FULLSPEED)
3885                         level |= 4;     /* safety min speed 4 */
3886
3887                 if (!acpi_ec_write(fan_status_offset, level))
3888                         return -EIO;
3889                 else
3890                         tp_features.fan_ctrl_status_undef = 0;
3891                 break;
3892
3893         default:
3894                 return -ENXIO;
3895         }
3896         return 0;
3897 }
3898
3899 static int fan_set_level_safe(int level)
3900 {
3901         int rc;
3902
3903         if (!fan_control_allowed)
3904                 return -EPERM;
3905
3906         rc = mutex_lock_interruptible(&fan_mutex);
3907         if (rc < 0)
3908                 return rc;
3909
3910         if (level == TPACPI_FAN_LAST_LEVEL)
3911                 level = fan_control_desired_level;
3912
3913         rc = fan_set_level(level);
3914         if (!rc)
3915                 fan_update_desired_level(level);
3916
3917         mutex_unlock(&fan_mutex);
3918         return rc;
3919 }
3920
3921 static int fan_set_enable(void)
3922 {
3923         u8 s;
3924         int rc;
3925
3926         if (!fan_control_allowed)
3927                 return -EPERM;
3928
3929         rc = mutex_lock_interruptible(&fan_mutex);
3930         if (rc < 0)
3931                 return rc;
3932
3933         switch (fan_control_access_mode) {
3934         case TPACPI_FAN_WR_ACPI_FANS:
3935         case TPACPI_FAN_WR_TPEC:
3936                 rc = fan_get_status(&s);
3937                 if (rc < 0)
3938                         break;
3939
3940                 /* Don't go out of emergency fan mode */
3941                 if (s != 7) {
3942                         s &= 0x07;
3943                         s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3944                 }
3945
3946                 if (!acpi_ec_write(fan_status_offset, s))
3947                         rc = -EIO;
3948                 else {
3949                         tp_features.fan_ctrl_status_undef = 0;
3950                         rc = 0;
3951                 }
3952                 break;
3953
3954         case TPACPI_FAN_WR_ACPI_SFAN:
3955                 rc = fan_get_status(&s);
3956                 if (rc < 0)
3957                         break;
3958
3959                 s &= 0x07;
3960
3961                 /* Set fan to at least level 4 */
3962                 s |= 4;
3963
3964                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3965                         rc= -EIO;
3966                 else
3967                         rc = 0;
3968                 break;
3969
3970         default:
3971                 rc = -ENXIO;
3972         }
3973
3974         mutex_unlock(&fan_mutex);
3975         return rc;
3976 }
3977
3978 static int fan_set_disable(void)
3979 {
3980         int rc;
3981
3982         if (!fan_control_allowed)
3983                 return -EPERM;
3984
3985         rc = mutex_lock_interruptible(&fan_mutex);
3986         if (rc < 0)
3987                 return rc;
3988
3989         rc = 0;
3990         switch (fan_control_access_mode) {
3991         case TPACPI_FAN_WR_ACPI_FANS:
3992         case TPACPI_FAN_WR_TPEC:
3993                 if (!acpi_ec_write(fan_status_offset, 0x00))
3994                         rc = -EIO;
3995                 else {
3996                         fan_control_desired_level = 0;
3997                         tp_features.fan_ctrl_status_undef = 0;
3998                 }
3999                 break;
4000
4001         case TPACPI_FAN_WR_ACPI_SFAN:
4002                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4003                         rc = -EIO;
4004                 else
4005                         fan_control_desired_level = 0;
4006                 break;
4007
4008         default:
4009                 rc = -ENXIO;
4010         }
4011
4012
4013         mutex_unlock(&fan_mutex);
4014         return rc;
4015 }
4016
4017 static int fan_set_speed(int speed)
4018 {
4019         int rc;
4020
4021         if (!fan_control_allowed)
4022                 return -EPERM;
4023
4024         rc = mutex_lock_interruptible(&fan_mutex);
4025         if (rc < 0)
4026                 return rc;
4027
4028         rc = 0;
4029         switch (fan_control_access_mode) {
4030         case TPACPI_FAN_WR_ACPI_FANS:
4031                 if (speed >= 0 && speed <= 65535) {
4032                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4033                                         speed, speed, speed))
4034                                 rc = -EIO;
4035                 } else
4036                         rc = -EINVAL;
4037                 break;
4038
4039         default:
4040                 rc = -ENXIO;
4041         }
4042
4043         mutex_unlock(&fan_mutex);
4044         return rc;
4045 }
4046
4047 static int fan_read(char *p)
4048 {
4049         int len = 0;
4050         int rc;
4051         u8 status;
4052         unsigned int speed = 0;
4053
4054         switch (fan_status_access_mode) {
4055         case TPACPI_FAN_RD_ACPI_GFAN:
4056                 /* 570, 600e/x, 770e, 770x */
4057                 if ((rc = fan_get_status_safe(&status)) < 0)
4058                         return rc;
4059
4060                 len += sprintf(p + len, "status:\t\t%s\n"
4061                                "level:\t\t%d\n",
4062                                (status != 0) ? "enabled" : "disabled", status);
4063                 break;
4064
4065         case TPACPI_FAN_RD_TPEC:
4066                 /* all except 570, 600e/x, 770e, 770x */
4067                 if ((rc = fan_get_status_safe(&status)) < 0)
4068                         return rc;
4069
4070                 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4071                         if (status != fan_control_initial_status)
4072                                 tp_features.fan_ctrl_status_undef = 0;
4073                         else
4074                                 /* Return most likely status. In fact, it
4075                                  * might be the only possible status */
4076                                 status = TP_EC_FAN_AUTO;
4077                 }
4078
4079                 len += sprintf(p + len, "status:\t\t%s\n",
4080                                (status != 0) ? "enabled" : "disabled");
4081
4082                 if ((rc = fan_get_speed(&speed)) < 0)
4083                         return rc;
4084
4085                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4086
4087                 if (status & TP_EC_FAN_FULLSPEED)
4088                         /* Disengaged mode takes precedence */
4089                         len += sprintf(p + len, "level:\t\tdisengaged\n");
4090                 else if (status & TP_EC_FAN_AUTO)
4091                         len += sprintf(p + len, "level:\t\tauto\n");
4092                 else
4093                         len += sprintf(p + len, "level:\t\t%d\n", status);
4094                 break;
4095
4096         case TPACPI_FAN_NONE:
4097         default:
4098                 len += sprintf(p + len, "status:\t\tnot supported\n");
4099         }
4100
4101         if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4102                 len += sprintf(p + len, "commands:\tlevel <level>");
4103
4104                 switch (fan_control_access_mode) {
4105                 case TPACPI_FAN_WR_ACPI_SFAN:
4106                         len += sprintf(p + len, " (<level> is 0-7)\n");
4107                         break;
4108
4109                 default:
4110                         len += sprintf(p + len, " (<level> is 0-7, "
4111                                        "auto, disengaged, full-speed)\n");
4112                         break;
4113                 }
4114         }
4115
4116         if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4117                 len += sprintf(p + len, "commands:\tenable, disable\n"
4118                                "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4119                                "1-120 (seconds))\n");
4120
4121         if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4122                 len += sprintf(p + len, "commands:\tspeed <speed>"
4123                                " (<speed> is 0-65535)\n");
4124
4125         return len;
4126 }
4127
4128 static int fan_write_cmd_level(const char *cmd, int *rc)
4129 {
4130         int level;
4131
4132         if (strlencmp(cmd, "level auto") == 0)
4133                 level = TP_EC_FAN_AUTO;
4134         else if ((strlencmp(cmd, "level disengaged") == 0) |
4135                  (strlencmp(cmd, "level full-speed") == 0))
4136                 level = TP_EC_FAN_FULLSPEED;
4137         else if (sscanf(cmd, "level %d", &level) != 1)
4138                 return 0;
4139
4140         if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4141                 printk(IBM_ERR "level command accepted for unsupported "
4142                        "access mode %d", fan_control_access_mode);
4143
4144         return 1;
4145 }
4146
4147 static int fan_write_cmd_enable(const char *cmd, int *rc)
4148 {
4149         if (strlencmp(cmd, "enable") != 0)
4150                 return 0;
4151
4152         if ((*rc = fan_set_enable()) == -ENXIO)
4153                 printk(IBM_ERR "enable command accepted for unsupported "
4154                        "access mode %d", fan_control_access_mode);
4155
4156         return 1;
4157 }
4158
4159 static int fan_write_cmd_disable(const char *cmd, int *rc)
4160 {
4161         if (strlencmp(cmd, "disable") != 0)
4162                 return 0;
4163
4164         if ((*rc = fan_set_disable()) == -ENXIO)
4165                 printk(IBM_ERR "disable command accepted for unsupported "
4166                        "access mode %d", fan_control_access_mode);
4167
4168         return 1;
4169 }
4170
4171 static int fan_write_cmd_speed(const char *cmd, int *rc)
4172 {
4173         int speed;
4174
4175         /* TODO:
4176          * Support speed <low> <medium> <high> ? */
4177
4178         if (sscanf(cmd, "speed %d", &speed) != 1)
4179                 return 0;
4180
4181         if ((*rc = fan_set_speed(speed)) == -ENXIO)
4182                 printk(IBM_ERR "speed command accepted for unsupported "
4183                        "access mode %d", fan_control_access_mode);
4184
4185         return 1;
4186 }
4187
4188 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4189 {
4190         int interval;
4191
4192         if (sscanf(cmd, "watchdog %d", &interval) != 1)
4193                 return 0;
4194
4195         if (interval < 0 || interval > 120)
4196                 *rc = -EINVAL;
4197         else
4198                 fan_watchdog_maxinterval = interval;
4199
4200         return 1;
4201 }
4202
4203 static int fan_write(char *buf)
4204 {
4205         char *cmd;
4206         int rc = 0;
4207
4208         while (!rc && (cmd = next_cmd(&buf))) {
4209                 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4210                       fan_write_cmd_level(cmd, &rc)) &&
4211                     !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4212                       (fan_write_cmd_enable(cmd, &rc) ||
4213                        fan_write_cmd_disable(cmd, &rc) ||
4214                        fan_write_cmd_watchdog(cmd, &rc))) &&
4215                     !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4216                       fan_write_cmd_speed(cmd, &rc))
4217                     )
4218                         rc = -EINVAL;
4219                 else if (!rc)
4220                         fan_watchdog_reset();
4221         }
4222
4223         return rc;
4224 }
4225
4226 static struct ibm_struct fan_driver_data = {
4227         .name = "fan",
4228         .read = fan_read,
4229         .write = fan_write,
4230         .exit = fan_exit,
4231 };
4232
4233 /****************************************************************************
4234  ****************************************************************************
4235  *
4236  * Infrastructure
4237  *
4238  ****************************************************************************
4239  ****************************************************************************/
4240
4241 /* /proc support */
4242 static struct proc_dir_entry *proc_dir;
4243
4244 /* Subdriver registry */
4245 static LIST_HEAD(tpacpi_all_drivers);
4246
4247
4248 /*
4249  * Module and infrastructure proble, init and exit handling
4250  */
4251
4252 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4253 static const char * __init str_supported(int is_supported)
4254 {
4255         static char text_unsupported[] __initdata = "not supported";
4256
4257         return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4258 }
4259 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4260
4261 static int __init ibm_init(struct ibm_init_struct *iibm)
4262 {
4263         int ret;
4264         struct ibm_struct *ibm = iibm->data;
4265         struct proc_dir_entry *entry;
4266
4267         BUG_ON(ibm == NULL);
4268
4269         INIT_LIST_HEAD(&ibm->all_drivers);
4270
4271         if (ibm->flags.experimental && !experimental)
4272                 return 0;
4273
4274         dbg_printk(TPACPI_DBG_INIT,
4275                 "probing for %s\n", ibm->name);
4276
4277         if (iibm->init) {
4278                 ret = iibm->init(iibm);
4279                 if (ret > 0)
4280                         return 0;       /* probe failed */
4281                 if (ret)
4282                         return ret;
4283
4284                 ibm->flags.init_called = 1;
4285         }
4286
4287         if (ibm->acpi) {
4288                 if (ibm->acpi->hid) {
4289                         ret = register_tpacpi_subdriver(ibm);
4290                         if (ret)
4291                                 goto err_out;
4292                 }
4293
4294                 if (ibm->acpi->notify) {
4295                         ret = setup_acpi_notify(ibm);
4296                         if (ret == -ENODEV) {
4297                                 printk(IBM_NOTICE "disabling subdriver %s\n",
4298                                         ibm->name);
4299                                 ret = 0;
4300                                 goto err_out;
4301                         }
4302                         if (ret < 0)
4303                                 goto err_out;
4304                 }
4305         }
4306
4307         dbg_printk(TPACPI_DBG_INIT,
4308                 "%s installed\n", ibm->name);
4309
4310         if (ibm->read) {
4311                 entry = create_proc_entry(ibm->name,
4312                                           S_IFREG | S_IRUGO | S_IWUSR,
4313                                           proc_dir);
4314                 if (!entry) {
4315                         printk(IBM_ERR "unable to create proc entry %s\n",
4316                                ibm->name);
4317                         ret = -ENODEV;
4318                         goto err_out;
4319                 }
4320                 entry->owner = THIS_MODULE;
4321                 entry->data = ibm;
4322                 entry->read_proc = &dispatch_procfs_read;
4323                 if (ibm->write)
4324                         entry->write_proc = &dispatch_procfs_write;
4325                 ibm->flags.proc_created = 1;
4326         }
4327
4328         list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4329
4330         return 0;
4331
4332 err_out:
4333         dbg_printk(TPACPI_DBG_INIT,
4334                 "%s: at error exit path with result %d\n",
4335                 ibm->name, ret);
4336
4337         ibm_exit(ibm);
4338         return (ret < 0)? ret : 0;
4339 }
4340
4341 static void ibm_exit(struct ibm_struct *ibm)
4342 {
4343         dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4344
4345         list_del_init(&ibm->all_drivers);
4346
4347         if (ibm->flags.acpi_notify_installed) {
4348                 dbg_printk(TPACPI_DBG_EXIT,
4349                         "%s: acpi_remove_notify_handler\n", ibm->name);
4350                 BUG_ON(!ibm->acpi);
4351                 acpi_remove_notify_handler(*ibm->acpi->handle,
4352                                            ibm->acpi->type,
4353                                            dispatch_acpi_notify);
4354                 ibm->flags.acpi_notify_installed = 0;
4355                 ibm->flags.acpi_notify_installed = 0;
4356         }
4357
4358         if (ibm->flags.proc_created) {
4359                 dbg_printk(TPACPI_DBG_EXIT,
4360                         "%s: remove_proc_entry\n", ibm->name);
4361                 remove_proc_entry(ibm->name, proc_dir);
4362                 ibm->flags.proc_created = 0;
4363         }
4364
4365         if (ibm->flags.acpi_driver_registered) {
4366                 dbg_printk(TPACPI_DBG_EXIT,
4367                         "%s: acpi_bus_unregister_driver\n", ibm->name);
4368                 BUG_ON(!ibm->acpi);
4369                 acpi_bus_unregister_driver(ibm->acpi->driver);
4370                 kfree(ibm->acpi->driver);
4371                 ibm->acpi->driver = NULL;
4372                 ibm->flags.acpi_driver_registered = 0;
4373         }
4374
4375         if (ibm->flags.init_called && ibm->exit) {
4376                 ibm->exit();
4377                 ibm->flags.init_called = 0;
4378         }
4379
4380         dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4381 }
4382
4383 /* Probing */
4384
4385 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4386 {
4387         struct dmi_device *dev = NULL;
4388         char ec_fw_string[18];
4389
4390         if (!tp)
4391                 return;
4392
4393         memset(tp, 0, sizeof(*tp));
4394
4395         if (dmi_name_in_vendors("IBM"))
4396                 tp->vendor = PCI_VENDOR_ID_IBM;
4397         else if (dmi_name_in_vendors("LENOVO"))
4398                 tp->vendor = PCI_VENDOR_ID_LENOVO;
4399         else
4400                 return;
4401
4402         tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4403                                         GFP_KERNEL);
4404         if (!tp->bios_version_str)
4405                 return;
4406         tp->bios_model = tp->bios_version_str[0]
4407                          | (tp->bios_version_str[1] << 8);
4408
4409         /*
4410          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4411          * X32 or newer, all Z series;  Some models must have an
4412          * up-to-date BIOS or they will not be detected.
4413          *
4414          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4415          */
4416         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4417                 if (sscanf(dev->name,
4418                            "IBM ThinkPad Embedded Controller -[%17c",
4419                            ec_fw_string) == 1) {
4420                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4421                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4422
4423                         tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4424                         tp->ec_model = ec_fw_string[0]
4425                                         | (ec_fw_string[1] << 8);
4426                         break;
4427                 }
4428         }
4429
4430         tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4431                                         GFP_KERNEL);
4432         if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4433                 kfree(tp->model_str);
4434                 tp->model_str = NULL;
4435         }
4436 }
4437
4438 static int __init probe_for_thinkpad(void)
4439 {
4440         int is_thinkpad;
4441
4442         if (acpi_disabled)
4443                 return -ENODEV;
4444
4445         /*
4446          * Non-ancient models have better DMI tagging, but very old models
4447          * don't.
4448          */
4449         is_thinkpad = (thinkpad_id.model_str != NULL);
4450
4451         /* ec is required because many other handles are relative to it */
4452         IBM_ACPIHANDLE_INIT(ec);
4453         if (!ec_handle) {
4454                 if (is_thinkpad)
4455                         printk(IBM_ERR
4456                                 "Not yet supported ThinkPad detected!\n");
4457                 return -ENODEV;
4458         }
4459
4460         /*
4461          * Risks a regression on very old machines, but reduces potential
4462          * false positives a damn great deal
4463          */
4464         if (!is_thinkpad)
4465                 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4466
4467         if (!is_thinkpad && !force_load)
4468                 return -ENODEV;
4469
4470         return 0;
4471 }
4472
4473
4474 /* Module init, exit, parameters */
4475
4476 static struct ibm_init_struct ibms_init[] __initdata = {
4477         {
4478                 .init = thinkpad_acpi_driver_init,
4479                 .data = &thinkpad_acpi_driver_data,
4480         },
4481         {
4482                 .init = hotkey_init,
4483                 .data = &hotkey_driver_data,
4484         },
4485         {
4486                 .init = bluetooth_init,
4487                 .data = &bluetooth_driver_data,
4488         },
4489         {
4490                 .init = wan_init,
4491                 .data = &wan_driver_data,
4492         },
4493         {
4494                 .init = video_init,
4495                 .data = &video_driver_data,
4496         },
4497         {
4498                 .init = light_init,
4499                 .data = &light_driver_data,
4500         },
4501 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4502         {
4503                 .init = dock_init,
4504                 .data = &dock_driver_data[0],
4505         },
4506         {
4507                 .init = dock_init2,
4508                 .data = &dock_driver_data[1],
4509         },
4510 #endif
4511 #ifdef CONFIG_THINKPAD_ACPI_BAY
4512         {
4513                 .init = bay_init,
4514                 .data = &bay_driver_data,
4515         },
4516 #endif
4517         {
4518                 .init = cmos_init,
4519                 .data = &cmos_driver_data,
4520         },
4521         {
4522                 .init = led_init,
4523                 .data = &led_driver_data,
4524         },
4525         {
4526                 .init = beep_init,
4527                 .data = &beep_driver_data,
4528         },
4529         {
4530                 .init = thermal_init,
4531                 .data = &thermal_driver_data,
4532         },
4533         {
4534                 .data = &ecdump_driver_data,
4535         },
4536         {
4537                 .init = brightness_init,
4538                 .data = &brightness_driver_data,
4539         },
4540         {
4541                 .data = &volume_driver_data,
4542         },
4543         {
4544                 .init = fan_init,
4545                 .data = &fan_driver_data,
4546         },
4547 };
4548
4549 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4550 {
4551         unsigned int i;
4552         struct ibm_struct *ibm;
4553
4554         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4555                 ibm = ibms_init[i].data;
4556                 BUG_ON(ibm == NULL);
4557
4558                 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4559                         if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4560                                 return -ENOSPC;
4561                         strcpy(ibms_init[i].param, val);
4562                         strcat(ibms_init[i].param, ",");
4563                         return 0;
4564                 }
4565         }
4566
4567         return -EINVAL;
4568 }
4569
4570 static int experimental;
4571 module_param(experimental, int, 0);
4572
4573 static u32 dbg_level;
4574 module_param_named(debug, dbg_level, uint, 0);
4575
4576 static int force_load;
4577 module_param(force_load, bool, 0);
4578
4579 static int fan_control_allowed;
4580 module_param_named(fan_control, fan_control_allowed, bool, 0);
4581
4582 static int brightness_mode;
4583 module_param_named(brightness_mode, brightness_mode, int, 0);
4584
4585 #define IBM_PARAM(feature) \
4586         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4587
4588 IBM_PARAM(hotkey);
4589 IBM_PARAM(bluetooth);
4590 IBM_PARAM(video);
4591 IBM_PARAM(light);
4592 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4593 IBM_PARAM(dock);
4594 #endif
4595 #ifdef CONFIG_THINKPAD_ACPI_BAY
4596 IBM_PARAM(bay);
4597 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4598 IBM_PARAM(cmos);
4599 IBM_PARAM(led);
4600 IBM_PARAM(beep);
4601 IBM_PARAM(ecdump);
4602 IBM_PARAM(brightness);
4603 IBM_PARAM(volume);
4604 IBM_PARAM(fan);
4605
4606 static int __init thinkpad_acpi_module_init(void)
4607 {
4608         int ret, i;
4609
4610         /* Driver-level probe */
4611
4612         get_thinkpad_model_data(&thinkpad_id);
4613         ret = probe_for_thinkpad();
4614         if (ret) {
4615                 thinkpad_acpi_module_exit();
4616                 return ret;
4617         }
4618
4619         /* Driver initialization */
4620
4621         IBM_ACPIHANDLE_INIT(ecrd);
4622         IBM_ACPIHANDLE_INIT(ecwr);
4623
4624         proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4625         if (!proc_dir) {
4626                 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4627                 thinkpad_acpi_module_exit();
4628                 return -ENODEV;
4629         }
4630         proc_dir->owner = THIS_MODULE;
4631
4632         ret = platform_driver_register(&tpacpi_pdriver);
4633         if (ret) {
4634                 printk(IBM_ERR "unable to register platform driver\n");
4635                 thinkpad_acpi_module_exit();
4636                 return ret;
4637         }
4638         ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4639         if (ret) {
4640                 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4641                 thinkpad_acpi_module_exit();
4642                 return ret;
4643         }
4644
4645
4646         /* Device initialization */
4647         tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4648                                                         NULL, 0);
4649         if (IS_ERR(tpacpi_pdev)) {
4650                 ret = PTR_ERR(tpacpi_pdev);
4651                 tpacpi_pdev = NULL;
4652                 printk(IBM_ERR "unable to register platform device\n");
4653                 thinkpad_acpi_module_exit();
4654                 return ret;
4655         }
4656         tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4657         if (IS_ERR(tpacpi_hwmon)) {
4658                 ret = PTR_ERR(tpacpi_hwmon);
4659                 tpacpi_hwmon = NULL;
4660                 printk(IBM_ERR "unable to register hwmon device\n");
4661                 thinkpad_acpi_module_exit();
4662                 return ret;
4663         }
4664         tpacpi_inputdev = input_allocate_device();
4665         if (!tpacpi_inputdev) {
4666                 printk(IBM_ERR "unable to allocate input device\n");
4667                 thinkpad_acpi_module_exit();
4668                 return -ENOMEM;
4669         } else {
4670                 /* Prepare input device, but don't register */
4671                 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4672                 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4673                 tpacpi_inputdev->id.bustype = BUS_HOST;
4674                 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4675                                                 thinkpad_id.vendor :
4676                                                 PCI_VENDOR_ID_IBM;
4677                 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4678                 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4679         }
4680         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4681                 ret = ibm_init(&ibms_init[i]);
4682                 if (ret >= 0 && *ibms_init[i].param)
4683                         ret = ibms_init[i].data->write(ibms_init[i].param);
4684                 if (ret < 0) {
4685                         thinkpad_acpi_module_exit();
4686                         return ret;
4687                 }
4688         }
4689         ret = input_register_device(tpacpi_inputdev);
4690         if (ret < 0) {
4691                 printk(IBM_ERR "unable to register input device\n");
4692                 thinkpad_acpi_module_exit();
4693                 return ret;
4694         } else {
4695                 tp_features.input_device_registered = 1;
4696         }
4697
4698         return 0;
4699 }
4700
4701 static void thinkpad_acpi_module_exit(void)
4702 {
4703         struct ibm_struct *ibm, *itmp;
4704
4705         list_for_each_entry_safe_reverse(ibm, itmp,
4706                                          &tpacpi_all_drivers,
4707                                          all_drivers) {
4708                 ibm_exit(ibm);
4709         }
4710
4711         dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4712
4713         if (tpacpi_inputdev) {
4714                 if (tp_features.input_device_registered)
4715                         input_unregister_device(tpacpi_inputdev);
4716                 else
4717                         input_free_device(tpacpi_inputdev);
4718         }
4719
4720         if (tpacpi_hwmon)
4721                 hwmon_device_unregister(tpacpi_hwmon);
4722
4723         if (tpacpi_pdev)
4724                 platform_device_unregister(tpacpi_pdev);
4725
4726         tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4727         platform_driver_unregister(&tpacpi_pdriver);
4728
4729         if (proc_dir)
4730                 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4731
4732         kfree(thinkpad_id.bios_version_str);
4733         kfree(thinkpad_id.ec_version_str);
4734         kfree(thinkpad_id.model_str);
4735 }
4736
4737 module_init(thinkpad_acpi_module_init);
4738 module_exit(thinkpad_acpi_module_exit);