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