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