[LinuxPPS] [PATCH 1/3] pps: add kernel consumer support

Alexander Gordeev lasaine at lvk.cs.msu.su
Thu Oct 1 22:37:01 CEST 2009


Add an optional feature of PPSAPI, kernel consumer support, with a stub
hardpps function.

Signed-off-by: Alexander Gordeev <lasaine at lvk.cs.msu.su>
---
 Documentation/ioctl/ioctl-number.txt |    2 +-
 Documentation/pps/timepps.h          |   10 ++++--
 drivers/pps/kapi.c                   |   37 +++++++++++++++++++++
 drivers/pps/pps.c                    |   60 +++++++++++++++++++++++++++++++++-
 include/linux/pps.h                  |   13 +++++++
 5 files changed, 117 insertions(+), 5 deletions(-)

diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt
index df7aeca..eda1e93 100644
--- a/Documentation/ioctl/ioctl-number.txt
+++ b/Documentation/ioctl/ioctl-number.txt
@@ -151,7 +151,7 @@ Code	Seq#	Include File		Comments
 'p'	40-7F	linux/nvram.h
 'p'	80-9F				user-space parport
 					<mailto:tim at cyberelk.net>
-'p'	a1-a4	linux/pps.h		LinuxPPS
+'p'	a1-a5	linux/pps.h		LinuxPPS
 					<mailto:giometti at linux.it>
 'q'	00-1F	linux/serio.h
 'q'	80-FF				Internet PhoneJACK, Internet LineJACK
diff --git a/Documentation/pps/timepps.h b/Documentation/pps/timepps.h
index d2628d2..84e5544 100644
--- a/Documentation/pps/timepps.h
+++ b/Documentation/pps/timepps.h
@@ -190,9 +190,13 @@ static __inline int time_pps_kcbind(pps_handle_t handle,
 					const int kernel_consumer,
 					const int edge, const int tsformat)
 {
-	/* LinuxPPS doesn't implement kernel consumer feature */
-	errno = EOPNOTSUPP;
-	return -1;
+	struct pps_bind_args __bind_args;
+
+	__bind_args.tsformat = tsformat;
+	__bind_args.edge = edge;
+	__bind_args.consumer = kernel_consumer;
+
+	return ioctl(handle, PPS_KC_BIND, &__bind_args);
 }
 
 #endif				/* _SYS_TIMEPPS_H_ */
diff --git a/drivers/pps/kapi.c b/drivers/pps/kapi.c
index e0b75ed..edb5315 100644
--- a/drivers/pps/kapi.c
+++ b/drivers/pps/kapi.c
@@ -25,6 +25,7 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/time.h>
+#include <linux/timex.h>
 #include <linux/spinlock.h>
 #include <linux/idr.h>
 #include <linux/fs.h>
@@ -37,6 +38,12 @@
 DEFINE_SPINLOCK(pps_idr_lock);
 DEFINE_IDR(pps_idr);
 
+/* state variables to bind kernel consumer */
+/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */
+DEFINE_SPINLOCK(pps_kc_hardpps_lock);
+void	*pps_kc_hardpps_dev = NULL;	/* some unique pointer to device */
+int	pps_kc_hardpps_mode = 0;	/* mode bits for kernel consumer */
+
 /*
  * Local functions
  */
@@ -253,6 +260,30 @@ void pps_unregister_source(int source)
 }
 EXPORT_SYMBOL(pps_unregister_source);
 
+/*
+ * hardpps() - discipline CPU clock oscillator to external PPS signal
+ *
+ * This routine is called at each PPS interrupt in order to discipline
+ * the CPU clock oscillator to the PPS signal. There are two independent
+ * first-order feedback loops, one for the phase, the other for the
+ * frequency. The phase loop measures and grooms the PPS phase offset
+ * and leaves it in a handy spot for the seconds overflow routine. The
+ * frequency loop averages successive PPS phase differences and
+ * calculates the PPS frequency offset, which is also processed by the
+ * seconds overflow routine. The code requires the caller to capture the
+ * time and architecture-dependent hardware counter values in
+ * nanoseconds at the on-time PPS signal transition.
+ *
+ * Note that, on some Unix systems this routine runs at an interrupt
+ * priority level higher than the timer interrupt routine second_overflow().
+ * Therefore, the variables used are distinct from the second_overflow()
+ * variables, except for the actual time and frequency variables, which
+ * are determined by this routine and updated atomically.
+ */
+void hardpps(const struct timespec *p_ts, long nsec)
+{
+}
+
 /* pps_event - register a PPS event into the system
  * @source: the PPS source ID
  * @ts: the event timestamp
@@ -315,6 +346,12 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 		pr_debug("capture clear seq #%u for source %d\n",
 			pps->clear_sequence, source);
 	}
+	spin_lock(&pps_kc_hardpps_lock);
+	if (pps == pps_kc_hardpps_dev && event & pps_kc_hardpps_mode) {
+		struct timespec p_ts = { .tv_sec = ts->sec, .tv_nsec = ts->nsec };
+		hardpps(&p_ts, 0);
+	}
+	spin_unlock(&pps_kc_hardpps_lock);
 
 	pps->go = ~0;
 	wake_up_interruptible(&pps->queue);
diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
index 23ea070..4404763 100644
--- a/drivers/pps/pps.c
+++ b/drivers/pps/pps.c
@@ -62,6 +62,8 @@ static long pps_cdev_ioctl(struct file *file,
 	struct pps_device *pps = file->private_data;
 	struct pps_kparams params;
 	struct pps_fdata fdata;
+	struct pps_bind_args bind_args;
+	unsigned long irq_flags;
 	unsigned long ticks;
 	void __user *uarg = (void __user *) arg;
 	int __user *iuarg = (int __user *) arg;
@@ -181,9 +183,65 @@ static long pps_cdev_ioctl(struct file *file,
 
 		break;
 
+	case PPS_KC_BIND:
+		pr_info("PPS_KC_BIND: source %d\n", pps->id);
+
+		/* Check the capabilities */
+		if (!capable(CAP_SYS_TIME))
+			return -EPERM;
+
+		if (copy_from_user(&bind_args, uarg, sizeof(struct pps_bind_args)))
+			return -EFAULT;
+
+		/* Check for supported capabilities */
+		if ((bind_args.edge & ~pps->info.mode) != 0) {
+			pr_err("PPS_KC_BIND: "
+					"unsupported capabilities (%x)\n",
+					bind_args.edge);
+			return -EINVAL;
+		}
+
+		/* Validate parameters roughly */
+		if (bind_args.tsformat != PPS_TSFMT_TSPEC ||
+				(bind_args.edge & ~PPS_CAPTUREBOTH) != 0 ||
+				bind_args.consumer != PPS_KC_HARDPPS) {
+			pr_err("PPS_KC_BIND: "
+					"invalid kcbind parameters (%x)\n",
+					bind_args.edge);
+			return -EINVAL;
+		}
+
+		/* Check if another consumer is already bound */
+		spin_lock_irqsave(&pps_kc_hardpps_lock, irq_flags);
+
+		if (bind_args.edge == 0)
+			if (pps_kc_hardpps_dev == pps) {
+				pps_kc_hardpps_mode = 0;
+				pps_kc_hardpps_dev = NULL;
+				spin_unlock_irqrestore(&pps_kc_hardpps_lock, irq_flags);
+				pr_info("unbound kernel consumer\n");
+			} else {
+				spin_unlock_irqrestore(&pps_kc_hardpps_lock, irq_flags);
+				pr_err("selected kernel consumer is not bound\n");
+				return -EINVAL;
+			}
+		else
+			if (pps_kc_hardpps_dev == NULL ||
+					pps_kc_hardpps_dev == pps) {
+				pps_kc_hardpps_mode = bind_args.edge;
+				pps_kc_hardpps_dev = pps;
+				spin_unlock_irqrestore(&pps_kc_hardpps_lock, irq_flags);
+				pr_info("bound kernel consumer: dev=%p, "
+					"edge=0x%x\n", pps, bind_args.edge);
+			} else {
+				spin_unlock_irqrestore(&pps_kc_hardpps_lock, irq_flags);
+				pr_err("another kernel consumer is already bound\n");
+				return -EINVAL;
+			}
+		break;
+
 	default:
 		return -ENOTTY;
-		break;
 	}
 
 	return 0;
diff --git a/include/linux/pps.h b/include/linux/pps.h
index 8a92204..558d679 100644
--- a/include/linux/pps.h
+++ b/include/linux/pps.h
@@ -113,12 +113,19 @@ struct pps_fdata {
 	struct pps_ktime timeout;
 };
 
+struct pps_bind_args {
+	int tsformat;	/* format of time stamps */
+	int edge;	/* selected event type */
+	int consumer;	/* selected kernel consumer */
+};
+
 #include <linux/ioctl.h>
 
 #define PPS_GETPARAMS		_IOR('p', 0xa1, struct pps_kparams *)
 #define PPS_SETPARAMS		_IOW('p', 0xa2, struct pps_kparams *)
 #define PPS_GETCAP		_IOR('p', 0xa3, int *)
 #define PPS_FETCH		_IOWR('p', 0xa4, struct pps_fdata *)
+#define PPS_KC_BIND		_IOW('p', 0xa5, struct pps_bind_args *)
 
 #ifdef __KERNEL__
 
@@ -177,6 +184,12 @@ extern struct timespec pps_irq_ts[];
 
 extern struct device_attribute pps_attrs[];
 
+/* state variables to bind kernel consumer */
+/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */
+extern spinlock_t pps_kc_hardpps_lock;
+extern void *pps_kc_hardpps_dev;	/* some unique pointer to device */
+extern int pps_kc_hardpps_mode;		/* mode bits for kernel consumer */
+
 /*
  * Exported functions
  */
-- 
1.6.3.3




More information about the LinuxPPS mailing list