include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / acpi / dock.c
index 98ec717..a9c429c 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/types.h>
 #include <linux/notifier.h>
@@ -33,6 +34,8 @@
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
 
+#define PREFIX "ACPI: "
+
 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
 
 ACPI_MODULE_NAME("dock");
@@ -48,8 +51,12 @@ MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
        " before undocking");
 
 static struct atomic_notifier_head dock_notifier_list;
-static struct platform_device *dock_device;
-static char dock_device_name[] = "dock";
+
+static const struct acpi_device_id dock_device_ids[] = {
+       {"LNXDOCK", 0},
+       {"", 0},
+};
+MODULE_DEVICE_TABLE(acpi, dock_device_ids);
 
 struct dock_station {
        acpi_handle handle;
@@ -59,61 +66,57 @@ struct dock_station {
        struct mutex hp_lock;
        struct list_head dependent_devices;
        struct list_head hotplug_devices;
+
+       struct list_head sibling;
+       struct platform_device *dock_device;
 };
+static LIST_HEAD(dock_stations);
+static int dock_station_count;
 
 struct dock_dependent_device {
        struct list_head list;
        struct list_head hotplug_list;
        acpi_handle handle;
-       acpi_notify_handler handler;
+       struct acpi_dock_ops *ops;
        void *context;
 };
 
 #define DOCK_DOCKING   0x00000001
 #define DOCK_UNDOCKING  0x00000002
+#define DOCK_IS_DOCK   0x00000010
+#define DOCK_IS_ATA    0x00000020
+#define DOCK_IS_BAT    0x00000040
 #define DOCK_EVENT     3
 #define UNDOCK_EVENT   2
 
-static struct dock_station *dock_station;
-
 /*****************************************************************************
  *                         Dock Dependent device functions                   *
  *****************************************************************************/
 /**
- *  alloc_dock_dependent_device - allocate and init a dependent device
- *  @handle: the acpi_handle of the dependent device
+ * add_dock_dependent_device - associate a device with the dock station
+ * @ds: The dock station
+ * @handle: handle of the dependent device
  *
- *  Allocate memory for a dependent device structure for a device referenced
- *  by the acpi handle
+ * Add the dependent device to the dock's dependent device list.
  */
-static struct dock_dependent_device *
-alloc_dock_dependent_device(acpi_handle handle)
+static int
+add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
 {
        struct dock_dependent_device *dd;
 
        dd = kzalloc(sizeof(*dd), GFP_KERNEL);
-       if (dd) {
-               dd->handle = handle;
-               INIT_LIST_HEAD(&dd->list);
-               INIT_LIST_HEAD(&dd->hotplug_list);
-       }
-       return dd;
-}
+       if (!dd)
+               return -ENOMEM;
+
+       dd->handle = handle;
+       INIT_LIST_HEAD(&dd->list);
+       INIT_LIST_HEAD(&dd->hotplug_list);
 
-/**
- * add_dock_dependent_device - associate a device with the dock station
- * @ds: The dock station
- * @dd: The dependent device
- *
- * Add the dependent device to the dock's dependent device list.
- */
-static void
-add_dock_dependent_device(struct dock_station *ds,
-                         struct dock_dependent_device *dd)
-{
        spin_lock(&ds->dd_lock);
        list_add_tail(&dd->list, &ds->dependent_devices);
        spin_unlock(&ds->dd_lock);
+
+       return 0;
 }
 
 /**
@@ -193,6 +196,59 @@ static int is_dock(acpi_handle handle)
        return 1;
 }
 
+static int is_ejectable(acpi_handle handle)
+{
+       acpi_status status;
+       acpi_handle tmp;
+
+       status = acpi_get_handle(handle, "_EJ0", &tmp);
+       if (ACPI_FAILURE(status))
+               return 0;
+       return 1;
+}
+
+static int is_ata(acpi_handle handle)
+{
+       acpi_handle tmp;
+
+       if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
+          (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
+          (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
+          (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
+               return 1;
+
+       return 0;
+}
+
+static int is_battery(acpi_handle handle)
+{
+       struct acpi_device_info *info;
+       int ret = 1;
+
+       if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
+               return 0;
+       if (!(info->valid & ACPI_VALID_HID))
+               ret = 0;
+       else
+               ret = !strcmp("PNP0C0A", info->hardware_id.string);
+
+       kfree(info);
+       return ret;
+}
+
+static int is_ejectable_bay(acpi_handle handle)
+{
+       acpi_handle phandle;
+
+       if (!is_ejectable(handle))
+               return 0;
+       if (is_battery(handle) || is_ata(handle))
+               return 1;
+       if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
+               return 1;
+       return 0;
+}
+
 /**
  * is_dock_device - see if a device is on a dock station
  * @handle: acpi handle of the device
@@ -203,15 +259,20 @@ static int is_dock(acpi_handle handle)
  */
 int is_dock_device(acpi_handle handle)
 {
-       if (!dock_station)
+       struct dock_station *dock_station;
+
+       if (!dock_station_count)
                return 0;
 
-       if (is_dock(handle) || find_dock_dependent_device(dock_station, handle))
+       if (is_dock(handle))
                return 1;
 
+       list_for_each_entry(dock_station, &dock_stations, sibling)
+               if (find_dock_dependent_device(dock_station, handle))
+                       return 1;
+
        return 0;
 }
-
 EXPORT_SYMBOL_GPL(is_dock_device);
 
 /**
@@ -223,7 +284,7 @@ EXPORT_SYMBOL_GPL(is_dock_device);
  */
 static int dock_present(struct dock_station *ds)
 {
-       unsigned long sta;
+       unsigned long long sta;
        acpi_status status;
 
        if (ds) {
@@ -234,8 +295,6 @@ static int dock_present(struct dock_station *ds)
        return 0;
 }
 
-
-
 /**
  * dock_create_acpi_device - add new devices to acpi
  * @handle - handle of the device to add
@@ -249,7 +308,7 @@ static int dock_present(struct dock_station *ds)
  */
 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
 {
-       struct acpi_device *device = NULL;
+       struct acpi_device *device;
        struct acpi_device *parent_device;
        acpi_handle parent;
        int ret;
@@ -266,8 +325,7 @@ static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
                ret = acpi_bus_add(&device, parent_device, handle,
                        ACPI_BUS_TYPE_DEVICE);
                if (ret) {
-                       pr_debug("error adding bus, %x\n",
-                               -ret);
+                       pr_debug("error adding bus, %x\n", -ret);
                        return NULL;
                }
        }
@@ -293,7 +351,6 @@ static void dock_remove_acpi_device(acpi_handle handle)
        }
 }
 
-
 /**
  * hotplug_dock_devices - insert or remove devices on the dock station
  * @ds: the dock station
@@ -313,10 +370,9 @@ static void hotplug_dock_devices(struct dock_station *ds, u32 event)
        /*
         * First call driver specific hotplug functions
         */
-       list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
-               if (dd->handler)
-                       dd->handler(dd->handle, event, dd->context);
-       }
+       list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
+               if (dd->ops && dd->ops->handler)
+                       dd->ops->handler(dd->handle, event, dd->context);
 
        /*
         * Now make sure that an acpi_device is created for each
@@ -335,12 +391,29 @@ static void hotplug_dock_devices(struct dock_station *ds, u32 event)
 
 static void dock_event(struct dock_station *ds, u32 event, int num)
 {
-       struct device *dev = &dock_device->dev;
+       struct device *dev = &ds->dock_device->dev;
+       char event_string[13];
+       char *envp[] = { event_string, NULL };
+       struct dock_dependent_device *dd;
+
+       if (num == UNDOCK_EVENT)
+               sprintf(event_string, "EVENT=undock");
+       else
+               sprintf(event_string, "EVENT=dock");
+
        /*
         * Indicate that the status of the dock station has
         * changed.
         */
-       kobject_uevent(&dev->kobj, KOBJ_CHANGE);
+       if (num == DOCK_EVENT)
+               kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
+
+       list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
+               if (dd->ops && dd->ops->uevent)
+                       dd->ops->uevent(dd->handle, event, dd->context);
+
+       if (num != DOCK_EVENT)
+               kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
 }
 
 /**
@@ -369,8 +442,8 @@ static void eject_dock(struct dock_station *ds)
        arg.type = ACPI_TYPE_INTEGER;
        arg.integer.value = 1;
 
-       if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
-                                             &arg_list, NULL)))
+       status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
+       if (ACPI_FAILURE(status))
                pr_debug("Failed to evaluate _EJ0!\n");
 }
 
@@ -388,12 +461,11 @@ static void handle_dock(struct dock_station *ds, int dock)
        union acpi_object arg;
        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
        struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-       union acpi_object *obj;
 
        acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
-       obj = name_buffer.pointer;
 
-       printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
+       printk(KERN_INFO PREFIX "%s - %s\n",
+               (char *)name_buffer.pointer, dock ? "docking" : "undocking");
 
        /* _DCK method has one argument */
        arg_list.count = 1;
@@ -401,8 +473,10 @@ static void handle_dock(struct dock_station *ds, int dock)
        arg.type = ACPI_TYPE_INTEGER;
        arg.integer.value = dock;
        status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
-       if (ACPI_FAILURE(status))
-               pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
+       if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
+               ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
+                       " _DCK\n", (char *)name_buffer.pointer));
+
        kfree(buffer.pointer);
        kfree(name_buffer.pointer);
 }
@@ -438,6 +512,25 @@ static inline void complete_undock(struct dock_station *ds)
        ds->flags &= ~(DOCK_UNDOCKING);
 }
 
+static void dock_lock(struct dock_station *ds, int lock)
+{
+       struct acpi_object_list arg_list;
+       union acpi_object arg;
+       acpi_status status;
+
+       arg_list.count = 1;
+       arg_list.pointer = &arg;
+       arg.type = ACPI_TYPE_INTEGER;
+       arg.integer.value = !!lock;
+       status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
+       if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
+               if (lock)
+                       printk(KERN_WARNING PREFIX "Locking device failed\n");
+               else
+                       printk(KERN_WARNING PREFIX "Unlocking device failed\n");
+       }
+}
+
 /**
  * dock_in_progress - see if we are in the middle of handling a dock event
  * @ds: the dock station
@@ -465,12 +558,11 @@ static int dock_in_progress(struct dock_station *ds)
  */
 int register_dock_notifier(struct notifier_block *nb)
 {
-       if (!dock_station)
+       if (!dock_station_count)
                return -ENODEV;
 
        return atomic_notifier_chain_register(&dock_notifier_list, nb);
 }
-
 EXPORT_SYMBOL_GPL(register_dock_notifier);
 
 /**
@@ -479,18 +571,17 @@ EXPORT_SYMBOL_GPL(register_dock_notifier);
  */
 void unregister_dock_notifier(struct notifier_block *nb)
 {
-       if (!dock_station)
+       if (!dock_station_count)
                return;
 
        atomic_notifier_chain_unregister(&dock_notifier_list, nb);
 }
-
 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
 
 /**
  * register_hotplug_dock_device - register a hotplug function
  * @handle: the handle of the device
- * @handler: the acpi_notifier_handler to call after docking
+ * @ops: handlers to call after docking
  * @context: device specific data
  *
  * If a driver would like to perform a hotplug operation after a dock
@@ -498,29 +589,37 @@ EXPORT_SYMBOL_GPL(unregister_dock_notifier);
  * the dock driver after _DCK is executed.
  */
 int
-register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
+register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
                             void *context)
 {
        struct dock_dependent_device *dd;
+       struct dock_station *dock_station;
+       int ret = -EINVAL;
 
-       if (!dock_station)
+       if (!dock_station_count)
                return -ENODEV;
 
        /*
         * make sure this handle is for a device dependent on the dock,
         * this would include the dock station itself
         */
-       dd = find_dock_dependent_device(dock_station, handle);
-       if (dd) {
-               dd->handler = handler;
-               dd->context = context;
-               dock_add_hotplug_device(dock_station, dd);
-               return 0;
+       list_for_each_entry(dock_station, &dock_stations, sibling) {
+               /*
+                * An ATA bay can be in a dock and itself can be ejected
+                * separately, so there are two 'dock stations' which need the
+                * ops
+                */
+               dd = find_dock_dependent_device(dock_station, handle);
+               if (dd) {
+                       dd->ops = ops;
+                       dd->context = context;
+                       dock_add_hotplug_device(dock_station, dd);
+                       ret = 0;
+               }
        }
 
-       return -EINVAL;
+       return ret;
 }
-
 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
 
 /**
@@ -530,15 +629,17 @@ EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
 void unregister_hotplug_dock_device(acpi_handle handle)
 {
        struct dock_dependent_device *dd;
+       struct dock_station *dock_station;
 
-       if (!dock_station)
+       if (!dock_station_count)
                return;
 
-       dd = find_dock_dependent_device(dock_station, handle);
-       if (dd)
-               dock_del_hotplug_device(dock_station, dd);
+       list_for_each_entry(dock_station, &dock_stations, sibling) {
+               dd = find_dock_dependent_device(dock_station, handle);
+               if (dd)
+                       dock_del_hotplug_device(dock_station, dd);
+       }
 }
-
 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
 
 /**
@@ -549,9 +650,6 @@ EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  */
 static int handle_eject_request(struct dock_station *ds, u32 event)
 {
-       if (!dock_present(ds))
-               return -ENODEV;
-
        if (dock_in_progress(ds))
                return -EBUSY;
 
@@ -559,10 +657,14 @@ static int handle_eject_request(struct dock_station *ds, u32 event)
         * here we need to generate the undock
         * event prior to actually doing the undock
         * so that the device struct still exists.
+        * Also, even send the dock event if the
+        * device is not present anymore
         */
        dock_event(ds, event, UNDOCK_EVENT);
+
        hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
        undock(ds);
+       dock_lock(ds, 0);
        eject_dock(ds);
        if (dock_present(ds)) {
                printk(KERN_ERR PREFIX "Unable to undock!\n");
@@ -585,14 +687,36 @@ static int handle_eject_request(struct dock_station *ds, u32 event)
 static void dock_notify(acpi_handle handle, u32 event, void *data)
 {
        struct dock_station *ds = data;
+       struct acpi_device *tmp;
+       int surprise_removal = 0;
+
+       /*
+        * According to acpi spec 3.0a, if a DEVICE_CHECK notification
+        * is sent and _DCK is present, it is assumed to mean an undock
+        * request.
+        */
+       if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
+               event = ACPI_NOTIFY_EJECT_REQUEST;
 
+       /*
+        * dock station: BUS_CHECK - docked or surprise removal
+        *               DEVICE_CHECK - undocked
+        * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
+        *
+        * To simplify event handling, dock dependent device handler always
+        * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
+        * ACPI_NOTIFY_EJECT_REQUEST for removal
+        */
        switch (event) {
        case ACPI_NOTIFY_BUS_CHECK:
-               if (!dock_in_progress(ds) && dock_present(ds)) {
+       case ACPI_NOTIFY_DEVICE_CHECK:
+               if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
+                  &tmp)) {
                        begin_dock(ds);
                        dock(ds);
                        if (!dock_present(ds)) {
                                printk(KERN_ERR PREFIX "Unable to dock!\n");
+                               complete_dock(ds);
                                break;
                        }
                        atomic_notifier_call_chain(&dock_notifier_list,
@@ -600,20 +724,19 @@ static void dock_notify(acpi_handle handle, u32 event, void *data)
                        hotplug_dock_devices(ds, event);
                        complete_dock(ds);
                        dock_event(ds, event, DOCK_EVENT);
+                       dock_lock(ds, 1);
+                       break;
                }
-               break;
-       case ACPI_NOTIFY_DEVICE_CHECK:
-       /*
-         * According to acpi spec 3.0a, if a DEVICE_CHECK notification
-         * is sent and _DCK is present, it is assumed to mean an
-         * undock request.  This notify routine will only be called
-         * for objects defining _DCK, so we will fall through to eject
-         * request here.  However, we will pass an eject request through
-        * to the driver who wish to hotplug.
-         */
+               if (dock_present(ds) || dock_in_progress(ds))
+                       break;
+               /* This is a surprise removal */
+               surprise_removal = 1;
+               event = ACPI_NOTIFY_EJECT_REQUEST;
+               /* Fall back */
        case ACPI_NOTIFY_EJECT_REQUEST:
                begin_undock(ds);
-               if (immediate_undock)
+               if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
+                  || surprise_removal)
                        handle_eject_request(ds, event);
                else
                        dock_event(ds, event, UNDOCK_EVENT);
@@ -623,6 +746,50 @@ static void dock_notify(acpi_handle handle, u32 event, void *data)
        }
 }
 
+struct dock_data {
+       acpi_handle handle;
+       unsigned long event;
+       struct dock_station *ds;
+};
+
+static void acpi_dock_deferred_cb(void *context)
+{
+       struct dock_data *data = context;
+
+       dock_notify(data->handle, data->event, data->ds);
+       kfree(data);
+}
+
+static int acpi_dock_notifier_call(struct notifier_block *this,
+       unsigned long event, void *data)
+{
+       struct dock_station *dock_station;
+       acpi_handle handle = data;
+
+       if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
+          && event != ACPI_NOTIFY_EJECT_REQUEST)
+               return 0;
+       list_for_each_entry(dock_station, &dock_stations, sibling) {
+               if (dock_station->handle == handle) {
+                       struct dock_data *dd;
+
+                       dd = kmalloc(sizeof(*dd), GFP_KERNEL);
+                       if (!dd)
+                               return 0;
+                       dd->handle = handle;
+                       dd->event = event;
+                       dd->ds = dock_station;
+                       acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
+                       return 0 ;
+               }
+       }
+       return 0;
+}
+
+static struct notifier_block dock_acpi_notifier = {
+       .notifier_call = acpi_dock_notifier_call,
+};
+
 /**
  * find_dock_devices - find devices on the dock station
  * @handle: the handle of the device we are examining
@@ -640,7 +807,6 @@ find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
        acpi_status status;
        acpi_handle tmp, parent;
        struct dock_station *ds = context;
-       struct dock_dependent_device *dd;
 
        status = acpi_bus_get_ejd(handle, &tmp);
        if (ACPI_FAILURE(status)) {
@@ -654,11 +820,9 @@ find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
                        goto fdd_out;
        }
 
-       if (tmp == ds->handle) {
-               dd = alloc_dock_dependent_device(handle);
-               if (dd)
-                       add_dock_dependent_device(ds, dd);
-       }
+       if (tmp == ds->handle)
+               add_dock_dependent_device(ds, handle);
+
 fdd_out:
        return AE_OK;
 }
@@ -669,10 +833,15 @@ fdd_out:
 static ssize_t show_docked(struct device *dev,
                           struct device_attribute *attr, char *buf)
 {
-       return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
+       struct acpi_device *tmp;
+
+       struct dock_station *dock_station = dev->platform_data;
 
+       if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
+               return snprintf(buf, PAGE_SIZE, "1\n");
+       return snprintf(buf, PAGE_SIZE, "0\n");
 }
-DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
+static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
 
 /*
  * show_flags - read method for flags file in sysfs
@@ -680,10 +849,11 @@ DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
 static ssize_t show_flags(struct device *dev,
                          struct device_attribute *attr, char *buf)
 {
+       struct dock_station *dock_station = dev->platform_data;
        return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
 
 }
-DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
+static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
 
 /*
  * write_undock - write method for "undock" file in sysfs
@@ -692,14 +862,16 @@ static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
                           const char *buf, size_t count)
 {
        int ret;
+       struct dock_station *dock_station = dev->platform_data;
 
        if (!count)
                return -EINVAL;
 
+       begin_undock(dock_station);
        ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
        return ret ? ret: count;
 }
-DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
+static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
 
 /*
  * show_dock_uid - read method for "uid" file in sysfs
@@ -707,15 +879,48 @@ DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
 static ssize_t show_dock_uid(struct device *dev,
                             struct device_attribute *attr, char *buf)
 {
-       unsigned long lbuf;
+       unsigned long long lbuf;
+       struct dock_station *dock_station = dev->platform_data;
        acpi_status status = acpi_evaluate_integer(dock_station->handle,
                                        "_UID", NULL, &lbuf);
        if (ACPI_FAILURE(status))
            return 0;
 
-       return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
+       return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
 }
-DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
+static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
+
+static ssize_t show_dock_type(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       struct dock_station *dock_station = dev->platform_data;
+       char *type;
+
+       if (dock_station->flags & DOCK_IS_DOCK)
+               type = "dock_station";
+       else if (dock_station->flags & DOCK_IS_ATA)
+               type = "ata_bay";
+       else if (dock_station->flags & DOCK_IS_BAT)
+               type = "battery_bay";
+       else
+               type = "unknown";
+
+       return snprintf(buf, PAGE_SIZE, "%s\n", type);
+}
+static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
+
+static struct attribute *dock_attributes[] = {
+       &dev_attr_docked.attr,
+       &dev_attr_flags.attr,
+       &dev_attr_undock.attr,
+       &dev_attr_uid.attr,
+       &dev_attr_type.attr,
+       NULL
+};
+
+static struct attribute_group dock_attribute_group = {
+       .attrs = dock_attributes
+};
 
 /**
  * dock_add - add a new dock station
@@ -726,146 +931,86 @@ DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
  */
 static int dock_add(acpi_handle handle)
 {
-       int ret;
-       acpi_status status;
-       struct dock_dependent_device *dd;
+       int ret, id;
+       struct dock_station ds, *dock_station;
+       struct platform_device *dd;
+
+       id = dock_station_count;
+       memset(&ds, 0, sizeof(ds));
+       dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
+       if (IS_ERR(dd))
+               return PTR_ERR(dd);
+
+       dock_station = dd->dev.platform_data;
 
-       /* allocate & initialize the dock_station private data */
-       dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
-       if (!dock_station)
-               return -ENOMEM;
        dock_station->handle = handle;
+       dock_station->dock_device = dd;
        dock_station->last_dock_time = jiffies - HZ;
-       INIT_LIST_HEAD(&dock_station->dependent_devices);
-       INIT_LIST_HEAD(&dock_station->hotplug_devices);
-       spin_lock_init(&dock_station->dd_lock);
+
        mutex_init(&dock_station->hp_lock);
+       spin_lock_init(&dock_station->dd_lock);
+       INIT_LIST_HEAD(&dock_station->sibling);
+       INIT_LIST_HEAD(&dock_station->hotplug_devices);
        ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
+       INIT_LIST_HEAD(&dock_station->dependent_devices);
 
-       /* initialize platform device stuff */
-       dock_device =
-               platform_device_register_simple(dock_device_name, 0, NULL, 0);
-       if (IS_ERR(dock_device)) {
-               kfree(dock_station);
-               dock_station = NULL;
-               return PTR_ERR(dock_device);
-       }
+       /* we want the dock device to send uevents */
+       dev_set_uevent_suppress(&dd->dev, 0);
 
-       ret = device_create_file(&dock_device->dev, &dev_attr_docked);
-       if (ret) {
-               printk("Error %d adding sysfs file\n", ret);
-               platform_device_unregister(dock_device);
-               kfree(dock_station);
-               dock_station = NULL;
-               return ret;
-       }
-       ret = device_create_file(&dock_device->dev, &dev_attr_undock);
-       if (ret) {
-               printk("Error %d adding sysfs file\n", ret);
-               device_remove_file(&dock_device->dev, &dev_attr_docked);
-               platform_device_unregister(dock_device);
-               kfree(dock_station);
-               dock_station = NULL;
-               return ret;
-       }
-       ret = device_create_file(&dock_device->dev, &dev_attr_uid);
-       if (ret) {
-               printk("Error %d adding sysfs file\n", ret);
-               device_remove_file(&dock_device->dev, &dev_attr_docked);
-               device_remove_file(&dock_device->dev, &dev_attr_undock);
-               platform_device_unregister(dock_device);
-               kfree(dock_station);
-               dock_station = NULL;
-               return ret;
-       }
-       ret = device_create_file(&dock_device->dev, &dev_attr_flags);
-       if (ret) {
-               printk("Error %d adding sysfs file\n", ret);
-               device_remove_file(&dock_device->dev, &dev_attr_docked);
-               device_remove_file(&dock_device->dev, &dev_attr_undock);
-               device_remove_file(&dock_device->dev, &dev_attr_uid);
-               platform_device_unregister(dock_device);
-               kfree(dock_station);
-               dock_station = NULL;
-               return ret;
-       }
+       if (is_dock(handle))
+               dock_station->flags |= DOCK_IS_DOCK;
+       if (is_ata(handle))
+               dock_station->flags |= DOCK_IS_ATA;
+       if (is_battery(handle))
+               dock_station->flags |= DOCK_IS_BAT;
+
+       ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
+       if (ret)
+               goto err_unregister;
 
        /* Find dependent devices */
        acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
-                           ACPI_UINT32_MAX, find_dock_devices, dock_station,
-                           NULL);
+                           ACPI_UINT32_MAX, find_dock_devices, NULL,
+                           dock_station, NULL);
 
        /* add the dock station as a device dependent on itself */
-       dd = alloc_dock_dependent_device(handle);
-       if (!dd) {
-               kfree(dock_station);
-               dock_station = NULL;
-               ret = -ENOMEM;
-               goto dock_add_err_unregister;
-       }
-       add_dock_dependent_device(dock_station, dd);
-
-       /* register for dock events */
-       status = acpi_install_notify_handler(dock_station->handle,
-                                            ACPI_SYSTEM_NOTIFY,
-                                            dock_notify, dock_station);
-
-       if (ACPI_FAILURE(status)) {
-               printk(KERN_ERR PREFIX "Error installing notify handler\n");
-               ret = -ENODEV;
-               goto dock_add_err;
-       }
-
-       printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_DESCRIPTION);
+       ret = add_dock_dependent_device(dock_station, handle);
+       if (ret)
+               goto err_rmgroup;
 
+       dock_station_count++;
+       list_add(&dock_station->sibling, &dock_stations);
        return 0;
 
-dock_add_err:
-       kfree(dd);
-dock_add_err_unregister:
-       device_remove_file(&dock_device->dev, &dev_attr_docked);
-       device_remove_file(&dock_device->dev, &dev_attr_undock);
-       device_remove_file(&dock_device->dev, &dev_attr_uid);
-       device_remove_file(&dock_device->dev, &dev_attr_flags);
-       platform_device_unregister(dock_device);
-       kfree(dock_station);
-       dock_station = NULL;
+err_rmgroup:
+       sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
+err_unregister:
+       platform_device_unregister(dd);
+       printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
        return ret;
 }
 
 /**
  * dock_remove - free up resources related to the dock station
  */
-static int dock_remove(void)
+static int dock_remove(struct dock_station *ds)
 {
        struct dock_dependent_device *dd, *tmp;
-       acpi_status status;
+       struct platform_device *dock_device = ds->dock_device;
 
-       if (!dock_station)
+       if (!dock_station_count)
                return 0;
 
        /* remove dependent devices */
-       list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
-                                list)
-           kfree(dd);
-
-       /* remove dock notify handler */
-       status = acpi_remove_notify_handler(dock_station->handle,
-                                           ACPI_SYSTEM_NOTIFY,
-                                           dock_notify);
-       if (ACPI_FAILURE(status))
-               printk(KERN_ERR "Error removing notify handler\n");
+       list_for_each_entry_safe(dd, tmp, &ds->dependent_devices, list)
+               kfree(dd);
+
+       list_del(&ds->sibling);
 
        /* cleanup sysfs */
-       device_remove_file(&dock_device->dev, &dev_attr_docked);
-       device_remove_file(&dock_device->dev, &dev_attr_undock);
-       device_remove_file(&dock_device->dev, &dev_attr_uid);
-       device_remove_file(&dock_device->dev, &dev_attr_flags);
+       sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
        platform_device_unregister(dock_device);
 
-       /* free dock station memory */
-       kfree(dock_station);
-       dock_station = NULL;
        return 0;
 }
 
@@ -881,38 +1026,59 @@ static int dock_remove(void)
 static acpi_status
 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
 {
-       int *count = context;
        acpi_status status = AE_OK;
 
-       if (is_dock(handle)) {
-               if (dock_add(handle) >= 0) {
-                       (*count)++;
+       if (is_dock(handle))
+               if (dock_add(handle) >= 0)
                        status = AE_CTRL_TERMINATE;
-               }
-       }
+
        return status;
 }
 
-static int __init dock_init(void)
+static acpi_status
+find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
 {
-       int num = 0;
+       /* If bay is a dock, it's already handled */
+       if (is_ejectable_bay(handle) && !is_dock(handle))
+               dock_add(handle);
+       return AE_OK;
+}
 
-       dock_station = NULL;
+static int __init dock_init(void)
+{
+       if (acpi_disabled)
+               return 0;
 
        /* look for a dock station */
        acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
-                           ACPI_UINT32_MAX, find_dock, &num, NULL);
+                           ACPI_UINT32_MAX, find_dock, NULL, NULL, NULL);
 
-       if (!num)
-               printk(KERN_INFO "No dock devices found.\n");
+       /* look for bay */
+       acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
+                       ACPI_UINT32_MAX, find_bay, NULL, NULL, NULL);
+       if (!dock_station_count) {
+               printk(KERN_INFO PREFIX "No dock devices found.\n");
+               return 0;
+       }
 
+       register_acpi_bus_notifier(&dock_acpi_notifier);
+       printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
+               ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
        return 0;
 }
 
 static void __exit dock_exit(void)
 {
-       dock_remove();
+       struct dock_station *tmp, *dock_station;
+
+       unregister_acpi_bus_notifier(&dock_acpi_notifier);
+       list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
+               dock_remove(dock_station);
 }
 
-postcore_initcall(dock_init);
+/*
+ * Must be called before drivers of devices in dock, otherwise we can't know
+ * which devices are in a dock
+ */
+subsys_initcall(dock_init);
 module_exit(dock_exit);