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