Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 2 | /* |
| 3 | * PTP 1588 clock support - character device implementation. |
| 4 | * |
| 5 | * Copyright (C) 2010 OMICRON electronics GmbH |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/posix-clock.h> |
| 9 | #include <linux/poll.h> |
| 10 | #include <linux/sched.h> |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 11 | #include <linux/slab.h> |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 12 | #include <linux/timekeeping.h> |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 13 | |
Gustavo A. R. Silva | efa61c8 | 2018-10-16 15:06:41 +0200 | [diff] [blame] | 14 | #include <linux/nospec.h> |
| 15 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 16 | #include "ptp_private.h" |
| 17 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 18 | static int ptp_disable_pinfunc(struct ptp_clock_info *ops, |
| 19 | enum ptp_pin_function func, unsigned int chan) |
| 20 | { |
| 21 | struct ptp_clock_request rq; |
| 22 | int err = 0; |
| 23 | |
| 24 | memset(&rq, 0, sizeof(rq)); |
| 25 | |
| 26 | switch (func) { |
| 27 | case PTP_PF_NONE: |
| 28 | break; |
| 29 | case PTP_PF_EXTTS: |
| 30 | rq.type = PTP_CLK_REQ_EXTTS; |
| 31 | rq.extts.index = chan; |
| 32 | err = ops->enable(ops, &rq, 0); |
| 33 | break; |
| 34 | case PTP_PF_PEROUT: |
| 35 | rq.type = PTP_CLK_REQ_PEROUT; |
| 36 | rq.perout.index = chan; |
| 37 | err = ops->enable(ops, &rq, 0); |
| 38 | break; |
| 39 | case PTP_PF_PHYSYNC: |
| 40 | break; |
| 41 | default: |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, |
| 49 | enum ptp_pin_function func, unsigned int chan) |
| 50 | { |
| 51 | struct ptp_clock_info *info = ptp->info; |
| 52 | struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin]; |
| 53 | unsigned int i; |
| 54 | |
| 55 | /* Check to see if any other pin previously had this function. */ |
| 56 | for (i = 0; i < info->n_pins; i++) { |
| 57 | if (info->pin_config[i].func == func && |
| 58 | info->pin_config[i].chan == chan) { |
| 59 | pin1 = &info->pin_config[i]; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | if (pin1 && i == pin) |
| 64 | return 0; |
| 65 | |
| 66 | /* Check the desired function and channel. */ |
| 67 | switch (func) { |
| 68 | case PTP_PF_NONE: |
| 69 | break; |
| 70 | case PTP_PF_EXTTS: |
| 71 | if (chan >= info->n_ext_ts) |
| 72 | return -EINVAL; |
| 73 | break; |
| 74 | case PTP_PF_PEROUT: |
| 75 | if (chan >= info->n_per_out) |
| 76 | return -EINVAL; |
| 77 | break; |
| 78 | case PTP_PF_PHYSYNC: |
Stefan Sørensen | 72df7a7 | 2014-06-27 12:05:33 +0200 | [diff] [blame] | 79 | if (chan != 0) |
| 80 | return -EINVAL; |
Gustavo A. R. Silva | 9ba8376 | 2018-07-17 20:17:33 -0500 | [diff] [blame] | 81 | break; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 82 | default: |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 86 | if (info->verify(info, pin, func, chan)) { |
| 87 | pr_err("driver cannot use function %u on pin %u\n", func, chan); |
| 88 | return -EOPNOTSUPP; |
| 89 | } |
| 90 | |
| 91 | /* Disable whatever function was previously assigned. */ |
| 92 | if (pin1) { |
| 93 | ptp_disable_pinfunc(info, func, chan); |
| 94 | pin1->func = PTP_PF_NONE; |
| 95 | pin1->chan = 0; |
| 96 | } |
| 97 | ptp_disable_pinfunc(info, pin2->func, pin2->chan); |
| 98 | pin2->func = func; |
| 99 | pin2->chan = chan; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 104 | int ptp_open(struct posix_clock *pc, fmode_t fmode) |
| 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) |
| 110 | { |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 111 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 112 | struct ptp_sys_offset_extended *extoff = NULL; |
Miroslav Lichvar | fbb960a | 2018-11-09 11:14:42 +0100 | [diff] [blame] | 113 | struct ptp_sys_offset_precise precise_offset; |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 114 | struct system_device_crosststamp xtstamp; |
Miroslav Lichvar | fbb960a | 2018-11-09 11:14:42 +0100 | [diff] [blame] | 115 | struct ptp_clock_info *ops = ptp->info; |
| 116 | struct ptp_sys_offset *sysoff = NULL; |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 117 | struct ptp_system_timestamp sts; |
Miroslav Lichvar | fbb960a | 2018-11-09 11:14:42 +0100 | [diff] [blame] | 118 | struct ptp_clock_request req; |
| 119 | struct ptp_clock_caps caps; |
| 120 | struct ptp_clock_time *pct; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 121 | unsigned int i, pin_index; |
Miroslav Lichvar | fbb960a | 2018-11-09 11:14:42 +0100 | [diff] [blame] | 122 | struct ptp_pin_desc pd; |
| 123 | struct timespec64 ts; |
| 124 | int enable, err = 0; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 125 | |
| 126 | switch (cmd) { |
| 127 | |
| 128 | case PTP_CLOCK_GETCAPS: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 129 | case PTP_CLOCK_GETCAPS2: |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 130 | memset(&caps, 0, sizeof(caps)); |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 131 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 132 | caps.max_adj = ptp->info->max_adj; |
| 133 | caps.n_alarm = ptp->info->n_alarm; |
| 134 | caps.n_ext_ts = ptp->info->n_ext_ts; |
| 135 | caps.n_per_out = ptp->info->n_per_out; |
| 136 | caps.pps = ptp->info->pps; |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 137 | caps.n_pins = ptp->info->n_pins; |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 138 | caps.cross_timestamping = ptp->info->getcrosststamp != NULL; |
Vincent Cheng | d3f1cbd | 2020-05-01 23:35:37 -0400 | [diff] [blame] | 139 | caps.adjust_phase = ptp->info->adjphase != NULL; |
Dan Carpenter | e23ef22 | 2011-05-29 22:53:12 +0300 | [diff] [blame] | 140 | if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) |
| 141 | err = -EFAULT; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case PTP_EXTTS_REQUEST: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 145 | case PTP_EXTTS_REQUEST2: |
| 146 | memset(&req, 0, sizeof(req)); |
| 147 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 148 | if (copy_from_user(&req.extts, (void __user *)arg, |
| 149 | sizeof(req.extts))) { |
| 150 | err = -EFAULT; |
| 151 | break; |
| 152 | } |
Richard Cochran | cd734d5 | 2019-11-14 10:44:55 -0800 | [diff] [blame] | 153 | if (cmd == PTP_EXTTS_REQUEST2) { |
Richard Cochran | 6138e68 | 2019-11-14 10:45:02 -0800 | [diff] [blame] | 154 | /* Tell the drivers to check the flags carefully. */ |
| 155 | req.extts.flags |= PTP_STRICT_FLAGS; |
Richard Cochran | cd734d5 | 2019-11-14 10:44:55 -0800 | [diff] [blame] | 156 | /* Make sure no reserved bit is set. */ |
| 157 | if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || |
| 158 | req.extts.rsv[0] || req.extts.rsv[1]) { |
| 159 | err = -EINVAL; |
| 160 | break; |
| 161 | } |
| 162 | /* Ensure one of the rising/falling edge bits is set. */ |
| 163 | if ((req.extts.flags & PTP_ENABLE_FEATURE) && |
| 164 | (req.extts.flags & PTP_EXTTS_EDGES) == 0) { |
| 165 | err = -EINVAL; |
| 166 | break; |
| 167 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 168 | } else if (cmd == PTP_EXTTS_REQUEST) { |
Jacob Keller | 2df4de1 | 2019-09-25 19:28:19 -0700 | [diff] [blame] | 169 | req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS; |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 170 | req.extts.rsv[0] = 0; |
| 171 | req.extts.rsv[1] = 0; |
| 172 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 173 | if (req.extts.index >= ops->n_ext_ts) { |
| 174 | err = -EINVAL; |
| 175 | break; |
| 176 | } |
| 177 | req.type = PTP_CLK_REQ_EXTTS; |
| 178 | enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 179 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 180 | return -ERESTARTSYS; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 181 | err = ops->enable(ops, &req, enable); |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 182 | mutex_unlock(&ptp->pincfg_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 183 | break; |
| 184 | |
| 185 | case PTP_PEROUT_REQUEST: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 186 | case PTP_PEROUT_REQUEST2: |
| 187 | memset(&req, 0, sizeof(req)); |
| 188 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 189 | if (copy_from_user(&req.perout, (void __user *)arg, |
| 190 | sizeof(req.perout))) { |
| 191 | err = -EFAULT; |
| 192 | break; |
| 193 | } |
Vladimir Oltean | f65b71a | 2020-07-17 01:45:29 +0300 | [diff] [blame] | 194 | if (cmd == PTP_PEROUT_REQUEST2) { |
| 195 | struct ptp_perout_request *perout = &req.perout; |
| 196 | |
| 197 | if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) { |
| 198 | err = -EINVAL; |
| 199 | break; |
| 200 | } |
| 201 | /* |
| 202 | * The "on" field has undefined meaning if |
| 203 | * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat |
| 204 | * it as reserved, which must be set to zero. |
| 205 | */ |
| 206 | if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && |
| 207 | (perout->rsv[0] || perout->rsv[1] || |
| 208 | perout->rsv[2] || perout->rsv[3])) { |
| 209 | err = -EINVAL; |
| 210 | break; |
| 211 | } |
| 212 | if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { |
| 213 | /* The duty cycle must be subunitary. */ |
| 214 | if (perout->on.sec > perout->period.sec || |
| 215 | (perout->on.sec == perout->period.sec && |
| 216 | perout->on.nsec > perout->period.nsec)) { |
| 217 | err = -ERANGE; |
| 218 | break; |
| 219 | } |
| 220 | } |
Vladimir Oltean | c29f9aa | 2020-08-05 03:10:47 +0300 | [diff] [blame] | 221 | if (perout->flags & PTP_PEROUT_PHASE) { |
| 222 | /* |
| 223 | * The phase should be specified modulo the |
| 224 | * period, therefore anything equal or larger |
| 225 | * than 1 period is invalid. |
| 226 | */ |
| 227 | if (perout->phase.sec > perout->period.sec || |
| 228 | (perout->phase.sec == perout->period.sec && |
| 229 | perout->phase.nsec >= perout->period.nsec)) { |
| 230 | err = -ERANGE; |
| 231 | break; |
| 232 | } |
| 233 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 234 | } else if (cmd == PTP_PEROUT_REQUEST) { |
Jacob Keller | 2df4de1 | 2019-09-25 19:28:19 -0700 | [diff] [blame] | 235 | req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS; |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 236 | req.perout.rsv[0] = 0; |
| 237 | req.perout.rsv[1] = 0; |
| 238 | req.perout.rsv[2] = 0; |
| 239 | req.perout.rsv[3] = 0; |
| 240 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 241 | if (req.perout.index >= ops->n_per_out) { |
| 242 | err = -EINVAL; |
| 243 | break; |
| 244 | } |
| 245 | req.type = PTP_CLK_REQ_PEROUT; |
| 246 | enable = req.perout.period.sec || req.perout.period.nsec; |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 247 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 248 | return -ERESTARTSYS; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 249 | err = ops->enable(ops, &req, enable); |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 250 | mutex_unlock(&ptp->pincfg_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | case PTP_ENABLE_PPS: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 254 | case PTP_ENABLE_PPS2: |
| 255 | memset(&req, 0, sizeof(req)); |
| 256 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 257 | if (!capable(CAP_SYS_TIME)) |
| 258 | return -EPERM; |
| 259 | req.type = PTP_CLK_REQ_PPS; |
| 260 | enable = arg ? 1 : 0; |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 261 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 262 | return -ERESTARTSYS; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 263 | err = ops->enable(ops, &req, enable); |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 264 | mutex_unlock(&ptp->pincfg_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 265 | break; |
| 266 | |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 267 | case PTP_SYS_OFFSET_PRECISE: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 268 | case PTP_SYS_OFFSET_PRECISE2: |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 269 | if (!ptp->info->getcrosststamp) { |
| 270 | err = -EOPNOTSUPP; |
| 271 | break; |
| 272 | } |
| 273 | err = ptp->info->getcrosststamp(ptp->info, &xtstamp); |
| 274 | if (err) |
| 275 | break; |
| 276 | |
Vlad Tsyrklevich | 02a9079 | 2016-10-11 15:02:47 +0200 | [diff] [blame] | 277 | memset(&precise_offset, 0, sizeof(precise_offset)); |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 278 | ts = ktime_to_timespec64(xtstamp.device); |
| 279 | precise_offset.device.sec = ts.tv_sec; |
| 280 | precise_offset.device.nsec = ts.tv_nsec; |
| 281 | ts = ktime_to_timespec64(xtstamp.sys_realtime); |
| 282 | precise_offset.sys_realtime.sec = ts.tv_sec; |
| 283 | precise_offset.sys_realtime.nsec = ts.tv_nsec; |
| 284 | ts = ktime_to_timespec64(xtstamp.sys_monoraw); |
| 285 | precise_offset.sys_monoraw.sec = ts.tv_sec; |
| 286 | precise_offset.sys_monoraw.nsec = ts.tv_nsec; |
| 287 | if (copy_to_user((void __user *)arg, &precise_offset, |
| 288 | sizeof(precise_offset))) |
| 289 | err = -EFAULT; |
| 290 | break; |
| 291 | |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 292 | case PTP_SYS_OFFSET_EXTENDED: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 293 | case PTP_SYS_OFFSET_EXTENDED2: |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 294 | if (!ptp->info->gettimex64) { |
| 295 | err = -EOPNOTSUPP; |
| 296 | break; |
| 297 | } |
| 298 | extoff = memdup_user((void __user *)arg, sizeof(*extoff)); |
| 299 | if (IS_ERR(extoff)) { |
| 300 | err = PTR_ERR(extoff); |
| 301 | extoff = NULL; |
| 302 | break; |
| 303 | } |
Eugene Syromiatnikov | 895ac13 | 2019-01-07 16:22:29 +0100 | [diff] [blame] | 304 | if (extoff->n_samples > PTP_MAX_SAMPLES |
| 305 | || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) { |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 306 | err = -EINVAL; |
| 307 | break; |
| 308 | } |
| 309 | for (i = 0; i < extoff->n_samples; i++) { |
| 310 | err = ptp->info->gettimex64(ptp->info, &ts, &sts); |
| 311 | if (err) |
| 312 | goto out; |
| 313 | extoff->ts[i][0].sec = sts.pre_ts.tv_sec; |
| 314 | extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; |
| 315 | extoff->ts[i][1].sec = ts.tv_sec; |
| 316 | extoff->ts[i][1].nsec = ts.tv_nsec; |
| 317 | extoff->ts[i][2].sec = sts.post_ts.tv_sec; |
| 318 | extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; |
| 319 | } |
| 320 | if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff))) |
| 321 | err = -EFAULT; |
| 322 | break; |
| 323 | |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 324 | case PTP_SYS_OFFSET: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 325 | case PTP_SYS_OFFSET2: |
Muhammad Falak R Wani | 2ece068 | 2016-05-20 17:51:02 +0530 | [diff] [blame] | 326 | sysoff = memdup_user((void __user *)arg, sizeof(*sysoff)); |
| 327 | if (IS_ERR(sysoff)) { |
| 328 | err = PTR_ERR(sysoff); |
Dan Carpenter | 6756325 | 2016-05-26 09:46:22 +0300 | [diff] [blame] | 329 | sysoff = NULL; |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 330 | break; |
| 331 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 332 | if (sysoff->n_samples > PTP_MAX_SAMPLES) { |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 333 | err = -EINVAL; |
| 334 | break; |
| 335 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 336 | pct = &sysoff->ts[0]; |
| 337 | for (i = 0; i < sysoff->n_samples; i++) { |
Arnd Bergmann | f696a21 | 2018-06-18 16:20:39 +0200 | [diff] [blame] | 338 | ktime_get_real_ts64(&ts); |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 339 | pct->sec = ts.tv_sec; |
| 340 | pct->nsec = ts.tv_nsec; |
| 341 | pct++; |
Miroslav Lichvar | 916444d | 2018-11-09 11:14:45 +0100 | [diff] [blame] | 342 | if (ops->gettimex64) |
| 343 | err = ops->gettimex64(ops, &ts, NULL); |
| 344 | else |
| 345 | err = ops->gettime64(ops, &ts); |
Miroslav Lichvar | 83d0bdc | 2018-11-09 11:14:43 +0100 | [diff] [blame] | 346 | if (err) |
| 347 | goto out; |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 348 | pct->sec = ts.tv_sec; |
| 349 | pct->nsec = ts.tv_nsec; |
| 350 | pct++; |
| 351 | } |
Arnd Bergmann | f696a21 | 2018-06-18 16:20:39 +0200 | [diff] [blame] | 352 | ktime_get_real_ts64(&ts); |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 353 | pct->sec = ts.tv_sec; |
| 354 | pct->nsec = ts.tv_nsec; |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 355 | if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 356 | err = -EFAULT; |
| 357 | break; |
| 358 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 359 | case PTP_PIN_GETFUNC: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 360 | case PTP_PIN_GETFUNC2: |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 361 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { |
| 362 | err = -EFAULT; |
| 363 | break; |
| 364 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 365 | if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] |
| 366 | || pd.rsv[3] || pd.rsv[4]) |
| 367 | && cmd == PTP_PIN_GETFUNC2) { |
| 368 | err = -EINVAL; |
| 369 | break; |
| 370 | } else if (cmd == PTP_PIN_GETFUNC) { |
| 371 | pd.rsv[0] = 0; |
| 372 | pd.rsv[1] = 0; |
| 373 | pd.rsv[2] = 0; |
| 374 | pd.rsv[3] = 0; |
| 375 | pd.rsv[4] = 0; |
| 376 | } |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 377 | pin_index = pd.index; |
| 378 | if (pin_index >= ops->n_pins) { |
| 379 | err = -EINVAL; |
| 380 | break; |
| 381 | } |
Gustavo A. R. Silva | efa61c8 | 2018-10-16 15:06:41 +0200 | [diff] [blame] | 382 | pin_index = array_index_nospec(pin_index, ops->n_pins); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 383 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 384 | return -ERESTARTSYS; |
| 385 | pd = ops->pin_config[pin_index]; |
| 386 | mutex_unlock(&ptp->pincfg_mux); |
| 387 | if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) |
| 388 | err = -EFAULT; |
| 389 | break; |
| 390 | |
| 391 | case PTP_PIN_SETFUNC: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 392 | case PTP_PIN_SETFUNC2: |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 393 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { |
| 394 | err = -EFAULT; |
| 395 | break; |
| 396 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 397 | if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] |
| 398 | || pd.rsv[3] || pd.rsv[4]) |
| 399 | && cmd == PTP_PIN_SETFUNC2) { |
| 400 | err = -EINVAL; |
| 401 | break; |
| 402 | } else if (cmd == PTP_PIN_SETFUNC) { |
| 403 | pd.rsv[0] = 0; |
| 404 | pd.rsv[1] = 0; |
| 405 | pd.rsv[2] = 0; |
| 406 | pd.rsv[3] = 0; |
| 407 | pd.rsv[4] = 0; |
| 408 | } |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 409 | pin_index = pd.index; |
| 410 | if (pin_index >= ops->n_pins) { |
| 411 | err = -EINVAL; |
| 412 | break; |
| 413 | } |
Gustavo A. R. Silva | efa61c8 | 2018-10-16 15:06:41 +0200 | [diff] [blame] | 414 | pin_index = array_index_nospec(pin_index, ops->n_pins); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 415 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 416 | return -ERESTARTSYS; |
| 417 | err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); |
| 418 | mutex_unlock(&ptp->pincfg_mux); |
| 419 | break; |
| 420 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 421 | default: |
| 422 | err = -ENOTTY; |
| 423 | break; |
| 424 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 425 | |
Miroslav Lichvar | 83d0bdc | 2018-11-09 11:14:43 +0100 | [diff] [blame] | 426 | out: |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 427 | kfree(extoff); |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 428 | kfree(sysoff); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 429 | return err; |
| 430 | } |
| 431 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 432 | __poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 433 | { |
| 434 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 435 | |
| 436 | poll_wait(fp, &ptp->tsev_wq, wait); |
| 437 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 438 | return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 441 | #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) |
| 442 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 443 | ssize_t ptp_read(struct posix_clock *pc, |
| 444 | uint rdflags, char __user *buf, size_t cnt) |
| 445 | { |
| 446 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 447 | struct timestamp_event_queue *queue = &ptp->tsevq; |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 448 | struct ptp_extts_event *event; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 449 | unsigned long flags; |
| 450 | size_t qcnt, i; |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 451 | int result; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 452 | |
| 453 | if (cnt % sizeof(struct ptp_extts_event) != 0) |
| 454 | return -EINVAL; |
| 455 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 456 | if (cnt > EXTTS_BUFSIZE) |
| 457 | cnt = EXTTS_BUFSIZE; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 458 | |
| 459 | cnt = cnt / sizeof(struct ptp_extts_event); |
| 460 | |
| 461 | if (mutex_lock_interruptible(&ptp->tsevq_mux)) |
| 462 | return -ERESTARTSYS; |
| 463 | |
| 464 | if (wait_event_interruptible(ptp->tsev_wq, |
| 465 | ptp->defunct || queue_cnt(queue))) { |
| 466 | mutex_unlock(&ptp->tsevq_mux); |
| 467 | return -ERESTARTSYS; |
| 468 | } |
| 469 | |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 470 | if (ptp->defunct) { |
| 471 | mutex_unlock(&ptp->tsevq_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 472 | return -ENODEV; |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 473 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 474 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 475 | event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); |
| 476 | if (!event) { |
| 477 | mutex_unlock(&ptp->tsevq_mux); |
| 478 | return -ENOMEM; |
| 479 | } |
| 480 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 481 | spin_lock_irqsave(&queue->lock, flags); |
| 482 | |
| 483 | qcnt = queue_cnt(queue); |
| 484 | |
| 485 | if (cnt > qcnt) |
| 486 | cnt = qcnt; |
| 487 | |
| 488 | for (i = 0; i < cnt; i++) { |
| 489 | event[i] = queue->buf[queue->head]; |
| 490 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; |
| 491 | } |
| 492 | |
| 493 | spin_unlock_irqrestore(&queue->lock, flags); |
| 494 | |
| 495 | cnt = cnt * sizeof(struct ptp_extts_event); |
| 496 | |
| 497 | mutex_unlock(&ptp->tsevq_mux); |
| 498 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 499 | result = cnt; |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 500 | if (copy_to_user(buf, event, cnt)) |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 501 | result = -EFAULT; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 502 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 503 | kfree(event); |
| 504 | return result; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 505 | } |