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