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