[POWERPC] celleb: Fix parsing of machine type hack command line option
[powerpc.git] / drivers / acpi / bay.c
index 0c0a620..fb3f31b 100644 (file)
 #include <acpi/acpi_drivers.h>
 #include <linux/seq_file.h>
 #include <asm/uaccess.h>
+#include <linux/platform_device.h>
 
-#define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver"
-
-ACPI_MODULE_NAME("bay")
+ACPI_MODULE_NAME("bay");
 MODULE_AUTHOR("Kristen Carlson Accardi");
-MODULE_DESCRIPTION(ACPI_BAY_DRIVER_NAME);
+MODULE_DESCRIPTION("ACPI Removable Drive Bay Driver");
 MODULE_LICENSE("GPL");
 #define ACPI_BAY_CLASS "bay"
 #define ACPI_BAY_COMPONENT     0x10000000
@@ -45,30 +44,16 @@ MODULE_LICENSE("GPL");
        struct acpi_buffer buffer = {sizeof(prefix), prefix};\
        acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
        printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); }
-
 static void bay_notify(acpi_handle handle, u32 event, void *data);
-static int acpi_bay_add(struct acpi_device *device);
-static int acpi_bay_remove(struct acpi_device *device, int type);
-static int acpi_bay_match(struct acpi_device *device,
-                               struct acpi_driver *driver);
-
-static struct acpi_driver acpi_bay_driver = {
-       .name = ACPI_BAY_DRIVER_NAME,
-       .class = ACPI_BAY_CLASS,
-       .ops = {
-               .add = acpi_bay_add,
-               .remove = acpi_bay_remove,
-               .match = acpi_bay_match,
-               },
-};
 
 struct bay {
        acpi_handle handle;
        char *name;
        struct list_head list;
+       struct platform_device *pdev;
 };
 
-LIST_HEAD(drive_bays);
+static LIST_HEAD(drive_bays);
 
 
 /*****************************************************************************
@@ -133,6 +118,33 @@ static void eject_device(acpi_handle handle)
                pr_debug("Failed to evaluate _EJ0!\n");
 }
 
+/*
+ * show_present - read method for "present" file in sysfs
+ */
+static ssize_t show_present(struct device *dev,
+                          struct device_attribute *attr, char *buf)
+{
+       struct bay *bay = dev_get_drvdata(dev);
+       return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
+
+}
+DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
+
+/*
+ * write_eject - write method for "eject" file in sysfs
+ */
+static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
+                          const char *buf, size_t count)
+{
+       struct bay *bay = dev_get_drvdata(dev);
+
+       if (!count)
+               return -EINVAL;
+
+       eject_device(bay->handle);
+       return count;
+}
+DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
 
 /**
  * is_ata - see if a device is an ata device
@@ -208,24 +220,33 @@ int eject_removable_drive(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(eject_removable_drive);
 
-static int acpi_bay_add(struct acpi_device *device)
+static int acpi_bay_add_fs(struct bay *bay)
 {
-       bay_dprintk(device->handle, "adding bay device");
-       strcpy(acpi_device_name(device), "Dockable Bay");
-       strcpy(acpi_device_class(device), "bay");
+       int ret;
+       struct device *dev = &bay->pdev->dev;
+
+       ret = device_create_file(dev, &dev_attr_present);
+       if (ret)
+               goto add_fs_err;
+       ret = device_create_file(dev, &dev_attr_eject);
+       if (ret) {
+               device_remove_file(dev, &dev_attr_present);
+               goto add_fs_err;
+       }
        return 0;
-}
 
-static int acpi_bay_add_fs(struct bay *bay)
-{
-       if (!bay)
-               return -EINVAL;
+ add_fs_err:
+       bay_dprintk(bay->handle, "Error adding sysfs files\n");
+       return ret;
 }
 
 static void acpi_bay_remove_fs(struct bay *bay)
 {
-       if (!bay)
-               return;
+       struct device *dev = &bay->pdev->dev;
+
+       /* cleanup sysfs */
+       device_remove_file(dev, &dev_attr_present);
+       device_remove_file(dev, &dev_attr_eject);
 }
 
 static int bay_is_dock_device(acpi_handle handle)
@@ -240,10 +261,11 @@ static int bay_is_dock_device(acpi_handle handle)
        return (is_dock_device(handle) || is_dock_device(parent));
 }
 
-static int bay_add(acpi_handle handle)
+static int bay_add(acpi_handle handle, int id)
 {
        acpi_status status;
        struct bay *new_bay;
+       struct platform_device *pdev;
        struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
        acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
 
@@ -252,12 +274,24 @@ static int bay_add(acpi_handle handle)
        /*
         * Initialize bay device structure
         */
-       new_bay = kmalloc(GFP_ATOMIC, sizeof(*new_bay));
+       new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
        INIT_LIST_HEAD(&new_bay->list);
        new_bay->handle = handle;
        new_bay->name = (char *)nbuffer.pointer;
-       list_add(&new_bay->list, &drive_bays);
-       acpi_bay_add_fs(new_bay);
+
+       /* initialize platform device stuff */
+       pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
+       if (IS_ERR(pdev)) {
+               printk(KERN_ERR PREFIX "Error registering bay device\n");
+               goto bay_add_err;
+       }
+       new_bay->pdev = pdev;
+       platform_set_drvdata(pdev, new_bay);
+
+       if (acpi_bay_add_fs(new_bay)) {
+               platform_device_unregister(new_bay->pdev);
+               goto bay_add_err;
+       }
 
        /* register for events on this device */
        status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
@@ -273,70 +307,16 @@ static int bay_add(acpi_handle handle)
                bay_dprintk(handle, "Is dependent on dock\n");
                register_hotplug_dock_device(handle, bay_notify, new_bay);
        }
+       list_add(&new_bay->list, &drive_bays);
        printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
        return 0;
-}
-
-static int acpi_bay_remove(struct acpi_device *device, int type)
-{
-       /*** FIXME: do something here */
-       return 0;
-}
-
-static int acpi_bay_match(struct acpi_device *device,
-                               struct acpi_driver *driver)
-{
-       if (!device || !driver)
-               return -EINVAL;
-
-       if (is_ejectable_bay(device->handle)) {
-               bay_dprintk(device->handle, "matching bay device");
-               return 0;
-       }
 
+bay_add_err:
+       kfree(new_bay->name);
+       kfree(new_bay);
        return -ENODEV;
 }
 
-/**
- * bay_create_acpi_device - add new devices to acpi
- * @handle - handle of the device to add
- *
- *  This function will create a new acpi_device for the given
- *  handle if one does not exist already.  This should cause
- *  acpi to scan for drivers for the given devices, and call
- *  matching driver's add routine.
- *
- *  Returns a pointer to the acpi_device corresponding to the handle.
- */
-static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
-{
-       struct acpi_device *device = NULL;
-       struct acpi_device *parent_device;
-       acpi_handle parent;
-       int ret;
-
-       bay_dprintk(handle, "Trying to get device");
-       if (acpi_bus_get_device(handle, &device)) {
-               /*
-                * no device created for this object,
-                * so we should create one.
-                */
-               bay_dprintk(handle, "No device for handle");
-               acpi_get_parent(handle, &parent);
-               if (acpi_bus_get_device(parent, &parent_device))
-                       parent_device = NULL;
-
-               ret = acpi_bus_add(&device, parent_device, handle,
-                       ACPI_BUS_TYPE_DEVICE);
-               if (ret) {
-                       pr_debug("error adding bus, %x\n",
-                               -ret);
-                       return NULL;
-               }
-       }
-       return device;
-}
-
 /**
  * bay_notify - act upon an acpi bay notification
  * @handle: the bay handle
@@ -346,38 +326,19 @@ static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
  */
 static void bay_notify(acpi_handle handle, u32 event, void *data)
 {
-       struct acpi_device *dev;
+       struct bay *bay_dev = (struct bay *)data;
+       struct device *dev = &bay_dev->pdev->dev;
 
        bay_dprintk(handle, "Bay event");
 
        switch(event) {
        case ACPI_NOTIFY_BUS_CHECK:
-               printk("Bus Check\n");
        case ACPI_NOTIFY_DEVICE_CHECK:
-               printk("Device Check\n");
-               dev = bay_create_acpi_device(handle);
-               if (dev)
-                       acpi_bus_generate_event(dev, event, 0);
-               else
-                       printk("No device for generating event\n");
-               /* wouldn't it be a good idea to just rescan SATA
-                * right here?
-                */
-               break;
        case ACPI_NOTIFY_EJECT_REQUEST:
-               printk("Eject request\n");
-               dev = bay_create_acpi_device(handle);
-               if (dev)
-                       acpi_bus_generate_event(dev, event, 0);
-               else
-                       printk("No device for generating eventn");
-
-               /* wouldn't it be a good idea to just call the
-                * eject_device here if we were a SATA device?
-                */
+               kobject_uevent(&dev->kobj, KOBJ_CHANGE);
                break;
        default:
-               printk("unknown event %d\n", event);
+               printk(KERN_ERR PREFIX "Bay: unknown event %d\n", event);
        }
 }
 
@@ -393,8 +354,8 @@ find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
         */
        if (is_ejectable_bay(handle)) {
                bay_dprintk(handle, "found ejectable bay");
-               bay_add(handle);
-               (*count)++;
+               if (!bay_add(handle, *count))
+                       (*count)++;
        }
        return AE_OK;
 }
@@ -409,10 +370,6 @@ static int __init bay_init(void)
        acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
                ACPI_UINT32_MAX, find_bay, &bays, NULL);
 
-       if (bays)
-               if ((acpi_bus_register_driver(&acpi_bay_driver) < 0))
-                       printk(KERN_ERR "Unable to register bay driver\n");
-
        if (!bays)
                return -ENODEV;
 
@@ -429,11 +386,10 @@ static void __exit bay_exit(void)
                acpi_bay_remove_fs(bay);
                acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
                        bay_notify);
+               platform_device_unregister(bay->pdev);
                kfree(bay->name);
                kfree(bay);
        }
-
-       acpi_bus_unregister_driver(&acpi_bay_driver);
 }
 
 postcore_initcall(bay_init);