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 memset(&caps
, 0, sizeof(caps
));
130 caps
.max_adj
= ptp
->info
->max_adj
;
131 caps
.n_alarm
= ptp
->info
->n_alarm
;
132 caps
.n_ext_ts
= ptp
->info
->n_ext_ts
;
133 caps
.n_per_out
= ptp
->info
->n_per_out
;
134 caps
.pps
= ptp
->info
->pps
;
135 caps
.n_pins
= ptp
->info
->n_pins
;
136 caps
.cross_timestamping
= ptp
->info
->getcrosststamp
!= NULL
;
137 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
141 case PTP_EXTTS_REQUEST
:
142 if (copy_from_user(&req
.extts
, (void __user
*)arg
,
143 sizeof(req
.extts
))) {
147 if (req
.extts
.index
>= ops
->n_ext_ts
) {
151 req
.type
= PTP_CLK_REQ_EXTTS
;
152 enable
= req
.extts
.flags
& PTP_ENABLE_FEATURE
? 1 : 0;
153 err
= ops
->enable(ops
, &req
, enable
);
156 case PTP_PEROUT_REQUEST
:
157 if (copy_from_user(&req
.perout
, (void __user
*)arg
,
158 sizeof(req
.perout
))) {
162 if (req
.perout
.index
>= ops
->n_per_out
) {
166 req
.type
= PTP_CLK_REQ_PEROUT
;
167 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
168 err
= ops
->enable(ops
, &req
, enable
);
172 if (!capable(CAP_SYS_TIME
))
174 req
.type
= PTP_CLK_REQ_PPS
;
175 enable
= arg
? 1 : 0;
176 err
= ops
->enable(ops
, &req
, enable
);
179 case PTP_SYS_OFFSET_PRECISE
:
180 if (!ptp
->info
->getcrosststamp
) {
184 err
= ptp
->info
->getcrosststamp(ptp
->info
, &xtstamp
);
188 memset(&precise_offset
, 0, sizeof(precise_offset
));
189 ts
= ktime_to_timespec64(xtstamp
.device
);
190 precise_offset
.device
.sec
= ts
.tv_sec
;
191 precise_offset
.device
.nsec
= ts
.tv_nsec
;
192 ts
= ktime_to_timespec64(xtstamp
.sys_realtime
);
193 precise_offset
.sys_realtime
.sec
= ts
.tv_sec
;
194 precise_offset
.sys_realtime
.nsec
= ts
.tv_nsec
;
195 ts
= ktime_to_timespec64(xtstamp
.sys_monoraw
);
196 precise_offset
.sys_monoraw
.sec
= ts
.tv_sec
;
197 precise_offset
.sys_monoraw
.nsec
= ts
.tv_nsec
;
198 if (copy_to_user((void __user
*)arg
, &precise_offset
,
199 sizeof(precise_offset
)))
203 case PTP_SYS_OFFSET_EXTENDED
:
204 if (!ptp
->info
->gettimex64
) {
208 extoff
= memdup_user((void __user
*)arg
, sizeof(*extoff
));
209 if (IS_ERR(extoff
)) {
210 err
= PTR_ERR(extoff
);
214 if (extoff
->n_samples
> PTP_MAX_SAMPLES
215 || extoff
->rsv
[0] || extoff
->rsv
[1] || extoff
->rsv
[2]) {
219 for (i
= 0; i
< extoff
->n_samples
; i
++) {
220 err
= ptp
->info
->gettimex64(ptp
->info
, &ts
, &sts
);
223 extoff
->ts
[i
][0].sec
= sts
.pre_ts
.tv_sec
;
224 extoff
->ts
[i
][0].nsec
= sts
.pre_ts
.tv_nsec
;
225 extoff
->ts
[i
][1].sec
= ts
.tv_sec
;
226 extoff
->ts
[i
][1].nsec
= ts
.tv_nsec
;
227 extoff
->ts
[i
][2].sec
= sts
.post_ts
.tv_sec
;
228 extoff
->ts
[i
][2].nsec
= sts
.post_ts
.tv_nsec
;
230 if (copy_to_user((void __user
*)arg
, extoff
, sizeof(*extoff
)))
235 sysoff
= memdup_user((void __user
*)arg
, sizeof(*sysoff
));
236 if (IS_ERR(sysoff
)) {
237 err
= PTR_ERR(sysoff
);
241 if (sysoff
->n_samples
> PTP_MAX_SAMPLES
) {
245 pct
= &sysoff
->ts
[0];
246 for (i
= 0; i
< sysoff
->n_samples
; i
++) {
247 ktime_get_real_ts64(&ts
);
248 pct
->sec
= ts
.tv_sec
;
249 pct
->nsec
= ts
.tv_nsec
;
252 err
= ops
->gettimex64(ops
, &ts
, NULL
);
254 err
= ops
->gettime64(ops
, &ts
);
257 pct
->sec
= ts
.tv_sec
;
258 pct
->nsec
= ts
.tv_nsec
;
261 ktime_get_real_ts64(&ts
);
262 pct
->sec
= ts
.tv_sec
;
263 pct
->nsec
= ts
.tv_nsec
;
264 if (copy_to_user((void __user
*)arg
, sysoff
, sizeof(*sysoff
)))
268 case PTP_PIN_GETFUNC
:
269 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
273 pin_index
= pd
.index
;
274 if (pin_index
>= ops
->n_pins
) {
278 pin_index
= array_index_nospec(pin_index
, ops
->n_pins
);
279 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
281 pd
= ops
->pin_config
[pin_index
];
282 mutex_unlock(&ptp
->pincfg_mux
);
283 if (!err
&& copy_to_user((void __user
*)arg
, &pd
, sizeof(pd
)))
287 case PTP_PIN_SETFUNC
:
288 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
292 pin_index
= pd
.index
;
293 if (pin_index
>= ops
->n_pins
) {
297 pin_index
= array_index_nospec(pin_index
, ops
->n_pins
);
298 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
300 err
= ptp_set_pinfunc(ptp
, pin_index
, pd
.func
, pd
.chan
);
301 mutex_unlock(&ptp
->pincfg_mux
);
315 __poll_t
ptp_poll(struct posix_clock
*pc
, struct file
*fp
, poll_table
*wait
)
317 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
319 poll_wait(fp
, &ptp
->tsev_wq
, wait
);
321 return queue_cnt(&ptp
->tsevq
) ? EPOLLIN
: 0;
324 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
326 ssize_t
ptp_read(struct posix_clock
*pc
,
327 uint rdflags
, char __user
*buf
, size_t cnt
)
329 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
330 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
331 struct ptp_extts_event
*event
;
336 if (cnt
% sizeof(struct ptp_extts_event
) != 0)
339 if (cnt
> EXTTS_BUFSIZE
)
342 cnt
= cnt
/ sizeof(struct ptp_extts_event
);
344 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
347 if (wait_event_interruptible(ptp
->tsev_wq
,
348 ptp
->defunct
|| queue_cnt(queue
))) {
349 mutex_unlock(&ptp
->tsevq_mux
);
354 mutex_unlock(&ptp
->tsevq_mux
);
358 event
= kmalloc(EXTTS_BUFSIZE
, GFP_KERNEL
);
360 mutex_unlock(&ptp
->tsevq_mux
);
364 spin_lock_irqsave(&queue
->lock
, flags
);
366 qcnt
= queue_cnt(queue
);
371 for (i
= 0; i
< cnt
; i
++) {
372 event
[i
] = queue
->buf
[queue
->head
];
373 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
376 spin_unlock_irqrestore(&queue
->lock
, flags
);
378 cnt
= cnt
* sizeof(struct ptp_extts_event
);
380 mutex_unlock(&ptp
->tsevq_mux
);
383 if (copy_to_user(buf
, event
, cnt
))