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