xref: /linux/drivers/ptp/ptp_chardev.c (revision f26b118031205135c23b43a311712fe8f34febf9)
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 #include <linux/debugfs.h>
14 
15 #include <linux/nospec.h>
16 
17 #include "ptp_private.h"
18 
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 
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 on pin %u\n", func, chan);
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_context *pccontext, fmode_t fmode)
106 {
107 	struct ptp_clock *ptp =
108 		container_of(pccontext->clk, struct ptp_clock, clock);
109 	struct timestamp_event_queue *queue;
110 	char debugfsname[32];
111 
112 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
113 	if (!queue)
114 		return -EINVAL;
115 	queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL);
116 	if (!queue->mask) {
117 		kfree(queue);
118 		return -EINVAL;
119 	}
120 	bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS);
121 	spin_lock_init(&queue->lock);
122 	list_add_tail(&queue->qlist, &ptp->tsevqs);
123 	pccontext->private_clkdata = queue;
124 
125 	/* Debugfs contents */
126 	sprintf(debugfsname, "0x%p", queue);
127 	queue->debugfs_instance =
128 		debugfs_create_dir(debugfsname, ptp->debugfs_root);
129 	queue->dfs_bitmap.array = (u32 *)queue->mask;
130 	queue->dfs_bitmap.n_elements =
131 		DIV_ROUND_UP(PTP_MAX_CHANNELS, BITS_PER_BYTE * sizeof(u32));
132 	debugfs_create_u32_array("mask", 0444, queue->debugfs_instance,
133 				 &queue->dfs_bitmap);
134 
135 	return 0;
136 }
137 
138 int ptp_release(struct posix_clock_context *pccontext)
139 {
140 	struct timestamp_event_queue *queue = pccontext->private_clkdata;
141 	unsigned long flags;
142 
143 	if (queue) {
144 		debugfs_remove(queue->debugfs_instance);
145 		pccontext->private_clkdata = NULL;
146 		spin_lock_irqsave(&queue->lock, flags);
147 		list_del(&queue->qlist);
148 		spin_unlock_irqrestore(&queue->lock, flags);
149 		bitmap_free(queue->mask);
150 		kfree(queue);
151 	}
152 	return 0;
153 }
154 
155 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
156 	       unsigned long arg)
157 {
158 	struct ptp_clock *ptp =
159 		container_of(pccontext->clk, struct ptp_clock, clock);
160 	struct ptp_sys_offset_extended *extoff = NULL;
161 	struct ptp_sys_offset_precise precise_offset;
162 	struct system_device_crosststamp xtstamp;
163 	struct ptp_clock_info *ops = ptp->info;
164 	struct ptp_sys_offset *sysoff = NULL;
165 	struct timestamp_event_queue *tsevq;
166 	struct ptp_system_timestamp sts;
167 	struct ptp_clock_request req;
168 	struct ptp_clock_caps caps;
169 	struct ptp_clock_time *pct;
170 	unsigned int i, pin_index;
171 	struct ptp_pin_desc pd;
172 	struct timespec64 ts;
173 	int enable, err = 0;
174 
175 	tsevq = pccontext->private_clkdata;
176 
177 	switch (cmd) {
178 
179 	case PTP_CLOCK_GETCAPS:
180 	case PTP_CLOCK_GETCAPS2:
181 		memset(&caps, 0, sizeof(caps));
182 
183 		caps.max_adj = ptp->info->max_adj;
184 		caps.n_alarm = ptp->info->n_alarm;
185 		caps.n_ext_ts = ptp->info->n_ext_ts;
186 		caps.n_per_out = ptp->info->n_per_out;
187 		caps.pps = ptp->info->pps;
188 		caps.n_pins = ptp->info->n_pins;
189 		caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
190 		caps.adjust_phase = ptp->info->adjphase != NULL &&
191 				    ptp->info->getmaxphase != NULL;
192 		if (caps.adjust_phase)
193 			caps.max_phase_adj = ptp->info->getmaxphase(ptp->info);
194 		if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
195 			err = -EFAULT;
196 		break;
197 
198 	case PTP_EXTTS_REQUEST:
199 	case PTP_EXTTS_REQUEST2:
200 		memset(&req, 0, sizeof(req));
201 
202 		if (copy_from_user(&req.extts, (void __user *)arg,
203 				   sizeof(req.extts))) {
204 			err = -EFAULT;
205 			break;
206 		}
207 		if (cmd == PTP_EXTTS_REQUEST2) {
208 			/* Tell the drivers to check the flags carefully. */
209 			req.extts.flags |= PTP_STRICT_FLAGS;
210 			/* Make sure no reserved bit is set. */
211 			if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
212 			    req.extts.rsv[0] || req.extts.rsv[1]) {
213 				err = -EINVAL;
214 				break;
215 			}
216 			/* Ensure one of the rising/falling edge bits is set. */
217 			if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
218 			    (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
219 				err = -EINVAL;
220 				break;
221 			}
222 		} else if (cmd == PTP_EXTTS_REQUEST) {
223 			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
224 			req.extts.rsv[0] = 0;
225 			req.extts.rsv[1] = 0;
226 		}
227 		if (req.extts.index >= ops->n_ext_ts) {
228 			err = -EINVAL;
229 			break;
230 		}
231 		req.type = PTP_CLK_REQ_EXTTS;
232 		enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
233 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
234 			return -ERESTARTSYS;
235 		err = ops->enable(ops, &req, enable);
236 		mutex_unlock(&ptp->pincfg_mux);
237 		break;
238 
239 	case PTP_PEROUT_REQUEST:
240 	case PTP_PEROUT_REQUEST2:
241 		memset(&req, 0, sizeof(req));
242 
243 		if (copy_from_user(&req.perout, (void __user *)arg,
244 				   sizeof(req.perout))) {
245 			err = -EFAULT;
246 			break;
247 		}
248 		if (cmd == PTP_PEROUT_REQUEST2) {
249 			struct ptp_perout_request *perout = &req.perout;
250 
251 			if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) {
252 				err = -EINVAL;
253 				break;
254 			}
255 			/*
256 			 * The "on" field has undefined meaning if
257 			 * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat
258 			 * it as reserved, which must be set to zero.
259 			 */
260 			if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
261 			    (perout->rsv[0] || perout->rsv[1] ||
262 			     perout->rsv[2] || perout->rsv[3])) {
263 				err = -EINVAL;
264 				break;
265 			}
266 			if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
267 				/* The duty cycle must be subunitary. */
268 				if (perout->on.sec > perout->period.sec ||
269 				    (perout->on.sec == perout->period.sec &&
270 				     perout->on.nsec > perout->period.nsec)) {
271 					err = -ERANGE;
272 					break;
273 				}
274 			}
275 			if (perout->flags & PTP_PEROUT_PHASE) {
276 				/*
277 				 * The phase should be specified modulo the
278 				 * period, therefore anything equal or larger
279 				 * than 1 period is invalid.
280 				 */
281 				if (perout->phase.sec > perout->period.sec ||
282 				    (perout->phase.sec == perout->period.sec &&
283 				     perout->phase.nsec >= perout->period.nsec)) {
284 					err = -ERANGE;
285 					break;
286 				}
287 			}
288 		} else if (cmd == PTP_PEROUT_REQUEST) {
289 			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
290 			req.perout.rsv[0] = 0;
291 			req.perout.rsv[1] = 0;
292 			req.perout.rsv[2] = 0;
293 			req.perout.rsv[3] = 0;
294 		}
295 		if (req.perout.index >= ops->n_per_out) {
296 			err = -EINVAL;
297 			break;
298 		}
299 		req.type = PTP_CLK_REQ_PEROUT;
300 		enable = req.perout.period.sec || req.perout.period.nsec;
301 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
302 			return -ERESTARTSYS;
303 		err = ops->enable(ops, &req, enable);
304 		mutex_unlock(&ptp->pincfg_mux);
305 		break;
306 
307 	case PTP_ENABLE_PPS:
308 	case PTP_ENABLE_PPS2:
309 		memset(&req, 0, sizeof(req));
310 
311 		if (!capable(CAP_SYS_TIME))
312 			return -EPERM;
313 		req.type = PTP_CLK_REQ_PPS;
314 		enable = arg ? 1 : 0;
315 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
316 			return -ERESTARTSYS;
317 		err = ops->enable(ops, &req, enable);
318 		mutex_unlock(&ptp->pincfg_mux);
319 		break;
320 
321 	case PTP_SYS_OFFSET_PRECISE:
322 	case PTP_SYS_OFFSET_PRECISE2:
323 		if (!ptp->info->getcrosststamp) {
324 			err = -EOPNOTSUPP;
325 			break;
326 		}
327 		err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
328 		if (err)
329 			break;
330 
331 		memset(&precise_offset, 0, sizeof(precise_offset));
332 		ts = ktime_to_timespec64(xtstamp.device);
333 		precise_offset.device.sec = ts.tv_sec;
334 		precise_offset.device.nsec = ts.tv_nsec;
335 		ts = ktime_to_timespec64(xtstamp.sys_realtime);
336 		precise_offset.sys_realtime.sec = ts.tv_sec;
337 		precise_offset.sys_realtime.nsec = ts.tv_nsec;
338 		ts = ktime_to_timespec64(xtstamp.sys_monoraw);
339 		precise_offset.sys_monoraw.sec = ts.tv_sec;
340 		precise_offset.sys_monoraw.nsec = ts.tv_nsec;
341 		if (copy_to_user((void __user *)arg, &precise_offset,
342 				 sizeof(precise_offset)))
343 			err = -EFAULT;
344 		break;
345 
346 	case PTP_SYS_OFFSET_EXTENDED:
347 	case PTP_SYS_OFFSET_EXTENDED2:
348 		if (!ptp->info->gettimex64) {
349 			err = -EOPNOTSUPP;
350 			break;
351 		}
352 		extoff = memdup_user((void __user *)arg, sizeof(*extoff));
353 		if (IS_ERR(extoff)) {
354 			err = PTR_ERR(extoff);
355 			extoff = NULL;
356 			break;
357 		}
358 		if (extoff->n_samples > PTP_MAX_SAMPLES
359 		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
360 			err = -EINVAL;
361 			break;
362 		}
363 		for (i = 0; i < extoff->n_samples; i++) {
364 			err = ptp->info->gettimex64(ptp->info, &ts, &sts);
365 			if (err)
366 				goto out;
367 			extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
368 			extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
369 			extoff->ts[i][1].sec = ts.tv_sec;
370 			extoff->ts[i][1].nsec = ts.tv_nsec;
371 			extoff->ts[i][2].sec = sts.post_ts.tv_sec;
372 			extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
373 		}
374 		if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
375 			err = -EFAULT;
376 		break;
377 
378 	case PTP_SYS_OFFSET:
379 	case PTP_SYS_OFFSET2:
380 		sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
381 		if (IS_ERR(sysoff)) {
382 			err = PTR_ERR(sysoff);
383 			sysoff = NULL;
384 			break;
385 		}
386 		if (sysoff->n_samples > PTP_MAX_SAMPLES) {
387 			err = -EINVAL;
388 			break;
389 		}
390 		pct = &sysoff->ts[0];
391 		for (i = 0; i < sysoff->n_samples; i++) {
392 			ktime_get_real_ts64(&ts);
393 			pct->sec = ts.tv_sec;
394 			pct->nsec = ts.tv_nsec;
395 			pct++;
396 			if (ops->gettimex64)
397 				err = ops->gettimex64(ops, &ts, NULL);
398 			else
399 				err = ops->gettime64(ops, &ts);
400 			if (err)
401 				goto out;
402 			pct->sec = ts.tv_sec;
403 			pct->nsec = ts.tv_nsec;
404 			pct++;
405 		}
406 		ktime_get_real_ts64(&ts);
407 		pct->sec = ts.tv_sec;
408 		pct->nsec = ts.tv_nsec;
409 		if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
410 			err = -EFAULT;
411 		break;
412 
413 	case PTP_PIN_GETFUNC:
414 	case PTP_PIN_GETFUNC2:
415 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
416 			err = -EFAULT;
417 			break;
418 		}
419 		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
420 				|| pd.rsv[3] || pd.rsv[4])
421 			&& cmd == PTP_PIN_GETFUNC2) {
422 			err = -EINVAL;
423 			break;
424 		} else if (cmd == PTP_PIN_GETFUNC) {
425 			pd.rsv[0] = 0;
426 			pd.rsv[1] = 0;
427 			pd.rsv[2] = 0;
428 			pd.rsv[3] = 0;
429 			pd.rsv[4] = 0;
430 		}
431 		pin_index = pd.index;
432 		if (pin_index >= ops->n_pins) {
433 			err = -EINVAL;
434 			break;
435 		}
436 		pin_index = array_index_nospec(pin_index, ops->n_pins);
437 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
438 			return -ERESTARTSYS;
439 		pd = ops->pin_config[pin_index];
440 		mutex_unlock(&ptp->pincfg_mux);
441 		if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
442 			err = -EFAULT;
443 		break;
444 
445 	case PTP_PIN_SETFUNC:
446 	case PTP_PIN_SETFUNC2:
447 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
448 			err = -EFAULT;
449 			break;
450 		}
451 		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
452 				|| pd.rsv[3] || pd.rsv[4])
453 			&& cmd == PTP_PIN_SETFUNC2) {
454 			err = -EINVAL;
455 			break;
456 		} else if (cmd == PTP_PIN_SETFUNC) {
457 			pd.rsv[0] = 0;
458 			pd.rsv[1] = 0;
459 			pd.rsv[2] = 0;
460 			pd.rsv[3] = 0;
461 			pd.rsv[4] = 0;
462 		}
463 		pin_index = pd.index;
464 		if (pin_index >= ops->n_pins) {
465 			err = -EINVAL;
466 			break;
467 		}
468 		pin_index = array_index_nospec(pin_index, ops->n_pins);
469 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
470 			return -ERESTARTSYS;
471 		err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
472 		mutex_unlock(&ptp->pincfg_mux);
473 		break;
474 
475 	case PTP_MASK_CLEAR_ALL:
476 		bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS);
477 		break;
478 
479 	case PTP_MASK_EN_SINGLE:
480 		if (copy_from_user(&i, (void __user *)arg, sizeof(i))) {
481 			err = -EFAULT;
482 			break;
483 		}
484 		if (i >= PTP_MAX_CHANNELS) {
485 			err = -EFAULT;
486 			break;
487 		}
488 		set_bit(i, tsevq->mask);
489 		break;
490 
491 	default:
492 		err = -ENOTTY;
493 		break;
494 	}
495 
496 out:
497 	kfree(extoff);
498 	kfree(sysoff);
499 	return err;
500 }
501 
502 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp,
503 		  poll_table *wait)
504 {
505 	struct ptp_clock *ptp =
506 		container_of(pccontext->clk, struct ptp_clock, clock);
507 	struct timestamp_event_queue *queue;
508 
509 	queue = pccontext->private_clkdata;
510 	if (!queue)
511 		return EPOLLERR;
512 
513 	poll_wait(fp, &ptp->tsev_wq, wait);
514 
515 	return queue_cnt(queue) ? EPOLLIN : 0;
516 }
517 
518 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
519 
520 ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,
521 		 char __user *buf, size_t cnt)
522 {
523 	struct ptp_clock *ptp =
524 		container_of(pccontext->clk, struct ptp_clock, clock);
525 	struct timestamp_event_queue *queue;
526 	struct ptp_extts_event *event;
527 	unsigned long flags;
528 	size_t qcnt, i;
529 	int result;
530 
531 	queue = pccontext->private_clkdata;
532 	if (!queue) {
533 		result = -EINVAL;
534 		goto exit;
535 	}
536 
537 	if (cnt % sizeof(struct ptp_extts_event) != 0) {
538 		result = -EINVAL;
539 		goto exit;
540 	}
541 
542 	if (cnt > EXTTS_BUFSIZE)
543 		cnt = EXTTS_BUFSIZE;
544 
545 	cnt = cnt / sizeof(struct ptp_extts_event);
546 
547 	if (wait_event_interruptible(ptp->tsev_wq,
548 				     ptp->defunct || queue_cnt(queue))) {
549 		return -ERESTARTSYS;
550 	}
551 
552 	if (ptp->defunct) {
553 		result = -ENODEV;
554 		goto exit;
555 	}
556 
557 	event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
558 	if (!event) {
559 		result = -ENOMEM;
560 		goto exit;
561 	}
562 
563 	spin_lock_irqsave(&queue->lock, flags);
564 
565 	qcnt = queue_cnt(queue);
566 
567 	if (cnt > qcnt)
568 		cnt = qcnt;
569 
570 	for (i = 0; i < cnt; i++) {
571 		event[i] = queue->buf[queue->head];
572 		queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
573 	}
574 
575 	spin_unlock_irqrestore(&queue->lock, flags);
576 
577 	cnt = cnt * sizeof(struct ptp_extts_event);
578 
579 	result = cnt;
580 	if (copy_to_user(buf, event, cnt)) {
581 		result = -EFAULT;
582 		goto free_event;
583 	}
584 
585 free_event:
586 	kfree(event);
587 exit:
588 	if (result < 0)
589 		ptp_release(pccontext);
590 	return result;
591 }
592