ACPI: thinkpad-acpi: second TP_EC_FAN_FULLSPEED should be TP_EC_FAN_AUTO
[sfrench/cifs-2.6.git] / drivers / misc / thinkpad_acpi.c
1 /*
2  *  thinkpad_acpi.c - ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6  *  Copyright (C) 2006-2008 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.19"
25 #define TPACPI_SYSFS_VERSION 0x020200
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/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
60
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
71
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
75
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
78
79 #include <linux/pci_ids.h>
80
81
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN     0
84 #define TP_CMOS_VOLUME_UP       1
85 #define TP_CMOS_VOLUME_MUTE     2
86 #define TP_CMOS_BRIGHTNESS_UP   4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
88
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91         TP_NVRAM_ADDR_HK2               = 0x57,
92         TP_NVRAM_ADDR_THINKLIGHT        = 0x58,
93         TP_NVRAM_ADDR_VIDEO             = 0x59,
94         TP_NVRAM_ADDR_BRIGHTNESS        = 0x5e,
95         TP_NVRAM_ADDR_MIXER             = 0x60,
96 };
97
98 /* NVRAM bit masks */
99 enum {
100         TP_NVRAM_MASK_HKT_THINKPAD      = 0x08,
101         TP_NVRAM_MASK_HKT_ZOOM          = 0x20,
102         TP_NVRAM_MASK_HKT_DISPLAY       = 0x40,
103         TP_NVRAM_MASK_HKT_HIBERNATE     = 0x80,
104         TP_NVRAM_MASK_THINKLIGHT        = 0x10,
105         TP_NVRAM_MASK_HKT_DISPEXPND     = 0x30,
106         TP_NVRAM_MASK_HKT_BRIGHTNESS    = 0x20,
107         TP_NVRAM_MASK_LEVEL_BRIGHTNESS  = 0x0f,
108         TP_NVRAM_POS_LEVEL_BRIGHTNESS   = 0,
109         TP_NVRAM_MASK_MUTE              = 0x40,
110         TP_NVRAM_MASK_HKT_VOLUME        = 0x80,
111         TP_NVRAM_MASK_LEVEL_VOLUME      = 0x0f,
112         TP_NVRAM_POS_LEVEL_VOLUME       = 0,
113 };
114
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID            "IBM0068"
117
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT       0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION       0x4101
121
122
123 /****************************************************************************
124  * Main driver
125  */
126
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
132
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
137
138 #define TPACPI_MAX_ACPI_ARGS 3
139
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR         KERN_ERR    TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO   KERN_INFO   TPACPI_LOG
145 #define TPACPI_DEBUG  KERN_DEBUG  TPACPI_LOG
146
147 #define TPACPI_DBG_ALL          0xffff
148 #define TPACPI_DBG_ALL          0xffff
149 #define TPACPI_DBG_INIT         0x0001
150 #define TPACPI_DBG_EXIT         0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152         do { if (dbg_level & a_dbg_level) \
153                 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154         } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157         dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
162
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
166
167
168 /****************************************************************************
169  * Driver-wide structs and misc. variables
170  */
171
172 struct ibm_struct;
173
174 struct tp_acpi_drv_struct {
175         const struct acpi_device_id *hid;
176         struct acpi_driver *driver;
177
178         void (*notify) (struct ibm_struct *, u32);
179         acpi_handle *handle;
180         u32 type;
181         struct acpi_device *device;
182 };
183
184 struct ibm_struct {
185         char *name;
186
187         int (*read) (char *);
188         int (*write) (char *);
189         void (*exit) (void);
190         void (*resume) (void);
191         void (*suspend) (pm_message_t state);
192
193         struct list_head all_drivers;
194
195         struct tp_acpi_drv_struct *acpi;
196
197         struct {
198                 u8 acpi_driver_registered:1;
199                 u8 acpi_notify_installed:1;
200                 u8 proc_created:1;
201                 u8 init_called:1;
202                 u8 experimental:1;
203         } flags;
204 };
205
206 struct ibm_init_struct {
207         char param[32];
208
209         int (*init) (struct ibm_init_struct *);
210         struct ibm_struct *data;
211 };
212
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215         u32 bay_status:1;
216         u32 bay_eject:1;
217         u32 bay_status2:1;
218         u32 bay_eject2:1;
219 #endif
220         u32 bluetooth:1;
221         u32 hotkey:1;
222         u32 hotkey_mask:1;
223         u32 hotkey_wlsw:1;
224         u32 light:1;
225         u32 light_status:1;
226         u32 bright_16levels:1;
227         u32 wan:1;
228         u32 fan_ctrl_status_undef:1;
229         u32 input_device_registered:1;
230         u32 platform_drv_registered:1;
231         u32 platform_drv_attrs_registered:1;
232         u32 sensors_pdrv_registered:1;
233         u32 sensors_pdrv_attrs_registered:1;
234         u32 sensors_pdev_attrs_registered:1;
235         u32 hotkey_poll_active:1;
236 } tp_features;
237
238 struct thinkpad_id_data {
239         unsigned int vendor;    /* ThinkPad vendor:
240                                  * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
241
242         char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243         char *ec_version_str;   /* Something like 1ZHT51WW-1.04a */
244
245         u16 bios_model;         /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
246         u16 ec_model;
247
248         char *model_str;
249 };
250 static struct thinkpad_id_data thinkpad_id;
251
252 static enum {
253         TPACPI_LIFE_INIT = 0,
254         TPACPI_LIFE_RUNNING,
255         TPACPI_LIFE_EXITING,
256 } tpacpi_lifecycle;
257
258 static int experimental;
259 static u32 dbg_level;
260
261 /****************************************************************************
262  ****************************************************************************
263  *
264  * ACPI Helpers and device model
265  *
266  ****************************************************************************
267  ****************************************************************************/
268
269 /*************************************************************************
270  * ACPI basic handles
271  */
272
273 static acpi_handle root_handle;
274
275 #define TPACPI_HANDLE(object, parent, paths...)                 \
276         static acpi_handle  object##_handle;                    \
277         static acpi_handle *object##_parent = &parent##_handle; \
278         static char        *object##_path;                      \
279         static char        *object##_paths[] = { paths }
280
281 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",   /* 240, 240x */
282            "\\_SB.PCI.ISA.EC",  /* 570 */
283            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
284            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
286            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
287            "\\_SB.PCI0.LPC.EC", /* all others */
288            );
289
290 TPACPI_HANDLE(ecrd, ec, "ECRD");        /* 570 */
291 TPACPI_HANDLE(ecwr, ec, "ECWR");        /* 570 */
292
293 TPACPI_HANDLE(cmos, root, "\\UCMS",     /* R50, R50e, R50p, R51, */
294                                         /* T4x, X31, X40 */
295            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296            "\\CMS",             /* R40, R40e */
297            );                   /* all others */
298
299 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY",   /* 600e/x, 770e, 770x */
300            "^HKEY",             /* R30, R31 */
301            "HKEY",              /* all others */
302            );                   /* 570 */
303
304
305 /*************************************************************************
306  * ACPI helpers
307  */
308
309 static int acpi_evalf(acpi_handle handle,
310                       void *res, char *method, char *fmt, ...)
311 {
312         char *fmt0 = fmt;
313         struct acpi_object_list params;
314         union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
315         struct acpi_buffer result, *resultp;
316         union acpi_object out_obj;
317         acpi_status status;
318         va_list ap;
319         char res_type;
320         int success;
321         int quiet;
322
323         if (!*fmt) {
324                 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
325                 return 0;
326         }
327
328         if (*fmt == 'q') {
329                 quiet = 1;
330                 fmt++;
331         } else
332                 quiet = 0;
333
334         res_type = *(fmt++);
335
336         params.count = 0;
337         params.pointer = &in_objs[0];
338
339         va_start(ap, fmt);
340         while (*fmt) {
341                 char c = *(fmt++);
342                 switch (c) {
343                 case 'd':       /* int */
344                         in_objs[params.count].integer.value = va_arg(ap, int);
345                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346                         break;
347                         /* add more types as needed */
348                 default:
349                         printk(TPACPI_ERR "acpi_evalf() called "
350                                "with invalid format character '%c'\n", c);
351                         return 0;
352                 }
353         }
354         va_end(ap);
355
356         if (res_type != 'v') {
357                 result.length = sizeof(out_obj);
358                 result.pointer = &out_obj;
359                 resultp = &result;
360         } else
361                 resultp = NULL;
362
363         status = acpi_evaluate_object(handle, method, &params, resultp);
364
365         switch (res_type) {
366         case 'd':               /* int */
367                 if (res)
368                         *(int *)res = out_obj.integer.value;
369                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370                 break;
371         case 'v':               /* void */
372                 success = status == AE_OK;
373                 break;
374                 /* add more types as needed */
375         default:
376                 printk(TPACPI_ERR "acpi_evalf() called "
377                        "with invalid format character '%c'\n", res_type);
378                 return 0;
379         }
380
381         if (!success && !quiet)
382                 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
383                        method, fmt0, status);
384
385         return success;
386 }
387
388 static int acpi_ec_read(int i, u8 *p)
389 {
390         int v;
391
392         if (ecrd_handle) {
393                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394                         return 0;
395                 *p = v;
396         } else {
397                 if (ec_read(i, p) < 0)
398                         return 0;
399         }
400
401         return 1;
402 }
403
404 static int acpi_ec_write(int i, u8 v)
405 {
406         if (ecwr_handle) {
407                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408                         return 0;
409         } else {
410                 if (ec_write(i, v) < 0)
411                         return 0;
412         }
413
414         return 1;
415 }
416
417 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
418 static int _sta(acpi_handle handle)
419 {
420         int status;
421
422         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
423                 status = 0;
424
425         return status;
426 }
427 #endif
428
429 static int issue_thinkpad_cmos_command(int cmos_cmd)
430 {
431         if (!cmos_handle)
432                 return -ENXIO;
433
434         if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
435                 return -EIO;
436
437         return 0;
438 }
439
440 /*************************************************************************
441  * ACPI device model
442  */
443
444 #define TPACPI_ACPIHANDLE_INIT(object) \
445         drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
446                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
447
448 static void drv_acpi_handle_init(char *name,
449                            acpi_handle *handle, acpi_handle parent,
450                            char **paths, int num_paths, char **path)
451 {
452         int i;
453         acpi_status status;
454
455         vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
456                 name);
457
458         for (i = 0; i < num_paths; i++) {
459                 status = acpi_get_handle(parent, paths[i], handle);
460                 if (ACPI_SUCCESS(status)) {
461                         *path = paths[i];
462                         dbg_printk(TPACPI_DBG_INIT,
463                                    "Found ACPI handle %s for %s\n",
464                                    *path, name);
465                         return;
466                 }
467         }
468
469         vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
470                     name);
471         *handle = NULL;
472 }
473
474 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
475 {
476         struct ibm_struct *ibm = data;
477
478         if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
479                 return;
480
481         if (!ibm || !ibm->acpi || !ibm->acpi->notify)
482                 return;
483
484         ibm->acpi->notify(ibm, event);
485 }
486
487 static int __init setup_acpi_notify(struct ibm_struct *ibm)
488 {
489         acpi_status status;
490         int rc;
491
492         BUG_ON(!ibm->acpi);
493
494         if (!*ibm->acpi->handle)
495                 return 0;
496
497         vdbg_printk(TPACPI_DBG_INIT,
498                 "setting up ACPI notify for %s\n", ibm->name);
499
500         rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
501         if (rc < 0) {
502                 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
503                         ibm->name, rc);
504                 return -ENODEV;
505         }
506
507         acpi_driver_data(ibm->acpi->device) = ibm;
508         sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
509                 TPACPI_ACPI_EVENT_PREFIX,
510                 ibm->name);
511
512         status = acpi_install_notify_handler(*ibm->acpi->handle,
513                         ibm->acpi->type, dispatch_acpi_notify, ibm);
514         if (ACPI_FAILURE(status)) {
515                 if (status == AE_ALREADY_EXISTS) {
516                         printk(TPACPI_NOTICE
517                                "another device driver is already "
518                                "handling %s events\n", ibm->name);
519                 } else {
520                         printk(TPACPI_ERR
521                                "acpi_install_notify_handler(%s) failed: %d\n",
522                                ibm->name, status);
523                 }
524                 return -ENODEV;
525         }
526         ibm->flags.acpi_notify_installed = 1;
527         return 0;
528 }
529
530 static int __init tpacpi_device_add(struct acpi_device *device)
531 {
532         return 0;
533 }
534
535 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
536 {
537         int rc;
538
539         dbg_printk(TPACPI_DBG_INIT,
540                 "registering %s as an ACPI driver\n", ibm->name);
541
542         BUG_ON(!ibm->acpi);
543
544         ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
545         if (!ibm->acpi->driver) {
546                 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
547                 return -ENOMEM;
548         }
549
550         sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
551         ibm->acpi->driver->ids = ibm->acpi->hid;
552
553         ibm->acpi->driver->ops.add = &tpacpi_device_add;
554
555         rc = acpi_bus_register_driver(ibm->acpi->driver);
556         if (rc < 0) {
557                 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
558                        ibm->name, rc);
559                 kfree(ibm->acpi->driver);
560                 ibm->acpi->driver = NULL;
561         } else if (!rc)
562                 ibm->flags.acpi_driver_registered = 1;
563
564         return rc;
565 }
566
567
568 /****************************************************************************
569  ****************************************************************************
570  *
571  * Procfs Helpers
572  *
573  ****************************************************************************
574  ****************************************************************************/
575
576 static int dispatch_procfs_read(char *page, char **start, off_t off,
577                         int count, int *eof, void *data)
578 {
579         struct ibm_struct *ibm = data;
580         int len;
581
582         if (!ibm || !ibm->read)
583                 return -EINVAL;
584
585         len = ibm->read(page);
586         if (len < 0)
587                 return len;
588
589         if (len <= off + count)
590                 *eof = 1;
591         *start = page + off;
592         len -= off;
593         if (len > count)
594                 len = count;
595         if (len < 0)
596                 len = 0;
597
598         return len;
599 }
600
601 static int dispatch_procfs_write(struct file *file,
602                         const char __user *userbuf,
603                         unsigned long count, void *data)
604 {
605         struct ibm_struct *ibm = data;
606         char *kernbuf;
607         int ret;
608
609         if (!ibm || !ibm->write)
610                 return -EINVAL;
611
612         kernbuf = kmalloc(count + 2, GFP_KERNEL);
613         if (!kernbuf)
614                 return -ENOMEM;
615
616         if (copy_from_user(kernbuf, userbuf, count)) {
617                 kfree(kernbuf);
618                 return -EFAULT;
619         }
620
621         kernbuf[count] = 0;
622         strcat(kernbuf, ",");
623         ret = ibm->write(kernbuf);
624         if (ret == 0)
625                 ret = count;
626
627         kfree(kernbuf);
628
629         return ret;
630 }
631
632 static char *next_cmd(char **cmds)
633 {
634         char *start = *cmds;
635         char *end;
636
637         while ((end = strchr(start, ',')) && end == start)
638                 start = end + 1;
639
640         if (!end)
641                 return NULL;
642
643         *end = 0;
644         *cmds = end + 1;
645         return start;
646 }
647
648
649 /****************************************************************************
650  ****************************************************************************
651  *
652  * Device model: input, hwmon and platform
653  *
654  ****************************************************************************
655  ****************************************************************************/
656
657 static struct platform_device *tpacpi_pdev;
658 static struct platform_device *tpacpi_sensors_pdev;
659 static struct device *tpacpi_hwmon;
660 static struct input_dev *tpacpi_inputdev;
661 static struct mutex tpacpi_inputdev_send_mutex;
662 static LIST_HEAD(tpacpi_all_drivers);
663
664 static int tpacpi_suspend_handler(struct platform_device *pdev,
665                                   pm_message_t state)
666 {
667         struct ibm_struct *ibm, *itmp;
668
669         list_for_each_entry_safe(ibm, itmp,
670                                  &tpacpi_all_drivers,
671                                  all_drivers) {
672                 if (ibm->suspend)
673                         (ibm->suspend)(state);
674         }
675
676         return 0;
677 }
678
679 static int tpacpi_resume_handler(struct platform_device *pdev)
680 {
681         struct ibm_struct *ibm, *itmp;
682
683         list_for_each_entry_safe(ibm, itmp,
684                                  &tpacpi_all_drivers,
685                                  all_drivers) {
686                 if (ibm->resume)
687                         (ibm->resume)();
688         }
689
690         return 0;
691 }
692
693 static struct platform_driver tpacpi_pdriver = {
694         .driver = {
695                 .name = TPACPI_DRVR_NAME,
696                 .owner = THIS_MODULE,
697         },
698         .suspend = tpacpi_suspend_handler,
699         .resume = tpacpi_resume_handler,
700 };
701
702 static struct platform_driver tpacpi_hwmon_pdriver = {
703         .driver = {
704                 .name = TPACPI_HWMON_DRVR_NAME,
705                 .owner = THIS_MODULE,
706         },
707 };
708
709 /*************************************************************************
710  * sysfs support helpers
711  */
712
713 struct attribute_set {
714         unsigned int members, max_members;
715         struct attribute_group group;
716 };
717
718 struct attribute_set_obj {
719         struct attribute_set s;
720         struct attribute *a;
721 } __attribute__((packed));
722
723 static struct attribute_set *create_attr_set(unsigned int max_members,
724                                                 const char *name)
725 {
726         struct attribute_set_obj *sobj;
727
728         if (max_members == 0)
729                 return NULL;
730
731         /* Allocates space for implicit NULL at the end too */
732         sobj = kzalloc(sizeof(struct attribute_set_obj) +
733                     max_members * sizeof(struct attribute *),
734                     GFP_KERNEL);
735         if (!sobj)
736                 return NULL;
737         sobj->s.max_members = max_members;
738         sobj->s.group.attrs = &sobj->a;
739         sobj->s.group.name = name;
740
741         return &sobj->s;
742 }
743
744 #define destroy_attr_set(_set) \
745         kfree(_set);
746
747 /* not multi-threaded safe, use it in a single thread per set */
748 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
749 {
750         if (!s || !attr)
751                 return -EINVAL;
752
753         if (s->members >= s->max_members)
754                 return -ENOMEM;
755
756         s->group.attrs[s->members] = attr;
757         s->members++;
758
759         return 0;
760 }
761
762 static int add_many_to_attr_set(struct attribute_set *s,
763                         struct attribute **attr,
764                         unsigned int count)
765 {
766         int i, res;
767
768         for (i = 0; i < count; i++) {
769                 res = add_to_attr_set(s, attr[i]);
770                 if (res)
771                         return res;
772         }
773
774         return 0;
775 }
776
777 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
778 {
779         sysfs_remove_group(kobj, &s->group);
780         destroy_attr_set(s);
781 }
782
783 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
784         sysfs_create_group(_kobj, &_attr_set->group)
785
786 static int parse_strtoul(const char *buf,
787                 unsigned long max, unsigned long *value)
788 {
789         char *endp;
790
791         while (*buf && isspace(*buf))
792                 buf++;
793         *value = simple_strtoul(buf, &endp, 0);
794         while (*endp && isspace(*endp))
795                 endp++;
796         if (*endp || *value > max)
797                 return -EINVAL;
798
799         return 0;
800 }
801
802 /*************************************************************************
803  * thinkpad-acpi driver attributes
804  */
805
806 /* interface_version --------------------------------------------------- */
807 static ssize_t tpacpi_driver_interface_version_show(
808                                 struct device_driver *drv,
809                                 char *buf)
810 {
811         return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
812 }
813
814 static DRIVER_ATTR(interface_version, S_IRUGO,
815                 tpacpi_driver_interface_version_show, NULL);
816
817 /* debug_level --------------------------------------------------------- */
818 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
819                                                 char *buf)
820 {
821         return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
822 }
823
824 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
825                                                 const char *buf, size_t count)
826 {
827         unsigned long t;
828
829         if (parse_strtoul(buf, 0xffff, &t))
830                 return -EINVAL;
831
832         dbg_level = t;
833
834         return count;
835 }
836
837 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
838                 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
839
840 /* version ------------------------------------------------------------- */
841 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
842                                                 char *buf)
843 {
844         return snprintf(buf, PAGE_SIZE, "%s v%s\n",
845                         TPACPI_DESC, TPACPI_VERSION);
846 }
847
848 static DRIVER_ATTR(version, S_IRUGO,
849                 tpacpi_driver_version_show, NULL);
850
851 /* --------------------------------------------------------------------- */
852
853 static struct driver_attribute *tpacpi_driver_attributes[] = {
854         &driver_attr_debug_level, &driver_attr_version,
855         &driver_attr_interface_version,
856 };
857
858 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
859 {
860         int i, res;
861
862         i = 0;
863         res = 0;
864         while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
865                 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
866                 i++;
867         }
868
869         return res;
870 }
871
872 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
873 {
874         int i;
875
876         for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
877                 driver_remove_file(drv, tpacpi_driver_attributes[i]);
878 }
879
880 /****************************************************************************
881  ****************************************************************************
882  *
883  * Subdrivers
884  *
885  ****************************************************************************
886  ****************************************************************************/
887
888 /*************************************************************************
889  * thinkpad-acpi init subdriver
890  */
891
892 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
893 {
894         printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
895         printk(TPACPI_INFO "%s\n", TPACPI_URL);
896
897         printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
898                 (thinkpad_id.bios_version_str) ?
899                         thinkpad_id.bios_version_str : "unknown",
900                 (thinkpad_id.ec_version_str) ?
901                         thinkpad_id.ec_version_str : "unknown");
902
903         if (thinkpad_id.vendor && thinkpad_id.model_str)
904                 printk(TPACPI_INFO "%s %s\n",
905                         (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
906                                 "IBM" : ((thinkpad_id.vendor ==
907                                                 PCI_VENDOR_ID_LENOVO) ?
908                                         "Lenovo" : "Unknown vendor"),
909                         thinkpad_id.model_str);
910
911         return 0;
912 }
913
914 static int thinkpad_acpi_driver_read(char *p)
915 {
916         int len = 0;
917
918         len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
919         len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
920
921         return len;
922 }
923
924 static struct ibm_struct thinkpad_acpi_driver_data = {
925         .name = "driver",
926         .read = thinkpad_acpi_driver_read,
927 };
928
929 /*************************************************************************
930  * Hotkey subdriver
931  */
932
933 enum {  /* hot key scan codes (derived from ACPI DSDT) */
934         TP_ACPI_HOTKEYSCAN_FNF1         = 0,
935         TP_ACPI_HOTKEYSCAN_FNF2,
936         TP_ACPI_HOTKEYSCAN_FNF3,
937         TP_ACPI_HOTKEYSCAN_FNF4,
938         TP_ACPI_HOTKEYSCAN_FNF5,
939         TP_ACPI_HOTKEYSCAN_FNF6,
940         TP_ACPI_HOTKEYSCAN_FNF7,
941         TP_ACPI_HOTKEYSCAN_FNF8,
942         TP_ACPI_HOTKEYSCAN_FNF9,
943         TP_ACPI_HOTKEYSCAN_FNF10,
944         TP_ACPI_HOTKEYSCAN_FNF11,
945         TP_ACPI_HOTKEYSCAN_FNF12,
946         TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
947         TP_ACPI_HOTKEYSCAN_FNINSERT,
948         TP_ACPI_HOTKEYSCAN_FNDELETE,
949         TP_ACPI_HOTKEYSCAN_FNHOME,
950         TP_ACPI_HOTKEYSCAN_FNEND,
951         TP_ACPI_HOTKEYSCAN_FNPAGEUP,
952         TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
953         TP_ACPI_HOTKEYSCAN_FNSPACE,
954         TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955         TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956         TP_ACPI_HOTKEYSCAN_MUTE,
957         TP_ACPI_HOTKEYSCAN_THINKPAD,
958 };
959
960 enum {  /* Keys available through NVRAM polling */
961         TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
962         TPACPI_HKEY_NVRAM_GOOD_MASK  = 0x00fb8000U,
963 };
964
965 enum {  /* Positions of some of the keys in hotkey masks */
966         TP_ACPI_HKEY_DISPSWTCH_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
967         TP_ACPI_HKEY_DISPXPAND_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
968         TP_ACPI_HKEY_HIBERNATE_MASK     = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
969         TP_ACPI_HKEY_BRGHTUP_MASK       = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
970         TP_ACPI_HKEY_BRGHTDWN_MASK      = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
971         TP_ACPI_HKEY_THNKLGHT_MASK      = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
972         TP_ACPI_HKEY_ZOOM_MASK          = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
973         TP_ACPI_HKEY_VOLUP_MASK         = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
974         TP_ACPI_HKEY_VOLDWN_MASK        = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
975         TP_ACPI_HKEY_MUTE_MASK          = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
976         TP_ACPI_HKEY_THINKPAD_MASK      = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
977 };
978
979 enum {  /* NVRAM to ACPI HKEY group map */
980         TP_NVRAM_HKEY_GROUP_HK2         = TP_ACPI_HKEY_THINKPAD_MASK |
981                                           TP_ACPI_HKEY_ZOOM_MASK |
982                                           TP_ACPI_HKEY_DISPSWTCH_MASK |
983                                           TP_ACPI_HKEY_HIBERNATE_MASK,
984         TP_NVRAM_HKEY_GROUP_BRIGHTNESS  = TP_ACPI_HKEY_BRGHTUP_MASK |
985                                           TP_ACPI_HKEY_BRGHTDWN_MASK,
986         TP_NVRAM_HKEY_GROUP_VOLUME      = TP_ACPI_HKEY_VOLUP_MASK |
987                                           TP_ACPI_HKEY_VOLDWN_MASK |
988                                           TP_ACPI_HKEY_MUTE_MASK,
989 };
990
991 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
992 struct tp_nvram_state {
993        u16 thinkpad_toggle:1;
994        u16 zoom_toggle:1;
995        u16 display_toggle:1;
996        u16 thinklight_toggle:1;
997        u16 hibernate_toggle:1;
998        u16 displayexp_toggle:1;
999        u16 display_state:1;
1000        u16 brightness_toggle:1;
1001        u16 volume_toggle:1;
1002        u16 mute:1;
1003
1004        u8 brightness_level;
1005        u8 volume_level;
1006 };
1007
1008 static struct task_struct *tpacpi_hotkey_task;
1009 static u32 hotkey_source_mask;          /* bit mask 0=ACPI,1=NVRAM */
1010 static int hotkey_poll_freq = 10;       /* Hz */
1011 static struct mutex hotkey_thread_mutex;
1012 static struct mutex hotkey_thread_data_mutex;
1013 static unsigned int hotkey_config_change;
1014
1015 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1016
1017 #define hotkey_source_mask 0U
1018
1019 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1020
1021 static struct mutex hotkey_mutex;
1022
1023 static enum {   /* Reasons for waking up */
1024         TP_ACPI_WAKEUP_NONE = 0,        /* None or unknown */
1025         TP_ACPI_WAKEUP_BAYEJ,           /* Bay ejection request */
1026         TP_ACPI_WAKEUP_UNDOCK,          /* Undock request */
1027 } hotkey_wakeup_reason;
1028
1029 static int hotkey_autosleep_ack;
1030
1031 static int hotkey_orig_status;
1032 static u32 hotkey_orig_mask;
1033 static u32 hotkey_all_mask;
1034 static u32 hotkey_reserved_mask;
1035 static u32 hotkey_mask;
1036
1037 static unsigned int hotkey_report_mode;
1038
1039 static u16 *hotkey_keycode_map;
1040
1041 static struct attribute_set *hotkey_dev_attributes;
1042
1043 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1044 #define HOTKEY_CONFIG_CRITICAL_START \
1045         do { \
1046                 mutex_lock(&hotkey_thread_data_mutex); \
1047                 hotkey_config_change++; \
1048         } while (0);
1049 #define HOTKEY_CONFIG_CRITICAL_END \
1050         mutex_unlock(&hotkey_thread_data_mutex);
1051 #else
1052 #define HOTKEY_CONFIG_CRITICAL_START
1053 #define HOTKEY_CONFIG_CRITICAL_END
1054 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1055
1056 static int hotkey_get_wlsw(int *status)
1057 {
1058         if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1059                 return -EIO;
1060         return 0;
1061 }
1062
1063 /*
1064  * Call with hotkey_mutex held
1065  */
1066 static int hotkey_mask_get(void)
1067 {
1068         u32 m = 0;
1069
1070         if (tp_features.hotkey_mask) {
1071                 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1072                         return -EIO;
1073         }
1074         hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1075
1076         return 0;
1077 }
1078
1079 /*
1080  * Call with hotkey_mutex held
1081  */
1082 static int hotkey_mask_set(u32 mask)
1083 {
1084         int i;
1085         int rc = 0;
1086
1087         if (tp_features.hotkey_mask) {
1088                 HOTKEY_CONFIG_CRITICAL_START
1089                 for (i = 0; i < 32; i++) {
1090                         u32 m = 1 << i;
1091                         /* enable in firmware mask only keys not in NVRAM
1092                          * mode, but enable the key in the cached hotkey_mask
1093                          * regardless of mode, or the key will end up
1094                          * disabled by hotkey_mask_get() */
1095                         if (!acpi_evalf(hkey_handle,
1096                                         NULL, "MHKM", "vdd", i + 1,
1097                                         !!((mask & ~hotkey_source_mask) & m))) {
1098                                 rc = -EIO;
1099                                 break;
1100                         } else {
1101                                 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1102                         }
1103                 }
1104                 HOTKEY_CONFIG_CRITICAL_END
1105
1106                 /* hotkey_mask_get must be called unconditionally below */
1107                 if (!hotkey_mask_get() && !rc &&
1108                     (hotkey_mask & ~hotkey_source_mask) !=
1109                      (mask & ~hotkey_source_mask)) {
1110                         printk(TPACPI_NOTICE
1111                                "requested hot key mask 0x%08x, but "
1112                                "firmware forced it to 0x%08x\n",
1113                                mask, hotkey_mask);
1114                 }
1115         } else {
1116 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1117                 HOTKEY_CONFIG_CRITICAL_START
1118                 hotkey_mask = mask & hotkey_source_mask;
1119                 HOTKEY_CONFIG_CRITICAL_END
1120                 hotkey_mask_get();
1121                 if (hotkey_mask != mask) {
1122                         printk(TPACPI_NOTICE
1123                                "requested hot key mask 0x%08x, "
1124                                "forced to 0x%08x (NVRAM poll mask is "
1125                                "0x%08x): no firmware mask support\n",
1126                                mask, hotkey_mask, hotkey_source_mask);
1127                 }
1128 #else
1129                 hotkey_mask_get();
1130                 rc = -ENXIO;
1131 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1132         }
1133
1134         return rc;
1135 }
1136
1137 static int hotkey_status_get(int *status)
1138 {
1139         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1140                 return -EIO;
1141
1142         return 0;
1143 }
1144
1145 static int hotkey_status_set(int status)
1146 {
1147         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1148                 return -EIO;
1149
1150         return 0;
1151 }
1152
1153 static void tpacpi_input_send_radiosw(void)
1154 {
1155         int wlsw;
1156
1157         mutex_lock(&tpacpi_inputdev_send_mutex);
1158
1159         if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1160                 input_report_switch(tpacpi_inputdev,
1161                                     SW_RADIO, !!wlsw);
1162                 input_sync(tpacpi_inputdev);
1163         }
1164
1165         mutex_unlock(&tpacpi_inputdev_send_mutex);
1166 }
1167
1168 static void tpacpi_input_send_key(unsigned int scancode)
1169 {
1170         unsigned int keycode;
1171
1172         keycode = hotkey_keycode_map[scancode];
1173
1174         if (keycode != KEY_RESERVED) {
1175                 mutex_lock(&tpacpi_inputdev_send_mutex);
1176
1177                 input_report_key(tpacpi_inputdev, keycode, 1);
1178                 if (keycode == KEY_UNKNOWN)
1179                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1180                                     scancode);
1181                 input_sync(tpacpi_inputdev);
1182
1183                 input_report_key(tpacpi_inputdev, keycode, 0);
1184                 if (keycode == KEY_UNKNOWN)
1185                         input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1186                                     scancode);
1187                 input_sync(tpacpi_inputdev);
1188
1189                 mutex_unlock(&tpacpi_inputdev_send_mutex);
1190         }
1191 }
1192
1193 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1194 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1195
1196 static void tpacpi_hotkey_send_key(unsigned int scancode)
1197 {
1198         tpacpi_input_send_key(scancode);
1199         if (hotkey_report_mode < 2) {
1200                 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1201                                                 0x80, 0x1001 + scancode);
1202         }
1203 }
1204
1205 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1206 {
1207         u8 d;
1208
1209         if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1210                 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1211                 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1212                 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1213                 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1214                 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1215         }
1216         if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1217                 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1218                 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1219         }
1220         if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1221                 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1222                 n->displayexp_toggle =
1223                                 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1224         }
1225         if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1226                 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1227                 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1228                                 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1229                 n->brightness_toggle =
1230                                 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1231         }
1232         if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1233                 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1234                 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1235                                 >> TP_NVRAM_POS_LEVEL_VOLUME;
1236                 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1237                 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1238         }
1239 }
1240
1241 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1242         do { \
1243                 if ((mask & (1 << __scancode)) && \
1244                     oldn->__member != newn->__member) \
1245                 tpacpi_hotkey_send_key(__scancode); \
1246         } while (0)
1247
1248 #define TPACPI_MAY_SEND_KEY(__scancode) \
1249         do { if (mask & (1 << __scancode)) \
1250                 tpacpi_hotkey_send_key(__scancode); } while (0)
1251
1252 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1253                                            struct tp_nvram_state *newn,
1254                                            u32 mask)
1255 {
1256         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1257         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1258         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1259         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1260
1261         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1262
1263         TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1264
1265         /* handle volume */
1266         if (oldn->volume_toggle != newn->volume_toggle) {
1267                 if (oldn->mute != newn->mute) {
1268                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1269                 }
1270                 if (oldn->volume_level > newn->volume_level) {
1271                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272                 } else if (oldn->volume_level < newn->volume_level) {
1273                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1274                 } else if (oldn->mute == newn->mute) {
1275                         /* repeated key presses that didn't change state */
1276                         if (newn->mute) {
1277                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1278                         } else if (newn->volume_level != 0) {
1279                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1280                         } else {
1281                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1282                         }
1283                 }
1284         }
1285
1286         /* handle brightness */
1287         if (oldn->brightness_toggle != newn->brightness_toggle) {
1288                 if (oldn->brightness_level < newn->brightness_level) {
1289                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1290                 } else if (oldn->brightness_level > newn->brightness_level) {
1291                         TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1292                 } else {
1293                         /* repeated key presses that didn't change state */
1294                         if (newn->brightness_level != 0) {
1295                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1296                         } else {
1297                                 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1298                         }
1299                 }
1300         }
1301 }
1302
1303 #undef TPACPI_COMPARE_KEY
1304 #undef TPACPI_MAY_SEND_KEY
1305
1306 static int hotkey_kthread(void *data)
1307 {
1308         struct tp_nvram_state s[2];
1309         u32 mask;
1310         unsigned int si, so;
1311         unsigned long t;
1312         unsigned int change_detector, must_reset;
1313
1314         mutex_lock(&hotkey_thread_mutex);
1315
1316         if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1317                 goto exit;
1318
1319         set_freezable();
1320
1321         so = 0;
1322         si = 1;
1323         t = 0;
1324
1325         /* Initial state for compares */
1326         mutex_lock(&hotkey_thread_data_mutex);
1327         change_detector = hotkey_config_change;
1328         mask = hotkey_source_mask & hotkey_mask;
1329         mutex_unlock(&hotkey_thread_data_mutex);
1330         hotkey_read_nvram(&s[so], mask);
1331
1332         while (!kthread_should_stop() && hotkey_poll_freq) {
1333                 if (t == 0)
1334                         t = 1000/hotkey_poll_freq;
1335                 t = msleep_interruptible(t);
1336                 if (unlikely(kthread_should_stop()))
1337                         break;
1338                 must_reset = try_to_freeze();
1339                 if (t > 0 && !must_reset)
1340                         continue;
1341
1342                 mutex_lock(&hotkey_thread_data_mutex);
1343                 if (must_reset || hotkey_config_change != change_detector) {
1344                         /* forget old state on thaw or config change */
1345                         si = so;
1346                         t = 0;
1347                         change_detector = hotkey_config_change;
1348                 }
1349                 mask = hotkey_source_mask & hotkey_mask;
1350                 mutex_unlock(&hotkey_thread_data_mutex);
1351
1352                 if (likely(mask)) {
1353                         hotkey_read_nvram(&s[si], mask);
1354                         if (likely(si != so)) {
1355                                 hotkey_compare_and_issue_event(&s[so], &s[si],
1356                                                                 mask);
1357                         }
1358                 }
1359
1360                 so = si;
1361                 si ^= 1;
1362         }
1363
1364 exit:
1365         mutex_unlock(&hotkey_thread_mutex);
1366         return 0;
1367 }
1368
1369 static void hotkey_poll_stop_sync(void)
1370 {
1371         if (tpacpi_hotkey_task) {
1372                 if (frozen(tpacpi_hotkey_task) ||
1373                     freezing(tpacpi_hotkey_task))
1374                         thaw_process(tpacpi_hotkey_task);
1375
1376                 kthread_stop(tpacpi_hotkey_task);
1377                 tpacpi_hotkey_task = NULL;
1378                 mutex_lock(&hotkey_thread_mutex);
1379                 /* at this point, the thread did exit */
1380                 mutex_unlock(&hotkey_thread_mutex);
1381         }
1382 }
1383
1384 /* call with hotkey_mutex held */
1385 static void hotkey_poll_setup(int may_warn)
1386 {
1387         if ((hotkey_source_mask & hotkey_mask) != 0 &&
1388             hotkey_poll_freq > 0 &&
1389             (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1390                 if (!tpacpi_hotkey_task) {
1391                         tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1392                                                          NULL,
1393                                                          TPACPI_FILE "d");
1394                         if (IS_ERR(tpacpi_hotkey_task)) {
1395                                 tpacpi_hotkey_task = NULL;
1396                                 printk(TPACPI_ERR
1397                                        "could not create kernel thread "
1398                                        "for hotkey polling\n");
1399                         }
1400                 }
1401         } else {
1402                 hotkey_poll_stop_sync();
1403                 if (may_warn &&
1404                     hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1405                         printk(TPACPI_NOTICE
1406                                 "hot keys 0x%08x require polling, "
1407                                 "which is currently disabled\n",
1408                                 hotkey_source_mask);
1409                 }
1410         }
1411 }
1412
1413 static void hotkey_poll_setup_safe(int may_warn)
1414 {
1415         mutex_lock(&hotkey_mutex);
1416         hotkey_poll_setup(may_warn);
1417         mutex_unlock(&hotkey_mutex);
1418 }
1419
1420 static int hotkey_inputdev_open(struct input_dev *dev)
1421 {
1422         switch (tpacpi_lifecycle) {
1423         case TPACPI_LIFE_INIT:
1424                 /*
1425                  * hotkey_init will call hotkey_poll_setup_safe
1426                  * at the appropriate moment
1427                  */
1428                 return 0;
1429         case TPACPI_LIFE_EXITING:
1430                 return -EBUSY;
1431         case TPACPI_LIFE_RUNNING:
1432                 hotkey_poll_setup_safe(0);
1433                 return 0;
1434         }
1435
1436         /* Should only happen if tpacpi_lifecycle is corrupt */
1437         BUG();
1438         return -EBUSY;
1439 }
1440
1441 static void hotkey_inputdev_close(struct input_dev *dev)
1442 {
1443         /* disable hotkey polling when possible */
1444         if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1445                 hotkey_poll_setup_safe(0);
1446 }
1447 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1448
1449 /* sysfs hotkey enable ------------------------------------------------- */
1450 static ssize_t hotkey_enable_show(struct device *dev,
1451                            struct device_attribute *attr,
1452                            char *buf)
1453 {
1454         int res, status;
1455
1456         res = hotkey_status_get(&status);
1457         if (res)
1458                 return res;
1459
1460         return snprintf(buf, PAGE_SIZE, "%d\n", status);
1461 }
1462
1463 static ssize_t hotkey_enable_store(struct device *dev,
1464                             struct device_attribute *attr,
1465                             const char *buf, size_t count)
1466 {
1467         unsigned long t;
1468         int res;
1469
1470         if (parse_strtoul(buf, 1, &t))
1471                 return -EINVAL;
1472
1473         res = hotkey_status_set(t);
1474
1475         return (res) ? res : count;
1476 }
1477
1478 static struct device_attribute dev_attr_hotkey_enable =
1479         __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1480                 hotkey_enable_show, hotkey_enable_store);
1481
1482 /* sysfs hotkey mask --------------------------------------------------- */
1483 static ssize_t hotkey_mask_show(struct device *dev,
1484                            struct device_attribute *attr,
1485                            char *buf)
1486 {
1487         int res;
1488
1489         if (mutex_lock_interruptible(&hotkey_mutex))
1490                 return -ERESTARTSYS;
1491         res = hotkey_mask_get();
1492         mutex_unlock(&hotkey_mutex);
1493
1494         return (res)?
1495                 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1496 }
1497
1498 static ssize_t hotkey_mask_store(struct device *dev,
1499                             struct device_attribute *attr,
1500                             const char *buf, size_t count)
1501 {
1502         unsigned long t;
1503         int res;
1504
1505         if (parse_strtoul(buf, 0xffffffffUL, &t))
1506                 return -EINVAL;
1507
1508         if (mutex_lock_interruptible(&hotkey_mutex))
1509                 return -ERESTARTSYS;
1510
1511         res = hotkey_mask_set(t);
1512
1513 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1514         hotkey_poll_setup(1);
1515 #endif
1516
1517         mutex_unlock(&hotkey_mutex);
1518
1519         return (res) ? res : count;
1520 }
1521
1522 static struct device_attribute dev_attr_hotkey_mask =
1523         __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1524                 hotkey_mask_show, hotkey_mask_store);
1525
1526 /* sysfs hotkey bios_enabled ------------------------------------------- */
1527 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1528                            struct device_attribute *attr,
1529                            char *buf)
1530 {
1531         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1532 }
1533
1534 static struct device_attribute dev_attr_hotkey_bios_enabled =
1535         __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1536
1537 /* sysfs hotkey bios_mask ---------------------------------------------- */
1538 static ssize_t hotkey_bios_mask_show(struct device *dev,
1539                            struct device_attribute *attr,
1540                            char *buf)
1541 {
1542         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1543 }
1544
1545 static struct device_attribute dev_attr_hotkey_bios_mask =
1546         __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1547
1548 /* sysfs hotkey all_mask ----------------------------------------------- */
1549 static ssize_t hotkey_all_mask_show(struct device *dev,
1550                            struct device_attribute *attr,
1551                            char *buf)
1552 {
1553         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1554                                 hotkey_all_mask | hotkey_source_mask);
1555 }
1556
1557 static struct device_attribute dev_attr_hotkey_all_mask =
1558         __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1559
1560 /* sysfs hotkey recommended_mask --------------------------------------- */
1561 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1562                                             struct device_attribute *attr,
1563                                             char *buf)
1564 {
1565         return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1566                         (hotkey_all_mask | hotkey_source_mask)
1567                         & ~hotkey_reserved_mask);
1568 }
1569
1570 static struct device_attribute dev_attr_hotkey_recommended_mask =
1571         __ATTR(hotkey_recommended_mask, S_IRUGO,
1572                 hotkey_recommended_mask_show, NULL);
1573
1574 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1575
1576 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1577 static ssize_t hotkey_source_mask_show(struct device *dev,
1578                            struct device_attribute *attr,
1579                            char *buf)
1580 {
1581         return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1582 }
1583
1584 static ssize_t hotkey_source_mask_store(struct device *dev,
1585                             struct device_attribute *attr,
1586                             const char *buf, size_t count)
1587 {
1588         unsigned long t;
1589
1590         if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1591                 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1592                 return -EINVAL;
1593
1594         if (mutex_lock_interruptible(&hotkey_mutex))
1595                 return -ERESTARTSYS;
1596
1597         HOTKEY_CONFIG_CRITICAL_START
1598         hotkey_source_mask = t;
1599         HOTKEY_CONFIG_CRITICAL_END
1600
1601         hotkey_poll_setup(1);
1602
1603         mutex_unlock(&hotkey_mutex);
1604
1605         return count;
1606 }
1607
1608 static struct device_attribute dev_attr_hotkey_source_mask =
1609         __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1610                 hotkey_source_mask_show, hotkey_source_mask_store);
1611
1612 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1613 static ssize_t hotkey_poll_freq_show(struct device *dev,
1614                            struct device_attribute *attr,
1615                            char *buf)
1616 {
1617         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1618 }
1619
1620 static ssize_t hotkey_poll_freq_store(struct device *dev,
1621                             struct device_attribute *attr,
1622                             const char *buf, size_t count)
1623 {
1624         unsigned long t;
1625
1626         if (parse_strtoul(buf, 25, &t))
1627                 return -EINVAL;
1628
1629         if (mutex_lock_interruptible(&hotkey_mutex))
1630                 return -ERESTARTSYS;
1631
1632         hotkey_poll_freq = t;
1633
1634         hotkey_poll_setup(1);
1635         mutex_unlock(&hotkey_mutex);
1636
1637         return count;
1638 }
1639
1640 static struct device_attribute dev_attr_hotkey_poll_freq =
1641         __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1642                 hotkey_poll_freq_show, hotkey_poll_freq_store);
1643
1644 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1645
1646 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1647 static ssize_t hotkey_radio_sw_show(struct device *dev,
1648                            struct device_attribute *attr,
1649                            char *buf)
1650 {
1651         int res, s;
1652         res = hotkey_get_wlsw(&s);
1653         if (res < 0)
1654                 return res;
1655
1656         return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1657 }
1658
1659 static struct device_attribute dev_attr_hotkey_radio_sw =
1660         __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1661
1662 static void hotkey_radio_sw_notify_change(void)
1663 {
1664         if (tp_features.hotkey_wlsw)
1665                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1666                              "hotkey_radio_sw");
1667 }
1668
1669 /* sysfs hotkey report_mode -------------------------------------------- */
1670 static ssize_t hotkey_report_mode_show(struct device *dev,
1671                            struct device_attribute *attr,
1672                            char *buf)
1673 {
1674         return snprintf(buf, PAGE_SIZE, "%d\n",
1675                 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1676 }
1677
1678 static struct device_attribute dev_attr_hotkey_report_mode =
1679         __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1680
1681 /* sysfs wakeup reason (pollable) -------------------------------------- */
1682 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1683                            struct device_attribute *attr,
1684                            char *buf)
1685 {
1686         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1687 }
1688
1689 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1690         __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1691
1692 void hotkey_wakeup_reason_notify_change(void)
1693 {
1694         if (tp_features.hotkey_mask)
1695                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1696                              "wakeup_reason");
1697 }
1698
1699 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1700 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1701                            struct device_attribute *attr,
1702                            char *buf)
1703 {
1704         return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1705 }
1706
1707 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1708         __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1709                hotkey_wakeup_hotunplug_complete_show, NULL);
1710
1711 void hotkey_wakeup_hotunplug_complete_notify_change(void)
1712 {
1713         if (tp_features.hotkey_mask)
1714                 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1715                              "wakeup_hotunplug_complete");
1716 }
1717
1718 /* --------------------------------------------------------------------- */
1719
1720 static struct attribute *hotkey_attributes[] __initdata = {
1721         &dev_attr_hotkey_enable.attr,
1722         &dev_attr_hotkey_bios_enabled.attr,
1723         &dev_attr_hotkey_report_mode.attr,
1724 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1725         &dev_attr_hotkey_mask.attr,
1726         &dev_attr_hotkey_all_mask.attr,
1727         &dev_attr_hotkey_recommended_mask.attr,
1728         &dev_attr_hotkey_source_mask.attr,
1729         &dev_attr_hotkey_poll_freq.attr,
1730 #endif
1731 };
1732
1733 static struct attribute *hotkey_mask_attributes[] __initdata = {
1734         &dev_attr_hotkey_bios_mask.attr,
1735 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1736         &dev_attr_hotkey_mask.attr,
1737         &dev_attr_hotkey_all_mask.attr,
1738         &dev_attr_hotkey_recommended_mask.attr,
1739 #endif
1740         &dev_attr_hotkey_wakeup_reason.attr,
1741         &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1742 };
1743
1744 static int __init hotkey_init(struct ibm_init_struct *iibm)
1745 {
1746         /* Requirements for changing the default keymaps:
1747          *
1748          * 1. Many of the keys are mapped to KEY_RESERVED for very
1749          *    good reasons.  Do not change them unless you have deep
1750          *    knowledge on the IBM and Lenovo ThinkPad firmware for
1751          *    the various ThinkPad models.  The driver behaves
1752          *    differently for KEY_RESERVED: such keys have their
1753          *    hot key mask *unset* in mask_recommended, and also
1754          *    in the initial hot key mask programmed into the
1755          *    firmware at driver load time, which means the firm-
1756          *    ware may react very differently if you change them to
1757          *    something else;
1758          *
1759          * 2. You must be subscribed to the linux-thinkpad and
1760          *    ibm-acpi-devel mailing lists, and you should read the
1761          *    list archives since 2007 if you want to change the
1762          *    keymaps.  This requirement exists so that you will
1763          *    know the past history of problems with the thinkpad-
1764          *    acpi driver keymaps, and also that you will be
1765          *    listening to any bug reports;
1766          *
1767          * 3. Do not send thinkpad-acpi specific patches directly to
1768          *    for merging, *ever*.  Send them to the linux-acpi
1769          *    mailinglist for comments.  Merging is to be done only
1770          *    through acpi-test and the ACPI maintainer.
1771          *
1772          * If the above is too much to ask, don't change the keymap.
1773          * Ask the thinkpad-acpi maintainer to do it, instead.
1774          */
1775         static u16 ibm_keycode_map[] __initdata = {
1776                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1777                 KEY_FN_F1,      KEY_FN_F2,      KEY_COFFEE,     KEY_SLEEP,
1778                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1779                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
1780
1781                 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1782                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
1783                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
1784                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
1785
1786                 /* brightness: firmware always reacts to them, unless
1787                  * X.org did some tricks in the radeon BIOS scratch
1788                  * registers of *some* models */
1789                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
1790                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
1791
1792                 /* Thinklight: firmware always react to it */
1793                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
1794
1795                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
1796                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
1797
1798                 /* Volume: firmware always react to it and reprograms
1799                  * the built-in *extra* mixer.  Never map it to control
1800                  * another mixer by default. */
1801                 KEY_RESERVED,   /* 0x14: VOLUME UP */
1802                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
1803                 KEY_RESERVED,   /* 0x16: MUTE */
1804
1805                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
1806
1807                 /* (assignments unknown, please report if found) */
1808                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1809                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1810         };
1811         static u16 lenovo_keycode_map[] __initdata = {
1812                 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1813                 KEY_FN_F1,      KEY_COFFEE,     KEY_BATTERY,    KEY_SLEEP,
1814                 KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1815                 KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
1816
1817                 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1818                 KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
1819                 KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
1820                 KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
1821
1822                 KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
1823                 KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
1824
1825                 KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
1826
1827                 KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
1828                 KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
1829
1830                 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1831                  * react to it and reprograms the built-in *extra* mixer.
1832                  * Never map it to control another mixer by default.
1833                  *
1834                  * T60?, T61, R60?, R61: firmware and EC tries to send
1835                  * these over the regular keyboard, so these are no-ops,
1836                  * but there are still weird bugs re. MUTE, so do not
1837                  * change unless you get test reports from all Lenovo
1838                  * models.  May cause the BIOS to interfere with the
1839                  * HDA mixer.
1840                  */
1841                 KEY_RESERVED,   /* 0x14: VOLUME UP */
1842                 KEY_RESERVED,   /* 0x15: VOLUME DOWN */
1843                 KEY_RESERVED,   /* 0x16: MUTE */
1844
1845                 KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
1846
1847                 /* (assignments unknown, please report if found) */
1848                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1849                 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1850         };
1851
1852 #define TPACPI_HOTKEY_MAP_LEN           ARRAY_SIZE(ibm_keycode_map)
1853 #define TPACPI_HOTKEY_MAP_SIZE          sizeof(ibm_keycode_map)
1854 #define TPACPI_HOTKEY_MAP_TYPESIZE      sizeof(ibm_keycode_map[0])
1855
1856         int res, i;
1857         int status;
1858         int hkeyv;
1859
1860         vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1861
1862         BUG_ON(!tpacpi_inputdev);
1863         BUG_ON(tpacpi_inputdev->open != NULL ||
1864                tpacpi_inputdev->close != NULL);
1865
1866         TPACPI_ACPIHANDLE_INIT(hkey);
1867         mutex_init(&hotkey_mutex);
1868
1869 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1870         mutex_init(&hotkey_thread_mutex);
1871         mutex_init(&hotkey_thread_data_mutex);
1872 #endif
1873
1874         /* hotkey not supported on 570 */
1875         tp_features.hotkey = hkey_handle != NULL;
1876
1877         vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1878                 str_supported(tp_features.hotkey));
1879
1880         if (tp_features.hotkey) {
1881                 hotkey_dev_attributes = create_attr_set(12, NULL);
1882                 if (!hotkey_dev_attributes)
1883                         return -ENOMEM;
1884                 res = add_many_to_attr_set(hotkey_dev_attributes,
1885                                 hotkey_attributes,
1886                                 ARRAY_SIZE(hotkey_attributes));
1887                 if (res)
1888                         return res;
1889
1890                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1891                    A30, R30, R31, T20-22, X20-21, X22-24.  Detected by checking
1892                    for HKEY interface version 0x100 */
1893                 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1894                         if ((hkeyv >> 8) != 1) {
1895                                 printk(TPACPI_ERR "unknown version of the "
1896                                        "HKEY interface: 0x%x\n", hkeyv);
1897                                 printk(TPACPI_ERR "please report this to %s\n",
1898                                        TPACPI_MAIL);
1899                         } else {
1900                                 /*
1901                                  * MHKV 0x100 in A31, R40, R40e,
1902                                  * T4x, X31, and later
1903                                  */
1904                                 tp_features.hotkey_mask = 1;
1905                         }
1906                 }
1907
1908                 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1909                         str_supported(tp_features.hotkey_mask));
1910
1911                 if (tp_features.hotkey_mask) {
1912                         if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1913                                         "MHKA", "qd")) {
1914                                 printk(TPACPI_ERR
1915                                        "missing MHKA handler, "
1916                                        "please report this to %s\n",
1917                                        TPACPI_MAIL);
1918                                 /* FN+F12, FN+F4, FN+F3 */
1919                                 hotkey_all_mask = 0x080cU;
1920                         }
1921                 }
1922
1923                 /* hotkey_source_mask *must* be zero for
1924                  * the first hotkey_mask_get */
1925                 res = hotkey_status_get(&hotkey_orig_status);
1926                 if (!res && tp_features.hotkey_mask) {
1927                         res = hotkey_mask_get();
1928                         hotkey_orig_mask = hotkey_mask;
1929                         if (!res) {
1930                                 res = add_many_to_attr_set(
1931                                         hotkey_dev_attributes,
1932                                         hotkey_mask_attributes,
1933                                         ARRAY_SIZE(hotkey_mask_attributes));
1934                         }
1935                 }
1936
1937 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1938                 if (tp_features.hotkey_mask) {
1939                         hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1940                                                 & ~hotkey_all_mask;
1941                 } else {
1942                         hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1943                 }
1944
1945                 vdbg_printk(TPACPI_DBG_INIT,
1946                             "hotkey source mask 0x%08x, polling freq %d\n",
1947                             hotkey_source_mask, hotkey_poll_freq);
1948 #endif
1949
1950                 /* Not all thinkpads have a hardware radio switch */
1951                 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1952                         tp_features.hotkey_wlsw = 1;
1953                         printk(TPACPI_INFO
1954                                 "radio switch found; radios are %s\n",
1955                                 enabled(status, 0));
1956                         res = add_to_attr_set(hotkey_dev_attributes,
1957                                         &dev_attr_hotkey_radio_sw.attr);
1958                 }
1959
1960                 if (!res)
1961                         res = register_attr_set_with_sysfs(
1962                                         hotkey_dev_attributes,
1963                                         &tpacpi_pdev->dev.kobj);
1964                 if (res)
1965                         return res;
1966
1967                 /* Set up key map */
1968
1969                 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1970                                                 GFP_KERNEL);
1971                 if (!hotkey_keycode_map) {
1972                         printk(TPACPI_ERR
1973                                 "failed to allocate memory for key map\n");
1974                         return -ENOMEM;
1975                 }
1976
1977                 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1978                         dbg_printk(TPACPI_DBG_INIT,
1979                                    "using Lenovo default hot key map\n");
1980                         memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1981                                 TPACPI_HOTKEY_MAP_SIZE);
1982                 } else {
1983                         dbg_printk(TPACPI_DBG_INIT,
1984                                    "using IBM default hot key map\n");
1985                         memcpy(hotkey_keycode_map, &ibm_keycode_map,
1986                                 TPACPI_HOTKEY_MAP_SIZE);
1987                 }
1988
1989                 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1990                 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1991                 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1992                 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1993                 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1994                 tpacpi_inputdev->keycode = hotkey_keycode_map;
1995                 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1996                         if (hotkey_keycode_map[i] != KEY_RESERVED) {
1997                                 set_bit(hotkey_keycode_map[i],
1998                                         tpacpi_inputdev->keybit);
1999                         } else {
2000                                 if (i < sizeof(hotkey_reserved_mask)*8)
2001                                         hotkey_reserved_mask |= 1 << i;
2002                         }
2003                 }
2004
2005                 if (tp_features.hotkey_wlsw) {
2006                         set_bit(EV_SW, tpacpi_inputdev->evbit);
2007                         set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2008                 }
2009
2010                 dbg_printk(TPACPI_DBG_INIT,
2011                                 "enabling hot key handling\n");
2012                 res = hotkey_status_set(1);
2013                 if (res)
2014                         return res;
2015                 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2016                                         & ~hotkey_reserved_mask)
2017                                         | hotkey_orig_mask);
2018                 if (res < 0 && res != -ENXIO)
2019                         return res;
2020
2021                 dbg_printk(TPACPI_DBG_INIT,
2022                                 "legacy hot key reporting over procfs %s\n",
2023                                 (hotkey_report_mode < 2) ?
2024                                         "enabled" : "disabled");
2025
2026 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2027                 tpacpi_inputdev->open = &hotkey_inputdev_open;
2028                 tpacpi_inputdev->close = &hotkey_inputdev_close;
2029
2030                 hotkey_poll_setup_safe(1);
2031 #endif
2032         }
2033
2034         return (tp_features.hotkey)? 0 : 1;
2035 }
2036
2037 static void hotkey_exit(void)
2038 {
2039 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2040         hotkey_poll_stop_sync();
2041 #endif
2042
2043         if (tp_features.hotkey) {
2044                 dbg_printk(TPACPI_DBG_EXIT,
2045                            "restoring original hot key mask\n");
2046                 /* no short-circuit boolean operator below! */
2047                 if ((hotkey_mask_set(hotkey_orig_mask) |
2048                      hotkey_status_set(hotkey_orig_status)) != 0)
2049                         printk(TPACPI_ERR
2050                                "failed to restore hot key mask "
2051                                "to BIOS defaults\n");
2052         }
2053
2054         if (hotkey_dev_attributes) {
2055                 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2056                 hotkey_dev_attributes = NULL;
2057         }
2058 }
2059
2060 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2061 {
2062         u32 hkey;
2063         unsigned int scancode;
2064         int send_acpi_ev;
2065         int ignore_acpi_ev;
2066         int unk_ev;
2067
2068         if (event != 0x80) {
2069                 printk(TPACPI_ERR
2070                        "unknown HKEY notification event %d\n", event);
2071                 /* forward it to userspace, maybe it knows how to handle it */
2072                 acpi_bus_generate_netlink_event(
2073                                         ibm->acpi->device->pnp.device_class,
2074                                         ibm->acpi->device->dev.bus_id,
2075                                         event, 0);
2076                 return;
2077         }
2078
2079         while (1) {
2080                 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2081                         printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2082                         return;
2083                 }
2084
2085                 if (hkey == 0) {
2086                         /* queue empty */
2087                         return;
2088                 }
2089
2090                 send_acpi_ev = 1;
2091                 ignore_acpi_ev = 0;
2092                 unk_ev = 0;
2093
2094                 switch (hkey >> 12) {
2095                 case 1:
2096                         /* 0x1000-0x1FFF: key presses */
2097                         scancode = hkey & 0xfff;
2098                         if (scancode > 0 && scancode < 0x21) {
2099                                 scancode--;
2100                                 if (!(hotkey_source_mask & (1 << scancode))) {
2101                                         tpacpi_input_send_key(scancode);
2102                                         send_acpi_ev = 0;
2103                                 } else {
2104                                         ignore_acpi_ev = 1;
2105                                 }
2106                         } else {
2107                                 unk_ev = 1;
2108                         }
2109                         break;
2110                 case 2:
2111                         /* Wakeup reason */
2112                         switch (hkey) {
2113                         case 0x2304: /* suspend, undock */
2114                         case 0x2404: /* hibernation, undock */
2115                                 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2116                                 ignore_acpi_ev = 1;
2117                                 break;
2118                         case 0x2305: /* suspend, bay eject */
2119                         case 0x2405: /* hibernation, bay eject */
2120                                 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2121                                 ignore_acpi_ev = 1;
2122                                 break;
2123                         default:
2124                                 unk_ev = 1;
2125                         }
2126                         if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2127                                 printk(TPACPI_INFO
2128                                        "woke up due to a hot-unplug "
2129                                        "request...\n");
2130                                 hotkey_wakeup_reason_notify_change();
2131                         }
2132                         break;
2133                 case 3:
2134                         /* bay-related wakeups */
2135                         if (hkey == 0x3003) {
2136                                 hotkey_autosleep_ack = 1;
2137                                 printk(TPACPI_INFO
2138                                        "bay ejected\n");
2139                                 hotkey_wakeup_hotunplug_complete_notify_change();
2140                         } else {
2141                                 unk_ev = 1;
2142                         }
2143                         break;
2144                 case 4:
2145                         /* dock-related wakeups */
2146                         if (hkey == 0x4003) {
2147                                 hotkey_autosleep_ack = 1;
2148                                 printk(TPACPI_INFO
2149                                        "undocked\n");
2150                                 hotkey_wakeup_hotunplug_complete_notify_change();
2151                         } else {
2152                                 unk_ev = 1;
2153                         }
2154                         break;
2155                 case 5:
2156                         /* 0x5000-0x5FFF: human interface helpers */
2157                         switch (hkey) {
2158                         case 0x5010: /* Lenovo new BIOS: brightness changed */
2159                         case 0x5009: /* X61t: swivel up (tablet mode) */
2160                         case 0x500a: /* X61t: swivel down (normal mode) */
2161                         case 0x500b: /* X61t: tablet pen inserted into bay */
2162                         case 0x500c: /* X61t: tablet pen removed from bay */
2163                                 break;
2164                         case 0x5001:
2165                         case 0x5002:
2166                                 /* LID switch events.  Do not propagate */
2167                                 ignore_acpi_ev = 1;
2168                                 break;
2169                         default:
2170                                 unk_ev = 1;
2171                         }
2172                         break;
2173                 case 7:
2174                         /* 0x7000-0x7FFF: misc */
2175                         if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2176                                 tpacpi_input_send_radiosw();
2177                                 hotkey_radio_sw_notify_change();
2178                                 send_acpi_ev = 0;
2179                                 break;
2180                         }
2181                         /* fallthrough to default */
2182                 default:
2183                         unk_ev = 1;
2184                 }
2185                 if (unk_ev) {
2186                         printk(TPACPI_NOTICE
2187                                "unhandled HKEY event 0x%04x\n", hkey);
2188                 }
2189
2190                 /* Legacy events */
2191                 if (!ignore_acpi_ev &&
2192                     (send_acpi_ev || hotkey_report_mode < 2)) {
2193                         acpi_bus_generate_proc_event(ibm->acpi->device,
2194                                                      event, hkey);
2195                 }
2196
2197                 /* netlink events */
2198                 if (!ignore_acpi_ev && send_acpi_ev) {
2199                         acpi_bus_generate_netlink_event(
2200                                         ibm->acpi->device->pnp.device_class,
2201                                         ibm->acpi->device->dev.bus_id,
2202                                         event, hkey);
2203                 }
2204         }
2205 }
2206
2207 static void hotkey_suspend(pm_message_t state)
2208 {
2209         /* Do these on suspend, we get the events on early resume! */
2210         hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2211         hotkey_autosleep_ack = 0;
2212 }
2213
2214 static void hotkey_resume(void)
2215 {
2216         if (hotkey_mask_get())
2217                 printk(TPACPI_ERR
2218                        "error while trying to read hot key mask "
2219                        "from firmware\n");
2220         tpacpi_input_send_radiosw();
2221         hotkey_radio_sw_notify_change();
2222         hotkey_wakeup_reason_notify_change();
2223         hotkey_wakeup_hotunplug_complete_notify_change();
2224 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2225         hotkey_poll_setup_safe(0);
2226 #endif
2227 }
2228
2229 /* procfs -------------------------------------------------------------- */
2230 static int hotkey_read(char *p)
2231 {
2232         int res, status;
2233         int len = 0;
2234
2235         if (!tp_features.hotkey) {
2236                 len += sprintf(p + len, "status:\t\tnot supported\n");
2237                 return len;
2238         }
2239
2240         if (mutex_lock_interruptible(&hotkey_mutex))
2241                 return -ERESTARTSYS;
2242         res = hotkey_status_get(&status);
2243         if (!res)
2244                 res = hotkey_mask_get();
2245         mutex_unlock(&hotkey_mutex);
2246         if (res)
2247                 return res;
2248
2249         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2250         if (tp_features.hotkey_mask) {
2251                 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2252                 len += sprintf(p + len,
2253                                "commands:\tenable, disable, reset, <mask>\n");
2254         } else {
2255                 len += sprintf(p + len, "mask:\t\tnot supported\n");
2256                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2257         }
2258
2259         return len;
2260 }
2261
2262 static int hotkey_write(char *buf)
2263 {
2264         int res, status;
2265         u32 mask;
2266         char *cmd;
2267
2268         if (!tp_features.hotkey)
2269                 return -ENODEV;
2270
2271         if (mutex_lock_interruptible(&hotkey_mutex))
2272                 return -ERESTARTSYS;
2273
2274         status = -1;
2275         mask = hotkey_mask;
2276
2277         res = 0;
2278         while ((cmd = next_cmd(&buf))) {
2279                 if (strlencmp(cmd, "enable") == 0) {
2280                         status = 1;
2281                 } else if (strlencmp(cmd, "disable") == 0) {
2282                         status = 0;
2283                 } else if (strlencmp(cmd, "reset") == 0) {
2284                         status = hotkey_orig_status;
2285                         mask = hotkey_orig_mask;
2286                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2287                         /* mask set */
2288                 } else if (sscanf(cmd, "%x", &mask) == 1) {
2289                         /* mask set */
2290                 } else {
2291                         res = -EINVAL;
2292                         goto errexit;
2293                 }
2294         }
2295         if (status != -1)
2296                 res = hotkey_status_set(status);
2297
2298         if (!res && mask != hotkey_mask)
2299                 res = hotkey_mask_set(mask);
2300
2301 errexit:
2302         mutex_unlock(&hotkey_mutex);
2303         return res;
2304 }
2305
2306 static const struct acpi_device_id ibm_htk_device_ids[] = {
2307         {TPACPI_ACPI_HKEY_HID, 0},
2308         {"", 0},
2309 };
2310
2311 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2312         .hid = ibm_htk_device_ids,
2313         .notify = hotkey_notify,
2314         .handle = &hkey_handle,
2315         .type = ACPI_DEVICE_NOTIFY,
2316 };
2317
2318 static struct ibm_struct hotkey_driver_data = {
2319         .name = "hotkey",
2320         .read = hotkey_read,
2321         .write = hotkey_write,
2322         .exit = hotkey_exit,
2323         .resume = hotkey_resume,
2324         .suspend = hotkey_suspend,
2325         .acpi = &ibm_hotkey_acpidriver,
2326 };
2327
2328 /*************************************************************************
2329  * Bluetooth subdriver
2330  */
2331
2332 enum {
2333         /* ACPI GBDC/SBDC bits */
2334         TP_ACPI_BLUETOOTH_HWPRESENT     = 0x01, /* Bluetooth hw available */
2335         TP_ACPI_BLUETOOTH_RADIOSSW      = 0x02, /* Bluetooth radio enabled */
2336         TP_ACPI_BLUETOOTH_UNK           = 0x04, /* unknown function */
2337 };
2338
2339 static int bluetooth_get_radiosw(void);
2340 static int bluetooth_set_radiosw(int radio_on);
2341
2342 /* sysfs bluetooth enable ---------------------------------------------- */
2343 static ssize_t bluetooth_enable_show(struct device *dev,
2344                            struct device_attribute *attr,
2345                            char *buf)
2346 {
2347         int status;
2348
2349         status = bluetooth_get_radiosw();
2350         if (status < 0)
2351                 return status;
2352
2353         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2354 }
2355
2356 static ssize_t bluetooth_enable_store(struct device *dev,
2357                             struct device_attribute *attr,
2358                             const char *buf, size_t count)
2359 {
2360         unsigned long t;
2361         int res;
2362
2363         if (parse_strtoul(buf, 1, &t))
2364                 return -EINVAL;
2365
2366         res = bluetooth_set_radiosw(t);
2367
2368         return (res) ? res : count;
2369 }
2370
2371 static struct device_attribute dev_attr_bluetooth_enable =
2372         __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2373                 bluetooth_enable_show, bluetooth_enable_store);
2374
2375 /* --------------------------------------------------------------------- */
2376
2377 static struct attribute *bluetooth_attributes[] = {
2378         &dev_attr_bluetooth_enable.attr,
2379         NULL
2380 };
2381
2382 static const struct attribute_group bluetooth_attr_group = {
2383         .attrs = bluetooth_attributes,
2384 };
2385
2386 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2387 {
2388         int res;
2389         int status = 0;
2390
2391         vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2392
2393         TPACPI_ACPIHANDLE_INIT(hkey);
2394
2395         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2396            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2397         tp_features.bluetooth = hkey_handle &&
2398             acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2399
2400         vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2401                 str_supported(tp_features.bluetooth),
2402                 status);
2403
2404         if (tp_features.bluetooth) {
2405                 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2406                         /* no bluetooth hardware present in system */
2407                         tp_features.bluetooth = 0;
2408                         dbg_printk(TPACPI_DBG_INIT,
2409                                    "bluetooth hardware not installed\n");
2410                 } else {
2411                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2412                                         &bluetooth_attr_group);
2413                         if (res)
2414                                 return res;
2415                 }
2416         }
2417
2418         return (tp_features.bluetooth)? 0 : 1;
2419 }
2420
2421 static void bluetooth_exit(void)
2422 {
2423         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2424                         &bluetooth_attr_group);
2425 }
2426
2427 static int bluetooth_get_radiosw(void)
2428 {
2429         int status;
2430
2431         if (!tp_features.bluetooth)
2432                 return -ENODEV;
2433
2434         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2435                 return -EIO;
2436
2437         return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2438 }
2439
2440 static int bluetooth_set_radiosw(int radio_on)
2441 {
2442         int status;
2443
2444         if (!tp_features.bluetooth)
2445                 return -ENODEV;
2446
2447         if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2448                 return -EIO;
2449         if (radio_on)
2450                 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2451         else
2452                 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2453         if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2454                 return -EIO;
2455
2456         return 0;
2457 }
2458
2459 /* procfs -------------------------------------------------------------- */
2460 static int bluetooth_read(char *p)
2461 {
2462         int len = 0;
2463         int status = bluetooth_get_radiosw();
2464
2465         if (!tp_features.bluetooth)
2466                 len += sprintf(p + len, "status:\t\tnot supported\n");
2467         else {
2468                 len += sprintf(p + len, "status:\t\t%s\n",
2469                                 (status)? "enabled" : "disabled");
2470                 len += sprintf(p + len, "commands:\tenable, disable\n");
2471         }
2472
2473         return len;
2474 }
2475
2476 static int bluetooth_write(char *buf)
2477 {
2478         char *cmd;
2479
2480         if (!tp_features.bluetooth)
2481                 return -ENODEV;
2482
2483         while ((cmd = next_cmd(&buf))) {
2484                 if (strlencmp(cmd, "enable") == 0) {
2485                         bluetooth_set_radiosw(1);
2486                 } else if (strlencmp(cmd, "disable") == 0) {
2487                         bluetooth_set_radiosw(0);
2488                 } else
2489                         return -EINVAL;
2490         }
2491
2492         return 0;
2493 }
2494
2495 static struct ibm_struct bluetooth_driver_data = {
2496         .name = "bluetooth",
2497         .read = bluetooth_read,
2498         .write = bluetooth_write,
2499         .exit = bluetooth_exit,
2500 };
2501
2502 /*************************************************************************
2503  * Wan subdriver
2504  */
2505
2506 enum {
2507         /* ACPI GWAN/SWAN bits */
2508         TP_ACPI_WANCARD_HWPRESENT       = 0x01, /* Wan hw available */
2509         TP_ACPI_WANCARD_RADIOSSW        = 0x02, /* Wan radio enabled */
2510         TP_ACPI_WANCARD_UNK             = 0x04, /* unknown function */
2511 };
2512
2513 static int wan_get_radiosw(void);
2514 static int wan_set_radiosw(int radio_on);
2515
2516 /* sysfs wan enable ---------------------------------------------------- */
2517 static ssize_t wan_enable_show(struct device *dev,
2518                            struct device_attribute *attr,
2519                            char *buf)
2520 {
2521         int status;
2522
2523         status = wan_get_radiosw();
2524         if (status < 0)
2525                 return status;
2526
2527         return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2528 }
2529
2530 static ssize_t wan_enable_store(struct device *dev,
2531                             struct device_attribute *attr,
2532                             const char *buf, size_t count)
2533 {
2534         unsigned long t;
2535         int res;
2536
2537         if (parse_strtoul(buf, 1, &t))
2538                 return -EINVAL;
2539
2540         res = wan_set_radiosw(t);
2541
2542         return (res) ? res : count;
2543 }
2544
2545 static struct device_attribute dev_attr_wan_enable =
2546         __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2547                 wan_enable_show, wan_enable_store);
2548
2549 /* --------------------------------------------------------------------- */
2550
2551 static struct attribute *wan_attributes[] = {
2552         &dev_attr_wan_enable.attr,
2553         NULL
2554 };
2555
2556 static const struct attribute_group wan_attr_group = {
2557         .attrs = wan_attributes,
2558 };
2559
2560 static int __init wan_init(struct ibm_init_struct *iibm)
2561 {
2562         int res;
2563         int status = 0;
2564
2565         vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2566
2567         TPACPI_ACPIHANDLE_INIT(hkey);
2568
2569         tp_features.wan = hkey_handle &&
2570             acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2571
2572         vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2573                 str_supported(tp_features.wan),
2574                 status);
2575
2576         if (tp_features.wan) {
2577                 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2578                         /* no wan hardware present in system */
2579                         tp_features.wan = 0;
2580                         dbg_printk(TPACPI_DBG_INIT,
2581                                    "wan hardware not installed\n");
2582                 } else {
2583                         res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2584                                         &wan_attr_group);
2585                         if (res)
2586                                 return res;
2587                 }
2588         }
2589
2590         return (tp_features.wan)? 0 : 1;
2591 }
2592
2593 static void wan_exit(void)
2594 {
2595         sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2596                 &wan_attr_group);
2597 }
2598
2599 static int wan_get_radiosw(void)
2600 {
2601         int status;
2602
2603         if (!tp_features.wan)
2604                 return -ENODEV;
2605
2606         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2607                 return -EIO;
2608
2609         return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2610 }
2611
2612 static int wan_set_radiosw(int radio_on)
2613 {
2614         int status;
2615
2616         if (!tp_features.wan)
2617                 return -ENODEV;
2618
2619         if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2620                 return -EIO;
2621         if (radio_on)
2622                 status |= TP_ACPI_WANCARD_RADIOSSW;
2623         else
2624                 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2625         if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2626                 return -EIO;
2627
2628         return 0;
2629 }
2630
2631 /* procfs -------------------------------------------------------------- */
2632 static int wan_read(char *p)
2633 {
2634         int len = 0;
2635         int status = wan_get_radiosw();
2636
2637         if (!tp_features.wan)
2638                 len += sprintf(p + len, "status:\t\tnot supported\n");
2639         else {
2640                 len += sprintf(p + len, "status:\t\t%s\n",
2641                                 (status)? "enabled" : "disabled");
2642                 len += sprintf(p + len, "commands:\tenable, disable\n");
2643         }
2644
2645         return len;
2646 }
2647
2648 static int wan_write(char *buf)
2649 {
2650         char *cmd;
2651
2652         if (!tp_features.wan)
2653                 return -ENODEV;
2654
2655         while ((cmd = next_cmd(&buf))) {
2656                 if (strlencmp(cmd, "enable") == 0) {
2657                         wan_set_radiosw(1);
2658                 } else if (strlencmp(cmd, "disable") == 0) {
2659                         wan_set_radiosw(0);
2660                 } else
2661                         return -EINVAL;
2662         }
2663
2664         return 0;
2665 }
2666
2667 static struct ibm_struct wan_driver_data = {
2668         .name = "wan",
2669         .read = wan_read,
2670         .write = wan_write,
2671         .exit = wan_exit,
2672         .flags.experimental = 1,
2673 };
2674
2675 /*************************************************************************
2676  * Video subdriver
2677  */
2678
2679 enum video_access_mode {
2680         TPACPI_VIDEO_NONE = 0,
2681         TPACPI_VIDEO_570,       /* 570 */
2682         TPACPI_VIDEO_770,       /* 600e/x, 770e, 770x */
2683         TPACPI_VIDEO_NEW,       /* all others */
2684 };
2685
2686 enum {  /* video status flags, based on VIDEO_570 */
2687         TP_ACPI_VIDEO_S_LCD = 0x01,     /* LCD output enabled */
2688         TP_ACPI_VIDEO_S_CRT = 0x02,     /* CRT output enabled */
2689         TP_ACPI_VIDEO_S_DVI = 0x08,     /* DVI output enabled */
2690 };
2691
2692 enum {  /* TPACPI_VIDEO_570 constants */
2693         TP_ACPI_VIDEO_570_PHSCMD = 0x87,        /* unknown magic constant :( */
2694         TP_ACPI_VIDEO_570_PHSMASK = 0x03,       /* PHS bits that map to
2695                                                  * video_status_flags */
2696         TP_ACPI_VIDEO_570_PHS2CMD = 0x8b,       /* unknown magic constant :( */
2697         TP_ACPI_VIDEO_570_PHS2SET = 0x80,       /* unknown magic constant :( */
2698 };
2699
2700 static enum video_access_mode video_supported;
2701 static int video_orig_autosw;
2702
2703 static int video_autosw_get(void);
2704 static int video_autosw_set(int enable);
2705
2706 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",   /* 570 */
2707            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
2708            "\\_SB.PCI0.VID0",   /* 770e */
2709            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
2710            "\\_SB.PCI0.AGP.VID",        /* all others */
2711            );                           /* R30, R31 */
2712
2713 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");       /* G41 */
2714
2715 static int __init video_init(struct ibm_init_struct *iibm)
2716 {
2717         int ivga;
2718
2719         vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2720
2721         TPACPI_ACPIHANDLE_INIT(vid);
2722         TPACPI_ACPIHANDLE_INIT(vid2);
2723
2724         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2725                 /* G41, assume IVGA doesn't change */
2726                 vid_handle = vid2_handle;
2727
2728         if (!vid_handle)
2729                 /* video switching not supported on R30, R31 */
2730                 video_supported = TPACPI_VIDEO_NONE;
2731         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2732                 /* 570 */
2733                 video_supported = TPACPI_VIDEO_570;
2734         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2735                 /* 600e/x, 770e, 770x */
2736                 video_supported = TPACPI_VIDEO_770;
2737         else
2738                 /* all others */
2739                 video_supported = TPACPI_VIDEO_NEW;
2740
2741         vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2742                 str_supported(video_supported != TPACPI_VIDEO_NONE),
2743                 video_supported);
2744
2745         return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2746 }
2747
2748 static void video_exit(void)
2749 {
2750         dbg_printk(TPACPI_DBG_EXIT,
2751                    "restoring original video autoswitch mode\n");
2752         if (video_autosw_set(video_orig_autosw))
2753                 printk(TPACPI_ERR "error while trying to restore original "
2754                         "video autoswitch mode\n");
2755 }
2756
2757 static int video_outputsw_get(void)
2758 {
2759         int status = 0;
2760         int i;
2761
2762         switch (video_supported) {
2763         case TPACPI_VIDEO_570:
2764                 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2765                                  TP_ACPI_VIDEO_570_PHSCMD))
2766                         return -EIO;
2767                 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2768                 break;
2769         case TPACPI_VIDEO_770:
2770                 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2771                         return -EIO;
2772                 if (i)
2773                         status |= TP_ACPI_VIDEO_S_LCD;
2774                 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2775                         return -EIO;
2776                 if (i)
2777                         status |= TP_ACPI_VIDEO_S_CRT;
2778                 break;
2779         case TPACPI_VIDEO_NEW:
2780                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2781                     !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2782                         return -EIO;
2783                 if (i)
2784                         status |= TP_ACPI_VIDEO_S_CRT;
2785
2786                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2787                     !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2788                         return -EIO;
2789                 if (i)
2790                         status |= TP_ACPI_VIDEO_S_LCD;
2791                 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2792                         return -EIO;
2793                 if (i)
2794                         status |= TP_ACPI_VIDEO_S_DVI;
2795                 break;
2796         default:
2797                 return -ENOSYS;
2798         }
2799
2800         return status;
2801 }
2802
2803 static int video_outputsw_set(int status)
2804 {
2805         int autosw;
2806         int res = 0;
2807
2808         switch (video_supported) {
2809         case TPACPI_VIDEO_570:
2810                 res = acpi_evalf(NULL, NULL,
2811                                  "\\_SB.PHS2", "vdd",
2812                                  TP_ACPI_VIDEO_570_PHS2CMD,
2813                                  status | TP_ACPI_VIDEO_570_PHS2SET);
2814                 break;
2815         case TPACPI_VIDEO_770:
2816                 autosw = video_autosw_get();
2817                 if (autosw < 0)
2818                         return autosw;
2819
2820                 res = video_autosw_set(1);
2821                 if (res)
2822                         return res;
2823                 res = acpi_evalf(vid_handle, NULL,
2824                                  "ASWT", "vdd", status * 0x100, 0);
2825                 if (!autosw && video_autosw_set(autosw)) {
2826                         printk(TPACPI_ERR
2827                                "video auto-switch left enabled due to error\n");
2828                         return -EIO;
2829                 }
2830                 break;
2831         case TPACPI_VIDEO_NEW:
2832                 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2833                       acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2834                 break;
2835         default:
2836                 return -ENOSYS;
2837         }
2838
2839         return (res)? 0 : -EIO;
2840 }
2841
2842 static int video_autosw_get(void)
2843 {
2844         int autosw = 0;
2845
2846         switch (video_supported) {
2847         case TPACPI_VIDEO_570:
2848                 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2849                         return -EIO;
2850                 break;
2851         case TPACPI_VIDEO_770:
2852         case TPACPI_VIDEO_NEW:
2853                 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2854                         return -EIO;
2855                 break;
2856         default:
2857                 return -ENOSYS;
2858         }
2859
2860         return autosw & 1;
2861 }
2862
2863 static int video_autosw_set(int enable)
2864 {
2865         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2866                 return -EIO;
2867         return 0;
2868 }
2869
2870 static int video_outputsw_cycle(void)
2871 {
2872         int autosw = video_autosw_get();
2873         int res;
2874
2875         if (autosw < 0)
2876                 return autosw;
2877
2878         switch (video_supported) {
2879         case TPACPI_VIDEO_570:
2880                 res = video_autosw_set(1);
2881                 if (res)
2882                         return res;
2883                 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2884                 break;
2885         case TPACPI_VIDEO_770:
2886         case TPACPI_VIDEO_NEW:
2887                 res = video_autosw_set(1);
2888                 if (res)
2889                         return res;
2890                 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2891                 break;
2892         default:
2893                 return -ENOSYS;
2894         }
2895         if (!autosw && video_autosw_set(autosw)) {
2896                 printk(TPACPI_ERR
2897                        "video auto-switch left enabled due to error\n");
2898                 return -EIO;
2899         }
2900
2901         return (res)? 0 : -EIO;
2902 }
2903
2904 static int video_expand_toggle(void)
2905 {
2906         switch (video_supported) {
2907         case TPACPI_VIDEO_570:
2908                 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2909                         0 : -EIO;
2910         case TPACPI_VIDEO_770:
2911                 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2912                         0 : -EIO;
2913         case TPACPI_VIDEO_NEW:
2914                 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2915                         0 : -EIO;
2916         default:
2917                 return -ENOSYS;
2918         }
2919         /* not reached */
2920 }
2921
2922 static int video_read(char *p)
2923 {
2924         int status, autosw;
2925         int len = 0;
2926
2927         if (video_supported == TPACPI_VIDEO_NONE) {
2928                 len += sprintf(p + len, "status:\t\tnot supported\n");
2929                 return len;
2930         }
2931
2932         status = video_outputsw_get();
2933         if (status < 0)
2934                 return status;
2935
2936         autosw = video_autosw_get();
2937         if (autosw < 0)
2938                 return autosw;
2939
2940         len += sprintf(p + len, "status:\t\tsupported\n");
2941         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2942         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2943         if (video_supported == TPACPI_VIDEO_NEW)
2944                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2945         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2946         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2947         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2948         if (video_supported == TPACPI_VIDEO_NEW)
2949                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2950         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2951         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2952
2953         return len;
2954 }
2955
2956 static int video_write(char *buf)
2957 {
2958         char *cmd;
2959         int enable, disable, status;
2960         int res;
2961
2962         if (video_supported == TPACPI_VIDEO_NONE)
2963                 return -ENODEV;
2964
2965         enable = 0;
2966         disable = 0;
2967
2968         while ((cmd = next_cmd(&buf))) {
2969                 if (strlencmp(cmd, "lcd_enable") == 0) {
2970                         enable |= TP_ACPI_VIDEO_S_LCD;
2971                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2972                         disable |= TP_ACPI_VIDEO_S_LCD;
2973                 } else if (strlencmp(cmd, "crt_enable") == 0) {
2974                         enable |= TP_ACPI_VIDEO_S_CRT;
2975                 } else if (strlencmp(cmd, "crt_disable") == 0) {
2976                         disable |= TP_ACPI_VIDEO_S_CRT;
2977                 } else if (video_supported == TPACPI_VIDEO_NEW &&
2978                            strlencmp(cmd, "dvi_enable") == 0) {
2979                         enable |= TP_ACPI_VIDEO_S_DVI;
2980                 } else if (video_supported == TPACPI_VIDEO_NEW &&
2981                            strlencmp(cmd, "dvi_disable") == 0) {
2982                         disable |= TP_ACPI_VIDEO_S_DVI;
2983                 } else if (strlencmp(cmd, "auto_enable") == 0) {
2984                         res = video_autosw_set(1);
2985                         if (res)
2986                                 return res;
2987                 } else if (strlencmp(cmd, "auto_disable") == 0) {
2988                         res = video_autosw_set(0);
2989                         if (res)
2990                                 return res;
2991                 } else if (strlencmp(cmd, "video_switch") == 0) {
2992                         res = video_outputsw_cycle();
2993                         if (res)
2994                                 return res;
2995                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2996                         res = video_expand_toggle();
2997                         if (res)
2998                                 return res;
2999                 } else
3000                         return -EINVAL;
3001         }
3002
3003         if (enable || disable) {
3004                 status = video_outputsw_get();
3005                 if (status < 0)
3006                         return status;
3007                 res = video_outputsw_set((status & ~disable) | enable);
3008                 if (res)
3009                         return res;
3010         }
3011
3012         return 0;
3013 }
3014
3015 static struct ibm_struct video_driver_data = {
3016         .name = "video",
3017         .read = video_read,
3018         .write = video_write,
3019         .exit = video_exit,
3020 };
3021
3022 /*************************************************************************
3023  * Light (thinklight) subdriver
3024  */
3025
3026 TPACPI_HANDLE(lght, root, "\\LGHT");    /* A21e, A2xm/p, T20-22, X20-21 */
3027 TPACPI_HANDLE(ledb, ec, "LEDB");                /* G4x */
3028
3029 static int __init light_init(struct ibm_init_struct *iibm)
3030 {
3031         vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3032
3033         TPACPI_ACPIHANDLE_INIT(ledb);
3034         TPACPI_ACPIHANDLE_INIT(lght);
3035         TPACPI_ACPIHANDLE_INIT(cmos);
3036
3037         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3038         tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3039
3040         if (tp_features.light)
3041                 /* light status not supported on
3042                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3043                 tp_features.light_status =
3044                         acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3045
3046         vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3047                 str_supported(tp_features.light));
3048
3049         return (tp_features.light)? 0 : 1;
3050 }
3051
3052 static int light_read(char *p)
3053 {
3054         int len = 0;
3055         int status = 0;
3056
3057         if (!tp_features.light) {
3058                 len += sprintf(p + len, "status:\t\tnot supported\n");
3059         } else if (!tp_features.light_status) {
3060                 len += sprintf(p + len, "status:\t\tunknown\n");
3061                 len += sprintf(p + len, "commands:\ton, off\n");
3062         } else {
3063                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3064                         return -EIO;
3065                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3066                 len += sprintf(p + len, "commands:\ton, off\n");
3067         }
3068
3069         return len;
3070 }
3071
3072 static int light_write(char *buf)
3073 {
3074         int cmos_cmd, lght_cmd;
3075         char *cmd;
3076         int success;
3077
3078         if (!tp_features.light)
3079                 return -ENODEV;
3080
3081         while ((cmd = next_cmd(&buf))) {
3082                 if (strlencmp(cmd, "on") == 0) {
3083                         cmos_cmd = 0x0c;
3084                         lght_cmd = 1;
3085                 } else if (strlencmp(cmd, "off") == 0) {
3086                         cmos_cmd = 0x0d;
3087                         lght_cmd = 0;
3088                 } else
3089                         return -EINVAL;
3090
3091                 success = cmos_handle ?
3092                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3093                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3094                 if (!success)
3095                         return -EIO;
3096         }
3097
3098         return 0;
3099 }
3100
3101 static struct ibm_struct light_driver_data = {
3102         .name = "light",
3103         .read = light_read,
3104         .write = light_write,
3105 };
3106
3107 /*************************************************************************
3108  * Dock subdriver
3109  */
3110
3111 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3112
3113 static void dock_notify(struct ibm_struct *ibm, u32 event);
3114 static int dock_read(char *p);
3115 static int dock_write(char *buf);
3116
3117 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3118            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3119            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
3120            "\\_SB.PCI.ISA.SLCE",        /* 570 */
3121     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3122
3123 /* don't list other alternatives as we install a notify handler on the 570 */
3124 TPACPI_HANDLE(pci, root, "\\_SB.PCI");  /* 570 */
3125
3126 static const struct acpi_device_id ibm_pci_device_ids[] = {
3127         {PCI_ROOT_HID_STRING, 0},
3128         {"", 0},
3129 };
3130
3131 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3132         {
3133          .notify = dock_notify,
3134          .handle = &dock_handle,
3135          .type = ACPI_SYSTEM_NOTIFY,
3136         },
3137         {
3138         /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3139          * We just use it to get notifications of dock hotplug
3140          * in very old thinkpads */
3141          .hid = ibm_pci_device_ids,
3142          .notify = dock_notify,
3143          .handle = &pci_handle,
3144          .type = ACPI_SYSTEM_NOTIFY,
3145         },
3146 };
3147
3148 static struct ibm_struct dock_driver_data[2] = {
3149         {
3150          .name = "dock",
3151          .read = dock_read,
3152          .write = dock_write,
3153          .acpi = &ibm_dock_acpidriver[0],
3154         },
3155         {
3156          .name = "dock",
3157          .acpi = &ibm_dock_acpidriver[1],
3158         },
3159 };
3160
3161 #define dock_docked() (_sta(dock_handle) & 1)
3162
3163 static int __init dock_init(struct ibm_init_struct *iibm)
3164 {
3165         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3166
3167         TPACPI_ACPIHANDLE_INIT(dock);
3168
3169         vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3170                 str_supported(dock_handle != NULL));
3171
3172         return (dock_handle)? 0 : 1;
3173 }
3174
3175 static int __init dock_init2(struct ibm_init_struct *iibm)
3176 {
3177         int dock2_needed;
3178
3179         vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3180
3181         if (dock_driver_data[0].flags.acpi_driver_registered &&
3182             dock_driver_data[0].flags.acpi_notify_installed) {
3183                 TPACPI_ACPIHANDLE_INIT(pci);
3184                 dock2_needed = (pci_handle != NULL);
3185                 vdbg_printk(TPACPI_DBG_INIT,
3186                             "dock PCI handler for the TP 570 is %s\n",
3187                             str_supported(dock2_needed));
3188         } else {
3189                 vdbg_printk(TPACPI_DBG_INIT,
3190                 "dock subdriver part 2 not required\n");