blob: 37c4807f15c605553259832794307230dd53105e [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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 and channel %u on pin %u\n",
88 func, chan, pin);
89 return -EOPNOTSUPP;
90 }
91
92 /* Disable whatever function was previously assigned. */
93 if (pin1) {
94 ptp_disable_pinfunc(info, func, chan);
95 pin1->func = PTP_PF_NONE;
96 pin1->chan = 0;
97 }
98 ptp_disable_pinfunc(info, pin2->func, pin2->chan);
99 pin2->func = func;
100 pin2->chan = chan;
101
102 return 0;
103}
104
105int ptp_open(struct posix_clock *pc, fmode_t fmode)
106{
107 return 0;
108}
109
110long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
111{
112 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
113 struct ptp_sys_offset_extended *extoff = NULL;
114 struct ptp_sys_offset_precise precise_offset;
115 struct system_device_crosststamp xtstamp;
116 struct ptp_clock_info *ops = ptp->info;
117 struct ptp_sys_offset *sysoff = NULL;
118 struct ptp_system_timestamp sts;
119 struct ptp_clock_request req;
120 struct ptp_clock_caps caps;
121 struct ptp_clock_time *pct;
122 unsigned int i, pin_index;
123 struct ptp_pin_desc pd;
124 struct timespec64 ts;
125 int enable, err = 0;
126
127 switch (cmd) {
128
129 case PTP_CLOCK_GETCAPS:
130 case PTP_CLOCK_GETCAPS2:
131 memset(&caps, 0, sizeof(caps));
132
133 caps.max_adj = ptp->info->max_adj;
134 caps.n_alarm = ptp->info->n_alarm;
135 caps.n_ext_ts = ptp->info->n_ext_ts;
136 caps.n_per_out = ptp->info->n_per_out;
137 caps.pps = ptp->info->pps;
138 caps.n_pins = ptp->info->n_pins;
139 caps.cross_timestamping = ptp->info->getcrosststamp != 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 err = ops->enable(ops, &req, enable);
180 break;
181
182 case PTP_PEROUT_REQUEST:
183 case PTP_PEROUT_REQUEST2:
184 memset(&req, 0, sizeof(req));
185
186 if (copy_from_user(&req.perout, (void __user *)arg,
187 sizeof(req.perout))) {
188 err = -EFAULT;
189 break;
190 }
191 if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
192 req.perout.rsv[0] || req.perout.rsv[1] ||
193 req.perout.rsv[2] || req.perout.rsv[3]) &&
194 cmd == PTP_PEROUT_REQUEST2) {
195 err = -EINVAL;
196 break;
197 } else if (cmd == PTP_PEROUT_REQUEST) {
198 req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
199 req.perout.rsv[0] = 0;
200 req.perout.rsv[1] = 0;
201 req.perout.rsv[2] = 0;
202 req.perout.rsv[3] = 0;
203 }
204 if (req.perout.index >= ops->n_per_out) {
205 err = -EINVAL;
206 break;
207 }
208 req.type = PTP_CLK_REQ_PEROUT;
209 enable = req.perout.period.sec || req.perout.period.nsec;
210 err = ops->enable(ops, &req, enable);
211 break;
212
213 case PTP_ENABLE_PPS:
214 case PTP_ENABLE_PPS2:
215 memset(&req, 0, sizeof(req));
216
217 if (!capable(CAP_SYS_TIME))
218 return -EPERM;
219 req.type = PTP_CLK_REQ_PPS;
220 enable = arg ? 1 : 0;
221 err = ops->enable(ops, &req, enable);
222 break;
223
224 case PTP_SYS_OFFSET_PRECISE:
225 case PTP_SYS_OFFSET_PRECISE2:
226 if (!ptp->info->getcrosststamp) {
227 err = -EOPNOTSUPP;
228 break;
229 }
230 err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
231 if (err)
232 break;
233
234 memset(&precise_offset, 0, sizeof(precise_offset));
235 ts = ktime_to_timespec64(xtstamp.device);
236 precise_offset.device.sec = ts.tv_sec;
237 precise_offset.device.nsec = ts.tv_nsec;
238 ts = ktime_to_timespec64(xtstamp.sys_realtime);
239 precise_offset.sys_realtime.sec = ts.tv_sec;
240 precise_offset.sys_realtime.nsec = ts.tv_nsec;
241 ts = ktime_to_timespec64(xtstamp.sys_monoraw);
242 precise_offset.sys_monoraw.sec = ts.tv_sec;
243 precise_offset.sys_monoraw.nsec = ts.tv_nsec;
244 if (copy_to_user((void __user *)arg, &precise_offset,
245 sizeof(precise_offset)))
246 err = -EFAULT;
247 break;
248
249 case PTP_SYS_OFFSET_EXTENDED:
250 case PTP_SYS_OFFSET_EXTENDED2:
251 if (!ptp->info->gettimex64) {
252 err = -EOPNOTSUPP;
253 break;
254 }
255 extoff = memdup_user((void __user *)arg, sizeof(*extoff));
256 if (IS_ERR(extoff)) {
257 err = PTR_ERR(extoff);
258 extoff = NULL;
259 break;
260 }
261 if (extoff->n_samples > PTP_MAX_SAMPLES
262 || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
263 err = -EINVAL;
264 break;
265 }
266 for (i = 0; i < extoff->n_samples; i++) {
267 err = ptp->info->gettimex64(ptp->info, &ts, &sts);
268 if (err)
269 goto out;
270 extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
271 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
272 extoff->ts[i][1].sec = ts.tv_sec;
273 extoff->ts[i][1].nsec = ts.tv_nsec;
274 extoff->ts[i][2].sec = sts.post_ts.tv_sec;
275 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
276 }
277 if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
278 err = -EFAULT;
279 break;
280
281 case PTP_SYS_OFFSET:
282 case PTP_SYS_OFFSET2:
283 sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
284 if (IS_ERR(sysoff)) {
285 err = PTR_ERR(sysoff);
286 sysoff = NULL;
287 break;
288 }
289 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
290 err = -EINVAL;
291 break;
292 }
293 pct = &sysoff->ts[0];
294 for (i = 0; i < sysoff->n_samples; i++) {
295 ktime_get_real_ts64(&ts);
296 pct->sec = ts.tv_sec;
297 pct->nsec = ts.tv_nsec;
298 pct++;
299 if (ops->gettimex64)
300 err = ops->gettimex64(ops, &ts, NULL);
301 else
302 err = ops->gettime64(ops, &ts);
303 if (err)
304 goto out;
305 pct->sec = ts.tv_sec;
306 pct->nsec = ts.tv_nsec;
307 pct++;
308 }
309 ktime_get_real_ts64(&ts);
310 pct->sec = ts.tv_sec;
311 pct->nsec = ts.tv_nsec;
312 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
313 err = -EFAULT;
314 break;
315
316 case PTP_PIN_GETFUNC:
317 case PTP_PIN_GETFUNC2:
318 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
319 err = -EFAULT;
320 break;
321 }
322 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
323 || pd.rsv[3] || pd.rsv[4])
324 && cmd == PTP_PIN_GETFUNC2) {
325 err = -EINVAL;
326 break;
327 } else if (cmd == PTP_PIN_GETFUNC) {
328 pd.rsv[0] = 0;
329 pd.rsv[1] = 0;
330 pd.rsv[2] = 0;
331 pd.rsv[3] = 0;
332 pd.rsv[4] = 0;
333 }
334 pin_index = pd.index;
335 if (pin_index >= ops->n_pins) {
336 err = -EINVAL;
337 break;
338 }
339 pin_index = array_index_nospec(pin_index, ops->n_pins);
340 if (mutex_lock_interruptible(&ptp->pincfg_mux))
341 return -ERESTARTSYS;
342 pd = ops->pin_config[pin_index];
343 mutex_unlock(&ptp->pincfg_mux);
344 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
345 err = -EFAULT;
346 break;
347
348 case PTP_PIN_SETFUNC:
349 case PTP_PIN_SETFUNC2:
350 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
351 err = -EFAULT;
352 break;
353 }
354 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
355 || pd.rsv[3] || pd.rsv[4])
356 && cmd == PTP_PIN_SETFUNC2) {
357 err = -EINVAL;
358 break;
359 } else if (cmd == PTP_PIN_SETFUNC) {
360 pd.rsv[0] = 0;
361 pd.rsv[1] = 0;
362 pd.rsv[2] = 0;
363 pd.rsv[3] = 0;
364 pd.rsv[4] = 0;
365 }
366 pin_index = pd.index;
367 if (pin_index >= ops->n_pins) {
368 err = -EINVAL;
369 break;
370 }
371 pin_index = array_index_nospec(pin_index, ops->n_pins);
372 if (mutex_lock_interruptible(&ptp->pincfg_mux))
373 return -ERESTARTSYS;
374 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
375 mutex_unlock(&ptp->pincfg_mux);
376 break;
377
378 default:
379 err = -ENOTTY;
380 break;
381 }
382
383out:
384 kfree(extoff);
385 kfree(sysoff);
386 return err;
387}
388
389__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
390{
391 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
392
393 poll_wait(fp, &ptp->tsev_wq, wait);
394
395 return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
396}
397
398#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
399
400ssize_t ptp_read(struct posix_clock *pc,
401 uint rdflags, char __user *buf, size_t cnt)
402{
403 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
404 struct timestamp_event_queue *queue = &ptp->tsevq;
405 struct ptp_extts_event *event;
406 unsigned long flags;
407 size_t qcnt, i;
408 int result;
409
410 if (cnt % sizeof(struct ptp_extts_event) != 0)
411 return -EINVAL;
412
413 if (cnt > EXTTS_BUFSIZE)
414 cnt = EXTTS_BUFSIZE;
415
416 cnt = cnt / sizeof(struct ptp_extts_event);
417
418 if (mutex_lock_interruptible(&ptp->tsevq_mux))
419 return -ERESTARTSYS;
420
421 if (wait_event_interruptible(ptp->tsev_wq,
422 ptp->defunct || queue_cnt(queue))) {
423 mutex_unlock(&ptp->tsevq_mux);
424 return -ERESTARTSYS;
425 }
426
427 if (ptp->defunct) {
428 mutex_unlock(&ptp->tsevq_mux);
429 return -ENODEV;
430 }
431
432 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
433 if (!event) {
434 mutex_unlock(&ptp->tsevq_mux);
435 return -ENOMEM;
436 }
437
438 spin_lock_irqsave(&queue->lock, flags);
439
440 qcnt = queue_cnt(queue);
441
442 if (cnt > qcnt)
443 cnt = qcnt;
444
445 for (i = 0; i < cnt; i++) {
446 event[i] = queue->buf[queue->head];
447 /* Paired with READ_ONCE() in queue_cnt() */
448 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
449 }
450
451 spin_unlock_irqrestore(&queue->lock, flags);
452
453 cnt = cnt * sizeof(struct ptp_extts_event);
454
455 mutex_unlock(&ptp->tsevq_mux);
456
457 result = cnt;
458 if (copy_to_user(buf, event, cnt))
459 result = -EFAULT;
460
461 kfree(event);
462 return result;
463}