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