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