1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PTP 1588 clock support - character device implementation. 4 * 5 * Copyright (C) 2010 OMICRON electronics GmbH 6 */ 7 #include <linux/compat.h> 8 #include <linux/module.h> 9 #include <linux/posix-clock.h> 10 #include <linux/poll.h> 11 #include <linux/sched.h> 12 #include <linux/slab.h> 13 #include <linux/timekeeping.h> 14 #include <linux/debugfs.h> 15 16 #include <linux/nospec.h> 17 18 #include "ptp_private.h" 19 20 static int ptp_disable_pinfunc(struct ptp_clock_info *ops, 21 enum ptp_pin_function func, unsigned int chan) 22 { 23 struct ptp_clock_request rq; 24 int err = 0; 25 26 memset(&rq, 0, sizeof(rq)); 27 28 switch (func) { 29 case PTP_PF_NONE: 30 break; 31 case PTP_PF_EXTTS: 32 rq.type = PTP_CLK_REQ_EXTTS; 33 rq.extts.index = chan; 34 err = ops->enable(ops, &rq, 0); 35 break; 36 case PTP_PF_PEROUT: 37 rq.type = PTP_CLK_REQ_PEROUT; 38 rq.perout.index = chan; 39 err = ops->enable(ops, &rq, 0); 40 break; 41 case PTP_PF_PHYSYNC: 42 break; 43 default: 44 return -EINVAL; 45 } 46 47 return err; 48 } 49 50 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, 51 enum ptp_pin_function func, unsigned int chan) 52 { 53 struct ptp_clock_info *info = ptp->info; 54 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin]; 55 unsigned int i; 56 57 /* Check to see if any other pin previously had this function. */ 58 for (i = 0; i < info->n_pins; i++) { 59 if (info->pin_config[i].func == func && 60 info->pin_config[i].chan == chan) { 61 pin1 = &info->pin_config[i]; 62 break; 63 } 64 } 65 if (pin1 && i == pin) 66 return 0; 67 68 /* Check the desired function and channel. */ 69 switch (func) { 70 case PTP_PF_NONE: 71 break; 72 case PTP_PF_EXTTS: 73 if (chan >= info->n_ext_ts) 74 return -EINVAL; 75 break; 76 case PTP_PF_PEROUT: 77 if (chan >= info->n_per_out) 78 return -EINVAL; 79 break; 80 case PTP_PF_PHYSYNC: 81 if (chan != 0) 82 return -EINVAL; 83 break; 84 default: 85 return -EINVAL; 86 } 87 88 if (info->verify(info, pin, func, chan)) { 89 pr_err("driver cannot use function %u and channel %u on pin %u\n", 90 func, chan, pin); 91 return -EOPNOTSUPP; 92 } 93 94 /* Disable whatever function was previously assigned. */ 95 if (pin1) { 96 ptp_disable_pinfunc(info, func, chan); 97 pin1->func = PTP_PF_NONE; 98 pin1->chan = 0; 99 } 100 ptp_disable_pinfunc(info, pin2->func, pin2->chan); 101 pin2->func = func; 102 pin2->chan = chan; 103 104 return 0; 105 } 106 107 int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode) 108 { 109 struct ptp_clock *ptp = 110 container_of(pccontext->clk, struct ptp_clock, clock); 111 struct timestamp_event_queue *queue; 112 char debugfsname[32]; 113 unsigned long flags; 114 115 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 116 if (!queue) 117 return -EINVAL; 118 queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL); 119 if (!queue->mask) { 120 kfree(queue); 121 return -EINVAL; 122 } 123 bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); 124 spin_lock_init(&queue->lock); 125 spin_lock_irqsave(&ptp->tsevqs_lock, flags); 126 list_add_tail(&queue->qlist, &ptp->tsevqs); 127 spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); 128 pccontext->private_clkdata = queue; 129 130 /* Debugfs contents */ 131 sprintf(debugfsname, "0x%p", queue); 132 queue->debugfs_instance = 133 debugfs_create_dir(debugfsname, ptp->debugfs_root); 134 queue->dfs_bitmap.array = (u32 *)queue->mask; 135 queue->dfs_bitmap.n_elements = 136 DIV_ROUND_UP(PTP_MAX_CHANNELS, BITS_PER_BYTE * sizeof(u32)); 137 debugfs_create_u32_array("mask", 0444, queue->debugfs_instance, 138 &queue->dfs_bitmap); 139 140 return 0; 141 } 142 143 int ptp_release(struct posix_clock_context *pccontext) 144 { 145 struct timestamp_event_queue *queue = pccontext->private_clkdata; 146 unsigned long flags; 147 struct ptp_clock *ptp = 148 container_of(pccontext->clk, struct ptp_clock, clock); 149 150 debugfs_remove(queue->debugfs_instance); 151 pccontext->private_clkdata = NULL; 152 spin_lock_irqsave(&ptp->tsevqs_lock, flags); 153 list_del(&queue->qlist); 154 spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); 155 bitmap_free(queue->mask); 156 kfree(queue); 157 return 0; 158 } 159 160 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, 161 unsigned long arg) 162 { 163 struct ptp_clock *ptp = 164 container_of(pccontext->clk, struct ptp_clock, clock); 165 struct ptp_sys_offset_extended *extoff = NULL; 166 struct ptp_sys_offset_precise precise_offset; 167 struct system_device_crosststamp xtstamp; 168 struct ptp_clock_info *ops = ptp->info; 169 struct ptp_sys_offset *sysoff = NULL; 170 struct timestamp_event_queue *tsevq; 171 struct ptp_system_timestamp sts; 172 struct ptp_clock_request req; 173 struct ptp_clock_caps caps; 174 struct ptp_clock_time *pct; 175 unsigned int i, pin_index; 176 struct ptp_pin_desc pd; 177 struct timespec64 ts; 178 int enable, err = 0; 179 180 if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2) 181 arg = (unsigned long)compat_ptr(arg); 182 183 tsevq = pccontext->private_clkdata; 184 185 switch (cmd) { 186 187 case PTP_CLOCK_GETCAPS: 188 case PTP_CLOCK_GETCAPS2: 189 memset(&caps, 0, sizeof(caps)); 190 191 caps.max_adj = ptp->info->max_adj; 192 caps.n_alarm = ptp->info->n_alarm; 193 caps.n_ext_ts = ptp->info->n_ext_ts; 194 caps.n_per_out = ptp->info->n_per_out; 195 caps.pps = ptp->info->pps; 196 caps.n_pins = ptp->info->n_pins; 197 caps.cross_timestamping = ptp->info->getcrosststamp != NULL; 198 caps.adjust_phase = ptp->info->adjphase != NULL && 199 ptp->info->getmaxphase != NULL; 200 if (caps.adjust_phase) 201 caps.max_phase_adj = ptp->info->getmaxphase(ptp->info); 202 if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 203 err = -EFAULT; 204 break; 205 206 case PTP_EXTTS_REQUEST: 207 case PTP_EXTTS_REQUEST2: 208 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 209 err = -EACCES; 210 break; 211 } 212 memset(&req, 0, sizeof(req)); 213 214 if (copy_from_user(&req.extts, (void __user *)arg, 215 sizeof(req.extts))) { 216 err = -EFAULT; 217 break; 218 } 219 if (cmd == PTP_EXTTS_REQUEST2) { 220 /* Tell the drivers to check the flags carefully. */ 221 req.extts.flags |= PTP_STRICT_FLAGS; 222 /* Make sure no reserved bit is set. */ 223 if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || 224 req.extts.rsv[0] || req.extts.rsv[1]) { 225 err = -EINVAL; 226 break; 227 } 228 /* Ensure one of the rising/falling edge bits is set. */ 229 if ((req.extts.flags & PTP_ENABLE_FEATURE) && 230 (req.extts.flags & PTP_EXTTS_EDGES) == 0) { 231 err = -EINVAL; 232 break; 233 } 234 } else if (cmd == PTP_EXTTS_REQUEST) { 235 req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS; 236 req.extts.rsv[0] = 0; 237 req.extts.rsv[1] = 0; 238 } 239 if (req.extts.index >= ops->n_ext_ts) { 240 err = -EINVAL; 241 break; 242 } 243 req.type = PTP_CLK_REQ_EXTTS; 244 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; 245 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 246 return -ERESTARTSYS; 247 err = ops->enable(ops, &req, enable); 248 mutex_unlock(&ptp->pincfg_mux); 249 break; 250 251 case PTP_PEROUT_REQUEST: 252 case PTP_PEROUT_REQUEST2: 253 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 254 err = -EACCES; 255 break; 256 } 257 memset(&req, 0, sizeof(req)); 258 259 if (copy_from_user(&req.perout, (void __user *)arg, 260 sizeof(req.perout))) { 261 err = -EFAULT; 262 break; 263 } 264 if (cmd == PTP_PEROUT_REQUEST2) { 265 struct ptp_perout_request *perout = &req.perout; 266 267 if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) { 268 err = -EINVAL; 269 break; 270 } 271 /* 272 * The "on" field has undefined meaning if 273 * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat 274 * it as reserved, which must be set to zero. 275 */ 276 if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && 277 (perout->rsv[0] || perout->rsv[1] || 278 perout->rsv[2] || perout->rsv[3])) { 279 err = -EINVAL; 280 break; 281 } 282 if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { 283 /* The duty cycle must be subunitary. */ 284 if (perout->on.sec > perout->period.sec || 285 (perout->on.sec == perout->period.sec && 286 perout->on.nsec > perout->period.nsec)) { 287 err = -ERANGE; 288 break; 289 } 290 } 291 if (perout->flags & PTP_PEROUT_PHASE) { 292 /* 293 * The phase should be specified modulo the 294 * period, therefore anything equal or larger 295 * than 1 period is invalid. 296 */ 297 if (perout->phase.sec > perout->period.sec || 298 (perout->phase.sec == perout->period.sec && 299 perout->phase.nsec >= perout->period.nsec)) { 300 err = -ERANGE; 301 break; 302 } 303 } 304 } else if (cmd == PTP_PEROUT_REQUEST) { 305 req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS; 306 req.perout.rsv[0] = 0; 307 req.perout.rsv[1] = 0; 308 req.perout.rsv[2] = 0; 309 req.perout.rsv[3] = 0; 310 } 311 if (req.perout.index >= ops->n_per_out) { 312 err = -EINVAL; 313 break; 314 } 315 req.type = PTP_CLK_REQ_PEROUT; 316 enable = req.perout.period.sec || req.perout.period.nsec; 317 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 318 return -ERESTARTSYS; 319 err = ops->enable(ops, &req, enable); 320 mutex_unlock(&ptp->pincfg_mux); 321 break; 322 323 case PTP_ENABLE_PPS: 324 case PTP_ENABLE_PPS2: 325 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 326 err = -EACCES; 327 break; 328 } 329 memset(&req, 0, sizeof(req)); 330 331 if (!capable(CAP_SYS_TIME)) 332 return -EPERM; 333 req.type = PTP_CLK_REQ_PPS; 334 enable = arg ? 1 : 0; 335 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 336 return -ERESTARTSYS; 337 err = ops->enable(ops, &req, enable); 338 mutex_unlock(&ptp->pincfg_mux); 339 break; 340 341 case PTP_SYS_OFFSET_PRECISE: 342 case PTP_SYS_OFFSET_PRECISE2: 343 if (!ptp->info->getcrosststamp) { 344 err = -EOPNOTSUPP; 345 break; 346 } 347 err = ptp->info->getcrosststamp(ptp->info, &xtstamp); 348 if (err) 349 break; 350 351 memset(&precise_offset, 0, sizeof(precise_offset)); 352 ts = ktime_to_timespec64(xtstamp.device); 353 precise_offset.device.sec = ts.tv_sec; 354 precise_offset.device.nsec = ts.tv_nsec; 355 ts = ktime_to_timespec64(xtstamp.sys_realtime); 356 precise_offset.sys_realtime.sec = ts.tv_sec; 357 precise_offset.sys_realtime.nsec = ts.tv_nsec; 358 ts = ktime_to_timespec64(xtstamp.sys_monoraw); 359 precise_offset.sys_monoraw.sec = ts.tv_sec; 360 precise_offset.sys_monoraw.nsec = ts.tv_nsec; 361 if (copy_to_user((void __user *)arg, &precise_offset, 362 sizeof(precise_offset))) 363 err = -EFAULT; 364 break; 365 366 case PTP_SYS_OFFSET_EXTENDED: 367 case PTP_SYS_OFFSET_EXTENDED2: 368 if (!ptp->info->gettimex64) { 369 err = -EOPNOTSUPP; 370 break; 371 } 372 extoff = memdup_user((void __user *)arg, sizeof(*extoff)); 373 if (IS_ERR(extoff)) { 374 err = PTR_ERR(extoff); 375 extoff = NULL; 376 break; 377 } 378 if (extoff->n_samples > PTP_MAX_SAMPLES || 379 extoff->rsv[0] || extoff->rsv[1] || 380 (extoff->clockid != CLOCK_REALTIME && 381 extoff->clockid != CLOCK_MONOTONIC && 382 extoff->clockid != CLOCK_MONOTONIC_RAW)) { 383 err = -EINVAL; 384 break; 385 } 386 sts.clockid = extoff->clockid; 387 for (i = 0; i < extoff->n_samples; i++) { 388 err = ptp->info->gettimex64(ptp->info, &ts, &sts); 389 if (err) 390 goto out; 391 extoff->ts[i][0].sec = sts.pre_ts.tv_sec; 392 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; 393 extoff->ts[i][1].sec = ts.tv_sec; 394 extoff->ts[i][1].nsec = ts.tv_nsec; 395 extoff->ts[i][2].sec = sts.post_ts.tv_sec; 396 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; 397 } 398 if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff))) 399 err = -EFAULT; 400 break; 401 402 case PTP_SYS_OFFSET: 403 case PTP_SYS_OFFSET2: 404 sysoff = memdup_user((void __user *)arg, sizeof(*sysoff)); 405 if (IS_ERR(sysoff)) { 406 err = PTR_ERR(sysoff); 407 sysoff = NULL; 408 break; 409 } 410 if (sysoff->n_samples > PTP_MAX_SAMPLES) { 411 err = -EINVAL; 412 break; 413 } 414 pct = &sysoff->ts[0]; 415 for (i = 0; i < sysoff->n_samples; i++) { 416 ktime_get_real_ts64(&ts); 417 pct->sec = ts.tv_sec; 418 pct->nsec = ts.tv_nsec; 419 pct++; 420 if (ops->gettimex64) 421 err = ops->gettimex64(ops, &ts, NULL); 422 else 423 err = ops->gettime64(ops, &ts); 424 if (err) 425 goto out; 426 pct->sec = ts.tv_sec; 427 pct->nsec = ts.tv_nsec; 428 pct++; 429 } 430 ktime_get_real_ts64(&ts); 431 pct->sec = ts.tv_sec; 432 pct->nsec = ts.tv_nsec; 433 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) 434 err = -EFAULT; 435 break; 436 437 case PTP_PIN_GETFUNC: 438 case PTP_PIN_GETFUNC2: 439 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { 440 err = -EFAULT; 441 break; 442 } 443 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] 444 || pd.rsv[3] || pd.rsv[4]) 445 && cmd == PTP_PIN_GETFUNC2) { 446 err = -EINVAL; 447 break; 448 } else if (cmd == PTP_PIN_GETFUNC) { 449 pd.rsv[0] = 0; 450 pd.rsv[1] = 0; 451 pd.rsv[2] = 0; 452 pd.rsv[3] = 0; 453 pd.rsv[4] = 0; 454 } 455 pin_index = pd.index; 456 if (pin_index >= ops->n_pins) { 457 err = -EINVAL; 458 break; 459 } 460 pin_index = array_index_nospec(pin_index, ops->n_pins); 461 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 462 return -ERESTARTSYS; 463 pd = ops->pin_config[pin_index]; 464 mutex_unlock(&ptp->pincfg_mux); 465 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) 466 err = -EFAULT; 467 break; 468 469 case PTP_PIN_SETFUNC: 470 case PTP_PIN_SETFUNC2: 471 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 472 err = -EACCES; 473 break; 474 } 475 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { 476 err = -EFAULT; 477 break; 478 } 479 if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] 480 || pd.rsv[3] || pd.rsv[4]) 481 && cmd == PTP_PIN_SETFUNC2) { 482 err = -EINVAL; 483 break; 484 } else if (cmd == PTP_PIN_SETFUNC) { 485 pd.rsv[0] = 0; 486 pd.rsv[1] = 0; 487 pd.rsv[2] = 0; 488 pd.rsv[3] = 0; 489 pd.rsv[4] = 0; 490 } 491 pin_index = pd.index; 492 if (pin_index >= ops->n_pins) { 493 err = -EINVAL; 494 break; 495 } 496 pin_index = array_index_nospec(pin_index, ops->n_pins); 497 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 498 return -ERESTARTSYS; 499 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); 500 mutex_unlock(&ptp->pincfg_mux); 501 break; 502 503 case PTP_MASK_CLEAR_ALL: 504 bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); 505 break; 506 507 case PTP_MASK_EN_SINGLE: 508 if (copy_from_user(&i, (void __user *)arg, sizeof(i))) { 509 err = -EFAULT; 510 break; 511 } 512 if (i >= PTP_MAX_CHANNELS) { 513 err = -EFAULT; 514 break; 515 } 516 set_bit(i, tsevq->mask); 517 break; 518 519 default: 520 err = -ENOTTY; 521 break; 522 } 523 524 out: 525 kfree(extoff); 526 kfree(sysoff); 527 return err; 528 } 529 530 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp, 531 poll_table *wait) 532 { 533 struct ptp_clock *ptp = 534 container_of(pccontext->clk, struct ptp_clock, clock); 535 struct timestamp_event_queue *queue; 536 537 queue = pccontext->private_clkdata; 538 if (!queue) 539 return EPOLLERR; 540 541 poll_wait(fp, &ptp->tsev_wq, wait); 542 543 return queue_cnt(queue) ? EPOLLIN : 0; 544 } 545 546 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) 547 548 ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, 549 char __user *buf, size_t cnt) 550 { 551 struct ptp_clock *ptp = 552 container_of(pccontext->clk, struct ptp_clock, clock); 553 struct timestamp_event_queue *queue; 554 struct ptp_extts_event *event; 555 unsigned long flags; 556 size_t qcnt, i; 557 int result; 558 559 queue = pccontext->private_clkdata; 560 if (!queue) { 561 result = -EINVAL; 562 goto exit; 563 } 564 565 if (cnt % sizeof(struct ptp_extts_event) != 0) { 566 result = -EINVAL; 567 goto exit; 568 } 569 570 if (cnt > EXTTS_BUFSIZE) 571 cnt = EXTTS_BUFSIZE; 572 573 cnt = cnt / sizeof(struct ptp_extts_event); 574 575 if (wait_event_interruptible(ptp->tsev_wq, 576 ptp->defunct || queue_cnt(queue))) { 577 return -ERESTARTSYS; 578 } 579 580 if (ptp->defunct) { 581 result = -ENODEV; 582 goto exit; 583 } 584 585 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); 586 if (!event) { 587 result = -ENOMEM; 588 goto exit; 589 } 590 591 spin_lock_irqsave(&queue->lock, flags); 592 593 qcnt = queue_cnt(queue); 594 595 if (cnt > qcnt) 596 cnt = qcnt; 597 598 for (i = 0; i < cnt; i++) { 599 event[i] = queue->buf[queue->head]; 600 /* Paired with READ_ONCE() in queue_cnt() */ 601 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); 602 } 603 604 spin_unlock_irqrestore(&queue->lock, flags); 605 606 cnt = cnt * sizeof(struct ptp_extts_event); 607 608 result = cnt; 609 if (copy_to_user(buf, event, cnt)) { 610 result = -EFAULT; 611 goto free_event; 612 } 613 614 free_event: 615 kfree(event); 616 exit: 617 return result; 618 } 619