[PATCH] pps: Add support for read operations and return a useful value in poll

From: Christian Riesch
Date: Tue Mar 31 2015 - 17:43:45 EST


The PPS_FETCH ioctl in drivers/pps/pps.c blocks until a new PPS event
occurs, then returns the time stamp data. While this is fine for
lots of applications, sometimes it would be nice if the poll system
call and a subsequent read could be used to obtain the pps data.

This patch adds support for read operations. Furthermore pps_cdev_poll
is modified to return POLLIN | POLLRDNORM only when new PPS data is
available.

Signed-off-by: Christian Riesch <christian.riesch@xxxxxxxxxx>
---
drivers/pps/pps.c | 32 +++++++++++++++++++++++++++++++-
include/linux/pps_kernel.h | 1 +
2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
index 2f07cd6..f90dd2e 100644
--- a/drivers/pps/pps.c
+++ b/drivers/pps/pps.c
@@ -55,7 +55,7 @@ static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)

poll_wait(file, &pps->queue, wait);

- return POLLIN | POLLRDNORM;
+ return (pps->last_ev != pps->read_ev) ? POLLIN | POLLRDNORM : 0;
}

static int pps_cdev_fasync(int fd, struct file *file, int on)
@@ -191,6 +191,7 @@ static long pps_cdev_ioctl(struct file *file,
fdata.info.assert_tu = pps->assert_tu;
fdata.info.clear_tu = pps->clear_tu;
fdata.info.current_mode = pps->current_mode;
+ pps->read_ev = pps->last_ev;

spin_unlock_irq(&pps->lock);

@@ -251,6 +252,34 @@ static int pps_cdev_open(struct inode *inode, struct file *file)
return 0;
}

+ssize_t pps_cdev_read(struct file *file, char __user *buf, size_t count,
+ loff_t *ppos)
+{
+ struct pps_device *pps = file->private_data;
+ struct pps_fdata fdata;
+
+ if (count != sizeof(struct pps_fdata))
+ return -EINVAL;
+
+ if (wait_event_interruptible(pps->queue, pps->read_ev != pps->last_ev))
+ return -ERESTARTSYS;
+
+ /* Return the fetched timestamp */
+ spin_lock_irq(&pps->lock);
+ fdata.info.assert_sequence = pps->assert_sequence;
+ fdata.info.clear_sequence = pps->clear_sequence;
+ fdata.info.assert_tu = pps->assert_tu;
+ fdata.info.clear_tu = pps->clear_tu;
+ fdata.info.current_mode = pps->current_mode;
+ pps->read_ev = pps->last_ev;
+ spin_unlock_irq(&pps->lock);
+
+ if (copy_to_user(buf, &fdata, sizeof(struct pps_fdata)))
+ return -EFAULT;
+
+ return sizeof(struct pps_fdata);
+}
+
static int pps_cdev_release(struct inode *inode, struct file *file)
{
struct pps_device *pps = container_of(inode->i_cdev,
@@ -266,6 +295,7 @@ static int pps_cdev_release(struct inode *inode, struct file *file)
static const struct file_operations pps_cdev_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
+ .read = pps_cdev_read,
.poll = pps_cdev_poll,
.fasync = pps_cdev_fasync,
.unlocked_ioctl = pps_cdev_ioctl,
diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
index 1d2cd21..42f405b 100644
--- a/include/linux/pps_kernel.h
+++ b/include/linux/pps_kernel.h
@@ -66,6 +66,7 @@ struct pps_device {
int current_mode; /* PPS mode at event time */

unsigned int last_ev; /* last PPS event id */
+ unsigned int read_ev; /* last PPS event read by user */
wait_queue_head_t queue; /* PPS event queue */

unsigned int id; /* PPS source unique ID */
--
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/