1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PTP 1588 clock support - character device implementation.
5 * Copyright (C) 2010 OMICRON electronics GmbH
7 #include <linux/module.h>
8 #include <linux/posix-clock.h>
9 #include <linux/poll.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/timekeeping.h>
14 #include <linux/nospec.h>
16 #include "ptp_private.h"
18 static int ptp_disable_pinfunc(struct ptp_clock_info
*ops
,
19 enum ptp_pin_function func
, unsigned int chan
)
21 struct ptp_clock_request rq
;
24 memset(&rq
, 0, sizeof(rq
));
30 rq
.type
= PTP_CLK_REQ_EXTTS
;
31 rq
.extts
.index
= chan
;
32 err
= ops
->enable(ops
, &rq
, 0);
35 rq
.type
= PTP_CLK_REQ_PEROUT
;
36 rq
.perout
.index
= chan
;
37 err
= ops
->enable(ops
, &rq
, 0);
48 int ptp_set_pinfunc(struct ptp_clock
*ptp
, unsigned int pin
,
49 enum ptp_pin_function func
, unsigned int chan
)
51 struct ptp_clock_info
*info
= ptp
->info
;
52 struct ptp_pin_desc
*pin1
= NULL
, *pin2
= &info
->pin_config
[pin
];
55 /* Check to see if any other pin previously had this function. */
56 for (i
= 0; i
< info
->n_pins
; i
++) {
57 if (info
->pin_config
[i
].func
== func
&&
58 info
->pin_config
[i
].chan
== chan
) {
59 pin1
= &info
->pin_config
[i
];
66 /* Check the desired function and channel. */
71 if (chan
>= info
->n_ext_ts
)
75 if (chan
>= info
->n_per_out
)
86 if (info
->verify(info
, pin
, func
, chan
)) {
87 pr_err("driver cannot use function %u on pin %u\n", func
, chan
);
91 /* Disable whatever function was previously assigned. */
93 ptp_disable_pinfunc(info
, func
, chan
);
94 pin1
->func
= PTP_PF_NONE
;
97 ptp_disable_pinfunc(info
, pin2
->func
, pin2
->chan
);
104 int ptp_open(struct posix_clock
*pc
, fmode_t fmode
)
109 long ptp_ioctl(struct posix_clock
*pc
, unsigned int cmd
, unsigned long arg
)
111 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
112 struct ptp_sys_offset_extended
*extoff
= NULL
;
113 struct ptp_sys_offset_precise precise_offset
;
114 struct system_device_crosststamp xtstamp
;
115 struct ptp_clock_info
*ops
= ptp
->info
;
116 struct ptp_sys_offset
*sysoff
= NULL
;
117 struct ptp_system_timestamp sts
;
118 struct ptp_clock_request req
;
119 struct ptp_clock_caps caps
;
120 struct ptp_clock_time
*pct
;
121 unsigned int i
, pin_index
;
122 struct ptp_pin_desc pd
;
123 struct timespec64 ts
;
128 case PTP_CLOCK_GETCAPS
:
129 case PTP_CLOCK_GETCAPS2
:
130 memset(&caps
, 0, sizeof(caps
));
132 caps
.max_adj
= ptp
->info
->max_adj
;
133 caps
.n_alarm
= ptp
->info
->n_alarm
;
134 caps
.n_ext_ts
= ptp
->info
->n_ext_ts
;
135 caps
.n_per_out
= ptp
->info
->n_per_out
;
136 caps
.pps
= ptp
->info
->pps
;
137 caps
.n_pins
= ptp
->info
->n_pins
;
138 caps
.cross_timestamping
= ptp
->info
->getcrosststamp
!= NULL
;
139 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
143 case PTP_EXTTS_REQUEST
:
144 case PTP_EXTTS_REQUEST2
:
145 memset(&req
, 0, sizeof(req
));
147 if (copy_from_user(&req
.extts
, (void __user
*)arg
,
148 sizeof(req
.extts
))) {
152 if (cmd
== PTP_EXTTS_REQUEST2
) {
153 /* Tell the drivers to check the flags carefully. */
154 req
.extts
.flags
|= PTP_STRICT_FLAGS
;
155 /* Make sure no reserved bit is set. */
156 if ((req
.extts
.flags
& ~PTP_EXTTS_VALID_FLAGS
) ||
157 req
.extts
.rsv
[0] || req
.extts
.rsv
[1]) {
161 /* Ensure one of the rising/falling edge bits is set. */
162 if ((req
.extts
.flags
& PTP_ENABLE_FEATURE
) &&
163 (req
.extts
.flags
& PTP_EXTTS_EDGES
) == 0) {
167 } else if (cmd
== PTP_EXTTS_REQUEST
) {
168 req
.extts
.flags
&= PTP_EXTTS_V1_VALID_FLAGS
;
169 req
.extts
.rsv
[0] = 0;
170 req
.extts
.rsv
[1] = 0;
172 if (req
.extts
.index
>= ops
->n_ext_ts
) {
176 req
.type
= PTP_CLK_REQ_EXTTS
;
177 enable
= req
.extts
.flags
& PTP_ENABLE_FEATURE
? 1 : 0;
178 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
180 err
= ops
->enable(ops
, &req
, enable
);
181 mutex_unlock(&ptp
->pincfg_mux
);
184 case PTP_PEROUT_REQUEST
:
185 case PTP_PEROUT_REQUEST2
:
186 memset(&req
, 0, sizeof(req
));
188 if (copy_from_user(&req
.perout
, (void __user
*)arg
,
189 sizeof(req
.perout
))) {
193 if (((req
.perout
.flags
& ~PTP_PEROUT_VALID_FLAGS
) ||
194 req
.perout
.rsv
[0] || req
.perout
.rsv
[1] ||
195 req
.perout
.rsv
[2] || req
.perout
.rsv
[3]) &&
196 cmd
== PTP_PEROUT_REQUEST2
) {
199 } else if (cmd
== PTP_PEROUT_REQUEST
) {
200 req
.perout
.flags
&= PTP_PEROUT_V1_VALID_FLAGS
;
201 req
.perout
.rsv
[0] = 0;
202 req
.perout
.rsv
[1] = 0;
203 req
.perout
.rsv
[2] = 0;
204 req
.perout
.rsv
[3] = 0;
206 if (req
.perout
.index
>= ops
->n_per_out
) {
210 req
.type
= PTP_CLK_REQ_PEROUT
;
211 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
212 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
214 err
= ops
->enable(ops
, &req
, enable
);
215 mutex_unlock(&ptp
->pincfg_mux
);
219 case PTP_ENABLE_PPS2
:
220 memset(&req
, 0, sizeof(req
));
222 if (!capable(CAP_SYS_TIME
))
224 req
.type
= PTP_CLK_REQ_PPS
;
225 enable
= arg
? 1 : 0;
226 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
228 err
= ops
->enable(ops
, &req
, enable
);
229 mutex_unlock(&ptp
->pincfg_mux
);
232 case PTP_SYS_OFFSET_PRECISE
:
233 case PTP_SYS_OFFSET_PRECISE2
:
234 if (!ptp
->info
->getcrosststamp
) {
238 err
= ptp
->info
->getcrosststamp(ptp
->info
, &xtstamp
);
242 memset(&precise_offset
, 0, sizeof(precise_offset
));
243 ts
= ktime_to_timespec64(xtstamp
.device
);
244 precise_offset
.device
.sec
= ts
.tv_sec
;
245 precise_offset
.device
.nsec
= ts
.tv_nsec
;
246 ts
= ktime_to_timespec64(xtstamp
.sys_realtime
);
247 precise_offset
.sys_realtime
.sec
= ts
.tv_sec
;
248 precise_offset
.sys_realtime
.nsec
= ts
.tv_nsec
;
249 ts
= ktime_to_timespec64(xtstamp
.sys_monoraw
);
250 precise_offset
.sys_monoraw
.sec
= ts
.tv_sec
;
251 precise_offset
.sys_monoraw
.nsec
= ts
.tv_nsec
;
252 if (copy_to_user((void __user
*)arg
, &precise_offset
,
253 sizeof(precise_offset
)))
257 case PTP_SYS_OFFSET_EXTENDED
:
258 case PTP_SYS_OFFSET_EXTENDED2
:
259 if (!ptp
->info
->gettimex64
) {
263 extoff
= memdup_user((void __user
*)arg
, sizeof(*extoff
));
264 if (IS_ERR(extoff
)) {
265 err
= PTR_ERR(extoff
);
269 if (extoff
->n_samples
> PTP_MAX_SAMPLES
270 || extoff
->rsv
[0] || extoff
->rsv
[1] || extoff
->rsv
[2]) {
274 for (i
= 0; i
< extoff
->n_samples
; i
++) {
275 err
= ptp
->info
->gettimex64(ptp
->info
, &ts
, &sts
);
278 extoff
->ts
[i
][0].sec
= sts
.pre_ts
.tv_sec
;
279 extoff
->ts
[i
][0].nsec
= sts
.pre_ts
.tv_nsec
;
280 extoff
->ts
[i
][1].sec
= ts
.tv_sec
;
281 extoff
->ts
[i
][1].nsec
= ts
.tv_nsec
;
282 extoff
->ts
[i
][2].sec
= sts
.post_ts
.tv_sec
;
283 extoff
->ts
[i
][2].nsec
= sts
.post_ts
.tv_nsec
;
285 if (copy_to_user((void __user
*)arg
, extoff
, sizeof(*extoff
)))
290 case PTP_SYS_OFFSET2
:
291 sysoff
= memdup_user((void __user
*)arg
, sizeof(*sysoff
));
292 if (IS_ERR(sysoff
)) {
293 err
= PTR_ERR(sysoff
);
297 if (sysoff
->n_samples
> PTP_MAX_SAMPLES
) {
301 pct
= &sysoff
->ts
[0];
302 for (i
= 0; i
< sysoff
->n_samples
; i
++) {
303 ktime_get_real_ts64(&ts
);
304 pct
->sec
= ts
.tv_sec
;
305 pct
->nsec
= ts
.tv_nsec
;
308 err
= ops
->gettimex64(ops
, &ts
, NULL
);
310 err
= ops
->gettime64(ops
, &ts
);
313 pct
->sec
= ts
.tv_sec
;
314 pct
->nsec
= ts
.tv_nsec
;
317 ktime_get_real_ts64(&ts
);
318 pct
->sec
= ts
.tv_sec
;
319 pct
->nsec
= ts
.tv_nsec
;
320 if (copy_to_user((void __user
*)arg
, sysoff
, sizeof(*sysoff
)))
324 case PTP_PIN_GETFUNC
:
325 case PTP_PIN_GETFUNC2
:
326 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
330 if ((pd
.rsv
[0] || pd
.rsv
[1] || pd
.rsv
[2]
331 || pd
.rsv
[3] || pd
.rsv
[4])
332 && cmd
== PTP_PIN_GETFUNC2
) {
335 } else if (cmd
== PTP_PIN_GETFUNC
) {
342 pin_index
= pd
.index
;
343 if (pin_index
>= ops
->n_pins
) {
347 pin_index
= array_index_nospec(pin_index
, ops
->n_pins
);
348 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
350 pd
= ops
->pin_config
[pin_index
];
351 mutex_unlock(&ptp
->pincfg_mux
);
352 if (!err
&& copy_to_user((void __user
*)arg
, &pd
, sizeof(pd
)))
356 case PTP_PIN_SETFUNC
:
357 case PTP_PIN_SETFUNC2
:
358 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
362 if ((pd
.rsv
[0] || pd
.rsv
[1] || pd
.rsv
[2]
363 || pd
.rsv
[3] || pd
.rsv
[4])
364 && cmd
== PTP_PIN_SETFUNC2
) {
367 } else if (cmd
== PTP_PIN_SETFUNC
) {
374 pin_index
= pd
.index
;
375 if (pin_index
>= ops
->n_pins
) {
379 pin_index
= array_index_nospec(pin_index
, ops
->n_pins
);
380 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
382 err
= ptp_set_pinfunc(ptp
, pin_index
, pd
.func
, pd
.chan
);
383 mutex_unlock(&ptp
->pincfg_mux
);
397 __poll_t
ptp_poll(struct posix_clock
*pc
, struct file
*fp
, poll_table
*wait
)
399 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
401 poll_wait(fp
, &ptp
->tsev_wq
, wait
);
403 return queue_cnt(&ptp
->tsevq
) ? EPOLLIN
: 0;
406 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
408 ssize_t
ptp_read(struct posix_clock
*pc
,
409 uint rdflags
, char __user
*buf
, size_t cnt
)
411 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
412 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
413 struct ptp_extts_event
*event
;
418 if (cnt
% sizeof(struct ptp_extts_event
) != 0)
421 if (cnt
> EXTTS_BUFSIZE
)
424 cnt
= cnt
/ sizeof(struct ptp_extts_event
);
426 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
429 if (wait_event_interruptible(ptp
->tsev_wq
,
430 ptp
->defunct
|| queue_cnt(queue
))) {
431 mutex_unlock(&ptp
->tsevq_mux
);
436 mutex_unlock(&ptp
->tsevq_mux
);
440 event
= kmalloc(EXTTS_BUFSIZE
, GFP_KERNEL
);
442 mutex_unlock(&ptp
->tsevq_mux
);
446 spin_lock_irqsave(&queue
->lock
, flags
);
448 qcnt
= queue_cnt(queue
);
453 for (i
= 0; i
< cnt
; i
++) {
454 event
[i
] = queue
->buf
[queue
->head
];
455 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
458 spin_unlock_irqrestore(&queue
->lock
, flags
);
460 cnt
= cnt
* sizeof(struct ptp_extts_event
);
462 mutex_unlock(&ptp
->tsevq_mux
);
465 if (copy_to_user(buf
, event
, cnt
))