ptp_chardev.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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>
  24. #include "ptp_private.h"
  25. int ptp_open(struct posix_clock *pc, fmode_t fmode)
  26. {
  27. return 0;
  28. }
  29. long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
  30. {
  31. struct ptp_clock_caps caps;
  32. struct ptp_clock_request req;
  33. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  34. struct ptp_clock_info *ops = ptp->info;
  35. int enable, err = 0;
  36. switch (cmd) {
  37. case PTP_CLOCK_GETCAPS:
  38. memset(&caps, 0, sizeof(caps));
  39. caps.max_adj = ptp->info->max_adj;
  40. caps.n_alarm = ptp->info->n_alarm;
  41. caps.n_ext_ts = ptp->info->n_ext_ts;
  42. caps.n_per_out = ptp->info->n_per_out;
  43. caps.pps = ptp->info->pps;
  44. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  45. err = -EFAULT;
  46. break;
  47. case PTP_EXTTS_REQUEST:
  48. if (copy_from_user(&req.extts, (void __user *)arg,
  49. sizeof(req.extts))) {
  50. err = -EFAULT;
  51. break;
  52. }
  53. if (req.extts.index >= ops->n_ext_ts) {
  54. err = -EINVAL;
  55. break;
  56. }
  57. req.type = PTP_CLK_REQ_EXTTS;
  58. enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
  59. err = ops->enable(ops, &req, enable);
  60. break;
  61. case PTP_PEROUT_REQUEST:
  62. if (copy_from_user(&req.perout, (void __user *)arg,
  63. sizeof(req.perout))) {
  64. err = -EFAULT;
  65. break;
  66. }
  67. if (req.perout.index >= ops->n_per_out) {
  68. err = -EINVAL;
  69. break;
  70. }
  71. req.type = PTP_CLK_REQ_PEROUT;
  72. enable = req.perout.period.sec || req.perout.period.nsec;
  73. err = ops->enable(ops, &req, enable);
  74. break;
  75. case PTP_ENABLE_PPS:
  76. if (!capable(CAP_SYS_TIME))
  77. return -EPERM;
  78. req.type = PTP_CLK_REQ_PPS;
  79. enable = arg ? 1 : 0;
  80. err = ops->enable(ops, &req, enable);
  81. break;
  82. default:
  83. err = -ENOTTY;
  84. break;
  85. }
  86. return err;
  87. }
  88. unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
  89. {
  90. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  91. poll_wait(fp, &ptp->tsev_wq, wait);
  92. return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
  93. }
  94. ssize_t ptp_read(struct posix_clock *pc,
  95. uint rdflags, char __user *buf, size_t cnt)
  96. {
  97. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  98. struct timestamp_event_queue *queue = &ptp->tsevq;
  99. struct ptp_extts_event event[PTP_BUF_TIMESTAMPS];
  100. unsigned long flags;
  101. size_t qcnt, i;
  102. if (cnt % sizeof(struct ptp_extts_event) != 0)
  103. return -EINVAL;
  104. if (cnt > sizeof(event))
  105. cnt = sizeof(event);
  106. cnt = cnt / sizeof(struct ptp_extts_event);
  107. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  108. return -ERESTARTSYS;
  109. if (wait_event_interruptible(ptp->tsev_wq,
  110. ptp->defunct || queue_cnt(queue))) {
  111. mutex_unlock(&ptp->tsevq_mux);
  112. return -ERESTARTSYS;
  113. }
  114. if (ptp->defunct) {
  115. mutex_unlock(&ptp->tsevq_mux);
  116. return -ENODEV;
  117. }
  118. spin_lock_irqsave(&queue->lock, flags);
  119. qcnt = queue_cnt(queue);
  120. if (cnt > qcnt)
  121. cnt = qcnt;
  122. for (i = 0; i < cnt; i++) {
  123. event[i] = queue->buf[queue->head];
  124. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  125. }
  126. spin_unlock_irqrestore(&queue->lock, flags);
  127. cnt = cnt * sizeof(struct ptp_extts_event);
  128. mutex_unlock(&ptp->tsevq_mux);
  129. if (copy_to_user(buf, event, cnt))
  130. return -EFAULT;
  131. return cnt;
  132. }