ACPI: thinkpad-acpi: cleanup after rename
[safe/jmp/linux-2.6] / drivers / misc / thinkpad_acpi.c
1 /*
2  *  thinkpad_acpi.c - ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6  *  Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  *  02110-1301, USA.
22  */
23
24 #define IBM_VERSION "0.14"
25
26 /*
27  *  Changelog:
28  *  2007-03-27  0.14    renamed to thinkpad_acpi and moved to
29  *                      drivers/misc.
30  *
31  *  2006-11-22  0.13    new maintainer
32  *                      changelog now lives in git commit history, and will
33  *                      not be updated further in-file.
34  *
35  *  2005-08-17  0.12    fix compilation on 2.6.13-rc kernels
36  *  2005-03-17  0.11    support for 600e, 770x
37  *                          thanks to Jamie Lentin <lentinj@dial.pipex.com>
38  *                      support for 770e, G41
39  *                      G40 and G41 don't have a thinklight
40  *                      temperatures no longer experimental
41  *                      experimental brightness control
42  *                      experimental volume control
43  *                      experimental fan enable/disable
44  *  2005-01-16  0.10    fix module loading on R30, R31
45  *  2005-01-16  0.9     support for 570, R30, R31
46  *                      ultrabay support on A22p, A3x
47  *                      limit arg for cmos, led, beep, drop experimental status
48  *                      more capable led control on A21e, A22p, T20-22, X20
49  *                      experimental temperatures and fan speed
50  *                      experimental embedded controller register dump
51  *                      mark more functions as __init, drop incorrect __exit
52  *                      use MODULE_VERSION
53  *                          thanks to Henrik Brix Andersen <brix@gentoo.org>
54  *                      fix parameter passing on module loading
55  *                          thanks to Rusty Russell <rusty@rustcorp.com.au>
56  *                          thanks to Jim Radford <radford@blackbean.org>
57  *  2004-11-08  0.8     fix init error case, don't return from a macro
58  *                          thanks to Chris Wright <chrisw@osdl.org>
59  *  2004-10-23  0.7     fix module loading on A21e, A22p, T20, T21, X20
60  *                      fix led control on A21e
61  *  2004-10-19  0.6     use acpi_bus_register_driver() to claim HKEY device
62  *  2004-10-18  0.5     thinklight support on A21e, G40, R32, T20, T21, X20
63  *                      proc file format changed
64  *                      video_switch command
65  *                      experimental cmos control
66  *                      experimental led control
67  *                      experimental acpi sounds
68  *  2004-09-16  0.4     support for module parameters
69  *                      hotkey mask can be prefixed by 0x
70  *                      video output switching
71  *                      video expansion control
72  *                      ultrabay eject support
73  *                      removed lcd brightness/on/off control, didn't work
74  *  2004-08-17  0.3     support for R40
75  *                      lcd off, brightness control
76  *                      thinklight on/off
77  *  2004-08-14  0.2     support for T series, X20
78  *                      bluetooth enable/disable
79  *                      hotkey events disabled by default
80  *                      removed fan control, currently useless
81  *  2004-08-09  0.1     initial release, support for X series
82  */
83
84 #include "thinkpad_acpi.h"
85
86 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
87 MODULE_DESCRIPTION(IBM_DESC);
88 MODULE_VERSION(IBM_VERSION);
89 MODULE_LICENSE("GPL");
90
91 /* Please remove this in year 2009 */
92 MODULE_ALIAS("ibm_acpi");
93
94 #define __unused __attribute__ ((unused))
95
96 /****************************************************************************
97  ****************************************************************************
98  *
99  * ACPI Helpers and device model
100  *
101  ****************************************************************************
102  ****************************************************************************/
103
104 /*************************************************************************
105  * ACPI basic handles
106  */
107
108 static acpi_handle root_handle = NULL;
109
110 #define IBM_HANDLE(object, parent, paths...)                    \
111         static acpi_handle  object##_handle;                    \
112         static acpi_handle *object##_parent = &parent##_handle; \
113         static char        *object##_path;                      \
114         static char        *object##_paths[] = { paths }
115
116 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
117            "\\_SB.PCI.ISA.EC",  /* 570 */
118            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
119            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
120            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
121            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
122            "\\_SB.PCI0.LPC.EC", /* all others */
123            );
124
125 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
126 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
127
128
129 /*************************************************************************
130  * Misc ACPI handles
131  */
132
133 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
134            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
135            "\\CMS",             /* R40, R40e */
136            );                   /* all others */
137
138 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
139            "^HKEY",             /* R30, R31 */
140            "HKEY",              /* all others */
141            );                   /* 570 */
142
143
144 /*************************************************************************
145  * ACPI helpers
146  */
147
148 static int acpi_evalf(acpi_handle handle,
149                       void *res, char *method, char *fmt, ...)
150 {
151         char *fmt0 = fmt;
152         struct acpi_object_list params;
153         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
154         struct acpi_buffer result, *resultp;
155         union acpi_object out_obj;
156         acpi_status status;
157         va_list ap;
158         char res_type;
159         int success;
160         int quiet;
161
162         if (!*fmt) {
163                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
164                 return 0;
165         }
166
167         if (*fmt == 'q') {
168                 quiet = 1;
169                 fmt++;
170         } else
171                 quiet = 0;
172
173         res_type = *(fmt++);
174
175         params.count = 0;
176         params.pointer = &in_objs[0];
177
178         va_start(ap, fmt);
179         while (*fmt) {
180                 char c = *(fmt++);
181                 switch (c) {
182                 case 'd':       /* int */
183                         in_objs[params.count].integer.value = va_arg(ap, int);
184                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
185                         break;
186                         /* add more types as needed */
187                 default:
188                         printk(IBM_ERR "acpi_evalf() called "
189                                "with invalid format character '%c'\n", c);
190                         return 0;
191                 }
192         }
193         va_end(ap);
194
195         if (res_type != 'v') {
196                 result.length = sizeof(out_obj);
197                 result.pointer = &out_obj;
198                 resultp = &result;
199         } else
200                 resultp = NULL;
201
202         status = acpi_evaluate_object(handle, method, &params, resultp);
203
204         switch (res_type) {
205         case 'd':               /* int */
206                 if (res)
207                         *(int *)res = out_obj.integer.value;
208                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
209                 break;
210         case 'v':               /* void */
211                 success = status == AE_OK;
212                 break;
213                 /* add more types as needed */
214         default:
215                 printk(IBM_ERR "acpi_evalf() called "
216                        "with invalid format character '%c'\n", res_type);
217                 return 0;
218         }
219
220         if (!success && !quiet)
221                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
222                        method, fmt0, status);
223
224         return success;
225 }
226
227 static void __unused acpi_print_int(acpi_handle handle, char *method)
228 {
229         int i;
230
231         if (acpi_evalf(handle, &i, method, "d"))
232                 printk(IBM_INFO "%s = 0x%x\n", method, i);
233         else
234                 printk(IBM_ERR "error calling %s\n", method);
235 }
236
237 static int acpi_ec_read(int i, u8 * p)
238 {
239         int v;
240
241         if (ecrd_handle) {
242                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
243                         return 0;
244                 *p = v;
245         } else {
246                 if (ec_read(i, p) < 0)
247                         return 0;
248         }
249
250         return 1;
251 }
252
253 static int acpi_ec_write(int i, u8 v)
254 {
255         if (ecwr_handle) {
256                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
257                         return 0;
258         } else {
259                 if (ec_write(i, v) < 0)
260                         return 0;
261         }
262
263         return 1;
264 }
265
266 static int _sta(acpi_handle handle)
267 {
268         int status;
269
270         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
271                 status = 0;
272
273         return status;
274 }
275
276 /*************************************************************************
277  * ACPI device model
278  */
279
280 static void __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_ibmacpi_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  * ibm-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 = IBMACPI_VIDEO_NONE;
785         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
786                 /* 570 */
787                 video_supported = IBMACPI_VIDEO_570;
788         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
789                 /* 600e/x, 770e, 770x */
790                 video_supported = IBMACPI_VIDEO_770;
791         else
792                 /* all others */
793                 video_supported = IBMACPI_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 == IBMACPI_VIDEO_570) {
809                 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
810                         status = i & 3;
811         } else if (video_supported == IBMACPI_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 == IBMACPI_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 == IBMACPI_VIDEO_570)
836                 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
837         else if (video_supported == IBMACPI_VIDEO_770 ||
838                  video_supported == IBMACPI_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 == IBMACPI_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 == IBMACPI_VIDEO_570)
862                 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
863         else if (video_supported == IBMACPI_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 == IBMACPI_VIDEO_570) {
874                 ret = acpi_evalf(NULL, NULL,
875                                  "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
876         } else if (video_supported == IBMACPI_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 == IBMACPI_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 == IBMACPI_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 == IBMACPI_VIDEO_NEW &&
940                            strlencmp(cmd, "dvi_enable") == 0) {
941                         enable |= 0x08;
942                 } else if (video_supported == IBMACPI_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 = IBMACPI_LED_NONE;
1287         else if (strlencmp(led_path, "SLED") == 0)
1288                 /* 570 */
1289                 led_supported = IBMACPI_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 = IBMACPI_LED_OLD;
1293         else
1294                 /* all others */
1295                 led_supported = IBMACPI_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 == IBMACPI_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 == IBMACPI_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 == IBMACPI_LED_OLD) {
1364                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1365                         led = 1 << led;
1366                         ret = ec_write(IBMACPI_LED_EC_HLMS, led);
1367                         if (ret >= 0)
1368                                 ret =
1369                                     ec_write(IBMACPI_LED_EC_HLBL,
1370                                              led * led_exp_hlbl[ind]);
1371                         if (ret >= 0)
1372                                 ret =
1373                                     ec_write(IBMACPI_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 = IBMACPI_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 = IBMACPI_THERMAL_NONE;
1476                         }
1477                 } else {
1478                         thermal_read_mode =
1479                             (ta2 != 0) ?
1480                             IBMACPI_THERMAL_TPEC_16 : IBMACPI_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 = IBMACPI_THERMAL_ACPI_UPDT;
1486                 } else {
1487                         /* Standard ACPI TMPx access, max 8 sensors */
1488                         thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1489                 }
1490         } else {
1491                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1492                 thermal_read_mode = IBMACPI_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 IBMACPI_MAX_THERMAL_SENSORS >= 16
1509         case IBMACPI_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 IBMACPI_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 == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1524
1525         case IBMACPI_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 IBMACPI_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 IBMACPI_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  * IBMACPI_FAN_RD_ACPI_GFAN:
1852  *      ACPI GFAN method: returns fan level
1853  *
1854  *      see IBMACPI_FAN_WR_ACPI_SFAN
1855  *      EC 0x2f not available if GFAN exists
1856  *
1857  * IBMACPI_FAN_WR_ACPI_SFAN:
1858  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1859  *
1860  *      EC 0x2f might be available *for reading*, but never for writing.
1861  *
1862  * IBMACPI_FAN_WR_TPEC:
1863  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
1864  *      on almost all ThinkPads
1865  *
1866  *      Fan speed changes of any sort (including those caused by the
1867  *      disengaged mode) are usually done slowly by the firmware as the
1868  *      maximum ammount of fan duty cycle change per second seems to be
1869  *      limited.
1870  *
1871  *      Reading is not available if GFAN exists.
1872  *      Writing is not available if SFAN exists.
1873  *
1874  *      Bits
1875  *       7      automatic mode engaged;
1876  *              (default operation mode of the ThinkPad)
1877  *              fan level is ignored in this mode.
1878  *       6      disengage mode (takes precedence over bit 7);
1879  *              not available on all thinkpads.  May disable
1880  *              the tachometer, and speeds up fan to 100% duty-cycle,
1881  *              which speeds it up far above the standard RPM
1882  *              levels.  It is not impossible that it could cause
1883  *              hardware damage.
1884  *      5-3     unused in some models.  Extra bits for fan level
1885  *              in others, but still useless as all values above
1886  *              7 map to the same speed as level 7 in these models.
1887  *      2-0     fan level (0..7 usually)
1888  *                      0x00 = stop
1889  *                      0x07 = max (set when temperatures critical)
1890  *              Some ThinkPads may have other levels, see
1891  *              IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
1892  *
1893  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1894  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1895  *      does so, its initial value is meaningless (0x07).
1896  *
1897  *      For firmware bugs, refer to:
1898  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1899  *
1900  *      ----
1901  *
1902  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1903  *      Main fan tachometer reading (in RPM)
1904  *
1905  *      This register is present on all ThinkPads with a new-style EC, and
1906  *      it is known not to be present on the A21m/e, and T22, as there is
1907  *      something else in offset 0x84 according to the ACPI DSDT.  Other
1908  *      ThinkPads from this same time period (and earlier) probably lack the
1909  *      tachometer as well.
1910  *
1911  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1912  *      was never fixed by IBM to report the EC firmware version string
1913  *      probably support the tachometer (like the early X models), so
1914  *      detecting it is quite hard.  We need more data to know for sure.
1915  *
1916  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1917  *      might result.
1918  *
1919  *      FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
1920  *      register is not invalidated in ThinkPads that disable tachometer
1921  *      readings.  Thus, the tachometer readings go stale.
1922  *
1923  *      For firmware bugs, refer to:
1924  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1925  *
1926  * IBMACPI_FAN_WR_ACPI_FANS:
1927  *      ThinkPad X31, X40, X41.  Not available in the X60.
1928  *
1929  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
1930  *      high speed.  ACPI DSDT seems to map these three speeds to levels
1931  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1932  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1933  *
1934  *      The speeds are stored on handles
1935  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1936  *
1937  *      There are three default speed sets, acessible as handles:
1938  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1939  *
1940  *      ACPI DSDT switches which set is in use depending on various
1941  *      factors.
1942  *
1943  *      IBMACPI_FAN_WR_TPEC is also available and should be used to
1944  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
1945  *      but the ACPI tables just mention level 7.
1946  */
1947
1948 static enum fan_status_access_mode fan_status_access_mode;
1949 static enum fan_control_access_mode fan_control_access_mode;
1950 static enum fan_control_commands fan_control_commands;
1951
1952 static int fan_control_status_known;
1953 static u8 fan_control_initial_status;
1954
1955 static void fan_watchdog_fire(struct work_struct *ignored);
1956 static int fan_watchdog_maxinterval;
1957 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
1958
1959 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40, X41 */
1960 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
1961            "\\FSPD",            /* 600e/x, 770e, 770x */
1962            );                   /* all others */
1963 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
1964            "JFNS",              /* 770x-JL */
1965            );                   /* all others */
1966
1967 static int fan_init(void)
1968 {
1969         fan_status_access_mode = IBMACPI_FAN_NONE;
1970         fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1971         fan_control_commands = 0;
1972         fan_control_status_known = 1;
1973         fan_watchdog_maxinterval = 0;
1974
1975         if (gfan_handle) {
1976                 /* 570, 600e/x, 770e, 770x */
1977                 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1978         } else {
1979                 /* all other ThinkPads: note that even old-style
1980                  * ThinkPad ECs supports the fan control register */
1981                 if (likely(acpi_ec_read(fan_status_offset,
1982                                         &fan_control_initial_status))) {
1983                         fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1984
1985                         /* In some ThinkPads, neither the EC nor the ACPI
1986                          * DSDT initialize the fan status, and it ends up
1987                          * being set to 0x07 when it *could* be either
1988                          * 0x07 or 0x80.
1989                          *
1990                          * Enable for TP-1Y (T43), TP-78 (R51e),
1991                          * TP-76 (R52), TP-70 (T43, R52), which are known
1992                          * to be buggy. */
1993                         if (fan_control_initial_status == 0x07 &&
1994                             ibm_thinkpad_ec_found &&
1995                             ((ibm_thinkpad_ec_found[0] == '1' &&
1996                               ibm_thinkpad_ec_found[1] == 'Y') ||
1997                              (ibm_thinkpad_ec_found[0] == '7' &&
1998                               (ibm_thinkpad_ec_found[1] == '6' ||
1999                                ibm_thinkpad_ec_found[1] == '8' ||
2000                                ibm_thinkpad_ec_found[1] == '0'))
2001                             )) {
2002                                 printk(IBM_NOTICE
2003                                        "fan_init: initial fan status is "
2004                                        "unknown, assuming it is in auto "
2005                                        "mode\n");
2006                                 fan_control_status_known = 0;
2007                         }
2008                 } else {
2009                         printk(IBM_ERR
2010                                "ThinkPad ACPI EC access misbehaving, "
2011                                "fan status and control unavailable\n");
2012                         return 0;
2013                 }
2014         }
2015
2016         if (sfan_handle) {
2017                 /* 570, 770x-JL */
2018                 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
2019                 fan_control_commands |=
2020                     IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
2021         } else {
2022                 if (!gfan_handle) {
2023                         /* gfan without sfan means no fan control */
2024                         /* all other models implement TP EC 0x2f control */
2025
2026                         if (fans_handle) {
2027                                 /* X31, X40, X41 */
2028                                 fan_control_access_mode =
2029                                     IBMACPI_FAN_WR_ACPI_FANS;
2030                                 fan_control_commands |=
2031                                     IBMACPI_FAN_CMD_SPEED |
2032                                     IBMACPI_FAN_CMD_LEVEL |
2033                                     IBMACPI_FAN_CMD_ENABLE;
2034                         } else {
2035                                 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
2036                                 fan_control_commands |=
2037                                     IBMACPI_FAN_CMD_LEVEL |
2038                                     IBMACPI_FAN_CMD_ENABLE;
2039                         }
2040                 }
2041         }
2042
2043         return 0;
2044 }
2045
2046 static int fan_get_status(u8 *status)
2047 {
2048         u8 s;
2049
2050         /* TODO:
2051          * Add IBMACPI_FAN_RD_ACPI_FANS ? */
2052
2053         switch (fan_status_access_mode) {
2054         case IBMACPI_FAN_RD_ACPI_GFAN:
2055                 /* 570, 600e/x, 770e, 770x */
2056
2057                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2058                         return -EIO;
2059
2060                 if (likely(status))
2061                         *status = s & 0x07;
2062
2063                 break;
2064
2065         case IBMACPI_FAN_RD_TPEC:
2066                 /* all except 570, 600e/x, 770e, 770x */
2067                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2068                         return -EIO;
2069
2070                 if (likely(status))
2071                         *status = s;
2072
2073                 break;
2074
2075         default:
2076                 return -ENXIO;
2077         }
2078
2079         return 0;
2080 }
2081
2082 static void fan_exit(void)
2083 {
2084         cancel_delayed_work(&fan_watchdog_task);
2085         flush_scheduled_work();
2086 }
2087
2088 static int fan_get_speed(unsigned int *speed)
2089 {
2090         u8 hi, lo;
2091
2092         switch (fan_status_access_mode) {
2093         case IBMACPI_FAN_RD_TPEC:
2094                 /* all except 570, 600e/x, 770e, 770x */
2095                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2096                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2097                         return -EIO;
2098
2099                 if (likely(speed))
2100                         *speed = (hi << 8) | lo;
2101
2102                 break;
2103
2104         default:
2105                 return -ENXIO;
2106         }
2107
2108         return 0;
2109 }
2110
2111 static void fan_watchdog_fire(struct work_struct *ignored)
2112 {
2113         printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2114         if (fan_set_enable()) {
2115                 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2116                 /* reschedule for later */
2117                 fan_watchdog_reset();
2118         }
2119 }
2120
2121 static void fan_watchdog_reset(void)
2122 {
2123         static int fan_watchdog_active = 0;
2124
2125         if (fan_watchdog_active)
2126                 cancel_delayed_work(&fan_watchdog_task);
2127
2128         if (fan_watchdog_maxinterval > 0) {
2129                 fan_watchdog_active = 1;
2130                 if (!schedule_delayed_work(&fan_watchdog_task,
2131                                 msecs_to_jiffies(fan_watchdog_maxinterval
2132                                                  * 1000))) {
2133                         printk(IBM_ERR "failed to schedule the fan watchdog, "
2134                                "watchdog will not trigger\n");
2135                 }
2136         } else
2137                 fan_watchdog_active = 0;
2138 }
2139
2140 static int fan_set_level(int level)
2141 {
2142         switch (fan_control_access_mode) {
2143         case IBMACPI_FAN_WR_ACPI_SFAN:
2144                 if (level >= 0 && level <= 7) {
2145                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2146                                 return -EIO;
2147                 } else
2148                         return -EINVAL;
2149                 break;
2150
2151         case IBMACPI_FAN_WR_ACPI_FANS:
2152         case IBMACPI_FAN_WR_TPEC:
2153                 if ((level != IBMACPI_FAN_EC_AUTO) &&
2154                     (level != IBMACPI_FAN_EC_DISENGAGED) &&
2155                     ((level < 0) || (level > 7)))
2156                         return -EINVAL;
2157
2158                 if (!acpi_ec_write(fan_status_offset, level))
2159                         return -EIO;
2160                 else
2161                         fan_control_status_known = 1;
2162                 break;
2163
2164         default:
2165                 return -ENXIO;
2166         }
2167         return 0;
2168 }
2169
2170 static int fan_set_enable(void)
2171 {
2172         u8 s;
2173         int rc;
2174
2175         switch (fan_control_access_mode) {
2176         case IBMACPI_FAN_WR_ACPI_FANS:
2177         case IBMACPI_FAN_WR_TPEC:
2178                 if ((rc = fan_get_status(&s)) < 0)
2179                         return rc;
2180
2181                 /* Don't go out of emergency fan mode */
2182                 if (s != 7)
2183                         s = IBMACPI_FAN_EC_AUTO;
2184
2185                 if (!acpi_ec_write(fan_status_offset, s))
2186                         return -EIO;
2187                 else
2188                         fan_control_status_known = 1;
2189                 break;
2190
2191         case IBMACPI_FAN_WR_ACPI_SFAN:
2192                 if ((rc = fan_get_status(&s)) < 0)
2193                         return rc;
2194
2195                 s &= 0x07;
2196
2197                 /* Set fan to at least level 4 */
2198                 if (s < 4)
2199                         s = 4;
2200
2201                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2202                         return -EIO;
2203                 break;
2204
2205         default:
2206                 return -ENXIO;
2207         }
2208         return 0;
2209 }
2210
2211 static int fan_set_disable(void)
2212 {
2213         switch (fan_control_access_mode) {
2214         case IBMACPI_FAN_WR_ACPI_FANS:
2215         case IBMACPI_FAN_WR_TPEC:
2216                 if (!acpi_ec_write(fan_status_offset, 0x00))
2217                         return -EIO;
2218                 else
2219                         fan_control_status_known = 1;
2220                 break;
2221
2222         case IBMACPI_FAN_WR_ACPI_SFAN:
2223                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2224                         return -EIO;
2225                 break;
2226
2227         default:
2228                 return -ENXIO;
2229         }
2230         return 0;
2231 }
2232
2233 static int fan_set_speed(int speed)
2234 {
2235         switch (fan_control_access_mode) {
2236         case IBMACPI_FAN_WR_ACPI_FANS:
2237                 if (speed >= 0 && speed <= 65535) {
2238                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2239                                         speed, speed, speed))
2240                                 return -EIO;
2241                 } else
2242                         return -EINVAL;
2243                 break;
2244
2245         default:
2246                 return -ENXIO;
2247         }
2248         return 0;
2249 }
2250
2251 static int fan_read(char *p)
2252 {
2253         int len = 0;
2254         int rc;
2255         u8 status;
2256         unsigned int speed = 0;
2257
2258         switch (fan_status_access_mode) {
2259         case IBMACPI_FAN_RD_ACPI_GFAN:
2260                 /* 570, 600e/x, 770e, 770x */
2261                 if ((rc = fan_get_status(&status)) < 0)
2262                         return rc;
2263
2264                 len += sprintf(p + len, "status:\t\t%s\n"
2265                                "level:\t\t%d\n",
2266                                (status != 0) ? "enabled" : "disabled", status);
2267                 break;
2268
2269         case IBMACPI_FAN_RD_TPEC:
2270                 /* all except 570, 600e/x, 770e, 770x */
2271                 if ((rc = fan_get_status(&status)) < 0)
2272                         return rc;
2273
2274                 if (unlikely(!fan_control_status_known)) {
2275                         if (status != fan_control_initial_status)
2276                                 fan_control_status_known = 1;
2277                         else
2278                                 /* Return most likely status. In fact, it
2279                                  * might be the only possible status */
2280                                 status = IBMACPI_FAN_EC_AUTO;
2281                 }
2282
2283                 len += sprintf(p + len, "status:\t\t%s\n",
2284                                (status != 0) ? "enabled" : "disabled");
2285
2286                 /* No ThinkPad boots on disengaged mode, we can safely
2287                  * assume the tachometer is online if fan control status
2288                  * was unknown */
2289                 if ((rc = fan_get_speed(&speed)) < 0)
2290                         return rc;
2291
2292                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2293
2294                 if (status & IBMACPI_FAN_EC_DISENGAGED)
2295                         /* Disengaged mode takes precedence */
2296                         len += sprintf(p + len, "level:\t\tdisengaged\n");
2297                 else if (status & IBMACPI_FAN_EC_AUTO)
2298                         len += sprintf(p + len, "level:\t\tauto\n");
2299                 else
2300                         len += sprintf(p + len, "level:\t\t%d\n", status);
2301                 break;
2302
2303         case IBMACPI_FAN_NONE:
2304         default:
2305                 len += sprintf(p + len, "status:\t\tnot supported\n");
2306         }
2307
2308         if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2309                 len += sprintf(p + len, "commands:\tlevel <level>");
2310
2311                 switch (fan_control_access_mode) {
2312                 case IBMACPI_FAN_WR_ACPI_SFAN:
2313                         len += sprintf(p + len, " (<level> is 0-7)\n");
2314                         break;
2315
2316                 default:
2317                         len += sprintf(p + len, " (<level> is 0-7, "
2318                                        "auto, disengaged)\n");
2319                         break;
2320                 }
2321         }
2322
2323         if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
2324                 len += sprintf(p + len, "commands:\tenable, disable\n"
2325                                "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2326                                "1-120 (seconds))\n");
2327
2328         if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
2329                 len += sprintf(p + len, "commands:\tspeed <speed>"
2330                                " (<speed> is 0-65535)\n");
2331
2332         return len;
2333 }
2334
2335 static int fan_write_cmd_level(const char *cmd, int *rc)
2336 {
2337         int level;
2338
2339         if (strlencmp(cmd, "level auto") == 0)
2340                 level = IBMACPI_FAN_EC_AUTO;
2341         else if (strlencmp(cmd, "level disengaged") == 0)
2342                 level = IBMACPI_FAN_EC_DISENGAGED;
2343         else if (sscanf(cmd, "level %d", &level) != 1)
2344                 return 0;
2345
2346         if ((*rc = fan_set_level(level)) == -ENXIO)
2347                 printk(IBM_ERR "level command accepted for unsupported "
2348                        "access mode %d", fan_control_access_mode);
2349
2350         return 1;
2351 }
2352
2353 static int fan_write_cmd_enable(const char *cmd, int *rc)
2354 {
2355         if (strlencmp(cmd, "enable") != 0)
2356                 return 0;
2357
2358         if ((*rc = fan_set_enable()) == -ENXIO)
2359                 printk(IBM_ERR "enable command accepted for unsupported "
2360                        "access mode %d", fan_control_access_mode);
2361
2362         return 1;
2363 }
2364
2365 static int fan_write_cmd_disable(const char *cmd, int *rc)
2366 {
2367         if (strlencmp(cmd, "disable") != 0)
2368                 return 0;
2369
2370         if ((*rc = fan_set_disable()) == -ENXIO)
2371                 printk(IBM_ERR "disable command accepted for unsupported "
2372                        "access mode %d", fan_control_access_mode);
2373
2374         return 1;
2375 }
2376
2377 static int fan_write_cmd_speed(const char *cmd, int *rc)
2378 {
2379         int speed;
2380
2381         /* TODO:
2382          * Support speed <low> <medium> <high> ? */
2383
2384         if (sscanf(cmd, "speed %d", &speed) != 1)
2385                 return 0;
2386
2387         if ((*rc = fan_set_speed(speed)) == -ENXIO)
2388                 printk(IBM_ERR "speed command accepted for unsupported "
2389                        "access mode %d", fan_control_access_mode);
2390
2391         return 1;
2392 }
2393
2394 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2395 {
2396         int interval;
2397
2398         if (sscanf(cmd, "watchdog %d", &interval) != 1)
2399                 return 0;
2400
2401         if (interval < 0 || interval > 120)
2402                 *rc = -EINVAL;
2403         else
2404                 fan_watchdog_maxinterval = interval;
2405
2406         return 1;
2407 }
2408
2409 static int fan_write(char *buf)
2410 {
2411         char *cmd;
2412         int rc = 0;
2413
2414         while (!rc && (cmd = next_cmd(&buf))) {
2415                 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2416                       fan_write_cmd_level(cmd, &rc)) &&
2417                     !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2418                       (fan_write_cmd_enable(cmd, &rc) ||
2419                        fan_write_cmd_disable(cmd, &rc) ||
2420                        fan_write_cmd_watchdog(cmd, &rc))) &&
2421                     !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2422                       fan_write_cmd_speed(cmd, &rc))
2423                     )
2424                         rc = -EINVAL;
2425                 else if (!rc)
2426                         fan_watchdog_reset();
2427         }
2428
2429         return rc;
2430 }
2431
2432 /****************************************************************************
2433  ****************************************************************************
2434  *
2435  * Infrastructure
2436  *
2437  ****************************************************************************
2438  ****************************************************************************/
2439
2440 /* /proc support */
2441 static struct proc_dir_entry *proc_dir = NULL;
2442
2443 /* Subdriver registry */
2444 static struct ibm_struct ibms[] = {
2445         {
2446          .name = "driver",
2447          .init = thinkpad_acpi_driver_init,
2448          .read = thinkpad_acpi_driver_read,
2449          },
2450         {
2451          .name = "hotkey",
2452          .hid = IBM_HKEY_HID,
2453          .init = hotkey_init,
2454          .read = hotkey_read,
2455          .write = hotkey_write,
2456          .exit = hotkey_exit,
2457          .notify = hotkey_notify,
2458          .handle = &hkey_handle,
2459          .type = ACPI_DEVICE_NOTIFY,
2460          },
2461         {
2462          .name = "bluetooth",
2463          .init = bluetooth_init,
2464          .read = bluetooth_read,
2465          .write = bluetooth_write,
2466          },
2467         {
2468          .name = "wan",
2469          .init = wan_init,
2470          .read = wan_read,
2471          .write = wan_write,
2472          .experimental = 1,
2473          },
2474         {
2475          .name = "video",
2476          .init = video_init,
2477          .read = video_read,
2478          .write = video_write,
2479          .exit = video_exit,
2480          },
2481         {
2482          .name = "light",
2483          .init = light_init,
2484          .read = light_read,
2485          .write = light_write,
2486          },
2487 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2488         {
2489          .name = "dock",
2490          .read = dock_read,
2491          .write = dock_write,
2492          .notify = dock_notify,
2493          .handle = &dock_handle,
2494          .type = ACPI_SYSTEM_NOTIFY,
2495          },
2496         {
2497          .name = "dock",
2498          .hid = IBM_PCI_HID,
2499          .notify = dock_notify,
2500          .handle = &pci_handle,
2501          .type = ACPI_SYSTEM_NOTIFY,
2502          },
2503 #endif
2504 #ifdef CONFIG_THINKPAD_ACPI_BAY
2505         {
2506          .name = "bay",
2507          .init = bay_init,
2508          .read = bay_read,
2509          .write = bay_write,
2510          .notify = bay_notify,
2511          .handle = &bay_handle,
2512          .type = ACPI_SYSTEM_NOTIFY,
2513          },
2514 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2515         {
2516          .name = "cmos",
2517          .read = cmos_read,
2518          .write = cmos_write,
2519          },
2520         {
2521          .name = "led",
2522          .init = led_init,
2523          .read = led_read,
2524          .write = led_write,
2525          },
2526         {
2527          .name = "beep",
2528          .read = beep_read,
2529          .write = beep_write,
2530          },
2531         {
2532          .name = "thermal",
2533          .init = thermal_init,
2534          .read = thermal_read,
2535          },
2536         {
2537          .name = "ecdump",
2538          .read = ecdump_read,
2539          .write = ecdump_write,
2540          .experimental = 1,
2541          },
2542         {
2543          .name = "brightness",
2544          .read = brightness_read,
2545          .write = brightness_write,
2546          .init = brightness_init,
2547          .exit = brightness_exit,
2548          },
2549         {
2550          .name = "volume",
2551          .read = volume_read,
2552          .write = volume_write,
2553          },
2554         {
2555          .name = "fan",
2556          .read = fan_read,
2557          .write = fan_write,
2558          .init = fan_init,
2559          .exit = fan_exit,
2560          .experimental = 1,
2561          },
2562 };
2563
2564 /*
2565  * Module and infrastructure proble, init and exit handling
2566  */
2567
2568 static int __init ibm_init(struct ibm_struct *ibm)
2569 {
2570         int ret;
2571         struct proc_dir_entry *entry;
2572
2573         if (ibm->experimental && !experimental)
2574                 return 0;
2575
2576         if (ibm->hid) {
2577                 ret = register_ibmacpi_subdriver(ibm);
2578                 if (ret < 0)
2579                         return ret;
2580                 ibm->driver_registered = 1;
2581         }
2582
2583         if (ibm->init) {
2584                 ret = ibm->init();
2585                 if (ret != 0)
2586                         return ret;
2587                 ibm->init_called = 1;
2588         }
2589
2590         if (ibm->read) {
2591                 entry = create_proc_entry(ibm->name,
2592                                           S_IFREG | S_IRUGO | S_IWUSR,
2593                                           proc_dir);
2594                 if (!entry) {
2595                         printk(IBM_ERR "unable to create proc entry %s\n",
2596                                ibm->name);
2597                         return -ENODEV;
2598                 }
2599                 entry->owner = THIS_MODULE;
2600                 entry->data = ibm;
2601                 entry->read_proc = &dispatch_read;
2602                 if (ibm->write)
2603                         entry->write_proc = &dispatch_write;
2604                 ibm->proc_created = 1;
2605         }
2606
2607         if (ibm->notify) {
2608                 ret = setup_notify(ibm);
2609                 if (ret == -ENODEV) {
2610                         printk(IBM_NOTICE "disabling subdriver %s\n",
2611                                 ibm->name);
2612                         ibm_exit(ibm);
2613                         return 0;
2614                 }
2615                 if (ret < 0)
2616                         return ret;
2617         }
2618
2619         return 0;
2620 }
2621
2622 static void ibm_exit(struct ibm_struct *ibm)
2623 {
2624         if (ibm->notify_installed)
2625                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2626                                            dispatch_notify);
2627
2628         if (ibm->proc_created)
2629                 remove_proc_entry(ibm->name, proc_dir);
2630
2631         if (ibm->init_called && ibm->exit)
2632                 ibm->exit();
2633
2634         if (ibm->driver_registered) {
2635                 acpi_bus_unregister_driver(ibm->driver);
2636                 kfree(ibm->driver);
2637         }
2638 }
2639
2640 /* Probing */
2641
2642 static char *ibm_thinkpad_ec_found = NULL;
2643
2644 static char* __init check_dmi_for_ec(void)
2645 {
2646         struct dmi_device *dev = NULL;
2647         char ec_fw_string[18];
2648
2649         /*
2650          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2651          * X32 or newer, all Z series;  Some models must have an
2652          * up-to-date BIOS or they will not be detected.
2653          *
2654          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2655          */
2656         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2657                 if (sscanf(dev->name,
2658                            "IBM ThinkPad Embedded Controller -[%17c",
2659                            ec_fw_string) == 1) {
2660                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2661                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2662                         return kstrdup(ec_fw_string, GFP_KERNEL);
2663                 }
2664         }
2665         return NULL;
2666 }
2667
2668 /* Module init, exit, parameters */
2669
2670 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2671 {
2672         unsigned int i;
2673
2674         for (i = 0; i < ARRAY_SIZE(ibms); i++)
2675                 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2676                         if (strlen(val) > sizeof(ibms[i].param) - 2)
2677                                 return -ENOSPC;
2678                         strcpy(ibms[i].param, val);
2679                         strcat(ibms[i].param, ",");
2680                         return 0;
2681                 }
2682
2683         return -EINVAL;
2684 }
2685
2686 static int experimental;
2687 module_param(experimental, int, 0);
2688
2689 #define IBM_PARAM(feature) \
2690         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2691
2692 IBM_PARAM(hotkey);
2693 IBM_PARAM(bluetooth);
2694 IBM_PARAM(video);
2695 IBM_PARAM(light);
2696 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2697 IBM_PARAM(dock);
2698 #endif
2699 #ifdef CONFIG_THINKPAD_ACPI_BAY
2700 IBM_PARAM(bay);
2701 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2702 IBM_PARAM(cmos);
2703 IBM_PARAM(led);
2704 IBM_PARAM(beep);
2705 IBM_PARAM(ecdump);
2706 IBM_PARAM(brightness);
2707 IBM_PARAM(volume);
2708 IBM_PARAM(fan);
2709
2710 static int __init acpi_ibm_init(void)
2711 {
2712         int ret, i;
2713
2714         if (acpi_disabled)
2715                 return -ENODEV;
2716
2717         /* ec is required because many other handles are relative to it */
2718         IBM_HANDLE_INIT(ec);
2719         if (!ec_handle) {
2720                 printk(IBM_ERR "ec object not found\n");
2721                 return -ENODEV;
2722         }
2723
2724         /* Models with newer firmware report the EC in DMI */
2725         ibm_thinkpad_ec_found = check_dmi_for_ec();
2726
2727         /* these handles are not required */
2728         IBM_HANDLE_INIT(vid);
2729         IBM_HANDLE_INIT(vid2);
2730         IBM_HANDLE_INIT(ledb);
2731         IBM_HANDLE_INIT(led);
2732         IBM_HANDLE_INIT(hkey);
2733         IBM_HANDLE_INIT(lght);
2734         IBM_HANDLE_INIT(cmos);
2735 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2736         IBM_HANDLE_INIT(dock);
2737 #endif
2738         IBM_HANDLE_INIT(pci);
2739 #ifdef CONFIG_THINKPAD_ACPI_BAY
2740         IBM_HANDLE_INIT(bay);
2741         if (bay_handle)
2742                 IBM_HANDLE_INIT(bay_ej);
2743         IBM_HANDLE_INIT(bay2);
2744         if (bay2_handle)
2745                 IBM_HANDLE_INIT(bay2_ej);
2746 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2747         IBM_HANDLE_INIT(beep);
2748         IBM_HANDLE_INIT(ecrd);
2749         IBM_HANDLE_INIT(ecwr);
2750         IBM_HANDLE_INIT(fans);
2751         IBM_HANDLE_INIT(gfan);
2752         IBM_HANDLE_INIT(sfan);
2753
2754         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2755         if (!proc_dir) {
2756                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2757                 acpi_ibm_exit();
2758                 return -ENODEV;
2759         }
2760         proc_dir->owner = THIS_MODULE;
2761
2762         for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2763                 ret = ibm_init(&ibms[i]);
2764                 if (ret >= 0 && *ibms[i].param)
2765                         ret = ibms[i].write(ibms[i].param);
2766                 if (ret < 0) {
2767                         acpi_ibm_exit();
2768                         return ret;
2769                 }
2770         }
2771
2772         return 0;
2773 }
2774
2775 static void acpi_ibm_exit(void)
2776 {
2777         int i;
2778
2779         for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2780                 ibm_exit(&ibms[i]);
2781
2782         if (proc_dir)
2783                 remove_proc_entry(IBM_DIR, acpi_root_dir);
2784
2785         if (ibm_thinkpad_ec_found)
2786                 kfree(ibm_thinkpad_ec_found);
2787 }
2788
2789 module_init(acpi_ibm_init);
2790 module_exit(acpi_ibm_exit);