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