]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/ptp/ptp_chardev.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[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 152 if (cmd == PTP_EXTTS_REQUEST2) {
6138e687
RC
153 /* Tell the drivers to check the flags carefully. */
154 req.extts.flags |= PTP_STRICT_FLAGS;
cd734d54
RC
155 /* Make sure no reserved bit is set. */
156 if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
157 req.extts.rsv[0] || req.extts.rsv[1]) {
158 err = -EINVAL;
159 break;
160 }
161 /* Ensure one of the rising/falling edge bits is set. */
162 if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
163 (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
164 err = -EINVAL;
165 break;
166 }
41560658 167 } else if (cmd == PTP_EXTTS_REQUEST) {
2df4de16 168 req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
41560658
FB
169 req.extts.rsv[0] = 0;
170 req.extts.rsv[1] = 0;
171 }
d94ba80e
RC
172 if (req.extts.index >= ops->n_ext_ts) {
173 err = -EINVAL;
174 break;
175 }
176 req.type = PTP_CLK_REQ_EXTTS;
177 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
62582a7e
RC
178 if (mutex_lock_interruptible(&ptp->pincfg_mux))
179 return -ERESTARTSYS;
d94ba80e 180 err = ops->enable(ops, &req, enable);
62582a7e 181 mutex_unlock(&ptp->pincfg_mux);
d94ba80e
RC
182 break;
183
184 case PTP_PEROUT_REQUEST:
41560658
FB
185 case PTP_PEROUT_REQUEST2:
186 memset(&req, 0, sizeof(req));
187
d94ba80e
RC
188 if (copy_from_user(&req.perout, (void __user *)arg,
189 sizeof(req.perout))) {
190 err = -EFAULT;
191 break;
192 }
41560658
FB
193 if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
194 req.perout.rsv[0] || req.perout.rsv[1] ||
195 req.perout.rsv[2] || req.perout.rsv[3]) &&
196 cmd == PTP_PEROUT_REQUEST2) {
197 err = -EINVAL;
198 break;
199 } else if (cmd == PTP_PEROUT_REQUEST) {
2df4de16 200 req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
41560658
FB
201 req.perout.rsv[0] = 0;
202 req.perout.rsv[1] = 0;
203 req.perout.rsv[2] = 0;
204 req.perout.rsv[3] = 0;
205 }
d94ba80e
RC
206 if (req.perout.index >= ops->n_per_out) {
207 err = -EINVAL;
208 break;
209 }
210 req.type = PTP_CLK_REQ_PEROUT;
211 enable = req.perout.period.sec || req.perout.period.nsec;
62582a7e
RC
212 if (mutex_lock_interruptible(&ptp->pincfg_mux))
213 return -ERESTARTSYS;
d94ba80e 214 err = ops->enable(ops, &req, enable);
62582a7e 215 mutex_unlock(&ptp->pincfg_mux);
d94ba80e
RC
216 break;
217
218 case PTP_ENABLE_PPS:
41560658
FB
219 case PTP_ENABLE_PPS2:
220 memset(&req, 0, sizeof(req));
221
d94ba80e
RC
222 if (!capable(CAP_SYS_TIME))
223 return -EPERM;
224 req.type = PTP_CLK_REQ_PPS;
225 enable = arg ? 1 : 0;
62582a7e
RC
226 if (mutex_lock_interruptible(&ptp->pincfg_mux))
227 return -ERESTARTSYS;
d94ba80e 228 err = ops->enable(ops, &req, enable);
62582a7e 229 mutex_unlock(&ptp->pincfg_mux);
d94ba80e
RC
230 break;
231
719f1aa4 232 case PTP_SYS_OFFSET_PRECISE:
41560658 233 case PTP_SYS_OFFSET_PRECISE2:
719f1aa4
CH
234 if (!ptp->info->getcrosststamp) {
235 err = -EOPNOTSUPP;
236 break;
237 }
238 err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
239 if (err)
240 break;
241
02a9079c 242 memset(&precise_offset, 0, sizeof(precise_offset));
719f1aa4
CH
243 ts = ktime_to_timespec64(xtstamp.device);
244 precise_offset.device.sec = ts.tv_sec;
245 precise_offset.device.nsec = ts.tv_nsec;
246 ts = ktime_to_timespec64(xtstamp.sys_realtime);
247 precise_offset.sys_realtime.sec = ts.tv_sec;
248 precise_offset.sys_realtime.nsec = ts.tv_nsec;
249 ts = ktime_to_timespec64(xtstamp.sys_monoraw);
250 precise_offset.sys_monoraw.sec = ts.tv_sec;
251 precise_offset.sys_monoraw.nsec = ts.tv_nsec;
252 if (copy_to_user((void __user *)arg, &precise_offset,
253 sizeof(precise_offset)))
254 err = -EFAULT;
255 break;
256
36180087 257 case PTP_SYS_OFFSET_EXTENDED:
41560658 258 case PTP_SYS_OFFSET_EXTENDED2:
36180087
ML
259 if (!ptp->info->gettimex64) {
260 err = -EOPNOTSUPP;
261 break;
262 }
263 extoff = memdup_user((void __user *)arg, sizeof(*extoff));
264 if (IS_ERR(extoff)) {
265 err = PTR_ERR(extoff);
266 extoff = NULL;
267 break;
268 }
895ac137
ES
269 if (extoff->n_samples > PTP_MAX_SAMPLES
270 || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
36180087
ML
271 err = -EINVAL;
272 break;
273 }
274 for (i = 0; i < extoff->n_samples; i++) {
275 err = ptp->info->gettimex64(ptp->info, &ts, &sts);
276 if (err)
277 goto out;
278 extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
279 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
280 extoff->ts[i][1].sec = ts.tv_sec;
281 extoff->ts[i][1].nsec = ts.tv_nsec;
282 extoff->ts[i][2].sec = sts.post_ts.tv_sec;
283 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
284 }
285 if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
286 err = -EFAULT;
287 break;
288
215b13dd 289 case PTP_SYS_OFFSET:
41560658 290 case PTP_SYS_OFFSET2:
2ece068e
MFW
291 sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
292 if (IS_ERR(sysoff)) {
293 err = PTR_ERR(sysoff);
6756325a 294 sysoff = NULL;
215b13dd
RC
295 break;
296 }
c3484c27 297 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
215b13dd
RC
298 err = -EINVAL;
299 break;
300 }
c3484c27
RC
301 pct = &sysoff->ts[0];
302 for (i = 0; i < sysoff->n_samples; i++) {
f696a21c 303 ktime_get_real_ts64(&ts);
215b13dd
RC
304 pct->sec = ts.tv_sec;
305 pct->nsec = ts.tv_nsec;
306 pct++;
916444df
ML
307 if (ops->gettimex64)
308 err = ops->gettimex64(ops, &ts, NULL);
309 else
310 err = ops->gettime64(ops, &ts);
83d0bdc7
ML
311 if (err)
312 goto out;
215b13dd
RC
313 pct->sec = ts.tv_sec;
314 pct->nsec = ts.tv_nsec;
315 pct++;
316 }
f696a21c 317 ktime_get_real_ts64(&ts);
215b13dd
RC
318 pct->sec = ts.tv_sec;
319 pct->nsec = ts.tv_nsec;
c3484c27 320 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
215b13dd
RC
321 err = -EFAULT;
322 break;
323
6092315d 324 case PTP_PIN_GETFUNC:
41560658 325 case PTP_PIN_GETFUNC2:
6092315d
RC
326 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
327 err = -EFAULT;
328 break;
329 }
41560658
FB
330 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
331 || pd.rsv[3] || pd.rsv[4])
332 && cmd == PTP_PIN_GETFUNC2) {
333 err = -EINVAL;
334 break;
335 } else if (cmd == PTP_PIN_GETFUNC) {
336 pd.rsv[0] = 0;
337 pd.rsv[1] = 0;
338 pd.rsv[2] = 0;
339 pd.rsv[3] = 0;
340 pd.rsv[4] = 0;
341 }
6092315d
RC
342 pin_index = pd.index;
343 if (pin_index >= ops->n_pins) {
344 err = -EINVAL;
345 break;
346 }
efa61c8c 347 pin_index = array_index_nospec(pin_index, ops->n_pins);
6092315d
RC
348 if (mutex_lock_interruptible(&ptp->pincfg_mux))
349 return -ERESTARTSYS;
350 pd = ops->pin_config[pin_index];
351 mutex_unlock(&ptp->pincfg_mux);
352 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
353 err = -EFAULT;
354 break;
355
356 case PTP_PIN_SETFUNC:
41560658 357 case PTP_PIN_SETFUNC2:
6092315d
RC
358 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
359 err = -EFAULT;
360 break;
361 }
41560658
FB
362 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
363 || pd.rsv[3] || pd.rsv[4])
364 && cmd == PTP_PIN_SETFUNC2) {
365 err = -EINVAL;
366 break;
367 } else if (cmd == PTP_PIN_SETFUNC) {
368 pd.rsv[0] = 0;
369 pd.rsv[1] = 0;
370 pd.rsv[2] = 0;
371 pd.rsv[3] = 0;
372 pd.rsv[4] = 0;
373 }
6092315d
RC
374 pin_index = pd.index;
375 if (pin_index >= ops->n_pins) {
376 err = -EINVAL;
377 break;
378 }
efa61c8c 379 pin_index = array_index_nospec(pin_index, ops->n_pins);
6092315d
RC
380 if (mutex_lock_interruptible(&ptp->pincfg_mux))
381 return -ERESTARTSYS;
382 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
383 mutex_unlock(&ptp->pincfg_mux);
384 break;
385
d94ba80e
RC
386 default:
387 err = -ENOTTY;
388 break;
389 }
c3484c27 390
83d0bdc7 391out:
36180087 392 kfree(extoff);
c3484c27 393 kfree(sysoff);
d94ba80e
RC
394 return err;
395}
396
afc9a42b 397__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
d94ba80e
RC
398{
399 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
400
401 poll_wait(fp, &ptp->tsev_wq, wait);
402
a9a08845 403 return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
d94ba80e
RC
404}
405
c7ec0bad
RC
406#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
407
d94ba80e
RC
408ssize_t ptp_read(struct posix_clock *pc,
409 uint rdflags, char __user *buf, size_t cnt)
410{
411 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
412 struct timestamp_event_queue *queue = &ptp->tsevq;
c7ec0bad 413 struct ptp_extts_event *event;
d94ba80e
RC
414 unsigned long flags;
415 size_t qcnt, i;
c7ec0bad 416 int result;
d94ba80e
RC
417
418 if (cnt % sizeof(struct ptp_extts_event) != 0)
419 return -EINVAL;
420
c7ec0bad
RC
421 if (cnt > EXTTS_BUFSIZE)
422 cnt = EXTTS_BUFSIZE;
d94ba80e
RC
423
424 cnt = cnt / sizeof(struct ptp_extts_event);
425
426 if (mutex_lock_interruptible(&ptp->tsevq_mux))
427 return -ERESTARTSYS;
428
429 if (wait_event_interruptible(ptp->tsev_wq,
430 ptp->defunct || queue_cnt(queue))) {
431 mutex_unlock(&ptp->tsevq_mux);
432 return -ERESTARTSYS;
433 }
434
fb5a18cf
DC
435 if (ptp->defunct) {
436 mutex_unlock(&ptp->tsevq_mux);
d94ba80e 437 return -ENODEV;
fb5a18cf 438 }
d94ba80e 439
c7ec0bad
RC
440 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
441 if (!event) {
442 mutex_unlock(&ptp->tsevq_mux);
443 return -ENOMEM;
444 }
445
d94ba80e
RC
446 spin_lock_irqsave(&queue->lock, flags);
447
448 qcnt = queue_cnt(queue);
449
450 if (cnt > qcnt)
451 cnt = qcnt;
452
453 for (i = 0; i < cnt; i++) {
454 event[i] = queue->buf[queue->head];
455 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
456 }
457
458 spin_unlock_irqrestore(&queue->lock, flags);
459
460 cnt = cnt * sizeof(struct ptp_extts_event);
461
462 mutex_unlock(&ptp->tsevq_mux);
463
c7ec0bad 464 result = cnt;
fb5a18cf 465 if (copy_to_user(buf, event, cnt))
c7ec0bad 466 result = -EFAULT;
d94ba80e 467
c7ec0bad
RC
468 kfree(event);
469 return result;
d94ba80e 470}