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