[Bluetooth] Use in-kernel sockets API
[pandora-kernel.git] / drivers / misc / thinkpad_acpi.c
index cac73e5..6c36a55 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #define IBM_VERSION "0.14"
+#define TPACPI_SYSFS_VERSION 0x000100
 
 /*
  *  Changelog:
@@ -273,59 +274,84 @@ 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
  */
 
-static void __init ibm_handle_init(char *name,
-                                  acpi_handle * handle, acpi_handle parent,
-                                  char **paths, int num_paths, char **path)
+static void drv_acpi_handle_init(char *name,
+                          acpi_handle *handle, acpi_handle parent,
+                          char **paths, int num_paths, char **path)
 {
        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;
 }
 
-static void dispatch_notify(acpi_handle handle, u32 event, void *data)
+static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
 {
        struct ibm_struct *ibm = data;
 
-       if (!ibm || !ibm->notify)
+       if (!ibm || !ibm->acpi || !ibm->acpi->notify)
                return;
 
-       ibm->notify(ibm, event);
+       ibm->acpi->notify(ibm, event);
 }
 
-static int __init setup_notify(struct ibm_struct *ibm)
+static int __init setup_acpi_notify(struct ibm_struct *ibm)
 {
        acpi_status status;
-       int ret;
+       int rc;
 
-       if (!*ibm->handle)
+       BUG_ON(!ibm->acpi);
+
+       if (!*ibm->acpi->handle)
                return 0;
 
-       ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
-       if (ret < 0) {
-               printk(IBM_ERR "%s device not present\n", ibm->name);
+       vdbg_printk(TPACPI_DBG_INIT,
+               "setting up ACPI notify for %s\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;
        }
 
-       acpi_driver_data(ibm->device) = ibm;
-       sprintf(acpi_device_class(ibm->device), "%s/%s",
+       acpi_driver_data(ibm->acpi->device) = ibm;
+       sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
                IBM_ACPI_EVENT_PREFIX,
                ibm->name);
 
-       status = acpi_install_notify_handler(*ibm->handle, ibm->type,
-                                            dispatch_notify, ibm);
+       status = acpi_install_notify_handler(*ibm->acpi->handle,
+                       ibm->acpi->type, dispatch_acpi_notify, ibm);
        if (ACPI_FAILURE(status)) {
                if (status == AE_ALREADY_EXISTS) {
                        printk(IBM_NOTICE "another device driver is already handling %s events\n",
@@ -336,37 +362,44 @@ static int __init setup_notify(struct ibm_struct *ibm)
                }
                return -ENODEV;
        }
-       ibm->notify_installed = 1;
+       ibm->flags.acpi_notify_installed = 1;
        return 0;
 }
 
-static int __init ibm_device_add(struct acpi_device *device)
+static int __init tpacpi_device_add(struct acpi_device *device)
 {
        return 0;
 }
 
 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);
+
+       BUG_ON(!ibm->acpi);
 
-       ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
-       if (!ibm->driver) {
-               printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
-               return -1;
+       ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
+       if (!ibm->acpi->driver) {
+               printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
+               return -ENOMEM;
        }
 
-       sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
-       ibm->driver->ids = ibm->hid;
-       ibm->driver->ops.add = &ibm_device_add;
+       sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
+       ibm->acpi->driver->ids = ibm->acpi->hid;
+       ibm->acpi->driver->ops.add = &tpacpi_device_add;
 
-       ret = acpi_bus_register_driver(ibm->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->hid, ret);
-               kfree(ibm->driver);
-       }
+                      ibm->acpi->hid, rc);
+               kfree(ibm->acpi->driver);
+               ibm->acpi->driver = NULL;
+       } else if (!rc)
+               ibm->flags.acpi_driver_registered = 1;
 
-       return ret;
+       return rc;
 }
 
 
@@ -378,8 +411,8 @@ static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  ****************************************************************************
  ****************************************************************************/
 
-static int dispatch_read(char *page, char **start, off_t off, int count,
-                        int *eof, void *data)
+static int dispatch_procfs_read(char *page, char **start, off_t off,
+                       int count, int *eof, void *data)
 {
        struct ibm_struct *ibm = data;
        int len;
@@ -403,8 +436,9 @@ static int dispatch_read(char *page, char **start, off_t off, int count,
        return len;
 }
 
-static int dispatch_write(struct file *file, const char __user * userbuf,
-                         unsigned long count, void *data)
+static int dispatch_procfs_write(struct file *file,
+                       const char __user * userbuf,
+                       unsigned long count, void *data)
 {
        struct ibm_struct *ibm = data;
        char *kernbuf;
@@ -450,6 +484,182 @@ static char *next_cmd(char **cmds)
 }
 
 
+/****************************************************************************
+ ****************************************************************************
+ *
+ * Device model: hwmon and platform
+ *
+ ****************************************************************************
+ ****************************************************************************/
+
+static struct platform_device *tpacpi_pdev = NULL;
+static struct class_device *tpacpi_hwmon = NULL;
+
+static struct platform_driver tpacpi_pdriver = {
+       .driver = {
+               .name = IBM_DRVR_NAME,
+               .owner = THIS_MODULE,
+       },
+};
+
+
+/*************************************************************************
+ * 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;
+}
+
 /****************************************************************************
  ****************************************************************************
  *
@@ -459,10 +669,10 @@ static char *next_cmd(char **cmds)
  ****************************************************************************/
 
 /*************************************************************************
- * ibm-acpi init subdriver
+ * thinkpad-acpi init subdriver
  */
 
-static int thinkpad_acpi_driver_init(void)
+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);
@@ -484,37 +694,186 @@ static int thinkpad_acpi_driver_read(char *p)
        return len;
 }
 
+static struct ibm_struct thinkpad_acpi_driver_data = {
+       .name = "driver",
+       .read = thinkpad_acpi_driver_read,
+};
+
 /*************************************************************************
  * Hotkey subdriver
  */
 
-static int hotkey_supported;
-static int hotkey_mask_supported;
 static int hotkey_orig_status;
 static int hotkey_orig_mask;
 
-static int hotkey_init(void)
+static struct attribute_set *hotkey_dev_attributes = NULL;
+
+/* sysfs hotkey enable ------------------------------------------------- */
+static ssize_t hotkey_enable_show(struct device *dev,
+                          struct device_attribute *attr,
+                          char *buf)
+{
+       int res, status, 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, 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(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, mask;
+
+       res = hotkey_get(&status, &mask);
+       if (res)
+               return res;
+
+       return snprintf(buf, PAGE_SIZE, "0x%04x\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, mask;
+
+       if (parse_strtoul(buf, 0xffff, &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(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(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%04x\n", hotkey_orig_mask);
+}
+
+static struct device_attribute dev_attr_hotkey_bios_mask =
+       __ATTR(bios_mask, S_IRUGO, hotkey_bios_mask_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,
+};
+
+static int __init hotkey_init(struct ibm_init_struct *iibm)
 {
+       int res;
+
+       vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
+
+       IBM_ACPIHANDLE_INIT(hkey);
+       mutex_init(&hotkey_mutex);
+
        /* hotkey not supported on 570 */
-       hotkey_supported = hkey_handle != NULL;
+       tp_features.hotkey = hkey_handle != NULL;
+
+       vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
+               str_supported(tp_features.hotkey));
+
+       if (tp_features.hotkey) {
+               hotkey_dev_attributes = create_attr_set(4,
+                                               TPACPI_HOTKEY_SYSFS_GROUP);
+               if (!hotkey_dev_attributes)
+                       return -ENOMEM;
+               res = add_to_attr_set(hotkey_dev_attributes,
+                               &dev_attr_hotkey_enable.attr);
+               if (res)
+                       return res;
 
-       if (hotkey_supported) {
                /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
                   A30, R30, R31, T20-22, X20-21, X22-24 */
-               hotkey_mask_supported =
-                   acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
+               tp_features.hotkey_mask =
+                       acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
+
+               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;
+               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));
+               }
+               if (!res)
+                       res = register_attr_set_with_sysfs(
+                                       hotkey_dev_attributes,
+                                       &tpacpi_pdev->dev.kobj);
+
+               if (res)
+                       return res;
        }
 
-       return 0;
+       return (tp_features.hotkey)? 0 : 1;
 }
 
 static void hotkey_exit(void)
 {
-       if (hotkey_supported)
-               hotkey_set(hotkey_orig_status, hotkey_orig_mask);
+       int res;
+
+       if (tp_features.hotkey) {
+               dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
+               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 hotkey_notify(struct ibm_struct *ibm, u32 event)
@@ -522,58 +881,70 @@ static void hotkey_notify(struct ibm_struct *ibm, u32 event)
        int hkey;
 
        if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
-               acpi_bus_generate_event(ibm->device, event, hkey);
+               acpi_bus_generate_event(ibm->acpi->device, event, hkey);
        else {
                printk(IBM_ERR "unknown hotkey event %d\n", event);
-               acpi_bus_generate_event(ibm->device, event, 0);
+               acpi_bus_generate_event(ibm->acpi->device, event, 0);
        }
 }
 
+/*
+ * Call with hotkey_mutex held
+ */
 static int hotkey_get(int *status, int *mask)
 {
        if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
-               return 0;
+               return -EIO;
 
-       if (hotkey_mask_supported)
+       if (tp_features.hotkey_mask)
                if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
-                       return 0;
+                       return -EIO;
 
-       return 1;
+       return 0;
 }
 
+/*
+ * Call with hotkey_mutex held
+ */
 static int hotkey_set(int status, int mask)
 {
        int i;
 
        if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
-               return 0;
+               return -EIO;
 
-       if (hotkey_mask_supported)
+       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, mask;
        int len = 0;
 
-       if (!hotkey_supported) {
+       if (!tp_features.hotkey) {
                len += sprintf(p + len, "status:\t\tnot supported\n");
                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 (hotkey_mask_supported) {
+       if (tp_features.hotkey_mask) {
                len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
                len += sprintf(p + len,
                               "commands:\tenable, disable, reset, <mask>\n");
@@ -587,16 +958,22 @@ static int hotkey_read(char *p)
 
 static int hotkey_write(char *buf)
 {
-       int status, mask;
+       int res, status, mask;
        char *cmd;
        int do_cmd = 0;
 
-       if (!hotkey_supported)
+       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;
@@ -609,55 +986,169 @@ 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 = {
+       .hid = IBM_HKEY_HID,
+       .notify = hotkey_notify,
+       .handle = &hkey_handle,
+       .type = ACPI_DEVICE_NOTIFY,
+};
+
+static struct ibm_struct hotkey_driver_data = {
+       .name = "hotkey",
+       .read = hotkey_read,
+       .write = hotkey_write,
+       .exit = hotkey_exit,
+       .acpi = &ibm_hotkey_acpidriver,
+};
+
 /*************************************************************************
  * Bluetooth subdriver
  */
 
-static int bluetooth_supported;
+/* 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(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 = {
+       .name = TPACPI_BLUETH_SYSFS_GROUP,
+       .attrs = bluetooth_attributes,
+};
 
-static int bluetooth_init(void)
+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);
+
        /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
           G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
-       bluetooth_supported = hkey_handle &&
-           acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
+       tp_features.bluetooth = hkey_handle &&
+           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 0;
+       return (tp_features.bluetooth)? 0 : 1;
+}
+
+static void bluetooth_exit(void)
+{
+       sysfs_remove_group(&tpacpi_pdev->dev.kobj,
+                       &bluetooth_attr_group);
 }
 
-static int bluetooth_status(void)
+static int bluetooth_get_radiosw(void)
 {
        int status;
 
-       if (!bluetooth_supported ||
-           !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 (!bluetooth_supported)
+       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");
        }
 
@@ -666,64 +1157,161 @@ static int bluetooth_read(char *p)
 
 static int bluetooth_write(char *buf)
 {
-       int status = bluetooth_status();
        char *cmd;
-       int do_cmd = 0;
 
-       if (!bluetooth_supported)
+       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
  */
 
-static int wan_supported;
+/* 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 int wan_init(void)
+static ssize_t wan_enable_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
 {
-       wan_supported = hkey_handle &&
-           acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
+       unsigned long t;
+       int res;
 
-       return 0;
+       if (parse_strtoul(buf, 1, &t))
+               return -EINVAL;
+
+       res = wan_set_radiosw(t);
+
+       return (res) ? res : count;
+}
+
+static struct device_attribute dev_attr_wan_enable =
+       __ATTR(enable, S_IWUSR | S_IRUGO,
+               wan_enable_show, wan_enable_store);
+
+/* --------------------------------------------------------------------- */
+
+static struct attribute *wan_attributes[] = {
+       &dev_attr_wan_enable.attr,
+       NULL
+};
+
+static const struct attribute_group wan_attr_group = {
+       .name = TPACPI_WAN_SYSFS_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, &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 (!wan_supported || !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 (!wan_supported)
+       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");
        }
 
@@ -732,29 +1320,31 @@ static int wan_read(char *p)
 
 static int wan_write(char *buf)
 {
-       int status = wan_status();
        char *cmd;
-       int do_cmd = 0;
 
-       if (!wan_supported)
+       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;
 }
 
+static struct ibm_struct wan_driver_data = {
+       .name = "wan",
+       .read = wan_read,
+       .write = wan_write,
+       .exit = wan_exit,
+       .flags.experimental = 1,
+};
+
 /*************************************************************************
  * Video subdriver
  */
@@ -771,145 +1361,238 @@ IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",     /* 570 */
 
 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
 
-static int video_init(void)
+static int __init video_init(struct ibm_init_struct *iibm)
 {
        int ivga;
 
+       vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
+
+       IBM_ACPIHANDLE_INIT(vid);
+       IBM_ACPIHANDLE_INIT(vid2);
+
        if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
                /* G41, assume IVGA doesn't change */
                vid_handle = vid2_handle;
 
        if (!vid_handle)
                /* video switching not supported on R30, R31 */
-               video_supported = IBMACPI_VIDEO_NONE;
+               video_supported = TPACPI_VIDEO_NONE;
        else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
                /* 570 */
-               video_supported = IBMACPI_VIDEO_570;
+               video_supported = TPACPI_VIDEO_570;
        else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
                /* 600e/x, 770e, 770x */
-               video_supported = IBMACPI_VIDEO_770;
+               video_supported = TPACPI_VIDEO_770;
        else
                /* all others */
-               video_supported = IBMACPI_VIDEO_NEW;
+               video_supported = TPACPI_VIDEO_NEW;
 
-       return 0;
+       vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
+               str_supported(video_supported != TPACPI_VIDEO_NONE),
+               video_supported);
+
+       return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
 }
 
 static void video_exit(void)
 {
-       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 == IBMACPI_VIDEO_570) {
-               if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
-                       status = i & 3;
-       } else if (video_supported == IBMACPI_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 == IBMACPI_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 == IBMACPI_VIDEO_570)
-               acpi_evalf(vid_handle, &autosw, "SWIT", "d");
-       else if (video_supported == IBMACPI_VIDEO_770 ||
-                video_supported == IBMACPI_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 == IBMACPI_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 == IBMACPI_VIDEO_570)
-               return acpi_evalf(ec_handle, NULL, "_Q17", "v");
-       else if (video_supported == IBMACPI_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;
+       int autosw = video_autosw_get();
+       int res;
 
-       if (video_supported == IBMACPI_VIDEO_570) {
-               ret = acpi_evalf(NULL, NULL,
-                                "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
-       } else if (video_supported == IBMACPI_VIDEO_770) {
-               int autosw = video_autosw();
-               if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
-                       return -EIO;
+       if (autosw < 0)
+               return autosw;
 
-               ret = acpi_evalf(vid_handle, NULL,
-                                "ASWT", "vdd", status * 0x100, 0);
-
-               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));
-       if (video_supported == IBMACPI_VIDEO_NEW)
+       if (video_supported == TPACPI_VIDEO_NEW)
                len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
        len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
        len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
        len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
-       if (video_supported == IBMACPI_VIDEO_NEW)
+       if (video_supported == TPACPI_VIDEO_NEW)
                len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
        len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
        len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
@@ -921,74 +1604,96 @@ 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;
-               } else if (video_supported == IBMACPI_VIDEO_NEW &&
+                       disable |= TP_ACPI_VIDEO_S_CRT;
+               } else if (video_supported == TPACPI_VIDEO_NEW &&
                           strlencmp(cmd, "dvi_enable") == 0) {
-                       enable |= 0x08;
-               } else if (video_supported == IBMACPI_VIDEO_NEW &&
+                       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;
 }
 
+static struct ibm_struct video_driver_data = {
+       .name = "video",
+       .read = video_read,
+       .write = video_write,
+       .exit = video_exit,
+};
+
 /*************************************************************************
  * Light (thinklight) subdriver
  */
 
-static int light_supported;
-static int light_status_supported;
-
 IBM_HANDLE(lght, root, "\\LGHT");      /* A21e, A2xm/p, T20-22, X20-21 */
 IBM_HANDLE(ledb, ec, "LEDB");          /* G4x */
 
-static int light_init(void)
+static int __init light_init(struct ibm_init_struct *iibm)
 {
+       vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
+
+       IBM_ACPIHANDLE_INIT(ledb);
+       IBM_ACPIHANDLE_INIT(lght);
+       IBM_ACPIHANDLE_INIT(cmos);
+
        /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
-       light_supported = (cmos_handle || lght_handle) && !ledb_handle;
+       tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
 
-       if (light_supported)
+       if (tp_features.light)
                /* light status not supported on
                   570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
-               light_status_supported = acpi_evalf(ec_handle, NULL,
-                                                   "KBLT", "qv");
+               tp_features.light_status =
+                       acpi_evalf(ec_handle, NULL, "KBLT", "qv");
 
-       return 0;
+       vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
+               str_supported(tp_features.light));
+
+       return (tp_features.light)? 0 : 1;
 }
 
 static int light_read(char *p)
@@ -996,9 +1701,9 @@ static int light_read(char *p)
        int len = 0;
        int status = 0;
 
-       if (!light_supported) {
+       if (!tp_features.light) {
                len += sprintf(p + len, "status:\t\tnot supported\n");
-       } else if (!light_status_supported) {
+       } else if (!tp_features.light_status) {
                len += sprintf(p + len, "status:\t\tunknown\n");
                len += sprintf(p + len, "commands:\ton, off\n");
        } else {
@@ -1017,7 +1722,7 @@ static int light_write(char *buf)
        char *cmd;
        int success;
 
-       if (!light_supported)
+       if (!tp_features.light)
                return -ENODEV;
 
        while ((cmd = next_cmd(&buf))) {
@@ -1040,13 +1745,16 @@ static int light_write(char *buf)
        return 0;
 }
 
+static struct ibm_struct light_driver_data = {
+       .name = "light",
+       .read = light_read,
+       .write = light_write,
+};
+
 /*************************************************************************
  * Dock subdriver
  */
 
-/* don't list other alternatives as we install a notify handler on the 570 */
-IBM_HANDLE(pci, root, "\\_SB.PCI");    /* 570 */
-
 #ifdef CONFIG_THINKPAD_ACPI_DOCK
 
 IBM_HANDLE(dock, root, "\\_SB.GDCK",   /* X30, X31, X40 */
@@ -1055,27 +1763,91 @@ IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
           "\\_SB.PCI.ISA.SLCE",        /* 570 */
     );                         /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
 
+/* don't list other alternatives as we install a notify handler on the 570 */
+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,
+       },
+       {
+        .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],
+       },
+};
+
 #define dock_docked() (_sta(dock_handle) & 1)
 
+static int __init dock_init(struct ibm_init_struct *iibm)
+{
+       vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
+
+       IBM_ACPIHANDLE_INIT(dock);
+
+       vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
+               str_supported(dock_handle != NULL));
+
+       return (dock_handle)? 0 : 1;
+}
+
+static int __init dock_init2(struct ibm_init_struct *iibm)
+{
+       int dock2_needed;
+
+       vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
+
+       if (dock_driver_data[0].flags.acpi_driver_registered &&
+           dock_driver_data[0].flags.acpi_notify_installed) {
+               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->hid && strstr(ibm->hid, IBM_PCI_HID);
+       int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
 
        if (event == 1 && !pci) /* 570 */
-               acpi_bus_generate_event(ibm->device, event, 1); /* button */
+               acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
        else if (event == 1 && pci)     /* 570 */
-               acpi_bus_generate_event(ibm->device, event, 3); /* dock */
+               acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
        else if (event == 3 && docked)
-               acpi_bus_generate_event(ibm->device, event, 1); /* button */
+               acpi_bus_generate_event(ibm->acpi->device, event, 1);   /* button */
        else if (event == 3 && !docked)
-               acpi_bus_generate_event(ibm->device, event, 2); /* undock */
+               acpi_bus_generate_event(ibm->acpi->device, event, 2);   /* undock */
        else if (event == 0 && docked)
-               acpi_bus_generate_event(ibm->device, event, 3); /* dock */
+               acpi_bus_generate_event(ibm->acpi->device, event, 3);   /* dock */
        else {
                printk(IBM_ERR "unknown dock event %d, status %d\n",
                       event, _sta(dock_handle));
-               acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
+               acpi_bus_generate_event(ibm->acpi->device, event, 0);   /* unknown */
        }
 }
 
@@ -1125,11 +1897,6 @@ static int dock_write(char *buf)
  */
 
 #ifdef CONFIG_THINKPAD_ACPI_BAY
-static int bay_status_supported;
-static int bay_status2_supported;
-static int bay_eject_supported;
-static int bay_eject2_supported;
-
 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",       /* 570 */
           "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
           "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
@@ -1145,24 +1912,41 @@ IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
           "_EJ0",                      /* 770x */
           );                           /* all others */
 
-static int bay_init(void)
+static int __init bay_init(struct ibm_init_struct *iibm)
 {
-       bay_status_supported = bay_handle &&
-           acpi_evalf(bay_handle, NULL, "_STA", "qv");
-       bay_status2_supported = bay2_handle &&
-           acpi_evalf(bay2_handle, NULL, "_STA", "qv");
+       vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
 
-       bay_eject_supported = bay_handle && bay_ej_handle &&
-           (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
-       bay_eject2_supported = bay2_handle && bay2_ej_handle &&
-           (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
-
-       return 0;
+       IBM_ACPIHANDLE_INIT(bay);
+       if (bay_handle)
+               IBM_ACPIHANDLE_INIT(bay_ej);
+       IBM_ACPIHANDLE_INIT(bay2);
+       if (bay2_handle)
+               IBM_ACPIHANDLE_INIT(bay2_ej);
+
+       tp_features.bay_status = bay_handle &&
+               acpi_evalf(bay_handle, NULL, "_STA", "qv");
+       tp_features.bay_status2 = bay2_handle &&
+               acpi_evalf(bay2_handle, NULL, "_STA", "qv");
+
+       tp_features.bay_eject = bay_handle && bay_ej_handle &&
+               (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
+       tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
+               (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
+
+       vdbg_printk(TPACPI_DBG_INIT,
+               "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
+               str_supported(tp_features.bay_status),
+               str_supported(tp_features.bay_eject),
+               str_supported(tp_features.bay_status2),
+               str_supported(tp_features.bay_eject2));
+
+       return (tp_features.bay_status || tp_features.bay_eject ||
+               tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
 }
 
 static void bay_notify(struct ibm_struct *ibm, u32 event)
 {
-       acpi_bus_generate_event(ibm->device, event, 0);
+       acpi_bus_generate_event(ibm->acpi->device, event, 0);
 }
 
 #define bay_occupied(b) (_sta(b##_handle) & 1)
@@ -1174,15 +1958,16 @@ static int bay_read(char *p)
        int occupied2 = bay_occupied(bay2);
        int eject, eject2;
 
-       len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
-                      (occupied ? "occupied" : "unoccupied") :
-                      "not supported");
-       if (bay_status2_supported)
+       len += sprintf(p + len, "status:\t\t%s\n",
+               tp_features.bay_status ?
+                       (occupied ? "occupied" : "unoccupied") :
+                               "not supported");
+       if (tp_features.bay_status2)
                len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
                               "occupied" : "unoccupied");
 
-       eject = bay_eject_supported && occupied;
-       eject2 = bay_eject2_supported && occupied2;
+       eject = tp_features.bay_eject && occupied;
+       eject2 = tp_features.bay_eject2 && occupied2;
 
        if (eject && eject2)
                len += sprintf(p + len, "commands:\teject, eject2\n");
@@ -1198,14 +1983,14 @@ static int bay_write(char *buf)
 {
        char *cmd;
 
-       if (!bay_eject_supported && !bay_eject2_supported)
+       if (!tp_features.bay_eject && !tp_features.bay_eject2)
                return -ENODEV;
 
        while ((cmd = next_cmd(&buf))) {
-               if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
+               if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
                        if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
                                return -EIO;
-               } else if (bay_eject2_supported &&
+               } else if (tp_features.bay_eject2 &&
                           strlencmp(cmd, "eject2") == 0) {
                        if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
                                return -EIO;
@@ -1215,18 +2000,68 @@ static int bay_write(char *buf)
 
        return 0;
 }
+
+static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
+       .notify = bay_notify,
+       .handle = &bay_handle,
+       .type = ACPI_SYSTEM_NOTIFY,
+};
+
+static struct ibm_struct bay_driver_data = {
+       .name = "bay",
+       .read = bay_read,
+       .write = bay_write,
+       .acpi = &ibm_bay_acpidriver,
+};
+
 #endif /* CONFIG_THINKPAD_ACPI_BAY */
 
 /*************************************************************************
  * CMOS subdriver
  */
 
-static int cmos_eval(int cmos_cmd)
+/* sysfs cmos_command -------------------------------------------------- */
+static ssize_t cmos_command_store(struct device *dev,
+                           struct device_attribute *attr,
+                           const char *buf, size_t count)
 {
-       if (cmos_handle)
-               return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
-       else
-               return 1;
+       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");
+
+       IBM_ACPIHANDLE_INIT(cmos);
+
+       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 void cmos_exit(void)
+{
+       device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
 }
 
 static int cmos_read(char *p)
@@ -1248,10 +2083,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 &&
@@ -1260,13 +2092,20 @@ 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;
 }
 
+static struct ibm_struct cmos_driver_data = {
+       .name = "cmos",
+       .read = cmos_read,
+       .write = cmos_write,
+       .exit = cmos_exit,
+};
 
 /*************************************************************************
  * LED subdriver
@@ -1279,22 +2118,29 @@ IBM_HANDLE(led, ec, "SLED",     /* 570 */
           "LED",               /* all others */
           );                   /* R30, R31 */
 
-static int led_init(void)
+static int __init led_init(struct ibm_init_struct *iibm)
 {
+       vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
+
+       IBM_ACPIHANDLE_INIT(led);
+
        if (!led_handle)
                /* led not supported on R30, R31 */
-               led_supported = IBMACPI_LED_NONE;
+               led_supported = TPACPI_LED_NONE;
        else if (strlencmp(led_path, "SLED") == 0)
                /* 570 */
-               led_supported = IBMACPI_LED_570;
+               led_supported = TPACPI_LED_570;
        else if (strlencmp(led_path, "SYSL") == 0)
                /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
-               led_supported = IBMACPI_LED_OLD;
+               led_supported = TPACPI_LED_OLD;
        else
                /* all others */
-               led_supported = IBMACPI_LED_NEW;
+               led_supported = TPACPI_LED_NEW;
 
-       return 0;
+       vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
+               str_supported(led_supported), led_supported);
+
+       return (led_supported != TPACPI_LED_NONE)? 0 : 1;
 }
 
 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
@@ -1309,7 +2155,7 @@ static int led_read(char *p)
        }
        len += sprintf(p + len, "status:\t\tsupported\n");
 
-       if (led_supported == IBMACPI_LED_570) {
+       if (led_supported == TPACPI_LED_570) {
                /* 570 */
                int i, status;
                for (i = 0; i < 8; i++) {
@@ -1354,23 +2200,23 @@ static int led_write(char *buf)
                } else
                        return -EINVAL;
 
-               if (led_supported == IBMACPI_LED_570) {
+               if (led_supported == TPACPI_LED_570) {
                        /* 570 */
                        led = 1 << led;
                        if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
                                        led, led_sled_arg1[ind]))
                                return -EIO;
-               } else if (led_supported == IBMACPI_LED_OLD) {
+               } else if (led_supported == TPACPI_LED_OLD) {
                        /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
                        led = 1 << led;
-                       ret = ec_write(IBMACPI_LED_EC_HLMS, led);
+                       ret = ec_write(TPACPI_LED_EC_HLMS, led);
                        if (ret >= 0)
                                ret =
-                                   ec_write(IBMACPI_LED_EC_HLBL,
+                                   ec_write(TPACPI_LED_EC_HLBL,
                                             led * led_exp_hlbl[ind]);
                        if (ret >= 0)
                                ret =
-                                   ec_write(IBMACPI_LED_EC_HLCL,
+                                   ec_write(TPACPI_LED_EC_HLCL,
                                             led * led_exp_hlcl[ind]);
                        if (ret < 0)
                                return ret;
@@ -1385,12 +2231,30 @@ static int led_write(char *buf)
        return 0;
 }
 
+static struct ibm_struct led_driver_data = {
+       .name = "led",
+       .read = led_read,
+       .write = led_write,
+};
+
 /*************************************************************************
  * Beep subdriver
  */
 
 IBM_HANDLE(beep, ec, "BEEP");  /* all except R30, R31 */
 
+static int __init beep_init(struct ibm_init_struct *iibm)
+{
+       vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
+
+       IBM_ACPIHANDLE_INIT(beep);
+
+       vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
+               str_supported(beep_handle != NULL));
+
+       return (beep_handle)? 0 : 1;
+}
+
 static int beep_read(char *p)
 {
        int len = 0;
@@ -1426,17 +2290,107 @@ static int beep_write(char *buf)
        return 0;
 }
 
+static struct ibm_struct beep_driver_data = {
+       .name = "beep",
+       .read = beep_read,
+       .write = beep_write,
+};
+
 /*************************************************************************
  * Thermal subdriver
  */
 
 static enum thermal_access_mode thermal_read_mode;
 
-static int thermal_init(void)
+/* 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 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
+       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) {
                /*
@@ -1448,13 +2402,13 @@ static int thermal_init(void)
 
                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;
@@ -1467,85 +2421,155 @@ static int thermal_init(void)
                                printk(IBM_ERR
                                       "ThinkPad ACPI EC access misbehaving, "
                                       "falling back to ACPI TMPx access mode\n");
-                               thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
+                               thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
                        } else {
                                printk(IBM_ERR
                                       "ThinkPad ACPI EC access misbehaving, "
                                       "disabling thermal sensors access\n");
-                               thermal_read_mode = IBMACPI_THERMAL_NONE;
+                               thermal_read_mode = TPACPI_THERMAL_NONE;
                        }
                } else {
                        thermal_read_mode =
                            (ta2 != 0) ?
-                           IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
+                           TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
                }
        } else if (acpi_tmp7) {
                if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
                        /* 600e/x, 770e, 770x */
-                       thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
+                       thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
                } else {
                        /* Standard ACPI TMPx access, max 8 sensors */
-                       thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
+                       thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
                }
        } else {
                /* temperatures not supported on 570, G4x, R30, R31, R32 */
-               thermal_read_mode = IBMACPI_THERMAL_NONE;
+               thermal_read_mode = TPACPI_THERMAL_NONE;
+       }
+
+       vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
+               str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
+               thermal_read_mode);
+
+       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 IBMACPI_MAX_THERMAL_SENSORS >= 16
-       case IBMACPI_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 TPACPI_MAX_THERMAL_SENSORS >= 16
+       case TPACPI_THERMAL_TPEC_16:
+               if (idx >= 8 && idx <= 15) {
+                       t = TP_EC_THERMAL_TMP8;
+                       idx -= 8;
                }
                /* fallthrough */
 #endif
-       case IBMACPI_THERMAL_TPEC_8:
-               for (i = 0; i < 8; i++) {
-                       if (!acpi_ec_read(0x78 + i, &tmp))
+       case TPACPI_THERMAL_TPEC_8:
+               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 == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
+               break;
 
-       case IBMACPI_THERMAL_ACPI_UPDT:
-               if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
-                       return -EIO;
-               for (i = 0; i < 8; i++) {
-                       tmpi[3] = '0' + i;
+       case TPACPI_THERMAL_ACPI_UPDT:
+               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;
-                       s->temp[i] = (t - 2732) * 100;
+                       *value = (t - 2732) * 100;
+                       return 0;
                }
-               return 8;
+               break;
 
-       case IBMACPI_THERMAL_ACPI_TMP07:
-               for (i = 0; i < 8; i++) {
-                       tmpi[3] = '0' + i;
+       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 * 1000;
+                       *value = t * 1000;
+                       return 0;
                }
-               return 8;
+               break;
 
-       case IBMACPI_THERMAL_NONE:
+       case TPACPI_THERMAL_NONE:
        default:
-               return 0;
+               return -ENOSYS;
+       }
+
+       return -EINVAL;
+}
+
+static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
+{
+       int res, i;
+       int n;
+
+       n = 8;
+       i = 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)
@@ -1570,6 +2594,12 @@ static int thermal_read(char *p)
        return len;
 }
 
+static struct ibm_struct thermal_driver_data = {
+       .name = "thermal",
+       .read = thermal_read,
+       .exit = thermal_exit,
+};
+
 /*************************************************************************
  * EC Dump subdriver
  */
@@ -1633,6 +2663,13 @@ static int ecdump_write(char *buf)
        return 0;
 }
 
+static struct ibm_struct ecdump_driver_data = {
+       .name = "ecdump",
+       .read = ecdump_read,
+       .write = ecdump_write,
+       .flags.experimental = 1,
+};
+
 /*************************************************************************
  * Backlight/brightness subdriver
  */
@@ -1644,20 +2681,24 @@ static struct backlight_ops ibm_backlight_data = {
         .update_status  = brightness_update_status,
 };
 
-static int brightness_init(void)
+static int __init brightness_init(struct ibm_init_struct *iibm)
 {
        int b;
 
+       vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
+
        b = brightness_get(NULL);
        if (b < 0)
                return b;
 
-       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);
        }
+       vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
 
        ibm_backlight_device->props.max_brightness = 7;
        ibm_backlight_device->props.brightness = b;
@@ -1669,6 +2710,8 @@ static int brightness_init(void)
 static void brightness_exit(void)
 {
        if (ibm_backlight_device) {
+               vdbg_printk(TPACPI_DBG_EXIT,
+                           "calling backlight_device_unregister()\n");
                backlight_device_unregister(ibm_backlight_device);
                ibm_backlight_device = NULL;
        }
@@ -1703,7 +2746,7 @@ static int brightness_set(int value)
        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 (issue_thinkpad_cmos_command(cmos_cmd))
                        return -EIO;
                if (!acpi_ec_write(brightness_offset, i + inc))
                        return -EIO;
@@ -1756,6 +2799,13 @@ static int brightness_write(char *buf)
        return 0;
 }
 
+static struct ibm_struct brightness_driver_data = {
+       .name = "brightness",
+       .read = brightness_read,
+       .write = brightness_write,
+       .exit = brightness_exit,
+};
+
 /*************************************************************************
  * Volume subdriver
  */
@@ -1813,16 +2863,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;
@@ -1831,7 +2881,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;
                }
@@ -1840,6 +2890,11 @@ static int volume_write(char *buf)
        return 0;
 }
 
+static struct ibm_struct volume_driver_data = {
+       .name = "volume",
+       .read = volume_read,
+       .write = volume_write,
+};
 
 /*************************************************************************
  * Fan subdriver
@@ -1848,20 +2903,21 @@ static int volume_write(char *buf)
 /*
  * FAN ACCESS MODES
  *
- * IBMACPI_FAN_RD_ACPI_GFAN:
+ * TPACPI_FAN_RD_ACPI_GFAN:
  *     ACPI GFAN method: returns fan level
  *
- *     see IBMACPI_FAN_WR_ACPI_SFAN
- *     EC 0x2f not available if GFAN exists
+ *     see TPACPI_FAN_WR_ACPI_SFAN
+ *     EC 0x2f (HFSP) not available if GFAN exists
  *
- * IBMACPI_FAN_WR_ACPI_SFAN:
+ * TPACPI_FAN_WR_ACPI_SFAN:
  *     ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  *
- *     EC 0x2f might be available *for reading*, but never for writing.
+ *     EC 0x2f (HFSP) might be available *for reading*, but do not use
+ *     it for writing.
  *
- * IBMACPI_FAN_WR_TPEC:
- *     ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
- *     on almost all ThinkPads
+ * TPACPI_FAN_WR_TPEC:
+ *     ThinkPad EC register 0x2f (HFSP): fan control loop mode
+ *     Supported on almost all ThinkPads
  *
  *     Fan speed changes of any sort (including those caused by the
  *     disengaged mode) are usually done slowly by the firmware as the
@@ -1875,12 +2931,13 @@ static int volume_write(char *buf)
  *      7      automatic mode engaged;
  *             (default operation mode of the ThinkPad)
  *             fan level is ignored in this mode.
- *      6      disengage mode (takes precedence over bit 7);
+ *      6      full speed mode (takes precedence over bit 7);
  *             not available on all thinkpads.  May disable
- *             the tachometer, and speeds up fan to 100% duty-cycle,
- *             which speeds it up far above the standard RPM
- *             levels.  It is not impossible that it could cause
- *             hardware damage.
+ *             the tachometer while the fan controller ramps up
+ *             the speed (which can take up to a few *minutes*).
+ *             Speeds up fan to 100% duty-cycle, which is far above
+ *             the standard RPM levels.  It is not impossible that
+ *             it could cause hardware damage.
  *     5-3     unused in some models.  Extra bits for fan level
  *             in others, but still useless as all values above
  *             7 map to the same speed as level 7 in these models.
@@ -1888,7 +2945,7 @@ static int volume_write(char *buf)
  *                     0x00 = stop
  *                     0x07 = max (set when temperatures critical)
  *             Some ThinkPads may have other levels, see
- *             IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
+ *             TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  *
  *     FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  *     boot. Apparently the EC does not intialize it, so unless ACPI DSDT
@@ -1916,14 +2973,13 @@ static int volume_write(char *buf)
  *     FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  *     might result.
  *
- *     FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
- *     register is not invalidated in ThinkPads that disable tachometer
- *     readings.  Thus, the tachometer readings go stale.
+ *     FIRMWARE BUG: may go stale while the EC is switching to full speed
+ *     mode.
  *
  *     For firmware bugs, refer to:
  *     http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  *
- * IBMACPI_FAN_WR_ACPI_FANS:
+ * TPACPI_FAN_WR_ACPI_FANS:
  *     ThinkPad X31, X40, X41.  Not available in the X60.
  *
  *     FANS ACPI handle: takes three arguments: low speed, medium speed,
@@ -1940,47 +2996,277 @@ static int volume_write(char *buf)
  *     ACPI DSDT switches which set is in use depending on various
  *     factors.
  *
- *     IBMACPI_FAN_WR_TPEC is also available and should be used to
+ *     TPACPI_FAN_WR_TPEC is also available and should be used to
  *     command the fan.  The X31/X40/X41 seems to have 8 fan levels,
  *     but the ACPI tables just mention level 7.
  */
 
-static enum fan_status_access_mode fan_status_access_mode;
-static enum fan_control_access_mode fan_control_access_mode;
-static enum fan_control_commands fan_control_commands;
+static enum fan_status_access_mode fan_status_access_mode;
+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;
+static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
+
+IBM_HANDLE(fans, ec, "FANS");  /* X31, X40, X41 */
+IBM_HANDLE(gfan, ec, "GFAN",   /* 570 */
+          "\\FSPD",            /* 600e/x, 770e, 770x */
+          );                   /* all others */
+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);
 
-static int fan_control_status_known;
-static u8 fan_control_initial_status;
+/* 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 void fan_watchdog_fire(struct work_struct *ignored);
-static int fan_watchdog_maxinterval;
-static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
+static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
+                                     const char *buf, size_t count)
+{
+       unsigned long t;
 
-IBM_HANDLE(fans, ec, "FANS");  /* X31, X40, X41 */
-IBM_HANDLE(gfan, ec, "GFAN",   /* 570 */
-          "\\FSPD",            /* 600e/x, 770e, 770x */
-          );                   /* all others */
-IBM_HANDLE(sfan, ec, "SFAN",   /* 570 */
-          "JFNS",              /* 770x-JL */
-          );                   /* all others */
+       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 fan_init(void)
+static int __init fan_init(struct ibm_init_struct *iibm)
 {
-       fan_status_access_mode = IBMACPI_FAN_NONE;
-       fan_control_access_mode = IBMACPI_FAN_WR_NONE;
+       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_control_status_known = 1;
        fan_watchdog_maxinterval = 0;
+       tp_features.fan_ctrl_status_undef = 0;
+       fan_control_desired_level = 7;
+
+       IBM_ACPIHANDLE_INIT(fans);
+       IBM_ACPIHANDLE_INIT(gfan);
+       IBM_ACPIHANDLE_INIT(sfan);
 
        if (gfan_handle) {
                /* 570, 600e/x, 770e, 770x */
-               fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
+               fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
        } else {
                /* all other ThinkPads: note that even old-style
                 * ThinkPad ECs supports the fan control register */
                if (likely(acpi_ec_read(fan_status_offset,
                                        &fan_control_initial_status))) {
-                       fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
+                       fan_status_access_mode = TPACPI_FAN_RD_TPEC;
 
                        /* In some ThinkPads, neither the EC nor the ACPI
                         * DSDT initialize the fan status, and it ends up
@@ -2003,21 +3289,21 @@ static int fan_init(void)
                                       "fan_init: initial fan status is "
                                       "unknown, assuming it is in auto "
                                       "mode\n");
-                               fan_control_status_known = 0;
+                               tp_features.fan_ctrl_status_undef = 1;
                        }
                } else {
                        printk(IBM_ERR
                               "ThinkPad ACPI EC access misbehaving, "
                               "fan status and control unavailable\n");
-                       return 0;
+                       return 1;
                }
        }
 
        if (sfan_handle) {
                /* 570, 770x-JL */
-               fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
+               fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
                fan_control_commands |=
-                   IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
+                   TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
        } else {
                if (!gfan_handle) {
                        /* gfan without sfan means no fan control */
@@ -2026,21 +3312,63 @@ static int fan_init(void)
                        if (fans_handle) {
                                /* X31, X40, X41 */
                                fan_control_access_mode =
-                                   IBMACPI_FAN_WR_ACPI_FANS;
+                                   TPACPI_FAN_WR_ACPI_FANS;
                                fan_control_commands |=
-                                   IBMACPI_FAN_CMD_SPEED |
-                                   IBMACPI_FAN_CMD_LEVEL |
-                                   IBMACPI_FAN_CMD_ENABLE;
+                                   TPACPI_FAN_CMD_SPEED |
+                                   TPACPI_FAN_CMD_LEVEL |
+                                   TPACPI_FAN_CMD_ENABLE;
                        } else {
-                               fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
+                               fan_control_access_mode = TPACPI_FAN_WR_TPEC;
                                fan_control_commands |=
-                                   IBMACPI_FAN_CMD_LEVEL |
-                                   IBMACPI_FAN_CMD_ENABLE;
+                                   TPACPI_FAN_CMD_LEVEL |
+                                   TPACPI_FAN_CMD_ENABLE;
                        }
                }
        }
 
-       return 0;
+       vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
+               str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
+                 fan_control_access_mode != TPACPI_FAN_WR_NONE),
+               fan_status_access_mode, fan_control_access_mode);
+
+       /* 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)
@@ -2048,10 +3376,10 @@ static int fan_get_status(u8 *status)
        u8 s;
 
        /* TODO:
-        * Add IBMACPI_FAN_RD_ACPI_FANS ? */
+        * Add TPACPI_FAN_RD_ACPI_FANS ? */
 
        switch (fan_status_access_mode) {
-       case IBMACPI_FAN_RD_ACPI_GFAN:
+       case TPACPI_FAN_RD_ACPI_GFAN:
                /* 570, 600e/x, 770e, 770x */
 
                if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
@@ -2062,7 +3390,7 @@ static int fan_get_status(u8 *status)
 
                break;
 
-       case IBMACPI_FAN_RD_TPEC:
+       case TPACPI_FAN_RD_TPEC:
                /* all except 570, 600e/x, 770e, 770x */
                if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
                        return -EIO;
@@ -2079,8 +3407,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 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();
 }
@@ -2090,7 +3443,7 @@ static int fan_get_speed(unsigned int *speed)
        u8 hi, lo;
 
        switch (fan_status_access_mode) {
-       case IBMACPI_FAN_RD_TPEC:
+       case TPACPI_FAN_RD_TPEC:
                /* all except 570, 600e/x, 770e, 770x */
                if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
                             !acpi_ec_read(fan_rpm_offset + 1, &hi)))
@@ -2110,9 +3463,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();
        }
@@ -2122,6 +3479,9 @@ static void fan_watchdog_reset(void)
 {
        static int fan_watchdog_active = 0;
 
+       if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
+               return;
+
        if (fan_watchdog_active)
                cancel_delayed_work(&fan_watchdog_task);
 
@@ -2139,8 +3499,11 @@ 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 IBMACPI_FAN_WR_ACPI_SFAN:
+       case TPACPI_FAN_WR_ACPI_SFAN:
                if (level >= 0 && level <= 7) {
                        if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
                                return -EIO;
@@ -2148,17 +3511,24 @@ static int fan_set_level(int level)
                        return -EINVAL;
                break;
 
-       case IBMACPI_FAN_WR_ACPI_FANS:
-       case IBMACPI_FAN_WR_TPEC:
-               if ((level != IBMACPI_FAN_EC_AUTO) &&
-                   (level != IBMACPI_FAN_EC_DISENGAGED) &&
+       case TPACPI_FAN_WR_ACPI_FANS:
+       case TPACPI_FAN_WR_TPEC:
+               if ((level != TP_EC_FAN_AUTO) &&
+                   (level != TP_EC_FAN_FULLSPEED) &&
                    ((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
-                       fan_control_status_known = 1;
+                       tp_features.fan_ctrl_status_undef = 0;
                break;
 
        default:
@@ -2167,85 +3537,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 IBMACPI_FAN_WR_ACPI_FANS:
-       case IBMACPI_FAN_WR_TPEC:
-               if ((rc = fan_get_status(&s)) < 0)
-                       return rc;
+       case TPACPI_FAN_WR_ACPI_FANS:
+       case TPACPI_FAN_WR_TPEC:
+               rc = fan_get_status(&s);
+               if (rc < 0)
+                       break;
 
                /* Don't go out of emergency fan mode */
-               if (s != 7)
-                       s = IBMACPI_FAN_EC_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
-                       fan_control_status_known = 1;
+                       rc = -EIO;
+               else {
+                       tp_features.fan_ctrl_status_undef = 0;
+                       rc = 0;
+               }
                break;
 
-       case IBMACPI_FAN_WR_ACPI_SFAN:
-               if ((rc = fan_get_status(&s)) < 0)
-                       return rc;
+       case TPACPI_FAN_WR_ACPI_SFAN:
+               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 IBMACPI_FAN_WR_ACPI_FANS:
-       case IBMACPI_FAN_WR_TPEC:
+       case TPACPI_FAN_WR_ACPI_FANS:
+       case TPACPI_FAN_WR_TPEC:
                if (!acpi_ec_write(fan_status_offset, 0x00))
-                       return -EIO;
-               else
-                       fan_control_status_known = 1;
+                       rc = -EIO;
+               else {
+                       fan_control_desired_level = 0;
+                       tp_features.fan_ctrl_status_undef = 0;
+               }
                break;
 
-       case IBMACPI_FAN_WR_ACPI_SFAN:
+       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 IBMACPI_FAN_WR_ACPI_FANS:
+       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)
@@ -2256,9 +3693,9 @@ static int fan_read(char *p)
        unsigned int speed = 0;
 
        switch (fan_status_access_mode) {
-       case IBMACPI_FAN_RD_ACPI_GFAN:
+       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"
@@ -2266,66 +3703,63 @@ static int fan_read(char *p)
                               (status != 0) ? "enabled" : "disabled", status);
                break;
 
-       case IBMACPI_FAN_RD_TPEC:
+       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(!fan_control_status_known)) {
+               if (unlikely(tp_features.fan_ctrl_status_undef)) {
                        if (status != fan_control_initial_status)
-                               fan_control_status_known = 1;
+                               tp_features.fan_ctrl_status_undef = 0;
                        else
                                /* Return most likely status. In fact, it
                                 * might be the only possible status */
-                               status = IBMACPI_FAN_EC_AUTO;
+                               status = TP_EC_FAN_AUTO;
                }
 
                len += sprintf(p + len, "status:\t\t%s\n",
                               (status != 0) ? "enabled" : "disabled");
 
-               /* No ThinkPad boots on disengaged mode, we can safely
-                * assume the tachometer is online if fan control status
-                * was unknown */
                if ((rc = fan_get_speed(&speed)) < 0)
                        return rc;
 
                len += sprintf(p + len, "speed:\t\t%d\n", speed);
 
-               if (status & IBMACPI_FAN_EC_DISENGAGED)
+               if (status & TP_EC_FAN_FULLSPEED)
                        /* Disengaged mode takes precedence */
                        len += sprintf(p + len, "level:\t\tdisengaged\n");
-               else if (status & IBMACPI_FAN_EC_AUTO)
+               else if (status & TP_EC_FAN_AUTO)
                        len += sprintf(p + len, "level:\t\tauto\n");
                else
                        len += sprintf(p + len, "level:\t\t%d\n", status);
                break;
 
-       case IBMACPI_FAN_NONE:
+       case TPACPI_FAN_NONE:
        default:
                len += sprintf(p + len, "status:\t\tnot supported\n");
        }
 
-       if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
+       if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
                len += sprintf(p + len, "commands:\tlevel <level>");
 
                switch (fan_control_access_mode) {
-               case IBMACPI_FAN_WR_ACPI_SFAN:
+               case TPACPI_FAN_WR_ACPI_SFAN:
                        len += sprintf(p + len, " (<level> is 0-7)\n");
                        break;
 
                default:
                        len += sprintf(p + len, " (<level> is 0-7, "
-                                      "auto, disengaged)\n");
+                                      "auto, disengaged, full-speed)\n");
                        break;
                }
        }
 
-       if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
+       if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
                len += sprintf(p + len, "commands:\tenable, disable\n"
                               "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
                               "1-120 (seconds))\n");
 
-       if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
+       if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
                len += sprintf(p + len, "commands:\tspeed <speed>"
                               " (<speed> is 0-65535)\n");
 
@@ -2337,13 +3771,14 @@ static int fan_write_cmd_level(const char *cmd, int *rc)
        int level;
 
        if (strlencmp(cmd, "level auto") == 0)
-               level = IBMACPI_FAN_EC_AUTO;
-       else if (strlencmp(cmd, "level disengaged") == 0)
-               level = IBMACPI_FAN_EC_DISENGAGED;
+               level = TP_EC_FAN_AUTO;
+       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);
 
@@ -2412,13 +3847,13 @@ static int fan_write(char *buf)
        int rc = 0;
 
        while (!rc && (cmd = next_cmd(&buf))) {
-               if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
+               if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
                      fan_write_cmd_level(cmd, &rc)) &&
-                   !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
+                   !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
                      (fan_write_cmd_enable(cmd, &rc) ||
                       fan_write_cmd_disable(cmd, &rc) ||
                       fan_write_cmd_watchdog(cmd, &rc))) &&
-                   !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
+                   !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
                      fan_write_cmd_speed(cmd, &rc))
                    )
                        rc = -EINVAL;
@@ -2429,6 +3864,13 @@ static int fan_write(char *buf)
        return rc;
 }
 
+static struct ibm_struct fan_driver_data = {
+       .name = "fan",
+       .read = fan_read,
+       .write = fan_write,
+       .exit = fan_exit,
+};
+
 /****************************************************************************
  ****************************************************************************
  *
@@ -2441,152 +3883,71 @@ static int fan_write(char *buf)
 static struct proc_dir_entry *proc_dir = NULL;
 
 /* Subdriver registry */
-static struct ibm_struct ibms[] = {
-       {
-        .name = "driver",
-        .init = thinkpad_acpi_driver_init,
-        .read = thinkpad_acpi_driver_read,
-        },
-       {
-        .name = "hotkey",
-        .hid = IBM_HKEY_HID,
-        .init = hotkey_init,
-        .read = hotkey_read,
-        .write = hotkey_write,
-        .exit = hotkey_exit,
-        .notify = hotkey_notify,
-        .handle = &hkey_handle,
-        .type = ACPI_DEVICE_NOTIFY,
-        },
-       {
-        .name = "bluetooth",
-        .init = bluetooth_init,
-        .read = bluetooth_read,
-        .write = bluetooth_write,
-        },
-       {
-        .name = "wan",
-        .init = wan_init,
-        .read = wan_read,
-        .write = wan_write,
-        .experimental = 1,
-        },
-       {
-        .name = "video",
-        .init = video_init,
-        .read = video_read,
-        .write = video_write,
-        .exit = video_exit,
-        },
-       {
-        .name = "light",
-        .init = light_init,
-        .read = light_read,
-        .write = light_write,
-        },
-#ifdef CONFIG_THINKPAD_ACPI_DOCK
-       {
-        .name = "dock",
-        .read = dock_read,
-        .write = dock_write,
-        .notify = dock_notify,
-        .handle = &dock_handle,
-        .type = ACPI_SYSTEM_NOTIFY,
-        },
-       {
-        .name = "dock",
-        .hid = IBM_PCI_HID,
-        .notify = dock_notify,
-        .handle = &pci_handle,
-        .type = ACPI_SYSTEM_NOTIFY,
-        },
-#endif
-#ifdef CONFIG_THINKPAD_ACPI_BAY
-       {
-        .name = "bay",
-        .init = bay_init,
-        .read = bay_read,
-        .write = bay_write,
-        .notify = bay_notify,
-        .handle = &bay_handle,
-        .type = ACPI_SYSTEM_NOTIFY,
-        },
-#endif /* CONFIG_THINKPAD_ACPI_BAY */
-       {
-        .name = "cmos",
-        .read = cmos_read,
-        .write = cmos_write,
-        },
-       {
-        .name = "led",
-        .init = led_init,
-        .read = led_read,
-        .write = led_write,
-        },
-       {
-        .name = "beep",
-        .read = beep_read,
-        .write = beep_write,
-        },
-       {
-        .name = "thermal",
-        .init = thermal_init,
-        .read = thermal_read,
-        },
-       {
-        .name = "ecdump",
-        .read = ecdump_read,
-        .write = ecdump_write,
-        .experimental = 1,
-        },
-       {
-        .name = "brightness",
-        .read = brightness_read,
-        .write = brightness_write,
-        .init = brightness_init,
-        .exit = brightness_exit,
-        },
-       {
-        .name = "volume",
-        .read = volume_read,
-        .write = volume_write,
-        },
-       {
-        .name = "fan",
-        .read = fan_read,
-        .write = fan_write,
-        .init = fan_init,
-        .exit = fan_exit,
-        .experimental = 1,
-        },
-};
+static LIST_HEAD(tpacpi_all_drivers);
+
 
 /*
  * Module and infrastructure proble, init and exit handling
  */
 
-static int __init ibm_init(struct ibm_struct *ibm)
+#ifdef CONFIG_THINKPAD_ACPI_DEBUG
+static const char * __init str_supported(int is_supported)
+{
+       static char text_unsupported[] __initdata = "not supported";
+
+       return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
+}
+#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
+
+static int __init ibm_init(struct ibm_init_struct *iibm)
 {
        int ret;
+       struct ibm_struct *ibm = iibm->data;
        struct proc_dir_entry *entry;
 
-       if (ibm->experimental && !experimental)
+       BUG_ON(ibm == NULL);
+
+       INIT_LIST_HEAD(&ibm->all_drivers);
+
+       if (ibm->flags.experimental && !experimental)
                return 0;
 
-       if (ibm->hid) {
-               ret = register_tpacpi_subdriver(ibm);
-               if (ret < 0)
+       dbg_printk(TPACPI_DBG_INIT,
+               "probing for %s\n", ibm->name);
+
+       if (iibm->init) {
+               ret = iibm->init(iibm);
+               if (ret > 0)
+                       return 0;       /* probe failed */
+               if (ret)
                        return ret;
-               ibm->driver_registered = 1;
+
+               ibm->flags.init_called = 1;
        }
 
-       if (ibm->init) {
-               ret = ibm->init();
-               if (ret != 0)
-                       return ret;
-               ibm->init_called = 1;
+       if (ibm->acpi) {
+               if (ibm->acpi->hid) {
+                       ret = register_tpacpi_subdriver(ibm);
+                       if (ret)
+                               goto err_out;
+               }
+
+               if (ibm->acpi->notify) {
+                       ret = setup_acpi_notify(ibm);
+                       if (ret == -ENODEV) {
+                               printk(IBM_NOTICE "disabling subdriver %s\n",
+                                       ibm->name);
+                               ret = 0;
+                               goto err_out;
+                       }
+                       if (ret < 0)
+                               goto err_out;
+               }
        }
 
+       dbg_printk(TPACPI_DBG_INIT,
+               "%s installed\n", ibm->name);
+
        if (ibm->read) {
                entry = create_proc_entry(ibm->name,
                                          S_IFREG | S_IRUGO | S_IWUSR,
@@ -2594,47 +3955,70 @@ static int __init ibm_init(struct ibm_struct *ibm)
                if (!entry) {
                        printk(IBM_ERR "unable to create proc entry %s\n",
                               ibm->name);
-                       return -ENODEV;
+                       ret = -ENODEV;
+                       goto err_out;
                }
                entry->owner = THIS_MODULE;
                entry->data = ibm;
-               entry->read_proc = &dispatch_read;
+               entry->read_proc = &dispatch_procfs_read;
                if (ibm->write)
-                       entry->write_proc = &dispatch_write;
-               ibm->proc_created = 1;
+                       entry->write_proc = &dispatch_procfs_write;
+               ibm->flags.proc_created = 1;
        }
 
-       if (ibm->notify) {
-               ret = setup_notify(ibm);
-               if (ret == -ENODEV) {
-                       printk(IBM_NOTICE "disabling subdriver %s\n",
-                               ibm->name);
-                       ibm_exit(ibm);
-                       return 0;
-               }
-               if (ret < 0)
-                       return ret;
-       }
+       list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
 
        return 0;
+
+err_out:
+       dbg_printk(TPACPI_DBG_INIT,
+               "%s: at error exit path with result %d\n",
+               ibm->name, ret);
+
+       ibm_exit(ibm);
+       return (ret < 0)? ret : 0;
 }
 
 static void ibm_exit(struct ibm_struct *ibm)
 {
-       if (ibm->notify_installed)
-               acpi_remove_notify_handler(*ibm->handle, ibm->type,
-                                          dispatch_notify);
+       dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
+
+       list_del_init(&ibm->all_drivers);
+
+       if (ibm->flags.acpi_notify_installed) {
+               dbg_printk(TPACPI_DBG_EXIT,
+                       "%s: acpi_remove_notify_handler\n", ibm->name);
+               BUG_ON(!ibm->acpi);
+               acpi_remove_notify_handler(*ibm->acpi->handle,
+                                          ibm->acpi->type,
+                                          dispatch_acpi_notify);
+               ibm->flags.acpi_notify_installed = 0;
+               ibm->flags.acpi_notify_installed = 0;
+       }
 
-       if (ibm->proc_created)
+       if (ibm->flags.proc_created) {
+               dbg_printk(TPACPI_DBG_EXIT,
+                       "%s: remove_proc_entry\n", ibm->name);
                remove_proc_entry(ibm->name, proc_dir);
+               ibm->flags.proc_created = 0;
+       }
 
-       if (ibm->init_called && ibm->exit)
-               ibm->exit();
+       if (ibm->flags.acpi_driver_registered) {
+               dbg_printk(TPACPI_DBG_EXIT,
+                       "%s: acpi_bus_unregister_driver\n", ibm->name);
+               BUG_ON(!ibm->acpi);
+               acpi_bus_unregister_driver(ibm->acpi->driver);
+               kfree(ibm->acpi->driver);
+               ibm->acpi->driver = NULL;
+               ibm->flags.acpi_driver_registered = 0;
+       }
 
-       if (ibm->driver_registered) {
-               acpi_bus_unregister_driver(ibm->driver);
-               kfree(ibm->driver);
+       if (ibm->flags.init_called && ibm->exit) {
+               ibm->exit();
+               ibm->flags.init_called = 0;
        }
+
+       dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
 }
 
 /* Probing */
@@ -2665,20 +4049,134 @@ static char* __init check_dmi_for_ec(void)
        return NULL;
 }
 
+static int __init probe_for_thinkpad(void)
+{
+       int is_thinkpad;
+
+       if (acpi_disabled)
+               return -ENODEV;
+
+       /*
+        * Non-ancient models have better DMI tagging, but very old models
+        * don't.
+        */
+       is_thinkpad = dmi_name_in_vendors("ThinkPad");
+
+       /* ec is required because many other handles are relative to it */
+       IBM_ACPIHANDLE_INIT(ec);
+       if (!ec_handle) {
+               if (is_thinkpad)
+                       printk(IBM_ERR
+                               "Not yet supported ThinkPad detected!\n");
+               return -ENODEV;
+       }
+
+       /*
+        * Risks a regression on very old machines, but reduces potential
+        * false positives a damn great deal
+        */
+       if (!is_thinkpad)
+               is_thinkpad = dmi_name_in_vendors("IBM");
+
+       if (!is_thinkpad && !force_load)
+               return -ENODEV;
+
+       return 0;
+}
+
+
 /* Module init, exit, parameters */
 
+static struct ibm_init_struct ibms_init[] __initdata = {
+       {
+               .init = thinkpad_acpi_driver_init,
+               .data = &thinkpad_acpi_driver_data,
+       },
+       {
+               .init = hotkey_init,
+               .data = &hotkey_driver_data,
+       },
+       {
+               .init = bluetooth_init,
+               .data = &bluetooth_driver_data,
+       },
+       {
+               .init = wan_init,
+               .data = &wan_driver_data,
+       },
+       {
+               .init = video_init,
+               .data = &video_driver_data,
+       },
+       {
+               .init = light_init,
+               .data = &light_driver_data,
+       },
+#ifdef CONFIG_THINKPAD_ACPI_DOCK
+       {
+               .init = dock_init,
+               .data = &dock_driver_data[0],
+       },
+       {
+               .init = dock_init2,
+               .data = &dock_driver_data[1],
+       },
+#endif
+#ifdef CONFIG_THINKPAD_ACPI_BAY
+       {
+               .init = bay_init,
+               .data = &bay_driver_data,
+       },
+#endif
+       {
+               .init = cmos_init,
+               .data = &cmos_driver_data,
+       },
+       {
+               .init = led_init,
+               .data = &led_driver_data,
+       },
+       {
+               .init = beep_init,
+               .data = &beep_driver_data,
+       },
+       {
+               .init = thermal_init,
+               .data = &thermal_driver_data,
+       },
+       {
+               .data = &ecdump_driver_data,
+       },
+       {
+               .init = brightness_init,
+               .data = &brightness_driver_data,
+       },
+       {
+               .data = &volume_driver_data,
+       },
+       {
+               .init = fan_init,
+               .data = &fan_driver_data,
+       },
+};
+
 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
 {
        unsigned int i;
+       struct ibm_struct *ibm;
+
+       for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
+               ibm = ibms_init[i].data;
+               BUG_ON(ibm == NULL);
 
-       for (i = 0; i < ARRAY_SIZE(ibms); i++)
-               if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
-                       if (strlen(val) > sizeof(ibms[i].param) - 2)
+               if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
+                       if (strlen(val) > sizeof(ibms_init[i].param) - 2)
                                return -ENOSPC;
-                       strcpy(ibms[i].param, val);
-                       strcat(ibms[i].param, ",");
+                       strcpy(ibms_init[i].param, val);
+                       strcat(ibms_init[i].param, ",");
                        return 0;
                }
+       }
 
        return -EINVAL;
 }
@@ -2686,6 +4184,15 @@ static int __init set_ibm_param(const char *val, struct kernel_param *kp)
 static int experimental;
 module_param(experimental, int, 0);
 
+static u32 dbg_level;
+module_param_named(debug, dbg_level, uint, 0);
+
+static int force_load;
+module_param(force_load, int, 0);
+
+static int fan_control_allowed;
+module_param_named(fan_control, fan_control_allowed, int, 0);
+
 #define IBM_PARAM(feature) \
        module_param_call(feature, set_ibm_param, NULL, NULL, 0)
 
@@ -2707,64 +4214,66 @@ IBM_PARAM(brightness);
 IBM_PARAM(volume);
 IBM_PARAM(fan);
 
-static int __init acpi_ibm_init(void)
+static int __init thinkpad_acpi_module_init(void)
 {
        int ret, i;
 
-       if (acpi_disabled)
-               return -ENODEV;
-
-       /* ec is required because many other handles are relative to it */
-       IBM_HANDLE_INIT(ec);
-       if (!ec_handle) {
-               printk(IBM_ERR "ec object not found\n");
-               return -ENODEV;
-       }
+       /* Driver-level probe */
+       ret = probe_for_thinkpad();
+       if (ret)
+               return ret;
 
-       /* Models with newer firmware report the EC in DMI */
+       /* Driver initialization */
        ibm_thinkpad_ec_found = check_dmi_for_ec();
+       IBM_ACPIHANDLE_INIT(ecrd);
+       IBM_ACPIHANDLE_INIT(ecwr);
 
-       /* these handles are not required */
-       IBM_HANDLE_INIT(vid);
-       IBM_HANDLE_INIT(vid2);
-       IBM_HANDLE_INIT(ledb);
-       IBM_HANDLE_INIT(led);
-       IBM_HANDLE_INIT(hkey);
-       IBM_HANDLE_INIT(lght);
-       IBM_HANDLE_INIT(cmos);
-#ifdef CONFIG_THINKPAD_ACPI_DOCK
-       IBM_HANDLE_INIT(dock);
-#endif
-       IBM_HANDLE_INIT(pci);
-#ifdef CONFIG_THINKPAD_ACPI_BAY
-       IBM_HANDLE_INIT(bay);
-       if (bay_handle)
-               IBM_HANDLE_INIT(bay_ej);
-       IBM_HANDLE_INIT(bay2);
-       if (bay2_handle)
-               IBM_HANDLE_INIT(bay2_ej);
-#endif /* CONFIG_THINKPAD_ACPI_BAY */
-       IBM_HANDLE_INIT(beep);
-       IBM_HANDLE_INIT(ecrd);
-       IBM_HANDLE_INIT(ecwr);
-       IBM_HANDLE_INIT(fans);
-       IBM_HANDLE_INIT(gfan);
-       IBM_HANDLE_INIT(sfan);
-
-       proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
+       proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
        if (!proc_dir) {
-               printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
-               acpi_ibm_exit();
+               printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
+               thinkpad_acpi_module_exit();
                return -ENODEV;
        }
        proc_dir->owner = THIS_MODULE;
 
-       for (i = 0; i < ARRAY_SIZE(ibms); i++) {
-               ret = ibm_init(&ibms[i]);
-               if (ret >= 0 && *ibms[i].param)
-                       ret = ibms[i].write(ibms[i].param);
+       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;
+       }
+       for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
+               ret = ibm_init(&ibms_init[i]);
+               if (ret >= 0 && *ibms_init[i].param)
+                       ret = ibms_init[i].data->write(ibms_init[i].param);
                if (ret < 0) {
-                       acpi_ibm_exit();
+                       thinkpad_acpi_module_exit();
                        return ret;
                }
        }
@@ -2772,19 +4281,32 @@ static int __init acpi_ibm_init(void)
        return 0;
 }
 
-static void acpi_ibm_exit(void)
+static void thinkpad_acpi_module_exit(void)
 {
-       int i;
+       struct ibm_struct *ibm, *itmp;
+
+       list_for_each_entry_safe_reverse(ibm, itmp,
+                                        &tpacpi_all_drivers,
+                                        all_drivers) {
+               ibm_exit(ibm);
+       }
 
-       for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
-               ibm_exit(&ibms[i]);
+       dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
+
+       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_DIR, acpi_root_dir);
+               remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
 
-       if (ibm_thinkpad_ec_found)
-               kfree(ibm_thinkpad_ec_found);
+       kfree(ibm_thinkpad_ec_found);
 }
 
-module_init(acpi_ibm_init);
-module_exit(acpi_ibm_exit);
+module_init(thinkpad_acpi_module_init);
+module_exit(thinkpad_acpi_module_exit);