xref: /openbmc/linux/drivers/ptp/ptp_chardev.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
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/compat.h>
8 #include <linux/module.h>
9 #include <linux/posix-clock.h>
10 #include <linux/poll.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/timekeeping.h>
14 
15 #include <linux/nospec.h>
16 
17 #include "ptp_private.h"
18 
ptp_disable_pinfunc(struct ptp_clock_info * ops,enum ptp_pin_function func,unsigned int chan)19 static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
20 			       enum ptp_pin_function func, unsigned int chan)
21 {
22 	struct ptp_clock_request rq;
23 	int err = 0;
24 
25 	memset(&rq, 0, sizeof(rq));
26 
27 	switch (func) {
28 	case PTP_PF_NONE:
29 		break;
30 	case PTP_PF_EXTTS:
31 		rq.type = PTP_CLK_REQ_EXTTS;
32 		rq.extts.index = chan;
33 		err = ops->enable(ops, &rq, 0);
34 		break;
35 	case PTP_PF_PEROUT:
36 		rq.type = PTP_CLK_REQ_PEROUT;
37 		rq.perout.index = chan;
38 		err = ops->enable(ops, &rq, 0);
39 		break;
40 	case PTP_PF_PHYSYNC:
41 		break;
42 	default:
43 		return -EINVAL;
44 	}
45 
46 	return err;
47 }
48 
ptp_set_pinfunc(struct ptp_clock * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)49 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
50 		    enum ptp_pin_function func, unsigned int chan)
51 {
52 	struct ptp_clock_info *info = ptp->info;
53 	struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
54 	unsigned int i;
55 
56 	/* Check to see if any other pin previously had this function. */
57 	for (i = 0; i < info->n_pins; i++) {
58 		if (info->pin_config[i].func == func &&
59 		    info->pin_config[i].chan == chan) {
60 			pin1 = &info->pin_config[i];
61 			break;
62 		}
63 	}
64 	if (pin1 && i == pin)
65 		return 0;
66 
67 	/* Check the desired function and channel. */
68 	switch (func) {
69 	case PTP_PF_NONE:
70 		break;
71 	case PTP_PF_EXTTS:
72 		if (chan >= info->n_ext_ts)
73 			return -EINVAL;
74 		break;
75 	case PTP_PF_PEROUT:
76 		if (chan >= info->n_per_out)
77 			return -EINVAL;
78 		break;
79 	case PTP_PF_PHYSYNC:
80 		if (chan != 0)
81 			return -EINVAL;
82 		break;
83 	default:
84 		return -EINVAL;
85 	}
86 
87 	if (info->verify(info, pin, func, chan)) {
88 		pr_err("driver cannot use function %u and channel %u on pin %u\n",
89 		       func, chan, pin);
90 		return -EOPNOTSUPP;
91 	}
92 
93 	/* Disable whatever function was previously assigned. */
94 	if (pin1) {
95 		ptp_disable_pinfunc(info, func, chan);
96 		pin1->func = PTP_PF_NONE;
97 		pin1->chan = 0;
98 	}
99 	ptp_disable_pinfunc(info, pin2->func, pin2->chan);
100 	pin2->func = func;
101 	pin2->chan = chan;
102 
103 	return 0;
104 }
105 
ptp_open(struct posix_clock * pc,fmode_t fmode)106 int ptp_open(struct posix_clock *pc, fmode_t fmode)
107 {
108 	return 0;
109 }
110 
ptp_ioctl(struct posix_clock * pc,unsigned int cmd,unsigned long arg)111 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
112 {
113 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
114 	struct ptp_sys_offset_extended *extoff = NULL;
115 	struct ptp_sys_offset_precise precise_offset;
116 	struct system_device_crosststamp xtstamp;
117 	struct ptp_clock_info *ops = ptp->info;
118 	struct ptp_sys_offset *sysoff = NULL;
119 	struct ptp_system_timestamp sts;
120 	struct ptp_clock_request req;
121 	struct ptp_clock_caps caps;
122 	struct ptp_clock_time *pct;
123 	unsigned int i, pin_index;
124 	struct ptp_pin_desc pd;
125 	struct timespec64 ts;
126 	int enable, err = 0;
127 
128 	if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2)
129 		arg = (unsigned long)compat_ptr(arg);
130 
131 	switch (cmd) {
132 
133 	case PTP_CLOCK_GETCAPS:
134 	case PTP_CLOCK_GETCAPS2:
135 		memset(&caps, 0, sizeof(caps));
136 
137 		caps.max_adj = ptp->info->max_adj;
138 		caps.n_alarm = ptp->info->n_alarm;
139 		caps.n_ext_ts = ptp->info->n_ext_ts;
140 		caps.n_per_out = ptp->info->n_per_out;
141 		caps.pps = ptp->info->pps;
142 		caps.n_pins = ptp->info->n_pins;
143 		caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
144 		caps.adjust_phase = ptp->info->adjphase != NULL &&
145 				    ptp->info->getmaxphase != NULL;
146 		if (caps.adjust_phase)
147 			caps.max_phase_adj = ptp->info->getmaxphase(ptp->info);
148 		if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
149 			err = -EFAULT;
150 		break;
151 
152 	case PTP_EXTTS_REQUEST:
153 	case PTP_EXTTS_REQUEST2:
154 		memset(&req, 0, sizeof(req));
155 
156 		if (copy_from_user(&req.extts, (void __user *)arg,
157 				   sizeof(req.extts))) {
158 			err = -EFAULT;
159 			break;
160 		}
161 		if (cmd == PTP_EXTTS_REQUEST2) {
162 			/* Tell the drivers to check the flags carefully. */
163 			req.extts.flags |= PTP_STRICT_FLAGS;
164 			/* Make sure no reserved bit is set. */
165 			if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
166 			    req.extts.rsv[0] || req.extts.rsv[1]) {
167 				err = -EINVAL;
168 				break;
169 			}
170 			/* Ensure one of the rising/falling edge bits is set. */
171 			if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
172 			    (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
173 				err = -EINVAL;
174 				break;
175 			}
176 		} else if (cmd == PTP_EXTTS_REQUEST) {
177 			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
178 			req.extts.rsv[0] = 0;
179 			req.extts.rsv[1] = 0;
180 		}
181 		if (req.extts.index >= ops->n_ext_ts) {
182 			err = -EINVAL;
183 			break;
184 		}
185 		req.type = PTP_CLK_REQ_EXTTS;
186 		enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
187 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
188 			return -ERESTARTSYS;
189 		err = ops->enable(ops, &req, enable);
190 		mutex_unlock(&ptp->pincfg_mux);
191 		break;
192 
193 	case PTP_PEROUT_REQUEST:
194 	case PTP_PEROUT_REQUEST2:
195 		memset(&req, 0, sizeof(req));
196 
197 		if (copy_from_user(&req.perout, (void __user *)arg,
198 				   sizeof(req.perout))) {
199 			err = -EFAULT;
200 			break;
201 		}
202 		if (cmd == PTP_PEROUT_REQUEST2) {
203 			struct ptp_perout_request *perout = &req.perout;
204 
205 			if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) {
206 				err = -EINVAL;
207 				break;
208 			}
209 			/*
210 			 * The "on" field has undefined meaning if
211 			 * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat
212 			 * it as reserved, which must be set to zero.
213 			 */
214 			if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
215 			    (perout->rsv[0] || perout->rsv[1] ||
216 			     perout->rsv[2] || perout->rsv[3])) {
217 				err = -EINVAL;
218 				break;
219 			}
220 			if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
221 				/* The duty cycle must be subunitary. */
222 				if (perout->on.sec > perout->period.sec ||
223 				    (perout->on.sec == perout->period.sec &&
224 				     perout->on.nsec > perout->period.nsec)) {
225 					err = -ERANGE;
226 					break;
227 				}
228 			}
229 			if (perout->flags & PTP_PEROUT_PHASE) {
230 				/*
231 				 * The phase should be specified modulo the
232 				 * period, therefore anything equal or larger
233 				 * than 1 period is invalid.
234 				 */
235 				if (perout->phase.sec > perout->period.sec ||
236 				    (perout->phase.sec == perout->period.sec &&
237 				     perout->phase.nsec >= perout->period.nsec)) {
238 					err = -ERANGE;
239 					break;
240 				}
241 			}
242 		} else if (cmd == PTP_PEROUT_REQUEST) {
243 			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
244 			req.perout.rsv[0] = 0;
245 			req.perout.rsv[1] = 0;
246 			req.perout.rsv[2] = 0;
247 			req.perout.rsv[3] = 0;
248 		}
249 		if (req.perout.index >= ops->n_per_out) {
250 			err = -EINVAL;
251 			break;
252 		}
253 		req.type = PTP_CLK_REQ_PEROUT;
254 		enable = req.perout.period.sec || req.perout.period.nsec;
255 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
256 			return -ERESTARTSYS;
257 		err = ops->enable(ops, &req, enable);
258 		mutex_unlock(&ptp->pincfg_mux);
259 		break;
260 
261 	case PTP_ENABLE_PPS:
262 	case PTP_ENABLE_PPS2:
263 		memset(&req, 0, sizeof(req));
264 
265 		if (!capable(CAP_SYS_TIME))
266 			return -EPERM;
267 		req.type = PTP_CLK_REQ_PPS;
268 		enable = arg ? 1 : 0;
269 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
270 			return -ERESTARTSYS;
271 		err = ops->enable(ops, &req, enable);
272 		mutex_unlock(&ptp->pincfg_mux);
273 		break;
274 
275 	case PTP_SYS_OFFSET_PRECISE:
276 	case PTP_SYS_OFFSET_PRECISE2:
277 		if (!ptp->info->getcrosststamp) {
278 			err = -EOPNOTSUPP;
279 			break;
280 		}
281 		err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
282 		if (err)
283 			break;
284 
285 		memset(&precise_offset, 0, sizeof(precise_offset));
286 		ts = ktime_to_timespec64(xtstamp.device);
287 		precise_offset.device.sec = ts.tv_sec;
288 		precise_offset.device.nsec = ts.tv_nsec;
289 		ts = ktime_to_timespec64(xtstamp.sys_realtime);
290 		precise_offset.sys_realtime.sec = ts.tv_sec;
291 		precise_offset.sys_realtime.nsec = ts.tv_nsec;
292 		ts = ktime_to_timespec64(xtstamp.sys_monoraw);
293 		precise_offset.sys_monoraw.sec = ts.tv_sec;
294 		precise_offset.sys_monoraw.nsec = ts.tv_nsec;
295 		if (copy_to_user((void __user *)arg, &precise_offset,
296 				 sizeof(precise_offset)))
297 			err = -EFAULT;
298 		break;
299 
300 	case PTP_SYS_OFFSET_EXTENDED:
301 	case PTP_SYS_OFFSET_EXTENDED2:
302 		if (!ptp->info->gettimex64) {
303 			err = -EOPNOTSUPP;
304 			break;
305 		}
306 		extoff = memdup_user((void __user *)arg, sizeof(*extoff));
307 		if (IS_ERR(extoff)) {
308 			err = PTR_ERR(extoff);
309 			extoff = NULL;
310 			break;
311 		}
312 		if (extoff->n_samples > PTP_MAX_SAMPLES
313 		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
314 			err = -EINVAL;
315 			break;
316 		}
317 		for (i = 0; i < extoff->n_samples; i++) {
318 			err = ptp->info->gettimex64(ptp->info, &ts, &sts);
319 			if (err)
320 				goto out;
321 			extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
322 			extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
323 			extoff->ts[i][1].sec = ts.tv_sec;
324 			extoff->ts[i][1].nsec = ts.tv_nsec;
325 			extoff->ts[i][2].sec = sts.post_ts.tv_sec;
326 			extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
327 		}
328 		if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
329 			err = -EFAULT;
330 		break;
331 
332 	case PTP_SYS_OFFSET:
333 	case PTP_SYS_OFFSET2:
334 		sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
335 		if (IS_ERR(sysoff)) {
336 			err = PTR_ERR(sysoff);
337 			sysoff = NULL;
338 			break;
339 		}
340 		if (sysoff->n_samples > PTP_MAX_SAMPLES) {
341 			err = -EINVAL;
342 			break;
343 		}
344 		pct = &sysoff->ts[0];
345 		for (i = 0; i < sysoff->n_samples; i++) {
346 			ktime_get_real_ts64(&ts);
347 			pct->sec = ts.tv_sec;
348 			pct->nsec = ts.tv_nsec;
349 			pct++;
350 			if (ops->gettimex64)
351 				err = ops->gettimex64(ops, &ts, NULL);
352 			else
353 				err = ops->gettime64(ops, &ts);
354 			if (err)
355 				goto out;
356 			pct->sec = ts.tv_sec;
357 			pct->nsec = ts.tv_nsec;
358 			pct++;
359 		}
360 		ktime_get_real_ts64(&ts);
361 		pct->sec = ts.tv_sec;
362 		pct->nsec = ts.tv_nsec;
363 		if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
364 			err = -EFAULT;
365 		break;
366 
367 	case PTP_PIN_GETFUNC:
368 	case PTP_PIN_GETFUNC2:
369 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
370 			err = -EFAULT;
371 			break;
372 		}
373 		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
374 				|| pd.rsv[3] || pd.rsv[4])
375 			&& cmd == PTP_PIN_GETFUNC2) {
376 			err = -EINVAL;
377 			break;
378 		} else if (cmd == PTP_PIN_GETFUNC) {
379 			pd.rsv[0] = 0;
380 			pd.rsv[1] = 0;
381 			pd.rsv[2] = 0;
382 			pd.rsv[3] = 0;
383 			pd.rsv[4] = 0;
384 		}
385 		pin_index = pd.index;
386 		if (pin_index >= ops->n_pins) {
387 			err = -EINVAL;
388 			break;
389 		}
390 		pin_index = array_index_nospec(pin_index, ops->n_pins);
391 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
392 			return -ERESTARTSYS;
393 		pd = ops->pin_config[pin_index];
394 		mutex_unlock(&ptp->pincfg_mux);
395 		if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
396 			err = -EFAULT;
397 		break;
398 
399 	case PTP_PIN_SETFUNC:
400 	case PTP_PIN_SETFUNC2:
401 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
402 			err = -EFAULT;
403 			break;
404 		}
405 		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
406 				|| pd.rsv[3] || pd.rsv[4])
407 			&& cmd == PTP_PIN_SETFUNC2) {
408 			err = -EINVAL;
409 			break;
410 		} else if (cmd == PTP_PIN_SETFUNC) {
411 			pd.rsv[0] = 0;
412 			pd.rsv[1] = 0;
413 			pd.rsv[2] = 0;
414 			pd.rsv[3] = 0;
415 			pd.rsv[4] = 0;
416 		}
417 		pin_index = pd.index;
418 		if (pin_index >= ops->n_pins) {
419 			err = -EINVAL;
420 			break;
421 		}
422 		pin_index = array_index_nospec(pin_index, ops->n_pins);
423 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
424 			return -ERESTARTSYS;
425 		err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
426 		mutex_unlock(&ptp->pincfg_mux);
427 		break;
428 
429 	default:
430 		err = -ENOTTY;
431 		break;
432 	}
433 
434 out:
435 	kfree(extoff);
436 	kfree(sysoff);
437 	return err;
438 }
439 
ptp_poll(struct posix_clock * pc,struct file * fp,poll_table * wait)440 __poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
441 {
442 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
443 
444 	poll_wait(fp, &ptp->tsev_wq, wait);
445 
446 	return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
447 }
448 
449 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
450 
ptp_read(struct posix_clock * pc,uint rdflags,char __user * buf,size_t cnt)451 ssize_t ptp_read(struct posix_clock *pc,
452 		 uint rdflags, char __user *buf, size_t cnt)
453 {
454 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
455 	struct timestamp_event_queue *queue = &ptp->tsevq;
456 	struct ptp_extts_event *event;
457 	unsigned long flags;
458 	size_t qcnt, i;
459 	int result;
460 
461 	if (cnt % sizeof(struct ptp_extts_event) != 0)
462 		return -EINVAL;
463 
464 	if (cnt > EXTTS_BUFSIZE)
465 		cnt = EXTTS_BUFSIZE;
466 
467 	cnt = cnt / sizeof(struct ptp_extts_event);
468 
469 	if (mutex_lock_interruptible(&ptp->tsevq_mux))
470 		return -ERESTARTSYS;
471 
472 	if (wait_event_interruptible(ptp->tsev_wq,
473 				     ptp->defunct || queue_cnt(queue))) {
474 		mutex_unlock(&ptp->tsevq_mux);
475 		return -ERESTARTSYS;
476 	}
477 
478 	if (ptp->defunct) {
479 		mutex_unlock(&ptp->tsevq_mux);
480 		return -ENODEV;
481 	}
482 
483 	event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
484 	if (!event) {
485 		mutex_unlock(&ptp->tsevq_mux);
486 		return -ENOMEM;
487 	}
488 
489 	spin_lock_irqsave(&queue->lock, flags);
490 
491 	qcnt = queue_cnt(queue);
492 
493 	if (cnt > qcnt)
494 		cnt = qcnt;
495 
496 	for (i = 0; i < cnt; i++) {
497 		event[i] = queue->buf[queue->head];
498 		/* Paired with READ_ONCE() in queue_cnt() */
499 		WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
500 	}
501 
502 	spin_unlock_irqrestore(&queue->lock, flags);
503 
504 	cnt = cnt * sizeof(struct ptp_extts_event);
505 
506 	mutex_unlock(&ptp->tsevq_mux);
507 
508 	result = cnt;
509 	if (copy_to_user(buf, event, cnt))
510 		result = -EFAULT;
511 
512 	kfree(event);
513 	return result;
514 }
515