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