]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/ptp/ptp_chardev.c
renesas: reject unsupported external timestamp flags
[mirror_ubuntu-jammy-kernel.git] / drivers / ptp / ptp_chardev.c
CommitLineData
74ba9207 1// SPDX-License-Identifier: GPL-2.0-or-later
d94ba80e
RC
2/*
3 * PTP 1588 clock support - character device implementation.
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
d94ba80e
RC
6 */
7#include <linux/module.h>
8#include <linux/posix-clock.h>
9#include <linux/poll.h>
10#include <linux/sched.h>
c7ec0bad 11#include <linux/slab.h>
719f1aa4 12#include <linux/timekeeping.h>
d94ba80e 13
efa61c8c
GS
14#include <linux/nospec.h>
15
d94ba80e
RC
16#include "ptp_private.h"
17
6092315d
RC
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:
72df7a72
SS
79 if (chan != 0)
80 return -EINVAL;
9ba8376c 81 break;
6092315d
RC
82 default:
83 return -EINVAL;
84 }
85
6092315d
RC
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
d94ba80e
RC
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{
d94ba80e 111 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
36180087 112 struct ptp_sys_offset_extended *extoff = NULL;
fbb960ac
ML
113 struct ptp_sys_offset_precise precise_offset;
114 struct system_device_crosststamp xtstamp;
d94ba80e 115 struct ptp_clock_info *ops = ptp->info;
fbb960ac 116 struct ptp_sys_offset *sysoff = NULL;
36180087 117 struct ptp_system_timestamp sts;
fbb960ac
ML
118 struct ptp_clock_request req;
119 struct ptp_clock_caps caps;
215b13dd 120 struct ptp_clock_time *pct;
fbb960ac
ML
121 unsigned int i, pin_index;
122 struct ptp_pin_desc pd;
e13cfcb0 123 struct timespec64 ts;
d94ba80e
RC
124 int enable, err = 0;
125
126 switch (cmd) {
127
128 case PTP_CLOCK_GETCAPS:
41560658 129 case PTP_CLOCK_GETCAPS2:
d94ba80e 130 memset(&caps, 0, sizeof(caps));
41560658 131
d94ba80e
RC
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;
6092315d 137 caps.n_pins = ptp->info->n_pins;
719f1aa4 138 caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
e23ef227
DC
139 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
140 err = -EFAULT;
d94ba80e
RC
141 break;
142
143 case PTP_EXTTS_REQUEST:
41560658
FB
144 case PTP_EXTTS_REQUEST2:
145 memset(&req, 0, sizeof(req));
146
d94ba80e
RC
147 if (copy_from_user(&req.extts, (void __user *)arg,
148 sizeof(req.extts))) {
149 err = -EFAULT;
150 break;
151 }
cd734d54
RC
152 if (cmd == PTP_EXTTS_REQUEST2) {
153 /* Make sure no reserved bit is set. */
154 if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
155 req.extts.rsv[0] || req.extts.rsv[1]) {
156 err = -EINVAL;
157 break;
158 }
159 /* Ensure one of the rising/falling edge bits is set. */
160 if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
161 (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
162 err = -EINVAL;
163 break;
164 }
41560658 165 } else if (cmd == PTP_EXTTS_REQUEST) {
2df4de16 166 req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
41560658
FB
167 req.extts.rsv[0] = 0;
168 req.extts.rsv[1] = 0;
169 }
d94ba80e
RC
170 if (req.extts.index >= ops->n_ext_ts) {
171 err = -EINVAL;
172 break;
173 }
174 req.type = PTP_CLK_REQ_EXTTS;
175 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
176 err = ops->enable(ops, &req, enable);
177 break;
178
179 case PTP_PEROUT_REQUEST:
41560658
FB
180 case PTP_PEROUT_REQUEST2:
181 memset(&req, 0, sizeof(req));
182
d94ba80e
RC
183 if (copy_from_user(&req.perout, (void __user *)arg,
184 sizeof(req.perout))) {
185 err = -EFAULT;
186 break;
187 }
41560658
FB
188 if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
189 req.perout.rsv[0] || req.perout.rsv[1] ||
190 req.perout.rsv[2] || req.perout.rsv[3]) &&
191 cmd == PTP_PEROUT_REQUEST2) {
192 err = -EINVAL;
193 break;
194 } else if (cmd == PTP_PEROUT_REQUEST) {
2df4de16 195 req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
41560658
FB
196 req.perout.rsv[0] = 0;
197 req.perout.rsv[1] = 0;
198 req.perout.rsv[2] = 0;
199 req.perout.rsv[3] = 0;
200 }
d94ba80e
RC
201 if (req.perout.index >= ops->n_per_out) {
202 err = -EINVAL;
203 break;
204 }
205 req.type = PTP_CLK_REQ_PEROUT;
206 enable = req.perout.period.sec || req.perout.period.nsec;
207 err = ops->enable(ops, &req, enable);
208 break;
209
210 case PTP_ENABLE_PPS:
41560658
FB
211 case PTP_ENABLE_PPS2:
212 memset(&req, 0, sizeof(req));
213
d94ba80e
RC
214 if (!capable(CAP_SYS_TIME))
215 return -EPERM;
216 req.type = PTP_CLK_REQ_PPS;
217 enable = arg ? 1 : 0;
218 err = ops->enable(ops, &req, enable);
219 break;
220
719f1aa4 221 case PTP_SYS_OFFSET_PRECISE:
41560658 222 case PTP_SYS_OFFSET_PRECISE2:
719f1aa4
CH
223 if (!ptp->info->getcrosststamp) {
224 err = -EOPNOTSUPP;
225 break;
226 }
227 err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
228 if (err)
229 break;
230
02a9079c 231 memset(&precise_offset, 0, sizeof(precise_offset));
719f1aa4
CH
232 ts = ktime_to_timespec64(xtstamp.device);
233 precise_offset.device.sec = ts.tv_sec;
234 precise_offset.device.nsec = ts.tv_nsec;
235 ts = ktime_to_timespec64(xtstamp.sys_realtime);
236 precise_offset.sys_realtime.sec = ts.tv_sec;
237 precise_offset.sys_realtime.nsec = ts.tv_nsec;
238 ts = ktime_to_timespec64(xtstamp.sys_monoraw);
239 precise_offset.sys_monoraw.sec = ts.tv_sec;
240 precise_offset.sys_monoraw.nsec = ts.tv_nsec;
241 if (copy_to_user((void __user *)arg, &precise_offset,
242 sizeof(precise_offset)))
243 err = -EFAULT;
244 break;
245
36180087 246 case PTP_SYS_OFFSET_EXTENDED:
41560658 247 case PTP_SYS_OFFSET_EXTENDED2:
36180087
ML
248 if (!ptp->info->gettimex64) {
249 err = -EOPNOTSUPP;
250 break;
251 }
252 extoff = memdup_user((void __user *)arg, sizeof(*extoff));
253 if (IS_ERR(extoff)) {
254 err = PTR_ERR(extoff);
255 extoff = NULL;
256 break;
257 }
895ac137
ES
258 if (extoff->n_samples > PTP_MAX_SAMPLES
259 || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
36180087
ML
260 err = -EINVAL;
261 break;
262 }
263 for (i = 0; i < extoff->n_samples; i++) {
264 err = ptp->info->gettimex64(ptp->info, &ts, &sts);
265 if (err)
266 goto out;
267 extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
268 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
269 extoff->ts[i][1].sec = ts.tv_sec;
270 extoff->ts[i][1].nsec = ts.tv_nsec;
271 extoff->ts[i][2].sec = sts.post_ts.tv_sec;
272 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
273 }
274 if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
275 err = -EFAULT;
276 break;
277
215b13dd 278 case PTP_SYS_OFFSET:
41560658 279 case PTP_SYS_OFFSET2:
2ece068e
MFW
280 sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
281 if (IS_ERR(sysoff)) {
282 err = PTR_ERR(sysoff);
6756325a 283 sysoff = NULL;
215b13dd
RC
284 break;
285 }
c3484c27 286 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
215b13dd
RC
287 err = -EINVAL;
288 break;
289 }
c3484c27
RC
290 pct = &sysoff->ts[0];
291 for (i = 0; i < sysoff->n_samples; i++) {
f696a21c 292 ktime_get_real_ts64(&ts);
215b13dd
RC
293 pct->sec = ts.tv_sec;
294 pct->nsec = ts.tv_nsec;
295 pct++;
916444df
ML
296 if (ops->gettimex64)
297 err = ops->gettimex64(ops, &ts, NULL);
298 else
299 err = ops->gettime64(ops, &ts);
83d0bdc7
ML
300 if (err)
301 goto out;
215b13dd
RC
302 pct->sec = ts.tv_sec;
303 pct->nsec = ts.tv_nsec;
304 pct++;
305 }
f696a21c 306 ktime_get_real_ts64(&ts);
215b13dd
RC
307 pct->sec = ts.tv_sec;
308 pct->nsec = ts.tv_nsec;
c3484c27 309 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
215b13dd
RC
310 err = -EFAULT;
311 break;
312
6092315d 313 case PTP_PIN_GETFUNC:
41560658 314 case PTP_PIN_GETFUNC2:
6092315d
RC
315 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
316 err = -EFAULT;
317 break;
318 }
41560658
FB
319 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
320 || pd.rsv[3] || pd.rsv[4])
321 && cmd == PTP_PIN_GETFUNC2) {
322 err = -EINVAL;
323 break;
324 } else if (cmd == PTP_PIN_GETFUNC) {
325 pd.rsv[0] = 0;
326 pd.rsv[1] = 0;
327 pd.rsv[2] = 0;
328 pd.rsv[3] = 0;
329 pd.rsv[4] = 0;
330 }
6092315d
RC
331 pin_index = pd.index;
332 if (pin_index >= ops->n_pins) {
333 err = -EINVAL;
334 break;
335 }
efa61c8c 336 pin_index = array_index_nospec(pin_index, ops->n_pins);
6092315d
RC
337 if (mutex_lock_interruptible(&ptp->pincfg_mux))
338 return -ERESTARTSYS;
339 pd = ops->pin_config[pin_index];
340 mutex_unlock(&ptp->pincfg_mux);
341 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
342 err = -EFAULT;
343 break;
344
345 case PTP_PIN_SETFUNC:
41560658 346 case PTP_PIN_SETFUNC2:
6092315d
RC
347 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
348 err = -EFAULT;
349 break;
350 }
41560658
FB
351 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
352 || pd.rsv[3] || pd.rsv[4])
353 && cmd == PTP_PIN_SETFUNC2) {
354 err = -EINVAL;
355 break;
356 } else if (cmd == PTP_PIN_SETFUNC) {
357 pd.rsv[0] = 0;
358 pd.rsv[1] = 0;
359 pd.rsv[2] = 0;
360 pd.rsv[3] = 0;
361 pd.rsv[4] = 0;
362 }
6092315d
RC
363 pin_index = pd.index;
364 if (pin_index >= ops->n_pins) {
365 err = -EINVAL;
366 break;
367 }
efa61c8c 368 pin_index = array_index_nospec(pin_index, ops->n_pins);
6092315d
RC
369 if (mutex_lock_interruptible(&ptp->pincfg_mux))
370 return -ERESTARTSYS;
371 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
372 mutex_unlock(&ptp->pincfg_mux);
373 break;
374
d94ba80e
RC
375 default:
376 err = -ENOTTY;
377 break;
378 }
c3484c27 379
83d0bdc7 380out:
36180087 381 kfree(extoff);
c3484c27 382 kfree(sysoff);
d94ba80e
RC
383 return err;
384}
385
afc9a42b 386__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
d94ba80e
RC
387{
388 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
389
390 poll_wait(fp, &ptp->tsev_wq, wait);
391
a9a08845 392 return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
d94ba80e
RC
393}
394
c7ec0bad
RC
395#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
396
d94ba80e
RC
397ssize_t ptp_read(struct posix_clock *pc,
398 uint rdflags, char __user *buf, size_t cnt)
399{
400 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
401 struct timestamp_event_queue *queue = &ptp->tsevq;
c7ec0bad 402 struct ptp_extts_event *event;
d94ba80e
RC
403 unsigned long flags;
404 size_t qcnt, i;
c7ec0bad 405 int result;
d94ba80e
RC
406
407 if (cnt % sizeof(struct ptp_extts_event) != 0)
408 return -EINVAL;
409
c7ec0bad
RC
410 if (cnt > EXTTS_BUFSIZE)
411 cnt = EXTTS_BUFSIZE;
d94ba80e
RC
412
413 cnt = cnt / sizeof(struct ptp_extts_event);
414
415 if (mutex_lock_interruptible(&ptp->tsevq_mux))
416 return -ERESTARTSYS;
417
418 if (wait_event_interruptible(ptp->tsev_wq,
419 ptp->defunct || queue_cnt(queue))) {
420 mutex_unlock(&ptp->tsevq_mux);
421 return -ERESTARTSYS;
422 }
423
fb5a18cf
DC
424 if (ptp->defunct) {
425 mutex_unlock(&ptp->tsevq_mux);
d94ba80e 426 return -ENODEV;
fb5a18cf 427 }
d94ba80e 428
c7ec0bad
RC
429 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
430 if (!event) {
431 mutex_unlock(&ptp->tsevq_mux);
432 return -ENOMEM;
433 }
434
d94ba80e
RC
435 spin_lock_irqsave(&queue->lock, flags);
436
437 qcnt = queue_cnt(queue);
438
439 if (cnt > qcnt)
440 cnt = qcnt;
441
442 for (i = 0; i < cnt; i++) {
443 event[i] = queue->buf[queue->head];
444 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
445 }
446
447 spin_unlock_irqrestore(&queue->lock, flags);
448
449 cnt = cnt * sizeof(struct ptp_extts_event);
450
451 mutex_unlock(&ptp->tsevq_mux);
452
c7ec0bad 453 result = cnt;
fb5a18cf 454 if (copy_to_user(buf, event, cnt))
c7ec0bad 455 result = -EFAULT;
d94ba80e 456
c7ec0bad
RC
457 kfree(event);
458 return result;
d94ba80e 459}