Linux Audio

Check our new training course

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