Linux Audio

Check our new training course

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