Skip to contentSkip to conversation

rtc: add devm_rtc_device_{register,unregister}()

PATCH Superseded Jingoo Han 3 participants 5 messages Feb 25, 2013 Real time clock (RTC)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jingoo Han

author
#0 lore raw

PATCH 1/1 rtc: add devm_rtc_device_{register,unregister}()

These functios allows the driver core to automatically clean up any allocation made by rtc drivers. Thus, it simplifies the error paths.

---
 drivers/rtc/class.c |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h |    6 ++++
 2 files changed, 78 insertions(+), 0 deletions(-)
drivers/rtc/class.c +72 -0
index 9b742d3..bd3c8ca 100644
@@ -259,6 +259,78 @@ void rtc_device_unregister(struct rtc_device *rtc)
 }
 EXPORT_SYMBOL_GPL(rtc_device_unregister);
 
+static void devm_rtc_device_release(struct device *dev, void *res)
+{
+	struct rtc_device *rtc = *(struct rtc_device **)res;
+
+	rtc_device_unregister(rtc);
+}
+
+static int devm_rtc_device_match(struct device *dev, void *res, void *data)
+{
+	struct rtc **r = res;
+	if (!r || !*r) {
+		WARN_ON(!r || !*r);
+		return 0;
+	}
+	return *r == data;
+}
+
+/**
+ * devm_rtc_device_register - resource managed rtc_device_register()
+ * @name: the name of the device
+ * @dev: the device to register
+ * @ops: the rtc operations structure
+ * @owner: the module owner
+ *
+ * Managed rtc_device_register(). The rtc_device returned from this function
+ * are automatically freed on driver detach. See rtc_device_register()
+ * for more information.
+ */
+
+struct rtc_device *devm_rtc_device_register(const char *name,
+					struct device *dev,
+					const struct rtc_class_ops *ops,
+					struct module *owner)
+{
+	struct rtc_device **ptr, *rtc;
+
+	ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	rtc = rtc_device_register(name, dev, ops, owner);
+	if (!IS_ERR(rtc)) {
+		*ptr = rtc;
+		devres_add(dev, ptr);
+	} else {
+		devres_free(ptr);
+	}
+
+	return rtc;
+}
+EXPORT_SYMBOL_GPL(devm_rtc_device_register);
+
+/**
+ * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
+ * @dev: the device to unregister
+ * @rtc: the RTC class device to unregister
+ *
+ * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
+ * function will not need to be called and the resource management code will
+ * ensure that the resource is freed.
+ */
+void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
+{
+	int rc;
+
+	rc = devres_release(dev, devm_rtc_device_release,
+				devm_rtc_device_match, rtc);
+	if (rc != 0)
+		WARN_ON(rc);
+}
+EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
+
 static int __init rtc_init(void)
 {
 	rtc_class = class_create(THIS_MODULE, "rtc");
include/linux/rtc.h +6 -0
index 580b24c..d955768 100644
@@ -133,7 +133,13 @@ extern struct rtc_device *rtc_device_register(const char *name,
 					struct device *dev,
 					const struct rtc_class_ops *ops,
 					struct module *owner);
+extern struct rtc_device *devm_rtc_device_register(const char *name,
+					struct device *dev,
+					const struct rtc_class_ops *ops,
+					struct module *owner);
 extern void rtc_device_unregister(struct rtc_device *rtc);
+extern void devm_rtc_device_unregister(struct device *dev,
+					struct rtc_device *rtc);
 
 extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
 extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 14 lines · On Mon, 25 Feb 2013 16:35:53 +0900 Jingoo Han <[email protected]> wrote:

These functios allows the driver core to automatically clean up any allocation made by rtc drivers. Thus, it simplifies the error paths.

...

drivers/rtc/class.c +4 -0
@@ -259,6 +259,78 @@ void rtc_device_unregister(struct rtc_device *rtc)
 }
 EXPORT_SYMBOL_GPL(rtc_device_unregister);
 
+static void devm_rtc_device_release(struct device *dev, void *res)
+{
+	struct rtc_device *rtc = *(struct rtc_device **)res;
+

drivers/rtc/class.c void rtc_device_unregister(struct rtc_device *rtc) · line 275

Quoted 10 lines
drivers/rtc/class.c +10 -0
+	rtc_device_unregister(rtc);
+}
+
+static int devm_rtc_device_match(struct device *dev, void *res, void *data)
+{
+	struct rtc **r = res;
+	if (!r || !*r) {
+		WARN_ON(!r || !*r);
+		return 0;
+	}

Use

	if (WARN_ON(!r || !*r))
		return 0;

However, if anyone ever reports this warning you will be sad because you won't know which condition triggered it. So a more useful approach is

	if (WARN_ON(!r))
		return 0;
	if (WARN_ON(!*r))
		return 0;
Quoted 4 lines
drivers/rtc/class.c +4 -0
+	return *r == data;
+}
+
+/**

drivers/rtc/class.c void rtc_device_unregister(struct rtc_device *rtc) · line 289

Quoted 10 lines
drivers/rtc/class.c +10 -0
+ * devm_rtc_device_register - resource managed rtc_device_register()
+ * @name: the name of the device
+ * @dev: the device to register
+ * @ops: the rtc operations structure
+ * @owner: the module owner
+ *
+ * Managed rtc_device_register(). The rtc_device returned from this function
+ * are automatically freed on driver detach. See rtc_device_register()
+ * for more information.
+ */

I think the name is inappropriate. devm functions start with "devm" and rtc functions start with "rtc" and this code is a part of rtc core, not a part of devm core. Hence it should be something like "rtc_devm_device_register".

Quoted 12 lines
drivers/rtc/class.c +12 -0
+struct rtc_device *devm_rtc_device_register(const char *name,
+					struct device *dev,
+					const struct rtc_class_ops *ops,
+					struct module *owner)
+{
+	struct rtc_device **ptr, *rtc;
+
+	ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	rtc = rtc_device_register(name, dev, ops, owner);

drivers/rtc/class.c void rtc_device_unregister(struct rtc_device *rtc) · line 312

Quoted 10 lines
drivers/rtc/class.c +10 -0
+	if (!IS_ERR(rtc)) {
+		*ptr = rtc;
+		devres_add(dev, ptr);
+	} else {
+		devres_free(ptr);
+	}
+
+	return rtc;
+}
+EXPORT_SYMBOL_GPL(devm_rtc_device_register);

So this function will return an ERR_PTR on error. Please add a description of the return value into the kerneldoc.

Quoted 7 lines
drivers/rtc/class.c +7 -0
+/**
+ * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
+ * @dev: the device to unregister
+ * @rtc: the RTC class device to unregister
+ *
+ * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
+ * function will not need to be called and the resource management code will

drivers/rtc/class.c void rtc_device_unregister(struct rtc_device *rtc) · line 330

Quoted 10 lines
drivers/rtc/class.c +10 -0
+ * ensure that the resource is freed.
+ */
+void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
+{
+	int rc;
+
+	rc = devres_release(dev, devm_rtc_device_release,
+				devm_rtc_device_match, rtc);
+	if (rc != 0)
+		WARN_ON(rc);
	WARN_ON(!rc);
Quoted 6 lines
drivers/rtc/class.c +3 -0
+}
+EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
+
 
...
#2 lore raw
Quoted 9 lines · Andrew Morton
Quoted 4 lines

+ * Managed rtc_device_register(). The rtc_device returned from this function
+ * are automatically freed on driver detach. See rtc_device_register()
+ * for more information.
+ */

I think the name is inappropriate. devm functions start with "devm" and rtc functions start with "rtc" and this code is a part of rtc core, not a part of devm core. Hence it should be something like "rtc_devm_device_register".

I think devm_rtc_ is better. All other devm interface functions put devm_ prefix first and I think it's better to make devm_ more pronounced as it denotes a somewhat subtle difference in behavior.

Thanks.

Signature · 1 line
tejun
#3 lore raw
Quoted 6 lines · On Mon, 25 Feb 2013 15:33:45 -0800 "'Tejun Heo'" <[email protected]> wrote:
Quoted 5 lines · Andrew Morton
Quoted 4 lines

+ * Managed rtc_device_register(). The rtc_device returned from this function
+ * are automatically freed on driver detach. See rtc_device_register()
+ * for more information.
+ */

Quoted 7 lines
Quoted 4 lines

I think the name is inappropriate. devm functions start with "devm" and rtc functions start with "rtc" and this code is a part of rtc core, not a part of devm core. Hence it should be something like "rtc_devm_device_register".

I think devm_rtc_ is better. All other devm interface functions put devm_ prefix first

makes no sense. Whatever.

#4 lore raw
In reply to Jingoo Han
Quoted 4 lines · Jingoo Han
drivers/rtc/class.c +4 -0
+static void devm_rtc_device_release(struct device *dev, void *res)
+{
+	struct rtc_device *rtc = *(struct rtc_device **)res;
+

drivers/rtc/class.c void rtc_device_unregister(struct rtc_device *rtc) · line 275

Quoted 10 lines
drivers/rtc/class.c +10 -0
+	rtc_device_unregister(rtc);
+}
+
+static int devm_rtc_device_match(struct device *dev, void *res, void *data)
+{
+	struct rtc **r = res;
+	if (!r || !*r) {
+		WARN_ON(!r || !*r);
+		return 0;
+	}
	if (WARN_ON(!r || !*r))
		return 0;

But why do we need this at all? Other match functions don't need it. Is something different about devm_rtc?

Thanks.

Signature · 1 line
tejun