blob: f8a76090cbca1e8bbf694a4fbc16a54ef60490d5 [file] [log] [blame]
Richard Cochrand94ba802011-04-22 12:03:08 +02001/*
2 * PTP 1588 clock support - character device implementation.
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/module.h>
21#include <linux/posix-clock.h>
22#include <linux/poll.h>
23#include <linux/sched.h>
Richard Cochranc7ec0ba2012-11-26 01:44:34 +000024#include <linux/slab.h>
Richard Cochrand94ba802011-04-22 12:03:08 +020025
26#include "ptp_private.h"
27
Richard Cochran60923152014-03-20 22:21:52 +010028static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
29 enum ptp_pin_function func, unsigned int chan)
30{
31 struct ptp_clock_request rq;
32 int err = 0;
33
34 memset(&rq, 0, sizeof(rq));
35
36 switch (func) {
37 case PTP_PF_NONE:
38 break;
39 case PTP_PF_EXTTS:
40 rq.type = PTP_CLK_REQ_EXTTS;
41 rq.extts.index = chan;
42 err = ops->enable(ops, &rq, 0);
43 break;
44 case PTP_PF_PEROUT:
45 rq.type = PTP_CLK_REQ_PEROUT;
46 rq.perout.index = chan;
47 err = ops->enable(ops, &rq, 0);
48 break;
49 case PTP_PF_PHYSYNC:
50 break;
51 default:
52 return -EINVAL;
53 }
54
55 return err;
56}
57
58int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
59 enum ptp_pin_function func, unsigned int chan)
60{
61 struct ptp_clock_info *info = ptp->info;
62 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
63 unsigned int i;
64
65 /* Check to see if any other pin previously had this function. */
66 for (i = 0; i < info->n_pins; i++) {
67 if (info->pin_config[i].func == func &&
68 info->pin_config[i].chan == chan) {
69 pin1 = &info->pin_config[i];
70 break;
71 }
72 }
73 if (pin1 && i == pin)
74 return 0;
75
76 /* Check the desired function and channel. */
77 switch (func) {
78 case PTP_PF_NONE:
79 break;
80 case PTP_PF_EXTTS:
81 if (chan >= info->n_ext_ts)
82 return -EINVAL;
83 break;
84 case PTP_PF_PEROUT:
85 if (chan >= info->n_per_out)
86 return -EINVAL;
87 break;
88 case PTP_PF_PHYSYNC:
Stefan Sørensen72df7a72014-06-27 12:05:33 +020089 if (chan != 0)
90 return -EINVAL;
Richard Cochran60923152014-03-20 22:21:52 +010091 default:
92 return -EINVAL;
93 }
94
Richard Cochran60923152014-03-20 22:21:52 +010095 if (info->verify(info, pin, func, chan)) {
96 pr_err("driver cannot use function %u on pin %u\n", func, chan);
97 return -EOPNOTSUPP;
98 }
99
100 /* Disable whatever function was previously assigned. */
101 if (pin1) {
102 ptp_disable_pinfunc(info, func, chan);
103 pin1->func = PTP_PF_NONE;
104 pin1->chan = 0;
105 }
106 ptp_disable_pinfunc(info, pin2->func, pin2->chan);
107 pin2->func = func;
108 pin2->chan = chan;
109
110 return 0;
111}
112
Richard Cochrand94ba802011-04-22 12:03:08 +0200113int ptp_open(struct posix_clock *pc, fmode_t fmode)
114{
115 return 0;
116}
117
118long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
119{
120 struct ptp_clock_caps caps;
121 struct ptp_clock_request req;
Richard Cochranc3484c22012-11-26 01:44:35 +0000122 struct ptp_sys_offset *sysoff = NULL;
Richard Cochran60923152014-03-20 22:21:52 +0100123 struct ptp_pin_desc pd;
Richard Cochrand94ba802011-04-22 12:03:08 +0200124 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
125 struct ptp_clock_info *ops = ptp->info;
Richard Cochran215b13d2012-10-31 06:19:07 +0000126 struct ptp_clock_time *pct;
127 struct timespec ts;
Richard Cochrand94ba802011-04-22 12:03:08 +0200128 int enable, err = 0;
Richard Cochran60923152014-03-20 22:21:52 +0100129 unsigned int i, pin_index;
Richard Cochrand94ba802011-04-22 12:03:08 +0200130
131 switch (cmd) {
132
133 case PTP_CLOCK_GETCAPS:
134 memset(&caps, 0, sizeof(caps));
135 caps.max_adj = ptp->info->max_adj;
136 caps.n_alarm = ptp->info->n_alarm;
137 caps.n_ext_ts = ptp->info->n_ext_ts;
138 caps.n_per_out = ptp->info->n_per_out;
139 caps.pps = ptp->info->pps;
Richard Cochran60923152014-03-20 22:21:52 +0100140 caps.n_pins = ptp->info->n_pins;
Dan Carpentere23ef222011-05-29 22:53:12 +0300141 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
142 err = -EFAULT;
Richard Cochrand94ba802011-04-22 12:03:08 +0200143 break;
144
145 case PTP_EXTTS_REQUEST:
146 if (copy_from_user(&req.extts, (void __user *)arg,
147 sizeof(req.extts))) {
148 err = -EFAULT;
149 break;
150 }
151 if (req.extts.index >= ops->n_ext_ts) {
152 err = -EINVAL;
153 break;
154 }
155 req.type = PTP_CLK_REQ_EXTTS;
156 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
157 err = ops->enable(ops, &req, enable);
158 break;
159
160 case PTP_PEROUT_REQUEST:
161 if (copy_from_user(&req.perout, (void __user *)arg,
162 sizeof(req.perout))) {
163 err = -EFAULT;
164 break;
165 }
166 if (req.perout.index >= ops->n_per_out) {
167 err = -EINVAL;
168 break;
169 }
170 req.type = PTP_CLK_REQ_PEROUT;
171 enable = req.perout.period.sec || req.perout.period.nsec;
172 err = ops->enable(ops, &req, enable);
173 break;
174
175 case PTP_ENABLE_PPS:
176 if (!capable(CAP_SYS_TIME))
177 return -EPERM;
178 req.type = PTP_CLK_REQ_PPS;
179 enable = arg ? 1 : 0;
180 err = ops->enable(ops, &req, enable);
181 break;
182
Richard Cochran215b13d2012-10-31 06:19:07 +0000183 case PTP_SYS_OFFSET:
Richard Cochranc3484c22012-11-26 01:44:35 +0000184 sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL);
185 if (!sysoff) {
186 err = -ENOMEM;
187 break;
188 }
189 if (copy_from_user(sysoff, (void __user *)arg,
190 sizeof(*sysoff))) {
Richard Cochran215b13d2012-10-31 06:19:07 +0000191 err = -EFAULT;
192 break;
193 }
Richard Cochranc3484c22012-11-26 01:44:35 +0000194 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
Richard Cochran215b13d2012-10-31 06:19:07 +0000195 err = -EINVAL;
196 break;
197 }
Richard Cochranc3484c22012-11-26 01:44:35 +0000198 pct = &sysoff->ts[0];
199 for (i = 0; i < sysoff->n_samples; i++) {
Richard Cochran215b13d2012-10-31 06:19:07 +0000200 getnstimeofday(&ts);
201 pct->sec = ts.tv_sec;
202 pct->nsec = ts.tv_nsec;
203 pct++;
204 ptp->info->gettime(ptp->info, &ts);
205 pct->sec = ts.tv_sec;
206 pct->nsec = ts.tv_nsec;
207 pct++;
208 }
209 getnstimeofday(&ts);
210 pct->sec = ts.tv_sec;
211 pct->nsec = ts.tv_nsec;
Richard Cochranc3484c22012-11-26 01:44:35 +0000212 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
Richard Cochran215b13d2012-10-31 06:19:07 +0000213 err = -EFAULT;
214 break;
215
Richard Cochran60923152014-03-20 22:21:52 +0100216 case PTP_PIN_GETFUNC:
217 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
218 err = -EFAULT;
219 break;
220 }
221 pin_index = pd.index;
222 if (pin_index >= ops->n_pins) {
223 err = -EINVAL;
224 break;
225 }
226 if (mutex_lock_interruptible(&ptp->pincfg_mux))
227 return -ERESTARTSYS;
228 pd = ops->pin_config[pin_index];
229 mutex_unlock(&ptp->pincfg_mux);
230 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
231 err = -EFAULT;
232 break;
233
234 case PTP_PIN_SETFUNC:
235 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
236 err = -EFAULT;
237 break;
238 }
239 pin_index = pd.index;
240 if (pin_index >= ops->n_pins) {
241 err = -EINVAL;
242 break;
243 }
244 if (mutex_lock_interruptible(&ptp->pincfg_mux))
245 return -ERESTARTSYS;
246 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
247 mutex_unlock(&ptp->pincfg_mux);
248 break;
249
Richard Cochrand94ba802011-04-22 12:03:08 +0200250 default:
251 err = -ENOTTY;
252 break;
253 }
Richard Cochranc3484c22012-11-26 01:44:35 +0000254
255 kfree(sysoff);
Richard Cochrand94ba802011-04-22 12:03:08 +0200256 return err;
257}
258
259unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
260{
261 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
262
263 poll_wait(fp, &ptp->tsev_wq, wait);
264
265 return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
266}
267
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000268#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
269
Richard Cochrand94ba802011-04-22 12:03:08 +0200270ssize_t ptp_read(struct posix_clock *pc,
271 uint rdflags, char __user *buf, size_t cnt)
272{
273 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
274 struct timestamp_event_queue *queue = &ptp->tsevq;
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000275 struct ptp_extts_event *event;
Richard Cochrand94ba802011-04-22 12:03:08 +0200276 unsigned long flags;
277 size_t qcnt, i;
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000278 int result;
Richard Cochrand94ba802011-04-22 12:03:08 +0200279
280 if (cnt % sizeof(struct ptp_extts_event) != 0)
281 return -EINVAL;
282
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000283 if (cnt > EXTTS_BUFSIZE)
284 cnt = EXTTS_BUFSIZE;
Richard Cochrand94ba802011-04-22 12:03:08 +0200285
286 cnt = cnt / sizeof(struct ptp_extts_event);
287
288 if (mutex_lock_interruptible(&ptp->tsevq_mux))
289 return -ERESTARTSYS;
290
291 if (wait_event_interruptible(ptp->tsev_wq,
292 ptp->defunct || queue_cnt(queue))) {
293 mutex_unlock(&ptp->tsevq_mux);
294 return -ERESTARTSYS;
295 }
296
Dan Carpenterfb5a18c2011-05-29 22:54:07 +0300297 if (ptp->defunct) {
298 mutex_unlock(&ptp->tsevq_mux);
Richard Cochrand94ba802011-04-22 12:03:08 +0200299 return -ENODEV;
Dan Carpenterfb5a18c2011-05-29 22:54:07 +0300300 }
Richard Cochrand94ba802011-04-22 12:03:08 +0200301
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000302 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
303 if (!event) {
304 mutex_unlock(&ptp->tsevq_mux);
305 return -ENOMEM;
306 }
307
Richard Cochrand94ba802011-04-22 12:03:08 +0200308 spin_lock_irqsave(&queue->lock, flags);
309
310 qcnt = queue_cnt(queue);
311
312 if (cnt > qcnt)
313 cnt = qcnt;
314
315 for (i = 0; i < cnt; i++) {
316 event[i] = queue->buf[queue->head];
317 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
318 }
319
320 spin_unlock_irqrestore(&queue->lock, flags);
321
322 cnt = cnt * sizeof(struct ptp_extts_event);
323
324 mutex_unlock(&ptp->tsevq_mux);
325
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000326 result = cnt;
Dan Carpenterfb5a18c2011-05-29 22:54:07 +0300327 if (copy_to_user(buf, event, cnt))
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000328 result = -EFAULT;
Richard Cochrand94ba802011-04-22 12:03:08 +0200329
Richard Cochranc7ec0ba2012-11-26 01:44:34 +0000330 kfree(event);
331 return result;
Richard Cochrand94ba802011-04-22 12:03:08 +0200332}