From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753222AbbCaVnl (ORCPT ); Tue, 31 Mar 2015 17:43:41 -0400 Received: from smtprelay01.ispgateway.de ([80.67.31.35]:40513 "EHLO smtprelay01.ispgateway.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751797AbbCaVnj (ORCPT ); Tue, 31 Mar 2015 17:43:39 -0400 X-Greylist: delayed 654 seconds by postgrey-1.27 at vger.kernel.org; Tue, 31 Mar 2015 17:43:38 EDT From: Christian Riesch To: Rodolfo Giometti Cc: linux-kernel@vger.kernel.org, Christian Riesch Subject: [PATCH] pps: Add support for read operations and return a useful value in poll Date: Tue, 31 Mar 2015 23:31:22 +0200 Message-Id: <1427837482-2841-1-git-send-email-christian.riesch@omicron.at> X-Mailer: git-send-email 1.7.9.5 X-Df-Sender: Y2hyaXN0aWFuQHJpZXNjaC5hdA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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