ptp_sysfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <linux/slab.h>
  22. #include "ptp_private.h"
  23. static ssize_t clock_name_show(struct device *dev,
  24. struct device_attribute *attr, char *page)
  25. {
  26. struct ptp_clock *ptp = dev_get_drvdata(dev);
  27. return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
  28. }
  29. static DEVICE_ATTR_RO(clock_name);
  30. #define PTP_SHOW_INT(name, var) \
  31. static ssize_t var##_show(struct device *dev, \
  32. struct device_attribute *attr, char *page) \
  33. { \
  34. struct ptp_clock *ptp = dev_get_drvdata(dev); \
  35. return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
  36. } \
  37. static DEVICE_ATTR(name, 0444, var##_show, NULL);
  38. PTP_SHOW_INT(max_adjustment, max_adj);
  39. PTP_SHOW_INT(n_alarms, n_alarm);
  40. PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
  41. PTP_SHOW_INT(n_periodic_outputs, n_per_out);
  42. PTP_SHOW_INT(n_programmable_pins, n_pins);
  43. PTP_SHOW_INT(pps_available, pps);
  44. static ssize_t extts_enable_store(struct device *dev,
  45. struct device_attribute *attr,
  46. const char *buf, size_t count)
  47. {
  48. struct ptp_clock *ptp = dev_get_drvdata(dev);
  49. struct ptp_clock_info *ops = ptp->info;
  50. struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
  51. int cnt, enable;
  52. int err = -EINVAL;
  53. cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
  54. if (cnt != 2)
  55. goto out;
  56. if (req.extts.index >= ops->n_ext_ts)
  57. goto out;
  58. err = ops->enable(ops, &req, enable ? 1 : 0);
  59. if (err)
  60. goto out;
  61. return count;
  62. out:
  63. return err;
  64. }
  65. static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
  66. static ssize_t extts_fifo_show(struct device *dev,
  67. struct device_attribute *attr, char *page)
  68. {
  69. struct ptp_clock *ptp = dev_get_drvdata(dev);
  70. struct timestamp_event_queue *queue = &ptp->tsevq;
  71. struct ptp_extts_event event;
  72. unsigned long flags;
  73. size_t qcnt;
  74. int cnt = 0;
  75. memset(&event, 0, sizeof(event));
  76. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  77. return -ERESTARTSYS;
  78. spin_lock_irqsave(&queue->lock, flags);
  79. qcnt = queue_cnt(queue);
  80. if (qcnt) {
  81. event = queue->buf[queue->head];
  82. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  83. }
  84. spin_unlock_irqrestore(&queue->lock, flags);
  85. if (!qcnt)
  86. goto out;
  87. cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
  88. event.index, event.t.sec, event.t.nsec);
  89. out:
  90. mutex_unlock(&ptp->tsevq_mux);
  91. return cnt;
  92. }
  93. static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
  94. static ssize_t period_store(struct device *dev,
  95. struct device_attribute *attr,
  96. const char *buf, size_t count)
  97. {
  98. struct ptp_clock *ptp = dev_get_drvdata(dev);
  99. struct ptp_clock_info *ops = ptp->info;
  100. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
  101. int cnt, enable, err = -EINVAL;
  102. cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
  103. &req.perout.start.sec, &req.perout.start.nsec,
  104. &req.perout.period.sec, &req.perout.period.nsec);
  105. if (cnt != 5)
  106. goto out;
  107. if (req.perout.index >= ops->n_per_out)
  108. goto out;
  109. enable = req.perout.period.sec || req.perout.period.nsec;
  110. err = ops->enable(ops, &req, enable);
  111. if (err)
  112. goto out;
  113. return count;
  114. out:
  115. return err;
  116. }
  117. static DEVICE_ATTR(period, 0220, NULL, period_store);
  118. static ssize_t pps_enable_store(struct device *dev,
  119. struct device_attribute *attr,
  120. const char *buf, size_t count)
  121. {
  122. struct ptp_clock *ptp = dev_get_drvdata(dev);
  123. struct ptp_clock_info *ops = ptp->info;
  124. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
  125. int cnt, enable;
  126. int err = -EINVAL;
  127. if (!capable(CAP_SYS_TIME))
  128. return -EPERM;
  129. cnt = sscanf(buf, "%d", &enable);
  130. if (cnt != 1)
  131. goto out;
  132. err = ops->enable(ops, &req, enable ? 1 : 0);
  133. if (err)
  134. goto out;
  135. return count;
  136. out:
  137. return err;
  138. }
  139. static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
  140. static struct attribute *ptp_attrs[] = {
  141. &dev_attr_clock_name.attr,
  142. &dev_attr_max_adjustment.attr,
  143. &dev_attr_n_alarms.attr,
  144. &dev_attr_n_external_timestamps.attr,
  145. &dev_attr_n_periodic_outputs.attr,
  146. &dev_attr_n_programmable_pins.attr,
  147. &dev_attr_pps_available.attr,
  148. &dev_attr_extts_enable.attr,
  149. &dev_attr_fifo.attr,
  150. &dev_attr_period.attr,
  151. &dev_attr_pps_enable.attr,
  152. NULL
  153. };
  154. static umode_t ptp_is_attribute_visible(struct kobject *kobj,
  155. struct attribute *attr, int n)
  156. {
  157. struct device *dev = kobj_to_dev(kobj);
  158. struct ptp_clock *ptp = dev_get_drvdata(dev);
  159. struct ptp_clock_info *info = ptp->info;
  160. umode_t mode = attr->mode;
  161. if (attr == &dev_attr_extts_enable.attr ||
  162. attr == &dev_attr_fifo.attr) {
  163. if (!info->n_ext_ts)
  164. mode = 0;
  165. } else if (attr == &dev_attr_period.attr) {
  166. if (!info->n_per_out)
  167. mode = 0;
  168. } else if (attr == &dev_attr_pps_enable.attr) {
  169. if (!info->pps)
  170. mode = 0;
  171. }
  172. return mode;
  173. }
  174. static const struct attribute_group ptp_group = {
  175. .is_visible = ptp_is_attribute_visible,
  176. .attrs = ptp_attrs,
  177. };
  178. const struct attribute_group *ptp_groups[] = {
  179. &ptp_group,
  180. NULL
  181. };
  182. static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
  183. {
  184. int i;
  185. for (i = 0; i < ptp->info->n_pins; i++) {
  186. if (!strcmp(ptp->info->pin_config[i].name, name))
  187. return i;
  188. }
  189. return -1;
  190. }
  191. static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
  192. char *page)
  193. {
  194. struct ptp_clock *ptp = dev_get_drvdata(dev);
  195. unsigned int func, chan;
  196. int index;
  197. index = ptp_pin_name2index(ptp, attr->attr.name);
  198. if (index < 0)
  199. return -EINVAL;
  200. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  201. return -ERESTARTSYS;
  202. func = ptp->info->pin_config[index].func;
  203. chan = ptp->info->pin_config[index].chan;
  204. mutex_unlock(&ptp->pincfg_mux);
  205. return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan);
  206. }
  207. static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. struct ptp_clock *ptp = dev_get_drvdata(dev);
  211. unsigned int func, chan;
  212. int cnt, err, index;
  213. cnt = sscanf(buf, "%u %u", &func, &chan);
  214. if (cnt != 2)
  215. return -EINVAL;
  216. index = ptp_pin_name2index(ptp, attr->attr.name);
  217. if (index < 0)
  218. return -EINVAL;
  219. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  220. return -ERESTARTSYS;
  221. err = ptp_set_pinfunc(ptp, index, func, chan);
  222. mutex_unlock(&ptp->pincfg_mux);
  223. if (err)
  224. return err;
  225. return count;
  226. }
  227. int ptp_populate_pin_groups(struct ptp_clock *ptp)
  228. {
  229. struct ptp_clock_info *info = ptp->info;
  230. int err = -ENOMEM, i, n_pins = info->n_pins;
  231. if (!n_pins)
  232. return 0;
  233. ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
  234. GFP_KERNEL);
  235. if (!ptp->pin_dev_attr)
  236. goto no_dev_attr;
  237. ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
  238. if (!ptp->pin_attr)
  239. goto no_pin_attr;
  240. for (i = 0; i < n_pins; i++) {
  241. struct device_attribute *da = &ptp->pin_dev_attr[i];
  242. sysfs_attr_init(&da->attr);
  243. da->attr.name = info->pin_config[i].name;
  244. da->attr.mode = 0644;
  245. da->show = ptp_pin_show;
  246. da->store = ptp_pin_store;
  247. ptp->pin_attr[i] = &da->attr;
  248. }
  249. ptp->pin_attr_group.name = "pins";
  250. ptp->pin_attr_group.attrs = ptp->pin_attr;
  251. ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
  252. return 0;
  253. no_pin_attr:
  254. kfree(ptp->pin_dev_attr);
  255. no_dev_attr:
  256. return err;
  257. }
  258. void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
  259. {
  260. kfree(ptp->pin_attr);
  261. kfree(ptp->pin_dev_attr);
  262. }