public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dtor_core@ameritech.net>
To: Vojtech Pavlik <vojtech@suse.cz>
Cc: Andrew Morton <akpm@osdl.org>, linux-kernel@vger.kernel.org
Subject: [PATCH 11/19] add platform_device_register_simple
Date: Mon, 28 Jun 2004 00:19:00 -0500	[thread overview]
Message-ID: <200406280019.04030.dtor_core@ameritech.net> (raw)
In-Reply-To: <200406280017.56654.dtor_core@ameritech.net>


===================================================================


ChangeSet@1.1785, 2004-06-27 15:57:46-05:00, dtor_core@ameritech.net
  sysfs: 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.
  
         Suggested by Russell King
  
  Signed-off-by: Dmitry Torokhov <dtor@mail.ru>


 drivers/base/platform.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/device.h  |    2 +
 2 files changed, 70 insertions(+)


===================================================================



diff -Nru a/drivers/base/platform.c b/drivers/base/platform.c
--- a/drivers/base/platform.c	2004-06-27 17:50:23 -05:00
+++ b/drivers/base/platform.c	2004-06-27 17:50:23 -05:00
@@ -13,6 +13,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/init.h>
+#include <linux/err.h>
 
 struct device platform_bus = {
 	.bus_id		= "platform",
@@ -131,6 +132,13 @@
 	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)
 {
 	int i;
@@ -146,6 +154,65 @@
 	}
 }
 
+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.
@@ -213,6 +280,7 @@
 EXPORT_SYMBOL(platform_bus);
 EXPORT_SYMBOL(platform_bus_type);
 EXPORT_SYMBOL(platform_device_register);
+EXPORT_SYMBOL(platform_device_register_simple);
 EXPORT_SYMBOL(platform_device_unregister);
 EXPORT_SYMBOL(platform_get_irq);
 EXPORT_SYMBOL(platform_get_resource);
diff -Nru a/include/linux/device.h b/include/linux/device.h
--- a/include/linux/device.h	2004-06-27 17:50:23 -05:00
+++ b/include/linux/device.h	2004-06-27 17:50:23 -05:00
@@ -381,6 +381,8 @@
 extern int platform_get_irq(struct platform_device *, unsigned 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 */
 extern void device_shutdown(void);
 

  reply	other threads:[~2004-06-28  5:31 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-28  5:08 [PATCH 0/19] New set of input patches Dmitry Torokhov
2004-06-28  5:09 ` [PATCH 1/19] psmouse state locking Dmitry Torokhov
2004-06-28  5:10   ` [PATCH 2/19] serio connect/disconnect mandatory Dmitry Torokhov
2004-06-28  5:11     ` [PATCH 3/19] serio renames 1 Dmitry Torokhov
2004-06-28  5:12       ` [PATCH 4/19] serio renames 2 Dmitry Torokhov
2004-06-28  5:13         ` [PATCH 5/19] serio dynamic allocation Dmitry Torokhov
2004-06-28  5:14           ` [PATCH 6/19] serio avoid recursion Dmitry Torokhov
2004-06-28  5:15             ` [PATCH 7/19] serio sysfs intergration Dmitry Torokhov
2004-06-28  5:16               ` [PATCH 8/19] serio rebind Dmitry Torokhov
2004-06-28  5:17                 ` PATCH 9/19] serio manual bind Dmitry Torokhov
2004-06-28  5:17                   ` [PATCH 10/19] serio_raw driver Dmitry Torokhov
2004-06-28  5:19                     ` Dmitry Torokhov [this message]
2004-06-28  5:19                       ` [PATCH 12/19] convert i8042 into a platform device Dmitry Torokhov
2004-06-28  5:21                         ` [PATCH 13/19] more platform device conversions Dmitry Torokhov
2004-06-28  5:22                           ` [PATCH 14/19] bind serio ports and their parents Dmitry Torokhov
2004-06-28  5:23                             ` [PATCH 15/19] synaptics passthrough handling Dmitry Torokhov
2004-06-28  5:24                               ` [PATCH 16/19] add bus' default driver attributes Dmitry Torokhov
2004-06-28  5:25                                 ` [PATCH 17/19] serio use bus' default driver/device attributes Dmitry Torokhov
2004-06-28  5:26                                   ` [PATCH 18/19] add driver_find Dmitry Torokhov
2004-06-28  5:27                                     ` [PATCH 19/19] serio use driver_find Dmitry Torokhov
2004-06-28  6:52 ` [PATCH 0/19] New set of input patches Vojtech Pavlik
2004-06-28  7:13   ` Dmitry Torokhov
2004-06-28  7:51     ` Vojtech Pavlik
2004-06-28 10:32   ` Neil Brown
2004-06-28 12:13     ` Vojtech Pavlik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200406280019.04030.dtor_core@ameritech.net \
    --to=dtor_core@ameritech.net \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=vojtech@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox