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