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