• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 #include <linux/capability.h>
8 #include <linux/slab.h>
9 
10 #include "ptp_private.h"
11 
clock_name_show(struct device * dev,struct device_attribute * attr,char * page)12 static ssize_t clock_name_show(struct device *dev,
13 			       struct device_attribute *attr, char *page)
14 {
15 	struct ptp_clock *ptp = dev_get_drvdata(dev);
16 	return sysfs_emit(page, "%s\n", ptp->info->name);
17 }
18 static DEVICE_ATTR_RO(clock_name);
19 
20 #define PTP_SHOW_INT(name, var)						\
21 static ssize_t var##_show(struct device *dev,				\
22 			   struct device_attribute *attr, char *page)	\
23 {									\
24 	struct ptp_clock *ptp = dev_get_drvdata(dev);			\
25 	return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var);	\
26 }									\
27 static DEVICE_ATTR(name, 0444, var##_show, NULL);
28 
29 PTP_SHOW_INT(max_adjustment, max_adj);
30 PTP_SHOW_INT(n_alarms, n_alarm);
31 PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
32 PTP_SHOW_INT(n_periodic_outputs, n_per_out);
33 PTP_SHOW_INT(n_programmable_pins, n_pins);
34 PTP_SHOW_INT(pps_available, pps);
35 
extts_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)36 static ssize_t extts_enable_store(struct device *dev,
37 				  struct device_attribute *attr,
38 				  const char *buf, size_t count)
39 {
40 	struct ptp_clock *ptp = dev_get_drvdata(dev);
41 	struct ptp_clock_info *ops = ptp->info;
42 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
43 	int cnt, enable;
44 	int err = -EINVAL;
45 
46 	cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
47 	if (cnt != 2)
48 		goto out;
49 	if (req.extts.index >= ops->n_ext_ts)
50 		goto out;
51 
52 	err = ops->enable(ops, &req, enable ? 1 : 0);
53 	if (err)
54 		goto out;
55 
56 	return count;
57 out:
58 	return err;
59 }
60 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
61 
extts_fifo_show(struct device * dev,struct device_attribute * attr,char * page)62 static ssize_t extts_fifo_show(struct device *dev,
63 			       struct device_attribute *attr, char *page)
64 {
65 	struct ptp_clock *ptp = dev_get_drvdata(dev);
66 	struct timestamp_event_queue *queue = &ptp->tsevq;
67 	struct ptp_extts_event event;
68 	unsigned long flags;
69 	size_t qcnt;
70 	int cnt = 0;
71 
72 	memset(&event, 0, sizeof(event));
73 
74 	if (mutex_lock_interruptible(&ptp->tsevq_mux))
75 		return -ERESTARTSYS;
76 
77 	spin_lock_irqsave(&queue->lock, flags);
78 	qcnt = queue_cnt(queue);
79 	if (qcnt) {
80 		event = queue->buf[queue->head];
81 		/* Paired with READ_ONCE() in queue_cnt() */
82 		WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
83 	}
84 	spin_unlock_irqrestore(&queue->lock, flags);
85 
86 	if (!qcnt)
87 		goto out;
88 
89 	cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
90 		       event.index, event.t.sec, event.t.nsec);
91 out:
92 	mutex_unlock(&ptp->tsevq_mux);
93 	return cnt;
94 }
95 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
96 
period_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)97 static ssize_t period_store(struct device *dev,
98 			    struct device_attribute *attr,
99 			    const char *buf, size_t count)
100 {
101 	struct ptp_clock *ptp = dev_get_drvdata(dev);
102 	struct ptp_clock_info *ops = ptp->info;
103 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
104 	int cnt, enable, err = -EINVAL;
105 
106 	cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
107 		     &req.perout.start.sec, &req.perout.start.nsec,
108 		     &req.perout.period.sec, &req.perout.period.nsec);
109 	if (cnt != 5)
110 		goto out;
111 	if (req.perout.index >= ops->n_per_out)
112 		goto out;
113 
114 	enable = req.perout.period.sec || req.perout.period.nsec;
115 	err = ops->enable(ops, &req, enable);
116 	if (err)
117 		goto out;
118 
119 	return count;
120 out:
121 	return err;
122 }
123 static DEVICE_ATTR(period, 0220, NULL, period_store);
124 
pps_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)125 static ssize_t pps_enable_store(struct device *dev,
126 				struct device_attribute *attr,
127 				const char *buf, size_t count)
128 {
129 	struct ptp_clock *ptp = dev_get_drvdata(dev);
130 	struct ptp_clock_info *ops = ptp->info;
131 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
132 	int cnt, enable;
133 	int err = -EINVAL;
134 
135 	if (!capable(CAP_SYS_TIME))
136 		return -EPERM;
137 
138 	cnt = sscanf(buf, "%d", &enable);
139 	if (cnt != 1)
140 		goto out;
141 
142 	err = ops->enable(ops, &req, enable ? 1 : 0);
143 	if (err)
144 		goto out;
145 
146 	return count;
147 out:
148 	return err;
149 }
150 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
151 
152 static struct attribute *ptp_attrs[] = {
153 	&dev_attr_clock_name.attr,
154 
155 	&dev_attr_max_adjustment.attr,
156 	&dev_attr_n_alarms.attr,
157 	&dev_attr_n_external_timestamps.attr,
158 	&dev_attr_n_periodic_outputs.attr,
159 	&dev_attr_n_programmable_pins.attr,
160 	&dev_attr_pps_available.attr,
161 
162 	&dev_attr_extts_enable.attr,
163 	&dev_attr_fifo.attr,
164 	&dev_attr_period.attr,
165 	&dev_attr_pps_enable.attr,
166 	NULL
167 };
168 
ptp_is_attribute_visible(struct kobject * kobj,struct attribute * attr,int n)169 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
170 					struct attribute *attr, int n)
171 {
172 	struct device *dev = kobj_to_dev(kobj);
173 	struct ptp_clock *ptp = dev_get_drvdata(dev);
174 	struct ptp_clock_info *info = ptp->info;
175 	umode_t mode = attr->mode;
176 
177 	if (attr == &dev_attr_extts_enable.attr ||
178 	    attr == &dev_attr_fifo.attr) {
179 		if (!info->n_ext_ts)
180 			mode = 0;
181 	} else if (attr == &dev_attr_period.attr) {
182 		if (!info->n_per_out)
183 			mode = 0;
184 	} else if (attr == &dev_attr_pps_enable.attr) {
185 		if (!info->pps)
186 			mode = 0;
187 	}
188 
189 	return mode;
190 }
191 
192 static const struct attribute_group ptp_group = {
193 	.is_visible	= ptp_is_attribute_visible,
194 	.attrs		= ptp_attrs,
195 };
196 
197 const struct attribute_group *ptp_groups[] = {
198 	&ptp_group,
199 	NULL
200 };
201 
ptp_pin_name2index(struct ptp_clock * ptp,const char * name)202 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
203 {
204 	int i;
205 	for (i = 0; i < ptp->info->n_pins; i++) {
206 		if (!strcmp(ptp->info->pin_config[i].name, name))
207 			return i;
208 	}
209 	return -1;
210 }
211 
ptp_pin_show(struct device * dev,struct device_attribute * attr,char * page)212 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
213 			    char *page)
214 {
215 	struct ptp_clock *ptp = dev_get_drvdata(dev);
216 	unsigned int func, chan;
217 	int index;
218 
219 	index = ptp_pin_name2index(ptp, attr->attr.name);
220 	if (index < 0)
221 		return -EINVAL;
222 
223 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
224 		return -ERESTARTSYS;
225 
226 	func = ptp->info->pin_config[index].func;
227 	chan = ptp->info->pin_config[index].chan;
228 
229 	mutex_unlock(&ptp->pincfg_mux);
230 
231 	return sysfs_emit(page, "%u %u\n", func, chan);
232 }
233 
ptp_pin_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)234 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
235 			     const char *buf, size_t count)
236 {
237 	struct ptp_clock *ptp = dev_get_drvdata(dev);
238 	unsigned int func, chan;
239 	int cnt, err, index;
240 
241 	cnt = sscanf(buf, "%u %u", &func, &chan);
242 	if (cnt != 2)
243 		return -EINVAL;
244 
245 	index = ptp_pin_name2index(ptp, attr->attr.name);
246 	if (index < 0)
247 		return -EINVAL;
248 
249 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
250 		return -ERESTARTSYS;
251 	err = ptp_set_pinfunc(ptp, index, func, chan);
252 	mutex_unlock(&ptp->pincfg_mux);
253 	if (err)
254 		return err;
255 
256 	return count;
257 }
258 
ptp_populate_pin_groups(struct ptp_clock * ptp)259 int ptp_populate_pin_groups(struct ptp_clock *ptp)
260 {
261 	struct ptp_clock_info *info = ptp->info;
262 	int err = -ENOMEM, i, n_pins = info->n_pins;
263 
264 	if (!n_pins)
265 		return 0;
266 
267 	ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
268 				    GFP_KERNEL);
269 	if (!ptp->pin_dev_attr)
270 		goto no_dev_attr;
271 
272 	ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
273 	if (!ptp->pin_attr)
274 		goto no_pin_attr;
275 
276 	for (i = 0; i < n_pins; i++) {
277 		struct device_attribute *da = &ptp->pin_dev_attr[i];
278 		sysfs_attr_init(&da->attr);
279 		da->attr.name = info->pin_config[i].name;
280 		da->attr.mode = 0644;
281 		da->show = ptp_pin_show;
282 		da->store = ptp_pin_store;
283 		ptp->pin_attr[i] = &da->attr;
284 	}
285 
286 	ptp->pin_attr_group.name = "pins";
287 	ptp->pin_attr_group.attrs = ptp->pin_attr;
288 
289 	ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
290 
291 	return 0;
292 
293 no_pin_attr:
294 	kfree(ptp->pin_dev_attr);
295 no_dev_attr:
296 	return err;
297 }
298 
ptp_cleanup_pin_groups(struct ptp_clock * ptp)299 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
300 {
301 	kfree(ptp->pin_attr);
302 	kfree(ptp->pin_dev_attr);
303 }
304