Commit 5966c837 authored by Dmitry Torokhov's avatar Dmitry Torokhov Committed by Greg Kroah-Hartman

[PATCH] Driver core: add platform_device_register_simple to register platform

Add platform_device_register_simple to register platform devices
requiring minimal resource and memory management.  The device
will have standard release function that just frees memory
occupied by the platform device. By having release function in
the driver core modules using such devices can be unloaded
without waiting for the last reference to the device to be
dropped.
Signed-off-by: default avatarDmitry Torokhov <dtor@mail.ru>
Signed-off-by: default avatarGreg Kroah-Hartman <greg@kroah.com>
parent 33778cca
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/dma-mapping.h> #include <linux/dma-mapping.h>
#include <linux/bootmem.h> #include <linux/bootmem.h>
#include <linux/err.h>
struct device platform_bus = { struct device platform_bus = {
.bus_id = "platform", .bus_id = "platform",
...@@ -133,6 +134,13 @@ int platform_device_register(struct platform_device * pdev) ...@@ -133,6 +134,13 @@ int platform_device_register(struct platform_device * pdev)
return ret; return ret;
} }
/**
* platform_device_unregister - remove a platform-level device
* @dev: platform device we're removing
*
* Note that this function will also release all memory- and port-based
* resources owned by the device (@dev->resource).
*/
void platform_device_unregister(struct platform_device * pdev) void platform_device_unregister(struct platform_device * pdev)
{ {
int i; int i;
...@@ -148,6 +156,65 @@ void platform_device_unregister(struct platform_device * pdev) ...@@ -148,6 +156,65 @@ void platform_device_unregister(struct platform_device * pdev)
} }
} }
struct platform_object {
struct platform_device pdev;
struct resource resources[0];
};
static void platform_device_release_simple(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
kfree(container_of(pdev, struct platform_object, pdev));
}
/**
* platform_device_register_simple
* @name: base name of the device we're adding
* @id: instance id
* @res: set of resources that needs to be allocated for the device
* @num: number of resources
*
* 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 dropped.
*/
struct platform_device *platform_device_register_simple(char *name, unsigned int id,
struct resource *res, unsigned int num)
{
struct platform_object *pobj;
int retval;
pobj = kmalloc(sizeof(struct platform_object) + sizeof(struct resource) * num, GFP_KERNEL);
if (!pobj) {
retval = -ENOMEM;
goto error;
}
memset(pobj, 0, sizeof(*pobj));
pobj->pdev.name = name;
pobj->pdev.id = id;
pobj->pdev.dev.release = platform_device_release_simple;
if (num) {
memcpy(pobj->resources, res, sizeof(struct resource) * num);
pobj->pdev.resource = pobj->resources;
pobj->pdev.num_resources = num;
}
retval = platform_device_register(&pobj->pdev);
if (retval)
goto error;
return &pobj->pdev;
error:
kfree(pobj);
return ERR_PTR(retval);
}
/** /**
* platform_match - bind platform device to platform driver. * platform_match - bind platform device to platform driver.
...@@ -237,6 +304,7 @@ EXPORT_SYMBOL(dma_get_required_mask); ...@@ -237,6 +304,7 @@ EXPORT_SYMBOL(dma_get_required_mask);
EXPORT_SYMBOL(platform_bus); EXPORT_SYMBOL(platform_bus);
EXPORT_SYMBOL(platform_bus_type); EXPORT_SYMBOL(platform_bus_type);
EXPORT_SYMBOL(platform_device_register); EXPORT_SYMBOL(platform_device_register);
EXPORT_SYMBOL(platform_device_register_simple);
EXPORT_SYMBOL(platform_device_unregister); EXPORT_SYMBOL(platform_device_unregister);
EXPORT_SYMBOL(platform_get_irq); EXPORT_SYMBOL(platform_get_irq);
EXPORT_SYMBOL(platform_get_resource); EXPORT_SYMBOL(platform_get_resource);
...@@ -381,6 +381,8 @@ extern struct resource *platform_get_resource(struct platform_device *, unsigned ...@@ -381,6 +381,8 @@ extern struct resource *platform_get_resource(struct platform_device *, unsigned
extern int platform_get_irq(struct platform_device *, unsigned int); extern int platform_get_irq(struct platform_device *, unsigned int);
extern int platform_add_devices(struct platform_device **, int); extern int platform_add_devices(struct platform_device **, int);
extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
/* drivers/base/power.c */ /* drivers/base/power.c */
extern void device_shutdown(void); extern void device_shutdown(void);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment