xref: /linux/drivers/ptp/ptp_sysfs.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PTP 1588 clock support - sysfs interface.
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  * Copyright 2021 NXP
7  */
8 #include <linux/capability.h>
9 #include <linux/slab.h>
10 
11 #include "ptp_private.h"
12 
13 static ssize_t clock_name_show(struct device *dev,
14 			       struct device_attribute *attr, char *page)
15 {
16 	struct ptp_clock *ptp = dev_get_drvdata(dev);
17 	return sysfs_emit(page, "%s\n", ptp->info->name);
18 }
19 static DEVICE_ATTR_RO(clock_name);
20 
21 static ssize_t max_phase_adjustment_show(struct device *dev,
22 					 struct device_attribute *attr,
23 					 char *page)
24 {
25 	struct ptp_clock *ptp = dev_get_drvdata(dev);
26 
27 	return sysfs_emit(page, "%d\n", ptp->info->getmaxphase(ptp->info));
28 }
29 static DEVICE_ATTR_RO(max_phase_adjustment);
30 
31 #define PTP_SHOW_INT(name, var)						\
32 static ssize_t var##_show(struct device *dev,				\
33 			   struct device_attribute *attr, char *page)	\
34 {									\
35 	struct ptp_clock *ptp = dev_get_drvdata(dev);			\
36 	return sysfs_emit(page, "%d\n", ptp->info->var);	\
37 }									\
38 static DEVICE_ATTR(name, 0444, var##_show, NULL);
39 
40 PTP_SHOW_INT(max_adjustment, max_adj);
41 PTP_SHOW_INT(n_alarms, n_alarm);
42 PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
43 PTP_SHOW_INT(n_periodic_outputs, n_per_out);
44 PTP_SHOW_INT(n_programmable_pins, n_pins);
45 PTP_SHOW_INT(pps_available, pps);
46 
47 static ssize_t extts_enable_store(struct device *dev,
48 				  struct device_attribute *attr,
49 				  const char *buf, size_t count)
50 {
51 	struct ptp_clock *ptp = dev_get_drvdata(dev);
52 	struct ptp_clock_info *ops = ptp->info;
53 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
54 	int cnt, enable;
55 	int err = -EINVAL;
56 
57 	cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
58 	if (cnt != 2)
59 		goto out;
60 	if (req.extts.index >= ops->n_ext_ts)
61 		goto out;
62 
63 	err = ops->enable(ops, &req, enable ? 1 : 0);
64 	if (err)
65 		goto out;
66 
67 	return count;
68 out:
69 	return err;
70 }
71 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
72 
73 static ssize_t extts_fifo_show(struct device *dev,
74 			       struct device_attribute *attr, char *page)
75 {
76 	struct ptp_clock *ptp = dev_get_drvdata(dev);
77 	struct timestamp_event_queue *queue;
78 	struct ptp_extts_event event;
79 	unsigned long flags;
80 	size_t qcnt;
81 	int cnt = 0;
82 
83 	spin_lock_irqsave(&ptp->tsevqs_lock, flags);
84 	cnt = list_count_nodes(&ptp->tsevqs);
85 	if (cnt <= 0) {
86 		spin_unlock_irqrestore(&ptp->tsevqs_lock, flags);
87 		goto out;
88 	}
89 
90 	/* The sysfs fifo will always draw from the fist queue */
91 	queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue,
92 				 qlist);
93 	spin_unlock_irqrestore(&ptp->tsevqs_lock, flags);
94 
95 	memset(&event, 0, sizeof(event));
96 	spin_lock_irqsave(&queue->lock, flags);
97 	qcnt = queue_cnt(queue);
98 	if (qcnt) {
99 		event = queue->buf[queue->head];
100 		/* Paired with READ_ONCE() in queue_cnt() */
101 		WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
102 	}
103 	spin_unlock_irqrestore(&queue->lock, flags);
104 
105 	if (!qcnt)
106 		goto out;
107 
108 	cnt = sysfs_emit(page, "%u %lld %u\n",
109 			 event.index, event.t.sec, event.t.nsec);
110 out:
111 	return cnt;
112 }
113 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
114 
115 static ssize_t period_store(struct device *dev,
116 			    struct device_attribute *attr,
117 			    const char *buf, size_t count)
118 {
119 	struct ptp_clock *ptp = dev_get_drvdata(dev);
120 	struct ptp_clock_info *ops = ptp->info;
121 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
122 	int cnt, enable, err = -EINVAL;
123 
124 	cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
125 		     &req.perout.start.sec, &req.perout.start.nsec,
126 		     &req.perout.period.sec, &req.perout.period.nsec);
127 	if (cnt != 5)
128 		goto out;
129 	if (req.perout.index >= ops->n_per_out)
130 		goto out;
131 
132 	enable = req.perout.period.sec || req.perout.period.nsec;
133 	err = ops->enable(ops, &req, enable);
134 	if (err)
135 		goto out;
136 
137 	return count;
138 out:
139 	return err;
140 }
141 static DEVICE_ATTR(period, 0220, NULL, period_store);
142 
143 static ssize_t pps_enable_store(struct device *dev,
144 				struct device_attribute *attr,
145 				const char *buf, size_t count)
146 {
147 	struct ptp_clock *ptp = dev_get_drvdata(dev);
148 	struct ptp_clock_info *ops = ptp->info;
149 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
150 	int cnt, enable;
151 	int err = -EINVAL;
152 
153 	if (!capable(CAP_SYS_TIME))
154 		return -EPERM;
155 
156 	cnt = sscanf(buf, "%d", &enable);
157 	if (cnt != 1)
158 		goto out;
159 
160 	err = ops->enable(ops, &req, enable ? 1 : 0);
161 	if (err)
162 		goto out;
163 
164 	return count;
165 out:
166 	return err;
167 }
168 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
169 
170 static int unregister_vclock(struct device *dev, void *data)
171 {
172 	struct ptp_clock *ptp = dev_get_drvdata(dev);
173 	struct ptp_clock_info *info = ptp->info;
174 	struct ptp_vclock *vclock;
175 	u32 *num = data;
176 
177 	vclock = info_to_vclock(info);
178 	dev_info(dev->parent, "delete virtual clock ptp%d\n",
179 		 vclock->clock->index);
180 
181 	ptp_vclock_unregister(vclock);
182 	(*num)--;
183 
184 	/* For break. Not error. */
185 	if (*num == 0)
186 		return -EINVAL;
187 
188 	return 0;
189 }
190 
191 static ssize_t n_vclocks_show(struct device *dev,
192 			      struct device_attribute *attr, char *page)
193 {
194 	struct ptp_clock *ptp = dev_get_drvdata(dev);
195 	ssize_t size;
196 
197 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
198 		return -ERESTARTSYS;
199 
200 	size = sysfs_emit(page, "%u\n", ptp->n_vclocks);
201 
202 	mutex_unlock(&ptp->n_vclocks_mux);
203 
204 	return size;
205 }
206 
207 static ssize_t n_vclocks_store(struct device *dev,
208 			       struct device_attribute *attr,
209 			       const char *buf, size_t count)
210 {
211 	struct ptp_clock *ptp = dev_get_drvdata(dev);
212 	struct ptp_vclock *vclock;
213 	int err = -EINVAL;
214 	u32 num, i;
215 
216 	if (kstrtou32(buf, 0, &num))
217 		return err;
218 
219 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
220 		return -ERESTARTSYS;
221 
222 	if (num > ptp->max_vclocks) {
223 		dev_err(dev, "max value is %d\n", ptp->max_vclocks);
224 		goto out;
225 	}
226 
227 	/* Need to create more vclocks */
228 	if (num > ptp->n_vclocks) {
229 		for (i = 0; i < num - ptp->n_vclocks; i++) {
230 			vclock = ptp_vclock_register(ptp);
231 			if (IS_ERR(vclock)) {
232 				err = PTR_ERR(vclock);
233 				goto err_register;
234 			}
235 
236 			*(ptp->vclock_index + ptp->n_vclocks + i) =
237 				vclock->clock->index;
238 
239 			dev_info(dev, "new virtual clock ptp%d\n",
240 				 vclock->clock->index);
241 		}
242 	}
243 
244 	/* Need to delete vclocks */
245 	if (num < ptp->n_vclocks) {
246 		i = ptp->n_vclocks - num;
247 		device_for_each_child_reverse(dev, &i,
248 					      unregister_vclock);
249 
250 		for (i = 1; i <= ptp->n_vclocks - num; i++)
251 			*(ptp->vclock_index + ptp->n_vclocks - i) = -1;
252 	}
253 
254 	/* Need to inform about changed physical clock behavior */
255 	if (!ptp->has_cycles) {
256 		if (num == 0)
257 			dev_info(dev, "only physical clock in use now\n");
258 		else
259 			dev_info(dev, "guarantee physical clock free running\n");
260 	}
261 
262 	ptp->n_vclocks = num;
263 	mutex_unlock(&ptp->n_vclocks_mux);
264 
265 	return count;
266 err_register:
267 	num = i;
268 	if (num)
269 		device_for_each_child_reverse(dev, &num, unregister_vclock);
270 	for (num = 0; num < i; num++)
271 		ptp->vclock_index[ptp->n_vclocks + num] = -1;
272 out:
273 	mutex_unlock(&ptp->n_vclocks_mux);
274 	return err;
275 }
276 static DEVICE_ATTR_RW(n_vclocks);
277 
278 void ptp_vclock_remove_sysfs(struct ptp_clock *ptp)
279 {
280 	if (!ptp->is_virtual_clock)
281 		device_remove_file(&ptp->dev, &dev_attr_n_vclocks);
282 }
283 
284 static ssize_t max_vclocks_show(struct device *dev,
285 				struct device_attribute *attr, char *page)
286 {
287 	struct ptp_clock *ptp = dev_get_drvdata(dev);
288 	ssize_t size;
289 
290 	size = sysfs_emit(page, "%u\n", ptp->max_vclocks);
291 
292 	return size;
293 }
294 
295 static ssize_t max_vclocks_store(struct device *dev,
296 				 struct device_attribute *attr,
297 				 const char *buf, size_t count)
298 {
299 	struct ptp_clock *ptp = dev_get_drvdata(dev);
300 	unsigned int *vclock_index;
301 	int err = -EINVAL;
302 	size_t size;
303 	u32 max;
304 
305 	if (kstrtou32(buf, 0, &max) || max == 0 || max > PTP_MAX_VCLOCKS_LIMIT)
306 		return -EINVAL;
307 
308 	if (max == ptp->max_vclocks)
309 		return count;
310 
311 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
312 		return -ERESTARTSYS;
313 
314 	if (max < ptp->n_vclocks)
315 		goto out;
316 
317 	vclock_index = kcalloc(max, sizeof(int), GFP_KERNEL);
318 	if (!vclock_index) {
319 		err = -ENOMEM;
320 		goto out;
321 	}
322 
323 	size = sizeof(int) * ptp->n_vclocks;
324 	memcpy(vclock_index, ptp->vclock_index, size);
325 
326 	kfree(ptp->vclock_index);
327 	ptp->vclock_index = vclock_index;
328 	ptp->max_vclocks = max;
329 
330 	mutex_unlock(&ptp->n_vclocks_mux);
331 
332 	return count;
333 out:
334 	mutex_unlock(&ptp->n_vclocks_mux);
335 	return err;
336 }
337 static DEVICE_ATTR_RW(max_vclocks);
338 
339 static struct attribute *ptp_attrs[] = {
340 	&dev_attr_clock_name.attr,
341 
342 	&dev_attr_max_adjustment.attr,
343 	&dev_attr_max_phase_adjustment.attr,
344 	&dev_attr_n_alarms.attr,
345 	&dev_attr_n_external_timestamps.attr,
346 	&dev_attr_n_periodic_outputs.attr,
347 	&dev_attr_n_programmable_pins.attr,
348 	&dev_attr_pps_available.attr,
349 
350 	&dev_attr_extts_enable.attr,
351 	&dev_attr_fifo.attr,
352 	&dev_attr_period.attr,
353 	&dev_attr_pps_enable.attr,
354 	&dev_attr_n_vclocks.attr,
355 	&dev_attr_max_vclocks.attr,
356 	NULL
357 };
358 
359 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
360 					struct attribute *attr, int n)
361 {
362 	struct device *dev = kobj_to_dev(kobj);
363 	struct ptp_clock *ptp = dev_get_drvdata(dev);
364 	struct ptp_clock_info *info = ptp->info;
365 	umode_t mode = attr->mode;
366 
367 	if (attr == &dev_attr_extts_enable.attr ||
368 	    attr == &dev_attr_fifo.attr) {
369 		if (!info->n_ext_ts)
370 			mode = 0;
371 	} else if (attr == &dev_attr_period.attr) {
372 		if (!info->n_per_out)
373 			mode = 0;
374 	} else if (attr == &dev_attr_pps_enable.attr) {
375 		if (!info->pps)
376 			mode = 0;
377 	} else if (attr == &dev_attr_n_vclocks.attr ||
378 		   attr == &dev_attr_max_vclocks.attr) {
379 		if (ptp->is_virtual_clock)
380 			mode = 0;
381 	} else if (attr == &dev_attr_max_phase_adjustment.attr) {
382 		if (!info->adjphase || !info->getmaxphase)
383 			mode = 0;
384 	}
385 
386 	return mode;
387 }
388 
389 static const struct attribute_group ptp_group = {
390 	.is_visible	= ptp_is_attribute_visible,
391 	.attrs		= ptp_attrs,
392 };
393 
394 const struct attribute_group *ptp_groups[] = {
395 	&ptp_group,
396 	NULL
397 };
398 
399 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
400 {
401 	int i;
402 	for (i = 0; i < ptp->info->n_pins; i++) {
403 		if (!strcmp(ptp->info->pin_config[i].name, name))
404 			return i;
405 	}
406 	return -1;
407 }
408 
409 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
410 			    char *page)
411 {
412 	struct ptp_clock *ptp = dev_get_drvdata(dev);
413 	unsigned int func, chan;
414 	int index;
415 
416 	index = ptp_pin_name2index(ptp, attr->attr.name);
417 	if (index < 0)
418 		return -EINVAL;
419 
420 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
421 		return -ERESTARTSYS;
422 
423 	func = ptp->info->pin_config[index].func;
424 	chan = ptp->info->pin_config[index].chan;
425 
426 	mutex_unlock(&ptp->pincfg_mux);
427 
428 	return sysfs_emit(page, "%u %u\n", func, chan);
429 }
430 
431 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
432 			     const char *buf, size_t count)
433 {
434 	struct ptp_clock *ptp = dev_get_drvdata(dev);
435 	unsigned int func, chan;
436 	int cnt, err, index;
437 
438 	cnt = sscanf(buf, "%u %u", &func, &chan);
439 	if (cnt != 2)
440 		return -EINVAL;
441 
442 	index = ptp_pin_name2index(ptp, attr->attr.name);
443 	if (index < 0)
444 		return -EINVAL;
445 
446 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
447 		return -ERESTARTSYS;
448 	err = ptp_set_pinfunc(ptp, index, func, chan);
449 	mutex_unlock(&ptp->pincfg_mux);
450 	if (err)
451 		return err;
452 
453 	return count;
454 }
455 
456 int ptp_populate_pin_groups(struct ptp_clock *ptp)
457 {
458 	struct ptp_clock_info *info = ptp->info;
459 	int err = -ENOMEM, i, n_pins = info->n_pins;
460 
461 	if (!n_pins)
462 		return 0;
463 
464 	ptp->pin_dev_attr = kzalloc_objs(*ptp->pin_dev_attr, n_pins);
465 	if (!ptp->pin_dev_attr)
466 		goto no_dev_attr;
467 
468 	ptp->pin_attr = kzalloc_objs(*ptp->pin_attr, 1 + n_pins);
469 	if (!ptp->pin_attr)
470 		goto no_pin_attr;
471 
472 	for (i = 0; i < n_pins; i++) {
473 		struct device_attribute *da = &ptp->pin_dev_attr[i];
474 		sysfs_attr_init(&da->attr);
475 		da->attr.name = info->pin_config[i].name;
476 		da->attr.mode = 0644;
477 		da->show = ptp_pin_show;
478 		da->store = ptp_pin_store;
479 		ptp->pin_attr[i] = &da->attr;
480 	}
481 
482 	ptp->pin_attr_group.name = "pins";
483 	ptp->pin_attr_group.attrs = ptp->pin_attr;
484 
485 	ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
486 
487 	return 0;
488 
489 no_pin_attr:
490 	kfree(ptp->pin_dev_attr);
491 no_dev_attr:
492 	return err;
493 }
494 
495 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
496 {
497 	kfree(ptp->pin_attr);
498 	kfree(ptp->pin_dev_attr);
499 }
500