ptp_chardev.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <linux/nospec.h>
  26. #include "ptp_private.h"
  27. static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
  28. enum ptp_pin_function func, unsigned int chan)
  29. {
  30. struct ptp_clock_request rq;
  31. int err = 0;
  32. memset(&rq, 0, sizeof(rq));
  33. switch (func) {
  34. case PTP_PF_NONE:
  35. break;
  36. case PTP_PF_EXTTS:
  37. rq.type = PTP_CLK_REQ_EXTTS;
  38. rq.extts.index = chan;
  39. err = ops->enable(ops, &rq, 0);
  40. break;
  41. case PTP_PF_PEROUT:
  42. rq.type = PTP_CLK_REQ_PEROUT;
  43. rq.perout.index = chan;
  44. err = ops->enable(ops, &rq, 0);
  45. break;
  46. case PTP_PF_PHYSYNC:
  47. break;
  48. default:
  49. return -EINVAL;
  50. }
  51. return err;
  52. }
  53. int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
  54. enum ptp_pin_function func, unsigned int chan)
  55. {
  56. struct ptp_clock_info *info = ptp->info;
  57. struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
  58. unsigned int i;
  59. /* Check to see if any other pin previously had this function. */
  60. for (i = 0; i < info->n_pins; i++) {
  61. if (info->pin_config[i].func == func &&
  62. info->pin_config[i].chan == chan) {
  63. pin1 = &info->pin_config[i];
  64. break;
  65. }
  66. }
  67. if (pin1 && i == pin)
  68. return 0;
  69. /* Check the desired function and channel. */
  70. switch (func) {
  71. case PTP_PF_NONE:
  72. break;
  73. case PTP_PF_EXTTS:
  74. if (chan >= info->n_ext_ts)
  75. return -EINVAL;
  76. break;
  77. case PTP_PF_PEROUT:
  78. if (chan >= info->n_per_out)
  79. return -EINVAL;
  80. break;
  81. case PTP_PF_PHYSYNC:
  82. if (chan != 0)
  83. return -EINVAL;
  84. break;
  85. default:
  86. return -EINVAL;
  87. }
  88. if (info->verify(info, pin, func, chan)) {
  89. pr_err("driver cannot use function %u on pin %u\n", func, chan);
  90. return -EOPNOTSUPP;
  91. }
  92. /* Disable whatever function was previously assigned. */
  93. if (pin1) {
  94. ptp_disable_pinfunc(info, func, chan);
  95. pin1->func = PTP_PF_NONE;
  96. pin1->chan = 0;
  97. }
  98. ptp_disable_pinfunc(info, pin2->func, pin2->chan);
  99. pin2->func = func;
  100. pin2->chan = chan;
  101. return 0;
  102. }
  103. int ptp_open(struct posix_clock *pc, fmode_t fmode)
  104. {
  105. return 0;
  106. }
  107. long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
  108. {
  109. struct ptp_clock_caps caps;
  110. struct ptp_clock_request req;
  111. struct ptp_sys_offset *sysoff = NULL;
  112. struct ptp_pin_desc pd;
  113. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  114. struct ptp_clock_info *ops = ptp->info;
  115. struct ptp_clock_time *pct;
  116. struct timespec64 ts;
  117. int enable, err = 0;
  118. unsigned int i, pin_index;
  119. switch (cmd) {
  120. case PTP_CLOCK_GETCAPS:
  121. memset(&caps, 0, sizeof(caps));
  122. caps.max_adj = ptp->info->max_adj;
  123. caps.n_alarm = ptp->info->n_alarm;
  124. caps.n_ext_ts = ptp->info->n_ext_ts;
  125. caps.n_per_out = ptp->info->n_per_out;
  126. caps.pps = ptp->info->pps;
  127. caps.n_pins = ptp->info->n_pins;
  128. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  129. err = -EFAULT;
  130. break;
  131. case PTP_EXTTS_REQUEST:
  132. if (copy_from_user(&req.extts, (void __user *)arg,
  133. sizeof(req.extts))) {
  134. err = -EFAULT;
  135. break;
  136. }
  137. if (req.extts.index >= ops->n_ext_ts) {
  138. err = -EINVAL;
  139. break;
  140. }
  141. req.type = PTP_CLK_REQ_EXTTS;
  142. enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
  143. err = ops->enable(ops, &req, enable);
  144. break;
  145. case PTP_PEROUT_REQUEST:
  146. if (copy_from_user(&req.perout, (void __user *)arg,
  147. sizeof(req.perout))) {
  148. err = -EFAULT;
  149. break;
  150. }
  151. if (req.perout.index >= ops->n_per_out) {
  152. err = -EINVAL;
  153. break;
  154. }
  155. req.type = PTP_CLK_REQ_PEROUT;
  156. enable = req.perout.period.sec || req.perout.period.nsec;
  157. err = ops->enable(ops, &req, enable);
  158. break;
  159. case PTP_ENABLE_PPS:
  160. if (!capable(CAP_SYS_TIME))
  161. return -EPERM;
  162. req.type = PTP_CLK_REQ_PPS;
  163. enable = arg ? 1 : 0;
  164. err = ops->enable(ops, &req, enable);
  165. break;
  166. case PTP_SYS_OFFSET:
  167. sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL);
  168. if (!sysoff) {
  169. err = -ENOMEM;
  170. break;
  171. }
  172. if (copy_from_user(sysoff, (void __user *)arg,
  173. sizeof(*sysoff))) {
  174. err = -EFAULT;
  175. break;
  176. }
  177. if (sysoff->n_samples > PTP_MAX_SAMPLES) {
  178. err = -EINVAL;
  179. break;
  180. }
  181. pct = &sysoff->ts[0];
  182. for (i = 0; i < sysoff->n_samples; i++) {
  183. getnstimeofday64(&ts);
  184. pct->sec = ts.tv_sec;
  185. pct->nsec = ts.tv_nsec;
  186. pct++;
  187. err = ptp->info->gettime64(ptp->info, &ts);
  188. if (err)
  189. goto out;
  190. pct->sec = ts.tv_sec;
  191. pct->nsec = ts.tv_nsec;
  192. pct++;
  193. }
  194. getnstimeofday64(&ts);
  195. pct->sec = ts.tv_sec;
  196. pct->nsec = ts.tv_nsec;
  197. if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
  198. err = -EFAULT;
  199. break;
  200. case PTP_PIN_GETFUNC:
  201. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  202. err = -EFAULT;
  203. break;
  204. }
  205. pin_index = pd.index;
  206. if (pin_index >= ops->n_pins) {
  207. err = -EINVAL;
  208. break;
  209. }
  210. pin_index = array_index_nospec(pin_index, ops->n_pins);
  211. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  212. return -ERESTARTSYS;
  213. pd = ops->pin_config[pin_index];
  214. mutex_unlock(&ptp->pincfg_mux);
  215. if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
  216. err = -EFAULT;
  217. break;
  218. case PTP_PIN_SETFUNC:
  219. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  220. err = -EFAULT;
  221. break;
  222. }
  223. pin_index = pd.index;
  224. if (pin_index >= ops->n_pins) {
  225. err = -EINVAL;
  226. break;
  227. }
  228. pin_index = array_index_nospec(pin_index, ops->n_pins);
  229. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  230. return -ERESTARTSYS;
  231. err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
  232. mutex_unlock(&ptp->pincfg_mux);
  233. break;
  234. default:
  235. err = -ENOTTY;
  236. break;
  237. }
  238. out:
  239. kfree(sysoff);
  240. return err;
  241. }
  242. unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
  243. {
  244. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  245. poll_wait(fp, &ptp->tsev_wq, wait);
  246. return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
  247. }
  248. #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
  249. ssize_t ptp_read(struct posix_clock *pc,
  250. uint rdflags, char __user *buf, size_t cnt)
  251. {
  252. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  253. struct timestamp_event_queue *queue = &ptp->tsevq;
  254. struct ptp_extts_event *event;
  255. unsigned long flags;
  256. size_t qcnt, i;
  257. int result;
  258. if (cnt % sizeof(struct ptp_extts_event) != 0)
  259. return -EINVAL;
  260. if (cnt > EXTTS_BUFSIZE)
  261. cnt = EXTTS_BUFSIZE;
  262. cnt = cnt / sizeof(struct ptp_extts_event);
  263. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  264. return -ERESTARTSYS;
  265. if (wait_event_interruptible(ptp->tsev_wq,
  266. ptp->defunct || queue_cnt(queue))) {
  267. mutex_unlock(&ptp->tsevq_mux);
  268. return -ERESTARTSYS;
  269. }
  270. if (ptp->defunct) {
  271. mutex_unlock(&ptp->tsevq_mux);
  272. return -ENODEV;
  273. }
  274. event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
  275. if (!event) {
  276. mutex_unlock(&ptp->tsevq_mux);
  277. return -ENOMEM;
  278. }
  279. spin_lock_irqsave(&queue->lock, flags);
  280. qcnt = queue_cnt(queue);
  281. if (cnt > qcnt)
  282. cnt = qcnt;
  283. for (i = 0; i < cnt; i++) {
  284. event[i] = queue->buf[queue->head];
  285. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  286. }
  287. spin_unlock_irqrestore(&queue->lock, flags);
  288. cnt = cnt * sizeof(struct ptp_extts_event);
  289. mutex_unlock(&ptp->tsevq_mux);
  290. result = cnt;
  291. if (copy_to_user(buf, event, cnt))
  292. result = -EFAULT;
  293. kfree(event);
  294. return result;
  295. }