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