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				    ptp->info->getmaxphase != NULL;
141		if (caps.adjust_phase)
142			caps.max_phase_adj = ptp->info->getmaxphase(ptp->info);
143		if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
144			err = -EFAULT;
145		break;
146
147	case PTP_EXTTS_REQUEST:
148	case PTP_EXTTS_REQUEST2:
149		memset(&req, 0, sizeof(req));
150
151		if (copy_from_user(&req.extts, (void __user *)arg,
152				   sizeof(req.extts))) {
153			err = -EFAULT;
154			break;
155		}
156		if (cmd == PTP_EXTTS_REQUEST2) {
157			/* Tell the drivers to check the flags carefully. */
158			req.extts.flags |= PTP_STRICT_FLAGS;
159			/* Make sure no reserved bit is set. */
160			if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
161			    req.extts.rsv[0] || req.extts.rsv[1]) {
162				err = -EINVAL;
163				break;
164			}
165			/* Ensure one of the rising/falling edge bits is set. */
166			if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
167			    (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
168				err = -EINVAL;
169				break;
170			}
171		} else if (cmd == PTP_EXTTS_REQUEST) {
172			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
173			req.extts.rsv[0] = 0;
174			req.extts.rsv[1] = 0;
175		}
176		if (req.extts.index >= ops->n_ext_ts) {
177			err = -EINVAL;
178			break;
179		}
180		req.type = PTP_CLK_REQ_EXTTS;
181		enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
182		if (mutex_lock_interruptible(&ptp->pincfg_mux))
183			return -ERESTARTSYS;
184		err = ops->enable(ops, &req, enable);
185		mutex_unlock(&ptp->pincfg_mux);
186		break;
187
188	case PTP_PEROUT_REQUEST:
189	case PTP_PEROUT_REQUEST2:
190		memset(&req, 0, sizeof(req));
191
192		if (copy_from_user(&req.perout, (void __user *)arg,
193				   sizeof(req.perout))) {
194			err = -EFAULT;
195			break;
196		}
197		if (cmd == PTP_PEROUT_REQUEST2) {
198			struct ptp_perout_request *perout = &req.perout;
199
200			if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) {
201				err = -EINVAL;
202				break;
203			}
204			/*
205			 * The "on" field has undefined meaning if
206			 * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat
207			 * it as reserved, which must be set to zero.
208			 */
209			if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
210			    (perout->rsv[0] || perout->rsv[1] ||
211			     perout->rsv[2] || perout->rsv[3])) {
212				err = -EINVAL;
213				break;
214			}
215			if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
216				/* The duty cycle must be subunitary. */
217				if (perout->on.sec > perout->period.sec ||
218				    (perout->on.sec == perout->period.sec &&
219				     perout->on.nsec > perout->period.nsec)) {
220					err = -ERANGE;
221					break;
222				}
223			}
224			if (perout->flags & PTP_PEROUT_PHASE) {
225				/*
226				 * The phase should be specified modulo the
227				 * period, therefore anything equal or larger
228				 * than 1 period is invalid.
229				 */
230				if (perout->phase.sec > perout->period.sec ||
231				    (perout->phase.sec == perout->period.sec &&
232				     perout->phase.nsec >= perout->period.nsec)) {
233					err = -ERANGE;
234					break;
235				}
236			}
237		} else if (cmd == PTP_PEROUT_REQUEST) {
238			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
239			req.perout.rsv[0] = 0;
240			req.perout.rsv[1] = 0;
241			req.perout.rsv[2] = 0;
242			req.perout.rsv[3] = 0;
243		}
244		if (req.perout.index >= ops->n_per_out) {
245			err = -EINVAL;
246			break;
247		}
248		req.type = PTP_CLK_REQ_PEROUT;
249		enable = req.perout.period.sec || req.perout.period.nsec;
250		if (mutex_lock_interruptible(&ptp->pincfg_mux))
251			return -ERESTARTSYS;
252		err = ops->enable(ops, &req, enable);
253		mutex_unlock(&ptp->pincfg_mux);
254		break;
255
256	case PTP_ENABLE_PPS:
257	case PTP_ENABLE_PPS2:
258		memset(&req, 0, sizeof(req));
259
260		if (!capable(CAP_SYS_TIME))
261			return -EPERM;
262		req.type = PTP_CLK_REQ_PPS;
263		enable = arg ? 1 : 0;
264		if (mutex_lock_interruptible(&ptp->pincfg_mux))
265			return -ERESTARTSYS;
266		err = ops->enable(ops, &req, enable);
267		mutex_unlock(&ptp->pincfg_mux);
268		break;
269
270	case PTP_SYS_OFFSET_PRECISE:
271	case PTP_SYS_OFFSET_PRECISE2:
272		if (!ptp->info->getcrosststamp) {
273			err = -EOPNOTSUPP;
274			break;
275		}
276		err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
277		if (err)
278			break;
279
280		memset(&precise_offset, 0, sizeof(precise_offset));
281		ts = ktime_to_timespec64(xtstamp.device);
282		precise_offset.device.sec = ts.tv_sec;
283		precise_offset.device.nsec = ts.tv_nsec;
284		ts = ktime_to_timespec64(xtstamp.sys_realtime);
285		precise_offset.sys_realtime.sec = ts.tv_sec;
286		precise_offset.sys_realtime.nsec = ts.tv_nsec;
287		ts = ktime_to_timespec64(xtstamp.sys_monoraw);
288		precise_offset.sys_monoraw.sec = ts.tv_sec;
289		precise_offset.sys_monoraw.nsec = ts.tv_nsec;
290		if (copy_to_user((void __user *)arg, &precise_offset,
291				 sizeof(precise_offset)))
292			err = -EFAULT;
293		break;
294
295	case PTP_SYS_OFFSET_EXTENDED:
296	case PTP_SYS_OFFSET_EXTENDED2:
297		if (!ptp->info->gettimex64) {
298			err = -EOPNOTSUPP;
299			break;
300		}
301		extoff = memdup_user((void __user *)arg, sizeof(*extoff));
302		if (IS_ERR(extoff)) {
303			err = PTR_ERR(extoff);
304			extoff = NULL;
305			break;
306		}
307		if (extoff->n_samples > PTP_MAX_SAMPLES
308		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
309			err = -EINVAL;
310			break;
311		}
312		for (i = 0; i < extoff->n_samples; i++) {
313			err = ptp->info->gettimex64(ptp->info, &ts, &sts);
314			if (err)
315				goto out;
316			extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
317			extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
318			extoff->ts[i][1].sec = ts.tv_sec;
319			extoff->ts[i][1].nsec = ts.tv_nsec;
320			extoff->ts[i][2].sec = sts.post_ts.tv_sec;
321			extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
322		}
323		if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
324			err = -EFAULT;
325		break;
326
327	case PTP_SYS_OFFSET:
328	case PTP_SYS_OFFSET2:
329		sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
330		if (IS_ERR(sysoff)) {
331			err = PTR_ERR(sysoff);
332			sysoff = NULL;
333			break;
334		}
335		if (sysoff->n_samples > PTP_MAX_SAMPLES) {
336			err = -EINVAL;
337			break;
338		}
339		pct = &sysoff->ts[0];
340		for (i = 0; i < sysoff->n_samples; i++) {
341			ktime_get_real_ts64(&ts);
342			pct->sec = ts.tv_sec;
343			pct->nsec = ts.tv_nsec;
344			pct++;
345			if (ops->gettimex64)
346				err = ops->gettimex64(ops, &ts, NULL);
347			else
348				err = ops->gettime64(ops, &ts);
349			if (err)
350				goto out;
351			pct->sec = ts.tv_sec;
352			pct->nsec = ts.tv_nsec;
353			pct++;
354		}
355		ktime_get_real_ts64(&ts);
356		pct->sec = ts.tv_sec;
357		pct->nsec = ts.tv_nsec;
358		if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
359			err = -EFAULT;
360		break;
361
362	case PTP_PIN_GETFUNC:
363	case PTP_PIN_GETFUNC2:
364		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
365			err = -EFAULT;
366			break;
367		}
368		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
369				|| pd.rsv[3] || pd.rsv[4])
370			&& cmd == PTP_PIN_GETFUNC2) {
371			err = -EINVAL;
372			break;
373		} else if (cmd == PTP_PIN_GETFUNC) {
374			pd.rsv[0] = 0;
375			pd.rsv[1] = 0;
376			pd.rsv[2] = 0;
377			pd.rsv[3] = 0;
378			pd.rsv[4] = 0;
379		}
380		pin_index = pd.index;
381		if (pin_index >= ops->n_pins) {
382			err = -EINVAL;
383			break;
384		}
385		pin_index = array_index_nospec(pin_index, ops->n_pins);
386		if (mutex_lock_interruptible(&ptp->pincfg_mux))
387			return -ERESTARTSYS;
388		pd = ops->pin_config[pin_index];
389		mutex_unlock(&ptp->pincfg_mux);
390		if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
391			err = -EFAULT;
392		break;
393
394	case PTP_PIN_SETFUNC:
395	case PTP_PIN_SETFUNC2:
396		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
397			err = -EFAULT;
398			break;
399		}
400		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
401				|| pd.rsv[3] || pd.rsv[4])
402			&& cmd == PTP_PIN_SETFUNC2) {
403			err = -EINVAL;
404			break;
405		} else if (cmd == PTP_PIN_SETFUNC) {
406			pd.rsv[0] = 0;
407			pd.rsv[1] = 0;
408			pd.rsv[2] = 0;
409			pd.rsv[3] = 0;
410			pd.rsv[4] = 0;
411		}
412		pin_index = pd.index;
413		if (pin_index >= ops->n_pins) {
414			err = -EINVAL;
415			break;
416		}
417		pin_index = array_index_nospec(pin_index, ops->n_pins);
418		if (mutex_lock_interruptible(&ptp->pincfg_mux))
419			return -ERESTARTSYS;
420		err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
421		mutex_unlock(&ptp->pincfg_mux);
422		break;
423
424	default:
425		err = -ENOTTY;
426		break;
427	}
428
429out:
430	kfree(extoff);
431	kfree(sysoff);
432	return err;
433}
434
435__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
436{
437	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
438
439	poll_wait(fp, &ptp->tsev_wq, wait);
440
441	return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
442}
443
444#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
445
446ssize_t ptp_read(struct posix_clock *pc,
447		 uint rdflags, char __user *buf, size_t cnt)
448{
449	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
450	struct timestamp_event_queue *queue = &ptp->tsevq;
451	struct ptp_extts_event *event;
452	unsigned long flags;
453	size_t qcnt, i;
454	int result;
455
456	if (cnt % sizeof(struct ptp_extts_event) != 0)
457		return -EINVAL;
458
459	if (cnt > EXTTS_BUFSIZE)
460		cnt = EXTTS_BUFSIZE;
461
462	cnt = cnt / sizeof(struct ptp_extts_event);
463
464	if (mutex_lock_interruptible(&ptp->tsevq_mux))
465		return -ERESTARTSYS;
466
467	if (wait_event_interruptible(ptp->tsev_wq,
468				     ptp->defunct || queue_cnt(queue))) {
469		mutex_unlock(&ptp->tsevq_mux);
470		return -ERESTARTSYS;
471	}
472
473	if (ptp->defunct) {
474		mutex_unlock(&ptp->tsevq_mux);
475		return -ENODEV;
476	}
477
478	event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
479	if (!event) {
480		mutex_unlock(&ptp->tsevq_mux);
481		return -ENOMEM;
482	}
483
484	spin_lock_irqsave(&queue->lock, flags);
485
486	qcnt = queue_cnt(queue);
487
488	if (cnt > qcnt)
489		cnt = qcnt;
490
491	for (i = 0; i < cnt; i++) {
492		event[i] = queue->buf[queue->head];
493		/* Paired with READ_ONCE() in queue_cnt() */
494		WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
495	}
496
497	spin_unlock_irqrestore(&queue->lock, flags);
498
499	cnt = cnt * sizeof(struct ptp_extts_event);
500
501	mutex_unlock(&ptp->tsevq_mux);
502
503	result = cnt;
504	if (copy_to_user(buf, event, cnt))
505		result = -EFAULT;
506
507	kfree(event);
508	return result;
509}
510