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