ptp_chardev.c 10.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
4
5
6
7
8
9
10
/*
 * PTP 1588 clock support - character device implementation.
 *
 * Copyright (C) 2010 OMICRON electronics GmbH
 */
#include <linux/module.h>
#include <linux/posix-clock.h>
#include <linux/poll.h>
#include <linux/sched.h>
11
#include <linux/slab.h>
12
#include <linux/timekeeping.h>
13

14
15
#include <linux/nospec.h>

16
17
#include "ptp_private.h"

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
			       enum ptp_pin_function func, unsigned int chan)
{
	struct ptp_clock_request rq;
	int err = 0;

	memset(&rq, 0, sizeof(rq));

	switch (func) {
	case PTP_PF_NONE:
		break;
	case PTP_PF_EXTTS:
		rq.type = PTP_CLK_REQ_EXTTS;
		rq.extts.index = chan;
		err = ops->enable(ops, &rq, 0);
		break;
	case PTP_PF_PEROUT:
		rq.type = PTP_CLK_REQ_PEROUT;
		rq.perout.index = chan;
		err = ops->enable(ops, &rq, 0);
		break;
	case PTP_PF_PHYSYNC:
		break;
	default:
		return -EINVAL;
	}

	return err;
}

int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
		    enum ptp_pin_function func, unsigned int chan)
{
	struct ptp_clock_info *info = ptp->info;
	struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
	unsigned int i;

	/* Check to see if any other pin previously had this function. */
	for (i = 0; i < info->n_pins; i++) {
		if (info->pin_config[i].func == func &&
		    info->pin_config[i].chan == chan) {
			pin1 = &info->pin_config[i];
			break;
		}
	}
	if (pin1 && i == pin)
		return 0;

	/* Check the desired function and channel. */
	switch (func) {
	case PTP_PF_NONE:
		break;
	case PTP_PF_EXTTS:
		if (chan >= info->n_ext_ts)
			return -EINVAL;
		break;
	case PTP_PF_PEROUT:
		if (chan >= info->n_per_out)
			return -EINVAL;
		break;
	case PTP_PF_PHYSYNC:
79
80
		if (chan != 0)
			return -EINVAL;
81
		break;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	default:
		return -EINVAL;
	}

	if (info->verify(info, pin, func, chan)) {
		pr_err("driver cannot use function %u on pin %u\n", func, chan);
		return -EOPNOTSUPP;
	}

	/* Disable whatever function was previously assigned. */
	if (pin1) {
		ptp_disable_pinfunc(info, func, chan);
		pin1->func = PTP_PF_NONE;
		pin1->chan = 0;
	}
	ptp_disable_pinfunc(info, pin2->func, pin2->chan);
	pin2->func = func;
	pin2->chan = chan;

	return 0;
}

104
105
106
107
108
109
110
111
int ptp_open(struct posix_clock *pc, fmode_t fmode)
{
	return 0;
}

long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
{
	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
112
	struct ptp_sys_offset_extended *extoff = NULL;
113
114
	struct ptp_sys_offset_precise precise_offset;
	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
119
	struct ptp_clock_request req;
	struct ptp_clock_caps caps;
120
	struct ptp_clock_time *pct;
121
122
	unsigned int i, pin_index;
	struct ptp_pin_desc pd;
123
	struct timespec64 ts;
124
125
126
127
128
	int enable, err = 0;

	switch (cmd) {

	case PTP_CLOCK_GETCAPS:
129
	case PTP_CLOCK_GETCAPS2:
130
		memset(&caps, 0, sizeof(caps));
131

132
133
134
135
136
		caps.max_adj = ptp->info->max_adj;
		caps.n_alarm = ptp->info->n_alarm;
		caps.n_ext_ts = ptp->info->n_ext_ts;
		caps.n_per_out = ptp->info->n_per_out;
		caps.pps = ptp->info->pps;
137
		caps.n_pins = ptp->info->n_pins;
138
		caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
139
140
		if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
			err = -EFAULT;
141
142
143
		break;

	case PTP_EXTTS_REQUEST:
144
145
146
	case PTP_EXTTS_REQUEST2:
		memset(&req, 0, sizeof(req));

147
148
149
150
151
		if (copy_from_user(&req.extts, (void __user *)arg,
				   sizeof(req.extts))) {
			err = -EFAULT;
			break;
		}
152
		if (cmd == PTP_EXTTS_REQUEST2) {
153
154
			/* Tell the drivers to check the flags carefully. */
			req.extts.flags |= PTP_STRICT_FLAGS;
155
156
157
158
159
160
161
162
163
164
165
166
			/* Make sure no reserved bit is set. */
			if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
			    req.extts.rsv[0] || req.extts.rsv[1]) {
				err = -EINVAL;
				break;
			}
			/* Ensure one of the rising/falling edge bits is set. */
			if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
			    (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
				err = -EINVAL;
				break;
			}
167
		} else if (cmd == PTP_EXTTS_REQUEST) {
168
			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
169
170
171
			req.extts.rsv[0] = 0;
			req.extts.rsv[1] = 0;
		}
172
173
174
175
176
177
178
179
180
181
		if (req.extts.index >= ops->n_ext_ts) {
			err = -EINVAL;
			break;
		}
		req.type = PTP_CLK_REQ_EXTTS;
		enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
		err = ops->enable(ops, &req, enable);
		break;

	case PTP_PEROUT_REQUEST:
182
183
184
	case PTP_PEROUT_REQUEST2:
		memset(&req, 0, sizeof(req));

185
186
187
188
189
		if (copy_from_user(&req.perout, (void __user *)arg,
				   sizeof(req.perout))) {
			err = -EFAULT;
			break;
		}
190
191
192
193
194
195
196
		if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
			req.perout.rsv[0] || req.perout.rsv[1] ||
			req.perout.rsv[2] || req.perout.rsv[3]) &&
			cmd == PTP_PEROUT_REQUEST2) {
			err = -EINVAL;
			break;
		} else if (cmd == PTP_PEROUT_REQUEST) {
197
			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
198
199
200
201
202
			req.perout.rsv[0] = 0;
			req.perout.rsv[1] = 0;
			req.perout.rsv[2] = 0;
			req.perout.rsv[3] = 0;
		}
203
204
205
206
207
208
209
210
211
212
		if (req.perout.index >= ops->n_per_out) {
			err = -EINVAL;
			break;
		}
		req.type = PTP_CLK_REQ_PEROUT;
		enable = req.perout.period.sec || req.perout.period.nsec;
		err = ops->enable(ops, &req, enable);
		break;

	case PTP_ENABLE_PPS:
213
214
215
	case PTP_ENABLE_PPS2:
		memset(&req, 0, sizeof(req));

216
217
218
219
220
221
222
		if (!capable(CAP_SYS_TIME))
			return -EPERM;
		req.type = PTP_CLK_REQ_PPS;
		enable = arg ? 1 : 0;
		err = ops->enable(ops, &req, enable);
		break;

223
	case PTP_SYS_OFFSET_PRECISE:
224
	case PTP_SYS_OFFSET_PRECISE2:
225
226
227
228
229
230
231
232
		if (!ptp->info->getcrosststamp) {
			err = -EOPNOTSUPP;
			break;
		}
		err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
		if (err)
			break;

233
		memset(&precise_offset, 0, sizeof(precise_offset));
234
235
236
237
238
239
240
241
242
243
244
245
246
247
		ts = ktime_to_timespec64(xtstamp.device);
		precise_offset.device.sec = ts.tv_sec;
		precise_offset.device.nsec = ts.tv_nsec;
		ts = ktime_to_timespec64(xtstamp.sys_realtime);
		precise_offset.sys_realtime.sec = ts.tv_sec;
		precise_offset.sys_realtime.nsec = ts.tv_nsec;
		ts = ktime_to_timespec64(xtstamp.sys_monoraw);
		precise_offset.sys_monoraw.sec = ts.tv_sec;
		precise_offset.sys_monoraw.nsec = ts.tv_nsec;
		if (copy_to_user((void __user *)arg, &precise_offset,
				 sizeof(precise_offset)))
			err = -EFAULT;
		break;

248
	case PTP_SYS_OFFSET_EXTENDED:
249
	case PTP_SYS_OFFSET_EXTENDED2:
250
251
252
253
254
255
256
257
258
259
		if (!ptp->info->gettimex64) {
			err = -EOPNOTSUPP;
			break;
		}
		extoff = memdup_user((void __user *)arg, sizeof(*extoff));
		if (IS_ERR(extoff)) {
			err = PTR_ERR(extoff);
			extoff = NULL;
			break;
		}
260
261
		if (extoff->n_samples > PTP_MAX_SAMPLES
		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
			err = -EINVAL;
			break;
		}
		for (i = 0; i < extoff->n_samples; i++) {
			err = ptp->info->gettimex64(ptp->info, &ts, &sts);
			if (err)
				goto out;
			extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
			extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
			extoff->ts[i][1].sec = ts.tv_sec;
			extoff->ts[i][1].nsec = ts.tv_nsec;
			extoff->ts[i][2].sec = sts.post_ts.tv_sec;
			extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
		}
		if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
			err = -EFAULT;
		break;

280
	case PTP_SYS_OFFSET:
281
	case PTP_SYS_OFFSET2:
282
283
284
		sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
		if (IS_ERR(sysoff)) {
			err = PTR_ERR(sysoff);
Dan Carpenter's avatar
Dan Carpenter committed
285
			sysoff = NULL;
286
287
			break;
		}
288
		if (sysoff->n_samples > PTP_MAX_SAMPLES) {
289
290
291
			err = -EINVAL;
			break;
		}
292
293
		pct = &sysoff->ts[0];
		for (i = 0; i < sysoff->n_samples; i++) {
294
			ktime_get_real_ts64(&ts);
295
296
297
			pct->sec = ts.tv_sec;
			pct->nsec = ts.tv_nsec;
			pct++;
298
299
300
301
			if (ops->gettimex64)
				err = ops->gettimex64(ops, &ts, NULL);
			else
				err = ops->gettime64(ops, &ts);
302
303
			if (err)
				goto out;
304
305
306
307
			pct->sec = ts.tv_sec;
			pct->nsec = ts.tv_nsec;
			pct++;
		}
308
		ktime_get_real_ts64(&ts);
309
310
		pct->sec = ts.tv_sec;
		pct->nsec = ts.tv_nsec;
311
		if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
312
313
314
			err = -EFAULT;
		break;

315
	case PTP_PIN_GETFUNC:
316
	case PTP_PIN_GETFUNC2:
317
318
319
320
		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
			err = -EFAULT;
			break;
		}
321
322
323
324
325
326
327
328
329
330
331
332
		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
				|| pd.rsv[3] || pd.rsv[4])
			&& cmd == PTP_PIN_GETFUNC2) {
			err = -EINVAL;
			break;
		} else if (cmd == PTP_PIN_GETFUNC) {
			pd.rsv[0] = 0;
			pd.rsv[1] = 0;
			pd.rsv[2] = 0;
			pd.rsv[3] = 0;
			pd.rsv[4] = 0;
		}
333
334
335
336
337
		pin_index = pd.index;
		if (pin_index >= ops->n_pins) {
			err = -EINVAL;
			break;
		}
338
		pin_index = array_index_nospec(pin_index, ops->n_pins);
339
340
341
342
343
344
345
346
347
		if (mutex_lock_interruptible(&ptp->pincfg_mux))
			return -ERESTARTSYS;
		pd = ops->pin_config[pin_index];
		mutex_unlock(&ptp->pincfg_mux);
		if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
			err = -EFAULT;
		break;

	case PTP_PIN_SETFUNC:
348
	case PTP_PIN_SETFUNC2:
349
350
351
352
		if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
			err = -EFAULT;
			break;
		}
353
354
355
356
357
358
359
360
361
362
363
364
		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
				|| pd.rsv[3] || pd.rsv[4])
			&& cmd == PTP_PIN_SETFUNC2) {
			err = -EINVAL;
			break;
		} else if (cmd == PTP_PIN_SETFUNC) {
			pd.rsv[0] = 0;
			pd.rsv[1] = 0;
			pd.rsv[2] = 0;
			pd.rsv[3] = 0;
			pd.rsv[4] = 0;
		}
365
366
367
368
369
		pin_index = pd.index;
		if (pin_index >= ops->n_pins) {
			err = -EINVAL;
			break;
		}
370
		pin_index = array_index_nospec(pin_index, ops->n_pins);
371
372
373
374
375
376
		if (mutex_lock_interruptible(&ptp->pincfg_mux))
			return -ERESTARTSYS;
		err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
		mutex_unlock(&ptp->pincfg_mux);
		break;

377
378
379
380
	default:
		err = -ENOTTY;
		break;
	}
381

382
out:
383
	kfree(extoff);
384
	kfree(sysoff);
385
386
387
	return err;
}

388
__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
389
390
391
392
393
{
	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);

	poll_wait(fp, &ptp->tsev_wq, wait);

394
	return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
395
396
}

397
398
#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))

399
400
401
402
403
ssize_t ptp_read(struct posix_clock *pc,
		 uint rdflags, char __user *buf, size_t cnt)
{
	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
	struct timestamp_event_queue *queue = &ptp->tsevq;
404
	struct ptp_extts_event *event;
405
406
	unsigned long flags;
	size_t qcnt, i;
407
	int result;
408
409
410
411

	if (cnt % sizeof(struct ptp_extts_event) != 0)
		return -EINVAL;

412
413
	if (cnt > EXTTS_BUFSIZE)
		cnt = EXTTS_BUFSIZE;
414
415
416
417
418
419
420
421
422
423
424
425

	cnt = cnt / sizeof(struct ptp_extts_event);

	if (mutex_lock_interruptible(&ptp->tsevq_mux))
		return -ERESTARTSYS;

	if (wait_event_interruptible(ptp->tsev_wq,
				     ptp->defunct || queue_cnt(queue))) {
		mutex_unlock(&ptp->tsevq_mux);
		return -ERESTARTSYS;
	}

426
427
	if (ptp->defunct) {
		mutex_unlock(&ptp->tsevq_mux);
428
		return -ENODEV;
429
	}
430

431
432
433
434
435
436
	event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
	if (!event) {
		mutex_unlock(&ptp->tsevq_mux);
		return -ENOMEM;
	}

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
	spin_lock_irqsave(&queue->lock, flags);

	qcnt = queue_cnt(queue);

	if (cnt > qcnt)
		cnt = qcnt;

	for (i = 0; i < cnt; i++) {
		event[i] = queue->buf[queue->head];
		queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
	}

	spin_unlock_irqrestore(&queue->lock, flags);

	cnt = cnt * sizeof(struct ptp_extts_event);

	mutex_unlock(&ptp->tsevq_mux);

455
	result = cnt;
456
	if (copy_to_user(buf, event, cnt))
457
		result = -EFAULT;
458

459
460
	kfree(event);
	return result;
461
}