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