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