Skip to contentSkip to conversation

rtc: add devm_rtc_device_{register,unregister}()

PATCH In review Jingoo Han 4 participants 4 messages Feb 26, 2013 – Mar 6, 2013 Real time clock (RTC)

lore
Share & tools
lore

Jingoo Han

author
#0 lore raw

PATCH v3 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.

---
Changes since v2:
- Removed unnecessary WARN_ON() of devm_rtc_device_match()

Changes since v1:
- Simplified 'if' statements using WARN_ON()
- Added a description of the return value of devm_rtc_device_register()

 drivers/rtc/class.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h |    6 ++++
 2 files changed, 76 insertions(+), 0 deletions(-)
drivers/rtc/class.c +70 -0
index 9b742d3..b72b40b 100644
@@ -259,6 +259,76 @@ 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;
+
+	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
+ *
+ * @return a struct rtc on success, or an ERR_PTR on error
+ *
+ * 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);
+	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
#2 lore raw
In reply to Jingoo Han
Quoted 46 lines

-----Original Message-----
From: [email protected] [mailto:linux-kernel- [email protected]] On Behalf Of Jingoo Han
Sent: Tuesday, February 26, 2013 7:20 AM
To: 'Andrew Morton'
Cc: [email protected]; 'Tejun Heo'; 'Greg KH'; 'Alessandro Zummo'; [email protected]; 'Jingoo Han'
Subject: [PATCH v3] 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.

---
Changes since v2:
- Removed unnecessary WARN_ON() of devm_rtc_device_match()

Changes since v1:
- Simplified 'if' statements using WARN_ON()
- Added a description of the return value of devm_rtc_device_register()

 drivers/rtc/class.c |   70
+++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h |    6 ++++
 2 files changed, 76 insertions(+), 0 deletions(-)
drivers/rtc/class.c +10 -0
index 9b742d3..b72b40b 100644
 
+/**
+ * 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
+ *
+ * @return a struct rtc on success, or an ERR_PTR on error
+ *
+ * Managed rtc_device_register(). The rtc_device returned from this

function
+ * are automatically freed on driver detach. See rtc_device_register()
+ * for more information.
+ */

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

Quoted 3 lines
drivers/rtc/class.c +3 -0
+
+struct rtc_device *devm_rtc_device_register(const char *name,
+					struct device *dev,

As most of devm_* functions use " struct device *dev" as their first param, why not this function also modified to be in sync with them?

Quoted 4 lines
drivers/rtc/class.c +4 -0
+					const struct rtc_class_ops *ops,
+					struct module *owner)
+{
+	struct rtc_device **ptr, *rtc;
#3 lore raw
Quoted 12 lines · Venu Byravarasu
Quoted 12 lines

+/**
+ * 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
+ *
+ * @return a struct rtc on success, or an ERR_PTR on error
+ *
+ * Managed rtc_device_register(). The rtc_device returned from this function

Quoted 8 lines

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

Quoted 6 lines

+ * are automatically freed on driver detach. See rtc_device_register()
+ * for more information.
+ */

drivers/rtc/class.c +3 -0
+
+struct rtc_device *devm_rtc_device_register(const char *name,
+					struct device *dev,

As most of devm_* functions use " struct device *dev" as their first param, why not this function also modified to be in sync with them?

I suspect it's because the signature matches rtc_device_register(). This way you can do a simple search and replace and avoid having to reorder arguments.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation