blob: 58a97d4205723fff7e25f454480169b6f658b0a4 [file] [log] [blame]
Richard Cochrand94ba802011-04-22 12:03:08 +02001/*
2 * PTP 1588 clock support - character device implementation.
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
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.
10 *
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.
15 *
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.
19 */
20#include <linux/module.h>
21#include <linux/posix-clock.h>
22#include <linux/poll.h>
23#include <linux/sched.h>
Richard Cochranc7ec0ba2012-11-26 01:44:34 +000024#include <linux/slab.h>
Christopher S. Hall719f1aa2016-02-22 03:15:25 -080025#include <linux/timekeeping.h>
Richard Cochrand94ba802011-04-22 12:03:08 +020026
27#include "ptp_private.h"
28
Richard Cochran60923152014-03-20 22:21:52 +010029static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
30 enum ptp_pin_function func, unsigned int chan)
31{
32 struct ptp_clock_request rq;
33 int err = 0;
34
35 memset(&rq, 0, sizeof(rq));
36
37 switch (func) {
38 case PTP_PF_NONE:
39 break;
40 case PTP_PF_EXTTS:
41 rq.type = PTP_CLK_REQ_EXTTS;
42 rq.extts.index = chan;
43 err = ops->enable(ops, &rq, 0);
44 break;
45 case PTP_PF_PEROUT:
46 rq.type = PTP_CLK_REQ_PEROUT;
47 rq.perout.index = chan;
48 err = ops->enable(ops, &rq, 0);
49 break;
50 case PTP_PF_PHYSYNC:
51 break;
52 default:
53 return -EINVAL;
54 }
55
56 return err;
57}
58
59int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
60 enum ptp_pin_function func, unsigned int chan)
61{
62 struct ptp_clock_info *info = ptp->info;
63 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
64 unsigned int i;
65
66 /* Check to see if any other pin previously had this function. */
67 for (i = 0; i < info->n_pins; i++) {
68 if (info->pin_config[i].func == func &&
69 info->pin_config[i].chan == chan) {
70 pin1 = &info->pin_config[i];
71 break;
72 }
73 }
74 if (pin1 && i == pin)
75 return 0;
76
77 /* Check the desired function and channel. */
78 switch (func) {
79 case PTP_PF_NONE:
80 break;
81 case PTP_PF_EXTTS:
82 if (chan >= info->n_ext_ts)
83 return -EINVAL;
84 break;
85 case PTP_PF_PEROUT:
86 if (chan >= info->n_per_out)
87 return -EINVAL;
88 break;
89 case PTP_PF_PHYSYNC:
Stefan Sørensen72df7a72014-06-27 12:05:33 +020090 if (chan != 0)
91 return -EINVAL;
Richard Cochran60923152014-03-20 22:21:52 +010092 default:
93 return -EINVAL;
94 }
95
Richard Cochran60923152014-03-20 22:21:52 +010096 if (info->verify(info, pin, func, chan)) {
97 pr_err("driver cannot use function %u on pin %u\n", func, chan);
98 return -EOPNOTSUPP;
99 }
100
101 /* Disable whatever function was previously assigned. */
102 if (pin1) {
103 ptp_disable_pinfunc(info, func, chan);
104 pin1->func = PTP_PF_NONE;
105 pin1->chan = 0;
106 }
107 ptp_disable_pinfunc(info, pin2->func, pin2->chan);
108 pin2->func = func;
109 pin2->chan = chan;
110
111 return 0;
112}
113
Richard Cochrand94ba802011-04-22 12:03:08 +0200114int ptp_open(struct posix_clock *pc, fmode_t fmode)
115{
116 return 0;
117}
118
119long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
120{
121 struct ptp_clock_caps caps;
122 struct ptp_clock_request req;
Richard Cochranc3484c22012-11-26 01:44:35 +0000123 struct ptp_sys_offset *sysoff = NULL;
Christopher S. Hall719f1aa2016-02-22 03:15:25 -0800124 struct ptp_sys_offset_precise precise_offset;
Richard Cochran60923152014-03-20 22:21:52 +0100125 struct ptp_pin_desc pd;
Richard Cochrand94ba802011-04-22 12:03:08 +0200126 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
127 struct ptp_clock_info *ops = ptp->info;
Richard Cochran215b13d2012-10-31 06:19:07 +0000128 struct ptp_clock_time *pct;
Richard Cochrane13cfcb2015-03-29 23:11:52 +0200129 struct timespec64 ts;
Christopher S. Hall719f1aa2016-02-22 03:15:25 -0800130 struct system_device_crosststamp xtstamp;
Richard Cochrand94ba802011-04-22 12:03:08 +0200131 int enable, err = 0;
Richard Cochran60923152014-03-20 22:21:52 +0100132 unsigned int i, pin_index;
Richard Cochrand94ba802011-04-22 12:03:08 +0200133
134 switch (cmd) {
135
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;
Richard Cochran60923152014-03-20 22:21:52 +0100143 caps.n_pins = ptp->info->n_pins;
Christopher S. Hall719f1aa2016-02-22 03:15:25 -0800144 caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
Dan Carpentere23ef222011-05-29 22:53:12 +0300145 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
146 err = -EFAULT;
Richard Cochrand94ba802011-04-22 12:03:08 +0200147 break;
148
149 case PTP_EXTTS_REQUEST:
150 if (copy_from_user(&req.extts, (void __user *)arg,
151 sizeof(req.extts))) {
152 err = -EFAULT;
153 break;
154 }
155 if (req.extts.index >= ops->n_ext_ts) {
156 err = -EINVAL;
157 break;
158 }
159 req.type = PTP_CLK_REQ_EXTTS;
160 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
161 err = ops->enable(ops, &req, enable);
162 break;
163
164 case PTP_PEROUT_REQUEST:
165 if (copy_from_user(&req.perout, (void __user *)arg,
166 sizeof(req.perout))) {
167 err = -EFAULT;
168 break;
169 }
170 if (req.perout.index >= ops->n_per_out) {
171 err = -EINVAL;
172 break;
173 }
174 req.type = PTP_CLK_REQ_PEROUT;
175 enable = req.perout.period.sec || req.perout.period.nsec;
176 err = ops->enable(ops, &req, enable);
177 break;
178
179 case PTP_ENABLE_PPS:
180 if (!capable(CAP_SYS_TIME))
181 return -EPERM;
182 req.type = PTP_CLK_REQ_PPS;
183 enable = arg ? 1 : 0;
184 err = ops->enable(ops, &req, enable);
185 break;
186
Christopher S. Hall719f1aa2016-02-22 03:15:25 -0800187 case PTP_SYS_OFFSET_PRECISE:
188 if (!ptp->info->getcrosststamp) {
189 err = -EOPNOTSUPP;
190 break;
191 }
192 err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
193 if (err)
194 break;
195
Vlad Tsyrklevich02a90792016-10-11 15:02:47 +0200196 memset(&precise_offset, 0, sizeof(precise_offset));
Christopher S. Hall719f1aa2016-02-22 03:15:25 -0800197 ts = ktime_to_timespec64(xtstamp.device);
198 precise_offset.device.sec = ts.tv_sec;
199 precise_offset.device.nsec = ts.tv_nsec;
200 ts = ktime_to_timespec64(xtstamp.sys_realtime);
201 precise_offset.sys_realtime.sec = ts.tv_sec;
202 precise_offset.sys_realtime.nsec = ts.tv_nsec;
203 ts = ktime_to_timespec64(xtstamp.sys_monoraw);
204 precise_offset.sys_monoraw.sec = ts.tv_sec;
205 precise_offset.sys_monoraw.nsec = ts.tv_nsec;
206 if (copy_to_user((void __user *)arg, &precise_offset,
207 sizeof(precise_offset)))
208 err = -EFAULT;
209 break;
210
Richard Cochran215b13d2012-10-31 06:19:07 +0000211 case PTP_SYS_OFFSET:
Muhammad Falak R Wani2ece0682016-05-20 17:51:02 +0530212 sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
213 if (IS_ERR(sysoff)) {
214 err = PTR_ERR(sysoff);
Dan Carpenter67563252016-05-26 09:46:22 +0300215 sysoff = NULL;
Richard Cochran215b13d2012-10-31 06:19:07 +0000216 break;
217 }
Richard Cochranc3484c22012-11-26 01:44:35 +0000218 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
Richard Cochran215b13d2012-10-31 06:19:07 +0000219 err = -EINVAL;
220 break;
221 }
Richard Cochranc3484c22012-11-26 01:44:35 +0000222 pct = &sysoff->ts[0];
223 for (i = 0; i < sysoff->n_samples; i++) {
Richard Cochrane13cfcb2015-03-29 23:11:52 +0200224 getnstimeofday64(&ts);
Richard Cochran215b13d2012-10-31 06:19:07 +0000225 pct->sec = ts.tv_sec;
226 pct->nsec = ts.tv_nsec;
227 pct++;
Richard Cochraned7c6312015-03-29 23:12:13 +0200228 ptp->info->gettime64(ptp->info, &ts);
Richard Cochran215b13d2012-10-31 06:19:07 +0000229 pct->sec = ts.tv_sec;
230 pct->nsec = ts.tv_nsec;
231 pct++;
232 }
Richard Cochrane13cfcb2015-03-29 23:11:52 +0200233 getnstimeofday64(&ts);
Richard Cochran215b13d2012-10-31 06:19:07 +0000234 pct->sec = ts.tv_sec;
235 pct->nsec = ts.tv_nsec;
Richard Cochranc3484c22012-11-26 01:44:35 +0000236 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
Richard Cochran215b13d2012-10-31 06:19:07 +0000237 err = -EFAULT;
238 break;
239
Richard Cochran60923152014-03-20 22:21:52 +0100240 case PTP_PIN_GETFUNC:
241 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
242 err = -EFAULT;
243 break;
244 }
245 pin_index = pd.index;
246 if (pin_index >= ops->n_pins) {
247 err = -EINVAL;
248 break;
249 }
250 if (mutex_lock_interruptible(&ptp->pincfg_mux))
251 return -ERESTARTSYS;
252 pd = ops->pin_config[pin_index];
253 mutex_unlock(&ptp->pincfg_mux);
254 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
255 err = -EFAULT;
256 break;
257
258 case PTP_PIN_SETFUNC:
259 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
260 err = -EFAULT;
261 break;
262 }
263 pin_index = pd.index;
264 if (pin_index >= ops->n_pins) {
265 err = -EINVAL;
266 break;
267 }
268 if (mutex_lock_interruptible(&ptp->pincfg_mux))
269 return -ERESTARTSYS;
270 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
271 mutex_unlock(&ptp->pincfg_mux);
272 break;
273
Richard Cochrand94ba802011-04-22 12:03:08 +0200274 default:
275 err = -ENOTTY;
276 break;
277 }
Richard Cochranc3484c22012-11-26 01:44:35 +0000278
279 kfree(sysoff);
Richard Cochrand94ba802011-04-22 12:03:08 +0200280 return err;
281}
282
283unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
284{
285 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
286
287 poll_wait(fp, &ptp->tsev_wq, wait);
288
289 return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
290}
291
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000292#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
293
Richard Cochrand94ba802011-04-22 12:03:08 +0200294ssize_t ptp_read(struct posix_clock *pc,
295 uint rdflags, char __user *buf, size_t cnt)
296{
297 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
298 struct timestamp_event_queue *queue = &ptp->tsevq;
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000299 struct ptp_extts_event *event;
Richard Cochrand94ba802011-04-22 12:03:08 +0200300 unsigned long flags;
301 size_t qcnt, i;
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000302 int result;
Richard Cochrand94ba802011-04-22 12:03:08 +0200303
304 if (cnt % sizeof(struct ptp_extts_event) != 0)
305 return -EINVAL;
306
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000307 if (cnt > EXTTS_BUFSIZE)
308 cnt = EXTTS_BUFSIZE;
Richard Cochrand94ba802011-04-22 12:03:08 +0200309
310 cnt = cnt / sizeof(struct ptp_extts_event);
311
312 if (mutex_lock_interruptible(&ptp->tsevq_mux))
313 return -ERESTARTSYS;
314
315 if (wait_event_interruptible(ptp->tsev_wq,
316 ptp->defunct || queue_cnt(queue))) {
317 mutex_unlock(&ptp->tsevq_mux);
318 return -ERESTARTSYS;
319 }
320
Dan Carpenterfb5a18c2011-05-29 22:54:07 +0300321 if (ptp->defunct) {
322 mutex_unlock(&ptp->tsevq_mux);
Richard Cochrand94ba802011-04-22 12:03:08 +0200323 return -ENODEV;
Dan Carpenterfb5a18c2011-05-29 22:54:07 +0300324 }
Richard Cochrand94ba802011-04-22 12:03:08 +0200325
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000326 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
327 if (!event) {
328 mutex_unlock(&ptp->tsevq_mux);
329 return -ENOMEM;
330 }
331
Richard Cochrand94ba802011-04-22 12:03:08 +0200332 spin_lock_irqsave(&queue->lock, flags);
333
334 qcnt = queue_cnt(queue);
335
336 if (cnt > qcnt)
337 cnt = qcnt;
338
339 for (i = 0; i < cnt; i++) {
340 event[i] = queue->buf[queue->head];
341 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
342 }
343
344 spin_unlock_irqrestore(&queue->lock, flags);
345
346 cnt = cnt * sizeof(struct ptp_extts_event);
347
348 mutex_unlock(&ptp->tsevq_mux);
349
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000350 result = cnt;
Dan Carpenterfb5a18c2011-05-29 22:54:07 +0300351 if (copy_to_user(buf, event, cnt))
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000352 result = -EFAULT;
Richard Cochrand94ba802011-04-22 12:03:08 +0200353
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000354 kfree(event);
355 return result;
Richard Cochrand94ba802011-04-22 12:03:08 +0200356}