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