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