[PATCH 40/46] staging: comedi: comedi_fc: introduce cfc_check_trigger_src

H Hartley Sweeten hartleys at visionengravers.com
Tue Sep 11 02:05:18 UTC 2012


All of the comedi drivers that support the 'cmd' callback also
require a 'do_cmdtest' callback. The do_cmdtest validates the
comedi_cmd before it is executed.

"Step 1" of each do_cmdtest does a trivial validation of the
trigger sources by doing something like this for each trigger:

	int err = 0;
	unsigned int tmp;

	tmp = cmd->start_src;
	src &= TRIG_NOW;
	if (!src || tmp != src)
		err++;
	/* Test the remaining triggers similarly */

	if (err)
		return 1;

Introduce a helper function in comedi_fc to handle this boilerplate.
The drivers can then just do:

	err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
	/* Test the remaining trigger similarly */

	if (err)
		return 1;

Signed-off-by: H Hartley Sweeten <hsweeten at visionengravers.com>
Cc: Ian Abbott <abbotti at mev.co.uk>
Cc: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
---
 drivers/staging/comedi/drivers/comedi_fc.c | 30 +++++++++++++++++++++++++-----
 drivers/staging/comedi/drivers/comedi_fc.h |  2 ++
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/comedi/drivers/comedi_fc.c b/drivers/staging/comedi/drivers/comedi_fc.c
index 63be619..a2397b3 100644
--- a/drivers/staging/comedi/drivers/comedi_fc.c
+++ b/drivers/staging/comedi/drivers/comedi_fc.c
@@ -102,18 +102,38 @@ unsigned int cfc_handle_events(struct comedi_device *dev,
 }
 EXPORT_SYMBOL(cfc_handle_events);
 
-MODULE_AUTHOR("Frank Mori Hess <fmhess at users.sourceforge.net>");
-MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
-MODULE_LICENSE("GPL");
+/**
+ * cfc_check_trigger_src() - trivially validate a comedi_cmd trigger source
+ * @src: pointer to the trigger source to validate
+ * @flags: bitmask of valid TRIG_* for the trigger
+ *
+ * This is used in "step 1" of the do_cmdtest functions of comedi drivers
+ * to vaildate the comedi_cmd triggers. The mask of the @src against the
+ * @flags allows the userspace comedilib to pass all the comedi_cmd
+ * triggers as TRIG_ANY and get back the actual valid trigger sources.
+ */
+int cfc_check_trigger_src(unsigned int *src, unsigned int flags)
+{
+	unsigned int orig_src = *src;
+
+	*src = orig_src & flags;
+	if (*src == TRIG_INVALID || *src != orig_src)
+		return -EINVAL;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(cfc_check_trigger_src);
 
 static int __init comedi_fc_init_module(void)
 {
 	return 0;
 }
+module_init(comedi_fc_init_module);
 
 static void __exit comedi_fc_cleanup_module(void)
 {
 }
-
-module_init(comedi_fc_init_module);
 module_exit(comedi_fc_cleanup_module);
+
+MODULE_AUTHOR("Frank Mori Hess <fmhess at users.sourceforge.net>");
+MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
+MODULE_LICENSE("GPL");
diff --git a/drivers/staging/comedi/drivers/comedi_fc.h b/drivers/staging/comedi/drivers/comedi_fc.h
index 4b2cfd3..0ff9cee 100644
--- a/drivers/staging/comedi/drivers/comedi_fc.h
+++ b/drivers/staging/comedi/drivers/comedi_fc.h
@@ -73,4 +73,6 @@ static inline unsigned int cfc_bytes_per_scan(struct comedi_subdevice *subd)
 	return num_samples * bytes_per_sample(subd);
 }
 
+int cfc_check_trigger_src(unsigned int *src, unsigned int flags);
+
 #endif /* _COMEDI_FC_H */
-- 
1.7.11




More information about the devel mailing list