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
);
31 #define PTP_SHOW_INT(name) \
32 static ssize_t name##_show(struct device *dev, \
33 struct device_attribute *attr, char *page) \
35 struct ptp_clock *ptp = dev_get_drvdata(dev); \
36 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->name); \
39 PTP_SHOW_INT(max_adj
);
40 PTP_SHOW_INT(n_alarm
);
41 PTP_SHOW_INT(n_ext_ts
);
42 PTP_SHOW_INT(n_per_out
);
45 #define PTP_RO_ATTR(_var, _name) { \
46 .attr = { .name = __stringify(_name), .mode = 0444 }, \
47 .show = _var##_show, \
50 struct device_attribute ptp_dev_attrs
[] = {
51 PTP_RO_ATTR(clock_name
, clock_name
),
52 PTP_RO_ATTR(max_adj
, max_adjustment
),
53 PTP_RO_ATTR(n_alarm
, n_alarms
),
54 PTP_RO_ATTR(n_ext_ts
, n_external_timestamps
),
55 PTP_RO_ATTR(n_per_out
, n_periodic_outputs
),
56 PTP_RO_ATTR(pps
, pps_available
),
60 static ssize_t
extts_enable_store(struct device
*dev
,
61 struct device_attribute
*attr
,
62 const char *buf
, size_t count
)
64 struct ptp_clock
*ptp
= dev_get_drvdata(dev
);
65 struct ptp_clock_info
*ops
= ptp
->info
;
66 struct ptp_clock_request req
= { .type
= PTP_CLK_REQ_EXTTS
};
70 cnt
= sscanf(buf
, "%u %d", &req
.extts
.index
, &enable
);
73 if (req
.extts
.index
>= ops
->n_ext_ts
)
76 err
= ops
->enable(ops
, &req
, enable
? 1 : 0);
85 static ssize_t
extts_fifo_show(struct device
*dev
,
86 struct device_attribute
*attr
, char *page
)
88 struct ptp_clock
*ptp
= dev_get_drvdata(dev
);
89 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
90 struct ptp_extts_event event
;
95 memset(&event
, 0, sizeof(event
));
97 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
100 spin_lock_irqsave(&queue
->lock
, flags
);
101 qcnt
= queue_cnt(queue
);
103 event
= queue
->buf
[queue
->head
];
104 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
106 spin_unlock_irqrestore(&queue
->lock
, flags
);
111 cnt
= snprintf(page
, PAGE_SIZE
, "%u %lld %u\n",
112 event
.index
, event
.t
.sec
, event
.t
.nsec
);
114 mutex_unlock(&ptp
->tsevq_mux
);
118 static ssize_t
period_store(struct device
*dev
,
119 struct device_attribute
*attr
,
120 const char *buf
, size_t count
)
122 struct ptp_clock
*ptp
= dev_get_drvdata(dev
);
123 struct ptp_clock_info
*ops
= ptp
->info
;
124 struct ptp_clock_request req
= { .type
= PTP_CLK_REQ_PEROUT
};
125 int cnt
, enable
, err
= -EINVAL
;
127 cnt
= sscanf(buf
, "%u %lld %u %lld %u", &req
.perout
.index
,
128 &req
.perout
.start
.sec
, &req
.perout
.start
.nsec
,
129 &req
.perout
.period
.sec
, &req
.perout
.period
.nsec
);
132 if (req
.perout
.index
>= ops
->n_per_out
)
135 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
136 err
= ops
->enable(ops
, &req
, enable
);
145 static ssize_t
pps_enable_store(struct device
*dev
,
146 struct device_attribute
*attr
,
147 const char *buf
, size_t count
)
149 struct ptp_clock
*ptp
= dev_get_drvdata(dev
);
150 struct ptp_clock_info
*ops
= ptp
->info
;
151 struct ptp_clock_request req
= { .type
= PTP_CLK_REQ_PPS
};
155 if (!capable(CAP_SYS_TIME
))
158 cnt
= sscanf(buf
, "%d", &enable
);
162 err
= ops
->enable(ops
, &req
, enable
? 1 : 0);
171 static DEVICE_ATTR(extts_enable
, 0220, NULL
, extts_enable_store
);
172 static DEVICE_ATTR(fifo
, 0444, extts_fifo_show
, NULL
);
173 static DEVICE_ATTR(period
, 0220, NULL
, period_store
);
174 static DEVICE_ATTR(pps_enable
, 0220, NULL
, pps_enable_store
);
176 int ptp_cleanup_sysfs(struct ptp_clock
*ptp
)
178 struct device
*dev
= ptp
->dev
;
179 struct ptp_clock_info
*info
= ptp
->info
;
181 if (info
->n_ext_ts
) {
182 device_remove_file(dev
, &dev_attr_extts_enable
);
183 device_remove_file(dev
, &dev_attr_fifo
);
186 device_remove_file(dev
, &dev_attr_period
);
189 device_remove_file(dev
, &dev_attr_pps_enable
);
194 int ptp_populate_sysfs(struct ptp_clock
*ptp
)
196 struct device
*dev
= ptp
->dev
;
197 struct ptp_clock_info
*info
= ptp
->info
;
200 if (info
->n_ext_ts
) {
201 err
= device_create_file(dev
, &dev_attr_extts_enable
);
204 err
= device_create_file(dev
, &dev_attr_fifo
);
208 if (info
->n_per_out
) {
209 err
= device_create_file(dev
, &dev_attr_period
);
214 err
= device_create_file(dev
, &dev_attr_pps_enable
);
221 device_remove_file(dev
, &dev_attr_period
);
224 device_remove_file(dev
, &dev_attr_fifo
);
227 device_remove_file(dev
, &dev_attr_extts_enable
);