2 * PTP 1588 clock support - character device implementation.
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/module.h>
21 #include <linux/posix-clock.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/timekeeping.h>
27 #include <linux/nospec.h>
29 #include "ptp_private.h"
31 static int ptp_disable_pinfunc(struct ptp_clock_info
*ops
,
32 enum ptp_pin_function func
, unsigned int chan
)
34 struct ptp_clock_request rq
;
37 memset(&rq
, 0, sizeof(rq
));
43 rq
.type
= PTP_CLK_REQ_EXTTS
;
44 rq
.extts
.index
= chan
;
45 err
= ops
->enable(ops
, &rq
, 0);
48 rq
.type
= PTP_CLK_REQ_PEROUT
;
49 rq
.perout
.index
= chan
;
50 err
= ops
->enable(ops
, &rq
, 0);
61 int ptp_set_pinfunc(struct ptp_clock
*ptp
, unsigned int pin
,
62 enum ptp_pin_function func
, unsigned int chan
)
64 struct ptp_clock_info
*info
= ptp
->info
;
65 struct ptp_pin_desc
*pin1
= NULL
, *pin2
= &info
->pin_config
[pin
];
68 /* Check to see if any other pin previously had this function. */
69 for (i
= 0; i
< info
->n_pins
; i
++) {
70 if (info
->pin_config
[i
].func
== func
&&
71 info
->pin_config
[i
].chan
== chan
) {
72 pin1
= &info
->pin_config
[i
];
79 /* Check the desired function and channel. */
84 if (chan
>= info
->n_ext_ts
)
88 if (chan
>= info
->n_per_out
)
99 if (info
->verify(info
, pin
, func
, chan
)) {
100 pr_err("driver cannot use function %u on pin %u\n", func
, chan
);
104 /* Disable whatever function was previously assigned. */
106 ptp_disable_pinfunc(info
, func
, chan
);
107 pin1
->func
= PTP_PF_NONE
;
110 ptp_disable_pinfunc(info
, pin2
->func
, pin2
->chan
);
117 int ptp_open(struct posix_clock
*pc
, fmode_t fmode
)
122 long ptp_ioctl(struct posix_clock
*pc
, unsigned int cmd
, unsigned long arg
)
124 struct ptp_clock_caps caps
;
125 struct ptp_clock_request req
;
126 struct ptp_sys_offset
*sysoff
= NULL
;
127 struct ptp_sys_offset_precise precise_offset
;
128 struct ptp_pin_desc pd
;
129 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
130 struct ptp_clock_info
*ops
= ptp
->info
;
131 struct ptp_clock_time
*pct
;
132 struct timespec64 ts
;
133 struct system_device_crosststamp xtstamp
;
135 unsigned int i
, pin_index
;
139 case PTP_CLOCK_GETCAPS
:
140 memset(&caps
, 0, sizeof(caps
));
141 caps
.max_adj
= ptp
->info
->max_adj
;
142 caps
.n_alarm
= ptp
->info
->n_alarm
;
143 caps
.n_ext_ts
= ptp
->info
->n_ext_ts
;
144 caps
.n_per_out
= ptp
->info
->n_per_out
;
145 caps
.pps
= ptp
->info
->pps
;
146 caps
.n_pins
= ptp
->info
->n_pins
;
147 caps
.cross_timestamping
= ptp
->info
->getcrosststamp
!= NULL
;
148 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
152 case PTP_EXTTS_REQUEST
:
153 if (copy_from_user(&req
.extts
, (void __user
*)arg
,
154 sizeof(req
.extts
))) {
158 if (req
.extts
.index
>= ops
->n_ext_ts
) {
162 req
.type
= PTP_CLK_REQ_EXTTS
;
163 enable
= req
.extts
.flags
& PTP_ENABLE_FEATURE
? 1 : 0;
164 err
= ops
->enable(ops
, &req
, enable
);
167 case PTP_PEROUT_REQUEST
:
168 if (copy_from_user(&req
.perout
, (void __user
*)arg
,
169 sizeof(req
.perout
))) {
173 if (req
.perout
.index
>= ops
->n_per_out
) {
177 req
.type
= PTP_CLK_REQ_PEROUT
;
178 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
179 err
= ops
->enable(ops
, &req
, enable
);
183 if (!capable(CAP_SYS_TIME
))
185 req
.type
= PTP_CLK_REQ_PPS
;
186 enable
= arg
? 1 : 0;
187 err
= ops
->enable(ops
, &req
, enable
);
190 case PTP_SYS_OFFSET_PRECISE
:
191 if (!ptp
->info
->getcrosststamp
) {
195 err
= ptp
->info
->getcrosststamp(ptp
->info
, &xtstamp
);
199 memset(&precise_offset
, 0, sizeof(precise_offset
));
200 ts
= ktime_to_timespec64(xtstamp
.device
);
201 precise_offset
.device
.sec
= ts
.tv_sec
;
202 precise_offset
.device
.nsec
= ts
.tv_nsec
;
203 ts
= ktime_to_timespec64(xtstamp
.sys_realtime
);
204 precise_offset
.sys_realtime
.sec
= ts
.tv_sec
;
205 precise_offset
.sys_realtime
.nsec
= ts
.tv_nsec
;
206 ts
= ktime_to_timespec64(xtstamp
.sys_monoraw
);
207 precise_offset
.sys_monoraw
.sec
= ts
.tv_sec
;
208 precise_offset
.sys_monoraw
.nsec
= ts
.tv_nsec
;
209 if (copy_to_user((void __user
*)arg
, &precise_offset
,
210 sizeof(precise_offset
)))
215 sysoff
= memdup_user((void __user
*)arg
, sizeof(*sysoff
));
216 if (IS_ERR(sysoff
)) {
217 err
= PTR_ERR(sysoff
);
221 if (sysoff
->n_samples
> PTP_MAX_SAMPLES
) {
225 pct
= &sysoff
->ts
[0];
226 for (i
= 0; i
< sysoff
->n_samples
; i
++) {
227 ktime_get_real_ts64(&ts
);
228 pct
->sec
= ts
.tv_sec
;
229 pct
->nsec
= ts
.tv_nsec
;
231 err
= ptp
->info
->gettime64(ptp
->info
, &ts
);
234 pct
->sec
= ts
.tv_sec
;
235 pct
->nsec
= ts
.tv_nsec
;
238 ktime_get_real_ts64(&ts
);
239 pct
->sec
= ts
.tv_sec
;
240 pct
->nsec
= ts
.tv_nsec
;
241 if (copy_to_user((void __user
*)arg
, sysoff
, sizeof(*sysoff
)))
245 case PTP_PIN_GETFUNC
:
246 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
250 pin_index
= pd
.index
;
251 if (pin_index
>= ops
->n_pins
) {
255 pin_index
= array_index_nospec(pin_index
, ops
->n_pins
);
256 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
258 pd
= ops
->pin_config
[pin_index
];
259 mutex_unlock(&ptp
->pincfg_mux
);
260 if (!err
&& copy_to_user((void __user
*)arg
, &pd
, sizeof(pd
)))
264 case PTP_PIN_SETFUNC
:
265 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
269 pin_index
= pd
.index
;
270 if (pin_index
>= ops
->n_pins
) {
274 pin_index
= array_index_nospec(pin_index
, ops
->n_pins
);
275 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
277 err
= ptp_set_pinfunc(ptp
, pin_index
, pd
.func
, pd
.chan
);
278 mutex_unlock(&ptp
->pincfg_mux
);
291 __poll_t
ptp_poll(struct posix_clock
*pc
, struct file
*fp
, poll_table
*wait
)
293 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
295 poll_wait(fp
, &ptp
->tsev_wq
, wait
);
297 return queue_cnt(&ptp
->tsevq
) ? EPOLLIN
: 0;
300 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
302 ssize_t
ptp_read(struct posix_clock
*pc
,
303 uint rdflags
, char __user
*buf
, size_t cnt
)
305 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
306 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
307 struct ptp_extts_event
*event
;
312 if (cnt
% sizeof(struct ptp_extts_event
) != 0)
315 if (cnt
> EXTTS_BUFSIZE
)
318 cnt
= cnt
/ sizeof(struct ptp_extts_event
);
320 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
323 if (wait_event_interruptible(ptp
->tsev_wq
,
324 ptp
->defunct
|| queue_cnt(queue
))) {
325 mutex_unlock(&ptp
->tsevq_mux
);
330 mutex_unlock(&ptp
->tsevq_mux
);
334 event
= kmalloc(EXTTS_BUFSIZE
, GFP_KERNEL
);
336 mutex_unlock(&ptp
->tsevq_mux
);
340 spin_lock_irqsave(&queue
->lock
, flags
);
342 qcnt
= queue_cnt(queue
);
347 for (i
= 0; i
< cnt
; i
++) {
348 event
[i
] = queue
->buf
[queue
->head
];
349 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
352 spin_unlock_irqrestore(&queue
->lock
, flags
);
354 cnt
= cnt
* sizeof(struct ptp_extts_event
);
356 mutex_unlock(&ptp
->tsevq_mux
);
359 if (copy_to_user(buf
, event
, cnt
))