2 * PTP 1588 clock support - character device implementation.
4 * Copyright (C) 2010 OMICRON electronics GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/posix-clock.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
25 #include "ptp_private.h"
27 int ptp_open(struct posix_clock *pc, fmode_t fmode)
32 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
34 struct ptp_clock_caps caps;
35 struct ptp_clock_request req;
36 struct ptp_sys_offset sysoff;
37 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
38 struct ptp_clock_info *ops = ptp->info;
39 struct ptp_clock_time *pct;
46 case PTP_CLOCK_GETCAPS:
47 memset(&caps, 0, sizeof(caps));
48 caps.max_adj = ptp->info->max_adj;
49 caps.n_alarm = ptp->info->n_alarm;
50 caps.n_ext_ts = ptp->info->n_ext_ts;
51 caps.n_per_out = ptp->info->n_per_out;
52 caps.pps = ptp->info->pps;
53 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
57 case PTP_EXTTS_REQUEST:
58 if (copy_from_user(&req.extts, (void __user *)arg,
63 if (req.extts.index >= ops->n_ext_ts) {
67 req.type = PTP_CLK_REQ_EXTTS;
68 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
69 err = ops->enable(ops, &req, enable);
72 case PTP_PEROUT_REQUEST:
73 if (copy_from_user(&req.perout, (void __user *)arg,
74 sizeof(req.perout))) {
78 if (req.perout.index >= ops->n_per_out) {
82 req.type = PTP_CLK_REQ_PEROUT;
83 enable = req.perout.period.sec || req.perout.period.nsec;
84 err = ops->enable(ops, &req, enable);
88 if (!capable(CAP_SYS_TIME))
90 req.type = PTP_CLK_REQ_PPS;
92 err = ops->enable(ops, &req, enable);
96 if (copy_from_user(&sysoff, (void __user *)arg,
101 if (sysoff.n_samples > PTP_MAX_SAMPLES) {
106 for (i = 0; i < sysoff.n_samples; i++) {
108 pct->sec = ts.tv_sec;
109 pct->nsec = ts.tv_nsec;
111 ptp->info->gettime(ptp->info, &ts);
112 pct->sec = ts.tv_sec;
113 pct->nsec = ts.tv_nsec;
117 pct->sec = ts.tv_sec;
118 pct->nsec = ts.tv_nsec;
119 if (copy_to_user((void __user *)arg, &sysoff, sizeof(sysoff)))
130 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
132 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
134 poll_wait(fp, &ptp->tsev_wq, wait);
136 return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
139 ssize_t ptp_read(struct posix_clock *pc,
140 uint rdflags, char __user *buf, size_t cnt)
142 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
143 struct timestamp_event_queue *queue = &ptp->tsevq;
144 struct ptp_extts_event event[PTP_BUF_TIMESTAMPS];
148 if (cnt % sizeof(struct ptp_extts_event) != 0)
151 if (cnt > sizeof(event))
154 cnt = cnt / sizeof(struct ptp_extts_event);
156 if (mutex_lock_interruptible(&ptp->tsevq_mux))
159 if (wait_event_interruptible(ptp->tsev_wq,
160 ptp->defunct || queue_cnt(queue))) {
161 mutex_unlock(&ptp->tsevq_mux);
166 mutex_unlock(&ptp->tsevq_mux);
170 spin_lock_irqsave(&queue->lock, flags);
172 qcnt = queue_cnt(queue);
177 for (i = 0; i < cnt; i++) {
178 event[i] = queue->buf[queue->head];
179 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
182 spin_unlock_irqrestore(&queue->lock, flags);
184 cnt = cnt * sizeof(struct ptp_extts_event);
186 mutex_unlock(&ptp->tsevq_mux);
188 if (copy_to_user(buf, event, cnt))