2 * PTP 1588 clock support - sysfs interface.
4 * Copyright (C) 2010 OMICRON electronics GmbH
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.
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.
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.
20 #include <linux/capability.h>
22 #include "ptp_private.h"
24 static ssize_t clock_name_show(struct device *dev,
25 struct device_attribute *attr, char *page)
27 struct ptp_clock *ptp = dev_get_drvdata(dev);
28 return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
30 static DEVICE_ATTR(clock_name, 0444, clock_name_show, NULL);
32 #define PTP_SHOW_INT(name, var) \
33 static ssize_t var##_show(struct device *dev, \
34 struct device_attribute *attr, char *page) \
36 struct ptp_clock *ptp = dev_get_drvdata(dev); \
37 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
39 static DEVICE_ATTR(name, 0444, var##_show, NULL);
41 PTP_SHOW_INT(max_adjustment, max_adj);
42 PTP_SHOW_INT(n_alarms, n_alarm);
43 PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
44 PTP_SHOW_INT(n_periodic_outputs, n_per_out);
45 PTP_SHOW_INT(pps_available, pps);
47 static struct attribute *ptp_attrs[] = {
48 &dev_attr_clock_name.attr,
49 &dev_attr_max_adjustment.attr,
50 &dev_attr_n_alarms.attr,
51 &dev_attr_n_external_timestamps.attr,
52 &dev_attr_n_periodic_outputs.attr,
53 &dev_attr_pps_available.attr,
57 static const struct attribute_group ptp_group = {
61 const struct attribute_group *ptp_groups[] = {
67 static ssize_t extts_enable_store(struct device *dev,
68 struct device_attribute *attr,
69 const char *buf, size_t count)
71 struct ptp_clock *ptp = dev_get_drvdata(dev);
72 struct ptp_clock_info *ops = ptp->info;
73 struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
77 cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
80 if (req.extts.index >= ops->n_ext_ts)
83 err = ops->enable(ops, &req, enable ? 1 : 0);
92 static ssize_t extts_fifo_show(struct device *dev,
93 struct device_attribute *attr, char *page)
95 struct ptp_clock *ptp = dev_get_drvdata(dev);
96 struct timestamp_event_queue *queue = &ptp->tsevq;
97 struct ptp_extts_event event;
102 memset(&event, 0, sizeof(event));
104 if (mutex_lock_interruptible(&ptp->tsevq_mux))
107 spin_lock_irqsave(&queue->lock, flags);
108 qcnt = queue_cnt(queue);
110 event = queue->buf[queue->head];
111 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
113 spin_unlock_irqrestore(&queue->lock, flags);
118 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
119 event.index, event.t.sec, event.t.nsec);
121 mutex_unlock(&ptp->tsevq_mux);
125 static ssize_t period_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
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_PEROUT };
132 int cnt, enable, err = -EINVAL;
134 cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
135 &req.perout.start.sec, &req.perout.start.nsec,
136 &req.perout.period.sec, &req.perout.period.nsec);
139 if (req.perout.index >= ops->n_per_out)
142 enable = req.perout.period.sec || req.perout.period.nsec;
143 err = ops->enable(ops, &req, enable);
152 static ssize_t pps_enable_store(struct device *dev,
153 struct device_attribute *attr,
154 const char *buf, size_t count)
156 struct ptp_clock *ptp = dev_get_drvdata(dev);
157 struct ptp_clock_info *ops = ptp->info;
158 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
162 if (!capable(CAP_SYS_TIME))
165 cnt = sscanf(buf, "%d", &enable);
169 err = ops->enable(ops, &req, enable ? 1 : 0);
178 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
179 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
180 static DEVICE_ATTR(period, 0220, NULL, period_store);
181 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
183 int ptp_cleanup_sysfs(struct ptp_clock *ptp)
185 struct device *dev = ptp->dev;
186 struct ptp_clock_info *info = ptp->info;
188 if (info->n_ext_ts) {
189 device_remove_file(dev, &dev_attr_extts_enable);
190 device_remove_file(dev, &dev_attr_fifo);
193 device_remove_file(dev, &dev_attr_period);
196 device_remove_file(dev, &dev_attr_pps_enable);
201 int ptp_populate_sysfs(struct ptp_clock *ptp)
203 struct device *dev = ptp->dev;
204 struct ptp_clock_info *info = ptp->info;
207 if (info->n_ext_ts) {
208 err = device_create_file(dev, &dev_attr_extts_enable);
211 err = device_create_file(dev, &dev_attr_fifo);
215 if (info->n_per_out) {
216 err = device_create_file(dev, &dev_attr_period);
221 err = device_create_file(dev, &dev_attr_pps_enable);
228 device_remove_file(dev, &dev_attr_period);
231 device_remove_file(dev, &dev_attr_fifo);
234 device_remove_file(dev, &dev_attr_extts_enable);