ptp_sysfs.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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(clock_name, 0444, clock_name_show, NULL);
  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 struct attribute *ptp_attrs[] = {
  45. &dev_attr_clock_name.attr,
  46. &dev_attr_max_adjustment.attr,
  47. &dev_attr_n_alarms.attr,
  48. &dev_attr_n_external_timestamps.attr,
  49. &dev_attr_n_periodic_outputs.attr,
  50. &dev_attr_n_programmable_pins.attr,
  51. &dev_attr_pps_available.attr,
  52. NULL,
  53. };
  54. static const struct attribute_group ptp_group = {
  55. .attrs = ptp_attrs,
  56. };
  57. const struct attribute_group *ptp_groups[] = {
  58. &ptp_group,
  59. NULL,
  60. };
  61. static ssize_t extts_enable_store(struct device *dev,
  62. struct device_attribute *attr,
  63. const char *buf, size_t count)
  64. {
  65. struct ptp_clock *ptp = dev_get_drvdata(dev);
  66. struct ptp_clock_info *ops = ptp->info;
  67. struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
  68. int cnt, enable;
  69. int err = -EINVAL;
  70. cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
  71. if (cnt != 2)
  72. goto out;
  73. if (req.extts.index >= ops->n_ext_ts)
  74. goto out;
  75. err = ops->enable(ops, &req, enable ? 1 : 0);
  76. if (err)
  77. goto out;
  78. return count;
  79. out:
  80. return err;
  81. }
  82. static ssize_t extts_fifo_show(struct device *dev,
  83. struct device_attribute *attr, char *page)
  84. {
  85. struct ptp_clock *ptp = dev_get_drvdata(dev);
  86. struct timestamp_event_queue *queue = &ptp->tsevq;
  87. struct ptp_extts_event event;
  88. unsigned long flags;
  89. size_t qcnt;
  90. int cnt = 0;
  91. memset(&event, 0, sizeof(event));
  92. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  93. return -ERESTARTSYS;
  94. spin_lock_irqsave(&queue->lock, flags);
  95. qcnt = queue_cnt(queue);
  96. if (qcnt) {
  97. event = queue->buf[queue->head];
  98. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  99. }
  100. spin_unlock_irqrestore(&queue->lock, flags);
  101. if (!qcnt)
  102. goto out;
  103. cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
  104. event.index, event.t.sec, event.t.nsec);
  105. out:
  106. mutex_unlock(&ptp->tsevq_mux);
  107. return cnt;
  108. }
  109. static ssize_t period_store(struct device *dev,
  110. struct device_attribute *attr,
  111. const char *buf, size_t count)
  112. {
  113. struct ptp_clock *ptp = dev_get_drvdata(dev);
  114. struct ptp_clock_info *ops = ptp->info;
  115. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
  116. int cnt, enable, err = -EINVAL;
  117. cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
  118. &req.perout.start.sec, &req.perout.start.nsec,
  119. &req.perout.period.sec, &req.perout.period.nsec);
  120. if (cnt != 5)
  121. goto out;
  122. if (req.perout.index >= ops->n_per_out)
  123. goto out;
  124. enable = req.perout.period.sec || req.perout.period.nsec;
  125. err = ops->enable(ops, &req, enable);
  126. if (err)
  127. goto out;
  128. return count;
  129. out:
  130. return err;
  131. }
  132. static ssize_t pps_enable_store(struct device *dev,
  133. struct device_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. struct ptp_clock *ptp = dev_get_drvdata(dev);
  137. struct ptp_clock_info *ops = ptp->info;
  138. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
  139. int cnt, enable;
  140. int err = -EINVAL;
  141. if (!capable(CAP_SYS_TIME))
  142. return -EPERM;
  143. cnt = sscanf(buf, "%d", &enable);
  144. if (cnt != 1)
  145. goto out;
  146. err = ops->enable(ops, &req, enable ? 1 : 0);
  147. if (err)
  148. goto out;
  149. return count;
  150. out:
  151. return err;
  152. }
  153. static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
  154. {
  155. int i;
  156. for (i = 0; i < ptp->info->n_pins; i++) {
  157. if (!strcmp(ptp->info->pin_config[i].name, name))
  158. return i;
  159. }
  160. return -1;
  161. }
  162. static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
  163. char *page)
  164. {
  165. struct ptp_clock *ptp = dev_get_drvdata(dev);
  166. unsigned int func, chan;
  167. int index;
  168. index = ptp_pin_name2index(ptp, attr->attr.name);
  169. if (index < 0)
  170. return -EINVAL;
  171. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  172. return -ERESTARTSYS;
  173. func = ptp->info->pin_config[index].func;
  174. chan = ptp->info->pin_config[index].chan;
  175. mutex_unlock(&ptp->pincfg_mux);
  176. return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan);
  177. }
  178. static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
  179. const char *buf, size_t count)
  180. {
  181. struct ptp_clock *ptp = dev_get_drvdata(dev);
  182. unsigned int func, chan;
  183. int cnt, err, index;
  184. cnt = sscanf(buf, "%u %u", &func, &chan);
  185. if (cnt != 2)
  186. return -EINVAL;
  187. index = ptp_pin_name2index(ptp, attr->attr.name);
  188. if (index < 0)
  189. return -EINVAL;
  190. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  191. return -ERESTARTSYS;
  192. err = ptp_set_pinfunc(ptp, index, func, chan);
  193. mutex_unlock(&ptp->pincfg_mux);
  194. if (err)
  195. return err;
  196. return count;
  197. }
  198. static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
  199. static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
  200. static DEVICE_ATTR(period, 0220, NULL, period_store);
  201. static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
  202. int ptp_cleanup_sysfs(struct ptp_clock *ptp)
  203. {
  204. struct device *dev = ptp->dev;
  205. struct ptp_clock_info *info = ptp->info;
  206. if (info->n_ext_ts) {
  207. device_remove_file(dev, &dev_attr_extts_enable);
  208. device_remove_file(dev, &dev_attr_fifo);
  209. }
  210. if (info->n_per_out)
  211. device_remove_file(dev, &dev_attr_period);
  212. if (info->pps)
  213. device_remove_file(dev, &dev_attr_pps_enable);
  214. if (info->n_pins) {
  215. sysfs_remove_group(&dev->kobj, &ptp->pin_attr_group);
  216. kfree(ptp->pin_attr);
  217. kfree(ptp->pin_dev_attr);
  218. }
  219. return 0;
  220. }
  221. static int ptp_populate_pins(struct ptp_clock *ptp)
  222. {
  223. struct device *dev = ptp->dev;
  224. struct ptp_clock_info *info = ptp->info;
  225. int err = -ENOMEM, i, n_pins = info->n_pins;
  226. ptp->pin_dev_attr = kzalloc(n_pins * sizeof(*ptp->pin_dev_attr),
  227. GFP_KERNEL);
  228. if (!ptp->pin_dev_attr)
  229. goto no_dev_attr;
  230. ptp->pin_attr = kzalloc((1 + n_pins) * sizeof(struct attribute *),
  231. GFP_KERNEL);
  232. if (!ptp->pin_attr)
  233. goto no_pin_attr;
  234. for (i = 0; i < n_pins; i++) {
  235. struct device_attribute *da = &ptp->pin_dev_attr[i];
  236. sysfs_attr_init(&da->attr);
  237. da->attr.name = info->pin_config[i].name;
  238. da->attr.mode = 0644;
  239. da->show = ptp_pin_show;
  240. da->store = ptp_pin_store;
  241. ptp->pin_attr[i] = &da->attr;
  242. }
  243. ptp->pin_attr_group.name = "pins";
  244. ptp->pin_attr_group.attrs = ptp->pin_attr;
  245. err = sysfs_create_group(&dev->kobj, &ptp->pin_attr_group);
  246. if (err)
  247. goto no_group;
  248. return 0;
  249. no_group:
  250. kfree(ptp->pin_attr);
  251. no_pin_attr:
  252. kfree(ptp->pin_dev_attr);
  253. no_dev_attr:
  254. return err;
  255. }
  256. int ptp_populate_sysfs(struct ptp_clock *ptp)
  257. {
  258. struct device *dev = ptp->dev;
  259. struct ptp_clock_info *info = ptp->info;
  260. int err;
  261. if (info->n_ext_ts) {
  262. err = device_create_file(dev, &dev_attr_extts_enable);
  263. if (err)
  264. goto out1;
  265. err = device_create_file(dev, &dev_attr_fifo);
  266. if (err)
  267. goto out2;
  268. }
  269. if (info->n_per_out) {
  270. err = device_create_file(dev, &dev_attr_period);
  271. if (err)
  272. goto out3;
  273. }
  274. if (info->pps) {
  275. err = device_create_file(dev, &dev_attr_pps_enable);
  276. if (err)
  277. goto out4;
  278. }
  279. if (info->n_pins) {
  280. err = ptp_populate_pins(ptp);
  281. if (err)
  282. goto out5;
  283. }
  284. return 0;
  285. out5:
  286. if (info->pps)
  287. device_remove_file(dev, &dev_attr_pps_enable);
  288. out4:
  289. if (info->n_per_out)
  290. device_remove_file(dev, &dev_attr_period);
  291. out3:
  292. if (info->n_ext_ts)
  293. device_remove_file(dev, &dev_attr_fifo);
  294. out2:
  295. if (info->n_ext_ts)
  296. device_remove_file(dev, &dev_attr_extts_enable);
  297. out1:
  298. return err;
  299. }