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>
24 #include <linux/slab.h>
26 #include <linux/nospec.h>
28 #include "ptp_private.h"
30 static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
31 enum ptp_pin_function func, unsigned int chan)
33 struct ptp_clock_request rq;
36 memset(&rq, 0, sizeof(rq));
42 rq.type = PTP_CLK_REQ_EXTTS;
43 rq.extts.index = chan;
44 err = ops->enable(ops, &rq, 0);
47 rq.type = PTP_CLK_REQ_PEROUT;
48 rq.perout.index = chan;
49 err = ops->enable(ops, &rq, 0);
60 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
61 enum ptp_pin_function func, unsigned int chan)
63 struct ptp_clock_info *info = ptp->info;
64 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
67 /* Check to see if any other pin previously had this function. */
68 for (i = 0; i < info->n_pins; i++) {
69 if (info->pin_config[i].func == func &&
70 info->pin_config[i].chan == chan) {
71 pin1 = &info->pin_config[i];
78 /* Check the desired function and channel. */
83 if (chan >= info->n_ext_ts)
87 if (chan >= info->n_per_out)
98 if (info->verify(info, pin, func, chan)) {
99 pr_err("driver cannot use function %u on pin %u\n", func, chan);
103 /* Disable whatever function was previously assigned. */
105 ptp_disable_pinfunc(info, func, chan);
106 pin1->func = PTP_PF_NONE;
109 ptp_disable_pinfunc(info, pin2->func, pin2->chan);
116 int ptp_open(struct posix_clock *pc, fmode_t fmode)
121 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
123 struct ptp_clock_caps caps;
124 struct ptp_clock_request req;
125 struct ptp_sys_offset *sysoff = NULL;
126 struct ptp_pin_desc pd;
127 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
128 struct ptp_clock_info *ops = ptp->info;
129 struct ptp_clock_time *pct;
130 struct timespec64 ts;
132 unsigned int i, pin_index;
136 case PTP_CLOCK_GETCAPS:
137 memset(&caps, 0, sizeof(caps));
138 caps.max_adj = ptp->info->max_adj;
139 caps.n_alarm = ptp->info->n_alarm;
140 caps.n_ext_ts = ptp->info->n_ext_ts;
141 caps.n_per_out = ptp->info->n_per_out;
142 caps.pps = ptp->info->pps;
143 caps.n_pins = ptp->info->n_pins;
144 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
148 case PTP_EXTTS_REQUEST:
149 if (copy_from_user(&req.extts, (void __user *)arg,
150 sizeof(req.extts))) {
154 if (req.extts.index >= ops->n_ext_ts) {
158 req.type = PTP_CLK_REQ_EXTTS;
159 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
160 err = ops->enable(ops, &req, enable);
163 case PTP_PEROUT_REQUEST:
164 if (copy_from_user(&req.perout, (void __user *)arg,
165 sizeof(req.perout))) {
169 if (req.perout.index >= ops->n_per_out) {
173 req.type = PTP_CLK_REQ_PEROUT;
174 enable = req.perout.period.sec || req.perout.period.nsec;
175 err = ops->enable(ops, &req, enable);
179 if (!capable(CAP_SYS_TIME))
181 req.type = PTP_CLK_REQ_PPS;
182 enable = arg ? 1 : 0;
183 err = ops->enable(ops, &req, enable);
187 sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL);
192 if (copy_from_user(sysoff, (void __user *)arg,
197 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
201 pct = &sysoff->ts[0];
202 for (i = 0; i < sysoff->n_samples; i++) {
203 getnstimeofday64(&ts);
204 pct->sec = ts.tv_sec;
205 pct->nsec = ts.tv_nsec;
207 err = ptp->info->gettime64(ptp->info, &ts);
210 pct->sec = ts.tv_sec;
211 pct->nsec = ts.tv_nsec;
214 getnstimeofday64(&ts);
215 pct->sec = ts.tv_sec;
216 pct->nsec = ts.tv_nsec;
217 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
221 case PTP_PIN_GETFUNC:
222 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
226 pin_index = pd.index;
227 if (pin_index >= ops->n_pins) {
231 pin_index = array_index_nospec(pin_index, ops->n_pins);
232 if (mutex_lock_interruptible(&ptp->pincfg_mux))
234 pd = ops->pin_config[pin_index];
235 mutex_unlock(&ptp->pincfg_mux);
236 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
240 case PTP_PIN_SETFUNC:
241 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
245 pin_index = pd.index;
246 if (pin_index >= ops->n_pins) {
250 pin_index = array_index_nospec(pin_index, ops->n_pins);
251 if (mutex_lock_interruptible(&ptp->pincfg_mux))
253 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
254 mutex_unlock(&ptp->pincfg_mux);
267 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
269 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
271 poll_wait(fp, &ptp->tsev_wq, wait);
273 return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
276 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
278 ssize_t ptp_read(struct posix_clock *pc,
279 uint rdflags, char __user *buf, size_t cnt)
281 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
282 struct timestamp_event_queue *queue = &ptp->tsevq;
283 struct ptp_extts_event *event;
288 if (cnt % sizeof(struct ptp_extts_event) != 0)
291 if (cnt > EXTTS_BUFSIZE)
294 cnt = cnt / sizeof(struct ptp_extts_event);
296 if (mutex_lock_interruptible(&ptp->tsevq_mux))
299 if (wait_event_interruptible(ptp->tsev_wq,
300 ptp->defunct || queue_cnt(queue))) {
301 mutex_unlock(&ptp->tsevq_mux);
306 mutex_unlock(&ptp->tsevq_mux);
310 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
312 mutex_unlock(&ptp->tsevq_mux);
316 spin_lock_irqsave(&queue->lock, flags);
318 qcnt = queue_cnt(queue);
323 for (i = 0; i < cnt; i++) {
324 event[i] = queue->buf[queue->head];
325 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
328 spin_unlock_irqrestore(&queue->lock, flags);
330 cnt = cnt * sizeof(struct ptp_extts_event);
332 mutex_unlock(&ptp->tsevq_mux);
335 if (copy_to_user(buf, event, cnt))