Skip to contentSkip to conversation

platform: add new device registration helper

PATCH In review Dmitry Baryshkov 2 participants 5 messages Sep 4, 2008 – Sep 5, 2008 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform: add new device registration helper

Add a helper that registers simple platform_device w/o resources but with parent and device data.

This is usefull to cleanup platform code from code that registers such simple devices as leds-gpio, generic-bl, etc.

---
 drivers/base/platform.c         |   47 +++++++++++++++++++++++++++++++++++++++
 include/linux/platform_device.h |    2 +
 2 files changed, 49 insertions(+), 0 deletions(-)
drivers/base/platform.c +47 -0
index 3f94039..fcd9f97 100644
@@ -391,6 +391,53 @@ error:
 }
 EXPORT_SYMBOL_GPL(platform_device_register_simple);
 
+/**
+ * platform_device_register_data
+ * @parent: parent device for the device we're adding
+ * @name: base name of the device we're adding
+ * @id: instance id
+ * @data: platform specific data for this platform device
+ * @size: size of platform specific data
+ *
+ * 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 without waiting for the last reference to the device to be
+ * dropped.
+ */
+struct platform_device *platform_device_register_data(
+		struct device *parent,
+		const char *name, int id,
+		const void *data, size_t size)
+{
+	struct platform_device *pdev;
+	int retval;
+
+	pdev = platform_device_alloc(name, id);
+	if (!pdev) {
+		retval = -ENOMEM;
+		goto error;
+	}
+
+	pdev->dev.parent = parent;
+
+	if (size) {
+		retval = platform_device_add_data(pdev, data, size);
+		if (retval)
+			goto error;
+	}
+
+	retval = platform_device_add(pdev);
+	if (retval)
+		goto error;
+
+	return pdev;
+
+error:
+	platform_device_put(pdev);
+	return ERR_PTR(retval);
+}
+
 static int platform_drv_probe(struct device *_dev)
 {
 	struct platform_driver *drv = to_platform_driver(_dev->driver);
include/linux/platform_device.h +2 -0
index 95ac21a..4b8cc6a 100644
@@ -37,6 +37,8 @@ extern int platform_add_devices(struct platform_device **, int);
 
 extern struct platform_device *platform_device_register_simple(const char *, int id,
 					struct resource *, unsigned int);
+extern struct platform_device *platform_device_register_data(struct device *,
+		const char *, int, const void *, size_t);
 
 extern struct platform_device *platform_device_alloc(const char *name, int id);
 extern int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num);
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 7 lines · On Fri, 5 Sep 2008 01:30:16 +0400 Dmitry Baryshkov <[email protected]>…

Add a helper that registers simple platform_device w/o resources but with parent and device data.

This is usefull to cleanup platform code from code that registers such simple devices as leds-gpio, generic-bl, etc.

nits:

Quoted 14 lines
 include/linux/platform_device.h |    2 +
 2 files changed, 49 insertions(+), 0 deletions(-)
drivers/base/platform.c +3 -0
index 3f94039..fcd9f97 100644
@@ -391,6 +391,53 @@ error:
 }
 EXPORT_SYMBOL_GPL(platform_device_register_simple);
 
+/**
+ * platform_device_register_data
+ * @parent: parent device for the device we're adding

drivers/base/platform.c error: · line 406

Quoted 10 lines
drivers/base/platform.c +10 -0
+ * @name: base name of the device we're adding
+ * @id: instance id
+ * @data: platform specific data for this platform device
+ * @size: size of platform specific data
+ *
+ * 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 without waiting for the last reference to the device to be
+ * dropped.

Should document the return value (tested with IS_ERR)

drivers/base/platform.c error: · line 411

Quoted 5 lines
drivers/base/platform.c +5 -0
+ */
+struct platform_device *platform_device_register_data(
+		struct device *parent,
+		const char *name, int id,
+		const void *data, size_t size)

Something like

struct platform_device *platform_device_register_data(struct device *parent,
const char *name, int id, const void *data, size_t size)

would be more typical.

Quoted 16 lines
drivers/base/platform.c +16 -0
+{
+	struct platform_device *pdev;
+	int retval;
+
+	pdev = platform_device_alloc(name, id);
+	if (!pdev) {
+		retval = -ENOMEM;
+		goto error;
+	}
+
+	pdev->dev.parent = parent;
+
+	if (size) {
+		retval = platform_device_add_data(pdev, data, size);
+		if (retval)
+			goto error;

drivers/base/platform.c error: · line 437

Quoted 10 lines
drivers/base/platform.c +10 -0
+	}
+
+	retval = platform_device_add(pdev);
+	if (retval)
+		goto error;
+
+	return pdev;
+
+error:
+	platform_device_put(pdev);

Are you sure this can't trigger a !kobj->state_initialized warning in kobject_put()?

drivers/base/platform.c error:

Quoted 8 lines
drivers/base/platform.c +5 -0
+	return ERR_PTR(retval);
+}
+
 
...
 
+extern struct platform_device *platform_device_register_data(struct device *,
+		const char *, int, const void *, size_t);

It's nice (IMO) to include the names of the args, for documentation purposes. Obviously the surrounding code didn't agree.

#2 lore raw
Quoted 13 lines · Andrew Morton
Quoted 7 lines · On Fri, 5 Sep 2008 01:30:16 +0400 Dmitry Baryshkov <[email protected]>…

Add a helper that registers simple platform_device w/o resources but with parent and device data.

This is usefull to cleanup platform code from code that registers such simple devices as leds-gpio, generic-bl, etc.

nits:

Quoted 8 lines
Quoted 6 lines

+ *
+ * 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 without waiting for the last reference to the device to be
+ * dropped.

Should document the return value (tested with IS_ERR)

Will the wording "Returns a valid platform device, or a valid IS_ERR condition containing errno" be appropriate?

Quoted 21 lines
Quoted 21 lines
drivers/base/platform.c +21 -0
+{
+	struct platform_device *pdev;
+	int retval;
+
+	pdev = platform_device_alloc(name, id);
+	if (!pdev) {
+		retval = -ENOMEM;
+		goto error;
+	}
+
+	pdev->dev.parent = parent;
+
+	if (size) {
+		retval = platform_device_add_data(pdev, data, size);
+		if (retval)
+			goto error;
+	}
+
+	retval = platform_device_add(pdev);
+	if (retval)
+		goto error;
Quoted 8 lines

drivers/base/platform.c error: · line 437

Quoted 5 lines
drivers/base/platform.c +5 -0
+
+	return pdev;
+
+error:
+	platform_device_put(pdev);

Are you sure this can't trigger a !kobj->state_initialized warning in kobject_put()?

No. The kobject is initialised in platform_device_alloc() -> device_initialise()

Quoted 8 lines

include/linux/platform_device.h extern int platform_add_devices(struct platform_device **, int); · line 41

Quoted 5 lines

...

include/linux/platform_device.h +2 -0
+extern struct platform_device *platform_device_register_data(struct device *,
+		const char *, int, const void *, size_t);

It's nice (IMO) to include the names of the args, for documentation purposes. Obviously the surrounding code didn't agree.

I just copied the surrounding code style. If you wish, I can put the names for this function back.

BTW: would you prefer the followup "fix" patch or just the replacement one?

Signature · 2 lines
With best wishes
Dmitry
#3 lore raw
Quoted 21 lines · On Fri, 5 Sep 2008 14:33:18 +0400 Dmitry Baryshkov <[email protected]> wrote:
Quoted 20 lines · Andrew Morton
Quoted 7 lines · On Fri, 5 Sep 2008 01:30:16 +0400 Dmitry Baryshkov <[email protected]>…

Add a helper that registers simple platform_device w/o resources but with parent and device data.

This is usefull to cleanup platform code from code that registers such simple devices as leds-gpio, generic-bl, etc.

nits:

Quoted 6 lines

+ *
+ * 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 without waiting for the last reference to the device to be
+ * dropped.

Quoted 4 lines
Quoted 1 line

Should document the return value (tested with IS_ERR)

Will the wording "Returns a valid platform device, or a valid IS_ERR condition containing errno" be appropriate?

Sounds good.

Quoted 4 lines

BTW: would you prefer the followup "fix" patch or just the replacement one?

Doesn't worry me at this stage, but Greg would probably like a whole new patch, as he probably didn't merge version 1.

#4 lore raw
In reply to Dmitry Baryshkov

also,

Quoted 6 lines · On Fri, 5 Sep 2008 01:30:16 +0400 Dmitry Baryshkov <[email protected]>…

} EXPORT_SYMBOL_GPL(platform_device_register_simple);

...

+struct platform_device *platform_device_register_data(

When you start implementing callers, please check whether platform_device_register_data() should have been exported to modules.