ACPI: thinkpad-acpi: protect fan and hotkey data structures
[pandora-kernel.git] / 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 0x000100
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 #define __unused __attribute__ ((unused))
96
97 /****************************************************************************
98  ****************************************************************************
99  *
100  * ACPI Helpers and device model
101  *
102  ****************************************************************************
103  ****************************************************************************/
104
105 /*************************************************************************
106  * ACPI basic handles
107  */
108
109 static acpi_handle root_handle = NULL;
110
111 #define IBM_HANDLE(object, parent, paths...)                    \
112         static acpi_handle  object##_handle;                    \
113         static acpi_handle *object##_parent = &parent##_handle; \
114         static char        *object##_path;                      \
115         static char        *object##_paths[] = { paths }
116
117 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
118            "\\_SB.PCI.ISA.EC",  /* 570 */
119            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
120            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
121            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
122            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
123            "\\_SB.PCI0.LPC.EC", /* all others */
124            );
125
126 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
127 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
128
129
130 /*************************************************************************
131  * Misc ACPI handles
132  */
133
134 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
135            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
136            "\\CMS",             /* R40, R40e */
137            );                   /* all others */
138
139 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
140            "^HKEY",             /* R30, R31 */
141            "HKEY",              /* all others */
142            );                   /* 570 */
143
144
145 /*************************************************************************
146  * ACPI helpers
147  */
148
149 static int acpi_evalf(acpi_handle handle,
150                       void *res, char *method, char *fmt, ...)
151 {
152         char *fmt0 = fmt;
153         struct acpi_object_list params;
154         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
155         struct acpi_buffer result, *resultp;
156         union acpi_object out_obj;
157         acpi_status status;
158         va_list ap;
159         char res_type;
160         int success;
161         int quiet;
162
163         if (!*fmt) {
164                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
165                 return 0;
166         }
167
168         if (*fmt == 'q') {
169                 quiet = 1;
170                 fmt++;
171         } else
172                 quiet = 0;
173
174         res_type = *(fmt++);
175
176         params.count = 0;
177         params.pointer = &in_objs[0];
178
179         va_start(ap, fmt);
180         while (*fmt) {
181                 char c = *(fmt++);
182                 switch (c) {
183                 case 'd':       /* int */
184                         in_objs[params.count].integer.value = va_arg(ap, int);
185                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
186                         break;
187                         /* add more types as needed */
188                 default:
189                         printk(IBM_ERR "acpi_evalf() called "
190                                "with invalid format character '%c'\n", c);
191                         return 0;
192                 }
193         }
194         va_end(ap);
195
196         if (res_type != 'v') {
197                 result.length = sizeof(out_obj);
198                 result.pointer = &out_obj;
199                 resultp = &result;
200         } else
201                 resultp = NULL;
202
203         status = acpi_evaluate_object(handle, method, &params, resultp);
204
205         switch (res_type) {
206         case 'd':               /* int */
207                 if (res)
208                         *(int *)res = out_obj.integer.value;
209                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
210                 break;
211         case 'v':               /* void */
212                 success = status == AE_OK;
213                 break;
214                 /* add more types as needed */
215         default:
216                 printk(IBM_ERR "acpi_evalf() called "
217                        "with invalid format character '%c'\n", res_type);
218                 return 0;
219         }
220
221         if (!success && !quiet)
222                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
223                        method, fmt0, status);
224
225         return success;
226 }
227
228 static void __unused acpi_print_int(acpi_handle handle, char *method)
229 {
230         int i;
231
232         if (acpi_evalf(handle, &i, method, "d"))
233                 printk(IBM_INFO "%s = 0x%x\n", method, i);
234         else
235                 printk(IBM_ERR "error calling %s\n", method);
236 }
237
238 static int acpi_ec_read(int i, u8 * p)
239 {
240         int v;
241
242         if (ecrd_handle) {
243                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
244                         return 0;
245                 *p = v;
246         } else {
247                 if (ec_read(i, p) < 0)
248                         return 0;
249         }
250
251         return 1;
252 }
253
254 static int acpi_ec_write(int i, u8 v)
255 {
256         if (ecwr_handle) {
257                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
258                         return 0;
259         } else {
260                 if (ec_write(i, v) < 0)
261                         return 0;
262         }
263
264         return 1;
265 }
266
267 static int _sta(acpi_handle handle)
268 {
269         int status;
270
271         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
272                 status = 0;
273
274         return status;
275 }
276
277 static int issue_thinkpad_cmos_command(int cmos_cmd)
278 {
279         if (!cmos_handle)
280                 return -ENXIO;
281
282         if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
283                 return -EIO;
284
285         return 0;
286 }
287
288 /*************************************************************************
289  * ACPI device model
290  */
291
292 static void drv_acpi_handle_init(char *name,
293                            acpi_handle *handle, acpi_handle parent,
294                            char **paths, int num_paths, char **path)
295 {
296         int i;
297         acpi_status status;
298
299         for (i = 0; i < num_paths; i++) {
300                 status = acpi_get_handle(parent, paths[i], handle);
301                 if (ACPI_SUCCESS(status)) {
302                         *path = paths[i];
303                         return;
304                 }
305         }
306
307         *handle = NULL;
308 }
309
310 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
311 {
312         struct ibm_struct *ibm = data;
313
314         if (!ibm || !ibm->acpi || !ibm->acpi->notify)
315                 return;
316
317         ibm->acpi->notify(ibm, event);
318 }
319
320 static int __init setup_acpi_notify(struct ibm_struct *ibm)
321 {
322         acpi_status status;
323         int ret;
324
325         BUG_ON(!ibm->acpi);
326
327         if (!*ibm->acpi->handle)
328                 return 0;
329
330         dbg_printk(TPACPI_DBG_INIT,
331                 "setting up ACPI notify for %s\n", ibm->name);
332
333         ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
334         if (ret < 0) {
335                 printk(IBM_ERR "%s device not present\n", ibm->name);
336                 return -ENODEV;
337         }
338
339         acpi_driver_data(ibm->acpi->device) = ibm;
340         sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
341                 IBM_ACPI_EVENT_PREFIX,
342                 ibm->name);
343
344         status = acpi_install_notify_handler(*ibm->acpi->handle,
345                         ibm->acpi->type, dispatch_acpi_notify, ibm);
346         if (ACPI_FAILURE(status)) {
347                 if (status == AE_ALREADY_EXISTS) {
348                         printk(IBM_NOTICE "another device driver is already handling %s events\n",
349                                 ibm->name);
350                 } else {
351                         printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
352                                 ibm->name, status);
353                 }
354                 return -ENODEV;
355         }
356         ibm->flags.acpi_notify_installed = 1;
357         return 0;
358 }
359
360 static int __init tpacpi_device_add(struct acpi_device *device)
361 {
362         return 0;
363 }
364
365 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
366 {
367         int ret;
368
369         dbg_printk(TPACPI_DBG_INIT,
370                 "registering %s as an ACPI driver\n", ibm->name);
371
372         BUG_ON(!ibm->acpi);
373
374         ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
375         if (!ibm->acpi->driver) {
376                 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
377                 return -ENOMEM;
378         }
379
380         sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
381         ibm->acpi->driver->ids = ibm->acpi->hid;
382         ibm->acpi->driver->ops.add = &tpacpi_device_add;
383
384         ret = acpi_bus_register_driver(ibm->acpi->driver);
385         if (ret < 0) {
386                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
387                        ibm->acpi->hid, ret);
388                 kfree(ibm->acpi->driver);
389                 ibm->acpi->driver = NULL;
390         } else if (!ret)
391                 ibm->flags.acpi_driver_registered = 1;
392
393         return ret;
394 }
395
396
397 /****************************************************************************
398  ****************************************************************************
399  *
400  * Procfs Helpers
401  *
402  ****************************************************************************
403  ****************************************************************************/
404
405 static int dispatch_procfs_read(char *page, char **start, off_t off,
406                         int count, int *eof, void *data)
407 {
408         struct ibm_struct *ibm = data;
409         int len;
410
411         if (!ibm || !ibm->read)
412                 return -EINVAL;
413
414         len = ibm->read(page);
415         if (len < 0)
416                 return len;
417
418         if (len <= off + count)
419                 *eof = 1;
420         *start = page + off;
421         len -= off;
422         if (len > count)
423                 len = count;
424         if (len < 0)
425                 len = 0;
426
427         return len;
428 }
429
430 static int dispatch_procfs_write(struct file *file,
431                         const char __user * userbuf,
432                         unsigned long count, void *data)
433 {
434         struct ibm_struct *ibm = data;
435         char *kernbuf;
436         int ret;
437
438         if (!ibm || !ibm->write)
439                 return -EINVAL;
440
441         kernbuf = kmalloc(count + 2, GFP_KERNEL);
442         if (!kernbuf)
443                 return -ENOMEM;
444
445         if (copy_from_user(kernbuf, userbuf, count)) {
446                 kfree(kernbuf);
447                 return -EFAULT;
448         }
449
450         kernbuf[count] = 0;
451         strcat(kernbuf, ",");
452         ret = ibm->write(kernbuf);
453         if (ret == 0)
454                 ret = count;
455
456         kfree(kernbuf);
457
458         return ret;
459 }
460
461 static char *next_cmd(char **cmds)
462 {
463         char *start = *cmds;
464         char *end;
465
466         while ((end = strchr(start, ',')) && end == start)
467                 start = end + 1;
468
469         if (!end)
470                 return NULL;
471
472         *end = 0;
473         *cmds = end + 1;
474         return start;
475 }
476
477
478 /****************************************************************************
479  ****************************************************************************
480  *
481  * Device model: hwmon and platform
482  *
483  ****************************************************************************
484  ****************************************************************************/
485
486 static struct platform_device *tpacpi_pdev = NULL;
487 static struct class_device *tpacpi_hwmon = NULL;
488
489 static struct platform_driver tpacpi_pdriver = {
490         .driver = {
491                 .name = IBM_DRVR_NAME,
492                 .owner = THIS_MODULE,
493         },
494 };
495
496
497 /*************************************************************************
498  * thinkpad-acpi driver attributes
499  */
500
501 /* interface_version --------------------------------------------------- */
502 static ssize_t tpacpi_driver_interface_version_show(
503                                 struct device_driver *drv,
504                                 char *buf)
505 {
506         return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
507 }
508
509 static DRIVER_ATTR(interface_version, S_IRUGO,
510                 tpacpi_driver_interface_version_show, NULL);
511
512 /* debug_level --------------------------------------------------------- */
513 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
514                                                 char *buf)
515 {
516         return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
517 }
518
519 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
520                                                 const char *buf, size_t count)
521 {
522         unsigned long t;
523
524         if (parse_strtoul(buf, 0xffff, &t))
525                 return -EINVAL;
526
527         dbg_level = t;
528
529         return count;
530 }
531
532 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
533                 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
534
535 /* version ------------------------------------------------------------- */
536 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
537                                                 char *buf)
538 {
539         return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
540 }
541
542 static DRIVER_ATTR(version, S_IRUGO,
543                 tpacpi_driver_version_show, NULL);
544
545 /* --------------------------------------------------------------------- */
546
547 static struct driver_attribute* tpacpi_driver_attributes[] = {
548         &driver_attr_debug_level, &driver_attr_version,
549         &driver_attr_interface_version,
550 };
551
552 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
553 {
554         int i, res;
555
556         i = 0;
557         res = 0;
558         while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
559                 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
560                 i++;
561         }
562
563         return res;
564 }
565
566 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
567 {
568         int i;
569
570         for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
571                 driver_remove_file(drv, tpacpi_driver_attributes[i]);
572 }
573
574 /*************************************************************************
575  * sysfs support helpers
576  */
577
578 struct attribute_set_obj {
579         struct attribute_set s;
580         struct attribute *a;
581 } __attribute__((packed));
582
583 static struct attribute_set *create_attr_set(unsigned int max_members,
584                                                 const char* name)
585 {
586         struct attribute_set_obj *sobj;
587
588         if (max_members == 0)
589                 return NULL;
590
591         /* Allocates space for implicit NULL at the end too */
592         sobj = kzalloc(sizeof(struct attribute_set_obj) +
593                     max_members * sizeof(struct attribute *),
594                     GFP_KERNEL);
595         if (!sobj)
596                 return NULL;
597         sobj->s.max_members = max_members;
598         sobj->s.group.attrs = &sobj->a;
599         sobj->s.group.name = name;
600
601         return &sobj->s;
602 }
603
604 /* not multi-threaded safe, use it in a single thread per set */
605 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
606 {
607         if (!s || !attr)
608                 return -EINVAL;
609
610         if (s->members >= s->max_members)
611                 return -ENOMEM;
612
613         s->group.attrs[s->members] = attr;
614         s->members++;
615
616         return 0;
617 }
618
619 static int add_many_to_attr_set(struct attribute_set* s,
620                         struct attribute **attr,
621                         unsigned int count)
622 {
623         int i, res;
624
625         for (i = 0; i < count; i++) {
626                 res = add_to_attr_set(s, attr[i]);
627                 if (res)
628                         return res;
629         }
630
631         return 0;
632 }
633
634 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
635 {
636         sysfs_remove_group(kobj, &s->group);
637         destroy_attr_set(s);
638 }
639
640 static int parse_strtoul(const char *buf,
641                 unsigned long max, unsigned long *value)
642 {
643         char *endp;
644
645         *value = simple_strtoul(buf, &endp, 0);
646         while (*endp && isspace(*endp))
647                 endp++;
648         if (*endp || *value > max)
649                 return -EINVAL;
650
651         return 0;
652 }
653
654 /****************************************************************************
655  ****************************************************************************
656  *
657  * Subdrivers
658  *
659  ****************************************************************************
660  ****************************************************************************/
661
662 /*************************************************************************
663  * thinkpad-acpi init subdriver
664  */
665
666 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
667 {
668         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
669         printk(IBM_INFO "%s\n", IBM_URL);
670
671         if (ibm_thinkpad_ec_found)
672                 printk(IBM_INFO "ThinkPad EC firmware %s\n",
673                        ibm_thinkpad_ec_found);
674
675         return 0;
676 }
677
678 static int thinkpad_acpi_driver_read(char *p)
679 {
680         int len = 0;
681
682         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
683         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
684
685         return len;
686 }
687
688 static struct ibm_struct thinkpad_acpi_driver_data = {
689         .name = "driver",
690         .read = thinkpad_acpi_driver_read,
691 };
692
693 /*************************************************************************
694  * Hotkey subdriver
695  */
696
697 static int hotkey_orig_status;
698 static int hotkey_orig_mask;
699
700 static int __init hotkey_init(struct ibm_init_struct *iibm)
701 {
702         int res;
703
704         vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
705
706         IBM_ACPIHANDLE_INIT(hkey);
707         mutex_init(&hotkey_mutex);
708
709         /* hotkey not supported on 570 */
710         tp_features.hotkey = hkey_handle != NULL;
711
712         vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
713                 str_supported(tp_features.hotkey));
714
715         if (tp_features.hotkey) {
716                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
717                    A30, R30, R31, T20-22, X20-21, X22-24 */
718                 tp_features.hotkey_mask =
719                         acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
720
721                 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
722                         str_supported(tp_features.hotkey_mask));
723
724                 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
725                 if (res)
726                         return res;
727         }
728
729         return (tp_features.hotkey)? 0 : 1;
730 }
731
732 static void hotkey_exit(void)
733 {
734         int res;
735
736         if (tp_features.hotkey) {
737                 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
738                 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
739                 if (res)
740                         printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
741         }
742 }
743
744 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
745 {
746         int hkey;
747
748         if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
749                 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
750         else {
751                 printk(IBM_ERR "unknown hotkey event %d\n", event);
752                 acpi_bus_generate_event(ibm->acpi->device, event, 0);
753         }
754 }
755
756 /*
757  * Call with hotkey_mutex held
758  */
759 static int hotkey_get(int *status, int *mask)
760 {
761         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
762                 return -EIO;
763
764         if (tp_features.hotkey_mask)
765                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
766                         return -EIO;
767
768         return 0;
769 }
770
771 /*
772  * Call with hotkey_mutex held
773  */
774 static int hotkey_set(int status, int mask)
775 {
776         int i;
777
778         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
779                 return -EIO;
780
781         if (tp_features.hotkey_mask)
782                 for (i = 0; i < 32; i++) {
783                         int bit = ((1 << i) & mask) != 0;
784                         if (!acpi_evalf(hkey_handle,
785                                         NULL, "MHKM", "vdd", i + 1, bit))
786                                 return -EIO;
787                 }
788
789         return 0;
790 }
791
792 static int hotkey_read(char *p)
793 {
794         int res, status, mask;
795         int len = 0;
796
797         if (!tp_features.hotkey) {
798                 len += sprintf(p + len, "status:\t\tnot supported\n");
799                 return len;
800         }
801
802         res = mutex_lock_interruptible(&hotkey_mutex);
803         if (res < 0)
804                 return res;
805         res = hotkey_get(&status, &mask);
806         mutex_unlock(&hotkey_mutex);
807         if (res)
808                 return res;
809
810         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
811         if (tp_features.hotkey_mask) {
812                 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
813                 len += sprintf(p + len,
814                                "commands:\tenable, disable, reset, <mask>\n");
815         } else {
816                 len += sprintf(p + len, "mask:\t\tnot supported\n");
817                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
818         }
819
820         return len;
821 }
822
823 static int hotkey_write(char *buf)
824 {
825         int res, status, mask;
826         char *cmd;
827         int do_cmd = 0;
828
829         if (!tp_features.hotkey)
830                 return -ENODEV;
831
832         res = mutex_lock_interruptible(&hotkey_mutex);
833         if (res < 0)
834                 return res;
835
836         res = hotkey_get(&status, &mask);
837         if (res)
838                 goto errexit;
839
840         res = 0;
841         while ((cmd = next_cmd(&buf))) {
842                 if (strlencmp(cmd, "enable") == 0) {
843                         status = 1;
844                 } else if (strlencmp(cmd, "disable") == 0) {
845                         status = 0;
846                 } else if (strlencmp(cmd, "reset") == 0) {
847                         status = hotkey_orig_status;
848                         mask = hotkey_orig_mask;
849                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
850                         /* mask set */
851                 } else if (sscanf(cmd, "%x", &mask) == 1) {
852                         /* mask set */
853                 } else {
854                         res = -EINVAL;
855                         goto errexit;
856                 }
857                 do_cmd = 1;
858         }
859
860         if (do_cmd)
861                 res = hotkey_set(status, mask);
862
863 errexit:
864         mutex_unlock(&hotkey_mutex);
865         return res;
866 }
867
868 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
869         .hid = IBM_HKEY_HID,
870         .notify = hotkey_notify,
871         .handle = &hkey_handle,
872         .type = ACPI_DEVICE_NOTIFY,
873 };
874
875 static struct ibm_struct hotkey_driver_data = {
876         .name = "hotkey",
877         .read = hotkey_read,
878         .write = hotkey_write,
879         .exit = hotkey_exit,
880         .acpi = &ibm_hotkey_acpidriver,
881 };
882
883 /*************************************************************************
884  * Bluetooth subdriver
885  */
886
887 static int __init bluetooth_init(struct ibm_init_struct *iibm)
888 {
889         int status = 0;
890
891         vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
892
893         IBM_ACPIHANDLE_INIT(hkey);
894
895         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
896            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
897         tp_features.bluetooth = hkey_handle &&
898             acpi_evalf(hkey_handle, &status, "GBDC", "qd");
899
900         vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
901                 str_supported(tp_features.bluetooth),
902                 status);
903
904         if (tp_features.bluetooth &&
905             !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
906                 /* no bluetooth hardware present in system */
907                 tp_features.bluetooth = 0;
908                 dbg_printk(TPACPI_DBG_INIT,
909                            "bluetooth hardware not installed\n");
910         }
911
912         return (tp_features.bluetooth)? 0 : 1;
913 }
914
915 static int bluetooth_get_radiosw(void)
916 {
917         int status;
918
919         if (!tp_features.bluetooth)
920                 return -ENODEV;
921
922         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
923                 return -EIO;
924
925         return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
926 }
927
928 static int bluetooth_set_radiosw(int radio_on)
929 {
930         int status;
931
932         if (!tp_features.bluetooth)
933                 return -ENODEV;
934
935         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
936                 return -EIO;
937         if (radio_on)
938                 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
939         else
940                 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
941         if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
942                 return -EIO;
943
944         return 0;
945 }
946
947 static int bluetooth_read(char *p)
948 {
949         int len = 0;
950         int status = bluetooth_get_radiosw();
951
952         if (!tp_features.bluetooth)
953                 len += sprintf(p + len, "status:\t\tnot supported\n");
954         else {
955                 len += sprintf(p + len, "status:\t\t%s\n",
956                                 (status)? "enabled" : "disabled");
957                 len += sprintf(p + len, "commands:\tenable, disable\n");
958         }
959
960         return len;
961 }
962
963 static int bluetooth_write(char *buf)
964 {
965         char *cmd;
966
967         if (!tp_features.bluetooth)
968                 return -ENODEV;
969
970         while ((cmd = next_cmd(&buf))) {
971                 if (strlencmp(cmd, "enable") == 0) {
972                         bluetooth_set_radiosw(1);
973                 } else if (strlencmp(cmd, "disable") == 0) {
974                         bluetooth_set_radiosw(0);
975                 } else
976                         return -EINVAL;
977         }
978
979         return 0;
980 }
981
982 static struct ibm_struct bluetooth_driver_data = {
983         .name = "bluetooth",
984         .read = bluetooth_read,
985         .write = bluetooth_write,
986 };
987
988 /*************************************************************************
989  * Wan subdriver
990  */
991
992 static int __init wan_init(struct ibm_init_struct *iibm)
993 {
994         int status = 0;
995
996         vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
997
998         IBM_ACPIHANDLE_INIT(hkey);
999
1000         tp_features.wan = hkey_handle &&
1001             acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1002
1003         vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1004                 str_supported(tp_features.wan),
1005                 status);
1006
1007         if (tp_features.wan &&
1008             !(status & TP_ACPI_WANCARD_HWPRESENT)) {
1009                 /* no wan hardware present in system */
1010                 tp_features.wan = 0;
1011                 dbg_printk(TPACPI_DBG_INIT,
1012                            "wan hardware not installed\n");
1013         }
1014
1015         return (tp_features.wan)? 0 : 1;
1016 }
1017
1018 static int wan_get_radiosw(void)
1019 {
1020         int status;
1021
1022         if (!tp_features.wan)
1023                 return -ENODEV;
1024
1025         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1026                 return -EIO;
1027
1028         return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1029 }
1030
1031 static int wan_set_radiosw(int radio_on)
1032 {
1033         int status;
1034
1035         if (!tp_features.wan)
1036                 return -ENODEV;
1037
1038         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1039                 return -EIO;
1040         if (radio_on)
1041                 status |= TP_ACPI_WANCARD_RADIOSSW;
1042         else
1043                 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1044         if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1045                 return -EIO;
1046
1047         return 0;
1048 }
1049
1050 static int wan_read(char *p)
1051 {
1052         int len = 0;
1053         int status = wan_get_radiosw();
1054
1055         if (!tp_features.wan)
1056                 len += sprintf(p + len, "status:\t\tnot supported\n");
1057         else {
1058                 len += sprintf(p + len, "status:\t\t%s\n",
1059                                 (status)? "enabled" : "disabled");
1060                 len += sprintf(p + len, "commands:\tenable, disable\n");
1061         }
1062
1063         return len;
1064 }
1065
1066 static int wan_write(char *buf)
1067 {
1068         char *cmd;
1069
1070         if (!tp_features.wan)
1071                 return -ENODEV;
1072
1073         while ((cmd = next_cmd(&buf))) {
1074                 if (strlencmp(cmd, "enable") == 0) {
1075                         wan_set_radiosw(1);
1076                 } else if (strlencmp(cmd, "disable") == 0) {
1077                         wan_set_radiosw(0);
1078                 } else
1079                         return -EINVAL;
1080         }
1081
1082         return 0;
1083 }
1084
1085 static struct ibm_struct wan_driver_data = {
1086         .name = "wan",
1087         .read = wan_read,
1088         .write = wan_write,
1089         .flags.experimental = 1,
1090 };
1091
1092 /*************************************************************************
1093  * Video subdriver
1094  */
1095
1096 static enum video_access_mode video_supported;
1097 static int video_orig_autosw;
1098
1099 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
1100            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
1101            "\\_SB.PCI0.VID0",   /* 770e */
1102            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
1103            "\\_SB.PCI0.AGP.VID",        /* all others */
1104            );                           /* R30, R31 */
1105
1106 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
1107
1108 static int __init video_init(struct ibm_init_struct *iibm)
1109 {
1110         int ivga;
1111
1112         vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1113
1114         IBM_ACPIHANDLE_INIT(vid);
1115         IBM_ACPIHANDLE_INIT(vid2);
1116
1117         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1118                 /* G41, assume IVGA doesn't change */
1119                 vid_handle = vid2_handle;
1120
1121         if (!vid_handle)
1122                 /* video switching not supported on R30, R31 */
1123                 video_supported = TPACPI_VIDEO_NONE;
1124         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1125                 /* 570 */
1126                 video_supported = TPACPI_VIDEO_570;
1127         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1128                 /* 600e/x, 770e, 770x */
1129                 video_supported = TPACPI_VIDEO_770;
1130         else
1131                 /* all others */
1132                 video_supported = TPACPI_VIDEO_NEW;
1133
1134         vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1135                 str_supported(video_supported != TPACPI_VIDEO_NONE),
1136                 video_supported);
1137
1138         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1139 }
1140
1141 static void video_exit(void)
1142 {
1143         dbg_printk(TPACPI_DBG_EXIT,
1144                    "restoring original video autoswitch mode\n");
1145         if (video_autosw_set(video_orig_autosw))
1146                 printk(IBM_ERR "error while trying to restore original "
1147                         "video autoswitch mode\n");
1148 }
1149
1150 static int video_outputsw_get(void)
1151 {
1152         int status = 0;
1153         int i;
1154
1155         switch (video_supported) {
1156         case TPACPI_VIDEO_570:
1157                 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1158                                  TP_ACPI_VIDEO_570_PHSCMD))
1159                         return -EIO;
1160                 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1161                 break;
1162         case TPACPI_VIDEO_770:
1163                 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1164                         return -EIO;
1165                 if (i)
1166                         status |= TP_ACPI_VIDEO_S_LCD;
1167                 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1168                         return -EIO;
1169                 if (i)
1170                         status |= TP_ACPI_VIDEO_S_CRT;
1171                 break;
1172         case TPACPI_VIDEO_NEW:
1173                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1174                     !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1175                         return -EIO;
1176                 if (i)
1177                         status |= TP_ACPI_VIDEO_S_CRT;
1178
1179                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1180                     !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1181                         return -EIO;
1182                 if (i)
1183                         status |= TP_ACPI_VIDEO_S_LCD;
1184                 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1185                         return -EIO;
1186                 if (i)
1187                         status |= TP_ACPI_VIDEO_S_DVI;
1188                 break;
1189         default:
1190                 return -ENOSYS;
1191         }
1192
1193         return status;
1194 }
1195
1196 static int video_outputsw_set(int status)
1197 {
1198         int autosw;
1199         int res = 0;
1200
1201         switch (video_supported) {
1202         case TPACPI_VIDEO_570:
1203                 res = acpi_evalf(NULL, NULL,
1204                                  "\\_SB.PHS2", "vdd",
1205                                  TP_ACPI_VIDEO_570_PHS2CMD,
1206                                  status | TP_ACPI_VIDEO_570_PHS2SET);
1207                 break;
1208         case TPACPI_VIDEO_770:
1209                 autosw = video_autosw_get();
1210                 if (autosw < 0)
1211                         return autosw;
1212
1213                 res = video_autosw_set(1);
1214                 if (res)
1215                         return res;
1216                 res = acpi_evalf(vid_handle, NULL,
1217                                  "ASWT", "vdd", status * 0x100, 0);
1218                 if (!autosw && video_autosw_set(autosw)) {
1219                         printk(IBM_ERR "video auto-switch left enabled due to error\n");
1220                         return -EIO;
1221                 }
1222                 break;
1223         case TPACPI_VIDEO_NEW:
1224                 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1225                         acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1226                 break;
1227         default:
1228                 return -ENOSYS;
1229         }
1230
1231         return (res)? 0 : -EIO;
1232 }
1233
1234 static int video_autosw_get(void)
1235 {
1236         int autosw = 0;
1237
1238         switch (video_supported) {
1239         case TPACPI_VIDEO_570:
1240                 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1241                         return -EIO;
1242                 break;
1243         case TPACPI_VIDEO_770:
1244         case TPACPI_VIDEO_NEW:
1245                 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1246                         return -EIO;
1247                 break;
1248         default:
1249                 return -ENOSYS;
1250         }
1251
1252         return autosw & 1;
1253 }
1254
1255 static int video_autosw_set(int enable)
1256 {
1257         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1258                 return -EIO;
1259         return 0;
1260 }
1261
1262 static int video_outputsw_cycle(void)
1263 {
1264         int autosw = video_autosw_get();
1265         int res;
1266
1267         if (autosw < 0)
1268                 return autosw;
1269
1270         switch (video_supported) {
1271         case TPACPI_VIDEO_570:
1272                 res = video_autosw_set(1);
1273                 if (res)
1274                         return res;
1275                 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1276                 break;
1277         case TPACPI_VIDEO_770:
1278         case TPACPI_VIDEO_NEW:
1279                 res = video_autosw_set(1);
1280                 if (res)
1281                         return res;
1282                 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1283                 break;
1284         default:
1285                 return -ENOSYS;
1286         }
1287         if (!autosw && video_autosw_set(autosw)) {
1288                 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1289                 return -EIO;
1290         }
1291
1292         return (res)? 0 : -EIO;
1293 }
1294
1295 static int video_expand_toggle(void)
1296 {
1297         switch (video_supported) {
1298         case TPACPI_VIDEO_570:
1299                 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1300                         0 : -EIO;
1301         case TPACPI_VIDEO_770:
1302                 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1303                         0 : -EIO;
1304         case TPACPI_VIDEO_NEW:
1305                 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1306                         0 : -EIO;
1307         default:
1308                 return -ENOSYS;
1309         }
1310         /* not reached */
1311 }
1312
1313 static int video_read(char *p)
1314 {
1315         int status, autosw;
1316         int len = 0;
1317
1318         if (video_supported == TPACPI_VIDEO_NONE) {
1319                 len += sprintf(p + len, "status:\t\tnot supported\n");
1320                 return len;
1321         }
1322
1323         status = video_outputsw_get();
1324         if (status < 0)
1325                 return status;
1326
1327         autosw = video_autosw_get();
1328         if (autosw < 0)
1329                 return autosw;
1330
1331         len += sprintf(p + len, "status:\t\tsupported\n");
1332         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1333         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1334         if (video_supported == TPACPI_VIDEO_NEW)
1335                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1336         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1337         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1338         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1339         if (video_supported == TPACPI_VIDEO_NEW)
1340                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1341         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1342         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1343
1344         return len;
1345 }
1346
1347 static int video_write(char *buf)
1348 {
1349         char *cmd;
1350         int enable, disable, status;
1351         int res;
1352
1353         if (video_supported == TPACPI_VIDEO_NONE)
1354                 return -ENODEV;
1355
1356         enable = 0;
1357         disable = 0;
1358
1359         while ((cmd = next_cmd(&buf))) {
1360                 if (strlencmp(cmd, "lcd_enable") == 0) {
1361                         enable |= TP_ACPI_VIDEO_S_LCD;
1362                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1363                         disable |= TP_ACPI_VIDEO_S_LCD;
1364                 } else if (strlencmp(cmd, "crt_enable") == 0) {
1365                         enable |= TP_ACPI_VIDEO_S_CRT;
1366                 } else if (strlencmp(cmd, "crt_disable") == 0) {
1367                         disable |= TP_ACPI_VIDEO_S_CRT;
1368                 } else if (video_supported == TPACPI_VIDEO_NEW &&
1369                            strlencmp(cmd, "dvi_enable") == 0) {
1370                         enable |= TP_ACPI_VIDEO_S_DVI;
1371                 } else if (video_supported == TPACPI_VIDEO_NEW &&
1372                            strlencmp(cmd, "dvi_disable") == 0) {
1373                         disable |= TP_ACPI_VIDEO_S_DVI;
1374                 } else if (strlencmp(cmd, "auto_enable") == 0) {
1375                         res = video_autosw_set(1);
1376                         if (res)
1377                                 return res;
1378                 } else if (strlencmp(cmd, "auto_disable") == 0) {
1379                         res = video_autosw_set(0);
1380                         if (res)
1381                                 return res;
1382                 } else if (strlencmp(cmd, "video_switch") == 0) {
1383                         res = video_outputsw_cycle();
1384                         if (res)
1385                                 return res;
1386                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1387                         res = video_expand_toggle();
1388                         if (res)
1389                                 return res;
1390                 } else
1391                         return -EINVAL;
1392         }
1393
1394         if (enable || disable) {
1395                 status = video_outputsw_get();
1396                 if (status < 0)
1397                         return status;
1398                 res = video_outputsw_set((status & ~disable) | enable);
1399                 if (res)
1400                         return res;
1401         }
1402
1403         return 0;
1404 }
1405
1406 static struct ibm_struct video_driver_data = {
1407         .name = "video",
1408         .read = video_read,
1409         .write = video_write,
1410         .exit = video_exit,
1411 };
1412
1413 /*************************************************************************
1414  * Light (thinklight) subdriver
1415  */
1416
1417 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A2xm/p, T20-22, X20-21 */
1418 IBM_HANDLE(ledb, ec, "LEDB");           /* G4x */
1419
1420 static int __init light_init(struct ibm_init_struct *iibm)
1421 {
1422         vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1423
1424         IBM_ACPIHANDLE_INIT(ledb);
1425         IBM_ACPIHANDLE_INIT(lght);
1426         IBM_ACPIHANDLE_INIT(cmos);
1427
1428         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1429         tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1430
1431         if (tp_features.light)
1432                 /* light status not supported on
1433                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1434                 tp_features.light_status =
1435                         acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1436
1437         vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1438                 str_supported(tp_features.light));
1439
1440         return (tp_features.light)? 0 : 1;
1441 }
1442
1443 static int light_read(char *p)
1444 {
1445         int len = 0;
1446         int status = 0;
1447
1448         if (!tp_features.light) {
1449                 len += sprintf(p + len, "status:\t\tnot supported\n");
1450         } else if (!tp_features.light_status) {
1451                 len += sprintf(p + len, "status:\t\tunknown\n");
1452                 len += sprintf(p + len, "commands:\ton, off\n");
1453         } else {
1454                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1455                         return -EIO;
1456                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1457                 len += sprintf(p + len, "commands:\ton, off\n");
1458         }
1459
1460         return len;
1461 }
1462
1463 static int light_write(char *buf)
1464 {
1465         int cmos_cmd, lght_cmd;
1466         char *cmd;
1467         int success;
1468
1469         if (!tp_features.light)
1470                 return -ENODEV;
1471
1472         while ((cmd = next_cmd(&buf))) {
1473                 if (strlencmp(cmd, "on") == 0) {
1474                         cmos_cmd = 0x0c;
1475                         lght_cmd = 1;
1476                 } else if (strlencmp(cmd, "off") == 0) {
1477                         cmos_cmd = 0x0d;
1478                         lght_cmd = 0;
1479                 } else
1480                         return -EINVAL;
1481
1482                 success = cmos_handle ?
1483                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1484                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1485                 if (!success)
1486                         return -EIO;
1487         }
1488
1489         return 0;
1490 }
1491
1492 static struct ibm_struct light_driver_data = {
1493         .name = "light",
1494         .read = light_read,
1495         .write = light_write,
1496 };
1497
1498 /*************************************************************************
1499  * Dock subdriver
1500  */
1501
1502 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1503
1504 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
1505            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1506            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
1507            "\\_SB.PCI.ISA.SLCE",        /* 570 */
1508     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1509
1510 /* don't list other alternatives as we install a notify handler on the 570 */
1511 IBM_HANDLE(pci, root, "\\_SB.PCI");     /* 570 */
1512
1513 #define dock_docked() (_sta(dock_handle) & 1)
1514
1515 static int __init dock_init(struct ibm_init_struct *iibm)
1516 {
1517         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1518
1519         IBM_ACPIHANDLE_INIT(dock);
1520         IBM_ACPIHANDLE_INIT(pci);
1521
1522         vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1523                 str_supported(dock_handle != NULL));
1524
1525         return (dock_handle)? 0 : 1;
1526 }
1527
1528 static void dock_notify(struct ibm_struct *ibm, u32 event)
1529 {
1530         int docked = dock_docked();
1531         int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1532
1533         if (event == 1 && !pci) /* 570 */
1534                 acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
1535         else if (event == 1 && pci)     /* 570 */
1536                 acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
1537         else if (event == 3 && docked)
1538                 acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
1539         else if (event == 3 && !docked)
1540                 acpi_bus_generate_event(ibm->acpi->device, event, 2);   /* undock */
1541         else if (event == 0 && docked)
1542                 acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
1543         else {
1544                 printk(IBM_ERR "unknown dock event %d, status %d\n",
1545                        event, _sta(dock_handle));
1546                 acpi_bus_generate_event(ibm->acpi->device, event, 0);   /* unknown */
1547         }
1548 }
1549
1550 static int dock_read(char *p)
1551 {
1552         int len = 0;
1553         int docked = dock_docked();
1554
1555         if (!dock_handle)
1556                 len += sprintf(p + len, "status:\t\tnot supported\n");
1557         else if (!docked)
1558                 len += sprintf(p + len, "status:\t\tundocked\n");
1559         else {
1560                 len += sprintf(p + len, "status:\t\tdocked\n");
1561                 len += sprintf(p + len, "commands:\tdock, undock\n");
1562         }
1563
1564         return len;
1565 }
1566
1567 static int dock_write(char *buf)
1568 {
1569         char *cmd;
1570
1571         if (!dock_docked())
1572                 return -ENODEV;
1573
1574         while ((cmd = next_cmd(&buf))) {
1575                 if (strlencmp(cmd, "undock") == 0) {
1576                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1577                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1578                                 return -EIO;
1579                 } else if (strlencmp(cmd, "dock") == 0) {
1580                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1581                                 return -EIO;
1582                 } else
1583                         return -EINVAL;
1584         }
1585
1586         return 0;
1587 }
1588
1589 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1590         {
1591          .notify = dock_notify,
1592          .handle = &dock_handle,
1593          .type = ACPI_SYSTEM_NOTIFY,
1594         },
1595         {
1596          .hid = IBM_PCI_HID,
1597          .notify = dock_notify,
1598          .handle = &pci_handle,
1599          .type = ACPI_SYSTEM_NOTIFY,
1600         },
1601 };
1602
1603 static struct ibm_struct dock_driver_data[2] = {
1604         {
1605          .name = "dock",
1606          .read = dock_read,
1607          .write = dock_write,
1608          .acpi = &ibm_dock_acpidriver[0],
1609         },
1610         {
1611          .name = "dock",
1612          .acpi = &ibm_dock_acpidriver[1],
1613         },
1614 };
1615
1616 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1617
1618 /*************************************************************************
1619  * Bay subdriver
1620  */
1621
1622 #ifdef CONFIG_THINKPAD_ACPI_BAY
1623 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",        /* 570 */
1624            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1625            "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1626            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1627            );                           /* A21e, R30, R31 */
1628 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1629            "_EJ0",              /* all others */
1630            );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1631 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",     /* A3x, R32 */
1632            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1633            );                           /* all others */
1634 IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
1635            "_EJ0",                      /* 770x */
1636            );                           /* all others */
1637
1638 static int __init bay_init(struct ibm_init_struct *iibm)
1639 {
1640         vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1641
1642         IBM_ACPIHANDLE_INIT(bay);
1643         if (bay_handle)
1644                 IBM_ACPIHANDLE_INIT(bay_ej);
1645         IBM_ACPIHANDLE_INIT(bay2);
1646         if (bay2_handle)
1647                 IBM_ACPIHANDLE_INIT(bay2_ej);
1648
1649         tp_features.bay_status = bay_handle &&
1650                 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1651         tp_features.bay_status2 = bay2_handle &&
1652                 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1653
1654         tp_features.bay_eject = bay_handle && bay_ej_handle &&
1655                 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1656         tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1657                 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1658
1659         vdbg_printk(TPACPI_DBG_INIT,
1660                 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1661                 str_supported(tp_features.bay_status),
1662                 str_supported(tp_features.bay_eject),
1663                 str_supported(tp_features.bay_status2),
1664                 str_supported(tp_features.bay_eject2));
1665
1666         return (tp_features.bay_status || tp_features.bay_eject ||
1667                 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1668 }
1669
1670 static void bay_notify(struct ibm_struct *ibm, u32 event)
1671 {
1672         acpi_bus_generate_event(ibm->acpi->device, event, 0);
1673 }
1674
1675 #define bay_occupied(b) (_sta(b##_handle) & 1)
1676
1677 static int bay_read(char *p)
1678 {
1679         int len = 0;
1680         int occupied = bay_occupied(bay);
1681         int occupied2 = bay_occupied(bay2);
1682         int eject, eject2;
1683
1684         len += sprintf(p + len, "status:\t\t%s\n",
1685                 tp_features.bay_status ?
1686                         (occupied ? "occupied" : "unoccupied") :
1687                                 "not supported");
1688         if (tp_features.bay_status2)
1689                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1690                                "occupied" : "unoccupied");
1691
1692         eject = tp_features.bay_eject && occupied;
1693         eject2 = tp_features.bay_eject2 && occupied2;
1694
1695         if (eject && eject2)
1696                 len += sprintf(p + len, "commands:\teject, eject2\n");
1697         else if (eject)
1698                 len += sprintf(p + len, "commands:\teject\n");
1699         else if (eject2)
1700                 len += sprintf(p + len, "commands:\teject2\n");
1701
1702         return len;
1703 }
1704
1705 static int bay_write(char *buf)
1706 {
1707         char *cmd;
1708
1709         if (!tp_features.bay_eject && !tp_features.bay_eject2)
1710                 return -ENODEV;
1711
1712         while ((cmd = next_cmd(&buf))) {
1713                 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
1714                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1715                                 return -EIO;
1716                 } else if (tp_features.bay_eject2 &&
1717                            strlencmp(cmd, "eject2") == 0) {
1718                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1719                                 return -EIO;
1720                 } else
1721                         return -EINVAL;
1722         }
1723
1724         return 0;
1725 }
1726
1727 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1728         .notify = bay_notify,
1729         .handle = &bay_handle,
1730         .type = ACPI_SYSTEM_NOTIFY,
1731 };
1732
1733 static struct ibm_struct bay_driver_data = {
1734         .name = "bay",
1735         .read = bay_read,
1736         .write = bay_write,
1737         .acpi = &ibm_bay_acpidriver,
1738 };
1739
1740 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1741
1742 /*************************************************************************
1743  * CMOS subdriver
1744  */
1745
1746 static int __init cmos_init(struct ibm_init_struct *iibm)
1747 {
1748         vdbg_printk(TPACPI_DBG_INIT,
1749                 "initializing cmos commands subdriver\n");
1750
1751         IBM_ACPIHANDLE_INIT(cmos);
1752
1753         vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1754                 str_supported(cmos_handle != NULL));
1755         return (cmos_handle)? 0 : 1;
1756 }
1757
1758 static int cmos_read(char *p)
1759 {
1760         int len = 0;
1761
1762         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1763            R30, R31, T20-22, X20-21 */
1764         if (!cmos_handle)
1765                 len += sprintf(p + len, "status:\t\tnot supported\n");
1766         else {
1767                 len += sprintf(p + len, "status:\t\tsupported\n");
1768                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1769         }
1770
1771         return len;
1772 }
1773
1774 static int cmos_write(char *buf)
1775 {
1776         char *cmd;
1777         int cmos_cmd, res;
1778
1779         while ((cmd = next_cmd(&buf))) {
1780                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1781                     cmos_cmd >= 0 && cmos_cmd <= 21) {
1782                         /* cmos_cmd set */
1783                 } else
1784                         return -EINVAL;
1785
1786                 res = issue_thinkpad_cmos_command(cmos_cmd);
1787                 if (res)
1788                         return res;
1789         }
1790
1791         return 0;
1792 }
1793
1794 static struct ibm_struct cmos_driver_data = {
1795         .name = "cmos",
1796         .read = cmos_read,
1797         .write = cmos_write,
1798 };
1799
1800 /*************************************************************************
1801  * LED subdriver
1802  */
1803
1804 static enum led_access_mode led_supported;
1805
1806 IBM_HANDLE(led, ec, "SLED",     /* 570 */
1807            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1808            "LED",               /* all others */
1809            );                   /* R30, R31 */
1810
1811 static int __init led_init(struct ibm_init_struct *iibm)
1812 {
1813         vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1814
1815         IBM_ACPIHANDLE_INIT(led);
1816
1817         if (!led_handle)
1818                 /* led not supported on R30, R31 */
1819                 led_supported = TPACPI_LED_NONE;
1820         else if (strlencmp(led_path, "SLED") == 0)
1821                 /* 570 */
1822                 led_supported = TPACPI_LED_570;
1823         else if (strlencmp(led_path, "SYSL") == 0)
1824                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1825                 led_supported = TPACPI_LED_OLD;
1826         else
1827                 /* all others */
1828                 led_supported = TPACPI_LED_NEW;
1829
1830         vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1831                 str_supported(led_supported), led_supported);
1832
1833         return (led_supported != TPACPI_LED_NONE)? 0 : 1;
1834 }
1835
1836 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1837
1838 static int led_read(char *p)
1839 {
1840         int len = 0;
1841
1842         if (!led_supported) {
1843                 len += sprintf(p + len, "status:\t\tnot supported\n");
1844                 return len;
1845         }
1846         len += sprintf(p + len, "status:\t\tsupported\n");
1847
1848         if (led_supported == TPACPI_LED_570) {
1849                 /* 570 */
1850                 int i, status;
1851                 for (i = 0; i < 8; i++) {
1852                         if (!acpi_evalf(ec_handle,
1853                                         &status, "GLED", "dd", 1 << i))
1854                                 return -EIO;
1855                         len += sprintf(p + len, "%d:\t\t%s\n",
1856                                        i, led_status(status));
1857                 }
1858         }
1859
1860         len += sprintf(p + len, "commands:\t"
1861                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1862
1863         return len;
1864 }
1865
1866 /* off, on, blink */
1867 static const int led_sled_arg1[] = { 0, 1, 3 };
1868 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
1869 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
1870 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1871
1872 static int led_write(char *buf)
1873 {
1874         char *cmd;
1875         int led, ind, ret;
1876
1877         if (!led_supported)
1878                 return -ENODEV;
1879
1880         while ((cmd = next_cmd(&buf))) {
1881                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1882                         return -EINVAL;
1883
1884                 if (strstr(cmd, "off")) {
1885                         ind = 0;
1886                 } else if (strstr(cmd, "on")) {
1887                         ind = 1;
1888                 } else if (strstr(cmd, "blink")) {
1889                         ind = 2;
1890                 } else
1891                         return -EINVAL;
1892
1893                 if (led_supported == TPACPI_LED_570) {
1894                         /* 570 */
1895                         led = 1 << led;
1896                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1897                                         led, led_sled_arg1[ind]))
1898                                 return -EIO;
1899                 } else if (led_supported == TPACPI_LED_OLD) {
1900                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1901                         led = 1 << led;
1902                         ret = ec_write(TPACPI_LED_EC_HLMS, led);
1903                         if (ret >= 0)
1904                                 ret =
1905                                     ec_write(TPACPI_LED_EC_HLBL,
1906                                              led * led_exp_hlbl[ind]);
1907                         if (ret >= 0)
1908                                 ret =
1909                                     ec_write(TPACPI_LED_EC_HLCL,
1910                                              led * led_exp_hlcl[ind]);
1911                         if (ret < 0)
1912                                 return ret;
1913                 } else {
1914                         /* all others */
1915                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1916                                         led, led_led_arg1[ind]))
1917                                 return -EIO;
1918                 }
1919         }
1920
1921         return 0;
1922 }
1923
1924 static struct ibm_struct led_driver_data = {
1925         .name = "led",
1926         .read = led_read,
1927         .write = led_write,
1928 };
1929
1930 /*************************************************************************
1931  * Beep subdriver
1932  */
1933
1934 IBM_HANDLE(beep, ec, "BEEP");   /* all except R30, R31 */
1935
1936 static int __init beep_init(struct ibm_init_struct *iibm)
1937 {
1938         vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1939
1940         IBM_ACPIHANDLE_INIT(beep);
1941
1942         vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1943                 str_supported(beep_handle != NULL));
1944
1945         return (beep_handle)? 0 : 1;
1946 }
1947
1948 static int beep_read(char *p)
1949 {
1950         int len = 0;
1951
1952         if (!beep_handle)
1953                 len += sprintf(p + len, "status:\t\tnot supported\n");
1954         else {
1955                 len += sprintf(p + len, "status:\t\tsupported\n");
1956                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1957         }
1958
1959         return len;
1960 }
1961
1962 static int beep_write(char *buf)
1963 {
1964         char *cmd;
1965         int beep_cmd;
1966
1967         if (!beep_handle)
1968                 return -ENODEV;
1969
1970         while ((cmd = next_cmd(&buf))) {
1971                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1972                     beep_cmd >= 0 && beep_cmd <= 17) {
1973                         /* beep_cmd set */
1974                 } else
1975                         return -EINVAL;
1976                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1977                         return -EIO;
1978         }
1979
1980         return 0;
1981 }
1982
1983 static struct ibm_struct beep_driver_data = {
1984         .name = "beep",
1985         .read = beep_read,
1986         .write = beep_write,
1987 };
1988
1989 /*************************************************************************
1990  * Thermal subdriver
1991  */
1992
1993 static enum thermal_access_mode thermal_read_mode;
1994
1995 static int __init thermal_init(struct ibm_init_struct *iibm)
1996 {
1997         u8 t, ta1, ta2;
1998         int i;
1999         int acpi_tmp7;
2000
2001         vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2002
2003         acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2004
2005         if (ibm_thinkpad_ec_found && experimental) {
2006                 /*
2007                  * Direct EC access mode: sensors at registers
2008                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
2009                  * non-implemented, thermal sensors return 0x80 when
2010                  * not available
2011                  */
2012
2013                 ta1 = ta2 = 0;
2014                 for (i = 0; i < 8; i++) {
2015                         if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2016                                 ta1 |= t;
2017                         } else {
2018                                 ta1 = 0;
2019                                 break;
2020                         }
2021                         if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2022                                 ta2 |= t;
2023                         } else {
2024                                 ta1 = 0;
2025                                 break;
2026                         }
2027                 }
2028                 if (ta1 == 0) {
2029                         /* This is sheer paranoia, but we handle it anyway */
2030                         if (acpi_tmp7) {
2031                                 printk(IBM_ERR
2032                                        "ThinkPad ACPI EC access misbehaving, "
2033                                        "falling back to ACPI TMPx access mode\n");
2034                                 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2035                         } else {
2036                                 printk(IBM_ERR
2037                                        "ThinkPad ACPI EC access misbehaving, "
2038                                        "disabling thermal sensors access\n");
2039                                 thermal_read_mode = TPACPI_THERMAL_NONE;
2040                         }
2041                 } else {
2042                         thermal_read_mode =
2043                             (ta2 != 0) ?
2044                             TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2045                 }
2046         } else if (acpi_tmp7) {
2047                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2048                         /* 600e/x, 770e, 770x */
2049                         thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2050                 } else {
2051                         /* Standard ACPI TMPx access, max 8 sensors */
2052                         thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2053                 }
2054         } else {
2055                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2056                 thermal_read_mode = TPACPI_THERMAL_NONE;
2057         }
2058
2059         vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2060                 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2061                 thermal_read_mode);
2062
2063         return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
2064 }
2065
2066 /* idx is zero-based */
2067 static int thermal_get_sensor(int idx, s32 *value)
2068 {
2069         int t;
2070         s8 tmp;
2071         char tmpi[5];
2072
2073         t = TP_EC_THERMAL_TMP0;
2074
2075         switch (thermal_read_mode) {
2076 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2077         case TPACPI_THERMAL_TPEC_16:
2078                 if (idx >= 8 && idx <= 15) {
2079                         t = TP_EC_THERMAL_TMP8;
2080                         idx -= 8;
2081                 }
2082                 /* fallthrough */
2083 #endif
2084         case TPACPI_THERMAL_TPEC_8:
2085                 if (idx <= 7) {
2086                         if (!acpi_ec_read(t + idx, &tmp))
2087                                 return -EIO;
2088                         *value = tmp * 1000;
2089                         return 0;
2090                 }
2091                 break;
2092
2093         case TPACPI_THERMAL_ACPI_UPDT:
2094                 if (idx <= 7) {
2095                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2096                         if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2097                                 return -EIO;
2098                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2099                                 return -EIO;
2100                         *value = (t - 2732) * 100;
2101                         return 0;
2102                 }
2103                 break;
2104
2105         case TPACPI_THERMAL_ACPI_TMP07:
2106                 if (idx <= 7) {
2107                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2108                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2109                                 return -EIO;
2110                         *value = t * 1000;
2111                         return 0;
2112                 }
2113                 break;
2114
2115         case TPACPI_THERMAL_NONE:
2116         default:
2117                 return -ENOSYS;
2118         }
2119
2120         return -EINVAL;
2121 }
2122
2123 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2124 {
2125         int res, i;
2126         int n;
2127
2128         n = 8;
2129         i = 0;
2130
2131         if (!s)
2132                 return -EINVAL;
2133
2134         if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2135                 n = 16;
2136
2137         for(i = 0 ; i < n; i++) {
2138                 res = thermal_get_sensor(i, &s->temp[i]);
2139                 if (res)
2140                         return res;
2141         }
2142
2143         return n;
2144 }
2145
2146 static int thermal_read(char *p)
2147 {
2148         int len = 0;
2149         int n, i;
2150         struct ibm_thermal_sensors_struct t;
2151
2152         n = thermal_get_sensors(&t);
2153         if (unlikely(n < 0))
2154                 return n;
2155
2156         len += sprintf(p + len, "temperatures:\t");
2157
2158         if (n > 0) {
2159                 for (i = 0; i < (n - 1); i++)
2160                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2161                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2162         } else
2163                 len += sprintf(p + len, "not supported\n");
2164
2165         return len;
2166 }
2167
2168 static struct ibm_struct thermal_driver_data = {
2169         .name = "thermal",
2170         .read = thermal_read,
2171 };
2172
2173 /*************************************************************************
2174  * EC Dump subdriver
2175  */
2176
2177 static u8 ecdump_regs[256];
2178
2179 static int ecdump_read(char *p)
2180 {
2181         int len = 0;
2182         int i, j;
2183         u8 v;
2184
2185         len += sprintf(p + len, "EC      "
2186                        " +00 +01 +02 +03 +04 +05 +06 +07"
2187                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2188         for (i = 0; i < 256; i += 16) {
2189                 len += sprintf(p + len, "EC 0x%02x:", i);
2190                 for (j = 0; j < 16; j++) {
2191                         if (!acpi_ec_read(i + j, &v))
2192                                 break;
2193                         if (v != ecdump_regs[i + j])
2194                                 len += sprintf(p + len, " *%02x", v);
2195                         else
2196                                 len += sprintf(p + len, "  %02x", v);
2197                         ecdump_regs[i + j] = v;
2198                 }
2199                 len += sprintf(p + len, "\n");
2200                 if (j != 16)
2201                         break;
2202         }
2203
2204         /* These are way too dangerous to advertise openly... */
2205 #if 0
2206         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2207                        " (<offset> is 00-ff, <value> is 00-ff)\n");
2208         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
2209                        " (<offset> is 00-ff, <value> is 0-255)\n");
2210 #endif
2211         return len;
2212 }
2213
2214 static int ecdump_write(char *buf)
2215 {
2216         char *cmd;
2217         int i, v;
2218
2219         while ((cmd = next_cmd(&buf))) {
2220                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2221                         /* i and v set */
2222                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2223                         /* i and v set */
2224                 } else
2225                         return -EINVAL;
2226                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2227                         if (!acpi_ec_write(i, v))
2228                                 return -EIO;
2229                 } else
2230                         return -EINVAL;
2231         }
2232
2233         return 0;
2234 }
2235
2236 static struct ibm_struct ecdump_driver_data = {
2237         .name = "ecdump",
2238         .read = ecdump_read,
2239         .write = ecdump_write,
2240         .flags.experimental = 1,
2241 };
2242
2243 /*************************************************************************
2244  * Backlight/brightness subdriver
2245  */
2246
2247 static struct backlight_device *ibm_backlight_device = NULL;
2248
2249 static struct backlight_ops ibm_backlight_data = {
2250         .get_brightness = brightness_get,
2251         .update_status  = brightness_update_status,
2252 };
2253
2254 static int __init brightness_init(struct ibm_init_struct *iibm)
2255 {
2256         int b;
2257
2258         vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2259
2260         b = brightness_get(NULL);
2261         if (b < 0)
2262                 return b;
2263
2264         ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
2265                                                          &ibm_backlight_data);
2266         if (IS_ERR(ibm_backlight_device)) {
2267                 printk(IBM_ERR "Could not register backlight device\n");
2268                 return PTR_ERR(ibm_backlight_device);
2269         }
2270         vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2271
2272         ibm_backlight_device->props.max_brightness = 7;
2273         ibm_backlight_device->props.brightness = b;
2274         backlight_update_status(ibm_backlight_device);
2275
2276         return 0;
2277 }
2278
2279 static void brightness_exit(void)
2280 {
2281         if (ibm_backlight_device) {
2282                 vdbg_printk(TPACPI_DBG_EXIT,
2283                             "calling backlight_device_unregister()\n");
2284                 backlight_device_unregister(ibm_backlight_device);
2285                 ibm_backlight_device = NULL;
2286         }
2287 }
2288
2289 static int brightness_update_status(struct backlight_device *bd)
2290 {
2291         return brightness_set(
2292                 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2293                  bd->props.power == FB_BLANK_UNBLANK) ?
2294                                 bd->props.brightness : 0);
2295 }
2296
2297 static int brightness_get(struct backlight_device *bd)
2298 {
2299         u8 level;
2300         if (!acpi_ec_read(brightness_offset, &level))
2301                 return -EIO;
2302
2303         level &= 0x7;
2304
2305         return level;
2306 }
2307
2308 static int brightness_set(int value)
2309 {
2310         int cmos_cmd, inc, i;
2311         int current_value = brightness_get(NULL);
2312
2313         value &= 7;
2314
2315         cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2316         inc = value > current_value ? 1 : -1;
2317         for (i = current_value; i != value; i += inc) {
2318                 if (issue_thinkpad_cmos_command(cmos_cmd))
2319                         return -EIO;
2320                 if (!acpi_ec_write(brightness_offset, i + inc))
2321                         return -EIO;
2322         }
2323
2324         return 0;
2325 }
2326
2327 static int brightness_read(char *p)
2328 {
2329         int len = 0;
2330         int level;
2331
2332         if ((level = brightness_get(NULL)) < 0) {
2333                 len += sprintf(p + len, "level:\t\tunreadable\n");
2334         } else {
2335                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2336                 len += sprintf(p + len, "commands:\tup, down\n");
2337                 len += sprintf(p + len, "commands:\tlevel <level>"
2338                                " (<level> is 0-7)\n");
2339         }
2340
2341         return len;
2342 }
2343
2344 static int brightness_write(char *buf)
2345 {
2346         int level;
2347         int new_level;
2348         char *cmd;
2349
2350         while ((cmd = next_cmd(&buf))) {
2351                 if ((level = brightness_get(NULL)) < 0)
2352                         return level;
2353                 level &= 7;
2354
2355                 if (strlencmp(cmd, "up") == 0) {
2356                         new_level = level == 7 ? 7 : level + 1;
2357                 } else if (strlencmp(cmd, "down") == 0) {
2358                         new_level = level == 0 ? 0 : level - 1;
2359                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2360                            new_level >= 0 && new_level <= 7) {
2361                         /* new_level set */
2362                 } else
2363                         return -EINVAL;
2364
2365                 brightness_set(new_level);
2366         }
2367
2368         return 0;
2369 }
2370
2371 static struct ibm_struct brightness_driver_data = {
2372         .name = "brightness",
2373         .read = brightness_read,
2374         .write = brightness_write,
2375         .exit = brightness_exit,
2376 };
2377
2378 /*************************************************************************
2379  * Volume subdriver
2380  */
2381
2382 static int volume_read(char *p)
2383 {
2384         int len = 0;
2385         u8 level;
2386
2387         if (!acpi_ec_read(volume_offset, &level)) {
2388                 len += sprintf(p + len, "level:\t\tunreadable\n");
2389         } else {
2390                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2391                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2392                 len += sprintf(p + len, "commands:\tup, down, mute\n");
2393                 len += sprintf(p + len, "commands:\tlevel <level>"
2394                                " (<level> is 0-15)\n");
2395         }
2396
2397         return len;
2398 }
2399
2400 static int volume_write(char *buf)
2401 {
2402         int cmos_cmd, inc, i;
2403         u8 level, mute;
2404         int new_level, new_mute;
2405         char *cmd;
2406
2407         while ((cmd = next_cmd(&buf))) {
2408                 if (!acpi_ec_read(volume_offset, &level))
2409                         return -EIO;
2410                 new_mute = mute = level & 0x40;
2411                 new_level = level = level & 0xf;
2412
2413                 if (strlencmp(cmd, "up") == 0) {
2414                         if (mute)
2415                                 new_mute = 0;
2416                         else
2417                                 new_level = level == 15 ? 15 : level + 1;
2418                 } else if (strlencmp(cmd, "down") == 0) {
2419                         if (mute)
2420                                 new_mute = 0;
2421                         else
2422                                 new_level = level == 0 ? 0 : level - 1;
2423                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2424                            new_level >= 0 && new_level <= 15) {
2425                         /* new_level set */
2426                 } else if (strlencmp(cmd, "mute") == 0) {
2427                         new_mute = 0x40;
2428                 } else
2429                         return -EINVAL;
2430
2431                 if (new_level != level) {       /* mute doesn't change */
2432                         cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2433                         inc = new_level > level ? 1 : -1;
2434
2435                         if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2436                                      !acpi_ec_write(volume_offset, level)))
2437                                 return -EIO;
2438
2439                         for (i = level; i != new_level; i += inc)
2440                                 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2441                                     !acpi_ec_write(volume_offset, i + inc))
2442                                         return -EIO;
2443
2444                         if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2445                                      !acpi_ec_write(volume_offset,
2446                                                     new_level + mute)))
2447                                 return -EIO;
2448                 }
2449
2450                 if (new_mute != mute) { /* level doesn't change */
2451                         cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2452
2453                         if (issue_thinkpad_cmos_command(cmos_cmd) ||
2454                             !acpi_ec_write(volume_offset, level + new_mute))
2455                                 return -EIO;
2456                 }
2457         }
2458
2459         return 0;
2460 }
2461
2462 static struct ibm_struct volume_driver_data = {
2463         .name = "volume",
2464         .read = volume_read,
2465         .write = volume_write,
2466 };
2467
2468 /*************************************************************************
2469  * Fan subdriver
2470  */
2471
2472 /*
2473  * FAN ACCESS MODES
2474  *
2475  * TPACPI_FAN_RD_ACPI_GFAN:
2476  *      ACPI GFAN method: returns fan level
2477  *
2478  *      see TPACPI_FAN_WR_ACPI_SFAN
2479  *      EC 0x2f (HFSP) not available if GFAN exists
2480  *
2481  * TPACPI_FAN_WR_ACPI_SFAN:
2482  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2483  *
2484  *      EC 0x2f (HFSP) might be available *for reading*, but do not use
2485  *      it for writing.
2486  *
2487  * TPACPI_FAN_WR_TPEC:
2488  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode
2489  *      Supported on almost all ThinkPads
2490  *
2491  *      Fan speed changes of any sort (including those caused by the
2492  *      disengaged mode) are usually done slowly by the firmware as the
2493  *      maximum ammount of fan duty cycle change per second seems to be
2494  *      limited.
2495  *
2496  *      Reading is not available if GFAN exists.
2497  *      Writing is not available if SFAN exists.
2498  *
2499  *      Bits
2500  *       7      automatic mode engaged;
2501  *              (default operation mode of the ThinkPad)
2502  *              fan level is ignored in this mode.
2503  *       6      full speed mode (takes precedence over bit 7);
2504  *              not available on all thinkpads.  May disable
2505  *              the tachometer while the fan controller ramps up
2506  *              the speed (which can take up to a few *minutes*).
2507  *              Speeds up fan to 100% duty-cycle, which is far above
2508  *              the standard RPM levels.  It is not impossible that
2509  *              it could cause hardware damage.
2510  *      5-3     unused in some models.  Extra bits for fan level
2511  *              in others, but still useless as all values above
2512  *              7 map to the same speed as level 7 in these models.
2513  *      2-0     fan level (0..7 usually)
2514  *                      0x00 = stop
2515  *                      0x07 = max (set when temperatures critical)
2516  *              Some ThinkPads may have other levels, see
2517  *              TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2518  *
2519  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2520  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2521  *      does so, its initial value is meaningless (0x07).
2522  *
2523  *      For firmware bugs, refer to:
2524  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2525  *
2526  *      ----
2527  *
2528  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2529  *      Main fan tachometer reading (in RPM)
2530  *
2531  *      This register is present on all ThinkPads with a new-style EC, and
2532  *      it is known not to be present on the A21m/e, and T22, as there is
2533  *      something else in offset 0x84 according to the ACPI DSDT.  Other
2534  *      ThinkPads from this same time period (and earlier) probably lack the
2535  *      tachometer as well.
2536  *
2537  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2538  *      was never fixed by IBM to report the EC firmware version string
2539  *      probably support the tachometer (like the early X models), so
2540  *      detecting it is quite hard.  We need more data to know for sure.
2541  *
2542  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2543  *      might result.
2544  *
2545  *      FIRMWARE BUG: may go stale while the EC is switching to full speed
2546  *      mode.
2547  *
2548  *      For firmware bugs, refer to:
2549  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2550  *
2551  * TPACPI_FAN_WR_ACPI_FANS:
2552  *      ThinkPad X31, X40, X41.  Not available in the X60.
2553  *
2554  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
2555  *      high speed.  ACPI DSDT seems to map these three speeds to levels
2556  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2557  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2558  *
2559  *      The speeds are stored on handles
2560  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2561  *
2562  *      There are three default speed sets, acessible as handles:
2563  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2564  *
2565  *      ACPI DSDT switches which set is in use depending on various
2566  *      factors.
2567  *
2568  *      TPACPI_FAN_WR_TPEC is also available and should be used to
2569  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
2570  *      but the ACPI tables just mention level 7.
2571  */
2572
2573 static enum fan_status_access_mode fan_status_access_mode;
2574 static enum fan_control_access_mode fan_control_access_mode;
2575 static enum fan_control_commands fan_control_commands;
2576
2577 static u8 fan_control_initial_status;
2578
2579 static void fan_watchdog_fire(struct work_struct *ignored);
2580 static int fan_watchdog_maxinterval;
2581 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
2582
2583 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40, X41 */
2584 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
2585            "\\FSPD",            /* 600e/x, 770e, 770x */
2586            );                   /* all others */
2587 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
2588            "JFNS",              /* 770x-JL */
2589            );                   /* all others */
2590
2591 static int __init fan_init(struct ibm_init_struct *iibm)
2592 {
2593         vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2594
2595         mutex_init(&fan_mutex);
2596         fan_status_access_mode = TPACPI_FAN_NONE;
2597         fan_control_access_mode = TPACPI_FAN_WR_NONE;
2598         fan_control_commands = 0;
2599         fan_watchdog_maxinterval = 0;
2600         tp_features.fan_ctrl_status_undef = 0;
2601
2602         IBM_ACPIHANDLE_INIT(fans);
2603         IBM_ACPIHANDLE_INIT(gfan);
2604         IBM_ACPIHANDLE_INIT(sfan);
2605
2606         if (gfan_handle) {
2607                 /* 570, 600e/x, 770e, 770x */
2608                 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
2609         } else {
2610                 /* all other ThinkPads: note that even old-style
2611                  * ThinkPad ECs supports the fan control register */
2612                 if (likely(acpi_ec_read(fan_status_offset,
2613                                         &fan_control_initial_status))) {
2614                         fan_status_access_mode = TPACPI_FAN_RD_TPEC;
2615
2616                         /* In some ThinkPads, neither the EC nor the ACPI
2617                          * DSDT initialize the fan status, and it ends up
2618                          * being set to 0x07 when it *could* be either
2619                          * 0x07 or 0x80.
2620                          *
2621                          * Enable for TP-1Y (T43), TP-78 (R51e),
2622                          * TP-76 (R52), TP-70 (T43, R52), which are known
2623                          * to be buggy. */
2624                         if (fan_control_initial_status == 0x07 &&
2625                             ibm_thinkpad_ec_found &&
2626                             ((ibm_thinkpad_ec_found[0] == '1' &&
2627                               ibm_thinkpad_ec_found[1] == 'Y') ||
2628                              (ibm_thinkpad_ec_found[0] == '7' &&
2629                               (ibm_thinkpad_ec_found[1] == '6' ||
2630                                ibm_thinkpad_ec_found[1] == '8' ||
2631                                ibm_thinkpad_ec_found[1] == '0'))
2632                             )) {
2633                                 printk(IBM_NOTICE
2634                                        "fan_init: initial fan status is "
2635                                        "unknown, assuming it is in auto "
2636                                        "mode\n");
2637                                 tp_features.fan_ctrl_status_undef = 1;
2638                         }
2639                 } else {
2640                         printk(IBM_ERR
2641                                "ThinkPad ACPI EC access misbehaving, "
2642                                "fan status and control unavailable\n");
2643                         return 1;
2644                 }
2645         }
2646
2647         if (sfan_handle) {
2648                 /* 570, 770x-JL */
2649                 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
2650                 fan_control_commands |=
2651                     TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
2652         } else {
2653                 if (!gfan_handle) {
2654                         /* gfan without sfan means no fan control */
2655                         /* all other models implement TP EC 0x2f control */
2656
2657                         if (fans_handle) {
2658                                 /* X31, X40, X41 */
2659                                 fan_control_access_mode =
2660                                     TPACPI_FAN_WR_ACPI_FANS;
2661                                 fan_control_commands |=
2662                                     TPACPI_FAN_CMD_SPEED |
2663                                     TPACPI_FAN_CMD_LEVEL |
2664                                     TPACPI_FAN_CMD_ENABLE;
2665                         } else {
2666                                 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
2667                                 fan_control_commands |=
2668                                     TPACPI_FAN_CMD_LEVEL |
2669                                     TPACPI_FAN_CMD_ENABLE;
2670                         }
2671                 }
2672         }
2673
2674         vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2675                 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2676                   fan_control_access_mode != TPACPI_FAN_WR_NONE),
2677                 fan_status_access_mode, fan_control_access_mode);
2678
2679         return (fan_status_access_mode != TPACPI_FAN_NONE ||
2680                 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2681                         0 : 1;
2682 }
2683
2684 static int fan_get_status(u8 *status)
2685 {
2686         u8 s;
2687
2688         /* TODO:
2689          * Add TPACPI_FAN_RD_ACPI_FANS ? */
2690
2691         switch (fan_status_access_mode) {
2692         case TPACPI_FAN_RD_ACPI_GFAN:
2693                 /* 570, 600e/x, 770e, 770x */
2694
2695                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2696                         return -EIO;
2697
2698                 if (likely(status))
2699                         *status = s & 0x07;
2700
2701                 break;
2702
2703         case TPACPI_FAN_RD_TPEC:
2704                 /* all except 570, 600e/x, 770e, 770x */
2705                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2706                         return -EIO;
2707
2708                 if (likely(status))
2709                         *status = s;
2710
2711                 break;
2712
2713         default:
2714                 return -ENXIO;
2715         }
2716
2717         return 0;
2718 }
2719
2720 static void fan_exit(void)
2721 {
2722         vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
2723         cancel_delayed_work(&fan_watchdog_task);
2724         flush_scheduled_work();
2725 }
2726
2727 static int fan_get_speed(unsigned int *speed)
2728 {
2729         u8 hi, lo;
2730
2731         switch (fan_status_access_mode) {
2732         case TPACPI_FAN_RD_TPEC:
2733                 /* all except 570, 600e/x, 770e, 770x */
2734                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2735                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2736                         return -EIO;
2737
2738                 if (likely(speed))
2739                         *speed = (hi << 8) | lo;
2740
2741                 break;
2742
2743         default:
2744                 return -ENXIO;
2745         }
2746
2747         return 0;
2748 }
2749
2750 static void fan_watchdog_fire(struct work_struct *ignored)
2751 {
2752         int rc;
2753
2754         printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2755         rc = fan_set_enable();
2756         if (rc < 0) {
2757                 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
2758                         "will try again later...\n", -rc);
2759                 /* reschedule for later */
2760                 fan_watchdog_reset();
2761         }
2762 }
2763
2764 static void fan_watchdog_reset(void)
2765 {
2766         static int fan_watchdog_active = 0;
2767
2768         if (fan_watchdog_active)
2769                 cancel_delayed_work(&fan_watchdog_task);
2770
2771         if (fan_watchdog_maxinterval > 0) {
2772                 fan_watchdog_active = 1;
2773                 if (!schedule_delayed_work(&fan_watchdog_task,
2774                                 msecs_to_jiffies(fan_watchdog_maxinterval
2775                                                  * 1000))) {
2776                         printk(IBM_ERR "failed to schedule the fan watchdog, "
2777                                "watchdog will not trigger\n");
2778                 }
2779         } else
2780                 fan_watchdog_active = 0;
2781 }
2782
2783 static int fan_set_level(int level)
2784 {
2785         int res;
2786
2787         switch (fan_control_access_mode) {
2788         case TPACPI_FAN_WR_ACPI_SFAN:
2789                 if (level >= 0 && level <= 7) {
2790                         res = mutex_lock_interruptible(&fan_mutex);
2791                         if (res < 0)
2792                                 return res;
2793                         res = acpi_evalf(sfan_handle, NULL, NULL, "vd", level);
2794                         mutex_unlock(&fan_mutex);
2795                         if (!res)
2796                                 return -EIO;
2797                 } else
2798                         return -EINVAL;
2799                 break;
2800
2801         case TPACPI_FAN_WR_ACPI_FANS:
2802         case TPACPI_FAN_WR_TPEC:
2803                 if ((level != TP_EC_FAN_AUTO) &&
2804                     (level != TP_EC_FAN_FULLSPEED) &&
2805                     ((level < 0) || (level > 7)))
2806                         return -EINVAL;
2807
2808                 res = mutex_lock_interruptible(&fan_mutex);
2809                 if (res < 0)
2810                         return res;
2811                 res = acpi_ec_write(fan_status_offset, level);
2812                 mutex_unlock(&fan_mutex);
2813                 if (!res)
2814                         return -EIO;
2815                 else
2816                         tp_features.fan_ctrl_status_undef = 0;
2817                 break;
2818
2819         default:
2820                 return -ENXIO;
2821         }
2822         return 0;
2823 }
2824
2825 static int fan_set_enable(void)
2826 {
2827         u8 s;
2828         int rc;
2829
2830         rc = mutex_lock_interruptible(&fan_mutex);
2831         if (rc < 0)
2832                 return rc;
2833
2834         switch (fan_control_access_mode) {
2835         case TPACPI_FAN_WR_ACPI_FANS:
2836         case TPACPI_FAN_WR_TPEC:
2837                 rc = fan_get_status(&s);
2838                 if (rc < 0)
2839                         break;
2840
2841                 /* Don't go out of emergency fan mode */
2842                 if (s != 7)
2843                         s = TP_EC_FAN_AUTO;
2844
2845                 if (!acpi_ec_write(fan_status_offset, s))
2846                         rc = -EIO;
2847                 else {
2848                         tp_features.fan_ctrl_status_undef = 0;
2849                         rc = 0;
2850                 }
2851                 break;
2852
2853         case TPACPI_FAN_WR_ACPI_SFAN:
2854                 rc = fan_get_status(&s);
2855                 if (rc < 0)
2856                         break;
2857
2858                 s &= 0x07;
2859
2860                 /* Set fan to at least level 4 */
2861                 if (s < 4)
2862                         s = 4;
2863
2864                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2865                         rc= -EIO;
2866                 else
2867                         rc = 0;
2868                 break;
2869
2870         default:
2871                 rc = -ENXIO;
2872         }
2873
2874         mutex_unlock(&fan_mutex);
2875         return rc;
2876 }
2877
2878 static int fan_set_disable(void)
2879 {
2880         int rc;
2881
2882         rc = mutex_lock_interruptible(&fan_mutex);
2883         if (rc < 0)
2884                 return rc;
2885
2886         rc = 0;
2887         switch (fan_control_access_mode) {
2888         case TPACPI_FAN_WR_ACPI_FANS:
2889         case TPACPI_FAN_WR_TPEC:
2890                 if (!acpi_ec_write(fan_status_offset, 0x00))
2891                         rc = -EIO;
2892                 else
2893                         tp_features.fan_ctrl_status_undef = 0;
2894                 break;
2895
2896         case TPACPI_FAN_WR_ACPI_SFAN:
2897                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2898                         rc = -EIO;
2899                 break;
2900
2901         default:
2902                 rc = -ENXIO;
2903         }
2904
2905         mutex_unlock(&fan_mutex);
2906         return rc;
2907 }
2908
2909 static int fan_set_speed(int speed)
2910 {
2911         int rc;
2912
2913         rc = mutex_lock_interruptible(&fan_mutex);
2914         if (rc < 0)
2915                 return rc;
2916
2917         rc = 0;
2918         switch (fan_control_access_mode) {
2919         case TPACPI_FAN_WR_ACPI_FANS:
2920                 if (speed >= 0 && speed <= 65535) {
2921                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2922                                         speed, speed, speed))
2923                                 rc = -EIO;
2924                 } else
2925                         rc = -EINVAL;
2926                 break;
2927
2928         default:
2929                 rc = -ENXIO;
2930         }
2931
2932         mutex_unlock(&fan_mutex);
2933         return rc;
2934 }
2935
2936 static int fan_read(char *p)
2937 {
2938         int len = 0;
2939         int rc;
2940         u8 status;
2941         unsigned int speed = 0;
2942
2943         switch (fan_status_access_mode) {
2944         case TPACPI_FAN_RD_ACPI_GFAN:
2945                 /* 570, 600e/x, 770e, 770x */
2946                 if ((rc = fan_get_status(&status)) < 0)
2947                         return rc;
2948
2949                 len += sprintf(p + len, "status:\t\t%s\n"
2950                                "level:\t\t%d\n",
2951                                (status != 0) ? "enabled" : "disabled", status);
2952                 break;
2953
2954         case TPACPI_FAN_RD_TPEC:
2955                 /* all except 570, 600e/x, 770e, 770x */
2956                 if ((rc = fan_get_status(&status)) < 0)
2957                         return rc;
2958
2959                 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2960                         if (status != fan_control_initial_status)
2961                                 tp_features.fan_ctrl_status_undef = 0;
2962                         else
2963                                 /* Return most likely status. In fact, it
2964                                  * might be the only possible status */
2965                                 status = TP_EC_FAN_AUTO;
2966                 }
2967
2968                 len += sprintf(p + len, "status:\t\t%s\n",
2969                                (status != 0) ? "enabled" : "disabled");
2970
2971                 if ((rc = fan_get_speed(&speed)) < 0)
2972                         return rc;
2973
2974                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2975
2976                 if (status & TP_EC_FAN_FULLSPEED)
2977                         /* Disengaged mode takes precedence */
2978                         len += sprintf(p + len, "level:\t\tdisengaged\n");
2979                 else if (status & TP_EC_FAN_AUTO)
2980                         len += sprintf(p + len, "level:\t\tauto\n");
2981                 else
2982                         len += sprintf(p + len, "level:\t\t%d\n", status);
2983                 break;
2984
2985         case TPACPI_FAN_NONE:
2986         default:
2987                 len += sprintf(p + len, "status:\t\tnot supported\n");
2988         }
2989
2990         if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
2991                 len += sprintf(p + len, "commands:\tlevel <level>");
2992
2993                 switch (fan_control_access_mode) {
2994                 case TPACPI_FAN_WR_ACPI_SFAN:
2995                         len += sprintf(p + len, " (<level> is 0-7)\n");
2996                         break;
2997
2998                 default:
2999                         len += sprintf(p + len, " (<level> is 0-7, "
3000                                        "auto, disengaged)\n");
3001                         break;
3002                 }
3003         }
3004
3005         if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3006                 len += sprintf(p + len, "commands:\tenable, disable\n"
3007                                "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3008                                "1-120 (seconds))\n");
3009
3010         if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3011                 len += sprintf(p + len, "commands:\tspeed <speed>"
3012                                " (<speed> is 0-65535)\n");
3013
3014         return len;
3015 }
3016
3017 static int fan_write_cmd_level(const char *cmd, int *rc)
3018 {
3019         int level;
3020
3021         if (strlencmp(cmd, "level auto") == 0)
3022                 level = TP_EC_FAN_AUTO;
3023         else if (strlencmp(cmd, "level disengaged") == 0)
3024                 level = TP_EC_FAN_FULLSPEED;
3025         else if (sscanf(cmd, "level %d", &level) != 1)
3026                 return 0;
3027
3028         if ((*rc = fan_set_level(level)) == -ENXIO)
3029                 printk(IBM_ERR "level command accepted for unsupported "
3030                        "access mode %d", fan_control_access_mode);
3031
3032         return 1;
3033 }
3034
3035 static int fan_write_cmd_enable(const char *cmd, int *rc)
3036 {
3037         if (strlencmp(cmd, "enable") != 0)
3038                 return 0;
3039
3040         if ((*rc = fan_set_enable()) == -ENXIO)
3041                 printk(IBM_ERR "enable command accepted for unsupported "
3042                        "access mode %d", fan_control_access_mode);
3043
3044         return 1;
3045 }
3046
3047 static int fan_write_cmd_disable(const char *cmd, int *rc)
3048 {
3049         if (strlencmp(cmd, "disable") != 0)
3050                 return 0;
3051
3052         if ((*rc = fan_set_disable()) == -ENXIO)
3053                 printk(IBM_ERR "disable command accepted for unsupported "
3054                        "access mode %d", fan_control_access_mode);
3055
3056         return 1;
3057 }
3058
3059 static int fan_write_cmd_speed(const char *cmd, int *rc)
3060 {
3061         int speed;
3062
3063         /* TODO:
3064          * Support speed <low> <medium> <high> ? */
3065
3066         if (sscanf(cmd, "speed %d", &speed) != 1)
3067                 return 0;
3068
3069         if ((*rc = fan_set_speed(speed)) == -ENXIO)
3070                 printk(IBM_ERR "speed command accepted for unsupported "
3071                        "access mode %d", fan_control_access_mode);
3072
3073         return 1;
3074 }
3075
3076 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3077 {
3078         int interval;
3079
3080         if (sscanf(cmd, "watchdog %d", &interval) != 1)
3081                 return 0;
3082
3083         if (interval < 0 || interval > 120)
3084                 *rc = -EINVAL;
3085         else
3086                 fan_watchdog_maxinterval = interval;
3087
3088         return 1;
3089 }
3090
3091 static int fan_write(char *buf)
3092 {
3093         char *cmd;
3094         int rc = 0;
3095
3096         while (!rc && (cmd = next_cmd(&buf))) {
3097                 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3098                       fan_write_cmd_level(cmd, &rc)) &&
3099                     !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3100                       (fan_write_cmd_enable(cmd, &rc) ||
3101                        fan_write_cmd_disable(cmd, &rc) ||
3102                        fan_write_cmd_watchdog(cmd, &rc))) &&
3103                     !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3104                       fan_write_cmd_speed(cmd, &rc))
3105                     )
3106                         rc = -EINVAL;
3107                 else if (!rc)
3108                         fan_watchdog_reset();
3109         }
3110
3111         return rc;
3112 }
3113
3114 static struct ibm_struct fan_driver_data = {
3115         .name = "fan",
3116         .read = fan_read,
3117         .write = fan_write,
3118         .exit = fan_exit,
3119         .flags.experimental = 1,
3120 };
3121
3122 /****************************************************************************
3123  ****************************************************************************
3124  *
3125  * Infrastructure
3126  *
3127  ****************************************************************************
3128  ****************************************************************************/
3129
3130 /* /proc support */
3131 static struct proc_dir_entry *proc_dir = NULL;
3132
3133 /* Subdriver registry */
3134 static LIST_HEAD(tpacpi_all_drivers);
3135
3136
3137 /*
3138  * Module and infrastructure proble, init and exit handling
3139  */
3140
3141 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3142 static const char * __init str_supported(int is_supported)
3143 {
3144         static char text_unsupported[] __initdata = "not supported";
3145
3146         return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3147 }
3148 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3149
3150 static int __init ibm_init(struct ibm_init_struct *iibm)
3151 {
3152         int ret;
3153         struct ibm_struct *ibm = iibm->data;
3154         struct proc_dir_entry *entry;
3155
3156         BUG_ON(ibm == NULL);
3157
3158         INIT_LIST_HEAD(&ibm->all_drivers);
3159
3160         if (ibm->flags.experimental && !experimental)
3161                 return 0;
3162
3163         dbg_printk(TPACPI_DBG_INIT,
3164                 "probing for %s\n", ibm->name);
3165
3166         if (iibm->init) {
3167                 ret = iibm->init(iibm);
3168                 if (ret > 0)
3169                         return 0;       /* probe failed */
3170                 if (ret)
3171                         return ret;
3172
3173                 ibm->flags.init_called = 1;
3174         }
3175
3176         if (ibm->acpi) {
3177                 if (ibm->acpi->hid) {
3178                         ret = register_tpacpi_subdriver(ibm);
3179                         if (ret)
3180                                 goto err_out;
3181                 }
3182
3183                 if (ibm->acpi->notify) {
3184                         ret = setup_acpi_notify(ibm);
3185                         if (ret == -ENODEV) {
3186                                 printk(IBM_NOTICE "disabling subdriver %s\n",
3187                                         ibm->name);
3188                                 ret = 0;
3189                                 goto err_out;
3190                         }
3191                         if (ret < 0)
3192                                 goto err_out;
3193                 }
3194         }
3195
3196         dbg_printk(TPACPI_DBG_INIT,
3197                 "%s installed\n", ibm->name);
3198
3199         if (ibm->read) {
3200                 entry = create_proc_entry(ibm->name,
3201                                           S_IFREG | S_IRUGO | S_IWUSR,
3202                                           proc_dir);
3203                 if (!entry) {
3204                         printk(IBM_ERR "unable to create proc entry %s\n",
3205                                ibm->name);
3206                         ret = -ENODEV;
3207                         goto err_out;
3208                 }
3209                 entry->owner = THIS_MODULE;
3210                 entry->data = ibm;
3211                 entry->read_proc = &dispatch_procfs_read;
3212                 if (ibm->write)
3213                         entry->write_proc = &dispatch_procfs_write;
3214                 ibm->flags.proc_created = 1;
3215         }
3216
3217         list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3218
3219         return 0;
3220
3221 err_out:
3222         dbg_printk(TPACPI_DBG_INIT,
3223                 "%s: at error exit path with result %d\n",
3224                 ibm->name, ret);
3225
3226         ibm_exit(ibm);
3227         return (ret < 0)? ret : 0;
3228 }
3229
3230 static void ibm_exit(struct ibm_struct *ibm)
3231 {
3232         dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
3233
3234         list_del_init(&ibm->all_drivers);
3235
3236         if (ibm->flags.acpi_notify_installed) {
3237                 dbg_printk(TPACPI_DBG_EXIT,
3238                         "%s: acpi_remove_notify_handler\n", ibm->name);
3239                 BUG_ON(!ibm->acpi);
3240                 acpi_remove_notify_handler(*ibm->acpi->handle,
3241                                            ibm->acpi->type,
3242                                            dispatch_acpi_notify);
3243                 ibm->flags.acpi_notify_installed = 0;
3244                 ibm->flags.acpi_notify_installed = 0;
3245         }
3246
3247         if (ibm->flags.proc_created) {
3248                 dbg_printk(TPACPI_DBG_EXIT,
3249                         "%s: remove_proc_entry\n", ibm->name);
3250                 remove_proc_entry(ibm->name, proc_dir);
3251                 ibm->flags.proc_created = 0;
3252         }
3253
3254         if (ibm->flags.acpi_driver_registered) {
3255                 dbg_printk(TPACPI_DBG_EXIT,
3256                         "%s: acpi_bus_unregister_driver\n", ibm->name);
3257                 BUG_ON(!ibm->acpi);
3258                 acpi_bus_unregister_driver(ibm->acpi->driver);
3259                 kfree(ibm->acpi->driver);
3260                 ibm->acpi->driver = NULL;
3261                 ibm->flags.acpi_driver_registered = 0;
3262         }
3263
3264         if (ibm->flags.init_called && ibm->exit) {
3265                 ibm->exit();
3266                 ibm->flags.init_called = 0;
3267         }
3268
3269         dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
3270 }
3271
3272 /* Probing */
3273
3274 static char *ibm_thinkpad_ec_found = NULL;
3275
3276 static char* __init check_dmi_for_ec(void)
3277 {
3278         struct dmi_device *dev = NULL;
3279         char ec_fw_string[18];
3280
3281         /*
3282          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3283          * X32 or newer, all Z series;  Some models must have an
3284          * up-to-date BIOS or they will not be detected.
3285          *
3286          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3287          */
3288         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
3289                 if (sscanf(dev->name,
3290                            "IBM ThinkPad Embedded Controller -[%17c",
3291                            ec_fw_string) == 1) {
3292                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3293                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3294                         return kstrdup(ec_fw_string, GFP_KERNEL);
3295                 }
3296         }
3297         return NULL;
3298 }
3299
3300 static int __init probe_for_thinkpad(void)
3301 {
3302         int is_thinkpad;
3303
3304         if (acpi_disabled)
3305                 return -ENODEV;
3306
3307         /*
3308          * Non-ancient models have better DMI tagging, but very old models
3309          * don't.
3310          */
3311         is_thinkpad = dmi_name_in_vendors("ThinkPad");
3312
3313         /* ec is required because many other handles are relative to it */
3314         IBM_ACPIHANDLE_INIT(ec);
3315         if (!ec_handle) {
3316                 if (is_thinkpad)
3317                         printk(IBM_ERR
3318                                 "Not yet supported ThinkPad detected!\n");
3319                 return -ENODEV;
3320         }
3321
3322         /*
3323          * Risks a regression on very old machines, but reduces potential
3324          * false positives a damn great deal
3325          */
3326         if (!is_thinkpad)
3327                 is_thinkpad = dmi_name_in_vendors("IBM");
3328
3329         if (!is_thinkpad && !force_load)
3330                 return -ENODEV;
3331
3332         return 0;
3333 }
3334
3335
3336 /* Module init, exit, parameters */
3337
3338 static struct ibm_init_struct ibms_init[] __initdata = {
3339         {
3340                 .init = thinkpad_acpi_driver_init,
3341                 .data = &thinkpad_acpi_driver_data,
3342         },
3343         {
3344                 .init = hotkey_init,
3345                 .data = &hotkey_driver_data,
3346         },
3347         {
3348                 .init = bluetooth_init,
3349                 .data = &bluetooth_driver_data,
3350         },
3351         {
3352                 .init = wan_init,
3353                 .data = &wan_driver_data,
3354         },
3355         {
3356                 .init = video_init,
3357                 .data = &video_driver_data,
3358         },
3359         {
3360                 .init = light_init,
3361                 .data = &light_driver_data,
3362         },
3363 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3364         {
3365                 .init = dock_init,
3366                 .data = &dock_driver_data[0],
3367         },
3368         {
3369                 .data = &dock_driver_data[1],
3370         },
3371 #endif
3372 #ifdef CONFIG_THINKPAD_ACPI_BAY
3373         {
3374                 .init = bay_init,
3375                 .data = &bay_driver_data,
3376         },
3377 #endif
3378         {
3379                 .init = cmos_init,
3380                 .data = &cmos_driver_data,
3381         },
3382         {
3383                 .init = led_init,
3384                 .data = &led_driver_data,
3385         },
3386         {
3387                 .init = beep_init,
3388                 .data = &beep_driver_data,
3389         },
3390         {
3391                 .init = thermal_init,
3392                 .data = &thermal_driver_data,
3393         },
3394         {
3395                 .data = &ecdump_driver_data,
3396         },
3397         {
3398                 .init = brightness_init,
3399                 .data = &brightness_driver_data,
3400         },
3401         {
3402                 .data = &volume_driver_data,
3403         },
3404         {
3405                 .init = fan_init,
3406                 .data = &fan_driver_data,
3407         },
3408 };
3409
3410 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3411 {
3412         unsigned int i;
3413         struct ibm_struct *ibm;
3414
3415         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3416                 ibm = ibms_init[i].data;
3417                 BUG_ON(ibm == NULL);
3418
3419                 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3420                         if (strlen(val) > sizeof(ibms_init[i].param) - 2)
3421                                 return -ENOSPC;
3422                         strcpy(ibms_init[i].param, val);
3423                         strcat(ibms_init[i].param, ",");
3424                         return 0;
3425                 }
3426         }
3427
3428         return -EINVAL;
3429 }
3430
3431 static int experimental;
3432 module_param(experimental, int, 0);
3433
3434 static u32 dbg_level;
3435 module_param_named(debug, dbg_level, uint, 0);
3436
3437 static int force_load;
3438 module_param(force_load, int, 0);
3439
3440 #define IBM_PARAM(feature) \
3441         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3442
3443 IBM_PARAM(hotkey);
3444 IBM_PARAM(bluetooth);
3445 IBM_PARAM(video);
3446 IBM_PARAM(light);
3447 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3448 IBM_PARAM(dock);
3449 #endif
3450 #ifdef CONFIG_THINKPAD_ACPI_BAY
3451 IBM_PARAM(bay);
3452 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3453 IBM_PARAM(cmos);
3454 IBM_PARAM(led);
3455 IBM_PARAM(beep);
3456 IBM_PARAM(ecdump);
3457 IBM_PARAM(brightness);
3458 IBM_PARAM(volume);
3459 IBM_PARAM(fan);
3460
3461 static int __init thinkpad_acpi_module_init(void)
3462 {
3463         int ret, i;
3464
3465         /* Driver-level probe */
3466         ret = probe_for_thinkpad();
3467         if (ret)
3468                 return ret;
3469
3470         /* Driver initialization */
3471         ibm_thinkpad_ec_found = check_dmi_for_ec();
3472         IBM_ACPIHANDLE_INIT(ecrd);
3473         IBM_ACPIHANDLE_INIT(ecwr);
3474
3475         proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
3476         if (!proc_dir) {
3477                 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
3478                 thinkpad_acpi_module_exit();
3479                 return -ENODEV;
3480         }
3481         proc_dir->owner = THIS_MODULE;
3482
3483         ret = platform_driver_register(&tpacpi_pdriver);
3484         if (ret) {
3485                 printk(IBM_ERR "unable to register platform driver\n");
3486                 thinkpad_acpi_module_exit();
3487                 return ret;
3488         }
3489         ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3490         if (ret) {
3491                 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3492                 thinkpad_acpi_module_exit();
3493                 return ret;
3494         }
3495
3496
3497         /* Device initialization */
3498         tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3499                                                         NULL, 0);
3500         if (IS_ERR(tpacpi_pdev)) {
3501                 ret = PTR_ERR(tpacpi_pdev);
3502                 tpacpi_pdev = NULL;
3503                 printk(IBM_ERR "unable to register platform device\n");
3504                 thinkpad_acpi_module_exit();
3505                 return ret;
3506         }
3507         tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3508         if (IS_ERR(tpacpi_hwmon)) {
3509                 ret = PTR_ERR(tpacpi_hwmon);
3510                 tpacpi_hwmon = NULL;
3511                 printk(IBM_ERR "unable to register hwmon device\n");
3512                 thinkpad_acpi_module_exit();
3513                 return ret;
3514         }
3515         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3516                 ret = ibm_init(&ibms_init[i]);
3517                 if (ret >= 0 && *ibms_init[i].param)
3518                         ret = ibms_init[i].data->write(ibms_init[i].param);
3519                 if (ret < 0) {
3520                         thinkpad_acpi_module_exit();
3521                         return ret;
3522                 }
3523         }
3524
3525         return 0;
3526 }
3527
3528 static void thinkpad_acpi_module_exit(void)
3529 {
3530         struct ibm_struct *ibm, *itmp;
3531
3532         list_for_each_entry_safe_reverse(ibm, itmp,
3533                                          &tpacpi_all_drivers,
3534                                          all_drivers) {
3535                 ibm_exit(ibm);
3536         }
3537
3538         dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
3539
3540         if (tpacpi_hwmon)
3541                 hwmon_device_unregister(tpacpi_hwmon);
3542
3543         if (tpacpi_pdev)
3544                 platform_device_unregister(tpacpi_pdev);
3545
3546         tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
3547         platform_driver_unregister(&tpacpi_pdriver);
3548
3549         if (proc_dir)
3550                 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
3551
3552         kfree(ibm_thinkpad_ec_found);
3553 }
3554
3555 module_init(thinkpad_acpi_module_init);
3556 module_exit(thinkpad_acpi_module_exit);