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