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