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 "ptp_private.h"
29 static int ptp_disable_pinfunc(struct ptp_clock_info
*ops
,
30 enum ptp_pin_function func
, unsigned int chan
)
32 struct ptp_clock_request rq
;
35 memset(&rq
, 0, sizeof(rq
));
41 rq
.type
= PTP_CLK_REQ_EXTTS
;
42 rq
.extts
.index
= chan
;
43 err
= ops
->enable(ops
, &rq
, 0);
46 rq
.type
= PTP_CLK_REQ_PEROUT
;
47 rq
.perout
.index
= chan
;
48 err
= ops
->enable(ops
, &rq
, 0);
59 int ptp_set_pinfunc(struct ptp_clock
*ptp
, unsigned int pin
,
60 enum ptp_pin_function func
, unsigned int chan
)
62 struct ptp_clock_info
*info
= ptp
->info
;
63 struct ptp_pin_desc
*pin1
= NULL
, *pin2
= &info
->pin_config
[pin
];
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
];
77 /* Check the desired function and channel. */
82 if (chan
>= info
->n_ext_ts
)
86 if (chan
>= info
->n_per_out
)
96 if (info
->verify(info
, pin
, func
, chan
)) {
97 pr_err("driver cannot use function %u on pin %u\n", func
, chan
);
101 /* Disable whatever function was previously assigned. */
103 ptp_disable_pinfunc(info
, func
, chan
);
104 pin1
->func
= PTP_PF_NONE
;
107 ptp_disable_pinfunc(info
, pin2
->func
, pin2
->chan
);
114 int ptp_open(struct posix_clock
*pc
, fmode_t fmode
)
119 long ptp_ioctl(struct posix_clock
*pc
, unsigned int cmd
, unsigned long arg
)
121 struct ptp_clock_caps caps
;
122 struct ptp_clock_request req
;
123 struct ptp_sys_offset
*sysoff
= NULL
;
124 struct ptp_sys_offset_precise precise_offset
;
125 struct ptp_pin_desc pd
;
126 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
127 struct ptp_clock_info
*ops
= ptp
->info
;
128 struct ptp_clock_time
*pct
;
129 struct timespec64 ts
;
130 struct system_device_crosststamp xtstamp
;
132 unsigned int i
, pin_index
;
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
;
143 caps
.n_pins
= ptp
->info
->n_pins
;
144 caps
.cross_timestamping
= ptp
->info
->getcrosststamp
!= NULL
;
145 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
149 case PTP_EXTTS_REQUEST
:
150 if (copy_from_user(&req
.extts
, (void __user
*)arg
,
151 sizeof(req
.extts
))) {
155 if (req
.extts
.index
>= ops
->n_ext_ts
) {
159 req
.type
= PTP_CLK_REQ_EXTTS
;
160 enable
= req
.extts
.flags
& PTP_ENABLE_FEATURE
? 1 : 0;
161 err
= ops
->enable(ops
, &req
, enable
);
164 case PTP_PEROUT_REQUEST
:
165 if (copy_from_user(&req
.perout
, (void __user
*)arg
,
166 sizeof(req
.perout
))) {
170 if (req
.perout
.index
>= ops
->n_per_out
) {
174 req
.type
= PTP_CLK_REQ_PEROUT
;
175 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
176 err
= ops
->enable(ops
, &req
, enable
);
180 if (!capable(CAP_SYS_TIME
))
182 req
.type
= PTP_CLK_REQ_PPS
;
183 enable
= arg
? 1 : 0;
184 err
= ops
->enable(ops
, &req
, enable
);
187 case PTP_SYS_OFFSET_PRECISE
:
188 if (!ptp
->info
->getcrosststamp
) {
192 err
= ptp
->info
->getcrosststamp(ptp
->info
, &xtstamp
);
196 memset(&precise_offset
, 0, sizeof(precise_offset
));
197 ts
= ktime_to_timespec64(xtstamp
.device
);
198 precise_offset
.device
.sec
= ts
.tv_sec
;
199 precise_offset
.device
.nsec
= ts
.tv_nsec
;
200 ts
= ktime_to_timespec64(xtstamp
.sys_realtime
);
201 precise_offset
.sys_realtime
.sec
= ts
.tv_sec
;
202 precise_offset
.sys_realtime
.nsec
= ts
.tv_nsec
;
203 ts
= ktime_to_timespec64(xtstamp
.sys_monoraw
);
204 precise_offset
.sys_monoraw
.sec
= ts
.tv_sec
;
205 precise_offset
.sys_monoraw
.nsec
= ts
.tv_nsec
;
206 if (copy_to_user((void __user
*)arg
, &precise_offset
,
207 sizeof(precise_offset
)))
212 sysoff
= memdup_user((void __user
*)arg
, sizeof(*sysoff
));
213 if (IS_ERR(sysoff
)) {
214 err
= PTR_ERR(sysoff
);
218 if (sysoff
->n_samples
> PTP_MAX_SAMPLES
) {
222 pct
= &sysoff
->ts
[0];
223 for (i
= 0; i
< sysoff
->n_samples
; i
++) {
224 getnstimeofday64(&ts
);
225 pct
->sec
= ts
.tv_sec
;
226 pct
->nsec
= ts
.tv_nsec
;
228 ptp
->info
->gettime64(ptp
->info
, &ts
);
229 pct
->sec
= ts
.tv_sec
;
230 pct
->nsec
= ts
.tv_nsec
;
233 getnstimeofday64(&ts
);
234 pct
->sec
= ts
.tv_sec
;
235 pct
->nsec
= ts
.tv_nsec
;
236 if (copy_to_user((void __user
*)arg
, sysoff
, sizeof(*sysoff
)))
240 case PTP_PIN_GETFUNC
:
241 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
245 pin_index
= pd
.index
;
246 if (pin_index
>= ops
->n_pins
) {
250 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
252 pd
= ops
->pin_config
[pin_index
];
253 mutex_unlock(&ptp
->pincfg_mux
);
254 if (!err
&& copy_to_user((void __user
*)arg
, &pd
, sizeof(pd
)))
258 case PTP_PIN_SETFUNC
:
259 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
263 pin_index
= pd
.index
;
264 if (pin_index
>= ops
->n_pins
) {
268 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
270 err
= ptp_set_pinfunc(ptp
, pin_index
, pd
.func
, pd
.chan
);
271 mutex_unlock(&ptp
->pincfg_mux
);
283 __poll_t
ptp_poll(struct posix_clock
*pc
, struct file
*fp
, poll_table
*wait
)
285 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
287 poll_wait(fp
, &ptp
->tsev_wq
, wait
);
289 return queue_cnt(&ptp
->tsevq
) ? EPOLLIN
: 0;
292 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
294 ssize_t
ptp_read(struct posix_clock
*pc
,
295 uint rdflags
, char __user
*buf
, size_t cnt
)
297 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
298 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
299 struct ptp_extts_event
*event
;
304 if (cnt
% sizeof(struct ptp_extts_event
) != 0)
307 if (cnt
> EXTTS_BUFSIZE
)
310 cnt
= cnt
/ sizeof(struct ptp_extts_event
);
312 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
315 if (wait_event_interruptible(ptp
->tsev_wq
,
316 ptp
->defunct
|| queue_cnt(queue
))) {
317 mutex_unlock(&ptp
->tsevq_mux
);
322 mutex_unlock(&ptp
->tsevq_mux
);
326 event
= kmalloc(EXTTS_BUFSIZE
, GFP_KERNEL
);
328 mutex_unlock(&ptp
->tsevq_mux
);
332 spin_lock_irqsave(&queue
->lock
, flags
);
334 qcnt
= queue_cnt(queue
);
339 for (i
= 0; i
< cnt
; i
++) {
340 event
[i
] = queue
->buf
[queue
->head
];
341 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
344 spin_unlock_irqrestore(&queue
->lock
, flags
);
346 cnt
= cnt
* sizeof(struct ptp_extts_event
);
348 mutex_unlock(&ptp
->tsevq_mux
);
351 if (copy_to_user(buf
, event
, cnt
))