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