ptp_sysfs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * PTP 1588 clock support - sysfs interface.
  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/capability.h>
  21. #include "ptp_private.h"
  22. static ssize_t clock_name_show(struct device *dev,
  23. struct device_attribute *attr, char *page)
  24. {
  25. struct ptp_clock *ptp = dev_get_drvdata(dev);
  26. return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
  27. }
  28. static DEVICE_ATTR(clock_name, 0444, clock_name_show, NULL);
  29. #define PTP_SHOW_INT(name, var) \
  30. static ssize_t var##_show(struct device *dev, \
  31. struct device_attribute *attr, char *page) \
  32. { \
  33. struct ptp_clock *ptp = dev_get_drvdata(dev); \
  34. return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
  35. } \
  36. static DEVICE_ATTR(name, 0444, var##_show, NULL);
  37. PTP_SHOW_INT(max_adjustment, max_adj);
  38. PTP_SHOW_INT(n_alarms, n_alarm);
  39. PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
  40. PTP_SHOW_INT(n_periodic_outputs, n_per_out);
  41. PTP_SHOW_INT(pps_available, pps);
  42. static struct attribute *ptp_attrs[] = {
  43. &dev_attr_clock_name.attr,
  44. &dev_attr_max_adjustment.attr,
  45. &dev_attr_n_alarms.attr,
  46. &dev_attr_n_external_timestamps.attr,
  47. &dev_attr_n_periodic_outputs.attr,
  48. &dev_attr_pps_available.attr,
  49. NULL,
  50. };
  51. static const struct attribute_group ptp_group = {
  52. .attrs = ptp_attrs,
  53. };
  54. const struct attribute_group *ptp_groups[] = {
  55. &ptp_group,
  56. NULL,
  57. };
  58. static ssize_t extts_enable_store(struct device *dev,
  59. struct device_attribute *attr,
  60. const char *buf, size_t count)
  61. {
  62. struct ptp_clock *ptp = dev_get_drvdata(dev);
  63. struct ptp_clock_info *ops = ptp->info;
  64. struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
  65. int cnt, enable;
  66. int err = -EINVAL;
  67. cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
  68. if (cnt != 2)
  69. goto out;
  70. if (req.extts.index >= ops->n_ext_ts)
  71. goto out;
  72. err = ops->enable(ops, &req, enable ? 1 : 0);
  73. if (err)
  74. goto out;
  75. return count;
  76. out:
  77. return err;
  78. }
  79. static ssize_t extts_fifo_show(struct device *dev,
  80. struct device_attribute *attr, char *page)
  81. {
  82. struct ptp_clock *ptp = dev_get_drvdata(dev);
  83. struct timestamp_event_queue *queue = &ptp->tsevq;
  84. struct ptp_extts_event event;
  85. unsigned long flags;
  86. size_t qcnt;
  87. int cnt = 0;
  88. memset(&event, 0, sizeof(event));
  89. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  90. return -ERESTARTSYS;
  91. spin_lock_irqsave(&queue->lock, flags);
  92. qcnt = queue_cnt(queue);
  93. if (qcnt) {
  94. event = queue->buf[queue->head];
  95. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  96. }
  97. spin_unlock_irqrestore(&queue->lock, flags);
  98. if (!qcnt)
  99. goto out;
  100. cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
  101. event.index, event.t.sec, event.t.nsec);
  102. out:
  103. mutex_unlock(&ptp->tsevq_mux);
  104. return cnt;
  105. }
  106. static ssize_t period_store(struct device *dev,
  107. struct device_attribute *attr,
  108. const char *buf, size_t count)
  109. {
  110. struct ptp_clock *ptp = dev_get_drvdata(dev);
  111. struct ptp_clock_info *ops = ptp->info;
  112. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
  113. int cnt, enable, err = -EINVAL;
  114. cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
  115. &req.perout.start.sec, &req.perout.start.nsec,
  116. &req.perout.period.sec, &req.perout.period.nsec);
  117. if (cnt != 5)
  118. goto out;
  119. if (req.perout.index >= ops->n_per_out)
  120. goto out;
  121. enable = req.perout.period.sec || req.perout.period.nsec;
  122. err = ops->enable(ops, &req, enable);
  123. if (err)
  124. goto out;
  125. return count;
  126. out:
  127. return err;
  128. }
  129. static ssize_t pps_enable_store(struct device *dev,
  130. struct device_attribute *attr,
  131. const char *buf, size_t count)
  132. {
  133. struct ptp_clock *ptp = dev_get_drvdata(dev);
  134. struct ptp_clock_info *ops = ptp->info;
  135. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
  136. int cnt, enable;
  137. int err = -EINVAL;
  138. if (!capable(CAP_SYS_TIME))
  139. return -EPERM;
  140. cnt = sscanf(buf, "%d", &enable);
  141. if (cnt != 1)
  142. goto out;
  143. err = ops->enable(ops, &req, enable ? 1 : 0);
  144. if (err)
  145. goto out;
  146. return count;
  147. out:
  148. return err;
  149. }
  150. static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
  151. static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
  152. static DEVICE_ATTR(period, 0220, NULL, period_store);
  153. static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
  154. int ptp_cleanup_sysfs(struct ptp_clock *ptp)
  155. {
  156. struct device *dev = ptp->dev;
  157. struct ptp_clock_info *info = ptp->info;
  158. if (info->n_ext_ts) {
  159. device_remove_file(dev, &dev_attr_extts_enable);
  160. device_remove_file(dev, &dev_attr_fifo);
  161. }
  162. if (info->n_per_out)
  163. device_remove_file(dev, &dev_attr_period);
  164. if (info->pps)
  165. device_remove_file(dev, &dev_attr_pps_enable);
  166. return 0;
  167. }
  168. int ptp_populate_sysfs(struct ptp_clock *ptp)
  169. {
  170. struct device *dev = ptp->dev;
  171. struct ptp_clock_info *info = ptp->info;
  172. int err;
  173. if (info->n_ext_ts) {
  174. err = device_create_file(dev, &dev_attr_extts_enable);
  175. if (err)
  176. goto out1;
  177. err = device_create_file(dev, &dev_attr_fifo);
  178. if (err)
  179. goto out2;
  180. }
  181. if (info->n_per_out) {
  182. err = device_create_file(dev, &dev_attr_period);
  183. if (err)
  184. goto out3;
  185. }
  186. if (info->pps) {
  187. err = device_create_file(dev, &dev_attr_pps_enable);
  188. if (err)
  189. goto out4;
  190. }
  191. return 0;
  192. out4:
  193. if (info->n_per_out)
  194. device_remove_file(dev, &dev_attr_period);
  195. out3:
  196. if (info->n_ext_ts)
  197. device_remove_file(dev, &dev_attr_fifo);
  198. out2:
  199. if (info->n_ext_ts)
  200. device_remove_file(dev, &dev_attr_extts_enable);
  201. out1:
  202. return err;
  203. }