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