]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/thinkpad_acpi.c
ACPI: thinkpad-acpi: remove all uneeded initializers
[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 /*
96  * DMI matching for module autoloading
97  *
98  * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99  * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
100  *
101  * Only models listed in thinkwiki will be supported, so add yours
102  * if it is not there yet.
103  */
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105         MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
106
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
110
111 /* Ancient thinkpad BIOSes have to be identified by
112  * BIOS type or model number, and there are far less
113  * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
117
118 #define __unused __attribute__ ((unused))
119
120 /****************************************************************************
121  ****************************************************************************
122  *
123  * ACPI Helpers and device model
124  *
125  ****************************************************************************
126  ****************************************************************************/
127
128 /*************************************************************************
129  * ACPI basic handles
130  */
131
132 static acpi_handle root_handle;
133
134 #define IBM_HANDLE(object, parent, paths...)                    \
135         static acpi_handle  object##_handle;                    \
136         static acpi_handle *object##_parent = &parent##_handle; \
137         static char        *object##_path;                      \
138         static char        *object##_paths[] = { paths }
139
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
141            "\\_SB.PCI.ISA.EC",  /* 570 */
142            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
143            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
145            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
146            "\\_SB.PCI0.LPC.EC", /* all others */
147            );
148
149 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
151
152
153 /*************************************************************************
154  * Misc ACPI handles
155  */
156
157 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
158            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159            "\\CMS",             /* R40, R40e */
160            );                   /* all others */
161
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
163            "^HKEY",             /* R30, R31 */
164            "HKEY",              /* all others */
165            );                   /* 570 */
166
167
168 /*************************************************************************
169  * ACPI helpers
170  */
171
172 static int acpi_evalf(acpi_handle handle,
173                       void *res, char *method, char *fmt, ...)
174 {
175         char *fmt0 = fmt;
176         struct acpi_object_list params;
177         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178         struct acpi_buffer result, *resultp;
179         union acpi_object out_obj;
180         acpi_status status;
181         va_list ap;
182         char res_type;
183         int success;
184         int quiet;
185
186         if (!*fmt) {
187                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188                 return 0;
189         }
190
191         if (*fmt == 'q') {
192                 quiet = 1;
193                 fmt++;
194         } else
195                 quiet = 0;
196
197         res_type = *(fmt++);
198
199         params.count = 0;
200         params.pointer = &in_objs[0];
201
202         va_start(ap, fmt);
203         while (*fmt) {
204                 char c = *(fmt++);
205                 switch (c) {
206                 case 'd':       /* int */
207                         in_objs[params.count].integer.value = va_arg(ap, int);
208                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209                         break;
210                         /* add more types as needed */
211                 default:
212                         printk(IBM_ERR "acpi_evalf() called "
213                                "with invalid format character '%c'\n", c);
214                         return 0;
215                 }
216         }
217         va_end(ap);
218
219         if (res_type != 'v') {
220                 result.length = sizeof(out_obj);
221                 result.pointer = &out_obj;
222                 resultp = &result;
223         } else
224                 resultp = NULL;
225
226         status = acpi_evaluate_object(handle, method, &params, resultp);
227
228         switch (res_type) {
229         case 'd':               /* int */
230                 if (res)
231                         *(int *)res = out_obj.integer.value;
232                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233                 break;
234         case 'v':               /* void */
235                 success = status == AE_OK;
236                 break;
237                 /* add more types as needed */
238         default:
239                 printk(IBM_ERR "acpi_evalf() called "
240                        "with invalid format character '%c'\n", res_type);
241                 return 0;
242         }
243
244         if (!success && !quiet)
245                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246                        method, fmt0, status);
247
248         return success;
249 }
250
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
252 {
253         int i;
254
255         if (acpi_evalf(handle, &i, method, "d"))
256                 printk(IBM_INFO "%s = 0x%x\n", method, i);
257         else
258                 printk(IBM_ERR "error calling %s\n", method);
259 }
260
261 static int acpi_ec_read(int i, u8 * p)
262 {
263         int v;
264
265         if (ecrd_handle) {
266                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267                         return 0;
268                 *p = v;
269         } else {
270                 if (ec_read(i, p) < 0)
271                         return 0;
272         }
273
274         return 1;
275 }
276
277 static int acpi_ec_write(int i, u8 v)
278 {
279         if (ecwr_handle) {
280                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281                         return 0;
282         } else {
283                 if (ec_write(i, v) < 0)
284                         return 0;
285         }
286
287         return 1;
288 }
289
290 static int _sta(acpi_handle handle)
291 {
292         int status;
293
294         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295                 status = 0;
296
297         return status;
298 }
299
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
301 {
302         if (!cmos_handle)
303                 return -ENXIO;
304
305         if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306                 return -EIO;
307
308         return 0;
309 }
310
311 /*************************************************************************
312  * ACPI device model
313  */
314
315 static void drv_acpi_handle_init(char *name,
316                            acpi_handle *handle, acpi_handle parent,
317                            char **paths, int num_paths, char **path)
318 {
319         int i;
320         acpi_status status;
321
322         vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323                 name);
324
325         for (i = 0; i < num_paths; i++) {
326                 status = acpi_get_handle(parent, paths[i], handle);
327                 if (ACPI_SUCCESS(status)) {
328                         *path = paths[i];
329                         dbg_printk(TPACPI_DBG_INIT,
330                                    "Found ACPI handle %s for %s\n",
331                                    *path, name);
332                         return;
333                 }
334         }
335
336         vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337                     name);
338         *handle = NULL;
339 }
340
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
342 {
343         struct ibm_struct *ibm = data;
344
345         if (!ibm || !ibm->acpi || !ibm->acpi->notify)
346                 return;
347
348         ibm->acpi->notify(ibm, event);
349 }
350
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
352 {
353         acpi_status status;
354         int rc;
355
356         BUG_ON(!ibm->acpi);
357
358         if (!*ibm->acpi->handle)
359                 return 0;
360
361         vdbg_printk(TPACPI_DBG_INIT,
362                 "setting up ACPI notify for %s\n", ibm->name);
363
364         rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365         if (rc < 0) {
366                 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367                         ibm->name, rc);
368                 return -ENODEV;
369         }
370
371         acpi_driver_data(ibm->acpi->device) = ibm;
372         sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373                 IBM_ACPI_EVENT_PREFIX,
374                 ibm->name);
375
376         status = acpi_install_notify_handler(*ibm->acpi->handle,
377                         ibm->acpi->type, dispatch_acpi_notify, ibm);
378         if (ACPI_FAILURE(status)) {
379                 if (status == AE_ALREADY_EXISTS) {
380                         printk(IBM_NOTICE "another device driver is already handling %s events\n",
381                                 ibm->name);
382                 } else {
383                         printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384                                 ibm->name, status);
385                 }
386                 return -ENODEV;
387         }
388         ibm->flags.acpi_notify_installed = 1;
389         return 0;
390 }
391
392 static int __init tpacpi_device_add(struct acpi_device *device)
393 {
394         return 0;
395 }
396
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
398 {
399         int rc;
400
401         dbg_printk(TPACPI_DBG_INIT,
402                 "registering %s as an ACPI driver\n", ibm->name);
403
404         BUG_ON(!ibm->acpi);
405
406         ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407         if (!ibm->acpi->driver) {
408                 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409                 return -ENOMEM;
410         }
411
412         sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413         ibm->acpi->driver->ids = ibm->acpi->hid;
414         ibm->acpi->driver->ops.add = &tpacpi_device_add;
415
416         rc = acpi_bus_register_driver(ibm->acpi->driver);
417         if (rc < 0) {
418                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
419                        ibm->acpi->hid, rc);
420                 kfree(ibm->acpi->driver);
421                 ibm->acpi->driver = NULL;
422         } else if (!rc)
423                 ibm->flags.acpi_driver_registered = 1;
424
425         return rc;
426 }
427
428
429 /****************************************************************************
430  ****************************************************************************
431  *
432  * Procfs Helpers
433  *
434  ****************************************************************************
435  ****************************************************************************/
436
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438                         int count, int *eof, void *data)
439 {
440         struct ibm_struct *ibm = data;
441         int len;
442
443         if (!ibm || !ibm->read)
444                 return -EINVAL;
445
446         len = ibm->read(page);
447         if (len < 0)
448                 return len;
449
450         if (len <= off + count)
451                 *eof = 1;
452         *start = page + off;
453         len -= off;
454         if (len > count)
455                 len = count;
456         if (len < 0)
457                 len = 0;
458
459         return len;
460 }
461
462 static int dispatch_procfs_write(struct file *file,
463                         const char __user * userbuf,
464                         unsigned long count, void *data)
465 {
466         struct ibm_struct *ibm = data;
467         char *kernbuf;
468         int ret;
469
470         if (!ibm || !ibm->write)
471                 return -EINVAL;
472
473         kernbuf = kmalloc(count + 2, GFP_KERNEL);
474         if (!kernbuf)
475                 return -ENOMEM;
476
477         if (copy_from_user(kernbuf, userbuf, count)) {
478                 kfree(kernbuf);
479                 return -EFAULT;
480         }
481
482         kernbuf[count] = 0;
483         strcat(kernbuf, ",");
484         ret = ibm->write(kernbuf);
485         if (ret == 0)
486                 ret = count;
487
488         kfree(kernbuf);
489
490         return ret;
491 }
492
493 static char *next_cmd(char **cmds)
494 {
495         char *start = *cmds;
496         char *end;
497
498         while ((end = strchr(start, ',')) && end == start)
499                 start = end + 1;
500
501         if (!end)
502                 return NULL;
503
504         *end = 0;
505         *cmds = end + 1;
506         return start;
507 }
508
509
510 /****************************************************************************
511  ****************************************************************************
512  *
513  * Device model: hwmon and platform
514  *
515  ****************************************************************************
516  ****************************************************************************/
517
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520
521 static struct platform_driver tpacpi_pdriver = {
522         .driver = {
523                 .name = IBM_DRVR_NAME,
524                 .owner = THIS_MODULE,
525         },
526 };
527
528
529 /*************************************************************************
530  * thinkpad-acpi driver attributes
531  */
532
533 /* interface_version --------------------------------------------------- */
534 static ssize_t tpacpi_driver_interface_version_show(
535                                 struct device_driver *drv,
536                                 char *buf)
537 {
538         return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
539 }
540
541 static DRIVER_ATTR(interface_version, S_IRUGO,
542                 tpacpi_driver_interface_version_show, NULL);
543
544 /* debug_level --------------------------------------------------------- */
545 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
546                                                 char *buf)
547 {
548         return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
549 }
550
551 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
552                                                 const char *buf, size_t count)
553 {
554         unsigned long t;
555
556         if (parse_strtoul(buf, 0xffff, &t))
557                 return -EINVAL;
558
559         dbg_level = t;
560
561         return count;
562 }
563
564 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
565                 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
566
567 /* version ------------------------------------------------------------- */
568 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
569                                                 char *buf)
570 {
571         return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
572 }
573
574 static DRIVER_ATTR(version, S_IRUGO,
575                 tpacpi_driver_version_show, NULL);
576
577 /* --------------------------------------------------------------------- */
578
579 static struct driver_attribute* tpacpi_driver_attributes[] = {
580         &driver_attr_debug_level, &driver_attr_version,
581         &driver_attr_interface_version,
582 };
583
584 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
585 {
586         int i, res;
587
588         i = 0;
589         res = 0;
590         while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
591                 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
592                 i++;
593         }
594
595         return res;
596 }
597
598 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
599 {
600         int i;
601
602         for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
603                 driver_remove_file(drv, tpacpi_driver_attributes[i]);
604 }
605
606 /*************************************************************************
607  * sysfs support helpers
608  */
609
610 struct attribute_set_obj {
611         struct attribute_set s;
612         struct attribute *a;
613 } __attribute__((packed));
614
615 static struct attribute_set *create_attr_set(unsigned int max_members,
616                                                 const char* name)
617 {
618         struct attribute_set_obj *sobj;
619
620         if (max_members == 0)
621                 return NULL;
622
623         /* Allocates space for implicit NULL at the end too */
624         sobj = kzalloc(sizeof(struct attribute_set_obj) +
625                     max_members * sizeof(struct attribute *),
626                     GFP_KERNEL);
627         if (!sobj)
628                 return NULL;
629         sobj->s.max_members = max_members;
630         sobj->s.group.attrs = &sobj->a;
631         sobj->s.group.name = name;
632
633         return &sobj->s;
634 }
635
636 /* not multi-threaded safe, use it in a single thread per set */
637 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
638 {
639         if (!s || !attr)
640                 return -EINVAL;
641
642         if (s->members >= s->max_members)
643                 return -ENOMEM;
644
645         s->group.attrs[s->members] = attr;
646         s->members++;
647
648         return 0;
649 }
650
651 static int add_many_to_attr_set(struct attribute_set* s,
652                         struct attribute **attr,
653                         unsigned int count)
654 {
655         int i, res;
656
657         for (i = 0; i < count; i++) {
658                 res = add_to_attr_set(s, attr[i]);
659                 if (res)
660                         return res;
661         }
662
663         return 0;
664 }
665
666 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
667 {
668         sysfs_remove_group(kobj, &s->group);
669         destroy_attr_set(s);
670 }
671
672 static int parse_strtoul(const char *buf,
673                 unsigned long max, unsigned long *value)
674 {
675         char *endp;
676
677         *value = simple_strtoul(buf, &endp, 0);
678         while (*endp && isspace(*endp))
679                 endp++;
680         if (*endp || *value > max)
681                 return -EINVAL;
682
683         return 0;
684 }
685
686 /****************************************************************************
687  ****************************************************************************
688  *
689  * Subdrivers
690  *
691  ****************************************************************************
692  ****************************************************************************/
693
694 /*************************************************************************
695  * thinkpad-acpi init subdriver
696  */
697
698 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
699 {
700         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
701         printk(IBM_INFO "%s\n", IBM_URL);
702
703         if (ibm_thinkpad_ec_found)
704                 printk(IBM_INFO "ThinkPad EC firmware %s\n",
705                        ibm_thinkpad_ec_found);
706
707         return 0;
708 }
709
710 static int thinkpad_acpi_driver_read(char *p)
711 {
712         int len = 0;
713
714         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
715         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
716
717         return len;
718 }
719
720 static struct ibm_struct thinkpad_acpi_driver_data = {
721         .name = "driver",
722         .read = thinkpad_acpi_driver_read,
723 };
724
725 /*************************************************************************
726  * Hotkey subdriver
727  */
728
729 static int hotkey_orig_status;
730 static int hotkey_orig_mask;
731
732 static struct attribute_set *hotkey_dev_attributes;
733
734 /* sysfs hotkey enable ------------------------------------------------- */
735 static ssize_t hotkey_enable_show(struct device *dev,
736                            struct device_attribute *attr,
737                            char *buf)
738 {
739         int res, status, mask;
740
741         res = hotkey_get(&status, &mask);
742         if (res)
743                 return res;
744
745         return snprintf(buf, PAGE_SIZE, "%d\n", status);
746 }
747
748 static ssize_t hotkey_enable_store(struct device *dev,
749                             struct device_attribute *attr,
750                             const char *buf, size_t count)
751 {
752         unsigned long t;
753         int res, status, mask;
754
755         if (parse_strtoul(buf, 1, &t))
756                 return -EINVAL;
757
758         res = hotkey_get(&status, &mask);
759         if (!res)
760                 res = hotkey_set(t, mask);
761
762         return (res) ? res : count;
763 }
764
765 static struct device_attribute dev_attr_hotkey_enable =
766         __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
767                 hotkey_enable_show, hotkey_enable_store);
768
769 /* sysfs hotkey mask --------------------------------------------------- */
770 static ssize_t hotkey_mask_show(struct device *dev,
771                            struct device_attribute *attr,
772                            char *buf)
773 {
774         int res, status, mask;
775
776         res = hotkey_get(&status, &mask);
777         if (res)
778                 return res;
779
780         return snprintf(buf, PAGE_SIZE, "0x%04x\n", mask);
781 }
782
783 static ssize_t hotkey_mask_store(struct device *dev,
784                             struct device_attribute *attr,
785                             const char *buf, size_t count)
786 {
787         unsigned long t;
788         int res, status, mask;
789
790         if (parse_strtoul(buf, 0xffff, &t))
791                 return -EINVAL;
792
793         res = hotkey_get(&status, &mask);
794         if (!res)
795                 hotkey_set(status, t);
796
797         return (res) ? res : count;
798 }
799
800 static struct device_attribute dev_attr_hotkey_mask =
801         __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
802                 hotkey_mask_show, hotkey_mask_store);
803
804 /* sysfs hotkey bios_enabled ------------------------------------------- */
805 static ssize_t hotkey_bios_enabled_show(struct device *dev,
806                            struct device_attribute *attr,
807                            char *buf)
808 {
809         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
810 }
811
812 static struct device_attribute dev_attr_hotkey_bios_enabled =
813         __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
814
815 /* sysfs hotkey bios_mask ---------------------------------------------- */
816 static ssize_t hotkey_bios_mask_show(struct device *dev,
817                            struct device_attribute *attr,
818                            char *buf)
819 {
820         return snprintf(buf, PAGE_SIZE, "0x%04x\n", hotkey_orig_mask);
821 }
822
823 static struct device_attribute dev_attr_hotkey_bios_mask =
824         __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
825
826 /* --------------------------------------------------------------------- */
827
828 static struct attribute *hotkey_mask_attributes[] = {
829         &dev_attr_hotkey_mask.attr,
830         &dev_attr_hotkey_bios_enabled.attr,
831         &dev_attr_hotkey_bios_mask.attr,
832 };
833
834 static int __init hotkey_init(struct ibm_init_struct *iibm)
835 {
836         int res;
837
838         vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
839
840         IBM_ACPIHANDLE_INIT(hkey);
841         mutex_init(&hotkey_mutex);
842
843         /* hotkey not supported on 570 */
844         tp_features.hotkey = hkey_handle != NULL;
845
846         vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
847                 str_supported(tp_features.hotkey));
848
849         if (tp_features.hotkey) {
850                 hotkey_dev_attributes = create_attr_set(4, NULL);
851                 if (!hotkey_dev_attributes)
852                         return -ENOMEM;
853                 res = add_to_attr_set(hotkey_dev_attributes,
854                                 &dev_attr_hotkey_enable.attr);
855                 if (res)
856                         return res;
857
858                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
859                    A30, R30, R31, T20-22, X20-21, X22-24 */
860                 tp_features.hotkey_mask =
861                         acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
862
863                 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
864                         str_supported(tp_features.hotkey_mask));
865
866                 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
867                 if (!res && tp_features.hotkey_mask) {
868                         res = add_many_to_attr_set(hotkey_dev_attributes,
869                                 hotkey_mask_attributes,
870                                 ARRAY_SIZE(hotkey_mask_attributes));
871                 }
872                 if (!res)
873                         res = register_attr_set_with_sysfs(
874                                         hotkey_dev_attributes,
875                                         &tpacpi_pdev->dev.kobj);
876
877                 if (res)
878                         return res;
879         }
880
881         return (tp_features.hotkey)? 0 : 1;
882 }
883
884 static void hotkey_exit(void)
885 {
886         int res;
887
888         if (tp_features.hotkey) {
889                 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
890                 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
891                 if (res)
892                         printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
893         }
894
895         if (hotkey_dev_attributes) {
896                 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
897                 hotkey_dev_attributes = NULL;
898         }
899 }
900
901 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
902 {
903         int hkey;
904
905         if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
906                 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
907         else {
908                 printk(IBM_ERR "unknown hotkey event %d\n", event);
909                 acpi_bus_generate_event(ibm->acpi->device, event, 0);
910         }
911 }
912
913 /*
914  * Call with hotkey_mutex held
915  */
916 static int hotkey_get(int *status, int *mask)
917 {
918         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
919                 return -EIO;
920
921         if (tp_features.hotkey_mask)
922                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
923                         return -EIO;
924
925         return 0;
926 }
927
928 /*
929  * Call with hotkey_mutex held
930  */
931 static int hotkey_set(int status, int mask)
932 {
933         int i;
934
935         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
936                 return -EIO;
937
938         if (tp_features.hotkey_mask)
939                 for (i = 0; i < 32; i++) {
940                         int bit = ((1 << i) & mask) != 0;
941                         if (!acpi_evalf(hkey_handle,
942                                         NULL, "MHKM", "vdd", i + 1, bit))
943                                 return -EIO;
944                 }
945
946         return 0;
947 }
948
949 /* procfs -------------------------------------------------------------- */
950 static int hotkey_read(char *p)
951 {
952         int res, status, mask;
953         int len = 0;
954
955         if (!tp_features.hotkey) {
956                 len += sprintf(p + len, "status:\t\tnot supported\n");
957                 return len;
958         }
959
960         res = mutex_lock_interruptible(&hotkey_mutex);
961         if (res < 0)
962                 return res;
963         res = hotkey_get(&status, &mask);
964         mutex_unlock(&hotkey_mutex);
965         if (res)
966                 return res;
967
968         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
969         if (tp_features.hotkey_mask) {
970                 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
971                 len += sprintf(p + len,
972                                "commands:\tenable, disable, reset, <mask>\n");
973         } else {
974                 len += sprintf(p + len, "mask:\t\tnot supported\n");
975                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
976         }
977
978         return len;
979 }
980
981 static int hotkey_write(char *buf)
982 {
983         int res, status, mask;
984         char *cmd;
985         int do_cmd = 0;
986
987         if (!tp_features.hotkey)
988                 return -ENODEV;
989
990         res = mutex_lock_interruptible(&hotkey_mutex);
991         if (res < 0)
992                 return res;
993
994         res = hotkey_get(&status, &mask);
995         if (res)
996                 goto errexit;
997
998         res = 0;
999         while ((cmd = next_cmd(&buf))) {
1000                 if (strlencmp(cmd, "enable") == 0) {
1001                         status = 1;
1002                 } else if (strlencmp(cmd, "disable") == 0) {
1003                         status = 0;
1004                 } else if (strlencmp(cmd, "reset") == 0) {
1005                         status = hotkey_orig_status;
1006                         mask = hotkey_orig_mask;
1007                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1008                         /* mask set */
1009                 } else if (sscanf(cmd, "%x", &mask) == 1) {
1010                         /* mask set */
1011                 } else {
1012                         res = -EINVAL;
1013                         goto errexit;
1014                 }
1015                 do_cmd = 1;
1016         }
1017
1018         if (do_cmd)
1019                 res = hotkey_set(status, mask);
1020
1021 errexit:
1022         mutex_unlock(&hotkey_mutex);
1023         return res;
1024 }
1025
1026 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1027         .hid = IBM_HKEY_HID,
1028         .notify = hotkey_notify,
1029         .handle = &hkey_handle,
1030         .type = ACPI_DEVICE_NOTIFY,
1031 };
1032
1033 static struct ibm_struct hotkey_driver_data = {
1034         .name = "hotkey",
1035         .read = hotkey_read,
1036         .write = hotkey_write,
1037         .exit = hotkey_exit,
1038         .acpi = &ibm_hotkey_acpidriver,
1039 };
1040
1041 /*************************************************************************
1042  * Bluetooth subdriver
1043  */
1044
1045 /* sysfs bluetooth enable ---------------------------------------------- */
1046 static ssize_t bluetooth_enable_show(struct device *dev,
1047                            struct device_attribute *attr,
1048                            char *buf)
1049 {
1050         int status;
1051
1052         status = bluetooth_get_radiosw();
1053         if (status < 0)
1054                 return status;
1055
1056         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1057 }
1058
1059 static ssize_t bluetooth_enable_store(struct device *dev,
1060                             struct device_attribute *attr,
1061                             const char *buf, size_t count)
1062 {
1063         unsigned long t;
1064         int res;
1065
1066         if (parse_strtoul(buf, 1, &t))
1067                 return -EINVAL;
1068
1069         res = bluetooth_set_radiosw(t);
1070
1071         return (res) ? res : count;
1072 }
1073
1074 static struct device_attribute dev_attr_bluetooth_enable =
1075         __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1076                 bluetooth_enable_show, bluetooth_enable_store);
1077
1078 /* --------------------------------------------------------------------- */
1079
1080 static struct attribute *bluetooth_attributes[] = {
1081         &dev_attr_bluetooth_enable.attr,
1082         NULL
1083 };
1084
1085 static const struct attribute_group bluetooth_attr_group = {
1086         .attrs = bluetooth_attributes,
1087 };
1088
1089 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1090 {
1091         int res;
1092         int status = 0;
1093
1094         vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1095
1096         IBM_ACPIHANDLE_INIT(hkey);
1097
1098         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1099            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1100         tp_features.bluetooth = hkey_handle &&
1101             acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1102
1103         vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1104                 str_supported(tp_features.bluetooth),
1105                 status);
1106
1107         if (tp_features.bluetooth) {
1108                 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1109                         /* no bluetooth hardware present in system */
1110                         tp_features.bluetooth = 0;
1111                         dbg_printk(TPACPI_DBG_INIT,
1112                                    "bluetooth hardware not installed\n");
1113                 } else {
1114                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1115                                         &bluetooth_attr_group);
1116                         if (res)
1117                                 return res;
1118                 }
1119         }
1120
1121         return (tp_features.bluetooth)? 0 : 1;
1122 }
1123
1124 static void bluetooth_exit(void)
1125 {
1126         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1127                         &bluetooth_attr_group);
1128 }
1129
1130 static int bluetooth_get_radiosw(void)
1131 {
1132         int status;
1133
1134         if (!tp_features.bluetooth)
1135                 return -ENODEV;
1136
1137         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1138                 return -EIO;
1139
1140         return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1141 }
1142
1143 static int bluetooth_set_radiosw(int radio_on)
1144 {
1145         int status;
1146
1147         if (!tp_features.bluetooth)
1148                 return -ENODEV;
1149
1150         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1151                 return -EIO;
1152         if (radio_on)
1153                 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1154         else
1155                 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1156         if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1157                 return -EIO;
1158
1159         return 0;
1160 }
1161
1162 /* procfs -------------------------------------------------------------- */
1163 static int bluetooth_read(char *p)
1164 {
1165         int len = 0;
1166         int status = bluetooth_get_radiosw();
1167
1168         if (!tp_features.bluetooth)
1169                 len += sprintf(p + len, "status:\t\tnot supported\n");
1170         else {
1171                 len += sprintf(p + len, "status:\t\t%s\n",
1172                                 (status)? "enabled" : "disabled");
1173                 len += sprintf(p + len, "commands:\tenable, disable\n");
1174         }
1175
1176         return len;
1177 }
1178
1179 static int bluetooth_write(char *buf)
1180 {
1181         char *cmd;
1182
1183         if (!tp_features.bluetooth)
1184                 return -ENODEV;
1185
1186         while ((cmd = next_cmd(&buf))) {
1187                 if (strlencmp(cmd, "enable") == 0) {
1188                         bluetooth_set_radiosw(1);
1189                 } else if (strlencmp(cmd, "disable") == 0) {
1190                         bluetooth_set_radiosw(0);
1191                 } else
1192                         return -EINVAL;
1193         }
1194
1195         return 0;
1196 }
1197
1198 static struct ibm_struct bluetooth_driver_data = {
1199         .name = "bluetooth",
1200         .read = bluetooth_read,
1201         .write = bluetooth_write,
1202         .exit = bluetooth_exit,
1203 };
1204
1205 /*************************************************************************
1206  * Wan subdriver
1207  */
1208
1209 /* sysfs wan enable ---------------------------------------------------- */
1210 static ssize_t wan_enable_show(struct device *dev,
1211                            struct device_attribute *attr,
1212                            char *buf)
1213 {
1214         int status;
1215
1216         status = wan_get_radiosw();
1217         if (status < 0)
1218                 return status;
1219
1220         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1221 }
1222
1223 static ssize_t wan_enable_store(struct device *dev,
1224                             struct device_attribute *attr,
1225                             const char *buf, size_t count)
1226 {
1227         unsigned long t;
1228         int res;
1229
1230         if (parse_strtoul(buf, 1, &t))
1231                 return -EINVAL;
1232
1233         res = wan_set_radiosw(t);
1234
1235         return (res) ? res : count;
1236 }
1237
1238 static struct device_attribute dev_attr_wan_enable =
1239         __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1240                 wan_enable_show, wan_enable_store);
1241
1242 /* --------------------------------------------------------------------- */
1243
1244 static struct attribute *wan_attributes[] = {
1245         &dev_attr_wan_enable.attr,
1246         NULL
1247 };
1248
1249 static const struct attribute_group wan_attr_group = {
1250         .attrs = wan_attributes,
1251 };
1252
1253 static int __init wan_init(struct ibm_init_struct *iibm)
1254 {
1255         int res;
1256         int status = 0;
1257
1258         vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1259
1260         IBM_ACPIHANDLE_INIT(hkey);
1261
1262         tp_features.wan = hkey_handle &&
1263             acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1264
1265         vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1266                 str_supported(tp_features.wan),
1267                 status);
1268
1269         if (tp_features.wan) {
1270                 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1271                         /* no wan hardware present in system */
1272                         tp_features.wan = 0;
1273                         dbg_printk(TPACPI_DBG_INIT,
1274                                    "wan hardware not installed\n");
1275                 } else {
1276                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1277                                         &wan_attr_group);
1278                         if (res)
1279                                 return res;
1280                 }
1281         }
1282
1283         return (tp_features.wan)? 0 : 1;
1284 }
1285
1286 static void wan_exit(void)
1287 {
1288         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1289                 &wan_attr_group);
1290 }
1291
1292 static int wan_get_radiosw(void)
1293 {
1294         int status;
1295
1296         if (!tp_features.wan)
1297                 return -ENODEV;
1298
1299         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1300                 return -EIO;
1301
1302         return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1303 }
1304
1305 static int wan_set_radiosw(int radio_on)
1306 {
1307         int status;
1308
1309         if (!tp_features.wan)
1310                 return -ENODEV;
1311
1312         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1313                 return -EIO;
1314         if (radio_on)
1315                 status |= TP_ACPI_WANCARD_RADIOSSW;
1316         else
1317                 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1318         if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1319                 return -EIO;
1320
1321         return 0;
1322 }
1323
1324 /* procfs -------------------------------------------------------------- */
1325 static int wan_read(char *p)
1326 {
1327         int len = 0;
1328         int status = wan_get_radiosw();
1329
1330         if (!tp_features.wan)
1331                 len += sprintf(p + len, "status:\t\tnot supported\n");
1332         else {
1333                 len += sprintf(p + len, "status:\t\t%s\n",
1334                                 (status)? "enabled" : "disabled");
1335                 len += sprintf(p + len, "commands:\tenable, disable\n");
1336         }
1337
1338         return len;
1339 }
1340
1341 static int wan_write(char *buf)
1342 {
1343         char *cmd;
1344
1345         if (!tp_features.wan)
1346                 return -ENODEV;
1347
1348         while ((cmd = next_cmd(&buf))) {
1349                 if (strlencmp(cmd, "enable") == 0) {
1350                         wan_set_radiosw(1);
1351                 } else if (strlencmp(cmd, "disable") == 0) {
1352                         wan_set_radiosw(0);
1353                 } else
1354                         return -EINVAL;
1355         }
1356
1357         return 0;
1358 }
1359
1360 static struct ibm_struct wan_driver_data = {
1361         .name = "wan",
1362         .read = wan_read,
1363         .write = wan_write,
1364         .exit = wan_exit,
1365         .flags.experimental = 1,
1366 };
1367
1368 /*************************************************************************
1369  * Video subdriver
1370  */
1371
1372 static enum video_access_mode video_supported;
1373 static int video_orig_autosw;
1374
1375 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
1376            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
1377            "\\_SB.PCI0.VID0",   /* 770e */
1378            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
1379            "\\_SB.PCI0.AGP.VID",        /* all others */
1380            );                           /* R30, R31 */
1381
1382 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
1383
1384 static int __init video_init(struct ibm_init_struct *iibm)
1385 {
1386         int ivga;
1387
1388         vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1389
1390         IBM_ACPIHANDLE_INIT(vid);
1391         IBM_ACPIHANDLE_INIT(vid2);
1392
1393         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1394                 /* G41, assume IVGA doesn't change */
1395                 vid_handle = vid2_handle;
1396
1397         if (!vid_handle)
1398                 /* video switching not supported on R30, R31 */
1399                 video_supported = TPACPI_VIDEO_NONE;
1400         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1401                 /* 570 */
1402                 video_supported = TPACPI_VIDEO_570;
1403         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1404                 /* 600e/x, 770e, 770x */
1405                 video_supported = TPACPI_VIDEO_770;
1406         else
1407                 /* all others */
1408                 video_supported = TPACPI_VIDEO_NEW;
1409
1410         vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1411                 str_supported(video_supported != TPACPI_VIDEO_NONE),
1412                 video_supported);
1413
1414         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1415 }
1416
1417 static void video_exit(void)
1418 {
1419         dbg_printk(TPACPI_DBG_EXIT,
1420                    "restoring original video autoswitch mode\n");
1421         if (video_autosw_set(video_orig_autosw))
1422                 printk(IBM_ERR "error while trying to restore original "
1423                         "video autoswitch mode\n");
1424 }
1425
1426 static int video_outputsw_get(void)
1427 {
1428         int status = 0;
1429         int i;
1430
1431         switch (video_supported) {
1432         case TPACPI_VIDEO_570:
1433                 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1434                                  TP_ACPI_VIDEO_570_PHSCMD))
1435                         return -EIO;
1436                 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1437                 break;
1438         case TPACPI_VIDEO_770:
1439                 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1440                         return -EIO;
1441                 if (i)
1442                         status |= TP_ACPI_VIDEO_S_LCD;
1443                 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1444                         return -EIO;
1445                 if (i)
1446                         status |= TP_ACPI_VIDEO_S_CRT;
1447                 break;
1448         case TPACPI_VIDEO_NEW:
1449                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1450                     !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1451                         return -EIO;
1452                 if (i)
1453                         status |= TP_ACPI_VIDEO_S_CRT;
1454
1455                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1456                     !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1457                         return -EIO;
1458                 if (i)
1459                         status |= TP_ACPI_VIDEO_S_LCD;
1460                 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1461                         return -EIO;
1462                 if (i)
1463                         status |= TP_ACPI_VIDEO_S_DVI;
1464                 break;
1465         default:
1466                 return -ENOSYS;
1467         }
1468
1469         return status;
1470 }
1471
1472 static int video_outputsw_set(int status)
1473 {
1474         int autosw;
1475         int res = 0;
1476
1477         switch (video_supported) {
1478         case TPACPI_VIDEO_570:
1479                 res = acpi_evalf(NULL, NULL,
1480                                  "\\_SB.PHS2", "vdd",
1481                                  TP_ACPI_VIDEO_570_PHS2CMD,
1482                                  status | TP_ACPI_VIDEO_570_PHS2SET);
1483                 break;
1484         case TPACPI_VIDEO_770:
1485                 autosw = video_autosw_get();
1486                 if (autosw < 0)
1487                         return autosw;
1488
1489                 res = video_autosw_set(1);
1490                 if (res)
1491                         return res;
1492                 res = acpi_evalf(vid_handle, NULL,
1493                                  "ASWT", "vdd", status * 0x100, 0);
1494                 if (!autosw && video_autosw_set(autosw)) {
1495                         printk(IBM_ERR "video auto-switch left enabled due to error\n");
1496                         return -EIO;
1497                 }
1498                 break;
1499         case TPACPI_VIDEO_NEW:
1500                 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1501                         acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1502                 break;
1503         default:
1504                 return -ENOSYS;
1505         }
1506
1507         return (res)? 0 : -EIO;
1508 }
1509
1510 static int video_autosw_get(void)
1511 {
1512         int autosw = 0;
1513
1514         switch (video_supported) {
1515         case TPACPI_VIDEO_570:
1516                 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1517                         return -EIO;
1518                 break;
1519         case TPACPI_VIDEO_770:
1520         case TPACPI_VIDEO_NEW:
1521                 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1522                         return -EIO;
1523                 break;
1524         default:
1525                 return -ENOSYS;
1526         }
1527
1528         return autosw & 1;
1529 }
1530
1531 static int video_autosw_set(int enable)
1532 {
1533         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1534                 return -EIO;
1535         return 0;
1536 }
1537
1538 static int video_outputsw_cycle(void)
1539 {
1540         int autosw = video_autosw_get();
1541         int res;
1542
1543         if (autosw < 0)
1544                 return autosw;
1545
1546         switch (video_supported) {
1547         case TPACPI_VIDEO_570:
1548                 res = video_autosw_set(1);
1549                 if (res)
1550                         return res;
1551                 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1552                 break;
1553         case TPACPI_VIDEO_770:
1554         case TPACPI_VIDEO_NEW:
1555                 res = video_autosw_set(1);
1556                 if (res)
1557                         return res;
1558                 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1559                 break;
1560         default:
1561                 return -ENOSYS;
1562         }
1563         if (!autosw && video_autosw_set(autosw)) {
1564                 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1565                 return -EIO;
1566         }
1567
1568         return (res)? 0 : -EIO;
1569 }
1570
1571 static int video_expand_toggle(void)
1572 {
1573         switch (video_supported) {
1574         case TPACPI_VIDEO_570:
1575                 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1576                         0 : -EIO;
1577         case TPACPI_VIDEO_770:
1578                 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1579                         0 : -EIO;
1580         case TPACPI_VIDEO_NEW:
1581                 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1582                         0 : -EIO;
1583         default:
1584                 return -ENOSYS;
1585         }
1586         /* not reached */
1587 }
1588
1589 static int video_read(char *p)
1590 {
1591         int status, autosw;
1592         int len = 0;
1593
1594         if (video_supported == TPACPI_VIDEO_NONE) {
1595                 len += sprintf(p + len, "status:\t\tnot supported\n");
1596                 return len;
1597         }
1598
1599         status = video_outputsw_get();
1600         if (status < 0)
1601                 return status;
1602
1603         autosw = video_autosw_get();
1604         if (autosw < 0)
1605                 return autosw;
1606
1607         len += sprintf(p + len, "status:\t\tsupported\n");
1608         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1609         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1610         if (video_supported == TPACPI_VIDEO_NEW)
1611                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1612         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1613         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1614         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1615         if (video_supported == TPACPI_VIDEO_NEW)
1616                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1617         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1618         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1619
1620         return len;
1621 }
1622
1623 static int video_write(char *buf)
1624 {
1625         char *cmd;
1626         int enable, disable, status;
1627         int res;
1628
1629         if (video_supported == TPACPI_VIDEO_NONE)
1630                 return -ENODEV;
1631
1632         enable = 0;
1633         disable = 0;
1634
1635         while ((cmd = next_cmd(&buf))) {
1636                 if (strlencmp(cmd, "lcd_enable") == 0) {
1637                         enable |= TP_ACPI_VIDEO_S_LCD;
1638                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1639                         disable |= TP_ACPI_VIDEO_S_LCD;
1640                 } else if (strlencmp(cmd, "crt_enable") == 0) {
1641                         enable |= TP_ACPI_VIDEO_S_CRT;
1642                 } else if (strlencmp(cmd, "crt_disable") == 0) {
1643                         disable |= TP_ACPI_VIDEO_S_CRT;
1644                 } else if (video_supported == TPACPI_VIDEO_NEW &&
1645                            strlencmp(cmd, "dvi_enable") == 0) {
1646                         enable |= TP_ACPI_VIDEO_S_DVI;
1647                 } else if (video_supported == TPACPI_VIDEO_NEW &&
1648                            strlencmp(cmd, "dvi_disable") == 0) {
1649                         disable |= TP_ACPI_VIDEO_S_DVI;
1650                 } else if (strlencmp(cmd, "auto_enable") == 0) {
1651                         res = video_autosw_set(1);
1652                         if (res)
1653                                 return res;
1654                 } else if (strlencmp(cmd, "auto_disable") == 0) {
1655                         res = video_autosw_set(0);
1656                         if (res)
1657                                 return res;
1658                 } else if (strlencmp(cmd, "video_switch") == 0) {
1659                         res = video_outputsw_cycle();
1660                         if (res)
1661                                 return res;
1662                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1663                         res = video_expand_toggle();
1664                         if (res)
1665                                 return res;
1666                 } else
1667                         return -EINVAL;
1668         }
1669
1670         if (enable || disable) {
1671                 status = video_outputsw_get();
1672                 if (status < 0)
1673                         return status;
1674                 res = video_outputsw_set((status & ~disable) | enable);
1675                 if (res)
1676                         return res;
1677         }
1678
1679         return 0;
1680 }
1681
1682 static struct ibm_struct video_driver_data = {
1683         .name = "video",
1684         .read = video_read,
1685         .write = video_write,
1686         .exit = video_exit,
1687 };
1688
1689 /*************************************************************************
1690  * Light (thinklight) subdriver
1691  */
1692
1693 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A2xm/p, T20-22, X20-21 */
1694 IBM_HANDLE(ledb, ec, "LEDB");           /* G4x */
1695
1696 static int __init light_init(struct ibm_init_struct *iibm)
1697 {
1698         vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1699
1700         IBM_ACPIHANDLE_INIT(ledb);
1701         IBM_ACPIHANDLE_INIT(lght);
1702         IBM_ACPIHANDLE_INIT(cmos);
1703
1704         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1705         tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1706
1707         if (tp_features.light)
1708                 /* light status not supported on
1709                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1710                 tp_features.light_status =
1711                         acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1712
1713         vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1714                 str_supported(tp_features.light));
1715
1716         return (tp_features.light)? 0 : 1;
1717 }
1718
1719 static int light_read(char *p)
1720 {
1721         int len = 0;
1722         int status = 0;
1723
1724         if (!tp_features.light) {
1725                 len += sprintf(p + len, "status:\t\tnot supported\n");
1726         } else if (!tp_features.light_status) {
1727                 len += sprintf(p + len, "status:\t\tunknown\n");
1728                 len += sprintf(p + len, "commands:\ton, off\n");
1729         } else {
1730                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1731                         return -EIO;
1732                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1733                 len += sprintf(p + len, "commands:\ton, off\n");
1734         }
1735
1736         return len;
1737 }
1738
1739 static int light_write(char *buf)
1740 {
1741         int cmos_cmd, lght_cmd;
1742         char *cmd;
1743         int success;
1744
1745         if (!tp_features.light)
1746                 return -ENODEV;
1747
1748         while ((cmd = next_cmd(&buf))) {
1749                 if (strlencmp(cmd, "on") == 0) {
1750                         cmos_cmd = 0x0c;
1751                         lght_cmd = 1;
1752                 } else if (strlencmp(cmd, "off") == 0) {
1753                         cmos_cmd = 0x0d;
1754                         lght_cmd = 0;
1755                 } else
1756                         return -EINVAL;
1757
1758                 success = cmos_handle ?
1759                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1760                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1761                 if (!success)
1762                         return -EIO;
1763         }
1764
1765         return 0;
1766 }
1767
1768 static struct ibm_struct light_driver_data = {
1769         .name = "light",
1770         .read = light_read,
1771         .write = light_write,
1772 };
1773
1774 /*************************************************************************
1775  * Dock subdriver
1776  */
1777
1778 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1779
1780 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
1781            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1782            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
1783            "\\_SB.PCI.ISA.SLCE",        /* 570 */
1784     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1785
1786 /* don't list other alternatives as we install a notify handler on the 570 */
1787 IBM_HANDLE(pci, root, "\\_SB.PCI");     /* 570 */
1788
1789 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1790         {
1791          .notify = dock_notify,
1792          .handle = &dock_handle,
1793          .type = ACPI_SYSTEM_NOTIFY,
1794         },
1795         {
1796          .hid = IBM_PCI_HID,
1797          .notify = dock_notify,
1798          .handle = &pci_handle,
1799          .type = ACPI_SYSTEM_NOTIFY,
1800         },
1801 };
1802
1803 static struct ibm_struct dock_driver_data[2] = {
1804         {
1805          .name = "dock",
1806          .read = dock_read,
1807          .write = dock_write,
1808          .acpi = &ibm_dock_acpidriver[0],
1809         },
1810         {
1811          .name = "dock",
1812          .acpi = &ibm_dock_acpidriver[1],
1813         },
1814 };
1815
1816 #define dock_docked() (_sta(dock_handle) & 1)
1817
1818 static int __init dock_init(struct ibm_init_struct *iibm)
1819 {
1820         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1821
1822         IBM_ACPIHANDLE_INIT(dock);
1823
1824         vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1825                 str_supported(dock_handle != NULL));
1826
1827         return (dock_handle)? 0 : 1;
1828 }
1829
1830 static int __init dock_init2(struct ibm_init_struct *iibm)
1831 {
1832         int dock2_needed;
1833
1834         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1835
1836         if (dock_driver_data[0].flags.acpi_driver_registered &&
1837             dock_driver_data[0].flags.acpi_notify_installed) {
1838                 IBM_ACPIHANDLE_INIT(pci);
1839                 dock2_needed = (pci_handle != NULL);
1840                 vdbg_printk(TPACPI_DBG_INIT,
1841                             "dock PCI handler for the TP 570 is %s\n",
1842                             str_supported(dock2_needed));
1843         } else {
1844                 vdbg_printk(TPACPI_DBG_INIT,
1845                 "dock subdriver part 2 not required\n");
1846                 dock2_needed = 0;
1847         }
1848
1849         return (dock2_needed)? 0 : 1;
1850 }
1851
1852 static void dock_notify(struct ibm_struct *ibm, u32 event)
1853 {
1854         int docked = dock_docked();
1855         int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1856
1857         if (event == 1 && !pci) /* 570 */
1858                 acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
1859         else if (event == 1 && pci)     /* 570 */
1860                 acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
1861         else if (event == 3 && docked)
1862                 acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
1863         else if (event == 3 && !docked)
1864                 acpi_bus_generate_event(ibm->acpi->device, event, 2);   /* undock */
1865         else if (event == 0 && docked)
1866                 acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
1867         else {
1868                 printk(IBM_ERR "unknown dock event %d, status %d\n",
1869                        event, _sta(dock_handle));
1870                 acpi_bus_generate_event(ibm->acpi->device, event, 0);   /* unknown */
1871         }
1872 }
1873
1874 static int dock_read(char *p)
1875 {
1876         int len = 0;
1877         int docked = dock_docked();
1878
1879         if (!dock_handle)
1880                 len += sprintf(p + len, "status:\t\tnot supported\n");
1881         else if (!docked)
1882                 len += sprintf(p + len, "status:\t\tundocked\n");
1883         else {
1884                 len += sprintf(p + len, "status:\t\tdocked\n");
1885                 len += sprintf(p + len, "commands:\tdock, undock\n");
1886         }
1887
1888         return len;
1889 }
1890
1891 static int dock_write(char *buf)
1892 {
1893         char *cmd;
1894
1895         if (!dock_docked())
1896                 return -ENODEV;
1897
1898         while ((cmd = next_cmd(&buf))) {
1899                 if (strlencmp(cmd, "undock") == 0) {
1900                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1901                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1902                                 return -EIO;
1903                 } else if (strlencmp(cmd, "dock") == 0) {
1904                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1905                                 return -EIO;
1906                 } else
1907                         return -EINVAL;
1908         }
1909
1910         return 0;
1911 }
1912
1913 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1914
1915 /*************************************************************************
1916  * Bay subdriver
1917  */
1918
1919 #ifdef CONFIG_THINKPAD_ACPI_BAY
1920 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",        /* 570 */
1921            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1922            "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1923            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1924            );                           /* A21e, R30, R31 */
1925 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1926            "_EJ0",              /* all others */
1927            );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1928 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",     /* A3x, R32 */
1929            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1930            );                           /* all others */
1931 IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
1932            "_EJ0",                      /* 770x */
1933            );                           /* all others */
1934
1935 static int __init bay_init(struct ibm_init_struct *iibm)
1936 {
1937         vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1938
1939         IBM_ACPIHANDLE_INIT(bay);
1940         if (bay_handle)
1941                 IBM_ACPIHANDLE_INIT(bay_ej);
1942         IBM_ACPIHANDLE_INIT(bay2);
1943         if (bay2_handle)
1944                 IBM_ACPIHANDLE_INIT(bay2_ej);
1945
1946         tp_features.bay_status = bay_handle &&
1947                 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1948         tp_features.bay_status2 = bay2_handle &&
1949                 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1950
1951         tp_features.bay_eject = bay_handle && bay_ej_handle &&
1952                 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1953         tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1954                 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1955
1956         vdbg_printk(TPACPI_DBG_INIT,
1957                 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1958                 str_supported(tp_features.bay_status),
1959                 str_supported(tp_features.bay_eject),
1960                 str_supported(tp_features.bay_status2),
1961                 str_supported(tp_features.bay_eject2));
1962
1963         return (tp_features.bay_status || tp_features.bay_eject ||
1964                 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1965 }
1966
1967 static void bay_notify(struct ibm_struct *ibm, u32 event)
1968 {
1969         acpi_bus_generate_event(ibm->acpi->device, event, 0);
1970 }
1971
1972 #define bay_occupied(b) (_sta(b##_handle) & 1)
1973
1974 static int bay_read(char *p)
1975 {
1976         int len = 0;
1977         int occupied = bay_occupied(bay);
1978         int occupied2 = bay_occupied(bay2);
1979         int eject, eject2;
1980
1981         len += sprintf(p + len, "status:\t\t%s\n",
1982                 tp_features.bay_status ?
1983                         (occupied ? "occupied" : "unoccupied") :
1984                                 "not supported");
1985         if (tp_features.bay_status2)
1986                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1987                                "occupied" : "unoccupied");
1988
1989         eject = tp_features.bay_eject && occupied;
1990         eject2 = tp_features.bay_eject2 && occupied2;
1991
1992         if (eject && eject2)
1993                 len += sprintf(p + len, "commands:\teject, eject2\n");
1994         else if (eject)
1995                 len += sprintf(p + len, "commands:\teject\n");
1996         else if (eject2)
1997                 len += sprintf(p + len, "commands:\teject2\n");
1998
1999         return len;
2000 }
2001
2002 static int bay_write(char *buf)
2003 {
2004         char *cmd;
2005
2006         if (!tp_features.bay_eject && !tp_features.bay_eject2)
2007                 return -ENODEV;
2008
2009         while ((cmd = next_cmd(&buf))) {
2010                 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2011                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2012                                 return -EIO;
2013                 } else if (tp_features.bay_eject2 &&
2014                            strlencmp(cmd, "eject2") == 0) {
2015                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2016                                 return -EIO;
2017                 } else
2018                         return -EINVAL;
2019         }
2020
2021         return 0;
2022 }
2023
2024 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2025         .notify = bay_notify,
2026         .handle = &bay_handle,
2027         .type = ACPI_SYSTEM_NOTIFY,
2028 };
2029
2030 static struct ibm_struct bay_driver_data = {
2031         .name = "bay",
2032         .read = bay_read,
2033         .write = bay_write,
2034         .acpi = &ibm_bay_acpidriver,
2035 };
2036
2037 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2038
2039 /*************************************************************************
2040  * CMOS subdriver
2041  */
2042
2043 /* sysfs cmos_command -------------------------------------------------- */
2044 static ssize_t cmos_command_store(struct device *dev,
2045                             struct device_attribute *attr,
2046                             const char *buf, size_t count)
2047 {
2048         unsigned long cmos_cmd;
2049         int res;
2050
2051         if (parse_strtoul(buf, 21, &cmos_cmd))
2052                 return -EINVAL;
2053
2054         res = issue_thinkpad_cmos_command(cmos_cmd);
2055         return (res)? res : count;
2056 }
2057
2058 static struct device_attribute dev_attr_cmos_command =
2059         __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2060
2061 /* --------------------------------------------------------------------- */
2062
2063 static int __init cmos_init(struct ibm_init_struct *iibm)
2064 {
2065         int res;
2066
2067         vdbg_printk(TPACPI_DBG_INIT,
2068                 "initializing cmos commands subdriver\n");
2069
2070         IBM_ACPIHANDLE_INIT(cmos);
2071
2072         vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2073                 str_supported(cmos_handle != NULL));
2074
2075         res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2076         if (res)
2077                 return res;
2078
2079         return (cmos_handle)? 0 : 1;
2080 }
2081
2082 static void cmos_exit(void)
2083 {
2084         device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2085 }
2086
2087 static int cmos_read(char *p)
2088 {
2089         int len = 0;
2090
2091         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2092            R30, R31, T20-22, X20-21 */
2093         if (!cmos_handle)
2094                 len += sprintf(p + len, "status:\t\tnot supported\n");
2095         else {
2096                 len += sprintf(p + len, "status:\t\tsupported\n");
2097                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2098         }
2099
2100         return len;
2101 }
2102
2103 static int cmos_write(char *buf)
2104 {
2105         char *cmd;
2106         int cmos_cmd, res;
2107
2108         while ((cmd = next_cmd(&buf))) {
2109                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2110                     cmos_cmd >= 0 && cmos_cmd <= 21) {
2111                         /* cmos_cmd set */
2112                 } else
2113                         return -EINVAL;
2114
2115                 res = issue_thinkpad_cmos_command(cmos_cmd);
2116                 if (res)
2117                         return res;
2118         }
2119
2120         return 0;
2121 }
2122
2123 static struct ibm_struct cmos_driver_data = {
2124         .name = "cmos",
2125         .read = cmos_read,
2126         .write = cmos_write,
2127         .exit = cmos_exit,
2128 };
2129
2130 /*************************************************************************
2131  * LED subdriver
2132  */
2133
2134 static enum led_access_mode led_supported;
2135
2136 IBM_HANDLE(led, ec, "SLED",     /* 570 */
2137            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2138            "LED",               /* all others */
2139            );                   /* R30, R31 */
2140
2141 static int __init led_init(struct ibm_init_struct *iibm)
2142 {
2143         vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2144
2145         IBM_ACPIHANDLE_INIT(led);
2146
2147         if (!led_handle)
2148                 /* led not supported on R30, R31 */
2149                 led_supported = TPACPI_LED_NONE;
2150         else if (strlencmp(led_path, "SLED") == 0)
2151                 /* 570 */
2152                 led_supported = TPACPI_LED_570;
2153         else if (strlencmp(led_path, "SYSL") == 0)
2154                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2155                 led_supported = TPACPI_LED_OLD;
2156         else
2157                 /* all others */
2158                 led_supported = TPACPI_LED_NEW;
2159
2160         vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2161                 str_supported(led_supported), led_supported);
2162
2163         return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2164 }
2165
2166 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2167
2168 static int led_read(char *p)
2169 {
2170         int len = 0;
2171
2172         if (!led_supported) {
2173                 len += sprintf(p + len, "status:\t\tnot supported\n");
2174                 return len;
2175         }
2176         len += sprintf(p + len, "status:\t\tsupported\n");
2177
2178         if (led_supported == TPACPI_LED_570) {
2179                 /* 570 */
2180                 int i, status;
2181                 for (i = 0; i < 8; i++) {
2182                         if (!acpi_evalf(ec_handle,
2183                                         &status, "GLED", "dd", 1 << i))
2184                                 return -EIO;
2185                         len += sprintf(p + len, "%d:\t\t%s\n",
2186                                        i, led_status(status));
2187                 }
2188         }
2189
2190         len += sprintf(p + len, "commands:\t"
2191                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2192
2193         return len;
2194 }
2195
2196 /* off, on, blink */
2197 static const int led_sled_arg1[] = { 0, 1, 3 };
2198 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
2199 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
2200 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2201
2202 static int led_write(char *buf)
2203 {
2204         char *cmd;
2205         int led, ind, ret;
2206
2207         if (!led_supported)
2208                 return -ENODEV;
2209
2210         while ((cmd = next_cmd(&buf))) {
2211                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2212                         return -EINVAL;
2213
2214                 if (strstr(cmd, "off")) {
2215                         ind = 0;
2216                 } else if (strstr(cmd, "on")) {
2217                         ind = 1;
2218                 } else if (strstr(cmd, "blink")) {
2219                         ind = 2;
2220                 } else
2221                         return -EINVAL;
2222
2223                 if (led_supported == TPACPI_LED_570) {
2224                         /* 570 */
2225                         led = 1 << led;
2226                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2227                                         led, led_sled_arg1[ind]))
2228                                 return -EIO;
2229                 } else if (led_supported == TPACPI_LED_OLD) {
2230                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2231                         led = 1 << led;
2232                         ret = ec_write(TPACPI_LED_EC_HLMS, led);
2233                         if (ret >= 0)
2234                                 ret =
2235                                     ec_write(TPACPI_LED_EC_HLBL,
2236                                              led * led_exp_hlbl[ind]);
2237                         if (ret >= 0)
2238                                 ret =
2239                                     ec_write(TPACPI_LED_EC_HLCL,
2240                                              led * led_exp_hlcl[ind]);
2241                         if (ret < 0)
2242                                 return ret;
2243                 } else {
2244                         /* all others */
2245                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2246                                         led, led_led_arg1[ind]))
2247                                 return -EIO;
2248                 }
2249         }
2250
2251         return 0;
2252 }
2253
2254 static struct ibm_struct led_driver_data = {
2255         .name = "led",
2256         .read = led_read,
2257         .write = led_write,
2258 };
2259
2260 /*************************************************************************
2261  * Beep subdriver
2262  */
2263
2264 IBM_HANDLE(beep, ec, "BEEP");   /* all except R30, R31 */
2265
2266 static int __init beep_init(struct ibm_init_struct *iibm)
2267 {
2268         vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2269
2270         IBM_ACPIHANDLE_INIT(beep);
2271
2272         vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2273                 str_supported(beep_handle != NULL));
2274
2275         return (beep_handle)? 0 : 1;
2276 }
2277
2278 static int beep_read(char *p)
2279 {
2280         int len = 0;
2281
2282         if (!beep_handle)
2283                 len += sprintf(p + len, "status:\t\tnot supported\n");
2284         else {
2285                 len += sprintf(p + len, "status:\t\tsupported\n");
2286                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2287         }
2288
2289         return len;
2290 }
2291
2292 static int beep_write(char *buf)
2293 {
2294         char *cmd;
2295         int beep_cmd;
2296
2297         if (!beep_handle)
2298                 return -ENODEV;
2299
2300         while ((cmd = next_cmd(&buf))) {
2301                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2302                     beep_cmd >= 0 && beep_cmd <= 17) {
2303                         /* beep_cmd set */
2304                 } else
2305                         return -EINVAL;
2306                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2307                         return -EIO;
2308         }
2309
2310         return 0;
2311 }
2312
2313 static struct ibm_struct beep_driver_data = {
2314         .name = "beep",
2315         .read = beep_read,
2316         .write = beep_write,
2317 };
2318
2319 /*************************************************************************
2320  * Thermal subdriver
2321  */
2322
2323 static enum thermal_access_mode thermal_read_mode;
2324
2325 /* sysfs temp##_input -------------------------------------------------- */
2326
2327 static ssize_t thermal_temp_input_show(struct device *dev,
2328                            struct device_attribute *attr,
2329                            char *buf)
2330 {
2331         struct sensor_device_attribute *sensor_attr =
2332                                         to_sensor_dev_attr(attr);
2333         int idx = sensor_attr->index;
2334         s32 value;
2335         int res;
2336
2337         res = thermal_get_sensor(idx, &value);
2338         if (res)
2339                 return res;
2340         if (value == TP_EC_THERMAL_TMP_NA * 1000)
2341                 return -ENXIO;
2342
2343         return snprintf(buf, PAGE_SIZE, "%d\n", value);
2344 }
2345
2346 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2347          SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2348
2349 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2350         THERMAL_SENSOR_ATTR_TEMP(1, 0),
2351         THERMAL_SENSOR_ATTR_TEMP(2, 1),
2352         THERMAL_SENSOR_ATTR_TEMP(3, 2),
2353         THERMAL_SENSOR_ATTR_TEMP(4, 3),
2354         THERMAL_SENSOR_ATTR_TEMP(5, 4),
2355         THERMAL_SENSOR_ATTR_TEMP(6, 5),
2356         THERMAL_SENSOR_ATTR_TEMP(7, 6),
2357         THERMAL_SENSOR_ATTR_TEMP(8, 7),
2358         THERMAL_SENSOR_ATTR_TEMP(9, 8),
2359         THERMAL_SENSOR_ATTR_TEMP(10, 9),
2360         THERMAL_SENSOR_ATTR_TEMP(11, 10),
2361         THERMAL_SENSOR_ATTR_TEMP(12, 11),
2362         THERMAL_SENSOR_ATTR_TEMP(13, 12),
2363         THERMAL_SENSOR_ATTR_TEMP(14, 13),
2364         THERMAL_SENSOR_ATTR_TEMP(15, 14),
2365         THERMAL_SENSOR_ATTR_TEMP(16, 15),
2366 };
2367
2368 #define THERMAL_ATTRS(X) \
2369         &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2370
2371 static struct attribute *thermal_temp_input_attr[] = {
2372         THERMAL_ATTRS(8),
2373         THERMAL_ATTRS(9),
2374         THERMAL_ATTRS(10),
2375         THERMAL_ATTRS(11),
2376         THERMAL_ATTRS(12),
2377         THERMAL_ATTRS(13),
2378         THERMAL_ATTRS(14),
2379         THERMAL_ATTRS(15),
2380         THERMAL_ATTRS(0),
2381         THERMAL_ATTRS(1),
2382         THERMAL_ATTRS(2),
2383         THERMAL_ATTRS(3),
2384         THERMAL_ATTRS(4),
2385         THERMAL_ATTRS(5),
2386         THERMAL_ATTRS(6),
2387         THERMAL_ATTRS(7),
2388         NULL
2389 };
2390
2391 static const struct attribute_group thermal_temp_input16_group = {
2392         .attrs = thermal_temp_input_attr
2393 };
2394
2395 static const struct attribute_group thermal_temp_input8_group = {
2396         .attrs = &thermal_temp_input_attr[8]
2397 };
2398
2399 #undef THERMAL_SENSOR_ATTR_TEMP
2400 #undef THERMAL_ATTRS
2401
2402 /* --------------------------------------------------------------------- */
2403
2404 static int __init thermal_init(struct ibm_init_struct *iibm)
2405 {
2406         u8 t, ta1, ta2;
2407         int i;
2408         int acpi_tmp7;
2409         int res;
2410
2411         vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2412
2413         acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2414
2415         if (ibm_thinkpad_ec_found && experimental) {
2416                 /*
2417                  * Direct EC access mode: sensors at registers
2418                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
2419                  * non-implemented, thermal sensors return 0x80 when
2420                  * not available
2421                  */
2422
2423                 ta1 = ta2 = 0;
2424                 for (i = 0; i < 8; i++) {
2425                         if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2426                                 ta1 |= t;
2427                         } else {
2428                                 ta1 = 0;
2429                                 break;
2430                         }
2431                         if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2432                                 ta2 |= t;
2433                         } else {
2434                                 ta1 = 0;
2435                                 break;
2436                         }
2437                 }
2438                 if (ta1 == 0) {
2439                         /* This is sheer paranoia, but we handle it anyway */
2440                         if (acpi_tmp7) {
2441                                 printk(IBM_ERR
2442                                        "ThinkPad ACPI EC access misbehaving, "
2443                                        "falling back to ACPI TMPx access mode\n");
2444                                 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2445                         } else {
2446                                 printk(IBM_ERR
2447                                        "ThinkPad ACPI EC access misbehaving, "
2448                                        "disabling thermal sensors access\n");
2449                                 thermal_read_mode = TPACPI_THERMAL_NONE;
2450                         }
2451                 } else {
2452                         thermal_read_mode =
2453                             (ta2 != 0) ?
2454                             TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2455                 }
2456         } else if (acpi_tmp7) {
2457                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2458                         /* 600e/x, 770e, 770x */
2459                         thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2460                 } else {
2461                         /* Standard ACPI TMPx access, max 8 sensors */
2462                         thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2463                 }
2464         } else {
2465                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2466                 thermal_read_mode = TPACPI_THERMAL_NONE;
2467         }
2468
2469         vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2470                 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2471                 thermal_read_mode);
2472
2473         switch(thermal_read_mode) {
2474         case TPACPI_THERMAL_TPEC_16:
2475                 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2476                                 &thermal_temp_input16_group);
2477                 if (res)
2478                         return res;
2479                 break;
2480         case TPACPI_THERMAL_TPEC_8:
2481         case TPACPI_THERMAL_ACPI_TMP07:
2482         case TPACPI_THERMAL_ACPI_UPDT:
2483                 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2484                                 &thermal_temp_input8_group);
2485                 if (res)
2486                         return res;
2487                 break;
2488         case TPACPI_THERMAL_NONE:
2489         default:
2490                 return 1;
2491         }
2492
2493         return 0;
2494 }
2495
2496 static void thermal_exit(void)
2497 {
2498         switch(thermal_read_mode) {
2499         case TPACPI_THERMAL_TPEC_16:
2500                 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2501                                    &thermal_temp_input16_group);
2502                 break;
2503         case TPACPI_THERMAL_TPEC_8:
2504         case TPACPI_THERMAL_ACPI_TMP07:
2505         case TPACPI_THERMAL_ACPI_UPDT:
2506                 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2507                                    &thermal_temp_input16_group);
2508                 break;
2509         case TPACPI_THERMAL_NONE:
2510         default:
2511                 break;
2512         }
2513 }
2514
2515 /* idx is zero-based */
2516 static int thermal_get_sensor(int idx, s32 *value)
2517 {
2518         int t;
2519         s8 tmp;
2520         char tmpi[5];
2521
2522         t = TP_EC_THERMAL_TMP0;
2523
2524         switch (thermal_read_mode) {
2525 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2526         case TPACPI_THERMAL_TPEC_16:
2527                 if (idx >= 8 && idx <= 15) {
2528                         t = TP_EC_THERMAL_TMP8;
2529                         idx -= 8;
2530                 }
2531                 /* fallthrough */
2532 #endif
2533         case TPACPI_THERMAL_TPEC_8:
2534                 if (idx <= 7) {
2535                         if (!acpi_ec_read(t + idx, &tmp))
2536                                 return -EIO;
2537                         *value = tmp * 1000;
2538                         return 0;
2539                 }
2540                 break;
2541
2542         case TPACPI_THERMAL_ACPI_UPDT:
2543                 if (idx <= 7) {
2544                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2545                         if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2546                                 return -EIO;
2547                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2548                                 return -EIO;
2549                         *value = (t - 2732) * 100;
2550                         return 0;
2551                 }
2552                 break;
2553
2554         case TPACPI_THERMAL_ACPI_TMP07:
2555                 if (idx <= 7) {
2556                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2557                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2558                                 return -EIO;
2559                         *value = t * 1000;
2560                         return 0;
2561                 }
2562                 break;
2563
2564         case TPACPI_THERMAL_NONE:
2565         default:
2566                 return -ENOSYS;
2567         }
2568
2569         return -EINVAL;
2570 }
2571
2572 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2573 {
2574         int res, i;
2575         int n;
2576
2577         n = 8;
2578         i = 0;
2579
2580         if (!s)
2581                 return -EINVAL;
2582
2583         if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2584                 n = 16;
2585
2586         for(i = 0 ; i < n; i++) {
2587                 res = thermal_get_sensor(i, &s->temp[i]);
2588                 if (res)
2589                         return res;
2590         }
2591
2592         return n;
2593 }
2594
2595 static int thermal_read(char *p)
2596 {
2597         int len = 0;
2598         int n, i;
2599         struct ibm_thermal_sensors_struct t;
2600
2601         n = thermal_get_sensors(&t);
2602         if (unlikely(n < 0))
2603                 return n;
2604
2605         len += sprintf(p + len, "temperatures:\t");
2606
2607         if (n > 0) {
2608                 for (i = 0; i < (n - 1); i++)
2609                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2610                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2611         } else
2612                 len += sprintf(p + len, "not supported\n");
2613
2614         return len;
2615 }
2616
2617 static struct ibm_struct thermal_driver_data = {
2618         .name = "thermal",
2619         .read = thermal_read,
2620         .exit = thermal_exit,
2621 };
2622
2623 /*************************************************************************
2624  * EC Dump subdriver
2625  */
2626
2627 static u8 ecdump_regs[256];
2628
2629 static int ecdump_read(char *p)
2630 {
2631         int len = 0;
2632         int i, j;
2633         u8 v;
2634
2635         len += sprintf(p + len, "EC      "
2636                        " +00 +01 +02 +03 +04 +05 +06 +07"
2637                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2638         for (i = 0; i < 256; i += 16) {
2639                 len += sprintf(p + len, "EC 0x%02x:", i);
2640                 for (j = 0; j < 16; j++) {
2641                         if (!acpi_ec_read(i + j, &v))
2642                                 break;
2643                         if (v != ecdump_regs[i + j])
2644                                 len += sprintf(p + len, " *%02x", v);
2645                         else
2646                                 len += sprintf(p + len, "  %02x", v);
2647                         ecdump_regs[i + j] = v;
2648                 }
2649                 len += sprintf(p + len, "\n");
2650                 if (j != 16)
2651                         break;
2652         }
2653
2654         /* These are way too dangerous to advertise openly... */
2655 #if 0
2656         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2657                        " (<offset> is 00-ff, <value> is 00-ff)\n");
2658         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
2659                        " (<offset> is 00-ff, <value> is 0-255)\n");
2660 #endif
2661         return len;
2662 }
2663
2664 static int ecdump_write(char *buf)
2665 {
2666         char *cmd;
2667         int i, v;
2668
2669         while ((cmd = next_cmd(&buf))) {
2670                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2671                         /* i and v set */
2672                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2673                         /* i and v set */
2674                 } else
2675                         return -EINVAL;
2676                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2677                         if (!acpi_ec_write(i, v))
2678                                 return -EIO;
2679                 } else
2680                         return -EINVAL;
2681         }
2682
2683         return 0;
2684 }
2685
2686 static struct ibm_struct ecdump_driver_data = {
2687         .name = "ecdump",
2688         .read = ecdump_read,
2689         .write = ecdump_write,
2690         .flags.experimental = 1,
2691 };
2692
2693 /*************************************************************************
2694  * Backlight/brightness subdriver
2695  */
2696
2697 static struct backlight_device *ibm_backlight_device;
2698
2699 static struct backlight_ops ibm_backlight_data = {
2700         .get_brightness = brightness_get,
2701         .update_status  = brightness_update_status,
2702 };
2703
2704 static int __init brightness_init(struct ibm_init_struct *iibm)
2705 {
2706         int b;
2707
2708         vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2709
2710         b = brightness_get(NULL);
2711         if (b < 0)
2712                 return b;
2713
2714         ibm_backlight_device = backlight_device_register(
2715                                         TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2716                                         &ibm_backlight_data);
2717         if (IS_ERR(ibm_backlight_device)) {
2718                 printk(IBM_ERR "Could not register backlight device\n");
2719                 return PTR_ERR(ibm_backlight_device);
2720         }
2721         vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2722
2723         ibm_backlight_device->props.max_brightness = 7;
2724         ibm_backlight_device->props.brightness = b;
2725         backlight_update_status(ibm_backlight_device);
2726
2727         return 0;
2728 }
2729
2730 static void brightness_exit(void)
2731 {
2732         if (ibm_backlight_device) {
2733                 vdbg_printk(TPACPI_DBG_EXIT,
2734                             "calling backlight_device_unregister()\n");
2735                 backlight_device_unregister(ibm_backlight_device);
2736                 ibm_backlight_device = NULL;
2737         }
2738 }
2739
2740 static int brightness_update_status(struct backlight_device *bd)
2741 {
2742         return brightness_set(
2743                 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2744                  bd->props.power == FB_BLANK_UNBLANK) ?
2745                                 bd->props.brightness : 0);
2746 }
2747
2748 static int brightness_get(struct backlight_device *bd)
2749 {
2750         u8 level;
2751         if (!acpi_ec_read(brightness_offset, &level))
2752                 return -EIO;
2753
2754         level &= 0x7;
2755
2756         return level;
2757 }
2758
2759 static int brightness_set(int value)
2760 {
2761         int cmos_cmd, inc, i;
2762         int current_value = brightness_get(NULL);
2763
2764         value &= 7;
2765
2766         cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2767         inc = value > current_value ? 1 : -1;
2768         for (i = current_value; i != value; i += inc) {
2769                 if (issue_thinkpad_cmos_command(cmos_cmd))
2770                         return -EIO;
2771                 if (!acpi_ec_write(brightness_offset, i + inc))
2772                         return -EIO;
2773         }
2774
2775         return 0;
2776 }
2777
2778 static int brightness_read(char *p)
2779 {
2780         int len = 0;
2781         int level;
2782
2783         if ((level = brightness_get(NULL)) < 0) {
2784                 len += sprintf(p + len, "level:\t\tunreadable\n");
2785         } else {
2786                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2787                 len += sprintf(p + len, "commands:\tup, down\n");
2788                 len += sprintf(p + len, "commands:\tlevel <level>"
2789                                " (<level> is 0-7)\n");
2790         }
2791
2792         return len;
2793 }
2794
2795 static int brightness_write(char *buf)
2796 {
2797         int level;
2798         int new_level;
2799         char *cmd;
2800
2801         while ((cmd = next_cmd(&buf))) {
2802                 if ((level = brightness_get(NULL)) < 0)
2803                         return level;
2804                 level &= 7;
2805
2806                 if (strlencmp(cmd, "up") == 0) {
2807                         new_level = level == 7 ? 7 : level + 1;
2808                 } else if (strlencmp(cmd, "down") == 0) {
2809                         new_level = level == 0 ? 0 : level - 1;
2810                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2811                            new_level >= 0 && new_level <= 7) {
2812                         /* new_level set */
2813                 } else
2814                         return -EINVAL;
2815
2816                 brightness_set(new_level);
2817         }
2818
2819         return 0;
2820 }
2821
2822 static struct ibm_struct brightness_driver_data = {
2823         .name = "brightness",
2824         .read = brightness_read,
2825         .write = brightness_write,
2826         .exit = brightness_exit,
2827 };
2828
2829 /*************************************************************************
2830  * Volume subdriver
2831  */
2832
2833 static int volume_read(char *p)
2834 {
2835         int len = 0;
2836         u8 level;
2837
2838         if (!acpi_ec_read(volume_offset, &level)) {
2839                 len += sprintf(p + len, "level:\t\tunreadable\n");
2840         } else {
2841                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2842                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2843                 len += sprintf(p + len, "commands:\tup, down, mute\n");
2844                 len += sprintf(p + len, "commands:\tlevel <level>"
2845                                " (<level> is 0-15)\n");
2846         }
2847
2848         return len;
2849 }
2850
2851 static int volume_write(char *buf)
2852 {
2853         int cmos_cmd, inc, i;
2854         u8 level, mute;
2855         int new_level, new_mute;
2856         char *cmd;
2857
2858         while ((cmd = next_cmd(&buf))) {
2859                 if (!acpi_ec_read(volume_offset, &level))
2860                         return -EIO;
2861                 new_mute = mute = level & 0x40;
2862                 new_level = level = level & 0xf;
2863
2864                 if (strlencmp(cmd, "up") == 0) {
2865                         if (mute)
2866                                 new_mute = 0;
2867                         else
2868                                 new_level = level == 15 ? 15 : level + 1;
2869                 } else if (strlencmp(cmd, "down") == 0) {
2870                         if (mute)
2871                                 new_mute = 0;
2872                         else
2873                                 new_level = level == 0 ? 0 : level - 1;
2874                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2875                            new_level >= 0 && new_level <= 15) {
2876                         /* new_level set */
2877                 } else if (strlencmp(cmd, "mute") == 0) {
2878                         new_mute = 0x40;
2879                 } else
2880                         return -EINVAL;
2881
2882                 if (new_level != level) {       /* mute doesn't change */
2883                         cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2884                         inc = new_level > level ? 1 : -1;
2885
2886                         if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2887                                      !acpi_ec_write(volume_offset, level)))
2888                                 return -EIO;
2889
2890                         for (i = level; i != new_level; i += inc)
2891                                 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2892                                     !acpi_ec_write(volume_offset, i + inc))
2893                                         return -EIO;
2894
2895                         if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2896                                      !acpi_ec_write(volume_offset,
2897                                                     new_level + mute)))
2898                                 return -EIO;
2899                 }
2900
2901                 if (new_mute != mute) { /* level doesn't change */
2902                         cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2903
2904                         if (issue_thinkpad_cmos_command(cmos_cmd) ||
2905                             !acpi_ec_write(volume_offset, level + new_mute))
2906                                 return -EIO;
2907                 }
2908         }
2909
2910         return 0;
2911 }
2912
2913 static struct ibm_struct volume_driver_data = {
2914         .name = "volume",
2915         .read = volume_read,
2916         .write = volume_write,
2917 };
2918
2919 /*************************************************************************
2920  * Fan subdriver
2921  */
2922
2923 /*
2924  * FAN ACCESS MODES
2925  *
2926  * TPACPI_FAN_RD_ACPI_GFAN:
2927  *      ACPI GFAN method: returns fan level
2928  *
2929  *      see TPACPI_FAN_WR_ACPI_SFAN
2930  *      EC 0x2f (HFSP) not available if GFAN exists
2931  *
2932  * TPACPI_FAN_WR_ACPI_SFAN:
2933  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2934  *
2935  *      EC 0x2f (HFSP) might be available *for reading*, but do not use
2936  *      it for writing.
2937  *
2938  * TPACPI_FAN_WR_TPEC:
2939  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode
2940  *      Supported on almost all ThinkPads
2941  *
2942  *      Fan speed changes of any sort (including those caused by the
2943  *      disengaged mode) are usually done slowly by the firmware as the
2944  *      maximum ammount of fan duty cycle change per second seems to be
2945  *      limited.
2946  *
2947  *      Reading is not available if GFAN exists.
2948  *      Writing is not available if SFAN exists.
2949  *
2950  *      Bits
2951  *       7      automatic mode engaged;
2952  *              (default operation mode of the ThinkPad)
2953  *              fan level is ignored in this mode.
2954  *       6      full speed mode (takes precedence over bit 7);
2955  *              not available on all thinkpads.  May disable
2956  *              the tachometer while the fan controller ramps up
2957  *              the speed (which can take up to a few *minutes*).
2958  *              Speeds up fan to 100% duty-cycle, which is far above
2959  *              the standard RPM levels.  It is not impossible that
2960  *              it could cause hardware damage.
2961  *      5-3     unused in some models.  Extra bits for fan level
2962  *              in others, but still useless as all values above
2963  *              7 map to the same speed as level 7 in these models.
2964  *      2-0     fan level (0..7 usually)
2965  *                      0x00 = stop
2966  *                      0x07 = max (set when temperatures critical)
2967  *              Some ThinkPads may have other levels, see
2968  *              TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2969  *
2970  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2971  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2972  *      does so, its initial value is meaningless (0x07).
2973  *
2974  *      For firmware bugs, refer to:
2975  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2976  *
2977  *      ----
2978  *
2979  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2980  *      Main fan tachometer reading (in RPM)
2981  *
2982  *      This register is present on all ThinkPads with a new-style EC, and
2983  *      it is known not to be present on the A21m/e, and T22, as there is
2984  *      something else in offset 0x84 according to the ACPI DSDT.  Other
2985  *      ThinkPads from this same time period (and earlier) probably lack the
2986  *      tachometer as well.
2987  *
2988  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2989  *      was never fixed by IBM to report the EC firmware version string
2990  *      probably support the tachometer (like the early X models), so
2991  *      detecting it is quite hard.  We need more data to know for sure.
2992  *
2993  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2994  *      might result.
2995  *
2996  *      FIRMWARE BUG: may go stale while the EC is switching to full speed
2997  *      mode.
2998  *
2999  *      For firmware bugs, refer to:
3000  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3001  *
3002  * TPACPI_FAN_WR_ACPI_FANS:
3003  *      ThinkPad X31, X40, X41.  Not available in the X60.
3004  *
3005  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
3006  *      high speed.  ACPI DSDT seems to map these three speeds to levels
3007  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3008  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3009  *
3010  *      The speeds are stored on handles
3011  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3012  *
3013  *      There are three default speed sets, acessible as handles:
3014  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3015  *
3016  *      ACPI DSDT switches which set is in use depending on various
3017  *      factors.
3018  *
3019  *      TPACPI_FAN_WR_TPEC is also available and should be used to
3020  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
3021  *      but the ACPI tables just mention level 7.
3022  */
3023
3024 static enum fan_status_access_mode fan_status_access_mode;
3025 static enum fan_control_access_mode fan_control_access_mode;
3026 static enum fan_control_commands fan_control_commands;
3027
3028 static u8 fan_control_initial_status;
3029 static u8 fan_control_desired_level;
3030
3031 static void fan_watchdog_fire(struct work_struct *ignored);
3032 static int fan_watchdog_maxinterval;
3033 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3034
3035 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40, X41 */
3036 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
3037            "\\FSPD",            /* 600e/x, 770e, 770x */
3038            );                   /* all others */
3039 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
3040            "JFNS",              /* 770x-JL */
3041            );                   /* all others */
3042
3043 /*
3044  * SYSFS fan layout: hwmon compatible (device)
3045  *
3046  * pwm*_enable:
3047  *      0: "disengaged" mode
3048  *      1: manual mode
3049  *      2: native EC "auto" mode (recommended, hardware default)
3050  *
3051  * pwm*: set speed in manual mode, ignored otherwise.
3052  *      0 is level 0; 255 is level 7. Intermediate points done with linear
3053  *      interpolation.
3054  *
3055  * fan*_input: tachometer reading, RPM
3056  *
3057  *
3058  * SYSFS fan layout: extensions
3059  *
3060  * fan_watchdog (driver):
3061  *      fan watchdog interval in seconds, 0 disables (default), max 120
3062  */
3063
3064 /* sysfs fan pwm1_enable ----------------------------------------------- */
3065 static ssize_t fan_pwm1_enable_show(struct device *dev,
3066                                     struct device_attribute *attr,
3067                                     char *buf)
3068 {
3069         int res, mode;
3070         u8 status;
3071
3072         res = fan_get_status_safe(&status);
3073         if (res)
3074                 return res;
3075
3076         if (unlikely(tp_features.fan_ctrl_status_undef)) {
3077                 if (status != fan_control_initial_status) {
3078                         tp_features.fan_ctrl_status_undef = 0;
3079                 } else {
3080                         /* Return most likely status. In fact, it
3081                          * might be the only possible status */
3082                         status = TP_EC_FAN_AUTO;
3083                 }
3084         }
3085
3086         if (status & TP_EC_FAN_FULLSPEED) {
3087                 mode = 0;
3088         } else if (status & TP_EC_FAN_AUTO) {
3089                 mode = 2;
3090         } else
3091                 mode = 1;
3092
3093         return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3094 }
3095
3096 static ssize_t fan_pwm1_enable_store(struct device *dev,
3097                                      struct device_attribute *attr,
3098                                      const char *buf, size_t count)
3099 {
3100         unsigned long t;
3101         int res, level;
3102
3103         if (parse_strtoul(buf, 2, &t))
3104                 return -EINVAL;
3105
3106         switch (t) {
3107         case 0:
3108                 level = TP_EC_FAN_FULLSPEED;
3109                 break;
3110         case 1:
3111                 level = TPACPI_FAN_LAST_LEVEL;
3112                 break;
3113         case 2:
3114                 level = TP_EC_FAN_AUTO;
3115                 break;
3116         case 3:
3117                 /* reserved for software-controlled auto mode */
3118                 return -ENOSYS;
3119         default:
3120                 return -EINVAL;
3121         }
3122
3123         res = fan_set_level_safe(level);
3124         if (res == -ENXIO)
3125                 return -EINVAL;
3126         else if (res < 0)
3127                 return res;
3128
3129         fan_watchdog_reset();
3130
3131         return count;
3132 }
3133
3134 static struct device_attribute dev_attr_fan_pwm1_enable =
3135         __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3136                 fan_pwm1_enable_show, fan_pwm1_enable_store);
3137
3138 /* sysfs fan pwm1 ------------------------------------------------------ */
3139 static ssize_t fan_pwm1_show(struct device *dev,
3140                              struct device_attribute *attr,
3141                              char *buf)
3142 {
3143         int res;
3144         u8 status;
3145
3146         res = fan_get_status_safe(&status);
3147         if (res)
3148                 return res;
3149
3150         if (unlikely(tp_features.fan_ctrl_status_undef)) {
3151                 if (status != fan_control_initial_status) {
3152                         tp_features.fan_ctrl_status_undef = 0;
3153                 } else {
3154                         status = TP_EC_FAN_AUTO;
3155                 }
3156         }
3157
3158         if ((status &
3159              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3160                 status = fan_control_desired_level;
3161
3162         if (status > 7)
3163                 status = 7;
3164
3165         return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3166 }
3167
3168 static ssize_t fan_pwm1_store(struct device *dev,
3169                               struct device_attribute *attr,
3170                               const char *buf, size_t count)
3171 {
3172         unsigned long s;
3173         int rc;
3174         u8 status, newlevel;
3175
3176         if (parse_strtoul(buf, 255, &s))
3177                 return -EINVAL;
3178
3179         /* scale down from 0-255 to 0-7 */
3180         newlevel = (s >> 5) & 0x07;
3181
3182         rc = mutex_lock_interruptible(&fan_mutex);
3183         if (rc < 0)
3184                 return rc;
3185
3186         rc = fan_get_status(&status);
3187         if (!rc && (status &
3188                     (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3189                 rc = fan_set_level(newlevel);
3190                 if (rc == -ENXIO)
3191                         rc = -EINVAL;
3192                 else if (!rc) {
3193                         fan_update_desired_level(newlevel);
3194                         fan_watchdog_reset();
3195                 }
3196         }
3197
3198         mutex_unlock(&fan_mutex);
3199         return (rc)? rc : count;
3200 }
3201
3202 static struct device_attribute dev_attr_fan_pwm1 =
3203         __ATTR(pwm1, S_IWUSR | S_IRUGO,
3204                 fan_pwm1_show, fan_pwm1_store);
3205
3206 /* sysfs fan fan1_input ------------------------------------------------ */
3207 static ssize_t fan_fan1_input_show(struct device *dev,
3208                            struct device_attribute *attr,
3209                            char *buf)
3210 {
3211         int res;
3212         unsigned int speed;
3213
3214         res = fan_get_speed(&speed);
3215         if (res < 0)
3216                 return res;
3217
3218         return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3219 }
3220
3221 static struct device_attribute dev_attr_fan_fan1_input =
3222         __ATTR(fan1_input, S_IRUGO,
3223                 fan_fan1_input_show, NULL);
3224
3225 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3226 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3227                                      char *buf)
3228 {
3229         return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3230 }
3231
3232 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3233                                       const char *buf, size_t count)
3234 {
3235         unsigned long t;
3236
3237         if (parse_strtoul(buf, 120, &t))
3238                 return -EINVAL;
3239
3240         if (!fan_control_allowed)
3241                 return -EPERM;
3242
3243         fan_watchdog_maxinterval = t;
3244         fan_watchdog_reset();
3245
3246         return count;
3247 }
3248
3249 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3250                 fan_fan_watchdog_show, fan_fan_watchdog_store);
3251
3252 /* --------------------------------------------------------------------- */
3253 static struct attribute *fan_attributes[] = {
3254         &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3255         &dev_attr_fan_fan1_input.attr,
3256         NULL
3257 };
3258
3259 static const struct attribute_group fan_attr_group = {
3260         .attrs = fan_attributes,
3261 };
3262
3263 static int __init fan_init(struct ibm_init_struct *iibm)
3264 {
3265         int rc;
3266
3267         vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3268
3269         mutex_init(&fan_mutex);
3270         fan_status_access_mode = TPACPI_FAN_NONE;
3271         fan_control_access_mode = TPACPI_FAN_WR_NONE;
3272         fan_control_commands = 0;
3273         fan_watchdog_maxinterval = 0;
3274         tp_features.fan_ctrl_status_undef = 0;
3275         fan_control_desired_level = 7;
3276
3277         IBM_ACPIHANDLE_INIT(fans);
3278         IBM_ACPIHANDLE_INIT(gfan);
3279         IBM_ACPIHANDLE_INIT(sfan);
3280
3281         if (gfan_handle) {
3282                 /* 570, 600e/x, 770e, 770x */
3283                 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3284         } else {
3285                 /* all other ThinkPads: note that even old-style
3286                  * ThinkPad ECs supports the fan control register */
3287                 if (likely(acpi_ec_read(fan_status_offset,
3288                                         &fan_control_initial_status))) {
3289                         fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3290
3291                         /* In some ThinkPads, neither the EC nor the ACPI
3292                          * DSDT initialize the fan status, and it ends up
3293                          * being set to 0x07 when it *could* be either
3294                          * 0x07 or 0x80.
3295                          *
3296                          * Enable for TP-1Y (T43), TP-78 (R51e),
3297                          * TP-76 (R52), TP-70 (T43, R52), which are known
3298                          * to be buggy. */
3299                         if (fan_control_initial_status == 0x07 &&
3300                             ibm_thinkpad_ec_found &&
3301                             ((ibm_thinkpad_ec_found[0] == '1' &&
3302                               ibm_thinkpad_ec_found[1] == 'Y') ||
3303                              (ibm_thinkpad_ec_found[0] == '7' &&
3304                               (ibm_thinkpad_ec_found[1] == '6' ||
3305                                ibm_thinkpad_ec_found[1] == '8' ||
3306                                ibm_thinkpad_ec_found[1] == '0'))
3307                             )) {
3308                                 printk(IBM_NOTICE
3309                                        "fan_init: initial fan status is "
3310                                        "unknown, assuming it is in auto "
3311                                        "mode\n");
3312                                 tp_features.fan_ctrl_status_undef = 1;
3313                         }
3314                 } else {
3315                         printk(IBM_ERR
3316                                "ThinkPad ACPI EC access misbehaving, "
3317                                "fan status and control unavailable\n");
3318                         return 1;
3319                 }
3320         }
3321
3322         if (sfan_handle) {
3323                 /* 570, 770x-JL */
3324                 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3325                 fan_control_commands |=
3326                     TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3327         } else {
3328                 if (!gfan_handle) {
3329                         /* gfan without sfan means no fan control */
3330                         /* all other models implement TP EC 0x2f control */
3331
3332                         if (fans_handle) {
3333                                 /* X31, X40, X41 */
3334                                 fan_control_access_mode =
3335                                     TPACPI_FAN_WR_ACPI_FANS;
3336                                 fan_control_commands |=
3337                                     TPACPI_FAN_CMD_SPEED |
3338                                     TPACPI_FAN_CMD_LEVEL |
3339                                     TPACPI_FAN_CMD_ENABLE;
3340                         } else {
3341                                 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3342                                 fan_control_commands |=
3343                                     TPACPI_FAN_CMD_LEVEL |
3344                                     TPACPI_FAN_CMD_ENABLE;
3345                         }
3346                 }
3347         }
3348
3349         vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3350                 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3351                   fan_control_access_mode != TPACPI_FAN_WR_NONE),
3352                 fan_status_access_mode, fan_control_access_mode);
3353
3354         /* fan control master switch */
3355         if (!fan_control_allowed) {
3356                 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3357                 fan_control_commands = 0;
3358                 dbg_printk(TPACPI_DBG_INIT,
3359                            "fan control features disabled by parameter\n");
3360         }
3361
3362         /* update fan_control_desired_level */
3363         if (fan_status_access_mode != TPACPI_FAN_NONE)
3364                 fan_get_status_safe(NULL);
3365
3366         if (fan_status_access_mode != TPACPI_FAN_NONE ||
3367             fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3368                 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3369                                          &fan_attr_group);
3370                 if (!(rc < 0))
3371                         rc = driver_create_file(&tpacpi_pdriver.driver,
3372                                         &driver_attr_fan_watchdog);
3373                 if (rc < 0)
3374                         return rc;
3375                 return 0;
3376         } else
3377                 return 1;
3378 }
3379
3380 /*
3381  * Call with fan_mutex held
3382  */
3383 static void fan_update_desired_level(u8 status)
3384 {
3385         if ((status &
3386              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3387                 if (status > 7)
3388                         fan_control_desired_level = 7;
3389                 else
3390                         fan_control_desired_level = status;
3391         }
3392 }
3393
3394 static int fan_get_status(u8 *status)
3395 {
3396         u8 s;
3397
3398         /* TODO:
3399          * Add TPACPI_FAN_RD_ACPI_FANS ? */
3400
3401         switch (fan_status_access_mode) {
3402         case TPACPI_FAN_RD_ACPI_GFAN:
3403                 /* 570, 600e/x, 770e, 770x */
3404
3405                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3406                         return -EIO;
3407
3408                 if (likely(status))
3409                         *status = s & 0x07;
3410
3411                 break;
3412
3413         case TPACPI_FAN_RD_TPEC:
3414                 /* all except 570, 600e/x, 770e, 770x */
3415                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3416                         return -EIO;
3417
3418                 if (likely(status))
3419                         *status = s;
3420
3421                 break;
3422
3423         default:
3424                 return -ENXIO;
3425         }
3426
3427         return 0;
3428 }
3429
3430 static int fan_get_status_safe(u8 *status)
3431 {
3432         int rc;
3433         u8 s;
3434
3435         rc = mutex_lock_interruptible(&fan_mutex);
3436         if (rc < 0)
3437                 return rc;
3438         rc = fan_get_status(&s);
3439         if (!rc)
3440                 fan_update_desired_level(s);
3441         mutex_unlock(&fan_mutex);
3442
3443         if (status)
3444                 *status = s;
3445
3446         return rc;
3447 }
3448
3449 static void fan_exit(void)
3450 {
3451         vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3452
3453         /* FIXME: can we really do this unconditionally? */
3454         sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3455         driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3456
3457         cancel_delayed_work(&fan_watchdog_task);
3458         flush_scheduled_work();
3459 }
3460
3461 static int fan_get_speed(unsigned int *speed)
3462 {
3463         u8 hi, lo;
3464
3465         switch (fan_status_access_mode) {
3466         case TPACPI_FAN_RD_TPEC:
3467                 /* all except 570, 600e/x, 770e, 770x */
3468                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3469                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3470                         return -EIO;
3471
3472                 if (likely(speed))
3473                         *speed = (hi << 8) | lo;
3474
3475                 break;
3476
3477         default:
3478                 return -ENXIO;
3479         }
3480
3481         return 0;
3482 }
3483
3484 static void fan_watchdog_fire(struct work_struct *ignored)
3485 {
3486         int rc;
3487
3488         printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3489         rc = fan_set_enable();
3490         if (rc < 0) {
3491                 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3492                         "will try again later...\n", -rc);
3493                 /* reschedule for later */
3494                 fan_watchdog_reset();
3495         }
3496 }
3497
3498 static void fan_watchdog_reset(void)
3499 {
3500         static int fan_watchdog_active;
3501
3502         if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3503                 return;
3504
3505         if (fan_watchdog_active)
3506                 cancel_delayed_work(&fan_watchdog_task);
3507
3508         if (fan_watchdog_maxinterval > 0) {
3509                 fan_watchdog_active = 1;
3510                 if (!schedule_delayed_work(&fan_watchdog_task,
3511                                 msecs_to_jiffies(fan_watchdog_maxinterval
3512                                                  * 1000))) {
3513                         printk(IBM_ERR "failed to schedule the fan watchdog, "
3514                                "watchdog will not trigger\n");
3515                 }
3516         } else
3517                 fan_watchdog_active = 0;
3518 }
3519
3520 static int fan_set_level(int level)
3521 {
3522         if (!fan_control_allowed)
3523                 return -EPERM;
3524
3525         switch (fan_control_access_mode) {
3526         case TPACPI_FAN_WR_ACPI_SFAN:
3527                 if (level >= 0 && level <= 7) {
3528                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3529                                 return -EIO;
3530                 } else
3531                         return -EINVAL;
3532                 break;
3533
3534         case TPACPI_FAN_WR_ACPI_FANS:
3535         case TPACPI_FAN_WR_TPEC:
3536                 if ((level != TP_EC_FAN_AUTO) &&
3537                     (level != TP_EC_FAN_FULLSPEED) &&
3538                     ((level < 0) || (level > 7)))
3539                         return -EINVAL;
3540
3541                 /* safety net should the EC not support AUTO
3542                  * or FULLSPEED mode bits and just ignore them */
3543                 if (level & TP_EC_FAN_FULLSPEED)
3544                         level |= 7;     /* safety min speed 7 */
3545                 else if (level & TP_EC_FAN_FULLSPEED)
3546                         level |= 4;     /* safety min speed 4 */
3547
3548                 if (!acpi_ec_write(fan_status_offset, level))
3549                         return -EIO;
3550                 else
3551                         tp_features.fan_ctrl_status_undef = 0;
3552                 break;
3553
3554         default:
3555                 return -ENXIO;
3556         }
3557         return 0;
3558 }
3559
3560 static int fan_set_level_safe(int level)
3561 {
3562         int rc;
3563
3564         if (!fan_control_allowed)
3565                 return -EPERM;
3566
3567         rc = mutex_lock_interruptible(&fan_mutex);
3568         if (rc < 0)
3569                 return rc;
3570
3571         if (level == TPACPI_FAN_LAST_LEVEL)
3572                 level = fan_control_desired_level;
3573
3574         rc = fan_set_level(level);
3575         if (!rc)
3576                 fan_update_desired_level(level);
3577
3578         mutex_unlock(&fan_mutex);
3579         return rc;
3580 }
3581
3582 static int fan_set_enable(void)
3583 {
3584         u8 s;
3585         int rc;
3586
3587         if (!fan_control_allowed)
3588                 return -EPERM;
3589
3590         rc = mutex_lock_interruptible(&fan_mutex);
3591         if (rc < 0)
3592                 return rc;
3593
3594         switch (fan_control_access_mode) {
3595         case TPACPI_FAN_WR_ACPI_FANS:
3596         case TPACPI_FAN_WR_TPEC:
3597                 rc = fan_get_status(&s);
3598                 if (rc < 0)
3599                         break;
3600
3601                 /* Don't go out of emergency fan mode */
3602                 if (s != 7) {
3603                         s &= 0x07;
3604                         s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3605                 }
3606
3607                 if (!acpi_ec_write(fan_status_offset, s))
3608                         rc = -EIO;
3609                 else {
3610                         tp_features.fan_ctrl_status_undef = 0;
3611                         rc = 0;
3612                 }
3613                 break;
3614
3615         case TPACPI_FAN_WR_ACPI_SFAN:
3616                 rc = fan_get_status(&s);
3617                 if (rc < 0)
3618                         break;
3619
3620                 s &= 0x07;
3621
3622                 /* Set fan to at least level 4 */
3623                 s |= 4;
3624
3625                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3626                         rc= -EIO;
3627                 else
3628                         rc = 0;
3629                 break;
3630
3631         default:
3632                 rc = -ENXIO;
3633         }
3634
3635         mutex_unlock(&fan_mutex);
3636         return rc;
3637 }
3638
3639 static int fan_set_disable(void)
3640 {
3641         int rc;
3642
3643         if (!fan_control_allowed)
3644                 return -EPERM;
3645
3646         rc = mutex_lock_interruptible(&fan_mutex);
3647         if (rc < 0)
3648                 return rc;
3649
3650         rc = 0;
3651         switch (fan_control_access_mode) {
3652         case TPACPI_FAN_WR_ACPI_FANS:
3653         case TPACPI_FAN_WR_TPEC:
3654                 if (!acpi_ec_write(fan_status_offset, 0x00))
3655                         rc = -EIO;
3656                 else {
3657                         fan_control_desired_level = 0;
3658                         tp_features.fan_ctrl_status_undef = 0;
3659                 }
3660                 break;
3661
3662         case TPACPI_FAN_WR_ACPI_SFAN:
3663                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3664                         rc = -EIO;
3665                 else
3666                         fan_control_desired_level = 0;
3667                 break;
3668
3669         default:
3670                 rc = -ENXIO;
3671         }
3672
3673
3674         mutex_unlock(&fan_mutex);
3675         return rc;
3676 }
3677
3678 static int fan_set_speed(int speed)
3679 {
3680         int rc;
3681
3682         if (!fan_control_allowed)
3683                 return -EPERM;
3684
3685         rc = mutex_lock_interruptible(&fan_mutex);
3686         if (rc < 0)
3687                 return rc;
3688
3689         rc = 0;
3690         switch (fan_control_access_mode) {
3691         case TPACPI_FAN_WR_ACPI_FANS:
3692                 if (speed >= 0 && speed <= 65535) {
3693                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3694                                         speed, speed, speed))
3695                                 rc = -EIO;
3696                 } else
3697                         rc = -EINVAL;
3698                 break;
3699
3700         default:
3701                 rc = -ENXIO;
3702         }
3703
3704         mutex_unlock(&fan_mutex);
3705         return rc;
3706 }
3707
3708 static int fan_read(char *p)
3709 {
3710         int len = 0;
3711         int rc;
3712         u8 status;
3713         unsigned int speed = 0;
3714
3715         switch (fan_status_access_mode) {
3716         case TPACPI_FAN_RD_ACPI_GFAN:
3717                 /* 570, 600e/x, 770e, 770x */
3718                 if ((rc = fan_get_status_safe(&status)) < 0)
3719                         return rc;
3720
3721                 len += sprintf(p + len, "status:\t\t%s\n"
3722                                "level:\t\t%d\n",
3723                                (status != 0) ? "enabled" : "disabled", status);
3724                 break;
3725
3726         case TPACPI_FAN_RD_TPEC:
3727                 /* all except 570, 600e/x, 770e, 770x */
3728                 if ((rc = fan_get_status_safe(&status)) < 0)
3729                         return rc;
3730
3731                 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3732                         if (status != fan_control_initial_status)
3733                                 tp_features.fan_ctrl_status_undef = 0;
3734                         else
3735                                 /* Return most likely status. In fact, it
3736                                  * might be the only possible status */
3737                                 status = TP_EC_FAN_AUTO;
3738                 }
3739
3740                 len += sprintf(p + len, "status:\t\t%s\n",
3741                                (status != 0) ? "enabled" : "disabled");
3742
3743                 if ((rc = fan_get_speed(&speed)) < 0)
3744                         return rc;
3745
3746                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3747
3748                 if (status & TP_EC_FAN_FULLSPEED)
3749                         /* Disengaged mode takes precedence */
3750                         len += sprintf(p + len, "level:\t\tdisengaged\n");
3751                 else if (status & TP_EC_FAN_AUTO)
3752                         len += sprintf(p + len, "level:\t\tauto\n");
3753                 else
3754                         len += sprintf(p + len, "level:\t\t%d\n", status);
3755                 break;
3756
3757         case TPACPI_FAN_NONE:
3758         default:
3759                 len += sprintf(p + len, "status:\t\tnot supported\n");
3760         }
3761
3762         if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3763                 len += sprintf(p + len, "commands:\tlevel <level>");
3764
3765                 switch (fan_control_access_mode) {
3766                 case TPACPI_FAN_WR_ACPI_SFAN:
3767                         len += sprintf(p + len, " (<level> is 0-7)\n");
3768                         break;
3769
3770                 default:
3771                         len += sprintf(p + len, " (<level> is 0-7, "
3772                                        "auto, disengaged, full-speed)\n");
3773                         break;
3774                 }
3775         }
3776
3777         if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3778                 len += sprintf(p + len, "commands:\tenable, disable\n"
3779                                "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3780                                "1-120 (seconds))\n");
3781
3782         if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3783                 len += sprintf(p + len, "commands:\tspeed <speed>"
3784                                " (<speed> is 0-65535)\n");
3785
3786         return len;
3787 }
3788
3789 static int fan_write_cmd_level(const char *cmd, int *rc)
3790 {
3791         int level;
3792
3793         if (strlencmp(cmd, "level auto") == 0)
3794                 level = TP_EC_FAN_AUTO;
3795         else if ((strlencmp(cmd, "level disengaged") == 0) |
3796                  (strlencmp(cmd, "level full-speed") == 0))
3797                 level = TP_EC_FAN_FULLSPEED;
3798         else if (sscanf(cmd, "level %d", &level) != 1)
3799                 return 0;
3800
3801         if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3802                 printk(IBM_ERR "level command accepted for unsupported "
3803                        "access mode %d", fan_control_access_mode);
3804
3805         return 1;
3806 }
3807
3808 static int fan_write_cmd_enable(const char *cmd, int *rc)
3809 {
3810         if (strlencmp(cmd, "enable") != 0)
3811                 return 0;
3812
3813         if ((*rc = fan_set_enable()) == -ENXIO)
3814                 printk(IBM_ERR "enable command accepted for unsupported "
3815                        "access mode %d", fan_control_access_mode);
3816
3817         return 1;
3818 }
3819
3820 static int fan_write_cmd_disable(const char *cmd, int *rc)
3821 {
3822         if (strlencmp(cmd, "disable") != 0)
3823                 return 0;
3824
3825         if ((*rc = fan_set_disable()) == -ENXIO)
3826                 printk(IBM_ERR "disable command accepted for unsupported "
3827                        "access mode %d", fan_control_access_mode);
3828
3829         return 1;
3830 }
3831
3832 static int fan_write_cmd_speed(const char *cmd, int *rc)
3833 {
3834         int speed;
3835
3836         /* TODO:
3837          * Support speed <low> <medium> <high> ? */
3838
3839         if (sscanf(cmd, "speed %d", &speed) != 1)
3840                 return 0;
3841
3842         if ((*rc = fan_set_speed(speed)) == -ENXIO)
3843                 printk(IBM_ERR "speed command accepted for unsupported "
3844                        "access mode %d", fan_control_access_mode);
3845
3846         return 1;
3847 }
3848
3849 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3850 {
3851         int interval;
3852
3853         if (sscanf(cmd, "watchdog %d", &interval) != 1)
3854                 return 0;
3855
3856         if (interval < 0 || interval > 120)
3857                 *rc = -EINVAL;
3858         else
3859                 fan_watchdog_maxinterval = interval;
3860
3861         return 1;
3862 }
3863
3864 static int fan_write(char *buf)
3865 {
3866         char *cmd;
3867         int rc = 0;
3868
3869         while (!rc && (cmd = next_cmd(&buf))) {
3870                 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3871                       fan_write_cmd_level(cmd, &rc)) &&
3872                     !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3873                       (fan_write_cmd_enable(cmd, &rc) ||
3874                        fan_write_cmd_disable(cmd, &rc) ||
3875                        fan_write_cmd_watchdog(cmd, &rc))) &&
3876                     !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3877                       fan_write_cmd_speed(cmd, &rc))
3878                     )
3879                         rc = -EINVAL;
3880                 else if (!rc)
3881                         fan_watchdog_reset();
3882         }
3883
3884         return rc;
3885 }
3886
3887 static struct ibm_struct fan_driver_data = {
3888         .name = "fan",
3889         .read = fan_read,
3890         .write = fan_write,
3891         .exit = fan_exit,
3892 };
3893
3894 /****************************************************************************
3895  ****************************************************************************
3896  *
3897  * Infrastructure
3898  *
3899  ****************************************************************************
3900  ****************************************************************************/
3901
3902 /* /proc support */
3903 static struct proc_dir_entry *proc_dir;
3904
3905 /* Subdriver registry */
3906 static LIST_HEAD(tpacpi_all_drivers);
3907
3908
3909 /*
3910  * Module and infrastructure proble, init and exit handling
3911  */
3912
3913 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3914 static const char * __init str_supported(int is_supported)
3915 {
3916         static char text_unsupported[] __initdata = "not supported";
3917
3918         return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3919 }
3920 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3921
3922 static int __init ibm_init(struct ibm_init_struct *iibm)
3923 {
3924         int ret;
3925         struct ibm_struct *ibm = iibm->data;
3926         struct proc_dir_entry *entry;
3927
3928         BUG_ON(ibm == NULL);
3929
3930         INIT_LIST_HEAD(&ibm->all_drivers);
3931
3932         if (ibm->flags.experimental && !experimental)
3933                 return 0;
3934
3935         dbg_printk(TPACPI_DBG_INIT,
3936                 "probing for %s\n", ibm->name);
3937
3938         if (iibm->init) {
3939                 ret = iibm->init(iibm);
3940                 if (ret > 0)
3941                         return 0;       /* probe failed */
3942                 if (ret)
3943                         return ret;
3944
3945                 ibm->flags.init_called = 1;
3946         }
3947
3948         if (ibm->acpi) {
3949                 if (ibm->acpi->hid) {
3950                         ret = register_tpacpi_subdriver(ibm);
3951                         if (ret)
3952                                 goto err_out;
3953                 }
3954
3955                 if (ibm->acpi->notify) {
3956                         ret = setup_acpi_notify(ibm);
3957                         if (ret == -ENODEV) {
3958                                 printk(IBM_NOTICE "disabling subdriver %s\n",
3959                                         ibm->name);
3960                                 ret = 0;
3961                                 goto err_out;
3962                         }
3963                         if (ret < 0)
3964                                 goto err_out;
3965                 }
3966         }
3967
3968         dbg_printk(TPACPI_DBG_INIT,
3969                 "%s installed\n", ibm->name);
3970
3971         if (ibm->read) {
3972                 entry = create_proc_entry(ibm->name,
3973                                           S_IFREG | S_IRUGO | S_IWUSR,
3974                                           proc_dir);
3975                 if (!entry) {
3976                         printk(IBM_ERR "unable to create proc entry %s\n",
3977                                ibm->name);
3978                         ret = -ENODEV;
3979                         goto err_out;
3980                 }
3981                 entry->owner = THIS_MODULE;
3982                 entry->data = ibm;
3983                 entry->read_proc = &dispatch_procfs_read;
3984                 if (ibm->write)
3985                         entry->write_proc = &dispatch_procfs_write;
3986                 ibm->flags.proc_created = 1;
3987         }
3988
3989         list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3990
3991         return 0;
3992
3993 err_out:
3994         dbg_printk(TPACPI_DBG_INIT,
3995                 "%s: at error exit path with result %d\n",
3996                 ibm->name, ret);
3997
3998         ibm_exit(ibm);
3999         return (ret < 0)? ret : 0;
4000 }
4001
4002 static void ibm_exit(struct ibm_struct *ibm)
4003 {
4004         dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4005
4006         list_del_init(&ibm->all_drivers);
4007
4008         if (ibm->flags.acpi_notify_installed) {
4009                 dbg_printk(TPACPI_DBG_EXIT,
4010                         "%s: acpi_remove_notify_handler\n", ibm->name);
4011                 BUG_ON(!ibm->acpi);
4012                 acpi_remove_notify_handler(*ibm->acpi->handle,
4013                                            ibm->acpi->type,
4014                                            dispatch_acpi_notify);
4015                 ibm->flags.acpi_notify_installed = 0;
4016                 ibm->flags.acpi_notify_installed = 0;
4017         }
4018
4019         if (ibm->flags.proc_created) {
4020                 dbg_printk(TPACPI_DBG_EXIT,
4021                         "%s: remove_proc_entry\n", ibm->name);
4022                 remove_proc_entry(ibm->name, proc_dir);
4023                 ibm->flags.proc_created = 0;
4024         }
4025
4026         if (ibm->flags.acpi_driver_registered) {
4027                 dbg_printk(TPACPI_DBG_EXIT,
4028                         "%s: acpi_bus_unregister_driver\n", ibm->name);
4029                 BUG_ON(!ibm->acpi);
4030                 acpi_bus_unregister_driver(ibm->acpi->driver);
4031                 kfree(ibm->acpi->driver);
4032                 ibm->acpi->driver = NULL;
4033                 ibm->flags.acpi_driver_registered = 0;
4034         }
4035
4036         if (ibm->flags.init_called && ibm->exit) {
4037                 ibm->exit();
4038                 ibm->flags.init_called = 0;
4039         }
4040
4041         dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4042 }
4043
4044 /* Probing */
4045
4046 static char *ibm_thinkpad_ec_found;
4047
4048 static char* __init check_dmi_for_ec(void)
4049 {
4050         struct dmi_device *dev = NULL;
4051         char ec_fw_string[18];
4052
4053         /*
4054          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4055          * X32 or newer, all Z series;  Some models must have an
4056          * up-to-date BIOS or they will not be detected.
4057          *
4058          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4059          */
4060         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4061                 if (sscanf(dev->name,
4062                            "IBM ThinkPad Embedded Controller -[%17c",
4063                            ec_fw_string) == 1) {
4064                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4065                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4066                         return kstrdup(ec_fw_string, GFP_KERNEL);
4067                 }
4068         }
4069         return NULL;
4070 }
4071
4072 static int __init probe_for_thinkpad(void)
4073 {
4074         int is_thinkpad;
4075
4076         if (acpi_disabled)
4077                 return -ENODEV;
4078
4079         /*
4080          * Non-ancient models have better DMI tagging, but very old models
4081          * don't.
4082          */
4083         is_thinkpad = dmi_name_in_vendors("ThinkPad");
4084
4085         /* ec is required because many other handles are relative to it */
4086         IBM_ACPIHANDLE_INIT(ec);
4087         if (!ec_handle) {
4088                 if (is_thinkpad)
4089                         printk(IBM_ERR
4090                                 "Not yet supported ThinkPad detected!\n");
4091                 return -ENODEV;
4092         }
4093
4094         /*
4095          * Risks a regression on very old machines, but reduces potential
4096          * false positives a damn great deal
4097          */
4098         if (!is_thinkpad)
4099                 is_thinkpad = dmi_name_in_vendors("IBM");
4100
4101         if (!is_thinkpad && !force_load)
4102                 return -ENODEV;
4103
4104         return 0;
4105 }
4106
4107
4108 /* Module init, exit, parameters */
4109
4110 static struct ibm_init_struct ibms_init[] __initdata = {
4111         {
4112                 .init = thinkpad_acpi_driver_init,
4113                 .data = &thinkpad_acpi_driver_data,
4114         },
4115         {
4116                 .init = hotkey_init,
4117                 .data = &hotkey_driver_data,
4118         },
4119         {
4120                 .init = bluetooth_init,
4121                 .data = &bluetooth_driver_data,
4122         },
4123         {
4124                 .init = wan_init,
4125                 .data = &wan_driver_data,
4126         },
4127         {
4128                 .init = video_init,
4129                 .data = &video_driver_data,
4130         },
4131         {
4132                 .init = light_init,
4133                 .data = &light_driver_data,
4134         },
4135 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4136         {
4137                 .init = dock_init,
4138                 .data = &dock_driver_data[0],
4139         },
4140         {
4141                 .init = dock_init2,
4142                 .data = &dock_driver_data[1],
4143         },
4144 #endif
4145 #ifdef CONFIG_THINKPAD_ACPI_BAY
4146         {
4147                 .init = bay_init,
4148                 .data = &bay_driver_data,
4149         },
4150 #endif
4151         {
4152                 .init = cmos_init,
4153                 .data = &cmos_driver_data,
4154         },
4155         {
4156                 .init = led_init,
4157                 .data = &led_driver_data,
4158         },
4159         {
4160                 .init = beep_init,
4161                 .data = &beep_driver_data,
4162         },
4163         {
4164                 .init = thermal_init,
4165                 .data = &thermal_driver_data,
4166         },
4167         {
4168                 .data = &ecdump_driver_data,
4169         },
4170         {
4171                 .init = brightness_init,
4172                 .data = &brightness_driver_data,
4173         },
4174         {
4175                 .data = &volume_driver_data,
4176         },
4177         {
4178                 .init = fan_init,
4179                 .data = &fan_driver_data,
4180         },
4181 };
4182
4183 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4184 {
4185         unsigned int i;
4186         struct ibm_struct *ibm;
4187
4188         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4189                 ibm = ibms_init[i].data;
4190                 BUG_ON(ibm == NULL);
4191
4192                 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4193                         if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4194                                 return -ENOSPC;
4195                         strcpy(ibms_init[i].param, val);
4196                         strcat(ibms_init[i].param, ",");
4197                         return 0;
4198                 }
4199         }
4200
4201         return -EINVAL;
4202 }
4203
4204 static int experimental;
4205 module_param(experimental, int, 0);
4206
4207 static u32 dbg_level;
4208 module_param_named(debug, dbg_level, uint, 0);
4209
4210 static int force_load;
4211 module_param(force_load, int, 0);
4212
4213 static int fan_control_allowed;
4214 module_param_named(fan_control, fan_control_allowed, int, 0);
4215
4216 #define IBM_PARAM(feature) \
4217         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4218
4219 IBM_PARAM(hotkey);
4220 IBM_PARAM(bluetooth);
4221 IBM_PARAM(video);
4222 IBM_PARAM(light);
4223 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4224 IBM_PARAM(dock);
4225 #endif
4226 #ifdef CONFIG_THINKPAD_ACPI_BAY
4227 IBM_PARAM(bay);
4228 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4229 IBM_PARAM(cmos);
4230 IBM_PARAM(led);
4231 IBM_PARAM(beep);
4232 IBM_PARAM(ecdump);
4233 IBM_PARAM(brightness);
4234 IBM_PARAM(volume);
4235 IBM_PARAM(fan);
4236
4237 static int __init thinkpad_acpi_module_init(void)
4238 {
4239         int ret, i;
4240
4241         /* Driver-level probe */
4242         ret = probe_for_thinkpad();
4243         if (ret)
4244                 return ret;
4245
4246         /* Driver initialization */
4247         ibm_thinkpad_ec_found = check_dmi_for_ec();
4248         IBM_ACPIHANDLE_INIT(ecrd);
4249         IBM_ACPIHANDLE_INIT(ecwr);
4250
4251         proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4252         if (!proc_dir) {
4253                 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4254                 thinkpad_acpi_module_exit();
4255                 return -ENODEV;
4256         }
4257         proc_dir->owner = THIS_MODULE;
4258
4259         ret = platform_driver_register(&tpacpi_pdriver);
4260         if (ret) {
4261                 printk(IBM_ERR "unable to register platform driver\n");
4262                 thinkpad_acpi_module_exit();
4263                 return ret;
4264         }
4265         ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4266         if (ret) {
4267                 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4268                 thinkpad_acpi_module_exit();
4269                 return ret;
4270         }
4271
4272
4273         /* Device initialization */
4274         tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4275                                                         NULL, 0);
4276         if (IS_ERR(tpacpi_pdev)) {
4277                 ret = PTR_ERR(tpacpi_pdev);
4278                 tpacpi_pdev = NULL;
4279                 printk(IBM_ERR "unable to register platform device\n");
4280                 thinkpad_acpi_module_exit();
4281                 return ret;
4282         }
4283         tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4284         if (IS_ERR(tpacpi_hwmon)) {
4285                 ret = PTR_ERR(tpacpi_hwmon);
4286                 tpacpi_hwmon = NULL;
4287                 printk(IBM_ERR "unable to register hwmon device\n");
4288                 thinkpad_acpi_module_exit();
4289                 return ret;
4290         }
4291         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4292                 ret = ibm_init(&ibms_init[i]);
4293                 if (ret >= 0 && *ibms_init[i].param)
4294                         ret = ibms_init[i].data->write(ibms_init[i].param);
4295                 if (ret < 0) {
4296                         thinkpad_acpi_module_exit();
4297                         return ret;
4298                 }
4299         }
4300
4301         return 0;
4302 }
4303
4304 static void thinkpad_acpi_module_exit(void)
4305 {
4306         struct ibm_struct *ibm, *itmp;
4307
4308         list_for_each_entry_safe_reverse(ibm, itmp,
4309                                          &tpacpi_all_drivers,
4310                                          all_drivers) {
4311                 ibm_exit(ibm);
4312         }
4313
4314         dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4315
4316         if (tpacpi_hwmon)
4317                 hwmon_device_unregister(tpacpi_hwmon);
4318
4319         if (tpacpi_pdev)
4320                 platform_device_unregister(tpacpi_pdev);
4321
4322         tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4323         platform_driver_unregister(&tpacpi_pdriver);
4324
4325         if (proc_dir)
4326                 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4327
4328         kfree(ibm_thinkpad_ec_found);
4329 }
4330
4331 module_init(thinkpad_acpi_module_init);
4332 module_exit(thinkpad_acpi_module_exit);