ptp_sysfs.c 5.8 KB

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