Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * PTP 1588 clock support - character device implementation.
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
6 */
7#include <linux/module.h>
8#include <linux/posix-clock.h>
9#include <linux/poll.h>
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/timekeeping.h>
13
14#include <linux/nospec.h>
15
16#include "ptp_private.h"
17
18static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
19 enum ptp_pin_function func, unsigned int chan)
20{
21 struct ptp_clock_request rq;
22 int err = 0;
23
24 memset(&rq, 0, sizeof(rq));
25
26 switch (func) {
27 case PTP_PF_NONE:
28 break;
29 case PTP_PF_EXTTS:
30 rq.type = PTP_CLK_REQ_EXTTS;
31 rq.extts.index = chan;
32 err = ops->enable(ops, &rq, 0);
33 break;
34 case PTP_PF_PEROUT:
35 rq.type = PTP_CLK_REQ_PEROUT;
36 rq.perout.index = chan;
37 err = ops->enable(ops, &rq, 0);
38 break;
39 case PTP_PF_PHYSYNC:
40 break;
41 default:
42 return -EINVAL;
43 }
44
45 return err;
46}
47
48int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
49 enum ptp_pin_function func, unsigned int chan)
50{
51 struct ptp_clock_info *info = ptp->info;
52 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
53 unsigned int i;
54
55 /* Check to see if any other pin previously had this function. */
56 for (i = 0; i < info->n_pins; i++) {
57 if (info->pin_config[i].func == func &&
58 info->pin_config[i].chan == chan) {
59 pin1 = &info->pin_config[i];
60 break;
61 }
62 }
63 if (pin1 && i == pin)
64 return 0;
65
66 /* Check the desired function and channel. */
67 switch (func) {
68 case PTP_PF_NONE:
69 break;
70 case PTP_PF_EXTTS:
71 if (chan >= info->n_ext_ts)
72 return -EINVAL;
73 break;
74 case PTP_PF_PEROUT:
75 if (chan >= info->n_per_out)
76 return -EINVAL;
77 break;
78 case PTP_PF_PHYSYNC:
79 if (chan != 0)
80 return -EINVAL;
81 break;
82 default:
83 return -EINVAL;
84 }
85
86 if (info->verify(info, pin, func, chan)) {
87 pr_err("driver cannot use function %u on pin %u\n", func, chan);
88 return -EOPNOTSUPP;
89 }
90
91 /* Disable whatever function was previously assigned. */
92 if (pin1) {
93 ptp_disable_pinfunc(info, func, chan);
94 pin1->func = PTP_PF_NONE;
95 pin1->chan = 0;
96 }
97 ptp_disable_pinfunc(info, pin2->func, pin2->chan);
98 pin2->func = func;
99 pin2->chan = chan;
100
101 return 0;
102}
103
104int ptp_open(struct posix_clock *pc, fmode_t fmode)
105{
106 return 0;
107}
108
109long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
110{
111 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
112 struct ptp_sys_offset_extended *extoff = NULL;
113 struct ptp_sys_offset_precise precise_offset;
114 struct system_device_crosststamp xtstamp;
115 struct ptp_clock_info *ops = ptp->info;
116 struct ptp_sys_offset *sysoff = NULL;
117 struct ptp_system_timestamp sts;
118 struct ptp_clock_request req;
119 struct ptp_clock_caps caps;
120 struct ptp_clock_time *pct;
121 unsigned int i, pin_index;
122 struct ptp_pin_desc pd;
123 struct timespec64 ts;
124 int enable, err = 0;
125
126 switch (cmd) {
127
128 case PTP_CLOCK_GETCAPS:
129 case PTP_CLOCK_GETCAPS2:
130 memset(&caps, 0, sizeof(caps));
131
132 caps.max_adj = ptp->info->max_adj;
133 caps.n_alarm = ptp->info->n_alarm;
134 caps.n_ext_ts = ptp->info->n_ext_ts;
135 caps.n_per_out = ptp->info->n_per_out;
136 caps.pps = ptp->info->pps;
137 caps.n_pins = ptp->info->n_pins;
138 caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
139 caps.adjust_phase = ptp->info->adjphase != NULL;
140 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
141 err = -EFAULT;
142 break;
143
144 case PTP_EXTTS_REQUEST:
145 case PTP_EXTTS_REQUEST2:
146 memset(&req, 0, sizeof(req));
147
148 if (copy_from_user(&req.extts, (void __user *)arg,
149 sizeof(req.extts))) {
150 err = -EFAULT;
151 break;
152 }
153 if (cmd == PTP_EXTTS_REQUEST2) {
154 /* Tell the drivers to check the flags carefully. */
155 req.extts.flags |= PTP_STRICT_FLAGS;
156 /* Make sure no reserved bit is set. */
157 if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
158 req.extts.rsv[0] || req.extts.rsv[1]) {
159 err = -EINVAL;
160 break;
161 }
162 /* Ensure one of the rising/falling edge bits is set. */
163 if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
164 (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
165 err = -EINVAL;
166 break;
167 }
168 } else if (cmd == PTP_EXTTS_REQUEST) {
169 req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
170 req.extts.rsv[0] = 0;
171 req.extts.rsv[1] = 0;
172 }
173 if (req.extts.index >= ops->n_ext_ts) {
174 err = -EINVAL;
175 break;
176 }
177 req.type = PTP_CLK_REQ_EXTTS;
178 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
179 if (mutex_lock_interruptible(&ptp->pincfg_mux))
180 return -ERESTARTSYS;
181 err = ops->enable(ops, &req, enable);
182 mutex_unlock(&ptp->pincfg_mux);
183 break;
184
185 case PTP_PEROUT_REQUEST:
186 case PTP_PEROUT_REQUEST2:
187 memset(&req, 0, sizeof(req));
188
189 if (copy_from_user(&req.perout, (void __user *)arg,
190 sizeof(req.perout))) {
191 err = -EFAULT;
192 break;
193 }
194 if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
195 req.perout.rsv[0] || req.perout.rsv[1] ||
196 req.perout.rsv[2] || req.perout.rsv[3]) &&
197 cmd == PTP_PEROUT_REQUEST2) {
198 err = -EINVAL;
199 break;
200 } else if (cmd == PTP_PEROUT_REQUEST) {
201 req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
202 req.perout.rsv[0] = 0;
203 req.perout.rsv[1] = 0;
204 req.perout.rsv[2] = 0;
205 req.perout.rsv[3] = 0;
206 }
207 if (req.perout.index >= ops->n_per_out) {
208 err = -EINVAL;
209 break;
210 }
211 req.type = PTP_CLK_REQ_PEROUT;
212 enable = req.perout.period.sec || req.perout.period.nsec;
213 if (mutex_lock_interruptible(&ptp->pincfg_mux))
214 return -ERESTARTSYS;
215 err = ops->enable(ops, &req, enable);
216 mutex_unlock(&ptp->pincfg_mux);
217 break;
218
219 case PTP_ENABLE_PPS:
220 case PTP_ENABLE_PPS2:
221 memset(&req, 0, sizeof(req));
222
223 if (!capable(CAP_SYS_TIME))
224 return -EPERM;
225 req.type = PTP_CLK_REQ_PPS;
226 enable = arg ? 1 : 0;
227 if (mutex_lock_interruptible(&ptp->pincfg_mux))
228 return -ERESTARTSYS;
229 err = ops->enable(ops, &req, enable);
230 mutex_unlock(&ptp->pincfg_mux);
231 break;
232
233 case PTP_SYS_OFFSET_PRECISE:
234 case PTP_SYS_OFFSET_PRECISE2:
235 if (!ptp->info->getcrosststamp) {
236 err = -EOPNOTSUPP;
237 break;
238 }
239 err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
240 if (err)
241 break;
242
243 memset(&precise_offset, 0, sizeof(precise_offset));
244 ts = ktime_to_timespec64(xtstamp.device);
245 precise_offset.device.sec = ts.tv_sec;
246 precise_offset.device.nsec = ts.tv_nsec;
247 ts = ktime_to_timespec64(xtstamp.sys_realtime);
248 precise_offset.sys_realtime.sec = ts.tv_sec;
249 precise_offset.sys_realtime.nsec = ts.tv_nsec;
250 ts = ktime_to_timespec64(xtstamp.sys_monoraw);
251 precise_offset.sys_monoraw.sec = ts.tv_sec;
252 precise_offset.sys_monoraw.nsec = ts.tv_nsec;
253 if (copy_to_user((void __user *)arg, &precise_offset,
254 sizeof(precise_offset)))
255 err = -EFAULT;
256 break;
257
258 case PTP_SYS_OFFSET_EXTENDED:
259 case PTP_SYS_OFFSET_EXTENDED2:
260 if (!ptp->info->gettimex64) {
261 err = -EOPNOTSUPP;
262 break;
263 }
264 extoff = memdup_user((void __user *)arg, sizeof(*extoff));
265 if (IS_ERR(extoff)) {
266 err = PTR_ERR(extoff);
267 extoff = NULL;
268 break;
269 }
270 if (extoff->n_samples > PTP_MAX_SAMPLES
271 || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
272 err = -EINVAL;
273 break;
274 }
275 for (i = 0; i < extoff->n_samples; i++) {
276 err = ptp->info->gettimex64(ptp->info, &ts, &sts);
277 if (err)
278 goto out;
279 extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
280 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
281 extoff->ts[i][1].sec = ts.tv_sec;
282 extoff->ts[i][1].nsec = ts.tv_nsec;
283 extoff->ts[i][2].sec = sts.post_ts.tv_sec;
284 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
285 }
286 if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
287 err = -EFAULT;
288 break;
289
290 case PTP_SYS_OFFSET:
291 case PTP_SYS_OFFSET2:
292 sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
293 if (IS_ERR(sysoff)) {
294 err = PTR_ERR(sysoff);
295 sysoff = NULL;
296 break;
297 }
298 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
299 err = -EINVAL;
300 break;
301 }
302 pct = &sysoff->ts[0];
303 for (i = 0; i < sysoff->n_samples; i++) {
304 ktime_get_real_ts64(&ts);
305 pct->sec = ts.tv_sec;
306 pct->nsec = ts.tv_nsec;
307 pct++;
308 if (ops->gettimex64)
309 err = ops->gettimex64(ops, &ts, NULL);
310 else
311 err = ops->gettime64(ops, &ts);
312 if (err)
313 goto out;
314 pct->sec = ts.tv_sec;
315 pct->nsec = ts.tv_nsec;
316 pct++;
317 }
318 ktime_get_real_ts64(&ts);
319 pct->sec = ts.tv_sec;
320 pct->nsec = ts.tv_nsec;
321 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
322 err = -EFAULT;
323 break;
324
325 case PTP_PIN_GETFUNC:
326 case PTP_PIN_GETFUNC2:
327 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
328 err = -EFAULT;
329 break;
330 }
331 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
332 || pd.rsv[3] || pd.rsv[4])
333 && cmd == PTP_PIN_GETFUNC2) {
334 err = -EINVAL;
335 break;
336 } else if (cmd == PTP_PIN_GETFUNC) {
337 pd.rsv[0] = 0;
338 pd.rsv[1] = 0;
339 pd.rsv[2] = 0;
340 pd.rsv[3] = 0;
341 pd.rsv[4] = 0;
342 }
343 pin_index = pd.index;
344 if (pin_index >= ops->n_pins) {
345 err = -EINVAL;
346 break;
347 }
348 pin_index = array_index_nospec(pin_index, ops->n_pins);
349 if (mutex_lock_interruptible(&ptp->pincfg_mux))
350 return -ERESTARTSYS;
351 pd = ops->pin_config[pin_index];
352 mutex_unlock(&ptp->pincfg_mux);
353 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
354 err = -EFAULT;
355 break;
356
357 case PTP_PIN_SETFUNC:
358 case PTP_PIN_SETFUNC2:
359 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
360 err = -EFAULT;
361 break;
362 }
363 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
364 || pd.rsv[3] || pd.rsv[4])
365 && cmd == PTP_PIN_SETFUNC2) {
366 err = -EINVAL;
367 break;
368 } else if (cmd == PTP_PIN_SETFUNC) {
369 pd.rsv[0] = 0;
370 pd.rsv[1] = 0;
371 pd.rsv[2] = 0;
372 pd.rsv[3] = 0;
373 pd.rsv[4] = 0;
374 }
375 pin_index = pd.index;
376 if (pin_index >= ops->n_pins) {
377 err = -EINVAL;
378 break;
379 }
380 pin_index = array_index_nospec(pin_index, ops->n_pins);
381 if (mutex_lock_interruptible(&ptp->pincfg_mux))
382 return -ERESTARTSYS;
383 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
384 mutex_unlock(&ptp->pincfg_mux);
385 break;
386
387 default:
388 err = -ENOTTY;
389 break;
390 }
391
392out:
393 kfree(extoff);
394 kfree(sysoff);
395 return err;
396}
397
398__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
399{
400 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
401
402 poll_wait(fp, &ptp->tsev_wq, wait);
403
404 return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
405}
406
407#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
408
409ssize_t ptp_read(struct posix_clock *pc,
410 uint rdflags, char __user *buf, size_t cnt)
411{
412 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
413 struct timestamp_event_queue *queue = &ptp->tsevq;
414 struct ptp_extts_event *event;
415 unsigned long flags;
416 size_t qcnt, i;
417 int result;
418
419 if (cnt % sizeof(struct ptp_extts_event) != 0)
420 return -EINVAL;
421
422 if (cnt > EXTTS_BUFSIZE)
423 cnt = EXTTS_BUFSIZE;
424
425 cnt = cnt / sizeof(struct ptp_extts_event);
426
427 if (mutex_lock_interruptible(&ptp->tsevq_mux))
428 return -ERESTARTSYS;
429
430 if (wait_event_interruptible(ptp->tsev_wq,
431 ptp->defunct || queue_cnt(queue))) {
432 mutex_unlock(&ptp->tsevq_mux);
433 return -ERESTARTSYS;
434 }
435
436 if (ptp->defunct) {
437 mutex_unlock(&ptp->tsevq_mux);
438 return -ENODEV;
439 }
440
441 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
442 if (!event) {
443 mutex_unlock(&ptp->tsevq_mux);
444 return -ENOMEM;
445 }
446
447 spin_lock_irqsave(&queue->lock, flags);
448
449 qcnt = queue_cnt(queue);
450
451 if (cnt > qcnt)
452 cnt = qcnt;
453
454 for (i = 0; i < cnt; i++) {
455 event[i] = queue->buf[queue->head];
456 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
457 }
458
459 spin_unlock_irqrestore(&queue->lock, flags);
460
461 cnt = cnt * sizeof(struct ptp_extts_event);
462
463 mutex_unlock(&ptp->tsevq_mux);
464
465 result = cnt;
466 if (copy_to_user(buf, event, cnt))
467 result = -EFAULT;
468
469 kfree(event);
470 return result;
471}