Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 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> |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 24 | #include <linux/slab.h> |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 25 | |
| 26 | #include "ptp_private.h" |
| 27 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 28 | static int ptp_disable_pinfunc(struct ptp_clock_info *ops, |
| 29 | enum ptp_pin_function func, unsigned int chan) |
| 30 | { |
| 31 | struct ptp_clock_request rq; |
| 32 | int err = 0; |
| 33 | |
| 34 | memset(&rq, 0, sizeof(rq)); |
| 35 | |
| 36 | switch (func) { |
| 37 | case PTP_PF_NONE: |
| 38 | break; |
| 39 | case PTP_PF_EXTTS: |
| 40 | rq.type = PTP_CLK_REQ_EXTTS; |
| 41 | rq.extts.index = chan; |
| 42 | err = ops->enable(ops, &rq, 0); |
| 43 | break; |
| 44 | case PTP_PF_PEROUT: |
| 45 | rq.type = PTP_CLK_REQ_PEROUT; |
| 46 | rq.perout.index = chan; |
| 47 | err = ops->enable(ops, &rq, 0); |
| 48 | break; |
| 49 | case PTP_PF_PHYSYNC: |
| 50 | break; |
| 51 | default: |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | return err; |
| 56 | } |
| 57 | |
| 58 | int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, |
| 59 | enum ptp_pin_function func, unsigned int chan) |
| 60 | { |
| 61 | struct ptp_clock_info *info = ptp->info; |
| 62 | struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin]; |
| 63 | unsigned int i; |
| 64 | |
| 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]; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | if (pin1 && i == pin) |
| 74 | return 0; |
| 75 | |
| 76 | /* Check the desired function and channel. */ |
| 77 | switch (func) { |
| 78 | case PTP_PF_NONE: |
| 79 | break; |
| 80 | case PTP_PF_EXTTS: |
| 81 | if (chan >= info->n_ext_ts) |
| 82 | return -EINVAL; |
| 83 | break; |
| 84 | case PTP_PF_PEROUT: |
| 85 | if (chan >= info->n_per_out) |
| 86 | return -EINVAL; |
| 87 | break; |
| 88 | case PTP_PF_PHYSYNC: |
Stefan Sørensen | 72df7a7 | 2014-06-27 12:05:33 +0200 | [diff] [blame] | 89 | if (chan != 0) |
| 90 | return -EINVAL; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 91 | default: |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 95 | if (info->verify(info, pin, func, chan)) { |
| 96 | pr_err("driver cannot use function %u on pin %u\n", func, chan); |
| 97 | return -EOPNOTSUPP; |
| 98 | } |
| 99 | |
| 100 | /* Disable whatever function was previously assigned. */ |
| 101 | if (pin1) { |
| 102 | ptp_disable_pinfunc(info, func, chan); |
| 103 | pin1->func = PTP_PF_NONE; |
| 104 | pin1->chan = 0; |
| 105 | } |
| 106 | ptp_disable_pinfunc(info, pin2->func, pin2->chan); |
| 107 | pin2->func = func; |
| 108 | pin2->chan = chan; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 113 | int ptp_open(struct posix_clock *pc, fmode_t fmode) |
| 114 | { |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) |
| 119 | { |
| 120 | struct ptp_clock_caps caps; |
| 121 | struct ptp_clock_request req; |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 122 | struct ptp_sys_offset *sysoff = NULL; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 123 | struct ptp_pin_desc pd; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 124 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 125 | struct ptp_clock_info *ops = ptp->info; |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 126 | struct ptp_clock_time *pct; |
Richard Cochran | e13cfcb | 2015-03-29 23:11:52 +0200 | [diff] [blame^] | 127 | struct timespec64 ts; |
| 128 | struct timespec t2; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 129 | int enable, err = 0; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 130 | unsigned int i, pin_index; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 131 | |
| 132 | switch (cmd) { |
| 133 | |
| 134 | case PTP_CLOCK_GETCAPS: |
| 135 | memset(&caps, 0, sizeof(caps)); |
| 136 | caps.max_adj = ptp->info->max_adj; |
| 137 | caps.n_alarm = ptp->info->n_alarm; |
| 138 | caps.n_ext_ts = ptp->info->n_ext_ts; |
| 139 | caps.n_per_out = ptp->info->n_per_out; |
| 140 | caps.pps = ptp->info->pps; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 141 | caps.n_pins = ptp->info->n_pins; |
Dan Carpenter | e23ef22 | 2011-05-29 22:53:12 +0300 | [diff] [blame] | 142 | if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) |
| 143 | err = -EFAULT; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | case PTP_EXTTS_REQUEST: |
| 147 | if (copy_from_user(&req.extts, (void __user *)arg, |
| 148 | sizeof(req.extts))) { |
| 149 | err = -EFAULT; |
| 150 | break; |
| 151 | } |
| 152 | if (req.extts.index >= ops->n_ext_ts) { |
| 153 | err = -EINVAL; |
| 154 | break; |
| 155 | } |
| 156 | req.type = PTP_CLK_REQ_EXTTS; |
| 157 | enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; |
| 158 | err = ops->enable(ops, &req, enable); |
| 159 | break; |
| 160 | |
| 161 | case PTP_PEROUT_REQUEST: |
| 162 | if (copy_from_user(&req.perout, (void __user *)arg, |
| 163 | sizeof(req.perout))) { |
| 164 | err = -EFAULT; |
| 165 | break; |
| 166 | } |
| 167 | if (req.perout.index >= ops->n_per_out) { |
| 168 | err = -EINVAL; |
| 169 | break; |
| 170 | } |
| 171 | req.type = PTP_CLK_REQ_PEROUT; |
| 172 | enable = req.perout.period.sec || req.perout.period.nsec; |
| 173 | err = ops->enable(ops, &req, enable); |
| 174 | break; |
| 175 | |
| 176 | case PTP_ENABLE_PPS: |
| 177 | if (!capable(CAP_SYS_TIME)) |
| 178 | return -EPERM; |
| 179 | req.type = PTP_CLK_REQ_PPS; |
| 180 | enable = arg ? 1 : 0; |
| 181 | err = ops->enable(ops, &req, enable); |
| 182 | break; |
| 183 | |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 184 | case PTP_SYS_OFFSET: |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 185 | sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL); |
| 186 | if (!sysoff) { |
| 187 | err = -ENOMEM; |
| 188 | break; |
| 189 | } |
| 190 | if (copy_from_user(sysoff, (void __user *)arg, |
| 191 | sizeof(*sysoff))) { |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 192 | err = -EFAULT; |
| 193 | break; |
| 194 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 195 | if (sysoff->n_samples > PTP_MAX_SAMPLES) { |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 196 | err = -EINVAL; |
| 197 | break; |
| 198 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 199 | pct = &sysoff->ts[0]; |
| 200 | for (i = 0; i < sysoff->n_samples; i++) { |
Richard Cochran | e13cfcb | 2015-03-29 23:11:52 +0200 | [diff] [blame^] | 201 | getnstimeofday64(&ts); |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 202 | pct->sec = ts.tv_sec; |
| 203 | pct->nsec = ts.tv_nsec; |
| 204 | pct++; |
Richard Cochran | e13cfcb | 2015-03-29 23:11:52 +0200 | [diff] [blame^] | 205 | if (ptp->info->gettime64) { |
| 206 | ptp->info->gettime64(ptp->info, &ts); |
| 207 | } else { |
| 208 | ptp->info->gettime(ptp->info, &t2); |
| 209 | ts = timespec_to_timespec64(t2); |
| 210 | } |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 211 | pct->sec = ts.tv_sec; |
| 212 | pct->nsec = ts.tv_nsec; |
| 213 | pct++; |
| 214 | } |
Richard Cochran | e13cfcb | 2015-03-29 23:11:52 +0200 | [diff] [blame^] | 215 | getnstimeofday64(&ts); |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 216 | pct->sec = ts.tv_sec; |
| 217 | pct->nsec = ts.tv_nsec; |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 218 | if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 219 | err = -EFAULT; |
| 220 | break; |
| 221 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 222 | case PTP_PIN_GETFUNC: |
| 223 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { |
| 224 | err = -EFAULT; |
| 225 | break; |
| 226 | } |
| 227 | pin_index = pd.index; |
| 228 | if (pin_index >= ops->n_pins) { |
| 229 | err = -EINVAL; |
| 230 | break; |
| 231 | } |
| 232 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 233 | return -ERESTARTSYS; |
| 234 | pd = ops->pin_config[pin_index]; |
| 235 | mutex_unlock(&ptp->pincfg_mux); |
| 236 | if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) |
| 237 | err = -EFAULT; |
| 238 | break; |
| 239 | |
| 240 | case PTP_PIN_SETFUNC: |
| 241 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { |
| 242 | err = -EFAULT; |
| 243 | break; |
| 244 | } |
| 245 | pin_index = pd.index; |
| 246 | if (pin_index >= ops->n_pins) { |
| 247 | err = -EINVAL; |
| 248 | break; |
| 249 | } |
| 250 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 251 | return -ERESTARTSYS; |
| 252 | err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); |
| 253 | mutex_unlock(&ptp->pincfg_mux); |
| 254 | break; |
| 255 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 256 | default: |
| 257 | err = -ENOTTY; |
| 258 | break; |
| 259 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 260 | |
| 261 | kfree(sysoff); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 262 | return err; |
| 263 | } |
| 264 | |
| 265 | unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait) |
| 266 | { |
| 267 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 268 | |
| 269 | poll_wait(fp, &ptp->tsev_wq, wait); |
| 270 | |
| 271 | return queue_cnt(&ptp->tsevq) ? POLLIN : 0; |
| 272 | } |
| 273 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 274 | #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) |
| 275 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 276 | ssize_t ptp_read(struct posix_clock *pc, |
| 277 | uint rdflags, char __user *buf, size_t cnt) |
| 278 | { |
| 279 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 280 | struct timestamp_event_queue *queue = &ptp->tsevq; |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 281 | struct ptp_extts_event *event; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 282 | unsigned long flags; |
| 283 | size_t qcnt, i; |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 284 | int result; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 285 | |
| 286 | if (cnt % sizeof(struct ptp_extts_event) != 0) |
| 287 | return -EINVAL; |
| 288 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 289 | if (cnt > EXTTS_BUFSIZE) |
| 290 | cnt = EXTTS_BUFSIZE; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 291 | |
| 292 | cnt = cnt / sizeof(struct ptp_extts_event); |
| 293 | |
| 294 | if (mutex_lock_interruptible(&ptp->tsevq_mux)) |
| 295 | return -ERESTARTSYS; |
| 296 | |
| 297 | if (wait_event_interruptible(ptp->tsev_wq, |
| 298 | ptp->defunct || queue_cnt(queue))) { |
| 299 | mutex_unlock(&ptp->tsevq_mux); |
| 300 | return -ERESTARTSYS; |
| 301 | } |
| 302 | |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 303 | if (ptp->defunct) { |
| 304 | mutex_unlock(&ptp->tsevq_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 305 | return -ENODEV; |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 306 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 307 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 308 | event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); |
| 309 | if (!event) { |
| 310 | mutex_unlock(&ptp->tsevq_mux); |
| 311 | return -ENOMEM; |
| 312 | } |
| 313 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 314 | spin_lock_irqsave(&queue->lock, flags); |
| 315 | |
| 316 | qcnt = queue_cnt(queue); |
| 317 | |
| 318 | if (cnt > qcnt) |
| 319 | cnt = qcnt; |
| 320 | |
| 321 | for (i = 0; i < cnt; i++) { |
| 322 | event[i] = queue->buf[queue->head]; |
| 323 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; |
| 324 | } |
| 325 | |
| 326 | spin_unlock_irqrestore(&queue->lock, flags); |
| 327 | |
| 328 | cnt = cnt * sizeof(struct ptp_extts_event); |
| 329 | |
| 330 | mutex_unlock(&ptp->tsevq_mux); |
| 331 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 332 | result = cnt; |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 333 | if (copy_to_user(buf, event, cnt)) |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 334 | result = -EFAULT; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 335 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 336 | kfree(event); |
| 337 | return result; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 338 | } |