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