]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/misc/thinkpad_acpi.c
ACPI: thinkpad-acpi: add suspend handler
[mv-sheeva.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 TPACPI_VERSION "0.18"
25 #define TPACPI_SYSFS_VERSION 0x020101
26
27 /*
28  *  Changelog:
29  *  2007-10-20          changelog trimmed down
30  *
31  *  2007-03-27  0.14    renamed to thinkpad_acpi and moved to
32  *                      drivers/misc.
33  *
34  *  2006-11-22  0.13    new maintainer
35  *                      changelog now lives in git commit history, and will
36  *                      not be updated further in-file.
37  *
38  *  2005-03-17  0.11    support for 600e, 770x
39  *                          thanks to Jamie Lentin <lentinj@dial.pipex.com>
40  *
41  *  2005-01-16  0.9     use MODULE_VERSION
42  *                          thanks to Henrik Brix Andersen <brix@gentoo.org>
43  *                      fix parameter passing on module loading
44  *                          thanks to Rusty Russell <rusty@rustcorp.com.au>
45  *                          thanks to Jim Radford <radford@blackbean.org>
46  *  2004-11-08  0.8     fix init error case, don't return from a macro
47  *                          thanks to Chris Wright <chrisw@osdl.org>
48  */
49
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
60
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
71
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
75
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
78
79 #include <linux/pci_ids.h>
80
81
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN     0
84 #define TP_CMOS_VOLUME_UP       1
85 #define TP_CMOS_VOLUME_MUTE     2
86 #define TP_CMOS_BRIGHTNESS_UP   4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
88
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91         TP_NVRAM_ADDR_HK2               = 0x57,
92         TP_NVRAM_ADDR_THINKLIGHT        = 0x58,
93         TP_NVRAM_ADDR_VIDEO             = 0x59,
94         TP_NVRAM_ADDR_BRIGHTNESS        = 0x5e,
95         TP_NVRAM_ADDR_MIXER             = 0x60,
96 };
97
98 /* NVRAM bit masks */
99 enum {
100         TP_NVRAM_MASK_HKT_THINKPAD      = 0x08,
101         TP_NVRAM_MASK_HKT_ZOOM          = 0x20,
102         TP_NVRAM_MASK_HKT_DISPLAY       = 0x40,
103         TP_NVRAM_MASK_HKT_HIBERNATE     = 0x80,
104         TP_NVRAM_MASK_THINKLIGHT        = 0x10,
105         TP_NVRAM_MASK_HKT_DISPEXPND     = 0x30,
106         TP_NVRAM_MASK_HKT_BRIGHTNESS    = 0x20,
107         TP_NVRAM_MASK_LEVEL_BRIGHTNESS  = 0x0f,
108         TP_NVRAM_POS_LEVEL_BRIGHTNESS   = 0,
109         TP_NVRAM_MASK_MUTE              = 0x40,
110         TP_NVRAM_MASK_HKT_VOLUME        = 0x80,
111         TP_NVRAM_MASK_LEVEL_VOLUME      = 0x0f,
112         TP_NVRAM_POS_LEVEL_VOLUME       = 0,
113 };
114
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID            "IBM0068"
117
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT       0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION       0x4101
121
122
123 /****************************************************************************
124  * Main driver
125  */
126
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
132
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
137
138 #define TPACPI_MAX_ACPI_ARGS 3
139
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR         KERN_ERR    TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO   KERN_INFO   TPACPI_LOG
145 #define TPACPI_DEBUG  KERN_DEBUG  TPACPI_LOG
146
147 #define TPACPI_DBG_ALL          0xffff
148 #define TPACPI_DBG_ALL          0xffff
149 #define TPACPI_DBG_INIT         0x0001
150 #define TPACPI_DBG_EXIT         0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152         do { if (dbg_level & a_dbg_level) \
153                 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154         } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157         dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
162
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
166
167
168 /****************************************************************************
169  * Driver-wide structs and misc. variables
170  */
171
172 struct ibm_struct;
173
174 struct tp_acpi_drv_struct {
175         const struct acpi_device_id *hid;
176         struct acpi_driver *driver;
177
178         void (*notify) (struct ibm_struct *, u32);
179         acpi_handle *handle;
180         u32 type;
181         struct acpi_device *device;
182 };
183
184 struct ibm_struct {
185         char *name;
186
187         int (*read) (char *);
188         int (*write) (char *);
189         void (*exit) (void);
190         void (*resume) (void);
191         void (*suspend) (pm_message_t state);
192
193         struct list_head all_drivers;
194
195         struct tp_acpi_drv_struct *acpi;
196
197         struct {
198                 u8 acpi_driver_registered:1;
199                 u8 acpi_notify_installed:1;
200                 u8 proc_created:1;
201                 u8 init_called:1;
202                 u8 experimental:1;
203         } flags;
204 };
205
206 struct ibm_init_struct {
207         char param[32];
208
209         int (*init) (struct ibm_init_struct *);
210         struct ibm_struct *data;
211 };
212
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215         u32 bay_status:1;
216         u32 bay_eject:1;
217         u32 bay_status2:1;
218         u32 bay_eject2:1;
219 #endif
220         u32 bluetooth:1;
221         u32 hotkey:1;
222         u32 hotkey_mask:1;
223         u32 hotkey_wlsw:1;
224         u32 light:1;
225         u32 light_status:1;
226         u32 bright_16levels:1;
227         u32 wan:1;
228         u32 fan_ctrl_status_undef:1;
229         u32 input_device_registered:1;
230         u32 platform_drv_registered:1;
231         u32 platform_drv_attrs_registered:1;
232         u32 sensors_pdrv_registered:1;
233         u32 sensors_pdrv_attrs_registered:1;
234         u32 sensors_pdev_attrs_registered:1;
235         u32 hotkey_poll_active:1;
236 } tp_features;
237
238 struct thinkpad_id_data {
239         unsigned int vendor;    /* ThinkPad vendor:
240                                  * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
241
242         char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243         char *ec_version_str;   /* Something like 1ZHT51WW-1.04a */
244
245         u16 bios_model;         /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
246         u16 ec_model;
247
248         char *model_str;
249 };
250 static struct thinkpad_id_data thinkpad_id;
251
252 static enum {
253         TPACPI_LIFE_INIT = 0,
254         TPACPI_LIFE_RUNNING,
255         TPACPI_LIFE_EXITING,
256 } tpacpi_lifecycle;
257
258 static int experimental;
259 static u32 dbg_level;
260
261 /****************************************************************************
262  ****************************************************************************
263  *
264  * ACPI Helpers and device model
265  *
266  ****************************************************************************
267  ****************************************************************************/
268
269 /*************************************************************************
270  * ACPI basic handles
271  */
272
273 static acpi_handle root_handle;
274
275 #define TPACPI_HANDLE(object, parent, paths...)                 \
276         static acpi_handle  object##_handle;                    \
277         static acpi_handle *object##_parent = &parent##_handle; \
278         static char        *object##_path;                      \
279         static char        *object##_paths[] = { paths }
280
281 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",   /* 240, 240x */
282            "\\_SB.PCI.ISA.EC",  /* 570 */
283            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
284            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
286            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
287            "\\_SB.PCI0.LPC.EC", /* all others */
288            );
289
290 TPACPI_HANDLE(ecrd, ec, "ECRD");        /* 570 */
291 TPACPI_HANDLE(ecwr, ec, "ECWR");        /* 570 */
292
293 TPACPI_HANDLE(cmos, root, "\\UCMS",     /* R50, R50e, R50p, R51, */
294                                         /* T4x, X31, X40 */
295            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296            "\\CMS",             /* R40, R40e */
297            );                   /* all others */
298
299 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY",   /* 600e/x, 770e, 770x */
300            "^HKEY",             /* R30, R31 */
301            "HKEY",              /* all others */
302            );                   /* 570 */
303
304
305 /*************************************************************************
306  * ACPI helpers
307  */
308
309 static int acpi_evalf(acpi_handle handle,
310                       void *res, char *method, char *fmt, ...)
311 {
312         char *fmt0 = fmt;
313         struct acpi_object_list params;
314         union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
315         struct acpi_buffer result, *resultp;
316         union acpi_object out_obj;
317         acpi_status status;
318         va_list ap;
319         char res_type;
320         int success;
321         int quiet;
322
323         if (!*fmt) {
324                 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
325                 return 0;
326         }
327
328         if (*fmt == 'q') {
329                 quiet = 1;
330                 fmt++;
331         } else
332                 quiet = 0;
333
334         res_type = *(fmt++);
335
336         params.count = 0;
337         params.pointer = &in_objs[0];
338
339         va_start(ap, fmt);
340         while (*fmt) {
341                 char c = *(fmt++);
342                 switch (c) {
343                 case 'd':       /* int */
344                         in_objs[params.count].integer.value = va_arg(ap, int);
345                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346                         break;
347                         /* add more types as needed */
348                 default:
349                         printk(TPACPI_ERR "acpi_evalf() called "
350                                "with invalid format character '%c'\n", c);
351                         return 0;
352                 }
353         }
354         va_end(ap);
355
356         if (res_type != 'v') {
357                 result.length = sizeof(out_obj);
358                 result.pointer = &out_obj;
359                 resultp = &result;
360         } else
361                 resultp = NULL;
362
363         status = acpi_evaluate_object(handle, method, &params, resultp);
364
365         switch (res_type) {
366         case 'd':               /* int */
367                 if (res)
368                         *(int *)res = out_obj.integer.value;
369                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370                 break;
371         case 'v':               /* void */
372                 success = status == AE_OK;
373                 break;
374                 /* add more types as needed */
375         default:
376                 printk(TPACPI_ERR "acpi_evalf() called "
377                        "with invalid format character '%c'\n", res_type);
378                 return 0;
379         }
380
381         if (!success && !quiet)
382                 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
383                        method, fmt0, status);
384
385         return success;
386 }
387
388 static int acpi_ec_read(int i, u8 *p)
389 {
390         int v;
391
392         if (ecrd_handle) {
393                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394                         return 0;
395                 *p = v;
396         } else {
397                 if (ec_read(i, p) < 0)
398                         return 0;
399         }
400
401         return 1;
402 }
403
404 static int acpi_ec_write(int i, u8 v)
405 {
406         if (ecwr_handle) {
407                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408                         return 0;
409         } else {
410                 if (ec_write(i, v) < 0)
411                         return 0;
412         }
413
414         return 1;
415 }
416
417 static int _sta(acpi_handle handle)
418 {
419         int status;
420
421         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
422                 status = 0;
423
424         return status;
425 }
426
427 static int issue_thinkpad_cmos_command(int cmos_cmd)
428 {
429         if (!cmos_handle)
430                 return -ENXIO;
431
432         if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
433                 return -EIO;
434
435         return 0;
436 }
437
438 /*************************************************************************
439  * ACPI device model
440  */
441
442 #define TPACPI_ACPIHANDLE_INIT(object) \
443         drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
444                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
445
446 static void drv_acpi_handle_init(char *name,
447                            acpi_handle *handle, acpi_handle parent,
448                            char **paths, int num_paths, char **path)
449 {
450         int i;
451         acpi_status status;
452
453         vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
454                 name);
455
456         for (i = 0; i < num_paths; i++) {
457                 status = acpi_get_handle(parent, paths[i], handle);
458                 if (ACPI_SUCCESS(status)) {
459                         *path = paths[i];
460                         dbg_printk(TPACPI_DBG_INIT,
461                                    "Found ACPI handle %s for %s\n",
462                                    *path, name);
463                         return;
464                 }
465         }
466
467         vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
468                     name);
469         *handle = NULL;
470 }
471
472 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
473 {
474         struct ibm_struct *ibm = data;
475
476         if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
477                 return;
478
479         if (!ibm || !ibm->acpi || !ibm->acpi->notify)
480                 return;
481
482         ibm->acpi->notify(ibm, event);
483 }
484
485 static int __init setup_acpi_notify(struct ibm_struct *ibm)
486 {
487         acpi_status status;
488         int rc;
489
490         BUG_ON(!ibm->acpi);
491
492         if (!*ibm->acpi->handle)
493                 return 0;
494
495         vdbg_printk(TPACPI_DBG_INIT,
496                 "setting up ACPI notify for %s\n", ibm->name);
497
498         rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
499         if (rc < 0) {
500                 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
501                         ibm->name, rc);
502                 return -ENODEV;
503         }
504
505         acpi_driver_data(ibm->acpi->device) = ibm;
506         sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
507                 TPACPI_ACPI_EVENT_PREFIX,
508                 ibm->name);
509
510         status = acpi_install_notify_handler(*ibm->acpi->handle,
511                         ibm->acpi->type, dispatch_acpi_notify, ibm);
512         if (ACPI_FAILURE(status)) {
513                 if (status == AE_ALREADY_EXISTS) {
514                         printk(TPACPI_NOTICE
515                                "another device driver is already "
516                                "handling %s events\n", ibm->name);
517                 } else {
518                         printk(TPACPI_ERR
519                                "acpi_install_notify_handler(%s) failed: %d\n",
520                                ibm->name, status);
521                 }
522                 return -ENODEV;
523         }
524         ibm->flags.acpi_notify_installed = 1;
525         return 0;
526 }
527
528 static int __init tpacpi_device_add(struct acpi_device *device)
529 {
530         return 0;
531 }
532
533 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
534 {
535         int rc;
536
537         dbg_printk(TPACPI_DBG_INIT,
538                 "registering %s as an ACPI driver\n", ibm->name);
539
540         BUG_ON(!ibm->acpi);
541
542         ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
543         if (!ibm->acpi->driver) {
544                 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
545                 return -ENOMEM;
546         }
547
548         sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
549         ibm->acpi->driver->ids = ibm->acpi->hid;
550
551         ibm->acpi->driver->ops.add = &tpacpi_device_add;
552
553         rc = acpi_bus_register_driver(ibm->acpi->driver);
554         if (rc < 0) {
555                 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
556                        ibm->name, rc);
557                 kfree(ibm->acpi->driver);
558                 ibm->acpi->driver = NULL;
559         } else if (!rc)
560                 ibm->flags.acpi_driver_registered = 1;
561
562         return rc;
563 }
564
565
566 /****************************************************************************
567  ****************************************************************************
568  *
569  * Procfs Helpers
570  *
571  ****************************************************************************
572  ****************************************************************************/
573
574 static int dispatch_procfs_read(char *page, char **start, off_t off,
575                         int count, int *eof, void *data)
576 {
577         struct ibm_struct *ibm = data;
578         int len;
579
580         if (!ibm || !ibm->read)
581                 return -EINVAL;
582
583         len = ibm->read(page);
584         if (len < 0)
585                 return len;
586
587         if (len <= off + count)
588                 *eof = 1;
589         *start = page + off;
590         len -= off;
591         if (len > count)
592                 len = count;
593         if (len < 0)
594                 len = 0;
595
596         return len;
597 }
598
599 static int dispatch_procfs_write(struct file *file,
600                         const char __user *userbuf,
601                         unsigned long count, void *data)
602 {
603         struct ibm_struct *ibm = data;
604         char *kernbuf;
605         int ret;
606
607         if (!ibm || !ibm->write)
608                 return -EINVAL;
609
610         kernbuf = kmalloc(count + 2, GFP_KERNEL);
611         if (!kernbuf)
612                 return -ENOMEM;
613
614         if (copy_from_user(kernbuf, userbuf, count)) {
615                 kfree(kernbuf);
616                 return -EFAULT;
617         }
618
619         kernbuf[count] = 0;
620         strcat(kernbuf, ",");
621         ret = ibm->write(kernbuf);
622         if (ret == 0)
623                 ret = count;
624
625         kfree(kernbuf);
626
627         return ret;
628 }
629
630 static char *next_cmd(char **cmds)
631 {
632         char *start = *cmds;
633         char *end;
634
635         while ((end = strchr(start, ',')) && end == start)
636                 start = end + 1;
637
638         if (!end)
639                 return NULL;
640
641         *end = 0;
642         *cmds = end + 1;
643         return start;
644 }
645
646
647 /****************************************************************************
648  ****************************************************************************
649  *
650  * Device model: input, hwmon and platform
651  *
652  ****************************************************************************
653  ****************************************************************************/
654
655 static struct platform_device *tpacpi_pdev;
656 static struct platform_device *tpacpi_sensors_pdev;
657 static struct device *tpacpi_hwmon;
658 static struct input_dev *tpacpi_inputdev;
659 static struct mutex tpacpi_inputdev_send_mutex;
660 static LIST_HEAD(tpacpi_all_drivers);
661
662 static int tpacpi_suspend_handler(struct platform_device *pdev,
663                                   pm_message_t state)
664 {
665         struct ibm_struct *ibm, *itmp;
666
667         list_for_each_entry_safe(ibm, itmp,
668                                  &tpacpi_all_drivers,
669                                  all_drivers) {
670                 if (ibm->suspend)
671                         (ibm->suspend)(state);
672         }
673
674         return 0;
675 }
676
677 static int tpacpi_resume_handler(struct platform_device *pdev)
678 {
679         struct ibm_struct *ibm, *itmp;
680
681         list_for_each_entry_safe(ibm, itmp,
682                                  &tpacpi_all_drivers,
683                                  all_drivers) {
684                 if (ibm->resume)
685                         (ibm->resume)();
686         }
687
688         return 0;
689 }
690
691 static struct platform_driver tpacpi_pdriver = {
692         .driver = {
693                 .name = TPACPI_DRVR_NAME,
694                 .owner = THIS_MODULE,
695         },
696         .suspend = tpacpi_suspend_handler,
697         .resume = tpacpi_resume_handler,
698 };
699
700 static struct platform_driver tpacpi_hwmon_pdriver = {
701         .driver = {
702                 .name = TPACPI_HWMON_DRVR_NAME,
703                 .owner = THIS_MODULE,
704         },
705 };
706
707 /*************************************************************************
708  * sysfs support helpers
709  */
710
711 struct attribute_set {
712         unsigned int members, max_members;
713         struct attribute_group group;
714 };
715
716 struct attribute_set_obj {
717         struct attribute_set s;
718         struct attribute *a;
719 } __attribute__((packed));
720
721 static struct attribute_set *create_attr_set(unsigned int max_members,
722                                                 const char *name)
723 {
724         struct attribute_set_obj *sobj;
725
726         if (max_members == 0)
727                 return NULL;
728
729         /* Allocates space for implicit NULL at the end too */
730         sobj = kzalloc(sizeof(struct attribute_set_obj) +
731                     max_members * sizeof(struct attribute *),
732                     GFP_KERNEL);
733         if (!sobj)
734                 return NULL;
735         sobj->s.max_members = max_members;
736         sobj->s.group.attrs = &sobj->a;
737         sobj->s.group.name = name;
738
739         return &sobj->s;
740 }
741
742 #define destroy_attr_set(_set) \
743         kfree(_set);
744
745 /* not multi-threaded safe, use it in a single thread per set */
746 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
747 {
748         if (!s || !attr)
749                 return -EINVAL;
750
751         if (s->members >= s->max_members)
752                 return -ENOMEM;
753
754         s->group.attrs[s->members] = attr;
755         s->members++;
756
757         return 0;
758 }
759
760 static int add_many_to_attr_set(struct attribute_set *s,
761                         struct attribute **attr,
762                         unsigned int count)
763 {
764         int i, res;
765
766         for (i = 0; i < count; i++) {
767                 res = add_to_attr_set(s, attr[i]);
768                 if (res)
769                         return res;
770         }
771
772         return 0;
773 }
774
775 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
776 {
777         sysfs_remove_group(kobj, &s->group);
778         destroy_attr_set(s);
779 }
780
781 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
782         sysfs_create_group(_kobj, &_attr_set->group)
783
784 static int parse_strtoul(const char *buf,
785                 unsigned long max, unsigned long *value)
786 {
787         char *endp;
788
789         while (*buf && isspace(*buf))
790                 buf++;
791         *value = simple_strtoul(buf, &endp, 0);
792         while (*endp && isspace(*endp))
793                 endp++;
794         if (*endp || *value > max)
795                 return -EINVAL;
796
797         return 0;
798 }
799
800 /*************************************************************************
801  * thinkpad-acpi driver attributes
802  */
803
804 /* interface_version --------------------------------------------------- */
805 static ssize_t tpacpi_driver_interface_version_show(
806                                 struct device_driver *drv,
807                                 char *buf)
808 {
809         return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
810 }
811
812 static DRIVER_ATTR(interface_version, S_IRUGO,
813                 tpacpi_driver_interface_version_show, NULL);
814
815 /* debug_level --------------------------------------------------------- */
816 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
817                                                 char *buf)
818 {
819         return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
820 }
821
822 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
823                                                 const char *buf, size_t count)
824 {
825         unsigned long t;
826
827         if (parse_strtoul(buf, 0xffff, &t))
828                 return -EINVAL;
829
830         dbg_level = t;
831
832         return count;
833 }
834
835 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
836                 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
837
838 /* version ------------------------------------------------------------- */
839 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
840                                                 char *buf)
841 {
842         return snprintf(buf, PAGE_SIZE, "%s v%s\n",
843                         TPACPI_DESC, TPACPI_VERSION);
844 }
845
846 static DRIVER_ATTR(version, S_IRUGO,
847                 tpacpi_driver_version_show, NULL);
848
849 /* --------------------------------------------------------------------- */
850
851 static struct driver_attribute *tpacpi_driver_attributes[] = {
852         &driver_attr_debug_level, &driver_attr_version,
853         &driver_attr_interface_version,
854 };
855
856 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
857 {
858         int i, res;
859
860         i = 0;
861         res = 0;
862         while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
863                 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
864                 i++;
865         }
866
867         return res;
868 }
869
870 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
871 {
872         int i;
873
874         for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
875                 driver_remove_file(drv, tpacpi_driver_attributes[i]);
876 }
877
878 /****************************************************************************
879  ****************************************************************************
880  *
881  * Subdrivers
882  *
883  ****************************************************************************
884  ****************************************************************************/
885
886 /*************************************************************************
887  * thinkpad-acpi init subdriver
888  */
889
890 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
891 {
892         printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
893         printk(TPACPI_INFO "%s\n", TPACPI_URL);
894
895         printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
896                 (thinkpad_id.bios_version_str) ?
897                         thinkpad_id.bios_version_str : "unknown",
898                 (thinkpad_id.ec_version_str) ?
899                         thinkpad_id.ec_version_str : "unknown");
900
901         if (thinkpad_id.vendor && thinkpad_id.model_str)
902                 printk(TPACPI_INFO "%s %s\n",
903                         (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
904                                 "IBM" : ((thinkpad_id.vendor ==
905                                                 PCI_VENDOR_ID_LENOVO) ?
906                                         "Lenovo" : "Unknown vendor"),
907                         thinkpad_id.model_str);
908
909         return 0;
910 }
911
912 static int thinkpad_acpi_driver_read(char *p)
913 {
914         int len = 0;
915
916         len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
917         len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
918
919         return len;
920 }
921
922 static struct ibm_struct thinkpad_acpi_driver_data = {
923         .name = "driver",
924         .read = thinkpad_acpi_driver_read,
925 };
926
927 /*************************************************************************
928  * Hotkey subdriver
929  */
930
931 enum {  /* hot key scan codes (derived from ACPI DSDT) */
932         TP_ACPI_HOTKEYSCAN_FNF1         = 0,
933         TP_ACPI_HOTKEYSCAN_FNF2,
934         TP_ACPI_HOTKEYSCAN_FNF3,
935         TP_ACPI_HOTKEYSCAN_FNF4,
936         TP_ACPI_HOTKEYSCAN_FNF5,
937         TP_ACPI_HOTKEYSCAN_FNF6,
938         TP_ACPI_HOTKEYSCAN_FNF7,
939         TP_ACPI_HOTKEYSCAN_FNF8,
940         TP_ACPI_HOTKEYSCAN_FNF9,
941         TP_ACPI_HOTKEYSCAN_FNF10,
942         TP_ACPI_HOTKEYSCAN_FNF11,
943         TP_ACPI_HOTKEYSCAN_FNF12,
944         TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
945         TP_ACPI_HOTKEYSCAN_FNINSERT,
946         TP_ACPI_HOTKEYSCAN_FNDELETE,
947         TP_ACPI_HOTKEYSCAN_FNHOME,
948         TP_ACPI_HOTKEYSCAN_FNEND,
949         TP_ACPI_HOTKEYSCAN_FNPAGEUP,
950         TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
951         TP_ACPI_HOTKEYSCAN_FNSPACE,
952         TP_ACPI_HOTKEYSCAN_VOLUMEUP,
953         TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
954         TP_ACPI_HOTKEYSCAN_MUTE,
955         TP_ACPI_HOTKEYSCAN_THINKPAD,
956 };
957
958 enum {  /* Keys available through NVRAM polling */
959         TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
960         TPACPI_HKEY_NVRAM_GOOD_MASK  = 0x00fb8000U,
961 };
962
963 enum {  /* Positions of some of the keys in hotkey masks */
964         TP_ACPI_HKEY_DISPSWTCH_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
965         TP_ACPI_HKEY_DISPXPAND_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
966         TP_ACPI_HKEY_HIBERNATE_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
967         TP_ACPI_HKEY_BRGHTUP_MASK       = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
968         TP_ACPI_HKEY_BRGHTDWN_MASK      = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
969         TP_ACPI_HKEY_THNKLGHT_MASK      = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
970         TP_ACPI_HKEY_ZOOM_MASK          = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
971         TP_ACPI_HKEY_VOLUP_MASK         = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
972         TP_ACPI_HKEY_VOLDWN_MASK        = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
973         TP_ACPI_HKEY_MUTE_MASK          = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
974         TP_ACPI_HKEY_THINKPAD_MASK      = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
975 };
976
977 enum {  /* NVRAM to ACPI HKEY group map */
978         TP_NVRAM_HKEY_GROUP_HK2         = TP_ACPI_HKEY_THINKPAD_MASK |
979                                           TP_ACPI_HKEY_ZOOM_MASK |
980                                           TP_ACPI_HKEY_DISPSWTCH_MASK |
981                                           TP_ACPI_HKEY_HIBERNATE_MASK,
982         TP_NVRAM_HKEY_GROUP_BRIGHTNESS  = TP_ACPI_HKEY_BRGHTUP_MASK |
983                                           TP_ACPI_HKEY_BRGHTDWN_MASK,
984         TP_NVRAM_HKEY_GROUP_VOLUME      = TP_ACPI_HKEY_VOLUP_MASK |
985                                           TP_ACPI_HKEY_VOLDWN_MASK |
986                                           TP_ACPI_HKEY_MUTE_MASK,
987 };
988
989 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
990 struct tp_nvram_state {
991        u16 thinkpad_toggle:1;
992        u16 zoom_toggle:1;
993        u16 display_toggle:1;
994        u16 thinklight_toggle:1;
995        u16 hibernate_toggle:1;
996        u16 displayexp_toggle:1;
997        u16 display_state:1;
998        u16 brightness_toggle:1;
999        u16 volume_toggle:1;
1000        u16 mute:1;
1001
1002        u8 brightness_level;
1003        u8 volume_level;
1004 };
1005
1006 static struct task_struct *tpacpi_hotkey_task;
1007 static u32 hotkey_source_mask;          /* bit mask 0=ACPI,1=NVRAM */
1008 static int hotkey_poll_freq = 10;       /* Hz */
1009 static struct mutex hotkey_thread_mutex;
1010 static struct mutex hotkey_thread_data_mutex;
1011 static unsigned int hotkey_config_change;
1012
1013 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1014
1015 #define hotkey_source_mask 0U
1016
1017 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1018
1019 static struct mutex hotkey_mutex;
1020
1021 static int hotkey_orig_status;
1022 static u32 hotkey_orig_mask;
1023 static u32 hotkey_all_mask;
1024 static u32 hotkey_reserved_mask;
1025 static u32 hotkey_mask;
1026
1027 static unsigned int hotkey_report_mode;
1028
1029 static u16 *hotkey_keycode_map;
1030
1031 static struct attribute_set *hotkey_dev_attributes;
1032
1033 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1034 #define HOTKEY_CONFIG_CRITICAL_START \
1035         do { \
1036                 mutex_lock(&hotkey_thread_data_mutex); \
1037                 hotkey_config_change++; \
1038         } while (0);
1039 #define HOTKEY_CONFIG_CRITICAL_END \
1040         mutex_unlock(&hotkey_thread_data_mutex);
1041 #else
1042 #define HOTKEY_CONFIG_CRITICAL_START
1043 #define HOTKEY_CONFIG_CRITICAL_END
1044 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1045
1046 static int hotkey_get_wlsw(int *status)
1047 {
1048         if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1049                 return -EIO;
1050         return 0;
1051 }
1052
1053 /*
1054  * Call with hotkey_mutex held
1055  */
1056 static int hotkey_mask_get(void)
1057 {
1058         u32 m = 0;
1059
1060         if (tp_features.hotkey_mask) {
1061                 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1062                         return -EIO;
1063         }
1064         hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1065
1066         return 0;
1067 }
1068
1069 /*
1070  * Call with hotkey_mutex held
1071  */
1072 static int hotkey_mask_set(u32 mask)
1073 {
1074         int i;
1075         int rc = 0;
1076
1077         if (tp_features.hotkey_mask) {
1078                 HOTKEY_CONFIG_CRITICAL_START
1079                 for (i = 0; i < 32; i++) {
1080                         u32 m = 1 << i;
1081                         /* enable in firmware mask only keys not in NVRAM
1082                          * mode, but enable the key in the cached hotkey_mask
1083                          * regardless of mode, or the key will end up
1084                          * disabled by hotkey_mask_get() */
1085                         if (!acpi_evalf(hkey_handle,
1086                                         NULL, "MHKM", "vdd", i + 1,
1087                                         !!((mask & ~hotkey_source_mask) & m))) {
1088                                 rc = -EIO;
1089                                 break;
1090                         } else {
1091                                 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1092                         }
1093                 }
1094                 HOTKEY_CONFIG_CRITICAL_END
1095
1096                 /* hotkey_mask_get must be called unconditionally below */
1097                 if (!hotkey_mask_get() && !rc &&
1098                     (hotkey_mask & ~hotkey_source_mask) !=
1099                      (mask & ~hotkey_source_mask)) {
1100                         printk(TPACPI_NOTICE
1101                                "requested hot key mask 0x%08x, but "
1102                                "firmware forced it to 0x%08x\n",
1103                                mask, hotkey_mask);
1104                 }
1105         } else {
1106 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1107                 HOTKEY_CONFIG_CRITICAL_START
1108                 hotkey_mask = mask & hotkey_source_mask;
1109                 HOTKEY_CONFIG_CRITICAL_END
1110                 hotkey_mask_get();
1111                 if (hotkey_mask != mask) {
1112                         printk(TPACPI_NOTICE
1113                                "requested hot key mask 0x%08x, "
1114                                "forced to 0x%08x (NVRAM poll mask is "
1115                                "0x%08x): no firmware mask support\n",
1116                                mask, hotkey_mask, hotkey_source_mask);
1117                 }
1118 #else
1119                 hotkey_mask_get();
1120                 rc = -ENXIO;
1121 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1122         }
1123
1124         return rc;
1125 }
1126
1127 static int hotkey_status_get(int *status)
1128 {
1129         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1130                 return -EIO;
1131
1132         return 0;
1133 }
1134
1135 static int hotkey_status_set(int status)
1136 {
1137         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1138                 return -EIO;
1139
1140         return 0;
1141 }
1142
1143 static void tpacpi_input_send_radiosw(void)
1144 {
1145         int wlsw;
1146
1147         mutex_lock(&tpacpi_inputdev_send_mutex);
1148
1149         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1150                 input_report_switch(tpacpi_inputdev,
1151                                     SW_RADIO, !!wlsw);
1152                 input_sync(tpacpi_inputdev);
1153         }
1154
1155         mutex_unlock(&tpacpi_inputdev_send_mutex);
1156 }
1157
1158 static void tpacpi_input_send_key(unsigned int scancode)
1159 {
1160         unsigned int keycode;
1161
1162         keycode = hotkey_keycode_map[scancode];
1163
1164         if (keycode != KEY_RESERVED) {
1165                 mutex_lock(&tpacpi_inputdev_send_mutex);
1166
1167                 input_report_key(tpacpi_inputdev, keycode, 1);
1168                 if (keycode == KEY_UNKNOWN)
1169                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1170                                     scancode);
1171                 input_sync(tpacpi_inputdev);
1172
1173                 input_report_key(tpacpi_inputdev, keycode, 0);
1174                 if (keycode == KEY_UNKNOWN)
1175                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1176                                     scancode);
1177                 input_sync(tpacpi_inputdev);
1178
1179                 mutex_unlock(&tpacpi_inputdev_send_mutex);
1180         }
1181 }
1182
1183 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1184 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1185
1186 static void tpacpi_hotkey_send_key(unsigned int scancode)
1187 {
1188         tpacpi_input_send_key(scancode);
1189         if (hotkey_report_mode < 2) {
1190                 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1191                                                 0x80, 0x1001 + scancode);
1192         }
1193 }
1194
1195 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1196 {
1197         u8 d;
1198
1199         if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1200                 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1201                 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1202                 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1203                 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1204                 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1205         }
1206         if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1207                 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1208                 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1209         }
1210         if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1211                 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1212                 n->displayexp_toggle =
1213                                 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1214         }
1215         if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1216                 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1217                 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1218                                 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1219                 n->brightness_toggle =
1220                                 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1221         }
1222         if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1223                 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1224                 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1225                                 >> TP_NVRAM_POS_LEVEL_VOLUME;
1226                 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1227                 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1228         }
1229 }
1230
1231 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1232         do { \
1233                 if ((mask & (1 << __scancode)) && \
1234                     oldn->__member != newn->__member) \
1235                 tpacpi_hotkey_send_key(__scancode); \
1236         } while (0)
1237
1238 #define TPACPI_MAY_SEND_KEY(__scancode) \
1239         do { if (mask & (1 << __scancode)) \
1240                 tpacpi_hotkey_send_key(__scancode); } while (0)
1241
1242 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1243                                            struct tp_nvram_state *newn,
1244                                            u32 mask)
1245 {
1246         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1247         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1248         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1249         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1250
1251         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1252
1253         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1254
1255         /* handle volume */
1256         if (oldn->volume_toggle != newn->volume_toggle) {
1257                 if (oldn->mute != newn->mute) {
1258                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1259                 }
1260                 if (oldn->volume_level > newn->volume_level) {
1261                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1262                 } else if (oldn->volume_level < newn->volume_level) {
1263                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1264                 } else if (oldn->mute == newn->mute) {
1265                         /* repeated key presses that didn't change state */
1266                         if (newn->mute) {
1267                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1268                         } else if (newn->volume_level != 0) {
1269                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1270                         } else {
1271                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272                         }
1273                 }
1274         }
1275
1276         /* handle brightness */
1277         if (oldn->brightness_toggle != newn->brightness_toggle) {
1278                 if (oldn->brightness_level < newn->brightness_level) {
1279                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1280                 } else if (oldn->brightness_level > newn->brightness_level) {
1281                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1282                 } else {
1283                         /* repeated key presses that didn't change state */
1284                         if (newn->brightness_level != 0) {
1285                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1286                         } else {
1287                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1288                         }
1289                 }
1290         }
1291 }
1292
1293 #undef TPACPI_COMPARE_KEY
1294 #undef TPACPI_MAY_SEND_KEY
1295
1296 static int hotkey_kthread(void *data)
1297 {
1298         struct tp_nvram_state s[2];
1299         u32 mask;
1300         unsigned int si, so;
1301         unsigned long t;
1302         unsigned int change_detector, must_reset;
1303
1304         mutex_lock(&hotkey_thread_mutex);
1305
1306         if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1307                 goto exit;
1308
1309         set_freezable();
1310
1311         so = 0;
1312         si = 1;
1313         t = 0;
1314
1315         /* Initial state for compares */
1316         mutex_lock(&hotkey_thread_data_mutex);
1317         change_detector = hotkey_config_change;
1318         mask = hotkey_source_mask & hotkey_mask;
1319         mutex_unlock(&hotkey_thread_data_mutex);
1320         hotkey_read_nvram(&s[so], mask);
1321
1322         while (!kthread_should_stop() && hotkey_poll_freq) {
1323                 if (t == 0)
1324                         t = 1000/hotkey_poll_freq;
1325                 t = msleep_interruptible(t);
1326                 if (unlikely(kthread_should_stop()))
1327                         break;
1328                 must_reset = try_to_freeze();
1329                 if (t > 0 && !must_reset)
1330                         continue;
1331
1332                 mutex_lock(&hotkey_thread_data_mutex);
1333                 if (must_reset || hotkey_config_change != change_detector) {
1334                         /* forget old state on thaw or config change */
1335                         si = so;
1336                         t = 0;
1337                         change_detector = hotkey_config_change;
1338                 }
1339                 mask = hotkey_source_mask & hotkey_mask;
1340                 mutex_unlock(&hotkey_thread_data_mutex);
1341
1342                 if (likely(mask)) {
1343                         hotkey_read_nvram(&s[si], mask);
1344                         if (likely(si != so)) {
1345                                 hotkey_compare_and_issue_event(&s[so], &s[si],
1346                                                                 mask);
1347                         }
1348                 }
1349
1350                 so = si;
1351                 si ^= 1;
1352         }
1353
1354 exit:
1355         mutex_unlock(&hotkey_thread_mutex);
1356         return 0;
1357 }
1358
1359 static void hotkey_poll_stop_sync(void)
1360 {
1361         if (tpacpi_hotkey_task) {
1362                 if (frozen(tpacpi_hotkey_task) ||
1363                     freezing(tpacpi_hotkey_task))
1364                         thaw_process(tpacpi_hotkey_task);
1365
1366                 kthread_stop(tpacpi_hotkey_task);
1367                 tpacpi_hotkey_task = NULL;
1368                 mutex_lock(&hotkey_thread_mutex);
1369                 /* at this point, the thread did exit */
1370                 mutex_unlock(&hotkey_thread_mutex);
1371         }
1372 }
1373
1374 /* call with hotkey_mutex held */
1375 static void hotkey_poll_setup(int may_warn)
1376 {
1377         if ((hotkey_source_mask & hotkey_mask) != 0 &&
1378             hotkey_poll_freq > 0 &&
1379             (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1380                 if (!tpacpi_hotkey_task) {
1381                         tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1382                                                          NULL,
1383                                                          TPACPI_FILE "d");
1384                         if (IS_ERR(tpacpi_hotkey_task)) {
1385                                 tpacpi_hotkey_task = NULL;
1386                                 printk(TPACPI_ERR
1387                                        "could not create kernel thread "
1388                                        "for hotkey polling\n");
1389                         }
1390                 }
1391         } else {
1392                 hotkey_poll_stop_sync();
1393                 if (may_warn &&
1394                     hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1395                         printk(TPACPI_NOTICE
1396                                 "hot keys 0x%08x require polling, "
1397                                 "which is currently disabled\n",
1398                                 hotkey_source_mask);
1399                 }
1400         }
1401 }
1402
1403 static void hotkey_poll_setup_safe(int may_warn)
1404 {
1405         mutex_lock(&hotkey_mutex);
1406         hotkey_poll_setup(may_warn);
1407         mutex_unlock(&hotkey_mutex);
1408 }
1409
1410 static int hotkey_inputdev_open(struct input_dev *dev)
1411 {
1412         switch (tpacpi_lifecycle) {
1413         case TPACPI_LIFE_INIT:
1414                 /*
1415                  * hotkey_init will call hotkey_poll_setup_safe
1416                  * at the appropriate moment
1417                  */
1418                 return 0;
1419         case TPACPI_LIFE_EXITING:
1420                 return -EBUSY;
1421         case TPACPI_LIFE_RUNNING:
1422                 hotkey_poll_setup_safe(0);
1423                 return 0;
1424         }
1425
1426         /* Should only happen if tpacpi_lifecycle is corrupt */
1427         BUG();
1428         return -EBUSY;
1429 }
1430
1431 static void hotkey_inputdev_close(struct input_dev *dev)
1432 {
1433         /* disable hotkey polling when possible */
1434         if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1435                 hotkey_poll_setup_safe(0);
1436 }
1437 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1438
1439 /* sysfs hotkey enable ------------------------------------------------- */
1440 static ssize_t hotkey_enable_show(struct device *dev,
1441                            struct device_attribute *attr,
1442                            char *buf)
1443 {
1444         int res, status;
1445
1446         res = hotkey_status_get(&status);
1447         if (res)
1448                 return res;
1449
1450         return snprintf(buf, PAGE_SIZE, "%d\n", status);
1451 }
1452
1453 static ssize_t hotkey_enable_store(struct device *dev,
1454                             struct device_attribute *attr,
1455                             const char *buf, size_t count)
1456 {
1457         unsigned long t;
1458         int res;
1459
1460         if (parse_strtoul(buf, 1, &t))
1461                 return -EINVAL;
1462
1463         res = hotkey_status_set(t);
1464
1465         return (res) ? res : count;
1466 }
1467
1468 static struct device_attribute dev_attr_hotkey_enable =
1469         __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1470                 hotkey_enable_show, hotkey_enable_store);
1471
1472 /* sysfs hotkey mask --------------------------------------------------- */
1473 static ssize_t hotkey_mask_show(struct device *dev,
1474                            struct device_attribute *attr,
1475                            char *buf)
1476 {
1477         int res;
1478
1479         if (mutex_lock_interruptible(&hotkey_mutex))
1480                 return -ERESTARTSYS;
1481         res = hotkey_mask_get();
1482         mutex_unlock(&hotkey_mutex);
1483
1484         return (res)?
1485                 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1486 }
1487
1488 static ssize_t hotkey_mask_store(struct device *dev,
1489                             struct device_attribute *attr,
1490                             const char *buf, size_t count)
1491 {
1492         unsigned long t;
1493         int res;
1494
1495         if (parse_strtoul(buf, 0xffffffffUL, &t))
1496                 return -EINVAL;
1497
1498         if (mutex_lock_interruptible(&hotkey_mutex))
1499                 return -ERESTARTSYS;
1500
1501         res = hotkey_mask_set(t);
1502
1503 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1504         hotkey_poll_setup(1);
1505 #endif
1506
1507         mutex_unlock(&hotkey_mutex);
1508
1509         return (res) ? res : count;
1510 }
1511
1512 static struct device_attribute dev_attr_hotkey_mask =
1513         __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1514                 hotkey_mask_show, hotkey_mask_store);
1515
1516 /* sysfs hotkey bios_enabled ------------------------------------------- */
1517 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1518                            struct device_attribute *attr,
1519                            char *buf)
1520 {
1521         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1522 }
1523
1524 static struct device_attribute dev_attr_hotkey_bios_enabled =
1525         __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1526
1527 /* sysfs hotkey bios_mask ---------------------------------------------- */
1528 static ssize_t hotkey_bios_mask_show(struct device *dev,
1529                            struct device_attribute *attr,
1530                            char *buf)
1531 {
1532         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1533 }
1534
1535 static struct device_attribute dev_attr_hotkey_bios_mask =
1536         __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1537
1538 /* sysfs hotkey all_mask ----------------------------------------------- */
1539 static ssize_t hotkey_all_mask_show(struct device *dev,
1540                            struct device_attribute *attr,
1541                            char *buf)
1542 {
1543         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1544                                 hotkey_all_mask | hotkey_source_mask);
1545 }
1546
1547 static struct device_attribute dev_attr_hotkey_all_mask =
1548         __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1549
1550 /* sysfs hotkey recommended_mask --------------------------------------- */
1551 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1552                                             struct device_attribute *attr,
1553                                             char *buf)
1554 {
1555         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1556                         (hotkey_all_mask | hotkey_source_mask)
1557                         & ~hotkey_reserved_mask);
1558 }
1559
1560 static struct device_attribute dev_attr_hotkey_recommended_mask =
1561         __ATTR(hotkey_recommended_mask, S_IRUGO,
1562                 hotkey_recommended_mask_show, NULL);
1563
1564 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1565
1566 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1567 static ssize_t hotkey_source_mask_show(struct device *dev,
1568                            struct device_attribute *attr,
1569                            char *buf)
1570 {
1571         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1572 }
1573
1574 static ssize_t hotkey_source_mask_store(struct device *dev,
1575                             struct device_attribute *attr,
1576                             const char *buf, size_t count)
1577 {
1578         unsigned long t;
1579
1580         if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1581                 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1582                 return -EINVAL;
1583
1584         if (mutex_lock_interruptible(&hotkey_mutex))
1585                 return -ERESTARTSYS;
1586
1587         HOTKEY_CONFIG_CRITICAL_START
1588         hotkey_source_mask = t;
1589         HOTKEY_CONFIG_CRITICAL_END
1590
1591         hotkey_poll_setup(1);
1592
1593         mutex_unlock(&hotkey_mutex);
1594
1595         return count;
1596 }
1597
1598 static struct device_attribute dev_attr_hotkey_source_mask =
1599         __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1600                 hotkey_source_mask_show, hotkey_source_mask_store);
1601
1602 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1603 static ssize_t hotkey_poll_freq_show(struct device *dev,
1604                            struct device_attribute *attr,
1605                            char *buf)
1606 {
1607         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1608 }
1609
1610 static ssize_t hotkey_poll_freq_store(struct device *dev,
1611                             struct device_attribute *attr,
1612                             const char *buf, size_t count)
1613 {
1614         unsigned long t;
1615
1616         if (parse_strtoul(buf, 25, &t))
1617                 return -EINVAL;
1618
1619         if (mutex_lock_interruptible(&hotkey_mutex))
1620                 return -ERESTARTSYS;
1621
1622         hotkey_poll_freq = t;
1623
1624         hotkey_poll_setup(1);
1625         mutex_unlock(&hotkey_mutex);
1626
1627         return count;
1628 }
1629
1630 static struct device_attribute dev_attr_hotkey_poll_freq =
1631         __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1632                 hotkey_poll_freq_show, hotkey_poll_freq_store);
1633
1634 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1635
1636 /* sysfs hotkey radio_sw ----------------------------------------------- */
1637 static ssize_t hotkey_radio_sw_show(struct device *dev,
1638                            struct device_attribute *attr,
1639                            char *buf)
1640 {
1641         int res, s;
1642         res = hotkey_get_wlsw(&s);
1643         if (res < 0)
1644                 return res;
1645
1646         return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1647 }
1648
1649 static struct device_attribute dev_attr_hotkey_radio_sw =
1650         __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1651
1652 /* sysfs hotkey report_mode -------------------------------------------- */
1653 static ssize_t hotkey_report_mode_show(struct device *dev,
1654                            struct device_attribute *attr,
1655                            char *buf)
1656 {
1657         return snprintf(buf, PAGE_SIZE, "%d\n",
1658                 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1659 }
1660
1661 static struct device_attribute dev_attr_hotkey_report_mode =
1662         __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1663
1664 /* --------------------------------------------------------------------- */
1665
1666 static struct attribute *hotkey_attributes[] __initdata = {
1667         &dev_attr_hotkey_enable.attr,
1668         &dev_attr_hotkey_bios_enabled.attr,
1669         &dev_attr_hotkey_report_mode.attr,
1670 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1671         &dev_attr_hotkey_mask.attr,
1672         &dev_attr_hotkey_all_mask.attr,
1673         &dev_attr_hotkey_recommended_mask.attr,
1674         &dev_attr_hotkey_source_mask.attr,
1675         &dev_attr_hotkey_poll_freq.attr,
1676 #endif
1677 };
1678
1679 static struct attribute *hotkey_mask_attributes[] __initdata = {
1680         &dev_attr_hotkey_bios_mask.attr,
1681 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1682         &dev_attr_hotkey_mask.attr,
1683         &dev_attr_hotkey_all_mask.attr,
1684         &dev_attr_hotkey_recommended_mask.attr,
1685 #endif
1686 };
1687
1688 static int __init hotkey_init(struct ibm_init_struct *iibm)
1689 {
1690         /* Requirements for changing the default keymaps:
1691          *
1692          * 1. Many of the keys are mapped to KEY_RESERVED for very
1693          *    good reasons.  Do not change them unless you have deep
1694          *    knowledge on the IBM and Lenovo ThinkPad firmware for
1695          *    the various ThinkPad models.  The driver behaves
1696          *    differently for KEY_RESERVED: such keys have their
1697          *    hot key mask *unset* in mask_recommended, and also
1698          *    in the initial hot key mask programmed into the
1699          *    firmware at driver load time, which means the firm-
1700          *    ware may react very differently if you change them to
1701          *    something else;
1702          *
1703          * 2. You must be subscribed to the linux-thinkpad and
1704          *    ibm-acpi-devel mailing lists, and you should read the
1705          *    list archives since 2007 if you want to change the
1706          *    keymaps.  This requirement exists so that you will
1707          *    know the past history of problems with the thinkpad-
1708          *    acpi driver keymaps, and also that you will be
1709          *    listening to any bug reports;
1710          *
1711          * 3. Do not send thinkpad-acpi specific patches directly to
1712          *    for merging, *ever*.  Send them to the linux-acpi
1713          *    mailinglist for comments.  Merging is to be done only
1714          *    through acpi-test and the ACPI maintainer.
1715          *
1716          * If the above is too much to ask, don't change the keymap.
1717          * Ask the thinkpad-acpi maintainer to do it, instead.
1718          */
1719         static u16 ibm_keycode_map[] __initdata = {
1720                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1721                 KEY_FN_F1,      KEY_FN_F2,      KEY_COFFEE,     KEY_SLEEP,
1722                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1723                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
1724
1725                 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1726                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
1727                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
1728                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
1729
1730                 /* brightness: firmware always reacts to them, unless
1731                  * X.org did some tricks in the radeon BIOS scratch
1732                  * registers of *some* models */
1733                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
1734                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
1735
1736                 /* Thinklight: firmware always react to it */
1737                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
1738
1739                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
1740                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
1741
1742                 /* Volume: firmware always react to it and reprograms
1743                  * the built-in *extra* mixer.  Never map it to control
1744                  * another mixer by default. */
1745                 KEY_RESERVED,   /* 0x14: VOLUME UP */
1746                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
1747                 KEY_RESERVED,   /* 0x16: MUTE */
1748
1749                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
1750
1751                 /* (assignments unknown, please report if found) */
1752                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1753                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1754         };
1755         static u16 lenovo_keycode_map[] __initdata = {
1756                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1757                 KEY_FN_F1,      KEY_COFFEE,     KEY_BATTERY,    KEY_SLEEP,
1758                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1759                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
1760
1761                 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1762                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
1763                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
1764                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
1765
1766                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
1767                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
1768
1769                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
1770
1771                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
1772                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
1773
1774                 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1775                  * react to it and reprograms the built-in *extra* mixer.
1776                  * Never map it to control another mixer by default.
1777                  *
1778                  * T60?, T61, R60?, R61: firmware and EC tries to send
1779                  * these over the regular keyboard, so these are no-ops,
1780                  * but there are still weird bugs re. MUTE, so do not
1781                  * change unless you get test reports from all Lenovo
1782                  * models.  May cause the BIOS to interfere with the
1783                  * HDA mixer.
1784                  */
1785                 KEY_RESERVED,   /* 0x14: VOLUME UP */
1786                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
1787                 KEY_RESERVED,   /* 0x16: MUTE */
1788
1789                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
1790
1791                 /* (assignments unknown, please report if found) */
1792                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1793                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1794         };
1795
1796 #define TPACPI_HOTKEY_MAP_LEN           ARRAY_SIZE(ibm_keycode_map)
1797 #define TPACPI_HOTKEY_MAP_SIZE          sizeof(ibm_keycode_map)
1798 #define TPACPI_HOTKEY_MAP_TYPESIZE      sizeof(ibm_keycode_map[0])
1799
1800         int res, i;
1801         int status;
1802         int hkeyv;
1803
1804         vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1805
1806         BUG_ON(!tpacpi_inputdev);
1807         BUG_ON(tpacpi_inputdev->open != NULL ||
1808                tpacpi_inputdev->close != NULL);
1809
1810         TPACPI_ACPIHANDLE_INIT(hkey);
1811         mutex_init(&hotkey_mutex);
1812
1813 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1814         mutex_init(&hotkey_thread_mutex);
1815         mutex_init(&hotkey_thread_data_mutex);
1816 #endif
1817
1818         /* hotkey not supported on 570 */
1819         tp_features.hotkey = hkey_handle != NULL;
1820
1821         vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1822                 str_supported(tp_features.hotkey));
1823
1824         if (tp_features.hotkey) {
1825                 hotkey_dev_attributes = create_attr_set(10, NULL);
1826                 if (!hotkey_dev_attributes)
1827                         return -ENOMEM;
1828                 res = add_many_to_attr_set(hotkey_dev_attributes,
1829                                 hotkey_attributes,
1830                                 ARRAY_SIZE(hotkey_attributes));
1831                 if (res)
1832                         return res;
1833
1834                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1835                    A30, R30, R31, T20-22, X20-21, X22-24.  Detected by checking
1836                    for HKEY interface version 0x100 */
1837                 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1838                         if ((hkeyv >> 8) != 1) {
1839                                 printk(TPACPI_ERR "unknown version of the "
1840                                        "HKEY interface: 0x%x\n", hkeyv);
1841                                 printk(TPACPI_ERR "please report this to %s\n",
1842                                        TPACPI_MAIL);
1843                         } else {
1844                                 /*
1845                                  * MHKV 0x100 in A31, R40, R40e,
1846                                  * T4x, X31, and later
1847                                  */
1848                                 tp_features.hotkey_mask = 1;
1849                         }
1850                 }
1851
1852                 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1853                         str_supported(tp_features.hotkey_mask));
1854
1855                 if (tp_features.hotkey_mask) {
1856                         if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1857                                         "MHKA", "qd")) {
1858                                 printk(TPACPI_ERR
1859                                        "missing MHKA handler, "
1860                                        "please report this to %s\n",
1861                                        TPACPI_MAIL);
1862                                 /* FN+F12, FN+F4, FN+F3 */
1863                                 hotkey_all_mask = 0x080cU;
1864                         }
1865                 }
1866
1867                 /* hotkey_source_mask *must* be zero for
1868                  * the first hotkey_mask_get */
1869                 res = hotkey_status_get(&hotkey_orig_status);
1870                 if (!res && tp_features.hotkey_mask) {
1871                         res = hotkey_mask_get();
1872                         hotkey_orig_mask = hotkey_mask;
1873                         if (!res) {
1874                                 res = add_many_to_attr_set(
1875                                         hotkey_dev_attributes,
1876                                         hotkey_mask_attributes,
1877                                         ARRAY_SIZE(hotkey_mask_attributes));
1878                         }
1879                 }
1880
1881 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1882                 if (tp_features.hotkey_mask) {
1883                         hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1884                                                 & ~hotkey_all_mask;
1885                 } else {
1886                         hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1887                 }
1888
1889                 vdbg_printk(TPACPI_DBG_INIT,
1890                             "hotkey source mask 0x%08x, polling freq %d\n",
1891                             hotkey_source_mask, hotkey_poll_freq);
1892 #endif
1893
1894                 /* Not all thinkpads have a hardware radio switch */
1895                 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1896                         tp_features.hotkey_wlsw = 1;
1897                         printk(TPACPI_INFO
1898                                 "radio switch found; radios are %s\n",
1899                                 enabled(status, 0));
1900                         res = add_to_attr_set(hotkey_dev_attributes,
1901                                         &dev_attr_hotkey_radio_sw.attr);
1902                 }
1903
1904                 if (!res)
1905                         res = register_attr_set_with_sysfs(
1906                                         hotkey_dev_attributes,
1907                                         &tpacpi_pdev->dev.kobj);
1908                 if (res)
1909                         return res;
1910
1911                 /* Set up key map */
1912
1913                 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1914                                                 GFP_KERNEL);
1915                 if (!hotkey_keycode_map) {
1916                         printk(TPACPI_ERR
1917                                 "failed to allocate memory for key map\n");
1918                         return -ENOMEM;
1919                 }
1920
1921                 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1922                         dbg_printk(TPACPI_DBG_INIT,
1923                                    "using Lenovo default hot key map\n");
1924                         memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1925                                 TPACPI_HOTKEY_MAP_SIZE);
1926                 } else {
1927                         dbg_printk(TPACPI_DBG_INIT,
1928                                    "using IBM default hot key map\n");
1929                         memcpy(hotkey_keycode_map, &ibm_keycode_map,
1930                                 TPACPI_HOTKEY_MAP_SIZE);
1931                 }
1932
1933                 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1934                 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1935                 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1936                 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1937                 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1938                 tpacpi_inputdev->keycode = hotkey_keycode_map;
1939                 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1940                         if (hotkey_keycode_map[i] != KEY_RESERVED) {
1941                                 set_bit(hotkey_keycode_map[i],
1942                                         tpacpi_inputdev->keybit);
1943                         } else {
1944                                 if (i < sizeof(hotkey_reserved_mask)*8)
1945                                         hotkey_reserved_mask |= 1 << i;
1946                         }
1947                 }
1948
1949                 if (tp_features.hotkey_wlsw) {
1950                         set_bit(EV_SW, tpacpi_inputdev->evbit);
1951                         set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1952                 }
1953
1954                 dbg_printk(TPACPI_DBG_INIT,
1955                                 "enabling hot key handling\n");
1956                 res = hotkey_status_set(1);
1957                 if (res)
1958                         return res;
1959                 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1960                                         & ~hotkey_reserved_mask)
1961                                         | hotkey_orig_mask);
1962                 if (res < 0 && res != -ENXIO)
1963                         return res;
1964
1965                 dbg_printk(TPACPI_DBG_INIT,
1966                                 "legacy hot key reporting over procfs %s\n",
1967                                 (hotkey_report_mode < 2) ?
1968                                         "enabled" : "disabled");
1969
1970 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971                 tpacpi_inputdev->open = &hotkey_inputdev_open;
1972                 tpacpi_inputdev->close = &hotkey_inputdev_close;
1973
1974                 hotkey_poll_setup_safe(1);
1975 #endif
1976         }
1977
1978         return (tp_features.hotkey)? 0 : 1;
1979 }
1980
1981 static void hotkey_exit(void)
1982 {
1983 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1984         hotkey_poll_stop_sync();
1985 #endif
1986
1987         if (tp_features.hotkey) {
1988                 dbg_printk(TPACPI_DBG_EXIT,
1989                            "restoring original hot key mask\n");
1990                 /* no short-circuit boolean operator below! */
1991                 if ((hotkey_mask_set(hotkey_orig_mask) |
1992                      hotkey_status_set(hotkey_orig_status)) != 0)
1993                         printk(TPACPI_ERR
1994                                "failed to restore hot key mask "
1995                                "to BIOS defaults\n");
1996         }
1997
1998         if (hotkey_dev_attributes) {
1999                 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2000                 hotkey_dev_attributes = NULL;
2001         }
2002 }
2003
2004 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2005 {
2006         u32 hkey;
2007         unsigned int scancode;
2008         int send_acpi_ev;
2009         int ignore_acpi_ev;
2010
2011         if (event != 0x80) {
2012                 printk(TPACPI_ERR
2013                        "unknown HKEY notification event %d\n", event);
2014                 /* forward it to userspace, maybe it knows how to handle it */
2015                 acpi_bus_generate_netlink_event(
2016                                         ibm->acpi->device->pnp.device_class,
2017                                         ibm->acpi->device->dev.bus_id,
2018                                         event, 0);
2019                 return;
2020         }
2021
2022         while (1) {
2023                 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2024                         printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2025                         return;
2026                 }
2027
2028                 if (hkey == 0) {
2029                         /* queue empty */
2030                         return;
2031                 }
2032
2033                 send_acpi_ev = 0;
2034                 ignore_acpi_ev = 0;
2035
2036                 switch (hkey >> 12) {
2037                 case 1:
2038                         /* 0x1000-0x1FFF: key presses */
2039                         scancode = hkey & 0xfff;
2040                         if (scancode > 0 && scancode < 0x21) {
2041                                 scancode--;
2042                                 if (!(hotkey_source_mask & (1 << scancode))) {
2043                                         tpacpi_input_send_key(scancode);
2044                                 } else {
2045                                         ignore_acpi_ev = 1;
2046                                 }
2047                         } else {
2048                                 printk(TPACPI_ERR
2049                                        "hotkey 0x%04x out of range "
2050                                        "for keyboard map\n", hkey);
2051                                 send_acpi_ev = 1;
2052                         }
2053                         break;
2054                 case 5:
2055                         /* 0x5000-0x5FFF: LID */
2056                         /* we don't handle it through this path, just
2057                          * eat up known LID events */
2058                         if (hkey != 0x5001 && hkey != 0x5002) {
2059                                 printk(TPACPI_ERR
2060                                        "unknown LID-related HKEY event: "
2061                                        "0x%04x\n", hkey);
2062                                 send_acpi_ev = 1;
2063                         } else {
2064                                 ignore_acpi_ev = 1;
2065                         }
2066                         break;
2067                 case 7:
2068                         /* 0x7000-0x7FFF: misc */
2069                         if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2070                                 tpacpi_input_send_radiosw();
2071                                 break;
2072                         }
2073                         /* fallthrough to default */
2074                 default:
2075                         /* case 2: dock-related */
2076                         /*      0x2305 - T43 waking up due to bay lever
2077                          *               eject while aslept */
2078                         /* case 3: ultra-bay related. maybe bay in dock? */
2079                         /*      0x3003 - T43 after wake up by bay lever
2080                          *               eject (0x2305) */
2081                         printk(TPACPI_NOTICE
2082                                "unhandled HKEY event 0x%04x\n", hkey);
2083                         send_acpi_ev = 1;
2084                 }
2085
2086                 /* Legacy events */
2087                 if (!ignore_acpi_ev &&
2088                     (send_acpi_ev || hotkey_report_mode < 2)) {
2089                         acpi_bus_generate_proc_event(ibm->acpi->device,
2090                                                      event, hkey);
2091                 }
2092
2093                 /* netlink events */
2094                 if (!ignore_acpi_ev && send_acpi_ev) {
2095                         acpi_bus_generate_netlink_event(
2096                                         ibm->acpi->device->pnp.device_class,
2097                                         ibm->acpi->device->dev.bus_id,
2098                                         event, hkey);
2099                 }
2100         }
2101 }
2102
2103 static void hotkey_resume(void)
2104 {
2105         if (hotkey_mask_get())
2106                 printk(TPACPI_ERR
2107                        "error while trying to read hot key mask "
2108                        "from firmware\n");
2109         tpacpi_input_send_radiosw();
2110 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2111         hotkey_poll_setup_safe(0);
2112 #endif
2113 }
2114
2115 /* procfs -------------------------------------------------------------- */
2116 static int hotkey_read(char *p)
2117 {
2118         int res, status;
2119         int len = 0;
2120
2121         if (!tp_features.hotkey) {
2122                 len += sprintf(p + len, "status:\t\tnot supported\n");
2123                 return len;
2124         }
2125
2126         if (mutex_lock_interruptible(&hotkey_mutex))
2127                 return -ERESTARTSYS;
2128         res = hotkey_status_get(&status);
2129         if (!res)
2130                 res = hotkey_mask_get();
2131         mutex_unlock(&hotkey_mutex);
2132         if (res)
2133                 return res;
2134
2135         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2136         if (tp_features.hotkey_mask) {
2137                 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2138                 len += sprintf(p + len,
2139                                "commands:\tenable, disable, reset, <mask>\n");
2140         } else {
2141                 len += sprintf(p + len, "mask:\t\tnot supported\n");
2142                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2143         }
2144
2145         return len;
2146 }
2147
2148 static int hotkey_write(char *buf)
2149 {
2150         int res, status;
2151         u32 mask;
2152         char *cmd;
2153
2154         if (!tp_features.hotkey)
2155                 return -ENODEV;
2156
2157         if (mutex_lock_interruptible(&hotkey_mutex))
2158                 return -ERESTARTSYS;
2159
2160         status = -1;
2161         mask = hotkey_mask;
2162
2163         res = 0;
2164         while ((cmd = next_cmd(&buf))) {
2165                 if (strlencmp(cmd, "enable") == 0) {
2166                         status = 1;
2167                 } else if (strlencmp(cmd, "disable") == 0) {
2168                         status = 0;
2169                 } else if (strlencmp(cmd, "reset") == 0) {
2170                         status = hotkey_orig_status;
2171                         mask = hotkey_orig_mask;
2172                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2173                         /* mask set */
2174                 } else if (sscanf(cmd, "%x", &mask) == 1) {
2175                         /* mask set */
2176                 } else {
2177                         res = -EINVAL;
2178                         goto errexit;
2179                 }
2180         }
2181         if (status != -1)
2182                 res = hotkey_status_set(status);
2183
2184         if (!res && mask != hotkey_mask)
2185                 res = hotkey_mask_set(mask);
2186
2187 errexit:
2188         mutex_unlock(&hotkey_mutex);
2189         return res;
2190 }
2191
2192 static const struct acpi_device_id ibm_htk_device_ids[] = {
2193         {TPACPI_ACPI_HKEY_HID, 0},
2194         {"", 0},
2195 };
2196
2197 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2198         .hid = ibm_htk_device_ids,
2199         .notify = hotkey_notify,
2200         .handle = &hkey_handle,
2201         .type = ACPI_DEVICE_NOTIFY,
2202 };
2203
2204 static struct ibm_struct hotkey_driver_data = {
2205         .name = "hotkey",
2206         .read = hotkey_read,
2207         .write = hotkey_write,
2208         .exit = hotkey_exit,
2209         .resume = hotkey_resume,
2210         .acpi = &ibm_hotkey_acpidriver,
2211 };
2212
2213 /*************************************************************************
2214  * Bluetooth subdriver
2215  */
2216
2217 enum {
2218         /* ACPI GBDC/SBDC bits */
2219         TP_ACPI_BLUETOOTH_HWPRESENT     = 0x01, /* Bluetooth hw available */
2220         TP_ACPI_BLUETOOTH_RADIOSSW      = 0x02, /* Bluetooth radio enabled */
2221         TP_ACPI_BLUETOOTH_UNK           = 0x04, /* unknown function */
2222 };
2223
2224 static int bluetooth_get_radiosw(void);
2225 static int bluetooth_set_radiosw(int radio_on);
2226
2227 /* sysfs bluetooth enable ---------------------------------------------- */
2228 static ssize_t bluetooth_enable_show(struct device *dev,
2229                            struct device_attribute *attr,
2230                            char *buf)
2231 {
2232         int status;
2233
2234         status = bluetooth_get_radiosw();
2235         if (status < 0)
2236                 return status;
2237
2238         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2239 }
2240
2241 static ssize_t bluetooth_enable_store(struct device *dev,
2242                             struct device_attribute *attr,
2243                             const char *buf, size_t count)
2244 {
2245         unsigned long t;
2246         int res;
2247
2248         if (parse_strtoul(buf, 1, &t))
2249                 return -EINVAL;
2250
2251         res = bluetooth_set_radiosw(t);
2252
2253         return (res) ? res : count;
2254 }
2255
2256 static struct device_attribute dev_attr_bluetooth_enable =
2257         __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2258                 bluetooth_enable_show, bluetooth_enable_store);
2259
2260 /* --------------------------------------------------------------------- */
2261
2262 static struct attribute *bluetooth_attributes[] = {
2263         &dev_attr_bluetooth_enable.attr,
2264         NULL
2265 };
2266
2267 static const struct attribute_group bluetooth_attr_group = {
2268         .attrs = bluetooth_attributes,
2269 };
2270
2271 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2272 {
2273         int res;
2274         int status = 0;
2275
2276         vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2277
2278         TPACPI_ACPIHANDLE_INIT(hkey);
2279
2280         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2281            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2282         tp_features.bluetooth = hkey_handle &&
2283             acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2284
2285         vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2286                 str_supported(tp_features.bluetooth),
2287                 status);
2288
2289         if (tp_features.bluetooth) {
2290                 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2291                         /* no bluetooth hardware present in system */
2292                         tp_features.bluetooth = 0;
2293                         dbg_printk(TPACPI_DBG_INIT,
2294                                    "bluetooth hardware not installed\n");
2295                 } else {
2296                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2297                                         &bluetooth_attr_group);
2298                         if (res)
2299                                 return res;
2300                 }
2301         }
2302
2303         return (tp_features.bluetooth)? 0 : 1;
2304 }
2305
2306 static void bluetooth_exit(void)
2307 {
2308         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2309                         &bluetooth_attr_group);
2310 }
2311
2312 static int bluetooth_get_radiosw(void)
2313 {
2314         int status;
2315
2316         if (!tp_features.bluetooth)
2317                 return -ENODEV;
2318
2319         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2320                 return -EIO;
2321
2322         return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2323 }
2324
2325 static int bluetooth_set_radiosw(int radio_on)
2326 {
2327         int status;
2328
2329         if (!tp_features.bluetooth)
2330                 return -ENODEV;
2331
2332         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2333                 return -EIO;
2334         if (radio_on)
2335                 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2336         else
2337                 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2338         if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2339                 return -EIO;
2340
2341         return 0;
2342 }
2343
2344 /* procfs -------------------------------------------------------------- */
2345 static int bluetooth_read(char *p)
2346 {
2347         int len = 0;
2348         int status = bluetooth_get_radiosw();
2349
2350         if (!tp_features.bluetooth)
2351                 len += sprintf(p + len, "status:\t\tnot supported\n");
2352         else {
2353                 len += sprintf(p + len, "status:\t\t%s\n",
2354                                 (status)? "enabled" : "disabled");
2355                 len += sprintf(p + len, "commands:\tenable, disable\n");
2356         }
2357
2358         return len;
2359 }
2360
2361 static int bluetooth_write(char *buf)
2362 {
2363         char *cmd;
2364
2365         if (!tp_features.bluetooth)
2366                 return -ENODEV;
2367
2368         while ((cmd = next_cmd(&buf))) {
2369                 if (strlencmp(cmd, "enable") == 0) {
2370                         bluetooth_set_radiosw(1);
2371                 } else if (strlencmp(cmd, "disable") == 0) {
2372                         bluetooth_set_radiosw(0);
2373                 } else
2374                         return -EINVAL;
2375         }
2376
2377         return 0;
2378 }
2379
2380 static struct ibm_struct bluetooth_driver_data = {
2381         .name = "bluetooth",
2382         .read = bluetooth_read,
2383         .write = bluetooth_write,
2384         .exit = bluetooth_exit,
2385 };
2386
2387 /*************************************************************************
2388  * Wan subdriver
2389  */
2390
2391 enum {
2392         /* ACPI GWAN/SWAN bits */
2393         TP_ACPI_WANCARD_HWPRESENT       = 0x01, /* Wan hw available */
2394         TP_ACPI_WANCARD_RADIOSSW        = 0x02, /* Wan radio enabled */
2395         TP_ACPI_WANCARD_UNK             = 0x04, /* unknown function */
2396 };
2397
2398 static int wan_get_radiosw(void);
2399 static int wan_set_radiosw(int radio_on);
2400
2401 /* sysfs wan enable ---------------------------------------------------- */
2402 static ssize_t wan_enable_show(struct device *dev,
2403                            struct device_attribute *attr,
2404                            char *buf)
2405 {
2406         int status;
2407
2408         status = wan_get_radiosw();
2409         if (status < 0)
2410                 return status;
2411
2412         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2413 }
2414
2415 static ssize_t wan_enable_store(struct device *dev,
2416                             struct device_attribute *attr,
2417                             const char *buf, size_t count)
2418 {
2419         unsigned long t;
2420         int res;
2421
2422         if (parse_strtoul(buf, 1, &t))
2423                 return -EINVAL;
2424
2425         res = wan_set_radiosw(t);
2426
2427         return (res) ? res : count;
2428 }
2429
2430 static struct device_attribute dev_attr_wan_enable =
2431         __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2432                 wan_enable_show, wan_enable_store);
2433
2434 /* --------------------------------------------------------------------- */
2435
2436 static struct attribute *wan_attributes[] = {
2437         &dev_attr_wan_enable.attr,
2438         NULL
2439 };
2440
2441 static const struct attribute_group wan_attr_group = {
2442         .attrs = wan_attributes,
2443 };
2444
2445 static int __init wan_init(struct ibm_init_struct *iibm)
2446 {
2447         int res;
2448         int status = 0;
2449
2450         vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2451
2452         TPACPI_ACPIHANDLE_INIT(hkey);
2453
2454         tp_features.wan = hkey_handle &&
2455             acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2456
2457         vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2458                 str_supported(tp_features.wan),
2459                 status);
2460
2461         if (tp_features.wan) {
2462                 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2463                         /* no wan hardware present in system */
2464                         tp_features.wan = 0;
2465                         dbg_printk(TPACPI_DBG_INIT,
2466                                    "wan hardware not installed\n");
2467                 } else {
2468                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2469                                         &wan_attr_group);
2470                         if (res)
2471                                 return res;
2472                 }
2473         }
2474
2475         return (tp_features.wan)? 0 : 1;
2476 }
2477
2478 static void wan_exit(void)
2479 {
2480         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2481                 &wan_attr_group);
2482 }
2483
2484 static int wan_get_radiosw(void)
2485 {
2486         int status;
2487
2488         if (!tp_features.wan)
2489                 return -ENODEV;
2490
2491         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2492                 return -EIO;
2493
2494         return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2495 }
2496
2497 static int wan_set_radiosw(int radio_on)
2498 {
2499         int status;
2500
2501         if (!tp_features.wan)
2502                 return -ENODEV;
2503
2504         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2505                 return -EIO;
2506         if (radio_on)
2507                 status |= TP_ACPI_WANCARD_RADIOSSW;
2508         else
2509                 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2510         if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2511                 return -EIO;
2512
2513         return 0;
2514 }
2515
2516 /* procfs -------------------------------------------------------------- */
2517 static int wan_read(char *p)
2518 {
2519         int len = 0;
2520         int status = wan_get_radiosw();
2521
2522         if (!tp_features.wan)
2523                 len += sprintf(p + len, "status:\t\tnot supported\n");
2524         else {
2525                 len += sprintf(p + len, "status:\t\t%s\n",
2526                                 (status)? "enabled" : "disabled");
2527                 len += sprintf(p + len, "commands:\tenable, disable\n");
2528         }
2529
2530         return len;
2531 }
2532
2533 static int wan_write(char *buf)
2534 {
2535         char *cmd;
2536
2537         if (!tp_features.wan)
2538                 return -ENODEV;
2539
2540         while ((cmd = next_cmd(&buf))) {
2541                 if (strlencmp(cmd, "enable") == 0) {
2542                         wan_set_radiosw(1);
2543                 } else if (strlencmp(cmd, "disable") == 0) {
2544                         wan_set_radiosw(0);
2545                 } else
2546                         return -EINVAL;
2547         }
2548
2549         return 0;
2550 }
2551
2552 static struct ibm_struct wan_driver_data = {
2553         .name = "wan",
2554         .read = wan_read,
2555         .write = wan_write,
2556         .exit = wan_exit,
2557         .flags.experimental = 1,
2558 };
2559
2560 /*************************************************************************
2561  * Video subdriver
2562  */
2563
2564 enum video_access_mode {
2565         TPACPI_VIDEO_NONE = 0,
2566         TPACPI_VIDEO_570,       /* 570 */
2567         TPACPI_VIDEO_770,       /* 600e/x, 770e, 770x */
2568         TPACPI_VIDEO_NEW,       /* all others */
2569 };
2570
2571 enum {  /* video status flags, based on VIDEO_570 */
2572         TP_ACPI_VIDEO_S_LCD = 0x01,     /* LCD output enabled */
2573         TP_ACPI_VIDEO_S_CRT = 0x02,     /* CRT output enabled */
2574         TP_ACPI_VIDEO_S_DVI = 0x08,     /* DVI output enabled */
2575 };
2576
2577 enum {  /* TPACPI_VIDEO_570 constants */
2578         TP_ACPI_VIDEO_570_PHSCMD = 0x87,        /* unknown magic constant :( */
2579         TP_ACPI_VIDEO_570_PHSMASK = 0x03,       /* PHS bits that map to
2580                                                  * video_status_flags */
2581         TP_ACPI_VIDEO_570_PHS2CMD = 0x8b,       /* unknown magic constant :( */
2582         TP_ACPI_VIDEO_570_PHS2SET = 0x80,       /* unknown magic constant :( */
2583 };
2584
2585 static enum video_access_mode video_supported;
2586 static int video_orig_autosw;
2587
2588 static int video_autosw_get(void);
2589 static int video_autosw_set(int enable);
2590
2591 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",   /* 570 */
2592            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
2593            "\\_SB.PCI0.VID0",   /* 770e */
2594            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
2595            "\\_SB.PCI0.AGP.VID",        /* all others */
2596            );                           /* R30, R31 */
2597
2598 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");       /* G41 */
2599
2600 static int __init video_init(struct ibm_init_struct *iibm)
2601 {
2602         int ivga;
2603
2604         vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2605
2606         TPACPI_ACPIHANDLE_INIT(vid);
2607         TPACPI_ACPIHANDLE_INIT(vid2);
2608
2609         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2610                 /* G41, assume IVGA doesn't change */
2611                 vid_handle = vid2_handle;
2612
2613         if (!vid_handle)
2614                 /* video switching not supported on R30, R31 */
2615                 video_supported = TPACPI_VIDEO_NONE;
2616         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2617                 /* 570 */
2618                 video_supported = TPACPI_VIDEO_570;
2619         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2620                 /* 600e/x, 770e, 770x */
2621                 video_supported = TPACPI_VIDEO_770;
2622         else
2623                 /* all others */
2624                 video_supported = TPACPI_VIDEO_NEW;
2625
2626         vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2627                 str_supported(video_supported != TPACPI_VIDEO_NONE),
2628                 video_supported);
2629
2630         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2631 }
2632
2633 static void video_exit(void)
2634 {
2635         dbg_printk(TPACPI_DBG_EXIT,
2636                    "restoring original video autoswitch mode\n");
2637         if (video_autosw_set(video_orig_autosw))
2638                 printk(TPACPI_ERR "error while trying to restore original "
2639                         "video autoswitch mode\n");
2640 }
2641
2642 static int video_outputsw_get(void)
2643 {
2644         int status = 0;
2645         int i;
2646
2647         switch (video_supported) {
2648         case TPACPI_VIDEO_570:
2649                 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2650                                  TP_ACPI_VIDEO_570_PHSCMD))
2651                         return -EIO;
2652                 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2653                 break;
2654         case TPACPI_VIDEO_770:
2655                 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2656                         return -EIO;
2657                 if (i)
2658                         status |= TP_ACPI_VIDEO_S_LCD;
2659                 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2660                         return -EIO;
2661                 if (i)
2662                         status |= TP_ACPI_VIDEO_S_CRT;
2663                 break;
2664         case TPACPI_VIDEO_NEW:
2665                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2666                     !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2667                         return -EIO;
2668                 if (i)
2669                         status |= TP_ACPI_VIDEO_S_CRT;
2670
2671                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2672                     !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2673                         return -EIO;
2674                 if (i)
2675                         status |= TP_ACPI_VIDEO_S_LCD;
2676                 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2677                         return -EIO;
2678                 if (i)
2679                         status |= TP_ACPI_VIDEO_S_DVI;
2680                 break;
2681         default:
2682                 return -ENOSYS;
2683         }
2684
2685         return status;
2686 }
2687
2688 static int video_outputsw_set(int status)
2689 {
2690         int autosw;
2691         int res = 0;
2692
2693         switch (video_supported) {
2694         case TPACPI_VIDEO_570:
2695                 res = acpi_evalf(NULL, NULL,
2696                                  "\\_SB.PHS2", "vdd",
2697                                  TP_ACPI_VIDEO_570_PHS2CMD,
2698                                  status | TP_ACPI_VIDEO_570_PHS2SET);
2699                 break;
2700         case TPACPI_VIDEO_770:
2701                 autosw = video_autosw_get();
2702                 if (autosw < 0)
2703                         return autosw;
2704
2705                 res = video_autosw_set(1);
2706                 if (res)
2707                         return res;
2708                 res = acpi_evalf(vid_handle, NULL,
2709                                  "ASWT", "vdd", status * 0x100, 0);
2710                 if (!autosw && video_autosw_set(autosw)) {
2711                         printk(TPACPI_ERR
2712                                "video auto-switch left enabled due to error\n");
2713                         return -EIO;
2714                 }
2715                 break;
2716         case TPACPI_VIDEO_NEW:
2717                 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2718                       acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2719                 break;
2720         default:
2721                 return -ENOSYS;
2722         }
2723
2724         return (res)? 0 : -EIO;
2725 }
2726
2727 static int video_autosw_get(void)
2728 {
2729         int autosw = 0;
2730
2731         switch (video_supported) {
2732         case TPACPI_VIDEO_570:
2733                 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2734                         return -EIO;
2735                 break;
2736         case TPACPI_VIDEO_770:
2737         case TPACPI_VIDEO_NEW:
2738                 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2739                         return -EIO;
2740                 break;
2741         default:
2742                 return -ENOSYS;
2743         }
2744
2745         return autosw & 1;
2746 }
2747
2748 static int video_autosw_set(int enable)
2749 {
2750         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2751                 return -EIO;
2752         return 0;
2753 }
2754
2755 static int video_outputsw_cycle(void)
2756 {
2757         int autosw = video_autosw_get();
2758         int res;
2759
2760         if (autosw < 0)
2761                 return autosw;
2762
2763         switch (video_supported) {
2764         case TPACPI_VIDEO_570:
2765                 res = video_autosw_set(1);
2766                 if (res)
2767                         return res;
2768                 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2769                 break;
2770         case TPACPI_VIDEO_770:
2771         case TPACPI_VIDEO_NEW:
2772                 res = video_autosw_set(1);
2773                 if (res)
2774                         return res;
2775                 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2776                 break;
2777         default:
2778                 return -ENOSYS;
2779         }
2780         if (!autosw && video_autosw_set(autosw)) {
2781                 printk(TPACPI_ERR
2782                        "video auto-switch left enabled due to error\n");
2783                 return -EIO;
2784         }
2785
2786         return (res)? 0 : -EIO;
2787 }
2788
2789 static int video_expand_toggle(void)
2790 {
2791         switch (video_supported) {
2792         case TPACPI_VIDEO_570:
2793                 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2794                         0 : -EIO;
2795         case TPACPI_VIDEO_770:
2796                 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2797                         0 : -EIO;
2798         case TPACPI_VIDEO_NEW:
2799                 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2800                         0 : -EIO;
2801         default:
2802                 return -ENOSYS;
2803         }
2804         /* not reached */
2805 }
2806
2807 static int video_read(char *p)
2808 {
2809         int status, autosw;
2810         int len = 0;
2811
2812         if (video_supported == TPACPI_VIDEO_NONE) {
2813                 len += sprintf(p + len, "status:\t\tnot supported\n");
2814                 return len;
2815         }
2816
2817         status = video_outputsw_get();
2818         if (status < 0)
2819                 return status;
2820
2821         autosw = video_autosw_get();
2822         if (autosw < 0)
2823                 return autosw;
2824
2825         len += sprintf(p + len, "status:\t\tsupported\n");
2826         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2827         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2828         if (video_supported == TPACPI_VIDEO_NEW)
2829                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2830         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2831         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2832         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2833         if (video_supported == TPACPI_VIDEO_NEW)
2834                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2835         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2836         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2837
2838         return len;
2839 }
2840
2841 static int video_write(char *buf)
2842 {
2843         char *cmd;
2844         int enable, disable, status;
2845         int res;
2846
2847         if (video_supported == TPACPI_VIDEO_NONE)
2848                 return -ENODEV;
2849
2850         enable = 0;
2851         disable = 0;
2852
2853         while ((cmd = next_cmd(&buf))) {
2854                 if (strlencmp(cmd, "lcd_enable") == 0) {
2855                         enable |= TP_ACPI_VIDEO_S_LCD;
2856                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2857                         disable |= TP_ACPI_VIDEO_S_LCD;
2858                 } else if (strlencmp(cmd, "crt_enable") == 0) {
2859                         enable |= TP_ACPI_VIDEO_S_CRT;
2860                 } else if (strlencmp(cmd, "crt_disable") == 0) {
2861                         disable |= TP_ACPI_VIDEO_S_CRT;
2862                 } else if (video_supported == TPACPI_VIDEO_NEW &&
2863                            strlencmp(cmd, "dvi_enable") == 0) {
2864                         enable |= TP_ACPI_VIDEO_S_DVI;
2865                 } else if (video_supported == TPACPI_VIDEO_NEW &&
2866                            strlencmp(cmd, "dvi_disable") == 0) {
2867                         disable |= TP_ACPI_VIDEO_S_DVI;
2868                 } else if (strlencmp(cmd, "auto_enable") == 0) {
2869                         res = video_autosw_set(1);
2870                         if (res)
2871                                 return res;
2872                 } else if (strlencmp(cmd, "auto_disable") == 0) {
2873                         res = video_autosw_set(0);
2874                         if (res)
2875                                 return res;
2876                 } else if (strlencmp(cmd, "video_switch") == 0) {
2877                         res = video_outputsw_cycle();
2878                         if (res)
2879                                 return res;
2880                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2881                         res = video_expand_toggle();
2882                         if (res)
2883                                 return res;
2884                 } else
2885                         return -EINVAL;
2886         }
2887
2888         if (enable || disable) {
2889                 status = video_outputsw_get();
2890                 if (status < 0)
2891                         return status;
2892                 res = video_outputsw_set((status & ~disable) | enable);
2893                 if (res)
2894                         return res;
2895         }
2896
2897         return 0;
2898 }
2899
2900 static struct ibm_struct video_driver_data = {
2901         .name = "video",
2902         .read = video_read,
2903         .write = video_write,
2904         .exit = video_exit,
2905 };
2906
2907 /*************************************************************************
2908  * Light (thinklight) subdriver
2909  */
2910
2911 TPACPI_HANDLE(lght, root, "\\LGHT");    /* A21e, A2xm/p, T20-22, X20-21 */
2912 TPACPI_HANDLE(ledb, ec, "LEDB");                /* G4x */
2913
2914 static int __init light_init(struct ibm_init_struct *iibm)
2915 {
2916         vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2917
2918         TPACPI_ACPIHANDLE_INIT(ledb);
2919         TPACPI_ACPIHANDLE_INIT(lght);
2920         TPACPI_ACPIHANDLE_INIT(cmos);
2921
2922         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2923         tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2924
2925         if (tp_features.light)
2926                 /* light status not supported on
2927                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2928                 tp_features.light_status =
2929                         acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2930
2931         vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2932                 str_supported(tp_features.light));
2933
2934         return (tp_features.light)? 0 : 1;
2935 }
2936
2937 static int light_read(char *p)
2938 {
2939         int len = 0;
2940         int status = 0;
2941
2942         if (!tp_features.light) {
2943                 len += sprintf(p + len, "status:\t\tnot supported\n");
2944         } else if (!tp_features.light_status) {
2945                 len += sprintf(p + len, "status:\t\tunknown\n");
2946                 len += sprintf(p + len, "commands:\ton, off\n");
2947         } else {
2948                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2949                         return -EIO;
2950                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2951                 len += sprintf(p + len, "commands:\ton, off\n");
2952         }
2953
2954         return len;
2955 }
2956
2957 static int light_write(char *buf)
2958 {
2959         int cmos_cmd, lght_cmd;
2960         char *cmd;
2961         int success;
2962
2963         if (!tp_features.light)
2964                 return -ENODEV;
2965
2966         while ((cmd = next_cmd(&buf))) {
2967                 if (strlencmp(cmd, "on") == 0) {
2968                         cmos_cmd = 0x0c;
2969                         lght_cmd = 1;
2970                 } else if (strlencmp(cmd, "off") == 0) {
2971                         cmos_cmd = 0x0d;
2972                         lght_cmd = 0;
2973                 } else
2974                         return -EINVAL;
2975
2976                 success = cmos_handle ?
2977                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2978                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2979                 if (!success)
2980                         return -EIO;
2981         }
2982
2983         return 0;
2984 }
2985
2986 static struct ibm_struct light_driver_data = {
2987         .name = "light",
2988         .read = light_read,
2989         .write = light_write,
2990 };
2991
2992 /*************************************************************************
2993  * Dock subdriver
2994  */
2995
2996 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2997
2998 static void dock_notify(struct ibm_struct *ibm, u32 event);
2999 static int dock_read(char *p);
3000 static int dock_write(char *buf);
3001
3002 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3003            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3004            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
3005            "\\_SB.PCI.ISA.SLCE",        /* 570 */
3006     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3007
3008 /* don't list other alternatives as we install a notify handler on the 570 */
3009 TPACPI_HANDLE(pci, root, "\\_SB.PCI");  /* 570 */
3010
3011 static const struct acpi_device_id ibm_pci_device_ids[] = {
3012         {PCI_ROOT_HID_STRING, 0},
3013         {"", 0},
3014 };
3015
3016 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3017         {
3018          .notify = dock_notify,
3019          .handle = &dock_handle,
3020          .type = ACPI_SYSTEM_NOTIFY,
3021         },
3022         {
3023         /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3024          * We just use it to get notifications of dock hotplug
3025          * in very old thinkpads */
3026          .hid = ibm_pci_device_ids,
3027          .notify = dock_notify,
3028          .handle = &pci_handle,
3029          .type = ACPI_SYSTEM_NOTIFY,
3030         },
3031 };
3032
3033 static struct ibm_struct dock_driver_data[2] = {
3034         {
3035          .name = "dock",
3036          .read = dock_read,
3037          .write = dock_write,
3038          .acpi = &ibm_dock_acpidriver[0],
3039         },
3040         {
3041          .name = "dock",
3042          .acpi = &ibm_dock_acpidriver[1],
3043         },
3044 };
3045
3046 #define dock_docked() (_sta(dock_handle) & 1)
3047
3048 static int __init dock_init(struct ibm_init_struct *iibm)
3049 {
3050         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3051
3052         TPACPI_ACPIHANDLE_INIT(dock);
3053
3054         vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3055                 str_supported(dock_handle != NULL));
3056
3057         return (dock_handle)? 0 : 1;
3058 }
3059
3060 static int __init dock_init2(struct ibm_init_struct *iibm)
3061 {
3062         int dock2_needed;
3063
3064         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3065
3066         if (dock_driver_data[0].flags.acpi_driver_registered &&
3067             dock_driver_data[0].flags.acpi_notify_installed) {
3068                 TPACPI_ACPIHANDLE_INIT(pci);
3069                 dock2_needed = (pci_handle != NULL);
3070                 vdbg_printk(TPACPI_DBG_INIT,
3071                             "dock PCI handler for the TP 570 is %s\n",
3072                             str_supported(dock2_needed));
3073         } else {
3074                 vdbg_printk(TPACPI_DBG_INIT,
3075                 "dock subdriver part 2 not required\n");
3076                 dock2_needed = 0;
3077         }
3078
3079         return (dock2_needed)? 0 : 1;
3080 }
3081
3082 static void dock_notify(struct ibm_struct *ibm, u32 event)
3083 {
3084         int docked = dock_docked();
3085         int pci = ibm->acpi->hid && ibm->acpi->device &&
3086                 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3087         int data;
3088
3089         if (event == 1 && !pci) /* 570 */
3090                 data = 1;       /* button */
3091         else if (event == 1 && pci)     /* 570 */
3092                 data = 3;       /* dock */
3093         else if (event == 3 && docked)
3094                 data = 1;       /* button */
3095         else if (event == 3 && !docked)
3096                 data = 2;       /* undock */
3097         else if (event == 0 && docked)
3098                 data = 3;       /* dock */
3099         else {
3100                 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3101                        event, _sta(dock_handle));
3102                 data = 0;       /* unknown */
3103         }
3104         acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3105         acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3106                                           ibm->acpi->device->dev.bus_id,
3107                                           event, data);
3108 }
3109
3110 static int dock_read(char *p)
3111 {
3112         int len = 0;
3113         int docked = dock_docked();
3114
3115         if (!dock_handle)
3116                 len += sprintf(p + len, "status:\t\tnot supported\n");
3117         else if (!docked)
3118                 len += sprintf(p + len, "status:\t\tundocked\n");
3119         else {
3120                 len += sprintf(p + len, "status:\t\tdocked\n");
3121                 len += sprintf(p + len, "commands:\tdock, undock\n");
3122         }
3123
3124         return len;
3125 }
3126
3127 static int dock_write(char *buf)
3128 {
3129         char *cmd;
3130
3131         if (!dock_docked())
3132                 return -ENODEV;
3133
3134         while ((cmd = next_cmd(&buf))) {
3135                 if (strlencmp(cmd, "undock") == 0) {
3136                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3137                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3138                                 return -EIO;
3139                 } else if (strlencmp(cmd, "dock") == 0) {
3140                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3141                                 return -EIO;
3142                 } else
3143                         return -EINVAL;
3144         }
3145
3146         return 0;
3147 }
3148
3149 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3150
3151 /*************************************************************************
3152  * Bay subdriver
3153  */
3154
3155 #ifdef CONFIG_THINKPAD_ACPI_BAY
3156
3157 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",     /* 570 */
3158            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3159            "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3160            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3161            );                           /* A21e, R30, R31 */
3162 TPACPI_HANDLE(bay_ej, bay, "_EJ3",      /* 600e/x, A2xm/p, A3x */
3163            "_EJ0",              /* all others */
3164            );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3165 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",  /* A3x, R32 */
3166            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3167            );                           /* all others */
3168 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3",    /* 600e/x, 770e, A3x */
3169            "_EJ0",                      /* 770x */
3170            );                           /* all others */
3171
3172 static int __init bay_init(struct ibm_init_struct *iibm)
3173 {
3174         vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3175
3176         TPACPI_ACPIHANDLE_INIT(bay);
3177         if (bay_handle)
3178                 TPACPI_ACPIHANDLE_INIT(bay_ej);
3179         TPACPI_ACPIHANDLE_INIT(bay2);
3180         if (bay2_handle)
3181                 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3182
3183         tp_features.bay_status = bay_handle &&
3184                 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3185         tp_features.bay_status2 = bay2_handle &&
3186                 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3187
3188         tp_features.bay_eject = bay_handle && bay_ej_handle &&
3189                 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3190         tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3191                 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3192
3193         vdbg_printk(TPACPI_DBG_INIT,
3194                 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3195                 str_supported(tp_features.bay_status),
3196                 str_supported(tp_features.bay_eject),
3197                 str_supported(tp_features.bay_status2),
3198                 str_supported(tp_features.bay_eject2));
3199
3200         return (tp_features.bay_status || tp_features.bay_eject ||
3201                 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3202 }
3203
3204 static void bay_notify(struct ibm_struct *ibm, u32 event)
3205 {
3206         acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3207         acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3208                                           ibm->acpi->device->dev.bus_id,
3209                                           event, 0);
3210 }
3211
3212 #define bay_occupied(b) (_sta(b##_handle) & 1)
3213
3214 static int bay_read(char *p)
3215 {
3216         int len = 0;
3217         int occupied = bay_occupied(bay);
3218         int occupied2 = bay_occupied(bay2);
3219         int eject, eject2;
3220
3221         len += sprintf(p + len, "status:\t\t%s\n",
3222                 tp_features.bay_status ?
3223                         (occupied ? "occupied" : "unoccupied") :
3224                                 "not supported");
3225         if (tp_features.bay_status2)
3226                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3227                                "occupied" : "unoccupied");
3228
3229         eject = tp_features.bay_eject && occupied;
3230         eject2 = tp_features.bay_eject2 && occupied2;
3231
3232         if (eject && eject2)
3233                 len += sprintf(p + len, "commands:\teject, eject2\n");
3234         else if (eject)
3235                 len += sprintf(p + len, "commands:\teject\n");
3236         else if (eject2)
3237                 len += sprintf(p + len, "commands:\teject2\n");
3238
3239         return len;
3240 }
3241
3242 static int bay_write(char *buf)
3243 {
3244         char *cmd;
3245
3246         if (!tp_features.bay_eject && !tp_features.bay_eject2)
3247                 return -ENODEV;
3248
3249         while ((cmd = next_cmd(&buf))) {
3250                 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3251                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3252                                 return -EIO;
3253                 } else if (tp_features.bay_eject2 &&
3254                            strlencmp(cmd, "eject2") == 0) {
3255                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3256                                 return -EIO;
3257                 } else
3258                         return -EINVAL;
3259         }
3260
3261         return 0;
3262 }
3263
3264 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3265         .notify = bay_notify,
3266         .handle = &bay_handle,
3267         .type = ACPI_SYSTEM_NOTIFY,
3268 };
3269
3270 static struct ibm_struct bay_driver_data = {
3271         .name = "bay",
3272         .read = bay_read,
3273         .write = bay_write,
3274         .acpi = &ibm_bay_acpidriver,
3275 };
3276
3277 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3278
3279 /*************************************************************************
3280  * CMOS subdriver
3281  */
3282
3283 /* sysfs cmos_command -------------------------------------------------- */
3284 static ssize_t cmos_command_store(struct device *dev,
3285                             struct device_attribute *attr,
3286                             const char *buf, size_t count)
3287 {
3288         unsigned long cmos_cmd;
3289         int res;
3290
3291         if (parse_strtoul(buf, 21, &cmos_cmd))
3292                 return -EINVAL;
3293
3294         res = issue_thinkpad_cmos_command(cmos_cmd);
3295         return (res)? res : count;
3296 }
3297
3298 static struct device_attribute dev_attr_cmos_command =
3299         __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3300
3301 /* --------------------------------------------------------------------- */
3302
3303 static int __init cmos_init(struct ibm_init_struct *iibm)
3304 {
3305         int res;
3306
3307         vdbg_printk(TPACPI_DBG_INIT,
3308                 "initializing cmos commands subdriver\n");
3309
3310         TPACPI_ACPIHANDLE_INIT(cmos);
3311
3312         vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3313                 str_supported(cmos_handle != NULL));
3314
3315         res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3316         if (res)
3317                 return res;
3318
3319         return (cmos_handle)? 0 : 1;
3320 }
3321
3322 static void cmos_exit(void)
3323 {
3324         device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3325 }
3326
3327 static int cmos_read(char *p)
3328 {
3329         int len = 0;
3330
3331         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3332            R30, R31, T20-22, X20-21 */
3333         if (!cmos_handle)
3334                 len += sprintf(p + len, "status:\t\tnot supported\n");
3335         else {
3336                 len += sprintf(p + len, "status:\t\tsupported\n");
3337                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3338         }
3339
3340         return len;
3341 }
3342
3343 static int cmos_write(char *buf)
3344 {
3345         char *cmd;
3346         int cmos_cmd, res;
3347
3348         while ((cmd = next_cmd(&buf))) {
3349                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3350                     cmos_cmd >= 0 && cmos_cmd <= 21) {
3351                         /* cmos_cmd set */
3352                 } else
3353                         return -EINVAL;
3354
3355                 res = issue_thinkpad_cmos_command(cmos_cmd);
3356                 if (res)
3357                         return res;
3358         }
3359
3360         return 0;
3361 }
3362
3363 static struct ibm_struct cmos_driver_data = {
3364         .name = "cmos",
3365         .read = cmos_read,
3366         .write = cmos_write,
3367         .exit = cmos_exit,
3368 };
3369
3370 /*************************************************************************
3371  * LED subdriver
3372  */
3373
3374 enum led_access_mode {
3375         TPACPI_LED_NONE = 0,
3376         TPACPI_LED_570, /* 570 */
3377         TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3378         TPACPI_LED_NEW, /* all others */
3379 };
3380
3381 enum {  /* For TPACPI_LED_OLD */
3382         TPACPI_LED_EC_HLCL = 0x0c,      /* EC reg to get led to power on */
3383         TPACPI_LED_EC_HLBL = 0x0d,      /* EC reg to blink a lit led */
3384         TPACPI_LED_EC_HLMS = 0x0e,      /* EC reg to select led to command */
3385 };
3386
3387 static enum led_access_mode led_supported;
3388
3389 TPACPI_HANDLE(led, ec, "SLED",  /* 570 */
3390            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3391                                 /* T20-22, X20-21 */
3392            "LED",               /* all others */
3393            );                   /* R30, R31 */
3394
3395 static int __init led_init(struct ibm_init_struct *iibm)
3396 {
3397         vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3398
3399         TPACPI_ACPIHANDLE_INIT(led);
3400
3401         if (!led_handle)
3402                 /* led not supported on R30, R31 */
3403                 led_supported = TPACPI_LED_NONE;
3404         else if (strlencmp(led_path, "SLED") == 0)
3405                 /* 570 */
3406                 led_supported = TPACPI_LED_570;
3407         else if (strlencmp(led_path, "SYSL") == 0)
3408                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3409                 led_supported = TPACPI_LED_OLD;
3410         else
3411                 /* all others */
3412                 led_supported = TPACPI_LED_NEW;
3413
3414         vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3415                 str_supported(led_supported), led_supported);
3416
3417         return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3418 }
3419
3420 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3421
3422 static int led_read(char *p)
3423 {
3424         int len = 0;
3425
3426         if (!led_supported) {
3427                 len += sprintf(p + len, "status:\t\tnot supported\n");
3428                 return len;
3429         }
3430         len += sprintf(p + len, "status:\t\tsupported\n");
3431
3432         if (led_supported == TPACPI_LED_570) {
3433                 /* 570 */
3434                 int i, status;
3435                 for (i = 0; i < 8; i++) {
3436                         if (!acpi_evalf(ec_handle,
3437                                         &status, "GLED", "dd", 1 << i))
3438                                 return -EIO;
3439                         len += sprintf(p + len, "%d:\t\t%s\n",
3440                                        i, led_status(status));
3441                 }
3442         }
3443
3444         len += sprintf(p + len, "commands:\t"
3445                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3446
3447         return len;
3448 }
3449
3450 /* off, on, blink */
3451 static const int led_sled_arg1[] = { 0, 1, 3 };
3452 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
3453 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
3454 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3455
3456 static int led_write(char *buf)
3457 {
3458         char *cmd;
3459         int led, ind, ret;
3460
3461         if (!led_supported)
3462                 return -ENODEV;
3463
3464         while ((cmd = next_cmd(&buf))) {
3465                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3466                         return -EINVAL;
3467
3468                 if (strstr(cmd, "off")) {
3469                         ind = 0;
3470                 } else if (strstr(cmd, "on")) {
3471                         ind = 1;
3472                 } else if (strstr(cmd, "blink")) {
3473                         ind = 2;
3474                 } else
3475                         return -EINVAL;
3476
3477                 if (led_supported == TPACPI_LED_570) {
3478                         /* 570 */
3479                         led = 1 << led;
3480                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3481                                         led, led_sled_arg1[ind]))
3482                                 return -EIO;
3483                 } else if (led_supported == TPACPI_LED_OLD) {
3484                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3485                         led = 1 << led;
3486                         ret = ec_write(TPACPI_LED_EC_HLMS, led);
3487                         if (ret >= 0)
3488                                 ret = ec_write(TPACPI_LED_EC_HLBL,
3489                                                 led * led_exp_hlbl[ind]);
3490                         if (ret >= 0)
3491                                 ret = ec_write(TPACPI_LED_EC_HLCL,
3492                                                 led * led_exp_hlcl[ind]);
3493                         if (ret < 0)
3494                                 return ret;
3495                 } else {
3496                         /* all others */
3497                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3498                                         led, led_led_arg1[ind]))
3499                                 return -EIO;
3500                 }
3501         }
3502
3503         return 0;
3504 }
3505
3506 static struct ibm_struct led_driver_data = {
3507         .name = "led",
3508         .read = led_read,
3509         .write = led_write,
3510 };
3511
3512 /*************************************************************************
3513  * Beep subdriver
3514  */
3515
3516 TPACPI_HANDLE(beep, ec, "BEEP");        /* all except R30, R31 */
3517
3518 static int __init beep_init(struct ibm_init_struct *iibm)
3519 {
3520         vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3521
3522         TPACPI_ACPIHANDLE_INIT(beep);
3523
3524         vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3525                 str_supported(beep_handle != NULL));
3526
3527         return (beep_handle)? 0 : 1;
3528 }
3529
3530 static int beep_read(char *p)
3531 {
3532         int len = 0;
3533
3534         if (!beep_handle)
3535                 len += sprintf(p + len, "status:\t\tnot supported\n");
3536         else {
3537                 len += sprintf(p + len, "status:\t\tsupported\n");
3538                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3539         }
3540
3541         return len;
3542 }
3543
3544 static int beep_write(char *buf)
3545 {
3546         char *cmd;
3547         int beep_cmd;
3548
3549         if (!beep_handle)
3550                 return -ENODEV;
3551
3552         while ((cmd = next_cmd(&buf))) {
3553                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3554                     beep_cmd >= 0 && beep_cmd <= 17) {
3555                         /* beep_cmd set */
3556                 } else
3557                         return -EINVAL;
3558                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3559                         return -EIO;
3560         }
3561
3562         return 0;
3563 }
3564
3565 static struct ibm_struct beep_driver_data = {
3566         .name = "beep",
3567         .read = beep_read,
3568         .write = beep_write,
3569 };
3570
3571 /*************************************************************************
3572  * Thermal subdriver
3573  */
3574
3575 enum thermal_access_mode {
3576         TPACPI_THERMAL_NONE = 0,        /* No thermal support */
3577         TPACPI_THERMAL_ACPI_TMP07,      /* Use ACPI TMP0-7 */
3578         TPACPI_THERMAL_ACPI_UPDT,       /* Use ACPI TMP0-7 with UPDT */
3579         TPACPI_THERMAL_TPEC_8,          /* Use ACPI EC regs, 8 sensors */
3580         TPACPI_THERMAL_TPEC_16,         /* Use ACPI EC regs, 16 sensors */
3581 };
3582
3583 enum { /* TPACPI_THERMAL_TPEC_* */
3584         TP_EC_THERMAL_TMP0 = 0x78,      /* ACPI EC regs TMP 0..7 */
3585         TP_EC_THERMAL_TMP8 = 0xC0,      /* ACPI EC regs TMP 8..15 */
3586         TP_EC_THERMAL_TMP_NA = -128,    /* ACPI EC sensor not available */
3587 };
3588
3589 #define TPACPI_MAX_THERMAL_SENSORS 16   /* Max thermal sensors supported */
3590 struct ibm_thermal_sensors_struct {
3591         s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3592 };
3593
3594 static enum thermal_access_mode thermal_read_mode;
3595
3596 /* idx is zero-based */
3597 static int thermal_get_sensor(int idx, s32 *value)
3598 {
3599         int t;
3600         s8 tmp;
3601         char tmpi[5];
3602
3603         t = TP_EC_THERMAL_TMP0;
3604
3605         switch (thermal_read_mode) {
3606 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3607         case TPACPI_THERMAL_TPEC_16:
3608                 if (idx >= 8 && idx <= 15) {
3609                         t = TP_EC_THERMAL_TMP8;
3610                         idx -= 8;
3611                 }
3612                 /* fallthrough */
3613 #endif
3614         case TPACPI_THERMAL_TPEC_8:
3615                 if (idx <= 7) {
3616                         if (!acpi_ec_read(t + idx, &tmp))
3617                                 return -EIO;
3618                         *value = tmp * 1000;
3619                         return 0;
3620                 }
3621                 break;
3622
3623         case TPACPI_THERMAL_ACPI_UPDT:
3624                 if (idx <= 7) {
3625                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3626                         if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3627                                 return -EIO;
3628                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3629                                 return -EIO;
3630                         *value = (t - 2732) * 100;
3631                         return 0;
3632                 }
3633                 break;
3634
3635         case TPACPI_THERMAL_ACPI_TMP07:
3636                 if (idx <= 7) {
3637                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3638                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3639                                 return -EIO;
3640                         if (t > 127 || t < -127)
3641                                 t = TP_EC_THERMAL_TMP_NA;
3642                         *value = t * 1000;
3643                         return 0;
3644                 }
3645                 break;
3646
3647         case TPACPI_THERMAL_NONE:
3648         default:
3649                 return -ENOSYS;
3650         }
3651
3652         return -EINVAL;
3653 }
3654
3655 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3656 {
3657         int res, i;
3658         int n;
3659
3660         n = 8;
3661         i = 0;
3662
3663         if (!s)
3664                 return -EINVAL;
3665
3666         if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3667                 n = 16;
3668
3669         for (i = 0 ; i < n; i++) {
3670                 res = thermal_get_sensor(i, &s->temp[i]);
3671                 if (res)
3672                         return res;
3673         }
3674
3675         return n;
3676 }
3677
3678 /* sysfs temp##_input -------------------------------------------------- */
3679
3680 static ssize_t thermal_temp_input_show(struct device *dev,
3681                            struct device_attribute *attr,
3682                            char *buf)
3683 {
3684         struct sensor_device_attribute *sensor_attr =
3685                                         to_sensor_dev_attr(attr);
3686         int idx = sensor_attr->index;
3687         s32 value;
3688         int res;
3689
3690         res = thermal_get_sensor(idx, &value);
3691         if (res)
3692                 return res;
3693         if (value == TP_EC_THERMAL_TMP_NA * 1000)
3694                 return -ENXIO;
3695
3696         return snprintf(buf, PAGE_SIZE, "%d\n", value);
3697 }
3698
3699 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3700          SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3701                      thermal_temp_input_show, NULL, _idxB)
3702
3703 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3704         THERMAL_SENSOR_ATTR_TEMP(1, 0),
3705         THERMAL_SENSOR_ATTR_TEMP(2, 1),
3706         THERMAL_SENSOR_ATTR_TEMP(3, 2),
3707         THERMAL_SENSOR_ATTR_TEMP(4, 3),
3708         THERMAL_SENSOR_ATTR_TEMP(5, 4),
3709         THERMAL_SENSOR_ATTR_TEMP(6, 5),
3710         THERMAL_SENSOR_ATTR_TEMP(7, 6),
3711         THERMAL_SENSOR_ATTR_TEMP(8, 7),
3712         THERMAL_SENSOR_ATTR_TEMP(9, 8),
3713         THERMAL_SENSOR_ATTR_TEMP(10, 9),
3714         THERMAL_SENSOR_ATTR_TEMP(11, 10),
3715         THERMAL_SENSOR_ATTR_TEMP(12, 11),
3716         THERMAL_SENSOR_ATTR_TEMP(13, 12),
3717         THERMAL_SENSOR_ATTR_TEMP(14, 13),
3718         THERMAL_SENSOR_ATTR_TEMP(15, 14),
3719         THERMAL_SENSOR_ATTR_TEMP(16, 15),
3720 };
3721
3722 #define THERMAL_ATTRS(X) \
3723         &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3724
3725 static struct attribute *thermal_temp_input_attr[] = {
3726         THERMAL_ATTRS(8),
3727         THERMAL_ATTRS(9),
3728         THERMAL_ATTRS(10),
3729         THERMAL_ATTRS(11),
3730         THERMAL_ATTRS(12),
3731         THERMAL_ATTRS(13),
3732         THERMAL_ATTRS(14),
3733         THERMAL_ATTRS(15),
3734         THERMAL_ATTRS(0),
3735         THERMAL_ATTRS(1),
3736         THERMAL_ATTRS(2),
3737         THERMAL_ATTRS(3),
3738         THERMAL_ATTRS(4),
3739         THERMAL_ATTRS(5),
3740         THERMAL_ATTRS(6),
3741         THERMAL_ATTRS(7),
3742         NULL
3743 };
3744
3745 static const struct attribute_group thermal_temp_input16_group = {
3746         .attrs = thermal_temp_input_attr
3747 };
3748
3749 static const struct attribute_group thermal_temp_input8_group = {
3750         .attrs = &thermal_temp_input_attr[8]
3751 };
3752
3753 #undef THERMAL_SENSOR_ATTR_TEMP
3754 #undef THERMAL_ATTRS
3755
3756 /* --------------------------------------------------------------------- */
3757
3758 static int __init thermal_init(struct ibm_init_struct *iibm)
3759 {
3760         u8 t, ta1, ta2;
3761         int i;
3762         int acpi_tmp7;
3763         int res;
3764
3765         vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3766
3767         acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3768
3769         if (thinkpad_id.ec_model) {
3770                 /*
3771                  * Direct EC access mode: sensors at registers
3772                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
3773                  * non-implemented, thermal sensors return 0x80 when
3774                  * not available
3775                  */
3776
3777                 ta1 = ta2 = 0;
3778                 for (i = 0; i < 8; i++) {
3779                         if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3780                                 ta1 |= t;
3781                         } else {
3782                                 ta1 = 0;
3783                                 break;
3784                         }
3785                         if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3786                                 ta2 |= t;
3787                         } else {
3788                                 ta1 = 0;
3789                                 break;
3790                         }
3791                 }
3792                 if (ta1 == 0) {
3793                         /* This is sheer paranoia, but we handle it anyway */
3794                         if (acpi_tmp7) {
3795                                 printk(TPACPI_ERR
3796                                        "ThinkPad ACPI EC access misbehaving, "
3797                                        "falling back to ACPI TMPx access "
3798                                        "mode\n");
3799                                 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3800                         } else {
3801                                 printk(TPACPI_ERR
3802                                        "ThinkPad ACPI EC access misbehaving, "
3803                                        "disabling thermal sensors access\n");
3804                                 thermal_read_mode = TPACPI_THERMAL_NONE;
3805                         }
3806                 } else {
3807                         thermal_read_mode =
3808                             (ta2 != 0) ?
3809                             TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3810                 }
3811         } else if (acpi_tmp7) {
3812                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3813                         /* 600e/x, 770e, 770x */
3814                         thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3815                 } else {
3816                         /* Standard ACPI TMPx access, max 8 sensors */
3817                         thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3818                 }
3819         } else {
3820                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3821                 thermal_read_mode = TPACPI_THERMAL_NONE;
3822         }
3823
3824         vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3825                 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3826                 thermal_read_mode);
3827
3828         switch (thermal_read_mode) {
3829         case TPACPI_THERMAL_TPEC_16:
3830                 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3831                                 &thermal_temp_input16_group);
3832                 if (res)
3833                         return res;
3834                 break;
3835         case TPACPI_THERMAL_TPEC_8:
3836         case TPACPI_THERMAL_ACPI_TMP07:
3837         case TPACPI_THERMAL_ACPI_UPDT:
3838                 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3839                                 &thermal_temp_input8_group);
3840                 if (res)
3841                         return res;
3842                 break;
3843         case TPACPI_THERMAL_NONE:
3844         default:
3845                 return 1;
3846         }
3847
3848         return 0;
3849 }
3850
3851 static void thermal_exit(void)
3852 {
3853         switch (thermal_read_mode) {
3854         case TPACPI_THERMAL_TPEC_16:
3855                 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3856                                    &thermal_temp_input16_group);
3857                 break;
3858         case TPACPI_THERMAL_TPEC_8:
3859         case TPACPI_THERMAL_ACPI_TMP07:
3860         case TPACPI_THERMAL_ACPI_UPDT:
3861                 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3862                                    &thermal_temp_input16_group);
3863                 break;
3864         case TPACPI_THERMAL_NONE:
3865         default:
3866                 break;
3867         }
3868 }
3869
3870 static int thermal_read(char *p)
3871 {
3872         int len = 0;
3873         int n, i;
3874         struct ibm_thermal_sensors_struct t;
3875
3876         n = thermal_get_sensors(&t);
3877         if (unlikely(n < 0))
3878                 return n;
3879
3880         len += sprintf(p + len, "temperatures:\t");
3881
3882         if (n > 0) {
3883                 for (i = 0; i < (n - 1); i++)
3884                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3885                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3886         } else
3887                 len += sprintf(p + len, "not supported\n");
3888
3889         return len;
3890 }
3891
3892 static struct ibm_struct thermal_driver_data = {
3893         .name = "thermal",
3894         .read = thermal_read,
3895         .exit = thermal_exit,
3896 };
3897
3898 /*************************************************************************
3899  * EC Dump subdriver
3900  */
3901
3902 static u8 ecdump_regs[256];
3903
3904 static int ecdump_read(char *p)
3905 {
3906         int len = 0;
3907         int i, j;
3908         u8 v;
3909
3910         len += sprintf(p + len, "EC      "
3911                        " +00 +01 +02 +03 +04 +05 +06 +07"
3912                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3913         for (i = 0; i < 256; i += 16) {
3914                 len += sprintf(p + len, "EC 0x%02x:", i);
3915                 for (j = 0; j < 16; j++) {
3916                         if (!acpi_ec_read(i + j, &v))
3917                                 break;
3918                         if (v != ecdump_regs[i + j])
3919                                 len += sprintf(p + len, " *%02x", v);
3920                         else
3921                                 len += sprintf(p + len, "  %02x", v);
3922                         ecdump_regs[i + j] = v;
3923                 }
3924                 len += sprintf(p + len, "\n");
3925                 if (j != 16)
3926                         break;
3927         }
3928
3929         /* These are way too dangerous to advertise openly... */
3930 #if 0
3931         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3932                        " (<offset> is 00-ff, <value> is 00-ff)\n");
3933         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
3934                        " (<offset> is 00-ff, <value> is 0-255)\n");
3935 #endif
3936         return len;
3937 }
3938
3939 static int ecdump_write(char *buf)
3940 {
3941         char *cmd;
3942         int i, v;
3943
3944         while ((cmd = next_cmd(&buf))) {
3945                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3946                         /* i and v set */
3947                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3948                         /* i and v set */
3949                 } else
3950                         return -EINVAL;
3951                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3952                         if (!acpi_ec_write(i, v))
3953                                 return -EIO;
3954                 } else
3955                         return -EINVAL;
3956         }
3957
3958         return 0;
3959 }
3960
3961 static struct ibm_struct ecdump_driver_data = {
3962         .name = "ecdump",
3963         .read = ecdump_read,
3964         .write = ecdump_write,
3965         .flags.experimental = 1,
3966 };
3967
3968 /*************************************************************************
3969  * Backlight/brightness subdriver
3970  */
3971
3972 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3973
3974 static struct backlight_device *ibm_backlight_device;
3975 static int brightness_offset = 0x31;
3976 static int brightness_mode;
3977 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3978
3979 static struct mutex brightness_mutex;
3980
3981 /*
3982  * ThinkPads can read brightness from two places: EC 0x31, or
3983  * CMOS NVRAM byte 0x5E, bits 0-3.
3984  */
3985 static int brightness_get(struct backlight_device *bd)
3986 {
3987         u8 lec = 0, lcmos = 0, level = 0;
3988
3989         if (brightness_mode & 1) {
3990                 if (!acpi_ec_read(brightness_offset, &lec))
3991                         return -EIO;
3992                 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3993                 level = lec;
3994         };
3995         if (brightness_mode & 2) {
3996                 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3997                          & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3998                         >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3999                 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4000                 level = lcmos;
4001         }
4002
4003         if (brightness_mode == 3 && lec != lcmos) {
4004                 printk(TPACPI_ERR
4005                         "CMOS NVRAM (%u) and EC (%u) do not agree "
4006                         "on display brightness level\n",
4007                         (unsigned int) lcmos,
4008                         (unsigned int) lec);
4009                 return -EIO;
4010         }
4011
4012         return level;
4013 }
4014
4015 /* May return EINTR which can always be mapped to ERESTARTSYS */
4016 static int brightness_set(int value)
4017 {
4018         int cmos_cmd, inc, i, res;
4019         int current_value;
4020
4021         if (value > ((tp_features.bright_16levels)? 15 : 7))
4022                 return -EINVAL;
4023
4024         res = mutex_lock_interruptible(&brightness_mutex);
4025         if (res < 0)
4026                 return res;
4027
4028         current_value = brightness_get(NULL);
4029         if (current_value < 0) {
4030                 res = current_value;
4031                 goto errout;
4032         }
4033
4034         cmos_cmd = value > current_value ?
4035                         TP_CMOS_BRIGHTNESS_UP :
4036                         TP_CMOS_BRIGHTNESS_DOWN;
4037         inc = (value > current_value)? 1 : -1;
4038
4039         res = 0;
4040         for (i = current_value; i != value; i += inc) {
4041                 if ((brightness_mode & 2) &&
4042                     issue_thinkpad_cmos_command(cmos_cmd)) {
4043                         res = -EIO;
4044                         goto errout;
4045                 }
4046                 if ((brightness_mode & 1) &&
4047                     !acpi_ec_write(brightness_offset, i + inc)) {
4048                         res = -EIO;
4049                         goto errout;;
4050                 }
4051         }
4052
4053 errout:
4054         mutex_unlock(&brightness_mutex);
4055         return res;
4056 }
4057
4058 /* sysfs backlight class ----------------------------------------------- */
4059
4060 static int brightness_update_status(struct backlight_device *bd)
4061 {
4062         /* it is the backlight class's job (caller) to handle
4063          * EINTR and other errors properly */
4064         return brightness_set(
4065                 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4066                  bd->props.power == FB_BLANK_UNBLANK) ?
4067                                 bd->props.brightness : 0);
4068 }
4069
4070 static struct backlight_ops ibm_backlight_data = {
4071         .get_brightness = brightness_get,
4072         .update_status  = brightness_update_status,
4073 };
4074
4075 /* --------------------------------------------------------------------- */
4076
4077 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4078 {
4079         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4080         union acpi_object *obj;
4081         int rc;
4082
4083         if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4084                 obj = (union acpi_object *)buffer.pointer;
4085                 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4086                         printk(TPACPI_ERR "Unknown BCLL data, "
4087                                "please report this to %s\n", TPACPI_MAIL);
4088                         rc = 0;
4089                 } else {
4090                         rc = obj->package.count;
4091                 }
4092         } else {
4093                 return 0;
4094         }
4095
4096         kfree(buffer.pointer);
4097         return rc;
4098 }
4099
4100 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4101                                         void *context, void **rv)
4102 {
4103         char name[ACPI_PATH_SEGMENT_LENGTH];
4104         struct acpi_buffer buffer = { sizeof(name), &name };
4105
4106         if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4107             !strncmp("BCLL", name, sizeof(name) - 1)) {
4108                 if (tpacpi_query_bcll_levels(handle) == 16) {
4109                         *rv = handle;
4110                         return AE_CTRL_TERMINATE;
4111                 } else {
4112                         return AE_OK;
4113                 }
4114         } else {
4115                 return AE_OK;
4116         }
4117 }
4118
4119 static int __init brightness_check_levels(void)
4120 {
4121         int status;
4122         void *found_node = NULL;
4123
4124         if (!vid_handle) {
4125                 TPACPI_ACPIHANDLE_INIT(vid);
4126         }
4127         if (!vid_handle)
4128                 return 0;
4129
4130         /* Search for a BCLL package with 16 levels */
4131         status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4132                                         brightness_find_bcll, NULL,
4133                                         &found_node);
4134
4135         return (ACPI_SUCCESS(status) && found_node != NULL);
4136 }
4137
4138 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4139                                         void *context, void **rv)
4140 {
4141         char name[ACPI_PATH_SEGMENT_LENGTH];
4142         struct acpi_buffer buffer = { sizeof(name), &name };
4143
4144         if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4145             !strncmp("_BCL", name, sizeof(name) - 1)) {
4146                 *rv = handle;
4147                 return AE_CTRL_TERMINATE;
4148         } else {
4149                 return AE_OK;
4150         }
4151 }
4152
4153 static int __init brightness_check_std_acpi_support(void)
4154 {
4155         int status;
4156         void *found_node = NULL;
4157
4158         if (!vid_handle) {
4159                 TPACPI_ACPIHANDLE_INIT(vid);
4160         }
4161         if (!vid_handle)
4162                 return 0;
4163
4164         /* Search for a _BCL method, but don't execute it */
4165         status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4166                                      brightness_find_bcl, NULL, &found_node);
4167
4168         return (ACPI_SUCCESS(status) && found_node != NULL);
4169 }
4170
4171 static int __init brightness_init(struct ibm_init_struct *iibm)
4172 {
4173         int b;
4174
4175         vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4176
4177         mutex_init(&brightness_mutex);
4178
4179         if (!brightness_enable) {
4180                 dbg_printk(TPACPI_DBG_INIT,
4181                            "brightness support disabled by "
4182                            "module parameter\n");
4183                 return 1;
4184         } else if (brightness_enable > 1) {
4185                 if (brightness_check_std_acpi_support()) {
4186                         printk(TPACPI_NOTICE
4187                                "standard ACPI backlight interface "
4188                                "available, not loading native one...\n");
4189                         return 1;
4190                 }
4191         }
4192
4193         if (!brightness_mode) {
4194                 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4195                         brightness_mode = 2;
4196                 else
4197                         brightness_mode = 3;
4198
4199                 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4200                         brightness_mode);
4201         }
4202
4203         if (brightness_mode > 3)
4204                 return -EINVAL;
4205
4206         tp_features.bright_16levels =
4207                         thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4208                         brightness_check_levels();
4209
4210         b = brightness_get(NULL);
4211         if (b < 0)
4212                 return 1;
4213
4214         if (tp_features.bright_16levels)
4215                 printk(TPACPI_INFO
4216                        "detected a 16-level brightness capable ThinkPad\n");
4217
4218         ibm_backlight_device = backlight_device_register(
4219                                         TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4220                                         &ibm_backlight_data);
4221         if (IS_ERR(ibm_backlight_device)) {
4222                 printk(TPACPI_ERR "Could not register backlight device\n");
4223                 return PTR_ERR(ibm_backlight_device);
4224         }
4225         vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4226
4227         ibm_backlight_device->props.max_brightness =
4228                                 (tp_features.bright_16levels)? 15 : 7;
4229         ibm_backlight_device->props.brightness = b;
4230         backlight_update_status(ibm_backlight_device);
4231
4232         return 0;
4233 }
4234
4235 static void brightness_exit(void)
4236 {
4237         if (ibm_backlight_device) {
4238                 vdbg_printk(TPACPI_DBG_EXIT,
4239                             "calling backlight_device_unregister()\n");
4240                 backlight_device_unregister(ibm_backlight_device);
4241                 ibm_backlight_device = NULL;
4242         }
4243 }
4244
4245 static int brightness_read(char *p)
4246 {
4247         int len = 0;
4248         int level;
4249
4250         level = brightness_get(NULL);
4251         if (level < 0) {
4252                 len += sprintf(p + len, "level:\t\tunreadable\n");
4253         } else {
4254                 len += sprintf(p + len, "level:\t\t%d\n", level);
4255                 len += sprintf(p + len, "commands:\tup, down\n");
4256                 len += sprintf(p + len, "commands:\tlevel <level>"
4257                                " (<level> is 0-%d)\n",
4258                                (tp_features.bright_16levels) ? 15 : 7);
4259         }
4260
4261         return len;
4262 }
4263
4264 static int brightness_write(char *buf)
4265 {
4266         int level;
4267         int rc;
4268         char *cmd;
4269         int max_level = (tp_features.bright_16levels) ? 15 : 7;
4270
4271         level = brightness_get(NULL);
4272         if (level < 0)
4273                 return level;
4274
4275         while ((cmd = next_cmd(&buf))) {
4276                 if (strlencmp(cmd, "up") == 0) {
4277                         if (level < max_level)
4278                                 level++;
4279                 } else if (strlencmp(cmd, "down") == 0) {
4280                         if (level > 0)
4281                                 level--;
4282                 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4283                            level >= 0 && level <= max_level) {
4284                         /* new level set */
4285                 } else
4286                         return -EINVAL;
4287         }
4288
4289         /*
4290          * Now we know what the final level should be, so we try to set it.
4291          * Doing it this way makes the syscall restartable in case of EINTR
4292          */
4293         rc = brightness_set(level);
4294         return (rc == -EINTR)? ERESTARTSYS : rc;
4295 }
4296
4297 static struct ibm_struct brightness_driver_data = {
4298         .name = "brightness",
4299         .read = brightness_read,
4300         .write = brightness_write,
4301         .exit = brightness_exit,
4302 };
4303
4304 /*************************************************************************
4305  * Volume subdriver
4306  */
4307
4308 static int volume_offset = 0x30;
4309
4310 static int volume_read(char *p)
4311 {
4312         int len = 0;
4313         u8 level;
4314
4315         if (!acpi_ec_read(volume_offset, &level)) {
4316                 len += sprintf(p + len, "level:\t\tunreadable\n");
4317         } else {
4318                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4319                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4320                 len += sprintf(p + len, "commands:\tup, down, mute\n");
4321                 len += sprintf(p + len, "commands:\tlevel <level>"
4322                                " (<level> is 0-15)\n");
4323         }
4324
4325         return len;
4326 }
4327
4328 static int volume_write(char *buf)
4329 {
4330         int cmos_cmd, inc, i;
4331         u8 level, mute;
4332         int new_level, new_mute;
4333         char *cmd;
4334
4335         while ((cmd = next_cmd(&buf))) {
4336                 if (!acpi_ec_read(volume_offset, &level))
4337                         return -EIO;
4338                 new_mute = mute = level & 0x40;
4339                 new_level = level = level & 0xf;
4340
4341                 if (strlencmp(cmd, "up") == 0) {
4342                         if (mute)
4343                                 new_mute = 0;
4344                         else
4345                                 new_level = level == 15 ? 15 : level + 1;
4346                 } else if (strlencmp(cmd, "down") == 0) {
4347                         if (mute)
4348                                 new_mute = 0;
4349                         else
4350                                 new_level = level == 0 ? 0 : level - 1;
4351                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4352                            new_level >= 0 && new_level <= 15) {
4353                         /* new_level set */
4354                 } else if (strlencmp(cmd, "mute") == 0) {
4355                         new_mute = 0x40;
4356                 } else
4357                         return -EINVAL;
4358
4359                 if (new_level != level) {
4360                         /* mute doesn't change */
4361
4362                         cmos_cmd = (new_level > level) ?
4363                                         TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4364                         inc = new_level > level ? 1 : -1;
4365
4366                         if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4367                                      !acpi_ec_write(volume_offset, level)))
4368                                 return -EIO;
4369
4370                         for (i = level; i != new_level; i += inc)
4371                                 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4372                                     !acpi_ec_write(volume_offset, i + inc))
4373                                         return -EIO;
4374
4375                         if (mute &&
4376                             (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4377                              !acpi_ec_write(volume_offset, new_level + mute))) {
4378                                 return -EIO;
4379                         }
4380                 }
4381
4382                 if (new_mute != mute) {
4383                         /* level doesn't change */
4384
4385                         cmos_cmd = (new_mute) ?
4386                                    TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4387
4388                         if (issue_thinkpad_cmos_command(cmos_cmd) ||
4389                             !acpi_ec_write(volume_offset, level + new_mute))
4390                                 return -EIO;
4391                 }
4392         }
4393
4394         return 0;
4395 }
4396
4397 static struct ibm_struct volume_driver_data = {
4398         .name = "volume",
4399         .read = volume_read,
4400         .write = volume_write,
4401 };
4402
4403 /*************************************************************************
4404  * Fan subdriver
4405  */
4406
4407 /*
4408  * FAN ACCESS MODES
4409  *
4410  * TPACPI_FAN_RD_ACPI_GFAN:
4411  *      ACPI GFAN method: returns fan level
4412  *
4413  *      see TPACPI_FAN_WR_ACPI_SFAN
4414  *      EC 0x2f (HFSP) not available if GFAN exists
4415  *
4416  * TPACPI_FAN_WR_ACPI_SFAN:
4417  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4418  *
4419  *      EC 0x2f (HFSP) might be available *for reading*, but do not use
4420  *      it for writing.
4421  *
4422  * TPACPI_FAN_WR_TPEC:
4423  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode
4424  *      Supported on almost all ThinkPads
4425  *
4426  *      Fan speed changes of any sort (including those caused by the
4427  *      disengaged mode) are usually done slowly by the firmware as the
4428  *      maximum ammount of fan duty cycle change per second seems to be
4429  *      limited.
4430  *
4431  *      Reading is not available if GFAN exists.
4432  *      Writing is not available if SFAN exists.
4433  *
4434  *      Bits
4435  *       7      automatic mode engaged;
4436  *              (default operation mode of the ThinkPad)
4437  *              fan level is ignored in this mode.
4438  *       6      full speed mode (takes precedence over bit 7);
4439  *              not available on all thinkpads.  May disable
4440  *              the tachometer while the fan controller ramps up
4441  *              the speed (which can take up to a few *minutes*).
4442  *              Speeds up fan to 100% duty-cycle, which is far above
4443  *              the standard RPM levels.  It is not impossible that
4444  *              it could cause hardware damage.
4445  *      5-3     unused in some models.  Extra bits for fan level
4446  *              in others, but still useless as all values above
4447  *              7 map to the same speed as level 7 in these models.
4448  *      2-0     fan level (0..7 usually)
4449  *                      0x00 = stop
4450  *                      0x07 = max (set when temperatures critical)
4451  *              Some ThinkPads may have other levels, see
4452  *              TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4453  *
4454  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4455  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4456  *      does so, its initial value is meaningless (0x07).
4457  *
4458  *      For firmware bugs, refer to:
4459  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4460  *
4461  *      ----
4462  *
4463  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4464  *      Main fan tachometer reading (in RPM)
4465  *
4466  *      This register is present on all ThinkPads with a new-style EC, and
4467  *      it is known not to be present on the A21m/e, and T22, as there is
4468  *      something else in offset 0x84 according to the ACPI DSDT.  Other
4469  *      ThinkPads from this same time period (and earlier) probably lack the
4470  *      tachometer as well.
4471  *
4472  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4473  *      was never fixed by IBM to report the EC firmware version string
4474  *      probably support the tachometer (like the early X models), so
4475  *      detecting it is quite hard.  We need more data to know for sure.
4476  *
4477  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4478  *      might result.
4479  *
4480  *      FIRMWARE BUG: may go stale while the EC is switching to full speed
4481  *      mode.
4482  *
4483  *      For firmware bugs, refer to:
4484  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4485  *
4486  * TPACPI_FAN_WR_ACPI_FANS:
4487  *      ThinkPad X31, X40, X41.  Not available in the X60.
4488  *
4489  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
4490  *      high speed.  ACPI DSDT seems to map these three speeds to levels
4491  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4492  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4493  *
4494  *      The speeds are stored on handles
4495  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4496  *
4497  *      There are three default speed sets, acessible as handles:
4498  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4499  *
4500  *      ACPI DSDT switches which set is in use depending on various
4501  *      factors.
4502  *
4503  *      TPACPI_FAN_WR_TPEC is also available and should be used to
4504  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
4505  *      but the ACPI tables just mention level 7.
4506  */
4507
4508 enum {                                  /* Fan control constants */
4509         fan_status_offset = 0x2f,       /* EC register 0x2f */
4510         fan_rpm_offset = 0x84,          /* EC register 0x84: LSB, 0x85 MSB (RPM)
4511                                          * 0x84 must be read before 0x85 */
4512
4513         TP_EC_FAN_FULLSPEED = 0x40,     /* EC fan mode: full speed */
4514         TP_EC_FAN_AUTO      = 0x80,     /* EC fan mode: auto fan control */
4515
4516         TPACPI_FAN_LAST_LEVEL = 0x100,  /* Use cached last-seen fan level */
4517 };
4518
4519 enum fan_status_access_mode {
4520         TPACPI_FAN_NONE = 0,            /* No fan status or control */
4521         TPACPI_FAN_RD_ACPI_GFAN,        /* Use ACPI GFAN */
4522         TPACPI_FAN_RD_TPEC,             /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4523 };
4524
4525 enum fan_control_access_mode {
4526         TPACPI_FAN_WR_NONE = 0,         /* No fan control */
4527         TPACPI_FAN_WR_ACPI_SFAN,        /* Use ACPI SFAN */
4528         TPACPI_FAN_WR_TPEC,             /* Use ACPI EC reg 0x2f */
4529         TPACPI_FAN_WR_ACPI_FANS,        /* Use ACPI FANS and EC reg 0x2f */
4530 };
4531
4532 enum fan_control_commands {
4533         TPACPI_FAN_CMD_SPEED    = 0x0001,       /* speed command */
4534         TPACPI_FAN_CMD_LEVEL    = 0x0002,       /* level command  */
4535         TPACPI_FAN_CMD_ENABLE   = 0x0004,       /* enable/disable cmd,
4536                                                  * and also watchdog cmd */
4537 };
4538
4539 static int fan_control_allowed;
4540
4541 static enum fan_status_access_mode fan_status_access_mode;
4542 static enum fan_control_access_mode fan_control_access_mode;
4543 static enum fan_control_commands fan_control_commands;
4544
4545 static u8 fan_control_initial_status;
4546 static u8 fan_control_desired_level;
4547 static int fan_watchdog_maxinterval;
4548
4549 static struct mutex fan_mutex;
4550
4551 static void fan_watchdog_fire(struct work_struct *ignored);
4552 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4553
4554 TPACPI_HANDLE(fans, ec, "FANS");        /* X31, X40, X41 */
4555 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4556            "\\FSPD",            /* 600e/x, 770e, 770x */
4557            );                   /* all others */
4558 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4559            "JFNS",              /* 770x-JL */
4560            );                   /* all others */
4561
4562 /*
4563  * Call with fan_mutex held
4564  */
4565 static void fan_update_desired_level(u8 status)
4566 {
4567         if ((status &
4568              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4569                 if (status > 7)
4570                         fan_control_desired_level = 7;
4571                 else
4572                         fan_control_desired_level = status;
4573         }
4574 }
4575
4576 static int fan_get_status(u8 *status)
4577 {
4578         u8 s;
4579
4580         /* TODO:
4581          * Add TPACPI_FAN_RD_ACPI_FANS ? */
4582
4583         switch (fan_status_access_mode) {
4584         case TPACPI_FAN_RD_ACPI_GFAN:
4585                 /* 570, 600e/x, 770e, 770x */
4586
4587                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4588                         return -EIO;
4589
4590                 if (likely(status))
4591                         *status = s & 0x07;
4592
4593                 break;
4594
4595         case TPACPI_FAN_RD_TPEC:
4596                 /* all except 570, 600e/x, 770e, 770x */
4597                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4598                         return -EIO;
4599
4600                 if (likely(status))
4601                         *status = s;
4602
4603                 break;
4604
4605         default:
4606                 return -ENXIO;
4607         }
4608
4609         return 0;
4610 }
4611
4612 static int fan_get_status_safe(u8 *status)
4613 {
4614         int rc;
4615         u8 s;
4616
4617         if (mutex_lock_interruptible(&fan_mutex))
4618                 return -ERESTARTSYS;
4619         rc = fan_get_status(&s);
4620         if (!rc)
4621                 fan_update_desired_level(s);
4622         mutex_unlock(&fan_mutex);
4623
4624         if (status)
4625                 *status = s;
4626
4627         return rc;
4628 }
4629
4630 static int fan_get_speed(unsigned int *speed)
4631 {
4632         u8 hi, lo;
4633
4634         switch (fan_status_access_mode) {
4635         case TPACPI_FAN_RD_TPEC:
4636                 /* all except 570, 600e/x, 770e, 770x */
4637                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4638                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4639                         return -EIO;
4640
4641                 if (likely(speed))
4642                         *speed = (hi << 8) | lo;
4643
4644                 break;
4645
4646         default:
4647                 return -ENXIO;
4648         }
4649
4650         return 0;
4651 }
4652
4653 static int fan_set_level(int level)
4654 {
4655         if (!fan_control_allowed)
4656                 return -EPERM;
4657
4658         switch (fan_control_access_mode) {
4659         case TPACPI_FAN_WR_ACPI_SFAN:
4660                 if (level >= 0 && level <= 7) {
4661                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4662                                 return -EIO;
4663                 } else
4664                         return -EINVAL;
4665                 break;
4666
4667         case TPACPI_FAN_WR_ACPI_FANS:
4668         case TPACPI_FAN_WR_TPEC:
4669                 if ((level != TP_EC_FAN_AUTO) &&
4670                     (level != TP_EC_FAN_FULLSPEED) &&
4671                     ((level < 0) || (level > 7)))
4672                         return -EINVAL;
4673
4674                 /* safety net should the EC not support AUTO
4675                  * or FULLSPEED mode bits and just ignore them */
4676                 if (level & TP_EC_FAN_FULLSPEED)
4677                         level |= 7;     /* safety min speed 7 */
4678                 else if (level & TP_EC_FAN_FULLSPEED)
4679                         level |= 4;     /* safety min speed 4 */
4680
4681                 if (!acpi_ec_write(fan_status_offset, level))
4682                         return -EIO;
4683                 else
4684                         tp_features.fan_ctrl_status_undef = 0;
4685                 break;
4686
4687         default:
4688                 return -ENXIO;
4689         }
4690         return 0;
4691 }
4692
4693 static int fan_set_level_safe(int level)
4694 {
4695         int rc;
4696
4697         if (!fan_control_allowed)
4698                 return -EPERM;
4699
4700         if (mutex_lock_interruptible(&fan_mutex))
4701                 return -ERESTARTSYS;
4702
4703         if (level == TPACPI_FAN_LAST_LEVEL)
4704                 level = fan_control_desired_level;
4705
4706         rc = fan_set_level(level);
4707         if (!rc)
4708                 fan_update_desired_level(level);
4709
4710         mutex_unlock(&fan_mutex);
4711         return rc;
4712 }
4713
4714 static int fan_set_enable(void)
4715 {
4716         u8 s;
4717         int rc;
4718
4719         if (!fan_control_allowed)
4720                 return -EPERM;
4721
4722         if (mutex_lock_interruptible(&fan_mutex))
4723                 return -ERESTARTSYS;
4724
4725         switch (fan_control_access_mode) {
4726         case TPACPI_FAN_WR_ACPI_FANS:
4727         case TPACPI_FAN_WR_TPEC:
4728                 rc = fan_get_status(&s);
4729                 if (rc < 0)
4730                         break;
4731
4732                 /* Don't go out of emergency fan mode */
4733                 if (s != 7) {
4734                         s &= 0x07;
4735                         s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4736                 }
4737
4738                 if (!acpi_ec_write(fan_status_offset, s))
4739                         rc = -EIO;
4740                 else {
4741                         tp_features.fan_ctrl_status_undef = 0;
4742                         rc = 0;
4743                 }
4744                 break;
4745
4746         case TPACPI_FAN_WR_ACPI_SFAN:
4747                 rc = fan_get_status(&s);
4748                 if (rc < 0)
4749                         break;
4750
4751                 s &= 0x07;
4752
4753                 /* Set fan to at least level 4 */
4754                 s |= 4;
4755
4756                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4757                         rc = -EIO;
4758                 else
4759                         rc = 0;
4760                 break;
4761
4762         default:
4763                 rc = -ENXIO;
4764         }
4765
4766         mutex_unlock(&fan_mutex);
4767         return rc;
4768 }
4769
4770 static int fan_set_disable(void)
4771 {
4772         int rc;
4773
4774         if (!fan_control_allowed)
4775                 return -EPERM;
4776
4777         if (mutex_lock_interruptible(&fan_mutex))
4778                 return -ERESTARTSYS;
4779
4780         rc = 0;
4781         switch (fan_control_access_mode) {
4782         case TPACPI_FAN_WR_ACPI_FANS:
4783         case TPACPI_FAN_WR_TPEC:
4784                 if (!acpi_ec_write(fan_status_offset, 0x00))
4785                         rc = -EIO;
4786                 else {
4787                         fan_control_desired_level = 0;
4788                         tp_features.fan_ctrl_status_undef = 0;
4789                 }
4790                 break;
4791
4792         case TPACPI_FAN_WR_ACPI_SFAN:
4793                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4794                         rc = -EIO;
4795                 else
4796                         fan_control_desired_level = 0;
4797                 break;
4798
4799         default:
4800                 rc = -ENXIO;
4801         }
4802
4803
4804         mutex_unlock(&fan_mutex);
4805         return rc;
4806 }
4807
4808 static int fan_set_speed(int speed)
4809 {
4810         int rc;
4811
4812         if (!fan_control_allowed)
4813                 return -EPERM;
4814
4815         if (mutex_lock_interruptible(&fan_mutex))
4816                 return -ERESTARTSYS;
4817
4818         rc = 0;
4819         switch (fan_control_access_mode) {
4820         case TPACPI_FAN_WR_ACPI_FANS:
4821                 if (speed >= 0 && speed <= 65535) {
4822                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4823                                         speed, speed, speed))
4824                                 rc = -EIO;
4825                 } else
4826                         rc = -EINVAL;
4827                 break;
4828
4829         default:
4830                 rc = -ENXIO;
4831         }
4832
4833         mutex_unlock(&fan_mutex);
4834         return rc;
4835 }
4836
4837 static void fan_watchdog_reset(void)
4838 {
4839         static int fan_watchdog_active;
4840
4841         if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4842                 return;
4843
4844         if (fan_watchdog_active)
4845                 cancel_delayed_work(&fan_watchdog_task);
4846
4847         if (fan_watchdog_maxinterval > 0 &&
4848             tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4849                 fan_watchdog_active = 1;
4850                 if (!schedule_delayed_work(&fan_watchdog_task,
4851                                 msecs_to_jiffies(fan_watchdog_maxinterval
4852                                                  * 1000))) {
4853                         printk(TPACPI_ERR
4854                                "failed to schedule the fan watchdog, "
4855                                "watchdog will not trigger\n");
4856                 }
4857         } else
4858                 fan_watchdog_active = 0;
4859 }
4860
4861 static void fan_watchdog_fire(struct work_struct *ignored)
4862 {
4863         int rc;
4864
4865         if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4866                 return;
4867
4868         printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4869         rc = fan_set_enable();
4870         if (rc < 0) {
4871                 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4872                         "will try again later...\n", -rc);
4873                 /* reschedule for later */
4874                 fan_watchdog_reset();
4875         }
4876 }
4877
4878 /*
4879  * SYSFS fan layout: hwmon compatible (device)
4880  *
4881  * pwm*_enable:
4882  *      0: "disengaged" mode
4883  *      1: manual mode
4884  *      2: native EC "auto" mode (recommended, hardware default)
4885  *
4886  * pwm*: set speed in manual mode, ignored otherwise.
4887  *      0 is level 0; 255 is level 7. Intermediate points done with linear
4888  *      interpolation.
4889  *
4890  * fan*_input: tachometer reading, RPM
4891  *
4892  *
4893  * SYSFS fan layout: extensions
4894  *
4895  * fan_watchdog (driver):
4896  *      fan watchdog interval in seconds, 0 disables (default), max 120
4897  */
4898
4899 /* sysfs fan pwm1_enable ----------------------------------------------- */
4900 static ssize_t fan_pwm1_enable_show(struct device *dev,
4901                                     struct device_attribute *attr,
4902                                     char *buf)
4903 {
4904         int res, mode;
4905         u8 status;
4906
4907         res = fan_get_status_safe(&status);
4908         if (res)
4909                 return res;
4910
4911         if (unlikely(tp_features.fan_ctrl_status_undef)) {
4912                 if (status != fan_control_initial_status) {
4913                         tp_features.fan_ctrl_status_undef = 0;
4914                 } else {
4915                         /* Return most likely status. In fact, it
4916                          * might be the only possible status */
4917                         status = TP_EC_FAN_AUTO;
4918                 }
4919         }
4920
4921         if (status & TP_EC_FAN_FULLSPEED) {
4922                 mode = 0;
4923         } else if (status & TP_EC_FAN_AUTO) {
4924                 mode = 2;
4925         } else
4926                 mode = 1;
4927
4928         return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4929 }
4930
4931 static ssize_t fan_pwm1_enable_store(struct device *dev,
4932                                      struct device_attribute *attr,
4933                                      const char *buf, size_t count)
4934 {
4935         unsigned long t;
4936         int res, level;
4937
4938         if (parse_strtoul(buf, 2, &t))
4939                 return -EINVAL;
4940
4941         switch (t) {
4942         case 0:
4943                 level = TP_EC_FAN_FULLSPEED;
4944                 break;
4945         case 1:
4946                 level = TPACPI_FAN_LAST_LEVEL;
4947                 break;
4948         case 2:
4949                 level = TP_EC_FAN_AUTO;
4950                 break;
4951         case 3:
4952                 /* reserved for software-controlled auto mode */
4953                 return -ENOSYS;
4954         default:
4955                 return -EINVAL;
4956         }
4957
4958         res = fan_set_level_safe(level);
4959         if (res == -ENXIO)
4960                 return -EINVAL;
4961         else if (res < 0)
4962                 return res;
4963
4964         fan_watchdog_reset();
4965
4966         return count;
4967 }
4968
4969 static struct device_attribute dev_attr_fan_pwm1_enable =
4970         __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4971                 fan_pwm1_enable_show, fan_pwm1_enable_store);
4972
4973 /* sysfs fan pwm1 ------------------------------------------------------ */
4974 static ssize_t fan_pwm1_show(struct device *dev,
4975                              struct device_attribute *attr,
4976                              char *buf)
4977 {
4978         int res;
4979         u8 status;
4980
4981         res = fan_get_status_safe(&status);
4982         if (res)
4983                 return res;
4984
4985         if (unlikely(tp_features.fan_ctrl_status_undef)) {
4986                 if (status != fan_control_initial_status) {
4987                         tp_features.fan_ctrl_status_undef = 0;
4988                 } else {
4989                         status = TP_EC_FAN_AUTO;
4990                 }
4991         }
4992
4993         if ((status &
4994              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4995                 status = fan_control_desired_level;
4996
4997         if (status > 7)
4998                 status = 7;
4999
5000         return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5001 }
5002
5003 static ssize_t fan_pwm1_store(struct device *dev,
5004                               struct device_attribute *attr,
5005                               const char *buf, size_t count)
5006 {
5007         unsigned long s;
5008         int rc;
5009         u8 status, newlevel;
5010
5011         if (parse_strtoul(buf, 255, &s))
5012                 return -EINVAL;
5013
5014         /* scale down from 0-255 to 0-7 */
5015         newlevel = (s >> 5) & 0x07;
5016
5017         if (mutex_lock_interruptible(&fan_mutex))
5018                 return -ERESTARTSYS;
5019
5020         rc = fan_get_status(&status);
5021         if (!rc && (status &
5022                     (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5023                 rc = fan_set_level(newlevel);
5024                 if (rc == -ENXIO)
5025                         rc = -EINVAL;
5026                 else if (!rc) {
5027                         fan_update_desired_level(newlevel);
5028                         fan_watchdog_reset();
5029                 }
5030         }
5031
5032         mutex_unlock(&fan_mutex);
5033         return (rc)? rc : count;
5034 }
5035
5036 static struct device_attribute dev_attr_fan_pwm1 =
5037         __ATTR(pwm1, S_IWUSR | S_IRUGO,
5038                 fan_pwm1_show, fan_pwm1_store);
5039
5040 /* sysfs fan fan1_input ------------------------------------------------ */
5041 static ssize_t fan_fan1_input_show(struct device *dev,
5042                            struct device_attribute *attr,
5043                            char *buf)
5044 {
5045         int res;
5046         unsigned int speed;
5047
5048         res = fan_get_speed(&speed);
5049         if (res < 0)
5050                 return res;
5051
5052         return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5053 }
5054
5055 static struct device_attribute dev_attr_fan_fan1_input =
5056         __ATTR(fan1_input, S_IRUGO,
5057                 fan_fan1_input_show, NULL);
5058
5059 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5060 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5061                                      char *buf)
5062 {
5063         return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5064 }
5065
5066 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5067                                       const char *buf, size_t count)
5068 {
5069         unsigned long t;
5070
5071         if (parse_strtoul(buf, 120, &t))
5072                 return -EINVAL;
5073
5074         if (!fan_control_allowed)
5075                 return -EPERM;
5076
5077         fan_watchdog_maxinterval = t;
5078         fan_watchdog_reset();
5079
5080         return count;
5081 }
5082
5083 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5084                 fan_fan_watchdog_show, fan_fan_watchdog_store);
5085
5086 /* --------------------------------------------------------------------- */
5087 static struct attribute *fan_attributes[] = {
5088         &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5089         &dev_attr_fan_fan1_input.attr,
5090         NULL
5091 };
5092
5093 static const struct attribute_group fan_attr_group = {
5094         .attrs = fan_attributes,
5095 };
5096
5097 static int __init fan_init(struct ibm_init_struct *iibm)
5098 {
5099         int rc;
5100
5101         vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5102
5103         mutex_init(&fan_mutex);
5104         fan_status_access_mode = TPACPI_FAN_NONE;
5105         fan_control_access_mode = TPACPI_FAN_WR_NONE;
5106         fan_control_commands = 0;
5107         fan_watchdog_maxinterval = 0;
5108         tp_features.fan_ctrl_status_undef = 0;
5109         fan_control_desired_level = 7;
5110
5111         TPACPI_ACPIHANDLE_INIT(fans);
5112         TPACPI_ACPIHANDLE_INIT(gfan);
5113         TPACPI_ACPIHANDLE_INIT(sfan);
5114
5115         if (gfan_handle) {
5116                 /* 570, 600e/x, 770e, 770x */
5117                 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5118         } else {
5119                 /* all other ThinkPads: note that even old-style
5120                  * ThinkPad ECs supports the fan control register */
5121                 if (likely(acpi_ec_read(fan_status_offset,
5122                                         &fan_control_initial_status))) {
5123                         fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5124
5125                         /* In some ThinkPads, neither the EC nor the ACPI
5126                          * DSDT initialize the fan status, and it ends up
5127                          * being set to 0x07 when it *could* be either
5128                          * 0x07 or 0x80.
5129                          *
5130                          * Enable for TP-1Y (T43), TP-78 (R51e),
5131                          * TP-76 (R52), TP-70 (T43, R52), which are known
5132                          * to be buggy. */
5133                         if (fan_control_initial_status == 0x07) {
5134                                 switch (thinkpad_id.ec_model) {
5135                                 case 0x5931: /* TP-1Y */
5136                                 case 0x3837: /* TP-78 */
5137                                 case 0x3637: /* TP-76 */
5138                                 case 0x3037: /* TP-70 */
5139                                         printk(TPACPI_NOTICE
5140                                                "fan_init: initial fan status "
5141                                                "is unknown, assuming it is "
5142                                                "in auto mode\n");
5143                                         tp_features.fan_ctrl_status_undef = 1;
5144                                         ;;
5145                                 }
5146                         }
5147                 } else {
5148                         printk(TPACPI_ERR
5149                                "ThinkPad ACPI EC access misbehaving, "
5150                                "fan status and control unavailable\n");
5151                         return 1;
5152                 }
5153         }
5154
5155         if (sfan_handle) {
5156                 /* 570, 770x-JL */
5157                 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5158                 fan_control_commands |=
5159                     TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5160         } else {
5161                 if (!gfan_handle) {
5162                         /* gfan without sfan means no fan control */
5163                         /* all other models implement TP EC 0x2f control */
5164
5165                         if (fans_handle) {
5166                                 /* X31, X40, X41 */
5167                                 fan_control_access_mode =
5168                                     TPACPI_FAN_WR_ACPI_FANS;
5169                                 fan_control_commands |=
5170                                     TPACPI_FAN_CMD_SPEED |
5171                                     TPACPI_FAN_CMD_LEVEL |
5172                                     TPACPI_FAN_CMD_ENABLE;
5173                         } else {
5174                                 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5175                                 fan_control_commands |=
5176                                     TPACPI_FAN_CMD_LEVEL |
5177                                     TPACPI_FAN_CMD_ENABLE;
5178                         }
5179                 }
5180         }
5181
5182         vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5183                 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5184                   fan_control_access_mode != TPACPI_FAN_WR_NONE),
5185                 fan_status_access_mode, fan_control_access_mode);
5186
5187         /* fan control master switch */
5188         if (!fan_control_allowed) {
5189                 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5190                 fan_control_commands = 0;
5191                 dbg_printk(TPACPI_DBG_INIT,
5192                            "fan control features disabled by parameter\n");
5193         }
5194
5195         /* update fan_control_desired_level */
5196         if (fan_status_access_mode != TPACPI_FAN_NONE)
5197                 fan_get_status_safe(NULL);
5198
5199         if (fan_status_access_mode != TPACPI_FAN_NONE ||
5200             fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5201                 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5202                                          &fan_attr_group);
5203                 if (!(rc < 0))
5204                         rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5205                                         &driver_attr_fan_watchdog);
5206                 if (rc < 0)
5207                         return rc;
5208                 return 0;
5209         } else
5210                 return 1;
5211 }
5212
5213 static void fan_exit(void)
5214 {
5215         vdbg_printk(TPACPI_DBG_EXIT,
5216                     "cancelling any pending fan watchdog tasks\n");
5217
5218         /* FIXME: can we really do this unconditionally? */
5219         sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5220         driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5221                            &driver_attr_fan_watchdog);
5222
5223         cancel_delayed_work(&fan_watchdog_task);
5224         flush_scheduled_work();
5225 }
5226
5227 static int fan_read(char *p)
5228 {
5229         int len = 0;
5230         int rc;
5231         u8 status;
5232         unsigned int speed = 0;
5233
5234         switch (fan_status_access_mode) {
5235         case TPACPI_FAN_RD_ACPI_GFAN:
5236                 /* 570, 600e/x, 770e, 770x */
5237                 rc = fan_get_status_safe(&status);
5238                 if (rc < 0)
5239                         return rc;
5240
5241                 len += sprintf(p + len, "status:\t\t%s\n"
5242                                "level:\t\t%d\n",
5243                                (status != 0) ? "enabled" : "disabled", status);
5244                 break;
5245
5246         case TPACPI_FAN_RD_TPEC:
5247                 /* all except 570, 600e/x, 770e, 770x */
5248                 rc = fan_get_status_safe(&status);
5249                 if (rc < 0)
5250                         return rc;
5251
5252                 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5253                         if (status != fan_control_initial_status)
5254                                 tp_features.fan_ctrl_status_undef = 0;
5255                         else
5256                                 /* Return most likely status. In fact, it
5257                                  * might be the only possible status */
5258                                 status = TP_EC_FAN_AUTO;
5259                 }
5260
5261                 len += sprintf(p + len, "status:\t\t%s\n",
5262                                (status != 0) ? "enabled" : "disabled");
5263
5264                 rc = fan_get_speed(&speed);
5265                 if (rc < 0)
5266                         return rc;
5267
5268                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5269
5270                 if (status & TP_EC_FAN_FULLSPEED)
5271                         /* Disengaged mode takes precedence */
5272                         len += sprintf(p + len, "level:\t\tdisengaged\n");
5273                 else if (status & TP_EC_FAN_AUTO)
5274                         len += sprintf(p + len, "level:\t\tauto\n");
5275                 else
5276                         len += sprintf(p + len, "level:\t\t%d\n", status);
5277                 break;
5278
5279         case TPACPI_FAN_NONE:
5280         default:
5281                 len += sprintf(p + len, "status:\t\tnot supported\n");
5282         }
5283
5284         if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5285                 len += sprintf(p + len, "commands:\tlevel <level>");
5286
5287                 switch (fan_control_access_mode) {
5288                 case TPACPI_FAN_WR_ACPI_SFAN:
5289                         len += sprintf(p + len, " (<level> is 0-7)\n");
5290                         break;
5291
5292                 default:
5293                         len += sprintf(p + len, " (<level> is 0-7, "
5294                                        "auto, disengaged, full-speed)\n");
5295                         break;
5296                 }
5297         }
5298
5299         if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5300                 len += sprintf(p + len, "commands:\tenable, disable\n"
5301                                "commands:\twatchdog <timeout> (<timeout> "
5302                                "is 0 (off), 1-120 (seconds))\n");
5303
5304         if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5305                 len += sprintf(p + len, "commands:\tspeed <speed>"
5306                                " (<speed> is 0-65535)\n");
5307
5308         return len;
5309 }
5310
5311 static int fan_write_cmd_level(const char *cmd, int *rc)
5312 {
5313         int level;
5314
5315         if (strlencmp(cmd, "level auto") == 0)
5316                 level = TP_EC_FAN_AUTO;
5317         else if ((strlencmp(cmd, "level disengaged") == 0) |
5318                         (strlencmp(cmd, "level full-speed") == 0))
5319                 level = TP_EC_FAN_FULLSPEED;
5320         else if (sscanf(cmd, "level %d", &level) != 1)
5321                 return 0;
5322
5323         *rc = fan_set_level_safe(level);
5324         if (*rc == -ENXIO)
5325                 printk(TPACPI_ERR "level command accepted for unsupported "
5326                        "access mode %d", fan_control_access_mode);
5327
5328         return 1;
5329 }
5330
5331 static int fan_write_cmd_enable(const char *cmd, int *rc)
5332 {
5333         if (strlencmp(cmd, "enable") != 0)
5334                 return 0;
5335
5336         *rc = fan_set_enable();
5337         if (*rc == -ENXIO)
5338                 printk(TPACPI_ERR "enable command accepted for unsupported "
5339                        "access mode %d", fan_control_access_mode);
5340
5341         return 1;
5342 }
5343
5344 static int fan_write_cmd_disable(const char *cmd, int *rc)
5345 {
5346         if (strlencmp(cmd, "disable") != 0)
5347                 return 0;
5348
5349         *rc = fan_set_disable();
5350         if (*rc == -ENXIO)
5351                 printk(TPACPI_ERR "disable command accepted for unsupported "
5352                        "access mode %d", fan_control_access_mode);
5353
5354         return 1;
5355 }
5356
5357 static int fan_write_cmd_speed(const char *cmd, int *rc)
5358 {
5359         int speed;
5360
5361         /* TODO:
5362          * Support speed <low> <medium> <high> ? */
5363
5364         if (sscanf(cmd, "speed %d", &speed) != 1)
5365                 return 0;
5366
5367         *rc = fan_set_speed(speed);
5368         if (*rc == -ENXIO)
5369                 printk(TPACPI_ERR "speed command accepted for unsupported "
5370                        "access mode %d", fan_control_access_mode);
5371
5372         return 1;
5373 }
5374
5375 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5376 {
5377         int interval;
5378
5379         if (sscanf(cmd, "watchdog %d", &interval) != 1)
5380                 return 0;
5381
5382         if (interval < 0 || interval > 120)
5383                 *rc = -EINVAL;
5384         else
5385                 fan_watchdog_maxinterval = interval;
5386
5387         return 1;
5388 }
5389
5390 static int fan_write(char *buf)
5391 {
5392         char *cmd;
5393         int rc = 0;
5394
5395         while (!rc && (cmd = next_cmd(&buf))) {
5396                 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5397                       fan_write_cmd_level(cmd, &rc)) &&
5398                     !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5399                       (fan_write_cmd_enable(cmd, &rc) ||
5400                        fan_write_cmd_disable(cmd, &rc) ||
5401                        fan_write_cmd_watchdog(cmd, &rc))) &&
5402                     !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5403                       fan_write_cmd_speed(cmd, &rc))
5404                     )
5405                         rc = -EINVAL;
5406                 else if (!rc)
5407                         fan_watchdog_reset();
5408         }
5409
5410         return rc;
5411 }
5412
5413 static struct ibm_struct fan_driver_data = {
5414         .name = "fan",
5415         .read = fan_read,
5416         .write = fan_write,
5417         .exit = fan_exit,
5418 };
5419
5420 /****************************************************************************
5421  ****************************************************************************
5422  *
5423  * Infrastructure
5424  *
5425  ****************************************************************************
5426  ****************************************************************************/
5427
5428 /* sysfs name ---------------------------------------------------------- */
5429 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5430                            struct device_attribute *attr,
5431                            char *buf)
5432 {
5433         return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5434 }
5435
5436 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5437         __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5438
5439 /* --------------------------------------------------------------------- */
5440
5441 /* /proc support */
5442 static struct proc_dir_entry *proc_dir;
5443
5444 /*
5445  * Module and infrastructure proble, init and exit handling
5446  */
5447
5448 static int force_load;
5449
5450 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5451 static const char * __init str_supported(int is_supported)
5452 {
5453         static char text_unsupported[] __initdata = "not supported";
5454
5455         return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5456 }
5457 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5458
5459 static void ibm_exit(struct ibm_struct *ibm)
5460 {
5461         dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5462
5463         list_del_init(&ibm->all_drivers);
5464
5465         if (ibm->flags.acpi_notify_installed) {
5466                 dbg_printk(TPACPI_DBG_EXIT,
5467                         "%s: acpi_remove_notify_handler\n", ibm->name);
5468                 BUG_ON(!ibm->acpi);
5469                 acpi_remove_notify_handler(*ibm->acpi->handle,
5470                                            ibm->acpi->type,
5471                                            dispatch_acpi_notify);
5472                 ibm->flags.acpi_notify_installed = 0;
5473                 ibm->flags.acpi_notify_installed = 0;
5474         }
5475
5476         if (ibm->flags.proc_created) {
5477                 dbg_printk(TPACPI_DBG_EXIT,
5478                         "%s: remove_proc_entry\n", ibm->name);
5479                 remove_proc_entry(ibm->name, proc_dir);
5480                 ibm->flags.proc_created = 0;
5481         }
5482
5483         if (ibm->flags.acpi_driver_registered) {
5484                 dbg_printk(TPACPI_DBG_EXIT,
5485                         "%s: acpi_bus_unregister_driver\n", ibm->name);
5486                 BUG_ON(!ibm->acpi);
5487                 acpi_bus_unregister_driver(ibm->acpi->driver);
5488                 kfree(ibm->acpi->driver);
5489                 ibm->acpi->driver = NULL;
5490                 ibm->flags.acpi_driver_registered = 0;
5491         }
5492
5493         if (ibm->flags.init_called && ibm->exit) {
5494                 ibm->exit();
5495                 ibm->flags.init_called = 0;
5496         }
5497
5498         dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5499 }
5500
5501 static int __init ibm_init(struct ibm_init_struct *iibm)
5502 {
5503         int ret;
5504         struct ibm_struct *ibm = iibm->data;
5505         struct proc_dir_entry *entry;
5506
5507         BUG_ON(ibm == NULL);
5508
5509         INIT_LIST_HEAD(&ibm->all_drivers);
5510
5511         if (ibm->flags.experimental && !experimental)
5512                 return 0;
5513
5514         dbg_printk(TPACPI_DBG_INIT,
5515                 "probing for %s\n", ibm->name);
5516
5517         if (iibm->init) {
5518                 ret = iibm->init(iibm);
5519                 if (ret > 0)
5520                         return 0;       /* probe failed */
5521                 if (ret)
5522                         return ret;
5523
5524                 ibm->flags.init_called = 1;
5525         }
5526
5527         if (ibm->acpi) {
5528                 if (ibm->acpi->hid) {
5529                         ret = register_tpacpi_subdriver(ibm);
5530                         if (ret)
5531                                 goto err_out;
5532                 }
5533
5534                 if (ibm->acpi->notify) {
5535                         ret = setup_acpi_notify(ibm);
5536                         if (ret == -ENODEV) {
5537                                 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5538                                         ibm->name);
5539                                 ret = 0;
5540                                 goto err_out;
5541                         }
5542                         if (ret < 0)
5543                                 goto err_out;
5544                 }
5545         }
5546
5547         dbg_printk(TPACPI_DBG_INIT,
5548                 "%s installed\n", ibm->name);
5549
5550         if (ibm->read) {
5551                 entry = create_proc_entry(ibm->name,
5552                                           S_IFREG | S_IRUGO | S_IWUSR,
5553                                           proc_dir);
5554                 if (!entry) {
5555                         printk(TPACPI_ERR "unable to create proc entry %s\n",
5556                                ibm->name);
5557                         ret = -ENODEV;
5558                         goto err_out;
5559                 }
5560                 entry->owner = THIS_MODULE;
5561                 entry->data = ibm;
5562                 entry->read_proc = &dispatch_procfs_read;
5563                 if (ibm->write)
5564                         entry->write_proc = &dispatch_procfs_write;
5565                 ibm->flags.proc_created = 1;
5566         }
5567
5568         list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5569
5570         return 0;
5571
5572 err_out:
5573         dbg_printk(TPACPI_DBG_INIT,
5574                 "%s: at error exit path with result %d\n",
5575                 ibm->name, ret);
5576
5577         ibm_exit(ibm);
5578         return (ret < 0)? ret : 0;
5579 }
5580
5581 /* Probing */
5582
5583 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5584 {
5585         const struct dmi_device *dev = NULL;
5586         char ec_fw_string[18];
5587
5588         if (!tp)
5589                 return;
5590
5591         memset(tp, 0, sizeof(*tp));
5592
5593         if (dmi_name_in_vendors("IBM"))
5594                 tp->vendor = PCI_VENDOR_ID_IBM;
5595         else if (dmi_name_in_vendors("LENOVO"))
5596                 tp->vendor = PCI_VENDOR_ID_LENOVO;
5597         else
5598                 return;
5599
5600         tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5601                                         GFP_KERNEL);
5602         if (!tp->bios_version_str)
5603                 return;
5604         tp->bios_model = tp->bios_version_str[0]
5605                          | (tp->bios_version_str[1] << 8);
5606
5607         /*
5608          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5609          * X32 or newer, all Z series;  Some models must have an
5610          * up-to-date BIOS or they will not be detected.
5611          *
5612          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5613          */
5614         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5615                 if (sscanf(dev->name,
5616                            "IBM ThinkPad Embedded Controller -[%17c",
5617                            ec_fw_string) == 1) {
5618                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5619                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5620
5621                         tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5622                         tp->ec_model = ec_fw_string[0]
5623                                         | (ec_fw_string[1] << 8);
5624                         break;
5625                 }
5626         }
5627
5628         tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5629                                         GFP_KERNEL);
5630         if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5631                 kfree(tp->model_str);
5632                 tp->model_str = NULL;
5633         }
5634 }
5635
5636 static int __init probe_for_thinkpad(void)
5637 {
5638         int is_thinkpad;
5639
5640         if (acpi_disabled)
5641                 return -ENODEV;
5642
5643         /*
5644          * Non-ancient models have better DMI tagging, but very old models
5645          * don't.
5646          */
5647         is_thinkpad = (thinkpad_id.model_str != NULL);
5648
5649         /* ec is required because many other handles are relative to it */
5650         TPACPI_ACPIHANDLE_INIT(ec);
5651         if (!ec_handle) {
5652                 if (is_thinkpad)
5653                         printk(TPACPI_ERR
5654                                 "Not yet supported ThinkPad detected!\n");
5655                 return -ENODEV;
5656         }
5657
5658         /*
5659          * Risks a regression on very old machines, but reduces potential
5660          * false positives a damn great deal
5661          */
5662         if (!is_thinkpad)
5663                 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5664
5665         if (!is_thinkpad && !force_load)
5666                 return -ENODEV;
5667
5668         return 0;
5669 }
5670
5671
5672 /* Module init, exit, parameters */
5673
5674 static struct ibm_init_struct ibms_init[] __initdata = {
5675         {
5676                 .init = thinkpad_acpi_driver_init,
5677                 .data = &thinkpad_acpi_driver_data,
5678         },
5679         {
5680                 .init = hotkey_init,
5681                 .data = &hotkey_driver_data,
5682         },
5683         {
5684                 .init = bluetooth_init,
5685                 .data = &bluetooth_driver_data,
5686         },
5687         {
5688                 .init = wan_init,
5689                 .data = &wan_driver_data,
5690         },
5691         {
5692                 .init = video_init,
5693                 .data = &video_driver_data,
5694         },
5695         {
5696                 .init = light_init,
5697                 .data = &light_driver_data,
5698         },
5699 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5700         {
5701                 .init = dock_init,
5702                 .data = &dock_driver_data[0],
5703         },
5704         {
5705                 .init = dock_init2,
5706                 .data = &dock_driver_data[1],
5707         },
5708 #endif
5709 #ifdef CONFIG_THINKPAD_ACPI_BAY
5710         {
5711                 .init = bay_init,
5712                 .data = &bay_driver_data,
5713         },
5714 #endif
5715         {
5716                 .init = cmos_init,
5717                 .data = &cmos_driver_data,
5718         },
5719         {
5720                 .init = led_init,
5721                 .data = &led_driver_data,
5722         },
5723         {
5724                 .init = beep_init,
5725                 .data = &beep_driver_data,
5726         },
5727         {
5728                 .init = thermal_init,
5729                 .data = &thermal_driver_data,
5730         },
5731         {
5732                 .data = &ecdump_driver_data,
5733         },
5734         {
5735                 .init = brightness_init,
5736                 .data = &brightness_driver_data,
5737         },
5738         {
5739                 .data = &volume_driver_data,
5740         },
5741         {
5742                 .init = fan_init,
5743                 .data = &fan_driver_data,
5744         },
5745 };
5746
5747 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5748 {
5749         unsigned int i;
5750         struct ibm_struct *ibm;
5751
5752         if (!kp || !kp->name || !val)
5753                 return -EINVAL;
5754
5755         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5756                 ibm = ibms_init[i].data;
5757                 WARN_ON(ibm == NULL);
5758
5759                 if (!ibm || !ibm->name)
5760                         continue;
5761
5762                 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5763                         if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5764                                 return -ENOSPC;
5765                         strcpy(ibms_init[i].param, val);
5766                         strcat(ibms_init[i].param, ",");
5767                         return 0;
5768                 }
5769         }
5770
5771         return -EINVAL;
5772 }
5773
5774 module_param(experimental, int, 0);
5775 MODULE_PARM_DESC(experimental,
5776                  "Enables experimental features when non-zero");
5777
5778 module_param_named(debug, dbg_level, uint, 0);
5779 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5780
5781 module_param(force_load, bool, 0);
5782 MODULE_PARM_DESC(force_load,
5783                  "Attempts to load the driver even on a "
5784                  "mis-identified ThinkPad when true");
5785
5786 module_param_named(fan_control, fan_control_allowed, bool, 0);
5787 MODULE_PARM_DESC(fan_control,
5788                  "Enables setting fan parameters features when true");
5789
5790 module_param_named(brightness_mode, brightness_mode, int, 0);
5791 MODULE_PARM_DESC(brightness_mode,
5792                  "Selects brightness control strategy: "
5793                  "0=auto, 1=EC, 2=CMOS, 3=both");
5794
5795 module_param(brightness_enable, uint, 0);
5796 MODULE_PARM_DESC(brightness_enable,
5797                  "Enables backlight control when 1, disables when 0");
5798
5799 module_param(hotkey_report_mode, uint, 0);
5800 MODULE_PARM_DESC(hotkey_report_mode,
5801                  "used for backwards compatibility with userspace, "
5802                  "see documentation");
5803
5804 #define TPACPI_PARAM(feature) \
5805         module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5806         MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5807                          "at module load, see documentation")
5808
5809 TPACPI_PARAM(hotkey);
5810 TPACPI_PARAM(bluetooth);
5811 TPACPI_PARAM(video);
5812 TPACPI_PARAM(light);
5813 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5814 TPACPI_PARAM(dock);
5815 #endif
5816 #ifdef CONFIG_THINKPAD_ACPI_BAY
5817 TPACPI_PARAM(bay);
5818 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5819 TPACPI_PARAM(cmos);
5820 TPACPI_PARAM(led);
5821 TPACPI_PARAM(beep);
5822 TPACPI_PARAM(ecdump);
5823 TPACPI_PARAM(brightness);
5824 TPACPI_PARAM(volume);
5825 TPACPI_PARAM(fan);
5826
5827 static void thinkpad_acpi_module_exit(void)
5828 {
5829         struct ibm_struct *ibm, *itmp;
5830
5831         tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5832
5833         list_for_each_entry_safe_reverse(ibm, itmp,
5834                                          &tpacpi_all_drivers,
5835                                          all_drivers) {
5836                 ibm_exit(ibm);
5837         }
5838
5839         dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5840
5841         if (tpacpi_inputdev) {
5842                 if (tp_features.input_device_registered)
5843                         input_unregister_device(tpacpi_inputdev);
5844                 else
5845                         input_free_device(tpacpi_inputdev);
5846         }
5847
5848         if (tpacpi_hwmon)
5849                 hwmon_device_unregister(tpacpi_hwmon);
5850
5851         if (tp_features.sensors_pdev_attrs_registered)
5852                 device_remove_file(&tpacpi_sensors_pdev->dev,
5853                                    &dev_attr_thinkpad_acpi_pdev_name);
5854         if (tpacpi_sensors_pdev)
5855                 platform_device_unregister(tpacpi_sensors_pdev);
5856         if (tpacpi_pdev)
5857                 platform_device_unregister(tpacpi_pdev);
5858
5859         if (tp_features.sensors_pdrv_attrs_registered)
5860                 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5861         if (tp_features.platform_drv_attrs_registered)
5862                 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5863
5864         if (tp_features.sensors_pdrv_registered)
5865                 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5866
5867         if (tp_features.platform_drv_registered)
5868                 platform_driver_unregister(&tpacpi_pdriver);
5869
5870         if (proc_dir)
5871                 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5872
5873         kfree(thinkpad_id.bios_version_str);
5874         kfree(thinkpad_id.ec_version_str);
5875         kfree(thinkpad_id.model_str);
5876 }
5877
5878
5879 static int __init thinkpad_acpi_module_init(void)
5880 {
5881         int ret, i;
5882
5883         tpacpi_lifecycle = TPACPI_LIFE_INIT;
5884
5885         /* Parameter checking */
5886         if (hotkey_report_mode > 2)
5887                 return -EINVAL;
5888
5889         /* Driver-level probe */
5890
5891         get_thinkpad_model_data(&thinkpad_id);
5892         ret = probe_for_thinkpad();
5893         if (ret) {
5894                 thinkpad_acpi_module_exit();
5895                 return ret;
5896         }
5897
5898         /* Driver initialization */
5899
5900         TPACPI_ACPIHANDLE_INIT(ecrd);
5901         TPACPI_ACPIHANDLE_INIT(ecwr);
5902
5903         proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5904         if (!proc_dir) {
5905                 printk(TPACPI_ERR
5906                        "unable to create proc dir " TPACPI_PROC_DIR);
5907                 thinkpad_acpi_module_exit();
5908                 return -ENODEV;
5909         }
5910         proc_dir->owner = THIS_MODULE;
5911
5912         ret = platform_driver_register(&tpacpi_pdriver);
5913         if (ret) {
5914                 printk(TPACPI_ERR
5915                        "unable to register main platform driver\n");
5916                 thinkpad_acpi_module_exit();
5917                 return ret;
5918         }
5919         tp_features.platform_drv_registered = 1;
5920
5921         ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5922         if (ret) {
5923                 printk(TPACPI_ERR
5924                        "unable to register hwmon platform driver\n");
5925                 thinkpad_acpi_module_exit();
5926                 return ret;
5927         }
5928         tp_features.sensors_pdrv_registered = 1;
5929
5930         ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5931         if (!ret) {
5932                 tp_features.platform_drv_attrs_registered = 1;
5933                 ret = tpacpi_create_driver_attributes(
5934                                         &tpacpi_hwmon_pdriver.driver);
5935         }
5936         if (ret) {
5937                 printk(TPACPI_ERR
5938                        "unable to create sysfs driver attributes\n");
5939                 thinkpad_acpi_module_exit();
5940                 return ret;
5941         }
5942         tp_features.sensors_pdrv_attrs_registered = 1;
5943
5944
5945         /* Device initialization */
5946         tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
5947                                                         NULL, 0);
5948         if (IS_ERR(tpacpi_pdev)) {
5949                 ret = PTR_ERR(tpacpi_pdev);
5950                 tpacpi_pdev = NULL;
5951                 printk(TPACPI_ERR "unable to register platform device\n");
5952                 thinkpad_acpi_module_exit();
5953                 return ret;
5954         }
5955         tpacpi_sensors_pdev = platform_device_register_simple(
5956                                                 TPACPI_HWMON_DRVR_NAME,
5957                                                 -1, NULL, 0);
5958         if (IS_ERR(tpacpi_sensors_pdev)) {
5959                 ret = PTR_ERR(tpacpi_sensors_pdev);
5960                 tpacpi_sensors_pdev = NULL;
5961                 printk(TPACPI_ERR
5962                        "unable to register hwmon platform device\n");
5963                 thinkpad_acpi_module_exit();
5964                 return ret;
5965         }
5966         ret = device_create_file(&tpacpi_sensors_pdev->dev,
5967                                  &dev_attr_thinkpad_acpi_pdev_name);
5968         if (ret) {
5969                 printk(TPACPI_ERR
5970                        "unable to create sysfs hwmon device attributes\n");
5971                 thinkpad_acpi_module_exit();
5972                 return ret;
5973         }
5974         tp_features.sensors_pdev_attrs_registered = 1;
5975         tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5976         if (IS_ERR(tpacpi_hwmon)) {
5977                 ret = PTR_ERR(tpacpi_hwmon);
5978                 tpacpi_hwmon = NULL;
5979                 printk(TPACPI_ERR "unable to register hwmon device\n");
5980                 thinkpad_acpi_module_exit();
5981                 return ret;
5982         }
5983         mutex_init(&tpacpi_inputdev_send_mutex);
5984         tpacpi_inputdev = input_allocate_device();
5985         if (!tpacpi_inputdev) {
5986                 printk(TPACPI_ERR "unable to allocate input device\n");
5987                 thinkpad_acpi_module_exit();
5988                 return -ENOMEM;
5989         } else {
5990                 /* Prepare input device, but don't register */
5991                 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5992                 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
5993                 tpacpi_inputdev->id.bustype = BUS_HOST;
5994                 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5995                                                 thinkpad_id.vendor :
5996                                                 PCI_VENDOR_ID_IBM;
5997                 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5998                 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5999         }
6000         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6001                 ret = ibm_init(&ibms_init[i]);
6002                 if (ret >= 0 && *ibms_init[i].param)
6003                         ret = ibms_init[i].data->write(ibms_init[i].param);
6004                 if (ret < 0) {
6005                         thinkpad_acpi_module_exit();
6006                         return ret;
6007                 }
6008         }
6009         ret = input_register_device(tpacpi_inputdev);
6010         if (ret < 0) {
6011                 printk(TPACPI_ERR "unable to register input device\n");
6012                 thinkpad_acpi_module_exit();
6013                 return ret;
6014         } else {
6015                 tp_features.input_device_registered = 1;
6016         }
6017
6018         tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6019         return 0;
6020 }
6021
6022 /* Please remove this in year 2009 */
6023 MODULE_ALIAS("ibm_acpi");
6024
6025 /*
6026  * DMI matching for module autoloading
6027  *
6028  * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6029  * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6030  *
6031  * Only models listed in thinkwiki will be supported, so add yours
6032  * if it is not there yet.
6033  */
6034 #define IBM_BIOS_MODULE_ALIAS(__type) \
6035         MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6036
6037 /* Non-ancient thinkpads */
6038 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6039 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6040
6041 /* Ancient thinkpad BIOSes have to be identified by
6042  * BIOS type or model number, and there are far less
6043  * BIOS types than model numbers... */
6044 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6045 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6046 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6047
6048 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6049 MODULE_DESCRIPTION(TPACPI_DESC);
6050 MODULE_VERSION(TPACPI_VERSION);
6051 MODULE_LICENSE("GPL");
6052
6053 module_init(thinkpad_acpi_module_init);
6054 module_exit(thinkpad_acpi_module_exit);