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; |
Dan Carpenter | e23ef22 | 2011-05-29 22:53:12 +0300 | [diff] [blame] | 139 | if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) |
| 140 | err = -EFAULT; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 141 | break; |
| 142 | |
| 143 | case PTP_EXTTS_REQUEST: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 144 | case PTP_EXTTS_REQUEST2: |
| 145 | memset(&req, 0, sizeof(req)); |
| 146 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 147 | if (copy_from_user(&req.extts, (void __user *)arg, |
| 148 | sizeof(req.extts))) { |
| 149 | err = -EFAULT; |
| 150 | break; |
| 151 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 152 | if (((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || |
| 153 | req.extts.rsv[0] || req.extts.rsv[1]) && |
| 154 | cmd == PTP_EXTTS_REQUEST2) { |
| 155 | err = -EINVAL; |
| 156 | break; |
| 157 | } else if (cmd == PTP_EXTTS_REQUEST) { |
| 158 | req.extts.flags &= ~PTP_EXTTS_VALID_FLAGS; |
| 159 | req.extts.rsv[0] = 0; |
| 160 | req.extts.rsv[1] = 0; |
| 161 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 162 | if (req.extts.index >= ops->n_ext_ts) { |
| 163 | err = -EINVAL; |
| 164 | break; |
| 165 | } |
| 166 | req.type = PTP_CLK_REQ_EXTTS; |
| 167 | enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; |
| 168 | err = ops->enable(ops, &req, enable); |
| 169 | break; |
| 170 | |
| 171 | case PTP_PEROUT_REQUEST: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 172 | case PTP_PEROUT_REQUEST2: |
| 173 | memset(&req, 0, sizeof(req)); |
| 174 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 175 | if (copy_from_user(&req.perout, (void __user *)arg, |
| 176 | sizeof(req.perout))) { |
| 177 | err = -EFAULT; |
| 178 | break; |
| 179 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 180 | if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) || |
| 181 | req.perout.rsv[0] || req.perout.rsv[1] || |
| 182 | req.perout.rsv[2] || req.perout.rsv[3]) && |
| 183 | cmd == PTP_PEROUT_REQUEST2) { |
| 184 | err = -EINVAL; |
| 185 | break; |
| 186 | } else if (cmd == PTP_PEROUT_REQUEST) { |
| 187 | req.perout.flags &= ~PTP_PEROUT_VALID_FLAGS; |
| 188 | req.perout.rsv[0] = 0; |
| 189 | req.perout.rsv[1] = 0; |
| 190 | req.perout.rsv[2] = 0; |
| 191 | req.perout.rsv[3] = 0; |
| 192 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 193 | if (req.perout.index >= ops->n_per_out) { |
| 194 | err = -EINVAL; |
| 195 | break; |
| 196 | } |
| 197 | req.type = PTP_CLK_REQ_PEROUT; |
| 198 | enable = req.perout.period.sec || req.perout.period.nsec; |
| 199 | err = ops->enable(ops, &req, enable); |
| 200 | break; |
| 201 | |
| 202 | case PTP_ENABLE_PPS: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 203 | case PTP_ENABLE_PPS2: |
| 204 | memset(&req, 0, sizeof(req)); |
| 205 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 206 | if (!capable(CAP_SYS_TIME)) |
| 207 | return -EPERM; |
| 208 | req.type = PTP_CLK_REQ_PPS; |
| 209 | enable = arg ? 1 : 0; |
| 210 | err = ops->enable(ops, &req, enable); |
| 211 | break; |
| 212 | |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 213 | case PTP_SYS_OFFSET_PRECISE: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 214 | case PTP_SYS_OFFSET_PRECISE2: |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 215 | if (!ptp->info->getcrosststamp) { |
| 216 | err = -EOPNOTSUPP; |
| 217 | break; |
| 218 | } |
| 219 | err = ptp->info->getcrosststamp(ptp->info, &xtstamp); |
| 220 | if (err) |
| 221 | break; |
| 222 | |
Vlad Tsyrklevich | 02a9079 | 2016-10-11 15:02:47 +0200 | [diff] [blame] | 223 | memset(&precise_offset, 0, sizeof(precise_offset)); |
Christopher S. Hall | 719f1aa | 2016-02-22 03:15:25 -0800 | [diff] [blame] | 224 | ts = ktime_to_timespec64(xtstamp.device); |
| 225 | precise_offset.device.sec = ts.tv_sec; |
| 226 | precise_offset.device.nsec = ts.tv_nsec; |
| 227 | ts = ktime_to_timespec64(xtstamp.sys_realtime); |
| 228 | precise_offset.sys_realtime.sec = ts.tv_sec; |
| 229 | precise_offset.sys_realtime.nsec = ts.tv_nsec; |
| 230 | ts = ktime_to_timespec64(xtstamp.sys_monoraw); |
| 231 | precise_offset.sys_monoraw.sec = ts.tv_sec; |
| 232 | precise_offset.sys_monoraw.nsec = ts.tv_nsec; |
| 233 | if (copy_to_user((void __user *)arg, &precise_offset, |
| 234 | sizeof(precise_offset))) |
| 235 | err = -EFAULT; |
| 236 | break; |
| 237 | |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 238 | case PTP_SYS_OFFSET_EXTENDED: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 239 | case PTP_SYS_OFFSET_EXTENDED2: |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 240 | if (!ptp->info->gettimex64) { |
| 241 | err = -EOPNOTSUPP; |
| 242 | break; |
| 243 | } |
| 244 | extoff = memdup_user((void __user *)arg, sizeof(*extoff)); |
| 245 | if (IS_ERR(extoff)) { |
| 246 | err = PTR_ERR(extoff); |
| 247 | extoff = NULL; |
| 248 | break; |
| 249 | } |
Eugene Syromiatnikov | 895ac13 | 2019-01-07 16:22:29 +0100 | [diff] [blame] | 250 | if (extoff->n_samples > PTP_MAX_SAMPLES |
| 251 | || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) { |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 252 | err = -EINVAL; |
| 253 | break; |
| 254 | } |
| 255 | for (i = 0; i < extoff->n_samples; i++) { |
| 256 | err = ptp->info->gettimex64(ptp->info, &ts, &sts); |
| 257 | if (err) |
| 258 | goto out; |
| 259 | extoff->ts[i][0].sec = sts.pre_ts.tv_sec; |
| 260 | extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; |
| 261 | extoff->ts[i][1].sec = ts.tv_sec; |
| 262 | extoff->ts[i][1].nsec = ts.tv_nsec; |
| 263 | extoff->ts[i][2].sec = sts.post_ts.tv_sec; |
| 264 | extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; |
| 265 | } |
| 266 | if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff))) |
| 267 | err = -EFAULT; |
| 268 | break; |
| 269 | |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 270 | case PTP_SYS_OFFSET: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 271 | case PTP_SYS_OFFSET2: |
Muhammad Falak R Wani | 2ece068 | 2016-05-20 17:51:02 +0530 | [diff] [blame] | 272 | sysoff = memdup_user((void __user *)arg, sizeof(*sysoff)); |
| 273 | if (IS_ERR(sysoff)) { |
| 274 | err = PTR_ERR(sysoff); |
Dan Carpenter | 6756325 | 2016-05-26 09:46:22 +0300 | [diff] [blame] | 275 | sysoff = NULL; |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 276 | break; |
| 277 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 278 | if (sysoff->n_samples > PTP_MAX_SAMPLES) { |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 279 | err = -EINVAL; |
| 280 | break; |
| 281 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 282 | pct = &sysoff->ts[0]; |
| 283 | for (i = 0; i < sysoff->n_samples; i++) { |
Arnd Bergmann | f696a21 | 2018-06-18 16:20:39 +0200 | [diff] [blame] | 284 | ktime_get_real_ts64(&ts); |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 285 | pct->sec = ts.tv_sec; |
| 286 | pct->nsec = ts.tv_nsec; |
| 287 | pct++; |
Miroslav Lichvar | 916444d | 2018-11-09 11:14:45 +0100 | [diff] [blame] | 288 | if (ops->gettimex64) |
| 289 | err = ops->gettimex64(ops, &ts, NULL); |
| 290 | else |
| 291 | err = ops->gettime64(ops, &ts); |
Miroslav Lichvar | 83d0bdc | 2018-11-09 11:14:43 +0100 | [diff] [blame] | 292 | if (err) |
| 293 | goto out; |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 294 | pct->sec = ts.tv_sec; |
| 295 | pct->nsec = ts.tv_nsec; |
| 296 | pct++; |
| 297 | } |
Arnd Bergmann | f696a21 | 2018-06-18 16:20:39 +0200 | [diff] [blame] | 298 | ktime_get_real_ts64(&ts); |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 299 | pct->sec = ts.tv_sec; |
| 300 | pct->nsec = ts.tv_nsec; |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 301 | if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) |
Richard Cochran | 215b13d | 2012-10-31 06:19:07 +0000 | [diff] [blame] | 302 | err = -EFAULT; |
| 303 | break; |
| 304 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 305 | case PTP_PIN_GETFUNC: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 306 | case PTP_PIN_GETFUNC2: |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 307 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { |
| 308 | err = -EFAULT; |
| 309 | break; |
| 310 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 311 | if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] |
| 312 | || pd.rsv[3] || pd.rsv[4]) |
| 313 | && cmd == PTP_PIN_GETFUNC2) { |
| 314 | err = -EINVAL; |
| 315 | break; |
| 316 | } else if (cmd == PTP_PIN_GETFUNC) { |
| 317 | pd.rsv[0] = 0; |
| 318 | pd.rsv[1] = 0; |
| 319 | pd.rsv[2] = 0; |
| 320 | pd.rsv[3] = 0; |
| 321 | pd.rsv[4] = 0; |
| 322 | } |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 323 | pin_index = pd.index; |
| 324 | if (pin_index >= ops->n_pins) { |
| 325 | err = -EINVAL; |
| 326 | break; |
| 327 | } |
Gustavo A. R. Silva | efa61c8 | 2018-10-16 15:06:41 +0200 | [diff] [blame] | 328 | pin_index = array_index_nospec(pin_index, ops->n_pins); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 329 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 330 | return -ERESTARTSYS; |
| 331 | pd = ops->pin_config[pin_index]; |
| 332 | mutex_unlock(&ptp->pincfg_mux); |
| 333 | if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) |
| 334 | err = -EFAULT; |
| 335 | break; |
| 336 | |
| 337 | case PTP_PIN_SETFUNC: |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 338 | case PTP_PIN_SETFUNC2: |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 339 | if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { |
| 340 | err = -EFAULT; |
| 341 | break; |
| 342 | } |
Felipe Balbi | 4156065 | 2019-09-11 09:16:21 +0300 | [diff] [blame] | 343 | if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] |
| 344 | || pd.rsv[3] || pd.rsv[4]) |
| 345 | && cmd == PTP_PIN_SETFUNC2) { |
| 346 | err = -EINVAL; |
| 347 | break; |
| 348 | } else if (cmd == PTP_PIN_SETFUNC) { |
| 349 | pd.rsv[0] = 0; |
| 350 | pd.rsv[1] = 0; |
| 351 | pd.rsv[2] = 0; |
| 352 | pd.rsv[3] = 0; |
| 353 | pd.rsv[4] = 0; |
| 354 | } |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 355 | pin_index = pd.index; |
| 356 | if (pin_index >= ops->n_pins) { |
| 357 | err = -EINVAL; |
| 358 | break; |
| 359 | } |
Gustavo A. R. Silva | efa61c8 | 2018-10-16 15:06:41 +0200 | [diff] [blame] | 360 | pin_index = array_index_nospec(pin_index, ops->n_pins); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 361 | if (mutex_lock_interruptible(&ptp->pincfg_mux)) |
| 362 | return -ERESTARTSYS; |
| 363 | err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); |
| 364 | mutex_unlock(&ptp->pincfg_mux); |
| 365 | break; |
| 366 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 367 | default: |
| 368 | err = -ENOTTY; |
| 369 | break; |
| 370 | } |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 371 | |
Miroslav Lichvar | 83d0bdc | 2018-11-09 11:14:43 +0100 | [diff] [blame] | 372 | out: |
Miroslav Lichvar | 3618008 | 2018-11-09 11:14:44 +0100 | [diff] [blame] | 373 | kfree(extoff); |
Richard Cochran | c3484c2 | 2012-11-26 01:44:35 +0000 | [diff] [blame] | 374 | kfree(sysoff); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 375 | return err; |
| 376 | } |
| 377 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 378 | __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] | 379 | { |
| 380 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 381 | |
| 382 | poll_wait(fp, &ptp->tsev_wq, wait); |
| 383 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 384 | return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 387 | #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) |
| 388 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 389 | ssize_t ptp_read(struct posix_clock *pc, |
| 390 | uint rdflags, char __user *buf, size_t cnt) |
| 391 | { |
| 392 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 393 | struct timestamp_event_queue *queue = &ptp->tsevq; |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 394 | struct ptp_extts_event *event; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 395 | unsigned long flags; |
| 396 | size_t qcnt, i; |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 397 | int result; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 398 | |
| 399 | if (cnt % sizeof(struct ptp_extts_event) != 0) |
| 400 | return -EINVAL; |
| 401 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 402 | if (cnt > EXTTS_BUFSIZE) |
| 403 | cnt = EXTTS_BUFSIZE; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 404 | |
| 405 | cnt = cnt / sizeof(struct ptp_extts_event); |
| 406 | |
| 407 | if (mutex_lock_interruptible(&ptp->tsevq_mux)) |
| 408 | return -ERESTARTSYS; |
| 409 | |
| 410 | if (wait_event_interruptible(ptp->tsev_wq, |
| 411 | ptp->defunct || queue_cnt(queue))) { |
| 412 | mutex_unlock(&ptp->tsevq_mux); |
| 413 | return -ERESTARTSYS; |
| 414 | } |
| 415 | |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 416 | if (ptp->defunct) { |
| 417 | mutex_unlock(&ptp->tsevq_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 418 | return -ENODEV; |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 419 | } |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 420 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 421 | event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); |
| 422 | if (!event) { |
| 423 | mutex_unlock(&ptp->tsevq_mux); |
| 424 | return -ENOMEM; |
| 425 | } |
| 426 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 427 | spin_lock_irqsave(&queue->lock, flags); |
| 428 | |
| 429 | qcnt = queue_cnt(queue); |
| 430 | |
| 431 | if (cnt > qcnt) |
| 432 | cnt = qcnt; |
| 433 | |
| 434 | for (i = 0; i < cnt; i++) { |
| 435 | event[i] = queue->buf[queue->head]; |
| 436 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; |
| 437 | } |
| 438 | |
| 439 | spin_unlock_irqrestore(&queue->lock, flags); |
| 440 | |
| 441 | cnt = cnt * sizeof(struct ptp_extts_event); |
| 442 | |
| 443 | mutex_unlock(&ptp->tsevq_mux); |
| 444 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 445 | result = cnt; |
Dan Carpenter | fb5a18c | 2011-05-29 22:54:07 +0300 | [diff] [blame] | 446 | if (copy_to_user(buf, event, cnt)) |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 447 | result = -EFAULT; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 448 | |
Richard Cochran | c7ec0ba | 2012-11-26 01:44:34 +0000 | [diff] [blame] | 449 | kfree(event); |
| 450 | return result; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 451 | } |