ACPI: thinkpad-acpi: make sure DSDT TMPx readings don't return +128
[pandora-kernel.git] / drivers / misc / thinkpad_acpi.c
index 809ec84..f74d7d6 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #define IBM_VERSION "0.14"
+#define TPACPI_SYSFS_VERSION 0x010000
 
 /*
  *  Changelog:
@@ -91,6 +92,29 @@ MODULE_LICENSE("GPL");
 /* Please remove this in year 2009 */
 MODULE_ALIAS("ibm_acpi");
 
+/*
+ * DMI matching for module autoloading
+ *
+ * See http://thinkwiki.org/wiki/List_of_DMI_IDs
+ * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
+ *
+ * Only models listed in thinkwiki will be supported, so add yours
+ * if it is not there yet.
+ */
+#define IBM_BIOS_MODULE_ALIAS(__type) \
+       MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
+
+/* Non-ancient thinkpads */
+MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
+MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
+
+/* Ancient thinkpad BIOSes have to be identified by
+ * BIOS type or model number, and there are far less
+ * BIOS types than model numbers... */
+IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
+IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
+IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
+
 #define __unused __attribute__ ((unused))
 
 /****************************************************************************
@@ -105,7 +129,7 @@ MODULE_ALIAS("ibm_acpi");
  * ACPI basic handles
  */
 
-static acpi_handle root_handle = NULL;
+static acpi_handle root_handle;
 
 #define IBM_HANDLE(object, parent, paths...)                   \
        static acpi_handle  object##_handle;                    \
@@ -273,6 +297,17 @@ static int _sta(acpi_handle handle)
        return status;
 }
 
+static int issue_thinkpad_cmos_command(int cmos_cmd)
+{
+       if (!cmos_handle)
+               return -ENXIO;
+
+       if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
+               return -EIO;
+
+       return 0;
+}
+
 /*************************************************************************
  * ACPI device model
  */
@@ -284,14 +319,22 @@ static void drv_acpi_handle_init(char *name,
        int i;
        acpi_status status;
 
+       vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
+               name);
+
        for (i = 0; i < num_paths; i++) {
                status = acpi_get_handle(parent, paths[i], handle);
                if (ACPI_SUCCESS(status)) {
                        *path = paths[i];
+                       dbg_printk(TPACPI_DBG_INIT,
+                                  "Found ACPI handle %s for %s\n",
+                                  *path, name);
                        return;
                }
        }
 
+       vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
+                   name);
        *handle = NULL;
 }
 
@@ -308,19 +351,20 @@ static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
 static int __init setup_acpi_notify(struct ibm_struct *ibm)
 {
        acpi_status status;
-       int ret;
+       int rc;
 
        BUG_ON(!ibm->acpi);
 
        if (!*ibm->acpi->handle)
                return 0;
 
-       dbg_printk(TPACPI_DBG_INIT,
+       vdbg_printk(TPACPI_DBG_INIT,
                "setting up ACPI notify for %s\n", ibm->name);
 
-       ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
-       if (ret < 0) {
-               printk(IBM_ERR "%s device not present\n", ibm->name);
+       rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
+       if (rc < 0) {
+               printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
+                       ibm->name, rc);
                return -ENODEV;
        }
 
@@ -352,7 +396,7 @@ static int __init tpacpi_device_add(struct acpi_device *device)
 
 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
 {
-       int ret;
+       int rc;
 
        dbg_printk(TPACPI_DBG_INIT,
                "registering %s as an ACPI driver\n", ibm->name);
@@ -369,16 +413,16 @@ static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
        ibm->acpi->driver->ids = ibm->acpi->hid;
        ibm->acpi->driver->ops.add = &tpacpi_device_add;
 
-       ret = acpi_bus_register_driver(ibm->acpi->driver);
-       if (ret < 0) {
+       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, ret);
+                      ibm->acpi->hid, rc);
                kfree(ibm->acpi->driver);
                ibm->acpi->driver = NULL;
-       } else if (!ret)
+       } else if (!rc)
                ibm->flags.acpi_driver_registered = 1;
 
-       return ret;
+       return rc;
 }
 
 
@@ -463,6 +507,199 @@ static char *next_cmd(char **cmds)
 }
 
 
+/****************************************************************************
+ ****************************************************************************
+ *
+ * Device model: input, hwmon and platform
+ *
+ ****************************************************************************
+ ****************************************************************************/
+
+static struct platform_device *tpacpi_pdev;
+static struct class_device *tpacpi_hwmon;
+static struct input_dev *tpacpi_inputdev;
+
+
+static int tpacpi_resume_handler(struct platform_device *pdev)
+{
+       struct ibm_struct *ibm, *itmp;
+
+       list_for_each_entry_safe(ibm, itmp,
+                                &tpacpi_all_drivers,
+                                all_drivers) {
+               if (ibm->resume)
+                       (ibm->resume)();
+       }
+
+       return 0;
+}
+
+static struct platform_driver tpacpi_pdriver = {
+       .driver = {
+               .name = IBM_DRVR_NAME,
+               .owner = THIS_MODULE,
+       },
+       .resume = tpacpi_resume_handler,
+};
+
+
+/*************************************************************************
+ * thinkpad-acpi driver attributes
+ */
+
+/* interface_version --------------------------------------------------- */
+static ssize_t tpacpi_driver_interface_version_show(
+                               struct device_driver *drv,
+                               char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
+}
+
+static DRIVER_ATTR(interface_version, S_IRUGO,
+               tpacpi_driver_interface_version_show, NULL);
+
+/* debug_level --------------------------------------------------------- */
+static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
+                                               char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
+}
+
+static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
+                                               const char *buf, size_t count)
+{
+       unsigned long t;
+
+       if (parse_strtoul(buf, 0xffff, &t))
+               return -EINVAL;
+
+       dbg_level = t;
+
+       return count;
+}
+
+static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
+               tpacpi_driver_debug_show, tpacpi_driver_debug_store);
+
+/* version ------------------------------------------------------------- */
+static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
+                                               char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
+}
+
+static DRIVER_ATTR(version, S_IRUGO,
+               tpacpi_driver_version_show, NULL);
+
+/* --------------------------------------------------------------------- */
+
+static struct driver_attribute* tpacpi_driver_attributes[] = {
+       &driver_attr_debug_level, &driver_attr_version,
+       &driver_attr_interface_version,
+};
+
+static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
+{
+       int i, res;
+
+       i = 0;
+       res = 0;
+       while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
+               res = driver_create_file(drv, tpacpi_driver_attributes[i]);
+               i++;
+       }
+
+       return res;
+}
+
+static void tpacpi_remove_driver_attributes(struct device_driver *drv)
+{
+       int i;
+
+       for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
+               driver_remove_file(drv, tpacpi_driver_attributes[i]);
+}
+
+/*************************************************************************
+ * sysfs support helpers
+ */
+
+struct attribute_set_obj {
+       struct attribute_set s;
+       struct attribute *a;
+} __attribute__((packed));
+
+static struct attribute_set *create_attr_set(unsigned int max_members,
+                                               const char* name)
+{
+       struct attribute_set_obj *sobj;
+
+       if (max_members == 0)
+               return NULL;
+
+       /* Allocates space for implicit NULL at the end too */
+       sobj = kzalloc(sizeof(struct attribute_set_obj) +
+                   max_members * sizeof(struct attribute *),
+                   GFP_KERNEL);
+       if (!sobj)
+               return NULL;
+       sobj->s.max_members = max_members;
+       sobj->s.group.attrs = &sobj->a;
+       sobj->s.group.name = name;
+
+       return &sobj->s;
+}
+
+/* not multi-threaded safe, use it in a single thread per set */
+static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
+{
+       if (!s || !attr)
+               return -EINVAL;
+
+       if (s->members >= s->max_members)
+               return -ENOMEM;
+
+       s->group.attrs[s->members] = attr;
+       s->members++;
+
+       return 0;
+}
+
+static int add_many_to_attr_set(struct attribute_set* s,
+                       struct attribute **attr,
+                       unsigned int count)
+{
+       int i, res;
+
+       for (i = 0; i < count; i++) {
+               res = add_to_attr_set(s, attr[i]);
+               if (res)
+                       return res;
+       }
+
+       return 0;
+}
+
+static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
+{
+       sysfs_remove_group(kobj, &s->group);
+       destroy_attr_set(s);
+}
+
+static int parse_strtoul(const char *buf,
+               unsigned long max, unsigned long *value)
+{
+       char *endp;
+
+       *value = simple_strtoul(buf, &endp, 0);
+       while (*endp && isspace(*endp))
+               endp++;
+       if (*endp || *value > max)
+               return -EINVAL;
+
+       return 0;
+}
+
 /****************************************************************************
  ****************************************************************************
  *
@@ -480,9 +717,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;
 }
@@ -507,13 +754,230 @@ static struct ibm_struct thinkpad_acpi_driver_data = {
  */
 
 static int hotkey_orig_status;
-static int hotkey_orig_mask;
+static u32 hotkey_orig_mask;
+static u32 hotkey_all_mask;
+static u32 hotkey_reserved_mask;
+
+static u16 *hotkey_keycode_map;
+
+static struct attribute_set *hotkey_dev_attributes;
+
+static int hotkey_get_wlsw(int *status)
+{
+       if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
+               return -EIO;
+       return 0;
+}
+
+/* sysfs hotkey enable ------------------------------------------------- */
+static ssize_t hotkey_enable_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int res, status;
+       u32 mask;
+
+       res = hotkey_get(&status, &mask);
+       if (res)
+               return res;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", status);
+}
+
+static ssize_t hotkey_enable_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
+{
+       unsigned long t;
+       int res, status;
+       u32 mask;
+
+       if (parse_strtoul(buf, 1, &t))
+               return -EINVAL;
+
+       res = hotkey_get(&status, &mask);
+       if (!res)
+               res = hotkey_set(t, mask);
+
+       return (res) ? res : count;
+}
+
+static struct device_attribute dev_attr_hotkey_enable =
+       __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
+               hotkey_enable_show, hotkey_enable_store);
+
+/* sysfs hotkey mask --------------------------------------------------- */
+static ssize_t hotkey_mask_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int res, status;
+       u32 mask;
+
+       res = hotkey_get(&status, &mask);
+       if (res)
+               return res;
+
+       return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
+}
+
+static ssize_t hotkey_mask_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
+{
+       unsigned long t;
+       int res, status;
+       u32 mask;
+
+       if (parse_strtoul(buf, 0xffffffffUL, &t))
+               return -EINVAL;
+
+       res = hotkey_get(&status, &mask);
+       if (!res)
+               hotkey_set(status, t);
+
+       return (res) ? res : count;
+}
+
+static struct device_attribute dev_attr_hotkey_mask =
+       __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
+               hotkey_mask_show, hotkey_mask_store);
+
+/* sysfs hotkey bios_enabled ------------------------------------------- */
+static ssize_t hotkey_bios_enabled_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
+}
+
+static struct device_attribute dev_attr_hotkey_bios_enabled =
+       __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
+
+/* sysfs hotkey bios_mask ---------------------------------------------- */
+static ssize_t hotkey_bios_mask_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
+}
+
+static struct device_attribute dev_attr_hotkey_bios_mask =
+       __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
+
+/* sysfs hotkey all_mask ----------------------------------------------- */
+static ssize_t hotkey_all_mask_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
+}
+
+static struct device_attribute dev_attr_hotkey_all_mask =
+       __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
+
+/* sysfs hotkey recommended_mask --------------------------------------- */
+static ssize_t hotkey_recommended_mask_show(struct device *dev,
+                                           struct device_attribute *attr,
+                                           char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "0x%08x\n",
+                       hotkey_all_mask & ~hotkey_reserved_mask);
+}
+
+static struct device_attribute dev_attr_hotkey_recommended_mask =
+       __ATTR(hotkey_recommended_mask, S_IRUGO,
+               hotkey_recommended_mask_show, NULL);
+
+/* sysfs hotkey radio_sw ----------------------------------------------- */
+static ssize_t hotkey_radio_sw_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int res, s;
+       res = hotkey_get_wlsw(&s);
+       if (res < 0)
+               return res;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
+}
+
+static struct device_attribute dev_attr_hotkey_radio_sw =
+       __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
+
+/* --------------------------------------------------------------------- */
+
+static struct attribute *hotkey_mask_attributes[] = {
+       &dev_attr_hotkey_mask.attr,
+       &dev_attr_hotkey_bios_enabled.attr,
+       &dev_attr_hotkey_bios_mask.attr,
+       &dev_attr_hotkey_all_mask.attr,
+       &dev_attr_hotkey_recommended_mask.attr,
+};
 
 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;
+
        vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
 
+       BUG_ON(!tpacpi_inputdev);
+
        IBM_ACPIHANDLE_INIT(hkey);
+       mutex_init(&hotkey_mutex);
 
        /* hotkey not supported on 570 */
        tp_features.hotkey = hkey_handle != NULL;
@@ -522,6 +986,14 @@ 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);
+               if (!hotkey_dev_attributes)
+                       return -ENOMEM;
+               res = add_to_attr_set(hotkey_dev_attributes,
+                               &dev_attr_hotkey_enable.attr);
+               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 =
@@ -530,8 +1002,92 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
                vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
                        str_supported(tp_features.hotkey_mask));
 
-               if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
-                       return -ENODEV;
+               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"))
+                               hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
+               }
+
+               res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
+               if (!res && tp_features.hotkey_mask) {
+                       res = add_many_to_attr_set(hotkey_dev_attributes,
+                               hotkey_mask_attributes,
+                               ARRAY_SIZE(hotkey_mask_attributes));
+               }
+
+               /* Not all thinkpads have a hardware radio switch */
+               if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
+                       tp_features.hotkey_wlsw = 1;
+                       printk(IBM_INFO
+                               "radio switch found; radios are %s\n",
+                               enabled(status, 0));
+                       res = add_to_attr_set(hotkey_dev_attributes,
+                                       &dev_attr_hotkey_radio_sw.attr);
+               }
+
+               if (!res)
+                       res = register_attr_set_with_sysfs(
+                                       hotkey_dev_attributes,
+                                       &tpacpi_pdev->dev.kobj);
+               if (res)
+                       return res;
+
+               /* 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);
+               }
+
+#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_bit(EV_KEY, tpacpi_inputdev->evbit);
+               set_bit(EV_MSC, tpacpi_inputdev->evbit);
+               set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
+               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);
+                       } else {
+                               if (i < sizeof(hotkey_reserved_mask)*8)
+                                       hotkey_reserved_mask |= 1 << i;
+                       }
+               }
+
+               if (tp_features.hotkey_wlsw) {
+                       set_bit(EV_SW, tpacpi_inputdev->evbit);
+                       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 */
        }
 
        return (tp_features.hotkey)? 0 : 1;
@@ -539,57 +1095,153 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
 
 static void hotkey_exit(void)
 {
+       int res;
+
        if (tp_features.hotkey) {
                dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
-               hotkey_set(hotkey_orig_status, hotkey_orig_mask);
+               res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
+               if (res)
+                       printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
+       }
+
+       if (hotkey_dev_attributes) {
+               delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
+               hotkey_dev_attributes = NULL;
+       }
+}
+
+static void tpacpi_input_send_key(unsigned int scancode,
+                                 unsigned int keycode)
+{
+       if (keycode != KEY_RESERVED) {
+               input_report_key(tpacpi_inputdev, keycode, 1);
+               if (keycode == KEY_UNKNOWN)
+                       input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
+                                   scancode);
+               input_sync(tpacpi_inputdev);
+
+               input_report_key(tpacpi_inputdev, keycode, 0);
+               if (keycode == KEY_UNKNOWN)
+                       input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
+                                   scancode);
+               input_sync(tpacpi_inputdev);
        }
 }
 
+static void tpacpi_input_send_radiosw(void)
+{
+       int wlsw;
+
+       if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
+               input_report_switch(tpacpi_inputdev,
+                                   SW_RADIO, !!wlsw);
+}
+
 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
 {
-       int hkey;
+       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);
+                               }
+                               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);
+                       }
+               }
 
-       if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
-               acpi_bus_generate_event(ibm->acpi->device, event, hkey);
-       else {
-               printk(IBM_ERR "unknown hotkey event %d\n", event);
+               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);
        }
 }
 
-static int hotkey_get(int *status, int *mask)
+static void hotkey_resume(void)
+{
+       tpacpi_input_send_radiosw();
+}
+
+/*
+ * Call with hotkey_mutex held
+ */
+static int hotkey_get(int *status, u32 *mask)
 {
        if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
-               return 0;
+               return -EIO;
 
        if (tp_features.hotkey_mask)
                if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
-                       return 0;
+                       return -EIO;
 
-       return 1;
+       return 0;
 }
 
-static int hotkey_set(int status, int mask)
+/*
+ * Call with hotkey_mutex held
+ */
+static int hotkey_set(int status, u32 mask)
 {
        int i;
 
        if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
-               return 0;
+               return -EIO;
 
        if (tp_features.hotkey_mask)
                for (i = 0; i < 32; i++) {
                        int bit = ((1 << i) & mask) != 0;
                        if (!acpi_evalf(hkey_handle,
                                        NULL, "MHKM", "vdd", i + 1, bit))
-                               return 0;
+                               return -EIO;
                }
 
-       return 1;
+       return 0;
 }
 
+/* procfs -------------------------------------------------------------- */
 static int hotkey_read(char *p)
 {
-       int status, mask;
+       int res, status;
+       u32 mask;
        int len = 0;
 
        if (!tp_features.hotkey) {
@@ -597,12 +1249,17 @@ static int hotkey_read(char *p)
                return len;
        }
 
-       if (!hotkey_get(&status, &mask))
-               return -EIO;
+       res = mutex_lock_interruptible(&hotkey_mutex);
+       if (res < 0)
+               return res;
+       res = hotkey_get(&status, &mask);
+       mutex_unlock(&hotkey_mutex);
+       if (res)
+               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%04x\n", mask);
+               len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
                len += sprintf(p + len,
                               "commands:\tenable, disable, reset, <mask>\n");
        } else {
@@ -615,16 +1272,23 @@ static int hotkey_read(char *p)
 
 static int hotkey_write(char *buf)
 {
-       int status, mask;
+       int res, status;
+       u32 mask;
        char *cmd;
        int do_cmd = 0;
 
        if (!tp_features.hotkey)
                return -ENODEV;
 
-       if (!hotkey_get(&status, &mask))
-               return -EIO;
+       res = mutex_lock_interruptible(&hotkey_mutex);
+       if (res < 0)
+               return res;
 
+       res = hotkey_get(&status, &mask);
+       if (res)
+               goto errexit;
+
+       res = 0;
        while ((cmd = next_cmd(&buf))) {
                if (strlencmp(cmd, "enable") == 0) {
                        status = 1;
@@ -637,15 +1301,19 @@ static int hotkey_write(char *buf)
                        /* mask set */
                } else if (sscanf(cmd, "%x", &mask) == 1) {
                        /* mask set */
-               } else
-                       return -EINVAL;
+               } else {
+                       res = -EINVAL;
+                       goto errexit;
+               }
                do_cmd = 1;
        }
 
-       if (do_cmd && !hotkey_set(status, mask))
-               return -EIO;
+       if (do_cmd)
+               res = hotkey_set(status, mask);
 
-       return 0;
+errexit:
+       mutex_unlock(&hotkey_mutex);
+       return res;
 }
 
 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
@@ -660,6 +1328,7 @@ static struct ibm_struct hotkey_driver_data = {
        .read = hotkey_read,
        .write = hotkey_write,
        .exit = hotkey_exit,
+       .resume = hotkey_resume,
        .acpi = &ibm_hotkey_acpidriver,
 };
 
@@ -667,8 +1336,55 @@ static struct ibm_struct hotkey_driver_data = {
  * Bluetooth subdriver
  */
 
+/* sysfs bluetooth enable ---------------------------------------------- */
+static ssize_t bluetooth_enable_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int status;
+
+       status = bluetooth_get_radiosw();
+       if (status < 0)
+               return status;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
+}
+
+static ssize_t bluetooth_enable_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
+{
+       unsigned long t;
+       int res;
+
+       if (parse_strtoul(buf, 1, &t))
+               return -EINVAL;
+
+       res = bluetooth_set_radiosw(t);
+
+       return (res) ? res : count;
+}
+
+static struct device_attribute dev_attr_bluetooth_enable =
+       __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
+               bluetooth_enable_show, bluetooth_enable_store);
+
+/* --------------------------------------------------------------------- */
+
+static struct attribute *bluetooth_attributes[] = {
+       &dev_attr_bluetooth_enable.attr,
+       NULL
+};
+
+static const struct attribute_group bluetooth_attr_group = {
+       .attrs = bluetooth_attributes,
+};
+
 static int __init bluetooth_init(struct ibm_init_struct *iibm)
 {
+       int res;
+       int status = 0;
+
        vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
 
        IBM_ACPIHANDLE_INIT(hkey);
@@ -676,36 +1392,78 @@ static int __init bluetooth_init(struct ibm_init_struct *iibm)
        /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
           G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
        tp_features.bluetooth = hkey_handle &&
-           acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
-
-       vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s\n",
-               str_supported(tp_features.bluetooth));
+           acpi_evalf(hkey_handle, &status, "GBDC", "qd");
+
+       vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
+               str_supported(tp_features.bluetooth),
+               status);
+
+       if (tp_features.bluetooth) {
+               if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
+                       /* no bluetooth hardware present in system */
+                       tp_features.bluetooth = 0;
+                       dbg_printk(TPACPI_DBG_INIT,
+                                  "bluetooth hardware not installed\n");
+               } else {
+                       res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+                                       &bluetooth_attr_group);
+                       if (res)
+                               return res;
+               }
+       }
 
        return (tp_features.bluetooth)? 0 : 1;
 }
 
-static int bluetooth_status(void)
+static void bluetooth_exit(void)
+{
+       sysfs_remove_group(&tpacpi_pdev->dev.kobj,
+                       &bluetooth_attr_group);
+}
+
+static int bluetooth_get_radiosw(void)
 {
        int status;
 
-       if (!tp_features.bluetooth ||
-           !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
-               status = 0;
+       if (!tp_features.bluetooth)
+               return -ENODEV;
 
-       return status;
+       if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
+               return -EIO;
+
+       return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
 }
 
+static int bluetooth_set_radiosw(int radio_on)
+{
+       int status;
+
+       if (!tp_features.bluetooth)
+               return -ENODEV;
+
+       if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
+               return -EIO;
+       if (radio_on)
+               status |= TP_ACPI_BLUETOOTH_RADIOSSW;
+       else
+               status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
+       if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
+               return -EIO;
+
+       return 0;
+}
+
+/* procfs -------------------------------------------------------------- */
 static int bluetooth_read(char *p)
 {
        int len = 0;
-       int status = bluetooth_status();
+       int status = bluetooth_get_radiosw();
 
        if (!tp_features.bluetooth)
                len += sprintf(p + len, "status:\t\tnot supported\n");
-       else if (!(status & 1))
-               len += sprintf(p + len, "status:\t\tnot installed\n");
        else {
-               len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
+               len += sprintf(p + len, "status:\t\t%s\n",
+                               (status)? "enabled" : "disabled");
                len += sprintf(p + len, "commands:\tenable, disable\n");
        }
 
@@ -714,76 +1472,160 @@ static int bluetooth_read(char *p)
 
 static int bluetooth_write(char *buf)
 {
-       int status = bluetooth_status();
        char *cmd;
-       int do_cmd = 0;
 
        if (!tp_features.bluetooth)
                return -ENODEV;
 
        while ((cmd = next_cmd(&buf))) {
                if (strlencmp(cmd, "enable") == 0) {
-                       status |= 2;
+                       bluetooth_set_radiosw(1);
                } else if (strlencmp(cmd, "disable") == 0) {
-                       status &= ~2;
+                       bluetooth_set_radiosw(0);
                } else
                        return -EINVAL;
-               do_cmd = 1;
        }
 
-       if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
-               return -EIO;
+       return 0;
+}
+
+static struct ibm_struct bluetooth_driver_data = {
+       .name = "bluetooth",
+       .read = bluetooth_read,
+       .write = bluetooth_write,
+       .exit = bluetooth_exit,
+};
+
+/*************************************************************************
+ * Wan subdriver
+ */
+
+/* sysfs wan enable ---------------------------------------------------- */
+static ssize_t wan_enable_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int status;
+
+       status = wan_get_radiosw();
+       if (status < 0)
+               return status;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
+}
+
+static ssize_t wan_enable_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
+{
+       unsigned long t;
+       int res;
+
+       if (parse_strtoul(buf, 1, &t))
+               return -EINVAL;
+
+       res = wan_set_radiosw(t);
 
-       return 0;
+       return (res) ? res : count;
 }
 
-static struct ibm_struct bluetooth_driver_data = {
-       .name = "bluetooth",
-       .read = bluetooth_read,
-       .write = bluetooth_write,
+static struct device_attribute dev_attr_wan_enable =
+       __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
+               wan_enable_show, wan_enable_store);
+
+/* --------------------------------------------------------------------- */
+
+static struct attribute *wan_attributes[] = {
+       &dev_attr_wan_enable.attr,
+       NULL
 };
 
-/*************************************************************************
- * Wan subdriver
- */
+static const struct attribute_group wan_attr_group = {
+       .attrs = wan_attributes,
+};
 
 static int __init wan_init(struct ibm_init_struct *iibm)
 {
+       int res;
+       int status = 0;
+
        vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
 
        IBM_ACPIHANDLE_INIT(hkey);
 
        tp_features.wan = hkey_handle &&
-                         acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
-
-       vdbg_printk(TPACPI_DBG_INIT, "wan is %s\n",
-               str_supported(tp_features.wan));
+           acpi_evalf(hkey_handle, &status, "GWAN", "qd");
+
+       vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
+               str_supported(tp_features.wan),
+               status);
+
+       if (tp_features.wan) {
+               if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
+                       /* no wan hardware present in system */
+                       tp_features.wan = 0;
+                       dbg_printk(TPACPI_DBG_INIT,
+                                  "wan hardware not installed\n");
+               } else {
+                       res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+                                       &wan_attr_group);
+                       if (res)
+                               return res;
+               }
+       }
 
        return (tp_features.wan)? 0 : 1;
 }
 
-static int wan_status(void)
+static void wan_exit(void)
+{
+       sysfs_remove_group(&tpacpi_pdev->dev.kobj,
+               &wan_attr_group);
+}
+
+static int wan_get_radiosw(void)
 {
        int status;
 
-       if (!tp_features.wan ||
-           !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
-               status = 0;
+       if (!tp_features.wan)
+               return -ENODEV;
 
-       return status;
+       if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
+               return -EIO;
+
+       return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
+}
+
+static int wan_set_radiosw(int radio_on)
+{
+       int status;
+
+       if (!tp_features.wan)
+               return -ENODEV;
+
+       if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
+               return -EIO;
+       if (radio_on)
+               status |= TP_ACPI_WANCARD_RADIOSSW;
+       else
+               status &= ~TP_ACPI_WANCARD_RADIOSSW;
+       if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
+               return -EIO;
+
+       return 0;
 }
 
+/* procfs -------------------------------------------------------------- */
 static int wan_read(char *p)
 {
        int len = 0;
-       int status = wan_status();
+       int status = wan_get_radiosw();
 
        if (!tp_features.wan)
                len += sprintf(p + len, "status:\t\tnot supported\n");
-       else if (!(status & 1))
-               len += sprintf(p + len, "status:\t\tnot installed\n");
        else {
-               len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
+               len += sprintf(p + len, "status:\t\t%s\n",
+                               (status)? "enabled" : "disabled");
                len += sprintf(p + len, "commands:\tenable, disable\n");
        }
 
@@ -792,26 +1634,20 @@ static int wan_read(char *p)
 
 static int wan_write(char *buf)
 {
-       int status = wan_status();
        char *cmd;
-       int do_cmd = 0;
 
        if (!tp_features.wan)
                return -ENODEV;
 
        while ((cmd = next_cmd(&buf))) {
                if (strlencmp(cmd, "enable") == 0) {
-                       status |= 2;
+                       wan_set_radiosw(1);
                } else if (strlencmp(cmd, "disable") == 0) {
-                       status &= ~2;
+                       wan_set_radiosw(0);
                } else
                        return -EINVAL;
-               do_cmd = 1;
        }
 
-       if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
-               return -EIO;
-
        return 0;
 }
 
@@ -819,6 +1655,7 @@ static struct ibm_struct wan_driver_data = {
        .name = "wan",
        .read = wan_read,
        .write = wan_write,
+       .exit = wan_exit,
        .flags.experimental = 1,
 };
 
@@ -873,111 +1710,194 @@ static int __init video_init(struct ibm_init_struct *iibm)
 
 static void video_exit(void)
 {
-       dbg_printk(TPACPI_DBG_EXIT, "restoring original video autoswitch mode\n");
-       acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
+       dbg_printk(TPACPI_DBG_EXIT,
+                  "restoring original video autoswitch mode\n");
+       if (video_autosw_set(video_orig_autosw))
+               printk(IBM_ERR "error while trying to restore original "
+                       "video autoswitch mode\n");
 }
 
-static int video_status(void)
+static int video_outputsw_get(void)
 {
        int status = 0;
        int i;
 
-       if (video_supported == TPACPI_VIDEO_570) {
-               if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
-                       status = i & 3;
-       } else if (video_supported == TPACPI_VIDEO_770) {
-               if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
-                       status |= 0x01 * i;
-               if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
-                       status |= 0x02 * i;
-       } else if (video_supported == TPACPI_VIDEO_NEW) {
-               acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
-               if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
-                       status |= 0x02 * i;
-
-               acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
-               if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
-                       status |= 0x01 * i;
-               if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
-                       status |= 0x08 * i;
+       switch (video_supported) {
+       case TPACPI_VIDEO_570:
+               if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
+                                TP_ACPI_VIDEO_570_PHSCMD))
+                       return -EIO;
+               status = i & TP_ACPI_VIDEO_570_PHSMASK;
+               break;
+       case TPACPI_VIDEO_770:
+               if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
+                       return -EIO;
+               if (i)
+                       status |= TP_ACPI_VIDEO_S_LCD;
+               if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
+                       return -EIO;
+               if (i)
+                       status |= TP_ACPI_VIDEO_S_CRT;
+               break;
+       case TPACPI_VIDEO_NEW:
+               if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
+                   !acpi_evalf(NULL, &i, "\\VCDC", "d"))
+                       return -EIO;
+               if (i)
+                       status |= TP_ACPI_VIDEO_S_CRT;
+
+               if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
+                   !acpi_evalf(NULL, &i, "\\VCDL", "d"))
+                       return -EIO;
+               if (i)
+                       status |= TP_ACPI_VIDEO_S_LCD;
+               if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
+                       return -EIO;
+               if (i)
+                       status |= TP_ACPI_VIDEO_S_DVI;
+               break;
+       default:
+               return -ENOSYS;
        }
 
        return status;
 }
 
-static int video_autosw(void)
+static int video_outputsw_set(int status)
 {
-       int autosw = 0;
+       int autosw;
+       int res = 0;
+
+       switch (video_supported) {
+       case TPACPI_VIDEO_570:
+               res = acpi_evalf(NULL, NULL,
+                                "\\_SB.PHS2", "vdd",
+                                TP_ACPI_VIDEO_570_PHS2CMD,
+                                status | TP_ACPI_VIDEO_570_PHS2SET);
+               break;
+       case TPACPI_VIDEO_770:
+               autosw = video_autosw_get();
+               if (autosw < 0)
+                       return autosw;
 
-       if (video_supported == TPACPI_VIDEO_570)
-               acpi_evalf(vid_handle, &autosw, "SWIT", "d");
-       else if (video_supported == TPACPI_VIDEO_770 ||
-                video_supported == TPACPI_VIDEO_NEW)
-               acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
+               res = video_autosw_set(1);
+               if (res)
+                       return res;
+               res = acpi_evalf(vid_handle, NULL,
+                                "ASWT", "vdd", status * 0x100, 0);
+               if (!autosw && video_autosw_set(autosw)) {
+                       printk(IBM_ERR "video auto-switch left enabled due to error\n");
+                       return -EIO;
+               }
+               break;
+       case TPACPI_VIDEO_NEW:
+               res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
+                       acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
+               break;
+       default:
+               return -ENOSYS;
+       }
 
-       return autosw & 1;
+       return (res)? 0 : -EIO;
 }
 
-static int video_switch(void)
+static int video_autosw_get(void)
 {
-       int autosw = video_autosw();
-       int ret;
+       int autosw = 0;
 
-       if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
-               return -EIO;
-       ret = video_supported == TPACPI_VIDEO_570 ?
-           acpi_evalf(ec_handle, NULL, "_Q16", "v") :
-           acpi_evalf(vid_handle, NULL, "VSWT", "v");
-       acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
+       switch (video_supported) {
+       case TPACPI_VIDEO_570:
+               if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
+                       return -EIO;
+               break;
+       case TPACPI_VIDEO_770:
+       case TPACPI_VIDEO_NEW:
+               if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
+                       return -EIO;
+               break;
+       default:
+               return -ENOSYS;
+       }
 
-       return ret;
+       return autosw & 1;
 }
 
-static int video_expand(void)
+static int video_autosw_set(int enable)
 {
-       if (video_supported == TPACPI_VIDEO_570)
-               return acpi_evalf(ec_handle, NULL, "_Q17", "v");
-       else if (video_supported == TPACPI_VIDEO_770)
-               return acpi_evalf(vid_handle, NULL, "VEXP", "v");
-       else
-               return acpi_evalf(NULL, NULL, "\\VEXP", "v");
+       if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
+               return -EIO;
+       return 0;
 }
 
-static int video_switch2(int status)
+static int video_outputsw_cycle(void)
 {
-       int ret;
-
-       if (video_supported == TPACPI_VIDEO_570) {
-               ret = acpi_evalf(NULL, NULL,
-                                "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
-       } else if (video_supported == TPACPI_VIDEO_770) {
-               int autosw = video_autosw();
-               if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
-                       return -EIO;
+       int autosw = video_autosw_get();
+       int res;
 
-               ret = acpi_evalf(vid_handle, NULL,
-                                "ASWT", "vdd", status * 0x100, 0);
+       if (autosw < 0)
+               return autosw;
 
-               acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
-       } else {
-               ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
-                   acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
+       switch (video_supported) {
+       case TPACPI_VIDEO_570:
+               res = video_autosw_set(1);
+               if (res)
+                       return res;
+               res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
+               break;
+       case TPACPI_VIDEO_770:
+       case TPACPI_VIDEO_NEW:
+               res = video_autosw_set(1);
+               if (res)
+                       return res;
+               res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
+               break;
+       default:
+               return -ENOSYS;
+       }
+       if (!autosw && video_autosw_set(autosw)) {
+               printk(IBM_ERR "video auto-switch left enabled due to error\n");
+               return -EIO;
        }
 
-       return ret;
+       return (res)? 0 : -EIO;
+}
+
+static int video_expand_toggle(void)
+{
+       switch (video_supported) {
+       case TPACPI_VIDEO_570:
+               return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
+                       0 : -EIO;
+       case TPACPI_VIDEO_770:
+               return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
+                       0 : -EIO;
+       case TPACPI_VIDEO_NEW:
+               return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
+                       0 : -EIO;
+       default:
+               return -ENOSYS;
+       }
+       /* not reached */
 }
 
 static int video_read(char *p)
 {
-       int status = video_status();
-       int autosw = video_autosw();
+       int status, autosw;
        int len = 0;
 
-       if (!video_supported) {
+       if (video_supported == TPACPI_VIDEO_NONE) {
                len += sprintf(p + len, "status:\t\tnot supported\n");
                return len;
        }
 
+       status = video_outputsw_get();
+       if (status < 0)
+               return status;
+
+       autosw = video_autosw_get();
+       if (autosw < 0)
+               return autosw;
+
        len += sprintf(p + len, "status:\t\tsupported\n");
        len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
        len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
@@ -998,47 +1918,56 @@ static int video_write(char *buf)
 {
        char *cmd;
        int enable, disable, status;
+       int res;
 
-       if (!video_supported)
+       if (video_supported == TPACPI_VIDEO_NONE)
                return -ENODEV;
 
-       enable = disable = 0;
+       enable = 0;
+       disable = 0;
 
        while ((cmd = next_cmd(&buf))) {
                if (strlencmp(cmd, "lcd_enable") == 0) {
-                       enable |= 0x01;
+                       enable |= TP_ACPI_VIDEO_S_LCD;
                } else if (strlencmp(cmd, "lcd_disable") == 0) {
-                       disable |= 0x01;
+                       disable |= TP_ACPI_VIDEO_S_LCD;
                } else if (strlencmp(cmd, "crt_enable") == 0) {
-                       enable |= 0x02;
+                       enable |= TP_ACPI_VIDEO_S_CRT;
                } else if (strlencmp(cmd, "crt_disable") == 0) {
-                       disable |= 0x02;
+                       disable |= TP_ACPI_VIDEO_S_CRT;
                } else if (video_supported == TPACPI_VIDEO_NEW &&
                           strlencmp(cmd, "dvi_enable") == 0) {
-                       enable |= 0x08;
+                       enable |= TP_ACPI_VIDEO_S_DVI;
                } else if (video_supported == TPACPI_VIDEO_NEW &&
                           strlencmp(cmd, "dvi_disable") == 0) {
-                       disable |= 0x08;
+                       disable |= TP_ACPI_VIDEO_S_DVI;
                } else if (strlencmp(cmd, "auto_enable") == 0) {
-                       if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
-                               return -EIO;
+                       res = video_autosw_set(1);
+                       if (res)
+                               return res;
                } else if (strlencmp(cmd, "auto_disable") == 0) {
-                       if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
-                               return -EIO;
+                       res = video_autosw_set(0);
+                       if (res)
+                               return res;
                } else if (strlencmp(cmd, "video_switch") == 0) {
-                       if (!video_switch())
-                               return -EIO;
+                       res = video_outputsw_cycle();
+                       if (res)
+                               return res;
                } else if (strlencmp(cmd, "expand_toggle") == 0) {
-                       if (!video_expand())
-                               return -EIO;
+                       res = video_expand_toggle();
+                       if (res)
+                               return res;
                } else
                        return -EINVAL;
        }
 
        if (enable || disable) {
-               status = (video_status() & 0x0f & ~disable) | enable;
-               if (!video_switch2(status))
-                       return -EIO;
+               status = video_outputsw_get();
+               if (status < 0)
+                       return status;
+               res = video_outputsw_set((status & ~disable) | enable);
+               if (res)
+                       return res;
        }
 
        return 0;
@@ -1151,6 +2080,36 @@ 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 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 = PCI_ROOT_HID_STRING,
+        .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)
@@ -1158,7 +2117,6 @@ static int __init dock_init(struct ibm_init_struct *iibm)
        vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
 
        IBM_ACPIHANDLE_INIT(dock);
-       IBM_ACPIHANDLE_INIT(pci);
 
        vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
                str_supported(dock_handle != NULL));
@@ -1166,10 +2124,32 @@ static int __init dock_init(struct ibm_init_struct *iibm)
        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) {
+               IBM_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 && strstr(ibm->acpi->hid, IBM_PCI_HID);
+       int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
 
        if (event == 1 && !pci) /* 570 */
                acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
@@ -1227,33 +2207,6 @@ static int dock_write(char *buf)
        return 0;
 }
 
-static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
-       {
-        .notify = dock_notify,
-        .handle = &dock_handle,
-        .type = ACPI_SYSTEM_NOTIFY,
-       },
-       {
-        .hid = IBM_PCI_HID,
-        .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],
-       },
-};
-
 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
 
 /*************************************************************************
@@ -1384,8 +2337,30 @@ static struct ibm_struct bay_driver_data = {
  * CMOS subdriver
  */
 
+/* sysfs cmos_command -------------------------------------------------- */
+static ssize_t cmos_command_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
+{
+       unsigned long cmos_cmd;
+       int res;
+
+       if (parse_strtoul(buf, 21, &cmos_cmd))
+               return -EINVAL;
+
+       res = issue_thinkpad_cmos_command(cmos_cmd);
+       return (res)? res : count;
+}
+
+static struct device_attribute dev_attr_cmos_command =
+       __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
+
+/* --------------------------------------------------------------------- */
+
 static int __init cmos_init(struct ibm_init_struct *iibm)
 {
+       int res;
+
        vdbg_printk(TPACPI_DBG_INIT,
                "initializing cmos commands subdriver\n");
 
@@ -1393,15 +2368,17 @@ static int __init cmos_init(struct ibm_init_struct *iibm)
 
        vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
                str_supported(cmos_handle != NULL));
+
+       res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
+       if (res)
+               return res;
+
        return (cmos_handle)? 0 : 1;
 }
 
-static int cmos_eval(int cmos_cmd)
+static void cmos_exit(void)
 {
-       if (cmos_handle)
-               return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
-       else
-               return 1;
+       device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
 }
 
 static int cmos_read(char *p)
@@ -1423,10 +2400,7 @@ static int cmos_read(char *p)
 static int cmos_write(char *buf)
 {
        char *cmd;
-       int cmos_cmd;
-
-       if (!cmos_handle)
-               return -EINVAL;
+       int cmos_cmd, res;
 
        while ((cmd = next_cmd(&buf))) {
                if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
@@ -1435,8 +2409,9 @@ static int cmos_write(char *buf)
                } else
                        return -EINVAL;
 
-               if (!cmos_eval(cmos_cmd))
-                       return -EIO;
+               res = issue_thinkpad_cmos_command(cmos_cmd);
+               if (res)
+                       return res;
        }
 
        return 0;
@@ -1446,6 +2421,7 @@ static struct ibm_struct cmos_driver_data = {
        .name = "cmos",
        .read = cmos_read,
        .write = cmos_write,
+       .exit = cmos_exit,
 };
 
 /*************************************************************************
@@ -1643,17 +2619,97 @@ static struct ibm_struct beep_driver_data = {
 
 static enum thermal_access_mode thermal_read_mode;
 
+/* sysfs temp##_input -------------------------------------------------- */
+
+static ssize_t thermal_temp_input_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       struct sensor_device_attribute *sensor_attr =
+                                       to_sensor_dev_attr(attr);
+       int idx = sensor_attr->index;
+       s32 value;
+       int res;
+
+       res = thermal_get_sensor(idx, &value);
+       if (res)
+               return res;
+       if (value == TP_EC_THERMAL_TMP_NA * 1000)
+               return -ENXIO;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", value);
+}
+
+#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
+        SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
+
+static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
+       THERMAL_SENSOR_ATTR_TEMP(1, 0),
+       THERMAL_SENSOR_ATTR_TEMP(2, 1),
+       THERMAL_SENSOR_ATTR_TEMP(3, 2),
+       THERMAL_SENSOR_ATTR_TEMP(4, 3),
+       THERMAL_SENSOR_ATTR_TEMP(5, 4),
+       THERMAL_SENSOR_ATTR_TEMP(6, 5),
+       THERMAL_SENSOR_ATTR_TEMP(7, 6),
+       THERMAL_SENSOR_ATTR_TEMP(8, 7),
+       THERMAL_SENSOR_ATTR_TEMP(9, 8),
+       THERMAL_SENSOR_ATTR_TEMP(10, 9),
+       THERMAL_SENSOR_ATTR_TEMP(11, 10),
+       THERMAL_SENSOR_ATTR_TEMP(12, 11),
+       THERMAL_SENSOR_ATTR_TEMP(13, 12),
+       THERMAL_SENSOR_ATTR_TEMP(14, 13),
+       THERMAL_SENSOR_ATTR_TEMP(15, 14),
+       THERMAL_SENSOR_ATTR_TEMP(16, 15),
+};
+
+#define THERMAL_ATTRS(X) \
+       &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
+
+static struct attribute *thermal_temp_input_attr[] = {
+       THERMAL_ATTRS(8),
+       THERMAL_ATTRS(9),
+       THERMAL_ATTRS(10),
+       THERMAL_ATTRS(11),
+       THERMAL_ATTRS(12),
+       THERMAL_ATTRS(13),
+       THERMAL_ATTRS(14),
+       THERMAL_ATTRS(15),
+       THERMAL_ATTRS(0),
+       THERMAL_ATTRS(1),
+       THERMAL_ATTRS(2),
+       THERMAL_ATTRS(3),
+       THERMAL_ATTRS(4),
+       THERMAL_ATTRS(5),
+       THERMAL_ATTRS(6),
+       THERMAL_ATTRS(7),
+       NULL
+};
+
+static const struct attribute_group thermal_temp_input16_group = {
+       .attrs = thermal_temp_input_attr
+};
+
+static const struct attribute_group thermal_temp_input8_group = {
+       .attrs = &thermal_temp_input_attr[8]
+};
+
+#undef THERMAL_SENSOR_ATTR_TEMP
+#undef THERMAL_ATTRS
+
+/* --------------------------------------------------------------------- */
+
 static int __init thermal_init(struct ibm_init_struct *iibm)
 {
        u8 t, ta1, ta2;
        int i;
        int acpi_tmp7;
+       int res;
 
        vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
 
        acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
 
-       if (ibm_thinkpad_ec_found && experimental) {
+       if (thinkpad_id.ec_model && experimental) {
                /*
                 * Direct EC access mode: sensors at registers
                 * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
@@ -1663,13 +2719,13 @@ static int __init thermal_init(struct ibm_init_struct *iibm)
 
                ta1 = ta2 = 0;
                for (i = 0; i < 8; i++) {
-                       if (likely(acpi_ec_read(0x78 + i, &t))) {
+                       if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
                                ta1 |= t;
                        } else {
                                ta1 = 0;
                                break;
                        }
-                       if (likely(acpi_ec_read(0xC0 + i, &t))) {
+                       if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
                                ta2 |= t;
                        } else {
                                ta1 = 0;
@@ -1711,60 +2767,128 @@ static int __init thermal_init(struct ibm_init_struct *iibm)
                str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
                thermal_read_mode);
 
-       return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
+       switch(thermal_read_mode) {
+       case TPACPI_THERMAL_TPEC_16:
+               res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+                               &thermal_temp_input16_group);
+               if (res)
+                       return res;
+               break;
+       case TPACPI_THERMAL_TPEC_8:
+       case TPACPI_THERMAL_ACPI_TMP07:
+       case TPACPI_THERMAL_ACPI_UPDT:
+               res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+                               &thermal_temp_input8_group);
+               if (res)
+                       return res;
+               break;
+       case TPACPI_THERMAL_NONE:
+       default:
+               return 1;
+       }
+
+       return 0;
 }
 
-static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
+static void thermal_exit(void)
+{
+       switch(thermal_read_mode) {
+       case TPACPI_THERMAL_TPEC_16:
+               sysfs_remove_group(&tpacpi_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,
+                                  &thermal_temp_input16_group);
+               break;
+       case TPACPI_THERMAL_NONE:
+       default:
+               break;
+       }
+}
+
+/* idx is zero-based */
+static int thermal_get_sensor(int idx, s32 *value)
 {
-       int i, t;
+       int t;
        s8 tmp;
-       char tmpi[] = "TMPi";
+       char tmpi[5];
 
-       if (!s)
-               return -EINVAL;
+       t = TP_EC_THERMAL_TMP0;
 
        switch (thermal_read_mode) {
 #if TPACPI_MAX_THERMAL_SENSORS >= 16
        case TPACPI_THERMAL_TPEC_16:
-               for (i = 0; i < 8; i++) {
-                       if (!acpi_ec_read(0xC0 + i, &tmp))
-                               return -EIO;
-                       s->temp[i + 8] = tmp * 1000;
+               if (idx >= 8 && idx <= 15) {
+                       t = TP_EC_THERMAL_TMP8;
+                       idx -= 8;
                }
                /* fallthrough */
 #endif
        case TPACPI_THERMAL_TPEC_8:
-               for (i = 0; i < 8; i++) {
-                       if (!acpi_ec_read(0x78 + i, &tmp))
+               if (idx <= 7) {
+                       if (!acpi_ec_read(t + idx, &tmp))
                                return -EIO;
-                       s->temp[i] = tmp * 1000;
+                       *value = tmp * 1000;
+                       return 0;
                }
-               return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
+               break;
 
        case TPACPI_THERMAL_ACPI_UPDT:
-               if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
-                       return -EIO;
-               for (i = 0; i < 8; i++) {
-                       tmpi[3] = '0' + i;
+               if (idx <= 7) {
+                       snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
+                       if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
+                               return -EIO;
+                       if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
+                               return -EIO;
+                       *value = (t - 2732) * 100;
+                       return 0;
+               }
+               break;
+
+       case TPACPI_THERMAL_ACPI_TMP07:
+               if (idx <= 7) {
+                       snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
                        if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
                                return -EIO;
-                       s->temp[i] = (t - 2732) * 100;
+                       if (t > 127 || t < -127)
+                               t = TP_EC_THERMAL_TMP_NA;
+                       *value = t * 1000;
+                       return 0;
                }
-               return 8;
+               break;
+
+       case TPACPI_THERMAL_NONE:
+       default:
+               return -ENOSYS;
+       }
+
+       return -EINVAL;
+}
+
+static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
+{
+       int res, i;
+       int n;
 
-       case TPACPI_THERMAL_ACPI_TMP07:
-               for (i = 0; i < 8; i++) {
-                       tmpi[3] = '0' + i;
-                       if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
-                               return -EIO;
-                       s->temp[i] = t * 1000;
-               }
-               return 8;
+       n = 8;
+       i = 0;
 
-       case TPACPI_THERMAL_NONE:
-       default:
-               return 0;
+       if (!s)
+               return -EINVAL;
+
+       if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
+               n = 16;
+
+       for(i = 0 ; i < n; i++) {
+               res = thermal_get_sensor(i, &s->temp[i]);
+               if (res)
+                       return res;
        }
+
+       return n;
 }
 
 static int thermal_read(char *p)
@@ -1792,6 +2916,7 @@ static int thermal_read(char *p)
 static struct ibm_struct thermal_driver_data = {
        .name = "thermal",
        .read = thermal_read,
+       .exit = thermal_exit,
 };
 
 /*************************************************************************
@@ -1868,7 +2993,7 @@ static struct ibm_struct ecdump_driver_data = {
  * Backlight/brightness subdriver
  */
 
-static struct backlight_device *ibm_backlight_device = NULL;
+static struct backlight_device *ibm_backlight_device;
 
 static struct backlight_ops ibm_backlight_data = {
         .get_brightness = brightness_get,
@@ -1881,12 +3006,26 @@ static int __init brightness_init(struct ibm_init_struct *iibm)
 
        vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
 
+       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;
+
        b = brightness_get(NULL);
        if (b < 0)
-               return b;
+               return 1;
 
-       ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
-                                                        &ibm_backlight_data);
+       ibm_backlight_device = backlight_device_register(
+                                       TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
+                                       &ibm_backlight_data);
        if (IS_ERR(ibm_backlight_device)) {
                printk(IBM_ERR "Could not register backlight device\n");
                return PTR_ERR(ibm_backlight_device);
@@ -1918,13 +3057,35 @@ static int brightness_update_status(struct backlight_device *bd)
                                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;
+
+       if (brightness_mode & 1) {
+               if (!acpi_ec_read(brightness_offset, &lec))
+                       return -EIO;
+               lec &= 7;
+               level = lec;
+       };
+       if (brightness_mode & 2) {
+               lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
+                        & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
+                       >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
+               level = lcmos;
+       }
 
-       level &= 0x7;
+       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;
 }
@@ -1934,14 +3095,20 @@ static int brightness_set(int value)
        int cmos_cmd, inc, i;
        int current_value = brightness_get(NULL);
 
-       value &= 7;
+       if (value > 7)
+               return -EINVAL;
 
-       cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
+       cmos_cmd = value > current_value ?
+                       TP_CMOS_BRIGHTNESS_UP :
+                       TP_CMOS_BRIGHTNESS_DOWN;
        inc = value > current_value ? 1 : -1;
+
        for (i = current_value; i != value; i += inc) {
-               if (!cmos_eval(cmos_cmd))
+               if ((brightness_mode & 2) &&
+                   issue_thinkpad_cmos_command(cmos_cmd))
                        return -EIO;
-               if (!acpi_ec_write(brightness_offset, i + inc))
+               if ((brightness_mode & 1) &&
+                   !acpi_ec_write(brightness_offset, i + inc))
                        return -EIO;
        }
 
@@ -2056,16 +3223,16 @@ static int volume_write(char *buf)
                        cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
                        inc = new_level > level ? 1 : -1;
 
-                       if (mute && (!cmos_eval(cmos_cmd) ||
+                       if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
                                     !acpi_ec_write(volume_offset, level)))
                                return -EIO;
 
                        for (i = level; i != new_level; i += inc)
-                               if (!cmos_eval(cmos_cmd) ||
+                               if (issue_thinkpad_cmos_command(cmos_cmd) ||
                                    !acpi_ec_write(volume_offset, i + inc))
                                        return -EIO;
 
-                       if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
+                       if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
                                     !acpi_ec_write(volume_offset,
                                                    new_level + mute)))
                                return -EIO;
@@ -2074,7 +3241,7 @@ static int volume_write(char *buf)
                if (new_mute != mute) { /* level doesn't change */
                        cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
 
-                       if (!cmos_eval(cmos_cmd) ||
+                       if (issue_thinkpad_cmos_command(cmos_cmd) ||
                            !acpi_ec_write(volume_offset, level + new_mute))
                                return -EIO;
                }
@@ -2199,6 +3366,7 @@ static enum fan_control_access_mode fan_control_access_mode;
 static enum fan_control_commands fan_control_commands;
 
 static u8 fan_control_initial_status;
+static u8 fan_control_desired_level;
 
 static void fan_watchdog_fire(struct work_struct *ignored);
 static int fan_watchdog_maxinterval;
@@ -2212,15 +3380,239 @@ IBM_HANDLE(sfan, ec, "SFAN",   /* 570 */
           "JFNS",              /* 770x-JL */
           );                   /* all others */
 
+/*
+ * SYSFS fan layout: hwmon compatible (device)
+ *
+ * pwm*_enable:
+ *     0: "disengaged" mode
+ *     1: manual mode
+ *     2: native EC "auto" mode (recommended, hardware default)
+ *
+ * pwm*: set speed in manual mode, ignored otherwise.
+ *     0 is level 0; 255 is level 7. Intermediate points done with linear
+ *     interpolation.
+ *
+ * fan*_input: tachometer reading, RPM
+ *
+ *
+ * SYSFS fan layout: extensions
+ *
+ * fan_watchdog (driver):
+ *     fan watchdog interval in seconds, 0 disables (default), max 120
+ */
+
+/* sysfs fan pwm1_enable ----------------------------------------------- */
+static ssize_t fan_pwm1_enable_show(struct device *dev,
+                                   struct device_attribute *attr,
+                                   char *buf)
+{
+       int res, mode;
+       u8 status;
+
+       res = fan_get_status_safe(&status);
+       if (res)
+               return res;
+
+       if (unlikely(tp_features.fan_ctrl_status_undef)) {
+               if (status != fan_control_initial_status) {
+                       tp_features.fan_ctrl_status_undef = 0;
+               } else {
+                       /* Return most likely status. In fact, it
+                        * might be the only possible status */
+                       status = TP_EC_FAN_AUTO;
+               }
+       }
+
+       if (status & TP_EC_FAN_FULLSPEED) {
+               mode = 0;
+       } else if (status & TP_EC_FAN_AUTO) {
+               mode = 2;
+       } else
+               mode = 1;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", mode);
+}
+
+static ssize_t fan_pwm1_enable_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+       unsigned long t;
+       int res, level;
+
+       if (parse_strtoul(buf, 2, &t))
+               return -EINVAL;
+
+       switch (t) {
+       case 0:
+               level = TP_EC_FAN_FULLSPEED;
+               break;
+       case 1:
+               level = TPACPI_FAN_LAST_LEVEL;
+               break;
+       case 2:
+               level = TP_EC_FAN_AUTO;
+               break;
+       case 3:
+               /* reserved for software-controlled auto mode */
+               return -ENOSYS;
+       default:
+               return -EINVAL;
+       }
+
+       res = fan_set_level_safe(level);
+       if (res == -ENXIO)
+               return -EINVAL;
+       else if (res < 0)
+               return res;
+
+       fan_watchdog_reset();
+
+       return count;
+}
+
+static struct device_attribute dev_attr_fan_pwm1_enable =
+       __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
+               fan_pwm1_enable_show, fan_pwm1_enable_store);
+
+/* sysfs fan pwm1 ------------------------------------------------------ */
+static ssize_t fan_pwm1_show(struct device *dev,
+                            struct device_attribute *attr,
+                            char *buf)
+{
+       int res;
+       u8 status;
+
+       res = fan_get_status_safe(&status);
+       if (res)
+               return res;
+
+       if (unlikely(tp_features.fan_ctrl_status_undef)) {
+               if (status != fan_control_initial_status) {
+                       tp_features.fan_ctrl_status_undef = 0;
+               } else {
+                       status = TP_EC_FAN_AUTO;
+               }
+       }
+
+       if ((status &
+            (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
+               status = fan_control_desired_level;
+
+       if (status > 7)
+               status = 7;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
+}
+
+static ssize_t fan_pwm1_store(struct device *dev,
+                             struct device_attribute *attr,
+                             const char *buf, size_t count)
+{
+       unsigned long s;
+       int rc;
+       u8 status, newlevel;
+
+       if (parse_strtoul(buf, 255, &s))
+               return -EINVAL;
+
+       /* scale down from 0-255 to 0-7 */
+       newlevel = (s >> 5) & 0x07;
+
+       rc = mutex_lock_interruptible(&fan_mutex);
+       if (rc < 0)
+               return rc;
+
+       rc = fan_get_status(&status);
+       if (!rc && (status &
+                   (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
+               rc = fan_set_level(newlevel);
+               if (rc == -ENXIO)
+                       rc = -EINVAL;
+               else if (!rc) {
+                       fan_update_desired_level(newlevel);
+                       fan_watchdog_reset();
+               }
+       }
+
+       mutex_unlock(&fan_mutex);
+       return (rc)? rc : count;
+}
+
+static struct device_attribute dev_attr_fan_pwm1 =
+       __ATTR(pwm1, S_IWUSR | S_IRUGO,
+               fan_pwm1_show, fan_pwm1_store);
+
+/* sysfs fan fan1_input ------------------------------------------------ */
+static ssize_t fan_fan1_input_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int res;
+       unsigned int speed;
+
+       res = fan_get_speed(&speed);
+       if (res < 0)
+               return res;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", speed);
+}
+
+static struct device_attribute dev_attr_fan_fan1_input =
+       __ATTR(fan1_input, S_IRUGO,
+               fan_fan1_input_show, NULL);
+
+/* sysfs fan fan_watchdog (driver) ------------------------------------- */
+static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
+                                    char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
+}
+
+static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
+                                     const char *buf, size_t count)
+{
+       unsigned long t;
+
+       if (parse_strtoul(buf, 120, &t))
+               return -EINVAL;
+
+       if (!fan_control_allowed)
+               return -EPERM;
+
+       fan_watchdog_maxinterval = t;
+       fan_watchdog_reset();
+
+       return count;
+}
+
+static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
+               fan_fan_watchdog_show, fan_fan_watchdog_store);
+
+/* --------------------------------------------------------------------- */
+static struct attribute *fan_attributes[] = {
+       &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
+       &dev_attr_fan_fan1_input.attr,
+       NULL
+};
+
+static const struct attribute_group fan_attr_group = {
+       .attrs = fan_attributes,
+};
+
 static int __init fan_init(struct ibm_init_struct *iibm)
 {
+       int rc;
+
        vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
 
+       mutex_init(&fan_mutex);
        fan_status_access_mode = TPACPI_FAN_NONE;
        fan_control_access_mode = TPACPI_FAN_WR_NONE;
        fan_control_commands = 0;
        fan_watchdog_maxinterval = 0;
        tp_features.fan_ctrl_status_undef = 0;
+       fan_control_desired_level = 7;
 
        IBM_ACPIHANDLE_INIT(fans);
        IBM_ACPIHANDLE_INIT(gfan);
@@ -2244,20 +3636,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
@@ -2299,9 +3690,44 @@ static int __init fan_init(struct ibm_init_struct *iibm)
                  fan_control_access_mode != TPACPI_FAN_WR_NONE),
                fan_status_access_mode, fan_control_access_mode);
 
-       return (fan_status_access_mode != TPACPI_FAN_NONE ||
-               fan_control_access_mode != TPACPI_FAN_WR_NONE)?
-                       0 : 1;
+       /* fan control master switch */
+       if (!fan_control_allowed) {
+               fan_control_access_mode = TPACPI_FAN_WR_NONE;
+               fan_control_commands = 0;
+               dbg_printk(TPACPI_DBG_INIT,
+                          "fan control features disabled by parameter\n");
+       }
+
+       /* update fan_control_desired_level */
+       if (fan_status_access_mode != TPACPI_FAN_NONE)
+               fan_get_status_safe(NULL);
+
+       if (fan_status_access_mode != TPACPI_FAN_NONE ||
+           fan_control_access_mode != TPACPI_FAN_WR_NONE) {
+               rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
+                                        &fan_attr_group);
+               if (!(rc < 0))
+                       rc = driver_create_file(&tpacpi_pdriver.driver,
+                                       &driver_attr_fan_watchdog);
+               if (rc < 0)
+                       return rc;
+               return 0;
+       } else
+               return 1;
+}
+
+/*
+ * Call with fan_mutex held
+ */
+static void fan_update_desired_level(u8 status)
+{
+       if ((status &
+            (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
+               if (status > 7)
+                       fan_control_desired_level = 7;
+               else
+                       fan_control_desired_level = status;
+       }
 }
 
 static int fan_get_status(u8 *status)
@@ -2340,9 +3766,33 @@ static int fan_get_status(u8 *status)
        return 0;
 }
 
+static int fan_get_status_safe(u8 *status)
+{
+       int rc;
+       u8 s;
+
+       rc = mutex_lock_interruptible(&fan_mutex);
+       if (rc < 0)
+               return rc;
+       rc = fan_get_status(&s);
+       if (!rc)
+               fan_update_desired_level(s);
+       mutex_unlock(&fan_mutex);
+
+       if (status)
+               *status = s;
+
+       return rc;
+}
+
 static void fan_exit(void)
 {
-       vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending watchdogs\n");
+       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);
+
        cancel_delayed_work(&fan_watchdog_task);
        flush_scheduled_work();
 }
@@ -2372,9 +3822,13 @@ static int fan_get_speed(unsigned int *speed)
 
 static void fan_watchdog_fire(struct work_struct *ignored)
 {
+       int rc;
+
        printk(IBM_NOTICE "fan watchdog: enabling fan\n");
-       if (fan_set_enable()) {
-               printk(IBM_ERR "fan watchdog: error while enabling fan\n");
+       rc = fan_set_enable();
+       if (rc < 0) {
+               printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
+                       "will try again later...\n", -rc);
                /* reschedule for later */
                fan_watchdog_reset();
        }
@@ -2382,7 +3836,10 @@ static void fan_watchdog_fire(struct work_struct *ignored)
 
 static void fan_watchdog_reset(void)
 {
-       static int fan_watchdog_active = 0;
+       static int fan_watchdog_active;
+
+       if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
+               return;
 
        if (fan_watchdog_active)
                cancel_delayed_work(&fan_watchdog_task);
@@ -2401,6 +3858,9 @@ static void fan_watchdog_reset(void)
 
 static int fan_set_level(int level)
 {
+       if (!fan_control_allowed)
+               return -EPERM;
+
        switch (fan_control_access_mode) {
        case TPACPI_FAN_WR_ACPI_SFAN:
                if (level >= 0 && level <= 7) {
@@ -2417,6 +3877,13 @@ static int fan_set_level(int level)
                    ((level < 0) || (level > 7)))
                        return -EINVAL;
 
+               /* safety net should the EC not support AUTO
+                * or FULLSPEED mode bits and just ignore them */
+               if (level & TP_EC_FAN_FULLSPEED)
+                       level |= 7;     /* safety min speed 7 */
+               else if (level & TP_EC_FAN_FULLSPEED)
+                       level |= 4;     /* safety min speed 4 */
+
                if (!acpi_ec_write(fan_status_offset, level))
                        return -EIO;
                else
@@ -2429,85 +3896,152 @@ static int fan_set_level(int level)
        return 0;
 }
 
+static int fan_set_level_safe(int level)
+{
+       int rc;
+
+       if (!fan_control_allowed)
+               return -EPERM;
+
+       rc = mutex_lock_interruptible(&fan_mutex);
+       if (rc < 0)
+               return rc;
+
+       if (level == TPACPI_FAN_LAST_LEVEL)
+               level = fan_control_desired_level;
+
+       rc = fan_set_level(level);
+       if (!rc)
+               fan_update_desired_level(level);
+
+       mutex_unlock(&fan_mutex);
+       return rc;
+}
+
 static int fan_set_enable(void)
 {
        u8 s;
        int rc;
 
+       if (!fan_control_allowed)
+               return -EPERM;
+
+       rc = mutex_lock_interruptible(&fan_mutex);
+       if (rc < 0)
+               return rc;
+
        switch (fan_control_access_mode) {
        case TPACPI_FAN_WR_ACPI_FANS:
        case TPACPI_FAN_WR_TPEC:
-               if ((rc = fan_get_status(&s)) < 0)
-                       return rc;
+               rc = fan_get_status(&s);
+               if (rc < 0)
+                       break;
 
                /* Don't go out of emergency fan mode */
-               if (s != 7)
-                       s = TP_EC_FAN_AUTO;
+               if (s != 7) {
+                       s &= 0x07;
+                       s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
+               }
 
                if (!acpi_ec_write(fan_status_offset, s))
-                       return -EIO;
-               else
+                       rc = -EIO;
+               else {
                        tp_features.fan_ctrl_status_undef = 0;
+                       rc = 0;
+               }
                break;
 
        case TPACPI_FAN_WR_ACPI_SFAN:
-               if ((rc = fan_get_status(&s)) < 0)
-                       return rc;
+               rc = fan_get_status(&s);
+               if (rc < 0)
+                       break;
 
                s &= 0x07;
 
                /* Set fan to at least level 4 */
-               if (s < 4)
-                       s = 4;
+               s |= 4;
 
                if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
-                       return -EIO;
+                       rc= -EIO;
+               else
+                       rc = 0;
                break;
 
        default:
-               return -ENXIO;
+               rc = -ENXIO;
        }
-       return 0;
+
+       mutex_unlock(&fan_mutex);
+       return rc;
 }
 
 static int fan_set_disable(void)
 {
+       int rc;
+
+       if (!fan_control_allowed)
+               return -EPERM;
+
+       rc = mutex_lock_interruptible(&fan_mutex);
+       if (rc < 0)
+               return rc;
+
+       rc = 0;
        switch (fan_control_access_mode) {
        case TPACPI_FAN_WR_ACPI_FANS:
        case TPACPI_FAN_WR_TPEC:
                if (!acpi_ec_write(fan_status_offset, 0x00))
-                       return -EIO;
-               else
+                       rc = -EIO;
+               else {
+                       fan_control_desired_level = 0;
                        tp_features.fan_ctrl_status_undef = 0;
+               }
                break;
 
        case TPACPI_FAN_WR_ACPI_SFAN:
                if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
-                       return -EIO;
+                       rc = -EIO;
+               else
+                       fan_control_desired_level = 0;
                break;
 
        default:
-               return -ENXIO;
+               rc = -ENXIO;
        }
-       return 0;
+
+
+       mutex_unlock(&fan_mutex);
+       return rc;
 }
 
 static int fan_set_speed(int speed)
 {
+       int rc;
+
+       if (!fan_control_allowed)
+               return -EPERM;
+
+       rc = mutex_lock_interruptible(&fan_mutex);
+       if (rc < 0)
+               return rc;
+
+       rc = 0;
        switch (fan_control_access_mode) {
        case TPACPI_FAN_WR_ACPI_FANS:
                if (speed >= 0 && speed <= 65535) {
                        if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
                                        speed, speed, speed))
-                               return -EIO;
+                               rc = -EIO;
                } else
-                       return -EINVAL;
+                       rc = -EINVAL;
                break;
 
        default:
-               return -ENXIO;
+               rc = -ENXIO;
        }
-       return 0;
+
+       mutex_unlock(&fan_mutex);
+       return rc;
 }
 
 static int fan_read(char *p)
@@ -2520,7 +4054,7 @@ static int fan_read(char *p)
        switch (fan_status_access_mode) {
        case TPACPI_FAN_RD_ACPI_GFAN:
                /* 570, 600e/x, 770e, 770x */
-               if ((rc = fan_get_status(&status)) < 0)
+               if ((rc = fan_get_status_safe(&status)) < 0)
                        return rc;
 
                len += sprintf(p + len, "status:\t\t%s\n"
@@ -2530,7 +4064,7 @@ static int fan_read(char *p)
 
        case TPACPI_FAN_RD_TPEC:
                /* all except 570, 600e/x, 770e, 770x */
-               if ((rc = fan_get_status(&status)) < 0)
+               if ((rc = fan_get_status_safe(&status)) < 0)
                        return rc;
 
                if (unlikely(tp_features.fan_ctrl_status_undef)) {
@@ -2574,7 +4108,7 @@ static int fan_read(char *p)
 
                default:
                        len += sprintf(p + len, " (<level> is 0-7, "
-                                      "auto, disengaged)\n");
+                                      "auto, disengaged, full-speed)\n");
                        break;
                }
        }
@@ -2597,12 +4131,13 @@ static int fan_write_cmd_level(const char *cmd, int *rc)
 
        if (strlencmp(cmd, "level auto") == 0)
                level = TP_EC_FAN_AUTO;
-       else if (strlencmp(cmd, "level disengaged") == 0)
+       else if ((strlencmp(cmd, "level disengaged") == 0) |
+                (strlencmp(cmd, "level full-speed") == 0))
                level = TP_EC_FAN_FULLSPEED;
        else if (sscanf(cmd, "level %d", &level) != 1)
                return 0;
 
-       if ((*rc = fan_set_level(level)) == -ENXIO)
+       if ((*rc = fan_set_level_safe(level)) == -ENXIO)
                printk(IBM_ERR "level command accepted for unsupported "
                       "access mode %d", fan_control_access_mode);
 
@@ -2693,7 +4228,6 @@ static struct ibm_struct fan_driver_data = {
        .read = fan_read,
        .write = fan_write,
        .exit = fan_exit,
-       .flags.experimental = 1,
 };
 
 /****************************************************************************
@@ -2705,7 +4239,7 @@ static struct ibm_struct fan_driver_data = {
  ****************************************************************************/
 
 /* /proc support */
-static struct proc_dir_entry *proc_dir = NULL;
+static struct proc_dir_entry *proc_dir;
 
 /* Subdriver registry */
 static LIST_HEAD(tpacpi_all_drivers);
@@ -2848,13 +4382,30 @@ static void ibm_exit(struct ibm_struct *ibm)
 
 /* Probing */
 
-static char *ibm_thinkpad_ec_found = NULL;
-
-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;
        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
@@ -2868,10 +4419,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)
@@ -2885,7 +4446,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);
@@ -2901,7 +4462,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;
@@ -2943,6 +4504,7 @@ static struct ibm_init_struct ibms_init[] __initdata = {
                .data = &dock_driver_data[0],
        },
        {
+               .init = dock_init2,
                .data = &dock_driver_data[1],
        },
 #endif
@@ -3012,7 +4574,13 @@ 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, bool, 0);
+
+static int brightness_mode;
+module_param_named(brightness_mode, brightness_mode, int, 0);
 
 #define IBM_PARAM(feature) \
        module_param_call(feature, set_ibm_param, NULL, NULL, 0)
@@ -3039,11 +4607,17 @@ static int __init thinkpad_acpi_module_init(void)
 {
        int ret, i;
 
+       /* 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);
 
@@ -3055,6 +4629,54 @@ static int __init thinkpad_acpi_module_init(void)
        }
        proc_dir->owner = THIS_MODULE;
 
+       ret = platform_driver_register(&tpacpi_pdriver);
+       if (ret) {
+               printk(IBM_ERR "unable to register platform driver\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+       ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
+       if (ret) {
+               printk(IBM_ERR "unable to create sysfs driver attributes\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+
+
+       /* Device initialization */
+       tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
+                                                       NULL, 0);
+       if (IS_ERR(tpacpi_pdev)) {
+               ret = PTR_ERR(tpacpi_pdev);
+               tpacpi_pdev = NULL;
+               printk(IBM_ERR "unable to register platform device\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+       tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
+       if (IS_ERR(tpacpi_hwmon)) {
+               ret = PTR_ERR(tpacpi_hwmon);
+               tpacpi_hwmon = NULL;
+               printk(IBM_ERR "unable to register hwmon device\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       }
+       tpacpi_inputdev = input_allocate_device();
+       if (!tpacpi_inputdev) {
+               printk(IBM_ERR "unable to allocate input device\n");
+               thinkpad_acpi_module_exit();
+               return -ENOMEM;
+       } else {
+               /* Prepare input device, but don't register */
+               tpacpi_inputdev->name = "ThinkPad Extra Buttons";
+               tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
+               tpacpi_inputdev->id.bustype = BUS_HOST;
+               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;
+       }
        for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
                ret = ibm_init(&ibms_init[i]);
                if (ret >= 0 && *ibms_init[i].param)
@@ -3064,6 +4686,14 @@ static int __init thinkpad_acpi_module_init(void)
                        return ret;
                }
        }
+       ret = input_register_device(tpacpi_inputdev);
+       if (ret < 0) {
+               printk(IBM_ERR "unable to register input device\n");
+               thinkpad_acpi_module_exit();
+               return ret;
+       } else {
+               tp_features.input_device_registered = 1;
+       }
 
        return 0;
 }
@@ -3080,10 +4710,28 @@ static void thinkpad_acpi_module_exit(void)
 
        dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
 
+       if (tpacpi_inputdev) {
+               if (tp_features.input_device_registered)
+                       input_unregister_device(tpacpi_inputdev);
+               else
+                       input_free_device(tpacpi_inputdev);
+       }
+
+       if (tpacpi_hwmon)
+               hwmon_device_unregister(tpacpi_hwmon);
+
+       if (tpacpi_pdev)
+               platform_device_unregister(tpacpi_pdev);
+
+       tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
+       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);