Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/jkirsher/net...
[firefly-linux-kernel-4.4.55.git] / drivers / ptp / ptp_chardev.c
1 /*
2  * PTP 1588 clock support - character device implementation.
3  *
4  * Copyright (C) 2010 OMICRON electronics GmbH
5  *
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.
10  *
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.
15  *
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.
19  */
20 #include <linux/module.h>
21 #include <linux/posix-clock.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
24
25 #include "ptp_private.h"
26
27 int ptp_open(struct posix_clock *pc, fmode_t fmode)
28 {
29         return 0;
30 }
31
32 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
33 {
34         struct ptp_clock_caps caps;
35         struct ptp_clock_request req;
36         struct ptp_sys_offset sysoff;
37         struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
38         struct ptp_clock_info *ops = ptp->info;
39         struct ptp_clock_time *pct;
40         struct timespec ts;
41         int enable, err = 0;
42         unsigned int i;
43
44         switch (cmd) {
45
46         case PTP_CLOCK_GETCAPS:
47                 memset(&caps, 0, sizeof(caps));
48                 caps.max_adj = ptp->info->max_adj;
49                 caps.n_alarm = ptp->info->n_alarm;
50                 caps.n_ext_ts = ptp->info->n_ext_ts;
51                 caps.n_per_out = ptp->info->n_per_out;
52                 caps.pps = ptp->info->pps;
53                 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
54                         err = -EFAULT;
55                 break;
56
57         case PTP_EXTTS_REQUEST:
58                 if (copy_from_user(&req.extts, (void __user *)arg,
59                                    sizeof(req.extts))) {
60                         err = -EFAULT;
61                         break;
62                 }
63                 if (req.extts.index >= ops->n_ext_ts) {
64                         err = -EINVAL;
65                         break;
66                 }
67                 req.type = PTP_CLK_REQ_EXTTS;
68                 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
69                 err = ops->enable(ops, &req, enable);
70                 break;
71
72         case PTP_PEROUT_REQUEST:
73                 if (copy_from_user(&req.perout, (void __user *)arg,
74                                    sizeof(req.perout))) {
75                         err = -EFAULT;
76                         break;
77                 }
78                 if (req.perout.index >= ops->n_per_out) {
79                         err = -EINVAL;
80                         break;
81                 }
82                 req.type = PTP_CLK_REQ_PEROUT;
83                 enable = req.perout.period.sec || req.perout.period.nsec;
84                 err = ops->enable(ops, &req, enable);
85                 break;
86
87         case PTP_ENABLE_PPS:
88                 if (!capable(CAP_SYS_TIME))
89                         return -EPERM;
90                 req.type = PTP_CLK_REQ_PPS;
91                 enable = arg ? 1 : 0;
92                 err = ops->enable(ops, &req, enable);
93                 break;
94
95         case PTP_SYS_OFFSET:
96                 if (copy_from_user(&sysoff, (void __user *)arg,
97                                    sizeof(sysoff))) {
98                         err = -EFAULT;
99                         break;
100                 }
101                 if (sysoff.n_samples > PTP_MAX_SAMPLES) {
102                         err = -EINVAL;
103                         break;
104                 }
105                 pct = &sysoff.ts[0];
106                 for (i = 0; i < sysoff.n_samples; i++) {
107                         getnstimeofday(&ts);
108                         pct->sec = ts.tv_sec;
109                         pct->nsec = ts.tv_nsec;
110                         pct++;
111                         ptp->info->gettime(ptp->info, &ts);
112                         pct->sec = ts.tv_sec;
113                         pct->nsec = ts.tv_nsec;
114                         pct++;
115                 }
116                 getnstimeofday(&ts);
117                 pct->sec = ts.tv_sec;
118                 pct->nsec = ts.tv_nsec;
119                 if (copy_to_user((void __user *)arg, &sysoff, sizeof(sysoff)))
120                         err = -EFAULT;
121                 break;
122
123         default:
124                 err = -ENOTTY;
125                 break;
126         }
127         return err;
128 }
129
130 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
131 {
132         struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
133
134         poll_wait(fp, &ptp->tsev_wq, wait);
135
136         return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
137 }
138
139 ssize_t ptp_read(struct posix_clock *pc,
140                  uint rdflags, char __user *buf, size_t cnt)
141 {
142         struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
143         struct timestamp_event_queue *queue = &ptp->tsevq;
144         struct ptp_extts_event event[PTP_BUF_TIMESTAMPS];
145         unsigned long flags;
146         size_t qcnt, i;
147
148         if (cnt % sizeof(struct ptp_extts_event) != 0)
149                 return -EINVAL;
150
151         if (cnt > sizeof(event))
152                 cnt = sizeof(event);
153
154         cnt = cnt / sizeof(struct ptp_extts_event);
155
156         if (mutex_lock_interruptible(&ptp->tsevq_mux))
157                 return -ERESTARTSYS;
158
159         if (wait_event_interruptible(ptp->tsev_wq,
160                                      ptp->defunct || queue_cnt(queue))) {
161                 mutex_unlock(&ptp->tsevq_mux);
162                 return -ERESTARTSYS;
163         }
164
165         if (ptp->defunct) {
166                 mutex_unlock(&ptp->tsevq_mux);
167                 return -ENODEV;
168         }
169
170         spin_lock_irqsave(&queue->lock, flags);
171
172         qcnt = queue_cnt(queue);
173
174         if (cnt > qcnt)
175                 cnt = qcnt;
176
177         for (i = 0; i < cnt; i++) {
178                 event[i] = queue->buf[queue->head];
179                 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
180         }
181
182         spin_unlock_irqrestore(&queue->lock, flags);
183
184         cnt = cnt * sizeof(struct ptp_extts_event);
185
186         mutex_unlock(&ptp->tsevq_mux);
187
188         if (copy_to_user(buf, event, cnt))
189                 return -EFAULT;
190
191         return cnt;
192 }