Skip to contentSkip to conversation

watchdog: Add a device managed API for watchdog_register_device()

PATCH In review Neil Armstrong 3 participants 3 messages May 27, 2016 – Jul 17, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 watchdog: Add a device managed API for watchdog_register_device()

This helps in reducing code in .remove callbacks and sometimes dropping .remove callbacks entirely.

Changes since v1 at http://lkml.kernel.org/r/[email protected] :
- Fix brackets in devm_watchdog_register_device()

---
 Documentation/driver-model/devres.txt |  3 +++
 drivers/watchdog/watchdog_core.c      | 37 +++++++++++++++++++++++++++++++++++
 include/linux/watchdog.h              |  3 +++
 3 files changed, 43 insertions(+)
Documentation/driver-model/devres.txt +3 -0
index c63eea0..589296b 100644
@@ -357,3 +357,6 @@ SLAVE DMA ENGINE
 
 SPI
   devm_spi_register_master()
+
+WATCHDOG
+  devm_watchdog_register_device()
drivers/watchdog/watchdog_core.c +37 -0
index 981a668..7c5e3dc 100644
@@ -329,6 +329,43 @@ void watchdog_unregister_device(struct watchdog_device *wdd)
 
 EXPORT_SYMBOL_GPL(watchdog_unregister_device);
 
+static void devm_watchdog_unregister_device(struct device *dev, void *res)
+{
+	watchdog_unregister_device(*(struct watchdog_device **)res);
+}
+
+/**
+ * devm_watchdog_register_device() - resource managed watchdog_register_device()
+ * @dev: device that is registering this watchdog device
+ * @wdd: watchdog device
+ *
+ * Managed watchdog_register_device(). For watchdog device registered by this
+ * function,  watchdog_unregister_device() is automatically called on driver
+ * detach. See watchdog_register_device() for more information.
+ */
+int devm_watchdog_register_device(struct device *dev,
+				struct watchdog_device *wdd)
+{
+	struct watchdog_device **rcwdd;
+	int ret;
+
+	rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*wdd),
+			     GFP_KERNEL);
+	if (!rcwdd)
+		return -ENOMEM;
+
+	ret = watchdog_register_device(wdd);
+	if (!ret) {
+		*rcwdd = wdd;
+		devres_add(dev, rcwdd);
+	} else {
+		devres_free(rcwdd);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
+
 static int __init watchdog_deferred_registration(void)
 {
 	mutex_lock(&wtd_deferred_reg_mutex);
include/linux/watchdog.h +3 -0
index 51732d6c..6b75e38 100644
@@ -180,4 +180,7 @@ extern int watchdog_init_timeout(struct watchdog_device *wdd,
 extern int watchdog_register_device(struct watchdog_device *);
 extern void watchdog_unregister_device(struct watchdog_device *);
 
+/* devres register variant */
+int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
+
 #endif  /* ifndef _LINUX_WATCHDOG_H */
Signature · 1 line
2.7.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Neil Armstrong

This helps in reducing code in .remove callbacks and sometimes dropping .remove callbacks entirely.

Changes since v1 at http://lkml.kernel.org/r/[email protected] :
- Fix brackets in devm_watchdog_register_device()

Hi Neil,

For future patches: Change log after '---', please.

Quoted 1 line

Thanks,
Guenter

Quoted 78 lines
---
  Documentation/driver-model/devres.txt |  3 +++
  drivers/watchdog/watchdog_core.c      | 37 +++++++++++++++++++++++++++++++++++
  include/linux/watchdog.h              |  3 +++
  3 files changed, 43 insertions(+)
Documentation/driver-model/devres.txt +3 -0
index c63eea0..589296b 100644
@@ -357,3 +357,6 @@ SLAVE DMA ENGINE
 
  SPI
    devm_spi_register_master()
+
+WATCHDOG
+  devm_watchdog_register_device()
drivers/watchdog/watchdog_core.c +37 -0
index 981a668..7c5e3dc 100644
@@ -329,6 +329,43 @@ void watchdog_unregister_device(struct watchdog_device *wdd)
 
  EXPORT_SYMBOL_GPL(watchdog_unregister_device);
 
+static void devm_watchdog_unregister_device(struct device *dev, void *res)
+{
+	watchdog_unregister_device(*(struct watchdog_device **)res);
+}
+
+/**
+ * devm_watchdog_register_device() - resource managed watchdog_register_device()
+ * @dev: device that is registering this watchdog device
+ * @wdd: watchdog device
+ *
+ * Managed watchdog_register_device(). For watchdog device registered by this
+ * function,  watchdog_unregister_device() is automatically called on driver
+ * detach. See watchdog_register_device() for more information.
+ */
+int devm_watchdog_register_device(struct device *dev,
+				struct watchdog_device *wdd)
+{
+	struct watchdog_device **rcwdd;
+	int ret;
+
+	rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*wdd),
+			     GFP_KERNEL);
+	if (!rcwdd)
+		return -ENOMEM;
+
+	ret = watchdog_register_device(wdd);
+	if (!ret) {
+		*rcwdd = wdd;
+		devres_add(dev, rcwdd);
+	} else {
+		devres_free(rcwdd);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
+
  static int __init watchdog_deferred_registration(void)
  {
  	mutex_lock(&wtd_deferred_reg_mutex);
include/linux/watchdog.h +3 -0
index 51732d6c..6b75e38 100644
@@ -180,4 +180,7 @@ extern int watchdog_init_timeout(struct watchdog_device *wdd,
  extern int watchdog_register_device(struct watchdog_device *);
  extern void watchdog_unregister_device(struct watchdog_device *);
 
+/* devres register variant */
+int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
+
  #endif  /* ifndef _LINUX_WATCHDOG_H */
#2 lore raw
In reply to Neil Armstrong

Hi Neil,

Quoted 87 lines

This helps in reducing code in .remove callbacks and sometimes dropping .remove callbacks entirely.

Changes since v1 at http://lkml.kernel.org/r/[email protected] :
- Fix brackets in devm_watchdog_register_device()

---
 Documentation/driver-model/devres.txt |  3 +++
 drivers/watchdog/watchdog_core.c      | 37 +++++++++++++++++++++++++++++++++++
 include/linux/watchdog.h              |  3 +++
 3 files changed, 43 insertions(+)
Documentation/driver-model/devres.txt +3 -0
index c63eea0..589296b 100644
@@ -357,3 +357,6 @@ SLAVE DMA ENGINE
 
 SPI
   devm_spi_register_master()
+
+WATCHDOG
+  devm_watchdog_register_device()
drivers/watchdog/watchdog_core.c +37 -0
index 981a668..7c5e3dc 100644
@@ -329,6 +329,43 @@ void watchdog_unregister_device(struct watchdog_device *wdd)
 
 EXPORT_SYMBOL_GPL(watchdog_unregister_device);
 
+static void devm_watchdog_unregister_device(struct device *dev, void *res)
+{
+	watchdog_unregister_device(*(struct watchdog_device **)res);
+}
+
+/**
+ * devm_watchdog_register_device() - resource managed watchdog_register_device()
+ * @dev: device that is registering this watchdog device
+ * @wdd: watchdog device
+ *
+ * Managed watchdog_register_device(). For watchdog device registered by this
+ * function,  watchdog_unregister_device() is automatically called on driver
+ * detach. See watchdog_register_device() for more information.
+ */
+int devm_watchdog_register_device(struct device *dev,
+				struct watchdog_device *wdd)
+{
+	struct watchdog_device **rcwdd;
+	int ret;
+
+	rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*wdd),
+			     GFP_KERNEL);
+	if (!rcwdd)
+		return -ENOMEM;
+
+	ret = watchdog_register_device(wdd);
+	if (!ret) {
+		*rcwdd = wdd;
+		devres_add(dev, rcwdd);
+	} else {
+		devres_free(rcwdd);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
+
 static int __init watchdog_deferred_registration(void)
 {
 	mutex_lock(&wtd_deferred_reg_mutex);
include/linux/watchdog.h +3 -0
index 51732d6c..6b75e38 100644
@@ -180,4 +180,7 @@ extern int watchdog_init_timeout(struct watchdog_device *wdd,
 extern int watchdog_register_device(struct watchdog_device *);
 extern void watchdog_unregister_device(struct watchdog_device *);
 
+/* devres register variant */
+int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
+
 #endif  /* ifndef _LINUX_WATCHDOG_H */
Signature · 1 line
2.7.0

This patch has been added to linux-watchdog-next.

Kind regards,
Wim.