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