ptp_chardev.c 3.9 KB

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