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