kobject: get rid of kobject_add_dir
[safe/jmp/linux-2.6] / drivers / base / platform.c
index 6d4736e..fb56092 100644 (file)
 
 #include "base.h"
 
+#define to_platform_driver(drv)        (container_of((drv), struct platform_driver, driver))
+
 struct device platform_bus = {
        .bus_id         = "platform",
 };
+EXPORT_SYMBOL_GPL(platform_bus);
 
 /**
  *     platform_get_resource - get a resource for a device
@@ -47,6 +50,7 @@ platform_get_resource(struct platform_device *dev, unsigned int type,
        }
        return NULL;
 }
+EXPORT_SYMBOL_GPL(platform_get_resource);
 
 /**
  *     platform_get_irq - get an IRQ for a device
@@ -57,8 +61,9 @@ int platform_get_irq(struct platform_device *dev, unsigned int num)
 {
        struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
 
-       return r ? r->start : 0;
+       return r ? r->start : -ENXIO;
 }
+EXPORT_SYMBOL_GPL(platform_get_irq);
 
 /**
  *     platform_get_resource_byname - get a resource for a device by name
@@ -82,6 +87,7 @@ platform_get_resource_byname(struct platform_device *dev, unsigned int type,
        }
        return NULL;
 }
+EXPORT_SYMBOL_GPL(platform_get_resource_byname);
 
 /**
  *     platform_get_irq - get an IRQ for a device
@@ -92,8 +98,9 @@ int platform_get_irq_byname(struct platform_device *dev, char *name)
 {
        struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
 
-       return r ? r->start : 0;
+       return r ? r->start : -ENXIO;
 }
+EXPORT_SYMBOL_GPL(platform_get_irq_byname);
 
 /**
  *     platform_add_devices - add a numbers of platform devices
@@ -115,6 +122,7 @@ int platform_add_devices(struct platform_device **devs, int num)
 
        return ret;
 }
+EXPORT_SYMBOL_GPL(platform_add_devices);
 
 struct platform_object {
        struct platform_device pdev;
@@ -153,7 +161,7 @@ static void platform_device_release(struct device *dev)
  *     Create a platform device object which can have other objects attached
  *     to it, and which will have attached objects freed when it is released.
  */
-struct platform_device *platform_device_alloc(const char *name, unsigned int id)
+struct platform_device *platform_device_alloc(const char *name, int id)
 {
        struct platform_object *pa;
 
@@ -166,7 +174,7 @@ struct platform_device *platform_device_alloc(const char *name, unsigned int id)
                pa->pdev.dev.release = platform_device_release;
        }
 
-       return pa ? &pa->pdev : NULL;   
+       return pa ? &pa->pdev : NULL;
 }
 EXPORT_SYMBOL_GPL(platform_device_alloc);
 
@@ -204,7 +212,7 @@ EXPORT_SYMBOL_GPL(platform_device_add_resources);
  *     pointer.  The memory associated with the platform data will be freed
  *     when the platform device is released.
  */
-int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
+int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size)
 {
        void *d;
 
@@ -237,7 +245,8 @@ int platform_device_add(struct platform_device *pdev)
        pdev->dev.bus = &platform_bus_type;
 
        if (pdev->id != -1)
-               snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
+               snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name,
+                        pdev->id);
        else
                strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
 
@@ -255,7 +264,7 @@ int platform_device_add(struct platform_device *pdev)
                                p = &ioport_resource;
                }
 
-               if (p && request_resource(p, r)) {
+               if (p && insert_resource(p, r)) {
                        printk(KERN_ERR
                               "%s: failed to claim resource %d\n",
                               pdev->dev.bus_id, i);
@@ -267,7 +276,7 @@ int platform_device_add(struct platform_device *pdev)
        pr_debug("Registering platform device '%s'. Parent at %s\n",
                 pdev->dev.bus_id, pdev->dev.parent->bus_id);
 
-       ret = device_register(&pdev->dev);
+       ret = device_add(&pdev->dev);
        if (ret == 0)
                return ret;
 
@@ -280,6 +289,31 @@ int platform_device_add(struct platform_device *pdev)
 EXPORT_SYMBOL_GPL(platform_device_add);
 
 /**
+ *     platform_device_del - remove a platform-level device
+ *     @pdev:  platform device we're removing
+ *
+ *     Note that this function will also release all memory- and port-based
+ *     resources owned by the device (@dev->resource).  This function
+ *     must _only_ be externally called in error cases.  All other usage
+ *     is a bug.
+ */
+void platform_device_del(struct platform_device *pdev)
+{
+       int i;
+
+       if (pdev) {
+               device_del(&pdev->dev);
+
+               for (i = 0; i < pdev->num_resources; i++) {
+                       struct resource *r = &pdev->resource[i];
+                       if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
+                               release_resource(r);
+               }
+       }
+}
+EXPORT_SYMBOL_GPL(platform_device_del);
+
+/**
  *     platform_device_register - add a platform-level device
  *     @pdev:  platform device we're adding
  *
@@ -289,28 +323,22 @@ int platform_device_register(struct platform_device * pdev)
        device_initialize(&pdev->dev);
        return platform_device_add(pdev);
 }
+EXPORT_SYMBOL_GPL(platform_device_register);
 
 /**
- *     platform_device_unregister - remove a platform-level device
- *     @pdev:  platform device we're removing
+ *     platform_device_unregister - unregister a platform-level device
+ *     @pdev:  platform device we're unregistering
  *
- *     Note that this function will also release all memory- and port-based
- *     resources owned by the device (@dev->resource).
+ *     Unregistration is done in 2 steps. First we release all resources
+ *     and remove it from the subsystem, then we drop reference count by
+ *     calling platform_device_put().
  */
 void platform_device_unregister(struct platform_device * pdev)
 {
-       int i;
-
-       if (pdev) {
-               for (i = 0; i < pdev->num_resources; i++) {
-                       struct resource *r = &pdev->resource[i];
-                       if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
-                               release_resource(r);
-               }
-
-               device_unregister(&pdev->dev);
-       }
+       platform_device_del(pdev);
+       platform_device_put(pdev);
 }
+EXPORT_SYMBOL_GPL(platform_device_unregister);
 
 /**
  *     platform_device_register_simple
@@ -322,10 +350,17 @@ void platform_device_unregister(struct platform_device * pdev)
  *     This function creates a simple platform device that requires minimal
  *     resource and memory management. Canned release function freeing
  *     memory allocated for the device allows drivers using such devices
- *     to be unloaded iwithout waiting for the last reference to the device
+ *     to be unloaded without waiting for the last reference to the device
  *     to be dropped.
+ *
+ *     This interface is primarily intended for use with legacy drivers
+ *     which probe hardware directly.  Because such drivers create sysfs
+ *     device nodes themselves, rather than letting system infrastructure
+ *     handle such device enumeration tasks, they don't fully conform to
+ *     the Linux driver model.  In particular, when such drivers are built
+ *     as modules, they can't be "hotplugged".
  */
-struct platform_device *platform_device_register_simple(char *name, unsigned int id,
+struct platform_device *platform_device_register_simple(char *name, int id,
                                                        struct resource *res, unsigned int num)
 {
        struct platform_device *pdev;
@@ -353,6 +388,155 @@ error:
        platform_device_put(pdev);
        return ERR_PTR(retval);
 }
+EXPORT_SYMBOL_GPL(platform_device_register_simple);
+
+static int platform_drv_probe(struct device *_dev)
+{
+       struct platform_driver *drv = to_platform_driver(_dev->driver);
+       struct platform_device *dev = to_platform_device(_dev);
+
+       return drv->probe(dev);
+}
+
+static int platform_drv_probe_fail(struct device *_dev)
+{
+       return -ENXIO;
+}
+
+static int platform_drv_remove(struct device *_dev)
+{
+       struct platform_driver *drv = to_platform_driver(_dev->driver);
+       struct platform_device *dev = to_platform_device(_dev);
+
+       return drv->remove(dev);
+}
+
+static void platform_drv_shutdown(struct device *_dev)
+{
+       struct platform_driver *drv = to_platform_driver(_dev->driver);
+       struct platform_device *dev = to_platform_device(_dev);
+
+       drv->shutdown(dev);
+}
+
+static int platform_drv_suspend(struct device *_dev, pm_message_t state)
+{
+       struct platform_driver *drv = to_platform_driver(_dev->driver);
+       struct platform_device *dev = to_platform_device(_dev);
+
+       return drv->suspend(dev, state);
+}
+
+static int platform_drv_resume(struct device *_dev)
+{
+       struct platform_driver *drv = to_platform_driver(_dev->driver);
+       struct platform_device *dev = to_platform_device(_dev);
+
+       return drv->resume(dev);
+}
+
+/**
+ *     platform_driver_register
+ *     @drv: platform driver structure
+ */
+int platform_driver_register(struct platform_driver *drv)
+{
+       drv->driver.bus = &platform_bus_type;
+       if (drv->probe)
+               drv->driver.probe = platform_drv_probe;
+       if (drv->remove)
+               drv->driver.remove = platform_drv_remove;
+       if (drv->shutdown)
+               drv->driver.shutdown = platform_drv_shutdown;
+       if (drv->suspend)
+               drv->driver.suspend = platform_drv_suspend;
+       if (drv->resume)
+               drv->driver.resume = platform_drv_resume;
+       return driver_register(&drv->driver);
+}
+EXPORT_SYMBOL_GPL(platform_driver_register);
+
+/**
+ *     platform_driver_unregister
+ *     @drv: platform driver structure
+ */
+void platform_driver_unregister(struct platform_driver *drv)
+{
+       driver_unregister(&drv->driver);
+}
+EXPORT_SYMBOL_GPL(platform_driver_unregister);
+
+/**
+ * platform_driver_probe - register driver for non-hotpluggable device
+ * @drv: platform driver structure
+ * @probe: the driver probe routine, probably from an __init section
+ *
+ * Use this instead of platform_driver_register() when you know the device
+ * is not hotpluggable and has already been registered, and you want to
+ * remove its run-once probe() infrastructure from memory after the driver
+ * has bound to the device.
+ *
+ * One typical use for this would be with drivers for controllers integrated
+ * into system-on-chip processors, where the controller devices have been
+ * configured as part of board setup.
+ *
+ * Returns zero if the driver registered and bound to a device, else returns
+ * a negative error code and with the driver not registered.
+ */
+int __init_or_module platform_driver_probe(struct platform_driver *drv,
+               int (*probe)(struct platform_device *))
+{
+       int retval, code;
+
+       /* temporary section violation during probe() */
+       drv->probe = probe;
+       retval = code = platform_driver_register(drv);
+
+       /* Fixup that section violation, being paranoid about code scanning
+        * the list of drivers in order to probe new devices.  Check to see
+        * if the probe was successful, and make sure any forced probes of
+        * new devices fail.
+        */
+       spin_lock(&platform_bus_type.klist_drivers.k_lock);
+       drv->probe = NULL;
+       if (code == 0 && list_empty(&drv->driver.klist_devices.k_list))
+               retval = -ENODEV;
+       drv->driver.probe = platform_drv_probe_fail;
+       spin_unlock(&platform_bus_type.klist_drivers.k_lock);
+
+       if (code != retval)
+               platform_driver_unregister(drv);
+       return retval;
+}
+EXPORT_SYMBOL_GPL(platform_driver_probe);
+
+/* modalias support enables more hands-off userspace setup:
+ * (a) environment variable lets new-style hotplug events work once system is
+ *     fully running:  "modprobe $MODALIAS"
+ * (b) sysfs attribute lets new-style coldplug recover from hotplug events
+ *     mishandled before system is fully running:  "modprobe $(cat modalias)"
+ */
+static ssize_t
+modalias_show(struct device *dev, struct device_attribute *a, char *buf)
+{
+       struct platform_device  *pdev = to_platform_device(dev);
+       int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
+
+       return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
+}
+
+static struct device_attribute platform_dev_attrs[] = {
+       __ATTR_RO(modalias),
+       __ATTR_NULL,
+};
+
+static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
+{
+       struct platform_device  *pdev = to_platform_device(dev);
+
+       add_uevent_var(env, "MODALIAS=platform:%s", pdev->name);
+       return 0;
+}
 
 
 /**
@@ -377,12 +561,36 @@ static int platform_match(struct device * dev, struct device_driver * drv)
        return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
 }
 
-static int platform_suspend(struct device * dev, pm_message_t state)
+static int platform_suspend(struct device *dev, pm_message_t mesg)
 {
        int ret = 0;
 
        if (dev->driver && dev->driver->suspend)
-               ret = dev->driver->suspend(dev, state);
+               ret = dev->driver->suspend(dev, mesg);
+
+       return ret;
+}
+
+static int platform_suspend_late(struct device *dev, pm_message_t mesg)
+{
+       struct platform_driver *drv = to_platform_driver(dev->driver);
+       struct platform_device *pdev = container_of(dev, struct platform_device, dev);
+       int ret = 0;
+
+       if (dev->driver && drv->suspend_late)
+               ret = drv->suspend_late(pdev, mesg);
+
+       return ret;
+}
+
+static int platform_resume_early(struct device *dev)
+{
+       struct platform_driver *drv = to_platform_driver(dev->driver);
+       struct platform_device *pdev = container_of(dev, struct platform_device, dev);
+       int ret = 0;
+
+       if (dev->driver && drv->resume_early)
+               ret = drv->resume_early(pdev);
 
        return ret;
 }
@@ -399,15 +607,27 @@ static int platform_resume(struct device * dev)
 
 struct bus_type platform_bus_type = {
        .name           = "platform",
+       .dev_attrs      = platform_dev_attrs,
        .match          = platform_match,
+       .uevent         = platform_uevent,
        .suspend        = platform_suspend,
+       .suspend_late   = platform_suspend_late,
+       .resume_early   = platform_resume_early,
        .resume         = platform_resume,
 };
+EXPORT_SYMBOL_GPL(platform_bus_type);
 
 int __init platform_bus_init(void)
 {
-       device_register(&platform_bus);
-       return bus_register(&platform_bus_type);
+       int error;
+
+       error = device_register(&platform_bus);
+       if (error)
+               return error;
+       error =  bus_register(&platform_bus_type);
+       if (error)
+               device_unregister(&platform_bus);
+       return error;
 }
 
 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
@@ -431,14 +651,3 @@ u64 dma_get_required_mask(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(dma_get_required_mask);
 #endif
-
-EXPORT_SYMBOL_GPL(platform_bus);
-EXPORT_SYMBOL_GPL(platform_bus_type);
-EXPORT_SYMBOL_GPL(platform_add_devices);
-EXPORT_SYMBOL_GPL(platform_device_register);
-EXPORT_SYMBOL_GPL(platform_device_register_simple);
-EXPORT_SYMBOL_GPL(platform_device_unregister);
-EXPORT_SYMBOL_GPL(platform_get_irq);
-EXPORT_SYMBOL_GPL(platform_get_resource);
-EXPORT_SYMBOL_GPL(platform_get_irq_byname);
-EXPORT_SYMBOL_GPL(platform_get_resource_byname);