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