linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] IIO: Added write function in iio_buffer_fileops
@ 2014-08-13  6:29 a.mathur
  2014-08-13  9:08 ` Lars-Peter Clausen
  0 siblings, 1 reply; 13+ messages in thread
From: a.mathur @ 2014-08-13  6:29 UTC (permalink / raw)
  To: jic23, lars
  Cc: cpgs, linux-kernel, linux-iio, aniroop.mathur, p.shailesh,
	r.mahale, vidushi.koul, narendra.m1

From: Aniroop Mathur <a.mathur@samsung.com>

Earlier, user space can only read from iio device node but cannot write to it.
This patch adds write function in iio buffer file operations, 
which will allow user-space applications/HAL to write the data 
to iio device node.
So now there will be two way communication between IIO subsystem 
and user space. (userspace <--> kernel) 

It can be used by HAL or any user-space application which wants to
write data to iio device node/buffer upon receiving some data from it.
As an example, 
It is useful for iio device simulator application which need to record 
the data by reading from iio device node and replay the recorded data 
by writing back to iio device node.

Signed-off-by: Aniroop Mathur <a.mathur@samsung.com>
---
 drivers/iio/iio_core.h            |    5 ++++-
 drivers/iio/industrialio-buffer.c |   34 ++++++++++++++++++++++++++++++++++
 drivers/iio/industrialio-core.c   |    1 +
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 5f0ea77..ba3fe53 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -47,10 +47,12 @@ unsigned int iio_buffer_poll(struct file *filp,
 			     struct poll_table_struct *wait);
 ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
 				      size_t n, loff_t *f_ps);
-
+ssize_t iio_buffer_write_first_n_outer(struct file *filp,
+			     const char __user *buf, size_t n, loff_t *f_ps);
 
 #define iio_buffer_poll_addr (&iio_buffer_poll)
 #define iio_buffer_read_first_n_outer_addr (&iio_buffer_read_first_n_outer)
+#define iio_buffer_write_first_n_outer_addr (&iio_buffer_write_first_n_outer)
 
 void iio_disable_all_buffers(struct iio_dev *indio_dev);
 void iio_buffer_wakeup_poll(struct iio_dev *indio_dev);
@@ -59,6 +61,7 @@ void iio_buffer_wakeup_poll(struct iio_dev *indio_dev);
 
 #define iio_buffer_poll_addr NULL
 #define iio_buffer_read_first_n_outer_addr NULL
+#define iio_buffer_write_first_n_outer_addr NULL
 
 static inline void iio_disable_all_buffers(struct iio_dev *indio_dev) {}
 static inline void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) {}
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 9f1a140..ef889af 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -21,6 +21,7 @@
 #include <linux/slab.h>
 #include <linux/poll.h>
 #include <linux/sched.h>
+#include <asm/uaccess.h>
 
 #include <linux/iio/iio.h>
 #include "iio_core.h"
@@ -87,6 +88,39 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
 }
 
 /**
+ * iio_buffer_write_first_n_outer() - chrdev write to buffer
+ *
+ * This function pushes the user space data to kernel iio buffer
+ **/
+ssize_t iio_buffer_write_first_n_outer(struct file *filp,
+				const char __user *buf, size_t n, loff_t *f_ps)
+{
+	struct iio_dev *indio_dev = filp->private_data;
+	struct iio_buffer *rb = indio_dev->buffer;
+	int ret = -1;
+	unsigned char *data = NULL;
+
+	if (!indio_dev->info)
+		return -ENODEV;
+
+	if (n != 1)
+		return -EINVAL;
+
+	data = kzalloc(1, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	if (copy_from_user(data, buf, 1)) {
+		kfree(data);
+		return -EFAULT;
+	}
+
+	ret = iio_push_to_buffer(rb, data);
+	kfree(data);
+	return ret;
+}
+
+/**
  * iio_buffer_poll() - poll the buffer to find out if it has data
  */
 unsigned int iio_buffer_poll(struct file *filp,
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 4b1f375..a98dba9 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1114,6 +1114,7 @@ static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 
 static const struct file_operations iio_buffer_fileops = {
 	.read = iio_buffer_read_first_n_outer_addr,
+	.write = iio_buffer_write_first_n_outer_addr,
 	.release = iio_chrdev_release,
 	.open = iio_chrdev_open,
 	.poll = iio_buffer_poll_addr,
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-08-23 18:26 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-13  6:29 [PATCH 1/1] IIO: Added write function in iio_buffer_fileops a.mathur
2014-08-13  9:08 ` Lars-Peter Clausen
2014-08-13 13:42   ` Aniroop Mathur
2014-08-13 14:48     ` Lars-Peter Clausen
2014-08-13 16:33       ` Aniroop Mathur
2014-08-14  9:41         ` Lars-Peter Clausen
2014-08-14 14:38           ` Jonathan Cameron
2014-08-14 19:10             ` Aniroop Mathur
2014-08-16 14:44             ` Aniroop Mathur
2014-08-22 18:28               ` Jonathan Cameron
2014-08-23 18:26                 ` Aniroop Mathur
2014-08-18 15:29             ` Aniroop Mathur
2014-08-19 19:27               ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).