]>
Commit | Line | Data |
---|---|---|
d94ba80e RC |
1 | /* |
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> | |
c7ec0bad | 24 | #include <linux/slab.h> |
719f1aa4 | 25 | #include <linux/timekeeping.h> |
d94ba80e RC |
26 | |
27 | #include "ptp_private.h" | |
28 | ||
6092315d RC |
29 | static int ptp_disable_pinfunc(struct ptp_clock_info *ops, |
30 | enum ptp_pin_function func, unsigned int chan) | |
31 | { | |
32 | struct ptp_clock_request rq; | |
33 | int err = 0; | |
34 | ||
35 | memset(&rq, 0, sizeof(rq)); | |
36 | ||
37 | switch (func) { | |
38 | case PTP_PF_NONE: | |
39 | break; | |
40 | case PTP_PF_EXTTS: | |
41 | rq.type = PTP_CLK_REQ_EXTTS; | |
42 | rq.extts.index = chan; | |
43 | err = ops->enable(ops, &rq, 0); | |
44 | break; | |
45 | case PTP_PF_PEROUT: | |
46 | rq.type = PTP_CLK_REQ_PEROUT; | |
47 | rq.perout.index = chan; | |
48 | err = ops->enable(ops, &rq, 0); | |
49 | break; | |
50 | case PTP_PF_PHYSYNC: | |
51 | break; | |
52 | default: | |
53 | return -EINVAL; | |
54 | } | |
55 | ||
56 | return err; | |
57 | } | |
58 | ||
59 | int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, | |
60 | enum ptp_pin_function func, unsigned int chan) | |
61 | { | |
62 | struct ptp_clock_info *info = ptp->info; | |
63 | struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin]; | |
64 | unsigned int i; | |
65 | ||
66 | /* Check to see if any other pin previously had this function. */ | |
67 | for (i = 0; i < info->n_pins; i++) { | |
68 | if (info->pin_config[i].func == func && | |
69 | info->pin_config[i].chan == chan) { | |
70 | pin1 = &info->pin_config[i]; | |
71 | break; | |
72 | } | |
73 | } | |
74 | if (pin1 && i == pin) | |
75 | return 0; | |
76 | ||
77 | /* Check the desired function and channel. */ | |
78 | switch (func) { | |
79 | case PTP_PF_NONE: | |
80 | break; | |
81 | case PTP_PF_EXTTS: | |
82 | if (chan >= info->n_ext_ts) | |
83 | return -EINVAL; | |
84 | break; | |
85 | case PTP_PF_PEROUT: | |
86 | if (chan >= info->n_per_out) | |
87 | return -EINVAL; | |
88 | break; | |
89 | case PTP_PF_PHYSYNC: | |
72df7a72 SS |
90 | if (chan != 0) |
91 | return -EINVAL; | |
6092315d RC |
92 | default: |
93 | return -EINVAL; | |
94 | } | |
95 | ||
6092315d RC |
96 | if (info->verify(info, pin, func, chan)) { |
97 | pr_err("driver cannot use function %u on pin %u\n", func, chan); | |
98 | return -EOPNOTSUPP; | |
99 | } | |
100 | ||
101 | /* Disable whatever function was previously assigned. */ | |
102 | if (pin1) { | |
103 | ptp_disable_pinfunc(info, func, chan); | |
104 | pin1->func = PTP_PF_NONE; | |
105 | pin1->chan = 0; | |
106 | } | |
107 | ptp_disable_pinfunc(info, pin2->func, pin2->chan); | |
108 | pin2->func = func; | |
109 | pin2->chan = chan; | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
d94ba80e RC |
114 | int ptp_open(struct posix_clock *pc, fmode_t fmode) |
115 | { | |
116 | return 0; | |
117 | } | |
118 | ||
119 | long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) | |
120 | { | |
121 | struct ptp_clock_caps caps; | |
122 | struct ptp_clock_request req; | |
c3484c27 | 123 | struct ptp_sys_offset *sysoff = NULL; |
719f1aa4 | 124 | struct ptp_sys_offset_precise precise_offset; |
6092315d | 125 | struct ptp_pin_desc pd; |
d94ba80e RC |
126 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
127 | struct ptp_clock_info *ops = ptp->info; | |
215b13dd | 128 | struct ptp_clock_time *pct; |
e13cfcb0 | 129 | struct timespec64 ts; |
719f1aa4 | 130 | struct system_device_crosststamp xtstamp; |
d94ba80e | 131 | int enable, err = 0; |
6092315d | 132 | unsigned int i, pin_index; |
d94ba80e RC |
133 | |
134 | switch (cmd) { | |
135 | ||
136 | case PTP_CLOCK_GETCAPS: | |
137 | memset(&caps, 0, sizeof(caps)); | |
138 | caps.max_adj = ptp->info->max_adj; | |
139 | caps.n_alarm = ptp->info->n_alarm; | |
140 | caps.n_ext_ts = ptp->info->n_ext_ts; | |
141 | caps.n_per_out = ptp->info->n_per_out; | |
142 | caps.pps = ptp->info->pps; | |
6092315d | 143 | caps.n_pins = ptp->info->n_pins; |
719f1aa4 | 144 | caps.cross_timestamping = ptp->info->getcrosststamp != NULL; |
e23ef227 DC |
145 | if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) |
146 | err = -EFAULT; | |
d94ba80e RC |
147 | break; |
148 | ||
149 | case PTP_EXTTS_REQUEST: | |
150 | if (copy_from_user(&req.extts, (void __user *)arg, | |
151 | sizeof(req.extts))) { | |
152 | err = -EFAULT; | |
153 | break; | |
154 | } | |
155 | if (req.extts.index >= ops->n_ext_ts) { | |
156 | err = -EINVAL; | |
157 | break; | |
158 | } | |
159 | req.type = PTP_CLK_REQ_EXTTS; | |
160 | enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; | |
161 | err = ops->enable(ops, &req, enable); | |
162 | break; | |
163 | ||
164 | case PTP_PEROUT_REQUEST: | |
165 | if (copy_from_user(&req.perout, (void __user *)arg, | |
166 | sizeof(req.perout))) { | |
167 | err = -EFAULT; | |
168 | break; | |
169 | } | |
170 | if (req.perout.index >= ops->n_per_out) { | |
171 | err = -EINVAL; | |
172 | break; | |
173 | } | |
174 | req.type = PTP_CLK_REQ_PEROUT; | |
175 | enable = req.perout.period.sec || req.perout.period.nsec; | |
176 | err = ops->enable(ops, &req, enable); | |
177 | break; | |
178 | ||
179 | case PTP_ENABLE_PPS: | |
180 | if (!capable(CAP_SYS_TIME)) | |
181 | return -EPERM; | |
182 | req.type = PTP_CLK_REQ_PPS; | |
183 | enable = arg ? 1 : 0; | |
184 | err = ops->enable(ops, &req, enable); | |
185 | break; | |
186 | ||
719f1aa4 CH |
187 | case PTP_SYS_OFFSET_PRECISE: |
188 | if (!ptp->info->getcrosststamp) { | |
189 | err = -EOPNOTSUPP; | |
190 | break; | |
191 | } | |
192 | err = ptp->info->getcrosststamp(ptp->info, &xtstamp); | |
193 | if (err) | |
194 | break; | |
195 | ||
196 | ts = ktime_to_timespec64(xtstamp.device); | |
197 | precise_offset.device.sec = ts.tv_sec; | |
198 | precise_offset.device.nsec = ts.tv_nsec; | |
199 | ts = ktime_to_timespec64(xtstamp.sys_realtime); | |
200 | precise_offset.sys_realtime.sec = ts.tv_sec; | |
201 | precise_offset.sys_realtime.nsec = ts.tv_nsec; | |
202 | ts = ktime_to_timespec64(xtstamp.sys_monoraw); | |
203 | precise_offset.sys_monoraw.sec = ts.tv_sec; | |
204 | precise_offset.sys_monoraw.nsec = ts.tv_nsec; | |
205 | if (copy_to_user((void __user *)arg, &precise_offset, | |
206 | sizeof(precise_offset))) | |
207 | err = -EFAULT; | |
208 | break; | |
209 | ||
215b13dd | 210 | case PTP_SYS_OFFSET: |
c3484c27 RC |
211 | sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL); |
212 | if (!sysoff) { | |
213 | err = -ENOMEM; | |
214 | break; | |
215 | } | |
216 | if (copy_from_user(sysoff, (void __user *)arg, | |
217 | sizeof(*sysoff))) { | |
215b13dd RC |
218 | err = -EFAULT; |
219 | break; | |
220 | } | |
c3484c27 | 221 | if (sysoff->n_samples > PTP_MAX_SAMPLES) { |
215b13dd RC |
222 | err = -EINVAL; |
223 | break; | |
224 | } | |
c3484c27 RC |
225 | pct = &sysoff->ts[0]; |
226 | for (i = 0; i < sysoff->n_samples; i++) { | |
e13cfcb0 | 227 | getnstimeofday64(&ts); |
215b13dd RC |
228 | pct->sec = ts.tv_sec; |
229 | pct->nsec = ts.tv_nsec; | |
230 | pct++; | |
ed7c6317 | 231 | ptp->info->gettime64(ptp->info, &ts); |
215b13dd RC |
232 | pct->sec = ts.tv_sec; |
233 | pct->nsec = ts.tv_nsec; | |
234 | pct++; | |
235 | } | |
e13cfcb0 | 236 | getnstimeofday64(&ts); |
215b13dd RC |
237 | pct->sec = ts.tv_sec; |
238 | pct->nsec = ts.tv_nsec; | |
c3484c27 | 239 | if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) |
215b13dd RC |
240 | err = -EFAULT; |
241 | break; | |
242 | ||
6092315d RC |
243 | case PTP_PIN_GETFUNC: |
244 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { | |
245 | err = -EFAULT; | |
246 | break; | |
247 | } | |
248 | pin_index = pd.index; | |
249 | if (pin_index >= ops->n_pins) { | |
250 | err = -EINVAL; | |
251 | break; | |
252 | } | |
253 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) | |
254 | return -ERESTARTSYS; | |
255 | pd = ops->pin_config[pin_index]; | |
256 | mutex_unlock(&ptp->pincfg_mux); | |
257 | if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) | |
258 | err = -EFAULT; | |
259 | break; | |
260 | ||
261 | case PTP_PIN_SETFUNC: | |
262 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { | |
263 | err = -EFAULT; | |
264 | break; | |
265 | } | |
266 | pin_index = pd.index; | |
267 | if (pin_index >= ops->n_pins) { | |
268 | err = -EINVAL; | |
269 | break; | |
270 | } | |
271 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) | |
272 | return -ERESTARTSYS; | |
273 | err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); | |
274 | mutex_unlock(&ptp->pincfg_mux); | |
275 | break; | |
276 | ||
d94ba80e RC |
277 | default: |
278 | err = -ENOTTY; | |
279 | break; | |
280 | } | |
c3484c27 RC |
281 | |
282 | kfree(sysoff); | |
d94ba80e RC |
283 | return err; |
284 | } | |
285 | ||
286 | unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait) | |
287 | { | |
288 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); | |
289 | ||
290 | poll_wait(fp, &ptp->tsev_wq, wait); | |
291 | ||
292 | return queue_cnt(&ptp->tsevq) ? POLLIN : 0; | |
293 | } | |
294 | ||
c7ec0bad RC |
295 | #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) |
296 | ||
d94ba80e RC |
297 | ssize_t ptp_read(struct posix_clock *pc, |
298 | uint rdflags, char __user *buf, size_t cnt) | |
299 | { | |
300 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); | |
301 | struct timestamp_event_queue *queue = &ptp->tsevq; | |
c7ec0bad | 302 | struct ptp_extts_event *event; |
d94ba80e RC |
303 | unsigned long flags; |
304 | size_t qcnt, i; | |
c7ec0bad | 305 | int result; |
d94ba80e RC |
306 | |
307 | if (cnt % sizeof(struct ptp_extts_event) != 0) | |
308 | return -EINVAL; | |
309 | ||
c7ec0bad RC |
310 | if (cnt > EXTTS_BUFSIZE) |
311 | cnt = EXTTS_BUFSIZE; | |
d94ba80e RC |
312 | |
313 | cnt = cnt / sizeof(struct ptp_extts_event); | |
314 | ||
315 | if (mutex_lock_interruptible(&ptp->tsevq_mux)) | |
316 | return -ERESTARTSYS; | |
317 | ||
318 | if (wait_event_interruptible(ptp->tsev_wq, | |
319 | ptp->defunct || queue_cnt(queue))) { | |
320 | mutex_unlock(&ptp->tsevq_mux); | |
321 | return -ERESTARTSYS; | |
322 | } | |
323 | ||
fb5a18cf DC |
324 | if (ptp->defunct) { |
325 | mutex_unlock(&ptp->tsevq_mux); | |
d94ba80e | 326 | return -ENODEV; |
fb5a18cf | 327 | } |
d94ba80e | 328 | |
c7ec0bad RC |
329 | event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); |
330 | if (!event) { | |
331 | mutex_unlock(&ptp->tsevq_mux); | |
332 | return -ENOMEM; | |
333 | } | |
334 | ||
d94ba80e RC |
335 | spin_lock_irqsave(&queue->lock, flags); |
336 | ||
337 | qcnt = queue_cnt(queue); | |
338 | ||
339 | if (cnt > qcnt) | |
340 | cnt = qcnt; | |
341 | ||
342 | for (i = 0; i < cnt; i++) { | |
343 | event[i] = queue->buf[queue->head]; | |
344 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; | |
345 | } | |
346 | ||
347 | spin_unlock_irqrestore(&queue->lock, flags); | |
348 | ||
349 | cnt = cnt * sizeof(struct ptp_extts_event); | |
350 | ||
351 | mutex_unlock(&ptp->tsevq_mux); | |
352 | ||
c7ec0bad | 353 | result = cnt; |
fb5a18cf | 354 | if (copy_to_user(buf, event, cnt)) |
c7ec0bad | 355 | result = -EFAULT; |
d94ba80e | 356 | |
c7ec0bad RC |
357 | kfree(event); |
358 | return result; | |
d94ba80e | 359 | } |