ptp_chardev.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <linux/slab.h>
  25. #include "ptp_private.h"
  26. int ptp_open(struct posix_clock *pc, fmode_t fmode)
  27. {
  28. return 0;
  29. }
  30. long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
  31. {
  32. struct ptp_clock_caps caps;
  33. struct ptp_clock_request req;
  34. struct ptp_sys_offset *sysoff = NULL;
  35. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  36. struct ptp_clock_info *ops = ptp->info;
  37. struct ptp_clock_time *pct;
  38. struct timespec ts;
  39. int enable, err = 0;
  40. unsigned int i;
  41. switch (cmd) {
  42. case PTP_CLOCK_GETCAPS:
  43. memset(&caps, 0, sizeof(caps));
  44. caps.max_adj = ptp->info->max_adj;
  45. caps.n_alarm = ptp->info->n_alarm;
  46. caps.n_ext_ts = ptp->info->n_ext_ts;
  47. caps.n_per_out = ptp->info->n_per_out;
  48. caps.pps = ptp->info->pps;
  49. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  50. err = -EFAULT;
  51. break;
  52. case PTP_EXTTS_REQUEST:
  53. if (copy_from_user(&req.extts, (void __user *)arg,
  54. sizeof(req.extts))) {
  55. err = -EFAULT;
  56. break;
  57. }
  58. if (req.extts.index >= ops->n_ext_ts) {
  59. err = -EINVAL;
  60. break;
  61. }
  62. req.type = PTP_CLK_REQ_EXTTS;
  63. enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
  64. err = ops->enable(ops, &req, enable);
  65. break;
  66. case PTP_PEROUT_REQUEST:
  67. if (copy_from_user(&req.perout, (void __user *)arg,
  68. sizeof(req.perout))) {
  69. err = -EFAULT;
  70. break;
  71. }
  72. if (req.perout.index >= ops->n_per_out) {
  73. err = -EINVAL;
  74. break;
  75. }
  76. req.type = PTP_CLK_REQ_PEROUT;
  77. enable = req.perout.period.sec || req.perout.period.nsec;
  78. err = ops->enable(ops, &req, enable);
  79. break;
  80. case PTP_ENABLE_PPS:
  81. if (!capable(CAP_SYS_TIME))
  82. return -EPERM;
  83. req.type = PTP_CLK_REQ_PPS;
  84. enable = arg ? 1 : 0;
  85. err = ops->enable(ops, &req, enable);
  86. break;
  87. case PTP_SYS_OFFSET:
  88. sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL);
  89. if (!sysoff) {
  90. err = -ENOMEM;
  91. break;
  92. }
  93. if (copy_from_user(sysoff, (void __user *)arg,
  94. sizeof(*sysoff))) {
  95. err = -EFAULT;
  96. break;
  97. }
  98. if (sysoff->n_samples > PTP_MAX_SAMPLES) {
  99. err = -EINVAL;
  100. break;
  101. }
  102. pct = &sysoff->ts[0];
  103. for (i = 0; i < sysoff->n_samples; i++) {
  104. getnstimeofday(&ts);
  105. pct->sec = ts.tv_sec;
  106. pct->nsec = ts.tv_nsec;
  107. pct++;
  108. ptp->info->gettime(ptp->info, &ts);
  109. pct->sec = ts.tv_sec;
  110. pct->nsec = ts.tv_nsec;
  111. pct++;
  112. }
  113. getnstimeofday(&ts);
  114. pct->sec = ts.tv_sec;
  115. pct->nsec = ts.tv_nsec;
  116. if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
  117. err = -EFAULT;
  118. break;
  119. default:
  120. err = -ENOTTY;
  121. break;
  122. }
  123. kfree(sysoff);
  124. return err;
  125. }
  126. unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
  127. {
  128. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  129. poll_wait(fp, &ptp->tsev_wq, wait);
  130. return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
  131. }
  132. #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
  133. ssize_t ptp_read(struct posix_clock *pc,
  134. uint rdflags, char __user *buf, size_t cnt)
  135. {
  136. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  137. struct timestamp_event_queue *queue = &ptp->tsevq;
  138. struct ptp_extts_event *event;
  139. unsigned long flags;
  140. size_t qcnt, i;
  141. int result;
  142. if (cnt % sizeof(struct ptp_extts_event) != 0)
  143. return -EINVAL;
  144. if (cnt > EXTTS_BUFSIZE)
  145. cnt = EXTTS_BUFSIZE;
  146. cnt = cnt / sizeof(struct ptp_extts_event);
  147. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  148. return -ERESTARTSYS;
  149. if (wait_event_interruptible(ptp->tsev_wq,
  150. ptp->defunct || queue_cnt(queue))) {
  151. mutex_unlock(&ptp->tsevq_mux);
  152. return -ERESTARTSYS;
  153. }
  154. if (ptp->defunct) {
  155. mutex_unlock(&ptp->tsevq_mux);
  156. return -ENODEV;
  157. }
  158. event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
  159. if (!event) {
  160. mutex_unlock(&ptp->tsevq_mux);
  161. return -ENOMEM;
  162. }
  163. spin_lock_irqsave(&queue->lock, flags);
  164. qcnt = queue_cnt(queue);
  165. if (cnt > qcnt)
  166. cnt = qcnt;
  167. for (i = 0; i < cnt; i++) {
  168. event[i] = queue->buf[queue->head];
  169. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  170. }
  171. spin_unlock_irqrestore(&queue->lock, flags);
  172. cnt = cnt * sizeof(struct ptp_extts_event);
  173. mutex_unlock(&ptp->tsevq_mux);
  174. result = cnt;
  175. if (copy_to_user(buf, event, cnt))
  176. result = -EFAULT;
  177. kfree(event);
  178. return result;
  179. }