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