Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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