ACPI: thinkpad-acpi: clean up probing and move init to subdrivers
[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
26 /*
27  *  Changelog:
28  *  2007-03-27  0.14    renamed to thinkpad_acpi and moved to
29  *                      drivers/misc.
30  *
31  *  2006-11-22  0.13    new maintainer
32  *                      changelog now lives in git commit history, and will
33  *                      not be updated further in-file.
34  *
35  *  2005-08-17  0.12    fix compilation on 2.6.13-rc kernels
36  *  2005-03-17  0.11    support for 600e, 770x
37  *                          thanks to Jamie Lentin <lentinj@dial.pipex.com>
38  *                      support for 770e, G41
39  *                      G40 and G41 don't have a thinklight
40  *                      temperatures no longer experimental
41  *                      experimental brightness control
42  *                      experimental volume control
43  *                      experimental fan enable/disable
44  *  2005-01-16  0.10    fix module loading on R30, R31
45  *  2005-01-16  0.9     support for 570, R30, R31
46  *                      ultrabay support on A22p, A3x
47  *                      limit arg for cmos, led, beep, drop experimental status
48  *                      more capable led control on A21e, A22p, T20-22, X20
49  *                      experimental temperatures and fan speed
50  *                      experimental embedded controller register dump
51  *                      mark more functions as __init, drop incorrect __exit
52  *                      use MODULE_VERSION
53  *                          thanks to Henrik Brix Andersen <brix@gentoo.org>
54  *                      fix parameter passing on module loading
55  *                          thanks to Rusty Russell <rusty@rustcorp.com.au>
56  *                          thanks to Jim Radford <radford@blackbean.org>
57  *  2004-11-08  0.8     fix init error case, don't return from a macro
58  *                          thanks to Chris Wright <chrisw@osdl.org>
59  *  2004-10-23  0.7     fix module loading on A21e, A22p, T20, T21, X20
60  *                      fix led control on A21e
61  *  2004-10-19  0.6     use acpi_bus_register_driver() to claim HKEY device
62  *  2004-10-18  0.5     thinklight support on A21e, G40, R32, T20, T21, X20
63  *                      proc file format changed
64  *                      video_switch command
65  *                      experimental cmos control
66  *                      experimental led control
67  *                      experimental acpi sounds
68  *  2004-09-16  0.4     support for module parameters
69  *                      hotkey mask can be prefixed by 0x
70  *                      video output switching
71  *                      video expansion control
72  *                      ultrabay eject support
73  *                      removed lcd brightness/on/off control, didn't work
74  *  2004-08-17  0.3     support for R40
75  *                      lcd off, brightness control
76  *                      thinklight on/off
77  *  2004-08-14  0.2     support for T series, X20
78  *                      bluetooth enable/disable
79  *                      hotkey events disabled by default
80  *                      removed fan control, currently useless
81  *  2004-08-09  0.1     initial release, support for X series
82  */
83
84 #include "thinkpad_acpi.h"
85
86 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
87 MODULE_DESCRIPTION(IBM_DESC);
88 MODULE_VERSION(IBM_VERSION);
89 MODULE_LICENSE("GPL");
90
91 /* Please remove this in year 2009 */
92 MODULE_ALIAS("ibm_acpi");
93
94 #define __unused __attribute__ ((unused))
95
96 /****************************************************************************
97  ****************************************************************************
98  *
99  * ACPI Helpers and device model
100  *
101  ****************************************************************************
102  ****************************************************************************/
103
104 /*************************************************************************
105  * ACPI basic handles
106  */
107
108 static acpi_handle root_handle = NULL;
109
110 #define IBM_HANDLE(object, parent, paths...)                    \
111         static acpi_handle  object##_handle;                    \
112         static acpi_handle *object##_parent = &parent##_handle; \
113         static char        *object##_path;                      \
114         static char        *object##_paths[] = { paths }
115
116 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
117            "\\_SB.PCI.ISA.EC",  /* 570 */
118            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
119            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
120            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
121            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
122            "\\_SB.PCI0.LPC.EC", /* all others */
123            );
124
125 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
126 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
127
128
129 /*************************************************************************
130  * Misc ACPI handles
131  */
132
133 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
134            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
135            "\\CMS",             /* R40, R40e */
136            );                   /* all others */
137
138 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
139            "^HKEY",             /* R30, R31 */
140            "HKEY",              /* all others */
141            );                   /* 570 */
142
143
144 /*************************************************************************
145  * ACPI helpers
146  */
147
148 static int acpi_evalf(acpi_handle handle,
149                       void *res, char *method, char *fmt, ...)
150 {
151         char *fmt0 = fmt;
152         struct acpi_object_list params;
153         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
154         struct acpi_buffer result, *resultp;
155         union acpi_object out_obj;
156         acpi_status status;
157         va_list ap;
158         char res_type;
159         int success;
160         int quiet;
161
162         if (!*fmt) {
163                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
164                 return 0;
165         }
166
167         if (*fmt == 'q') {
168                 quiet = 1;
169                 fmt++;
170         } else
171                 quiet = 0;
172
173         res_type = *(fmt++);
174
175         params.count = 0;
176         params.pointer = &in_objs[0];
177
178         va_start(ap, fmt);
179         while (*fmt) {
180                 char c = *(fmt++);
181                 switch (c) {
182                 case 'd':       /* int */
183                         in_objs[params.count].integer.value = va_arg(ap, int);
184                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
185                         break;
186                         /* add more types as needed */
187                 default:
188                         printk(IBM_ERR "acpi_evalf() called "
189                                "with invalid format character '%c'\n", c);
190                         return 0;
191                 }
192         }
193         va_end(ap);
194
195         if (res_type != 'v') {
196                 result.length = sizeof(out_obj);
197                 result.pointer = &out_obj;
198                 resultp = &result;
199         } else
200                 resultp = NULL;
201
202         status = acpi_evaluate_object(handle, method, &params, resultp);
203
204         switch (res_type) {
205         case 'd':               /* int */
206                 if (res)
207                         *(int *)res = out_obj.integer.value;
208                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
209                 break;
210         case 'v':               /* void */
211                 success = status == AE_OK;
212                 break;
213                 /* add more types as needed */
214         default:
215                 printk(IBM_ERR "acpi_evalf() called "
216                        "with invalid format character '%c'\n", res_type);
217                 return 0;
218         }
219
220         if (!success && !quiet)
221                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
222                        method, fmt0, status);
223
224         return success;
225 }
226
227 static void __unused acpi_print_int(acpi_handle handle, char *method)
228 {
229         int i;
230
231         if (acpi_evalf(handle, &i, method, "d"))
232                 printk(IBM_INFO "%s = 0x%x\n", method, i);
233         else
234                 printk(IBM_ERR "error calling %s\n", method);
235 }
236
237 static int acpi_ec_read(int i, u8 * p)
238 {
239         int v;
240
241         if (ecrd_handle) {
242                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
243                         return 0;
244                 *p = v;
245         } else {
246                 if (ec_read(i, p) < 0)
247                         return 0;
248         }
249
250         return 1;
251 }
252
253 static int acpi_ec_write(int i, u8 v)
254 {
255         if (ecwr_handle) {
256                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
257                         return 0;
258         } else {
259                 if (ec_write(i, v) < 0)
260                         return 0;
261         }
262
263         return 1;
264 }
265
266 static int _sta(acpi_handle handle)
267 {
268         int status;
269
270         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
271                 status = 0;
272
273         return status;
274 }
275
276 /*************************************************************************
277  * ACPI device model
278  */
279
280 static void ibm_handle_init(char *name,
281                            acpi_handle *handle, acpi_handle parent,
282                            char **paths, int num_paths, char **path)
283 {
284         int i;
285         acpi_status status;
286
287         for (i = 0; i < num_paths; i++) {
288                 status = acpi_get_handle(parent, paths[i], handle);
289                 if (ACPI_SUCCESS(status)) {
290                         *path = paths[i];
291                         return;
292                 }
293         }
294
295         *handle = NULL;
296 }
297
298 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
299 {
300         struct ibm_struct *ibm = data;
301
302         if (!ibm || !ibm->notify)
303                 return;
304
305         ibm->notify(ibm, event);
306 }
307
308 static int __init setup_notify(struct ibm_struct *ibm)
309 {
310         acpi_status status;
311         int ret;
312
313         if (!*ibm->handle)
314                 return 0;
315
316         ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
317         if (ret < 0) {
318                 printk(IBM_ERR "%s device not present\n", ibm->name);
319                 return -ENODEV;
320         }
321
322         acpi_driver_data(ibm->device) = ibm;
323         sprintf(acpi_device_class(ibm->device), "%s/%s",
324                 IBM_ACPI_EVENT_PREFIX,
325                 ibm->name);
326
327         status = acpi_install_notify_handler(*ibm->handle, ibm->type,
328                                              dispatch_notify, ibm);
329         if (ACPI_FAILURE(status)) {
330                 if (status == AE_ALREADY_EXISTS) {
331                         printk(IBM_NOTICE "another device driver is already handling %s events\n",
332                                 ibm->name);
333                 } else {
334                         printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
335                                 ibm->name, status);
336                 }
337                 return -ENODEV;
338         }
339         ibm->notify_installed = 1;
340         return 0;
341 }
342
343 static int __init ibm_device_add(struct acpi_device *device)
344 {
345         return 0;
346 }
347
348 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
349 {
350         int ret;
351
352         ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
353         if (!ibm->driver) {
354                 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
355                 return -ENOMEM;
356         }
357
358         sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
359         ibm->driver->ids = ibm->hid;
360         ibm->driver->ops.add = &ibm_device_add;
361
362         ret = acpi_bus_register_driver(ibm->driver);
363         if (ret < 0) {
364                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
365                        ibm->hid, ret);
366                 kfree(ibm->driver);
367                 ibm->driver = NULL;
368         } else if (!ret)
369                 ibm->driver_registered = 1;
370
371         return ret;
372 }
373
374
375 /****************************************************************************
376  ****************************************************************************
377  *
378  * Procfs Helpers
379  *
380  ****************************************************************************
381  ****************************************************************************/
382
383 static int dispatch_read(char *page, char **start, off_t off, int count,
384                          int *eof, void *data)
385 {
386         struct ibm_struct *ibm = data;
387         int len;
388
389         if (!ibm || !ibm->read)
390                 return -EINVAL;
391
392         len = ibm->read(page);
393         if (len < 0)
394                 return len;
395
396         if (len <= off + count)
397                 *eof = 1;
398         *start = page + off;
399         len -= off;
400         if (len > count)
401                 len = count;
402         if (len < 0)
403                 len = 0;
404
405         return len;
406 }
407
408 static int dispatch_write(struct file *file, const char __user * userbuf,
409                           unsigned long count, void *data)
410 {
411         struct ibm_struct *ibm = data;
412         char *kernbuf;
413         int ret;
414
415         if (!ibm || !ibm->write)
416                 return -EINVAL;
417
418         kernbuf = kmalloc(count + 2, GFP_KERNEL);
419         if (!kernbuf)
420                 return -ENOMEM;
421
422         if (copy_from_user(kernbuf, userbuf, count)) {
423                 kfree(kernbuf);
424                 return -EFAULT;
425         }
426
427         kernbuf[count] = 0;
428         strcat(kernbuf, ",");
429         ret = ibm->write(kernbuf);
430         if (ret == 0)
431                 ret = count;
432
433         kfree(kernbuf);
434
435         return ret;
436 }
437
438 static char *next_cmd(char **cmds)
439 {
440         char *start = *cmds;
441         char *end;
442
443         while ((end = strchr(start, ',')) && end == start)
444                 start = end + 1;
445
446         if (!end)
447                 return NULL;
448
449         *end = 0;
450         *cmds = end + 1;
451         return start;
452 }
453
454
455 /****************************************************************************
456  ****************************************************************************
457  *
458  * Subdrivers
459  *
460  ****************************************************************************
461  ****************************************************************************/
462
463 /*************************************************************************
464  * thinkpad-acpi init subdriver
465  */
466
467 static int thinkpad_acpi_driver_init(void)
468 {
469         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
470         printk(IBM_INFO "%s\n", IBM_URL);
471
472         if (ibm_thinkpad_ec_found)
473                 printk(IBM_INFO "ThinkPad EC firmware %s\n",
474                        ibm_thinkpad_ec_found);
475
476         return 0;
477 }
478
479 static int thinkpad_acpi_driver_read(char *p)
480 {
481         int len = 0;
482
483         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
484         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
485
486         return len;
487 }
488
489 /*************************************************************************
490  * Hotkey subdriver
491  */
492
493 static int hotkey_supported;
494 static int hotkey_mask_supported;
495 static int hotkey_orig_status;
496 static int hotkey_orig_mask;
497
498 static int hotkey_init(void)
499 {
500         IBM_HANDLE_INIT(hkey);
501
502         /* hotkey not supported on 570 */
503         hotkey_supported = hkey_handle != NULL;
504
505         if (hotkey_supported) {
506                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
507                    A30, R30, R31, T20-22, X20-21, X22-24 */
508                 hotkey_mask_supported =
509                     acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
510
511                 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
512                         return -ENODEV;
513         }
514
515         return (hotkey_supported)? 0 : 1;
516 }
517
518 static void hotkey_exit(void)
519 {
520         if (hotkey_supported) {
521                 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
522         }
523 }
524
525 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
526 {
527         int hkey;
528
529         if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
530                 acpi_bus_generate_event(ibm->device, event, hkey);
531         else {
532                 printk(IBM_ERR "unknown hotkey event %d\n", event);
533                 acpi_bus_generate_event(ibm->device, event, 0);
534         }
535 }
536
537 static int hotkey_get(int *status, int *mask)
538 {
539         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
540                 return 0;
541
542         if (hotkey_mask_supported)
543                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
544                         return 0;
545
546         return 1;
547 }
548
549 static int hotkey_set(int status, int mask)
550 {
551         int i;
552
553         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
554                 return 0;
555
556         if (hotkey_mask_supported)
557                 for (i = 0; i < 32; i++) {
558                         int bit = ((1 << i) & mask) != 0;
559                         if (!acpi_evalf(hkey_handle,
560                                         NULL, "MHKM", "vdd", i + 1, bit))
561                                 return 0;
562                 }
563
564         return 1;
565 }
566
567 static int hotkey_read(char *p)
568 {
569         int status, mask;
570         int len = 0;
571
572         if (!hotkey_supported) {
573                 len += sprintf(p + len, "status:\t\tnot supported\n");
574                 return len;
575         }
576
577         if (!hotkey_get(&status, &mask))
578                 return -EIO;
579
580         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
581         if (hotkey_mask_supported) {
582                 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
583                 len += sprintf(p + len,
584                                "commands:\tenable, disable, reset, <mask>\n");
585         } else {
586                 len += sprintf(p + len, "mask:\t\tnot supported\n");
587                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
588         }
589
590         return len;
591 }
592
593 static int hotkey_write(char *buf)
594 {
595         int status, mask;
596         char *cmd;
597         int do_cmd = 0;
598
599         if (!hotkey_supported)
600                 return -ENODEV;
601
602         if (!hotkey_get(&status, &mask))
603                 return -EIO;
604
605         while ((cmd = next_cmd(&buf))) {
606                 if (strlencmp(cmd, "enable") == 0) {
607                         status = 1;
608                 } else if (strlencmp(cmd, "disable") == 0) {
609                         status = 0;
610                 } else if (strlencmp(cmd, "reset") == 0) {
611                         status = hotkey_orig_status;
612                         mask = hotkey_orig_mask;
613                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
614                         /* mask set */
615                 } else if (sscanf(cmd, "%x", &mask) == 1) {
616                         /* mask set */
617                 } else
618                         return -EINVAL;
619                 do_cmd = 1;
620         }
621
622         if (do_cmd && !hotkey_set(status, mask))
623                 return -EIO;
624
625         return 0;
626 }
627
628 /*************************************************************************
629  * Bluetooth subdriver
630  */
631
632 static int bluetooth_supported;
633
634 static int bluetooth_init(void)
635 {
636         IBM_HANDLE_INIT(hkey);
637
638         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
639            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
640         bluetooth_supported = hkey_handle &&
641             acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
642
643         return (bluetooth_supported)? 0 : 1;
644 }
645
646 static int bluetooth_status(void)
647 {
648         int status;
649
650         if (!bluetooth_supported ||
651             !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
652                 status = 0;
653
654         return status;
655 }
656
657 static int bluetooth_read(char *p)
658 {
659         int len = 0;
660         int status = bluetooth_status();
661
662         if (!bluetooth_supported)
663                 len += sprintf(p + len, "status:\t\tnot supported\n");
664         else if (!(status & 1))
665                 len += sprintf(p + len, "status:\t\tnot installed\n");
666         else {
667                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
668                 len += sprintf(p + len, "commands:\tenable, disable\n");
669         }
670
671         return len;
672 }
673
674 static int bluetooth_write(char *buf)
675 {
676         int status = bluetooth_status();
677         char *cmd;
678         int do_cmd = 0;
679
680         if (!bluetooth_supported)
681                 return -ENODEV;
682
683         while ((cmd = next_cmd(&buf))) {
684                 if (strlencmp(cmd, "enable") == 0) {
685                         status |= 2;
686                 } else if (strlencmp(cmd, "disable") == 0) {
687                         status &= ~2;
688                 } else
689                         return -EINVAL;
690                 do_cmd = 1;
691         }
692
693         if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
694                 return -EIO;
695
696         return 0;
697 }
698
699 /*************************************************************************
700  * Wan subdriver
701  */
702
703 static int wan_supported;
704
705 static int wan_init(void)
706 {
707         IBM_HANDLE_INIT(hkey);
708
709         wan_supported = hkey_handle &&
710             acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
711
712         return (wan_supported)? 0 : 1;
713 }
714
715 static int wan_status(void)
716 {
717         int status;
718
719         if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
720                 status = 0;
721
722         return status;
723 }
724
725 static int wan_read(char *p)
726 {
727         int len = 0;
728         int status = wan_status();
729
730         if (!wan_supported)
731                 len += sprintf(p + len, "status:\t\tnot supported\n");
732         else if (!(status & 1))
733                 len += sprintf(p + len, "status:\t\tnot installed\n");
734         else {
735                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
736                 len += sprintf(p + len, "commands:\tenable, disable\n");
737         }
738
739         return len;
740 }
741
742 static int wan_write(char *buf)
743 {
744         int status = wan_status();
745         char *cmd;
746         int do_cmd = 0;
747
748         if (!wan_supported)
749                 return -ENODEV;
750
751         while ((cmd = next_cmd(&buf))) {
752                 if (strlencmp(cmd, "enable") == 0) {
753                         status |= 2;
754                 } else if (strlencmp(cmd, "disable") == 0) {
755                         status &= ~2;
756                 } else
757                         return -EINVAL;
758                 do_cmd = 1;
759         }
760
761         if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
762                 return -EIO;
763
764         return 0;
765 }
766
767 /*************************************************************************
768  * Video subdriver
769  */
770
771 static enum video_access_mode video_supported;
772 static int video_orig_autosw;
773
774 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
775            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
776            "\\_SB.PCI0.VID0",   /* 770e */
777            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
778            "\\_SB.PCI0.AGP.VID",        /* all others */
779            );                           /* R30, R31 */
780
781 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
782
783 static int video_init(void)
784 {
785         int ivga;
786
787         IBM_HANDLE_INIT(vid);
788         IBM_HANDLE_INIT(vid2);
789
790         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
791                 /* G41, assume IVGA doesn't change */
792                 vid_handle = vid2_handle;
793
794         if (!vid_handle)
795                 /* video switching not supported on R30, R31 */
796                 video_supported = TPACPI_VIDEO_NONE;
797         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
798                 /* 570 */
799                 video_supported = TPACPI_VIDEO_570;
800         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
801                 /* 600e/x, 770e, 770x */
802                 video_supported = TPACPI_VIDEO_770;
803         else
804                 /* all others */
805                 video_supported = TPACPI_VIDEO_NEW;
806
807         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
808 }
809
810 static void video_exit(void)
811 {
812         acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
813 }
814
815 static int video_status(void)
816 {
817         int status = 0;
818         int i;
819
820         if (video_supported == TPACPI_VIDEO_570) {
821                 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
822                         status = i & 3;
823         } else if (video_supported == TPACPI_VIDEO_770) {
824                 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
825                         status |= 0x01 * i;
826                 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
827                         status |= 0x02 * i;
828         } else if (video_supported == TPACPI_VIDEO_NEW) {
829                 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
830                 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
831                         status |= 0x02 * i;
832
833                 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
834                 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
835                         status |= 0x01 * i;
836                 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
837                         status |= 0x08 * i;
838         }
839
840         return status;
841 }
842
843 static int video_autosw(void)
844 {
845         int autosw = 0;
846
847         if (video_supported == TPACPI_VIDEO_570)
848                 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
849         else if (video_supported == TPACPI_VIDEO_770 ||
850                  video_supported == TPACPI_VIDEO_NEW)
851                 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
852
853         return autosw & 1;
854 }
855
856 static int video_switch(void)
857 {
858         int autosw = video_autosw();
859         int ret;
860
861         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
862                 return -EIO;
863         ret = video_supported == TPACPI_VIDEO_570 ?
864             acpi_evalf(ec_handle, NULL, "_Q16", "v") :
865             acpi_evalf(vid_handle, NULL, "VSWT", "v");
866         acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
867
868         return ret;
869 }
870
871 static int video_expand(void)
872 {
873         if (video_supported == TPACPI_VIDEO_570)
874                 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
875         else if (video_supported == TPACPI_VIDEO_770)
876                 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
877         else
878                 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
879 }
880
881 static int video_switch2(int status)
882 {
883         int ret;
884
885         if (video_supported == TPACPI_VIDEO_570) {
886                 ret = acpi_evalf(NULL, NULL,
887                                  "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
888         } else if (video_supported == TPACPI_VIDEO_770) {
889                 int autosw = video_autosw();
890                 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
891                         return -EIO;
892
893                 ret = acpi_evalf(vid_handle, NULL,
894                                  "ASWT", "vdd", status * 0x100, 0);
895
896                 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
897         } else {
898                 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
899                     acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
900         }
901
902         return ret;
903 }
904
905 static int video_read(char *p)
906 {
907         int status = video_status();
908         int autosw = video_autosw();
909         int len = 0;
910
911         if (!video_supported) {
912                 len += sprintf(p + len, "status:\t\tnot supported\n");
913                 return len;
914         }
915
916         len += sprintf(p + len, "status:\t\tsupported\n");
917         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
918         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
919         if (video_supported == TPACPI_VIDEO_NEW)
920                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
921         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
922         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
923         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
924         if (video_supported == TPACPI_VIDEO_NEW)
925                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
926         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
927         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
928
929         return len;
930 }
931
932 static int video_write(char *buf)
933 {
934         char *cmd;
935         int enable, disable, status;
936
937         if (!video_supported)
938                 return -ENODEV;
939
940         enable = disable = 0;
941
942         while ((cmd = next_cmd(&buf))) {
943                 if (strlencmp(cmd, "lcd_enable") == 0) {
944                         enable |= 0x01;
945                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
946                         disable |= 0x01;
947                 } else if (strlencmp(cmd, "crt_enable") == 0) {
948                         enable |= 0x02;
949                 } else if (strlencmp(cmd, "crt_disable") == 0) {
950                         disable |= 0x02;
951                 } else if (video_supported == TPACPI_VIDEO_NEW &&
952                            strlencmp(cmd, "dvi_enable") == 0) {
953                         enable |= 0x08;
954                 } else if (video_supported == TPACPI_VIDEO_NEW &&
955                            strlencmp(cmd, "dvi_disable") == 0) {
956                         disable |= 0x08;
957                 } else if (strlencmp(cmd, "auto_enable") == 0) {
958                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
959                                 return -EIO;
960                 } else if (strlencmp(cmd, "auto_disable") == 0) {
961                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
962                                 return -EIO;
963                 } else if (strlencmp(cmd, "video_switch") == 0) {
964                         if (!video_switch())
965                                 return -EIO;
966                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
967                         if (!video_expand())
968                                 return -EIO;
969                 } else
970                         return -EINVAL;
971         }
972
973         if (enable || disable) {
974                 status = (video_status() & 0x0f & ~disable) | enable;
975                 if (!video_switch2(status))
976                         return -EIO;
977         }
978
979         return 0;
980 }
981
982 /*************************************************************************
983  * Light (thinklight) subdriver
984  */
985
986 static int light_supported;
987 static int light_status_supported;
988
989 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A2xm/p, T20-22, X20-21 */
990 IBM_HANDLE(ledb, ec, "LEDB");           /* G4x */
991
992 static int light_init(void)
993 {
994         IBM_HANDLE_INIT(ledb);
995         IBM_HANDLE_INIT(lght);
996         IBM_HANDLE_INIT(cmos);
997
998         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
999         light_supported = (cmos_handle || lght_handle) && !ledb_handle;
1000
1001         if (light_supported)
1002                 /* light status not supported on
1003                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1004                 light_status_supported = acpi_evalf(ec_handle, NULL,
1005                                                     "KBLT", "qv");
1006
1007         return (light_supported)? 0 : 1;
1008 }
1009
1010 static int light_read(char *p)
1011 {
1012         int len = 0;
1013         int status = 0;
1014
1015         if (!light_supported) {
1016                 len += sprintf(p + len, "status:\t\tnot supported\n");
1017         } else if (!light_status_supported) {
1018                 len += sprintf(p + len, "status:\t\tunknown\n");
1019                 len += sprintf(p + len, "commands:\ton, off\n");
1020         } else {
1021                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1022                         return -EIO;
1023                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1024                 len += sprintf(p + len, "commands:\ton, off\n");
1025         }
1026
1027         return len;
1028 }
1029
1030 static int light_write(char *buf)
1031 {
1032         int cmos_cmd, lght_cmd;
1033         char *cmd;
1034         int success;
1035
1036         if (!light_supported)
1037                 return -ENODEV;
1038
1039         while ((cmd = next_cmd(&buf))) {
1040                 if (strlencmp(cmd, "on") == 0) {
1041                         cmos_cmd = 0x0c;
1042                         lght_cmd = 1;
1043                 } else if (strlencmp(cmd, "off") == 0) {
1044                         cmos_cmd = 0x0d;
1045                         lght_cmd = 0;
1046                 } else
1047                         return -EINVAL;
1048
1049                 success = cmos_handle ?
1050                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1051                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1052                 if (!success)
1053                         return -EIO;
1054         }
1055
1056         return 0;
1057 }
1058
1059 /*************************************************************************
1060  * Dock subdriver
1061  */
1062
1063 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1064
1065 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
1066            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1067            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
1068            "\\_SB.PCI.ISA.SLCE",        /* 570 */
1069     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1070
1071 /* don't list other alternatives as we install a notify handler on the 570 */
1072 IBM_HANDLE(pci, root, "\\_SB.PCI");     /* 570 */
1073
1074 #define dock_docked() (_sta(dock_handle) & 1)
1075
1076 static int dock_init(void)
1077 {
1078         IBM_HANDLE_INIT(dock);
1079         IBM_HANDLE_INIT(pci);
1080
1081         return (dock_handle)? 0 : 1;
1082 }
1083
1084 static void dock_notify(struct ibm_struct *ibm, u32 event)
1085 {
1086         int docked = dock_docked();
1087         int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1088
1089         if (event == 1 && !pci) /* 570 */
1090                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1091         else if (event == 1 && pci)     /* 570 */
1092                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1093         else if (event == 3 && docked)
1094                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1095         else if (event == 3 && !docked)
1096                 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1097         else if (event == 0 && docked)
1098                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1099         else {
1100                 printk(IBM_ERR "unknown dock event %d, status %d\n",
1101                        event, _sta(dock_handle));
1102                 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1103         }
1104 }
1105
1106 static int dock_read(char *p)
1107 {
1108         int len = 0;
1109         int docked = dock_docked();
1110
1111         if (!dock_handle)
1112                 len += sprintf(p + len, "status:\t\tnot supported\n");
1113         else if (!docked)
1114                 len += sprintf(p + len, "status:\t\tundocked\n");
1115         else {
1116                 len += sprintf(p + len, "status:\t\tdocked\n");
1117                 len += sprintf(p + len, "commands:\tdock, undock\n");
1118         }
1119
1120         return len;
1121 }
1122
1123 static int dock_write(char *buf)
1124 {
1125         char *cmd;
1126
1127         if (!dock_docked())
1128                 return -ENODEV;
1129
1130         while ((cmd = next_cmd(&buf))) {
1131                 if (strlencmp(cmd, "undock") == 0) {
1132                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1133                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1134                                 return -EIO;
1135                 } else if (strlencmp(cmd, "dock") == 0) {
1136                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1137                                 return -EIO;
1138                 } else
1139                         return -EINVAL;
1140         }
1141
1142         return 0;
1143 }
1144
1145 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1146
1147 /*************************************************************************
1148  * Bay subdriver
1149  */
1150
1151 #ifdef CONFIG_THINKPAD_ACPI_BAY
1152 static int bay_status_supported;
1153 static int bay_status2_supported;
1154 static int bay_eject_supported;
1155 static int bay_eject2_supported;
1156
1157 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",        /* 570 */
1158            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1159            "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1160            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1161            );                           /* A21e, R30, R31 */
1162 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1163            "_EJ0",              /* all others */
1164            );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1165 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",     /* A3x, R32 */
1166            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1167            );                           /* all others */
1168 IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
1169            "_EJ0",                      /* 770x */
1170            );                           /* all others */
1171
1172 static int bay_init(void)
1173 {
1174         IBM_HANDLE_INIT(bay);
1175         if (bay_handle)
1176                 IBM_HANDLE_INIT(bay_ej);
1177         IBM_HANDLE_INIT(bay2);
1178         if (bay2_handle)
1179                 IBM_HANDLE_INIT(bay2_ej);
1180
1181         bay_status_supported = bay_handle &&
1182             acpi_evalf(bay_handle, NULL, "_STA", "qv");
1183         bay_status2_supported = bay2_handle &&
1184             acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1185
1186         bay_eject_supported = bay_handle && bay_ej_handle &&
1187             (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1188         bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1189             (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1190
1191         return (bay_status_supported || bay_eject_supported ||
1192                 bay_status2_supported || bay_eject2_supported)? 0 : 1;
1193 }
1194
1195 static void bay_notify(struct ibm_struct *ibm, u32 event)
1196 {
1197         acpi_bus_generate_event(ibm->device, event, 0);
1198 }
1199
1200 #define bay_occupied(b) (_sta(b##_handle) & 1)
1201
1202 static int bay_read(char *p)
1203 {
1204         int len = 0;
1205         int occupied = bay_occupied(bay);
1206         int occupied2 = bay_occupied(bay2);
1207         int eject, eject2;
1208
1209         len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1210                        (occupied ? "occupied" : "unoccupied") :
1211                        "not supported");
1212         if (bay_status2_supported)
1213                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1214                                "occupied" : "unoccupied");
1215
1216         eject = bay_eject_supported && occupied;
1217         eject2 = bay_eject2_supported && occupied2;
1218
1219         if (eject && eject2)
1220                 len += sprintf(p + len, "commands:\teject, eject2\n");
1221         else if (eject)
1222                 len += sprintf(p + len, "commands:\teject\n");
1223         else if (eject2)
1224                 len += sprintf(p + len, "commands:\teject2\n");
1225
1226         return len;
1227 }
1228
1229 static int bay_write(char *buf)
1230 {
1231         char *cmd;
1232
1233         if (!bay_eject_supported && !bay_eject2_supported)
1234                 return -ENODEV;
1235
1236         while ((cmd = next_cmd(&buf))) {
1237                 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1238                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1239                                 return -EIO;
1240                 } else if (bay_eject2_supported &&
1241                            strlencmp(cmd, "eject2") == 0) {
1242                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1243                                 return -EIO;
1244                 } else
1245                         return -EINVAL;
1246         }
1247
1248         return 0;
1249 }
1250 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1251
1252 /*************************************************************************
1253  * CMOS subdriver
1254  */
1255
1256 static int cmos_init(void)
1257 {
1258         IBM_HANDLE_INIT(cmos);
1259
1260         return (cmos_handle)? 0 : 1;
1261 }
1262
1263 static int cmos_eval(int cmos_cmd)
1264 {
1265         if (cmos_handle)
1266                 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1267         else
1268                 return 1;
1269 }
1270
1271 static int cmos_read(char *p)
1272 {
1273         int len = 0;
1274
1275         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1276            R30, R31, T20-22, X20-21 */
1277         if (!cmos_handle)
1278                 len += sprintf(p + len, "status:\t\tnot supported\n");
1279         else {
1280                 len += sprintf(p + len, "status:\t\tsupported\n");
1281                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1282         }
1283
1284         return len;
1285 }
1286
1287 static int cmos_write(char *buf)
1288 {
1289         char *cmd;
1290         int cmos_cmd;
1291
1292         if (!cmos_handle)
1293                 return -EINVAL;
1294
1295         while ((cmd = next_cmd(&buf))) {
1296                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1297                     cmos_cmd >= 0 && cmos_cmd <= 21) {
1298                         /* cmos_cmd set */
1299                 } else
1300                         return -EINVAL;
1301
1302                 if (!cmos_eval(cmos_cmd))
1303                         return -EIO;
1304         }
1305
1306         return 0;
1307 }
1308
1309
1310 /*************************************************************************
1311  * LED subdriver
1312  */
1313
1314 static enum led_access_mode led_supported;
1315
1316 IBM_HANDLE(led, ec, "SLED",     /* 570 */
1317            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1318            "LED",               /* all others */
1319            );                   /* R30, R31 */
1320
1321 static int led_init(void)
1322 {
1323         IBM_HANDLE_INIT(led);
1324
1325         if (!led_handle)
1326                 /* led not supported on R30, R31 */
1327                 led_supported = TPACPI_LED_NONE;
1328         else if (strlencmp(led_path, "SLED") == 0)
1329                 /* 570 */
1330                 led_supported = TPACPI_LED_570;
1331         else if (strlencmp(led_path, "SYSL") == 0)
1332                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1333                 led_supported = TPACPI_LED_OLD;
1334         else
1335                 /* all others */
1336                 led_supported = TPACPI_LED_NEW;
1337
1338         return (led_supported != TPACPI_LED_NONE)? 0 : 1;
1339 }
1340
1341 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1342
1343 static int led_read(char *p)
1344 {
1345         int len = 0;
1346
1347         if (!led_supported) {
1348                 len += sprintf(p + len, "status:\t\tnot supported\n");
1349                 return len;
1350         }
1351         len += sprintf(p + len, "status:\t\tsupported\n");
1352
1353         if (led_supported == TPACPI_LED_570) {
1354                 /* 570 */
1355                 int i, status;
1356                 for (i = 0; i < 8; i++) {
1357                         if (!acpi_evalf(ec_handle,
1358                                         &status, "GLED", "dd", 1 << i))
1359                                 return -EIO;
1360                         len += sprintf(p + len, "%d:\t\t%s\n",
1361                                        i, led_status(status));
1362                 }
1363         }
1364
1365         len += sprintf(p + len, "commands:\t"
1366                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1367
1368         return len;
1369 }
1370
1371 /* off, on, blink */
1372 static const int led_sled_arg1[] = { 0, 1, 3 };
1373 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
1374 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
1375 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1376
1377 static int led_write(char *buf)
1378 {
1379         char *cmd;
1380         int led, ind, ret;
1381
1382         if (!led_supported)
1383                 return -ENODEV;
1384
1385         while ((cmd = next_cmd(&buf))) {
1386                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1387                         return -EINVAL;
1388
1389                 if (strstr(cmd, "off")) {
1390                         ind = 0;
1391                 } else if (strstr(cmd, "on")) {
1392                         ind = 1;
1393                 } else if (strstr(cmd, "blink")) {
1394                         ind = 2;
1395                 } else
1396                         return -EINVAL;
1397
1398                 if (led_supported == TPACPI_LED_570) {
1399                         /* 570 */
1400                         led = 1 << led;
1401                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1402                                         led, led_sled_arg1[ind]))
1403                                 return -EIO;
1404                 } else if (led_supported == TPACPI_LED_OLD) {
1405                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1406                         led = 1 << led;
1407                         ret = ec_write(TPACPI_LED_EC_HLMS, led);
1408                         if (ret >= 0)
1409                                 ret =
1410                                     ec_write(TPACPI_LED_EC_HLBL,
1411                                              led * led_exp_hlbl[ind]);
1412                         if (ret >= 0)
1413                                 ret =
1414                                     ec_write(TPACPI_LED_EC_HLCL,
1415                                              led * led_exp_hlcl[ind]);
1416                         if (ret < 0)
1417                                 return ret;
1418                 } else {
1419                         /* all others */
1420                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1421                                         led, led_led_arg1[ind]))
1422                                 return -EIO;
1423                 }
1424         }
1425
1426         return 0;
1427 }
1428
1429 /*************************************************************************
1430  * Beep subdriver
1431  */
1432
1433 IBM_HANDLE(beep, ec, "BEEP");   /* all except R30, R31 */
1434
1435 static int beep_init(void)
1436 {
1437         IBM_HANDLE_INIT(beep);
1438
1439         return (beep_handle)? 0 : 1;
1440 }
1441
1442 static int beep_read(char *p)
1443 {
1444         int len = 0;
1445
1446         if (!beep_handle)
1447                 len += sprintf(p + len, "status:\t\tnot supported\n");
1448         else {
1449                 len += sprintf(p + len, "status:\t\tsupported\n");
1450                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1451         }
1452
1453         return len;
1454 }
1455
1456 static int beep_write(char *buf)
1457 {
1458         char *cmd;
1459         int beep_cmd;
1460
1461         if (!beep_handle)
1462                 return -ENODEV;
1463
1464         while ((cmd = next_cmd(&buf))) {
1465                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1466                     beep_cmd >= 0 && beep_cmd <= 17) {
1467                         /* beep_cmd set */
1468                 } else
1469                         return -EINVAL;
1470                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1471                         return -EIO;
1472         }
1473
1474         return 0;
1475 }
1476
1477 /*************************************************************************
1478  * Thermal subdriver
1479  */
1480
1481 static enum thermal_access_mode thermal_read_mode;
1482
1483 static int thermal_init(void)
1484 {
1485         u8 t, ta1, ta2;
1486         int i;
1487         int acpi_tmp7;
1488
1489         acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1490
1491         if (ibm_thinkpad_ec_found && experimental) {
1492                 /*
1493                  * Direct EC access mode: sensors at registers
1494                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
1495                  * non-implemented, thermal sensors return 0x80 when
1496                  * not available
1497                  */
1498
1499                 ta1 = ta2 = 0;
1500                 for (i = 0; i < 8; i++) {
1501                         if (likely(acpi_ec_read(0x78 + i, &t))) {
1502                                 ta1 |= t;
1503                         } else {
1504                                 ta1 = 0;
1505                                 break;
1506                         }
1507                         if (likely(acpi_ec_read(0xC0 + i, &t))) {
1508                                 ta2 |= t;
1509                         } else {
1510                                 ta1 = 0;
1511                                 break;
1512                         }
1513                 }
1514                 if (ta1 == 0) {
1515                         /* This is sheer paranoia, but we handle it anyway */
1516                         if (acpi_tmp7) {
1517                                 printk(IBM_ERR
1518                                        "ThinkPad ACPI EC access misbehaving, "
1519                                        "falling back to ACPI TMPx access mode\n");
1520                                 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
1521                         } else {
1522                                 printk(IBM_ERR
1523                                        "ThinkPad ACPI EC access misbehaving, "
1524                                        "disabling thermal sensors access\n");
1525                                 thermal_read_mode = TPACPI_THERMAL_NONE;
1526                         }
1527                 } else {
1528                         thermal_read_mode =
1529                             (ta2 != 0) ?
1530                             TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
1531                 }
1532         } else if (acpi_tmp7) {
1533                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1534                         /* 600e/x, 770e, 770x */
1535                         thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
1536                 } else {
1537                         /* Standard ACPI TMPx access, max 8 sensors */
1538                         thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
1539                 }
1540         } else {
1541                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1542                 thermal_read_mode = TPACPI_THERMAL_NONE;
1543         }
1544
1545         return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
1546 }
1547
1548 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1549 {
1550         int i, t;
1551         s8 tmp;
1552         char tmpi[] = "TMPi";
1553
1554         if (!s)
1555                 return -EINVAL;
1556
1557         switch (thermal_read_mode) {
1558 #if TPACPI_MAX_THERMAL_SENSORS >= 16
1559         case TPACPI_THERMAL_TPEC_16:
1560                 for (i = 0; i < 8; i++) {
1561                         if (!acpi_ec_read(0xC0 + i, &tmp))
1562                                 return -EIO;
1563                         s->temp[i + 8] = tmp * 1000;
1564                 }
1565                 /* fallthrough */
1566 #endif
1567         case TPACPI_THERMAL_TPEC_8:
1568                 for (i = 0; i < 8; i++) {
1569                         if (!acpi_ec_read(0x78 + i, &tmp))
1570                                 return -EIO;
1571                         s->temp[i] = tmp * 1000;
1572                 }
1573                 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
1574
1575         case TPACPI_THERMAL_ACPI_UPDT:
1576                 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1577                         return -EIO;
1578                 for (i = 0; i < 8; i++) {
1579                         tmpi[3] = '0' + i;
1580                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1581                                 return -EIO;
1582                         s->temp[i] = (t - 2732) * 100;
1583                 }
1584                 return 8;
1585
1586         case TPACPI_THERMAL_ACPI_TMP07:
1587                 for (i = 0; i < 8; i++) {
1588                         tmpi[3] = '0' + i;
1589                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1590                                 return -EIO;
1591                         s->temp[i] = t * 1000;
1592                 }
1593                 return 8;
1594
1595         case TPACPI_THERMAL_NONE:
1596         default:
1597                 return 0;
1598         }
1599 }
1600
1601 static int thermal_read(char *p)
1602 {
1603         int len = 0;
1604         int n, i;
1605         struct ibm_thermal_sensors_struct t;
1606
1607         n = thermal_get_sensors(&t);
1608         if (unlikely(n < 0))
1609                 return n;
1610
1611         len += sprintf(p + len, "temperatures:\t");
1612
1613         if (n > 0) {
1614                 for (i = 0; i < (n - 1); i++)
1615                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1616                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1617         } else
1618                 len += sprintf(p + len, "not supported\n");
1619
1620         return len;
1621 }
1622
1623 /*************************************************************************
1624  * EC Dump subdriver
1625  */
1626
1627 static u8 ecdump_regs[256];
1628
1629 static int ecdump_read(char *p)
1630 {
1631         int len = 0;
1632         int i, j;
1633         u8 v;
1634
1635         len += sprintf(p + len, "EC      "
1636                        " +00 +01 +02 +03 +04 +05 +06 +07"
1637                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1638         for (i = 0; i < 256; i += 16) {
1639                 len += sprintf(p + len, "EC 0x%02x:", i);
1640                 for (j = 0; j < 16; j++) {
1641                         if (!acpi_ec_read(i + j, &v))
1642                                 break;
1643                         if (v != ecdump_regs[i + j])
1644                                 len += sprintf(p + len, " *%02x", v);
1645                         else
1646                                 len += sprintf(p + len, "  %02x", v);
1647                         ecdump_regs[i + j] = v;
1648                 }
1649                 len += sprintf(p + len, "\n");
1650                 if (j != 16)
1651                         break;
1652         }
1653
1654         /* These are way too dangerous to advertise openly... */
1655 #if 0
1656         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1657                        " (<offset> is 00-ff, <value> is 00-ff)\n");
1658         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
1659                        " (<offset> is 00-ff, <value> is 0-255)\n");
1660 #endif
1661         return len;
1662 }
1663
1664 static int ecdump_write(char *buf)
1665 {
1666         char *cmd;
1667         int i, v;
1668
1669         while ((cmd = next_cmd(&buf))) {
1670                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1671                         /* i and v set */
1672                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1673                         /* i and v set */
1674                 } else
1675                         return -EINVAL;
1676                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1677                         if (!acpi_ec_write(i, v))
1678                                 return -EIO;
1679                 } else
1680                         return -EINVAL;
1681         }
1682
1683         return 0;
1684 }
1685
1686 /*************************************************************************
1687  * Backlight/brightness subdriver
1688  */
1689
1690 static struct backlight_device *ibm_backlight_device = NULL;
1691
1692 static struct backlight_ops ibm_backlight_data = {
1693         .get_brightness = brightness_get,
1694         .update_status  = brightness_update_status,
1695 };
1696
1697 static int brightness_init(void)
1698 {
1699         int b;
1700
1701         b = brightness_get(NULL);
1702         if (b < 0)
1703                 return b;
1704
1705         ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
1706                                                          &ibm_backlight_data);
1707         if (IS_ERR(ibm_backlight_device)) {
1708                 printk(IBM_ERR "Could not register backlight device\n");
1709                 return PTR_ERR(ibm_backlight_device);
1710         }
1711
1712         ibm_backlight_device->props.max_brightness = 7;
1713         ibm_backlight_device->props.brightness = b;
1714         backlight_update_status(ibm_backlight_device);
1715
1716         return 0;
1717 }
1718
1719 static void brightness_exit(void)
1720 {
1721         if (ibm_backlight_device) {
1722                 backlight_device_unregister(ibm_backlight_device);
1723                 ibm_backlight_device = NULL;
1724         }
1725 }
1726
1727 static int brightness_update_status(struct backlight_device *bd)
1728 {
1729         return brightness_set(
1730                 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1731                  bd->props.power == FB_BLANK_UNBLANK) ?
1732                                 bd->props.brightness : 0);
1733 }
1734
1735 static int brightness_get(struct backlight_device *bd)
1736 {
1737         u8 level;
1738         if (!acpi_ec_read(brightness_offset, &level))
1739                 return -EIO;
1740
1741         level &= 0x7;
1742
1743         return level;
1744 }
1745
1746 static int brightness_set(int value)
1747 {
1748         int cmos_cmd, inc, i;
1749         int current_value = brightness_get(NULL);
1750
1751         value &= 7;
1752
1753         cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1754         inc = value > current_value ? 1 : -1;
1755         for (i = current_value; i != value; i += inc) {
1756                 if (!cmos_eval(cmos_cmd))
1757                         return -EIO;
1758                 if (!acpi_ec_write(brightness_offset, i + inc))
1759                         return -EIO;
1760         }
1761
1762         return 0;
1763 }
1764
1765 static int brightness_read(char *p)
1766 {
1767         int len = 0;
1768         int level;
1769
1770         if ((level = brightness_get(NULL)) < 0) {
1771                 len += sprintf(p + len, "level:\t\tunreadable\n");
1772         } else {
1773                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1774                 len += sprintf(p + len, "commands:\tup, down\n");
1775                 len += sprintf(p + len, "commands:\tlevel <level>"
1776                                " (<level> is 0-7)\n");
1777         }
1778
1779         return len;
1780 }
1781
1782 static int brightness_write(char *buf)
1783 {
1784         int level;
1785         int new_level;
1786         char *cmd;
1787
1788         while ((cmd = next_cmd(&buf))) {
1789                 if ((level = brightness_get(NULL)) < 0)
1790                         return level;
1791                 level &= 7;
1792
1793                 if (strlencmp(cmd, "up") == 0) {
1794                         new_level = level == 7 ? 7 : level + 1;
1795                 } else if (strlencmp(cmd, "down") == 0) {
1796                         new_level = level == 0 ? 0 : level - 1;
1797                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1798                            new_level >= 0 && new_level <= 7) {
1799                         /* new_level set */
1800                 } else
1801                         return -EINVAL;
1802
1803                 brightness_set(new_level);
1804         }
1805
1806         return 0;
1807 }
1808
1809 /*************************************************************************
1810  * Volume subdriver
1811  */
1812
1813 static int volume_read(char *p)
1814 {
1815         int len = 0;
1816         u8 level;
1817
1818         if (!acpi_ec_read(volume_offset, &level)) {
1819                 len += sprintf(p + len, "level:\t\tunreadable\n");
1820         } else {
1821                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1822                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1823                 len += sprintf(p + len, "commands:\tup, down, mute\n");
1824                 len += sprintf(p + len, "commands:\tlevel <level>"
1825                                " (<level> is 0-15)\n");
1826         }
1827
1828         return len;
1829 }
1830
1831 static int volume_write(char *buf)
1832 {
1833         int cmos_cmd, inc, i;
1834         u8 level, mute;
1835         int new_level, new_mute;
1836         char *cmd;
1837
1838         while ((cmd = next_cmd(&buf))) {
1839                 if (!acpi_ec_read(volume_offset, &level))
1840                         return -EIO;
1841                 new_mute = mute = level & 0x40;
1842                 new_level = level = level & 0xf;
1843
1844                 if (strlencmp(cmd, "up") == 0) {
1845                         if (mute)
1846                                 new_mute = 0;
1847                         else
1848                                 new_level = level == 15 ? 15 : level + 1;
1849                 } else if (strlencmp(cmd, "down") == 0) {
1850                         if (mute)
1851                                 new_mute = 0;
1852                         else
1853                                 new_level = level == 0 ? 0 : level - 1;
1854                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1855                            new_level >= 0 && new_level <= 15) {
1856                         /* new_level set */
1857                 } else if (strlencmp(cmd, "mute") == 0) {
1858                         new_mute = 0x40;
1859                 } else
1860                         return -EINVAL;
1861
1862                 if (new_level != level) {       /* mute doesn't change */
1863                         cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
1864                         inc = new_level > level ? 1 : -1;
1865
1866                         if (mute && (!cmos_eval(cmos_cmd) ||
1867                                      !acpi_ec_write(volume_offset, level)))
1868                                 return -EIO;
1869
1870                         for (i = level; i != new_level; i += inc)
1871                                 if (!cmos_eval(cmos_cmd) ||
1872                                     !acpi_ec_write(volume_offset, i + inc))
1873                                         return -EIO;
1874
1875                         if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
1876                                      !acpi_ec_write(volume_offset,
1877                                                     new_level + mute)))
1878                                 return -EIO;
1879                 }
1880
1881                 if (new_mute != mute) { /* level doesn't change */
1882                         cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
1883
1884                         if (!cmos_eval(cmos_cmd) ||
1885                             !acpi_ec_write(volume_offset, level + new_mute))
1886                                 return -EIO;
1887                 }
1888         }
1889
1890         return 0;
1891 }
1892
1893
1894 /*************************************************************************
1895  * Fan subdriver
1896  */
1897
1898 /*
1899  * FAN ACCESS MODES
1900  *
1901  * TPACPI_FAN_RD_ACPI_GFAN:
1902  *      ACPI GFAN method: returns fan level
1903  *
1904  *      see TPACPI_FAN_WR_ACPI_SFAN
1905  *      EC 0x2f (HFSP) not available if GFAN exists
1906  *
1907  * TPACPI_FAN_WR_ACPI_SFAN:
1908  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1909  *
1910  *      EC 0x2f (HFSP) might be available *for reading*, but do not use
1911  *      it for writing.
1912  *
1913  * TPACPI_FAN_WR_TPEC:
1914  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode
1915  *      Supported on almost all ThinkPads
1916  *
1917  *      Fan speed changes of any sort (including those caused by the
1918  *      disengaged mode) are usually done slowly by the firmware as the
1919  *      maximum ammount of fan duty cycle change per second seems to be
1920  *      limited.
1921  *
1922  *      Reading is not available if GFAN exists.
1923  *      Writing is not available if SFAN exists.
1924  *
1925  *      Bits
1926  *       7      automatic mode engaged;
1927  *              (default operation mode of the ThinkPad)
1928  *              fan level is ignored in this mode.
1929  *       6      full speed mode (takes precedence over bit 7);
1930  *              not available on all thinkpads.  May disable
1931  *              the tachometer while the fan controller ramps up
1932  *              the speed (which can take up to a few *minutes*).
1933  *              Speeds up fan to 100% duty-cycle, which is far above
1934  *              the standard RPM levels.  It is not impossible that
1935  *              it could cause hardware damage.
1936  *      5-3     unused in some models.  Extra bits for fan level
1937  *              in others, but still useless as all values above
1938  *              7 map to the same speed as level 7 in these models.
1939  *      2-0     fan level (0..7 usually)
1940  *                      0x00 = stop
1941  *                      0x07 = max (set when temperatures critical)
1942  *              Some ThinkPads may have other levels, see
1943  *              TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
1944  *
1945  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1946  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1947  *      does so, its initial value is meaningless (0x07).
1948  *
1949  *      For firmware bugs, refer to:
1950  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1951  *
1952  *      ----
1953  *
1954  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1955  *      Main fan tachometer reading (in RPM)
1956  *
1957  *      This register is present on all ThinkPads with a new-style EC, and
1958  *      it is known not to be present on the A21m/e, and T22, as there is
1959  *      something else in offset 0x84 according to the ACPI DSDT.  Other
1960  *      ThinkPads from this same time period (and earlier) probably lack the
1961  *      tachometer as well.
1962  *
1963  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1964  *      was never fixed by IBM to report the EC firmware version string
1965  *      probably support the tachometer (like the early X models), so
1966  *      detecting it is quite hard.  We need more data to know for sure.
1967  *
1968  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1969  *      might result.
1970  *
1971  *      FIRMWARE BUG: may go stale while the EC is switching to full speed
1972  *      mode.
1973  *
1974  *      For firmware bugs, refer to:
1975  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1976  *
1977  * TPACPI_FAN_WR_ACPI_FANS:
1978  *      ThinkPad X31, X40, X41.  Not available in the X60.
1979  *
1980  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
1981  *      high speed.  ACPI DSDT seems to map these three speeds to levels
1982  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1983  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1984  *
1985  *      The speeds are stored on handles
1986  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1987  *
1988  *      There are three default speed sets, acessible as handles:
1989  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1990  *
1991  *      ACPI DSDT switches which set is in use depending on various
1992  *      factors.
1993  *
1994  *      TPACPI_FAN_WR_TPEC is also available and should be used to
1995  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
1996  *      but the ACPI tables just mention level 7.
1997  */
1998
1999 static enum fan_status_access_mode fan_status_access_mode;
2000 static enum fan_control_access_mode fan_control_access_mode;
2001 static enum fan_control_commands fan_control_commands;
2002
2003 static int fan_control_status_known;
2004 static u8 fan_control_initial_status;
2005
2006 static void fan_watchdog_fire(struct work_struct *ignored);
2007 static int fan_watchdog_maxinterval;
2008 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
2009
2010 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40, X41 */
2011 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
2012            "\\FSPD",            /* 600e/x, 770e, 770x */
2013            );                   /* all others */
2014 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
2015            "JFNS",              /* 770x-JL */
2016            );                   /* all others */
2017
2018 static int fan_init(void)
2019 {
2020         fan_status_access_mode = TPACPI_FAN_NONE;
2021         fan_control_access_mode = TPACPI_FAN_WR_NONE;
2022         fan_control_commands = 0;
2023         fan_control_status_known = 1;
2024         fan_watchdog_maxinterval = 0;
2025
2026         IBM_HANDLE_INIT(fans);
2027         IBM_HANDLE_INIT(gfan);
2028         IBM_HANDLE_INIT(sfan);
2029
2030         if (gfan_handle) {
2031                 /* 570, 600e/x, 770e, 770x */
2032                 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
2033         } else {
2034                 /* all other ThinkPads: note that even old-style
2035                  * ThinkPad ECs supports the fan control register */
2036                 if (likely(acpi_ec_read(fan_status_offset,
2037                                         &fan_control_initial_status))) {
2038                         fan_status_access_mode = TPACPI_FAN_RD_TPEC;
2039
2040                         /* In some ThinkPads, neither the EC nor the ACPI
2041                          * DSDT initialize the fan status, and it ends up
2042                          * being set to 0x07 when it *could* be either
2043                          * 0x07 or 0x80.
2044                          *
2045                          * Enable for TP-1Y (T43), TP-78 (R51e),
2046                          * TP-76 (R52), TP-70 (T43, R52), which are known
2047                          * to be buggy. */
2048                         if (fan_control_initial_status == 0x07 &&
2049                             ibm_thinkpad_ec_found &&
2050                             ((ibm_thinkpad_ec_found[0] == '1' &&
2051                               ibm_thinkpad_ec_found[1] == 'Y') ||
2052                              (ibm_thinkpad_ec_found[0] == '7' &&
2053                               (ibm_thinkpad_ec_found[1] == '6' ||
2054                                ibm_thinkpad_ec_found[1] == '8' ||
2055                                ibm_thinkpad_ec_found[1] == '0'))
2056                             )) {
2057                                 printk(IBM_NOTICE
2058                                        "fan_init: initial fan status is "
2059                                        "unknown, assuming it is in auto "
2060                                        "mode\n");
2061                                 fan_control_status_known = 0;
2062                         }
2063                 } else {
2064                         printk(IBM_ERR
2065                                "ThinkPad ACPI EC access misbehaving, "
2066                                "fan status and control unavailable\n");
2067                         return 1;
2068                 }
2069         }
2070
2071         if (sfan_handle) {
2072                 /* 570, 770x-JL */
2073                 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
2074                 fan_control_commands |=
2075                     TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
2076         } else {
2077                 if (!gfan_handle) {
2078                         /* gfan without sfan means no fan control */
2079                         /* all other models implement TP EC 0x2f control */
2080
2081                         if (fans_handle) {
2082                                 /* X31, X40, X41 */
2083                                 fan_control_access_mode =
2084                                     TPACPI_FAN_WR_ACPI_FANS;
2085                                 fan_control_commands |=
2086                                     TPACPI_FAN_CMD_SPEED |
2087                                     TPACPI_FAN_CMD_LEVEL |
2088                                     TPACPI_FAN_CMD_ENABLE;
2089                         } else {
2090                                 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
2091                                 fan_control_commands |=
2092                                     TPACPI_FAN_CMD_LEVEL |
2093                                     TPACPI_FAN_CMD_ENABLE;
2094                         }
2095                 }
2096         }
2097
2098         return (fan_status_access_mode != TPACPI_FAN_NONE ||
2099                 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2100                         0 : 1;
2101 }
2102
2103 static int fan_get_status(u8 *status)
2104 {
2105         u8 s;
2106
2107         /* TODO:
2108          * Add TPACPI_FAN_RD_ACPI_FANS ? */
2109
2110         switch (fan_status_access_mode) {
2111         case TPACPI_FAN_RD_ACPI_GFAN:
2112                 /* 570, 600e/x, 770e, 770x */
2113
2114                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2115                         return -EIO;
2116
2117                 if (likely(status))
2118                         *status = s & 0x07;
2119
2120                 break;
2121
2122         case TPACPI_FAN_RD_TPEC:
2123                 /* all except 570, 600e/x, 770e, 770x */
2124                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2125                         return -EIO;
2126
2127                 if (likely(status))
2128                         *status = s;
2129
2130                 break;
2131
2132         default:
2133                 return -ENXIO;
2134         }
2135
2136         return 0;
2137 }
2138
2139 static void fan_exit(void)
2140 {
2141         cancel_delayed_work(&fan_watchdog_task);
2142         flush_scheduled_work();
2143 }
2144
2145 static int fan_get_speed(unsigned int *speed)
2146 {
2147         u8 hi, lo;
2148
2149         switch (fan_status_access_mode) {
2150         case TPACPI_FAN_RD_TPEC:
2151                 /* all except 570, 600e/x, 770e, 770x */
2152                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2153                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2154                         return -EIO;
2155
2156                 if (likely(speed))
2157                         *speed = (hi << 8) | lo;
2158
2159                 break;
2160
2161         default:
2162                 return -ENXIO;
2163         }
2164
2165         return 0;
2166 }
2167
2168 static void fan_watchdog_fire(struct work_struct *ignored)
2169 {
2170         printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2171         if (fan_set_enable()) {
2172                 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2173                 /* reschedule for later */
2174                 fan_watchdog_reset();
2175         }
2176 }
2177
2178 static void fan_watchdog_reset(void)
2179 {
2180         static int fan_watchdog_active = 0;
2181
2182         if (fan_watchdog_active)
2183                 cancel_delayed_work(&fan_watchdog_task);
2184
2185         if (fan_watchdog_maxinterval > 0) {
2186                 fan_watchdog_active = 1;
2187                 if (!schedule_delayed_work(&fan_watchdog_task,
2188                                 msecs_to_jiffies(fan_watchdog_maxinterval
2189                                                  * 1000))) {
2190                         printk(IBM_ERR "failed to schedule the fan watchdog, "
2191                                "watchdog will not trigger\n");
2192                 }
2193         } else
2194                 fan_watchdog_active = 0;
2195 }
2196
2197 static int fan_set_level(int level)
2198 {
2199         switch (fan_control_access_mode) {
2200         case TPACPI_FAN_WR_ACPI_SFAN:
2201                 if (level >= 0 && level <= 7) {
2202                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2203                                 return -EIO;
2204                 } else
2205                         return -EINVAL;
2206                 break;
2207
2208         case TPACPI_FAN_WR_ACPI_FANS:
2209         case TPACPI_FAN_WR_TPEC:
2210                 if ((level != TP_EC_FAN_AUTO) &&
2211                     (level != TP_EC_FAN_FULLSPEED) &&
2212                     ((level < 0) || (level > 7)))
2213                         return -EINVAL;
2214
2215                 if (!acpi_ec_write(fan_status_offset, level))
2216                         return -EIO;
2217                 else
2218                         fan_control_status_known = 1;
2219                 break;
2220
2221         default:
2222                 return -ENXIO;
2223         }
2224         return 0;
2225 }
2226
2227 static int fan_set_enable(void)
2228 {
2229         u8 s;
2230         int rc;
2231
2232         switch (fan_control_access_mode) {
2233         case TPACPI_FAN_WR_ACPI_FANS:
2234         case TPACPI_FAN_WR_TPEC:
2235                 if ((rc = fan_get_status(&s)) < 0)
2236                         return rc;
2237
2238                 /* Don't go out of emergency fan mode */
2239                 if (s != 7)
2240                         s = TP_EC_FAN_AUTO;
2241
2242                 if (!acpi_ec_write(fan_status_offset, s))
2243                         return -EIO;
2244                 else
2245                         fan_control_status_known = 1;
2246                 break;
2247
2248         case TPACPI_FAN_WR_ACPI_SFAN:
2249                 if ((rc = fan_get_status(&s)) < 0)
2250                         return rc;
2251
2252                 s &= 0x07;
2253
2254                 /* Set fan to at least level 4 */
2255                 if (s < 4)
2256                         s = 4;
2257
2258                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2259                         return -EIO;
2260                 break;
2261
2262         default:
2263                 return -ENXIO;
2264         }
2265         return 0;
2266 }
2267
2268 static int fan_set_disable(void)
2269 {
2270         switch (fan_control_access_mode) {
2271         case TPACPI_FAN_WR_ACPI_FANS:
2272         case TPACPI_FAN_WR_TPEC:
2273                 if (!acpi_ec_write(fan_status_offset, 0x00))
2274                         return -EIO;
2275                 else
2276                         fan_control_status_known = 1;
2277                 break;
2278
2279         case TPACPI_FAN_WR_ACPI_SFAN:
2280                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2281                         return -EIO;
2282                 break;
2283
2284         default:
2285                 return -ENXIO;
2286         }
2287         return 0;
2288 }
2289
2290 static int fan_set_speed(int speed)
2291 {
2292         switch (fan_control_access_mode) {
2293         case TPACPI_FAN_WR_ACPI_FANS:
2294                 if (speed >= 0 && speed <= 65535) {
2295                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2296                                         speed, speed, speed))
2297                                 return -EIO;
2298                 } else
2299                         return -EINVAL;
2300                 break;
2301
2302         default:
2303                 return -ENXIO;
2304         }
2305         return 0;
2306 }
2307
2308 static int fan_read(char *p)
2309 {
2310         int len = 0;
2311         int rc;
2312         u8 status;
2313         unsigned int speed = 0;
2314
2315         switch (fan_status_access_mode) {
2316         case TPACPI_FAN_RD_ACPI_GFAN:
2317                 /* 570, 600e/x, 770e, 770x */
2318                 if ((rc = fan_get_status(&status)) < 0)
2319                         return rc;
2320
2321                 len += sprintf(p + len, "status:\t\t%s\n"
2322                                "level:\t\t%d\n",
2323                                (status != 0) ? "enabled" : "disabled", status);
2324                 break;
2325
2326         case TPACPI_FAN_RD_TPEC:
2327                 /* all except 570, 600e/x, 770e, 770x */
2328                 if ((rc = fan_get_status(&status)) < 0)
2329                         return rc;
2330
2331                 if (unlikely(!fan_control_status_known)) {
2332                         if (status != fan_control_initial_status)
2333                                 fan_control_status_known = 1;
2334                         else
2335                                 /* Return most likely status. In fact, it
2336                                  * might be the only possible status */
2337                                 status = TP_EC_FAN_AUTO;
2338                 }
2339
2340                 len += sprintf(p + len, "status:\t\t%s\n",
2341                                (status != 0) ? "enabled" : "disabled");
2342
2343                 if ((rc = fan_get_speed(&speed)) < 0)
2344                         return rc;
2345
2346                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2347
2348                 if (status & TP_EC_FAN_FULLSPEED)
2349                         /* Disengaged mode takes precedence */
2350                         len += sprintf(p + len, "level:\t\tdisengaged\n");
2351                 else if (status & TP_EC_FAN_AUTO)
2352                         len += sprintf(p + len, "level:\t\tauto\n");
2353                 else
2354                         len += sprintf(p + len, "level:\t\t%d\n", status);
2355                 break;
2356
2357         case TPACPI_FAN_NONE:
2358         default:
2359                 len += sprintf(p + len, "status:\t\tnot supported\n");
2360         }
2361
2362         if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
2363                 len += sprintf(p + len, "commands:\tlevel <level>");
2364
2365                 switch (fan_control_access_mode) {
2366                 case TPACPI_FAN_WR_ACPI_SFAN:
2367                         len += sprintf(p + len, " (<level> is 0-7)\n");
2368                         break;
2369
2370                 default:
2371                         len += sprintf(p + len, " (<level> is 0-7, "
2372                                        "auto, disengaged)\n");
2373                         break;
2374                 }
2375         }
2376
2377         if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
2378                 len += sprintf(p + len, "commands:\tenable, disable\n"
2379                                "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2380                                "1-120 (seconds))\n");
2381
2382         if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
2383                 len += sprintf(p + len, "commands:\tspeed <speed>"
2384                                " (<speed> is 0-65535)\n");
2385
2386         return len;
2387 }
2388
2389 static int fan_write_cmd_level(const char *cmd, int *rc)
2390 {
2391         int level;
2392
2393         if (strlencmp(cmd, "level auto") == 0)
2394                 level = TP_EC_FAN_AUTO;
2395         else if (strlencmp(cmd, "level disengaged") == 0)
2396                 level = TP_EC_FAN_FULLSPEED;
2397         else if (sscanf(cmd, "level %d", &level) != 1)
2398                 return 0;
2399
2400         if ((*rc = fan_set_level(level)) == -ENXIO)
2401                 printk(IBM_ERR "level command accepted for unsupported "
2402                        "access mode %d", fan_control_access_mode);
2403
2404         return 1;
2405 }
2406
2407 static int fan_write_cmd_enable(const char *cmd, int *rc)
2408 {
2409         if (strlencmp(cmd, "enable") != 0)
2410                 return 0;
2411
2412         if ((*rc = fan_set_enable()) == -ENXIO)
2413                 printk(IBM_ERR "enable command accepted for unsupported "
2414                        "access mode %d", fan_control_access_mode);
2415
2416         return 1;
2417 }
2418
2419 static int fan_write_cmd_disable(const char *cmd, int *rc)
2420 {
2421         if (strlencmp(cmd, "disable") != 0)
2422                 return 0;
2423
2424         if ((*rc = fan_set_disable()) == -ENXIO)
2425                 printk(IBM_ERR "disable command accepted for unsupported "
2426                        "access mode %d", fan_control_access_mode);
2427
2428         return 1;
2429 }
2430
2431 static int fan_write_cmd_speed(const char *cmd, int *rc)
2432 {
2433         int speed;
2434
2435         /* TODO:
2436          * Support speed <low> <medium> <high> ? */
2437
2438         if (sscanf(cmd, "speed %d", &speed) != 1)
2439                 return 0;
2440
2441         if ((*rc = fan_set_speed(speed)) == -ENXIO)
2442                 printk(IBM_ERR "speed command accepted for unsupported "
2443                        "access mode %d", fan_control_access_mode);
2444
2445         return 1;
2446 }
2447
2448 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2449 {
2450         int interval;
2451
2452         if (sscanf(cmd, "watchdog %d", &interval) != 1)
2453                 return 0;
2454
2455         if (interval < 0 || interval > 120)
2456                 *rc = -EINVAL;
2457         else
2458                 fan_watchdog_maxinterval = interval;
2459
2460         return 1;
2461 }
2462
2463 static int fan_write(char *buf)
2464 {
2465         char *cmd;
2466         int rc = 0;
2467
2468         while (!rc && (cmd = next_cmd(&buf))) {
2469                 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
2470                       fan_write_cmd_level(cmd, &rc)) &&
2471                     !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
2472                       (fan_write_cmd_enable(cmd, &rc) ||
2473                        fan_write_cmd_disable(cmd, &rc) ||
2474                        fan_write_cmd_watchdog(cmd, &rc))) &&
2475                     !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
2476                       fan_write_cmd_speed(cmd, &rc))
2477                     )
2478                         rc = -EINVAL;
2479                 else if (!rc)
2480                         fan_watchdog_reset();
2481         }
2482
2483         return rc;
2484 }
2485
2486 /****************************************************************************
2487  ****************************************************************************
2488  *
2489  * Infrastructure
2490  *
2491  ****************************************************************************
2492  ****************************************************************************/
2493
2494 /* /proc support */
2495 static struct proc_dir_entry *proc_dir = NULL;
2496
2497 /* Subdriver registry */
2498 static struct ibm_struct ibms[] = {
2499         {
2500          .name = "driver",
2501          .init = thinkpad_acpi_driver_init,
2502          .read = thinkpad_acpi_driver_read,
2503          },
2504         {
2505          .name = "hotkey",
2506          .hid = IBM_HKEY_HID,
2507          .init = hotkey_init,
2508          .read = hotkey_read,
2509          .write = hotkey_write,
2510          .exit = hotkey_exit,
2511          .notify = hotkey_notify,
2512          .handle = &hkey_handle,
2513          .type = ACPI_DEVICE_NOTIFY,
2514          },
2515         {
2516          .name = "bluetooth",
2517          .init = bluetooth_init,
2518          .read = bluetooth_read,
2519          .write = bluetooth_write,
2520          },
2521         {
2522          .name = "wan",
2523          .init = wan_init,
2524          .read = wan_read,
2525          .write = wan_write,
2526          .experimental = 1,
2527          },
2528         {
2529          .name = "video",
2530          .init = video_init,
2531          .read = video_read,
2532          .write = video_write,
2533          .exit = video_exit,
2534          },
2535         {
2536          .name = "light",
2537          .init = light_init,
2538          .read = light_read,
2539          .write = light_write,
2540          },
2541 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2542         {
2543          .name = "dock",
2544          .init = dock_init,
2545          .read = dock_read,
2546          .write = dock_write,
2547          .notify = dock_notify,
2548          .handle = &dock_handle,
2549          .type = ACPI_SYSTEM_NOTIFY,
2550          },
2551         {
2552          .name = "dock",
2553          .hid = IBM_PCI_HID,
2554          .notify = dock_notify,
2555          .handle = &pci_handle,
2556          .type = ACPI_SYSTEM_NOTIFY,
2557          },
2558 #endif
2559 #ifdef CONFIG_THINKPAD_ACPI_BAY
2560         {
2561          .name = "bay",
2562          .init = bay_init,
2563          .read = bay_read,
2564          .write = bay_write,
2565          .notify = bay_notify,
2566          .handle = &bay_handle,
2567          .type = ACPI_SYSTEM_NOTIFY,
2568          },
2569 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2570         {
2571          .name = "cmos",
2572          .init = cmos_init,
2573          .read = cmos_read,
2574          .write = cmos_write,
2575          },
2576         {
2577          .name = "led",
2578          .init = led_init,
2579          .read = led_read,
2580          .write = led_write,
2581          },
2582         {
2583          .name = "beep",
2584          .init = beep_init,
2585          .read = beep_read,
2586          .write = beep_write,
2587          },
2588         {
2589          .name = "thermal",
2590          .init = thermal_init,
2591          .read = thermal_read,
2592          },
2593         {
2594          .name = "ecdump",
2595          .read = ecdump_read,
2596          .write = ecdump_write,
2597          .experimental = 1,
2598          },
2599         {
2600          .name = "brightness",
2601          .read = brightness_read,
2602          .write = brightness_write,
2603          .init = brightness_init,
2604          .exit = brightness_exit,
2605          },
2606         {
2607          .name = "volume",
2608          .read = volume_read,
2609          .write = volume_write,
2610          },
2611         {
2612          .name = "fan",
2613          .read = fan_read,
2614          .write = fan_write,
2615          .init = fan_init,
2616          .exit = fan_exit,
2617          .experimental = 1,
2618          },
2619 };
2620
2621 /*
2622  * Module and infrastructure proble, init and exit handling
2623  */
2624
2625 static int __init ibm_init(struct ibm_struct *ibm)
2626 {
2627         int ret;
2628         struct proc_dir_entry *entry;
2629
2630         if (ibm->experimental && !experimental)
2631                 return 0;
2632
2633         if (ibm->init) {
2634                 ret = ibm->init();
2635                 if (ret > 0)
2636                         return 0;       /* probe failed */
2637                 if (ret)
2638                         return ret;
2639                 ibm->init_called = 1;
2640         }
2641
2642         if (ibm->hid) {
2643                 ret = register_tpacpi_subdriver(ibm);
2644                 if (ret)
2645                         goto err_out;
2646         }
2647
2648         if (ibm->notify) {
2649                 ret = setup_notify(ibm);
2650                 if (ret == -ENODEV) {
2651                         printk(IBM_NOTICE "disabling subdriver %s\n",
2652                                 ibm->name);
2653                         ret = 0;
2654                         goto err_out;
2655                 }
2656                 if (ret < 0)
2657                         goto err_out;
2658         }
2659
2660         if (ibm->read) {
2661                 entry = create_proc_entry(ibm->name,
2662                                           S_IFREG | S_IRUGO | S_IWUSR,
2663                                           proc_dir);
2664                 if (!entry) {
2665                         printk(IBM_ERR "unable to create proc entry %s\n",
2666                                ibm->name);
2667                         ret = -ENODEV;
2668                         goto err_out;
2669                 }
2670                 entry->owner = THIS_MODULE;
2671                 entry->data = ibm;
2672                 entry->read_proc = &dispatch_read;
2673                 if (ibm->write)
2674                         entry->write_proc = &dispatch_write;
2675                 ibm->proc_created = 1;
2676         }
2677
2678         return 0;
2679
2680 err_out:
2681         ibm_exit(ibm);
2682         return (ret < 0)? ret : 0;
2683 }
2684
2685 static void ibm_exit(struct ibm_struct *ibm)
2686 {
2687         if (ibm->notify_installed) {
2688                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2689                                            dispatch_notify);
2690                 ibm->notify_installed = 0;
2691         }
2692
2693         if (ibm->proc_created) {
2694                 remove_proc_entry(ibm->name, proc_dir);
2695                 ibm->proc_created = 0;
2696         }
2697
2698         if (ibm->driver_registered) {
2699                 acpi_bus_unregister_driver(ibm->driver);
2700                 kfree(ibm->driver);
2701                 ibm->driver = NULL;
2702                 ibm->driver_registered = 0;
2703         }
2704
2705         if (ibm->init_called && ibm->exit) {
2706                 ibm->exit();
2707                 ibm->init_called = 0;
2708         }
2709 }
2710
2711 /* Probing */
2712
2713 static char *ibm_thinkpad_ec_found = NULL;
2714
2715 static char* __init check_dmi_for_ec(void)
2716 {
2717         struct dmi_device *dev = NULL;
2718         char ec_fw_string[18];
2719
2720         /*
2721          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2722          * X32 or newer, all Z series;  Some models must have an
2723          * up-to-date BIOS or they will not be detected.
2724          *
2725          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2726          */
2727         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2728                 if (sscanf(dev->name,
2729                            "IBM ThinkPad Embedded Controller -[%17c",
2730                            ec_fw_string) == 1) {
2731                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2732                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2733                         return kstrdup(ec_fw_string, GFP_KERNEL);
2734                 }
2735         }
2736         return NULL;
2737 }
2738
2739 static int __init probe_for_thinkpad(void)
2740 {
2741         int is_thinkpad;
2742
2743         if (acpi_disabled)
2744                 return -ENODEV;
2745
2746         /*
2747          * Non-ancient models have better DMI tagging, but very old models
2748          * don't.
2749          */
2750         is_thinkpad = dmi_name_in_vendors("ThinkPad");
2751
2752         /* ec is required because many other handles are relative to it */
2753         IBM_HANDLE_INIT(ec);
2754         if (!ec_handle) {
2755                 if (is_thinkpad)
2756                         printk(IBM_ERR
2757                                 "Not yet supported ThinkPad detected!\n");
2758                 return -ENODEV;
2759         }
2760
2761         return 0;
2762 }
2763
2764
2765 /* Module init, exit, parameters */
2766
2767 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2768 {
2769         unsigned int i;
2770
2771         for (i = 0; i < ARRAY_SIZE(ibms); i++)
2772                 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2773                         if (strlen(val) > sizeof(ibms[i].param) - 2)
2774                                 return -ENOSPC;
2775                         strcpy(ibms[i].param, val);
2776                         strcat(ibms[i].param, ",");
2777                         return 0;
2778                 }
2779
2780         return -EINVAL;
2781 }
2782
2783 static int experimental;
2784 module_param(experimental, int, 0);
2785
2786 static u32 dbg_level;
2787 module_param_named(debug, dbg_level, uint, 0);
2788
2789 #define IBM_PARAM(feature) \
2790         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2791
2792 IBM_PARAM(hotkey);
2793 IBM_PARAM(bluetooth);
2794 IBM_PARAM(video);
2795 IBM_PARAM(light);
2796 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2797 IBM_PARAM(dock);
2798 #endif
2799 #ifdef CONFIG_THINKPAD_ACPI_BAY
2800 IBM_PARAM(bay);
2801 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2802 IBM_PARAM(cmos);
2803 IBM_PARAM(led);
2804 IBM_PARAM(beep);
2805 IBM_PARAM(ecdump);
2806 IBM_PARAM(brightness);
2807 IBM_PARAM(volume);
2808 IBM_PARAM(fan);
2809
2810 static int __init thinkpad_acpi_module_init(void)
2811 {
2812         int ret, i;
2813
2814         ret = probe_for_thinkpad();
2815         if (ret)
2816                 return ret;
2817
2818         ibm_thinkpad_ec_found = check_dmi_for_ec();
2819         IBM_HANDLE_INIT(ecrd);
2820         IBM_HANDLE_INIT(ecwr);
2821
2822         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2823         if (!proc_dir) {
2824                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2825                 thinkpad_acpi_module_exit();
2826                 return -ENODEV;
2827         }
2828         proc_dir->owner = THIS_MODULE;
2829
2830         for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2831                 ret = ibm_init(&ibms[i]);
2832                 if (ret >= 0 && *ibms[i].param)
2833                         ret = ibms[i].write(ibms[i].param);
2834                 if (ret < 0) {
2835                         thinkpad_acpi_module_exit();
2836                         return ret;
2837                 }
2838         }
2839
2840         return 0;
2841 }
2842
2843 static void thinkpad_acpi_module_exit(void)
2844 {
2845         int i;
2846
2847         for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2848                 ibm_exit(&ibms[i]);
2849
2850         if (proc_dir)
2851                 remove_proc_entry(IBM_DIR, acpi_root_dir);
2852
2853         if (ibm_thinkpad_ec_found)
2854                 kfree(ibm_thinkpad_ec_found);
2855 }
2856
2857 module_init(thinkpad_acpi_module_init);
2858 module_exit(thinkpad_acpi_module_exit);