Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / platform / x86 / thinkpad_acpi.c
index a463fd7..3910f2f 100644 (file)
@@ -22,7 +22,7 @@
  */
 
 #define TPACPI_VERSION "0.23"
-#define TPACPI_SYSFS_VERSION 0x020400
+#define TPACPI_SYSFS_VERSION 0x020500
 
 /*
  *  Changelog:
@@ -145,6 +145,51 @@ enum {
        TP_ACPI_WGSV_STATE_UWBPWR       = 0x0020, /* UWB radio enabled */
 };
 
+/* HKEY events */
+enum tpacpi_hkey_event_t {
+       /* Hotkey-related */
+       TP_HKEY_EV_HOTKEY_BASE          = 0x1001, /* first hotkey (FN+F1) */
+       TP_HKEY_EV_BRGHT_UP             = 0x1010, /* Brightness up */
+       TP_HKEY_EV_BRGHT_DOWN           = 0x1011, /* Brightness down */
+       TP_HKEY_EV_VOL_UP               = 0x1015, /* Volume up or unmute */
+       TP_HKEY_EV_VOL_DOWN             = 0x1016, /* Volume down or unmute */
+       TP_HKEY_EV_VOL_MUTE             = 0x1017, /* Mixer output mute */
+
+       /* Reasons for waking up from S3/S4 */
+       TP_HKEY_EV_WKUP_S3_UNDOCK       = 0x2304, /* undock requested, S3 */
+       TP_HKEY_EV_WKUP_S4_UNDOCK       = 0x2404, /* undock requested, S4 */
+       TP_HKEY_EV_WKUP_S3_BAYEJ        = 0x2305, /* bay ejection req, S3 */
+       TP_HKEY_EV_WKUP_S4_BAYEJ        = 0x2405, /* bay ejection req, S4 */
+       TP_HKEY_EV_WKUP_S3_BATLOW       = 0x2313, /* battery empty, S3 */
+       TP_HKEY_EV_WKUP_S4_BATLOW       = 0x2413, /* battery empty, S4 */
+
+       /* Auto-sleep after eject request */
+       TP_HKEY_EV_BAYEJ_ACK            = 0x3003, /* bay ejection complete */
+       TP_HKEY_EV_UNDOCK_ACK           = 0x4003, /* undock complete */
+
+       /* Misc bay events */
+       TP_HKEY_EV_OPTDRV_EJ            = 0x3006, /* opt. drive tray ejected */
+
+       /* User-interface events */
+       TP_HKEY_EV_LID_CLOSE            = 0x5001, /* laptop lid closed */
+       TP_HKEY_EV_LID_OPEN             = 0x5002, /* laptop lid opened */
+       TP_HKEY_EV_TABLET_TABLET        = 0x5009, /* tablet swivel up */
+       TP_HKEY_EV_TABLET_NOTEBOOK      = 0x500a, /* tablet swivel down */
+       TP_HKEY_EV_PEN_INSERTED         = 0x500b, /* tablet pen inserted */
+       TP_HKEY_EV_PEN_REMOVED          = 0x500c, /* tablet pen removed */
+       TP_HKEY_EV_BRGHT_CHANGED        = 0x5010, /* backlight control event */
+
+       /* Thermal events */
+       TP_HKEY_EV_ALARM_BAT_HOT        = 0x6011, /* battery too hot */
+       TP_HKEY_EV_ALARM_BAT_XHOT       = 0x6012, /* battery critically hot */
+       TP_HKEY_EV_ALARM_SENSOR_HOT     = 0x6021, /* sensor too hot */
+       TP_HKEY_EV_ALARM_SENSOR_XHOT    = 0x6022, /* sensor critically hot */
+       TP_HKEY_EV_THM_TABLE_CHANGED    = 0x6030, /* thermal table changed */
+
+       /* Misc */
+       TP_HKEY_EV_RFKILL_CHANGED       = 0x7000, /* rfkill switch changed */
+};
+
 /****************************************************************************
  * Main driver
  */
@@ -239,12 +284,6 @@ struct ibm_init_struct {
 };
 
 static struct {
-#ifdef CONFIG_THINKPAD_ACPI_BAY
-       u32 bay_status:1;
-       u32 bay_eject:1;
-       u32 bay_status2:1;
-       u32 bay_eject2:1;
-#endif
        u32 bluetooth:1;
        u32 hotkey:1;
        u32 hotkey_mask:1;
@@ -589,18 +628,6 @@ static int acpi_ec_write(int i, u8 v)
        return 1;
 }
 
-#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
-static int _sta(acpi_handle handle)
-{
-       int status;
-
-       if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
-               status = 0;
-
-       return status;
-}
-#endif
-
 static int issue_thinkpad_cmos_command(int cmos_cmd)
 {
        if (!cmos_handle)
@@ -784,6 +811,8 @@ static int dispatch_procfs_write(struct file *file,
 
        if (!ibm || !ibm->write)
                return -EINVAL;
+       if (count > PAGE_SIZE - 2)
+               return -EINVAL;
 
        kernbuf = kmalloc(count + 2, GFP_KERNEL);
        if (!kernbuf)
@@ -1294,6 +1323,7 @@ static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
        tp_rfk = tpacpi_rfkill_switches[id];
        if (tp_rfk) {
                rfkill_unregister(tp_rfk->rfkill);
+               rfkill_destroy(tp_rfk->rfkill);
                tpacpi_rfkill_switches[id] = NULL;
                kfree(tp_rfk);
        }
@@ -1617,6 +1647,196 @@ static void tpacpi_remove_driver_attributes(struct device_driver *drv)
 #endif
 }
 
+/*************************************************************************
+ * Firmware Data
+ */
+
+/*
+ * Table of recommended minimum BIOS versions
+ *
+ * Reasons for listing:
+ *    1. Stable BIOS, listed because the unknown ammount of
+ *       bugs and bad ACPI behaviour on older versions
+ *
+ *    2. BIOS or EC fw with known bugs that trigger on Linux
+ *
+ *    3. BIOS with known reduced functionality in older versions
+ *
+ *  We recommend the latest BIOS and EC version.
+ *  We only support the latest BIOS and EC fw version as a rule.
+ *
+ *  Sources: IBM ThinkPad Public Web Documents (update changelogs),
+ *  Information from users in ThinkWiki
+ *
+ *  WARNING: we use this table also to detect that the machine is
+ *  a ThinkPad in some cases, so don't remove entries lightly.
+ */
+
+#define TPV_Q(__v, __id1, __id2, __bv1, __bv2)         \
+       { .vendor       = (__v),                        \
+         .bios         = TPID(__id1, __id2),           \
+         .ec           = TPACPI_MATCH_ANY,             \
+         .quirks       = TPACPI_MATCH_ANY << 16        \
+                         | (__bv1) << 8 | (__bv2) }
+
+#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2,     \
+               __eid1, __eid2, __ev1, __ev2)           \
+       { .vendor       = (__v),                        \
+         .bios         = TPID(__bid1, __bid2),         \
+         .ec           = TPID(__eid1, __eid2),         \
+         .quirks       = (__ev1) << 24 | (__ev2) << 16 \
+                         | (__bv1) << 8 | (__bv2) }
+
+#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
+       TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
+
+#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
+       TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2,        \
+               __bv1, __bv2, __id1, __id2, __ev1, __ev2)
+
+#define TPV_QI2(__bid1, __bid2, __bv1, __bv2,          \
+               __eid1, __eid2, __ev1, __ev2)           \
+       TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2,      \
+               __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
+
+#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
+       TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
+
+#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
+       TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2,     \
+               __bv1, __bv2, __id1, __id2, __ev1, __ev2)
+
+#define TPV_QL2(__bid1, __bid2, __bv1, __bv2,          \
+               __eid1, __eid2, __ev1, __ev2)           \
+       TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2,   \
+               __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
+
+static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
+       /*  Numeric models ------------------ */
+       /*      FW MODEL   BIOS VERS          */
+       TPV_QI0('I', 'M',  '6', '5'),            /* 570 */
+       TPV_QI0('I', 'U',  '2', '6'),            /* 570E */
+       TPV_QI0('I', 'B',  '5', '4'),            /* 600 */
+       TPV_QI0('I', 'H',  '4', '7'),            /* 600E */
+       TPV_QI0('I', 'N',  '3', '6'),            /* 600E */
+       TPV_QI0('I', 'T',  '5', '5'),            /* 600X */
+       TPV_QI0('I', 'D',  '4', '8'),            /* 770, 770E, 770ED */
+       TPV_QI0('I', 'I',  '4', '2'),            /* 770X */
+       TPV_QI0('I', 'O',  '2', '3'),            /* 770Z */
+
+       /* A-series ------------------------- */
+       /*      FW MODEL   BIOS VERS  EC VERS */
+       TPV_QI0('I', 'W',  '5', '9'),            /* A20m */
+       TPV_QI0('I', 'V',  '6', '9'),            /* A20p */
+       TPV_QI0('1', '0',  '2', '6'),            /* A21e, A22e */
+       TPV_QI0('K', 'U',  '3', '6'),            /* A21e */
+       TPV_QI0('K', 'X',  '3', '6'),            /* A21m, A22m */
+       TPV_QI0('K', 'Y',  '3', '8'),            /* A21p, A22p */
+       TPV_QI0('1', 'B',  '1', '7'),            /* A22e */
+       TPV_QI0('1', '3',  '2', '0'),            /* A22m */
+       TPV_QI0('1', 'E',  '7', '3'),            /* A30/p (0) */
+       TPV_QI1('1', 'G',  '4', '1',  '1', '7'), /* A31/p (0) */
+       TPV_QI1('1', 'N',  '1', '6',  '0', '7'), /* A31/p (0) */
+
+       /* G-series ------------------------- */
+       /*      FW MODEL   BIOS VERS          */
+       TPV_QI0('1', 'T',  'A', '6'),            /* G40 */
+       TPV_QI0('1', 'X',  '5', '7'),            /* G41 */
+
+       /* R-series, T-series --------------- */
+       /*      FW MODEL   BIOS VERS  EC VERS */
+       TPV_QI0('1', 'C',  'F', '0'),            /* R30 */
+       TPV_QI0('1', 'F',  'F', '1'),            /* R31 */
+       TPV_QI0('1', 'M',  '9', '7'),            /* R32 */
+       TPV_QI0('1', 'O',  '6', '1'),            /* R40 */
+       TPV_QI0('1', 'P',  '6', '5'),            /* R40 */
+       TPV_QI0('1', 'S',  '7', '0'),            /* R40e */
+       TPV_QI1('1', 'R',  'D', 'R',  '7', '1'), /* R50/p, R51,
+                                                   T40/p, T41/p, T42/p (1) */
+       TPV_QI1('1', 'V',  '7', '1',  '2', '8'), /* R50e, R51 (1) */
+       TPV_QI1('7', '8',  '7', '1',  '0', '6'), /* R51e (1) */
+       TPV_QI1('7', '6',  '6', '9',  '1', '6'), /* R52 (1) */
+       TPV_QI1('7', '0',  '6', '9',  '2', '8'), /* R52, T43 (1) */
+
+       TPV_QI0('I', 'Y',  '6', '1'),            /* T20 */
+       TPV_QI0('K', 'Z',  '3', '4'),            /* T21 */
+       TPV_QI0('1', '6',  '3', '2'),            /* T22 */
+       TPV_QI1('1', 'A',  '6', '4',  '2', '3'), /* T23 (0) */
+       TPV_QI1('1', 'I',  '7', '1',  '2', '0'), /* T30 (0) */
+       TPV_QI1('1', 'Y',  '6', '5',  '2', '9'), /* T43/p (1) */
+
+       TPV_QL1('7', '9',  'E', '3',  '5', '0'), /* T60/p */
+       TPV_QL1('7', 'C',  'D', '2',  '2', '2'), /* R60, R60i */
+       TPV_QL0('7', 'E',  'D', '0'),            /* R60e, R60i */
+
+       /*      BIOS FW    BIOS VERS  EC FW     EC VERS */
+       TPV_QI2('1', 'W',  '9', '0',  '1', 'V', '2', '8'), /* R50e (1) */
+       TPV_QL2('7', 'I',  '3', '4',  '7', '9', '5', '0'), /* T60/p wide */
+
+       /* X-series ------------------------- */
+       /*      FW MODEL   BIOS VERS  EC VERS */
+       TPV_QI0('I', 'Z',  '9', 'D'),            /* X20, X21 */
+       TPV_QI0('1', 'D',  '7', '0'),            /* X22, X23, X24 */
+       TPV_QI1('1', 'K',  '4', '8',  '1', '8'), /* X30 (0) */
+       TPV_QI1('1', 'Q',  '9', '7',  '2', '3'), /* X31, X32 (0) */
+       TPV_QI1('1', 'U',  'D', '3',  'B', '2'), /* X40 (0) */
+       TPV_QI1('7', '4',  '6', '4',  '2', '7'), /* X41 (0) */
+       TPV_QI1('7', '5',  '6', '0',  '2', '0'), /* X41t (0) */
+
+       TPV_QL0('7', 'B',  'D', '7'),            /* X60/s */
+       TPV_QL0('7', 'J',  '3', '0'),            /* X60t */
+
+       /* (0) - older versions lack DMI EC fw string and functionality */
+       /* (1) - older versions known to lack functionality */
+};
+
+#undef TPV_QL1
+#undef TPV_QL0
+#undef TPV_QI2
+#undef TPV_QI1
+#undef TPV_QI0
+#undef TPV_Q_X
+#undef TPV_Q
+
+static void __init tpacpi_check_outdated_fw(void)
+{
+       unsigned long fwvers;
+       u16 ec_version, bios_version;
+
+       fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
+                               ARRAY_SIZE(tpacpi_bios_version_qtable));
+
+       if (!fwvers)
+               return;
+
+       bios_version = fwvers & 0xffffU;
+       ec_version = (fwvers >> 16) & 0xffffU;
+
+       /* note that unknown versions are set to 0x0000 and we use that */
+       if ((bios_version > thinkpad_id.bios_release) ||
+           (ec_version > thinkpad_id.ec_release &&
+                               ec_version != TPACPI_MATCH_ANY)) {
+               /*
+                * The changelogs would let us track down the exact
+                * reason, but it is just too much of a pain to track
+                * it.  We only list BIOSes that are either really
+                * broken, or really stable to begin with, so it is
+                * best if the user upgrades the firmware anyway.
+                */
+               printk(TPACPI_WARN
+                       "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
+               printk(TPACPI_WARN
+                       "WARNING: This firmware may be missing critical bug "
+                       "fixes and/or important features\n");
+       }
+}
+
+static bool __init tpacpi_is_fw_known(void)
+{
+       return tpacpi_check_quirks(tpacpi_bios_version_qtable,
+                       ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
+}
+
 /****************************************************************************
  ****************************************************************************
  *
@@ -1650,6 +1870,7 @@ static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
                        (thinkpad_id.nummodel_str) ?
                                thinkpad_id.nummodel_str : "unknown");
 
+       tpacpi_check_outdated_fw();
        return 0;
 }
 
@@ -1672,6 +1893,27 @@ static struct ibm_struct thinkpad_acpi_driver_data = {
  * Hotkey subdriver
  */
 
+/*
+ * ThinkPad firmware event model
+ *
+ * The ThinkPad firmware has two main event interfaces: normal ACPI
+ * notifications (which follow the ACPI standard), and a private event
+ * interface.
+ *
+ * The private event interface also issues events for the hotkeys.  As
+ * the driver gained features, the event handling code ended up being
+ * built around the hotkey subdriver.  This will need to be refactored
+ * to a more formal event API eventually.
+ *
+ * Some "hotkeys" are actually supposed to be used as event reports,
+ * such as "brightness has changed", "volume has changed", depending on
+ * the ThinkPad model and how the firmware is operating.
+ *
+ * Unlike other classes, hotkey-class events have mask/unmask control on
+ * non-ancient firmware.  However, how it behaves changes a lot with the
+ * firmware model and version.
+ */
+
 enum { /* hot key scan codes (derived from ACPI DSDT) */
        TP_ACPI_HOTKEYSCAN_FNF1         = 0,
        TP_ACPI_HOTKEYSCAN_FNF2,
@@ -1699,7 +1941,7 @@ enum {    /* hot key scan codes (derived from ACPI DSDT) */
        TP_ACPI_HOTKEYSCAN_THINKPAD,
 };
 
-enum { /* Keys available through NVRAM polling */
+enum { /* Keys/events available through NVRAM polling */
        TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
        TPACPI_HKEY_NVRAM_GOOD_MASK  = 0x00fb8000U,
 };
@@ -1747,16 +1989,50 @@ struct tp_nvram_state {
        u8 volume_level;
 };
 
+/* kthread for the hotkey poller */
 static struct task_struct *tpacpi_hotkey_task;
-static u32 hotkey_source_mask;         /* bit mask 0=ACPI,1=NVRAM */
-static int hotkey_poll_freq = 10;      /* Hz */
+
+/* Acquired while the poller kthread is running, use to sync start/stop */
 static struct mutex hotkey_thread_mutex;
+
+/*
+ * Acquire mutex to write poller control variables as an
+ * atomic block.
+ *
+ * Increment hotkey_config_change when changing them if you
+ * want the kthread to forget old state.
+ *
+ * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
+ */
 static struct mutex hotkey_thread_data_mutex;
 static unsigned int hotkey_config_change;
 
+/*
+ * hotkey poller control variables
+ *
+ * Must be atomic or readers will also need to acquire mutex
+ *
+ * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
+ * should be used only when the changes need to be taken as
+ * a block, OR when one needs to force the kthread to forget
+ * old state.
+ */
+static u32 hotkey_source_mask;         /* bit mask 0=ACPI,1=NVRAM */
+static unsigned int hotkey_poll_freq = 10; /* Hz */
+
+#define HOTKEY_CONFIG_CRITICAL_START \
+       do { \
+               mutex_lock(&hotkey_thread_data_mutex); \
+               hotkey_config_change++; \
+       } while (0);
+#define HOTKEY_CONFIG_CRITICAL_END \
+       mutex_unlock(&hotkey_thread_data_mutex);
+
 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
 
 #define hotkey_source_mask 0U
+#define HOTKEY_CONFIG_CRITICAL_START
+#define HOTKEY_CONFIG_CRITICAL_END
 
 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
 
@@ -1770,10 +2046,12 @@ static enum {   /* Reasons for waking up */
 
 static int hotkey_autosleep_ack;
 
-static u32 hotkey_orig_mask;
-static u32 hotkey_all_mask;
-static u32 hotkey_reserved_mask;
-static u32 hotkey_mask;
+static u32 hotkey_orig_mask;           /* events the BIOS had enabled */
+static u32 hotkey_all_mask;            /* all events supported in fw */
+static u32 hotkey_reserved_mask;       /* events better left disabled */
+static u32 hotkey_driver_mask;         /* events needed by the driver */
+static u32 hotkey_user_mask;           /* events visible to userspace */
+static u32 hotkey_acpi_mask;           /* events enabled in firmware */
 
 static unsigned int hotkey_report_mode;
 
@@ -1781,18 +2059,8 @@ static u16 *hotkey_keycode_map;
 
 static struct attribute_set *hotkey_dev_attributes;
 
-#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
-#define HOTKEY_CONFIG_CRITICAL_START \
-       do { \
-               mutex_lock(&hotkey_thread_data_mutex); \
-               hotkey_config_change++; \
-       } while (0);
-#define HOTKEY_CONFIG_CRITICAL_END \
-       mutex_unlock(&hotkey_thread_data_mutex);
-#else
-#define HOTKEY_CONFIG_CRITICAL_START
-#define HOTKEY_CONFIG_CRITICAL_END
-#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
+static void tpacpi_driver_event(const unsigned int hkey_event);
+static void hotkey_driver_event(const unsigned int scancode);
 
 /* HKEY.MHKG() return bits */
 #define TP_HOTKEY_TABLET_MASK (1 << 3)
@@ -1828,22 +2096,53 @@ static int hotkey_get_tablet_mode(int *status)
 }
 
 /*
+ * Reads current event mask from firmware, and updates
+ * hotkey_acpi_mask accordingly.  Also resets any bits
+ * from hotkey_user_mask that are unavailable to be
+ * delivered (shadow requirement of the userspace ABI).
+ *
  * Call with hotkey_mutex held
  */
 static int hotkey_mask_get(void)
 {
-       u32 m = 0;
-
        if (tp_features.hotkey_mask) {
+               u32 m = 0;
+
                if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
                        return -EIO;
+
+               hotkey_acpi_mask = m;
+       } else {
+               /* no mask support doesn't mean no event support... */
+               hotkey_acpi_mask = hotkey_all_mask;
        }
-       hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
+
+       /* sync userspace-visible mask */
+       hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
 
        return 0;
 }
 
+void static hotkey_mask_warn_incomplete_mask(void)
+{
+       /* log only what the user can fix... */
+       const u32 wantedmask = hotkey_driver_mask &
+               ~(hotkey_acpi_mask | hotkey_source_mask) &
+               (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
+
+       if (wantedmask)
+               printk(TPACPI_NOTICE
+                       "required events 0x%08x not enabled!\n",
+                       wantedmask);
+}
+
 /*
+ * Set the firmware mask when supported
+ *
+ * Also calls hotkey_mask_get to update hotkey_acpi_mask.
+ *
+ * NOTE: does not set bits in hotkey_user_mask, but may reset them.
+ *
  * Call with hotkey_mutex held
  */
 static int hotkey_mask_set(u32 mask)
@@ -1851,66 +2150,98 @@ static int hotkey_mask_set(u32 mask)
        int i;
        int rc = 0;
 
-       if (tp_features.hotkey_mask) {
-               if (!tp_warned.hotkey_mask_ff &&
-                   (mask == 0xffff || mask == 0xffffff ||
-                    mask == 0xffffffff)) {
-                       tp_warned.hotkey_mask_ff = 1;
-                       printk(TPACPI_NOTICE
-                              "setting the hotkey mask to 0x%08x is likely "
-                              "not the best way to go about it\n", mask);
-                       printk(TPACPI_NOTICE
-                              "please consider using the driver defaults, "
-                              "and refer to up-to-date thinkpad-acpi "
-                              "documentation\n");
-               }
+       const u32 fwmask = mask & ~hotkey_source_mask;
 
-               HOTKEY_CONFIG_CRITICAL_START
+       if (tp_features.hotkey_mask) {
                for (i = 0; i < 32; i++) {
-                       u32 m = 1 << i;
-                       /* enable in firmware mask only keys not in NVRAM
-                        * mode, but enable the key in the cached hotkey_mask
-                        * regardless of mode, or the key will end up
-                        * disabled by hotkey_mask_get() */
                        if (!acpi_evalf(hkey_handle,
                                        NULL, "MHKM", "vdd", i + 1,
-                                       !!((mask & ~hotkey_source_mask) & m))) {
+                                       !!(mask & (1 << i)))) {
                                rc = -EIO;
                                break;
-                       } else {
-                               hotkey_mask = (hotkey_mask & ~m) | (mask & m);
                        }
                }
-               HOTKEY_CONFIG_CRITICAL_END
+       }
 
-               /* hotkey_mask_get must be called unconditionally below */
-               if (!hotkey_mask_get() && !rc &&
-                   (hotkey_mask & ~hotkey_source_mask) !=
-                    (mask & ~hotkey_source_mask)) {
-                       printk(TPACPI_NOTICE
-                              "requested hot key mask 0x%08x, but "
-                              "firmware forced it to 0x%08x\n",
-                              mask, hotkey_mask);
-               }
-       } else {
-#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
-               HOTKEY_CONFIG_CRITICAL_START
-               hotkey_mask = mask & hotkey_source_mask;
-               HOTKEY_CONFIG_CRITICAL_END
-               hotkey_mask_get();
-               if (hotkey_mask != mask) {
-                       printk(TPACPI_NOTICE
-                              "requested hot key mask 0x%08x, "
-                              "forced to 0x%08x (NVRAM poll mask is "
-                              "0x%08x): no firmware mask support\n",
-                              mask, hotkey_mask, hotkey_source_mask);
-               }
-#else
-               hotkey_mask_get();
-               rc = -ENXIO;
-#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
+       /*
+        * We *must* make an inconditional call to hotkey_mask_get to
+        * refresh hotkey_acpi_mask and update hotkey_user_mask
+        *
+        * Take the opportunity to also log when we cannot _enable_
+        * a given event.
+        */
+       if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
+               printk(TPACPI_NOTICE
+                      "asked for hotkey mask 0x%08x, but "
+                      "firmware forced it to 0x%08x\n",
+                      fwmask, hotkey_acpi_mask);
        }
 
+       hotkey_mask_warn_incomplete_mask();
+
+       return rc;
+}
+
+/*
+ * Sets hotkey_user_mask and tries to set the firmware mask
+ *
+ * Call with hotkey_mutex held
+ */
+static int hotkey_user_mask_set(const u32 mask)
+{
+       int rc;
+
+       /* Give people a chance to notice they are doing something that
+        * is bound to go boom on their users sooner or later */
+       if (!tp_warned.hotkey_mask_ff &&
+           (mask == 0xffff || mask == 0xffffff ||
+            mask == 0xffffffff)) {
+               tp_warned.hotkey_mask_ff = 1;
+               printk(TPACPI_NOTICE
+                      "setting the hotkey mask to 0x%08x is likely "
+                      "not the best way to go about it\n", mask);
+               printk(TPACPI_NOTICE
+                      "please consider using the driver defaults, "
+                      "and refer to up-to-date thinkpad-acpi "
+                      "documentation\n");
+       }
+
+       /* Try to enable what the user asked for, plus whatever we need.
+        * this syncs everything but won't enable bits in hotkey_user_mask */
+       rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
+
+       /* Enable the available bits in hotkey_user_mask */
+       hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
+
+       return rc;
+}
+
+/*
+ * Sets the driver hotkey mask.
+ *
+ * Can be called even if the hotkey subdriver is inactive
+ */
+static int tpacpi_hotkey_driver_mask_set(const u32 mask)
+{
+       int rc;
+
+       /* Do the right thing if hotkey_init has not been called yet */
+       if (!tp_features.hotkey) {
+               hotkey_driver_mask = mask;
+               return 0;
+       }
+
+       mutex_lock(&hotkey_mutex);
+
+       HOTKEY_CONFIG_CRITICAL_START
+       hotkey_driver_mask = mask;
+       hotkey_source_mask |= (mask & ~hotkey_all_mask);
+       HOTKEY_CONFIG_CRITICAL_END
+
+       rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
+                                                       ~hotkey_source_mask);
+       mutex_unlock(&hotkey_mutex);
+
        return rc;
 }
 
@@ -1946,11 +2277,10 @@ static void tpacpi_input_send_tabletsw(void)
        }
 }
 
-static void tpacpi_input_send_key(unsigned int scancode)
+/* Do NOT call without validating scancode first */
+static void tpacpi_input_send_key(const unsigned int scancode)
 {
-       unsigned int keycode;
-
-       keycode = hotkey_keycode_map[scancode];
+       const unsigned int keycode = hotkey_keycode_map[scancode];
 
        if (keycode != KEY_RESERVED) {
                mutex_lock(&tpacpi_inputdev_send_mutex);
@@ -1971,19 +2301,28 @@ static void tpacpi_input_send_key(unsigned int scancode)
        }
 }
 
+/* Do NOT call without validating scancode first */
+static void tpacpi_input_send_key_masked(const unsigned int scancode)
+{
+       hotkey_driver_event(scancode);
+       if (hotkey_user_mask & (1 << scancode))
+               tpacpi_input_send_key(scancode);
+}
+
 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
 
+/* Do NOT call without validating scancode first */
 static void tpacpi_hotkey_send_key(unsigned int scancode)
 {
-       tpacpi_input_send_key(scancode);
+       tpacpi_input_send_key_masked(scancode);
        if (hotkey_report_mode < 2) {
                acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
-                                               0x80, 0x1001 + scancode);
+                               0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
        }
 }
 
-static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
+static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
 {
        u8 d;
 
@@ -2019,21 +2358,24 @@ static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
        }
 }
 
+static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
+                                          struct tp_nvram_state *newn,
+                                          const u32 event_mask)
+{
+
 #define TPACPI_COMPARE_KEY(__scancode, __member) \
        do { \
-               if ((mask & (1 << __scancode)) && \
+               if ((event_mask & (1 << __scancode)) && \
                    oldn->__member != newn->__member) \
-               tpacpi_hotkey_send_key(__scancode); \
+                       tpacpi_hotkey_send_key(__scancode); \
        } while (0)
 
 #define TPACPI_MAY_SEND_KEY(__scancode) \
-       do { if (mask & (1 << __scancode)) \
-               tpacpi_hotkey_send_key(__scancode); } while (0)
+       do { \
+               if (event_mask & (1 << __scancode)) \
+                       tpacpi_hotkey_send_key(__scancode); \
+       } while (0)
 
-static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
-                                          struct tp_nvram_state *newn,
-                                          u32 mask)
-{
        TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
        TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
        TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
@@ -2079,18 +2421,26 @@ static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
                        }
                }
        }
-}
 
 #undef TPACPI_COMPARE_KEY
 #undef TPACPI_MAY_SEND_KEY
+}
 
+/*
+ * Polling driver
+ *
+ * We track all events in hotkey_source_mask all the time, since
+ * most of them are edge-based.  We only issue those requested by
+ * hotkey_user_mask or hotkey_driver_mask, though.
+ */
 static int hotkey_kthread(void *data)
 {
        struct tp_nvram_state s[2];
-       u32 mask;
+       u32 poll_mask, event_mask;
        unsigned int si, so;
        unsigned long t;
        unsigned int change_detector, must_reset;
+       unsigned int poll_freq;
 
        mutex_lock(&hotkey_thread_mutex);
 
@@ -2106,13 +2456,20 @@ static int hotkey_kthread(void *data)
        /* Initial state for compares */
        mutex_lock(&hotkey_thread_data_mutex);
        change_detector = hotkey_config_change;
-       mask = hotkey_source_mask & hotkey_mask;
+       poll_mask = hotkey_source_mask;
+       event_mask = hotkey_source_mask &
+                       (hotkey_driver_mask | hotkey_user_mask);
+       poll_freq = hotkey_poll_freq;
        mutex_unlock(&hotkey_thread_data_mutex);
-       hotkey_read_nvram(&s[so], mask);
+       hotkey_read_nvram(&s[so], poll_mask);
 
-       while (!kthread_should_stop() && hotkey_poll_freq) {
-               if (t == 0)
-                       t = 1000/hotkey_poll_freq;
+       while (!kthread_should_stop()) {
+               if (t == 0) {
+                       if (likely(poll_freq))
+                               t = 1000/poll_freq;
+                       else
+                               t = 100;        /* should never happen... */
+               }
                t = msleep_interruptible(t);
                if (unlikely(kthread_should_stop()))
                        break;
@@ -2127,14 +2484,17 @@ static int hotkey_kthread(void *data)
                        t = 0;
                        change_detector = hotkey_config_change;
                }
-               mask = hotkey_source_mask & hotkey_mask;
+               poll_mask = hotkey_source_mask;
+               event_mask = hotkey_source_mask &
+                               (hotkey_driver_mask | hotkey_user_mask);
+               poll_freq = hotkey_poll_freq;
                mutex_unlock(&hotkey_thread_data_mutex);
 
-               if (likely(mask)) {
-                       hotkey_read_nvram(&s[si], mask);
+               if (likely(poll_mask)) {
+                       hotkey_read_nvram(&s[si], poll_mask);
                        if (likely(si != so)) {
                                hotkey_compare_and_issue_event(&s[so], &s[si],
-                                                               mask);
+                                                               event_mask);
                        }
                }
 
@@ -2147,6 +2507,7 @@ exit:
        return 0;
 }
 
+/* call with hotkey_mutex held */
 static void hotkey_poll_stop_sync(void)
 {
        if (tpacpi_hotkey_task) {
@@ -2163,11 +2524,14 @@ static void hotkey_poll_stop_sync(void)
 }
 
 /* call with hotkey_mutex held */
-static void hotkey_poll_setup(int may_warn)
+static void hotkey_poll_setup(bool may_warn)
 {
-       if ((hotkey_source_mask & hotkey_mask) != 0 &&
-           hotkey_poll_freq > 0 &&
-           (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
+       const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
+       const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
+
+       if (hotkey_poll_freq > 0 &&
+           (poll_driver_mask ||
+            (poll_user_mask && tpacpi_inputdev->users > 0))) {
                if (!tpacpi_hotkey_task) {
                        tpacpi_hotkey_task = kthread_run(hotkey_kthread,
                                        NULL, TPACPI_NVRAM_KTHREAD_NAME);
@@ -2180,26 +2544,36 @@ static void hotkey_poll_setup(int may_warn)
                }
        } else {
                hotkey_poll_stop_sync();
-               if (may_warn &&
-                   hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
+               if (may_warn && (poll_driver_mask || poll_user_mask) &&
+                   hotkey_poll_freq == 0) {
                        printk(TPACPI_NOTICE
-                               "hot keys 0x%08x require polling, "
-                               "which is currently disabled\n",
-                               hotkey_source_mask);
+                               "hot keys 0x%08x and/or events 0x%08x "
+                               "require polling, which is currently "
+                               "disabled\n",
+                               poll_user_mask, poll_driver_mask);
                }
        }
 }
 
-static void hotkey_poll_setup_safe(int may_warn)
+static void hotkey_poll_setup_safe(bool may_warn)
 {
        mutex_lock(&hotkey_mutex);
        hotkey_poll_setup(may_warn);
        mutex_unlock(&hotkey_mutex);
 }
 
+/* call with hotkey_mutex held */
+static void hotkey_poll_set_freq(unsigned int freq)
+{
+       if (!freq)
+               hotkey_poll_stop_sync();
+
+       hotkey_poll_freq = freq;
+}
+
 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
 
-static void hotkey_poll_setup_safe(int __unused)
+static void hotkey_poll_setup_safe(bool __unused)
 {
 }
 
@@ -2217,7 +2591,7 @@ static int hotkey_inputdev_open(struct input_dev *dev)
        case TPACPI_LIFE_EXITING:
                return -EBUSY;
        case TPACPI_LIFE_RUNNING:
-               hotkey_poll_setup_safe(0);
+               hotkey_poll_setup_safe(false);
                return 0;
        }
 
@@ -2229,8 +2603,9 @@ static int hotkey_inputdev_open(struct input_dev *dev)
 static void hotkey_inputdev_close(struct input_dev *dev)
 {
        /* disable hotkey polling when possible */
-       if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
-               hotkey_poll_setup_safe(0);
+       if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
+           !(hotkey_source_mask & hotkey_driver_mask))
+               hotkey_poll_setup_safe(false);
 }
 
 /* sysfs hotkey enable ------------------------------------------------- */
@@ -2277,15 +2652,7 @@ static ssize_t hotkey_mask_show(struct device *dev,
                           struct device_attribute *attr,
                           char *buf)
 {
-       int res;
-
-       if (mutex_lock_killable(&hotkey_mutex))
-               return -ERESTARTSYS;
-       res = hotkey_mask_get();
-       mutex_unlock(&hotkey_mutex);
-
-       return (res)?
-               res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
+       return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
 }
 
 static ssize_t hotkey_mask_store(struct device *dev,
@@ -2301,10 +2668,10 @@ static ssize_t hotkey_mask_store(struct device *dev,
        if (mutex_lock_killable(&hotkey_mutex))
                return -ERESTARTSYS;
 
-       res = hotkey_mask_set(t);
+       res = hotkey_user_mask_set(t);
 
 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
-       hotkey_poll_setup(1);
+       hotkey_poll_setup(true);
 #endif
 
        mutex_unlock(&hotkey_mutex);
@@ -2334,6 +2701,8 @@ static ssize_t hotkey_bios_mask_show(struct device *dev,
                           struct device_attribute *attr,
                           char *buf)
 {
+       printk_deprecated_attribute("hotkey_bios_mask",
+                       "This attribute is useless.");
        return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
 }
 
@@ -2381,6 +2750,8 @@ static ssize_t hotkey_source_mask_store(struct device *dev,
                            const char *buf, size_t count)
 {
        unsigned long t;
+       u32 r_ev;
+       int rc;
 
        if (parse_strtoul(buf, 0xffffffffUL, &t) ||
                ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
@@ -2393,13 +2764,28 @@ static ssize_t hotkey_source_mask_store(struct device *dev,
        hotkey_source_mask = t;
        HOTKEY_CONFIG_CRITICAL_END
 
-       hotkey_poll_setup(1);
+       rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
+                       ~hotkey_source_mask);
+       hotkey_poll_setup(true);
+
+       /* check if events needed by the driver got disabled */
+       r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
+               & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
 
        mutex_unlock(&hotkey_mutex);
 
+       if (rc < 0)
+               printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
+                       "firmware event mask!\n");
+
+       if (r_ev)
+               printk(TPACPI_NOTICE "hotkey_source_mask: "
+                       "some important events were disabled: "
+                       "0x%04x\n", r_ev);
+
        tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
 
-       return count;
+       return (rc < 0) ? rc : count;
 }
 
 static struct device_attribute dev_attr_hotkey_source_mask =
@@ -2426,9 +2812,9 @@ static ssize_t hotkey_poll_freq_store(struct device *dev,
        if (mutex_lock_killable(&hotkey_mutex))
                return -ERESTARTSYS;
 
-       hotkey_poll_freq = t;
+       hotkey_poll_set_freq(t);
+       hotkey_poll_setup(true);
 
-       hotkey_poll_setup(1);
        mutex_unlock(&hotkey_mutex);
 
        tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
@@ -2517,9 +2903,8 @@ static struct device_attribute dev_attr_hotkey_wakeup_reason =
 
 static void hotkey_wakeup_reason_notify_change(void)
 {
-       if (tp_features.hotkey_mask)
-               sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
-                            "wakeup_reason");
+       sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
+                    "wakeup_reason");
 }
 
 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
@@ -2536,9 +2921,8 @@ static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
 
 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
 {
-       if (tp_features.hotkey_mask)
-               sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
-                            "wakeup_hotunplug_complete");
+       sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
+                    "wakeup_hotunplug_complete");
 }
 
 /* --------------------------------------------------------------------- */
@@ -2546,27 +2930,19 @@ static void hotkey_wakeup_hotunplug_complete_notify_change(void)
 static struct attribute *hotkey_attributes[] __initdata = {
        &dev_attr_hotkey_enable.attr,
        &dev_attr_hotkey_bios_enabled.attr,
+       &dev_attr_hotkey_bios_mask.attr,
        &dev_attr_hotkey_report_mode.attr,
-#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
+       &dev_attr_hotkey_wakeup_reason.attr,
+       &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
        &dev_attr_hotkey_mask.attr,
        &dev_attr_hotkey_all_mask.attr,
        &dev_attr_hotkey_recommended_mask.attr,
+#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
        &dev_attr_hotkey_source_mask.attr,
        &dev_attr_hotkey_poll_freq.attr,
 #endif
 };
 
-static struct attribute *hotkey_mask_attributes[] __initdata = {
-       &dev_attr_hotkey_bios_mask.attr,
-#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
-       &dev_attr_hotkey_mask.attr,
-       &dev_attr_hotkey_all_mask.attr,
-       &dev_attr_hotkey_recommended_mask.attr,
-#endif
-       &dev_attr_hotkey_wakeup_reason.attr,
-       &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
-};
-
 /*
  * Sync both the hw and sw blocking state of all switches
  */
@@ -2619,7 +2995,9 @@ static void tpacpi_send_radiosw_update(void)
 static void hotkey_exit(void)
 {
 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
+       mutex_lock(&hotkey_mutex);
        hotkey_poll_stop_sync();
+       mutex_unlock(&hotkey_mutex);
 #endif
 
        if (hotkey_dev_attributes)
@@ -2627,18 +3005,56 @@ static void hotkey_exit(void)
 
        kfree(hotkey_keycode_map);
 
-       if (tp_features.hotkey) {
-               dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
-                          "restoring original hot key mask\n");
-               /* no short-circuit boolean operator below! */
-               if ((hotkey_mask_set(hotkey_orig_mask) |
-                    hotkey_status_set(false)) != 0)
-                       printk(TPACPI_ERR
-                              "failed to restore hot key mask "
-                              "to BIOS defaults\n");
+       dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
+                  "restoring original HKEY status and mask\n");
+       /* yes, there is a bitwise or below, we want the
+        * functions to be called even if one of them fail */
+       if (((tp_features.hotkey_mask &&
+             hotkey_mask_set(hotkey_orig_mask)) |
+            hotkey_status_set(false)) != 0)
+               printk(TPACPI_ERR
+                      "failed to restore hot key mask "
+                      "to BIOS defaults\n");
+}
+
+static void __init hotkey_unmap(const unsigned int scancode)
+{
+       if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
+               clear_bit(hotkey_keycode_map[scancode],
+                         tpacpi_inputdev->keybit);
+               hotkey_keycode_map[scancode] = KEY_RESERVED;
        }
 }
 
+/*
+ * HKEY quirks:
+ *   TPACPI_HK_Q_INIMASK:      Supports FN+F3,FN+F4,FN+F12
+ */
+
+#define        TPACPI_HK_Q_INIMASK     0x0001
+
+static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
+       TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
+       TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
+       TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
+       TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
+       TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
+       TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
+       TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
+       TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
+       TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
+       TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
+       TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
+       TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
+       TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
+       TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
+       TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
+       TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
+       TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
+       TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
+       TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
+};
+
 static int __init hotkey_init(struct ibm_init_struct *iibm)
 {
        /* Requirements for changing the default keymaps:
@@ -2681,9 +3097,7 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
                KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
 
-               /* brightness: firmware always reacts to them, unless
-                * X.org did some tricks in the radeon BIOS scratch
-                * registers of *some* models */
+               /* brightness: firmware always reacts to them */
                KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
                KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
 
@@ -2717,11 +3131,11 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
                KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
 
-               /* These either have to go through ACPI video, or
-                * act like in the IBM ThinkPads, so don't ever
-                * enable them by default */
-               KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
-               KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
+               /* These should be enabled --only-- when ACPI video
+                * is disabled (i.e. in "vendor" mode), and are handled
+                * in a special way by the init code */
+               KEY_BRIGHTNESSUP,       /* 0x0F: FN+HOME (brightness up) */
+               KEY_BRIGHTNESSDOWN,     /* 0x10: FN+END (brightness down) */
 
                KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
 
@@ -2758,6 +3172,8 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
        int status;
        int hkeyv;
 
+       unsigned long quirks;
+
        vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
                        "initializing hotkey subdriver\n");
 
@@ -2783,9 +3199,16 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
        if (!tp_features.hotkey)
                return 1;
 
+       quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
+                                    ARRAY_SIZE(tpacpi_hotkey_qtable));
+
        tpacpi_disable_brightness_delay();
 
-       hotkey_dev_attributes = create_attr_set(13, NULL);
+       /* MUST have enough space for all attributes to be added to
+        * hotkey_dev_attributes */
+       hotkey_dev_attributes = create_attr_set(
+                                       ARRAY_SIZE(hotkey_attributes) + 2,
+                                       NULL);
        if (!hotkey_dev_attributes)
                return -ENOMEM;
        res = add_many_to_attr_set(hotkey_dev_attributes,
@@ -2794,7 +3217,7 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
        if (res)
                goto err_exit;
 
-       /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
+       /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
           A30, R30, R31, T20-22, X20-21, X22-24.  Detected by checking
           for HKEY interface version 0x100 */
        if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
@@ -2808,10 +3231,22 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                         * MHKV 0x100 in A31, R40, R40e,
                         * T4x, X31, and later
                         */
-                       tp_features.hotkey_mask = 1;
                        vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
                                "firmware HKEY interface version: 0x%x\n",
                                hkeyv);
+
+                       /* Paranoia check AND init hotkey_all_mask */
+                       if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
+                                       "MHKA", "qd")) {
+                               printk(TPACPI_ERR
+                                      "missing MHKA handler, "
+                                      "please report this to %s\n",
+                                      TPACPI_MAIL);
+                               /* Fallback: pre-init for FN+F3,F4,F12 */
+                               hotkey_all_mask = 0x080cU;
+                       } else {
+                               tp_features.hotkey_mask = 1;
+                       }
                }
        }
 
@@ -2819,47 +3254,25 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                "hotkey masks are %s\n",
                str_supported(tp_features.hotkey_mask));
 
-       if (tp_features.hotkey_mask) {
-               if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
-                               "MHKA", "qd")) {
-                       printk(TPACPI_ERR
-                              "missing MHKA handler, "
-                              "please report this to %s\n",
-                              TPACPI_MAIL);
-                       /* FN+F12, FN+F4, FN+F3 */
-                       hotkey_all_mask = 0x080cU;
-               }
-       }
+       /* Init hotkey_all_mask if not initialized yet */
+       if (!tp_features.hotkey_mask && !hotkey_all_mask &&
+           (quirks & TPACPI_HK_Q_INIMASK))
+               hotkey_all_mask = 0x080cU;  /* FN+F12, FN+F4, FN+F3 */
 
-       /* hotkey_source_mask *must* be zero for
-        * the first hotkey_mask_get */
+       /* Init hotkey_acpi_mask and hotkey_orig_mask */
        if (tp_features.hotkey_mask) {
+               /* hotkey_source_mask *must* be zero for
+                * the first hotkey_mask_get to return hotkey_orig_mask */
                res = hotkey_mask_get();
                if (res)
                        goto err_exit;
 
-               hotkey_orig_mask = hotkey_mask;
-               res = add_many_to_attr_set(
-                               hotkey_dev_attributes,
-                               hotkey_mask_attributes,
-                               ARRAY_SIZE(hotkey_mask_attributes));
-               if (res)
-                       goto err_exit;
-       }
-
-#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
-       if (tp_features.hotkey_mask) {
-               hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
-                                       & ~hotkey_all_mask;
+               hotkey_orig_mask = hotkey_acpi_mask;
        } else {
-               hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
+               hotkey_orig_mask = hotkey_all_mask;
+               hotkey_acpi_mask = hotkey_all_mask;
        }
 
-       vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
-                   "hotkey source mask 0x%08x, polling freq %d\n",
-                   hotkey_source_mask, hotkey_poll_freq);
-#endif
-
 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
        if (dbg_wlswemul) {
                tp_features.hotkey_wlsw = 1;
@@ -2960,17 +3373,26 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                       "Disabling thinkpad-acpi brightness events "
                       "by default...\n");
 
-               /* The hotkey_reserved_mask change below is not
-                * necessary while the keys are at KEY_RESERVED in the
-                * default map, but better safe than sorry, leave it
-                * here as a marker of what we have to do, especially
-                * when we finally become able to set this at runtime
-                * on response to X.org requests */
+               /* Disable brightness up/down on Lenovo thinkpads when
+                * ACPI is handling them, otherwise it is plain impossible
+                * for userspace to do something even remotely sane */
                hotkey_reserved_mask |=
                        (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
                        | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
+               hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
+               hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
        }
 
+#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
+       hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
+                               & ~hotkey_all_mask
+                               & ~hotkey_reserved_mask;
+
+       vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
+                   "hotkey source mask 0x%08x, polling freq %u\n",
+                   hotkey_source_mask, hotkey_poll_freq);
+#endif
+
        dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
                        "enabling firmware HKEY event interface...\n");
        res = hotkey_status_set(true);
@@ -2978,13 +3400,18 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                hotkey_exit();
                return res;
        }
-       res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
-                               & ~hotkey_reserved_mask)
-                               | hotkey_orig_mask);
+       res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
+                              | hotkey_driver_mask)
+                             & ~hotkey_source_mask);
        if (res < 0 && res != -ENXIO) {
                hotkey_exit();
                return res;
        }
+       hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
+                               & ~hotkey_reserved_mask;
+       vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
+               "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
+               hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
 
        dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
                        "legacy ibm/hotkey event reporting over procfs %s\n",
@@ -2994,7 +3421,7 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
        tpacpi_inputdev->open = &hotkey_inputdev_open;
        tpacpi_inputdev->close = &hotkey_inputdev_close;
 
-       hotkey_poll_setup_safe(1);
+       hotkey_poll_setup_safe(true);
        tpacpi_send_radiosw_update();
        tpacpi_input_send_tabletsw();
 
@@ -3019,7 +3446,7 @@ static bool hotkey_notify_hotkey(const u32 hkey,
        if (scancode > 0 && scancode < 0x21) {
                scancode--;
                if (!(hotkey_source_mask & (1 << scancode))) {
-                       tpacpi_input_send_key(scancode);
+                       tpacpi_input_send_key_masked(scancode);
                        *send_acpi_ev = false;
                } else {
                        *ignore_acpi_ev = true;
@@ -3038,20 +3465,20 @@ static bool hotkey_notify_wakeup(const u32 hkey,
        *ignore_acpi_ev = false;
 
        switch (hkey) {
-       case 0x2304: /* suspend, undock */
-       case 0x2404: /* hibernation, undock */
+       case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
+       case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
                hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
                *ignore_acpi_ev = true;
                break;
 
-       case 0x2305: /* suspend, bay eject */
-       case 0x2405: /* hibernation, bay eject */
+       case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
+       case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
                hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
                *ignore_acpi_ev = true;
                break;
 
-       case 0x2313: /* Battery on critical low level (S3) */
-       case 0x2413: /* Battery on critical low level (S4) */
+       case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
+       case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
                printk(TPACPI_ALERT
                        "EMERGENCY WAKEUP: battery almost empty\n");
                /* how to auto-heal: */
@@ -3081,21 +3508,21 @@ static bool hotkey_notify_usrevent(const u32 hkey,
        *ignore_acpi_ev = false;
 
        switch (hkey) {
-       case 0x5010: /* Lenovo new BIOS: brightness changed */
-       case 0x500b: /* X61t: tablet pen inserted into bay */
-       case 0x500c: /* X61t: tablet pen removed from bay */
+       case TP_HKEY_EV_PEN_INSERTED:  /* X61t: tablet pen inserted into bay */
+       case TP_HKEY_EV_PEN_REMOVED:   /* X61t: tablet pen removed from bay */
                return true;
 
-       case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
-       case 0x500a: /* X41t-X61t: swivel down (normal mode) */
+       case TP_HKEY_EV_TABLET_TABLET:   /* X41t-X61t: tablet mode */
+       case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
                tpacpi_input_send_tabletsw();
                hotkey_tablet_mode_notify_change();
                *send_acpi_ev = false;
                return true;
 
-       case 0x5001:
-       case 0x5002:
-               /* LID switch events.  Do not propagate */
+       case TP_HKEY_EV_LID_CLOSE:      /* Lid closed */
+       case TP_HKEY_EV_LID_OPEN:       /* Lid opened */
+       case TP_HKEY_EV_BRGHT_CHANGED:  /* brightness changed */
+               /* do not propagate these events */
                *ignore_acpi_ev = true;
                return true;
 
@@ -3113,30 +3540,30 @@ static bool hotkey_notify_thermal(const u32 hkey,
        *ignore_acpi_ev = false;
 
        switch (hkey) {
-       case 0x6011:
+       case TP_HKEY_EV_ALARM_BAT_HOT:
                printk(TPACPI_CRIT
                        "THERMAL ALARM: battery is too hot!\n");
                /* recommended action: warn user through gui */
                return true;
-       case 0x6012:
+       case TP_HKEY_EV_ALARM_BAT_XHOT:
                printk(TPACPI_ALERT
                        "THERMAL EMERGENCY: battery is extremely hot!\n");
                /* recommended action: immediate sleep/hibernate */
                return true;
-       case 0x6021:
+       case TP_HKEY_EV_ALARM_SENSOR_HOT:
                printk(TPACPI_CRIT
                        "THERMAL ALARM: "
                        "a sensor reports something is too hot!\n");
                /* recommended action: warn user through gui, that */
                /* some internal component is too hot */
                return true;
-       case 0x6022:
+       case TP_HKEY_EV_ALARM_SENSOR_XHOT:
                printk(TPACPI_ALERT
                        "THERMAL EMERGENCY: "
                        "a sensor reports something is extremely hot!\n");
                /* recommended action: immediate sleep/hibernate */
                return true;
-       case 0x6030:
+       case TP_HKEY_EV_THM_TABLE_CHANGED:
                printk(TPACPI_INFO
                        "EC reports that Thermal Table has changed\n");
                /* recommended action: do nothing, we don't have
@@ -3194,7 +3621,7 @@ static void hotkey_notify(struct ibm_struct *ibm, u32 event)
                        break;
                case 3:
                        /* 0x3000-0x3FFF: bay-related wakeups */
-                       if (hkey == 0x3003) {
+                       if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
                                hotkey_autosleep_ack = 1;
                                printk(TPACPI_INFO
                                       "bay ejected\n");
@@ -3206,7 +3633,7 @@ static void hotkey_notify(struct ibm_struct *ibm, u32 event)
                        break;
                case 4:
                        /* 0x4000-0x4FFF: dock-related wakeups */
-                       if (hkey == 0x4003) {
+                       if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
                                hotkey_autosleep_ack = 1;
                                printk(TPACPI_INFO
                                       "undocked\n");
@@ -3228,7 +3655,8 @@ static void hotkey_notify(struct ibm_struct *ibm, u32 event)
                        break;
                case 7:
                        /* 0x7000-0x7FFF: misc */
-                       if (tp_features.hotkey_wlsw && hkey == 0x7000) {
+                       if (tp_features.hotkey_wlsw &&
+                                       hkey == TP_HKEY_EV_RFKILL_CHANGED) {
                                tpacpi_send_radiosw_update();
                                send_acpi_ev = 0;
                                known_ev = true;
@@ -3274,15 +3702,17 @@ static void hotkey_resume(void)
 {
        tpacpi_disable_brightness_delay();
 
-       if (hotkey_mask_get())
+       if (hotkey_status_set(true) < 0 ||
+           hotkey_mask_set(hotkey_acpi_mask) < 0)
                printk(TPACPI_ERR
-                      "error while trying to read hot key mask "
-                      "from firmware\n");
+                      "error while attempting to reset the event "
+                      "firmware interface\n");
+
        tpacpi_send_radiosw_update();
        hotkey_tablet_mode_notify_change();
        hotkey_wakeup_reason_notify_change();
        hotkey_wakeup_hotunplug_complete_notify_change();
-       hotkey_poll_setup_safe(0);
+       hotkey_poll_setup_safe(false);
 }
 
 /* procfs -------------------------------------------------------------- */
@@ -3306,8 +3736,8 @@ static int hotkey_read(char *p)
                return res;
 
        len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
-       if (tp_features.hotkey_mask) {
-               len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
+       if (hotkey_all_mask) {
+               len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
                len += sprintf(p + len,
                               "commands:\tenable, disable, reset, <mask>\n");
        } else {
@@ -3344,7 +3774,7 @@ static int hotkey_write(char *buf)
        if (mutex_lock_killable(&hotkey_mutex))
                return -ERESTARTSYS;
 
-       mask = hotkey_mask;
+       mask = hotkey_user_mask;
 
        res = 0;
        while ((cmd = next_cmd(&buf))) {
@@ -3354,7 +3784,8 @@ static int hotkey_write(char *buf)
                        hotkey_enabledisable_warn(0);
                        res = -EPERM;
                } else if (strlencmp(cmd, "reset") == 0) {
-                       mask = hotkey_orig_mask;
+                       mask = (hotkey_all_mask | hotkey_source_mask)
+                               & ~hotkey_reserved_mask;
                } else if (sscanf(cmd, "0x%x", &mask) == 1) {
                        /* mask set */
                } else if (sscanf(cmd, "%x", &mask) == 1) {
@@ -3365,12 +3796,11 @@ static int hotkey_write(char *buf)
                }
        }
 
-       if (!res)
+       if (!res) {
                tpacpi_disclose_usertask("procfs hotkey",
                        "set mask to 0x%08x\n", mask);
-
-       if (!res && mask != hotkey_mask)
-               res = hotkey_mask_set(mask);
+               res = hotkey_user_mask_set(mask);
+       }
 
 errexit:
        mutex_unlock(&hotkey_mutex);
@@ -4441,293 +4871,6 @@ static struct ibm_struct light_driver_data = {
        .exit = light_exit,
 };
 
-/*************************************************************************
- * Dock subdriver
- */
-
-#ifdef CONFIG_THINKPAD_ACPI_DOCK
-
-static void dock_notify(struct ibm_struct *ibm, u32 event);
-static int dock_read(char *p);
-static int dock_write(char *buf);
-
-TPACPI_HANDLE(dock, root, "\\_SB.GDCK",        /* X30, X31, X40 */
-          "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
-          "\\_SB.PCI0.PCI1.DOCK",      /* all others */
-          "\\_SB.PCI.ISA.SLCE",        /* 570 */
-    );                         /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
-
-/* don't list other alternatives as we install a notify handler on the 570 */
-TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
-
-static const struct acpi_device_id ibm_pci_device_ids[] = {
-       {PCI_ROOT_HID_STRING, 0},
-       {"", 0},
-};
-
-static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
-       {
-        .notify = dock_notify,
-        .handle = &dock_handle,
-        .type = ACPI_SYSTEM_NOTIFY,
-       },
-       {
-       /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
-        * We just use it to get notifications of dock hotplug
-        * in very old thinkpads */
-        .hid = ibm_pci_device_ids,
-        .notify = dock_notify,
-        .handle = &pci_handle,
-        .type = ACPI_SYSTEM_NOTIFY,
-       },
-};
-
-static struct ibm_struct dock_driver_data[2] = {
-       {
-        .name = "dock",
-        .read = dock_read,
-        .write = dock_write,
-        .acpi = &ibm_dock_acpidriver[0],
-       },
-       {
-        .name = "dock",
-        .acpi = &ibm_dock_acpidriver[1],
-       },
-};
-
-#define dock_docked() (_sta(dock_handle) & 1)
-
-static int __init dock_init(struct ibm_init_struct *iibm)
-{
-       vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
-
-       TPACPI_ACPIHANDLE_INIT(dock);
-
-       vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
-               str_supported(dock_handle != NULL));
-
-       return (dock_handle)? 0 : 1;
-}
-
-static int __init dock_init2(struct ibm_init_struct *iibm)
-{
-       int dock2_needed;
-
-       vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
-
-       if (dock_driver_data[0].flags.acpi_driver_registered &&
-           dock_driver_data[0].flags.acpi_notify_installed) {
-               TPACPI_ACPIHANDLE_INIT(pci);
-               dock2_needed = (pci_handle != NULL);
-               vdbg_printk(TPACPI_DBG_INIT,
-                           "dock PCI handler for the TP 570 is %s\n",
-                           str_supported(dock2_needed));
-       } else {
-               vdbg_printk(TPACPI_DBG_INIT,
-               "dock subdriver part 2 not required\n");
-               dock2_needed = 0;
-       }
-
-       return (dock2_needed)? 0 : 1;
-}
-
-static void dock_notify(struct ibm_struct *ibm, u32 event)
-{
-       int docked = dock_docked();
-       int pci = ibm->acpi->hid && ibm->acpi->device &&
-               acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
-       int data;
-
-       if (event == 1 && !pci) /* 570 */
-               data = 1;       /* button */
-       else if (event == 1 && pci)     /* 570 */
-               data = 3;       /* dock */
-       else if (event == 3 && docked)
-               data = 1;       /* button */
-       else if (event == 3 && !docked)
-               data = 2;       /* undock */
-       else if (event == 0 && docked)
-               data = 3;       /* dock */
-       else {
-               printk(TPACPI_ERR "unknown dock event %d, status %d\n",
-                      event, _sta(dock_handle));
-               data = 0;       /* unknown */
-       }
-       acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
-       acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
-                                         dev_name(&ibm->acpi->device->dev),
-                                         event, data);
-}
-
-static int dock_read(char *p)
-{
-       int len = 0;
-       int docked = dock_docked();
-
-       if (!dock_handle)
-               len += sprintf(p + len, "status:\t\tnot supported\n");
-       else if (!docked)
-               len += sprintf(p + len, "status:\t\tundocked\n");
-       else {
-               len += sprintf(p + len, "status:\t\tdocked\n");
-               len += sprintf(p + len, "commands:\tdock, undock\n");
-       }
-
-       return len;
-}
-
-static int dock_write(char *buf)
-{
-       char *cmd;
-
-       if (!dock_docked())
-               return -ENODEV;
-
-       while ((cmd = next_cmd(&buf))) {
-               if (strlencmp(cmd, "undock") == 0) {
-                       if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
-                           !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
-                               return -EIO;
-               } else if (strlencmp(cmd, "dock") == 0) {
-                       if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
-                               return -EIO;
-               } else
-                       return -EINVAL;
-       }
-
-       return 0;
-}
-
-#endif /* CONFIG_THINKPAD_ACPI_DOCK */
-
-/*************************************************************************
- * Bay subdriver
- */
-
-#ifdef CONFIG_THINKPAD_ACPI_BAY
-
-TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",    /* 570 */
-          "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
-          "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
-          "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
-          );                           /* A21e, R30, R31 */
-TPACPI_HANDLE(bay_ej, bay, "_EJ3",     /* 600e/x, A2xm/p, A3x */
-          "_EJ0",              /* all others */
-          );                   /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
-TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
-          "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
-          );                           /* all others */
-TPACPI_HANDLE(bay2_ej, bay2, "_EJ3",   /* 600e/x, 770e, A3x */
-          "_EJ0",                      /* 770x */
-          );                           /* all others */
-
-static int __init bay_init(struct ibm_init_struct *iibm)
-{
-       vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
-
-       TPACPI_ACPIHANDLE_INIT(bay);
-       if (bay_handle)
-               TPACPI_ACPIHANDLE_INIT(bay_ej);
-       TPACPI_ACPIHANDLE_INIT(bay2);
-       if (bay2_handle)
-               TPACPI_ACPIHANDLE_INIT(bay2_ej);
-
-       tp_features.bay_status = bay_handle &&
-               acpi_evalf(bay_handle, NULL, "_STA", "qv");
-       tp_features.bay_status2 = bay2_handle &&
-               acpi_evalf(bay2_handle, NULL, "_STA", "qv");
-
-       tp_features.bay_eject = bay_handle && bay_ej_handle &&
-               (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
-       tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
-               (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
-
-       vdbg_printk(TPACPI_DBG_INIT,
-               "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
-               str_supported(tp_features.bay_status),
-               str_supported(tp_features.bay_eject),
-               str_supported(tp_features.bay_status2),
-               str_supported(tp_features.bay_eject2));
-
-       return (tp_features.bay_status || tp_features.bay_eject ||
-               tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
-}
-
-static void bay_notify(struct ibm_struct *ibm, u32 event)
-{
-       acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
-       acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
-                                         dev_name(&ibm->acpi->device->dev),
-                                         event, 0);
-}
-
-#define bay_occupied(b) (_sta(b##_handle) & 1)
-
-static int bay_read(char *p)
-{
-       int len = 0;
-       int occupied = bay_occupied(bay);
-       int occupied2 = bay_occupied(bay2);
-       int eject, eject2;
-
-       len += sprintf(p + len, "status:\t\t%s\n",
-               tp_features.bay_status ?
-                       (occupied ? "occupied" : "unoccupied") :
-                               "not supported");
-       if (tp_features.bay_status2)
-               len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
-                              "occupied" : "unoccupied");
-
-       eject = tp_features.bay_eject && occupied;
-       eject2 = tp_features.bay_eject2 && occupied2;
-
-       if (eject && eject2)
-               len += sprintf(p + len, "commands:\teject, eject2\n");
-       else if (eject)
-               len += sprintf(p + len, "commands:\teject\n");
-       else if (eject2)
-               len += sprintf(p + len, "commands:\teject2\n");
-
-       return len;
-}
-
-static int bay_write(char *buf)
-{
-       char *cmd;
-
-       if (!tp_features.bay_eject && !tp_features.bay_eject2)
-               return -ENODEV;
-
-       while ((cmd = next_cmd(&buf))) {
-               if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
-                       if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
-                               return -EIO;
-               } else if (tp_features.bay_eject2 &&
-                          strlencmp(cmd, "eject2") == 0) {
-                       if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
-                               return -EIO;
-               } else
-                       return -EINVAL;
-       }
-
-       return 0;
-}
-
-static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
-       .notify = bay_notify,
-       .handle = &bay_handle,
-       .type = ACPI_SYSTEM_NOTIFY,
-};
-
-static struct ibm_struct bay_driver_data = {
-       .name = "bay",
-       .read = bay_read,
-       .write = bay_write,
-       .acpi = &ibm_bay_acpidriver,
-};
-
-#endif /* CONFIG_THINKPAD_ACPI_BAY */
-
 /*************************************************************************
  * CMOS subdriver
  */
@@ -5945,14 +6088,48 @@ static struct backlight_ops ibm_backlight_data = {
 
 /* --------------------------------------------------------------------- */
 
+/*
+ * These are only useful for models that have only one possibility
+ * of GPU.  If the BIOS model handles both ATI and Intel, don't use
+ * these quirks.
+ */
+#define TPACPI_BRGHT_Q_NOEC    0x0001  /* Must NOT use EC HBRV */
+#define TPACPI_BRGHT_Q_EC      0x0002  /* Should or must use EC HBRV */
+#define TPACPI_BRGHT_Q_ASK     0x8000  /* Ask for user report */
+
+static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
+       /* Models with ATI GPUs known to require ECNVRAM mode */
+       TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC),      /* T43/p ATI */
+
+       /* Models with ATI GPUs that can use ECNVRAM */
+       TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
+       TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
+       TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
+       TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
+
+       /* Models with Intel Extreme Graphics 2 */
+       TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
+       TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
+       TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
+
+       /* Models with Intel GMA900 */
+       TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC),    /* T43, R52 */
+       TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC),    /* X41 */
+       TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC),    /* X41 Tablet */
+};
+
 static int __init brightness_init(struct ibm_init_struct *iibm)
 {
        int b;
+       unsigned long quirks;
 
        vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
 
        mutex_init(&brightness_mutex);
 
+       quirks = tpacpi_check_quirks(brightness_quirk_table,
+                               ARRAY_SIZE(brightness_quirk_table));
+
        /*
         * We always attempt to detect acpi support, so as to switch
         * Lenovo Vista BIOS to ACPI brightness mode even if we are not
@@ -6009,23 +6186,13 @@ static int __init brightness_init(struct ibm_init_struct *iibm)
        /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
        if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
            brightness_mode == TPACPI_BRGHT_MODE_MAX) {
-               if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
-                       /*
-                        * IBM models that define HBRV probably have
-                        * EC-based backlight level control
-                        */
-                       if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
-                               /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
-                               brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
-                       else
-                               /* all other IBM ThinkPads */
-                               brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
-               } else
-                       /* All Lenovo ThinkPads */
+               if (quirks & TPACPI_BRGHT_Q_EC)
+                       brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
+               else
                        brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
 
                dbg_printk(TPACPI_DBG_BRGHT,
-                          "selected brightness_mode=%d\n",
+                          "driver auto-selected brightness_mode=%d\n",
                           brightness_mode);
        }
 
@@ -6046,12 +6213,23 @@ static int __init brightness_init(struct ibm_init_struct *iibm)
                                        TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
                                        &ibm_backlight_data);
        if (IS_ERR(ibm_backlight_device)) {
+               int rc = PTR_ERR(ibm_backlight_device);
+               ibm_backlight_device = NULL;
                printk(TPACPI_ERR "Could not register backlight device\n");
-               return PTR_ERR(ibm_backlight_device);
+               return rc;
        }
        vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
                        "brightness is supported\n");
 
+       if (quirks & TPACPI_BRGHT_Q_ASK) {
+               printk(TPACPI_NOTICE
+                       "brightness: will use unverified default: "
+                       "brightness_mode=%d\n", brightness_mode);
+               printk(TPACPI_NOTICE
+                       "brightness: please report to %s whether it works well "
+                       "or not on your ThinkPad\n", TPACPI_MAIL);
+       }
+
        ibm_backlight_device->props.max_brightness =
                                (tp_features.bright_16levels)? 15 : 7;
        ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
@@ -7526,6 +7704,21 @@ static struct ibm_struct fan_driver_data = {
  ****************************************************************************
  ****************************************************************************/
 
+/*
+ * HKEY event callout for other subdrivers go here
+ * (yes, it is ugly, but it is quick, safe, and gets the job done
+ */
+static void tpacpi_driver_event(const unsigned int hkey_event)
+{
+}
+
+
+
+static void hotkey_driver_event(const unsigned int scancode)
+{
+       tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
+}
+
 /* sysfs name ---------------------------------------------------------- */
 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
                           struct device_attribute *attr,
@@ -7794,9 +7987,11 @@ static int __init probe_for_thinkpad(void)
 
        /*
         * Non-ancient models have better DMI tagging, but very old models
-        * don't.
+        * don't.  tpacpi_is_fw_known() is a cheat to help in that case.
         */
-       is_thinkpad = (thinkpad_id.model_str != NULL);
+       is_thinkpad = (thinkpad_id.model_str != NULL) ||
+                     (thinkpad_id.ec_model != 0) ||
+                     tpacpi_is_fw_known();
 
        /* ec is required because many other handles are relative to it */
        TPACPI_ACPIHANDLE_INIT(ec);
@@ -7807,13 +8002,6 @@ static int __init probe_for_thinkpad(void)
                return -ENODEV;
        }
 
-       /*
-        * Risks a regression on very old machines, but reduces potential
-        * false positives a damn great deal
-        */
-       if (!is_thinkpad)
-               is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
-
        if (!is_thinkpad && !force_load)
                return -ENODEV;
 
@@ -7854,22 +8042,6 @@ static struct ibm_init_struct ibms_init[] __initdata = {
                .init = light_init,
                .data = &light_driver_data,
        },
-#ifdef CONFIG_THINKPAD_ACPI_DOCK
-       {
-               .init = dock_init,
-               .data = &dock_driver_data[0],
-       },
-       {
-               .init = dock_init2,
-               .data = &dock_driver_data[1],
-       },
-#endif
-#ifdef CONFIG_THINKPAD_ACPI_BAY
-       {
-               .init = bay_init,
-               .data = &bay_driver_data,
-       },
-#endif
        {
                .init = cmos_init,
                .data = &cmos_driver_data,
@@ -7968,12 +8140,6 @@ TPACPI_PARAM(hotkey);
 TPACPI_PARAM(bluetooth);
 TPACPI_PARAM(video);
 TPACPI_PARAM(light);
-#ifdef CONFIG_THINKPAD_ACPI_DOCK
-TPACPI_PARAM(dock);
-#endif
-#ifdef CONFIG_THINKPAD_ACPI_BAY
-TPACPI_PARAM(bay);
-#endif /* CONFIG_THINKPAD_ACPI_BAY */
 TPACPI_PARAM(cmos);
 TPACPI_PARAM(led);
 TPACPI_PARAM(beep);