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>
26 #include "ptp_private.h"
28 static int ptp_disable_pinfunc(struct ptp_clock_info
*ops
,
29 enum ptp_pin_function func
, unsigned int chan
)
31 struct ptp_clock_request rq
;
34 memset(&rq
, 0, sizeof(rq
));
40 rq
.type
= PTP_CLK_REQ_EXTTS
;
41 rq
.extts
.index
= chan
;
42 err
= ops
->enable(ops
, &rq
, 0);
45 rq
.type
= PTP_CLK_REQ_PEROUT
;
46 rq
.perout
.index
= chan
;
47 err
= ops
->enable(ops
, &rq
, 0);
58 int ptp_set_pinfunc(struct ptp_clock
*ptp
, unsigned int pin
,
59 enum ptp_pin_function func
, unsigned int chan
)
61 struct ptp_clock_info
*info
= ptp
->info
;
62 struct ptp_pin_desc
*pin1
= NULL
, *pin2
= &info
->pin_config
[pin
];
65 /* Check to see if any other pin previously had this function. */
66 for (i
= 0; i
< info
->n_pins
; i
++) {
67 if (info
->pin_config
[i
].func
== func
&&
68 info
->pin_config
[i
].chan
== chan
) {
69 pin1
= &info
->pin_config
[i
];
76 /* Check the desired function and channel. */
81 if (chan
>= info
->n_ext_ts
)
85 if (chan
>= info
->n_per_out
)
95 if (info
->verify(info
, pin
, func
, chan
)) {
96 pr_err("driver cannot use function %u on pin %u\n", func
, chan
);
100 /* Disable whatever function was previously assigned. */
102 ptp_disable_pinfunc(info
, func
, chan
);
103 pin1
->func
= PTP_PF_NONE
;
106 ptp_disable_pinfunc(info
, pin2
->func
, pin2
->chan
);
113 int ptp_open(struct posix_clock
*pc
, fmode_t fmode
)
118 long ptp_ioctl(struct posix_clock
*pc
, unsigned int cmd
, unsigned long arg
)
120 struct ptp_clock_caps caps
;
121 struct ptp_clock_request req
;
122 struct ptp_sys_offset
*sysoff
= NULL
;
123 struct ptp_pin_desc pd
;
124 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
125 struct ptp_clock_info
*ops
= ptp
->info
;
126 struct ptp_clock_time
*pct
;
127 struct timespec64 ts
;
129 unsigned int i
, pin_index
;
133 case PTP_CLOCK_GETCAPS
:
134 memset(&caps
, 0, sizeof(caps
));
135 caps
.max_adj
= ptp
->info
->max_adj
;
136 caps
.n_alarm
= ptp
->info
->n_alarm
;
137 caps
.n_ext_ts
= ptp
->info
->n_ext_ts
;
138 caps
.n_per_out
= ptp
->info
->n_per_out
;
139 caps
.pps
= ptp
->info
->pps
;
140 caps
.n_pins
= ptp
->info
->n_pins
;
141 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
145 case PTP_EXTTS_REQUEST
:
146 if (copy_from_user(&req
.extts
, (void __user
*)arg
,
147 sizeof(req
.extts
))) {
151 if (req
.extts
.index
>= ops
->n_ext_ts
) {
155 req
.type
= PTP_CLK_REQ_EXTTS
;
156 enable
= req
.extts
.flags
& PTP_ENABLE_FEATURE
? 1 : 0;
157 err
= ops
->enable(ops
, &req
, enable
);
160 case PTP_PEROUT_REQUEST
:
161 if (copy_from_user(&req
.perout
, (void __user
*)arg
,
162 sizeof(req
.perout
))) {
166 if (req
.perout
.index
>= ops
->n_per_out
) {
170 req
.type
= PTP_CLK_REQ_PEROUT
;
171 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
172 err
= ops
->enable(ops
, &req
, enable
);
176 if (!capable(CAP_SYS_TIME
))
178 req
.type
= PTP_CLK_REQ_PPS
;
179 enable
= arg
? 1 : 0;
180 err
= ops
->enable(ops
, &req
, enable
);
184 sysoff
= kmalloc(sizeof(*sysoff
), GFP_KERNEL
);
189 if (copy_from_user(sysoff
, (void __user
*)arg
,
194 if (sysoff
->n_samples
> PTP_MAX_SAMPLES
) {
198 pct
= &sysoff
->ts
[0];
199 for (i
= 0; i
< sysoff
->n_samples
; i
++) {
200 getnstimeofday64(&ts
);
201 pct
->sec
= ts
.tv_sec
;
202 pct
->nsec
= ts
.tv_nsec
;
204 ptp
->info
->gettime64(ptp
->info
, &ts
);
205 pct
->sec
= ts
.tv_sec
;
206 pct
->nsec
= ts
.tv_nsec
;
209 getnstimeofday64(&ts
);
210 pct
->sec
= ts
.tv_sec
;
211 pct
->nsec
= ts
.tv_nsec
;
212 if (copy_to_user((void __user
*)arg
, sysoff
, sizeof(*sysoff
)))
216 case PTP_PIN_GETFUNC
:
217 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
221 pin_index
= pd
.index
;
222 if (pin_index
>= ops
->n_pins
) {
226 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
228 pd
= ops
->pin_config
[pin_index
];
229 mutex_unlock(&ptp
->pincfg_mux
);
230 if (!err
&& copy_to_user((void __user
*)arg
, &pd
, sizeof(pd
)))
234 case PTP_PIN_SETFUNC
:
235 if (copy_from_user(&pd
, (void __user
*)arg
, sizeof(pd
))) {
239 pin_index
= pd
.index
;
240 if (pin_index
>= ops
->n_pins
) {
244 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
246 err
= ptp_set_pinfunc(ptp
, pin_index
, pd
.func
, pd
.chan
);
247 mutex_unlock(&ptp
->pincfg_mux
);
259 unsigned int ptp_poll(struct posix_clock
*pc
, struct file
*fp
, poll_table
*wait
)
261 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
263 poll_wait(fp
, &ptp
->tsev_wq
, wait
);
265 return queue_cnt(&ptp
->tsevq
) ? POLLIN
: 0;
268 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
270 ssize_t
ptp_read(struct posix_clock
*pc
,
271 uint rdflags
, char __user
*buf
, size_t cnt
)
273 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
274 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
275 struct ptp_extts_event
*event
;
280 if (cnt
% sizeof(struct ptp_extts_event
) != 0)
283 if (cnt
> EXTTS_BUFSIZE
)
286 cnt
= cnt
/ sizeof(struct ptp_extts_event
);
288 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
291 if (wait_event_interruptible(ptp
->tsev_wq
,
292 ptp
->defunct
|| queue_cnt(queue
))) {
293 mutex_unlock(&ptp
->tsevq_mux
);
298 mutex_unlock(&ptp
->tsevq_mux
);
302 event
= kmalloc(EXTTS_BUFSIZE
, GFP_KERNEL
);
304 mutex_unlock(&ptp
->tsevq_mux
);
308 spin_lock_irqsave(&queue
->lock
, flags
);
310 qcnt
= queue_cnt(queue
);
315 for (i
= 0; i
< cnt
; i
++) {
316 event
[i
] = queue
->buf
[queue
->head
];
317 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
320 spin_unlock_irqrestore(&queue
->lock
, flags
);
322 cnt
= cnt
* sizeof(struct ptp_extts_event
);
324 mutex_unlock(&ptp
->tsevq_mux
);
327 if (copy_to_user(buf
, event
, cnt
))
This page took 0.037724 seconds and 5 git commands to generate.