]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/platform/x86/thinkpad_acpi.c
thinkpad-acpi: enhanced debugging messages for rfkill subdrivers
[linux-2.6-omap-h63xx.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.22"
25 #define TPACPI_SYSFS_VERSION 0x020300
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
194 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
195 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
196 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
197
198
199 /****************************************************************************
200  * Driver-wide structs and misc. variables
201  */
202
203 struct ibm_struct;
204
205 struct tp_acpi_drv_struct {
206         const struct acpi_device_id *hid;
207         struct acpi_driver *driver;
208
209         void (*notify) (struct ibm_struct *, u32);
210         acpi_handle *handle;
211         u32 type;
212         struct acpi_device *device;
213 };
214
215 struct ibm_struct {
216         char *name;
217
218         int (*read) (char *);
219         int (*write) (char *);
220         void (*exit) (void);
221         void (*resume) (void);
222         void (*suspend) (pm_message_t state);
223         void (*shutdown) (void);
224
225         struct list_head all_drivers;
226
227         struct tp_acpi_drv_struct *acpi;
228
229         struct {
230                 u8 acpi_driver_registered:1;
231                 u8 acpi_notify_installed:1;
232                 u8 proc_created:1;
233                 u8 init_called:1;
234                 u8 experimental:1;
235         } flags;
236 };
237
238 struct ibm_init_struct {
239         char param[32];
240
241         int (*init) (struct ibm_init_struct *);
242         struct ibm_struct *data;
243 };
244
245 static struct {
246 #ifdef CONFIG_THINKPAD_ACPI_BAY
247         u32 bay_status:1;
248         u32 bay_eject:1;
249         u32 bay_status2:1;
250         u32 bay_eject2:1;
251 #endif
252         u32 bluetooth:1;
253         u32 hotkey:1;
254         u32 hotkey_mask:1;
255         u32 hotkey_wlsw:1;
256         u32 hotkey_tablet:1;
257         u32 light:1;
258         u32 light_status:1;
259         u32 bright_16levels:1;
260         u32 bright_acpimode:1;
261         u32 wan:1;
262         u32 uwb:1;
263         u32 fan_ctrl_status_undef:1;
264         u32 input_device_registered:1;
265         u32 platform_drv_registered:1;
266         u32 platform_drv_attrs_registered:1;
267         u32 sensors_pdrv_registered:1;
268         u32 sensors_pdrv_attrs_registered:1;
269         u32 sensors_pdev_attrs_registered:1;
270         u32 hotkey_poll_active:1;
271 } tp_features;
272
273 static struct {
274         u16 hotkey_mask_ff:1;
275         u16 bright_cmos_ec_unsync:1;
276 } tp_warned;
277
278 struct thinkpad_id_data {
279         unsigned int vendor;    /* ThinkPad vendor:
280                                  * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
281
282         char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
283         char *ec_version_str;   /* Something like 1ZHT51WW-1.04a */
284
285         u16 bios_model;         /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
286         u16 ec_model;
287
288         char *model_str;        /* ThinkPad T43 */
289         char *nummodel_str;     /* 9384A9C for a 9384-A9C model */
290 };
291 static struct thinkpad_id_data thinkpad_id;
292
293 static enum {
294         TPACPI_LIFE_INIT = 0,
295         TPACPI_LIFE_RUNNING,
296         TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
298
299 static int experimental;
300 static u32 dbg_level;
301
302 static struct workqueue_struct *tpacpi_wq;
303
304 /* Special LED class that can defer work */
305 struct tpacpi_led_classdev {
306         struct led_classdev led_classdev;
307         struct work_struct work;
308         enum led_brightness new_brightness;
309         unsigned int led;
310 };
311
312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
313 static int dbg_wlswemul;
314 static int tpacpi_wlsw_emulstate;
315 static int dbg_bluetoothemul;
316 static int tpacpi_bluetooth_emulstate;
317 static int dbg_wwanemul;
318 static int tpacpi_wwan_emulstate;
319 static int dbg_uwbemul;
320 static int tpacpi_uwb_emulstate;
321 #endif
322
323
324 /*************************************************************************
325  *  Debugging helpers
326  */
327
328 #define dbg_printk(a_dbg_level, format, arg...) \
329         do { if (dbg_level & (a_dbg_level)) \
330                 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
331         } while (0)
332
333 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
334 #define vdbg_printk dbg_printk
335 static const char *str_supported(int is_supported);
336 #else
337 #define vdbg_printk(a_dbg_level, format, arg...) \
338         do { } while (0)
339 #endif
340
341 static void tpacpi_log_usertask(const char * const what)
342 {
343         printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
344                 what, task_tgid_vnr(current));
345 }
346
347 #define tpacpi_disclose_usertask(what, format, arg...) \
348         do { \
349                 if (unlikely( \
350                     (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
351                     (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
352                         printk(TPACPI_DEBUG "%s: PID %d: " format, \
353                                 what, task_tgid_vnr(current), ## arg); \
354                 } \
355         } while (0)
356
357 /****************************************************************************
358  ****************************************************************************
359  *
360  * ACPI Helpers and device model
361  *
362  ****************************************************************************
363  ****************************************************************************/
364
365 /*************************************************************************
366  * ACPI basic handles
367  */
368
369 static acpi_handle root_handle;
370
371 #define TPACPI_HANDLE(object, parent, paths...)                 \
372         static acpi_handle  object##_handle;                    \
373         static acpi_handle *object##_parent = &parent##_handle; \
374         static char        *object##_path;                      \
375         static char        *object##_paths[] = { paths }
376
377 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",   /* 240, 240x */
378            "\\_SB.PCI.ISA.EC",  /* 570 */
379            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
380            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
381            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
382            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
383            "\\_SB.PCI0.LPC.EC", /* all others */
384            );
385
386 TPACPI_HANDLE(ecrd, ec, "ECRD");        /* 570 */
387 TPACPI_HANDLE(ecwr, ec, "ECWR");        /* 570 */
388
389 TPACPI_HANDLE(cmos, root, "\\UCMS",     /* R50, R50e, R50p, R51, */
390                                         /* T4x, X31, X40 */
391            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
392            "\\CMS",             /* R40, R40e */
393            );                   /* all others */
394
395 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY",   /* 600e/x, 770e, 770x */
396            "^HKEY",             /* R30, R31 */
397            "HKEY",              /* all others */
398            );                   /* 570 */
399
400 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",   /* 570 */
401            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
402            "\\_SB.PCI0.VID0",   /* 770e */
403            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
404            "\\_SB.PCI0.AGP.VID",        /* all others */
405            );                           /* R30, R31 */
406
407
408 /*************************************************************************
409  * ACPI helpers
410  */
411
412 static int acpi_evalf(acpi_handle handle,
413                       void *res, char *method, char *fmt, ...)
414 {
415         char *fmt0 = fmt;
416         struct acpi_object_list params;
417         union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
418         struct acpi_buffer result, *resultp;
419         union acpi_object out_obj;
420         acpi_status status;
421         va_list ap;
422         char res_type;
423         int success;
424         int quiet;
425
426         if (!*fmt) {
427                 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
428                 return 0;
429         }
430
431         if (*fmt == 'q') {
432                 quiet = 1;
433                 fmt++;
434         } else
435                 quiet = 0;
436
437         res_type = *(fmt++);
438
439         params.count = 0;
440         params.pointer = &in_objs[0];
441
442         va_start(ap, fmt);
443         while (*fmt) {
444                 char c = *(fmt++);
445                 switch (c) {
446                 case 'd':       /* int */
447                         in_objs[params.count].integer.value = va_arg(ap, int);
448                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
449                         break;
450                         /* add more types as needed */
451                 default:
452                         printk(TPACPI_ERR "acpi_evalf() called "
453                                "with invalid format character '%c'\n", c);
454                         return 0;
455                 }
456         }
457         va_end(ap);
458
459         if (res_type != 'v') {
460                 result.length = sizeof(out_obj);
461                 result.pointer = &out_obj;
462                 resultp = &result;
463         } else
464                 resultp = NULL;
465
466         status = acpi_evaluate_object(handle, method, &params, resultp);
467
468         switch (res_type) {
469         case 'd':               /* int */
470                 if (res)
471                         *(int *)res = out_obj.integer.value;
472                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
473                 break;
474         case 'v':               /* void */
475                 success = status == AE_OK;
476                 break;
477                 /* add more types as needed */
478         default:
479                 printk(TPACPI_ERR "acpi_evalf() called "
480                        "with invalid format character '%c'\n", res_type);
481                 return 0;
482         }
483
484         if (!success && !quiet)
485                 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
486                        method, fmt0, status);
487
488         return success;
489 }
490
491 static int acpi_ec_read(int i, u8 *p)
492 {
493         int v;
494
495         if (ecrd_handle) {
496                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
497                         return 0;
498                 *p = v;
499         } else {
500                 if (ec_read(i, p) < 0)
501                         return 0;
502         }
503
504         return 1;
505 }
506
507 static int acpi_ec_write(int i, u8 v)
508 {
509         if (ecwr_handle) {
510                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
511                         return 0;
512         } else {
513                 if (ec_write(i, v) < 0)
514                         return 0;
515         }
516
517         return 1;
518 }
519
520 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
521 static int _sta(acpi_handle handle)
522 {
523         int status;
524
525         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
526                 status = 0;
527
528         return status;
529 }
530 #endif
531
532 static int issue_thinkpad_cmos_command(int cmos_cmd)
533 {
534         if (!cmos_handle)
535                 return -ENXIO;
536
537         if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
538                 return -EIO;
539
540         return 0;
541 }
542
543 /*************************************************************************
544  * ACPI device model
545  */
546
547 #define TPACPI_ACPIHANDLE_INIT(object) \
548         drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
549                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
550
551 static void drv_acpi_handle_init(char *name,
552                            acpi_handle *handle, acpi_handle parent,
553                            char **paths, int num_paths, char **path)
554 {
555         int i;
556         acpi_status status;
557
558         vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
559                 name);
560
561         for (i = 0; i < num_paths; i++) {
562                 status = acpi_get_handle(parent, paths[i], handle);
563                 if (ACPI_SUCCESS(status)) {
564                         *path = paths[i];
565                         dbg_printk(TPACPI_DBG_INIT,
566                                    "Found ACPI handle %s for %s\n",
567                                    *path, name);
568                         return;
569                 }
570         }
571
572         vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
573                     name);
574         *handle = NULL;
575 }
576
577 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
578 {
579         struct ibm_struct *ibm = data;
580
581         if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
582                 return;
583
584         if (!ibm || !ibm->acpi || !ibm->acpi->notify)
585                 return;
586
587         ibm->acpi->notify(ibm, event);
588 }
589
590 static int __init setup_acpi_notify(struct ibm_struct *ibm)
591 {
592         acpi_status status;
593         int rc;
594
595         BUG_ON(!ibm->acpi);
596
597         if (!*ibm->acpi->handle)
598                 return 0;
599
600         vdbg_printk(TPACPI_DBG_INIT,
601                 "setting up ACPI notify for %s\n", ibm->name);
602
603         rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
604         if (rc < 0) {
605                 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
606                         ibm->name, rc);
607                 return -ENODEV;
608         }
609
610         ibm->acpi->device->driver_data = ibm;
611         sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
612                 TPACPI_ACPI_EVENT_PREFIX,
613                 ibm->name);
614
615         status = acpi_install_notify_handler(*ibm->acpi->handle,
616                         ibm->acpi->type, dispatch_acpi_notify, ibm);
617         if (ACPI_FAILURE(status)) {
618                 if (status == AE_ALREADY_EXISTS) {
619                         printk(TPACPI_NOTICE
620                                "another device driver is already "
621                                "handling %s events\n", ibm->name);
622                 } else {
623                         printk(TPACPI_ERR
624                                "acpi_install_notify_handler(%s) failed: %d\n",
625                                ibm->name, status);
626                 }
627                 return -ENODEV;
628         }
629         ibm->flags.acpi_notify_installed = 1;
630         return 0;
631 }
632
633 static int __init tpacpi_device_add(struct acpi_device *device)
634 {
635         return 0;
636 }
637
638 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
639 {
640         int rc;
641
642         dbg_printk(TPACPI_DBG_INIT,
643                 "registering %s as an ACPI driver\n", ibm->name);
644
645         BUG_ON(!ibm->acpi);
646
647         ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
648         if (!ibm->acpi->driver) {
649                 printk(TPACPI_ERR
650                        "failed to allocate memory for ibm->acpi->driver\n");
651                 return -ENOMEM;
652         }
653
654         sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
655         ibm->acpi->driver->ids = ibm->acpi->hid;
656
657         ibm->acpi->driver->ops.add = &tpacpi_device_add;
658
659         rc = acpi_bus_register_driver(ibm->acpi->driver);
660         if (rc < 0) {
661                 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
662                        ibm->name, rc);
663                 kfree(ibm->acpi->driver);
664                 ibm->acpi->driver = NULL;
665         } else if (!rc)
666                 ibm->flags.acpi_driver_registered = 1;
667
668         return rc;
669 }
670
671
672 /****************************************************************************
673  ****************************************************************************
674  *
675  * Procfs Helpers
676  *
677  ****************************************************************************
678  ****************************************************************************/
679
680 static int dispatch_procfs_read(char *page, char **start, off_t off,
681                         int count, int *eof, void *data)
682 {
683         struct ibm_struct *ibm = data;
684         int len;
685
686         if (!ibm || !ibm->read)
687                 return -EINVAL;
688
689         len = ibm->read(page);
690         if (len < 0)
691                 return len;
692
693         if (len <= off + count)
694                 *eof = 1;
695         *start = page + off;
696         len -= off;
697         if (len > count)
698                 len = count;
699         if (len < 0)
700                 len = 0;
701
702         return len;
703 }
704
705 static int dispatch_procfs_write(struct file *file,
706                         const char __user *userbuf,
707                         unsigned long count, void *data)
708 {
709         struct ibm_struct *ibm = data;
710         char *kernbuf;
711         int ret;
712
713         if (!ibm || !ibm->write)
714                 return -EINVAL;
715
716         kernbuf = kmalloc(count + 2, GFP_KERNEL);
717         if (!kernbuf)
718                 return -ENOMEM;
719
720         if (copy_from_user(kernbuf, userbuf, count)) {
721                 kfree(kernbuf);
722                 return -EFAULT;
723         }
724
725         kernbuf[count] = 0;
726         strcat(kernbuf, ",");
727         ret = ibm->write(kernbuf);
728         if (ret == 0)
729                 ret = count;
730
731         kfree(kernbuf);
732
733         return ret;
734 }
735
736 static char *next_cmd(char **cmds)
737 {
738         char *start = *cmds;
739         char *end;
740
741         while ((end = strchr(start, ',')) && end == start)
742                 start = end + 1;
743
744         if (!end)
745                 return NULL;
746
747         *end = 0;
748         *cmds = end + 1;
749         return start;
750 }
751
752
753 /****************************************************************************
754  ****************************************************************************
755  *
756  * Device model: input, hwmon and platform
757  *
758  ****************************************************************************
759  ****************************************************************************/
760
761 static struct platform_device *tpacpi_pdev;
762 static struct platform_device *tpacpi_sensors_pdev;
763 static struct device *tpacpi_hwmon;
764 static struct input_dev *tpacpi_inputdev;
765 static struct mutex tpacpi_inputdev_send_mutex;
766 static LIST_HEAD(tpacpi_all_drivers);
767
768 static int tpacpi_suspend_handler(struct platform_device *pdev,
769                                   pm_message_t state)
770 {
771         struct ibm_struct *ibm, *itmp;
772
773         list_for_each_entry_safe(ibm, itmp,
774                                  &tpacpi_all_drivers,
775                                  all_drivers) {
776                 if (ibm->suspend)
777                         (ibm->suspend)(state);
778         }
779
780         return 0;
781 }
782
783 static int tpacpi_resume_handler(struct platform_device *pdev)
784 {
785         struct ibm_struct *ibm, *itmp;
786
787         list_for_each_entry_safe(ibm, itmp,
788                                  &tpacpi_all_drivers,
789                                  all_drivers) {
790                 if (ibm->resume)
791                         (ibm->resume)();
792         }
793
794         return 0;
795 }
796
797 static void tpacpi_shutdown_handler(struct platform_device *pdev)
798 {
799         struct ibm_struct *ibm, *itmp;
800
801         list_for_each_entry_safe(ibm, itmp,
802                                  &tpacpi_all_drivers,
803                                  all_drivers) {
804                 if (ibm->shutdown)
805                         (ibm->shutdown)();
806         }
807 }
808
809 static struct platform_driver tpacpi_pdriver = {
810         .driver = {
811                 .name = TPACPI_DRVR_NAME,
812                 .owner = THIS_MODULE,
813         },
814         .suspend = tpacpi_suspend_handler,
815         .resume = tpacpi_resume_handler,
816         .shutdown = tpacpi_shutdown_handler,
817 };
818
819 static struct platform_driver tpacpi_hwmon_pdriver = {
820         .driver = {
821                 .name = TPACPI_HWMON_DRVR_NAME,
822                 .owner = THIS_MODULE,
823         },
824 };
825
826 /*************************************************************************
827  * sysfs support helpers
828  */
829
830 struct attribute_set {
831         unsigned int members, max_members;
832         struct attribute_group group;
833 };
834
835 struct attribute_set_obj {
836         struct attribute_set s;
837         struct attribute *a;
838 } __attribute__((packed));
839
840 static struct attribute_set *create_attr_set(unsigned int max_members,
841                                                 const char *name)
842 {
843         struct attribute_set_obj *sobj;
844
845         if (max_members == 0)
846                 return NULL;
847
848         /* Allocates space for implicit NULL at the end too */
849         sobj = kzalloc(sizeof(struct attribute_set_obj) +
850                     max_members * sizeof(struct attribute *),
851                     GFP_KERNEL);
852         if (!sobj)
853                 return NULL;
854         sobj->s.max_members = max_members;
855         sobj->s.group.attrs = &sobj->a;
856         sobj->s.group.name = name;
857
858         return &sobj->s;
859 }
860
861 #define destroy_attr_set(_set) \
862         kfree(_set);
863
864 /* not multi-threaded safe, use it in a single thread per set */
865 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
866 {
867         if (!s || !attr)
868                 return -EINVAL;
869
870         if (s->members >= s->max_members)
871                 return -ENOMEM;
872
873         s->group.attrs[s->members] = attr;
874         s->members++;
875
876         return 0;
877 }
878
879 static int add_many_to_attr_set(struct attribute_set *s,
880                         struct attribute **attr,
881                         unsigned int count)
882 {
883         int i, res;
884
885         for (i = 0; i < count; i++) {
886                 res = add_to_attr_set(s, attr[i]);
887                 if (res)
888                         return res;
889         }
890
891         return 0;
892 }
893
894 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
895 {
896         sysfs_remove_group(kobj, &s->group);
897         destroy_attr_set(s);
898 }
899
900 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
901         sysfs_create_group(_kobj, &_attr_set->group)
902
903 static int parse_strtoul(const char *buf,
904                 unsigned long max, unsigned long *value)
905 {
906         char *endp;
907
908         while (*buf && isspace(*buf))
909                 buf++;
910         *value = simple_strtoul(buf, &endp, 0);
911         while (*endp && isspace(*endp))
912                 endp++;
913         if (*endp || *value > max)
914                 return -EINVAL;
915
916         return 0;
917 }
918
919 static void tpacpi_disable_brightness_delay(void)
920 {
921         if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
922                 printk(TPACPI_NOTICE
923                         "ACPI backlight control delay disabled\n");
924 }
925
926 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
927 {
928         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
929         union acpi_object *obj;
930         int rc;
931
932         if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
933                 obj = (union acpi_object *)buffer.pointer;
934                 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
935                         printk(TPACPI_ERR "Unknown _BCL data, "
936                                "please report this to %s\n", TPACPI_MAIL);
937                         rc = 0;
938                 } else {
939                         rc = obj->package.count;
940                 }
941         } else {
942                 return 0;
943         }
944
945         kfree(buffer.pointer);
946         return rc;
947 }
948
949 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
950                                         u32 lvl, void *context, void **rv)
951 {
952         char name[ACPI_PATH_SEGMENT_LENGTH];
953         struct acpi_buffer buffer = { sizeof(name), &name };
954
955         if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
956             !strncmp("_BCL", name, sizeof(name) - 1)) {
957                 BUG_ON(!rv || !*rv);
958                 **(int **)rv = tpacpi_query_bcl_levels(handle);
959                 return AE_CTRL_TERMINATE;
960         } else {
961                 return AE_OK;
962         }
963 }
964
965 /*
966  * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
967  */
968 static int __init tpacpi_check_std_acpi_brightness_support(void)
969 {
970         int status;
971         int bcl_levels = 0;
972         void *bcl_ptr = &bcl_levels;
973
974         if (!vid_handle) {
975                 TPACPI_ACPIHANDLE_INIT(vid);
976         }
977         if (!vid_handle)
978                 return 0;
979
980         /*
981          * Search for a _BCL method, and execute it.  This is safe on all
982          * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
983          * BIOS in ACPI backlight control mode.  We do NOT have to care
984          * about calling the _BCL method in an enabled video device, any
985          * will do for our purposes.
986          */
987
988         status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
989                                      tpacpi_acpi_walk_find_bcl, NULL,
990                                      &bcl_ptr);
991
992         if (ACPI_SUCCESS(status) && bcl_levels > 2) {
993                 tp_features.bright_acpimode = 1;
994                 return (bcl_levels - 2);
995         }
996
997         return 0;
998 }
999
1000 static int __init tpacpi_new_rfkill(const unsigned int id,
1001                         struct rfkill **rfk,
1002                         const enum rfkill_type rfktype,
1003                         const char *name,
1004                         const bool set_default,
1005                         int (*toggle_radio)(void *, enum rfkill_state),
1006                         int (*get_state)(void *, enum rfkill_state *))
1007 {
1008         int res;
1009         enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1010
1011         res = get_state(NULL, &initial_state);
1012         if (res < 0) {
1013                 printk(TPACPI_ERR
1014                         "failed to read initial state for %s, error %d; "
1015                         "will turn radio off\n", name, res);
1016         } else if (set_default) {
1017                 /* try to set the initial state as the default for the rfkill
1018                  * type, since we ask the firmware to preserve it across S5 in
1019                  * NVRAM */
1020                 if (rfkill_set_default(rfktype,
1021                                 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1022                                         RFKILL_STATE_UNBLOCKED :
1023                                         RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1024                         vdbg_printk(TPACPI_DBG_RFKILL,
1025                                     "Default state for %s cannot be changed\n",
1026                                     name);
1027         }
1028
1029         *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1030         if (!*rfk) {
1031                 printk(TPACPI_ERR
1032                         "failed to allocate memory for rfkill class\n");
1033                 return -ENOMEM;
1034         }
1035
1036         (*rfk)->name = name;
1037         (*rfk)->get_state = get_state;
1038         (*rfk)->toggle_radio = toggle_radio;
1039         (*rfk)->state = initial_state;
1040
1041         res = rfkill_register(*rfk);
1042         if (res < 0) {
1043                 printk(TPACPI_ERR
1044                         "failed to register %s rfkill switch: %d\n",
1045                         name, res);
1046                 rfkill_free(*rfk);
1047                 *rfk = NULL;
1048                 return res;
1049         }
1050
1051         return 0;
1052 }
1053
1054 static void printk_deprecated_attribute(const char * const what,
1055                                         const char * const details)
1056 {
1057         tpacpi_log_usertask("deprecated sysfs attribute");
1058         printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1059                 "will be removed. %s\n",
1060                 what, details);
1061 }
1062
1063 static void printk_deprecated_rfkill_attribute(const char * const what)
1064 {
1065         printk_deprecated_attribute(what,
1066                         "Please switch to generic rfkill before year 2010");
1067 }
1068
1069 /*************************************************************************
1070  * thinkpad-acpi driver attributes
1071  */
1072
1073 /* interface_version --------------------------------------------------- */
1074 static ssize_t tpacpi_driver_interface_version_show(
1075                                 struct device_driver *drv,
1076                                 char *buf)
1077 {
1078         return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1079 }
1080
1081 static DRIVER_ATTR(interface_version, S_IRUGO,
1082                 tpacpi_driver_interface_version_show, NULL);
1083
1084 /* debug_level --------------------------------------------------------- */
1085 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1086                                                 char *buf)
1087 {
1088         return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1089 }
1090
1091 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1092                                                 const char *buf, size_t count)
1093 {
1094         unsigned long t;
1095
1096         if (parse_strtoul(buf, 0xffff, &t))
1097                 return -EINVAL;
1098
1099         dbg_level = t;
1100
1101         return count;
1102 }
1103
1104 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1105                 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1106
1107 /* version ------------------------------------------------------------- */
1108 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1109                                                 char *buf)
1110 {
1111         return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1112                         TPACPI_DESC, TPACPI_VERSION);
1113 }
1114
1115 static DRIVER_ATTR(version, S_IRUGO,
1116                 tpacpi_driver_version_show, NULL);
1117
1118 /* --------------------------------------------------------------------- */
1119
1120 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1121
1122 static void tpacpi_send_radiosw_update(void);
1123
1124 /* wlsw_emulstate ------------------------------------------------------ */
1125 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1126                                                 char *buf)
1127 {
1128         return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1129 }
1130
1131 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1132                                                 const char *buf, size_t count)
1133 {
1134         unsigned long t;
1135
1136         if (parse_strtoul(buf, 1, &t))
1137                 return -EINVAL;
1138
1139         if (tpacpi_wlsw_emulstate != t) {
1140                 tpacpi_wlsw_emulstate = !!t;
1141                 tpacpi_send_radiosw_update();
1142         } else
1143                 tpacpi_wlsw_emulstate = !!t;
1144
1145         return count;
1146 }
1147
1148 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1149                 tpacpi_driver_wlsw_emulstate_show,
1150                 tpacpi_driver_wlsw_emulstate_store);
1151
1152 /* bluetooth_emulstate ------------------------------------------------- */
1153 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1154                                         struct device_driver *drv,
1155                                         char *buf)
1156 {
1157         return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1158 }
1159
1160 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1161                                         struct device_driver *drv,
1162                                         const char *buf, size_t count)
1163 {
1164         unsigned long t;
1165
1166         if (parse_strtoul(buf, 1, &t))
1167                 return -EINVAL;
1168
1169         tpacpi_bluetooth_emulstate = !!t;
1170
1171         return count;
1172 }
1173
1174 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1175                 tpacpi_driver_bluetooth_emulstate_show,
1176                 tpacpi_driver_bluetooth_emulstate_store);
1177
1178 /* wwan_emulstate ------------------------------------------------- */
1179 static ssize_t tpacpi_driver_wwan_emulstate_show(
1180                                         struct device_driver *drv,
1181                                         char *buf)
1182 {
1183         return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1184 }
1185
1186 static ssize_t tpacpi_driver_wwan_emulstate_store(
1187                                         struct device_driver *drv,
1188                                         const char *buf, size_t count)
1189 {
1190         unsigned long t;
1191
1192         if (parse_strtoul(buf, 1, &t))
1193                 return -EINVAL;
1194
1195         tpacpi_wwan_emulstate = !!t;
1196
1197         return count;
1198 }
1199
1200 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1201                 tpacpi_driver_wwan_emulstate_show,
1202                 tpacpi_driver_wwan_emulstate_store);
1203
1204 /* uwb_emulstate ------------------------------------------------- */
1205 static ssize_t tpacpi_driver_uwb_emulstate_show(
1206                                         struct device_driver *drv,
1207                                         char *buf)
1208 {
1209         return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1210 }
1211
1212 static ssize_t tpacpi_driver_uwb_emulstate_store(
1213                                         struct device_driver *drv,
1214                                         const char *buf, size_t count)
1215 {
1216         unsigned long t;
1217
1218         if (parse_strtoul(buf, 1, &t))
1219                 return -EINVAL;
1220
1221         tpacpi_uwb_emulstate = !!t;
1222
1223         return count;
1224 }
1225
1226 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1227                 tpacpi_driver_uwb_emulstate_show,
1228                 tpacpi_driver_uwb_emulstate_store);
1229 #endif
1230
1231 /* --------------------------------------------------------------------- */
1232
1233 static struct driver_attribute *tpacpi_driver_attributes[] = {
1234         &driver_attr_debug_level, &driver_attr_version,
1235         &driver_attr_interface_version,
1236 };
1237
1238 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1239 {
1240         int i, res;
1241
1242         i = 0;
1243         res = 0;
1244         while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1245                 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1246                 i++;
1247         }
1248
1249 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1250         if (!res && dbg_wlswemul)
1251                 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1252         if (!res && dbg_bluetoothemul)
1253                 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1254         if (!res && dbg_wwanemul)
1255                 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1256         if (!res && dbg_uwbemul)
1257                 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1258 #endif
1259
1260         return res;
1261 }
1262
1263 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1264 {
1265         int i;
1266
1267         for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1268                 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1269
1270 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1271         driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1272         driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1273         driver_remove_file(drv, &driver_attr_wwan_emulstate);
1274         driver_remove_file(drv, &driver_attr_uwb_emulstate);
1275 #endif
1276 }
1277
1278 /****************************************************************************
1279  ****************************************************************************
1280  *
1281  * Subdrivers
1282  *
1283  ****************************************************************************
1284  ****************************************************************************/
1285
1286 /*************************************************************************
1287  * thinkpad-acpi init subdriver
1288  */
1289
1290 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1291 {
1292         printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1293         printk(TPACPI_INFO "%s\n", TPACPI_URL);
1294
1295         printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1296                 (thinkpad_id.bios_version_str) ?
1297                         thinkpad_id.bios_version_str : "unknown",
1298                 (thinkpad_id.ec_version_str) ?
1299                         thinkpad_id.ec_version_str : "unknown");
1300
1301         if (thinkpad_id.vendor && thinkpad_id.model_str)
1302                 printk(TPACPI_INFO "%s %s, model %s\n",
1303                         (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1304                                 "IBM" : ((thinkpad_id.vendor ==
1305                                                 PCI_VENDOR_ID_LENOVO) ?
1306                                         "Lenovo" : "Unknown vendor"),
1307                         thinkpad_id.model_str,
1308                         (thinkpad_id.nummodel_str) ?
1309                                 thinkpad_id.nummodel_str : "unknown");
1310
1311         return 0;
1312 }
1313
1314 static int thinkpad_acpi_driver_read(char *p)
1315 {
1316         int len = 0;
1317
1318         len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1319         len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1320
1321         return len;
1322 }
1323
1324 static struct ibm_struct thinkpad_acpi_driver_data = {
1325         .name = "driver",
1326         .read = thinkpad_acpi_driver_read,
1327 };
1328
1329 /*************************************************************************
1330  * Hotkey subdriver
1331  */
1332
1333 enum {  /* hot key scan codes (derived from ACPI DSDT) */
1334         TP_ACPI_HOTKEYSCAN_FNF1         = 0,
1335         TP_ACPI_HOTKEYSCAN_FNF2,
1336         TP_ACPI_HOTKEYSCAN_FNF3,
1337         TP_ACPI_HOTKEYSCAN_FNF4,
1338         TP_ACPI_HOTKEYSCAN_FNF5,
1339         TP_ACPI_HOTKEYSCAN_FNF6,
1340         TP_ACPI_HOTKEYSCAN_FNF7,
1341         TP_ACPI_HOTKEYSCAN_FNF8,
1342         TP_ACPI_HOTKEYSCAN_FNF9,
1343         TP_ACPI_HOTKEYSCAN_FNF10,
1344         TP_ACPI_HOTKEYSCAN_FNF11,
1345         TP_ACPI_HOTKEYSCAN_FNF12,
1346         TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1347         TP_ACPI_HOTKEYSCAN_FNINSERT,
1348         TP_ACPI_HOTKEYSCAN_FNDELETE,
1349         TP_ACPI_HOTKEYSCAN_FNHOME,
1350         TP_ACPI_HOTKEYSCAN_FNEND,
1351         TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1352         TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1353         TP_ACPI_HOTKEYSCAN_FNSPACE,
1354         TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1355         TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1356         TP_ACPI_HOTKEYSCAN_MUTE,
1357         TP_ACPI_HOTKEYSCAN_THINKPAD,
1358 };
1359
1360 enum {  /* Keys available through NVRAM polling */
1361         TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1362         TPACPI_HKEY_NVRAM_GOOD_MASK  = 0x00fb8000U,
1363 };
1364
1365 enum {  /* Positions of some of the keys in hotkey masks */
1366         TP_ACPI_HKEY_DISPSWTCH_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1367         TP_ACPI_HKEY_DISPXPAND_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1368         TP_ACPI_HKEY_HIBERNATE_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1369         TP_ACPI_HKEY_BRGHTUP_MASK       = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1370         TP_ACPI_HKEY_BRGHTDWN_MASK      = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1371         TP_ACPI_HKEY_THNKLGHT_MASK      = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1372         TP_ACPI_HKEY_ZOOM_MASK          = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1373         TP_ACPI_HKEY_VOLUP_MASK         = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1374         TP_ACPI_HKEY_VOLDWN_MASK        = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1375         TP_ACPI_HKEY_MUTE_MASK          = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1376         TP_ACPI_HKEY_THINKPAD_MASK      = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1377 };
1378
1379 enum {  /* NVRAM to ACPI HKEY group map */
1380         TP_NVRAM_HKEY_GROUP_HK2         = TP_ACPI_HKEY_THINKPAD_MASK |
1381                                           TP_ACPI_HKEY_ZOOM_MASK |
1382                                           TP_ACPI_HKEY_DISPSWTCH_MASK |
1383                                           TP_ACPI_HKEY_HIBERNATE_MASK,
1384         TP_NVRAM_HKEY_GROUP_BRIGHTNESS  = TP_ACPI_HKEY_BRGHTUP_MASK |
1385                                           TP_ACPI_HKEY_BRGHTDWN_MASK,
1386         TP_NVRAM_HKEY_GROUP_VOLUME      = TP_ACPI_HKEY_VOLUP_MASK |
1387                                           TP_ACPI_HKEY_VOLDWN_MASK |
1388                                           TP_ACPI_HKEY_MUTE_MASK,
1389 };
1390
1391 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1392 struct tp_nvram_state {
1393        u16 thinkpad_toggle:1;
1394        u16 zoom_toggle:1;
1395        u16 display_toggle:1;
1396        u16 thinklight_toggle:1;
1397        u16 hibernate_toggle:1;
1398        u16 displayexp_toggle:1;
1399        u16 display_state:1;
1400        u16 brightness_toggle:1;
1401        u16 volume_toggle:1;
1402        u16 mute:1;
1403
1404        u8 brightness_level;
1405        u8 volume_level;
1406 };
1407
1408 static struct task_struct *tpacpi_hotkey_task;
1409 static u32 hotkey_source_mask;          /* bit mask 0=ACPI,1=NVRAM */
1410 static int hotkey_poll_freq = 10;       /* Hz */
1411 static struct mutex hotkey_thread_mutex;
1412 static struct mutex hotkey_thread_data_mutex;
1413 static unsigned int hotkey_config_change;
1414
1415 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1416
1417 #define hotkey_source_mask 0U
1418
1419 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1420
1421 static struct mutex hotkey_mutex;
1422
1423 static enum {   /* Reasons for waking up */
1424         TP_ACPI_WAKEUP_NONE = 0,        /* None or unknown */
1425         TP_ACPI_WAKEUP_BAYEJ,           /* Bay ejection request */
1426         TP_ACPI_WAKEUP_UNDOCK,          /* Undock request */
1427 } hotkey_wakeup_reason;
1428
1429 static int hotkey_autosleep_ack;
1430
1431 static u32 hotkey_orig_mask;
1432 static u32 hotkey_all_mask;
1433 static u32 hotkey_reserved_mask;
1434 static u32 hotkey_mask;
1435
1436 static unsigned int hotkey_report_mode;
1437
1438 static u16 *hotkey_keycode_map;
1439
1440 static struct attribute_set *hotkey_dev_attributes;
1441
1442 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1443 #define HOTKEY_CONFIG_CRITICAL_START \
1444         do { \
1445                 mutex_lock(&hotkey_thread_data_mutex); \
1446                 hotkey_config_change++; \
1447         } while (0);
1448 #define HOTKEY_CONFIG_CRITICAL_END \
1449         mutex_unlock(&hotkey_thread_data_mutex);
1450 #else
1451 #define HOTKEY_CONFIG_CRITICAL_START
1452 #define HOTKEY_CONFIG_CRITICAL_END
1453 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1454
1455 /* HKEY.MHKG() return bits */
1456 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1457
1458 static int hotkey_get_wlsw(int *status)
1459 {
1460 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1461         if (dbg_wlswemul) {
1462                 *status = !!tpacpi_wlsw_emulstate;
1463                 return 0;
1464         }
1465 #endif
1466         if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1467                 return -EIO;
1468         return 0;
1469 }
1470
1471 static int hotkey_get_tablet_mode(int *status)
1472 {
1473         int s;
1474
1475         if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1476                 return -EIO;
1477
1478         *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1479         return 0;
1480 }
1481
1482 /*
1483  * Call with hotkey_mutex held
1484  */
1485 static int hotkey_mask_get(void)
1486 {
1487         u32 m = 0;
1488
1489         if (tp_features.hotkey_mask) {
1490                 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1491                         return -EIO;
1492         }
1493         hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1494
1495         return 0;
1496 }
1497
1498 /*
1499  * Call with hotkey_mutex held
1500  */
1501 static int hotkey_mask_set(u32 mask)
1502 {
1503         int i;
1504         int rc = 0;
1505
1506         if (tp_features.hotkey_mask) {
1507                 if (!tp_warned.hotkey_mask_ff &&
1508                     (mask == 0xffff || mask == 0xffffff ||
1509                      mask == 0xffffffff)) {
1510                         tp_warned.hotkey_mask_ff = 1;
1511                         printk(TPACPI_NOTICE
1512                                "setting the hotkey mask to 0x%08x is likely "
1513                                "not the best way to go about it\n", mask);
1514                         printk(TPACPI_NOTICE
1515                                "please consider using the driver defaults, "
1516                                "and refer to up-to-date thinkpad-acpi "
1517                                "documentation\n");
1518                 }
1519
1520                 HOTKEY_CONFIG_CRITICAL_START
1521                 for (i = 0; i < 32; i++) {
1522                         u32 m = 1 << i;
1523                         /* enable in firmware mask only keys not in NVRAM
1524                          * mode, but enable the key in the cached hotkey_mask
1525                          * regardless of mode, or the key will end up
1526                          * disabled by hotkey_mask_get() */
1527                         if (!acpi_evalf(hkey_handle,
1528                                         NULL, "MHKM", "vdd", i + 1,
1529                                         !!((mask & ~hotkey_source_mask) & m))) {
1530                                 rc = -EIO;
1531                                 break;
1532                         } else {
1533                                 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1534                         }
1535                 }
1536                 HOTKEY_CONFIG_CRITICAL_END
1537
1538                 /* hotkey_mask_get must be called unconditionally below */
1539                 if (!hotkey_mask_get() && !rc &&
1540                     (hotkey_mask & ~hotkey_source_mask) !=
1541                      (mask & ~hotkey_source_mask)) {
1542                         printk(TPACPI_NOTICE
1543                                "requested hot key mask 0x%08x, but "
1544                                "firmware forced it to 0x%08x\n",
1545                                mask, hotkey_mask);
1546                 }
1547         } else {
1548 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1549                 HOTKEY_CONFIG_CRITICAL_START
1550                 hotkey_mask = mask & hotkey_source_mask;
1551                 HOTKEY_CONFIG_CRITICAL_END
1552                 hotkey_mask_get();
1553                 if (hotkey_mask != mask) {
1554                         printk(TPACPI_NOTICE
1555                                "requested hot key mask 0x%08x, "
1556                                "forced to 0x%08x (NVRAM poll mask is "
1557                                "0x%08x): no firmware mask support\n",
1558                                mask, hotkey_mask, hotkey_source_mask);
1559                 }
1560 #else
1561                 hotkey_mask_get();
1562                 rc = -ENXIO;
1563 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1564         }
1565
1566         return rc;
1567 }
1568
1569 static int hotkey_status_get(int *status)
1570 {
1571         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1572                 return -EIO;
1573
1574         return 0;
1575 }
1576
1577 static int hotkey_status_set(bool enable)
1578 {
1579         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1580                 return -EIO;
1581
1582         return 0;
1583 }
1584
1585 static void tpacpi_input_send_tabletsw(void)
1586 {
1587         int state;
1588
1589         if (tp_features.hotkey_tablet &&
1590             !hotkey_get_tablet_mode(&state)) {
1591                 mutex_lock(&tpacpi_inputdev_send_mutex);
1592
1593                 input_report_switch(tpacpi_inputdev,
1594                                     SW_TABLET_MODE, !!state);
1595                 input_sync(tpacpi_inputdev);
1596
1597                 mutex_unlock(&tpacpi_inputdev_send_mutex);
1598         }
1599 }
1600
1601 static void tpacpi_input_send_key(unsigned int scancode)
1602 {
1603         unsigned int keycode;
1604
1605         keycode = hotkey_keycode_map[scancode];
1606
1607         if (keycode != KEY_RESERVED) {
1608                 mutex_lock(&tpacpi_inputdev_send_mutex);
1609
1610                 input_report_key(tpacpi_inputdev, keycode, 1);
1611                 if (keycode == KEY_UNKNOWN)
1612                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1613                                     scancode);
1614                 input_sync(tpacpi_inputdev);
1615
1616                 input_report_key(tpacpi_inputdev, keycode, 0);
1617                 if (keycode == KEY_UNKNOWN)
1618                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1619                                     scancode);
1620                 input_sync(tpacpi_inputdev);
1621
1622                 mutex_unlock(&tpacpi_inputdev_send_mutex);
1623         }
1624 }
1625
1626 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1627 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1628
1629 static void tpacpi_hotkey_send_key(unsigned int scancode)
1630 {
1631         tpacpi_input_send_key(scancode);
1632         if (hotkey_report_mode < 2) {
1633                 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1634                                                 0x80, 0x1001 + scancode);
1635         }
1636 }
1637
1638 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1639 {
1640         u8 d;
1641
1642         if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1643                 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1644                 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1645                 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1646                 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1647                 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1648         }
1649         if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1650                 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1651                 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1652         }
1653         if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1654                 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1655                 n->displayexp_toggle =
1656                                 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1657         }
1658         if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1659                 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1660                 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1661                                 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1662                 n->brightness_toggle =
1663                                 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1664         }
1665         if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1666                 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1667                 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1668                                 >> TP_NVRAM_POS_LEVEL_VOLUME;
1669                 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1670                 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1671         }
1672 }
1673
1674 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1675         do { \
1676                 if ((mask & (1 << __scancode)) && \
1677                     oldn->__member != newn->__member) \
1678                 tpacpi_hotkey_send_key(__scancode); \
1679         } while (0)
1680
1681 #define TPACPI_MAY_SEND_KEY(__scancode) \
1682         do { if (mask & (1 << __scancode)) \
1683                 tpacpi_hotkey_send_key(__scancode); } while (0)
1684
1685 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1686                                            struct tp_nvram_state *newn,
1687                                            u32 mask)
1688 {
1689         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1690         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1691         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1692         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1693
1694         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1695
1696         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1697
1698         /* handle volume */
1699         if (oldn->volume_toggle != newn->volume_toggle) {
1700                 if (oldn->mute != newn->mute) {
1701                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1702                 }
1703                 if (oldn->volume_level > newn->volume_level) {
1704                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1705                 } else if (oldn->volume_level < newn->volume_level) {
1706                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1707                 } else if (oldn->mute == newn->mute) {
1708                         /* repeated key presses that didn't change state */
1709                         if (newn->mute) {
1710                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1711                         } else if (newn->volume_level != 0) {
1712                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1713                         } else {
1714                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1715                         }
1716                 }
1717         }
1718
1719         /* handle brightness */
1720         if (oldn->brightness_toggle != newn->brightness_toggle) {
1721                 if (oldn->brightness_level < newn->brightness_level) {
1722                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1723                 } else if (oldn->brightness_level > newn->brightness_level) {
1724                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1725                 } else {
1726                         /* repeated key presses that didn't change state */
1727                         if (newn->brightness_level != 0) {
1728                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1729                         } else {
1730                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1731                         }
1732                 }
1733         }
1734 }
1735
1736 #undef TPACPI_COMPARE_KEY
1737 #undef TPACPI_MAY_SEND_KEY
1738
1739 static int hotkey_kthread(void *data)
1740 {
1741         struct tp_nvram_state s[2];
1742         u32 mask;
1743         unsigned int si, so;
1744         unsigned long t;
1745         unsigned int change_detector, must_reset;
1746
1747         mutex_lock(&hotkey_thread_mutex);
1748
1749         if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1750                 goto exit;
1751
1752         set_freezable();
1753
1754         so = 0;
1755         si = 1;
1756         t = 0;
1757
1758         /* Initial state for compares */
1759         mutex_lock(&hotkey_thread_data_mutex);
1760         change_detector = hotkey_config_change;
1761         mask = hotkey_source_mask & hotkey_mask;
1762         mutex_unlock(&hotkey_thread_data_mutex);
1763         hotkey_read_nvram(&s[so], mask);
1764
1765         while (!kthread_should_stop() && hotkey_poll_freq) {
1766                 if (t == 0)
1767                         t = 1000/hotkey_poll_freq;
1768                 t = msleep_interruptible(t);
1769                 if (unlikely(kthread_should_stop()))
1770                         break;
1771                 must_reset = try_to_freeze();
1772                 if (t > 0 && !must_reset)
1773                         continue;
1774
1775                 mutex_lock(&hotkey_thread_data_mutex);
1776                 if (must_reset || hotkey_config_change != change_detector) {
1777                         /* forget old state on thaw or config change */
1778                         si = so;
1779                         t = 0;
1780                         change_detector = hotkey_config_change;
1781                 }
1782                 mask = hotkey_source_mask & hotkey_mask;
1783                 mutex_unlock(&hotkey_thread_data_mutex);
1784
1785                 if (likely(mask)) {
1786                         hotkey_read_nvram(&s[si], mask);
1787                         if (likely(si != so)) {
1788                                 hotkey_compare_and_issue_event(&s[so], &s[si],
1789                                                                 mask);
1790                         }
1791                 }
1792
1793                 so = si;
1794                 si ^= 1;
1795         }
1796
1797 exit:
1798         mutex_unlock(&hotkey_thread_mutex);
1799         return 0;
1800 }
1801
1802 static void hotkey_poll_stop_sync(void)
1803 {
1804         if (tpacpi_hotkey_task) {
1805                 if (frozen(tpacpi_hotkey_task) ||
1806                     freezing(tpacpi_hotkey_task))
1807                         thaw_process(tpacpi_hotkey_task);
1808
1809                 kthread_stop(tpacpi_hotkey_task);
1810                 tpacpi_hotkey_task = NULL;
1811                 mutex_lock(&hotkey_thread_mutex);
1812                 /* at this point, the thread did exit */
1813                 mutex_unlock(&hotkey_thread_mutex);
1814         }
1815 }
1816
1817 /* call with hotkey_mutex held */
1818 static void hotkey_poll_setup(int may_warn)
1819 {
1820         if ((hotkey_source_mask & hotkey_mask) != 0 &&
1821             hotkey_poll_freq > 0 &&
1822             (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1823                 if (!tpacpi_hotkey_task) {
1824                         tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1825                                         NULL, TPACPI_NVRAM_KTHREAD_NAME);
1826                         if (IS_ERR(tpacpi_hotkey_task)) {
1827                                 tpacpi_hotkey_task = NULL;
1828                                 printk(TPACPI_ERR
1829                                        "could not create kernel thread "
1830                                        "for hotkey polling\n");
1831                         }
1832                 }
1833         } else {
1834                 hotkey_poll_stop_sync();
1835                 if (may_warn &&
1836                     hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1837                         printk(TPACPI_NOTICE
1838                                 "hot keys 0x%08x require polling, "
1839                                 "which is currently disabled\n",
1840                                 hotkey_source_mask);
1841                 }
1842         }
1843 }
1844
1845 static void hotkey_poll_setup_safe(int may_warn)
1846 {
1847         mutex_lock(&hotkey_mutex);
1848         hotkey_poll_setup(may_warn);
1849         mutex_unlock(&hotkey_mutex);
1850 }
1851
1852 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1853
1854 static void hotkey_poll_setup_safe(int __unused)
1855 {
1856 }
1857
1858 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1859
1860 static int hotkey_inputdev_open(struct input_dev *dev)
1861 {
1862         switch (tpacpi_lifecycle) {
1863         case TPACPI_LIFE_INIT:
1864                 /*
1865                  * hotkey_init will call hotkey_poll_setup_safe
1866                  * at the appropriate moment
1867                  */
1868                 return 0;
1869         case TPACPI_LIFE_EXITING:
1870                 return -EBUSY;
1871         case TPACPI_LIFE_RUNNING:
1872                 hotkey_poll_setup_safe(0);
1873                 return 0;
1874         }
1875
1876         /* Should only happen if tpacpi_lifecycle is corrupt */
1877         BUG();
1878         return -EBUSY;
1879 }
1880
1881 static void hotkey_inputdev_close(struct input_dev *dev)
1882 {
1883         /* disable hotkey polling when possible */
1884         if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1885                 hotkey_poll_setup_safe(0);
1886 }
1887
1888 /* sysfs hotkey enable ------------------------------------------------- */
1889 static ssize_t hotkey_enable_show(struct device *dev,
1890                            struct device_attribute *attr,
1891                            char *buf)
1892 {
1893         int res, status;
1894
1895         printk_deprecated_attribute("hotkey_enable",
1896                         "Hotkey reporting is always enabled");
1897
1898         res = hotkey_status_get(&status);
1899         if (res)
1900                 return res;
1901
1902         return snprintf(buf, PAGE_SIZE, "%d\n", status);
1903 }
1904
1905 static ssize_t hotkey_enable_store(struct device *dev,
1906                             struct device_attribute *attr,
1907                             const char *buf, size_t count)
1908 {
1909         unsigned long t;
1910
1911         printk_deprecated_attribute("hotkey_enable",
1912                         "Hotkeys can be disabled through hotkey_mask");
1913
1914         if (parse_strtoul(buf, 1, &t))
1915                 return -EINVAL;
1916
1917         if (t == 0)
1918                 return -EPERM;
1919
1920         return count;
1921 }
1922
1923 static struct device_attribute dev_attr_hotkey_enable =
1924         __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1925                 hotkey_enable_show, hotkey_enable_store);
1926
1927 /* sysfs hotkey mask --------------------------------------------------- */
1928 static ssize_t hotkey_mask_show(struct device *dev,
1929                            struct device_attribute *attr,
1930                            char *buf)
1931 {
1932         int res;
1933
1934         if (mutex_lock_killable(&hotkey_mutex))
1935                 return -ERESTARTSYS;
1936         res = hotkey_mask_get();
1937         mutex_unlock(&hotkey_mutex);
1938
1939         return (res)?
1940                 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1941 }
1942
1943 static ssize_t hotkey_mask_store(struct device *dev,
1944                             struct device_attribute *attr,
1945                             const char *buf, size_t count)
1946 {
1947         unsigned long t;
1948         int res;
1949
1950         if (parse_strtoul(buf, 0xffffffffUL, &t))
1951                 return -EINVAL;
1952
1953         if (mutex_lock_killable(&hotkey_mutex))
1954                 return -ERESTARTSYS;
1955
1956         res = hotkey_mask_set(t);
1957
1958 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1959         hotkey_poll_setup(1);
1960 #endif
1961
1962         mutex_unlock(&hotkey_mutex);
1963
1964         return (res) ? res : count;
1965 }
1966
1967 static struct device_attribute dev_attr_hotkey_mask =
1968         __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1969                 hotkey_mask_show, hotkey_mask_store);
1970
1971 /* sysfs hotkey bios_enabled ------------------------------------------- */
1972 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1973                            struct device_attribute *attr,
1974                            char *buf)
1975 {
1976         return sprintf(buf, "0\n");
1977 }
1978
1979 static struct device_attribute dev_attr_hotkey_bios_enabled =
1980         __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1981
1982 /* sysfs hotkey bios_mask ---------------------------------------------- */
1983 static ssize_t hotkey_bios_mask_show(struct device *dev,
1984                            struct device_attribute *attr,
1985                            char *buf)
1986 {
1987         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1988 }
1989
1990 static struct device_attribute dev_attr_hotkey_bios_mask =
1991         __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1992
1993 /* sysfs hotkey all_mask ----------------------------------------------- */
1994 static ssize_t hotkey_all_mask_show(struct device *dev,
1995                            struct device_attribute *attr,
1996                            char *buf)
1997 {
1998         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1999                                 hotkey_all_mask | hotkey_source_mask);
2000 }
2001
2002 static struct device_attribute dev_attr_hotkey_all_mask =
2003         __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2004
2005 /* sysfs hotkey recommended_mask --------------------------------------- */
2006 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2007                                             struct device_attribute *attr,
2008                                             char *buf)
2009 {
2010         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2011                         (hotkey_all_mask | hotkey_source_mask)
2012                         & ~hotkey_reserved_mask);
2013 }
2014
2015 static struct device_attribute dev_attr_hotkey_recommended_mask =
2016         __ATTR(hotkey_recommended_mask, S_IRUGO,
2017                 hotkey_recommended_mask_show, NULL);
2018
2019 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2020
2021 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2022 static ssize_t hotkey_source_mask_show(struct device *dev,
2023                            struct device_attribute *attr,
2024                            char *buf)
2025 {
2026         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2027 }
2028
2029 static ssize_t hotkey_source_mask_store(struct device *dev,
2030                             struct device_attribute *attr,
2031                             const char *buf, size_t count)
2032 {
2033         unsigned long t;
2034
2035         if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2036                 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2037                 return -EINVAL;
2038
2039         if (mutex_lock_killable(&hotkey_mutex))
2040                 return -ERESTARTSYS;
2041
2042         HOTKEY_CONFIG_CRITICAL_START
2043         hotkey_source_mask = t;
2044         HOTKEY_CONFIG_CRITICAL_END
2045
2046         hotkey_poll_setup(1);
2047
2048         mutex_unlock(&hotkey_mutex);
2049
2050         return count;
2051 }
2052
2053 static struct device_attribute dev_attr_hotkey_source_mask =
2054         __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2055                 hotkey_source_mask_show, hotkey_source_mask_store);
2056
2057 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2058 static ssize_t hotkey_poll_freq_show(struct device *dev,
2059                            struct device_attribute *attr,
2060                            char *buf)
2061 {
2062         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2063 }
2064
2065 static ssize_t hotkey_poll_freq_store(struct device *dev,
2066                             struct device_attribute *attr,
2067                             const char *buf, size_t count)
2068 {
2069         unsigned long t;
2070
2071         if (parse_strtoul(buf, 25, &t))
2072                 return -EINVAL;
2073
2074         if (mutex_lock_killable(&hotkey_mutex))
2075                 return -ERESTARTSYS;
2076
2077         hotkey_poll_freq = t;
2078
2079         hotkey_poll_setup(1);
2080         mutex_unlock(&hotkey_mutex);
2081
2082         return count;
2083 }
2084
2085 static struct device_attribute dev_attr_hotkey_poll_freq =
2086         __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2087                 hotkey_poll_freq_show, hotkey_poll_freq_store);
2088
2089 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2090
2091 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2092 static ssize_t hotkey_radio_sw_show(struct device *dev,
2093                            struct device_attribute *attr,
2094                            char *buf)
2095 {
2096         int res, s;
2097         res = hotkey_get_wlsw(&s);
2098         if (res < 0)
2099                 return res;
2100
2101         return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2102 }
2103
2104 static struct device_attribute dev_attr_hotkey_radio_sw =
2105         __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2106
2107 static void hotkey_radio_sw_notify_change(void)
2108 {
2109         if (tp_features.hotkey_wlsw)
2110                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2111                              "hotkey_radio_sw");
2112 }
2113
2114 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2115 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2116                            struct device_attribute *attr,
2117                            char *buf)
2118 {
2119         int res, s;
2120         res = hotkey_get_tablet_mode(&s);
2121         if (res < 0)
2122                 return res;
2123
2124         return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2125 }
2126
2127 static struct device_attribute dev_attr_hotkey_tablet_mode =
2128         __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2129
2130 static void hotkey_tablet_mode_notify_change(void)
2131 {
2132         if (tp_features.hotkey_tablet)
2133                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2134                              "hotkey_tablet_mode");
2135 }
2136
2137 /* sysfs hotkey report_mode -------------------------------------------- */
2138 static ssize_t hotkey_report_mode_show(struct device *dev,
2139                            struct device_attribute *attr,
2140                            char *buf)
2141 {
2142         return snprintf(buf, PAGE_SIZE, "%d\n",
2143                 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2144 }
2145
2146 static struct device_attribute dev_attr_hotkey_report_mode =
2147         __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2148
2149 /* sysfs wakeup reason (pollable) -------------------------------------- */
2150 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2151                            struct device_attribute *attr,
2152                            char *buf)
2153 {
2154         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2155 }
2156
2157 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2158         __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2159
2160 static void hotkey_wakeup_reason_notify_change(void)
2161 {
2162         if (tp_features.hotkey_mask)
2163                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2164                              "wakeup_reason");
2165 }
2166
2167 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2168 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2169                            struct device_attribute *attr,
2170                            char *buf)
2171 {
2172         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2173 }
2174
2175 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2176         __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2177                hotkey_wakeup_hotunplug_complete_show, NULL);
2178
2179 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2180 {
2181         if (tp_features.hotkey_mask)
2182                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2183                              "wakeup_hotunplug_complete");
2184 }
2185
2186 /* --------------------------------------------------------------------- */
2187
2188 static struct attribute *hotkey_attributes[] __initdata = {
2189         &dev_attr_hotkey_enable.attr,
2190         &dev_attr_hotkey_bios_enabled.attr,
2191         &dev_attr_hotkey_report_mode.attr,
2192 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2193         &dev_attr_hotkey_mask.attr,
2194         &dev_attr_hotkey_all_mask.attr,
2195         &dev_attr_hotkey_recommended_mask.attr,
2196         &dev_attr_hotkey_source_mask.attr,
2197         &dev_attr_hotkey_poll_freq.attr,
2198 #endif
2199 };
2200
2201 static struct attribute *hotkey_mask_attributes[] __initdata = {
2202         &dev_attr_hotkey_bios_mask.attr,
2203 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2204         &dev_attr_hotkey_mask.attr,
2205         &dev_attr_hotkey_all_mask.attr,
2206         &dev_attr_hotkey_recommended_mask.attr,
2207 #endif
2208         &dev_attr_hotkey_wakeup_reason.attr,
2209         &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2210 };
2211
2212 static void bluetooth_update_rfk(void);
2213 static void wan_update_rfk(void);
2214 static void uwb_update_rfk(void);
2215 static void tpacpi_send_radiosw_update(void)
2216 {
2217         int wlsw;
2218
2219         /* Sync these BEFORE sending any rfkill events */
2220         if (tp_features.bluetooth)
2221                 bluetooth_update_rfk();
2222         if (tp_features.wan)
2223                 wan_update_rfk();
2224         if (tp_features.uwb)
2225                 uwb_update_rfk();
2226
2227         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2228                 mutex_lock(&tpacpi_inputdev_send_mutex);
2229
2230                 input_report_switch(tpacpi_inputdev,
2231                                     SW_RFKILL_ALL, !!wlsw);
2232                 input_sync(tpacpi_inputdev);
2233
2234                 mutex_unlock(&tpacpi_inputdev_send_mutex);
2235         }
2236         hotkey_radio_sw_notify_change();
2237 }
2238
2239 static void hotkey_exit(void)
2240 {
2241 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2242         hotkey_poll_stop_sync();
2243 #endif
2244
2245         if (hotkey_dev_attributes)
2246                 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2247
2248         kfree(hotkey_keycode_map);
2249
2250         if (tp_features.hotkey) {
2251                 dbg_printk(TPACPI_DBG_EXIT,
2252                            "restoring original hot key mask\n");
2253                 /* no short-circuit boolean operator below! */
2254                 if ((hotkey_mask_set(hotkey_orig_mask) |
2255                      hotkey_status_set(false)) != 0)
2256                         printk(TPACPI_ERR
2257                                "failed to restore hot key mask "
2258                                "to BIOS defaults\n");
2259         }
2260 }
2261
2262 static int __init hotkey_init(struct ibm_init_struct *iibm)
2263 {
2264         /* Requirements for changing the default keymaps:
2265          *
2266          * 1. Many of the keys are mapped to KEY_RESERVED for very
2267          *    good reasons.  Do not change them unless you have deep
2268          *    knowledge on the IBM and Lenovo ThinkPad firmware for
2269          *    the various ThinkPad models.  The driver behaves
2270          *    differently for KEY_RESERVED: such keys have their
2271          *    hot key mask *unset* in mask_recommended, and also
2272          *    in the initial hot key mask programmed into the
2273          *    firmware at driver load time, which means the firm-
2274          *    ware may react very differently if you change them to
2275          *    something else;
2276          *
2277          * 2. You must be subscribed to the linux-thinkpad and
2278          *    ibm-acpi-devel mailing lists, and you should read the
2279          *    list archives since 2007 if you want to change the
2280          *    keymaps.  This requirement exists so that you will
2281          *    know the past history of problems with the thinkpad-
2282          *    acpi driver keymaps, and also that you will be
2283          *    listening to any bug reports;
2284          *
2285          * 3. Do not send thinkpad-acpi specific patches directly to
2286          *    for merging, *ever*.  Send them to the linux-acpi
2287          *    mailinglist for comments.  Merging is to be done only
2288          *    through acpi-test and the ACPI maintainer.
2289          *
2290          * If the above is too much to ask, don't change the keymap.
2291          * Ask the thinkpad-acpi maintainer to do it, instead.
2292          */
2293         static u16 ibm_keycode_map[] __initdata = {
2294                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2295                 KEY_FN_F1,      KEY_FN_F2,      KEY_COFFEE,     KEY_SLEEP,
2296                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2297                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
2298
2299                 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2300                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
2301                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
2302                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
2303
2304                 /* brightness: firmware always reacts to them, unless
2305                  * X.org did some tricks in the radeon BIOS scratch
2306                  * registers of *some* models */
2307                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
2308                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
2309
2310                 /* Thinklight: firmware always react to it */
2311                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
2312
2313                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
2314                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
2315
2316                 /* Volume: firmware always react to it and reprograms
2317                  * the built-in *extra* mixer.  Never map it to control
2318                  * another mixer by default. */
2319                 KEY_RESERVED,   /* 0x14: VOLUME UP */
2320                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
2321                 KEY_RESERVED,   /* 0x16: MUTE */
2322
2323                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
2324
2325                 /* (assignments unknown, please report if found) */
2326                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2327                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2328         };
2329         static u16 lenovo_keycode_map[] __initdata = {
2330                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2331                 KEY_FN_F1,      KEY_COFFEE,     KEY_BATTERY,    KEY_SLEEP,
2332                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2333                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
2334
2335                 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2336                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
2337                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
2338                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
2339
2340                 /* These either have to go through ACPI video, or
2341                  * act like in the IBM ThinkPads, so don't ever
2342                  * enable them by default */
2343                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
2344                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
2345
2346                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
2347
2348                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
2349                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
2350
2351                 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2352                  * react to it and reprograms the built-in *extra* mixer.
2353                  * Never map it to control another mixer by default.
2354                  *
2355                  * T60?, T61, R60?, R61: firmware and EC tries to send
2356                  * these over the regular keyboard, so these are no-ops,
2357                  * but there are still weird bugs re. MUTE, so do not
2358                  * change unless you get test reports from all Lenovo
2359                  * models.  May cause the BIOS to interfere with the
2360                  * HDA mixer.
2361                  */
2362                 KEY_RESERVED,   /* 0x14: VOLUME UP */
2363                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
2364                 KEY_RESERVED,   /* 0x16: MUTE */
2365
2366                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
2367
2368                 /* (assignments unknown, please report if found) */
2369                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2370                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2371         };
2372
2373 #define TPACPI_HOTKEY_MAP_LEN           ARRAY_SIZE(ibm_keycode_map)
2374 #define TPACPI_HOTKEY_MAP_SIZE          sizeof(ibm_keycode_map)
2375 #define TPACPI_HOTKEY_MAP_TYPESIZE      sizeof(ibm_keycode_map[0])
2376
2377         int res, i;
2378         int status;
2379         int hkeyv;
2380
2381         vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2382
2383         BUG_ON(!tpacpi_inputdev);
2384         BUG_ON(tpacpi_inputdev->open != NULL ||
2385                tpacpi_inputdev->close != NULL);
2386
2387         TPACPI_ACPIHANDLE_INIT(hkey);
2388         mutex_init(&hotkey_mutex);
2389
2390 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2391         mutex_init(&hotkey_thread_mutex);
2392         mutex_init(&hotkey_thread_data_mutex);
2393 #endif
2394
2395         /* hotkey not supported on 570 */
2396         tp_features.hotkey = hkey_handle != NULL;
2397
2398         vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2399                 str_supported(tp_features.hotkey));
2400
2401         if (!tp_features.hotkey)
2402                 return 1;
2403
2404         tpacpi_disable_brightness_delay();
2405
2406         hotkey_dev_attributes = create_attr_set(13, NULL);
2407         if (!hotkey_dev_attributes)
2408                 return -ENOMEM;
2409         res = add_many_to_attr_set(hotkey_dev_attributes,
2410                         hotkey_attributes,
2411                         ARRAY_SIZE(hotkey_attributes));
2412         if (res)
2413                 goto err_exit;
2414
2415         /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2416            A30, R30, R31, T20-22, X20-21, X22-24.  Detected by checking
2417            for HKEY interface version 0x100 */
2418         if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2419                 if ((hkeyv >> 8) != 1) {
2420                         printk(TPACPI_ERR "unknown version of the "
2421                                "HKEY interface: 0x%x\n", hkeyv);
2422                         printk(TPACPI_ERR "please report this to %s\n",
2423                                TPACPI_MAIL);
2424                 } else {
2425                         /*
2426                          * MHKV 0x100 in A31, R40, R40e,
2427                          * T4x, X31, and later
2428                          */
2429                         tp_features.hotkey_mask = 1;
2430                 }
2431         }
2432
2433         vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2434                 str_supported(tp_features.hotkey_mask));
2435
2436         if (tp_features.hotkey_mask) {
2437                 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2438                                 "MHKA", "qd")) {
2439                         printk(TPACPI_ERR
2440                                "missing MHKA handler, "
2441                                "please report this to %s\n",
2442                                TPACPI_MAIL);
2443                         /* FN+F12, FN+F4, FN+F3 */
2444                         hotkey_all_mask = 0x080cU;
2445                 }
2446         }
2447
2448         /* hotkey_source_mask *must* be zero for
2449          * the first hotkey_mask_get */
2450         if (tp_features.hotkey_mask) {
2451                 res = hotkey_mask_get();
2452                 if (res)
2453                         goto err_exit;
2454
2455                 hotkey_orig_mask = hotkey_mask;
2456                 res = add_many_to_attr_set(
2457                                 hotkey_dev_attributes,
2458                                 hotkey_mask_attributes,
2459                                 ARRAY_SIZE(hotkey_mask_attributes));
2460                 if (res)
2461                         goto err_exit;
2462         }
2463
2464 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2465         if (tp_features.hotkey_mask) {
2466                 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2467                                         & ~hotkey_all_mask;
2468         } else {
2469                 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2470         }
2471
2472         vdbg_printk(TPACPI_DBG_INIT,
2473                     "hotkey source mask 0x%08x, polling freq %d\n",
2474                     hotkey_source_mask, hotkey_poll_freq);
2475 #endif
2476
2477 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2478         if (dbg_wlswemul) {
2479                 tp_features.hotkey_wlsw = 1;
2480                 printk(TPACPI_INFO
2481                         "radio switch emulation enabled\n");
2482         } else
2483 #endif
2484         /* Not all thinkpads have a hardware radio switch */
2485         if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2486                 tp_features.hotkey_wlsw = 1;
2487                 printk(TPACPI_INFO
2488                         "radio switch found; radios are %s\n",
2489                         enabled(status, 0));
2490         }
2491         if (tp_features.hotkey_wlsw)
2492                 res = add_to_attr_set(hotkey_dev_attributes,
2493                                 &dev_attr_hotkey_radio_sw.attr);
2494
2495         /* For X41t, X60t, X61t Tablets... */
2496         if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2497                 tp_features.hotkey_tablet = 1;
2498                 printk(TPACPI_INFO
2499                         "possible tablet mode switch found; "
2500                         "ThinkPad in %s mode\n",
2501                         (status & TP_HOTKEY_TABLET_MASK)?
2502                                 "tablet" : "laptop");
2503                 res = add_to_attr_set(hotkey_dev_attributes,
2504                                 &dev_attr_hotkey_tablet_mode.attr);
2505         }
2506
2507         if (!res)
2508                 res = register_attr_set_with_sysfs(
2509                                 hotkey_dev_attributes,
2510                                 &tpacpi_pdev->dev.kobj);
2511         if (res)
2512                 goto err_exit;
2513
2514         /* Set up key map */
2515
2516         hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2517                                         GFP_KERNEL);
2518         if (!hotkey_keycode_map) {
2519                 printk(TPACPI_ERR
2520                         "failed to allocate memory for key map\n");
2521                 res = -ENOMEM;
2522                 goto err_exit;
2523         }
2524
2525         if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2526                 dbg_printk(TPACPI_DBG_INIT,
2527                            "using Lenovo default hot key map\n");
2528                 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2529                         TPACPI_HOTKEY_MAP_SIZE);
2530         } else {
2531                 dbg_printk(TPACPI_DBG_INIT,
2532                            "using IBM default hot key map\n");
2533                 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2534                         TPACPI_HOTKEY_MAP_SIZE);
2535         }
2536
2537         set_bit(EV_KEY, tpacpi_inputdev->evbit);
2538         set_bit(EV_MSC, tpacpi_inputdev->evbit);
2539         set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2540         tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2541         tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2542         tpacpi_inputdev->keycode = hotkey_keycode_map;
2543         for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2544                 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2545                         set_bit(hotkey_keycode_map[i],
2546                                 tpacpi_inputdev->keybit);
2547                 } else {
2548                         if (i < sizeof(hotkey_reserved_mask)*8)
2549                                 hotkey_reserved_mask |= 1 << i;
2550                 }
2551         }
2552
2553         if (tp_features.hotkey_wlsw) {
2554                 set_bit(EV_SW, tpacpi_inputdev->evbit);
2555                 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2556         }
2557         if (tp_features.hotkey_tablet) {
2558                 set_bit(EV_SW, tpacpi_inputdev->evbit);
2559                 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2560         }
2561
2562         /* Do not issue duplicate brightness change events to
2563          * userspace */
2564         if (!tp_features.bright_acpimode)
2565                 /* update bright_acpimode... */
2566                 tpacpi_check_std_acpi_brightness_support();
2567
2568         if (tp_features.bright_acpimode) {
2569                 printk(TPACPI_INFO
2570                        "This ThinkPad has standard ACPI backlight "
2571                        "brightness control, supported by the ACPI "
2572                        "video driver\n");
2573                 printk(TPACPI_NOTICE
2574                        "Disabling thinkpad-acpi brightness events "
2575                        "by default...\n");
2576
2577                 /* The hotkey_reserved_mask change below is not
2578                  * necessary while the keys are at KEY_RESERVED in the
2579                  * default map, but better safe than sorry, leave it
2580                  * here as a marker of what we have to do, especially
2581                  * when we finally become able to set this at runtime
2582                  * on response to X.org requests */
2583                 hotkey_reserved_mask |=
2584                         (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2585                         | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2586         }
2587
2588         dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2589         res = hotkey_status_set(true);
2590         if (res) {
2591                 hotkey_exit();
2592                 return res;
2593         }
2594         res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2595                                 & ~hotkey_reserved_mask)
2596                                 | hotkey_orig_mask);
2597         if (res < 0 && res != -ENXIO) {
2598                 hotkey_exit();
2599                 return res;
2600         }
2601
2602         dbg_printk(TPACPI_DBG_INIT,
2603                         "legacy hot key reporting over procfs %s\n",
2604                         (hotkey_report_mode < 2) ?
2605                                 "enabled" : "disabled");
2606
2607         tpacpi_inputdev->open = &hotkey_inputdev_open;
2608         tpacpi_inputdev->close = &hotkey_inputdev_close;
2609
2610         hotkey_poll_setup_safe(1);
2611         tpacpi_send_radiosw_update();
2612         tpacpi_input_send_tabletsw();
2613
2614         return 0;
2615
2616 err_exit:
2617         delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2618         hotkey_dev_attributes = NULL;
2619
2620         return (res < 0)? res : 1;
2621 }
2622
2623 static bool hotkey_notify_hotkey(const u32 hkey,
2624                                  bool *send_acpi_ev,
2625                                  bool *ignore_acpi_ev)
2626 {
2627         /* 0x1000-0x1FFF: key presses */
2628         unsigned int scancode = hkey & 0xfff;
2629         *send_acpi_ev = true;
2630         *ignore_acpi_ev = false;
2631
2632         if (scancode > 0 && scancode < 0x21) {
2633                 scancode--;
2634                 if (!(hotkey_source_mask & (1 << scancode))) {
2635                         tpacpi_input_send_key(scancode);
2636                         *send_acpi_ev = false;
2637                 } else {
2638                         *ignore_acpi_ev = true;
2639                 }
2640                 return true;
2641         }
2642         return false;
2643 }
2644
2645 static bool hotkey_notify_wakeup(const u32 hkey,
2646                                  bool *send_acpi_ev,
2647                                  bool *ignore_acpi_ev)
2648 {
2649         /* 0x2000-0x2FFF: Wakeup reason */
2650         *send_acpi_ev = true;
2651         *ignore_acpi_ev = false;
2652
2653         switch (hkey) {
2654         case 0x2304: /* suspend, undock */
2655         case 0x2404: /* hibernation, undock */
2656                 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2657                 *ignore_acpi_ev = true;
2658                 break;
2659
2660         case 0x2305: /* suspend, bay eject */
2661         case 0x2405: /* hibernation, bay eject */
2662                 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2663                 *ignore_acpi_ev = true;
2664                 break;
2665
2666         case 0x2313: /* Battery on critical low level (S3) */
2667         case 0x2413: /* Battery on critical low level (S4) */
2668                 printk(TPACPI_ALERT
2669                         "EMERGENCY WAKEUP: battery almost empty\n");
2670                 /* how to auto-heal: */
2671                 /* 2313: woke up from S3, go to S4/S5 */
2672                 /* 2413: woke up from S4, go to S5 */
2673                 break;
2674
2675         default:
2676                 return false;
2677         }
2678
2679         if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2680                 printk(TPACPI_INFO
2681                        "woke up due to a hot-unplug "
2682                        "request...\n");
2683                 hotkey_wakeup_reason_notify_change();
2684         }
2685         return true;
2686 }
2687
2688 static bool hotkey_notify_usrevent(const u32 hkey,
2689                                  bool *send_acpi_ev,
2690                                  bool *ignore_acpi_ev)
2691 {
2692         /* 0x5000-0x5FFF: human interface helpers */
2693         *send_acpi_ev = true;
2694         *ignore_acpi_ev = false;
2695
2696         switch (hkey) {
2697         case 0x5010: /* Lenovo new BIOS: brightness changed */
2698         case 0x500b: /* X61t: tablet pen inserted into bay */
2699         case 0x500c: /* X61t: tablet pen removed from bay */
2700                 return true;
2701
2702         case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2703         case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2704                 tpacpi_input_send_tabletsw();
2705                 hotkey_tablet_mode_notify_change();
2706                 *send_acpi_ev = false;
2707                 return true;
2708
2709         case 0x5001:
2710         case 0x5002:
2711                 /* LID switch events.  Do not propagate */
2712                 *ignore_acpi_ev = true;
2713                 return true;
2714
2715         default:
2716                 return false;
2717         }
2718 }
2719
2720 static bool hotkey_notify_thermal(const u32 hkey,
2721                                  bool *send_acpi_ev,
2722                                  bool *ignore_acpi_ev)
2723 {
2724         /* 0x6000-0x6FFF: thermal alarms */
2725         *send_acpi_ev = true;
2726         *ignore_acpi_ev = false;
2727
2728         switch (hkey) {
2729         case 0x6011:
2730                 printk(TPACPI_CRIT
2731                         "THERMAL ALARM: battery is too hot!\n");
2732                 /* recommended action: warn user through gui */
2733                 return true;
2734         case 0x6012:
2735                 printk(TPACPI_ALERT
2736                         "THERMAL EMERGENCY: battery is extremely hot!\n");
2737                 /* recommended action: immediate sleep/hibernate */
2738                 return true;
2739         case 0x6021:
2740                 printk(TPACPI_CRIT
2741                         "THERMAL ALARM: "
2742                         "a sensor reports something is too hot!\n");
2743                 /* recommended action: warn user through gui, that */
2744                 /* some internal component is too hot */
2745                 return true;
2746         case 0x6022:
2747                 printk(TPACPI_ALERT
2748                         "THERMAL EMERGENCY: "
2749                         "a sensor reports something is extremely hot!\n");
2750                 /* recommended action: immediate sleep/hibernate */
2751                 return true;
2752         case 0x6030:
2753                 printk(TPACPI_INFO
2754                         "EC reports that Thermal Table has changed\n");
2755                 /* recommended action: do nothing, we don't have
2756                  * Lenovo ATM information */
2757                 return true;
2758         default:
2759                 printk(TPACPI_ALERT
2760                          "THERMAL ALERT: unknown thermal alarm received\n");
2761                 return false;
2762         }
2763 }
2764
2765 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2766 {
2767         u32 hkey;
2768         bool send_acpi_ev;
2769         bool ignore_acpi_ev;
2770         bool known_ev;
2771
2772         if (event != 0x80) {
2773                 printk(TPACPI_ERR
2774                        "unknown HKEY notification event %d\n", event);
2775                 /* forward it to userspace, maybe it knows how to handle it */
2776                 acpi_bus_generate_netlink_event(
2777                                         ibm->acpi->device->pnp.device_class,
2778                                         dev_name(&ibm->acpi->device->dev),
2779                                         event, 0);
2780                 return;
2781         }
2782
2783         while (1) {
2784                 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2785                         printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2786                         return;
2787                 }
2788
2789                 if (hkey == 0) {
2790                         /* queue empty */
2791                         return;
2792                 }
2793
2794                 send_acpi_ev = true;
2795                 ignore_acpi_ev = false;
2796
2797                 switch (hkey >> 12) {
2798                 case 1:
2799                         /* 0x1000-0x1FFF: key presses */
2800                         known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2801                                                  &ignore_acpi_ev);
2802                         break;
2803                 case 2:
2804                         /* 0x2000-0x2FFF: Wakeup reason */
2805                         known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2806                                                  &ignore_acpi_ev);
2807                         break;
2808                 case 3:
2809                         /* 0x3000-0x3FFF: bay-related wakeups */
2810                         if (hkey == 0x3003) {
2811                                 hotkey_autosleep_ack = 1;
2812                                 printk(TPACPI_INFO
2813                                        "bay ejected\n");
2814                                 hotkey_wakeup_hotunplug_complete_notify_change();
2815                                 known_ev = true;
2816                         } else {
2817                                 known_ev = false;
2818                         }
2819                         break;
2820                 case 4:
2821                         /* 0x4000-0x4FFF: dock-related wakeups */
2822                         if (hkey == 0x4003) {
2823                                 hotkey_autosleep_ack = 1;
2824                                 printk(TPACPI_INFO
2825                                        "undocked\n");
2826                                 hotkey_wakeup_hotunplug_complete_notify_change();
2827                                 known_ev = true;
2828                         } else {
2829                                 known_ev = false;
2830                         }
2831                         break;
2832                 case 5:
2833                         /* 0x5000-0x5FFF: human interface helpers */
2834                         known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2835                                                  &ignore_acpi_ev);
2836                         break;
2837                 case 6:
2838                         /* 0x6000-0x6FFF: thermal alarms */
2839                         known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2840                                                  &ignore_acpi_ev);
2841                         break;
2842                 case 7:
2843                         /* 0x7000-0x7FFF: misc */
2844                         if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2845                                 tpacpi_send_radiosw_update();
2846                                 send_acpi_ev = 0;
2847                                 known_ev = true;
2848                                 break;
2849                         }
2850                         /* fallthrough to default */
2851                 default:
2852                         known_ev = false;
2853                 }
2854                 if (!known_ev) {
2855                         printk(TPACPI_NOTICE
2856                                "unhandled HKEY event 0x%04x\n", hkey);
2857                         printk(TPACPI_NOTICE
2858                                "please report the conditions when this "
2859                                "event happened to %s\n", TPACPI_MAIL);
2860                 }
2861
2862                 /* Legacy events */
2863                 if (!ignore_acpi_ev &&
2864                     (send_acpi_ev || hotkey_report_mode < 2)) {
2865                         acpi_bus_generate_proc_event(ibm->acpi->device,
2866                                                      event, hkey);
2867                 }
2868
2869                 /* netlink events */
2870                 if (!ignore_acpi_ev && send_acpi_ev) {
2871                         acpi_bus_generate_netlink_event(
2872                                         ibm->acpi->device->pnp.device_class,
2873                                         dev_name(&ibm->acpi->device->dev),
2874                                         event, hkey);
2875                 }
2876         }
2877 }
2878
2879 static void hotkey_suspend(pm_message_t state)
2880 {
2881         /* Do these on suspend, we get the events on early resume! */
2882         hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2883         hotkey_autosleep_ack = 0;
2884 }
2885
2886 static void hotkey_resume(void)
2887 {
2888         tpacpi_disable_brightness_delay();
2889
2890         if (hotkey_mask_get())
2891                 printk(TPACPI_ERR
2892                        "error while trying to read hot key mask "
2893                        "from firmware\n");
2894         tpacpi_send_radiosw_update();
2895         hotkey_tablet_mode_notify_change();
2896         hotkey_wakeup_reason_notify_change();
2897         hotkey_wakeup_hotunplug_complete_notify_change();
2898         hotkey_poll_setup_safe(0);
2899 }
2900
2901 /* procfs -------------------------------------------------------------- */
2902 static int hotkey_read(char *p)
2903 {
2904         int res, status;
2905         int len = 0;
2906
2907         if (!tp_features.hotkey) {
2908                 len += sprintf(p + len, "status:\t\tnot supported\n");
2909                 return len;
2910         }
2911
2912         if (mutex_lock_killable(&hotkey_mutex))
2913                 return -ERESTARTSYS;
2914         res = hotkey_status_get(&status);
2915         if (!res)
2916                 res = hotkey_mask_get();
2917         mutex_unlock(&hotkey_mutex);
2918         if (res)
2919                 return res;
2920
2921         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2922         if (tp_features.hotkey_mask) {
2923                 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2924                 len += sprintf(p + len,
2925                                "commands:\tenable, disable, reset, <mask>\n");
2926         } else {
2927                 len += sprintf(p + len, "mask:\t\tnot supported\n");
2928                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2929         }
2930
2931         return len;
2932 }
2933
2934 static void hotkey_enabledisable_warn(void)
2935 {
2936         tpacpi_log_usertask("procfs hotkey enable/disable");
2937         WARN(1, TPACPI_WARN
2938              "hotkey enable/disable functionality has been "
2939              "removed from the driver. Hotkeys are always enabled.\n");
2940 }
2941
2942 static int hotkey_write(char *buf)
2943 {
2944         int res;
2945         u32 mask;
2946         char *cmd;
2947
2948         if (!tp_features.hotkey)
2949                 return -ENODEV;
2950
2951         if (mutex_lock_killable(&hotkey_mutex))
2952                 return -ERESTARTSYS;
2953
2954         mask = hotkey_mask;
2955
2956         res = 0;
2957         while ((cmd = next_cmd(&buf))) {
2958                 if (strlencmp(cmd, "enable") == 0) {
2959                         hotkey_enabledisable_warn();
2960                 } else if (strlencmp(cmd, "disable") == 0) {
2961                         hotkey_enabledisable_warn();
2962                         res = -EPERM;
2963                 } else if (strlencmp(cmd, "reset") == 0) {
2964                         mask = hotkey_orig_mask;
2965                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2966                         /* mask set */
2967                 } else if (sscanf(cmd, "%x", &mask) == 1) {
2968                         /* mask set */
2969                 } else {
2970                         res = -EINVAL;
2971                         goto errexit;
2972                 }
2973         }
2974         if (!res && mask != hotkey_mask)
2975                 res = hotkey_mask_set(mask);
2976
2977 errexit:
2978         mutex_unlock(&hotkey_mutex);
2979         return res;
2980 }
2981
2982 static const struct acpi_device_id ibm_htk_device_ids[] = {
2983         {TPACPI_ACPI_HKEY_HID, 0},
2984         {"", 0},
2985 };
2986
2987 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2988         .hid = ibm_htk_device_ids,
2989         .notify = hotkey_notify,
2990         .handle = &hkey_handle,
2991         .type = ACPI_DEVICE_NOTIFY,
2992 };
2993
2994 static struct ibm_struct hotkey_driver_data = {
2995         .name = "hotkey",
2996         .read = hotkey_read,
2997         .write = hotkey_write,
2998         .exit = hotkey_exit,
2999         .resume = hotkey_resume,
3000         .suspend = hotkey_suspend,
3001         .acpi = &ibm_hotkey_acpidriver,
3002 };
3003
3004 /*************************************************************************
3005  * Bluetooth subdriver
3006  */
3007
3008 enum {
3009         /* ACPI GBDC/SBDC bits */
3010         TP_ACPI_BLUETOOTH_HWPRESENT     = 0x01, /* Bluetooth hw available */
3011         TP_ACPI_BLUETOOTH_RADIOSSW      = 0x02, /* Bluetooth radio enabled */
3012         TP_ACPI_BLUETOOTH_RESUMECTRL    = 0x04, /* Bluetooth state at resume:
3013                                                    off / last state */
3014 };
3015
3016 enum {
3017         /* ACPI \BLTH commands */
3018         TP_ACPI_BLTH_GET_ULTRAPORT_ID   = 0x00, /* Get Ultraport BT ID */
3019         TP_ACPI_BLTH_GET_PWR_ON_RESUME  = 0x01, /* Get power-on-resume state */
3020         TP_ACPI_BLTH_PWR_ON_ON_RESUME   = 0x02, /* Resume powered on */
3021         TP_ACPI_BLTH_PWR_OFF_ON_RESUME  = 0x03, /* Resume powered off */
3022         TP_ACPI_BLTH_SAVE_STATE         = 0x05, /* Save state for S4/S5 */
3023 };
3024
3025 #define TPACPI_RFK_BLUETOOTH_SW_NAME    "tpacpi_bluetooth_sw"
3026
3027 static struct rfkill *tpacpi_bluetooth_rfkill;
3028
3029 static void bluetooth_suspend(pm_message_t state)
3030 {
3031         /* Try to make sure radio will resume powered off */
3032         if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3033                    TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3034                 vdbg_printk(TPACPI_DBG_RFKILL,
3035                         "bluetooth power down on resume request failed\n");
3036 }
3037
3038 static int bluetooth_get_radiosw(void)
3039 {
3040         int status;
3041
3042         if (!tp_features.bluetooth)
3043                 return -ENODEV;
3044
3045         /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3046         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3047                 return RFKILL_STATE_HARD_BLOCKED;
3048
3049 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3050         if (dbg_bluetoothemul)
3051                 return (tpacpi_bluetooth_emulstate) ?
3052                         RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3053 #endif
3054
3055         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3056                 return -EIO;
3057
3058         return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3059                 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3060 }
3061
3062 static void bluetooth_update_rfk(void)
3063 {
3064         int status;
3065
3066         if (!tpacpi_bluetooth_rfkill)
3067                 return;
3068
3069         status = bluetooth_get_radiosw();
3070         if (status < 0)
3071                 return;
3072         rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3073
3074         vdbg_printk(TPACPI_DBG_RFKILL,
3075                 "forced rfkill state to %d\n",
3076                 status);
3077 }
3078
3079 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3080 {
3081         int status;
3082
3083         if (!tp_features.bluetooth)
3084                 return -ENODEV;
3085
3086         /* WLSW overrides bluetooth in firmware/hardware, but there is no
3087          * reason to risk weird behaviour. */
3088         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3089             && radio_on)
3090                 return -EPERM;
3091
3092         vdbg_printk(TPACPI_DBG_RFKILL,
3093                 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3094
3095 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3096         if (dbg_bluetoothemul) {
3097                 tpacpi_bluetooth_emulstate = !!radio_on;
3098                 if (update_rfk)
3099                         bluetooth_update_rfk();
3100                 return 0;
3101         }
3102 #endif
3103
3104         /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3105         if (radio_on)
3106                 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3107         else
3108                 status = 0;
3109         if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3110                 return -EIO;
3111
3112         if (update_rfk)
3113                 bluetooth_update_rfk();
3114
3115         return 0;
3116 }
3117
3118 /* sysfs bluetooth enable ---------------------------------------------- */
3119 static ssize_t bluetooth_enable_show(struct device *dev,
3120                            struct device_attribute *attr,
3121                            char *buf)
3122 {
3123         int status;
3124
3125         printk_deprecated_rfkill_attribute("bluetooth_enable");
3126
3127         status = bluetooth_get_radiosw();
3128         if (status < 0)
3129                 return status;
3130
3131         return snprintf(buf, PAGE_SIZE, "%d\n",
3132                         (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3133 }
3134
3135 static ssize_t bluetooth_enable_store(struct device *dev,
3136                             struct device_attribute *attr,
3137                             const char *buf, size_t count)
3138 {
3139         unsigned long t;
3140         int res;
3141
3142         printk_deprecated_rfkill_attribute("bluetooth_enable");
3143
3144         if (parse_strtoul(buf, 1, &t))
3145                 return -EINVAL;
3146
3147         tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3148
3149         res = bluetooth_set_radiosw(t, 1);
3150
3151         return (res) ? res : count;
3152 }
3153
3154 static struct device_attribute dev_attr_bluetooth_enable =
3155         __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3156                 bluetooth_enable_show, bluetooth_enable_store);
3157
3158 /* --------------------------------------------------------------------- */
3159
3160 static struct attribute *bluetooth_attributes[] = {
3161         &dev_attr_bluetooth_enable.attr,
3162         NULL
3163 };
3164
3165 static const struct attribute_group bluetooth_attr_group = {
3166         .attrs = bluetooth_attributes,
3167 };
3168
3169 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3170 {
3171         int bts = bluetooth_get_radiosw();
3172
3173         if (bts < 0)
3174                 return bts;
3175
3176         *state = bts;
3177         return 0;
3178 }
3179
3180 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3181 {
3182         dbg_printk(TPACPI_DBG_RFKILL,
3183                    "request to change radio state to %d\n", state);
3184         return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3185 }
3186
3187 static void bluetooth_shutdown(void)
3188 {
3189         /* Order firmware to save current state to NVRAM */
3190         if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3191                         TP_ACPI_BLTH_SAVE_STATE))
3192                 printk(TPACPI_NOTICE
3193                         "failed to save bluetooth state to NVRAM\n");
3194         else
3195                 vdbg_printk(TPACPI_DBG_RFKILL,
3196                         "bluestooth state saved to NVRAM\n");
3197 }
3198
3199 static void bluetooth_exit(void)
3200 {
3201         bluetooth_shutdown();
3202
3203         if (tpacpi_bluetooth_rfkill)
3204                 rfkill_unregister(tpacpi_bluetooth_rfkill);
3205
3206         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3207                         &bluetooth_attr_group);
3208 }
3209
3210 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3211 {
3212         int res;
3213         int status = 0;
3214
3215         vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3216                         "initializing bluetooth subdriver\n");
3217
3218         TPACPI_ACPIHANDLE_INIT(hkey);
3219
3220         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3221            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3222         tp_features.bluetooth = hkey_handle &&
3223             acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3224
3225         vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3226                 "bluetooth is %s, status 0x%02x\n",
3227                 str_supported(tp_features.bluetooth),
3228                 status);
3229
3230 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3231         if (dbg_bluetoothemul) {
3232                 tp_features.bluetooth = 1;
3233                 printk(TPACPI_INFO
3234                         "bluetooth switch emulation enabled\n");
3235         } else
3236 #endif
3237         if (tp_features.bluetooth &&
3238             !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3239                 /* no bluetooth hardware present in system */
3240                 tp_features.bluetooth = 0;
3241                 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3242                            "bluetooth hardware not installed\n");
3243         }
3244
3245         if (!tp_features.bluetooth)
3246                 return 1;
3247
3248         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3249                                 &bluetooth_attr_group);
3250         if (res)
3251                 return res;
3252
3253         res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3254                                 &tpacpi_bluetooth_rfkill,
3255                                 RFKILL_TYPE_BLUETOOTH,
3256                                 TPACPI_RFK_BLUETOOTH_SW_NAME,
3257                                 true,
3258                                 tpacpi_bluetooth_rfk_set,
3259                                 tpacpi_bluetooth_rfk_get);
3260         if (res) {
3261                 bluetooth_exit();
3262                 return res;
3263         }
3264
3265         return 0;
3266 }
3267
3268 /* procfs -------------------------------------------------------------- */
3269 static int bluetooth_read(char *p)
3270 {
3271         int len = 0;
3272         int status = bluetooth_get_radiosw();
3273
3274         if (!tp_features.bluetooth)
3275                 len += sprintf(p + len, "status:\t\tnot supported\n");
3276         else {
3277                 len += sprintf(p + len, "status:\t\t%s\n",
3278                                 (status == RFKILL_STATE_UNBLOCKED) ?
3279                                         "enabled" : "disabled");
3280                 len += sprintf(p + len, "commands:\tenable, disable\n");
3281         }
3282
3283         return len;
3284 }
3285
3286 static int bluetooth_write(char *buf)
3287 {
3288         char *cmd;
3289         int state = -1;
3290
3291         if (!tp_features.bluetooth)
3292                 return -ENODEV;
3293
3294         while ((cmd = next_cmd(&buf))) {
3295                 if (strlencmp(cmd, "enable") == 0) {
3296                         state = 1;
3297                 } else if (strlencmp(cmd, "disable") == 0) {
3298                         state = 0;
3299                 } else
3300                         return -EINVAL;
3301         }
3302
3303         if (state != -1) {
3304                 tpacpi_disclose_usertask("procfs bluetooth",
3305                         "attempt to %s\n",
3306                         state ? "enable" : "disable");
3307                 bluetooth_set_radiosw(state, 1);
3308         }
3309
3310         return 0;
3311 }
3312
3313 static struct ibm_struct bluetooth_driver_data = {
3314         .name = "bluetooth",
3315         .read = bluetooth_read,
3316         .write = bluetooth_write,
3317         .exit = bluetooth_exit,
3318         .suspend = bluetooth_suspend,
3319         .shutdown = bluetooth_shutdown,
3320 };
3321
3322 /*************************************************************************
3323  * Wan subdriver
3324  */
3325
3326 enum {
3327         /* ACPI GWAN/SWAN bits */
3328         TP_ACPI_WANCARD_HWPRESENT       = 0x01, /* Wan hw available */
3329         TP_ACPI_WANCARD_RADIOSSW        = 0x02, /* Wan radio enabled */
3330         TP_ACPI_WANCARD_RESUMECTRL      = 0x04, /* Wan state at resume:
3331                                                    off / last state */
3332 };
3333
3334 #define TPACPI_RFK_WWAN_SW_NAME         "tpacpi_wwan_sw"
3335
3336 static struct rfkill *tpacpi_wan_rfkill;
3337
3338 static void wan_suspend(pm_message_t state)
3339 {
3340         /* Try to make sure radio will resume powered off */
3341         if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3342                    TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3343                 vdbg_printk(TPACPI_DBG_RFKILL,
3344                         "WWAN power down on resume request failed\n");
3345 }
3346
3347 static int wan_get_radiosw(void)
3348 {
3349         int status;
3350
3351         if (!tp_features.wan)
3352                 return -ENODEV;
3353
3354         /* WLSW overrides WWAN in firmware/hardware, reflect that */
3355         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3356                 return RFKILL_STATE_HARD_BLOCKED;
3357
3358 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3359         if (dbg_wwanemul)
3360                 return (tpacpi_wwan_emulstate) ?
3361                         RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3362 #endif
3363
3364         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3365                 return -EIO;
3366
3367         return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3368                 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3369 }
3370
3371 static void wan_update_rfk(void)
3372 {
3373         int status;
3374
3375         if (!tpacpi_wan_rfkill)
3376                 return;
3377
3378         status = wan_get_radiosw();
3379         if (status < 0)
3380                 return;
3381         rfkill_force_state(tpacpi_wan_rfkill, status);
3382
3383         vdbg_printk(TPACPI_DBG_RFKILL,
3384                 "forced rfkill state to %d\n",
3385                 status);
3386 }
3387
3388 static int wan_set_radiosw(int radio_on, int update_rfk)
3389 {
3390         int status;
3391
3392         if (!tp_features.wan)
3393                 return -ENODEV;
3394
3395         /* WLSW overrides bluetooth in firmware/hardware, but there is no
3396          * reason to risk weird behaviour. */
3397         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3398             && radio_on)
3399                 return -EPERM;
3400
3401         vdbg_printk(TPACPI_DBG_RFKILL,
3402                 "will %s WWAN\n", radio_on ? "enable" : "disable");
3403
3404 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3405         if (dbg_wwanemul) {
3406                 tpacpi_wwan_emulstate = !!radio_on;
3407                 if (update_rfk)
3408                         wan_update_rfk();
3409                 return 0;
3410         }
3411 #endif
3412
3413         /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3414         if (radio_on)
3415                 status = TP_ACPI_WANCARD_RADIOSSW;
3416         else
3417                 status = 0;
3418         if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3419                 return -EIO;
3420
3421         if (update_rfk)
3422                 wan_update_rfk();
3423
3424         return 0;
3425 }
3426
3427 /* sysfs wan enable ---------------------------------------------------- */
3428 static ssize_t wan_enable_show(struct device *dev,
3429                            struct device_attribute *attr,
3430                            char *buf)
3431 {
3432         int status;
3433
3434         printk_deprecated_rfkill_attribute("wwan_enable");
3435
3436         status = wan_get_radiosw();
3437         if (status < 0)
3438                 return status;
3439
3440         return snprintf(buf, PAGE_SIZE, "%d\n",
3441                         (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3442 }
3443
3444 static ssize_t wan_enable_store(struct device *dev,
3445                             struct device_attribute *attr,
3446                             const char *buf, size_t count)
3447 {
3448         unsigned long t;
3449         int res;
3450
3451         printk_deprecated_rfkill_attribute("wwan_enable");
3452
3453         if (parse_strtoul(buf, 1, &t))
3454                 return -EINVAL;
3455
3456         tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3457
3458         res = wan_set_radiosw(t, 1);
3459
3460         return (res) ? res : count;
3461 }
3462
3463 static struct device_attribute dev_attr_wan_enable =
3464         __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3465                 wan_enable_show, wan_enable_store);
3466
3467 /* --------------------------------------------------------------------- */
3468
3469 static struct attribute *wan_attributes[] = {
3470         &dev_attr_wan_enable.attr,
3471         NULL
3472 };
3473
3474 static const struct attribute_group wan_attr_group = {
3475         .attrs = wan_attributes,
3476 };
3477
3478 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3479 {
3480         int wans = wan_get_radiosw();
3481
3482         if (wans < 0)
3483                 return wans;
3484
3485         *state = wans;
3486         return 0;
3487 }
3488
3489 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3490 {
3491         dbg_printk(TPACPI_DBG_RFKILL,
3492                    "request to change radio state to %d\n", state);
3493         return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3494 }
3495
3496 static void wan_shutdown(void)
3497 {
3498         /* Order firmware to save current state to NVRAM */
3499         if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3500                         TP_ACPI_WGSV_SAVE_STATE))
3501                 printk(TPACPI_NOTICE
3502                         "failed to save WWAN state to NVRAM\n");
3503         else
3504                 vdbg_printk(TPACPI_DBG_RFKILL,
3505                         "WWAN state saved to NVRAM\n");
3506 }
3507
3508 static void wan_exit(void)
3509 {
3510         wan_shutdown();
3511
3512         if (tpacpi_wan_rfkill)
3513                 rfkill_unregister(tpacpi_wan_rfkill);
3514
3515         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3516                 &wan_attr_group);
3517 }
3518
3519 static int __init wan_init(struct ibm_init_struct *iibm)
3520 {
3521         int res;
3522         int status = 0;
3523
3524         vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3525                         "initializing wan subdriver\n");
3526
3527         TPACPI_ACPIHANDLE_INIT(hkey);
3528
3529         tp_features.wan = hkey_handle &&
3530             acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3531
3532         vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3533                 "wan is %s, status 0x%02x\n",
3534                 str_supported(tp_features.wan),
3535                 status);
3536
3537 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3538         if (dbg_wwanemul) {
3539                 tp_features.wan = 1;
3540                 printk(TPACPI_INFO
3541                         "wwan switch emulation enabled\n");
3542         } else
3543 #endif
3544         if (tp_features.wan &&
3545             !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3546                 /* no wan hardware present in system */
3547                 tp_features.wan = 0;
3548                 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3549                            "wan hardware not installed\n");
3550         }
3551
3552         if (!tp_features.wan)
3553                 return 1;
3554
3555         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3556                                 &wan_attr_group);
3557         if (res)
3558                 return res;
3559
3560         res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3561                                 &tpacpi_wan_rfkill,
3562                                 RFKILL_TYPE_WWAN,
3563                                 TPACPI_RFK_WWAN_SW_NAME,
3564                                 true,
3565                                 tpacpi_wan_rfk_set,
3566                                 tpacpi_wan_rfk_get);
3567         if (res) {
3568                 wan_exit();
3569                 return res;
3570         }
3571
3572         return 0;
3573 }
3574
3575 /* procfs -------------------------------------------------------------- */
3576 static int wan_read(char *p)
3577 {
3578         int len = 0;
3579         int status = wan_get_radiosw();
3580
3581         tpacpi_disclose_usertask("procfs wan", "read");
3582
3583         if (!tp_features.wan)
3584                 len += sprintf(p + len, "status:\t\tnot supported\n");
3585         else {
3586                 len += sprintf(p + len, "status:\t\t%s\n",
3587                                 (status == RFKILL_STATE_UNBLOCKED) ?
3588                                         "enabled" : "disabled");
3589                 len += sprintf(p + len, "commands:\tenable, disable\n");
3590         }
3591
3592         return len;
3593 }
3594
3595 static int wan_write(char *buf)
3596 {
3597         char *cmd;
3598         int state = -1;
3599
3600         if (!tp_features.wan)
3601                 return -ENODEV;
3602
3603         while ((cmd = next_cmd(&buf))) {
3604                 if (strlencmp(cmd, "enable") == 0) {
3605                         state = 1;
3606                 } else if (strlencmp(cmd, "disable") == 0) {
3607                         state = 0;
3608                 } else
3609                         return -EINVAL;
3610         }
3611
3612         if (state != -1) {
3613                 tpacpi_disclose_usertask("procfs wan",
3614                         "attempt to %s\n",
3615                         state ? "enable" : "disable");
3616                 wan_set_radiosw(state, 1);
3617         }
3618
3619         return 0;
3620 }
3621
3622 static struct ibm_struct wan_driver_data = {
3623         .name = "wan",
3624         .read = wan_read,
3625         .write = wan_write,
3626         .exit = wan_exit,
3627         .suspend = wan_suspend,
3628         .shutdown = wan_shutdown,
3629 };
3630
3631 /*************************************************************************
3632  * UWB subdriver
3633  */
3634
3635 enum {
3636         /* ACPI GUWB/SUWB bits */
3637         TP_ACPI_UWB_HWPRESENT   = 0x01, /* UWB hw available */
3638         TP_ACPI_UWB_RADIOSSW    = 0x02, /* UWB radio enabled */
3639 };
3640
3641 #define TPACPI_RFK_UWB_SW_NAME  "tpacpi_uwb_sw"
3642
3643 static struct rfkill *tpacpi_uwb_rfkill;
3644
3645 static int uwb_get_radiosw(void)
3646 {
3647         int status;
3648
3649         if (!tp_features.uwb)
3650                 return -ENODEV;
3651
3652         /* WLSW overrides UWB in firmware/hardware, reflect that */
3653         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3654                 return RFKILL_STATE_HARD_BLOCKED;
3655
3656 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3657         if (dbg_uwbemul)
3658                 return (tpacpi_uwb_emulstate) ?
3659                         RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3660 #endif
3661
3662         if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3663                 return -EIO;
3664
3665         return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3666                 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3667 }
3668
3669 static void uwb_update_rfk(void)
3670 {
3671         int status;
3672
3673         if (!tpacpi_uwb_rfkill)
3674                 return;
3675
3676         status = uwb_get_radiosw();
3677         if (status < 0)
3678                 return;
3679         rfkill_force_state(tpacpi_uwb_rfkill, status);
3680
3681         vdbg_printk(TPACPI_DBG_RFKILL,
3682                 "forced rfkill state to %d\n",
3683                 status);
3684 }
3685
3686 static int uwb_set_radiosw(int radio_on, int update_rfk)
3687 {
3688         int status;
3689
3690         if (!tp_features.uwb)
3691                 return -ENODEV;
3692
3693         /* WLSW overrides UWB in firmware/hardware, but there is no
3694          * reason to risk weird behaviour. */
3695         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3696             && radio_on)
3697                 return -EPERM;
3698
3699         vdbg_printk(TPACPI_DBG_RFKILL,
3700                         "will %s UWB\n", radio_on ? "enable" : "disable");
3701
3702 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3703         if (dbg_uwbemul) {
3704                 tpacpi_uwb_emulstate = !!radio_on;
3705                 if (update_rfk)
3706                         uwb_update_rfk();
3707                 return 0;
3708         }
3709 #endif
3710
3711         status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3712         if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3713                 return -EIO;
3714
3715         if (update_rfk)
3716                 uwb_update_rfk();
3717
3718         return 0;
3719 }
3720
3721 /* --------------------------------------------------------------------- */
3722
3723 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3724 {
3725         int uwbs = uwb_get_radiosw();
3726
3727         if (uwbs < 0)
3728                 return uwbs;
3729
3730         *state = uwbs;
3731         return 0;
3732 }
3733
3734 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3735 {
3736         dbg_printk(TPACPI_DBG_RFKILL,
3737                    "request to change radio state to %d\n", state);
3738         return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3739 }
3740
3741 static void uwb_exit(void)
3742 {
3743         if (tpacpi_uwb_rfkill)
3744                 rfkill_unregister(tpacpi_uwb_rfkill);
3745 }
3746
3747 static int __init uwb_init(struct ibm_init_struct *iibm)
3748 {
3749         int res;
3750         int status = 0;
3751
3752         vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3753                         "initializing uwb subdriver\n");
3754
3755         TPACPI_ACPIHANDLE_INIT(hkey);
3756
3757         tp_features.uwb = hkey_handle &&
3758             acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3759
3760         vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3761                 "uwb is %s, status 0x%02x\n",
3762                 str_supported(tp_features.uwb),
3763                 status);
3764
3765 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3766         if (dbg_uwbemul) {
3767                 tp_features.uwb = 1;
3768                 printk(TPACPI_INFO
3769                         "uwb switch emulation enabled\n");
3770         } else
3771 #endif
3772         if (tp_features.uwb &&
3773             !(status & TP_ACPI_UWB_HWPRESENT)) {
3774                 /* no uwb hardware present in system */
3775                 tp_features.uwb = 0;
3776                 dbg_printk(TPACPI_DBG_INIT,
3777                            "uwb hardware not installed\n");
3778         }
3779
3780         if (!tp_features.uwb)
3781                 return 1;
3782
3783         res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3784                                 &tpacpi_uwb_rfkill,
3785                                 RFKILL_TYPE_UWB,
3786                                 TPACPI_RFK_UWB_SW_NAME,
3787                                 false,
3788                                 tpacpi_uwb_rfk_set,
3789                                 tpacpi_uwb_rfk_get);
3790
3791         return res;
3792 }
3793
3794 static struct ibm_struct uwb_driver_data = {
3795         .name = "uwb",
3796         .exit = uwb_exit,
3797         .flags.experimental = 1,
3798 };
3799
3800 /*************************************************************************
3801  * Video subdriver
3802  */
3803
3804 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3805
3806 enum video_access_mode {
3807         TPACPI_VIDEO_NONE = 0,
3808         TPACPI_VIDEO_570,       /* 570 */
3809         TPACPI_VIDEO_770,       /* 600e/x, 770e, 770x */
3810         TPACPI_VIDEO_NEW,       /* all others */
3811 };
3812
3813 enum {  /* video status flags, based on VIDEO_570 */
3814         TP_ACPI_VIDEO_S_LCD = 0x01,     /* LCD output enabled */
3815         TP_ACPI_VIDEO_S_CRT = 0x02,     /* CRT output enabled */
3816         TP_ACPI_VIDEO_S_DVI = 0x08,     /* DVI output enabled */
3817 };
3818
3819 enum {  /* TPACPI_VIDEO_570 constants */
3820         TP_ACPI_VIDEO_570_PHSCMD = 0x87,        /* unknown magic constant :( */
3821         TP_ACPI_VIDEO_570_PHSMASK = 0x03,       /* PHS bits that map to
3822                                                  * video_status_flags */
3823         TP_ACPI_VIDEO_570_PHS2CMD = 0x8b,       /* unknown magic constant :( */
3824         TP_ACPI_VIDEO_570_PHS2SET = 0x80,       /* unknown magic constant :( */
3825 };
3826
3827 static enum video_access_mode video_supported;
3828 static int video_orig_autosw;
3829
3830 static int video_autosw_get(void);
3831 static int video_autosw_set(int enable);
3832
3833 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");       /* G41 */
3834
3835 static int __init video_init(struct ibm_init_struct *iibm)
3836 {
3837         int ivga;
3838
3839         vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3840
3841         TPACPI_ACPIHANDLE_INIT(vid);
3842         TPACPI_ACPIHANDLE_INIT(vid2);
3843
3844         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3845                 /* G41, assume IVGA doesn't change */
3846                 vid_handle = vid2_handle;
3847
3848         if (!vid_handle)
3849                 /* video switching not supported on R30, R31 */
3850                 video_supported = TPACPI_VIDEO_NONE;
3851         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3852                 /* 570 */
3853                 video_supported = TPACPI_VIDEO_570;
3854         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3855                 /* 600e/x, 770e, 770x */
3856                 video_supported = TPACPI_VIDEO_770;
3857         else
3858                 /* all others */
3859                 video_supported = TPACPI_VIDEO_NEW;
3860
3861         vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3862                 str_supported(video_supported != TPACPI_VIDEO_NONE),
3863                 video_supported);
3864
3865         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3866 }
3867
3868 static void video_exit(void)
3869 {
3870         dbg_printk(TPACPI_DBG_EXIT,
3871                    "restoring original video autoswitch mode\n");
3872         if (video_autosw_set(video_orig_autosw))
3873                 printk(TPACPI_ERR "error while trying to restore original "
3874                         "video autoswitch mode\n");
3875 }
3876
3877 static int video_outputsw_get(void)
3878 {
3879         int status = 0;
3880         int i;
3881
3882         switch (video_supported) {
3883         case TPACPI_VIDEO_570:
3884                 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3885                                  TP_ACPI_VIDEO_570_PHSCMD))
3886                         return -EIO;
3887                 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3888                 break;
3889         case TPACPI_VIDEO_770:
3890                 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3891                         return -EIO;
3892                 if (i)
3893                         status |= TP_ACPI_VIDEO_S_LCD;
3894                 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3895                         return -EIO;
3896                 if (i)
3897                         status |= TP_ACPI_VIDEO_S_CRT;
3898                 break;
3899         case TPACPI_VIDEO_NEW:
3900                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3901                     !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3902                         return -EIO;
3903                 if (i)
3904                         status |= TP_ACPI_VIDEO_S_CRT;
3905
3906                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3907                     !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3908                         return -EIO;
3909                 if (i)
3910                         status |= TP_ACPI_VIDEO_S_LCD;
3911                 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3912                         return -EIO;
3913                 if (i)
3914                         status |= TP_ACPI_VIDEO_S_DVI;
3915                 break;
3916         default:
3917                 return -ENOSYS;
3918         }
3919
3920         return status;
3921 }
3922
3923 static int video_outputsw_set(int status)
3924 {
3925         int autosw;
3926         int res = 0;
3927
3928         switch (video_supported) {
3929         case TPACPI_VIDEO_570:
3930                 res = acpi_evalf(NULL, NULL,
3931                                  "\\_SB.PHS2", "vdd",
3932                                  TP_ACPI_VIDEO_570_PHS2CMD,
3933                                  status | TP_ACPI_VIDEO_570_PHS2SET);
3934                 break;
3935         case TPACPI_VIDEO_770:
3936                 autosw = video_autosw_get();
3937                 if (autosw < 0)
3938                         return autosw;
3939
3940                 res = video_autosw_set(1);
3941                 if (res)
3942                         return res;
3943                 res = acpi_evalf(vid_handle, NULL,
3944                                  "ASWT", "vdd", status * 0x100, 0);
3945                 if (!autosw && video_autosw_set(autosw)) {
3946                         printk(TPACPI_ERR
3947                                "video auto-switch left enabled due to error\n");
3948                         return -EIO;
3949                 }
3950                 break;
3951         case TPACPI_VIDEO_NEW:
3952                 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3953                       acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3954                 break;
3955         default:
3956                 return -ENOSYS;
3957         }
3958
3959         return (res)? 0 : -EIO;
3960 }
3961
3962 static int video_autosw_get(void)
3963 {
3964         int autosw = 0;
3965
3966         switch (video_supported) {
3967         case TPACPI_VIDEO_570:
3968                 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3969                         return -EIO;
3970                 break;
3971         case TPACPI_VIDEO_770:
3972         case TPACPI_VIDEO_NEW:
3973                 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3974                         return -EIO;
3975                 break;
3976         default:
3977                 return -ENOSYS;
3978         }
3979
3980         return autosw & 1;
3981 }
3982
3983 static int video_autosw_set(int enable)
3984 {
3985         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3986                 return -EIO;
3987         return 0;
3988 }
3989
3990 static int video_outputsw_cycle(void)
3991 {
3992         int autosw = video_autosw_get();
3993         int res;
3994
3995         if (autosw < 0)
3996                 return autosw;
3997
3998         switch (video_supported) {
3999         case TPACPI_VIDEO_570:
4000                 res = video_autosw_set(1);
4001                 if (res)
4002                         return res;
4003                 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4004                 break;
4005         case TPACPI_VIDEO_770:
4006         case TPACPI_VIDEO_NEW:
4007                 res = video_autosw_set(1);
4008                 if (res)
4009                         return res;
4010                 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4011                 break;
4012         default:
4013                 return -ENOSYS;
4014         }
4015         if (!autosw && video_autosw_set(autosw)) {
4016                 printk(TPACPI_ERR
4017                        "video auto-switch left enabled due to error\n");
4018                 return -EIO;
4019         }
4020
4021         return (res)? 0 : -EIO;
4022 }
4023
4024 static int video_expand_toggle(void)
4025 {
4026         switch (video_supported) {
4027         case TPACPI_VIDEO_570:
4028                 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4029                         0 : -EIO;
4030         case TPACPI_VIDEO_770:
4031                 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4032                         0 : -EIO;
4033         case TPACPI_VIDEO_NEW:
4034                 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4035                         0 : -EIO;
4036         default:
4037                 return -ENOSYS;
4038         }
4039         /* not reached */
4040 }
4041
4042 static int video_read(char *p)
4043 {
4044         int status, autosw;
4045         int len = 0;
4046
4047         if (video_supported == TPACPI_VIDEO_NONE) {
4048                 len += sprintf(p + len, "status:\t\tnot supported\n");
4049                 return len;
4050         }
4051
4052         status = video_outputsw_get();
4053         if (status < 0)
4054                 return status;
4055
4056         autosw = video_autosw_get();
4057         if (autosw < 0)
4058                 return autosw;
4059
4060         len += sprintf(p + len, "status:\t\tsupported\n");
4061         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4062         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4063         if (video_supported == TPACPI_VIDEO_NEW)
4064                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4065         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4066         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4067         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4068         if (video_supported == TPACPI_VIDEO_NEW)
4069                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4070         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4071         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4072
4073         return len;
4074 }
4075
4076 static int video_write(char *buf)
4077 {
4078         char *cmd;
4079         int enable, disable, status;
4080         int res;
4081
4082         if (video_supported == TPACPI_VIDEO_NONE)
4083                 return -ENODEV;
4084
4085         enable = 0;
4086         disable = 0;
4087
4088         while ((cmd = next_cmd(&buf))) {
4089                 if (strlencmp(cmd, "lcd_enable") == 0) {
4090                         enable |= TP_ACPI_VIDEO_S_LCD;
4091                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4092                         disable |= TP_ACPI_VIDEO_S_LCD;
4093                 } else if (strlencmp(cmd, "crt_enable") == 0) {
4094                         enable |= TP_ACPI_VIDEO_S_CRT;
4095                 } else if (strlencmp(cmd, "crt_disable") == 0) {
4096                         disable |= TP_ACPI_VIDEO_S_CRT;
4097                 } else if (video_supported == TPACPI_VIDEO_NEW &&
4098                            strlencmp(cmd, "dvi_enable") == 0) {
4099                         enable |= TP_ACPI_VIDEO_S_DVI;
4100                 } else if (video_supported == TPACPI_VIDEO_NEW &&
4101                            strlencmp(cmd, "dvi_disable") == 0) {
4102                         disable |= TP_ACPI_VIDEO_S_DVI;
4103                 } else if (strlencmp(cmd, "auto_enable") == 0) {
4104                         res = video_autosw_set(1);
4105                         if (res)
4106                                 return res;
4107                 } else if (strlencmp(cmd, "auto_disable") == 0) {
4108                         res = video_autosw_set(0);
4109                         if (res)
4110                                 return res;
4111                 } else if (strlencmp(cmd, "video_switch") == 0) {
4112                         res = video_outputsw_cycle();
4113                         if (res)
4114                                 return res;
4115                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4116                         res = video_expand_toggle();
4117                         if (res)
4118                                 return res;
4119                 } else
4120                         return -EINVAL;
4121         }
4122
4123         if (enable || disable) {
4124                 status = video_outputsw_get();
4125                 if (status < 0)
4126                         return status;
4127                 res = video_outputsw_set((status & ~disable) | enable);
4128                 if (res)
4129                         return res;
4130         }
4131
4132         return 0;
4133 }
4134
4135 static struct ibm_struct video_driver_data = {
4136         .name = "video",
4137         .read = video_read,
4138         .write = video_write,
4139         .exit = video_exit,
4140 };
4141
4142 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4143
4144 /*************************************************************************
4145  * Light (thinklight) subdriver
4146  */
4147
4148 TPACPI_HANDLE(lght, root, "\\LGHT");    /* A21e, A2xm/p, T20-22, X20-21 */
4149 TPACPI_HANDLE(ledb, ec, "LEDB");                /* G4x */
4150
4151 static int light_get_status(void)
4152 {
4153         int status = 0;
4154
4155         if (tp_features.light_status) {
4156                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4157                         return -EIO;
4158                 return (!!status);
4159         }
4160
4161         return -ENXIO;
4162 }
4163
4164 static int light_set_status(int status)
4165 {
4166         int rc;
4167
4168         if (tp_features.light) {
4169                 if (cmos_handle) {
4170                         rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4171                                         (status)?
4172                                                 TP_CMOS_THINKLIGHT_ON :
4173                                                 TP_CMOS_THINKLIGHT_OFF);
4174                 } else {
4175                         rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4176                                         (status)? 1 : 0);
4177                 }
4178                 return (rc)? 0 : -EIO;
4179         }
4180
4181         return -ENXIO;
4182 }
4183
4184 static void light_set_status_worker(struct work_struct *work)
4185 {
4186         struct tpacpi_led_classdev *data =
4187                         container_of(work, struct tpacpi_led_classdev, work);
4188
4189         if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4190                 light_set_status((data->new_brightness != LED_OFF));
4191 }
4192
4193 static void light_sysfs_set(struct led_classdev *led_cdev,
4194                         enum led_brightness brightness)
4195 {
4196         struct tpacpi_led_classdev *data =
4197                 container_of(led_cdev,
4198                              struct tpacpi_led_classdev,
4199                              led_classdev);
4200         data->new_brightness = brightness;
4201         queue_work(tpacpi_wq, &data->work);
4202 }
4203
4204 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4205 {
4206         return (light_get_status() == 1)? LED_FULL : LED_OFF;
4207 }
4208
4209 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4210         .led_classdev = {
4211                 .name           = "tpacpi::thinklight",
4212                 .brightness_set = &light_sysfs_set,
4213                 .brightness_get = &light_sysfs_get,
4214         }
4215 };
4216
4217 static int __init light_init(struct ibm_init_struct *iibm)
4218 {
4219         int rc;
4220
4221         vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4222
4223         TPACPI_ACPIHANDLE_INIT(ledb);
4224         TPACPI_ACPIHANDLE_INIT(lght);
4225         TPACPI_ACPIHANDLE_INIT(cmos);
4226         INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4227
4228         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4229         tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4230
4231         if (tp_features.light)
4232                 /* light status not supported on
4233                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4234                 tp_features.light_status =
4235                         acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4236
4237         vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4238                 str_supported(tp_features.light),
4239                 str_supported(tp_features.light_status));
4240
4241         if (!tp_features.light)
4242                 return 1;
4243
4244         rc = led_classdev_register(&tpacpi_pdev->dev,
4245                                    &tpacpi_led_thinklight.led_classdev);
4246
4247         if (rc < 0) {
4248                 tp_features.light = 0;
4249                 tp_features.light_status = 0;
4250         } else  {
4251                 rc = 0;
4252         }
4253
4254         return rc;
4255 }
4256
4257 static void light_exit(void)
4258 {
4259         led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4260         if (work_pending(&tpacpi_led_thinklight.work))
4261                 flush_workqueue(tpacpi_wq);
4262 }
4263
4264 static int light_read(char *p)
4265 {
4266         int len = 0;
4267         int status;
4268
4269         if (!tp_features.light) {
4270                 len += sprintf(p + len, "status:\t\tnot supported\n");
4271         } else if (!tp_features.light_status) {
4272                 len += sprintf(p + len, "status:\t\tunknown\n");
4273                 len += sprintf(p + len, "commands:\ton, off\n");
4274         } else {
4275                 status = light_get_status();
4276                 if (status < 0)
4277                         return status;
4278                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4279                 len += sprintf(p + len, "commands:\ton, off\n");
4280         }
4281
4282         return len;
4283 }
4284
4285 static int light_write(char *buf)
4286 {
4287         char *cmd;
4288         int newstatus = 0;
4289
4290         if (!tp_features.light)
4291                 return -ENODEV;
4292
4293         while ((cmd = next_cmd(&buf))) {
4294                 if (strlencmp(cmd, "on") == 0) {
4295                         newstatus = 1;
4296                 } else if (strlencmp(cmd, "off") == 0) {
4297                         newstatus = 0;
4298                 } else
4299                         return -EINVAL;
4300         }
4301
4302         return light_set_status(newstatus);
4303 }
4304
4305 static struct ibm_struct light_driver_data = {
4306         .name = "light",
4307         .read = light_read,
4308         .write = light_write,
4309         .exit = light_exit,
4310 };
4311
4312 /*************************************************************************
4313  * Dock subdriver
4314  */
4315
4316 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4317
4318 static void dock_notify(struct ibm_struct *ibm, u32 event);
4319 static int dock_read(char *p);
4320 static int dock_write(char *buf);
4321
4322 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4323            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4324            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
4325            "\\_SB.PCI.ISA.SLCE",        /* 570 */
4326     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4327
4328 /* don't list other alternatives as we install a notify handler on the 570 */
4329 TPACPI_HANDLE(pci, root, "\\_SB.PCI");  /* 570 */
4330
4331 static const struct acpi_device_id ibm_pci_device_ids[] = {
4332         {PCI_ROOT_HID_STRING, 0},
4333         {"", 0},
4334 };
4335
4336 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4337         {
4338          .notify = dock_notify,
4339          .handle = &dock_handle,
4340          .type = ACPI_SYSTEM_NOTIFY,
4341         },
4342         {
4343         /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4344          * We just use it to get notifications of dock hotplug
4345          * in very old thinkpads */
4346          .hid = ibm_pci_device_ids,
4347          .notify = dock_notify,
4348          .handle = &pci_handle,
4349          .type = ACPI_SYSTEM_NOTIFY,
4350         },
4351 };
4352
4353 static struct ibm_struct dock_driver_data[2] = {
4354         {
4355          .name = "dock",
4356          .read = dock_read,
4357          .write = dock_write,
4358          .acpi = &ibm_dock_acpidriver[0],
4359         },
4360         {
4361          .name = "dock",
4362          .acpi = &ibm_dock_acpidriver[1],
4363         },
4364 };
4365
4366 #define dock_docked() (_sta(dock_handle) & 1)
4367
4368 static int __init dock_init(struct ibm_init_struct *iibm)
4369 {
4370         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4371
4372         TPACPI_ACPIHANDLE_INIT(dock);
4373
4374         vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4375                 str_supported(dock_handle != NULL));
4376
4377         return (dock_handle)? 0 : 1;
4378 }
4379
4380 static int __init dock_init2(struct ibm_init_struct *iibm)
4381 {
4382         int dock2_needed;
4383
4384         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4385
4386         if (dock_driver_data[0].flags.acpi_driver_registered &&
4387             dock_driver_data[0].flags.acpi_notify_installed) {
4388                 TPACPI_ACPIHANDLE_INIT(pci);
4389                 dock2_needed = (pci_handle != NULL);
4390                 vdbg_printk(TPACPI_DBG_INIT,
4391                             "dock PCI handler for the TP 570 is %s\n",
4392                             str_supported(dock2_needed));
4393         } else {
4394                 vdbg_printk(TPACPI_DBG_INIT,
4395                 "dock subdriver part 2 not required\n");
4396                 dock2_needed = 0;
4397         }
4398
4399         return (dock2_needed)? 0 : 1;
4400 }
4401
4402 static void dock_notify(struct ibm_struct *ibm, u32 event)
4403 {
4404         int docked = dock_docked();
4405         int pci = ibm->acpi->hid && ibm->acpi->device &&
4406                 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4407         int data;
4408
4409         if (event == 1 && !pci) /* 570 */
4410                 data = 1;       /* button */
4411         else if (event == 1 && pci)     /* 570 */
4412                 data = 3;       /* dock */
4413         else if (event == 3 && docked)
4414                 data = 1;       /* button */
4415         else if (event == 3 && !docked)
4416                 data = 2;       /* undock */
4417         else if (event == 0 && docked)
4418                 data = 3;       /* dock */
4419         else {
4420                 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4421                        event, _sta(dock_handle));
4422                 data = 0;       /* unknown */
4423         }
4424         acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4425         acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4426                                           dev_name(&ibm->acpi->device->dev),
4427                                           event, data);
4428 }
4429
4430 static int dock_read(char *p)
4431 {
4432         int len = 0;
4433         int docked = dock_docked();
4434
4435         if (!dock_handle)
4436                 len += sprintf(p + len, "status:\t\tnot supported\n");
4437         else if (!docked)
4438                 len += sprintf(p + len, "status:\t\tundocked\n");
4439         else {
4440                 len += sprintf(p + len, "status:\t\tdocked\n");
4441                 len += sprintf(p + len, "commands:\tdock, undock\n");
4442         }
4443
4444         return len;
4445 }
4446
4447 static int dock_write(char *buf)
4448 {
4449         char *cmd;
4450
4451         if (!dock_docked())
4452                 return -ENODEV;
4453
4454         while ((cmd = next_cmd(&buf))) {
4455                 if (strlencmp(cmd, "undock") == 0) {
4456                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4457                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4458                                 return -EIO;
4459                 } else if (strlencmp(cmd, "dock") == 0) {
4460                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4461                                 return -EIO;
4462                 } else
4463                         return -EINVAL;
4464         }
4465
4466         return 0;
4467 }
4468
4469 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4470
4471 /*************************************************************************
4472  * Bay subdriver
4473  */
4474
4475 #ifdef CONFIG_THINKPAD_ACPI_BAY
4476
4477 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",     /* 570 */
4478            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4479            "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4480            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4481            );                           /* A21e, R30, R31 */
4482 TPACPI_HANDLE(bay_ej, bay, "_EJ3",      /* 600e/x, A2xm/p, A3x */
4483            "_EJ0",              /* all others */
4484            );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4485 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",  /* A3x, R32 */
4486            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4487            );                           /* all others */
4488 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3",    /* 600e/x, 770e, A3x */
4489            "_EJ0",                      /* 770x */
4490            );                           /* all others */
4491
4492 static int __init bay_init(struct ibm_init_struct *iibm)
4493 {
4494         vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4495
4496         TPACPI_ACPIHANDLE_INIT(bay);
4497         if (bay_handle)
4498                 TPACPI_ACPIHANDLE_INIT(bay_ej);
4499         TPACPI_ACPIHANDLE_INIT(bay2);
4500         if (bay2_handle)
4501                 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4502
4503         tp_features.bay_status = bay_handle &&
4504                 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4505         tp_features.bay_status2 = bay2_handle &&
4506                 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4507
4508         tp_features.bay_eject = bay_handle && bay_ej_handle &&
4509                 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4510         tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4511                 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4512
4513         vdbg_printk(TPACPI_DBG_INIT,
4514                 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4515                 str_supported(tp_features.bay_status),
4516                 str_supported(tp_features.bay_eject),
4517                 str_supported(tp_features.bay_status2),
4518                 str_supported(tp_features.bay_eject2));
4519
4520         return (tp_features.bay_status || tp_features.bay_eject ||
4521                 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4522 }
4523
4524 static void bay_notify(struct ibm_struct *ibm, u32 event)
4525 {
4526         acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4527         acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4528                                           dev_name(&ibm->acpi->device->dev),
4529                                           event, 0);
4530 }
4531
4532 #define bay_occupied(b) (_sta(b##_handle) & 1)
4533
4534 static int bay_read(char *p)
4535 {
4536         int len = 0;
4537         int occupied = bay_occupied(bay);
4538         int occupied2 = bay_occupied(bay2);
4539         int eject, eject2;
4540
4541         len += sprintf(p + len, "status:\t\t%s\n",
4542                 tp_features.bay_status ?
4543                         (occupied ? "occupied" : "unoccupied") :
4544                                 "not supported");
4545         if (tp_features.bay_status2)
4546                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4547                                "occupied" : "unoccupied");
4548
4549         eject = tp_features.bay_eject && occupied;
4550         eject2 = tp_features.bay_eject2 && occupied2;
4551
4552         if (eject && eject2)
4553                 len += sprintf(p + len, "commands:\teject, eject2\n");
4554         else if (eject)
4555                 len += sprintf(p + len, "commands:\teject\n");
4556         else if (eject2)
4557                 len += sprintf(p + len, "commands:\teject2\n");
4558
4559         return len;
4560 }
4561
4562 static int bay_write(char *buf)
4563 {
4564         char *cmd;
4565
4566         if (!tp_features.bay_eject && !tp_features.bay_eject2)
4567                 return -ENODEV;
4568
4569         while ((cmd = next_cmd(&buf))) {
4570                 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4571                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4572                                 return -EIO;
4573                 } else if (tp_features.bay_eject2 &&
4574                            strlencmp(cmd, "eject2") == 0) {
4575                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4576                                 return -EIO;
4577                 } else
4578                         return -EINVAL;
4579         }
4580
4581         return 0;
4582 }
4583
4584 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4585         .notify = bay_notify,
4586         .handle = &bay_handle,
4587         .type = ACPI_SYSTEM_NOTIFY,
4588 };
4589
4590 static struct ibm_struct bay_driver_data = {
4591         .name = "bay",
4592         .read = bay_read,
4593         .write = bay_write,
4594         .acpi = &ibm_bay_acpidriver,
4595 };
4596
4597 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4598
4599 /*************************************************************************
4600  * CMOS subdriver
4601  */
4602
4603 /* sysfs cmos_command -------------------------------------------------- */
4604 static ssize_t cmos_command_store(struct device *dev,
4605                             struct device_attribute *attr,
4606                             const char *buf, size_t count)
4607 {
4608         unsigned long cmos_cmd;
4609         int res;
4610
4611         if (parse_strtoul(buf, 21, &cmos_cmd))
4612                 return -EINVAL;
4613
4614         res = issue_thinkpad_cmos_command(cmos_cmd);
4615         return (res)? res : count;
4616 }
4617
4618 static struct device_attribute dev_attr_cmos_command =
4619         __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4620
4621 /* --------------------------------------------------------------------- */
4622
4623 static int __init cmos_init(struct ibm_init_struct *iibm)
4624 {
4625         int res;
4626
4627         vdbg_printk(TPACPI_DBG_INIT,
4628                 "initializing cmos commands subdriver\n");
4629
4630         TPACPI_ACPIHANDLE_INIT(cmos);
4631
4632         vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4633                 str_supported(cmos_handle != NULL));
4634
4635         res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4636         if (res)
4637                 return res;
4638
4639         return (cmos_handle)? 0 : 1;
4640 }
4641
4642 static void cmos_exit(void)
4643 {
4644         device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4645 }
4646
4647 static int cmos_read(char *p)
4648 {
4649         int len = 0;
4650
4651         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4652            R30, R31, T20-22, X20-21 */
4653         if (!cmos_handle)
4654                 len += sprintf(p + len, "status:\t\tnot supported\n");
4655         else {
4656                 len += sprintf(p + len, "status:\t\tsupported\n");
4657                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4658         }
4659
4660         return len;
4661 }
4662
4663 static int cmos_write(char *buf)
4664 {
4665         char *cmd;
4666         int cmos_cmd, res;
4667
4668         while ((cmd = next_cmd(&buf))) {
4669                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4670                     cmos_cmd >= 0 && cmos_cmd <= 21) {
4671                         /* cmos_cmd set */
4672                 } else
4673                         return -EINVAL;
4674
4675                 res = issue_thinkpad_cmos_command(cmos_cmd);
4676                 if (res)
4677                         return res;
4678         }
4679
4680         return 0;
4681 }
4682
4683 static struct ibm_struct cmos_driver_data = {
4684         .name = "cmos",
4685         .read = cmos_read,
4686         .write = cmos_write,
4687         .exit = cmos_exit,
4688 };
4689
4690 /*************************************************************************
4691  * LED subdriver
4692  */
4693
4694 enum led_access_mode {
4695         TPACPI_LED_NONE = 0,
4696         TPACPI_LED_570, /* 570 */
4697         TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4698         TPACPI_LED_NEW, /* all others */
4699 };
4700
4701 enum {  /* For TPACPI_LED_OLD */
4702         TPACPI_LED_EC_HLCL = 0x0c,      /* EC reg to get led to power on */
4703         TPACPI_LED_EC_HLBL = 0x0d,      /* EC reg to blink a lit led */
4704         TPACPI_LED_EC_HLMS = 0x0e,      /* EC reg to select led to command */
4705 };
4706
4707 enum led_status_t {
4708         TPACPI_LED_OFF = 0,
4709         TPACPI_LED_ON,
4710         TPACPI_LED_BLINK,
4711 };
4712
4713 static enum led_access_mode led_supported;
4714
4715 TPACPI_HANDLE(led, ec, "SLED",  /* 570 */
4716            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4717                                 /* T20-22, X20-21 */
4718            "LED",               /* all others */
4719            );                   /* R30, R31 */
4720
4721 #define TPACPI_LED_NUMLEDS 8
4722 static struct tpacpi_led_classdev *tpacpi_leds;
4723 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4724 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4725         /* there's a limit of 19 chars + NULL before 2.6.26 */
4726         "tpacpi::power",
4727         "tpacpi:orange:batt",
4728         "tpacpi:green:batt",
4729         "tpacpi::dock_active",
4730         "tpacpi::bay_active",
4731         "tpacpi::dock_batt",
4732         "tpacpi::unknown_led",
4733         "tpacpi::standby",
4734 };
4735 #define TPACPI_SAFE_LEDS        0x0081U
4736
4737 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4738 {
4739 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4740         return false;
4741 #else
4742         return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4743 #endif
4744 }
4745
4746 static int led_get_status(const unsigned int led)
4747 {
4748         int status;
4749         enum led_status_t led_s;
4750
4751         switch (led_supported) {
4752         case TPACPI_LED_570:
4753                 if (!acpi_evalf(ec_handle,
4754                                 &status, "GLED", "dd", 1 << led))
4755                         return -EIO;
4756                 led_s = (status == 0)?
4757                                 TPACPI_LED_OFF :
4758                                 ((status == 1)?
4759                                         TPACPI_LED_ON :
4760                                         TPACPI_LED_BLINK);
4761                 tpacpi_led_state_cache[led] = led_s;
4762                 return led_s;
4763         default:
4764                 return -ENXIO;
4765         }
4766
4767         /* not reached */
4768 }
4769
4770 static int led_set_status(const unsigned int led,
4771                           const enum led_status_t ledstatus)
4772 {
4773         /* off, on, blink. Index is led_status_t */
4774         static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4775         static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4776
4777         int rc = 0;
4778
4779         switch (led_supported) {
4780         case TPACPI_LED_570:
4781                 /* 570 */
4782                 if (unlikely(led > 7))
4783                         return -EINVAL;
4784                 if (unlikely(tpacpi_is_led_restricted(led)))
4785                         return -EPERM;
4786                 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4787                                 (1 << led), led_sled_arg1[ledstatus]))
4788                         rc = -EIO;
4789                 break;
4790         case TPACPI_LED_OLD:
4791                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4792                 if (unlikely(led > 7))
4793                         return -EINVAL;
4794                 if (unlikely(tpacpi_is_led_restricted(led)))
4795                         return -EPERM;
4796                 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4797                 if (rc >= 0)
4798                         rc = ec_write(TPACPI_LED_EC_HLBL,
4799                                       (ledstatus == TPACPI_LED_BLINK) << led);
4800                 if (rc >= 0)
4801                         rc = ec_write(TPACPI_LED_EC_HLCL,
4802                                       (ledstatus != TPACPI_LED_OFF) << led);
4803                 break;
4804         case TPACPI_LED_NEW:
4805                 /* all others */
4806                 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4807                         return -EINVAL;
4808                 if (unlikely(tpacpi_is_led_restricted(led)))
4809                         return -EPERM;
4810                 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4811                                 led, led_led_arg1[ledstatus]))
4812                         rc = -EIO;
4813                 break;
4814         default:
4815                 rc = -ENXIO;
4816         }
4817
4818         if (!rc)
4819                 tpacpi_led_state_cache[led] = ledstatus;
4820
4821         return rc;
4822 }
4823
4824 static void led_sysfs_set_status(unsigned int led,
4825                                  enum led_brightness brightness)
4826 {
4827         led_set_status(led,
4828                         (brightness == LED_OFF) ?
4829                         TPACPI_LED_OFF :
4830                         (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4831                                 TPACPI_LED_BLINK : TPACPI_LED_ON);
4832 }
4833
4834 static void led_set_status_worker(struct work_struct *work)
4835 {
4836         struct tpacpi_led_classdev *data =
4837                 container_of(work, struct tpacpi_led_classdev, work);
4838
4839         if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4840                 led_sysfs_set_status(data->led, data->new_brightness);
4841 }
4842
4843 static void led_sysfs_set(struct led_classdev *led_cdev,
4844                         enum led_brightness brightness)
4845 {
4846         struct tpacpi_led_classdev *data = container_of(led_cdev,
4847                              struct tpacpi_led_classdev, led_classdev);
4848
4849         data->new_brightness = brightness;
4850         queue_work(tpacpi_wq, &data->work);
4851 }
4852
4853 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4854                         unsigned long *delay_on, unsigned long *delay_off)
4855 {
4856         struct tpacpi_led_classdev *data = container_of(led_cdev,
4857                              struct tpacpi_led_classdev, led_classdev);
4858
4859         /* Can we choose the flash rate? */
4860         if (*delay_on == 0 && *delay_off == 0) {
4861                 /* yes. set them to the hardware blink rate (1 Hz) */
4862                 *delay_on = 500; /* ms */
4863                 *delay_off = 500; /* ms */
4864         } else if ((*delay_on != 500) || (*delay_off != 500))
4865                 return -EINVAL;
4866
4867         data->new_brightness = TPACPI_LED_BLINK;
4868         queue_work(tpacpi_wq, &data->work);
4869
4870         return 0;
4871 }
4872
4873 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4874 {
4875         int rc;
4876
4877         struct tpacpi_led_classdev *data = container_of(led_cdev,
4878                              struct tpacpi_led_classdev, led_classdev);
4879
4880         rc = led_get_status(data->led);
4881
4882         if (rc == TPACPI_LED_OFF || rc < 0)
4883                 rc = LED_OFF;   /* no error handling in led class :( */
4884         else
4885                 rc = LED_FULL;
4886
4887         return rc;
4888 }
4889
4890 static void led_exit(void)
4891 {
4892         unsigned int i;
4893
4894         for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4895                 if (tpacpi_leds[i].led_classdev.name)
4896                         led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4897         }
4898
4899         kfree(tpacpi_leds);
4900 }
4901
4902 static int __init tpacpi_init_led(unsigned int led)
4903 {
4904         int rc;
4905
4906         tpacpi_leds[led].led = led;
4907
4908         tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4909         tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4910         if (led_supported == TPACPI_LED_570)
4911                 tpacpi_leds[led].led_classdev.brightness_get =
4912                                                 &led_sysfs_get;
4913
4914         tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4915
4916         INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4917
4918         rc = led_classdev_register(&tpacpi_pdev->dev,
4919                                 &tpacpi_leds[led].led_classdev);
4920         if (rc < 0)
4921                 tpacpi_leds[led].led_classdev.name = NULL;
4922
4923         return rc;
4924 }
4925
4926 static int __init led_init(struct ibm_init_struct *iibm)
4927 {
4928         unsigned int i;
4929         int rc;
4930
4931         vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4932
4933         TPACPI_ACPIHANDLE_INIT(led);
4934
4935         if (!led_handle)
4936                 /* led not supported on R30, R31 */
4937                 led_supported = TPACPI_LED_NONE;
4938         else if (strlencmp(led_path, "SLED") == 0)
4939                 /* 570 */
4940                 led_supported = TPACPI_LED_570;
4941         else if (strlencmp(led_path, "SYSL") == 0)
4942                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4943                 led_supported = TPACPI_LED_OLD;
4944         else
4945                 /* all others */
4946                 led_supported = TPACPI_LED_NEW;
4947
4948         vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4949                 str_supported(led_supported), led_supported);
4950
4951         tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4952                               GFP_KERNEL);
4953         if (!tpacpi_leds) {
4954                 printk(TPACPI_ERR "Out of memory for LED data\n");
4955                 return -ENOMEM;
4956         }
4957
4958         for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4959                 if (!tpacpi_is_led_restricted(i)) {
4960                         rc = tpacpi_init_led(i);
4961                         if (rc < 0) {
4962                                 led_exit();
4963                                 return rc;
4964                         }
4965                 }
4966         }
4967
4968 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4969         if (led_supported != TPACPI_LED_NONE)
4970                 printk(TPACPI_NOTICE
4971                         "warning: userspace override of important "
4972                         "firmware LEDs is enabled\n");
4973 #endif
4974         return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4975 }
4976
4977 #define str_led_status(s) \
4978         ((s) == TPACPI_LED_OFF ? "off" : \
4979                 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4980
4981 static int led_read(char *p)
4982 {
4983         int len = 0;
4984
4985         if (!led_supported) {
4986                 len += sprintf(p + len, "status:\t\tnot supported\n");
4987                 return len;
4988         }
4989         len += sprintf(p + len, "status:\t\tsupported\n");
4990
4991         if (led_supported == TPACPI_LED_570) {
4992                 /* 570 */
4993                 int i, status;
4994                 for (i = 0; i < 8; i++) {
4995                         status = led_get_status(i);
4996                         if (status < 0)
4997                                 return -EIO;
4998                         len += sprintf(p + len, "%d:\t\t%s\n",
4999                                        i, str_led_status(status));
5000                 }
5001         }
5002
5003         len += sprintf(p + len, "commands:\t"
5004                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5005
5006         return len;
5007 }
5008
5009 static int led_write(char *buf)
5010 {
5011         char *cmd;
5012         int led, rc;
5013         enum led_status_t s;
5014
5015         if (!led_supported)
5016                 return -ENODEV;
5017
5018         while ((cmd = next_cmd(&buf))) {
5019                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5020                         return -EINVAL;
5021
5022                 if (strstr(cmd, "off")) {
5023                         s = TPACPI_LED_OFF;
5024                 } else if (strstr(cmd, "on")) {
5025                         s = TPACPI_LED_ON;
5026                 } else if (strstr(cmd, "blink")) {
5027                         s = TPACPI_LED_BLINK;
5028                 } else {
5029                         return -EINVAL;
5030                 }
5031
5032                 rc = led_set_status(led, s);
5033                 if (rc < 0)
5034                         return rc;
5035         }
5036
5037         return 0;
5038 }
5039
5040 static struct ibm_struct led_driver_data = {
5041         .name = "led",
5042         .read = led_read,
5043         .write = led_write,
5044         .exit = led_exit,
5045 };
5046
5047 /*************************************************************************
5048  * Beep subdriver
5049  */
5050
5051 TPACPI_HANDLE(beep, ec, "BEEP");        /* all except R30, R31 */
5052
5053 static int __init beep_init(struct ibm_init_struct *iibm)
5054 {
5055         vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5056
5057         TPACPI_ACPIHANDLE_INIT(beep);
5058
5059         vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5060                 str_supported(beep_handle != NULL));
5061
5062         return (beep_handle)? 0 : 1;
5063 }
5064
5065 static int beep_read(char *p)
5066 {
5067         int len = 0;
5068
5069         if (!beep_handle)
5070                 len += sprintf(p + len, "status:\t\tnot supported\n");
5071         else {
5072                 len += sprintf(p + len, "status:\t\tsupported\n");
5073                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5074         }
5075
5076         return len;
5077 }
5078
5079 static int beep_write(char *buf)
5080 {
5081         char *cmd;
5082         int beep_cmd;
5083
5084         if (!beep_handle)
5085                 return -ENODEV;
5086
5087         while ((cmd = next_cmd(&buf))) {
5088                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5089                     beep_cmd >= 0 && beep_cmd <= 17) {
5090                         /* beep_cmd set */
5091                 } else
5092                         return -EINVAL;
5093                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5094                         return -EIO;
5095         }
5096
5097         return 0;
5098 }
5099
5100 static struct ibm_struct beep_driver_data = {
5101         .name = "beep",
5102         .read = beep_read,
5103         .write = beep_write,
5104 };
5105
5106 /*************************************************************************
5107  * Thermal subdriver
5108  */
5109
5110 enum thermal_access_mode {
5111         TPACPI_THERMAL_NONE = 0,        /* No thermal support */
5112         TPACPI_THERMAL_ACPI_TMP07,      /* Use ACPI TMP0-7 */
5113         TPACPI_THERMAL_ACPI_UPDT,       /* Use ACPI TMP0-7 with UPDT */
5114         TPACPI_THERMAL_TPEC_8,          /* Use ACPI EC regs, 8 sensors */
5115         TPACPI_THERMAL_TPEC_16,         /* Use ACPI EC regs, 16 sensors */
5116 };
5117
5118 enum { /* TPACPI_THERMAL_TPEC_* */
5119         TP_EC_THERMAL_TMP0 = 0x78,      /* ACPI EC regs TMP 0..7 */
5120         TP_EC_THERMAL_TMP8 = 0xC0,      /* ACPI EC regs TMP 8..15 */
5121         TP_EC_THERMAL_TMP_NA = -128,    /* ACPI EC sensor not available */
5122 };
5123
5124 #define TPACPI_MAX_THERMAL_SENSORS 16   /* Max thermal sensors supported */
5125 struct ibm_thermal_sensors_struct {
5126         s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5127 };
5128
5129 static enum thermal_access_mode thermal_read_mode;
5130
5131 /* idx is zero-based */
5132 static int thermal_get_sensor(int idx, s32 *value)
5133 {
5134         int t;
5135         s8 tmp;
5136         char tmpi[5];
5137
5138         t = TP_EC_THERMAL_TMP0;
5139
5140         switch (thermal_read_mode) {
5141 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5142         case TPACPI_THERMAL_TPEC_16:
5143                 if (idx >= 8 && idx <= 15) {
5144                         t = TP_EC_THERMAL_TMP8;
5145                         idx -= 8;
5146                 }
5147                 /* fallthrough */
5148 #endif
5149         case TPACPI_THERMAL_TPEC_8:
5150                 if (idx <= 7) {
5151                         if (!acpi_ec_read(t + idx, &tmp))
5152                                 return -EIO;
5153                         *value = tmp * 1000;
5154                         return 0;
5155                 }
5156                 break;
5157
5158         case TPACPI_THERMAL_ACPI_UPDT:
5159                 if (idx <= 7) {
5160                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5161                         if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5162                                 return -EIO;
5163                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5164                                 return -EIO;
5165                         *value = (t - 2732) * 100;
5166                         return 0;
5167                 }
5168                 break;
5169
5170         case TPACPI_THERMAL_ACPI_TMP07:
5171                 if (idx <= 7) {
5172                         snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5173                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5174                                 return -EIO;
5175                         if (t > 127 || t < -127)
5176                                 t = TP_EC_THERMAL_TMP_NA;
5177                         *value = t * 1000;
5178                         return 0;
5179                 }
5180                 break;
5181
5182         case TPACPI_THERMAL_NONE:
5183         default:
5184                 return -ENOSYS;
5185         }
5186
5187         return -EINVAL;
5188 }
5189
5190 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5191 {
5192         int res, i;
5193         int n;
5194
5195         n = 8;
5196         i = 0;
5197
5198         if (!s)
5199                 return -EINVAL;
5200
5201         if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5202                 n = 16;
5203
5204         for (i = 0 ; i < n; i++) {
5205                 res = thermal_get_sensor(i, &s->temp[i]);
5206                 if (res)
5207                         return res;
5208         }
5209
5210         return n;
5211 }
5212
5213 /* sysfs temp##_input -------------------------------------------------- */
5214
5215 static ssize_t thermal_temp_input_show(struct device *dev,
5216                            struct device_attribute *attr,
5217                            char *buf)
5218 {
5219         struct sensor_device_attribute *sensor_attr =
5220                                         to_sensor_dev_attr(attr);
5221         int idx = sensor_attr->index;
5222         s32 value;
5223         int res;
5224
5225         res = thermal_get_sensor(idx, &value);
5226         if (res)
5227                 return res;
5228         if (value == TP_EC_THERMAL_TMP_NA * 1000)
5229                 return -ENXIO;
5230
5231         return snprintf(buf, PAGE_SIZE, "%d\n", value);
5232 }
5233
5234 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5235          SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5236                      thermal_temp_input_show, NULL, _idxB)
5237
5238 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5239         THERMAL_SENSOR_ATTR_TEMP(1, 0),
5240         THERMAL_SENSOR_ATTR_TEMP(2, 1),
5241         THERMAL_SENSOR_ATTR_TEMP(3, 2),
5242         THERMAL_SENSOR_ATTR_TEMP(4, 3),
5243         THERMAL_SENSOR_ATTR_TEMP(5, 4),
5244         THERMAL_SENSOR_ATTR_TEMP(6, 5),
5245         THERMAL_SENSOR_ATTR_TEMP(7, 6),
5246         THERMAL_SENSOR_ATTR_TEMP(8, 7),
5247         THERMAL_SENSOR_ATTR_TEMP(9, 8),
5248         THERMAL_SENSOR_ATTR_TEMP(10, 9),
5249         THERMAL_SENSOR_ATTR_TEMP(11, 10),
5250         THERMAL_SENSOR_ATTR_TEMP(12, 11),
5251         THERMAL_SENSOR_ATTR_TEMP(13, 12),
5252         THERMAL_SENSOR_ATTR_TEMP(14, 13),
5253         THERMAL_SENSOR_ATTR_TEMP(15, 14),
5254         THERMAL_SENSOR_ATTR_TEMP(16, 15),
5255 };
5256
5257 #define THERMAL_ATTRS(X) \
5258         &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5259
5260 static struct attribute *thermal_temp_input_attr[] = {
5261         THERMAL_ATTRS(8),
5262         THERMAL_ATTRS(9),
5263         THERMAL_ATTRS(10),
5264         THERMAL_ATTRS(11),
5265         THERMAL_ATTRS(12),
5266         THERMAL_ATTRS(13),
5267         THERMAL_ATTRS(14),
5268         THERMAL_ATTRS(15),
5269         THERMAL_ATTRS(0),
5270         THERMAL_ATTRS(1),
5271         THERMAL_ATTRS(2),
5272         THERMAL_ATTRS(3),
5273         THERMAL_ATTRS(4),
5274         THERMAL_ATTRS(5),
5275         THERMAL_ATTRS(6),
5276         THERMAL_ATTRS(7),
5277         NULL
5278 };
5279
5280 static const struct attribute_group thermal_temp_input16_group = {
5281         .attrs = thermal_temp_input_attr
5282 };
5283
5284 static const struct attribute_group thermal_temp_input8_group = {
5285         .attrs = &thermal_temp_input_attr[8]
5286 };
5287
5288 #undef THERMAL_SENSOR_ATTR_TEMP
5289 #undef THERMAL_ATTRS
5290
5291 /* --------------------------------------------------------------------- */
5292
5293 static int __init thermal_init(struct ibm_init_struct *iibm)
5294 {
5295         u8 t, ta1, ta2;
5296         int i;
5297         int acpi_tmp7;
5298         int res;
5299
5300         vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5301
5302         acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5303
5304         if (thinkpad_id.ec_model) {
5305                 /*
5306                  * Direct EC access mode: sensors at registers
5307                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
5308                  * non-implemented, thermal sensors return 0x80 when
5309                  * not available
5310                  */
5311
5312                 ta1 = ta2 = 0;
5313                 for (i = 0; i < 8; i++) {
5314                         if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5315                                 ta1 |= t;
5316                         } else {
5317                                 ta1 = 0;
5318                                 break;
5319                         }
5320                         if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5321                                 ta2 |= t;
5322                         } else {
5323                                 ta1 = 0;
5324                                 break;
5325                         }
5326                 }
5327                 if (ta1 == 0) {
5328                         /* This is sheer paranoia, but we handle it anyway */
5329                         if (acpi_tmp7) {
5330                                 printk(TPACPI_ERR
5331                                        "ThinkPad ACPI EC access misbehaving, "
5332                                        "falling back to ACPI TMPx access "
5333                                        "mode\n");
5334                                 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5335                         } else {
5336                                 printk(TPACPI_ERR
5337                                        "ThinkPad ACPI EC access misbehaving, "
5338                                        "disabling thermal sensors access\n");
5339                                 thermal_read_mode = TPACPI_THERMAL_NONE;
5340                         }
5341                 } else {
5342                         thermal_read_mode =
5343                             (ta2 != 0) ?
5344                             TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5345                 }
5346         } else if (acpi_tmp7) {
5347                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5348                         /* 600e/x, 770e, 770x */
5349                         thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5350                 } else {
5351                         /* Standard ACPI TMPx access, max 8 sensors */
5352                         thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5353                 }
5354         } else {
5355                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5356                 thermal_read_mode = TPACPI_THERMAL_NONE;
5357         }
5358
5359         vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5360                 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5361                 thermal_read_mode);
5362
5363         switch (thermal_read_mode) {
5364         case TPACPI_THERMAL_TPEC_16:
5365                 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5366                                 &thermal_temp_input16_group);
5367                 if (res)
5368                         return res;
5369                 break;
5370         case TPACPI_THERMAL_TPEC_8:
5371         case TPACPI_THERMAL_ACPI_TMP07:
5372         case TPACPI_THERMAL_ACPI_UPDT:
5373                 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5374                                 &thermal_temp_input8_group);
5375                 if (res)
5376                         return res;
5377                 break;
5378         case TPACPI_THERMAL_NONE:
5379         default:
5380                 return 1;
5381         }
5382
5383         return 0;
5384 }
5385
5386 static void thermal_exit(void)
5387 {
5388         switch (thermal_read_mode) {
5389         case TPACPI_THERMAL_TPEC_16:
5390                 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5391                                    &thermal_temp_input16_group);
5392                 break;
5393         case TPACPI_THERMAL_TPEC_8:
5394         case TPACPI_THERMAL_ACPI_TMP07:
5395         case TPACPI_THERMAL_ACPI_UPDT:
5396                 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5397                                    &thermal_temp_input16_group);
5398                 break;
5399         case TPACPI_THERMAL_NONE:
5400         default:
5401                 break;
5402         }
5403 }
5404
5405 static int thermal_read(char *p)
5406 {
5407         int len = 0;
5408         int n, i;
5409         struct ibm_thermal_sensors_struct t;
5410
5411         n = thermal_get_sensors(&t);
5412         if (unlikely(n < 0))
5413                 return n;
5414
5415         len += sprintf(p + len, "temperatures:\t");
5416
5417         if (n > 0) {
5418                 for (i = 0; i < (n - 1); i++)
5419                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5420                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5421         } else
5422                 len += sprintf(p + len, "not supported\n");
5423
5424         return len;
5425 }
5426
5427 static struct ibm_struct thermal_driver_data = {
5428         .name = "thermal",
5429         .read = thermal_read,
5430         .exit = thermal_exit,
5431 };
5432
5433 /*************************************************************************
5434  * EC Dump subdriver
5435  */
5436
5437 static u8 ecdump_regs[256];
5438
5439 static int ecdump_read(char *p)
5440 {
5441         int len = 0;
5442         int i, j;
5443         u8 v;
5444
5445         len += sprintf(p + len, "EC      "
5446                        " +00 +01 +02 +03 +04 +05 +06 +07"
5447                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5448         for (i = 0; i < 256; i += 16) {
5449                 len += sprintf(p + len, "EC 0x%02x:", i);
5450                 for (j = 0; j < 16; j++) {
5451                         if (!acpi_ec_read(i + j, &v))
5452                                 break;
5453                         if (v != ecdump_regs[i + j])
5454                                 len += sprintf(p + len, " *%02x", v);
5455                         else
5456                                 len += sprintf(p + len, "  %02x", v);
5457                         ecdump_regs[i + j] = v;
5458                 }
5459                 len += sprintf(p + len, "\n");
5460                 if (j != 16)
5461                         break;
5462         }
5463
5464         /* These are way too dangerous to advertise openly... */
5465 #if 0
5466         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5467                        " (<offset> is 00-ff, <value> is 00-ff)\n");
5468         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
5469                        " (<offset> is 00-ff, <value> is 0-255)\n");
5470 #endif
5471         return len;
5472 }
5473
5474 static int ecdump_write(char *buf)
5475 {
5476         char *cmd;
5477         int i, v;
5478
5479         while ((cmd = next_cmd(&buf))) {
5480                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5481                         /* i and v set */
5482                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5483                         /* i and v set */
5484                 } else
5485                         return -EINVAL;
5486                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5487                         if (!acpi_ec_write(i, v))
5488                                 return -EIO;
5489                 } else
5490                         return -EINVAL;
5491         }
5492
5493         return 0;
5494 }
5495
5496 static struct ibm_struct ecdump_driver_data = {
5497         .name = "ecdump",
5498         .read = ecdump_read,
5499         .write = ecdump_write,
5500         .flags.experimental = 1,
5501 };
5502
5503 /*************************************************************************
5504  * Backlight/brightness subdriver
5505  */
5506
5507 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5508
5509 enum {
5510         TP_EC_BACKLIGHT = 0x31,
5511
5512         /* TP_EC_BACKLIGHT bitmasks */
5513         TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5514         TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5515         TP_EC_BACKLIGHT_MAPSW = 0x20,
5516 };
5517
5518 static struct backlight_device *ibm_backlight_device;
5519 static int brightness_mode;
5520 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5521
5522 static struct mutex brightness_mutex;
5523
5524 /*
5525  * ThinkPads can read brightness from two places: EC 0x31, or
5526  * CMOS NVRAM byte 0x5E, bits 0-3.
5527  *
5528  * EC 0x31 has the following layout
5529  *   Bit 7: unknown function
5530  *   Bit 6: unknown function
5531  *   Bit 5: Z: honour scale changes, NZ: ignore scale changes
5532  *   Bit 4: must be set to zero to avoid problems
5533  *   Bit 3-0: backlight brightness level
5534  *
5535  * brightness_get_raw returns status data in the EC 0x31 layout
5536  */
5537 static int brightness_get_raw(int *status)
5538 {
5539         u8 lec = 0, lcmos = 0, level = 0;
5540
5541         if (brightness_mode & 1) {
5542                 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5543                         return -EIO;
5544                 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5545         };
5546         if (brightness_mode & 2) {
5547                 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5548                          & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5549                         >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5550                 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5551                 level = lcmos;
5552         }
5553
5554         if (brightness_mode == 3) {
5555                 *status = lec;  /* Prefer EC, CMOS is just a backing store */
5556                 lec &= TP_EC_BACKLIGHT_LVLMSK;
5557                 if (lec == lcmos)
5558                         tp_warned.bright_cmos_ec_unsync = 0;
5559                 else {
5560                         if (!tp_warned.bright_cmos_ec_unsync) {
5561                                 printk(TPACPI_ERR
5562                                         "CMOS NVRAM (%u) and EC (%u) do not "
5563                                         "agree on display brightness level\n",
5564                                         (unsigned int) lcmos,
5565                                         (unsigned int) lec);
5566                                 tp_warned.bright_cmos_ec_unsync = 1;
5567                         }
5568                         return -EIO;
5569                 }
5570         } else {
5571                 *status = level;
5572         }
5573
5574         return 0;
5575 }
5576
5577 /* May return EINTR which can always be mapped to ERESTARTSYS */
5578 static int brightness_set(int value)
5579 {
5580         int cmos_cmd, inc, i, res;
5581         int current_value;
5582         int command_bits;
5583
5584         if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5585             value < 0)
5586                 return -EINVAL;
5587
5588         res = mutex_lock_killable(&brightness_mutex);
5589         if (res < 0)
5590                 return res;
5591
5592         res = brightness_get_raw(&current_value);
5593         if (res < 0)
5594                 goto errout;
5595
5596         command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5597         current_value &= TP_EC_BACKLIGHT_LVLMSK;
5598
5599         cmos_cmd = value > current_value ?
5600                         TP_CMOS_BRIGHTNESS_UP :
5601                         TP_CMOS_BRIGHTNESS_DOWN;
5602         inc = (value > current_value)? 1 : -1;
5603
5604         res = 0;
5605         for (i = current_value; i != value; i += inc) {
5606                 if ((brightness_mode & 2) &&
5607                     issue_thinkpad_cmos_command(cmos_cmd)) {
5608                         res = -EIO;
5609                         goto errout;
5610                 }
5611                 if ((brightness_mode & 1) &&
5612                     !acpi_ec_write(TP_EC_BACKLIGHT,
5613                                    (i + inc) | command_bits)) {
5614                         res = -EIO;
5615                         goto errout;;
5616                 }
5617         }
5618
5619 errout:
5620         mutex_unlock(&brightness_mutex);
5621         return res;
5622 }
5623
5624 /* sysfs backlight class ----------------------------------------------- */
5625
5626 static int brightness_update_status(struct backlight_device *bd)
5627 {
5628         /* it is the backlight class's job (caller) to handle
5629          * EINTR and other errors properly */
5630         return brightness_set(
5631                 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5632                  bd->props.power == FB_BLANK_UNBLANK) ?
5633                                 bd->props.brightness : 0);
5634 }
5635
5636 static int brightness_get(struct backlight_device *bd)
5637 {
5638         int status, res;
5639
5640         res = brightness_get_raw(&status);
5641         if (res < 0)
5642                 return 0; /* FIXME: teach backlight about error handling */
5643
5644         return status & TP_EC_BACKLIGHT_LVLMSK;
5645 }
5646
5647 static struct backlight_ops ibm_backlight_data = {
5648         .get_brightness = brightness_get,
5649         .update_status  = brightness_update_status,
5650 };
5651
5652 /* --------------------------------------------------------------------- */
5653
5654 static int __init brightness_init(struct ibm_init_struct *iibm)
5655 {
5656         int b;
5657
5658         vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5659
5660         mutex_init(&brightness_mutex);
5661
5662         /*
5663          * We always attempt to detect acpi support, so as to switch
5664          * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5665          * going to publish a backlight interface
5666          */
5667         b = tpacpi_check_std_acpi_brightness_support();
5668         if (b > 0) {
5669
5670                 if (acpi_video_backlight_support()) {
5671                         if (brightness_enable > 1) {
5672                                 printk(TPACPI_NOTICE
5673                                        "Standard ACPI backlight interface "
5674                                        "available, not loading native one.\n");
5675                                 return 1;
5676                         } else if (brightness_enable == 1) {
5677                                 printk(TPACPI_NOTICE
5678                                        "Backlight control force enabled, even if standard "
5679                                        "ACPI backlight interface is available\n");
5680                         }
5681                 } else {
5682                         if (brightness_enable > 1) {
5683                                 printk(TPACPI_NOTICE
5684                                        "Standard ACPI backlight interface not "
5685                                        "available, thinkpad_acpi native "
5686                                        "brightness control enabled\n");
5687                         }
5688                 }
5689         }
5690
5691         if (!brightness_enable) {
5692                 dbg_printk(TPACPI_DBG_INIT,
5693                            "brightness support disabled by "
5694                            "module parameter\n");
5695                 return 1;
5696         }
5697
5698         if (b > 16) {
5699                 printk(TPACPI_ERR
5700                        "Unsupported brightness interface, "
5701                        "please contact %s\n", TPACPI_MAIL);
5702                 return 1;
5703         }
5704         if (b == 16)
5705                 tp_features.bright_16levels = 1;
5706
5707         if (!brightness_mode) {
5708                 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5709                         brightness_mode = 2;
5710                 else
5711                         brightness_mode = 3;
5712
5713                 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5714                         brightness_mode);
5715         }
5716
5717         if (brightness_mode > 3)
5718                 return -EINVAL;
5719
5720         if (brightness_get_raw(&b) < 0)
5721                 return 1;
5722
5723         if (tp_features.bright_16levels)
5724                 printk(TPACPI_INFO
5725                        "detected a 16-level brightness capable ThinkPad\n");
5726
5727         ibm_backlight_device = backlight_device_register(
5728                                         TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5729                                         &ibm_backlight_data);
5730         if (IS_ERR(ibm_backlight_device)) {
5731                 printk(TPACPI_ERR "Could not register backlight device\n");
5732                 return PTR_ERR(ibm_backlight_device);
5733         }
5734         vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5735
5736         ibm_backlight_device->props.max_brightness =
5737                                 (tp_features.bright_16levels)? 15 : 7;
5738         ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5739         backlight_update_status(ibm_backlight_device);
5740
5741         return 0;
5742 }
5743
5744 static void brightness_exit(void)
5745 {
5746         if (ibm_backlight_device) {
5747                 vdbg_printk(TPACPI_DBG_EXIT,
5748                             "calling backlight_device_unregister()\n");
5749                 backlight_device_unregister(ibm_backlight_device);
5750         }
5751 }
5752
5753 static int brightness_read(char *p)
5754 {
5755         int len = 0;
5756         int level;
5757
5758         level = brightness_get(NULL);
5759         if (level < 0) {
5760                 len += sprintf(p + len, "level:\t\tunreadable\n");
5761         } else {
5762                 len += sprintf(p + len, "level:\t\t%d\n", level);
5763                 len += sprintf(p + len, "commands:\tup, down\n");
5764                 len += sprintf(p + len, "commands:\tlevel <level>"
5765                                " (<level> is 0-%d)\n",
5766                                (tp_features.bright_16levels) ? 15 : 7);
5767         }
5768
5769         return len;
5770 }
5771
5772 static int brightness_write(char *buf)
5773 {
5774         int level;
5775         int rc;
5776         char *cmd;
5777         int max_level = (tp_features.bright_16levels) ? 15 : 7;
5778
5779         level = brightness_get(NULL);
5780         if (level < 0)
5781                 return level;
5782
5783         while ((cmd = next_cmd(&buf))) {
5784                 if (strlencmp(cmd, "up") == 0) {
5785                         if (level < max_level)
5786                                 level++;
5787                 } else if (strlencmp(cmd, "down") == 0) {
5788                         if (level > 0)
5789                                 level--;
5790                 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5791                            level >= 0 && level <= max_level) {
5792                         /* new level set */
5793                 } else
5794                         return -EINVAL;
5795         }
5796
5797         /*
5798          * Now we know what the final level should be, so we try to set it.
5799          * Doing it this way makes the syscall restartable in case of EINTR
5800          */
5801         rc = brightness_set(level);
5802         return (rc == -EINTR)? ERESTARTSYS : rc;
5803 }
5804
5805 static struct ibm_struct brightness_driver_data = {
5806         .name = "brightness",
5807         .read = brightness_read,
5808         .write = brightness_write,
5809         .exit = brightness_exit,
5810 };
5811
5812 /*************************************************************************
5813  * Volume subdriver
5814  */
5815
5816 static int volume_offset = 0x30;
5817
5818 static int volume_read(char *p)
5819 {
5820         int len = 0;
5821         u8 level;
5822
5823         if (!acpi_ec_read(volume_offset, &level)) {
5824                 len += sprintf(p + len, "level:\t\tunreadable\n");
5825         } else {
5826                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5827                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5828                 len += sprintf(p + len, "commands:\tup, down, mute\n");
5829                 len += sprintf(p + len, "commands:\tlevel <level>"
5830                                " (<level> is 0-15)\n");
5831         }
5832
5833         return len;
5834 }
5835
5836 static int volume_write(char *buf)
5837 {
5838         int cmos_cmd, inc, i;
5839         u8 level, mute;
5840         int new_level, new_mute;
5841         char *cmd;
5842
5843         while ((cmd = next_cmd(&buf))) {
5844                 if (!acpi_ec_read(volume_offset, &level))
5845                         return -EIO;
5846                 new_mute = mute = level & 0x40;
5847                 new_level = level = level & 0xf;
5848
5849                 if (strlencmp(cmd, "up") == 0) {
5850                         if (mute)
5851                                 new_mute = 0;
5852                         else
5853                                 new_level = level == 15 ? 15 : level + 1;
5854                 } else if (strlencmp(cmd, "down") == 0) {
5855                         if (mute)
5856                                 new_mute = 0;
5857                         else
5858                                 new_level = level == 0 ? 0 : level - 1;
5859                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5860                            new_level >= 0 && new_level <= 15) {
5861                         /* new_level set */
5862                 } else if (strlencmp(cmd, "mute") == 0) {
5863                         new_mute = 0x40;
5864                 } else
5865                         return -EINVAL;
5866
5867                 if (new_level != level) {
5868                         /* mute doesn't change */
5869
5870                         cmos_cmd = (new_level > level) ?
5871                                         TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5872                         inc = new_level > level ? 1 : -1;
5873
5874                         if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5875                                      !acpi_ec_write(volume_offset, level)))
5876                                 return -EIO;
5877
5878                         for (i = level; i != new_level; i += inc)
5879                                 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5880                                     !acpi_ec_write(volume_offset, i + inc))
5881                                         return -EIO;
5882
5883                         if (mute &&
5884                             (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5885                              !acpi_ec_write(volume_offset, new_level + mute))) {
5886                                 return -EIO;
5887                         }
5888                 }
5889
5890                 if (new_mute != mute) {
5891                         /* level doesn't change */
5892
5893                         cmos_cmd = (new_mute) ?
5894                                    TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5895
5896                         if (issue_thinkpad_cmos_command(cmos_cmd) ||
5897                             !acpi_ec_write(volume_offset, level + new_mute))
5898                                 return -EIO;
5899                 }
5900         }
5901
5902         return 0;
5903 }
5904
5905 static struct ibm_struct volume_driver_data = {
5906         .name = "volume",
5907         .read = volume_read,
5908         .write = volume_write,
5909 };
5910
5911 /*************************************************************************
5912  * Fan subdriver
5913  */
5914
5915 /*
5916  * FAN ACCESS MODES
5917  *
5918  * TPACPI_FAN_RD_ACPI_GFAN:
5919  *      ACPI GFAN method: returns fan level
5920  *
5921  *      see TPACPI_FAN_WR_ACPI_SFAN
5922  *      EC 0x2f (HFSP) not available if GFAN exists
5923  *
5924  * TPACPI_FAN_WR_ACPI_SFAN:
5925  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5926  *
5927  *      EC 0x2f (HFSP) might be available *for reading*, but do not use
5928  *      it for writing.
5929  *
5930  * TPACPI_FAN_WR_TPEC:
5931  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode
5932  *      Supported on almost all ThinkPads
5933  *
5934  *      Fan speed changes of any sort (including those caused by the
5935  *      disengaged mode) are usually done slowly by the firmware as the
5936  *      maximum ammount of fan duty cycle change per second seems to be
5937  *      limited.
5938  *
5939  *      Reading is not available if GFAN exists.
5940  *      Writing is not available if SFAN exists.
5941  *
5942  *      Bits
5943  *       7      automatic mode engaged;
5944  *              (default operation mode of the ThinkPad)
5945  *              fan level is ignored in this mode.
5946  *       6      full speed mode (takes precedence over bit 7);
5947  *              not available on all thinkpads.  May disable
5948  *              the tachometer while the fan controller ramps up
5949  *              the speed (which can take up to a few *minutes*).
5950  *              Speeds up fan to 100% duty-cycle, which is far above
5951  *              the standard RPM levels.  It is not impossible that
5952  *              it could cause hardware damage.
5953  *      5-3     unused in some models.  Extra bits for fan level
5954  *              in others, but still useless as all values above
5955  *              7 map to the same speed as level 7 in these models.
5956  *      2-0     fan level (0..7 usually)
5957  *                      0x00 = stop
5958  *                      0x07 = max (set when temperatures critical)
5959  *              Some ThinkPads may have other levels, see
5960  *              TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5961  *
5962  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5963  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5964  *      does so, its initial value is meaningless (0x07).
5965  *
5966  *      For firmware bugs, refer to:
5967  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5968  *
5969  *      ----
5970  *
5971  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5972  *      Main fan tachometer reading (in RPM)
5973  *
5974  *      This register is present on all ThinkPads with a new-style EC, and
5975  *      it is known not to be present on the A21m/e, and T22, as there is
5976  *      something else in offset 0x84 according to the ACPI DSDT.  Other
5977  *      ThinkPads from this same time period (and earlier) probably lack the
5978  *      tachometer as well.
5979  *
5980  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5981  *      was never fixed by IBM to report the EC firmware version string
5982  *      probably support the tachometer (like the early X models), so
5983  *      detecting it is quite hard.  We need more data to know for sure.
5984  *
5985  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5986  *      might result.
5987  *
5988  *      FIRMWARE BUG: may go stale while the EC is switching to full speed
5989  *      mode.
5990  *
5991  *      For firmware bugs, refer to:
5992  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5993  *
5994  * TPACPI_FAN_WR_ACPI_FANS:
5995  *      ThinkPad X31, X40, X41.  Not available in the X60.
5996  *
5997  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
5998  *      high speed.  ACPI DSDT seems to map these three speeds to levels
5999  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6000  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6001  *
6002  *      The speeds are stored on handles
6003  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6004  *
6005  *      There are three default speed sets, acessible as handles:
6006  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6007  *
6008  *      ACPI DSDT switches which set is in use depending on various
6009  *      factors.
6010  *
6011  *      TPACPI_FAN_WR_TPEC is also available and should be used to
6012  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
6013  *      but the ACPI tables just mention level 7.
6014  */
6015
6016 enum {                                  /* Fan control constants */
6017         fan_status_offset = 0x2f,       /* EC register 0x2f */
6018         fan_rpm_offset = 0x84,          /* EC register 0x84: LSB, 0x85 MSB (RPM)
6019                                          * 0x84 must be read before 0x85 */
6020
6021         TP_EC_FAN_FULLSPEED = 0x40,     /* EC fan mode: full speed */
6022         TP_EC_FAN_AUTO      = 0x80,     /* EC fan mode: auto fan control */
6023
6024         TPACPI_FAN_LAST_LEVEL = 0x100,  /* Use cached last-seen fan level */
6025 };
6026
6027 enum fan_status_access_mode {
6028         TPACPI_FAN_NONE = 0,            /* No fan status or control */
6029         TPACPI_FAN_RD_ACPI_GFAN,        /* Use ACPI GFAN */
6030         TPACPI_FAN_RD_TPEC,             /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6031 };
6032
6033 enum fan_control_access_mode {
6034         TPACPI_FAN_WR_NONE = 0,         /* No fan control */
6035         TPACPI_FAN_WR_ACPI_SFAN,        /* Use ACPI SFAN */
6036         TPACPI_FAN_WR_TPEC,             /* Use ACPI EC reg 0x2f */
6037         TPACPI_FAN_WR_ACPI_FANS,        /* Use ACPI FANS and EC reg 0x2f */
6038 };
6039
6040 enum fan_control_commands {
6041         TPACPI_FAN_CMD_SPEED    = 0x0001,       /* speed command */
6042         TPACPI_FAN_CMD_LEVEL    = 0x0002,       /* level command  */
6043         TPACPI_FAN_CMD_ENABLE   = 0x0004,       /* enable/disable cmd,
6044                                                  * and also watchdog cmd */
6045 };
6046
6047 static int fan_control_allowed;
6048
6049 static enum fan_status_access_mode fan_status_access_mode;
6050 static enum fan_control_access_mode fan_control_access_mode;
6051 static enum fan_control_commands fan_control_commands;
6052
6053 static u8 fan_control_initial_status;
6054 static u8 fan_control_desired_level;
6055 static u8 fan_control_resume_level;
6056 static int fan_watchdog_maxinterval;
6057
6058 static struct mutex fan_mutex;
6059
6060 static void fan_watchdog_fire(struct work_struct *ignored);
6061 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6062
6063 TPACPI_HANDLE(fans, ec, "FANS");        /* X31, X40, X41 */
6064 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6065            "\\FSPD",            /* 600e/x, 770e, 770x */
6066            );                   /* all others */
6067 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6068            "JFNS",              /* 770x-JL */
6069            );                   /* all others */
6070
6071 /*
6072  * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6073  * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6074  * be in auto mode (0x80).
6075  *
6076  * This is corrected by any write to HFSP either by the driver, or
6077  * by the firmware.
6078  *
6079  * We assume 0x07 really means auto mode while this quirk is active,
6080  * as this is far more likely than the ThinkPad being in level 7,
6081  * which is only used by the firmware during thermal emergencies.
6082  */
6083
6084 static void fan_quirk1_detect(void)
6085 {
6086         /* In some ThinkPads, neither the EC nor the ACPI
6087          * DSDT initialize the HFSP register, and it ends up
6088          * being initially set to 0x07 when it *could* be
6089          * either 0x07 or 0x80.
6090          *
6091          * Enable for TP-1Y (T43), TP-78 (R51e),
6092          * TP-76 (R52), TP-70 (T43, R52), which are known
6093          * to be buggy. */
6094         if (fan_control_initial_status == 0x07) {
6095                 switch (thinkpad_id.ec_model) {
6096                 case 0x5931: /* TP-1Y */
6097                 case 0x3837: /* TP-78 */
6098                 case 0x3637: /* TP-76 */
6099                 case 0x3037: /* TP-70 */
6100                         printk(TPACPI_NOTICE
6101                                "fan_init: initial fan status is unknown, "
6102                                "assuming it is in auto mode\n");
6103                         tp_features.fan_ctrl_status_undef = 1;
6104                         ;;
6105                 }
6106         }
6107 }
6108
6109 static void fan_quirk1_handle(u8 *fan_status)
6110 {
6111         if (unlikely(tp_features.fan_ctrl_status_undef)) {
6112                 if (*fan_status != fan_control_initial_status) {
6113                         /* something changed the HFSP regisnter since
6114                          * driver init time, so it is not undefined
6115                          * anymore */
6116                         tp_features.fan_ctrl_status_undef = 0;
6117                 } else {
6118                         /* Return most likely status. In fact, it
6119                          * might be the only possible status */
6120                         *fan_status = TP_EC_FAN_AUTO;
6121                 }
6122         }
6123 }
6124
6125 /*
6126  * Call with fan_mutex held
6127  */
6128 static void fan_update_desired_level(u8 status)
6129 {
6130         if ((status &
6131              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6132                 if (status > 7)
6133                         fan_control_desired_level = 7;
6134                 else
6135                         fan_control_desired_level = status;
6136         }
6137 }
6138
6139 static int fan_get_status(u8 *status)
6140 {
6141         u8 s;
6142
6143         /* TODO:
6144          * Add TPACPI_FAN_RD_ACPI_FANS ? */
6145
6146         switch (fan_status_access_mode) {
6147         case TPACPI_FAN_RD_ACPI_GFAN:
6148                 /* 570, 600e/x, 770e, 770x */
6149
6150                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6151                         return -EIO;
6152
6153                 if (likely(status))
6154                         *status = s & 0x07;
6155
6156                 break;
6157
6158         case TPACPI_FAN_RD_TPEC:
6159                 /* all except 570, 600e/x, 770e, 770x */
6160                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6161                         return -EIO;
6162
6163                 if (likely(status)) {
6164                         *status = s;
6165                         fan_quirk1_handle(status);
6166                 }
6167
6168                 break;
6169
6170         default:
6171                 return -ENXIO;
6172         }
6173
6174         return 0;
6175 }
6176
6177 static int fan_get_status_safe(u8 *status)
6178 {
6179         int rc;
6180         u8 s;
6181
6182         if (mutex_lock_killable(&fan_mutex))
6183                 return -ERESTARTSYS;
6184         rc = fan_get_status(&s);
6185         if (!rc)
6186                 fan_update_desired_level(s);
6187         mutex_unlock(&fan_mutex);
6188
6189         if (status)
6190                 *status = s;
6191
6192         return rc;
6193 }
6194
6195 static int fan_get_speed(unsigned int *speed)
6196 {
6197         u8 hi, lo;
6198
6199         switch (fan_status_access_mode) {
6200         case TPACPI_FAN_RD_TPEC:
6201                 /* all except 570, 600e/x, 770e, 770x */
6202                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6203                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6204                         return -EIO;
6205
6206                 if (likely(speed))
6207                         *speed = (hi << 8) | lo;
6208
6209                 break;
6210
6211         default:
6212                 return -ENXIO;
6213         }
6214
6215         return 0;
6216 }
6217
6218 static int fan_set_level(int level)
6219 {
6220         if (!fan_control_allowed)
6221                 return -EPERM;
6222
6223         switch (fan_control_access_mode) {
6224         case TPACPI_FAN_WR_ACPI_SFAN:
6225                 if (level >= 0 && level <= 7) {
6226                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6227                                 return -EIO;
6228                 } else
6229                         return -EINVAL;
6230                 break;
6231
6232         case TPACPI_FAN_WR_ACPI_FANS:
6233         case TPACPI_FAN_WR_TPEC:
6234                 if (!(level & TP_EC_FAN_AUTO) &&
6235                     !(level & TP_EC_FAN_FULLSPEED) &&
6236                     ((level < 0) || (level > 7)))
6237                         return -EINVAL;
6238
6239                 /* safety net should the EC not support AUTO
6240                  * or FULLSPEED mode bits and just ignore them */
6241                 if (level & TP_EC_FAN_FULLSPEED)
6242                         level |= 7;     /* safety min speed 7 */
6243                 else if (level & TP_EC_FAN_AUTO)
6244                         level |= 4;     /* safety min speed 4 */
6245
6246                 if (!acpi_ec_write(fan_status_offset, level))
6247                         return -EIO;
6248                 else
6249                         tp_features.fan_ctrl_status_undef = 0;
6250                 break;
6251
6252         default:
6253                 return -ENXIO;
6254         }
6255         return 0;
6256 }
6257
6258 static int fan_set_level_safe(int level)
6259 {
6260         int rc;
6261
6262         if (!fan_control_allowed)
6263                 return -EPERM;
6264
6265         if (mutex_lock_killable(&fan_mutex))
6266                 return -ERESTARTSYS;
6267
6268         if (level == TPACPI_FAN_LAST_LEVEL)
6269                 level = fan_control_desired_level;
6270
6271         rc = fan_set_level(level);
6272         if (!rc)
6273                 fan_update_desired_level(level);
6274
6275         mutex_unlock(&fan_mutex);
6276         return rc;
6277 }
6278
6279 static int fan_set_enable(void)
6280 {
6281         u8 s;
6282         int rc;
6283
6284         if (!fan_control_allowed)
6285                 return -EPERM;
6286
6287         if (mutex_lock_killable(&fan_mutex))
6288                 return -ERESTARTSYS;
6289
6290         switch (fan_control_access_mode) {
6291         case TPACPI_FAN_WR_ACPI_FANS:
6292         case TPACPI_FAN_WR_TPEC:
6293                 rc = fan_get_status(&s);
6294                 if (rc < 0)
6295                         break;
6296
6297                 /* Don't go out of emergency fan mode */
6298                 if (s != 7) {
6299                         s &= 0x07;
6300                         s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6301                 }
6302
6303                 if (!acpi_ec_write(fan_status_offset, s))
6304                         rc = -EIO;
6305                 else {
6306                         tp_features.fan_ctrl_status_undef = 0;
6307                         rc = 0;
6308                 }
6309                 break;
6310
6311         case TPACPI_FAN_WR_ACPI_SFAN:
6312                 rc = fan_get_status(&s);
6313                 if (rc < 0)
6314                         break;
6315
6316                 s &= 0x07;
6317
6318                 /* Set fan to at least level 4 */
6319                 s |= 4;
6320
6321                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6322                         rc = -EIO;
6323                 else
6324                         rc = 0;
6325                 break;
6326
6327         default:
6328                 rc = -ENXIO;
6329         }
6330
6331         mutex_unlock(&fan_mutex);
6332         return rc;
6333 }
6334
6335 static int fan_set_disable(void)
6336 {
6337         int rc;
6338
6339         if (!fan_control_allowed)
6340                 return -EPERM;
6341
6342         if (mutex_lock_killable(&fan_mutex))
6343                 return -ERESTARTSYS;
6344
6345         rc = 0;
6346         switch (fan_control_access_mode) {
6347         case TPACPI_FAN_WR_ACPI_FANS:
6348         case TPACPI_FAN_WR_TPEC:
6349                 if (!acpi_ec_write(fan_status_offset, 0x00))
6350                         rc = -EIO;
6351                 else {
6352                         fan_control_desired_level = 0;
6353                         tp_features.fan_ctrl_status_undef = 0;
6354                 }
6355                 break;
6356
6357         case TPACPI_FAN_WR_ACPI_SFAN:
6358                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6359                         rc = -EIO;
6360                 else
6361                         fan_control_desired_level = 0;
6362                 break;
6363
6364         default:
6365                 rc = -ENXIO;
6366         }
6367
6368
6369         mutex_unlock(&fan_mutex);
6370         return rc;
6371 }
6372
6373 static int fan_set_speed(int speed)
6374 {
6375         int rc;
6376
6377         if (!fan_control_allowed)
6378                 return -EPERM;
6379
6380         if (mutex_lock_killable(&fan_mutex))
6381                 return -ERESTARTSYS;
6382
6383         rc = 0;
6384         switch (fan_control_access_mode) {
6385         case TPACPI_FAN_WR_ACPI_FANS:
6386                 if (speed >= 0 && speed <= 65535) {
6387                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6388                                         speed, speed, speed))
6389                                 rc = -EIO;
6390                 } else
6391                         rc = -EINVAL;
6392                 break;
6393
6394         default:
6395                 rc = -ENXIO;
6396         }
6397
6398         mutex_unlock(&fan_mutex);
6399         return rc;
6400 }
6401
6402 static void fan_watchdog_reset(void)
6403 {
6404         static int fan_watchdog_active;
6405
6406         if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6407                 return;
6408
6409         if (fan_watchdog_active)
6410                 cancel_delayed_work(&fan_watchdog_task);
6411
6412         if (fan_watchdog_maxinterval > 0 &&
6413             tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6414                 fan_watchdog_active = 1;
6415                 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6416                                 msecs_to_jiffies(fan_watchdog_maxinterval
6417                                                  * 1000))) {
6418                         printk(TPACPI_ERR
6419                                "failed to queue the fan watchdog, "
6420                                "watchdog will not trigger\n");
6421                 }
6422         } else
6423                 fan_watchdog_active = 0;
6424 }
6425
6426 static void fan_watchdog_fire(struct work_struct *ignored)
6427 {
6428         int rc;
6429
6430         if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6431                 return;
6432
6433         printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6434         rc = fan_set_enable();
6435         if (rc < 0) {
6436                 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6437                         "will try again later...\n", -rc);
6438                 /* reschedule for later */
6439                 fan_watchdog_reset();
6440         }
6441 }
6442
6443 /*
6444  * SYSFS fan layout: hwmon compatible (device)
6445  *
6446  * pwm*_enable:
6447  *      0: "disengaged" mode
6448  *      1: manual mode
6449  *      2: native EC "auto" mode (recommended, hardware default)
6450  *
6451  * pwm*: set speed in manual mode, ignored otherwise.
6452  *      0 is level 0; 255 is level 7. Intermediate points done with linear
6453  *      interpolation.
6454  *
6455  * fan*_input: tachometer reading, RPM
6456  *
6457  *
6458  * SYSFS fan layout: extensions
6459  *
6460  * fan_watchdog (driver):
6461  *      fan watchdog interval in seconds, 0 disables (default), max 120
6462  */
6463
6464 /* sysfs fan pwm1_enable ----------------------------------------------- */
6465 static ssize_t fan_pwm1_enable_show(struct device *dev,
6466                                     struct device_attribute *attr,
6467                                     char *buf)
6468 {
6469         int res, mode;
6470         u8 status;
6471
6472         res = fan_get_status_safe(&status);
6473         if (res)
6474                 return res;
6475
6476         if (status & TP_EC_FAN_FULLSPEED) {
6477                 mode = 0;
6478         } else if (status & TP_EC_FAN_AUTO) {
6479                 mode = 2;
6480         } else
6481                 mode = 1;
6482
6483         return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6484 }
6485
6486 static ssize_t fan_pwm1_enable_store(struct device *dev,
6487                                      struct device_attribute *attr,
6488                                      const char *buf, size_t count)
6489 {
6490         unsigned long t;
6491         int res, level;
6492
6493         if (parse_strtoul(buf, 2, &t))
6494                 return -EINVAL;
6495
6496         switch (t) {
6497         case 0:
6498                 level = TP_EC_FAN_FULLSPEED;
6499                 break;
6500         case 1:
6501                 level = TPACPI_FAN_LAST_LEVEL;
6502                 break;
6503         case 2:
6504                 level = TP_EC_FAN_AUTO;
6505                 break;
6506         case 3:
6507                 /* reserved for software-controlled auto mode */
6508                 return -ENOSYS;
6509         default:
6510                 return -EINVAL;
6511         }
6512
6513         res = fan_set_level_safe(level);
6514         if (res == -ENXIO)
6515                 return -EINVAL;
6516         else if (res < 0)
6517                 return res;
6518
6519         fan_watchdog_reset();
6520
6521         return count;
6522 }
6523
6524 static struct device_attribute dev_attr_fan_pwm1_enable =
6525         __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6526                 fan_pwm1_enable_show, fan_pwm1_enable_store);
6527
6528 /* sysfs fan pwm1 ------------------------------------------------------ */
6529 static ssize_t fan_pwm1_show(struct device *dev,
6530                              struct device_attribute *attr,
6531                              char *buf)
6532 {
6533         int res;
6534         u8 status;
6535
6536         res = fan_get_status_safe(&status);
6537         if (res)
6538                 return res;
6539
6540         if ((status &
6541              (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6542                 status = fan_control_desired_level;
6543
6544         if (status > 7)
6545                 status = 7;
6546
6547         return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6548 }
6549
6550 static ssize_t fan_pwm1_store(struct device *dev,
6551                               struct device_attribute *attr,
6552                               const char *buf, size_t count)
6553 {
6554         unsigned long s;
6555         int rc;
6556         u8 status, newlevel;
6557
6558         if (parse_strtoul(buf, 255, &s))
6559                 return -EINVAL;
6560
6561         /* scale down from 0-255 to 0-7 */
6562         newlevel = (s >> 5) & 0x07;
6563
6564         if (mutex_lock_killable(&fan_mutex))
6565                 return -ERESTARTSYS;
6566
6567         rc = fan_get_status(&status);
6568         if (!rc && (status &
6569                     (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6570                 rc = fan_set_level(newlevel);
6571                 if (rc == -ENXIO)
6572                         rc = -EINVAL;
6573                 else if (!rc) {
6574                         fan_update_desired_level(newlevel);
6575                         fan_watchdog_reset();
6576                 }
6577         }
6578
6579         mutex_unlock(&fan_mutex);
6580         return (rc)? rc : count;
6581 }
6582
6583 static struct device_attribute dev_attr_fan_pwm1 =
6584         __ATTR(pwm1, S_IWUSR | S_IRUGO,
6585                 fan_pwm1_show, fan_pwm1_store);
6586
6587 /* sysfs fan fan1_input ------------------------------------------------ */
6588 static ssize_t fan_fan1_input_show(struct device *dev,
6589                            struct device_attribute *attr,
6590                            char *buf)
6591 {
6592         int res;
6593         unsigned int speed;
6594
6595         res = fan_get_speed(&speed);
6596         if (res < 0)
6597                 return res;
6598
6599         return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6600 }
6601
6602 static struct device_attribute dev_attr_fan_fan1_input =
6603         __ATTR(fan1_input, S_IRUGO,
6604                 fan_fan1_input_show, NULL);
6605
6606 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6607 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6608                                      char *buf)
6609 {
6610         return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6611 }
6612
6613 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6614                                       const char *buf, size_t count)
6615 {
6616         unsigned long t;
6617
6618         if (parse_strtoul(buf, 120, &t))
6619                 return -EINVAL;
6620
6621         if (!fan_control_allowed)
6622                 return -EPERM;
6623
6624         fan_watchdog_maxinterval = t;
6625         fan_watchdog_reset();
6626
6627         return count;
6628 }
6629
6630 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6631                 fan_fan_watchdog_show, fan_fan_watchdog_store);
6632
6633 /* --------------------------------------------------------------------- */
6634 static struct attribute *fan_attributes[] = {
6635         &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6636         &dev_attr_fan_fan1_input.attr,
6637         NULL
6638 };
6639
6640 static const struct attribute_group fan_attr_group = {
6641         .attrs = fan_attributes,
6642 };
6643
6644 static int __init fan_init(struct ibm_init_struct *iibm)
6645 {
6646         int rc;
6647
6648         vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6649
6650         mutex_init(&fan_mutex);
6651         fan_status_access_mode = TPACPI_FAN_NONE;
6652         fan_control_access_mode = TPACPI_FAN_WR_NONE;
6653         fan_control_commands = 0;
6654         fan_watchdog_maxinterval = 0;
6655         tp_features.fan_ctrl_status_undef = 0;
6656         fan_control_desired_level = 7;
6657
6658         TPACPI_ACPIHANDLE_INIT(fans);
6659         TPACPI_ACPIHANDLE_INIT(gfan);
6660         TPACPI_ACPIHANDLE_INIT(sfan);
6661
6662         if (gfan_handle) {
6663                 /* 570, 600e/x, 770e, 770x */
6664                 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6665         } else {
6666                 /* all other ThinkPads: note that even old-style
6667                  * ThinkPad ECs supports the fan control register */
6668                 if (likely(acpi_ec_read(fan_status_offset,
6669                                         &fan_control_initial_status))) {
6670                         fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6671                         fan_quirk1_detect();
6672                 } else {
6673                         printk(TPACPI_ERR
6674                                "ThinkPad ACPI EC access misbehaving, "
6675                                "fan status and control unavailable\n");
6676                         return 1;
6677                 }
6678         }
6679
6680         if (sfan_handle) {
6681                 /* 570, 770x-JL */
6682                 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6683                 fan_control_commands |=
6684                     TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6685         } else {
6686                 if (!gfan_handle) {
6687                         /* gfan without sfan means no fan control */
6688                         /* all other models implement TP EC 0x2f control */
6689
6690                         if (fans_handle) {
6691                                 /* X31, X40, X41 */
6692                                 fan_control_access_mode =
6693                                     TPACPI_FAN_WR_ACPI_FANS;
6694                                 fan_control_commands |=
6695                                     TPACPI_FAN_CMD_SPEED |
6696                                     TPACPI_FAN_CMD_LEVEL |
6697                                     TPACPI_FAN_CMD_ENABLE;
6698                         } else {
6699                                 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6700                                 fan_control_commands |=
6701                                     TPACPI_FAN_CMD_LEVEL |
6702                                     TPACPI_FAN_CMD_ENABLE;
6703                         }
6704                 }
6705         }
6706
6707         vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6708                 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6709                   fan_control_access_mode != TPACPI_FAN_WR_NONE),
6710                 fan_status_access_mode, fan_control_access_mode);
6711
6712         /* fan control master switch */
6713         if (!fan_control_allowed) {
6714                 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6715                 fan_control_commands = 0;
6716                 dbg_printk(TPACPI_DBG_INIT,
6717                            "fan control features disabled by parameter\n");
6718         }
6719
6720         /* update fan_control_desired_level */
6721         if (fan_status_access_mode != TPACPI_FAN_NONE)
6722                 fan_get_status_safe(NULL);
6723
6724         if (fan_status_access_mode != TPACPI_FAN_NONE ||
6725             fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6726                 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6727                                          &fan_attr_group);
6728                 if (rc < 0)
6729                         return rc;
6730
6731                 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6732                                         &driver_attr_fan_watchdog);
6733                 if (rc < 0) {
6734                         sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6735                                         &fan_attr_group);
6736                         return rc;
6737                 }
6738                 return 0;
6739         } else
6740                 return 1;
6741 }
6742
6743 static void fan_exit(void)
6744 {
6745         vdbg_printk(TPACPI_DBG_EXIT,
6746                     "cancelling any pending fan watchdog tasks\n");
6747
6748         /* FIXME: can we really do this unconditionally? */
6749         sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6750         driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6751                            &driver_attr_fan_watchdog);
6752
6753         cancel_delayed_work(&fan_watchdog_task);
6754         flush_workqueue(tpacpi_wq);
6755 }
6756
6757 static void fan_suspend(pm_message_t state)
6758 {
6759         int rc;
6760
6761         if (!fan_control_allowed)
6762                 return;
6763
6764         /* Store fan status in cache */
6765         fan_control_resume_level = 0;
6766         rc = fan_get_status_safe(&fan_control_resume_level);
6767         if (rc < 0)
6768                 printk(TPACPI_NOTICE
6769                         "failed to read fan level for later "
6770                         "restore during resume: %d\n", rc);
6771
6772         /* if it is undefined, don't attempt to restore it.
6773          * KEEP THIS LAST */
6774         if (tp_features.fan_ctrl_status_undef)
6775                 fan_control_resume_level = 0;
6776 }
6777
6778 static void fan_resume(void)
6779 {
6780         u8 current_level = 7;
6781         bool do_set = false;
6782         int rc;
6783
6784         /* DSDT *always* updates status on resume */
6785         tp_features.fan_ctrl_status_undef = 0;
6786
6787         if (!fan_control_allowed ||
6788             !fan_control_resume_level ||
6789             (fan_get_status_safe(&current_level) < 0))
6790                 return;
6791
6792         switch (fan_control_access_mode) {
6793         case TPACPI_FAN_WR_ACPI_SFAN:
6794                 /* never decrease fan level */
6795                 do_set = (fan_control_resume_level > current_level);
6796                 break;
6797         case TPACPI_FAN_WR_ACPI_FANS:
6798         case TPACPI_FAN_WR_TPEC:
6799                 /* never decrease fan level, scale is:
6800                  * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6801                  *
6802                  * We expect the firmware to set either 7 or AUTO, but we
6803                  * handle FULLSPEED out of paranoia.
6804                  *
6805                  * So, we can safely only restore FULLSPEED or 7, anything
6806                  * else could slow the fan.  Restoring AUTO is useless, at
6807                  * best that's exactly what the DSDT already set (it is the
6808                  * slower it uses).
6809                  *
6810                  * Always keep in mind that the DSDT *will* have set the
6811                  * fans to what the vendor supposes is the best level.  We
6812                  * muck with it only to speed the fan up.
6813                  */
6814                 if (fan_control_resume_level != 7 &&
6815                     !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6816                         return;
6817                 else
6818                         do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6819                                  (current_level != fan_control_resume_level);
6820                 break;
6821         default:
6822                 return;
6823         }
6824         if (do_set) {
6825                 printk(TPACPI_NOTICE
6826                         "restoring fan level to 0x%02x\n",
6827                         fan_control_resume_level);
6828                 rc = fan_set_level_safe(fan_control_resume_level);
6829                 if (rc < 0)
6830                         printk(TPACPI_NOTICE
6831                                 "failed to restore fan level: %d\n", rc);
6832         }
6833 }
6834
6835 static int fan_read(char *p)
6836 {
6837         int len = 0;
6838         int rc;
6839         u8 status;
6840         unsigned int speed = 0;
6841
6842         switch (fan_status_access_mode) {
6843         case TPACPI_FAN_RD_ACPI_GFAN:
6844                 /* 570, 600e/x, 770e, 770x */
6845                 rc = fan_get_status_safe(&status);
6846                 if (rc < 0)
6847                         return rc;
6848
6849                 len += sprintf(p + len, "status:\t\t%s\n"
6850                                "level:\t\t%d\n",
6851                                (status != 0) ? "enabled" : "disabled", status);
6852                 break;
6853
6854         case TPACPI_FAN_RD_TPEC:
6855                 /* all except 570, 600e/x, 770e, 770x */
6856                 rc = fan_get_status_safe(&status);
6857                 if (rc < 0)
6858                         return rc;
6859
6860                 len += sprintf(p + len, "status:\t\t%s\n",
6861                                (status != 0) ? "enabled" : "disabled");
6862
6863                 rc = fan_get_speed(&speed);
6864                 if (rc < 0)
6865                         return rc;
6866
6867                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6868
6869                 if (status & TP_EC_FAN_FULLSPEED)
6870                         /* Disengaged mode takes precedence */
6871                         len += sprintf(p + len, "level:\t\tdisengaged\n");
6872                 else if (status & TP_EC_FAN_AUTO)
6873                         len += sprintf(p + len, "level:\t\tauto\n");
6874                 else
6875                         len += sprintf(p + len, "level:\t\t%d\n", status);
6876                 break;
6877
6878         case TPACPI_FAN_NONE:
6879         default:
6880                 len += sprintf(p + len, "status:\t\tnot supported\n");
6881         }
6882
6883         if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6884                 len += sprintf(p + len, "commands:\tlevel <level>");
6885
6886                 switch (fan_control_access_mode) {
6887                 case TPACPI_FAN_WR_ACPI_SFAN:
6888                         len += sprintf(p + len, " (<level> is 0-7)\n");
6889                         break;
6890
6891                 default:
6892                         len += sprintf(p + len, " (<level> is 0-7, "
6893                                        "auto, disengaged, full-speed)\n");
6894                         break;
6895                 }
6896         }
6897
6898         if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6899                 len += sprintf(p + len, "commands:\tenable, disable\n"
6900                                "commands:\twatchdog <timeout> (<timeout> "
6901                                "is 0 (off), 1-120 (seconds))\n");
6902
6903         if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6904                 len += sprintf(p + len, "commands:\tspeed <speed>"
6905                                " (<speed> is 0-65535)\n");
6906
6907         return len;
6908 }
6909
6910 static int fan_write_cmd_level(const char *cmd, int *rc)
6911 {
6912         int level;
6913
6914         if (strlencmp(cmd, "level auto") == 0)
6915                 level = TP_EC_FAN_AUTO;
6916         else if ((strlencmp(cmd, "level disengaged") == 0) |
6917                         (strlencmp(cmd, "level full-speed") == 0))
6918                 level = TP_EC_FAN_FULLSPEED;
6919         else if (sscanf(cmd, "level %d", &level) != 1)
6920                 return 0;
6921
6922         *rc = fan_set_level_safe(level);
6923         if (*rc == -ENXIO)
6924                 printk(TPACPI_ERR "level command accepted for unsupported "
6925                        "access mode %d", fan_control_access_mode);
6926
6927         return 1;
6928 }
6929
6930 static int fan_write_cmd_enable(const char *cmd, int *rc)
6931 {
6932         if (strlencmp(cmd, "enable") != 0)
6933                 return 0;
6934
6935         *rc = fan_set_enable();
6936         if (*rc == -ENXIO)
6937                 printk(TPACPI_ERR "enable command accepted for unsupported "
6938                        "access mode %d", fan_control_access_mode);
6939
6940         return 1;
6941 }
6942
6943 static int fan_write_cmd_disable(const char *cmd, int *rc)
6944 {
6945         if (strlencmp(cmd, "disable") != 0)
6946                 return 0;
6947
6948         *rc = fan_set_disable();
6949         if (*rc == -ENXIO)
6950                 printk(TPACPI_ERR "disable command accepted for unsupported "
6951                        "access mode %d", fan_control_access_mode);
6952
6953         return 1;
6954 }
6955
6956 static int fan_write_cmd_speed(const char *cmd, int *rc)
6957 {
6958         int speed;
6959
6960         /* TODO:
6961          * Support speed <low> <medium> <high> ? */
6962
6963         if (sscanf(cmd, "speed %d", &speed) != 1)
6964                 return 0;
6965
6966         *rc = fan_set_speed(speed);
6967         if (*rc == -ENXIO)
6968                 printk(TPACPI_ERR "speed command accepted for unsupported "
6969                        "access mode %d", fan_control_access_mode);
6970
6971         return 1;
6972 }
6973
6974 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6975 {
6976         int interval;
6977
6978         if (sscanf(cmd, "watchdog %d", &interval) != 1)
6979                 return 0;
6980
6981         if (interval < 0 || interval > 120)
6982                 *rc = -EINVAL;
6983         else
6984                 fan_watchdog_maxinterval = interval;
6985
6986         return 1;
6987 }
6988
6989 static int fan_write(char *buf)
6990 {
6991         char *cmd;
6992         int rc = 0;
6993
6994         while (!rc && (cmd = next_cmd(&buf))) {
6995                 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6996                       fan_write_cmd_level(cmd, &rc)) &&
6997                     !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6998                       (fan_write_cmd_enable(cmd, &rc) ||
6999                        fan_write_cmd_disable(cmd, &rc) ||
7000                        fan_write_cmd_watchdog(cmd, &rc))) &&
7001                     !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7002                       fan_write_cmd_speed(cmd, &rc))
7003                     )
7004                         rc = -EINVAL;
7005                 else if (!rc)
7006                         fan_watchdog_reset();
7007         }
7008
7009         return rc;
7010 }
7011
7012 static struct ibm_struct fan_driver_data = {
7013         .name = "fan",
7014         .read = fan_read,
7015         .write = fan_write,
7016         .exit = fan_exit,
7017         .suspend = fan_suspend,
7018         .resume = fan_resume,
7019 };
7020
7021 /****************************************************************************
7022  ****************************************************************************
7023  *
7024  * Infrastructure
7025  *
7026  ****************************************************************************
7027  ****************************************************************************/
7028
7029 /* sysfs name ---------------------------------------------------------- */
7030 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7031                            struct device_attribute *attr,
7032                            char *buf)
7033 {
7034         return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7035 }
7036
7037 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7038         __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7039
7040 /* --------------------------------------------------------------------- */
7041
7042 /* /proc support */
7043 static struct proc_dir_entry *proc_dir;
7044
7045 /*
7046  * Module and infrastructure proble, init and exit handling
7047  */
7048
7049 static int force_load;
7050
7051 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7052 static const char * __init str_supported(int is_supported)
7053 {
7054         static char text_unsupported[] __initdata = "not supported";
7055
7056         return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7057 }
7058 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7059
7060 static void ibm_exit(struct ibm_struct *ibm)
7061 {
7062         dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7063
7064         list_del_init(&ibm->all_drivers);
7065
7066         if (ibm->flags.acpi_notify_installed) {
7067                 dbg_printk(TPACPI_DBG_EXIT,
7068                         "%s: acpi_remove_notify_handler\n", ibm->name);
7069                 BUG_ON(!ibm->acpi);
7070                 acpi_remove_notify_handler(*ibm->acpi->handle,
7071                                            ibm->acpi->type,
7072                                            dispatch_acpi_notify);
7073                 ibm->flags.acpi_notify_installed = 0;
7074                 ibm->flags.acpi_notify_installed = 0;
7075         }
7076
7077         if (ibm->flags.proc_created) {
7078                 dbg_printk(TPACPI_DBG_EXIT,
7079                         "%s: remove_proc_entry\n", ibm->name);
7080                 remove_proc_entry(ibm->name, proc_dir);
7081                 ibm->flags.proc_created = 0;
7082         }
7083
7084         if (ibm->flags.acpi_driver_registered) {
7085                 dbg_printk(TPACPI_DBG_EXIT,
7086                         "%s: acpi_bus_unregister_driver\n", ibm->name);
7087                 BUG_ON(!ibm->acpi);
7088                 acpi_bus_unregister_driver(ibm->acpi->driver);
7089                 kfree(ibm->acpi->driver);
7090                 ibm->acpi->driver = NULL;
7091                 ibm->flags.acpi_driver_registered = 0;
7092         }
7093
7094         if (ibm->flags.init_called && ibm->exit) {
7095                 ibm->exit();
7096                 ibm->flags.init_called = 0;
7097         }
7098
7099         dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7100 }
7101
7102 static int __init ibm_init(struct ibm_init_struct *iibm)
7103 {
7104         int ret;
7105         struct ibm_struct *ibm = iibm->data;
7106         struct proc_dir_entry *entry;
7107
7108         BUG_ON(ibm == NULL);
7109
7110         INIT_LIST_HEAD(&ibm->all_drivers);
7111
7112         if (ibm->flags.experimental && !experimental)
7113                 return 0;
7114
7115         dbg_printk(TPACPI_DBG_INIT,
7116                 "probing for %s\n", ibm->name);
7117
7118         if (iibm->init) {
7119                 ret = iibm->init(iibm);
7120                 if (ret > 0)
7121                         return 0;       /* probe failed */
7122                 if (ret)
7123                         return ret;
7124
7125                 ibm->flags.init_called = 1;
7126         }
7127
7128         if (ibm->acpi) {
7129                 if (ibm->acpi->hid) {
7130                         ret = register_tpacpi_subdriver(ibm);
7131                         if (ret)
7132                                 goto err_out;
7133                 }
7134
7135                 if (ibm->acpi->notify) {
7136                         ret = setup_acpi_notify(ibm);
7137                         if (ret == -ENODEV) {
7138                                 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7139                                         ibm->name);
7140                                 ret = 0;
7141                                 goto err_out;
7142                         }
7143                         if (ret < 0)
7144                                 goto err_out;
7145                 }
7146         }
7147
7148         dbg_printk(TPACPI_DBG_INIT,
7149                 "%s installed\n", ibm->name);
7150
7151         if (ibm->read) {
7152                 entry = create_proc_entry(ibm->name,
7153                                           S_IFREG | S_IRUGO | S_IWUSR,
7154                                           proc_dir);
7155                 if (!entry) {
7156                         printk(TPACPI_ERR "unable to create proc entry %s\n",
7157                                ibm->name);
7158                         ret = -ENODEV;
7159                         goto err_out;
7160                 }
7161                 entry->owner = THIS_MODULE;
7162                 entry->data = ibm;
7163                 entry->read_proc = &dispatch_procfs_read;
7164                 if (ibm->write)
7165                         entry->write_proc = &dispatch_procfs_write;
7166                 ibm->flags.proc_created = 1;
7167         }
7168
7169         list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7170
7171         return 0;
7172
7173 err_out:
7174         dbg_printk(TPACPI_DBG_INIT,
7175                 "%s: at error exit path with result %d\n",
7176                 ibm->name, ret);
7177
7178         ibm_exit(ibm);
7179         return (ret < 0)? ret : 0;
7180 }
7181
7182 /* Probing */
7183
7184 /* returns 0 - probe ok, or < 0 - probe error.
7185  * Probe ok doesn't mean thinkpad found.
7186  * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7187 static int __must_check __init get_thinkpad_model_data(
7188                                                 struct thinkpad_id_data *tp)
7189 {
7190         const struct dmi_device *dev = NULL;
7191         char ec_fw_string[18];
7192         char const *s;
7193
7194         if (!tp)
7195                 return -EINVAL;
7196
7197         memset(tp, 0, sizeof(*tp));
7198
7199         if (dmi_name_in_vendors("IBM"))
7200                 tp->vendor = PCI_VENDOR_ID_IBM;
7201         else if (dmi_name_in_vendors("LENOVO"))
7202                 tp->vendor = PCI_VENDOR_ID_LENOVO;
7203         else
7204                 return 0;
7205
7206         s = dmi_get_system_info(DMI_BIOS_VERSION);
7207         tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7208         if (s && !tp->bios_version_str)
7209                 return -ENOMEM;
7210         if (!tp->bios_version_str)
7211                 return 0;
7212         tp->bios_model = tp->bios_version_str[0]
7213                          | (tp->bios_version_str[1] << 8);
7214
7215         /*
7216          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7217          * X32 or newer, all Z series;  Some models must have an
7218          * up-to-date BIOS or they will not be detected.
7219          *
7220          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7221          */
7222         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7223                 if (sscanf(dev->name,
7224                            "IBM ThinkPad Embedded Controller -[%17c",
7225                            ec_fw_string) == 1) {
7226                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7227                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7228
7229                         tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7230                         if (!tp->ec_version_str)
7231                                 return -ENOMEM;
7232                         tp->ec_model = ec_fw_string[0]
7233                                         | (ec_fw_string[1] << 8);
7234                         break;
7235                 }
7236         }
7237
7238         s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7239         if (s && !strnicmp(s, "ThinkPad", 8)) {
7240                 tp->model_str = kstrdup(s, GFP_KERNEL);
7241                 if (!tp->model_str)
7242                         return -ENOMEM;
7243         }
7244
7245         s = dmi_get_system_info(DMI_PRODUCT_NAME);
7246         tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7247         if (s && !tp->nummodel_str)
7248                 return -ENOMEM;
7249
7250         return 0;
7251 }
7252
7253 static int __init probe_for_thinkpad(void)
7254 {
7255         int is_thinkpad;
7256
7257         if (acpi_disabled)
7258                 return -ENODEV;
7259
7260         /*
7261          * Non-ancient models have better DMI tagging, but very old models
7262          * don't.
7263          */
7264         is_thinkpad = (thinkpad_id.model_str != NULL);
7265
7266         /* ec is required because many other handles are relative to it */
7267         TPACPI_ACPIHANDLE_INIT(ec);
7268         if (!ec_handle) {
7269                 if (is_thinkpad)
7270                         printk(TPACPI_ERR
7271                                 "Not yet supported ThinkPad detected!\n");
7272                 return -ENODEV;
7273         }
7274
7275         /*
7276          * Risks a regression on very old machines, but reduces potential
7277          * false positives a damn great deal
7278          */
7279         if (!is_thinkpad)
7280                 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7281
7282         if (!is_thinkpad && !force_load)
7283                 return -ENODEV;
7284
7285         return 0;
7286 }
7287
7288
7289 /* Module init, exit, parameters */
7290
7291 static struct ibm_init_struct ibms_init[] __initdata = {
7292         {
7293                 .init = thinkpad_acpi_driver_init,
7294                 .data = &thinkpad_acpi_driver_data,
7295         },
7296         {
7297                 .init = hotkey_init,
7298                 .data = &hotkey_driver_data,
7299         },
7300         {
7301                 .init = bluetooth_init,
7302                 .data = &bluetooth_driver_data,
7303         },
7304         {
7305                 .init = wan_init,
7306                 .data = &wan_driver_data,
7307         },
7308         {
7309                 .init = uwb_init,
7310                 .data = &uwb_driver_data,
7311         },
7312 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7313         {
7314                 .init = video_init,
7315                 .data = &video_driver_data,
7316         },
7317 #endif
7318         {
7319                 .init = light_init,
7320                 .data = &light_driver_data,
7321         },
7322 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7323         {
7324                 .init = dock_init,
7325                 .data = &dock_driver_data[0],
7326         },
7327         {
7328                 .init = dock_init2,
7329                 .data = &dock_driver_data[1],
7330         },
7331 #endif
7332 #ifdef CONFIG_THINKPAD_ACPI_BAY
7333         {
7334                 .init = bay_init,
7335                 .data = &bay_driver_data,
7336         },
7337 #endif
7338         {
7339                 .init = cmos_init,
7340                 .data = &cmos_driver_data,
7341         },
7342         {
7343                 .init = led_init,
7344                 .data = &led_driver_data,
7345         },
7346         {
7347                 .init = beep_init,
7348                 .data = &beep_driver_data,
7349         },
7350         {
7351                 .init = thermal_init,
7352                 .data = &thermal_driver_data,
7353         },
7354         {
7355                 .data = &ecdump_driver_data,
7356         },
7357         {
7358                 .init = brightness_init,
7359                 .data = &brightness_driver_data,
7360         },
7361         {
7362                 .data = &volume_driver_data,
7363         },
7364         {
7365                 .init = fan_init,
7366                 .data = &fan_driver_data,
7367         },
7368 };
7369
7370 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7371 {
7372         unsigned int i;
7373         struct ibm_struct *ibm;
7374
7375         if (!kp || !kp->name || !val)
7376                 return -EINVAL;
7377
7378         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7379                 ibm = ibms_init[i].data;
7380                 WARN_ON(ibm == NULL);
7381
7382                 if (!ibm || !ibm->name)
7383                         continue;
7384
7385                 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7386                         if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7387                                 return -ENOSPC;
7388                         strcpy(ibms_init[i].param, val);
7389                         strcat(ibms_init[i].param, ",");
7390                         return 0;
7391                 }
7392         }
7393
7394         return -EINVAL;
7395 }
7396
7397 module_param(experimental, int, 0);
7398 MODULE_PARM_DESC(experimental,
7399                  "Enables experimental features when non-zero");
7400
7401 module_param_named(debug, dbg_level, uint, 0);
7402 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7403
7404 module_param(force_load, bool, 0);
7405 MODULE_PARM_DESC(force_load,
7406                  "Attempts to load the driver even on a "
7407                  "mis-identified ThinkPad when true");
7408
7409 module_param_named(fan_control, fan_control_allowed, bool, 0);
7410 MODULE_PARM_DESC(fan_control,
7411                  "Enables setting fan parameters features when true");
7412
7413 module_param_named(brightness_mode, brightness_mode, int, 0);
7414 MODULE_PARM_DESC(brightness_mode,
7415                  "Selects brightness control strategy: "
7416                  "0=auto, 1=EC, 2=CMOS, 3=both");
7417
7418 module_param(brightness_enable, uint, 0);
7419 MODULE_PARM_DESC(brightness_enable,
7420                  "Enables backlight control when 1, disables when 0");
7421
7422 module_param(hotkey_report_mode, uint, 0);
7423 MODULE_PARM_DESC(hotkey_report_mode,
7424                  "used for backwards compatibility with userspace, "
7425                  "see documentation");
7426
7427 #define TPACPI_PARAM(feature) \
7428         module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7429         MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7430                          "at module load, see documentation")
7431
7432 TPACPI_PARAM(hotkey);
7433 TPACPI_PARAM(bluetooth);
7434 TPACPI_PARAM(video);
7435 TPACPI_PARAM(light);
7436 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7437 TPACPI_PARAM(dock);
7438 #endif
7439 #ifdef CONFIG_THINKPAD_ACPI_BAY
7440 TPACPI_PARAM(bay);
7441 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7442 TPACPI_PARAM(cmos);
7443 TPACPI_PARAM(led);
7444 TPACPI_PARAM(beep);
7445 TPACPI_PARAM(ecdump);
7446 TPACPI_PARAM(brightness);
7447 TPACPI_PARAM(volume);
7448 TPACPI_PARAM(fan);
7449
7450 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7451 module_param(dbg_wlswemul, uint, 0);
7452 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7453 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7454 MODULE_PARM_DESC(wlsw_state,
7455                  "Initial state of the emulated WLSW switch");
7456
7457 module_param(dbg_bluetoothemul, uint, 0);
7458 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7459 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7460 MODULE_PARM_DESC(bluetooth_state,
7461                  "Initial state of the emulated bluetooth switch");
7462
7463 module_param(dbg_wwanemul, uint, 0);
7464 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7465 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7466 MODULE_PARM_DESC(wwan_state,
7467                  "Initial state of the emulated WWAN switch");
7468
7469 module_param(dbg_uwbemul, uint, 0);
7470 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7471 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7472 MODULE_PARM_DESC(uwb_state,
7473                  "Initial state of the emulated UWB switch");
7474 #endif
7475
7476 static void thinkpad_acpi_module_exit(void)
7477 {
7478         struct ibm_struct *ibm, *itmp;
7479
7480         tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7481
7482         list_for_each_entry_safe_reverse(ibm, itmp,
7483                                          &tpacpi_all_drivers,
7484                                          all_drivers) {
7485                 ibm_exit(ibm);
7486         }
7487
7488         dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7489
7490         if (tpacpi_inputdev) {
7491                 if (tp_features.input_device_registered)
7492                         input_unregister_device(tpacpi_inputdev);
7493                 else
7494                         input_free_device(tpacpi_inputdev);
7495         }
7496
7497         if (tpacpi_hwmon)
7498                 hwmon_device_unregister(tpacpi_hwmon);
7499
7500         if (tp_features.sensors_pdev_attrs_registered)
7501                 device_remove_file(&tpacpi_sensors_pdev->dev,
7502                                    &dev_attr_thinkpad_acpi_pdev_name);
7503         if (tpacpi_sensors_pdev)
7504                 platform_device_unregister(tpacpi_sensors_pdev);
7505         if (tpacpi_pdev)
7506                 platform_device_unregister(tpacpi_pdev);
7507
7508         if (tp_features.sensors_pdrv_attrs_registered)
7509                 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7510         if (tp_features.platform_drv_attrs_registered)
7511                 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7512
7513         if (tp_features.sensors_pdrv_registered)
7514                 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7515
7516         if (tp_features.platform_drv_registered)
7517                 platform_driver_unregister(&tpacpi_pdriver);
7518
7519         if (proc_dir)
7520                 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7521
7522         if (tpacpi_wq)
7523                 destroy_workqueue(tpacpi_wq);
7524
7525         kfree(thinkpad_id.bios_version_str);
7526         kfree(thinkpad_id.ec_version_str);
7527         kfree(thinkpad_id.model_str);
7528 }
7529
7530
7531 static int __init thinkpad_acpi_module_init(void)
7532 {
7533         int ret, i;
7534
7535         tpacpi_lifecycle = TPACPI_LIFE_INIT;
7536
7537         /* Parameter checking */
7538         if (hotkey_report_mode > 2)
7539                 return -EINVAL;
7540
7541         /* Driver-level probe */
7542
7543         ret = get_thinkpad_model_data(&thinkpad_id);
7544         if (ret) {
7545                 printk(TPACPI_ERR
7546                         "unable to get DMI data: %d\n", ret);
7547                 thinkpad_acpi_module_exit();
7548                 return ret;
7549         }
7550         ret = probe_for_thinkpad();
7551         if (ret) {
7552                 thinkpad_acpi_module_exit();
7553                 return ret;
7554         }
7555
7556         /* Driver initialization */
7557
7558         TPACPI_ACPIHANDLE_INIT(ecrd);
7559         TPACPI_ACPIHANDLE_INIT(ecwr);
7560
7561         tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7562         if (!tpacpi_wq) {
7563                 thinkpad_acpi_module_exit();
7564                 return -ENOMEM;
7565         }
7566
7567         proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7568         if (!proc_dir) {
7569                 printk(TPACPI_ERR
7570                        "unable to create proc dir " TPACPI_PROC_DIR);
7571                 thinkpad_acpi_module_exit();
7572                 return -ENODEV;
7573         }
7574         proc_dir->owner = THIS_MODULE;
7575
7576         ret = platform_driver_register(&tpacpi_pdriver);
7577         if (ret) {
7578                 printk(TPACPI_ERR
7579                        "unable to register main platform driver\n");
7580                 thinkpad_acpi_module_exit();
7581                 return ret;
7582         }
7583         tp_features.platform_drv_registered = 1;
7584
7585         ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7586         if (ret) {
7587                 printk(TPACPI_ERR
7588                        "unable to register hwmon platform driver\n");
7589                 thinkpad_acpi_module_exit();
7590                 return ret;
7591         }
7592         tp_features.sensors_pdrv_registered = 1;
7593
7594         ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7595         if (!ret) {
7596                 tp_features.platform_drv_attrs_registered = 1;
7597                 ret = tpacpi_create_driver_attributes(
7598                                         &tpacpi_hwmon_pdriver.driver);
7599         }
7600         if (ret) {
7601                 printk(TPACPI_ERR
7602                        "unable to create sysfs driver attributes\n");
7603                 thinkpad_acpi_module_exit();
7604                 return ret;
7605         }
7606         tp_features.sensors_pdrv_attrs_registered = 1;
7607
7608
7609         /* Device initialization */
7610         tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7611                                                         NULL, 0);
7612         if (IS_ERR(tpacpi_pdev)) {
7613                 ret = PTR_ERR(tpacpi_pdev);
7614                 tpacpi_pdev = NULL;
7615                 printk(TPACPI_ERR "unable to register platform device\n");
7616                 thinkpad_acpi_module_exit();
7617                 return ret;
7618         }
7619         tpacpi_sensors_pdev = platform_device_register_simple(
7620                                                 TPACPI_HWMON_DRVR_NAME,
7621                                                 -1, NULL, 0);
7622         if (IS_ERR(tpacpi_sensors_pdev)) {
7623                 ret = PTR_ERR(tpacpi_sensors_pdev);
7624                 tpacpi_sensors_pdev = NULL;
7625                 printk(TPACPI_ERR
7626                        "unable to register hwmon platform device\n");
7627                 thinkpad_acpi_module_exit();
7628                 return ret;
7629         }
7630         ret = device_create_file(&tpacpi_sensors_pdev->dev,
7631                                  &dev_attr_thinkpad_acpi_pdev_name);
7632         if (ret) {
7633                 printk(TPACPI_ERR
7634                        "unable to create sysfs hwmon device attributes\n");
7635                 thinkpad_acpi_module_exit();
7636                 return ret;
7637         }
7638         tp_features.sensors_pdev_attrs_registered = 1;
7639         tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7640         if (IS_ERR(tpacpi_hwmon)) {
7641                 ret = PTR_ERR(tpacpi_hwmon);
7642                 tpacpi_hwmon = NULL;
7643                 printk(TPACPI_ERR "unable to register hwmon device\n");
7644                 thinkpad_acpi_module_exit();
7645                 return ret;
7646         }
7647         mutex_init(&tpacpi_inputdev_send_mutex);
7648         tpacpi_inputdev = input_allocate_device();
7649         if (!tpacpi_inputdev) {
7650                 printk(TPACPI_ERR "unable to allocate input device\n");
7651                 thinkpad_acpi_module_exit();
7652                 return -ENOMEM;
7653         } else {
7654                 /* Prepare input device, but don't register */
7655                 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7656                 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7657                 tpacpi_inputdev->id.bustype = BUS_HOST;
7658                 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7659                                                 thinkpad_id.vendor :
7660                                                 PCI_VENDOR_ID_IBM;
7661                 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7662                 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7663         }
7664         for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7665                 ret = ibm_init(&ibms_init[i]);
7666                 if (ret >= 0 && *ibms_init[i].param)
7667                         ret = ibms_init[i].data->write(ibms_init[i].param);
7668                 if (ret < 0) {
7669                         thinkpad_acpi_module_exit();
7670                         return ret;
7671                 }
7672         }
7673         ret = input_register_device(tpacpi_inputdev);
7674         if (ret < 0) {
7675                 printk(TPACPI_ERR "unable to register input device\n");
7676                 thinkpad_acpi_module_exit();
7677                 return ret;
7678         } else {
7679                 tp_features.input_device_registered = 1;
7680         }
7681
7682         tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7683         return 0;
7684 }
7685
7686 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7687
7688 /*
7689  * DMI matching for module autoloading
7690  *
7691  * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7692  * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7693  *
7694  * Only models listed in thinkwiki will be supported, so add yours
7695  * if it is not there yet.
7696  */
7697 #define IBM_BIOS_MODULE_ALIAS(__type) \
7698         MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7699
7700 /* Non-ancient thinkpads */
7701 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7702 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7703
7704 /* Ancient thinkpad BIOSes have to be identified by
7705  * BIOS type or model number, and there are far less
7706  * BIOS types than model numbers... */
7707 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7708 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7709 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7710
7711 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7712 MODULE_DESCRIPTION(TPACPI_DESC);
7713 MODULE_VERSION(TPACPI_VERSION);
7714 MODULE_LICENSE("GPL");
7715
7716 module_init(thinkpad_acpi_module_init);
7717 module_exit(thinkpad_acpi_module_exit);