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