xref: /linux/drivers/ptp/ptp_chardev.c (revision c942fddf8793b2013be8c901b47d0a8dc02bf99f)
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 		memset(&caps, 0, sizeof(caps));
130 		caps.max_adj = ptp->info->max_adj;
131 		caps.n_alarm = ptp->info->n_alarm;
132 		caps.n_ext_ts = ptp->info->n_ext_ts;
133 		caps.n_per_out = ptp->info->n_per_out;
134 		caps.pps = ptp->info->pps;
135 		caps.n_pins = ptp->info->n_pins;
136 		caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
137 		if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
138 			err = -EFAULT;
139 		break;
140 
141 	case PTP_EXTTS_REQUEST:
142 		if (copy_from_user(&req.extts, (void __user *)arg,
143 				   sizeof(req.extts))) {
144 			err = -EFAULT;
145 			break;
146 		}
147 		if (req.extts.index >= ops->n_ext_ts) {
148 			err = -EINVAL;
149 			break;
150 		}
151 		req.type = PTP_CLK_REQ_EXTTS;
152 		enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
153 		err = ops->enable(ops, &req, enable);
154 		break;
155 
156 	case PTP_PEROUT_REQUEST:
157 		if (copy_from_user(&req.perout, (void __user *)arg,
158 				   sizeof(req.perout))) {
159 			err = -EFAULT;
160 			break;
161 		}
162 		if (req.perout.index >= ops->n_per_out) {
163 			err = -EINVAL;
164 			break;
165 		}
166 		req.type = PTP_CLK_REQ_PEROUT;
167 		enable = req.perout.period.sec || req.perout.period.nsec;
168 		err = ops->enable(ops, &req, enable);
169 		break;
170 
171 	case PTP_ENABLE_PPS:
172 		if (!capable(CAP_SYS_TIME))
173 			return -EPERM;
174 		req.type = PTP_CLK_REQ_PPS;
175 		enable = arg ? 1 : 0;
176 		err = ops->enable(ops, &req, enable);
177 		break;
178 
179 	case PTP_SYS_OFFSET_PRECISE:
180 		if (!ptp->info->getcrosststamp) {
181 			err = -EOPNOTSUPP;
182 			break;
183 		}
184 		err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
185 		if (err)
186 			break;
187 
188 		memset(&precise_offset, 0, sizeof(precise_offset));
189 		ts = ktime_to_timespec64(xtstamp.device);
190 		precise_offset.device.sec = ts.tv_sec;
191 		precise_offset.device.nsec = ts.tv_nsec;
192 		ts = ktime_to_timespec64(xtstamp.sys_realtime);
193 		precise_offset.sys_realtime.sec = ts.tv_sec;
194 		precise_offset.sys_realtime.nsec = ts.tv_nsec;
195 		ts = ktime_to_timespec64(xtstamp.sys_monoraw);
196 		precise_offset.sys_monoraw.sec = ts.tv_sec;
197 		precise_offset.sys_monoraw.nsec = ts.tv_nsec;
198 		if (copy_to_user((void __user *)arg, &precise_offset,
199 				 sizeof(precise_offset)))
200 			err = -EFAULT;
201 		break;
202 
203 	case PTP_SYS_OFFSET_EXTENDED:
204 		if (!ptp->info->gettimex64) {
205 			err = -EOPNOTSUPP;
206 			break;
207 		}
208 		extoff = memdup_user((void __user *)arg, sizeof(*extoff));
209 		if (IS_ERR(extoff)) {
210 			err = PTR_ERR(extoff);
211 			extoff = NULL;
212 			break;
213 		}
214 		if (extoff->n_samples > PTP_MAX_SAMPLES
215 		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
216 			err = -EINVAL;
217 			break;
218 		}
219 		for (i = 0; i < extoff->n_samples; i++) {
220 			err = ptp->info->gettimex64(ptp->info, &ts, &sts);
221 			if (err)
222 				goto out;
223 			extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
224 			extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
225 			extoff->ts[i][1].sec = ts.tv_sec;
226 			extoff->ts[i][1].nsec = ts.tv_nsec;
227 			extoff->ts[i][2].sec = sts.post_ts.tv_sec;
228 			extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
229 		}
230 		if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
231 			err = -EFAULT;
232 		break;
233 
234 	case PTP_SYS_OFFSET:
235 		sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
236 		if (IS_ERR(sysoff)) {
237 			err = PTR_ERR(sysoff);
238 			sysoff = NULL;
239 			break;
240 		}
241 		if (sysoff->n_samples > PTP_MAX_SAMPLES) {
242 			err = -EINVAL;
243 			break;
244 		}
245 		pct = &sysoff->ts[0];
246 		for (i = 0; i < sysoff->n_samples; i++) {
247 			ktime_get_real_ts64(&ts);
248 			pct->sec = ts.tv_sec;
249 			pct->nsec = ts.tv_nsec;
250 			pct++;
251 			if (ops->gettimex64)
252 				err = ops->gettimex64(ops, &ts, NULL);
253 			else
254 				err = ops->gettime64(ops, &ts);
255 			if (err)
256 				goto out;
257 			pct->sec = ts.tv_sec;
258 			pct->nsec = ts.tv_nsec;
259 			pct++;
260 		}
261 		ktime_get_real_ts64(&ts);
262 		pct->sec = ts.tv_sec;
263 		pct->nsec = ts.tv_nsec;
264 		if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
265 			err = -EFAULT;
266 		break;
267 
268 	case PTP_PIN_GETFUNC:
269 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
270 			err = -EFAULT;
271 			break;
272 		}
273 		pin_index = pd.index;
274 		if (pin_index >= ops->n_pins) {
275 			err = -EINVAL;
276 			break;
277 		}
278 		pin_index = array_index_nospec(pin_index, ops->n_pins);
279 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
280 			return -ERESTARTSYS;
281 		pd = ops->pin_config[pin_index];
282 		mutex_unlock(&ptp->pincfg_mux);
283 		if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
284 			err = -EFAULT;
285 		break;
286 
287 	case PTP_PIN_SETFUNC:
288 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
289 			err = -EFAULT;
290 			break;
291 		}
292 		pin_index = pd.index;
293 		if (pin_index >= ops->n_pins) {
294 			err = -EINVAL;
295 			break;
296 		}
297 		pin_index = array_index_nospec(pin_index, ops->n_pins);
298 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
299 			return -ERESTARTSYS;
300 		err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
301 		mutex_unlock(&ptp->pincfg_mux);
302 		break;
303 
304 	default:
305 		err = -ENOTTY;
306 		break;
307 	}
308 
309 out:
310 	kfree(extoff);
311 	kfree(sysoff);
312 	return err;
313 }
314 
315 __poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
316 {
317 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
318 
319 	poll_wait(fp, &ptp->tsev_wq, wait);
320 
321 	return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
322 }
323 
324 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
325 
326 ssize_t ptp_read(struct posix_clock *pc,
327 		 uint rdflags, char __user *buf, size_t cnt)
328 {
329 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
330 	struct timestamp_event_queue *queue = &ptp->tsevq;
331 	struct ptp_extts_event *event;
332 	unsigned long flags;
333 	size_t qcnt, i;
334 	int result;
335 
336 	if (cnt % sizeof(struct ptp_extts_event) != 0)
337 		return -EINVAL;
338 
339 	if (cnt > EXTTS_BUFSIZE)
340 		cnt = EXTTS_BUFSIZE;
341 
342 	cnt = cnt / sizeof(struct ptp_extts_event);
343 
344 	if (mutex_lock_interruptible(&ptp->tsevq_mux))
345 		return -ERESTARTSYS;
346 
347 	if (wait_event_interruptible(ptp->tsev_wq,
348 				     ptp->defunct || queue_cnt(queue))) {
349 		mutex_unlock(&ptp->tsevq_mux);
350 		return -ERESTARTSYS;
351 	}
352 
353 	if (ptp->defunct) {
354 		mutex_unlock(&ptp->tsevq_mux);
355 		return -ENODEV;
356 	}
357 
358 	event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
359 	if (!event) {
360 		mutex_unlock(&ptp->tsevq_mux);
361 		return -ENOMEM;
362 	}
363 
364 	spin_lock_irqsave(&queue->lock, flags);
365 
366 	qcnt = queue_cnt(queue);
367 
368 	if (cnt > qcnt)
369 		cnt = qcnt;
370 
371 	for (i = 0; i < cnt; i++) {
372 		event[i] = queue->buf[queue->head];
373 		queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
374 	}
375 
376 	spin_unlock_irqrestore(&queue->lock, flags);
377 
378 	cnt = cnt * sizeof(struct ptp_extts_event);
379 
380 	mutex_unlock(&ptp->tsevq_mux);
381 
382 	result = cnt;
383 	if (copy_to_user(buf, event, cnt))
384 		result = -EFAULT;
385 
386 	kfree(event);
387 	return result;
388 }
389