xref: /openbmc/linux/drivers/ptp/ptp_chardev.c (revision e657c18a)
1 /*
2  * PTP 1588 clock support - character device implementation.
3  *
4  * Copyright (C) 2010 OMICRON electronics GmbH
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #include <linux/module.h>
21 #include <linux/posix-clock.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/timekeeping.h>
26 
27 #include <linux/nospec.h>
28 
29 #include "ptp_private.h"
30 
31 static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
32 			       enum ptp_pin_function func, unsigned int chan)
33 {
34 	struct ptp_clock_request rq;
35 	int err = 0;
36 
37 	memset(&rq, 0, sizeof(rq));
38 
39 	switch (func) {
40 	case PTP_PF_NONE:
41 		break;
42 	case PTP_PF_EXTTS:
43 		rq.type = PTP_CLK_REQ_EXTTS;
44 		rq.extts.index = chan;
45 		err = ops->enable(ops, &rq, 0);
46 		break;
47 	case PTP_PF_PEROUT:
48 		rq.type = PTP_CLK_REQ_PEROUT;
49 		rq.perout.index = chan;
50 		err = ops->enable(ops, &rq, 0);
51 		break;
52 	case PTP_PF_PHYSYNC:
53 		break;
54 	default:
55 		return -EINVAL;
56 	}
57 
58 	return err;
59 }
60 
61 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
62 		    enum ptp_pin_function func, unsigned int chan)
63 {
64 	struct ptp_clock_info *info = ptp->info;
65 	struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
66 	unsigned int i;
67 
68 	/* Check to see if any other pin previously had this function. */
69 	for (i = 0; i < info->n_pins; i++) {
70 		if (info->pin_config[i].func == func &&
71 		    info->pin_config[i].chan == chan) {
72 			pin1 = &info->pin_config[i];
73 			break;
74 		}
75 	}
76 	if (pin1 && i == pin)
77 		return 0;
78 
79 	/* Check the desired function and channel. */
80 	switch (func) {
81 	case PTP_PF_NONE:
82 		break;
83 	case PTP_PF_EXTTS:
84 		if (chan >= info->n_ext_ts)
85 			return -EINVAL;
86 		break;
87 	case PTP_PF_PEROUT:
88 		if (chan >= info->n_per_out)
89 			return -EINVAL;
90 		break;
91 	case PTP_PF_PHYSYNC:
92 		if (chan != 0)
93 			return -EINVAL;
94 		break;
95 	default:
96 		return -EINVAL;
97 	}
98 
99 	if (info->verify(info, pin, func, chan)) {
100 		pr_err("driver cannot use function %u on pin %u\n", func, chan);
101 		return -EOPNOTSUPP;
102 	}
103 
104 	/* Disable whatever function was previously assigned. */
105 	if (pin1) {
106 		ptp_disable_pinfunc(info, func, chan);
107 		pin1->func = PTP_PF_NONE;
108 		pin1->chan = 0;
109 	}
110 	ptp_disable_pinfunc(info, pin2->func, pin2->chan);
111 	pin2->func = func;
112 	pin2->chan = chan;
113 
114 	return 0;
115 }
116 
117 int ptp_open(struct posix_clock *pc, fmode_t fmode)
118 {
119 	return 0;
120 }
121 
122 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
123 {
124 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
125 	struct ptp_sys_offset_extended *extoff = NULL;
126 	struct ptp_sys_offset_precise precise_offset;
127 	struct system_device_crosststamp xtstamp;
128 	struct ptp_clock_info *ops = ptp->info;
129 	struct ptp_sys_offset *sysoff = NULL;
130 	struct ptp_system_timestamp sts;
131 	struct ptp_clock_request req;
132 	struct ptp_clock_caps caps;
133 	struct ptp_clock_time *pct;
134 	unsigned int i, pin_index;
135 	struct ptp_pin_desc pd;
136 	struct timespec64 ts;
137 	int enable, err = 0;
138 
139 	switch (cmd) {
140 
141 	case PTP_CLOCK_GETCAPS:
142 		memset(&caps, 0, sizeof(caps));
143 		caps.max_adj = ptp->info->max_adj;
144 		caps.n_alarm = ptp->info->n_alarm;
145 		caps.n_ext_ts = ptp->info->n_ext_ts;
146 		caps.n_per_out = ptp->info->n_per_out;
147 		caps.pps = ptp->info->pps;
148 		caps.n_pins = ptp->info->n_pins;
149 		caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
150 		if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
151 			err = -EFAULT;
152 		break;
153 
154 	case PTP_EXTTS_REQUEST:
155 		if (copy_from_user(&req.extts, (void __user *)arg,
156 				   sizeof(req.extts))) {
157 			err = -EFAULT;
158 			break;
159 		}
160 		if (req.extts.index >= ops->n_ext_ts) {
161 			err = -EINVAL;
162 			break;
163 		}
164 		req.type = PTP_CLK_REQ_EXTTS;
165 		enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
166 		err = ops->enable(ops, &req, enable);
167 		break;
168 
169 	case PTP_PEROUT_REQUEST:
170 		if (copy_from_user(&req.perout, (void __user *)arg,
171 				   sizeof(req.perout))) {
172 			err = -EFAULT;
173 			break;
174 		}
175 		if (req.perout.index >= ops->n_per_out) {
176 			err = -EINVAL;
177 			break;
178 		}
179 		req.type = PTP_CLK_REQ_PEROUT;
180 		enable = req.perout.period.sec || req.perout.period.nsec;
181 		err = ops->enable(ops, &req, enable);
182 		break;
183 
184 	case PTP_ENABLE_PPS:
185 		if (!capable(CAP_SYS_TIME))
186 			return -EPERM;
187 		req.type = PTP_CLK_REQ_PPS;
188 		enable = arg ? 1 : 0;
189 		err = ops->enable(ops, &req, enable);
190 		break;
191 
192 	case PTP_SYS_OFFSET_PRECISE:
193 		if (!ptp->info->getcrosststamp) {
194 			err = -EOPNOTSUPP;
195 			break;
196 		}
197 		err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
198 		if (err)
199 			break;
200 
201 		memset(&precise_offset, 0, sizeof(precise_offset));
202 		ts = ktime_to_timespec64(xtstamp.device);
203 		precise_offset.device.sec = ts.tv_sec;
204 		precise_offset.device.nsec = ts.tv_nsec;
205 		ts = ktime_to_timespec64(xtstamp.sys_realtime);
206 		precise_offset.sys_realtime.sec = ts.tv_sec;
207 		precise_offset.sys_realtime.nsec = ts.tv_nsec;
208 		ts = ktime_to_timespec64(xtstamp.sys_monoraw);
209 		precise_offset.sys_monoraw.sec = ts.tv_sec;
210 		precise_offset.sys_monoraw.nsec = ts.tv_nsec;
211 		if (copy_to_user((void __user *)arg, &precise_offset,
212 				 sizeof(precise_offset)))
213 			err = -EFAULT;
214 		break;
215 
216 	case PTP_SYS_OFFSET_EXTENDED:
217 		if (!ptp->info->gettimex64) {
218 			err = -EOPNOTSUPP;
219 			break;
220 		}
221 		extoff = memdup_user((void __user *)arg, sizeof(*extoff));
222 		if (IS_ERR(extoff)) {
223 			err = PTR_ERR(extoff);
224 			extoff = NULL;
225 			break;
226 		}
227 		if (extoff->n_samples > PTP_MAX_SAMPLES
228 		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
229 			err = -EINVAL;
230 			break;
231 		}
232 		for (i = 0; i < extoff->n_samples; i++) {
233 			err = ptp->info->gettimex64(ptp->info, &ts, &sts);
234 			if (err)
235 				goto out;
236 			extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
237 			extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
238 			extoff->ts[i][1].sec = ts.tv_sec;
239 			extoff->ts[i][1].nsec = ts.tv_nsec;
240 			extoff->ts[i][2].sec = sts.post_ts.tv_sec;
241 			extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
242 		}
243 		if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
244 			err = -EFAULT;
245 		break;
246 
247 	case PTP_SYS_OFFSET:
248 		sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
249 		if (IS_ERR(sysoff)) {
250 			err = PTR_ERR(sysoff);
251 			sysoff = NULL;
252 			break;
253 		}
254 		if (sysoff->n_samples > PTP_MAX_SAMPLES) {
255 			err = -EINVAL;
256 			break;
257 		}
258 		pct = &sysoff->ts[0];
259 		for (i = 0; i < sysoff->n_samples; i++) {
260 			ktime_get_real_ts64(&ts);
261 			pct->sec = ts.tv_sec;
262 			pct->nsec = ts.tv_nsec;
263 			pct++;
264 			if (ops->gettimex64)
265 				err = ops->gettimex64(ops, &ts, NULL);
266 			else
267 				err = ops->gettime64(ops, &ts);
268 			if (err)
269 				goto out;
270 			pct->sec = ts.tv_sec;
271 			pct->nsec = ts.tv_nsec;
272 			pct++;
273 		}
274 		ktime_get_real_ts64(&ts);
275 		pct->sec = ts.tv_sec;
276 		pct->nsec = ts.tv_nsec;
277 		if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
278 			err = -EFAULT;
279 		break;
280 
281 	case PTP_PIN_GETFUNC:
282 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
283 			err = -EFAULT;
284 			break;
285 		}
286 		pin_index = pd.index;
287 		if (pin_index >= ops->n_pins) {
288 			err = -EINVAL;
289 			break;
290 		}
291 		pin_index = array_index_nospec(pin_index, ops->n_pins);
292 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
293 			return -ERESTARTSYS;
294 		pd = ops->pin_config[pin_index];
295 		mutex_unlock(&ptp->pincfg_mux);
296 		if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
297 			err = -EFAULT;
298 		break;
299 
300 	case PTP_PIN_SETFUNC:
301 		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
302 			err = -EFAULT;
303 			break;
304 		}
305 		pin_index = pd.index;
306 		if (pin_index >= ops->n_pins) {
307 			err = -EINVAL;
308 			break;
309 		}
310 		pin_index = array_index_nospec(pin_index, ops->n_pins);
311 		if (mutex_lock_interruptible(&ptp->pincfg_mux))
312 			return -ERESTARTSYS;
313 		err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
314 		mutex_unlock(&ptp->pincfg_mux);
315 		break;
316 
317 	default:
318 		err = -ENOTTY;
319 		break;
320 	}
321 
322 out:
323 	kfree(extoff);
324 	kfree(sysoff);
325 	return err;
326 }
327 
328 __poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
329 {
330 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
331 
332 	poll_wait(fp, &ptp->tsev_wq, wait);
333 
334 	return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
335 }
336 
337 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
338 
339 ssize_t ptp_read(struct posix_clock *pc,
340 		 uint rdflags, char __user *buf, size_t cnt)
341 {
342 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
343 	struct timestamp_event_queue *queue = &ptp->tsevq;
344 	struct ptp_extts_event *event;
345 	unsigned long flags;
346 	size_t qcnt, i;
347 	int result;
348 
349 	if (cnt % sizeof(struct ptp_extts_event) != 0)
350 		return -EINVAL;
351 
352 	if (cnt > EXTTS_BUFSIZE)
353 		cnt = EXTTS_BUFSIZE;
354 
355 	cnt = cnt / sizeof(struct ptp_extts_event);
356 
357 	if (mutex_lock_interruptible(&ptp->tsevq_mux))
358 		return -ERESTARTSYS;
359 
360 	if (wait_event_interruptible(ptp->tsev_wq,
361 				     ptp->defunct || queue_cnt(queue))) {
362 		mutex_unlock(&ptp->tsevq_mux);
363 		return -ERESTARTSYS;
364 	}
365 
366 	if (ptp->defunct) {
367 		mutex_unlock(&ptp->tsevq_mux);
368 		return -ENODEV;
369 	}
370 
371 	event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
372 	if (!event) {
373 		mutex_unlock(&ptp->tsevq_mux);
374 		return -ENOMEM;
375 	}
376 
377 	spin_lock_irqsave(&queue->lock, flags);
378 
379 	qcnt = queue_cnt(queue);
380 
381 	if (cnt > qcnt)
382 		cnt = qcnt;
383 
384 	for (i = 0; i < cnt; i++) {
385 		event[i] = queue->buf[queue->head];
386 		queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
387 	}
388 
389 	spin_unlock_irqrestore(&queue->lock, flags);
390 
391 	cnt = cnt * sizeof(struct ptp_extts_event);
392 
393 	mutex_unlock(&ptp->tsevq_mux);
394 
395 	result = cnt;
396 	if (copy_to_user(buf, event, cnt))
397 		result = -EFAULT;
398 
399 	kfree(event);
400 	return result;
401 }
402