Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Mar 2023 07:25:06 GMT
From:      Emmanuel Vadot <manu@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: aaf6129c9dcd - main - linuxkpi: Add devm_add_action and devm_add_action_or_reset
Message-ID:  <202303280725.32S7P60m036533@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=aaf6129c9dcde5eb020f585901a6f32d850d8cf6

commit aaf6129c9dcde5eb020f585901a6f32d850d8cf6
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2023-03-17 11:22:06 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2023-03-28 07:19:05 +0000

    linuxkpi: Add devm_add_action and devm_add_action_or_reset
    
    Those adds a new devres and will exectute some function on release.
    
    Reviewed by:    bz
    Differential Revision:  https://reviews.freebsd.org/D39142
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
---
 sys/compat/linuxkpi/common/include/linux/device.h |  7 ++++
 sys/compat/linuxkpi/common/src/linux_devres.c     | 43 +++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/device.h b/sys/compat/linuxkpi/common/include/linux/device.h
index 4b665e8db8d1..df6f10bd573b 100644
--- a/sys/compat/linuxkpi/common/include/linux/device.h
+++ b/sys/compat/linuxkpi/common/include/linux/device.h
@@ -680,4 +680,11 @@ devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
 #define	devm_kcalloc(_dev, _sizen, _size, _gfp)			\
     devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)
 
+int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data);
+#define	devm_add_action(dev, action, data)	\
+	lkpi_devm_add_action(dev, action, data);
+int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data);
+#define	devm_add_action_or_reset(dev, action, data)	\
+	lkpi_devm_add_action_or_reset(dev, action, data)
+
 #endif	/* _LINUXKPI_LINUX_DEVICE_H_ */
diff --git a/sys/compat/linuxkpi/common/src/linux_devres.c b/sys/compat/linuxkpi/common/src/linux_devres.c
index 96ff3e486d1d..0072713af322 100644
--- a/sys/compat/linuxkpi/common/src/linux_devres.c
+++ b/sys/compat/linuxkpi/common/src/linux_devres.c
@@ -224,3 +224,46 @@ lkpi_devm_kmalloc_release(struct device *dev __unused, void *p __unused)
 
 	/* Nothing to do.  Freed with the devres. */
 }
+
+struct devres_action {
+	void *data;
+	void (*action)(void *);
+};
+
+static void
+lkpi_devm_action_release(struct device *dev, void *res)
+{
+	struct devres_action	*devres;
+
+	devres = (struct devres_action *)res;
+	devres->action(devres->data);
+}
+
+int
+lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data)
+{
+	struct devres_action *devres;
+
+	KASSERT(action != NULL, ("%s: action is NULL\n", __func__));
+	devres = lkpi_devres_alloc(lkpi_devm_action_release,
+		sizeof(struct devres_action), GFP_KERNEL);
+	if (devres == NULL)
+		return (-ENOMEM);
+	devres->data = data;
+	devres->action = action;
+	devres_add(dev, devres);
+
+	return (0);
+}
+
+int
+lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data)
+{
+	int rv;
+
+	rv = lkpi_devm_add_action(dev, action, data);
+	if (rv != 0)
+		action(data);
+
+	return (rv);
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202303280725.32S7P60m036533>