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>
25 #include "ptp_private.h"
27 int ptp_open(struct posix_clock
*pc
, fmode_t fmode
)
32 long ptp_ioctl(struct posix_clock
*pc
, unsigned int cmd
, unsigned long arg
)
34 struct ptp_clock_caps caps
;
35 struct ptp_clock_request req
;
36 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
37 struct ptp_clock_info
*ops
= ptp
->info
;
42 case PTP_CLOCK_GETCAPS
:
43 memset(&caps
, 0, sizeof(caps
));
44 caps
.max_adj
= ptp
->info
->max_adj
;
45 caps
.n_alarm
= ptp
->info
->n_alarm
;
46 caps
.n_ext_ts
= ptp
->info
->n_ext_ts
;
47 caps
.n_per_out
= ptp
->info
->n_per_out
;
48 caps
.pps
= ptp
->info
->pps
;
49 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
53 case PTP_EXTTS_REQUEST
:
54 if (copy_from_user(&req
.extts
, (void __user
*)arg
,
59 if (req
.extts
.index
>= ops
->n_ext_ts
) {
63 req
.type
= PTP_CLK_REQ_EXTTS
;
64 enable
= req
.extts
.flags
& PTP_ENABLE_FEATURE
? 1 : 0;
65 err
= ops
->enable(ops
, &req
, enable
);
68 case PTP_PEROUT_REQUEST
:
69 if (copy_from_user(&req
.perout
, (void __user
*)arg
,
70 sizeof(req
.perout
))) {
74 if (req
.perout
.index
>= ops
->n_per_out
) {
78 req
.type
= PTP_CLK_REQ_PEROUT
;
79 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
80 err
= ops
->enable(ops
, &req
, enable
);
84 if (!capable(CAP_SYS_TIME
))
86 req
.type
= PTP_CLK_REQ_PPS
;
88 err
= ops
->enable(ops
, &req
, enable
);
98 unsigned int ptp_poll(struct posix_clock
*pc
, struct file
*fp
, poll_table
*wait
)
100 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
102 poll_wait(fp
, &ptp
->tsev_wq
, wait
);
104 return queue_cnt(&ptp
->tsevq
) ? POLLIN
: 0;
107 ssize_t
ptp_read(struct posix_clock
*pc
,
108 uint rdflags
, char __user
*buf
, size_t cnt
)
110 struct ptp_clock
*ptp
= container_of(pc
, struct ptp_clock
, clock
);
111 struct timestamp_event_queue
*queue
= &ptp
->tsevq
;
112 struct ptp_extts_event event
[PTP_BUF_TIMESTAMPS
];
116 if (cnt
% sizeof(struct ptp_extts_event
) != 0)
119 if (cnt
> sizeof(event
))
122 cnt
= cnt
/ sizeof(struct ptp_extts_event
);
124 if (mutex_lock_interruptible(&ptp
->tsevq_mux
))
127 if (wait_event_interruptible(ptp
->tsev_wq
,
128 ptp
->defunct
|| queue_cnt(queue
))) {
129 mutex_unlock(&ptp
->tsevq_mux
);
134 mutex_unlock(&ptp
->tsevq_mux
);
138 spin_lock_irqsave(&queue
->lock
, flags
);
140 qcnt
= queue_cnt(queue
);
145 for (i
= 0; i
< cnt
; i
++) {
146 event
[i
] = queue
->buf
[queue
->head
];
147 queue
->head
= (queue
->head
+ 1) % PTP_MAX_TIMESTAMPS
;
150 spin_unlock_irqrestore(&queue
->lock
, flags
);
152 cnt
= cnt
* sizeof(struct ptp_extts_event
);
154 mutex_unlock(&ptp
->tsevq_mux
);
156 if (copy_to_user(buf
, event
, cnt
))