]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/misc/thinkpad_acpi.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / misc / thinkpad_acpi.c
index cfef218c4513095a39a5ae5b21f5b094c470d2aa..ab23a3221585302fec8a33d79ceade142b93f7dc 100644 (file)
@@ -21,8 +21,8 @@
  *  02110-1301, USA.
  */
 
-#define IBM_VERSION "0.14"
-#define TPACPI_SYSFS_VERSION 0x000200
+#define IBM_VERSION "0.17"
+#define TPACPI_SYSFS_VERSION 0x020000
 
 /*
  *  Changelog:
@@ -117,6 +117,12 @@ IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
 
 #define __unused __attribute__ ((unused))
 
+static enum {
+       TPACPI_LIFE_INIT = 0,
+       TPACPI_LIFE_RUNNING,
+       TPACPI_LIFE_EXITING,
+} tpacpi_lifecycle;
+
 /****************************************************************************
  ****************************************************************************
  *
@@ -342,6 +348,9 @@ static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
 {
        struct ibm_struct *ibm = data;
 
+       if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
+               return;
+
        if (!ibm || !ibm->acpi || !ibm->acpi->notify)
                return;
 
@@ -411,12 +420,13 @@ static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
 
        sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
        ibm->acpi->driver->ids = ibm->acpi->hid;
+
        ibm->acpi->driver->ops.add = &tpacpi_device_add;
 
        rc = acpi_bus_register_driver(ibm->acpi->driver);
        if (rc < 0) {
                printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
-                      ibm->acpi->hid, rc);
+                      ibm->name, rc);
                kfree(ibm->acpi->driver);
                ibm->acpi->driver = NULL;
        } else if (!rc)
@@ -516,8 +526,10 @@ static char *next_cmd(char **cmds)
  ****************************************************************************/
 
 static struct platform_device *tpacpi_pdev;
-static struct class_device *tpacpi_hwmon;
+static struct platform_device *tpacpi_sensors_pdev;
+static struct device *tpacpi_hwmon;
 static struct input_dev *tpacpi_inputdev;
+static struct mutex tpacpi_inputdev_send_mutex;
 
 
 static int tpacpi_resume_handler(struct platform_device *pdev)
@@ -542,6 +554,12 @@ static struct platform_driver tpacpi_pdriver = {
        .resume = tpacpi_resume_handler,
 };
 
+static struct platform_driver tpacpi_hwmon_pdriver = {
+       .driver = {
+               .name = IBM_HWMON_DRVR_NAME,
+               .owner = THIS_MODULE,
+       },
+};
 
 /*************************************************************************
  * thinkpad-acpi driver attributes
@@ -691,6 +709,8 @@ static int parse_strtoul(const char *buf,
 {
        char *endp;
 
+       while (*buf && isspace(*buf))
+               buf++;
        *value = simple_strtoul(buf, &endp, 0);
        while (*endp && isspace(*endp))
                endp++;
@@ -717,9 +737,19 @@ static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
        printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
        printk(IBM_INFO "%s\n", IBM_URL);
 
-       if (ibm_thinkpad_ec_found)
-               printk(IBM_INFO "ThinkPad EC firmware %s\n",
-                      ibm_thinkpad_ec_found);
+       printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
+               (thinkpad_id.bios_version_str) ?
+                       thinkpad_id.bios_version_str : "unknown",
+               (thinkpad_id.ec_version_str) ?
+                       thinkpad_id.ec_version_str : "unknown");
+
+       if (thinkpad_id.vendor && thinkpad_id.model_str)
+               printk(IBM_INFO "%s %s\n",
+                       (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
+                               "IBM" : ((thinkpad_id.vendor ==
+                                               PCI_VENDOR_ID_LENOVO) ?
+                                       "Lenovo" : "Unknown vendor"),
+                       thinkpad_id.model_str);
 
        return 0;
 }
@@ -748,29 +778,7 @@ static u32 hotkey_orig_mask;
 static u32 hotkey_all_mask;
 static u32 hotkey_reserved_mask;
 
-static u16 hotkey_keycode_map[] = {
-       /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
-       KEY_FN_F1,      KEY_FN_F2,      KEY_FN_F3,      KEY_SLEEP,
-       KEY_FN_F5,      KEY_FN_F6,      KEY_FN_F7,      KEY_FN_F8,
-       KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
-       /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
-       KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
-       KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
-       KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
-       KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
-       /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
-       KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
-       KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
-       KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
-       KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
-       KEY_RESERVED,   /* 0x14: VOLUME UP */
-       KEY_RESERVED,   /* 0x15: VOLUME DOWN */
-       KEY_RESERVED,   /* 0x16: MUTE */
-       KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
-       /* (assignments unknown, please report if found) */
-       KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
-       KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
-};
+static u16 *hotkey_keycode_map;
 
 static struct attribute_set *hotkey_dev_attributes;
 
@@ -917,9 +925,26 @@ static ssize_t hotkey_radio_sw_show(struct device *dev,
 static struct device_attribute dev_attr_hotkey_radio_sw =
        __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
 
+/* sysfs hotkey report_mode -------------------------------------------- */
+static ssize_t hotkey_report_mode_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%d\n",
+               (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
+}
+
+static struct device_attribute dev_attr_hotkey_report_mode =
+       __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
+
 /* --------------------------------------------------------------------- */
 
-static struct attribute *hotkey_mask_attributes[] = {
+static struct attribute *hotkey_attributes[] __initdata = {
+       &dev_attr_hotkey_enable.attr,
+       &dev_attr_hotkey_report_mode.attr,
+};
+
+static struct attribute *hotkey_mask_attributes[] __initdata = {
        &dev_attr_hotkey_mask.attr,
        &dev_attr_hotkey_bios_enabled.attr,
        &dev_attr_hotkey_bios_mask.attr,
@@ -929,8 +954,61 @@ static struct attribute *hotkey_mask_attributes[] = {
 
 static int __init hotkey_init(struct ibm_init_struct *iibm)
 {
+
+       static u16 ibm_keycode_map[] __initdata = {
+               /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
+               KEY_FN_F1,      KEY_FN_F2,      KEY_COFFEE,     KEY_SLEEP,
+               KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
+               KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
+               /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
+               KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
+               KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
+               KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
+               KEY_RESERVED,   /* 0x0F: FN+HOME (brightness up) */
+               /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
+               KEY_RESERVED,   /* 0x10: FN+END (brightness down) */
+               KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
+               KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
+               KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
+               KEY_RESERVED,   /* 0x14: VOLUME UP */
+               KEY_RESERVED,   /* 0x15: VOLUME DOWN */
+               KEY_RESERVED,   /* 0x16: MUTE */
+               KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
+               /* (assignments unknown, please report if found) */
+               KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
+               KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
+       };
+       static u16 lenovo_keycode_map[] __initdata = {
+               /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
+               KEY_FN_F1,      KEY_COFFEE,     KEY_BATTERY,    KEY_SLEEP,
+               KEY_WLAN,       KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
+               KEY_FN_F9,      KEY_FN_F10,     KEY_FN_F11,     KEY_SUSPEND,
+               /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
+               KEY_UNKNOWN,    /* 0x0C: FN+BACKSPACE */
+               KEY_UNKNOWN,    /* 0x0D: FN+INSERT */
+               KEY_UNKNOWN,    /* 0x0E: FN+DELETE */
+               KEY_BRIGHTNESSUP,       /* 0x0F: FN+HOME (brightness up) */
+               /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
+               KEY_BRIGHTNESSDOWN,     /* 0x10: FN+END (brightness down) */
+               KEY_RESERVED,   /* 0x11: FN+PGUP (thinklight toggle) */
+               KEY_UNKNOWN,    /* 0x12: FN+PGDOWN */
+               KEY_ZOOM,       /* 0x13: FN+SPACE (zoom) */
+               KEY_RESERVED,   /* 0x14: VOLUME UP */
+               KEY_RESERVED,   /* 0x15: VOLUME DOWN */
+               KEY_RESERVED,   /* 0x16: MUTE */
+               KEY_VENDOR,     /* 0x17: Thinkpad/AccessIBM/Lenovo */
+               /* (assignments unknown, please report if found) */
+               KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
+               KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
+       };
+
+#define TPACPI_HOTKEY_MAP_LEN          ARRAY_SIZE(ibm_keycode_map)
+#define TPACPI_HOTKEY_MAP_SIZE         sizeof(ibm_keycode_map)
+#define TPACPI_HOTKEY_MAP_TYPESIZE     sizeof(ibm_keycode_map[0])
+
        int res, i;
        int status;
+       int hkeyv;
 
        vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
 
@@ -946,27 +1024,45 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                str_supported(tp_features.hotkey));
 
        if (tp_features.hotkey) {
-               hotkey_dev_attributes = create_attr_set(7, NULL);
+               hotkey_dev_attributes = create_attr_set(8, NULL);
                if (!hotkey_dev_attributes)
                        return -ENOMEM;
-               res = add_to_attr_set(hotkey_dev_attributes,
-                               &dev_attr_hotkey_enable.attr);
+               res = add_many_to_attr_set(hotkey_dev_attributes,
+                               hotkey_attributes,
+                               ARRAY_SIZE(hotkey_attributes));
                if (res)
                        return res;
 
                /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
-                  A30, R30, R31, T20-22, X20-21, X22-24 */
-               tp_features.hotkey_mask =
-                       acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
+                  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")) {
+                       if ((hkeyv >> 8) != 1) {
+                               printk(IBM_ERR "unknown version of the "
+                                      "HKEY interface: 0x%x\n", hkeyv);
+                               printk(IBM_ERR "please report this to %s\n",
+                                      IBM_MAIL);
+                       } else {
+                               /*
+                                * MHKV 0x100 in A31, R40, R40e,
+                                * T4x, X31, and later
+                                * */
+                               tp_features.hotkey_mask = 1;
+                       }
+               }
 
                vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
                        str_supported(tp_features.hotkey_mask));
 
                if (tp_features.hotkey_mask) {
-                       /* MHKA available in A31, R40, R40e, T4x, X31, and later */
                        if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
-                                       "MHKA", "qd"))
+                                       "MHKA", "qd")) {
+                               printk(IBM_ERR
+                                      "missing MHKA handler, "
+                                      "please report this to %s\n",
+                                      IBM_MAIL);
                                hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
+                       }
                }
 
                res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
@@ -993,18 +1089,34 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                if (res)
                        return res;
 
-#ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
-               for (i = 0; i < 12; i++)
-                       hotkey_keycode_map[i] = KEY_UNKNOWN;
-#endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
+               /* Set up key map */
+
+               hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
+                                               GFP_KERNEL);
+               if (!hotkey_keycode_map) {
+                       printk(IBM_ERR "failed to allocate memory for key map\n");
+                       return -ENOMEM;
+               }
+
+               if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
+                       dbg_printk(TPACPI_DBG_INIT,
+                                  "using Lenovo default hot key map\n");
+                       memcpy(hotkey_keycode_map, &lenovo_keycode_map,
+                               TPACPI_HOTKEY_MAP_SIZE);
+               } else {
+                       dbg_printk(TPACPI_DBG_INIT,
+                                  "using IBM default hot key map\n");
+                       memcpy(hotkey_keycode_map, &ibm_keycode_map,
+                               TPACPI_HOTKEY_MAP_SIZE);
+               }
 
                set_bit(EV_KEY, tpacpi_inputdev->evbit);
                set_bit(EV_MSC, tpacpi_inputdev->evbit);
                set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
-               tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
-               tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
-               tpacpi_inputdev->keycode = &hotkey_keycode_map;
-               for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
+               tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
+               tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
+               tpacpi_inputdev->keycode = hotkey_keycode_map;
+               for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
                        if (hotkey_keycode_map[i] != KEY_RESERVED) {
                                set_bit(hotkey_keycode_map[i],
                                        tpacpi_inputdev->keybit);
@@ -1019,14 +1131,17 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                        set_bit(SW_RADIO, tpacpi_inputdev->swbit);
                }
 
-#ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
                dbg_printk(TPACPI_DBG_INIT,
                                "enabling hot key handling\n");
                res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
                                        | hotkey_orig_mask);
                if (res)
                        return res;
-#endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
+
+               dbg_printk(TPACPI_DBG_INIT,
+                               "legacy hot key reporting over procfs %s\n",
+                               (hotkey_report_mode < 2) ?
+                                       "enabled" : "disabled");
        }
 
        return (tp_features.hotkey)? 0 : 1;
@@ -1053,6 +1168,8 @@ static void tpacpi_input_send_key(unsigned int scancode,
                                  unsigned int keycode)
 {
        if (keycode != KEY_RESERVED) {
+               mutex_lock(&tpacpi_inputdev_send_mutex);
+
                input_report_key(tpacpi_inputdev, keycode, 1);
                if (keycode == KEY_UNKNOWN)
                        input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
@@ -1064,6 +1181,8 @@ static void tpacpi_input_send_key(unsigned int scancode,
                        input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
                                    scancode);
                input_sync(tpacpi_inputdev);
+
+               mutex_unlock(&tpacpi_inputdev_send_mutex);
        }
 }
 
@@ -1071,67 +1190,102 @@ static void tpacpi_input_send_radiosw(void)
 {
        int wlsw;
 
-       if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
+       mutex_lock(&tpacpi_inputdev_send_mutex);
+
+       if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
                input_report_switch(tpacpi_inputdev,
                                    SW_RADIO, !!wlsw);
+               input_sync(tpacpi_inputdev);
+       }
+
+       mutex_unlock(&tpacpi_inputdev_send_mutex);
 }
 
 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
 {
        u32 hkey;
        unsigned int keycode, scancode;
-       int sendacpi = 1;
-
-       if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
-               if (tpacpi_inputdev->users > 0) {
-                       switch (hkey >> 12) {
-                       case 1:
-                               /* 0x1000-0x1FFF: key presses */
-                               scancode = hkey & 0xfff;
-                               if (scancode > 0 && scancode < 0x21) {
-                                       scancode--;
-                                       keycode = hotkey_keycode_map[scancode];
-                                       tpacpi_input_send_key(scancode, keycode);
-                                       sendacpi = (keycode == KEY_RESERVED
-                                               || keycode == KEY_UNKNOWN);
-                               } else {
-                                       printk(IBM_ERR
-                                              "hotkey 0x%04x out of range for keyboard map\n",
-                                              hkey);
-                               }
-                               break;
-                       case 5:
-                               /* 0x5000-0x5FFF: LID */
-                               /* we don't handle it through this path, just
-                                * eat up known LID events */
-                               if (hkey != 0x5001 && hkey != 0x5002) {
-                                       printk(IBM_ERR
-                                               "unknown LID-related hotkey event: 0x%04x\n",
-                                               hkey);
-                               }
+       int send_acpi_ev;
+       int ignore_acpi_ev;
+
+       if (event != 0x80) {
+               printk(IBM_ERR "unknown HKEY notification event %d\n", event);
+               /* forward it to userspace, maybe it knows how to handle it */
+               acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
+                                               ibm->acpi->device->dev.bus_id,
+                                               event, 0);
+               return;
+       }
+
+       while (1) {
+               if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
+                       printk(IBM_ERR "failed to retrieve HKEY event\n");
+                       return;
+               }
+
+               if (hkey == 0) {
+                       /* queue empty */
+                       return;
+               }
+
+               send_acpi_ev = 0;
+               ignore_acpi_ev = 0;
+
+               switch (hkey >> 12) {
+               case 1:
+                       /* 0x1000-0x1FFF: key presses */
+                       scancode = hkey & 0xfff;
+                       if (scancode > 0 && scancode < 0x21) {
+                               scancode--;
+                               keycode = hotkey_keycode_map[scancode];
+                               tpacpi_input_send_key(scancode, keycode);
+                       } else {
+                               printk(IBM_ERR
+                                      "hotkey 0x%04x out of range for keyboard map\n",
+                                      hkey);
+                               send_acpi_ev = 1;
+                       }
+                       break;
+               case 5:
+                       /* 0x5000-0x5FFF: LID */
+                       /* we don't handle it through this path, just
+                        * eat up known LID events */
+                       if (hkey != 0x5001 && hkey != 0x5002) {
+                               printk(IBM_ERR
+                                      "unknown LID-related HKEY event: 0x%04x\n",
+                                      hkey);
+                               send_acpi_ev = 1;
+                       } else {
+                               ignore_acpi_ev = 1;
+                       }
+                       break;
+               case 7:
+                       /* 0x7000-0x7FFF: misc */
+                       if (tp_features.hotkey_wlsw && hkey == 0x7000) {
+                               tpacpi_input_send_radiosw();
                                break;
-                       case 7:
-                               /* 0x7000-0x7FFF: misc */
-                               if (tp_features.hotkey_wlsw && hkey == 0x7000) {
-                                               tpacpi_input_send_radiosw();
-                                               sendacpi = 0;
-                                       break;
-                               }
-                               /* fallthrough to default */
-                       default:
-                               /* case 2: dock-related */
-                               /*      0x2305 - T43 waking up due to bay lever eject while aslept */
-                               /* case 3: ultra-bay related. maybe bay in dock? */
-                               /*      0x3003 - T43 after wake up by bay lever eject (0x2305) */
-                               printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
                        }
+                       /* fallthrough to default */
+               default:
+                       /* case 2: dock-related */
+                       /*      0x2305 - T43 waking up due to bay lever eject while aslept */
+                       /* case 3: ultra-bay related. maybe bay in dock? */
+                       /*      0x3003 - T43 after wake up by bay lever eject (0x2305) */
+                       printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
+                       send_acpi_ev = 1;
                }
 
-               if (sendacpi)
-                       acpi_bus_generate_event(ibm->acpi->device, event, hkey);
-       } else {
-               printk(IBM_ERR "unknown hotkey notification event %d\n", event);
-               acpi_bus_generate_event(ibm->acpi->device, event, 0);
+               /* Legacy events */
+               if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
+                       acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
+               }
+
+               /* netlink events */
+               if (!ignore_acpi_ev && send_acpi_ev) {
+                       acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
+                                                       ibm->acpi->device->dev.bus_id,
+                                                       event, hkey);
+               }
        }
 }
 
@@ -1188,9 +1342,8 @@ static int hotkey_read(char *p)
                return len;
        }
 
-       res = mutex_lock_interruptible(&hotkey_mutex);
-       if (res < 0)
-               return res;
+       if (mutex_lock_interruptible(&hotkey_mutex))
+               return -ERESTARTSYS;
        res = hotkey_get(&status, &mask);
        mutex_unlock(&hotkey_mutex);
        if (res)
@@ -1219,9 +1372,8 @@ static int hotkey_write(char *buf)
        if (!tp_features.hotkey)
                return -ENODEV;
 
-       res = mutex_lock_interruptible(&hotkey_mutex);
-       if (res < 0)
-               return res;
+       if (mutex_lock_interruptible(&hotkey_mutex))
+               return -ERESTARTSYS;
 
        res = hotkey_get(&status, &mask);
        if (res)
@@ -1255,8 +1407,13 @@ errexit:
        return res;
 }
 
+static const struct acpi_device_id ibm_htk_device_ids[] = {
+       {IBM_HKEY_HID, 0},
+       {"", 0},
+};
+
 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
-       .hid = IBM_HKEY_HID,
+       .hid = ibm_htk_device_ids,
        .notify = hotkey_notify,
        .handle = &hkey_handle,
        .type = ACPI_DEVICE_NOTIFY,
@@ -2019,6 +2176,11 @@ IBM_HANDLE(dock, root, "\\_SB.GDCK",     /* X30, X31, X40 */
 /* don't list other alternatives as we install a notify handler on the 570 */
 IBM_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,
@@ -2026,7 +2188,10 @@ static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
         .type = ACPI_SYSTEM_NOTIFY,
        },
        {
-        .hid = IBM_PCI_HID,
+       /* 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,
@@ -2085,23 +2250,29 @@ static int __init dock_init2(struct ibm_init_struct *iibm)
 static void dock_notify(struct ibm_struct *ibm, u32 event)
 {
        int docked = dock_docked();
-       int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
+       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 */
-               acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
+               data = 1;       /* button */
        else if (event == 1 && pci)     /* 570 */
-               acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
+               data = 3;       /* dock */
        else if (event == 3 && docked)
-               acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
+               data = 1;       /* button */
        else if (event == 3 && !docked)
-               acpi_bus_generate_event(ibm->acpi->device, event, 2);   /* undock */
+               data = 2;       /* undock */
        else if (event == 0 && docked)
-               acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
+               data = 3;       /* dock */
        else {
                printk(IBM_ERR "unknown dock event %d, status %d\n",
                       event, _sta(dock_handle));
-               acpi_bus_generate_event(ibm->acpi->device, event, 0);   /* unknown */
+               data = 0;       /* unknown */
        }
+       acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
+       acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
+                                         ibm->acpi->device->dev.bus_id,
+                                         event, data);
 }
 
 static int dock_read(char *p)
@@ -2199,7 +2370,10 @@ static int __init bay_init(struct ibm_init_struct *iibm)
 
 static void bay_notify(struct ibm_struct *ibm, u32 event)
 {
-       acpi_bus_generate_event(ibm->acpi->device, event, 0);
+       acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
+       acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
+                                         ibm->acpi->device->dev.bus_id,
+                                         event, 0);
 }
 
 #define bay_occupied(b) (_sta(b##_handle) & 1)
@@ -2645,7 +2819,7 @@ static int __init thermal_init(struct ibm_init_struct *iibm)
 
        acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
 
-       if (ibm_thinkpad_ec_found && experimental) {
+       if (thinkpad_id.ec_model) {
                /*
                 * Direct EC access mode: sensors at registers
                 * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
@@ -2705,7 +2879,7 @@ static int __init thermal_init(struct ibm_init_struct *iibm)
 
        switch(thermal_read_mode) {
        case TPACPI_THERMAL_TPEC_16:
-               res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+               res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
                                &thermal_temp_input16_group);
                if (res)
                        return res;
@@ -2713,7 +2887,7 @@ static int __init thermal_init(struct ibm_init_struct *iibm)
        case TPACPI_THERMAL_TPEC_8:
        case TPACPI_THERMAL_ACPI_TMP07:
        case TPACPI_THERMAL_ACPI_UPDT:
-               res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+               res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
                                &thermal_temp_input8_group);
                if (res)
                        return res;
@@ -2730,13 +2904,13 @@ static void thermal_exit(void)
 {
        switch(thermal_read_mode) {
        case TPACPI_THERMAL_TPEC_16:
-               sysfs_remove_group(&tpacpi_pdev->dev.kobj,
+               sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
                                   &thermal_temp_input16_group);
                break;
        case TPACPI_THERMAL_TPEC_8:
        case TPACPI_THERMAL_ACPI_TMP07:
        case TPACPI_THERMAL_ACPI_UPDT:
-               sysfs_remove_group(&tpacpi_pdev->dev.kobj,
+               sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
                                   &thermal_temp_input16_group);
                break;
        case TPACPI_THERMAL_NONE:
@@ -2789,6 +2963,8 @@ static int thermal_get_sensor(int idx, s32 *value)
                        snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
                        if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
                                return -EIO;
+                       if (t > 127 || t < -127)
+                               t = TP_EC_THERMAL_TMP_NA;
                        *value = t * 1000;
                        return 0;
                }
@@ -2934,15 +3110,144 @@ static struct backlight_ops ibm_backlight_data = {
         .update_status  = brightness_update_status,
 };
 
+static struct mutex brightness_mutex;
+
+static int __init tpacpi_query_bcll_levels(acpi_handle handle)
+{
+       struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+       union acpi_object *obj;
+       int rc;
+
+       if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
+               obj = (union acpi_object *)buffer.pointer;
+               if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
+                       printk(IBM_ERR "Unknown BCLL data, "
+                              "please report this to %s\n", IBM_MAIL);
+                       rc = 0;
+               } else {
+                       rc = obj->package.count;
+               }
+       } else {
+               return 0;
+       }
+
+       kfree(buffer.pointer);
+       return rc;
+}
+
+static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
+                                       void *context, void **rv)
+{
+       char name[ACPI_PATH_SEGMENT_LENGTH];
+       struct acpi_buffer buffer = { sizeof(name), &name };
+
+       if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
+           !strncmp("BCLL", name, sizeof(name) - 1)) {
+               if (tpacpi_query_bcll_levels(handle) == 16) {
+                       *rv = handle;
+                       return AE_CTRL_TERMINATE;
+               } else {
+                       return AE_OK;
+               }
+       } else {
+               return AE_OK;
+       }
+}
+
+static int __init brightness_check_levels(void)
+{
+       int status;
+       void *found_node = NULL;
+
+       if (!vid_handle) {
+               IBM_ACPIHANDLE_INIT(vid);
+       }
+       if (!vid_handle)
+               return 0;
+
+       /* Search for a BCLL package with 16 levels */
+       status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
+                                       brightness_find_bcll, NULL, &found_node);
+
+       return (ACPI_SUCCESS(status) && found_node != NULL);
+}
+
+static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
+                                       void *context, void **rv)
+{
+       char name[ACPI_PATH_SEGMENT_LENGTH];
+       struct acpi_buffer buffer = { sizeof(name), &name };
+
+       if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
+           !strncmp("_BCL", name, sizeof(name) - 1)) {
+               *rv = handle;
+               return AE_CTRL_TERMINATE;
+       } else {
+               return AE_OK;
+       }
+}
+
+static int __init brightness_check_std_acpi_support(void)
+{
+       int status;
+       void *found_node = NULL;
+
+       if (!vid_handle) {
+               IBM_ACPIHANDLE_INIT(vid);
+       }
+       if (!vid_handle)
+               return 0;
+
+       /* Search for a _BCL method, but don't execute it */
+       status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
+                                    brightness_find_bcl, NULL, &found_node);
+
+       return (ACPI_SUCCESS(status) && found_node != NULL);
+}
+
 static int __init brightness_init(struct ibm_init_struct *iibm)
 {
        int b;
 
        vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
 
+       mutex_init(&brightness_mutex);
+
+       if (!brightness_enable) {
+               dbg_printk(TPACPI_DBG_INIT,
+                          "brightness support disabled by module parameter\n");
+               return 1;
+       } else if (brightness_enable > 1) {
+               if (brightness_check_std_acpi_support()) {
+                       printk(IBM_NOTICE
+                              "standard ACPI backlight interface available, not loading native one...\n");
+                       return 1;
+               }
+       }
+
+       if (!brightness_mode) {
+               if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
+                       brightness_mode = 2;
+               else
+                       brightness_mode = 3;
+
+               dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
+                       brightness_mode);
+       }
+
+       if (brightness_mode > 3)
+               return -EINVAL;
+
+       tp_features.bright_16levels =
+                       thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
+                       brightness_check_levels();
+
        b = brightness_get(NULL);
        if (b < 0)
-               return b;
+               return 1;
+
+       if (tp_features.bright_16levels)
+               printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
 
        ibm_backlight_device = backlight_device_register(
                                        TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
@@ -2953,7 +3258,8 @@ static int __init brightness_init(struct ibm_init_struct *iibm)
        }
        vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
 
-       ibm_backlight_device->props.max_brightness = 7;
+       ibm_backlight_device->props.max_brightness =
+                               (tp_features.bright_16levels)? 15 : 7;
        ibm_backlight_device->props.brightness = b;
        backlight_update_status(ibm_backlight_device);
 
@@ -2972,40 +3278,89 @@ static void brightness_exit(void)
 
 static int brightness_update_status(struct backlight_device *bd)
 {
+       /* it is the backlight class's job (caller) to handle
+        * EINTR and other errors properly */
        return brightness_set(
                (bd->props.fb_blank == FB_BLANK_UNBLANK &&
                 bd->props.power == FB_BLANK_UNBLANK) ?
                                bd->props.brightness : 0);
 }
 
+/*
+ * ThinkPads can read brightness from two places: EC 0x31, or
+ * CMOS NVRAM byte 0x5E, bits 0-3.
+ */
 static int brightness_get(struct backlight_device *bd)
 {
-       u8 level;
-       if (!acpi_ec_read(brightness_offset, &level))
-               return -EIO;
+       u8 lec = 0, lcmos = 0, level = 0;
 
-       level &= 0x7;
+       if (brightness_mode & 1) {
+               if (!acpi_ec_read(brightness_offset, &lec))
+                       return -EIO;
+               lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
+               level = lec;
+       };
+       if (brightness_mode & 2) {
+               lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
+                        & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
+                       >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
+               lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
+               level = lcmos;
+       }
+
+       if (brightness_mode == 3 && lec != lcmos) {
+               printk(IBM_ERR
+                       "CMOS NVRAM (%u) and EC (%u) do not agree "
+                       "on display brightness level\n",
+                       (unsigned int) lcmos,
+                       (unsigned int) lec);
+               return -EIO;
+       }
 
        return level;
 }
 
+/* May return EINTR which can always be mapped to ERESTARTSYS */
 static int brightness_set(int value)
 {
-       int cmos_cmd, inc, i;
-       int current_value = brightness_get(NULL);
+       int cmos_cmd, inc, i, res;
+       int current_value;
+
+       if (value > ((tp_features.bright_16levels)? 15 : 7))
+               return -EINVAL;
 
-       value &= 7;
+       res = mutex_lock_interruptible(&brightness_mutex);
+       if (res < 0)
+               return res;
 
-       cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
-       inc = value > current_value ? 1 : -1;
+       current_value = brightness_get(NULL);
+       if (current_value < 0) {
+               res = current_value;
+               goto errout;
+       }
+
+       cmos_cmd = value > current_value ?
+                       TP_CMOS_BRIGHTNESS_UP :
+                       TP_CMOS_BRIGHTNESS_DOWN;
+       inc = (value > current_value)? 1 : -1;
+
+       res = 0;
        for (i = current_value; i != value; i += inc) {
-               if (issue_thinkpad_cmos_command(cmos_cmd))
-                       return -EIO;
-               if (!acpi_ec_write(brightness_offset, i + inc))
-                       return -EIO;
+               if ((brightness_mode & 2) &&
+                   issue_thinkpad_cmos_command(cmos_cmd)) {
+                       res = -EIO;
+                       goto errout;
+               }
+               if ((brightness_mode & 1) &&
+                   !acpi_ec_write(brightness_offset, i + inc)) {
+                       res = -EIO;
+                       goto errout;;
+               }
        }
 
-       return 0;
+errout:
+       mutex_unlock(&brightness_mutex);
+       return res;
 }
 
 static int brightness_read(char *p)
@@ -3016,10 +3371,11 @@ static int brightness_read(char *p)
        if ((level = brightness_get(NULL)) < 0) {
                len += sprintf(p + len, "level:\t\tunreadable\n");
        } else {
-               len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
+               len += sprintf(p + len, "level:\t\t%d\n", level);
                len += sprintf(p + len, "commands:\tup, down\n");
                len += sprintf(p + len, "commands:\tlevel <level>"
-                              " (<level> is 0-7)\n");
+                              " (<level> is 0-%d)\n",
+                              (tp_features.bright_16levels) ? 15 : 7);
        }
 
        return len;
@@ -3028,28 +3384,34 @@ static int brightness_read(char *p)
 static int brightness_write(char *buf)
 {
        int level;
-       int new_level;
+       int rc;
        char *cmd;
+       int max_level = (tp_features.bright_16levels) ? 15 : 7;
 
-       while ((cmd = next_cmd(&buf))) {
-               if ((level = brightness_get(NULL)) < 0)
-                       return level;
-               level &= 7;
+       level = brightness_get(NULL);
+       if (level < 0)
+               return level;
 
+       while ((cmd = next_cmd(&buf))) {
                if (strlencmp(cmd, "up") == 0) {
-                       new_level = level == 7 ? 7 : level + 1;
+                       if (level < max_level)
+                               level++;
                } else if (strlencmp(cmd, "down") == 0) {
-                       new_level = level == 0 ? 0 : level - 1;
-               } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
-                          new_level >= 0 && new_level <= 7) {
-                       /* new_level set */
+                       if (level > 0)
+                               level--;
+               } else if (sscanf(cmd, "level %d", &level) == 1 &&
+                          level >= 0 && level <= max_level) {
+                       /* new level set */
                } else
                        return -EINVAL;
-
-               brightness_set(new_level);
        }
 
-       return 0;
+       /*
+        * Now we know what the final level should be, so we try to set it.
+        * Doing it this way makes the syscall restartable in case of EINTR
+        */
+       rc = brightness_set(level);
+       return (rc == -EINTR)? ERESTARTSYS : rc;
 }
 
 static struct ibm_struct brightness_driver_data = {
@@ -3412,9 +3774,8 @@ static ssize_t fan_pwm1_store(struct device *dev,
        /* scale down from 0-255 to 0-7 */
        newlevel = (s >> 5) & 0x07;
 
-       rc = mutex_lock_interruptible(&fan_mutex);
-       if (rc < 0)
-               return rc;
+       if (mutex_lock_interruptible(&fan_mutex))
+               return -ERESTARTSYS;
 
        rc = fan_get_status(&status);
        if (!rc && (status &
@@ -3455,7 +3816,7 @@ static struct device_attribute dev_attr_fan_fan1_input =
        __ATTR(fan1_input, S_IRUGO,
                fan_fan1_input_show, NULL);
 
-/* sysfs fan fan_watchdog (driver) ------------------------------------- */
+/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
                                     char *buf)
 {
@@ -3529,20 +3890,19 @@ static int __init fan_init(struct ibm_init_struct *iibm)
                         * Enable for TP-1Y (T43), TP-78 (R51e),
                         * TP-76 (R52), TP-70 (T43, R52), which are known
                         * to be buggy. */
-                       if (fan_control_initial_status == 0x07 &&
-                           ibm_thinkpad_ec_found &&
-                           ((ibm_thinkpad_ec_found[0] == '1' &&
-                             ibm_thinkpad_ec_found[1] == 'Y') ||
-                            (ibm_thinkpad_ec_found[0] == '7' &&
-                             (ibm_thinkpad_ec_found[1] == '6' ||
-                              ibm_thinkpad_ec_found[1] == '8' ||
-                              ibm_thinkpad_ec_found[1] == '0'))
-                           )) {
-                               printk(IBM_NOTICE
-                                      "fan_init: initial fan status is "
-                                      "unknown, assuming it is in auto "
-                                      "mode\n");
-                               tp_features.fan_ctrl_status_undef = 1;
+                       if (fan_control_initial_status == 0x07) {
+                               switch (thinkpad_id.ec_model) {
+                               case 0x5931: /* TP-1Y */
+                               case 0x3837: /* TP-78 */
+                               case 0x3637: /* TP-76 */
+                               case 0x3037: /* TP-70 */
+                                       printk(IBM_NOTICE
+                                              "fan_init: initial fan status is "
+                                              "unknown, assuming it is in auto "
+                                              "mode\n");
+                                       tp_features.fan_ctrl_status_undef = 1;
+                                       ;;
+                               }
                        }
                } else {
                        printk(IBM_ERR
@@ -3598,10 +3958,10 @@ static int __init fan_init(struct ibm_init_struct *iibm)
 
        if (fan_status_access_mode != TPACPI_FAN_NONE ||
            fan_control_access_mode != TPACPI_FAN_WR_NONE) {
-               rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+               rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
                                         &fan_attr_group);
                if (!(rc < 0))
-                       rc = driver_create_file(&tpacpi_pdriver.driver,
+                       rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
                                        &driver_attr_fan_watchdog);
                if (rc < 0)
                        return rc;
@@ -3665,9 +4025,8 @@ static int fan_get_status_safe(u8 *status)
        int rc;
        u8 s;
 
-       rc = mutex_lock_interruptible(&fan_mutex);
-       if (rc < 0)
-               return rc;
+       if (mutex_lock_interruptible(&fan_mutex))
+               return -ERESTARTSYS;
        rc = fan_get_status(&s);
        if (!rc)
                fan_update_desired_level(s);
@@ -3684,8 +4043,8 @@ static void fan_exit(void)
        vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
 
        /* FIXME: can we really do this unconditionally? */
-       sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
-       driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
+       sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
+       driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
 
        cancel_delayed_work(&fan_watchdog_task);
        flush_scheduled_work();
@@ -3718,6 +4077,9 @@ static void fan_watchdog_fire(struct work_struct *ignored)
 {
        int rc;
 
+       if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
+               return;
+
        printk(IBM_NOTICE "fan watchdog: enabling fan\n");
        rc = fan_set_enable();
        if (rc < 0) {
@@ -3738,7 +4100,8 @@ static void fan_watchdog_reset(void)
        if (fan_watchdog_active)
                cancel_delayed_work(&fan_watchdog_task);
 
-       if (fan_watchdog_maxinterval > 0) {
+       if (fan_watchdog_maxinterval > 0 &&
+           tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
                fan_watchdog_active = 1;
                if (!schedule_delayed_work(&fan_watchdog_task,
                                msecs_to_jiffies(fan_watchdog_maxinterval
@@ -3797,9 +4160,8 @@ static int fan_set_level_safe(int level)
        if (!fan_control_allowed)
                return -EPERM;
 
-       rc = mutex_lock_interruptible(&fan_mutex);
-       if (rc < 0)
-               return rc;
+       if (mutex_lock_interruptible(&fan_mutex))
+               return -ERESTARTSYS;
 
        if (level == TPACPI_FAN_LAST_LEVEL)
                level = fan_control_desired_level;
@@ -3820,9 +4182,8 @@ static int fan_set_enable(void)
        if (!fan_control_allowed)
                return -EPERM;
 
-       rc = mutex_lock_interruptible(&fan_mutex);
-       if (rc < 0)
-               return rc;
+       if (mutex_lock_interruptible(&fan_mutex))
+               return -ERESTARTSYS;
 
        switch (fan_control_access_mode) {
        case TPACPI_FAN_WR_ACPI_FANS:
@@ -3876,9 +4237,8 @@ static int fan_set_disable(void)
        if (!fan_control_allowed)
                return -EPERM;
 
-       rc = mutex_lock_interruptible(&fan_mutex);
-       if (rc < 0)
-               return rc;
+       if (mutex_lock_interruptible(&fan_mutex))
+               return -ERESTARTSYS;
 
        rc = 0;
        switch (fan_control_access_mode) {
@@ -3915,9 +4275,8 @@ static int fan_set_speed(int speed)
        if (!fan_control_allowed)
                return -EPERM;
 
-       rc = mutex_lock_interruptible(&fan_mutex);
-       if (rc < 0)
-               return rc;
+       if (mutex_lock_interruptible(&fan_mutex))
+               return -ERESTARTSYS;
 
        rc = 0;
        switch (fan_control_access_mode) {
@@ -4132,6 +4491,19 @@ static struct ibm_struct fan_driver_data = {
  ****************************************************************************
  ****************************************************************************/
 
+/* sysfs name ---------------------------------------------------------- */
+static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
+}
+
+static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
+       __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
+
+/* --------------------------------------------------------------------- */
+
 /* /proc support */
 static struct proc_dir_entry *proc_dir;
 
@@ -4276,13 +4648,30 @@ static void ibm_exit(struct ibm_struct *ibm)
 
 /* Probing */
 
-static char *ibm_thinkpad_ec_found;
-
-static char* __init check_dmi_for_ec(void)
+static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
 {
-       struct dmi_device *dev = NULL;
+       const struct dmi_device *dev = NULL;
        char ec_fw_string[18];
 
+       if (!tp)
+               return;
+
+       memset(tp, 0, sizeof(*tp));
+
+       if (dmi_name_in_vendors("IBM"))
+               tp->vendor = PCI_VENDOR_ID_IBM;
+       else if (dmi_name_in_vendors("LENOVO"))
+               tp->vendor = PCI_VENDOR_ID_LENOVO;
+       else
+               return;
+
+       tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
+                                       GFP_KERNEL);
+       if (!tp->bios_version_str)
+               return;
+       tp->bios_model = tp->bios_version_str[0]
+                        | (tp->bios_version_str[1] << 8);
+
        /*
         * ThinkPad T23 or newer, A31 or newer, R50e or newer,
         * X32 or newer, all Z series;  Some models must have an
@@ -4296,10 +4685,20 @@ static char* __init check_dmi_for_ec(void)
                           ec_fw_string) == 1) {
                        ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
                        ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
-                       return kstrdup(ec_fw_string, GFP_KERNEL);
+
+                       tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
+                       tp->ec_model = ec_fw_string[0]
+                                       | (ec_fw_string[1] << 8);
+                       break;
                }
        }
-       return NULL;
+
+       tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
+                                       GFP_KERNEL);
+       if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
+               kfree(tp->model_str);
+               tp->model_str = NULL;
+       }
 }
 
 static int __init probe_for_thinkpad(void)
@@ -4313,7 +4712,7 @@ static int __init probe_for_thinkpad(void)
         * Non-ancient models have better DMI tagging, but very old models
         * don't.
         */
-       is_thinkpad = dmi_name_in_vendors("ThinkPad");
+       is_thinkpad = (thinkpad_id.model_str != NULL);
 
        /* ec is required because many other handles are relative to it */
        IBM_ACPIHANDLE_INIT(ec);
@@ -4329,7 +4728,7 @@ static int __init probe_for_thinkpad(void)
         * false positives a damn great deal
         */
        if (!is_thinkpad)
-               is_thinkpad = dmi_name_in_vendors("IBM");
+               is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
 
        if (!is_thinkpad && !force_load)
                return -ENODEV;
@@ -4418,9 +4817,15 @@ static int __init set_ibm_param(const char *val, struct kernel_param *kp)
        unsigned int i;
        struct ibm_struct *ibm;
 
+       if (!kp || !kp->name || !val)
+               return -EINVAL;
+
        for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
                ibm = ibms_init[i].data;
-               BUG_ON(ibm == NULL);
+               WARN_ON(ibm == NULL);
+
+               if (!ibm || !ibm->name)
+                       continue;
 
                if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
                        if (strlen(val) > sizeof(ibms_init[i].param) - 2)
@@ -4441,10 +4846,19 @@ static u32 dbg_level;
 module_param_named(debug, dbg_level, uint, 0);
 
 static int force_load;
-module_param(force_load, int, 0);
+module_param(force_load, bool, 0);
 
 static int fan_control_allowed;
-module_param_named(fan_control, fan_control_allowed, int, 0);
+module_param_named(fan_control, fan_control_allowed, bool, 0);
+
+static int brightness_mode;
+module_param_named(brightness_mode, brightness_mode, int, 0);
+
+static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
+module_param(brightness_enable, uint, 0);
+
+static unsigned int hotkey_report_mode;
+module_param(hotkey_report_mode, uint, 0);
 
 #define IBM_PARAM(feature) \
        module_param_call(feature, set_ibm_param, NULL, NULL, 0)
@@ -4471,13 +4885,23 @@ static int __init thinkpad_acpi_module_init(void)
 {
        int ret, i;
 
+       tpacpi_lifecycle = TPACPI_LIFE_INIT;
+
+       /* Parameter checking */
+       if (hotkey_report_mode > 2)
+               return -EINVAL;
+
        /* Driver-level probe */
+
+       get_thinkpad_model_data(&thinkpad_id);
        ret = probe_for_thinkpad();
-       if (ret)
+       if (ret) {
+               thinkpad_acpi_module_exit();
                return ret;
+       }
 
        /* Driver initialization */
-       ibm_thinkpad_ec_found = check_dmi_for_ec();
+
        IBM_ACPIHANDLE_INIT(ecrd);
        IBM_ACPIHANDLE_INIT(ecwr);
 
@@ -4491,16 +4915,31 @@ static int __init thinkpad_acpi_module_init(void)
 
        ret = platform_driver_register(&tpacpi_pdriver);
        if (ret) {
-               printk(IBM_ERR "unable to register platform driver\n");
+               printk(IBM_ERR "unable to register main platform driver\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+       tp_features.platform_drv_registered = 1;
+
+       ret = platform_driver_register(&tpacpi_hwmon_pdriver);
+       if (ret) {
+               printk(IBM_ERR "unable to register hwmon platform driver\n");
                thinkpad_acpi_module_exit();
                return ret;
        }
+       tp_features.sensors_pdrv_registered = 1;
+
        ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
+       if (!ret) {
+               tp_features.platform_drv_attrs_registered = 1;
+               ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
+       }
        if (ret) {
                printk(IBM_ERR "unable to create sysfs driver attributes\n");
                thinkpad_acpi_module_exit();
                return ret;
        }
+       tp_features.sensors_pdrv_attrs_registered = 1;
 
 
        /* Device initialization */
@@ -4513,7 +4952,26 @@ static int __init thinkpad_acpi_module_init(void)
                thinkpad_acpi_module_exit();
                return ret;
        }
-       tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
+       tpacpi_sensors_pdev = platform_device_register_simple(
+                                                       IBM_HWMON_DRVR_NAME,
+                                                       -1, NULL, 0);
+       if (IS_ERR(tpacpi_sensors_pdev)) {
+               ret = PTR_ERR(tpacpi_sensors_pdev);
+               tpacpi_sensors_pdev = NULL;
+               printk(IBM_ERR "unable to register hwmon platform device\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+       ret = device_create_file(&tpacpi_sensors_pdev->dev,
+                                &dev_attr_thinkpad_acpi_pdev_name);
+       if (ret) {
+               printk(IBM_ERR
+                       "unable to create sysfs hwmon device attributes\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+       tp_features.sensors_pdev_attrs_registered = 1;
+       tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
        if (IS_ERR(tpacpi_hwmon)) {
                ret = PTR_ERR(tpacpi_hwmon);
                tpacpi_hwmon = NULL;
@@ -4521,6 +4979,7 @@ static int __init thinkpad_acpi_module_init(void)
                thinkpad_acpi_module_exit();
                return ret;
        }
+       mutex_init(&tpacpi_inputdev_send_mutex);
        tpacpi_inputdev = input_allocate_device();
        if (!tpacpi_inputdev) {
                printk(IBM_ERR "unable to allocate input device\n");
@@ -4531,7 +4990,9 @@ static int __init thinkpad_acpi_module_init(void)
                tpacpi_inputdev->name = "ThinkPad Extra Buttons";
                tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
                tpacpi_inputdev->id.bustype = BUS_HOST;
-               tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
+               tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
+                                               thinkpad_id.vendor :
+                                               PCI_VENDOR_ID_IBM;
                tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
                tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
        }
@@ -4553,6 +5014,7 @@ static int __init thinkpad_acpi_module_init(void)
                tp_features.input_device_registered = 1;
        }
 
+       tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
        return 0;
 }
 
@@ -4560,6 +5022,8 @@ static void thinkpad_acpi_module_exit(void)
 {
        struct ibm_struct *ibm, *itmp;
 
+       tpacpi_lifecycle = TPACPI_LIFE_EXITING;
+
        list_for_each_entry_safe_reverse(ibm, itmp,
                                         &tpacpi_all_drivers,
                                         all_drivers) {
@@ -4578,16 +5042,31 @@ static void thinkpad_acpi_module_exit(void)
        if (tpacpi_hwmon)
                hwmon_device_unregister(tpacpi_hwmon);
 
+       if (tp_features.sensors_pdev_attrs_registered)
+               device_remove_file(&tpacpi_sensors_pdev->dev,
+                                  &dev_attr_thinkpad_acpi_pdev_name);
+       if (tpacpi_sensors_pdev)
+               platform_device_unregister(tpacpi_sensors_pdev);
        if (tpacpi_pdev)
                platform_device_unregister(tpacpi_pdev);
 
-       tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
-       platform_driver_unregister(&tpacpi_pdriver);
+       if (tp_features.sensors_pdrv_attrs_registered)
+               tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
+       if (tp_features.platform_drv_attrs_registered)
+               tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
+
+       if (tp_features.sensors_pdrv_registered)
+               platform_driver_unregister(&tpacpi_hwmon_pdriver);
+
+       if (tp_features.platform_drv_registered)
+               platform_driver_unregister(&tpacpi_pdriver);
 
        if (proc_dir)
                remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
 
-       kfree(ibm_thinkpad_ec_found);
+       kfree(thinkpad_id.bios_version_str);
+       kfree(thinkpad_id.ec_version_str);
+       kfree(thinkpad_id.model_str);
 }
 
 module_init(thinkpad_acpi_module_init);