Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v4.6
  1/*
  2 * PTP 1588 clock support - sysfs interface.
  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/capability.h>
 21#include <linux/slab.h>
 22
 23#include "ptp_private.h"
 24
 25static ssize_t clock_name_show(struct device *dev,
 26			       struct device_attribute *attr, char *page)
 27{
 28	struct ptp_clock *ptp = dev_get_drvdata(dev);
 29	return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
 30}
 31static DEVICE_ATTR(clock_name, 0444, clock_name_show, NULL);
 32
 33#define PTP_SHOW_INT(name, var)						\
 34static ssize_t var##_show(struct device *dev,				\
 35			   struct device_attribute *attr, char *page)	\
 36{									\
 37	struct ptp_clock *ptp = dev_get_drvdata(dev);			\
 38	return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var);	\
 39}									\
 40static DEVICE_ATTR(name, 0444, var##_show, NULL);
 41
 42PTP_SHOW_INT(max_adjustment, max_adj);
 43PTP_SHOW_INT(n_alarms, n_alarm);
 44PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
 45PTP_SHOW_INT(n_periodic_outputs, n_per_out);
 46PTP_SHOW_INT(n_programmable_pins, n_pins);
 47PTP_SHOW_INT(pps_available, pps);
 48
 49static struct attribute *ptp_attrs[] = {
 50	&dev_attr_clock_name.attr,
 51	&dev_attr_max_adjustment.attr,
 52	&dev_attr_n_alarms.attr,
 53	&dev_attr_n_external_timestamps.attr,
 54	&dev_attr_n_periodic_outputs.attr,
 55	&dev_attr_n_programmable_pins.attr,
 56	&dev_attr_pps_available.attr,
 57	NULL,
 58};
 59
 60static const struct attribute_group ptp_group = {
 61	.attrs = ptp_attrs,
 62};
 63
 64const struct attribute_group *ptp_groups[] = {
 65	&ptp_group,
 66	NULL,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 67};
 68
 69
 70static ssize_t extts_enable_store(struct device *dev,
 71				  struct device_attribute *attr,
 72				  const char *buf, size_t count)
 73{
 74	struct ptp_clock *ptp = dev_get_drvdata(dev);
 75	struct ptp_clock_info *ops = ptp->info;
 76	struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
 77	int cnt, enable;
 78	int err = -EINVAL;
 79
 80	cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
 81	if (cnt != 2)
 82		goto out;
 83	if (req.extts.index >= ops->n_ext_ts)
 84		goto out;
 85
 86	err = ops->enable(ops, &req, enable ? 1 : 0);
 87	if (err)
 88		goto out;
 89
 90	return count;
 91out:
 92	return err;
 93}
 94
 95static ssize_t extts_fifo_show(struct device *dev,
 96			       struct device_attribute *attr, char *page)
 97{
 98	struct ptp_clock *ptp = dev_get_drvdata(dev);
 99	struct timestamp_event_queue *queue = &ptp->tsevq;
100	struct ptp_extts_event event;
101	unsigned long flags;
102	size_t qcnt;
103	int cnt = 0;
104
105	memset(&event, 0, sizeof(event));
106
107	if (mutex_lock_interruptible(&ptp->tsevq_mux))
108		return -ERESTARTSYS;
109
110	spin_lock_irqsave(&queue->lock, flags);
111	qcnt = queue_cnt(queue);
112	if (qcnt) {
113		event = queue->buf[queue->head];
114		queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
115	}
116	spin_unlock_irqrestore(&queue->lock, flags);
117
118	if (!qcnt)
119		goto out;
120
121	cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
122		       event.index, event.t.sec, event.t.nsec);
123out:
124	mutex_unlock(&ptp->tsevq_mux);
125	return cnt;
126}
127
128static ssize_t period_store(struct device *dev,
129			    struct device_attribute *attr,
130			    const char *buf, size_t count)
131{
132	struct ptp_clock *ptp = dev_get_drvdata(dev);
133	struct ptp_clock_info *ops = ptp->info;
134	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
135	int cnt, enable, err = -EINVAL;
136
137	cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
138		     &req.perout.start.sec, &req.perout.start.nsec,
139		     &req.perout.period.sec, &req.perout.period.nsec);
140	if (cnt != 5)
141		goto out;
142	if (req.perout.index >= ops->n_per_out)
143		goto out;
144
145	enable = req.perout.period.sec || req.perout.period.nsec;
146	err = ops->enable(ops, &req, enable);
147	if (err)
148		goto out;
149
150	return count;
151out:
152	return err;
153}
154
155static ssize_t pps_enable_store(struct device *dev,
156				struct device_attribute *attr,
157				const char *buf, size_t count)
158{
159	struct ptp_clock *ptp = dev_get_drvdata(dev);
160	struct ptp_clock_info *ops = ptp->info;
161	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
162	int cnt, enable;
163	int err = -EINVAL;
164
165	if (!capable(CAP_SYS_TIME))
166		return -EPERM;
167
168	cnt = sscanf(buf, "%d", &enable);
169	if (cnt != 1)
170		goto out;
171
172	err = ops->enable(ops, &req, enable ? 1 : 0);
173	if (err)
174		goto out;
175
176	return count;
177out:
178	return err;
179}
180
181static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
182{
183	int i;
184	for (i = 0; i < ptp->info->n_pins; i++) {
185		if (!strcmp(ptp->info->pin_config[i].name, name))
186			return i;
187	}
188	return -1;
189}
190
191static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
192			    char *page)
193{
194	struct ptp_clock *ptp = dev_get_drvdata(dev);
195	unsigned int func, chan;
196	int index;
197
198	index = ptp_pin_name2index(ptp, attr->attr.name);
199	if (index < 0)
200		return -EINVAL;
201
202	if (mutex_lock_interruptible(&ptp->pincfg_mux))
203		return -ERESTARTSYS;
204
205	func = ptp->info->pin_config[index].func;
206	chan = ptp->info->pin_config[index].chan;
207
208	mutex_unlock(&ptp->pincfg_mux);
209
210	return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan);
211}
212
213static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
214			     const char *buf, size_t count)
215{
216	struct ptp_clock *ptp = dev_get_drvdata(dev);
217	unsigned int func, chan;
218	int cnt, err, index;
219
220	cnt = sscanf(buf, "%u %u", &func, &chan);
221	if (cnt != 2)
222		return -EINVAL;
223
224	index = ptp_pin_name2index(ptp, attr->attr.name);
225	if (index < 0)
226		return -EINVAL;
227
228	if (mutex_lock_interruptible(&ptp->pincfg_mux))
229		return -ERESTARTSYS;
230	err = ptp_set_pinfunc(ptp, index, func, chan);
231	mutex_unlock(&ptp->pincfg_mux);
232	if (err)
233		return err;
234
235	return count;
236}
237
238static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
239static DEVICE_ATTR(fifo,         0444, extts_fifo_show, NULL);
240static DEVICE_ATTR(period,       0220, NULL, period_store);
241static DEVICE_ATTR(pps_enable,   0220, NULL, pps_enable_store);
242
243int ptp_cleanup_sysfs(struct ptp_clock *ptp)
244{
245	struct device *dev = ptp->dev;
246	struct ptp_clock_info *info = ptp->info;
247
248	if (info->n_ext_ts) {
249		device_remove_file(dev, &dev_attr_extts_enable);
250		device_remove_file(dev, &dev_attr_fifo);
251	}
252	if (info->n_per_out)
253		device_remove_file(dev, &dev_attr_period);
254
255	if (info->pps)
256		device_remove_file(dev, &dev_attr_pps_enable);
257
258	if (info->n_pins) {
259		sysfs_remove_group(&dev->kobj, &ptp->pin_attr_group);
260		kfree(ptp->pin_attr);
261		kfree(ptp->pin_dev_attr);
262	}
263	return 0;
264}
265
266static int ptp_populate_pins(struct ptp_clock *ptp)
267{
268	struct device *dev = ptp->dev;
269	struct ptp_clock_info *info = ptp->info;
270	int err = -ENOMEM, i, n_pins = info->n_pins;
271
272	ptp->pin_dev_attr = kzalloc(n_pins * sizeof(*ptp->pin_dev_attr),
273				    GFP_KERNEL);
274	if (!ptp->pin_dev_attr)
275		goto no_dev_attr;
276
277	ptp->pin_attr = kzalloc((1 + n_pins) * sizeof(struct attribute *),
278				GFP_KERNEL);
279	if (!ptp->pin_attr)
280		goto no_pin_attr;
281
282	for (i = 0; i < n_pins; i++) {
283		struct device_attribute *da = &ptp->pin_dev_attr[i];
284		sysfs_attr_init(&da->attr);
285		da->attr.name = info->pin_config[i].name;
286		da->attr.mode = 0644;
287		da->show = ptp_pin_show;
288		da->store = ptp_pin_store;
289		ptp->pin_attr[i] = &da->attr;
290	}
291
292	ptp->pin_attr_group.name = "pins";
293	ptp->pin_attr_group.attrs = ptp->pin_attr;
294
295	err = sysfs_create_group(&dev->kobj, &ptp->pin_attr_group);
296	if (err)
297		goto no_group;
298	return 0;
299
300no_group:
301	kfree(ptp->pin_attr);
302no_pin_attr:
303	kfree(ptp->pin_dev_attr);
304no_dev_attr:
305	return err;
306}
307
308int ptp_populate_sysfs(struct ptp_clock *ptp)
309{
310	struct device *dev = ptp->dev;
311	struct ptp_clock_info *info = ptp->info;
312	int err;
313
314	if (info->n_ext_ts) {
315		err = device_create_file(dev, &dev_attr_extts_enable);
316		if (err)
317			goto out1;
318		err = device_create_file(dev, &dev_attr_fifo);
319		if (err)
320			goto out2;
321	}
322	if (info->n_per_out) {
323		err = device_create_file(dev, &dev_attr_period);
324		if (err)
325			goto out3;
326	}
327	if (info->pps) {
328		err = device_create_file(dev, &dev_attr_pps_enable);
329		if (err)
330			goto out4;
331	}
332	if (info->n_pins) {
333		err = ptp_populate_pins(ptp);
334		if (err)
335			goto out5;
336	}
337	return 0;
338out5:
339	if (info->pps)
340		device_remove_file(dev, &dev_attr_pps_enable);
341out4:
342	if (info->n_per_out)
343		device_remove_file(dev, &dev_attr_period);
344out3:
345	if (info->n_ext_ts)
346		device_remove_file(dev, &dev_attr_fifo);
347out2:
348	if (info->n_ext_ts)
349		device_remove_file(dev, &dev_attr_extts_enable);
350out1:
351	return err;
352}
v3.1
  1/*
  2 * PTP 1588 clock support - sysfs interface.
  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/capability.h>
 
 21
 22#include "ptp_private.h"
 23
 24static ssize_t clock_name_show(struct device *dev,
 25			       struct device_attribute *attr, char *page)
 26{
 27	struct ptp_clock *ptp = dev_get_drvdata(dev);
 28	return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
 29}
 
 30
 31#define PTP_SHOW_INT(name)						\
 32static ssize_t name##_show(struct device *dev,				\
 33			   struct device_attribute *attr, char *page)	\
 34{									\
 35	struct ptp_clock *ptp = dev_get_drvdata(dev);			\
 36	return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->name);	\
 37}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 38
 39PTP_SHOW_INT(max_adj);
 40PTP_SHOW_INT(n_alarm);
 41PTP_SHOW_INT(n_ext_ts);
 42PTP_SHOW_INT(n_per_out);
 43PTP_SHOW_INT(pps);
 44
 45#define PTP_RO_ATTR(_var, _name) {				\
 46	.attr	= { .name = __stringify(_name), .mode = 0444 },	\
 47	.show	= _var##_show,					\
 48}
 49
 50struct device_attribute ptp_dev_attrs[] = {
 51	PTP_RO_ATTR(clock_name,	clock_name),
 52	PTP_RO_ATTR(max_adj,	max_adjustment),
 53	PTP_RO_ATTR(n_alarm,	n_alarms),
 54	PTP_RO_ATTR(n_ext_ts,	n_external_timestamps),
 55	PTP_RO_ATTR(n_per_out,	n_periodic_outputs),
 56	PTP_RO_ATTR(pps,	pps_available),
 57	__ATTR_NULL,
 58};
 59
 
 60static ssize_t extts_enable_store(struct device *dev,
 61				  struct device_attribute *attr,
 62				  const char *buf, size_t count)
 63{
 64	struct ptp_clock *ptp = dev_get_drvdata(dev);
 65	struct ptp_clock_info *ops = ptp->info;
 66	struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
 67	int cnt, enable;
 68	int err = -EINVAL;
 69
 70	cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
 71	if (cnt != 2)
 72		goto out;
 73	if (req.extts.index >= ops->n_ext_ts)
 74		goto out;
 75
 76	err = ops->enable(ops, &req, enable ? 1 : 0);
 77	if (err)
 78		goto out;
 79
 80	return count;
 81out:
 82	return err;
 83}
 84
 85static ssize_t extts_fifo_show(struct device *dev,
 86			       struct device_attribute *attr, char *page)
 87{
 88	struct ptp_clock *ptp = dev_get_drvdata(dev);
 89	struct timestamp_event_queue *queue = &ptp->tsevq;
 90	struct ptp_extts_event event;
 91	unsigned long flags;
 92	size_t qcnt;
 93	int cnt = 0;
 94
 95	memset(&event, 0, sizeof(event));
 96
 97	if (mutex_lock_interruptible(&ptp->tsevq_mux))
 98		return -ERESTARTSYS;
 99
100	spin_lock_irqsave(&queue->lock, flags);
101	qcnt = queue_cnt(queue);
102	if (qcnt) {
103		event = queue->buf[queue->head];
104		queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
105	}
106	spin_unlock_irqrestore(&queue->lock, flags);
107
108	if (!qcnt)
109		goto out;
110
111	cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
112		       event.index, event.t.sec, event.t.nsec);
113out:
114	mutex_unlock(&ptp->tsevq_mux);
115	return cnt;
116}
117
118static ssize_t period_store(struct device *dev,
119			    struct device_attribute *attr,
120			    const char *buf, size_t count)
121{
122	struct ptp_clock *ptp = dev_get_drvdata(dev);
123	struct ptp_clock_info *ops = ptp->info;
124	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
125	int cnt, enable, err = -EINVAL;
126
127	cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
128		     &req.perout.start.sec, &req.perout.start.nsec,
129		     &req.perout.period.sec, &req.perout.period.nsec);
130	if (cnt != 5)
131		goto out;
132	if (req.perout.index >= ops->n_per_out)
133		goto out;
134
135	enable = req.perout.period.sec || req.perout.period.nsec;
136	err = ops->enable(ops, &req, enable);
137	if (err)
138		goto out;
139
140	return count;
141out:
142	return err;
143}
144
145static ssize_t pps_enable_store(struct device *dev,
146				struct device_attribute *attr,
147				const char *buf, size_t count)
148{
149	struct ptp_clock *ptp = dev_get_drvdata(dev);
150	struct ptp_clock_info *ops = ptp->info;
151	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
152	int cnt, enable;
153	int err = -EINVAL;
154
155	if (!capable(CAP_SYS_TIME))
156		return -EPERM;
157
158	cnt = sscanf(buf, "%d", &enable);
159	if (cnt != 1)
160		goto out;
161
162	err = ops->enable(ops, &req, enable ? 1 : 0);
163	if (err)
164		goto out;
165
166	return count;
167out:
168	return err;
169}
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
172static DEVICE_ATTR(fifo,         0444, extts_fifo_show, NULL);
173static DEVICE_ATTR(period,       0220, NULL, period_store);
174static DEVICE_ATTR(pps_enable,   0220, NULL, pps_enable_store);
175
176int ptp_cleanup_sysfs(struct ptp_clock *ptp)
177{
178	struct device *dev = ptp->dev;
179	struct ptp_clock_info *info = ptp->info;
180
181	if (info->n_ext_ts) {
182		device_remove_file(dev, &dev_attr_extts_enable);
183		device_remove_file(dev, &dev_attr_fifo);
184	}
185	if (info->n_per_out)
186		device_remove_file(dev, &dev_attr_period);
187
188	if (info->pps)
189		device_remove_file(dev, &dev_attr_pps_enable);
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191	return 0;
 
 
 
 
 
 
 
192}
193
194int ptp_populate_sysfs(struct ptp_clock *ptp)
195{
196	struct device *dev = ptp->dev;
197	struct ptp_clock_info *info = ptp->info;
198	int err;
199
200	if (info->n_ext_ts) {
201		err = device_create_file(dev, &dev_attr_extts_enable);
202		if (err)
203			goto out1;
204		err = device_create_file(dev, &dev_attr_fifo);
205		if (err)
206			goto out2;
207	}
208	if (info->n_per_out) {
209		err = device_create_file(dev, &dev_attr_period);
210		if (err)
211			goto out3;
212	}
213	if (info->pps) {
214		err = device_create_file(dev, &dev_attr_pps_enable);
215		if (err)
216			goto out4;
217	}
 
 
 
 
 
218	return 0;
 
 
 
219out4:
220	if (info->n_per_out)
221		device_remove_file(dev, &dev_attr_period);
222out3:
223	if (info->n_ext_ts)
224		device_remove_file(dev, &dev_attr_fifo);
225out2:
226	if (info->n_ext_ts)
227		device_remove_file(dev, &dev_attr_extts_enable);
228out1:
229	return err;
230}